FTL的整个流程如下:
下面先来看写的流程:
写的代码如下:
if((hostCmd.reqInfo.Cmd == IDE_COMMAND_WRITE_DMA) || (hostCmd.reqInfo.Cmd == IDE_COMMAND_WRITE))
{
// xil_printf("write(%d, %d)\r\n", hostCmd.reqInfo.CurSect, hostCmd.reqInfo.ReqSect); PrePmRead(&hostCmd, RAM_DISK_BASE_ADDR); deviceAddr = RAM_DISK_BASE_ADDR + (hostCmd.reqInfo.CurSect % SECTOR_NUM_PER_PAGE)*SECTOR_SIZE;
reqSize = hostCmd.reqInfo.ReqSect * SECTOR_SIZE;
scatterLength = hostCmd.reqInfo.HostScatterNum; DmaHostToDevice(&hostCmd, deviceAddr, reqSize, scatterLength); PmWrite(&hostCmd, RAM_DISK_BASE_ADDR); CompleteCmd(&hostCmd);
}
首先来看PrePmRead,其中最开始会涉及一个FlushPageBuf函数,FlushPageBuf里面有个FindFreePage函数,所以我们先分析FindFreePage函数的功能
lpn = hostCmd->reqInfo.CurSect / SECTOR_NUM_PER_PAGE;
u32 dieNo = lpn % DIE_NUM;
这里传入一个dieNo参数
int FindFreePage(u32 dieNo)
{
blockMap = (struct bmArray*)(BLOCK_MAP_ADDR);
dieBlock = (struct dieArray*)(DIE_MAP_ADDR); if(blockMap->bmEntry[dieNo][dieBlock->dieEntry[dieNo].currentBlock].currentPage == PAGE_NUM_PER_BLOCK-) //当前块已经写完最后一页,则用下一个block
{
dieBlock->dieEntry[dieNo].currentBlock++; int i;
for(i=dieBlock->dieEntry[dieNo].currentBlock ; i<(dieBlock->dieEntry[dieNo].currentBlock + BLOCK_NUM_PER_DIE) ; i++) /*遍历整个die的所有block,到结尾之后又从开始找,直到找到一个可用的block*/
{
if((blockMap->bmEntry[dieNo][i % BLOCK_NUM_PER_DIE].free) && (!blockMap->bmEntry[dieNo][i % BLOCK_NUM_PER_DIE].bad)) //块free且不是坏块就可用
{
blockMap->bmEntry[dieNo][i % BLOCK_NUM_PER_DIE].free = ;
dieBlock->dieEntry[dieNo].currentBlock = i % BLOCK_NUM_PER_DIE; // xil_printf("allocated free block: %4d at %d-%d\r\n", dieBlock->dieEntry[dieNo].currentBlock, dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM); return dieBlock->dieEntry[dieNo].currentBlock * PAGE_NUM_PER_BLOCK; //返回页号
}
} dieBlock->dieEntry[dieNo].currentBlock = GarbageCollection(dieNo); //整个die没有可用块之后就进行垃圾回收 // xil_printf("allocated free block by GC: %4d at %d-%d\r\n", dieBlock->dieEntry[dieNo].currentBlock, dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM); return (dieBlock->dieEntry[dieNo].currentBlock * PAGE_NUM_PER_BLOCK) + blockMap->bmEntry[dieNo][dieBlock->dieEntry[dieNo].currentBlock].currentPage;
}
else //当前块还有页可用就直接接着上一页继续写
{
blockMap->bmEntry[dieNo][dieBlock->dieEntry[dieNo].currentBlock].currentPage++;
return (dieBlock->dieEntry[dieNo].currentBlock * PAGE_NUM_PER_BLOCK) + blockMap->bmEntry[dieNo][dieBlock->dieEntry[dieNo].currentBlock].currentPage;
}
}
由此可见,FindFreePage这个函数其实就是找一个可用的页,没有空间了就进行垃圾回收操作
接下来看上一级的函数FlushPageBuf
void FlushPageBuf(u32 lpn, u32 bufAddr)
{
if (lpn == 0xffffffff) //最开始page缓存内是没有东西的,所以无需flush
return; u32 dieNo = lpn % DIE_NUM; //计算出die number
u32 dieLpn = lpn / DIE_NUM; //计算出lpn在die中是第几个lpn,可以理解为die0上是lpn0,lpn16……对应为dieLpn0,dieLpn1
u32 ppn = pageMap->pmEntry[dieNo][dieLpn].ppn; if (ppn == 0xffffffff) //表示page缓存还没有写入ppn
{
u32 freePageNo = FindFreePage(dieNo); // xil_printf("free page: %6d(%d, %d, %4d)\r\n", freePageNo, dieNo%CHANNEL_NUM, dieNo/CHANNEL_NUM, freePageNo/PAGE_NUM_PER_BLOCK); WaitWayFree(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM);
SsdProgram(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM, freePageNo, bufAddr);
WaitWayFree(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM); // pageMap update
pageMap->pmEntry[dieNo][dieLpn].ppn = freePageNo;
pageMap->pmEntry[dieNo][freePageNo].lpn = dieLpn;
}
}
继续来看再上一级PrePmRead函数
int PrePmRead(P_HOST_CMD hostCmd, u32 bufferAddr)
{
u32 lpn;
u32 dieNo;
u32 dieLpn; pageMap = (struct pmArray*)(PAGE_MAP_ADDR);
lpn = hostCmd->reqInfo.CurSect / SECTOR_NUM_PER_PAGE; if (lpn != pageBufLpn) //新的请求和上个请求不是同一个lpn
{
FlushPageBuf(pageBufLpn, bufferAddr);
上面这一段进行了FlushPageBuf操作
if((((hostCmd->reqInfo.CurSect)%SECTOR_NUM_PER_PAGE) != )
|| ((hostCmd->reqInfo.CurSect / SECTOR_NUM_PER_PAGE) == (((hostCmd->reqInfo.CurSect)+(hostCmd->reqInfo.ReqSect))/SECTOR_NUM_PER_PAGE)))
{
dieNo = lpn % DIE_NUM;
dieLpn = lpn / DIE_NUM; if(pageMap->pmEntry[dieNo][dieLpn].ppn != 0xffffffff)
{
// xil_printf("PrePmRead pdie, ppn = %d, %d\r\n", dieNo, pageMap->pmEntry[dieNo][dieLpn].ppn); WaitWayFree(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM);
SsdRead(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM, pageMap->pmEntry[dieNo][dieLpn].ppn, bufferAddr);
WaitWayFree(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM); pageBufLpn = lpn;
}
}
}
疑问:这个判断条件第一个是请求开端不要是每个lpn的开始,第二个是请求的大小在同一页,那么这个条件就是只要满足不是请求跨页且从一个lpn的开始的就进入判断?有什么实际意义呢?
答:这里很关键,涉及一段数据,假如开始的地方没有和page对齐的话,那么在每个page里面请求开始前面的数据就得先读出来,如果是缓存命中的话,就无需操作,因为可以直接修改,那么没命中的话,即使是page对齐了,如果数据没有跨页的话,也还是要读出来,不然请求末尾内page的内容就丢失了。如下图
假如我要改写345678这些数据,因为数据是按页保存的,所以我只修改这些数据的话,我还得绿色部分的读取出来,然后修改后一起保存到一个页里面,所以请求的开始的lpn如果不是页对齐,我就得read-modify-write,同理,即使页对齐了,但是数据不足一页,那么一页后面几项数据也得先读出来。如果对齐且大小刚好等于一页的话,if失败,这个时候一页也是刚好可以直接修改。至于中间页的数据,本来就是一整页的,所以直接把原来的页无效,然后写入新页即可。这里这个判断条件是在页缓存没有命中的情况下,如果命中了,因为此页还没有刷新到nandflash,所以就无需取出而直接在SDRAM里面修改就行了
if(((((hostCmd->reqInfo.CurSect)+(hostCmd->reqInfo.ReqSect))% SECTOR_NUM_PER_PAGE) != )
&& ((hostCmd->reqInfo.CurSect / SECTOR_NUM_PER_PAGE) != (((hostCmd->reqInfo.CurSect)+(hostCmd->reqInfo.ReqSect))/SECTOR_NUM_PER_PAGE)))
{
lpn = ((hostCmd->reqInfo.CurSect)+(hostCmd->reqInfo.ReqSect))/SECTOR_NUM_PER_PAGE;
dieNo = lpn % DIE_NUM;
dieLpn = lpn / DIE_NUM; if(pageMap->pmEntry[dieNo][dieLpn].ppn != 0xffffffff)
{ // xil_printf("PrePmRead pdie, ppn = %d, %d\r\n", dieNo, pageMap->pmEntry[dieNo][dieLpn].ppn); WaitWayFree(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM);
SsdRead(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM, pageMap->pmEntry[dieNo][dieLpn].ppn,
bufferAddr + ((((hostCmd->reqInfo.CurSect)% SECTOR_NUM_PER_PAGE) + hostCmd->reqInfo.ReqSect)/SECTOR_NUM_PER_PAGE*PAGE_SIZE));
WaitWayFree(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM);
}
} return ;
}
上面是头,那这一部分就是尾,尾部如果不是请求对齐到page尾的话,那么也会有数据得不到更新,就如同上图lpn1的9到15,要把page内没修改的数据一起读出来更新,这里不仅是要不是结尾,而且是要跨页,这里分几种情况,假如缓存命中,没有跨页的话直接更新缓存就行了,只有跨页了,才需要进行read-modify-write操作;假如缓存没有命中,系统先把之前的缓存flush到nandflash里面,如果此时数据没有跨页的话,那么上面的操作就已经会读取那个页,也就无需下面再多此一举了,具体的写操作可以看下一篇文档。
接下来来看真正的写操作PmWrite
int PmWrite(P_HOST_CMD hostCmd, u32 bufferAddr)
{
u32 tempBuffer = bufferAddr; u32 lpn = hostCmd->reqInfo.CurSect / SECTOR_NUM_PER_PAGE; int loop = (hostCmd->reqInfo.CurSect % SECTOR_NUM_PER_PAGE) + hostCmd->reqInfo.ReqSect; u32 dieNo;
u32 dieLpn;
u32 freePageNo; pageMap = (struct pmArray*)(PAGE_MAP_ADDR); // page buffer utilization
if (lpn != pageBufLpn)
pageBufLpn = lpn; UpdateMetaForOverwrite(lpn); // pageMap update
dieNo = lpn % DIE_NUM;
dieLpn = lpn / DIE_NUM;
pageMap->pmEntry[dieNo][dieLpn].ppn = 0xffffffff; //写入一页不立即更新 lpn++;
tempBuffer += PAGE_SIZE;
loop -= SECTOR_NUM_PER_PAGE; while(loop > ) //接下来还有页请求的话,寻找新页,写入,更新页表
{
dieNo = lpn % DIE_NUM;
dieLpn = lpn / DIE_NUM;
freePageNo = FindFreePage(dieNo); // xil_printf("free page: %6d(%d, %d, %4d)\r\n", freePageNo, dieNo%CHANNEL_NUM, dieNo/CHANNEL_NUM, freePageNo/PAGE_NUM_PER_BLOCK); WaitWayFree(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM);
SsdProgram(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM, freePageNo, tempBuffer); UpdateMetaForOverwrite(lpn); // pageMap update
pageMap->pmEntry[dieNo][dieLpn].ppn = freePageNo;
pageMap->pmEntry[dieNo][freePageNo].lpn = dieLpn; lpn++;
tempBuffer += PAGE_SIZE;
loop -= SECTOR_NUM_PER_PAGE;
} int i;
for(i= ; i<DIE_NUM ; ++i)
WaitWayFree(i%CHANNEL_NUM, i/CHANNEL_NUM); return ;
}
接下来看读
int PmRead(P_HOST_CMD hostCmd, u32 bufferAddr)
{
u32 tempBuffer = bufferAddr; u32 lpn = hostCmd->reqInfo.CurSect / SECTOR_NUM_PER_PAGE;
int loop = (hostCmd->reqInfo.CurSect % SECTOR_NUM_PER_PAGE) + hostCmd->reqInfo.ReqSect; u32 dieNo;
u32 dieLpn; pageMap = (struct pmArray*)(PAGE_MAP_ADDR); if (lpn == pageBufLpn) //缓存命中,就无需读取第一页,直接就在内存里面
{
lpn++;
tempBuffer += PAGE_SIZE;
loop -= SECTOR_NUM_PER_PAGE;
}
else
{
dieNo = lpn % DIE_NUM;
dieLpn = lpn / DIE_NUM; if(pageMap->pmEntry[dieNo][dieLpn].ppn != 0xffffffff) //防止第一次读空页
{
FlushPageBuf(pageBufLpn, bufferAddr);
pageBufLpn = lpn;
}
} while(loop > ) //把接下来的页一次读取出来
{
dieNo = lpn % DIE_NUM;
dieLpn = lpn / DIE_NUM; // xil_printf("requested read lpn = %d\r\n", lpn);
// xil_printf("read pdie, ppn = %d, %d\r\n", dieNo, pageMap->pmEntry[dieNo][dieLpn].ppn); if(pageMap->pmEntry[dieNo][dieLpn].ppn != 0xffffffff)
{
// xil_printf("read at (%d, %2d, %4x)\r\n", dieNo%CHANNEL_NUM, dieNo/CHANNEL_NUM, pageMap->pmEntry[dieNo][dieLpn].ppn); WaitWayFree(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM);
SsdRead(dieNo % CHANNEL_NUM, dieNo / CHANNEL_NUM, pageMap->pmEntry[dieNo][dieLpn].ppn, tempBuffer);
} lpn++;
tempBuffer += PAGE_SIZE;
loop -= SECTOR_NUM_PER_PAGE;
} int i;
for(i= ; i<DIE_NUM ; ++i)
WaitWayFree(i%CHANNEL_NUM, i/CHANNEL_NUM); return ;
}