Skip to content

Commit 7ba5ecf

Browse files
tmon-nordiccarlescufi
authored andcommitted
usb: device_next: report medium error on Mass Storage failed writes
End Write command with Medium Error / Write Error when underlying disk write or sync fails. Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
1 parent 3532c25 commit 7ba5ecf

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

subsys/usb/device_next/class/usbd_msc_scsi.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,15 @@ static size_t not_ready(struct scsi_ctx *ctx, enum scsi_additional_sense_code as
373373
return 0;
374374
}
375375

376+
static size_t medium_error(struct scsi_ctx *ctx, enum scsi_additional_sense_code asc)
377+
{
378+
ctx->status = CHECK_CONDITION;
379+
ctx->sense_key = MEDIUM_ERROR;
380+
ctx->asc = asc;
381+
382+
return 0;
383+
}
384+
376385
void scsi_init(struct scsi_ctx *ctx, const char *disk, const char *vendor,
377386
const char *product, const char *revision)
378387
{
@@ -738,24 +747,32 @@ static size_t store_write_10(struct scsi_ctx *ctx, const uint8_t *buf, size_t le
738747
{
739748
uint32_t remaining_sectors;
740749
uint32_t sectors;
750+
bool error = false;
741751

742752
remaining_sectors = ctx->remaining_data / ctx->sector_size;
743753
sectors = MIN(length, ctx->remaining_data) / ctx->sector_size;
744754
if (disk_access_write(ctx->disk, buf, ctx->lba, sectors) != 0) {
745755
/* Flush cache and terminate transfer */
746756
sectors = 0;
747757
remaining_sectors = 0;
758+
error = true;
748759
}
749760

750761
/* Flush cache if this is the last sector in transfer */
751762
if (remaining_sectors - sectors == 0) {
752763
if (disk_access_ioctl(ctx->disk, DISK_IOCTL_CTRL_SYNC, NULL)) {
753764
LOG_ERR("Disk cache sync failed");
765+
error = true;
754766
}
755767
}
756768

757769
ctx->lba += sectors;
758-
return sectors * ctx->sector_size;
770+
771+
if (error) {
772+
return medium_error(ctx, WRITE_ERROR);
773+
} else {
774+
return sectors * ctx->sector_size;
775+
}
759776
}
760777

761778
SCSI_CMD_HANDLER(WRITE_10)

subsys/usb/device_next/class/usbd_msc_scsi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ enum scsi_additional_sense_code {
5858
INVALID_FIELD_IN_CDB = 0x2400,
5959
MEDIUM_NOT_PRESENT = 0x3A00,
6060
MEDIUM_REMOVAL_PREVENTED = 0x5302,
61+
WRITE_ERROR = 0x0C00,
6162
};
6263

6364
struct scsi_ctx {

0 commit comments

Comments
 (0)