refactor async io, add in_isr argument to tud_msc_async_io_done()

use cbw.command[0] for pending IO command
This commit is contained in:
hathach
2025-07-01 20:13:21 +07:00
parent 77e142ed76
commit d22cbe4cb5
2 changed files with 123 additions and 158 deletions

View File

@@ -52,36 +52,27 @@ enum {
MSC_STAGE_NEED_RESET,
};
enum {
MSC_NEXT_OP_NONE = 0,
MSC_NEXT_OP_READ10,
MSC_NEXT_OP_WRITE10,
MSC_NEXT_OP_STATUS
};
typedef struct {
TU_ATTR_ALIGNED(4) msc_cbw_t cbw;
TU_ATTR_ALIGNED(4) msc_csw_t csw;
TU_ATTR_ALIGNED(4) msc_cbw_t cbw; // 31 bytes
uint8_t rhport;
TU_ATTR_ALIGNED(4) msc_csw_t csw; // 13 bytes
uint8_t itf_num;
uint8_t ep_in;
uint8_t ep_out;
// Bulk Only Transfer (BOT) Protocol
uint8_t stage;
uint32_t total_len; // byte to be transferred, can be smaller than total_bytes in cbw
uint32_t xferred_len; // numbered of bytes transferred so far in the Data Stage
// Sense Response Data
// Bulk Only Transfer (BOT) Protocol
uint8_t stage;
// SCSI Sense Response Data
uint8_t sense_key;
uint8_t add_sense_code;
uint8_t add_sense_qualifier;
// Async IO
uint8_t next_op;
uint32_t xferred_bytes;
uint8_t pending_io; // pending async IO
}mscd_interface_t;
static mscd_interface_t _mscd_itf;
@@ -95,12 +86,11 @@ CFG_TUD_MEM_SECTION static struct {
//--------------------------------------------------------------------+
static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_t* buffer, uint32_t bufsize);
static void proc_read10_cmd(mscd_interface_t* p_msc);
static void proc_read10_next(mscd_interface_t* p_msc, int32_t nbytes);
static void proc_read_io_data(mscd_interface_t* p_msc, int32_t nbytes);
static void proc_write10_cmd(mscd_interface_t* p_msc);
static void proc_write10_new_data(mscd_interface_t* p_msc, uint32_t xferred_bytes);
static void proc_write10_next(mscd_interface_t* p_msc, uint32_t xferred_bytes, int32_t nbytes);
static void proc_write10_host_data(mscd_interface_t* p_msc, uint32_t xferred_bytes);
static void proc_write_io_data(mscd_interface_t* p_msc, uint32_t xferred_bytes, int32_t nbytes);
static bool proc_stage_status(mscd_interface_t* p_msc);
static void tud_msc_async_io_done_cb(void* bytes_processed);
TU_ATTR_ALWAYS_INLINE static inline bool is_data_in(uint8_t dir) {
return tu_bit_test(dir, 7);
@@ -198,6 +188,7 @@ static uint8_t rdwr10_validate_cmd(msc_cbw_t const* cbw) {
static bool proc_stage_status(mscd_interface_t *p_msc) {
uint8_t rhport = p_msc->rhport;
msc_cbw_t const *p_cbw = &p_msc->cbw;
// skip status if epin is currently stalled, will do it when received Clear Stall request
if (!usbd_edpt_stalled(rhport, p_msc->ep_in)) {
if ((p_cbw->total_bytes > p_msc->xferred_len) && is_data_in(p_cbw->dir)) {
@@ -258,39 +249,57 @@ bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, u
return true;
}
static inline void set_sense_medium_not_present(uint8_t lun) {
TU_ATTR_ALWAYS_INLINE static inline void set_sense_medium_not_present(uint8_t lun) {
// default sense is NOT READY, MEDIUM NOT PRESENT
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3A, 0x00);
}
void tud_msc_async_io_done(int32_t bytes_processed) {
// Precheck to avoid queueing multiple RW done callback
TU_VERIFY(_mscd_itf.next_op != MSC_NEXT_OP_NONE,);
// Call usbd_edpt_xfer() in tud_task() to avoid racing condition
usbd_defer_func(tud_msc_async_io_done_cb, (void*) (intptr_t)bytes_processed, false);
static void proc_async_io_done(void *bytes_processed) {
mscd_interface_t *p_msc = &_mscd_itf;
TU_VERIFY(p_msc->pending_io, );
const int32_t nbytes = (int32_t) (intptr_t) bytes_processed;
const uint8_t cmd = p_msc->cbw.command[0];
p_msc->pending_io = 0;
switch (cmd) {
case SCSI_CMD_READ_10:
proc_read_io_data(p_msc, nbytes);
break;
case SCSI_CMD_WRITE_10:
proc_write_io_data(p_msc, (uint32_t) nbytes, nbytes);
break;
default: break;
}
static void tud_msc_async_io_done_cb(void* bytes_processed) {
TU_VERIFY(_mscd_itf.next_op != MSC_NEXT_OP_NONE,);
uint8_t next_op = _mscd_itf.next_op;
_mscd_itf.next_op = MSC_NEXT_OP_NONE;
int32_t nbytes = (int32_t)(intptr_t)bytes_processed;
// READ10
if (next_op == MSC_NEXT_OP_READ10) {
proc_read10_next(&_mscd_itf, nbytes);
} else if (next_op == MSC_NEXT_OP_WRITE10) {
proc_write10_next(&_mscd_itf, _mscd_itf.xferred_bytes, nbytes);
// Need to manually invoke CSW transfer
if (_mscd_itf.stage == MSC_STAGE_STATUS) {
proc_stage_status(&_mscd_itf);
// send status if stage is transitioned to STATUS
if (p_msc->stage == MSC_STAGE_STATUS) {
proc_stage_status(p_msc);
}
}
bool tud_msc_async_io_done(int32_t bytes_io, bool in_isr) {
// Precheck to avoid queueing multiple RW done callback
TU_VERIFY(_mscd_itf.pending_io);
if (bytes_io == 0) {
bytes_io = TUD_MSC_RET_ERROR; // 0 is treated as error, no sense to call this with BUSY here
}
if (in_isr) {
usbd_defer_func(proc_async_io_done, (void*) (intptr_t)bytes_io, in_isr);
} else {
proc_async_io_done((void*)(intptr_t) bytes_io);
}
return true;
}
//--------------------------------------------------------------------+
// USBD Driver API
//--------------------------------------------------------------------+
void mscd_init(void) {
TU_LOG_INT(CFG_TUD_MSC_LOG_LEVEL, sizeof(mscd_interface_t));
tu_memclr(&_mscd_itf, sizeof(mscd_interface_t));
}
@@ -528,7 +537,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
proc_read10_cmd(p_msc);
}
} else if (SCSI_CMD_WRITE_10 == p_cbw->command[0]) {
proc_write10_new_data(p_msc, xferred_bytes);
proc_write10_host_data(p_msc, xferred_bytes);
} else {
p_msc->xferred_len += xferred_bytes;
@@ -560,7 +569,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
break;
case MSC_STAGE_STATUS_SENT:
// Wait for the Status phase to complete
// Status phase is complete
if ((ep_addr == p_msc->ep_in) && (xferred_bytes == sizeof(msc_csw_t))) {
TU_LOG_DRV(" SCSI Status [Lun%u] = %u\r\n", p_cbw->lun, p_csw->status);
// TU_LOG_MEM(CFG_TUD_MSC_LOG_LEVEL, p_csw, xferred_bytes, 2);
@@ -590,7 +599,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
TU_ASSERT(prepare_cbw(p_msc));
} else {
// Any xfer ended here is consider unknown error, ignore it
// Any xfer ended here is considered unknown error, ignore it
TU_LOG1(" Warning expect SCSI Status but received unknown data\r\n");
}
break;
@@ -696,8 +705,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
break;
case SCSI_CMD_READ_FORMAT_CAPACITY: {
scsi_read_format_capacity_data_t read_fmt_capa =
{
scsi_read_format_capacity_data_t read_fmt_capa = {
.list_length = 8,
.block_num = 0,
.descriptor_type = 2, // formatted media
@@ -729,8 +737,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
break;
case SCSI_CMD_INQUIRY: {
scsi_inquiry_resp_t inquiry_rsp =
{
scsi_inquiry_resp_t inquiry_rsp = {
.is_removable = 1,
.version = 2,
.response_data_format = 2,
@@ -750,8 +757,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
break;
case SCSI_CMD_MODE_SENSE_6: {
scsi_mode_sense6_resp_t mode_resp =
{
scsi_mode_sense6_resp_t mode_resp = {
.data_len = 3,
.medium_type = 0,
.write_protected = false,
@@ -772,8 +778,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
break;
case SCSI_CMD_REQUEST_SENSE: {
scsi_sense_fixed_resp_t sense_rsp =
{
scsi_sense_fixed_resp_t sense_rsp = {
.response_code = 0x70, // current, fixed format
.valid = 1
};
@@ -805,32 +810,27 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
static void proc_read10_cmd(mscd_interface_t* p_msc) {
msc_cbw_t const* p_cbw = &p_msc->cbw;
// block size already verified not zero
uint16_t const block_sz = rdwr10_get_blocksize(p_cbw);
// Adjust lba with transferred bytes
uint16_t const block_sz = rdwr10_get_blocksize(p_cbw); // already verified non-zero
// Adjust lba & offset with transferred bytes
uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz);
uint32_t const offset = p_msc->xferred_len % block_sz;
// remaining bytes capped at class buffer
int32_t nbytes = (int32_t)tu_min32(CFG_TUD_MSC_EP_BUFSIZE, p_cbw->total_bytes - p_msc->xferred_len);
// Application can consume smaller bytes
uint32_t const offset = p_msc->xferred_len % block_sz;
p_msc->next_op = MSC_NEXT_OP_READ10;
p_msc->pending_io = 1;
nbytes = tud_msc_read10_cb(p_cbw->lun, lba, offset, _mscd_epbuf.buf, (uint32_t)nbytes);
if (nbytes != TUD_MSC_RET_ASYNC) {
p_msc->next_op = MSC_NEXT_OP_NONE;
proc_read10_next(p_msc, nbytes);
p_msc->pending_io = 0;
proc_read_io_data(p_msc, nbytes);
}
}
static void proc_read10_next(mscd_interface_t* p_msc, int32_t nbytes) {
static void proc_read_io_data(mscd_interface_t* p_msc, int32_t nbytes) {
uint8_t rhport = p_msc->rhport;
if (nbytes < 0) {
// negative means error -> endpoint is stalled & status in CSW set to failed
TU_LOG_DRV(" tud_msc_read10_cb() return -1\r\n");
TU_LOG_DRV(" IO read() failed\r\n");
// set sense
msc_cbw_t const* p_cbw = &p_msc->cbw;
@@ -838,7 +838,7 @@ static void proc_read10_next(mscd_interface_t* p_msc, int32_t nbytes) {
fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
} else if (nbytes == 0) {
// zero means not ready -> simulate an transfer complete so that this driver callback will fired again
// zero means not ready -> fake a transfer complete so that this driver callback will fire again
dcd_event_xfer_complete(rhport, p_msc->ep_in, 0, XFER_RESULT_SUCCESS, false);
} else {
TU_ASSERT(usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_epbuf.buf, (uint16_t) nbytes),);
@@ -864,55 +864,42 @@ static void proc_write10_cmd(mscd_interface_t* p_msc) {
// remaining bytes capped at class buffer
uint16_t nbytes = (uint16_t)tu_min32(CFG_TUD_MSC_EP_BUFSIZE, p_cbw->total_bytes - p_msc->xferred_len);
// Write10 callback will be called later when usb transfer complete
uint8_t rhport = p_msc->rhport;
TU_ASSERT(usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_epbuf.buf, nbytes),);
TU_ASSERT(usbd_edpt_xfer(p_msc->rhport, p_msc->ep_out, _mscd_epbuf.buf, nbytes),);
}
// process new data arrived from WRITE10
static void proc_write10_new_data(mscd_interface_t* p_msc, uint32_t xferred_bytes) {
static void proc_write10_host_data(mscd_interface_t* p_msc, uint32_t xferred_bytes) {
msc_cbw_t const* p_cbw = &p_msc->cbw;
uint16_t const block_sz = rdwr10_get_blocksize(p_cbw); // already verified non-zero
// block size already verified not zero
uint16_t const block_sz = rdwr10_get_blocksize(p_cbw);
// Adjust lba with transferred bytes
// Adjust lba & offset with transferred bytes
uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz);
// Invoke callback to consume new data
uint32_t const offset = p_msc->xferred_len % block_sz;
p_msc->next_op = MSC_NEXT_OP_WRITE10;
p_msc->xferred_bytes = xferred_bytes;
p_msc->pending_io = 1;
int32_t nbytes = tud_msc_write10_cb(p_cbw->lun, lba, offset, _mscd_epbuf.buf, xferred_bytes);
if (nbytes != TUD_MSC_RET_ASYNC) {
p_msc->next_op = MSC_NEXT_OP_NONE;
proc_write10_next(p_msc, xferred_bytes, nbytes);
p_msc->pending_io = 0;
proc_write_io_data(p_msc, xferred_bytes, nbytes);
}
}
static void proc_write10_next(mscd_interface_t* p_msc, uint32_t xferred_bytes, int32_t nbytes) {
static void proc_write_io_data(mscd_interface_t* p_msc, uint32_t xferred_bytes, int32_t nbytes) {
if (nbytes < 0) {
// negative means error -> failed this scsi op
TU_LOG_DRV(" tud_msc_write10_cb() return -1\r\n");
// update actual byte before failed
p_msc->xferred_len += xferred_bytes;
msc_cbw_t const* p_cbw = &p_msc->cbw;
set_sense_medium_not_present(p_cbw->lun);
TU_LOG_DRV(" IO write() failed\r\n");
set_sense_medium_not_present(p_msc->cbw.lun);
fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
} else {
if ((uint32_t)nbytes < xferred_bytes) {
// Application consume less than what we got (including zero)
const uint32_t left_over = xferred_bytes - (uint32_t)nbytes;
if (nbytes > 0) {
p_msc->xferred_len += (uint16_t)nbytes;
memmove(_mscd_epbuf.buf, _mscd_epbuf.buf + nbytes, left_over);
}
// simulate a transfer complete with adjusted parameters --> callback will be invoked with adjusted parameter
uint8_t rhport = p_msc->rhport;
dcd_event_xfer_complete(rhport, p_msc->ep_out, left_over, XFER_RESULT_SUCCESS, false);
// fake a transfer complete with adjusted parameters --> callback will be invoked with adjusted parameters
dcd_event_xfer_complete(p_msc->rhport, p_msc->ep_out, left_over, XFER_RESULT_SUCCESS, false);
} else {
// Application consume all bytes in our buffer
p_msc->xferred_len += xferred_bytes;

View File

@@ -49,10 +49,11 @@
#endif
// Return value of callback functions
// Error
#define TUD_MSC_RET_ERROR -1
// Asynchronous IO
#define TUD_MSC_RET_ASYNC -16
enum {
TUD_MSC_RET_BUSY = 0, // Busy, e.g disk I/O is not ready
TUD_MSC_RET_ERROR = -1,
TUD_MSC_RET_ASYNC = -2, // Asynchronous IO
};
TU_VERIFY_STATIC(CFG_TUD_MSC_EP_BUFSIZE < UINT16_MAX, "Size is not correct");
@@ -63,54 +64,31 @@ TU_VERIFY_STATIC(CFG_TUD_MSC_EP_BUFSIZE < UINT16_MAX, "Size is not correct");
// Set SCSI sense response
bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, uint8_t add_sense_qualifier);
// Called once asynchronous read/write operation is done
// bytes_processed has the same meaning of tud_msc_read10_cb() /
// tud_msc_write10_cb() return value
void tud_msc_async_io_done(int32_t bytes_processed);
// Called by Application once asynchronous I/O operation is done
// bytes_io is number of bytes in I/O op, typically the bufsize in read/write_cb() or
// TUD_MSC_RET_ERROR (-1) for error. Note TUD_MSC_RET_BUSY (0) will be treated as error as well.
bool tud_msc_async_io_done(int32_t bytes_io, bool in_isr);
//--------------------------------------------------------------------+
// Application Callbacks (WEAK is optional)
//--------------------------------------------------------------------+
// Invoked when received SCSI READ10 command
// - Address = lba * BLOCK_SIZE + offset
// - offset is only needed if CFG_TUD_MSC_EP_BUFSIZE is smaller than BLOCK_SIZE.
//
// - Application fill the buffer (up to bufsize) with address contents and return number of bytes read or status.
//
// - ret < bufsize : These bytes are transferred first and callback will be invoked again for remaining data.
//
// - ret == 0 : Indicate application is not ready yet e.g disk I/O busy.
// Callback will be invoked again with the same parameters later on.
//
// - ret == TUD_MSC_RET_ERROR (-1)
// : Indicate application error e.g invalid address. This request will be STALLed
// and return failed status in command status wrapper phase.
//
// - ret == TUD_MSC_RET_ASYNC (-16)
// : Data reading will be done asynchronously in a background task. Application should return immediately.
// tud_msc_async_io_done() must be called once reading is done to signal completion.
/*
Invoked when received SCSI READ10/WRITE10 command
- Address = lba * BLOCK_SIZE + offset
- offset is only needed if CFG_TUD_MSC_EP_BUFSIZE is smaller than BLOCK_SIZE.
- Application fill the buffer (up to bufsize) with address contents and return number of bytes read or status.
- 0 < ret < bufsize: These bytes are transferred first and callback will be invoked again for remaining data.
- ret == TUD_MSC_RET_BUSY
Application is buys e.g disk I/O not ready.
Callback will be invoked again with the same parameters later on.
- ret == TUD_MSC_RET_ERROR
error such as invalid address. This request will be STALLed and scsi command will be failed
- ret == TUD_MSC_RET_ASYNC
Data I/O will be done asynchronously in a background task. Application should return immediately.
tud_msc_async_io_done() must be called once IO/ is done to signal completion.
*/
int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize);
// Invoked when received SCSI WRITE10 command
// - Address = lba * BLOCK_SIZE + offset
// - offset is only needed if CFG_TUD_MSC_EP_BUFSIZE is smaller than BLOCK_SIZE.
//
// - Application writes data from buffer to address contents (up to bufsize) and returns the number of bytes written or status.
//
// - ret < bufsize : Callback will be invoked again with remaining data later on.
//
// - ret == 0 : Indicate application is not ready yet e.g disk I/O busy.
// Callback will be invoked again with the same parameters later on.
//
// - ret == TUD_MSC_RET_ERROR (-1)
// : Indicate application error e.g invalid address. This request will be STALLed
// and return failed status in command status wrapper phase.
//
// - ret == TUD_MSC_RET_ASYNC (-16)
// : Data writing will be done asynchronously in a background task. Application should return immediately.
// tud_msc_async_io_done() must be called once writing is done to signal completion.
// TODO change buffer to const uint8_t*
int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize);
// Invoked when received SCSI_CMD_INQUIRY