📄 ondisk.c
字号:
if ((nStartSecNo + nNumOfSec - 1) > pDisk->d_DiskInfo.di_total_sectors)
{
status = ERROR_SECTOR_NOT_FOUND;
goto ddi_exit;
}
status = ERROR_SUCCESS;
pSg = &(pSgr->sr_sglist[0]);
// Index of Sector Number
nSecNoIdx = 0;
nSecBufOffset = 0;
pSecBuf = NULL;
#if ONDISK_MULTIPARTITION
nStartSecNo += (pDisk->nWMRStartSector >> OND_SECTOR_SHIFT); // remapping sector
#endif
/* Number of SG is 1 (Most of cases) */
if (nNumOfSG == 1)
{
nNumOfScts = nSGBufLen / (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT);
//pSGBuf = MapPtrToProcess((LPVOID)pSg->sb_buf, GetCallerProcess());
pSGBuf = MapCallerPtr((LPVOID)pSg->sb_buf,nSGBufLen);
if (nNumOfScts != 0)
{
nRet = FTL_Read((nStartSecNo << OND_SECTOR_SHIFT), /* Start Logical Sector Number */
(nNumOfScts << OND_SECTOR_SHIFT), /* Number of Sectors */
pSGBuf); /* Pointer ot buffer */
if (nRet != FTL_SUCCESS)
{
STDRV_ERR_PRINT((TEXT("[OND:ERR](DoDiskRead) FTL Read Error=0x%x\r\n"), nRet));
status = ERROR_SECTOR_NOT_FOUND;
goto ddi_req_done;
}
else STDRV_INF_PRINT((TEXT("[OND:INF](DoDiskRead) FTL Read OK+\r\n")));
nStartSecNo += nNumOfScts;
}
/* To solve 512 byte align problem */
if ((nSGBufLen % (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT)) != 0)
{
UINT nCpResumeOffset;
UINT nCpLeftSize;
/* Memory Allocation for 1 sector read */
pSecBuf = (UCHAR*)malloc(WMR_SECTOR_SIZE << OND_SECTOR_SHIFT);
nCpResumeOffset = nNumOfScts * (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT);
nCpLeftSize = nSGBufLen % (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT);
nRet = FTL_Read((nStartSecNo << OND_SECTOR_SHIFT), /* Start Logical Sector Number */
(1 << OND_SECTOR_SHIFT), /* Number of Sectors */
pSecBuf); /* Pointer ot buffer */
if (nRet != FTL_SUCCESS)
{
STDRV_ERR_PRINT((TEXT("[OND:ERR](DoDiskRead) FTL Read Error=0x%x\r\n"), nRet));
free(pSecBuf);
status = ERROR_SECTOR_NOT_FOUND;
goto ddi_req_done;
}
else STDRV_INF_PRINT((TEXT("[OND:INF](DoDiskRead) FTL Read OK++\r\n")));
memcpy((UCHAR*)(pSGBuf + nCpResumeOffset), pSecBuf, nCpLeftSize);
free(pSecBuf);
}
}
/* Number of SG is greater than 1 */
else
{
nDstOffset = 0;
while (nSGBufLen)
{
if (nSGBufLen >= PSII_MAX_READ_BUF_SIZE)
{
nNumOfScts = PSII_MAX_READ_BUF_SIZE / (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT);
nCpBytes = (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT) * nNumOfScts;
}
else
{
nNumOfScts = nSGBufLen / (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT);
nCpBytes = (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT) * nNumOfScts;
if (nSGBufLen % (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT))
{
nNumOfScts++;
nCpBytes += (nSGBufLen % (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT));
}
}
pSecBuf = (UCHAR *)malloc((WMR_SECTOR_SIZE << OND_SECTOR_SHIFT) * nNumOfScts);
nRet = FTL_Read((nStartSecNo << OND_SECTOR_SHIFT), /* Start Logical Sector Number */
(nNumOfScts << OND_SECTOR_SHIFT), /* Number of Sectors */
pSecBuf); /* Pointer ot buffer */
if (nRet != FTL_SUCCESS)
{
STDRV_ERR_PRINT((TEXT("[OND:ERR](DoDiskRead) FTL Read Error=0x%x\r\n"), nRet));
status = ERROR_SECTOR_NOT_FOUND;
free(pSecBuf);
goto ddi_req_done;
}
else STDRV_INF_PRINT((TEXT("[OND:INF](DoDiskRead) FTL Read OK+++\r\n")));
nStartSecNo += nNumOfScts;
nSrcOffset = 0;
while(nCpBytes)
{
/* Get New Sg buffer */
if (nDstOffset == 0)
{
nSizeOfSGBuf = pSg->sb_len;
pSGBuf = MapPtrToProcess((LPVOID)pSg->sb_buf, GetCallerProcess());
}
if (nSizeOfSGBuf <= nCpBytes)
{
memcpy((UCHAR*)(pSGBuf + nDstOffset), (UCHAR*)(pSecBuf + nSrcOffset), nSizeOfSGBuf);
nSrcOffset += nSizeOfSGBuf;
nDstOffset = 0;
nCpBytes -= nSizeOfSGBuf;
pSg++;
}
else
{
memcpy((UCHAR*)(pSGBuf + nDstOffset), (UCHAR*)pSecBuf + nSrcOffset, nCpBytes);
nDstOffset += nCpBytes;
nSizeOfSGBuf -= nCpBytes;
nCpBytes = 0;
}
}
if (nSGBufLen < ((WMR_SECTOR_SIZE << OND_SECTOR_SHIFT) * nNumOfScts))
{
free(pSecBuf);
break;
}
else
{
nSGBufLen -= ((WMR_SECTOR_SIZE << OND_SECTOR_SHIFT) * nNumOfScts);
}
free(pSecBuf);
}
}
ddi_req_done:
ddi_exit:
pSgr->sr_status = status;
LeaveCriticalSection(&(pDisk->d_DiskCardCrit));
return status;
}
/*****************************************************************************/
/* */
/* NAME */
/* DoDiskWrite */
/* DESCRIPTION */
/* Do write operation into NAND flash memory */
/* PARAMETERS */
/* pDisk PocketStoreII driver own structure pointer */
/* pData PSQ_REQ structure pointer,it contains request information*/
/* for write operations */
/* RETURN VALUES */
/* If it successes, it returns TRUE. otherwize it returns FALSE */
/* */
/*****************************************************************************/
static DWORD
DoDiskWrite(PDISK pDisk,
PVOID pData)
{
DWORD status = ERROR_SUCCESS;
DWORD nSizeOfSGBuf;
PSG_REQ pSgr;
PSG_BUF pSg;
PUCHAR pSGBuf;
UINT nStartSecNo, nNumOfSec, nSecNoIdx;
UINT nNumOfSG, nSGBufLen, nSGBufNum;
UINT nSecBufOffset;
UINT nSizeOfSec;
UCHAR *pSecBuf;
INT32 nRet;
UINT32 nNumOfScts;
UINT32 nDstOffset;
UINT32 nSrcOffset;
UINT32 nCpBytes;
STDRV_LOG_PRINT((TEXT("[OND: IN] ++DoDiskWrite()\r\n")));
EnterCriticalSection(&(pDisk->d_DiskCardCrit));
pSgr = (PSG_REQ) pData;
nStartSecNo = pSgr->sr_start;
nNumOfSec = pSgr->sr_num_sec;
nNumOfSG = pSgr->sr_num_sg;
nSizeOfSec = pDisk->d_DiskInfo.di_bytes_per_sect;
if (pDisk->bIsFTLOpen == FALSE)
{
status = ERROR_INVALID_PARAMETER;
goto ddi_exit;
}
if (nNumOfSG > MAX_SG_BUF)
{
status = ERROR_INVALID_PARAMETER;
goto ddi_exit;
}
nSGBufLen = 0;
// calculate total buffer space of scatter gather buffers
for (nSGBufNum = 0; nSGBufNum < nNumOfSG; nSGBufNum ++)
{
nSGBufLen += pSgr->sr_sglist[nSGBufNum].sb_len;
}
// check total SG buffer space is enough for reqeusted size
if (nSGBufLen < (nNumOfSec * nSizeOfSec))
{
STDRV_ERR_PRINT((TEXT("DoDiskRead : SG Buffer space %d bytes less than block read size %d bytes\r\n"),
nSGBufLen, nNumOfSec * nSizeOfSec));
status = ERROR_GEN_FAILURE;
goto ddi_exit;
}
pSgr->sr_status = ERROR_IO_PENDING;
//
// Make sure request doesn't exceed the disk
//
if ((nStartSecNo + nNumOfSec - 1) > pDisk->d_DiskInfo.di_total_sectors)
{
status = ERROR_SECTOR_NOT_FOUND;
goto ddi_exit;
}
status = ERROR_SUCCESS;
pSg = &(pSgr->sr_sglist[0]);
// Index of Sector Number
nSecNoIdx = 0;
nSecBufOffset = 0;
pSecBuf = NULL;
STDRV_INF_PRINT((TEXT("[OND:INF] nNumOfSG = %d\r\n"), nNumOfSG));
STDRV_INF_PRINT((TEXT("[OND:INF] nSGBufLen = %d(0x%x)\r\n"), nSGBufLen, nSGBufLen));
#if ONDISK_MULTIPARTITION
nStartSecNo += (pDisk->nWMRStartSector >> OND_SECTOR_SHIFT); // remapping sector
#endif
/* Number of SG is 1 (Most of cases) */
if (nNumOfSG == 1)
{
nNumOfScts = nSGBufLen / (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT);
//pSGBuf = MapPtrToProcess((LPVOID)pSg->sb_buf, GetCallerProcess());
pSGBuf = MapCallerPtr((LPVOID)pSg->sb_buf,nSGBufLen);
if (nNumOfScts != 0)
{
// RETAILMSG(1, (TEXT("FTL_Write1 : nStartSecNo = 0x%x, nNumOfSects = 0x%x \r\n"), nStartSecNo << OND_SECTOR_SHIFT, nNumOfScts << OND_SECTOR_SHIFT));
nRet = FTL_Write((nStartSecNo << OND_SECTOR_SHIFT), /* Start Logical Sector Number */
(nNumOfScts << OND_SECTOR_SHIFT), /* Number of Sectors */
pSGBuf); /* Pointer ot buffer */
if (nRet != FTL_SUCCESS)
{
STDRV_ERR_PRINT((TEXT("[OND:ERR](DoDiskWrite) FTL_Write Error=0x%x\r\n"), nRet));
status = ERROR_SECTOR_NOT_FOUND;
goto ddi_req_done;
}
nStartSecNo += nNumOfScts;
}
/* To solve 512 byte align problem */
if ((nSGBufLen % (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT)) != 0)
{
UINT nCpResumeOffset;
UINT nCpLeftSize;
/* Memory Allocation for 1 sector read */
pSecBuf = (UCHAR*)malloc((WMR_SECTOR_SIZE << OND_SECTOR_SHIFT));
nCpResumeOffset = nNumOfScts * (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT);
nCpLeftSize = nSGBufLen % (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT);
memset((UCHAR*)pSecBuf, 0xFF, (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT));
memcpy((UCHAR*)pSecBuf, (UCHAR*)(pSGBuf + nCpResumeOffset), nCpLeftSize);
// RETAILMSG(1, (TEXT("FTL_Write2 : nStartSecNo = 0x%x, nNumOfSects = 0x%x \r\n"), nStartSecNo, nNumOfScts));
nRet = FTL_Write((nStartSecNo << OND_SECTOR_SHIFT), /* Start Logical Sector Number */
(1 << OND_SECTOR_SHIFT), /* Number of Sectors */
pSecBuf); /* Pointer ot buffer */
if (nRet != FTL_SUCCESS)
{
STDRV_ERR_PRINT((TEXT("[OND:ERR](DoDiskWrite) FTL_Write Error=0x%x\r\n"), nRet));
free(pSecBuf);
status = ERROR_SECTOR_NOT_FOUND;
goto ddi_req_done;
}
free(pSecBuf);
}
}
/* Number of SG is greater than 1 */
else
{
nDstOffset = 0;
while (nSGBufLen)
{
UINT32 nNumOfScts;
if (nSGBufLen >= PSII_MAX_READ_BUF_SIZE)
{
nNumOfScts = PSII_MAX_READ_BUF_SIZE / (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT);
nCpBytes = (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT) * nNumOfScts;
}
else
{
nNumOfScts = nSGBufLen / (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT);
nCpBytes = (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT) * nNumOfScts;
if (nSGBufLen % (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT))
{
nNumOfScts++;
nCpBytes += (nSGBufLen % (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT));
}
}
pSecBuf = (UCHAR *)malloc((WMR_SECTOR_SIZE << OND_SECTOR_SHIFT) * nNumOfScts);
memset(pSecBuf, 0xFF, (WMR_SECTOR_SIZE << OND_SECTOR_SHIFT) * nNumOfScts);
nSrcOffset = 0;
while(nCpBytes)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -