Merge pull request #4983 from geky/bd-trim-erase

bd: Tweaked block device API to fit SD cards and FTLs better
pull/5169/merge
Jimmy Brisson 2017-09-21 16:05:37 -05:00 committed by GitHub
commit bfc1c5aa39
3 changed files with 34 additions and 3 deletions

View File

@ -91,7 +91,26 @@ public:
* @param size Size to erase in bytes, must be a multiple of erase block size
* @return 0 on success, negative error code on failure
*/
virtual int erase(bd_addr_t addr, bd_size_t size) = 0;
virtual int erase(bd_addr_t addr, bd_size_t size)
{
return 0;
}
/** Mark blocks as no longer in use
*
* This function provides a hint to the underlying block device that a region of blocks
* is no longer in use and may be erased without side effects. Erase must still be called
* before programming, but trimming allows flash-translation-layers to schedule erases when
* the device is not busy.
*
* @param addr Address of block to mark as unused
* @param size Size to mark as unused in bytes, must be a multiple of erase block size
* @return 0 on success, negative error code on failure
*/
virtual int trim(bd_addr_t addr, bd_size_t size)
{
return 0;
}
/** Get the size of a readable block
*
@ -111,7 +130,10 @@ public:
* @return Size of a eraseable block in bytes
* @note Must be a multiple of the program size
*/
virtual bd_size_t get_erase_size() const = 0;
virtual bd_size_t get_erase_size() const
{
return get_program_size();
}
/** Get the total size of the underlying device
*

View File

@ -171,7 +171,7 @@
/ disk_ioctl() function. */
#define _USE_TRIM 0
#define _USE_TRIM 1
/* This option switches ATA-TRIM feature. (0:Disable or 1:Enable)
/ To enable Trim feature, also CTRL_TRIM command should be implemented to the
/ disk_ioctl() function. */

View File

@ -244,6 +244,15 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
case GET_BLOCK_SIZE:
*((DWORD*)buff) = 1; // default when not known
return RES_OK;
case CTRL_TRIM:
if (_ffs[pdrv] == NULL) {
return RES_NOTRDY;
} else {
DWORD *sectors = (DWORD*)buff;
DWORD ssize = disk_get_sector_size(pdrv);
int err = _ffs[pdrv]->trim(sectors[0]*ssize, (sectors[1]-sectors[0]+1)*ssize);
return err ? RES_PARERR : RES_OK;
}
}
return RES_PARERR;