Skip to content

Commit 44bdde1

Browse files
damien-lemoalfloatious
authored andcommitted
ata: libata-scsi: Refactor ata_scsiop_read_cap()
Move the check for the scsi command service action being SAI_READ_CAPACITY_16 from ata_scsi_simulate() into ata_scsiop_read_cap() to simplify ata_scsi_simulate() for processing capacity reading commands (READ_CAPACITY and SERVICE_ACTION_IN_16). Signed-off-by: Damien Le Moal <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Niklas Cassel <[email protected]>
1 parent b055e3b commit 44bdde1

File tree

1 file changed

+46
-41
lines changed

1 file changed

+46
-41
lines changed

drivers/ata/libata-scsi.c

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,7 @@ static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
25792579
static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
25802580
{
25812581
struct ata_device *dev = args->dev;
2582+
u8 *scsicmd = args->cmd->cmnd;
25822583
u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
25832584
u32 sector_size; /* physical sector size in bytes */
25842585
u8 log2_per_phys;
@@ -2588,7 +2589,7 @@ static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
25882589
log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
25892590
lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
25902591

2591-
if (args->cmd->cmnd[0] == READ_CAPACITY) {
2592+
if (scsicmd[0] == READ_CAPACITY) {
25922593
if (last_lba >= 0xffffffffULL)
25932594
last_lba = 0xffffffff;
25942595

@@ -2603,42 +2604,52 @@ static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
26032604
rbuf[5] = sector_size >> (8 * 2);
26042605
rbuf[6] = sector_size >> (8 * 1);
26052606
rbuf[7] = sector_size;
2606-
} else {
2607-
/* sector count, 64-bit */
2608-
rbuf[0] = last_lba >> (8 * 7);
2609-
rbuf[1] = last_lba >> (8 * 6);
2610-
rbuf[2] = last_lba >> (8 * 5);
2611-
rbuf[3] = last_lba >> (8 * 4);
2612-
rbuf[4] = last_lba >> (8 * 3);
2613-
rbuf[5] = last_lba >> (8 * 2);
2614-
rbuf[6] = last_lba >> (8 * 1);
2615-
rbuf[7] = last_lba;
26162607

2617-
/* sector size */
2618-
rbuf[ 8] = sector_size >> (8 * 3);
2619-
rbuf[ 9] = sector_size >> (8 * 2);
2620-
rbuf[10] = sector_size >> (8 * 1);
2621-
rbuf[11] = sector_size;
2622-
2623-
rbuf[12] = 0;
2624-
rbuf[13] = log2_per_phys;
2625-
rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2626-
rbuf[15] = lowest_aligned;
2627-
2628-
if (ata_id_has_trim(args->id) &&
2629-
!(dev->quirks & ATA_QUIRK_NOTRIM)) {
2630-
rbuf[14] |= 0x80; /* LBPME */
2631-
2632-
if (ata_id_has_zero_after_trim(args->id) &&
2633-
dev->quirks & ATA_QUIRK_ZERO_AFTER_TRIM) {
2634-
ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2635-
rbuf[14] |= 0x40; /* LBPRZ */
2636-
}
2608+
return 0;
2609+
}
2610+
2611+
/*
2612+
* READ CAPACITY 16 command is defined as a service action
2613+
* (SERVICE_ACTION_IN_16 command).
2614+
*/
2615+
if (scsicmd[0] != SERVICE_ACTION_IN_16 ||
2616+
(scsicmd[1] & 0x1f) != SAI_READ_CAPACITY_16) {
2617+
ata_scsi_set_invalid_field(dev, args->cmd, 1, 0xff);
2618+
return 1;
2619+
}
2620+
2621+
/* sector count, 64-bit */
2622+
rbuf[0] = last_lba >> (8 * 7);
2623+
rbuf[1] = last_lba >> (8 * 6);
2624+
rbuf[2] = last_lba >> (8 * 5);
2625+
rbuf[3] = last_lba >> (8 * 4);
2626+
rbuf[4] = last_lba >> (8 * 3);
2627+
rbuf[5] = last_lba >> (8 * 2);
2628+
rbuf[6] = last_lba >> (8 * 1);
2629+
rbuf[7] = last_lba;
2630+
2631+
/* sector size */
2632+
rbuf[ 8] = sector_size >> (8 * 3);
2633+
rbuf[ 9] = sector_size >> (8 * 2);
2634+
rbuf[10] = sector_size >> (8 * 1);
2635+
rbuf[11] = sector_size;
2636+
2637+
if (ata_id_zoned_cap(args->id) || args->dev->class == ATA_DEV_ZAC)
2638+
rbuf[12] = (1 << 4); /* RC_BASIS */
2639+
rbuf[13] = log2_per_phys;
2640+
rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2641+
rbuf[15] = lowest_aligned;
2642+
2643+
if (ata_id_has_trim(args->id) && !(dev->quirks & ATA_QUIRK_NOTRIM)) {
2644+
rbuf[14] |= 0x80; /* LBPME */
2645+
2646+
if (ata_id_has_zero_after_trim(args->id) &&
2647+
dev->quirks & ATA_QUIRK_ZERO_AFTER_TRIM) {
2648+
ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2649+
rbuf[14] |= 0x40; /* LBPRZ */
26372650
}
2638-
if (ata_id_zoned_cap(args->id) ||
2639-
args->dev->class == ATA_DEV_ZAC)
2640-
rbuf[12] = (1 << 4); /* RC_BASIS */
26412651
}
2652+
26422653
return 0;
26432654
}
26442655

@@ -4333,14 +4344,8 @@ void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
43334344
break;
43344345

43354346
case READ_CAPACITY:
4336-
ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4337-
break;
4338-
43394347
case SERVICE_ACTION_IN_16:
4340-
if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
4341-
ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4342-
else
4343-
ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4348+
ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
43444349
break;
43454350

43464351
case REPORT_LUNS:

0 commit comments

Comments
 (0)