blob: ad8814c3b2bcf9482eb9650f39a60f80e4d6000d [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'Connorfeeb1c22014-12-29 09:40:46 -050070
71/****************************************************************
72 * Low level command requests
73 ****************************************************************/
74
75int
76cdb_get_inquiry(struct disk_op_s *op, struct cdbres_inquiry *data)
77{
78 struct cdb_request_sense cmd;
79 memset(&cmd, 0, sizeof(cmd));
80 cmd.command = CDB_CMD_INQUIRY;
81 cmd.length = sizeof(*data);
82 op->count = 1;
83 op->buf_fl = data;
84 return cdb_cmd_data(op, &cmd, sizeof(*data));
85}
86
87// Request SENSE
88int
89cdb_get_sense(struct disk_op_s *op, struct cdbres_request_sense *data)
90{
91 struct cdb_request_sense cmd;
92 memset(&cmd, 0, sizeof(cmd));
93 cmd.command = CDB_CMD_REQUEST_SENSE;
94 cmd.length = sizeof(*data);
95 op->count = 1;
96 op->buf_fl = data;
97 return cdb_cmd_data(op, &cmd, sizeof(*data));
98}
99
100// Test unit ready
101int
102cdb_test_unit_ready(struct disk_op_s *op)
103{
104 struct cdb_request_sense cmd;
105 memset(&cmd, 0, sizeof(cmd));
106 cmd.command = CDB_CMD_TEST_UNIT_READY;
107 op->count = 0;
108 op->buf_fl = NULL;
109 return cdb_cmd_data(op, &cmd, 0);
110}
111
112// Request capacity
113int
114cdb_read_capacity(struct disk_op_s *op, struct cdbres_read_capacity *data)
115{
116 struct cdb_read_capacity cmd;
117 memset(&cmd, 0, sizeof(cmd));
118 cmd.command = CDB_CMD_READ_CAPACITY;
119 op->count = 1;
120 op->buf_fl = data;
121 return cdb_cmd_data(op, &cmd, sizeof(*data));
122}
123
124// Mode sense, geometry page.
125int
126cdb_mode_sense_geom(struct disk_op_s *op, struct cdbres_mode_sense_geom *data)
127{
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));
134 op->count = 1;
135 op->buf_fl = data;
136 return cdb_cmd_data(op, &cmd, sizeof(*data));
137}
138
139// Read sectors.
140int
141cdb_read(struct disk_op_s *op)
142{
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));
149}
150
151// Write sectors.
152int
153cdb_write(struct disk_op_s *op)
154{
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));
161}
162
163
164/****************************************************************
165 * Main SCSI commands
166 ****************************************************************/
167
Kevin O'Connorf0a22eb2014-12-29 09:45:15 -0500168int VISIBLE32FLAT
169scsi_process_op(struct disk_op_s *op)
170{
171 switch (op->command) {
172 case CMD_READ:
173 return cdb_read(op);
174 case CMD_WRITE:
175 return cdb_write(op);
176 case CMD_FORMAT:
177 case CMD_RESET:
178 case CMD_ISREADY:
179 case CMD_VERIFY:
180 case CMD_SEEK:
181 return DISK_RET_SUCCESS;
182 default:
183 return DISK_RET_EPARAM;
184 }
185}
186
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500187int
Paolo Bonzini8c976e32011-11-16 13:02:51 +0100188scsi_is_ready(struct disk_op_s *op)
189{
Kevin O'Connor1902c942013-10-26 11:48:06 -0400190 dprintf(6, "scsi_is_ready (drive=%p)\n", op->drive_gf);
Paolo Bonzini8c976e32011-11-16 13:02:51 +0100191
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. */
195 int in_progress = 0;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400196 u32 end = timer_calc(5000);
Paolo Bonzini8c976e32011-11-16 13:02:51 +0100197 for (;;) {
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400198 if (timer_check(end)) {
Paolo Bonzini8c976e32011-11-16 13:02:51 +0100199 dprintf(1, "test unit ready failed\n");
200 return -1;
201 }
202
203 int ret = cdb_test_unit_ready(op);
204 if (!ret)
205 // Success
206 break;
207
208 struct cdbres_request_sense sense;
209 ret = cdb_get_sense(op, &sense);
210 if (ret)
211 // Error - retry.
212 continue;
213
214 // Sense succeeded.
215 if (sense.asc == 0x3a) { /* MEDIUM NOT PRESENT */
216 dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
217 return -1;
218 }
219
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 */
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400224 end = timer_calc(30000);
Paolo Bonzini8c976e32011-11-16 13:02:51 +0100225 in_progress = 1;
226 }
227 }
228 return 0;
229}
230
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500231// Validate drive, find block size / sector count, and register drive.
Paolo Bonzinided04a32011-11-17 10:23:02 +0100232int
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500233scsi_drive_setup(struct drive_s *drive, const char *s, int prio)
Paolo Bonzinided04a32011-11-17 10:23:02 +0100234{
Paolo Bonzinided04a32011-11-17 10:23:02 +0100235 struct disk_op_s dop;
236 memset(&dop, 0, sizeof(dop));
Kevin O'Connor1902c942013-10-26 11:48:06 -0400237 dop.drive_gf = drive;
Paolo Bonzinided04a32011-11-17 10:23:02 +0100238 struct cdbres_inquiry data;
239 int ret = cdb_get_inquiry(&dop, &data);
240 if (ret)
241 return ret;
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);
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500250 int pdt = data.pdt & 0x1f;
Paolo Bonzinided04a32011-11-17 10:23:02 +0100251 int removable = !!(data.removable & 0x80);
252 dprintf(1, "%s vendor='%s' product='%s' rev='%s' type=%d removable=%d\n"
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500253 , s, vendor, product, rev, pdt, removable);
Paolo Bonzinided04a32011-11-17 10:23:02 +0100254 drive->removable = removable;
255
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500256 if (pdt == SCSI_TYPE_CDROM) {
Paolo Bonzinided04a32011-11-17 10:23:02 +0100257 drive->blksize = CDROM_SECTOR_SIZE;
258 drive->sectors = (u64)-1;
259
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500260 char *desc = znprintf(MAXDESCSIZE, "DVD/CD [%s Drive %s %s %s]"
Paolo Bonzinided04a32011-11-17 10:23:02 +0100261 , s, vendor, product, rev);
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500262 boot_add_cd(drive, desc, prio);
Paolo Bonzinided04a32011-11-17 10:23:02 +0100263 return 0;
264 }
265
266 ret = scsi_is_ready(&dop);
267 if (ret) {
268 dprintf(1, "scsi_is_ready returned %d\n", ret);
269 return ret;
270 }
271
272 struct cdbres_read_capacity capdata;
273 ret = cdb_read_capacity(&dop, &capdata);
274 if (ret)
275 return ret;
276
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.
Kevin O'Connorb3064592012-08-14 21:20:10 -0400280 drive->blksize = be32_to_cpu(capdata.blksize);
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500281 if (drive->blksize != DISK_SECTOR_SIZE) {
282 dprintf(1, "%s: unsupported block size %d\n", s, drive->blksize);
283 return -1;
284 }
Kevin O'Connorb3064592012-08-14 21:20:10 -0400285 drive->sectors = (u64)be32_to_cpu(capdata.sectors) + 1;
Paolo Bonzinided04a32011-11-17 10:23:02 +0100286 dprintf(1, "%s blksize=%d sectors=%d\n"
287 , s, drive->blksize, (unsigned)drive->sectors);
288
Paolo Bonzinicb721712012-03-05 12:29:12 +0100289 // 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.
292 //
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!
296 //
Kevin O'Connor897fb112013-02-07 23:32:48 -0500297 if (CONFIG_QEMU_HARDWARE && memcmp(vendor, "QEMU", 5) == 0) {
Paolo Bonzinicb721712012-03-05 12:29:12 +0100298 struct cdbres_mode_sense_geom geomdata;
299 ret = cdb_mode_sense_geom(&dop, &geomdata);
300 if (ret == 0) {
301 u32 cylinders;
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)) {
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400308 drive->pchs.cylinder = cylinders;
309 drive->pchs.head = geomdata.heads;
310 drive->pchs.sector = (u32)drive->sectors / (geomdata.heads * cylinders);
Paolo Bonzinicb721712012-03-05 12:29:12 +0100311 }
Paolo Bonzini000e0842011-11-16 13:02:54 +0100312 }
313 }
314
Kevin O'Connor279dcb12012-02-18 11:02:27 -0500315 char *desc = znprintf(MAXDESCSIZE, "%s Drive %s %s %s"
316 , s, vendor, product, rev);
317 boot_add_hd(drive, desc, prio);
Paolo Bonzinided04a32011-11-17 10:23:02 +0100318 return 0;
319}