blob: eb531d46ead6788249619358c45ed1699d00cb86 [file] [log] [blame]
Kevin O'Connor76977b22010-02-17 01:01:32 -05001// Support for several common scsi like command data block requests
2//
3// Copyright (C) 2010 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2002 MandrakeSoft S.A.
5//
6// This file may be distributed under the terms of the GNU LGPLv3 license.
7
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04008#include "ahci.h" // atapi_cmd_data
9#include "ata.h" // atapi_cmd_data
Kevin O'Connor1902c942013-10-26 11:48:06 -040010#include "biosvar.h" // GET_GLOBALFLAT
Kevin O'Connor135f3f62013-09-14 23:57:26 -040011#include "block.h" // struct disk_op_s
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040012#include "blockcmd.h" // struct cdb_request_sense
Kevin O'Connorb3064592012-08-14 21:20:10 -040013#include "byteorder.h" // be32_to_cpu
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040014#include "esp-scsi.h" // esp_scsi_cmd_data
15#include "lsi-scsi.h" // lsi_scsi_cmd_data
16#include "megasas.h" // megasas_cmd_data
Evgeny Budilovsky83d60b32013-10-14 18:03:36 +030017#include "pvscsi.h" // pvscsi_cmd_data
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040018#include "output.h" // dprintf
Kevin O'Connor135f3f62013-09-14 23:57:26 -040019#include "std/disk.h" // DISK_RET_EPARAM
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040020#include "string.h" // memset
Kevin O'Connor7149fc82010-02-17 23:24:42 -050021#include "usb-msc.h" // usb_cmd_data
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +020022#include "usb-uas.h" // usb_cmd_data
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040023#include "util.h" // timer_calc
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010024#include "virtio-scsi.h" // virtio_scsi_cmd_data
Kevin O'Connor76977b22010-02-17 01:01:32 -050025
Kevin O'Connor7149fc82010-02-17 23:24:42 -050026// Route command to low-level handler.
Kevin O'Connor76977b22010-02-17 01:01:32 -050027static int
28cdb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
29{
Kevin O'Connor1902c942013-10-26 11:48:06 -040030 u8 type = GET_GLOBALFLAT(op->drive_gf->type);
Kevin O'Connor76977b22010-02-17 01:01:32 -050031 switch (type) {
Kevin O'Connorbd6afe52012-07-21 12:01:12 -040032 case DTYPE_ATA_ATAPI:
Kevin O'Connor76977b22010-02-17 01:01:32 -050033 return atapi_cmd_data(op, cdbcmd, blocksize);
Kevin O'Connor7149fc82010-02-17 23:24:42 -050034 case DTYPE_USB:
35 return usb_cmd_data(op, cdbcmd, blocksize);
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +020036 case DTYPE_UAS:
37 return uas_cmd_data(op, cdbcmd, blocksize);
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010038 case DTYPE_VIRTIO_SCSI:
39 return virtio_scsi_cmd_data(op, cdbcmd, blocksize);
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +020040 case DTYPE_LSI_SCSI:
41 return lsi_scsi_cmd_data(op, cdbcmd, blocksize);
Paolo Bonzini7a39e722012-08-06 13:15:06 +020042 case DTYPE_ESP_SCSI:
43 return esp_scsi_cmd_data(op, cdbcmd, blocksize);
Hannes Reinecke2df70bf2012-11-13 15:03:31 +010044 case DTYPE_MEGASAS:
45 return megasas_cmd_data(op, cdbcmd, blocksize);
Kevin O'Connorde30dad2013-12-30 22:09:04 -050046 case DTYPE_USB_32:
47 if (!MODESEGMENT)
48 return usb_cmd_data(op, cdbcmd, blocksize);
49 case DTYPE_UAS_32:
50 if (!MODESEGMENT)
51 return uas_cmd_data(op, cdbcmd, blocksize);
Evgeny Budilovsky83d60b32013-10-14 18:03:36 +030052 case DTYPE_PVSCSI:
Kevin O'Connor4b400a12013-12-24 00:46:15 -050053 if (!MODESEGMENT)
54 return pvscsi_cmd_data(op, cdbcmd, blocksize);
Kevin O'Connor7c80bb82013-10-02 21:28:08 -040055 case DTYPE_AHCI_ATAPI:
56 if (!MODESEGMENT)
57 return ahci_cmd_data(op, cdbcmd, blocksize);
Kevin O'Connor76977b22010-02-17 01:01:32 -050058 default:
Kevin O'Connor76977b22010-02-17 01:01:32 -050059 return DISK_RET_EPARAM;
60 }
61}
62
Kevin O'Connor1fd9a892012-03-14 21:27:45 -040063// Determine if the command is a request to pull data from the device
64int
65cdb_is_read(u8 *cdbcmd, u16 blocksize)
66{
67 return blocksize && cdbcmd[0] != CDB_CMD_WRITE_10;
68}
69
Kevin O'Connor7149fc82010-02-17 23:24:42 -050070int
Paolo Bonzini8c976e32011-11-16 13:02:51 +010071scsi_is_ready(struct disk_op_s *op)
72{
Kevin O'Connor1902c942013-10-26 11:48:06 -040073 dprintf(6, "scsi_is_ready (drive=%p)\n", op->drive_gf);
Paolo Bonzini8c976e32011-11-16 13:02:51 +010074
75 /* Retry TEST UNIT READY for 5 seconds unless MEDIUM NOT PRESENT is
76 * reported by the device. If the device reports "IN PROGRESS",
77 * 30 seconds is added. */
78 int in_progress = 0;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040079 u32 end = timer_calc(5000);
Paolo Bonzini8c976e32011-11-16 13:02:51 +010080 for (;;) {
Kevin O'Connor018bdd72013-07-20 18:22:57 -040081 if (timer_check(end)) {
Paolo Bonzini8c976e32011-11-16 13:02:51 +010082 dprintf(1, "test unit ready failed\n");
83 return -1;
84 }
85
86 int ret = cdb_test_unit_ready(op);
87 if (!ret)
88 // Success
89 break;
90
91 struct cdbres_request_sense sense;
92 ret = cdb_get_sense(op, &sense);
93 if (ret)
94 // Error - retry.
95 continue;
96
97 // Sense succeeded.
98 if (sense.asc == 0x3a) { /* MEDIUM NOT PRESENT */
99 dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
100 return -1;
101 }
102
103 if (sense.asc == 0x04 && sense.ascq == 0x01 && !in_progress) {
104 /* IN PROGRESS OF BECOMING READY */
105 printf("Waiting for device to detect medium... ");
106 /* Allow 30 seconds more */
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400107 end = timer_calc(30000);
Paolo Bonzini8c976e32011-11-16 13:02:51 +0100108 in_progress = 1;
109 }
110 }
111 return 0;
112}
113
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500114// Validate drive, find block size / sector count, and register drive.
Paolo Bonzinided04a32011-11-17 10:23:02 +0100115int
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500116scsi_drive_setup(struct drive_s *drive, const char *s, int prio)
Paolo Bonzinided04a32011-11-17 10:23:02 +0100117{
Paolo Bonzinided04a32011-11-17 10:23:02 +0100118 struct disk_op_s dop;
119 memset(&dop, 0, sizeof(dop));
Kevin O'Connor1902c942013-10-26 11:48:06 -0400120 dop.drive_gf = drive;
Paolo Bonzinided04a32011-11-17 10:23:02 +0100121 struct cdbres_inquiry data;
122 int ret = cdb_get_inquiry(&dop, &data);
123 if (ret)
124 return ret;
125 char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1];
126 char rev[sizeof(data.rev)+1];
127 strtcpy(vendor, data.vendor, sizeof(vendor));
128 nullTrailingSpace(vendor);
129 strtcpy(product, data.product, sizeof(product));
130 nullTrailingSpace(product);
131 strtcpy(rev, data.rev, sizeof(rev));
132 nullTrailingSpace(rev);
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500133 int pdt = data.pdt & 0x1f;
Paolo Bonzinided04a32011-11-17 10:23:02 +0100134 int removable = !!(data.removable & 0x80);
135 dprintf(1, "%s vendor='%s' product='%s' rev='%s' type=%d removable=%d\n"
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500136 , s, vendor, product, rev, pdt, removable);
Paolo Bonzinided04a32011-11-17 10:23:02 +0100137 drive->removable = removable;
138
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500139 if (pdt == SCSI_TYPE_CDROM) {
Paolo Bonzinided04a32011-11-17 10:23:02 +0100140 drive->blksize = CDROM_SECTOR_SIZE;
141 drive->sectors = (u64)-1;
142
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500143 char *desc = znprintf(MAXDESCSIZE, "DVD/CD [%s Drive %s %s %s]"
Paolo Bonzinided04a32011-11-17 10:23:02 +0100144 , s, vendor, product, rev);
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500145 boot_add_cd(drive, desc, prio);
Paolo Bonzinided04a32011-11-17 10:23:02 +0100146 return 0;
147 }
148
149 ret = scsi_is_ready(&dop);
150 if (ret) {
151 dprintf(1, "scsi_is_ready returned %d\n", ret);
152 return ret;
153 }
154
155 struct cdbres_read_capacity capdata;
156 ret = cdb_read_capacity(&dop, &capdata);
157 if (ret)
158 return ret;
159
160 // READ CAPACITY returns the address of the last block.
161 // We do not bother with READ CAPACITY(16) because BIOS does not support
162 // 64-bit LBA anyway.
Kevin O'Connorb3064592012-08-14 21:20:10 -0400163 drive->blksize = be32_to_cpu(capdata.blksize);
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500164 if (drive->blksize != DISK_SECTOR_SIZE) {
165 dprintf(1, "%s: unsupported block size %d\n", s, drive->blksize);
166 return -1;
167 }
Kevin O'Connorb3064592012-08-14 21:20:10 -0400168 drive->sectors = (u64)be32_to_cpu(capdata.sectors) + 1;
Paolo Bonzinided04a32011-11-17 10:23:02 +0100169 dprintf(1, "%s blksize=%d sectors=%d\n"
170 , s, drive->blksize, (unsigned)drive->sectors);
171
Paolo Bonzinicb721712012-03-05 12:29:12 +0100172 // We do not recover from USB stalls, so try to be safe and avoid
173 // sending the command if the (obsolete, but still provided by QEMU)
174 // fixed disk geometry page may not be supported.
175 //
176 // We could also send the command only to small disks (e.g. <504MiB)
177 // but some old USB keys only support a very small subset of SCSI which
178 // does not even include the MODE SENSE command!
179 //
Kevin O'Connor897fb112013-02-07 23:32:48 -0500180 if (CONFIG_QEMU_HARDWARE && memcmp(vendor, "QEMU", 5) == 0) {
Paolo Bonzinicb721712012-03-05 12:29:12 +0100181 struct cdbres_mode_sense_geom geomdata;
182 ret = cdb_mode_sense_geom(&dop, &geomdata);
183 if (ret == 0) {
184 u32 cylinders;
185 cylinders = geomdata.cyl[0] << 16;
186 cylinders |= geomdata.cyl[1] << 8;
187 cylinders |= geomdata.cyl[2];
188 if (cylinders && geomdata.heads &&
189 drive->sectors <= 0xFFFFFFFFULL &&
190 ((u32)drive->sectors % (geomdata.heads * cylinders) == 0)) {
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400191 drive->pchs.cylinder = cylinders;
192 drive->pchs.head = geomdata.heads;
193 drive->pchs.sector = (u32)drive->sectors / (geomdata.heads * cylinders);
Paolo Bonzinicb721712012-03-05 12:29:12 +0100194 }
Paolo Bonzini000e0842011-11-16 13:02:54 +0100195 }
196 }
197
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500198 char *desc = znprintf(MAXDESCSIZE, "%s Drive %s %s %s"
199 , s, vendor, product, rev);
200 boot_add_hd(drive, desc, prio);
Paolo Bonzinided04a32011-11-17 10:23:02 +0100201 return 0;
202}
203
Paolo Bonzini8c976e32011-11-16 13:02:51 +0100204int
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500205cdb_get_inquiry(struct disk_op_s *op, struct cdbres_inquiry *data)
206{
207 struct cdb_request_sense cmd;
208 memset(&cmd, 0, sizeof(cmd));
209 cmd.command = CDB_CMD_INQUIRY;
210 cmd.length = sizeof(*data);
211 op->count = 1;
212 op->buf_fl = data;
213 return cdb_cmd_data(op, &cmd, sizeof(*data));
214}
215
Kevin O'Connor76977b22010-02-17 01:01:32 -0500216// Request SENSE
217int
218cdb_get_sense(struct disk_op_s *op, struct cdbres_request_sense *data)
219{
220 struct cdb_request_sense cmd;
221 memset(&cmd, 0, sizeof(cmd));
222 cmd.command = CDB_CMD_REQUEST_SENSE;
223 cmd.length = sizeof(*data);
224 op->count = 1;
225 op->buf_fl = data;
226 return cdb_cmd_data(op, &cmd, sizeof(*data));
227}
228
Paolo Bonzini00823742011-11-16 13:02:42 +0100229// Test unit ready
230int
231cdb_test_unit_ready(struct disk_op_s *op)
232{
233 struct cdb_request_sense cmd;
234 memset(&cmd, 0, sizeof(cmd));
235 cmd.command = CDB_CMD_TEST_UNIT_READY;
236 op->count = 0;
237 op->buf_fl = NULL;
238 return cdb_cmd_data(op, &cmd, 0);
239}
240
Kevin O'Connor76977b22010-02-17 01:01:32 -0500241// Request capacity
242int
243cdb_read_capacity(struct disk_op_s *op, struct cdbres_read_capacity *data)
244{
245 struct cdb_read_capacity cmd;
246 memset(&cmd, 0, sizeof(cmd));
247 cmd.command = CDB_CMD_READ_CAPACITY;
248 op->count = 1;
249 op->buf_fl = data;
250 return cdb_cmd_data(op, &cmd, sizeof(*data));
251}
252
Paolo Bonzini000e0842011-11-16 13:02:54 +0100253// Mode sense, geometry page.
254int
255cdb_mode_sense_geom(struct disk_op_s *op, struct cdbres_mode_sense_geom *data)
256{
257 struct cdb_mode_sense cmd;
258 memset(&cmd, 0, sizeof(cmd));
259 cmd.command = CDB_CMD_MODE_SENSE;
260 cmd.flags = 8; /* DBD */
261 cmd.page = MODE_PAGE_HD_GEOMETRY;
Kevin O'Connorb3064592012-08-14 21:20:10 -0400262 cmd.count = cpu_to_be16(sizeof(*data));
Paolo Bonzini000e0842011-11-16 13:02:54 +0100263 op->count = 1;
264 op->buf_fl = data;
265 return cdb_cmd_data(op, &cmd, sizeof(*data));
266}
267
Kevin O'Connor76977b22010-02-17 01:01:32 -0500268// Read sectors.
269int
270cdb_read(struct disk_op_s *op)
271{
272 struct cdb_rwdata_10 cmd;
273 memset(&cmd, 0, sizeof(cmd));
274 cmd.command = CDB_CMD_READ_10;
Kevin O'Connorb3064592012-08-14 21:20:10 -0400275 cmd.lba = cpu_to_be32(op->lba);
276 cmd.count = cpu_to_be16(op->count);
Kevin O'Connor1902c942013-10-26 11:48:06 -0400277 return cdb_cmd_data(op, &cmd, GET_GLOBALFLAT(op->drive_gf->blksize));
Kevin O'Connor76977b22010-02-17 01:01:32 -0500278}
Paolo Bonziniddb8ceb2011-11-16 13:02:47 +0100279
280// Write sectors.
281int
282cdb_write(struct disk_op_s *op)
283{
284 struct cdb_rwdata_10 cmd;
285 memset(&cmd, 0, sizeof(cmd));
286 cmd.command = CDB_CMD_WRITE_10;
Kevin O'Connorb3064592012-08-14 21:20:10 -0400287 cmd.lba = cpu_to_be32(op->lba);
288 cmd.count = cpu_to_be16(op->count);
Kevin O'Connor1902c942013-10-26 11:48:06 -0400289 return cdb_cmd_data(op, &cmd, GET_GLOBALFLAT(op->drive_gf->blksize));
Paolo Bonziniddb8ceb2011-11-16 13:02:47 +0100290}