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         // PA-RISC: Beware alignment: do not write u64 to unaligned address.
115         struct cdb_rwdata_10 cmd;
116         memset(cdbcmd, 0, maxcdb);
117         memset(&cmd, 0, sizeof(cmd));
118         cmd.command = (op->command == CMD_READ ? CDB_CMD_READ_10
119                         : CDB_CMD_WRITE_10);
120         cmd.lba = cpu_to_be32(op->lba);
121         cmd.count = cpu_to_be16(op->count);
122         memcpy(cdbcmd, &cmd, sizeof(cmd));
123         return GET_FLATPTR(op->drive_fl->blksize);
124     case CMD_SCSI:
125         if (MODESEGMENT)
126             return -1;
127         memcpy(cdbcmd, op->cdbcmd, maxcdb);
128         return op->blocksize;
129     default:
130         return -1;
131     }
132 }
133 
134 // Determine if the command is a request to pull data from the device
135 int
scsi_is_read(struct disk_op_s * op)136 scsi_is_read(struct disk_op_s *op)
137 {
138     return op->command == CMD_READ || (
139         !MODESEGMENT && op->command == CMD_SCSI && op->blocksize);
140 }
141 
142 // Check if a SCSI device is ready to receive commands
143 int
scsi_is_ready(struct disk_op_s * op)144 scsi_is_ready(struct disk_op_s *op)
145 {
146     ASSERT32FLAT();
147     dprintf(6, "scsi_is_ready (drive=%p)\n", op->drive_fl);
148 
149     /* Retry TEST UNIT READY for 5 seconds unless MEDIUM NOT PRESENT is
150      * reported by the device 3 times.  If the device reports "IN PROGRESS",
151      * 30 seconds is added. */
152     int tries = 3;
153     int in_progress = 0;
154     u32 end = timer_calc(5000);
155     for (;;) {
156         if (timer_check(end)) {
157             dprintf(1, "test unit ready failed\n");
158             return -1;
159         }
160 
161         int ret = cdb_test_unit_ready(op);
162         if (!ret)
163             // Success
164             break;
165 
166         struct cdbres_request_sense sense;
167         ret = cdb_get_sense(op, &sense);
168         if (ret)
169             // Error - retry.
170             continue;
171 
172         // Sense succeeded.
173         if (sense.asc == 0x3a) { /* MEDIUM NOT PRESENT */
174             tries--;
175             dprintf(1, "Device reports MEDIUM NOT PRESENT - %d tries left\n",
176                 tries);
177             if (!tries)
178                 return -1;
179         }
180 
181         if (sense.asc == 0x04 && sense.ascq == 0x01 && !in_progress) {
182             /* IN PROGRESS OF BECOMING READY */
183             dprintf(1, "Waiting for device to detect medium... ");
184             /* Allow 30 seconds more */
185             end = timer_calc(30000);
186             in_progress = 1;
187         }
188     }
189     return 0;
190 }
191 
192 #define CDB_CMD_REPORT_LUNS  0xA0
193 
194 struct cdb_report_luns {
195     u8 command;
196     u8 reserved_01[5];
197     u32 length;
198     u8 pad[6];
199 } PACKED;
200 
201 struct scsi_lun {
202     u16 lun[4];
203 };
204 
205 struct cdbres_report_luns {
206     u32 length;
207     u32 reserved;
208     struct scsi_lun luns[];
209 };
210 
scsilun2u64(struct scsi_lun * scsi_lun)211 static u64 scsilun2u64(struct scsi_lun *scsi_lun)
212 {
213     int i;
214     u64 ret = 0;
215     for (i = 0; i < ARRAY_SIZE(scsi_lun->lun); i++)
216         ret |= be16_to_cpu(scsi_lun->lun[i]) << (16 * i);
217     return ret;
218 }
219 
220 // Issue REPORT LUNS on a temporary drive and iterate reported luns calling
221 // @add_lun for each
scsi_rep_luns_scan(struct drive_s * tmp_drive,scsi_add_lun add_lun)222 int scsi_rep_luns_scan(struct drive_s *tmp_drive, scsi_add_lun add_lun)
223 {
224     int ret = -1;
225     /* start with the smallest possible buffer, otherwise some devices in QEMU
226      * may (incorrectly) error out on returning less data than fits in it */
227     u32 maxluns = 1;
228     u32 nluns, i;
229     struct cdb_report_luns cdb = {
230         .command = CDB_CMD_REPORT_LUNS,
231     };
232     struct disk_op_s op = {
233         .drive_fl = tmp_drive,
234         .command = CMD_SCSI,
235         .count = 1,
236         .cdbcmd = &cdb,
237     };
238     struct cdbres_report_luns *resp;
239 
240     ASSERT32FLAT();
241 
242     while (1) {
243         op.blocksize = sizeof(struct cdbres_report_luns) +
244             maxluns * sizeof(struct scsi_lun);
245         op.buf_fl = malloc_tmp(op.blocksize);
246         if (!op.buf_fl) {
247             warn_noalloc();
248             return -1;
249         }
250 
251         cdb.length = cpu_to_be32(op.blocksize);
252         if (process_op(&op) != DISK_RET_SUCCESS)
253             goto out;
254 
255         resp = op.buf_fl;
256         nluns = be32_to_cpu(resp->length) / sizeof(struct scsi_lun);
257         if (nluns <= maxluns)
258             break;
259 
260         free(op.buf_fl);
261         maxluns = nluns;
262     }
263 
264     for (i = 0, ret = 0; i < nluns; i++) {
265         u64 lun = scsilun2u64(&resp->luns[i]);
266         if (lun >> 32)
267             continue;
268         ret += !add_lun((u32)lun, tmp_drive);
269     }
270 out:
271     free(op.buf_fl);
272     return ret;
273 }
274 
275 // 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)276 int scsi_sequential_scan(struct drive_s *tmp_drive, u32 maxluns,
277                          scsi_add_lun add_lun)
278 {
279     int ret;
280     u32 lun;
281 
282     for (lun = 0, ret = 0; lun < maxluns; lun++)
283         ret += !add_lun(lun, tmp_drive);
284     return ret;
285 }
286 
287 // Validate drive, find block size / sector count, and register drive.
288 int
scsi_drive_setup(struct drive_s * drive,const char * s,int prio,u8 target,u8 lun)289 scsi_drive_setup(struct drive_s *drive, const char *s, int prio, u8 target, u8 lun)
290 {
291     ASSERT32FLAT();
292     drive->target = target;
293     drive->lun = lun;
294     struct disk_op_s dop;
295     memset(&dop, 0, sizeof(dop));
296     dop.drive_fl = drive;
297     struct cdbres_inquiry data;
298     int ret = cdb_get_inquiry(&dop, &data);
299     if (ret)
300         return ret;
301     char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1];
302     char rev[sizeof(data.rev)+1];
303     strtcpy(vendor, data.vendor, sizeof(vendor));
304     nullTrailingSpace(vendor);
305     strtcpy(product, data.product, sizeof(product));
306     nullTrailingSpace(product);
307     strtcpy(rev, data.rev, sizeof(rev));
308     nullTrailingSpace(rev);
309     int pdt = data.pdt & 0x1f;
310     int removable = !!(data.removable & 0x80);
311     dprintf(1, "%s vendor='%s' product='%s' rev='%s' type=%d removable=%d\n"
312             , s, vendor, product, rev, pdt, removable);
313     drive->removable = removable;
314 
315     if (pdt == SCSI_TYPE_CDROM) {
316         drive->blksize = CDROM_SECTOR_SIZE;
317         drive->sectors = (u64)-1;
318 
319         char *desc = znprintf(MAXDESCSIZE, "DVD/CD [%s Drive %s %s %s]"
320                               , s, vendor, product, rev);
321         boot_add_cd(drive, desc, prio);
322         return 0;
323     }
324 
325     if (pdt != SCSI_TYPE_DISK)
326         return -1;
327 
328     ret = scsi_is_ready(&dop);
329     if (ret) {
330         dprintf(1, "scsi_is_ready returned %d\n", ret);
331         return ret;
332     }
333 
334     struct cdbres_read_capacity capdata;
335     ret = cdb_read_capacity(&dop, &capdata);
336     if (ret)
337         return ret;
338 
339     // READ CAPACITY returns the address of the last block.
340     // We do not bother with READ CAPACITY(16) because BIOS does not support
341     // 64-bit LBA anyway.
342     drive->blksize = be32_to_cpu(capdata.blksize);
343     if (drive->blksize != DISK_SECTOR_SIZE) {
344         dprintf(1, "%s: unsupported block size %d\n", s, drive->blksize);
345         return -1;
346     }
347     drive->sectors = (u64)be32_to_cpu(capdata.sectors) + 1;
348     dprintf(1, "%s blksize=%d sectors=%u\n"
349             , s, drive->blksize, (unsigned)drive->sectors);
350 
351     // We do not recover from USB stalls, so try to be safe and avoid
352     // sending the command if the (obsolete, but still provided by QEMU)
353     // fixed disk geometry page may not be supported.
354     //
355     // We could also send the command only to small disks (e.g. <504MiB)
356     // but some old USB keys only support a very small subset of SCSI which
357     // does not even include the MODE SENSE command!
358     //
359     if (CONFIG_QEMU_HARDWARE && memcmp(vendor, "QEMU", 5) == 0) {
360         struct cdbres_mode_sense_geom geomdata;
361         ret = cdb_mode_sense_geom(&dop, &geomdata);
362         if (ret == 0) {
363             u32 cylinders;
364             cylinders = geomdata.cyl[0] << 16;
365             cylinders |= geomdata.cyl[1] << 8;
366             cylinders |= geomdata.cyl[2];
367             if (cylinders && geomdata.heads &&
368                 drive->sectors <= 0xFFFFFFFFULL &&
369                 ((u32)drive->sectors % (geomdata.heads * cylinders) == 0)) {
370                 drive->pchs.cylinder = cylinders;
371                 drive->pchs.head = geomdata.heads;
372                 drive->pchs.sector = (u32)drive->sectors / (geomdata.heads * cylinders);
373             }
374         }
375     }
376 
377     char *desc = znprintf(MAXDESCSIZE, "%s Drive %s %s %s"
378                           , s, vendor, product, rev);
379     boot_add_hd(drive, desc, prio);
380     return 0;
381 }
382