1 // Support for several common scsi like command data block requests
3 // Copyright (C) 2010 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "ahci.h" // atapi_cmd_data
9 #include "ata.h" // atapi_cmd_data
10 #include "biosvar.h" // GET_GLOBALFLAT
11 #include "block.h" // struct disk_op_s
12 #include "blockcmd.h" // struct cdb_request_sense
13 #include "byteorder.h" // be32_to_cpu
14 #include "esp-scsi.h" // esp_scsi_cmd_data
15 #include "lsi-scsi.h" // lsi_scsi_cmd_data
16 #include "megasas.h" // megasas_cmd_data
17 #include "pvscsi.h" // pvscsi_cmd_data
18 #include "output.h" // dprintf
19 #include "std/disk.h" // DISK_RET_EPARAM
20 #include "string.h" // memset
21 #include "usb-msc.h" // usb_cmd_data
22 #include "usb-uas.h" // usb_cmd_data
23 #include "util.h" // timer_calc
24 #include "virtio-scsi.h" // virtio_scsi_cmd_data
26 // Route command to low-level handler.
28 cdb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
30 u8 type = GET_GLOBALFLAT(op->drive_gf->type);
33 return atapi_cmd_data(op, cdbcmd, blocksize);
35 return usb_cmd_data(op, cdbcmd, blocksize);
37 return uas_cmd_data(op, cdbcmd, blocksize);
38 case DTYPE_VIRTIO_SCSI:
39 return virtio_scsi_cmd_data(op, cdbcmd, blocksize);
41 return lsi_scsi_cmd_data(op, cdbcmd, blocksize);
43 return esp_scsi_cmd_data(op, cdbcmd, blocksize);
45 return megasas_cmd_data(op, cdbcmd, blocksize);
48 return usb_cmd_data(op, cdbcmd, blocksize);
51 return uas_cmd_data(op, cdbcmd, blocksize);
54 return pvscsi_cmd_data(op, cdbcmd, blocksize);
55 case DTYPE_AHCI_ATAPI:
57 return ahci_cmd_data(op, cdbcmd, blocksize);
59 return DISK_RET_EPARAM;
63 // Determine if the command is a request to pull data from the device
65 cdb_is_read(u8 *cdbcmd, u16 blocksize)
67 return blocksize && cdbcmd[0] != CDB_CMD_WRITE_10;
71 /****************************************************************
72 * Low level command requests
73 ****************************************************************/
76 cdb_get_inquiry(struct disk_op_s *op, struct cdbres_inquiry *data)
78 struct cdb_request_sense cmd;
79 memset(&cmd, 0, sizeof(cmd));
80 cmd.command = CDB_CMD_INQUIRY;
81 cmd.length = sizeof(*data);
84 return cdb_cmd_data(op, &cmd, sizeof(*data));
89 cdb_get_sense(struct disk_op_s *op, struct cdbres_request_sense *data)
91 struct cdb_request_sense cmd;
92 memset(&cmd, 0, sizeof(cmd));
93 cmd.command = CDB_CMD_REQUEST_SENSE;
94 cmd.length = sizeof(*data);
97 return cdb_cmd_data(op, &cmd, sizeof(*data));
102 cdb_test_unit_ready(struct disk_op_s *op)
104 struct cdb_request_sense cmd;
105 memset(&cmd, 0, sizeof(cmd));
106 cmd.command = CDB_CMD_TEST_UNIT_READY;
109 return cdb_cmd_data(op, &cmd, 0);
114 cdb_read_capacity(struct disk_op_s *op, struct cdbres_read_capacity *data)
116 struct cdb_read_capacity cmd;
117 memset(&cmd, 0, sizeof(cmd));
118 cmd.command = CDB_CMD_READ_CAPACITY;
121 return cdb_cmd_data(op, &cmd, sizeof(*data));
124 // Mode sense, geometry page.
126 cdb_mode_sense_geom(struct disk_op_s *op, struct cdbres_mode_sense_geom *data)
128 struct cdb_mode_sense cmd;
129 memset(&cmd, 0, sizeof(cmd));
130 cmd.command = CDB_CMD_MODE_SENSE;
131 cmd.flags = 8; /* DBD */
132 cmd.page = MODE_PAGE_HD_GEOMETRY;
133 cmd.count = cpu_to_be16(sizeof(*data));
136 return cdb_cmd_data(op, &cmd, sizeof(*data));
141 cdb_read(struct disk_op_s *op)
143 struct cdb_rwdata_10 cmd;
144 memset(&cmd, 0, sizeof(cmd));
145 cmd.command = CDB_CMD_READ_10;
146 cmd.lba = cpu_to_be32(op->lba);
147 cmd.count = cpu_to_be16(op->count);
148 return cdb_cmd_data(op, &cmd, GET_GLOBALFLAT(op->drive_gf->blksize));
153 cdb_write(struct disk_op_s *op)
155 struct cdb_rwdata_10 cmd;
156 memset(&cmd, 0, sizeof(cmd));
157 cmd.command = CDB_CMD_WRITE_10;
158 cmd.lba = cpu_to_be32(op->lba);
159 cmd.count = cpu_to_be16(op->count);
160 return cdb_cmd_data(op, &cmd, GET_GLOBALFLAT(op->drive_gf->blksize));
164 /****************************************************************
166 ****************************************************************/
169 scsi_process_op(struct disk_op_s *op)
171 switch (op->command) {
175 return cdb_write(op);
181 return DISK_RET_SUCCESS;
183 return DISK_RET_EPARAM;
188 scsi_is_ready(struct disk_op_s *op)
190 dprintf(6, "scsi_is_ready (drive=%p)\n", op->drive_gf);
192 /* Retry TEST UNIT READY for 5 seconds unless MEDIUM NOT PRESENT is
193 * reported by the device. If the device reports "IN PROGRESS",
194 * 30 seconds is added. */
196 u32 end = timer_calc(5000);
198 if (timer_check(end)) {
199 dprintf(1, "test unit ready failed\n");
203 int ret = cdb_test_unit_ready(op);
208 struct cdbres_request_sense sense;
209 ret = cdb_get_sense(op, &sense);
215 if (sense.asc == 0x3a) { /* MEDIUM NOT PRESENT */
216 dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
220 if (sense.asc == 0x04 && sense.ascq == 0x01 && !in_progress) {
221 /* IN PROGRESS OF BECOMING READY */
222 printf("Waiting for device to detect medium... ");
223 /* Allow 30 seconds more */
224 end = timer_calc(30000);
231 // Validate drive, find block size / sector count, and register drive.
233 scsi_drive_setup(struct drive_s *drive, const char *s, int prio)
235 struct disk_op_s dop;
236 memset(&dop, 0, sizeof(dop));
237 dop.drive_gf = drive;
238 struct cdbres_inquiry data;
239 int ret = cdb_get_inquiry(&dop, &data);
242 char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1];
243 char rev[sizeof(data.rev)+1];
244 strtcpy(vendor, data.vendor, sizeof(vendor));
245 nullTrailingSpace(vendor);
246 strtcpy(product, data.product, sizeof(product));
247 nullTrailingSpace(product);
248 strtcpy(rev, data.rev, sizeof(rev));
249 nullTrailingSpace(rev);
250 int pdt = data.pdt & 0x1f;
251 int removable = !!(data.removable & 0x80);
252 dprintf(1, "%s vendor='%s' product='%s' rev='%s' type=%d removable=%d\n"
253 , s, vendor, product, rev, pdt, removable);
254 drive->removable = removable;
256 if (pdt == SCSI_TYPE_CDROM) {
257 drive->blksize = CDROM_SECTOR_SIZE;
258 drive->sectors = (u64)-1;
260 char *desc = znprintf(MAXDESCSIZE, "DVD/CD [%s Drive %s %s %s]"
261 , s, vendor, product, rev);
262 boot_add_cd(drive, desc, prio);
266 ret = scsi_is_ready(&dop);
268 dprintf(1, "scsi_is_ready returned %d\n", ret);
272 struct cdbres_read_capacity capdata;
273 ret = cdb_read_capacity(&dop, &capdata);
277 // READ CAPACITY returns the address of the last block.
278 // We do not bother with READ CAPACITY(16) because BIOS does not support
279 // 64-bit LBA anyway.
280 drive->blksize = be32_to_cpu(capdata.blksize);
281 if (drive->blksize != DISK_SECTOR_SIZE) {
282 dprintf(1, "%s: unsupported block size %d\n", s, drive->blksize);
285 drive->sectors = (u64)be32_to_cpu(capdata.sectors) + 1;
286 dprintf(1, "%s blksize=%d sectors=%d\n"
287 , s, drive->blksize, (unsigned)drive->sectors);
289 // We do not recover from USB stalls, so try to be safe and avoid
290 // sending the command if the (obsolete, but still provided by QEMU)
291 // fixed disk geometry page may not be supported.
293 // We could also send the command only to small disks (e.g. <504MiB)
294 // but some old USB keys only support a very small subset of SCSI which
295 // does not even include the MODE SENSE command!
297 if (CONFIG_QEMU_HARDWARE && memcmp(vendor, "QEMU", 5) == 0) {
298 struct cdbres_mode_sense_geom geomdata;
299 ret = cdb_mode_sense_geom(&dop, &geomdata);
302 cylinders = geomdata.cyl[0] << 16;
303 cylinders |= geomdata.cyl[1] << 8;
304 cylinders |= geomdata.cyl[2];
305 if (cylinders && geomdata.heads &&
306 drive->sectors <= 0xFFFFFFFFULL &&
307 ((u32)drive->sectors % (geomdata.heads * cylinders) == 0)) {
308 drive->pchs.cylinder = cylinders;
309 drive->pchs.head = geomdata.heads;
310 drive->pchs.sector = (u32)drive->sectors / (geomdata.heads * cylinders);
315 char *desc = znprintf(MAXDESCSIZE, "%s Drive %s %s %s"
316 , s, vendor, product, rev);
317 boot_add_hd(drive, desc, prio);