Skip to content

MSC Device: Add asynchronous IO support #2967

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

HiFiPhile
Copy link
Collaborator

@HiFiPhile HiFiPhile commented Jan 29, 2025

Describe the PR

Currently, mixing the MSC class with other classes could be problematic since IO operations could block tud_task() for a long time and degrade real-time performance, especially for isochronous and interrupt transfer-based classes.

Now, it's possible to delegate IO operations to a background task.

The cdc_msc_freertos example has been updated for demonstration. One can change CFG_EXAMPLE_MSC_IO_DELAY_MS to a high value and type in the console while doing disk IO. With CFG_EXAMPLE_MSC_ASYNC_IO ON and OFF, the latency difference should be very noticeable.

@HiFiPhile HiFiPhile marked this pull request as ready for review January 29, 2025 16:18
Signed-off-by: HiFiPhile <[email protected]>
@602p
Copy link

602p commented Apr 9, 2025

I'm using this in a work project. I'd love to see something around these lines landed.

@DazzlingOkami
Copy link

This is a very practical feature, and I also hope that it can be implemented as soon as possible.

Comment on lines 419 to 602
}else {
proc_read10_cmd(rhport, p_msc);
proc_read10_cmd(p_msc);
}
} else if (SCSI_CMD_WRITE_10 == p_cbw->command[0]) {
proc_write10_new_data(rhport, p_msc, xferred_bytes);
proc_write10_new_data(p_msc, xferred_bytes);
} else {
p_msc->xferred_len += xferred_bytes;

// OUT transfer, invoke callback if needed
if ( !is_data_in(p_cbw->dir) ) {
int32_t cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_epbuf.buf, (uint16_t) p_msc->total_len);

if ( cb_result < 0 ) {
// unsupported command
TU_LOG_DRV(" SCSI unsupported command\r\n");
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
}else {
// TODO haven't implement this scenario any further yet
}
}

if ( p_msc->xferred_len >= p_msc->total_len ) {
// Data Stage is complete
p_msc->stage = MSC_STAGE_STATUS;
} else {
// This scenario with command that take more than one transfer is already rejected at Command stage
TU_BREAKPOINT();
}
}
break;

case MSC_STAGE_STATUS:
// processed immediately after this switch, supposedly to be empty
break;

case MSC_STAGE_STATUS_SENT:
// Wait for the Status phase to 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);

// Invoke complete callback if defined
// Note: There is racing issue with samd51 + qspi flash testing with arduino
// if complete_cb() is invoked after queuing the status.
switch (p_cbw->command[0]) {
case SCSI_CMD_READ_10:
if (tud_msc_read10_complete_cb) {
tud_msc_read10_complete_cb(p_cbw->lun);
}
break;

case SCSI_CMD_WRITE_10:
if (tud_msc_write10_complete_cb) {
tud_msc_write10_complete_cb(p_cbw->lun);
}
break;

default:
if (tud_msc_scsi_complete_cb) {
tud_msc_scsi_complete_cb(p_cbw->lun, p_cbw->command);
}
break;
}

TU_ASSERT(prepare_cbw(rhport, p_msc));
TU_ASSERT(prepare_cbw(p_msc));
} else {
// Any xfer ended here is consider unknown error, ignore it
TU_LOG1(" Warning expect SCSI Status but received unknown data\r\n");
}
break;

default: break;
}

if (p_msc->stage == MSC_STAGE_STATUS) {
// 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)) {
// 6.7 The 13 Cases: case 5 (Hi > Di): STALL before status
// TU_LOG_DRV(" SCSI case 5 (Hi > Di): %lu > %lu\r\n", p_cbw->total_bytes, p_msc->xferred_len);
usbd_edpt_stall(rhport, p_msc->ep_in);
} else {
TU_ASSERT(send_csw(rhport, p_msc));
}
}

#if TU_CHECK_MCU(OPT_MCU_CXD56)
// WORKAROUND: cxd56 has its own nuttx usb stack which does not forward Set/ClearFeature(Endpoint) to DCD.
// There is no way for us to know when EP is un-stall, therefore we will unconditionally un-stall here and
// hope everything will work
if ( usbd_edpt_stalled(rhport, p_msc->ep_in) ) {
usbd_edpt_clear_stall(rhport, p_msc->ep_in);
send_csw(rhport, p_msc);
}
#endif
TU_ASSERT(proc_stage_status(p_msc));

Check notice

Code scanning / CodeQL

Long switch case Note

Switch has at least one case that is too long:
MSC_STAGE_CMD (99 lines)
.
Switch has at least one case that is too long:
MSC_STAGE_DATA (41 lines)
.
Switch has at least one case that is too long:
MSC_STAGE_STATUS_SENT (35 lines)
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants