Skip to content

bd: Tweaked block device API to fit SD cards and FTLs better #4983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions features/filesystem/bd/BlockDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,26 @@ class BlockDevice
* @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
*
Expand All @@ -111,7 +130,10 @@ class BlockDevice
* @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
*
Expand Down
2 changes: 1 addition & 1 deletion features/filesystem/fat/ChaN/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
9 changes: 9 additions & 0 deletions features/filesystem/fat/FATFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguments to erase function were start sector, and total size to erase. Please clarify what are the arguments of trim? sectors[0] / sectors[1] what do they contain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sure: http://elm-chan.org/fsw/ff/doc/dioctl.html

The sector block is specified by a DWORD array {<start sector>, <end sector>}

So sectors[0] is the start sector, and sectors[1] is the end sector (inclusive) of the trim operation.

return err ? RES_PARERR : RES_OK;
}
}

return RES_PARERR;
Expand Down