Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / hw / blockcmd.c
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 "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
25
26 // Route command to low-level handler.
27 static int
28 cdb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
29 {
30     u8 type = GET_GLOBALFLAT(op->drive_gf->type);
31     switch (type) {
32     case DTYPE_ATA_ATAPI:
33         return atapi_cmd_data(op, cdbcmd, blocksize);
34     case DTYPE_USB:
35         return usb_cmd_data(op, cdbcmd, blocksize);
36     case DTYPE_UAS:
37         return uas_cmd_data(op, cdbcmd, blocksize);
38     case DTYPE_VIRTIO_SCSI:
39         return virtio_scsi_cmd_data(op, cdbcmd, blocksize);
40     case DTYPE_LSI_SCSI:
41         return lsi_scsi_cmd_data(op, cdbcmd, blocksize);
42     case DTYPE_ESP_SCSI:
43         return esp_scsi_cmd_data(op, cdbcmd, blocksize);
44     case DTYPE_MEGASAS:
45         return megasas_cmd_data(op, cdbcmd, blocksize);
46     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);
52     case DTYPE_PVSCSI:
53         if (!MODESEGMENT)
54             return pvscsi_cmd_data(op, cdbcmd, blocksize);
55     case DTYPE_AHCI_ATAPI:
56         if (!MODESEGMENT)
57             return ahci_cmd_data(op, cdbcmd, blocksize);
58     default:
59         return DISK_RET_EPARAM;
60     }
61 }
62
63 // Determine if the command is a request to pull data from the device
64 int
65 cdb_is_read(u8 *cdbcmd, u16 blocksize)
66 {
67     return blocksize && cdbcmd[0] != CDB_CMD_WRITE_10;
68 }
69
70
71 /****************************************************************
72  * Low level command requests
73  ****************************************************************/
74
75 static int
76 cdb_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
88 static int
89 cdb_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
101 static int
102 cdb_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
113 static int
114 cdb_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.
125 static int
126 cdb_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.
140 static int
141 cdb_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.
152 static int
153 cdb_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
168 int VISIBLE32FLAT
169 scsi_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
187 int
188 scsi_is_ready(struct disk_op_s *op)
189 {
190     dprintf(6, "scsi_is_ready (drive=%p)\n", op->drive_gf);
191
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;
196     u32 end = timer_calc(5000);
197     for (;;) {
198         if (timer_check(end)) {
199             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 */
224             end = timer_calc(30000);
225             in_progress = 1;
226         }
227     }
228     return 0;
229 }
230
231 // Validate drive, find block size / sector count, and register drive.
232 int
233 scsi_drive_setup(struct drive_s *drive, const char *s, int prio)
234 {
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);
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);
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;
255
256     if (pdt == SCSI_TYPE_CDROM) {
257         drive->blksize = CDROM_SECTOR_SIZE;
258         drive->sectors = (u64)-1;
259
260         char *desc = znprintf(MAXDESCSIZE, "DVD/CD [%s Drive %s %s %s]"
261                               , s, vendor, product, rev);
262         boot_add_cd(drive, desc, prio);
263         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.
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);
283         return -1;
284     }
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);
288
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.
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     //
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);
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)) {
308                 drive->pchs.cylinder = cylinders;
309                 drive->pchs.head = geomdata.heads;
310                 drive->pchs.sector = (u32)drive->sectors / (geomdata.heads * cylinders);
311             }
312         }
313     }
314
315     char *desc = znprintf(MAXDESCSIZE, "%s Drive %s %s %s"
316                           , s, vendor, product, rev);
317     boot_add_hd(drive, desc, prio);
318     return 0;
319 }