1 // 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
8 #include "block.h" // struct disk_op_s
9 #include "blockcmd.h" // struct cdb_request_sense
10 #include "byteorder.h" // be32_to_cpu
11 #include "farptr.h" // GET_FLATPTR
12 #include "output.h" // dprintf
13 #include "std/disk.h" // DISK_RET_EPARAM
14 #include "string.h" // memset
15 #include "util.h" // timer_calc
16 #include "malloc.h"
17
18
19 /****************************************************************
20 * Low level command requests
21 ****************************************************************/
22
23 static int
cdb_get_inquiry(struct disk_op_s * op,struct cdbres_inquiry * data)24 cdb_get_inquiry(struct disk_op_s *op, struct cdbres_inquiry *data)
25 {
26 struct cdb_request_sense cmd;
27 memset(&cmd, 0, sizeof(cmd));
28 cmd.command = CDB_CMD_INQUIRY;
29 cmd.length = sizeof(*data);
30 op->command = CMD_SCSI;
31 op->count = 1;
32 op->buf_fl = data;
33 op->cdbcmd = &cmd;
34 op->blocksize = sizeof(*data);
35 return process_op(op);
36 }
37
38 // Request SENSE
39 static int
cdb_get_sense(struct disk_op_s * op,struct cdbres_request_sense * data)40 cdb_get_sense(struct disk_op_s *op, struct cdbres_request_sense *data)
41 {
42 struct cdb_request_sense cmd;
43 memset(&cmd, 0, sizeof(cmd));
44 cmd.command = CDB_CMD_REQUEST_SENSE;
45 cmd.length = sizeof(*data);
46 op->command = CMD_SCSI;
47 op->count = 1;
48 op->buf_fl = data;
49 op->cdbcmd = &cmd;
50 op->blocksize = sizeof(*data);
51 return process_op(op);
52 }
53
54 // Test unit ready
55 static int
cdb_test_unit_ready(struct disk_op_s * op)56 cdb_test_unit_ready(struct disk_op_s *op)
57 {
58 struct cdb_request_sense cmd;
59 memset(&cmd, 0, sizeof(cmd));
60 cmd.command = CDB_CMD_TEST_UNIT_READY;
61 op->command = CMD_SCSI;
62 op->count = 0;
63 op->buf_fl = NULL;
64 op->cdbcmd = &cmd;
65 op->blocksize = 0;
66 return process_op(op);
67 }
68
69 // Request capacity
70 static int
cdb_read_capacity(struct disk_op_s * op,struct cdbres_read_capacity * data)71 cdb_read_capacity(struct disk_op_s *op, struct cdbres_read_capacity *data)
72 {
73 struct cdb_read_capacity cmd;
74 memset(&cmd, 0, sizeof(cmd));
75 cmd.command = CDB_CMD_READ_CAPACITY;
76 op->command = CMD_SCSI;
77 op->count = 1;
78 op->buf_fl = data;
79 op->cdbcmd = &cmd;
80 op->blocksize = sizeof(*data);
81 return process_op(op);
82 }
83
84 // Mode sense, geometry page.
85 static int
cdb_mode_sense_geom(struct disk_op_s * op,struct cdbres_mode_sense_geom * data)86 cdb_mode_sense_geom(struct disk_op_s *op, struct cdbres_mode_sense_geom *data)
87 {
88 struct cdb_mode_sense cmd;
89 memset(&cmd, 0, sizeof(cmd));
90 cmd.command = CDB_CMD_MODE_SENSE;
91 cmd.flags = 8; /* DBD */
92 cmd.page = MODE_PAGE_HD_GEOMETRY;
93 cmd.count = cpu_to_be16(sizeof(*data));
94 op->command = CMD_SCSI;
95 op->count = 1;
96 op->buf_fl = data;
97 op->cdbcmd = &cmd;
98 op->blocksize = sizeof(*data);
99 return process_op(op);
100 }
101
102
103 /****************************************************************
104 * Main SCSI commands
105 ****************************************************************/
106
107 // Create a scsi command request from a disk_op_s request
108 int
scsi_fill_cmd(struct disk_op_s * op,void * cdbcmd,int maxcdb)109 scsi_fill_cmd(struct disk_op_s *op, void *cdbcmd, int maxcdb)
110 {
111 switch (op->command) {
112 case CMD_READ:
113 case CMD_WRITE: ;
114 struct cdb_rwdata_10 *cmd = cdbcmd;
115 memset(cmd, 0, maxcdb);
116 cmd->command = (op->command == CMD_READ ? CDB_CMD_READ_10
117 : CDB_CMD_WRITE_10);
118 cmd->lba = cpu_to_be32(op->lba);
119 cmd->count = cpu_to_be16(op->count);
120 return GET_FLATPTR(op->drive_fl->blksize);
121 case CMD_SCSI:
122 if (MODESEGMENT)
123 return -1;
124 memcpy(cdbcmd, op->cdbcmd, maxcdb);
125 return op->blocksize;
126 default:
127 return -1;
128 }
129 }
130
131 // Determine if the command is a request to pull data from the device
132 int
scsi_is_read(struct disk_op_s * op)133 scsi_is_read(struct disk_op_s *op)
134 {
135 return op->command == CMD_READ || (
136 !MODESEGMENT && op->command == CMD_SCSI && op->blocksize);
137 }
138
139 // Check if a SCSI device is ready to receive commands
140 int
scsi_is_ready(struct disk_op_s * op)141 scsi_is_ready(struct disk_op_s *op)
142 {
143 ASSERT32FLAT();
144 dprintf(6, "scsi_is_ready (drive=%p)\n", op->drive_fl);
145
146 /* Retry TEST UNIT READY for 5 seconds unless MEDIUM NOT PRESENT is
147 * reported by the device 3 times. If the device reports "IN PROGRESS",
148 * 30 seconds is added. */
149 int tries = 3;
150 int in_progress = 0;
151 u32 end = timer_calc(5000);
152 for (;;) {
153 if (timer_check(end)) {
154 dprintf(1, "test unit ready failed\n");
155 return -1;
156 }
157
158 int ret = cdb_test_unit_ready(op);
159 if (!ret)
160 // Success
161 break;
162
163 struct cdbres_request_sense sense;
164 ret = cdb_get_sense(op, &sense);
165 if (ret)
166 // Error - retry.
167 continue;
168
169 // Sense succeeded.
170 if (sense.asc == 0x3a) { /* MEDIUM NOT PRESENT */
171 tries--;
172 dprintf(1, "Device reports MEDIUM NOT PRESENT - %d tries left\n",
173 tries);
174 if (!tries)
175 return -1;
176 }
177
178 if (sense.asc == 0x04 && sense.ascq == 0x01 && !in_progress) {
179 /* IN PROGRESS OF BECOMING READY */
180 dprintf(1, "Waiting for device to detect medium... ");
181 /* Allow 30 seconds more */
182 end = timer_calc(30000);
183 in_progress = 1;
184 }
185 }
186 return 0;
187 }
188
189 #define CDB_CMD_REPORT_LUNS 0xA0
190
191 struct cdb_report_luns {
192 u8 command;
193 u8 reserved_01[5];
194 u32 length;
195 u8 pad[6];
196 } PACKED;
197
198 struct scsi_lun {
199 u16 lun[4];
200 };
201
202 struct cdbres_report_luns {
203 u32 length;
204 u32 reserved;
205 struct scsi_lun luns[];
206 };
207
scsilun2u64(struct scsi_lun * scsi_lun)208 static u64 scsilun2u64(struct scsi_lun *scsi_lun)
209 {
210 int i;
211 u64 ret = 0;
212 for (i = 0; i < ARRAY_SIZE(scsi_lun->lun); i++)
213 ret |= be16_to_cpu(scsi_lun->lun[i]) << (16 * i);
214 return ret;
215 }
216
217 // Issue REPORT LUNS on a temporary drive and iterate reported luns calling
218 // @add_lun for each
scsi_rep_luns_scan(struct drive_s * tmp_drive,scsi_add_lun add_lun)219 int scsi_rep_luns_scan(struct drive_s *tmp_drive, scsi_add_lun add_lun)
220 {
221 int ret = -1;
222 /* start with the smallest possible buffer, otherwise some devices in QEMU
223 * may (incorrectly) error out on returning less data than fits in it */
224 u32 maxluns = 1;
225 u32 nluns, i;
226 struct cdb_report_luns cdb = {
227 .command = CDB_CMD_REPORT_LUNS,
228 };
229 struct disk_op_s op = {
230 .drive_fl = tmp_drive,
231 .command = CMD_SCSI,
232 .count = 1,
233 .cdbcmd = &cdb,
234 };
235 struct cdbres_report_luns *resp;
236
237 ASSERT32FLAT();
238
239 while (1) {
240 op.blocksize = sizeof(struct cdbres_report_luns) +
241 maxluns * sizeof(struct scsi_lun);
242 op.buf_fl = malloc_tmp(op.blocksize);
243 if (!op.buf_fl) {
244 warn_noalloc();
245 return -1;
246 }
247
248 cdb.length = cpu_to_be32(op.blocksize);
249 if (process_op(&op) != DISK_RET_SUCCESS)
250 goto out;
251
252 resp = op.buf_fl;
253 nluns = be32_to_cpu(resp->length) / sizeof(struct scsi_lun);
254 if (nluns <= maxluns)
255 break;
256
257 free(op.buf_fl);
258 maxluns = nluns;
259 }
260
261 for (i = 0, ret = 0; i < nluns; i++) {
262 u64 lun = scsilun2u64(&resp->luns[i]);
263 if (lun >> 32)
264 continue;
265 ret += !add_lun((u32)lun, tmp_drive);
266 }
267 out:
268 free(op.buf_fl);
269 return ret;
270 }
271
272 // Iterate LUNs on the target and call @add_lun for each
scsi_sequential_scan(struct drive_s * tmp_drive,u32 maxluns,scsi_add_lun add_lun)273 int scsi_sequential_scan(struct drive_s *tmp_drive, u32 maxluns,
274 scsi_add_lun add_lun)
275 {
276 int ret;
277 u32 lun;
278
279 for (lun = 0, ret = 0; lun < maxluns; lun++)
280 ret += !add_lun(lun, tmp_drive);
281 return ret;
282 }
283
284 // Validate drive, find block size / sector count, and register drive.
285 int
scsi_drive_setup(struct drive_s * drive,const char * s,int prio)286 scsi_drive_setup(struct drive_s *drive, const char *s, int prio)
287 {
288 ASSERT32FLAT();
289 struct disk_op_s dop;
290 memset(&dop, 0, sizeof(dop));
291 dop.drive_fl = drive;
292 struct cdbres_inquiry data;
293 int ret = cdb_get_inquiry(&dop, &data);
294 if (ret)
295 return ret;
296 char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1];
297 char rev[sizeof(data.rev)+1];
298 strtcpy(vendor, data.vendor, sizeof(vendor));
299 nullTrailingSpace(vendor);
300 strtcpy(product, data.product, sizeof(product));
301 nullTrailingSpace(product);
302 strtcpy(rev, data.rev, sizeof(rev));
303 nullTrailingSpace(rev);
304 int pdt = data.pdt & 0x1f;
305 int removable = !!(data.removable & 0x80);
306 dprintf(1, "%s vendor='%s' product='%s' rev='%s' type=%d removable=%d\n"
307 , s, vendor, product, rev, pdt, removable);
308 drive->removable = removable;
309
310 if (pdt == SCSI_TYPE_CDROM) {
311 drive->blksize = CDROM_SECTOR_SIZE;
312 drive->sectors = (u64)-1;
313
314 char *desc = znprintf(MAXDESCSIZE, "DVD/CD [%s Drive %s %s %s]"
315 , s, vendor, product, rev);
316 boot_add_cd(drive, desc, prio);
317 return 0;
318 }
319
320 if (pdt != SCSI_TYPE_DISK)
321 return -1;
322
323 ret = scsi_is_ready(&dop);
324 if (ret) {
325 dprintf(1, "scsi_is_ready returned %d\n", ret);
326 return ret;
327 }
328
329 struct cdbres_read_capacity capdata;
330 ret = cdb_read_capacity(&dop, &capdata);
331 if (ret)
332 return ret;
333
334 // READ CAPACITY returns the address of the last block.
335 // We do not bother with READ CAPACITY(16) because BIOS does not support
336 // 64-bit LBA anyway.
337 drive->blksize = be32_to_cpu(capdata.blksize);
338 if (drive->blksize != DISK_SECTOR_SIZE) {
339 dprintf(1, "%s: unsupported block size %d\n", s, drive->blksize);
340 return -1;
341 }
342 drive->sectors = (u64)be32_to_cpu(capdata.sectors) + 1;
343 dprintf(1, "%s blksize=%d sectors=%u\n"
344 , s, drive->blksize, (unsigned)drive->sectors);
345
346 // We do not recover from USB stalls, so try to be safe and avoid
347 // sending the command if the (obsolete, but still provided by QEMU)
348 // fixed disk geometry page may not be supported.
349 //
350 // We could also send the command only to small disks (e.g. <504MiB)
351 // but some old USB keys only support a very small subset of SCSI which
352 // does not even include the MODE SENSE command!
353 //
354 if (CONFIG_QEMU_HARDWARE && memcmp(vendor, "QEMU", 5) == 0) {
355 struct cdbres_mode_sense_geom geomdata;
356 ret = cdb_mode_sense_geom(&dop, &geomdata);
357 if (ret == 0) {
358 u32 cylinders;
359 cylinders = geomdata.cyl[0] << 16;
360 cylinders |= geomdata.cyl[1] << 8;
361 cylinders |= geomdata.cyl[2];
362 if (cylinders && geomdata.heads &&
363 drive->sectors <= 0xFFFFFFFFULL &&
364 ((u32)drive->sectors % (geomdata.heads * cylinders) == 0)) {
365 drive->pchs.cylinder = cylinders;
366 drive->pchs.head = geomdata.heads;
367 drive->pchs.sector = (u32)drive->sectors / (geomdata.heads * cylinders);
368 }
369 }
370 }
371
372 char *desc = znprintf(MAXDESCSIZE, "%s Drive %s %s %s"
373 , s, vendor, product, rev);
374 boot_add_hd(drive, desc, prio);
375 return 0;
376 }
377