/src/qemu/hw/scsi/scsi-disk.c
Line | Count | Source |
1 | | /* |
2 | | * SCSI Device emulation |
3 | | * |
4 | | * Copyright (c) 2006 CodeSourcery. |
5 | | * Based on code by Fabrice Bellard |
6 | | * |
7 | | * Written by Paul Brook |
8 | | * Modifications: |
9 | | * 2009-Dec-12 Artyom Tarasenko : implemented stamdard inquiry for the case |
10 | | * when the allocation length of CDB is smaller |
11 | | * than 36. |
12 | | * 2009-Oct-13 Artyom Tarasenko : implemented the block descriptor in the |
13 | | * MODE SENSE response. |
14 | | * |
15 | | * This code is licensed under the LGPL. |
16 | | * |
17 | | * Note that this file only handles the SCSI architecture model and device |
18 | | * commands. Emulation of interface/link layer protocols is handled by |
19 | | * the host adapter emulator. |
20 | | */ |
21 | | |
22 | | #include "qemu/osdep.h" |
23 | | #include "qemu/units.h" |
24 | | #include "qapi/error.h" |
25 | | #include "qemu/error-report.h" |
26 | | #include "qemu/main-loop.h" |
27 | | #include "qemu/module.h" |
28 | | #include "qemu/hw-version.h" |
29 | | #include "qemu/memalign.h" |
30 | | #include "qemu/target-info.h" |
31 | | #include "hw/scsi/scsi.h" |
32 | | #include "migration/misc.h" |
33 | | #include "migration/qemu-file-types.h" |
34 | | #include "migration/vmstate.h" |
35 | | #include "hw/scsi/emulation.h" |
36 | | #include "scsi/constants.h" |
37 | | #include "system/block-backend.h" |
38 | | #include "system/blockdev.h" |
39 | | #include "hw/block/block.h" |
40 | | #include "hw/core/qdev-properties.h" |
41 | | #include "hw/core/qdev-properties-system.h" |
42 | | #include "system/dma.h" |
43 | | #include "system/system.h" |
44 | | #include "qemu/cutils.h" |
45 | | #include "trace.h" |
46 | | #include "qom/object.h" |
47 | | |
48 | | #ifdef __linux |
49 | | #include <scsi/sg.h> |
50 | | #endif |
51 | | |
52 | | #define SCSI_WRITE_SAME_MAX (512 * KiB) |
53 | 51.2k | #define SCSI_DMA_BUF_SIZE (128 * KiB) |
54 | 3.02k | #define SCSI_MAX_INQUIRY_LEN 256 |
55 | 5.09k | #define SCSI_MAX_MODE_LEN 256 |
56 | | |
57 | | #define DEFAULT_DISCARD_GRANULARITY (4 * KiB) |
58 | | #define DEFAULT_MAX_UNMAP_SIZE (1 * GiB) |
59 | | #define DEFAULT_MAX_IO_SIZE INT_MAX /* 2 GB - 1 block */ |
60 | | |
61 | | #define TYPE_SCSI_DISK_BASE "scsi-disk-base" |
62 | | |
63 | 0 | #define MAX_SERIAL_LEN 36 |
64 | 0 | #define MAX_SERIAL_LEN_FOR_DEVID 20 |
65 | | |
66 | | OBJECT_DECLARE_TYPE(SCSIDiskState, SCSIDiskClass, SCSI_DISK_BASE) |
67 | | |
68 | | struct SCSIDiskClass { |
69 | | SCSIDeviceClass parent_class; |
70 | | /* |
71 | | * Callbacks receive ret == 0 for success. Errors are represented either as |
72 | | * negative errno values, or as positive SAM status codes. For host_status |
73 | | * errors, the function passes ret == -ENODEV and sets the host_status field |
74 | | * of the SCSIRequest. |
75 | | */ |
76 | | DMAIOFunc *dma_readv; |
77 | | DMAIOFunc *dma_writev; |
78 | | bool (*need_fua)(SCSICommand *cmd); |
79 | | void (*update_sense)(SCSIRequest *r); |
80 | | }; |
81 | | |
82 | | typedef struct SCSIDiskReq { |
83 | | SCSIRequest req; |
84 | | /* Both sector and sector_count are in terms of BDRV_SECTOR_SIZE bytes. */ |
85 | | uint64_t sector; |
86 | | uint32_t sector_count; |
87 | | uint32_t buflen; |
88 | | bool started; |
89 | | bool need_fua; |
90 | | struct iovec iov; |
91 | | QEMUIOVector qiov; |
92 | | BlockAcctCookie acct; |
93 | | } SCSIDiskReq; |
94 | | |
95 | 2.73k | #define SCSI_DISK_F_REMOVABLE 0 |
96 | 4.17k | #define SCSI_DISK_F_DPOFUA 1 |
97 | 3 | #define SCSI_DISK_F_NO_REMOVABLE_DEVOPS 2 |
98 | | |
99 | | struct SCSIDiskState { |
100 | | SCSIDevice qdev; |
101 | | uint32_t features; |
102 | | bool media_changed; |
103 | | bool media_event; |
104 | | bool eject_request; |
105 | | uint16_t port_index; |
106 | | uint64_t max_unmap_size; |
107 | | uint64_t max_io_size; |
108 | | uint32_t quirks; |
109 | | char *version; |
110 | | char *serial; |
111 | | char *vendor; |
112 | | char *product; |
113 | | char *device_id; |
114 | | char *loadparm; /* only for s390x */ |
115 | | bool tray_open; |
116 | | bool tray_locked; |
117 | | /* |
118 | | * 0x0000 - rotation rate not reported |
119 | | * 0x0001 - non-rotating medium (SSD) |
120 | | * 0x0002-0x0400 - reserved |
121 | | * 0x0401-0xfffe - rotations per minute |
122 | | * 0xffff - reserved |
123 | | */ |
124 | | uint16_t rotation_rate; |
125 | | bool migrate_emulated_scsi_request; |
126 | | NotifierWithReturn migration_notifier; |
127 | | }; |
128 | | |
129 | | static void scsi_free_request(SCSIRequest *req) |
130 | 196k | { |
131 | 196k | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
132 | | |
133 | 196k | qemu_vfree(r->iov.iov_base); |
134 | 196k | } |
135 | | |
136 | | /* Helper function for command completion with sense. */ |
137 | | static void scsi_check_condition(SCSIDiskReq *r, SCSISense sense) |
138 | 104k | { |
139 | 104k | trace_scsi_disk_check_condition(r->req.tag, sense.key, sense.asc, |
140 | 104k | sense.ascq); |
141 | 104k | scsi_req_build_sense(&r->req, sense); |
142 | 104k | scsi_req_complete(&r->req, CHECK_CONDITION); |
143 | 104k | } |
144 | | |
145 | | static void scsi_init_iovec(SCSIDiskReq *r, size_t size) |
146 | 51.2k | { |
147 | 51.2k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
148 | | |
149 | 51.2k | if (!r->iov.iov_base) { |
150 | 3.47k | r->buflen = size; |
151 | 3.47k | r->iov.iov_base = blk_blockalign(s->qdev.conf.blk, r->buflen); |
152 | 3.47k | } |
153 | 51.2k | r->iov.iov_len = MIN(r->sector_count * BDRV_SECTOR_SIZE, r->buflen); |
154 | 51.2k | qemu_iovec_init_external(&r->qiov, &r->iov, 1); |
155 | 51.2k | } |
156 | | |
157 | | static void scsi_disk_save_request(QEMUFile *f, SCSIRequest *req) |
158 | 0 | { |
159 | 0 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
160 | |
|
161 | 0 | qemu_put_be64s(f, &r->sector); |
162 | 0 | qemu_put_be32s(f, &r->sector_count); |
163 | 0 | qemu_put_be32s(f, &r->buflen); |
164 | 0 | if (r->buflen) { |
165 | 0 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
166 | 0 | qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len); |
167 | 0 | } else if (!req->retry) { |
168 | 0 | uint32_t len = r->iov.iov_len; |
169 | 0 | qemu_put_be32s(f, &len); |
170 | 0 | qemu_put_buffer(f, r->iov.iov_base, r->iov.iov_len); |
171 | 0 | } |
172 | 0 | } |
173 | 0 | } |
174 | | |
175 | | static void scsi_disk_emulate_save_request(QEMUFile *f, SCSIRequest *req) |
176 | 0 | { |
177 | 0 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
178 | |
|
179 | 0 | if (s->migrate_emulated_scsi_request) { |
180 | 0 | scsi_disk_save_request(f, req); |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | | static void scsi_disk_load_request(QEMUFile *f, SCSIRequest *req) |
185 | 0 | { |
186 | 0 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
187 | |
|
188 | 0 | qemu_get_be64s(f, &r->sector); |
189 | 0 | qemu_get_be32s(f, &r->sector_count); |
190 | 0 | qemu_get_be32s(f, &r->buflen); |
191 | 0 | if (r->buflen) { |
192 | 0 | scsi_init_iovec(r, r->buflen); |
193 | 0 | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
194 | 0 | qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len); |
195 | 0 | } else if (!r->req.retry) { |
196 | 0 | uint32_t len; |
197 | 0 | qemu_get_be32s(f, &len); |
198 | 0 | r->iov.iov_len = len; |
199 | 0 | assert(r->iov.iov_len <= r->buflen); |
200 | 0 | qemu_get_buffer(f, r->iov.iov_base, r->iov.iov_len); |
201 | 0 | } |
202 | 0 | } |
203 | |
|
204 | 0 | qemu_iovec_init_external(&r->qiov, &r->iov, 1); |
205 | 0 | } |
206 | | |
207 | | static void scsi_disk_emulate_load_request(QEMUFile *f, SCSIRequest *req) |
208 | 0 | { |
209 | 0 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
210 | |
|
211 | 0 | if (s->migrate_emulated_scsi_request) { |
212 | 0 | scsi_disk_load_request(f, req); |
213 | 0 | } |
214 | 0 | } |
215 | | |
216 | | /* |
217 | | * scsi_handle_rw_error has two return values. False means that the error |
218 | | * must be ignored, true means that the error has been processed and the |
219 | | * caller should not do anything else for this request. Note that |
220 | | * scsi_handle_rw_error always manages its reference counts, independent |
221 | | * of the return value. |
222 | | */ |
223 | | static bool scsi_handle_rw_error(SCSIDiskReq *r, int ret, bool acct_failed) |
224 | 0 | { |
225 | 0 | bool is_read = (r->req.cmd.mode == SCSI_XFER_FROM_DEV); |
226 | 0 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
227 | 0 | SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s)); |
228 | 0 | SCSISense sense = SENSE_CODE(NO_SENSE); |
229 | 0 | int16_t host_status; |
230 | 0 | int error; |
231 | 0 | bool req_has_sense = false; |
232 | 0 | BlockErrorAction action; |
233 | 0 | int status; |
234 | | |
235 | | /* |
236 | | * host_status should only be set for SG_IO requests that came back with a |
237 | | * host_status error in scsi_block_sgio_complete(). This error path passes |
238 | | * -ENODEV as the return value. |
239 | | * |
240 | | * Reset host_status in the request because we may still want to complete |
241 | | * the request successfully with the 'stop' or 'ignore' error policy. |
242 | | */ |
243 | 0 | host_status = r->req.host_status; |
244 | 0 | if (host_status != -1) { |
245 | 0 | assert(ret == -ENODEV); |
246 | 0 | r->req.host_status = -1; |
247 | 0 | } |
248 | |
|
249 | 0 | if (ret < 0) { |
250 | 0 | status = scsi_sense_from_errno(-ret, &sense); |
251 | 0 | error = -ret; |
252 | 0 | } else { |
253 | | /* A passthrough command has completed with nonzero status. */ |
254 | 0 | status = ret; |
255 | 0 | switch (status) { |
256 | 0 | case CHECK_CONDITION: |
257 | 0 | req_has_sense = true; |
258 | 0 | error = scsi_sense_buf_to_errno(r->req.sense, sizeof(r->req.sense)); |
259 | 0 | break; |
260 | 0 | case RESERVATION_CONFLICT: |
261 | | /* |
262 | | * Don't apply the error policy, always report to the guest. |
263 | | * |
264 | | * This is a passthrough code path, so it's not a backend error, but |
265 | | * a response to an invalid guest request. |
266 | | * |
267 | | * Windows Failover Cluster validation intentionally sends invalid |
268 | | * requests to verify that reservations work as intended. It is |
269 | | * crucial that it sees the resulting errors. |
270 | | * |
271 | | * Treating a reservation conflict as a guest-side error is obvious |
272 | | * when a pr-manager is in use. Without one, the situation is less |
273 | | * clear, but there might be nothing that can be fixed on the host |
274 | | * (like in the above example), and we don't want to be stuck in a |
275 | | * loop where resuming the VM and retrying the request immediately |
276 | | * stops it again. So always reporting is still the safer option in |
277 | | * this case, too. |
278 | | */ |
279 | 0 | error = 0; |
280 | 0 | break; |
281 | 0 | default: |
282 | 0 | error = EINVAL; |
283 | 0 | break; |
284 | 0 | } |
285 | 0 | } |
286 | | |
287 | | /* |
288 | | * Check whether the error has to be handled by the guest or should |
289 | | * rather follow the rerror=/werror= settings. Guest-handled errors |
290 | | * are usually retried immediately, so do not post them to QMP and |
291 | | * do not account them as failed I/O. |
292 | | */ |
293 | 0 | if (!error || (req_has_sense && |
294 | 0 | scsi_sense_buf_is_guest_recoverable(r->req.sense, |
295 | 0 | sizeof(r->req.sense)))) { |
296 | 0 | action = BLOCK_ERROR_ACTION_REPORT; |
297 | 0 | acct_failed = false; |
298 | 0 | } else { |
299 | 0 | action = blk_get_error_action(s->qdev.conf.blk, is_read, error); |
300 | 0 | blk_error_action(s->qdev.conf.blk, action, is_read, error); |
301 | 0 | } |
302 | |
|
303 | 0 | switch (action) { |
304 | 0 | case BLOCK_ERROR_ACTION_REPORT: |
305 | 0 | if (acct_failed) { |
306 | 0 | block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); |
307 | 0 | } |
308 | 0 | if (host_status != -1) { |
309 | 0 | scsi_req_complete_failed(&r->req, host_status); |
310 | 0 | return true; |
311 | 0 | } |
312 | 0 | if (req_has_sense) { |
313 | 0 | sdc->update_sense(&r->req); |
314 | 0 | } else if (status == CHECK_CONDITION) { |
315 | 0 | scsi_req_build_sense(&r->req, sense); |
316 | 0 | } |
317 | 0 | scsi_req_complete(&r->req, status); |
318 | 0 | return true; |
319 | | |
320 | 0 | case BLOCK_ERROR_ACTION_IGNORE: |
321 | | /* |
322 | | * The caller may have already finalized the accounting cookie |
323 | | * (e.g. scsi_dma_complete() for ret < 0), but calling |
324 | | * block_acct_done() afterwards is fine (just a no-op). Just |
325 | | * make sure all cookies are indeed accounted at some point. |
326 | | */ |
327 | 0 | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); |
328 | 0 | return false; |
329 | | |
330 | 0 | case BLOCK_ERROR_ACTION_STOP: |
331 | 0 | scsi_req_retry(&r->req); |
332 | 0 | return true; |
333 | | |
334 | 0 | default: |
335 | 0 | g_assert_not_reached(); |
336 | 0 | } |
337 | 0 | } |
338 | | |
339 | | static bool scsi_disk_req_check_error(SCSIDiskReq *r, int ret, bool acct_failed) |
340 | 654k | { |
341 | 654k | if (r->req.io_canceled) { |
342 | 2.91k | scsi_req_cancel_complete(&r->req); |
343 | 2.91k | return true; |
344 | 2.91k | } |
345 | | |
346 | 651k | if (ret != 0) { |
347 | 0 | return scsi_handle_rw_error(r, ret, acct_failed); |
348 | 0 | } |
349 | | |
350 | 651k | return false; |
351 | 651k | } |
352 | | |
353 | | static void scsi_aio_complete(void *opaque, int ret) |
354 | 1.25k | { |
355 | 1.25k | SCSIDiskReq *r = (SCSIDiskReq *)opaque; |
356 | 1.25k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
357 | | |
358 | | /* The request must run in its AioContext */ |
359 | 1.25k | assert(r->req.ctx == qemu_get_current_aio_context()); |
360 | | |
361 | 1.25k | assert(r->req.aiocb != NULL); |
362 | 1.25k | r->req.aiocb = NULL; |
363 | | |
364 | 1.25k | if (scsi_disk_req_check_error(r, ret, true)) { |
365 | 0 | goto done; |
366 | 0 | } |
367 | | |
368 | 1.25k | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); |
369 | 1.25k | scsi_req_complete(&r->req, GOOD); |
370 | | |
371 | 1.25k | done: |
372 | 1.25k | scsi_req_unref(&r->req); |
373 | 1.25k | } |
374 | | |
375 | | static bool scsi_is_cmd_fua(SCSICommand *cmd) |
376 | 11.5k | { |
377 | 11.5k | switch (cmd->buf[0]) { |
378 | 594 | case READ_10: |
379 | 2.13k | case READ_12: |
380 | 2.60k | case READ_16: |
381 | 6.32k | case WRITE_10: |
382 | 6.33k | case WRITE_12: |
383 | 6.83k | case WRITE_16: |
384 | 6.83k | return (cmd->buf[1] & 8) != 0; |
385 | | |
386 | 0 | case VERIFY_10: |
387 | 0 | case VERIFY_12: |
388 | 0 | case VERIFY_16: |
389 | 416 | case WRITE_VERIFY_10: |
390 | 2.07k | case WRITE_VERIFY_12: |
391 | 2.08k | case WRITE_VERIFY_16: |
392 | 2.08k | return true; |
393 | | |
394 | 678 | case READ_6: |
395 | 2.60k | case WRITE_6: |
396 | 2.60k | default: |
397 | 2.60k | return false; |
398 | 11.5k | } |
399 | 11.5k | } |
400 | | |
401 | | static void scsi_dma_complete_noio(SCSIDiskReq *r, int ret) |
402 | 5.41k | { |
403 | 5.41k | assert(r->req.aiocb == NULL); |
404 | 5.41k | if (scsi_disk_req_check_error(r, ret, ret > 0)) { |
405 | 0 | goto done; |
406 | 0 | } |
407 | | |
408 | 5.41k | r->sector += r->sector_count; |
409 | 5.41k | r->sector_count = 0; |
410 | 5.41k | scsi_req_complete(&r->req, GOOD); |
411 | | |
412 | 5.41k | done: |
413 | 5.41k | scsi_req_unref(&r->req); |
414 | 5.41k | } |
415 | | |
416 | | static void scsi_dma_complete(void *opaque, int ret) |
417 | 5.41k | { |
418 | 5.41k | SCSIDiskReq *r = (SCSIDiskReq *)opaque; |
419 | 5.41k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
420 | | |
421 | 5.41k | assert(r->req.aiocb != NULL); |
422 | 5.41k | r->req.aiocb = NULL; |
423 | | |
424 | | /* ret > 0 is accounted for in scsi_disk_req_check_error() */ |
425 | 5.41k | if (ret < 0) { |
426 | 0 | block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); |
427 | 5.41k | } else if (ret == 0) { |
428 | 5.41k | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); |
429 | 5.41k | } |
430 | 5.41k | scsi_dma_complete_noio(r, ret); |
431 | 5.41k | } |
432 | | |
433 | | static void scsi_read_complete_noio(SCSIDiskReq *r, int ret) |
434 | 17.3k | { |
435 | 17.3k | uint32_t n; |
436 | | |
437 | | /* The request must run in its AioContext */ |
438 | 17.3k | assert(r->req.ctx == qemu_get_current_aio_context()); |
439 | | |
440 | 17.3k | assert(r->req.aiocb == NULL); |
441 | 17.3k | if (scsi_disk_req_check_error(r, ret, ret > 0)) { |
442 | 116 | goto done; |
443 | 116 | } |
444 | | |
445 | 17.2k | n = r->qiov.size / BDRV_SECTOR_SIZE; |
446 | 17.2k | r->sector += n; |
447 | 17.2k | r->sector_count -= n; |
448 | 17.2k | scsi_req_data(&r->req, r->qiov.size); |
449 | | |
450 | 17.3k | done: |
451 | 17.3k | scsi_req_unref(&r->req); |
452 | 17.3k | } |
453 | | |
454 | | static void scsi_read_complete(void *opaque, int ret) |
455 | 17.3k | { |
456 | 17.3k | SCSIDiskReq *r = (SCSIDiskReq *)opaque; |
457 | 17.3k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
458 | | |
459 | 17.3k | assert(r->req.aiocb != NULL); |
460 | 17.3k | r->req.aiocb = NULL; |
461 | | |
462 | | /* ret > 0 is accounted for in scsi_disk_req_check_error() */ |
463 | 17.3k | if (ret < 0) { |
464 | 0 | block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); |
465 | 17.3k | } else if (ret == 0) { |
466 | 17.3k | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); |
467 | 17.3k | trace_scsi_disk_read_complete(r->req.tag, r->qiov.size); |
468 | 17.3k | } |
469 | 17.3k | scsi_read_complete_noio(r, ret); |
470 | 17.3k | } |
471 | | |
472 | | /* Actually issue a read to the block device. */ |
473 | | static void scsi_do_read(SCSIDiskReq *r, int ret) |
474 | 17.3k | { |
475 | 17.3k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
476 | 17.3k | SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s)); |
477 | | |
478 | 17.3k | assert (r->req.aiocb == NULL); |
479 | 17.3k | if (scsi_disk_req_check_error(r, ret, false)) { |
480 | 0 | goto done; |
481 | 0 | } |
482 | | |
483 | | /* The request is used as the AIO opaque value, so add a ref. */ |
484 | 17.3k | scsi_req_ref(&r->req); |
485 | | |
486 | 17.3k | if (r->req.sg) { |
487 | 0 | dma_acct_start(s->qdev.conf.blk, &r->acct, r->req.sg, BLOCK_ACCT_READ); |
488 | 0 | r->req.residual -= r->req.sg->size; |
489 | 0 | r->req.aiocb = dma_blk_io(r->req.sg, r->sector << BDRV_SECTOR_BITS, |
490 | 0 | BDRV_SECTOR_SIZE, |
491 | 0 | sdc->dma_readv, r, scsi_dma_complete, r, |
492 | 0 | DMA_DIRECTION_FROM_DEVICE); |
493 | 17.3k | } else { |
494 | 17.3k | scsi_init_iovec(r, SCSI_DMA_BUF_SIZE); |
495 | 17.3k | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, |
496 | 17.3k | r->qiov.size, BLOCK_ACCT_READ); |
497 | 17.3k | r->req.aiocb = sdc->dma_readv(r->sector << BDRV_SECTOR_BITS, &r->qiov, |
498 | 17.3k | scsi_read_complete, r, r); |
499 | 17.3k | } |
500 | | |
501 | 17.3k | done: |
502 | 17.3k | scsi_req_unref(&r->req); |
503 | 17.3k | } |
504 | | |
505 | | static void scsi_do_read_cb(void *opaque, int ret) |
506 | 883 | { |
507 | 883 | SCSIDiskReq *r = (SCSIDiskReq *)opaque; |
508 | 883 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
509 | | |
510 | 883 | assert (r->req.aiocb != NULL); |
511 | 883 | r->req.aiocb = NULL; |
512 | | |
513 | 883 | if (ret < 0) { |
514 | 0 | block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); |
515 | 883 | } else { |
516 | 883 | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); |
517 | 883 | } |
518 | 883 | scsi_do_read(opaque, ret); |
519 | 883 | } |
520 | | |
521 | | /* Read more data from scsi device into buffer. */ |
522 | | static void scsi_read_data(SCSIRequest *req) |
523 | 17.9k | { |
524 | 17.9k | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
525 | 17.9k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
526 | 17.9k | bool first; |
527 | | |
528 | 17.9k | trace_scsi_disk_read_data_count(r->sector_count); |
529 | 17.9k | if (r->sector_count == 0) { |
530 | | /* This also clears the sense buffer for REQUEST SENSE. */ |
531 | 558 | scsi_req_complete(&r->req, GOOD); |
532 | 558 | return; |
533 | 558 | } |
534 | | |
535 | | /* No data transfer may already be in progress */ |
536 | 17.9k | assert(r->req.aiocb == NULL); |
537 | | |
538 | | /* The request is used as the AIO opaque value, so add a ref. */ |
539 | 17.3k | scsi_req_ref(&r->req); |
540 | 17.3k | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
541 | 0 | trace_scsi_disk_read_data_invalid(); |
542 | 0 | scsi_read_complete_noio(r, -EINVAL); |
543 | 0 | return; |
544 | 0 | } |
545 | | |
546 | 17.3k | if (!blk_is_available(req->dev->conf.blk)) { |
547 | 0 | scsi_read_complete_noio(r, -ENOMEDIUM); |
548 | 0 | return; |
549 | 0 | } |
550 | | |
551 | 17.3k | first = !r->started; |
552 | 17.3k | r->started = true; |
553 | 17.3k | if (first && r->need_fua) { |
554 | 883 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0, |
555 | 883 | BLOCK_ACCT_FLUSH); |
556 | 883 | r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_do_read_cb, r); |
557 | 16.4k | } else { |
558 | 16.4k | scsi_do_read(r, 0); |
559 | 16.4k | } |
560 | 17.3k | } |
561 | | |
562 | | static void scsi_write_complete_noio(SCSIDiskReq *r, int ret) |
563 | 34.0k | { |
564 | 34.0k | uint32_t n; |
565 | | |
566 | | /* The request must run in its AioContext */ |
567 | 34.0k | assert(r->req.ctx == qemu_get_current_aio_context()); |
568 | | |
569 | 34.0k | assert (r->req.aiocb == NULL); |
570 | 34.0k | if (scsi_disk_req_check_error(r, ret, ret > 0)) { |
571 | 66 | goto done; |
572 | 66 | } |
573 | | |
574 | 33.9k | n = r->qiov.size / BDRV_SECTOR_SIZE; |
575 | 33.9k | r->sector += n; |
576 | 33.9k | r->sector_count -= n; |
577 | 33.9k | if (r->sector_count == 0) { |
578 | 66 | scsi_req_complete(&r->req, GOOD); |
579 | 33.9k | } else { |
580 | 33.9k | scsi_init_iovec(r, SCSI_DMA_BUF_SIZE); |
581 | 33.9k | trace_scsi_disk_write_complete_noio(r->req.tag, r->qiov.size); |
582 | 33.9k | scsi_req_data(&r->req, r->qiov.size); |
583 | 33.9k | } |
584 | | |
585 | 34.0k | done: |
586 | 34.0k | scsi_req_unref(&r->req); |
587 | 34.0k | } |
588 | | |
589 | | static void scsi_write_complete(void * opaque, int ret) |
590 | 32.4k | { |
591 | 32.4k | SCSIDiskReq *r = (SCSIDiskReq *)opaque; |
592 | 32.4k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
593 | | |
594 | 32.4k | assert (r->req.aiocb != NULL); |
595 | 32.4k | r->req.aiocb = NULL; |
596 | | |
597 | | /* ret > 0 is accounted for in scsi_disk_req_check_error() */ |
598 | 32.4k | if (ret < 0) { |
599 | 0 | block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); |
600 | 32.4k | } else if (ret == 0) { |
601 | 32.4k | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); |
602 | 32.4k | } |
603 | 32.4k | scsi_write_complete_noio(r, ret); |
604 | 32.4k | } |
605 | | |
606 | | static void scsi_write_data(SCSIRequest *req) |
607 | 39.4k | { |
608 | 39.4k | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
609 | 39.4k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
610 | 39.4k | SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s)); |
611 | 39.4k | BlockCompletionFunc *cb; |
612 | | |
613 | | /* No data transfer may already be in progress */ |
614 | 39.4k | assert(r->req.aiocb == NULL); |
615 | | |
616 | | /* The request is used as the AIO opaque value, so add a ref. */ |
617 | 39.4k | scsi_req_ref(&r->req); |
618 | 39.4k | if (r->req.cmd.mode != SCSI_XFER_TO_DEV) { |
619 | 0 | trace_scsi_disk_write_data_invalid(); |
620 | 0 | scsi_write_complete_noio(r, -EINVAL); |
621 | 0 | return; |
622 | 0 | } |
623 | | |
624 | 39.4k | if (!r->req.sg && !r->qiov.size) { |
625 | | /* Called for the first time. Ask the driver to send us more data. */ |
626 | 1.60k | r->started = true; |
627 | 1.60k | scsi_write_complete_noio(r, 0); |
628 | 1.60k | return; |
629 | 1.60k | } |
630 | 37.8k | if (!blk_is_available(req->dev->conf.blk)) { |
631 | 0 | scsi_write_complete_noio(r, -ENOMEDIUM); |
632 | 0 | return; |
633 | 0 | } |
634 | | |
635 | 37.8k | if (r->req.cmd.buf[0] == VERIFY_10 || r->req.cmd.buf[0] == VERIFY_12 || |
636 | 37.8k | r->req.cmd.buf[0] == VERIFY_16) { |
637 | 0 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0, |
638 | 0 | BLOCK_ACCT_FLUSH); |
639 | 0 | cb = r->req.sg ? scsi_dma_complete : scsi_write_complete; |
640 | 0 | r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, cb, r); |
641 | 0 | return; |
642 | 0 | } |
643 | | |
644 | 37.8k | if (r->req.sg) { |
645 | 5.41k | dma_acct_start(s->qdev.conf.blk, &r->acct, r->req.sg, BLOCK_ACCT_WRITE); |
646 | 5.41k | r->req.residual -= r->req.sg->size; |
647 | 5.41k | r->req.aiocb = dma_blk_io(r->req.sg, r->sector << BDRV_SECTOR_BITS, |
648 | 5.41k | BDRV_SECTOR_SIZE, |
649 | 5.41k | sdc->dma_writev, r, scsi_dma_complete, r, |
650 | 5.41k | DMA_DIRECTION_TO_DEVICE); |
651 | 32.4k | } else { |
652 | 32.4k | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, |
653 | 32.4k | r->qiov.size, BLOCK_ACCT_WRITE); |
654 | 32.4k | r->req.aiocb = sdc->dma_writev(r->sector << BDRV_SECTOR_BITS, &r->qiov, |
655 | 32.4k | scsi_write_complete, r, r); |
656 | 32.4k | } |
657 | 37.8k | } |
658 | | |
659 | | /* Return a pointer to the data buffer. */ |
660 | | static uint8_t *scsi_get_buf(SCSIRequest *req) |
661 | 119k | { |
662 | 119k | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
663 | | |
664 | 119k | return (uint8_t *)r->iov.iov_base; |
665 | 119k | } |
666 | | |
667 | | static int scsi_disk_emulate_vpd_page(SCSIRequest *req, uint8_t *outbuf) |
668 | 1.63k | { |
669 | 1.63k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
670 | 1.63k | uint8_t page_code = req->cmd.buf[2]; |
671 | 1.63k | int start, buflen = 0; |
672 | | |
673 | 1.63k | outbuf[buflen++] = s->qdev.type & 0x1f; |
674 | 1.63k | outbuf[buflen++] = page_code; |
675 | 1.63k | outbuf[buflen++] = 0x00; |
676 | 1.63k | outbuf[buflen++] = 0x00; |
677 | 1.63k | start = buflen; |
678 | | |
679 | 1.63k | switch (page_code) { |
680 | 884 | case 0x00: /* Supported page codes, mandatory */ |
681 | 884 | { |
682 | 884 | trace_scsi_disk_emulate_vpd_page_00(req->cmd.xfer); |
683 | 884 | outbuf[buflen++] = 0x00; /* list of supported pages (this page) */ |
684 | 884 | if (s->serial) { |
685 | 0 | outbuf[buflen++] = 0x80; /* unit serial number */ |
686 | 0 | } |
687 | 884 | outbuf[buflen++] = 0x83; /* device identification */ |
688 | 884 | if (s->qdev.type == TYPE_DISK) { |
689 | 884 | outbuf[buflen++] = 0xb0; /* block limits */ |
690 | 884 | outbuf[buflen++] = 0xb1; /* block device characteristics */ |
691 | 884 | outbuf[buflen++] = 0xb2; /* thin provisioning */ |
692 | 884 | } |
693 | 884 | break; |
694 | 0 | } |
695 | 270 | case 0x80: /* Device serial number, optional */ |
696 | 270 | { |
697 | 270 | int l; |
698 | | |
699 | 270 | if (!s->serial) { |
700 | 270 | trace_scsi_disk_emulate_vpd_page_80_not_supported(); |
701 | 270 | return -1; |
702 | 270 | } |
703 | | |
704 | 0 | l = strlen(s->serial); |
705 | 0 | if (l > MAX_SERIAL_LEN) { |
706 | 0 | l = MAX_SERIAL_LEN; |
707 | 0 | } |
708 | |
|
709 | 0 | trace_scsi_disk_emulate_vpd_page_80(req->cmd.xfer); |
710 | 0 | memcpy(outbuf + buflen, s->serial, l); |
711 | 0 | buflen += l; |
712 | 0 | break; |
713 | 270 | } |
714 | | |
715 | 130 | case 0x83: /* Device identification page, mandatory */ |
716 | 130 | { |
717 | 130 | int id_len = s->device_id ? MIN(strlen(s->device_id), 255 - 8) : 0; |
718 | | |
719 | 130 | trace_scsi_disk_emulate_vpd_page_83(req->cmd.xfer); |
720 | | |
721 | 130 | if (id_len) { |
722 | 130 | outbuf[buflen++] = 0x2; /* ASCII */ |
723 | 130 | outbuf[buflen++] = 0; /* not officially assigned */ |
724 | 130 | outbuf[buflen++] = 0; /* reserved */ |
725 | 130 | outbuf[buflen++] = id_len; /* length of data following */ |
726 | 130 | memcpy(outbuf + buflen, s->device_id, id_len); |
727 | 130 | buflen += id_len; |
728 | 130 | } |
729 | | |
730 | 130 | if (s->qdev.wwn) { |
731 | 0 | outbuf[buflen++] = 0x1; /* Binary */ |
732 | 0 | outbuf[buflen++] = 0x3; /* NAA */ |
733 | 0 | outbuf[buflen++] = 0; /* reserved */ |
734 | 0 | outbuf[buflen++] = 8; |
735 | 0 | stq_be_p(&outbuf[buflen], s->qdev.wwn); |
736 | 0 | buflen += 8; |
737 | 0 | } |
738 | | |
739 | 130 | if (s->qdev.port_wwn) { |
740 | 0 | outbuf[buflen++] = 0x61; /* SAS / Binary */ |
741 | 0 | outbuf[buflen++] = 0x93; /* PIV / Target port / NAA */ |
742 | 0 | outbuf[buflen++] = 0; /* reserved */ |
743 | 0 | outbuf[buflen++] = 8; |
744 | 0 | stq_be_p(&outbuf[buflen], s->qdev.port_wwn); |
745 | 0 | buflen += 8; |
746 | 0 | } |
747 | | |
748 | 130 | if (s->port_index) { |
749 | 0 | outbuf[buflen++] = 0x61; /* SAS / Binary */ |
750 | | |
751 | | /* PIV/Target port/relative target port */ |
752 | 0 | outbuf[buflen++] = 0x94; |
753 | |
|
754 | 0 | outbuf[buflen++] = 0; /* reserved */ |
755 | 0 | outbuf[buflen++] = 4; |
756 | 0 | stw_be_p(&outbuf[buflen + 2], s->port_index); |
757 | 0 | buflen += 4; |
758 | 0 | } |
759 | 130 | break; |
760 | 270 | } |
761 | 0 | case 0xb0: /* block limits */ |
762 | 0 | { |
763 | 0 | SCSIBlockLimits bl = {}; |
764 | |
|
765 | 0 | if (s->qdev.type == TYPE_ROM) { |
766 | 0 | trace_scsi_disk_emulate_vpd_page_b0_not_supported(); |
767 | 0 | return -1; |
768 | 0 | } |
769 | 0 | bl.wsnz = 1; |
770 | 0 | bl.unmap_sectors = |
771 | 0 | s->qdev.conf.discard_granularity / s->qdev.blocksize; |
772 | 0 | bl.min_io_size = |
773 | 0 | s->qdev.conf.min_io_size / s->qdev.blocksize; |
774 | 0 | bl.opt_io_size = |
775 | 0 | s->qdev.conf.opt_io_size / s->qdev.blocksize; |
776 | 0 | bl.max_unmap_sectors = |
777 | 0 | s->max_unmap_size / s->qdev.blocksize; |
778 | 0 | bl.max_io_sectors = |
779 | 0 | s->max_io_size / s->qdev.blocksize; |
780 | | /* 255 descriptors fit in 4 KiB with an 8-byte header */ |
781 | 0 | bl.max_unmap_descr = 255; |
782 | |
|
783 | 0 | if (s->qdev.type == TYPE_DISK) { |
784 | 0 | int max_transfer_blk = blk_get_max_transfer(s->qdev.conf.blk); |
785 | 0 | int max_io_sectors_blk = |
786 | 0 | max_transfer_blk / s->qdev.blocksize; |
787 | |
|
788 | 0 | bl.max_io_sectors = |
789 | 0 | MIN_NON_ZERO(max_io_sectors_blk, bl.max_io_sectors); |
790 | 0 | } |
791 | 0 | buflen += scsi_emulate_block_limits(outbuf + buflen, &bl); |
792 | 0 | break; |
793 | 0 | } |
794 | 44 | case 0xb1: /* block device characteristics */ |
795 | 44 | { |
796 | 44 | buflen = 0x40; |
797 | 44 | outbuf[4] = (s->rotation_rate >> 8) & 0xff; |
798 | 44 | outbuf[5] = s->rotation_rate & 0xff; |
799 | 44 | outbuf[6] = 0; /* PRODUCT TYPE */ |
800 | 44 | outbuf[7] = 0; /* WABEREQ | WACEREQ | NOMINAL FORM FACTOR */ |
801 | 44 | outbuf[8] = 0; /* VBULS */ |
802 | 44 | break; |
803 | 0 | } |
804 | 27 | case 0xb2: /* thin provisioning */ |
805 | 27 | { |
806 | 27 | buflen = 8; |
807 | 27 | outbuf[4] = 0; |
808 | 27 | outbuf[5] = 0xe0; /* unmap & write_same 10/16 all supported */ |
809 | 27 | outbuf[6] = s->qdev.conf.discard_granularity ? 2 : 1; |
810 | 27 | outbuf[7] = 0; |
811 | 27 | break; |
812 | 0 | } |
813 | 284 | default: |
814 | 284 | return -1; |
815 | 1.63k | } |
816 | | /* done with EVPD */ |
817 | 1.63k | assert(buflen - start <= 255); |
818 | 1.08k | outbuf[start - 1] = buflen - start; |
819 | 1.08k | return buflen; |
820 | 1.08k | } |
821 | | |
822 | | static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf) |
823 | 4.31k | { |
824 | 4.31k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
825 | 4.31k | int buflen = 0; |
826 | | |
827 | 4.31k | if (req->cmd.buf[1] & 0x1) { |
828 | | /* Vital product data */ |
829 | 1.63k | return scsi_disk_emulate_vpd_page(req, outbuf); |
830 | 1.63k | } |
831 | | |
832 | | /* Standard INQUIRY data */ |
833 | 2.67k | if (req->cmd.buf[2] != 0) { |
834 | 753 | return -1; |
835 | 753 | } |
836 | | |
837 | | /* PAGE CODE == 0 */ |
838 | 1.91k | buflen = req->cmd.xfer; |
839 | 1.91k | if (buflen > SCSI_MAX_INQUIRY_LEN) { |
840 | 1.10k | buflen = SCSI_MAX_INQUIRY_LEN; |
841 | 1.10k | } |
842 | | |
843 | 1.91k | outbuf[0] = s->qdev.type & 0x1f; |
844 | 1.91k | outbuf[1] = (s->features & (1 << SCSI_DISK_F_REMOVABLE)) ? 0x80 : 0; |
845 | | |
846 | 1.91k | strpadcpy((char *) &outbuf[16], 16, s->product, ' '); |
847 | 1.91k | strpadcpy((char *) &outbuf[8], 8, s->vendor, ' '); |
848 | | |
849 | 1.91k | memset(&outbuf[32], 0, 4); |
850 | 1.91k | memcpy(&outbuf[32], s->version, MIN(4, strlen(s->version))); |
851 | | /* |
852 | | * We claim conformance to SPC-3, which is required for guests |
853 | | * to ask for modern features like READ CAPACITY(16) or the |
854 | | * block characteristics VPD page by default. Not all of SPC-3 |
855 | | * is actually implemented, but we're good enough. |
856 | | */ |
857 | 1.91k | outbuf[2] = s->qdev.default_scsi_version; |
858 | 1.91k | outbuf[3] = 2 | 0x10; /* Format 2, HiSup */ |
859 | | |
860 | 1.91k | if (buflen > 36) { |
861 | 1.10k | outbuf[4] = buflen - 5; /* Additional Length = (Len - 1) - 4 */ |
862 | 1.10k | } else { |
863 | | /* If the allocation length of CDB is too small, |
864 | | the additional length is not adjusted */ |
865 | 815 | outbuf[4] = 36 - 5; |
866 | 815 | } |
867 | | |
868 | | /* Sync data transfer and TCQ. */ |
869 | 1.91k | outbuf[7] = 0x10 | (req->bus->info->tcq ? 0x02 : 0); |
870 | 1.91k | return buflen; |
871 | 2.67k | } |
872 | | |
873 | | static inline bool media_is_dvd(SCSIDiskState *s) |
874 | 0 | { |
875 | 0 | uint64_t nb_sectors; |
876 | 0 | if (s->qdev.type != TYPE_ROM) { |
877 | 0 | return false; |
878 | 0 | } |
879 | 0 | if (!blk_is_available(s->qdev.conf.blk)) { |
880 | 0 | return false; |
881 | 0 | } |
882 | 0 | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
883 | 0 | return nb_sectors > CD_MAX_SECTORS; |
884 | 0 | } |
885 | | |
886 | | static inline bool media_is_cd(SCSIDiskState *s) |
887 | 0 | { |
888 | 0 | uint64_t nb_sectors; |
889 | 0 | if (s->qdev.type != TYPE_ROM) { |
890 | 0 | return false; |
891 | 0 | } |
892 | 0 | if (!blk_is_available(s->qdev.conf.blk)) { |
893 | 0 | return false; |
894 | 0 | } |
895 | 0 | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
896 | 0 | return nb_sectors <= CD_MAX_SECTORS; |
897 | 0 | } |
898 | | |
899 | | static int scsi_read_disc_information(SCSIDiskState *s, SCSIDiskReq *r, |
900 | | uint8_t *outbuf) |
901 | 594 | { |
902 | 594 | uint8_t type = r->req.cmd.buf[1] & 7; |
903 | | |
904 | 594 | if (s->qdev.type != TYPE_ROM) { |
905 | 594 | return -1; |
906 | 594 | } |
907 | | |
908 | | /* Types 1/2 are only defined for Blu-Ray. */ |
909 | 0 | if (type != 0) { |
910 | 0 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
911 | 0 | return -1; |
912 | 0 | } |
913 | | |
914 | 0 | memset(outbuf, 0, 34); |
915 | 0 | outbuf[1] = 32; |
916 | 0 | outbuf[2] = 0xe; /* last session complete, disc finalized */ |
917 | 0 | outbuf[3] = 1; /* first track on disc */ |
918 | 0 | outbuf[4] = 1; /* # of sessions */ |
919 | 0 | outbuf[5] = 1; /* first track of last session */ |
920 | 0 | outbuf[6] = 1; /* last track of last session */ |
921 | 0 | outbuf[7] = 0x20; /* unrestricted use */ |
922 | 0 | outbuf[8] = 0x00; /* CD-ROM or DVD-ROM */ |
923 | | /* 9-10-11: most significant byte corresponding bytes 4-5-6 */ |
924 | | /* 12-23: not meaningful for CD-ROM or DVD-ROM */ |
925 | | /* 24-31: disc bar code */ |
926 | | /* 32: disc application code */ |
927 | | /* 33: number of OPC tables */ |
928 | |
|
929 | 0 | return 34; |
930 | 0 | } |
931 | | |
932 | | static int scsi_read_dvd_structure(SCSIDiskState *s, SCSIDiskReq *r, |
933 | | uint8_t *outbuf) |
934 | 27 | { |
935 | 27 | static const int rds_caps_size[5] = { |
936 | 27 | [0] = 2048 + 4, |
937 | 27 | [1] = 4 + 4, |
938 | 27 | [3] = 188 + 4, |
939 | 27 | [4] = 2048 + 4, |
940 | 27 | }; |
941 | | |
942 | 27 | uint8_t media = r->req.cmd.buf[1]; |
943 | 27 | uint8_t layer = r->req.cmd.buf[6]; |
944 | 27 | uint8_t format = r->req.cmd.buf[7]; |
945 | 27 | int size = -1; |
946 | | |
947 | 27 | if (s->qdev.type != TYPE_ROM) { |
948 | 27 | return -1; |
949 | 27 | } |
950 | 0 | if (media != 0) { |
951 | 0 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
952 | 0 | return -1; |
953 | 0 | } |
954 | | |
955 | 0 | if (format != 0xff) { |
956 | 0 | if (!blk_is_available(s->qdev.conf.blk)) { |
957 | 0 | scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); |
958 | 0 | return -1; |
959 | 0 | } |
960 | 0 | if (media_is_cd(s)) { |
961 | 0 | scsi_check_condition(r, SENSE_CODE(INCOMPATIBLE_FORMAT)); |
962 | 0 | return -1; |
963 | 0 | } |
964 | 0 | if (format >= ARRAY_SIZE(rds_caps_size)) { |
965 | 0 | return -1; |
966 | 0 | } |
967 | 0 | size = rds_caps_size[format]; |
968 | 0 | memset(outbuf, 0, size); |
969 | 0 | } |
970 | | |
971 | 0 | switch (format) { |
972 | 0 | case 0x00: { |
973 | | /* Physical format information */ |
974 | 0 | uint64_t nb_sectors; |
975 | 0 | if (layer != 0) { |
976 | 0 | goto fail; |
977 | 0 | } |
978 | 0 | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
979 | |
|
980 | 0 | outbuf[4] = 1; /* DVD-ROM, part version 1 */ |
981 | 0 | outbuf[5] = 0xf; /* 120mm disc, minimum rate unspecified */ |
982 | 0 | outbuf[6] = 1; /* one layer, read-only (per MMC-2 spec) */ |
983 | 0 | outbuf[7] = 0; /* default densities */ |
984 | |
|
985 | 0 | stl_be_p(&outbuf[12], (nb_sectors >> 2) - 1); /* end sector */ |
986 | 0 | stl_be_p(&outbuf[16], (nb_sectors >> 2) - 1); /* l0 end sector */ |
987 | 0 | break; |
988 | 0 | } |
989 | | |
990 | 0 | case 0x01: /* DVD copyright information, all zeros */ |
991 | 0 | break; |
992 | | |
993 | 0 | case 0x03: /* BCA information - invalid field for no BCA info */ |
994 | 0 | return -1; |
995 | | |
996 | 0 | case 0x04: /* DVD disc manufacturing information, all zeros */ |
997 | 0 | break; |
998 | | |
999 | 0 | case 0xff: { /* List capabilities */ |
1000 | 0 | int i; |
1001 | 0 | size = 4; |
1002 | 0 | for (i = 0; i < ARRAY_SIZE(rds_caps_size); i++) { |
1003 | 0 | if (!rds_caps_size[i]) { |
1004 | 0 | continue; |
1005 | 0 | } |
1006 | 0 | outbuf[size] = i; |
1007 | 0 | outbuf[size + 1] = 0x40; /* Not writable, readable */ |
1008 | 0 | stw_be_p(&outbuf[size + 2], rds_caps_size[i]); |
1009 | 0 | size += 4; |
1010 | 0 | } |
1011 | 0 | break; |
1012 | 0 | } |
1013 | | |
1014 | 0 | default: |
1015 | 0 | return -1; |
1016 | 0 | } |
1017 | | |
1018 | | /* Size of buffer, not including 2 byte size field */ |
1019 | 0 | stw_be_p(outbuf, size - 2); |
1020 | 0 | return size; |
1021 | | |
1022 | 0 | fail: |
1023 | 0 | return -1; |
1024 | 0 | } |
1025 | | |
1026 | | static int scsi_event_status_media(SCSIDiskState *s, uint8_t *outbuf) |
1027 | 0 | { |
1028 | 0 | uint8_t event_code, media_status; |
1029 | |
|
1030 | 0 | media_status = 0; |
1031 | 0 | if (s->tray_open) { |
1032 | 0 | media_status = MS_TRAY_OPEN; |
1033 | 0 | } else if (blk_is_inserted(s->qdev.conf.blk)) { |
1034 | 0 | media_status = MS_MEDIA_PRESENT; |
1035 | 0 | } |
1036 | | |
1037 | | /* Event notification descriptor */ |
1038 | 0 | event_code = MEC_NO_CHANGE; |
1039 | 0 | if (media_status != MS_TRAY_OPEN) { |
1040 | 0 | if (s->media_event) { |
1041 | 0 | event_code = MEC_NEW_MEDIA; |
1042 | 0 | s->media_event = false; |
1043 | 0 | } else if (s->eject_request) { |
1044 | 0 | event_code = MEC_EJECT_REQUESTED; |
1045 | 0 | s->eject_request = false; |
1046 | 0 | } |
1047 | 0 | } |
1048 | |
|
1049 | 0 | outbuf[0] = event_code; |
1050 | 0 | outbuf[1] = media_status; |
1051 | | |
1052 | | /* These fields are reserved, just clear them. */ |
1053 | 0 | outbuf[2] = 0; |
1054 | 0 | outbuf[3] = 0; |
1055 | 0 | return 4; |
1056 | 0 | } |
1057 | | |
1058 | | static int scsi_get_event_status_notification(SCSIDiskState *s, SCSIDiskReq *r, |
1059 | | uint8_t *outbuf) |
1060 | 530 | { |
1061 | 530 | int size; |
1062 | 530 | uint8_t *buf = r->req.cmd.buf; |
1063 | 530 | uint8_t notification_class_request = buf[4]; |
1064 | 530 | if (s->qdev.type != TYPE_ROM) { |
1065 | 530 | return -1; |
1066 | 530 | } |
1067 | 0 | if ((buf[1] & 1) == 0) { |
1068 | | /* asynchronous */ |
1069 | 0 | return -1; |
1070 | 0 | } |
1071 | | |
1072 | 0 | size = 4; |
1073 | 0 | outbuf[0] = outbuf[1] = 0; |
1074 | 0 | outbuf[3] = 1 << GESN_MEDIA; /* supported events */ |
1075 | 0 | if (notification_class_request & (1 << GESN_MEDIA)) { |
1076 | 0 | outbuf[2] = GESN_MEDIA; |
1077 | 0 | size += scsi_event_status_media(s, &outbuf[size]); |
1078 | 0 | } else { |
1079 | 0 | outbuf[2] = 0x80; |
1080 | 0 | } |
1081 | 0 | stw_be_p(outbuf, size - 4); |
1082 | 0 | return size; |
1083 | 0 | } |
1084 | | |
1085 | | static int scsi_get_configuration(SCSIDiskState *s, uint8_t *outbuf) |
1086 | 1.69k | { |
1087 | 1.69k | int current; |
1088 | | |
1089 | 1.69k | if (s->qdev.type != TYPE_ROM) { |
1090 | 1.69k | return -1; |
1091 | 1.69k | } |
1092 | | |
1093 | 0 | if (media_is_dvd(s)) { |
1094 | 0 | current = MMC_PROFILE_DVD_ROM; |
1095 | 0 | } else if (media_is_cd(s)) { |
1096 | 0 | current = MMC_PROFILE_CD_ROM; |
1097 | 0 | } else { |
1098 | 0 | current = MMC_PROFILE_NONE; |
1099 | 0 | } |
1100 | |
|
1101 | 0 | memset(outbuf, 0, 40); |
1102 | 0 | stl_be_p(&outbuf[0], 36); /* Bytes after the data length field */ |
1103 | 0 | stw_be_p(&outbuf[6], current); |
1104 | | /* outbuf[8] - outbuf[19]: Feature 0 - Profile list */ |
1105 | 0 | outbuf[10] = 0x03; /* persistent, current */ |
1106 | 0 | outbuf[11] = 8; /* two profiles */ |
1107 | 0 | stw_be_p(&outbuf[12], MMC_PROFILE_DVD_ROM); |
1108 | 0 | outbuf[14] = (current == MMC_PROFILE_DVD_ROM); |
1109 | 0 | stw_be_p(&outbuf[16], MMC_PROFILE_CD_ROM); |
1110 | 0 | outbuf[18] = (current == MMC_PROFILE_CD_ROM); |
1111 | | /* outbuf[20] - outbuf[31]: Feature 1 - Core feature */ |
1112 | 0 | stw_be_p(&outbuf[20], 1); |
1113 | 0 | outbuf[22] = 0x08 | 0x03; /* version 2, persistent, current */ |
1114 | 0 | outbuf[23] = 8; |
1115 | 0 | stl_be_p(&outbuf[24], 1); /* SCSI */ |
1116 | 0 | outbuf[28] = 1; /* DBE = 1, mandatory */ |
1117 | | /* outbuf[32] - outbuf[39]: Feature 3 - Removable media feature */ |
1118 | 0 | stw_be_p(&outbuf[32], 3); |
1119 | 0 | outbuf[34] = 0x08 | 0x03; /* version 2, persistent, current */ |
1120 | 0 | outbuf[35] = 4; |
1121 | 0 | outbuf[36] = 0x39; /* tray, load=1, eject=1, unlocked at powerup, lock=1 */ |
1122 | | /* TODO: Random readable, CD read, DVD read, drive serial number, |
1123 | | power management */ |
1124 | 0 | return 40; |
1125 | 1.69k | } |
1126 | | |
1127 | | static int scsi_emulate_mechanism_status(SCSIDiskState *s, uint8_t *outbuf) |
1128 | 409 | { |
1129 | 409 | if (s->qdev.type != TYPE_ROM) { |
1130 | 409 | return -1; |
1131 | 409 | } |
1132 | 0 | memset(outbuf, 0, 8); |
1133 | 0 | outbuf[5] = 1; /* CD-ROM */ |
1134 | 0 | return 8; |
1135 | 409 | } |
1136 | | |
1137 | | static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf, |
1138 | | int page_control) |
1139 | 32.3k | { |
1140 | 32.3k | static const int mode_sense_valid[0x3f] = { |
1141 | 32.3k | [MODE_PAGE_VENDOR_SPECIFIC] = (1 << TYPE_DISK) | (1 << TYPE_ROM), |
1142 | 32.3k | [MODE_PAGE_HD_GEOMETRY] = (1 << TYPE_DISK), |
1143 | 32.3k | [MODE_PAGE_FLEXIBLE_DISK_GEOMETRY] = (1 << TYPE_DISK), |
1144 | 32.3k | [MODE_PAGE_CACHING] = (1 << TYPE_DISK) | (1 << TYPE_ROM), |
1145 | 32.3k | [MODE_PAGE_R_W_ERROR] = (1 << TYPE_DISK) | (1 << TYPE_ROM), |
1146 | 32.3k | [MODE_PAGE_AUDIO_CTL] = (1 << TYPE_ROM), |
1147 | 32.3k | [MODE_PAGE_CAPABILITIES] = (1 << TYPE_ROM), |
1148 | 32.3k | [MODE_PAGE_APPLE_VENDOR] = (1 << TYPE_ROM), |
1149 | 32.3k | }; |
1150 | | |
1151 | 32.3k | uint8_t *p = *p_outbuf + 2; |
1152 | 32.3k | int length; |
1153 | | |
1154 | 32.3k | assert(page < ARRAY_SIZE(mode_sense_valid)); |
1155 | 32.3k | if ((mode_sense_valid[page] & (1 << s->qdev.type)) == 0) { |
1156 | 22.0k | return -1; |
1157 | 22.0k | } |
1158 | | |
1159 | | /* |
1160 | | * If Changeable Values are requested, a mask denoting those mode parameters |
1161 | | * that are changeable shall be returned. As we currently don't support |
1162 | | * parameter changes via MODE_SELECT all bits are returned set to zero. |
1163 | | * The buffer was already menset to zero by the caller of this function. |
1164 | | * |
1165 | | * The offsets here are off by two compared to the descriptions in the |
1166 | | * SCSI specs, because those include a 2-byte header. This is unfortunate, |
1167 | | * but it is done so that offsets are consistent within our implementation |
1168 | | * of MODE SENSE and MODE SELECT. MODE SELECT has to deal with both |
1169 | | * 2-byte and 4-byte headers. |
1170 | | */ |
1171 | 10.2k | switch (page) { |
1172 | 1.17k | case MODE_PAGE_HD_GEOMETRY: |
1173 | 1.17k | length = 0x16; |
1174 | 1.17k | if (page_control == 1) { /* Changeable Values */ |
1175 | 542 | break; |
1176 | 542 | } |
1177 | | /* if a geometry hint is available, use it */ |
1178 | 633 | p[0] = (s->qdev.conf.cyls >> 16) & 0xff; |
1179 | 633 | p[1] = (s->qdev.conf.cyls >> 8) & 0xff; |
1180 | 633 | p[2] = s->qdev.conf.cyls & 0xff; |
1181 | 633 | p[3] = s->qdev.conf.heads & 0xff; |
1182 | | /* Write precomp start cylinder, disabled */ |
1183 | 633 | p[4] = (s->qdev.conf.cyls >> 16) & 0xff; |
1184 | 633 | p[5] = (s->qdev.conf.cyls >> 8) & 0xff; |
1185 | 633 | p[6] = s->qdev.conf.cyls & 0xff; |
1186 | | /* Reduced current start cylinder, disabled */ |
1187 | 633 | p[7] = (s->qdev.conf.cyls >> 16) & 0xff; |
1188 | 633 | p[8] = (s->qdev.conf.cyls >> 8) & 0xff; |
1189 | 633 | p[9] = s->qdev.conf.cyls & 0xff; |
1190 | | /* Device step rate [ns], 200ns */ |
1191 | 633 | p[10] = 0; |
1192 | 633 | p[11] = 200; |
1193 | | /* Landing zone cylinder */ |
1194 | 633 | p[12] = 0xff; |
1195 | 633 | p[13] = 0xff; |
1196 | 633 | p[14] = 0xff; |
1197 | | /* Medium rotation rate [rpm], 5400 rpm */ |
1198 | 633 | p[18] = (5400 >> 8) & 0xff; |
1199 | 633 | p[19] = 5400 & 0xff; |
1200 | 633 | break; |
1201 | | |
1202 | 1.38k | case MODE_PAGE_FLEXIBLE_DISK_GEOMETRY: |
1203 | 1.38k | length = 0x1e; |
1204 | 1.38k | if (page_control == 1) { /* Changeable Values */ |
1205 | 658 | break; |
1206 | 658 | } |
1207 | | /* Transfer rate [kbit/s], 5Mbit/s */ |
1208 | 726 | p[0] = 5000 >> 8; |
1209 | 726 | p[1] = 5000 & 0xff; |
1210 | | /* if a geometry hint is available, use it */ |
1211 | 726 | p[2] = s->qdev.conf.heads & 0xff; |
1212 | 726 | p[3] = s->qdev.conf.secs & 0xff; |
1213 | 726 | p[4] = s->qdev.blocksize >> 8; |
1214 | 726 | p[6] = (s->qdev.conf.cyls >> 8) & 0xff; |
1215 | 726 | p[7] = s->qdev.conf.cyls & 0xff; |
1216 | | /* Write precomp start cylinder, disabled */ |
1217 | 726 | p[8] = (s->qdev.conf.cyls >> 8) & 0xff; |
1218 | 726 | p[9] = s->qdev.conf.cyls & 0xff; |
1219 | | /* Reduced current start cylinder, disabled */ |
1220 | 726 | p[10] = (s->qdev.conf.cyls >> 8) & 0xff; |
1221 | 726 | p[11] = s->qdev.conf.cyls & 0xff; |
1222 | | /* Device step rate [100us], 100us */ |
1223 | 726 | p[12] = 0; |
1224 | 726 | p[13] = 1; |
1225 | | /* Device step pulse width [us], 1us */ |
1226 | 726 | p[14] = 1; |
1227 | | /* Device head settle delay [100us], 100us */ |
1228 | 726 | p[15] = 0; |
1229 | 726 | p[16] = 1; |
1230 | | /* Motor on delay [0.1s], 0.1s */ |
1231 | 726 | p[17] = 1; |
1232 | | /* Motor off delay [0.1s], 0.1s */ |
1233 | 726 | p[18] = 1; |
1234 | | /* Medium rotation rate [rpm], 5400 rpm */ |
1235 | 726 | p[26] = (5400 >> 8) & 0xff; |
1236 | 726 | p[27] = 5400 & 0xff; |
1237 | 726 | break; |
1238 | | |
1239 | 1.03k | case MODE_PAGE_CACHING: |
1240 | 1.03k | length = 0x12; |
1241 | 1.03k | if (page_control == 1 || /* Changeable Values */ |
1242 | 1.03k | blk_enable_write_cache(s->qdev.conf.blk)) { |
1243 | 1.03k | p[0] = 4; /* WCE */ |
1244 | 1.03k | } |
1245 | 1.03k | break; |
1246 | | |
1247 | 1.82k | case MODE_PAGE_R_W_ERROR: |
1248 | 1.82k | length = 10; |
1249 | 1.82k | if (page_control == 1) { /* Changeable Values */ |
1250 | 496 | if (s->qdev.type == TYPE_ROM) { |
1251 | | /* Automatic Write Reallocation Enabled */ |
1252 | 0 | p[0] = 0x80; |
1253 | 0 | } |
1254 | 496 | break; |
1255 | 496 | } |
1256 | 1.32k | p[0] = 0x80; /* Automatic Write Reallocation Enabled */ |
1257 | 1.32k | if (s->qdev.type == TYPE_ROM) { |
1258 | 0 | p[1] = 0x20; /* Read Retry Count */ |
1259 | 0 | } |
1260 | 1.32k | break; |
1261 | | |
1262 | 0 | case MODE_PAGE_AUDIO_CTL: |
1263 | 0 | length = 14; |
1264 | 0 | break; |
1265 | | |
1266 | 0 | case MODE_PAGE_CAPABILITIES: |
1267 | 0 | length = 0x14; |
1268 | 0 | if (page_control == 1) { /* Changeable Values */ |
1269 | 0 | break; |
1270 | 0 | } |
1271 | | |
1272 | 0 | p[0] = 0x3b; /* CD-R & CD-RW read */ |
1273 | 0 | p[1] = 0; /* Writing not supported */ |
1274 | 0 | p[2] = 0x7f; /* Audio, composite, digital out, |
1275 | | mode 2 form 1&2, multi session */ |
1276 | 0 | p[3] = 0xff; /* CD DA, DA accurate, RW supported, |
1277 | | RW corrected, C2 errors, ISRC, |
1278 | | UPC, Bar code */ |
1279 | 0 | p[4] = 0x2d | (s->tray_locked ? 2 : 0); |
1280 | | /* Locking supported, jumper present, eject, tray */ |
1281 | 0 | p[5] = 0; /* no volume & mute control, no |
1282 | | changer */ |
1283 | 0 | p[6] = (50 * 176) >> 8; /* 50x read speed */ |
1284 | 0 | p[7] = (50 * 176) & 0xff; |
1285 | 0 | p[8] = 2 >> 8; /* Two volume levels */ |
1286 | 0 | p[9] = 2 & 0xff; |
1287 | 0 | p[10] = 2048 >> 8; /* 2M buffer */ |
1288 | 0 | p[11] = 2048 & 0xff; |
1289 | 0 | p[12] = (16 * 176) >> 8; /* 16x read speed current */ |
1290 | 0 | p[13] = (16 * 176) & 0xff; |
1291 | 0 | p[16] = (16 * 176) >> 8; /* 16x write speed */ |
1292 | 0 | p[17] = (16 * 176) & 0xff; |
1293 | 0 | p[18] = (16 * 176) >> 8; /* 16x write speed current */ |
1294 | 0 | p[19] = (16 * 176) & 0xff; |
1295 | 0 | break; |
1296 | | |
1297 | 0 | case MODE_PAGE_APPLE_VENDOR: |
1298 | 0 | if (s->quirks & (1 << SCSI_DISK_QUIRK_MODE_PAGE_APPLE_VENDOR)) { |
1299 | 0 | length = 0x1e; |
1300 | 0 | if (page_control == 1) { /* Changeable Values */ |
1301 | 0 | break; |
1302 | 0 | } |
1303 | | |
1304 | 0 | memset(p, 0, length); |
1305 | 0 | strcpy((char *)p + 8, "APPLE COMPUTER, INC "); |
1306 | 0 | break; |
1307 | 0 | } else { |
1308 | 0 | return -1; |
1309 | 0 | } |
1310 | | |
1311 | 4.84k | case MODE_PAGE_VENDOR_SPECIFIC: |
1312 | 4.84k | if (s->qdev.type == TYPE_DISK && (s->quirks & |
1313 | 4.84k | (1 << SCSI_DISK_QUIRK_MODE_PAGE_VENDOR_SPECIFIC_APPLE))) { |
1314 | 0 | length = 0x2; |
1315 | 0 | if (page_control == 1) { /* Changeable Values */ |
1316 | 0 | p[0] = 0xff; |
1317 | 0 | p[1] = 0xff; |
1318 | 0 | break; |
1319 | 0 | } |
1320 | 0 | p[0] = 0; |
1321 | 0 | p[1] = 0; |
1322 | 0 | break; |
1323 | 4.84k | } else { |
1324 | 4.84k | return -1; |
1325 | 4.84k | } |
1326 | | |
1327 | 0 | default: |
1328 | 0 | return -1; |
1329 | 10.2k | } |
1330 | | |
1331 | 10.2k | assert(length + 2 <= SCSI_MAX_MODE_LEN); |
1332 | 5.41k | (*p_outbuf)[0] = page; |
1333 | 5.41k | (*p_outbuf)[1] = length; |
1334 | 5.41k | *p_outbuf += length + 2; |
1335 | 5.41k | return length + 2; |
1336 | 5.41k | } |
1337 | | |
1338 | | static int scsi_disk_emulate_mode_sense(SCSIDiskReq *r, uint8_t *outbuf) |
1339 | 4.17k | { |
1340 | 4.17k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
1341 | 4.17k | uint64_t nb_sectors; |
1342 | 4.17k | bool dbd; |
1343 | 4.17k | int page, buflen, ret, page_control; |
1344 | 4.17k | uint8_t *p; |
1345 | 4.17k | uint8_t dev_specific_param; |
1346 | | |
1347 | 4.17k | dbd = (r->req.cmd.buf[1] & 0x8) != 0; |
1348 | 4.17k | page = r->req.cmd.buf[2] & 0x3f; |
1349 | 4.17k | page_control = (r->req.cmd.buf[2] & 0xc0) >> 6; |
1350 | | |
1351 | 4.17k | trace_scsi_disk_emulate_mode_sense((r->req.cmd.buf[0] == MODE_SENSE) ? 6 : |
1352 | 4.17k | 10, page, r->req.cmd.xfer, page_control); |
1353 | 4.17k | memset(outbuf, 0, r->req.cmd.xfer); |
1354 | 4.17k | p = outbuf; |
1355 | | |
1356 | 4.17k | if (s->qdev.type == TYPE_DISK) { |
1357 | 4.17k | dev_specific_param = s->features & (1 << SCSI_DISK_F_DPOFUA) ? 0x10 : 0; |
1358 | 4.17k | if (!blk_is_writable(s->qdev.conf.blk)) { |
1359 | 0 | dev_specific_param |= 0x80; /* Readonly. */ |
1360 | 0 | } |
1361 | 4.17k | } else { |
1362 | 0 | if (s->quirks & (1 << SCSI_DISK_QUIRK_MODE_SENSE_ROM_USE_DBD)) { |
1363 | | /* Use DBD from the request... */ |
1364 | 0 | dev_specific_param = 0x00; |
1365 | | |
1366 | | /* |
1367 | | * ... unless we receive a request for MODE_PAGE_APPLE_VENDOR |
1368 | | * which should never return a block descriptor even though DBD is |
1369 | | * not set, otherwise CDROM detection fails in MacOS |
1370 | | */ |
1371 | 0 | if (s->quirks & (1 << SCSI_DISK_QUIRK_MODE_PAGE_APPLE_VENDOR) && |
1372 | 0 | page == MODE_PAGE_APPLE_VENDOR) { |
1373 | 0 | dbd = true; |
1374 | 0 | } |
1375 | 0 | } else { |
1376 | | /* |
1377 | | * MMC prescribes that CD/DVD drives have no block descriptors, |
1378 | | * and defines no device-specific parameter. |
1379 | | */ |
1380 | 0 | dev_specific_param = 0x00; |
1381 | 0 | dbd = true; |
1382 | 0 | } |
1383 | 0 | } |
1384 | | |
1385 | 4.17k | if (r->req.cmd.buf[0] == MODE_SENSE) { |
1386 | 2.29k | p[1] = 0; /* Default media type. */ |
1387 | 2.29k | p[2] = dev_specific_param; |
1388 | 2.29k | p[3] = 0; /* Block descriptor length. */ |
1389 | 2.29k | p += 4; |
1390 | 2.29k | } else { /* MODE_SENSE_10 */ |
1391 | 1.87k | p[2] = 0; /* Default media type. */ |
1392 | 1.87k | p[3] = dev_specific_param; |
1393 | 1.87k | p[6] = p[7] = 0; /* Block descriptor length. */ |
1394 | 1.87k | p += 8; |
1395 | 1.87k | } |
1396 | | |
1397 | 4.17k | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
1398 | 4.17k | if (!dbd && nb_sectors) { |
1399 | 3.39k | if (r->req.cmd.buf[0] == MODE_SENSE) { |
1400 | 2.27k | outbuf[3] = 8; /* Block descriptor length */ |
1401 | 2.27k | } else { /* MODE_SENSE_10 */ |
1402 | 1.11k | outbuf[7] = 8; /* Block descriptor length */ |
1403 | 1.11k | } |
1404 | 3.39k | nb_sectors /= (s->qdev.blocksize / BDRV_SECTOR_SIZE); |
1405 | 3.39k | if (nb_sectors > 0xffffff) { |
1406 | 0 | nb_sectors = 0; |
1407 | 0 | } |
1408 | 3.39k | p[0] = 0; /* media density code */ |
1409 | 3.39k | p[1] = (nb_sectors >> 16) & 0xff; |
1410 | 3.39k | p[2] = (nb_sectors >> 8) & 0xff; |
1411 | 3.39k | p[3] = nb_sectors & 0xff; |
1412 | 3.39k | p[4] = 0; /* reserved */ |
1413 | 3.39k | p[5] = 0; /* bytes 5-7 are the sector size in bytes */ |
1414 | 3.39k | p[6] = s->qdev.blocksize >> 8; |
1415 | 3.39k | p[7] = 0; |
1416 | 3.39k | p += 8; |
1417 | 3.39k | } |
1418 | | |
1419 | 4.17k | if (page_control == 3) { |
1420 | | /* Saved Values */ |
1421 | 410 | scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED)); |
1422 | 410 | return -1; |
1423 | 410 | } |
1424 | | |
1425 | 3.76k | if (page == 0x3f) { |
1426 | 23.4k | for (page = 0; page <= 0x3e; page++) { |
1427 | 23.1k | mode_sense_page(s, page, &p, page_control); |
1428 | 23.1k | } |
1429 | 3.39k | } else { |
1430 | 3.39k | ret = mode_sense_page(s, page, &p, page_control); |
1431 | 3.39k | if (ret == -1) { |
1432 | 2.98k | return -1; |
1433 | 2.98k | } |
1434 | 3.39k | } |
1435 | | |
1436 | 779 | buflen = p - outbuf; |
1437 | | /* |
1438 | | * The mode data length field specifies the length in bytes of the |
1439 | | * following data that is available to be transferred. The mode data |
1440 | | * length does not include itself. |
1441 | | */ |
1442 | 779 | if (r->req.cmd.buf[0] == MODE_SENSE) { |
1443 | 81 | outbuf[0] = buflen - 1; |
1444 | 698 | } else { /* MODE_SENSE_10 */ |
1445 | 698 | outbuf[0] = ((buflen - 2) >> 8) & 0xff; |
1446 | 698 | outbuf[1] = (buflen - 2) & 0xff; |
1447 | 698 | } |
1448 | 779 | return buflen; |
1449 | 3.76k | } |
1450 | | |
1451 | | static int scsi_disk_emulate_read_toc(SCSIRequest *req, uint8_t *outbuf) |
1452 | 2.31k | { |
1453 | 2.31k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
1454 | 2.31k | int start_track, format, msf, toclen; |
1455 | 2.31k | uint64_t nb_sectors; |
1456 | | |
1457 | 2.31k | msf = req->cmd.buf[1] & 2; |
1458 | 2.31k | format = req->cmd.buf[2] & 0xf; |
1459 | 2.31k | start_track = req->cmd.buf[6]; |
1460 | 2.31k | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
1461 | 2.31k | trace_scsi_disk_emulate_read_toc(start_track, format, msf >> 1); |
1462 | 2.31k | nb_sectors /= s->qdev.blocksize / BDRV_SECTOR_SIZE; |
1463 | 2.31k | switch (format) { |
1464 | 1.66k | case 0: |
1465 | 1.66k | toclen = cdrom_read_toc(nb_sectors, outbuf, msf, start_track); |
1466 | 1.66k | break; |
1467 | 338 | case 1: |
1468 | | /* multi session : only a single session defined */ |
1469 | 338 | toclen = 12; |
1470 | 338 | memset(outbuf, 0, 12); |
1471 | 338 | outbuf[1] = 0x0a; |
1472 | 338 | outbuf[2] = 0x01; |
1473 | 338 | outbuf[3] = 0x01; |
1474 | 338 | break; |
1475 | 0 | case 2: |
1476 | 0 | toclen = cdrom_read_toc_raw(nb_sectors, outbuf, msf, start_track); |
1477 | 0 | break; |
1478 | 320 | default: |
1479 | 320 | return -1; |
1480 | 2.31k | } |
1481 | 1.99k | return toclen; |
1482 | 2.31k | } |
1483 | | |
1484 | | static int scsi_disk_emulate_start_stop(SCSIDiskReq *r) |
1485 | 1.26k | { |
1486 | 1.26k | SCSIRequest *req = &r->req; |
1487 | 1.26k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
1488 | 1.26k | bool start = req->cmd.buf[4] & 1; |
1489 | 1.26k | bool loej = req->cmd.buf[4] & 2; /* load on start, eject on !start */ |
1490 | 1.26k | int pwrcnd = req->cmd.buf[4] & 0xf0; |
1491 | | |
1492 | 1.26k | if (pwrcnd) { |
1493 | | /* eject/load only happens for power condition == 0 */ |
1494 | 476 | return 0; |
1495 | 476 | } |
1496 | | |
1497 | 790 | if ((s->features & (1 << SCSI_DISK_F_REMOVABLE)) && loej) { |
1498 | 0 | if (!start && !s->tray_open && s->tray_locked) { |
1499 | 0 | scsi_check_condition(r, |
1500 | 0 | blk_is_inserted(s->qdev.conf.blk) |
1501 | 0 | ? SENSE_CODE(ILLEGAL_REQ_REMOVAL_PREVENTED) |
1502 | 0 | : SENSE_CODE(NOT_READY_REMOVAL_PREVENTED)); |
1503 | 0 | return -1; |
1504 | 0 | } |
1505 | | |
1506 | 0 | if (s->tray_open != !start) { |
1507 | 0 | blk_eject(s->qdev.conf.blk, !start); |
1508 | 0 | s->tray_open = !start; |
1509 | 0 | } |
1510 | 0 | } |
1511 | 790 | return 0; |
1512 | 790 | } |
1513 | | |
1514 | | static void scsi_disk_emulate_read_data(SCSIRequest *req) |
1515 | 6.10k | { |
1516 | 6.10k | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
1517 | 6.10k | int buflen = r->iov.iov_len; |
1518 | | |
1519 | 6.10k | if (buflen) { |
1520 | 4.93k | trace_scsi_disk_emulate_read_data(buflen); |
1521 | 4.93k | r->iov.iov_len = 0; |
1522 | 4.93k | r->started = true; |
1523 | 4.93k | scsi_req_data(&r->req, buflen); |
1524 | 4.93k | return; |
1525 | 4.93k | } |
1526 | | |
1527 | | /* This also clears the sense buffer for REQUEST SENSE. */ |
1528 | 1.17k | scsi_req_complete(&r->req, GOOD); |
1529 | 1.17k | } |
1530 | | |
1531 | | static int scsi_disk_check_mode_select(SCSIDiskState *s, int page, |
1532 | | uint8_t *inbuf, int inlen) |
1533 | 5.09k | { |
1534 | 5.09k | uint8_t mode_current[SCSI_MAX_MODE_LEN] = { 0 }; |
1535 | 5.09k | uint8_t mode_changeable[SCSI_MAX_MODE_LEN]; |
1536 | 5.09k | uint8_t *p; |
1537 | 5.09k | int len, expected_len, changeable_len, i; |
1538 | | |
1539 | | /* The input buffer does not include the page header, so it is |
1540 | | * off by 2 bytes. |
1541 | | */ |
1542 | 5.09k | expected_len = inlen + 2; |
1543 | 5.09k | if (expected_len > SCSI_MAX_MODE_LEN) { |
1544 | 540 | return -1; |
1545 | 540 | } |
1546 | | |
1547 | | /* MODE_PAGE_ALLS is only valid for MODE SENSE commands */ |
1548 | 4.55k | if (page == MODE_PAGE_ALLS) { |
1549 | 269 | return -1; |
1550 | 269 | } |
1551 | | |
1552 | 4.28k | p = mode_current; |
1553 | 4.28k | len = mode_sense_page(s, page, &p, 0); |
1554 | | /* The guest may send a truncated page, but not a longer one. */ |
1555 | 4.28k | if (len < 0 || expected_len > len) { |
1556 | 2.75k | return -1; |
1557 | 2.75k | } |
1558 | | |
1559 | 1.53k | p = mode_changeable; |
1560 | 1.53k | memset(mode_changeable, 0, len); |
1561 | 1.53k | changeable_len = mode_sense_page(s, page, &p, 1); |
1562 | 1.53k | assert(changeable_len == len); |
1563 | | |
1564 | | /* Check that unchangeable bits are the same as what MODE SENSE |
1565 | | * would return. |
1566 | | */ |
1567 | 2.64k | for (i = 2; i < expected_len; i++) { |
1568 | 1.41k | if (((mode_current[i] ^ inbuf[i - 2]) & ~mode_changeable[i]) != 0) { |
1569 | 306 | return -1; |
1570 | 306 | } |
1571 | 1.41k | } |
1572 | 1.22k | return 0; |
1573 | 1.53k | } |
1574 | | |
1575 | | /* Note p may be truncated, so check any bytes you access against len. */ |
1576 | | static void scsi_disk_apply_mode_select(SCSIDiskState *s, int page, |
1577 | | uint8_t *p, int len) |
1578 | 207 | { |
1579 | 207 | switch (page) { |
1580 | 0 | case MODE_PAGE_CACHING: |
1581 | 0 | if (len > 0) { |
1582 | 0 | blk_set_enable_write_cache(s->qdev.conf.blk, (p[0] & 4) != 0); |
1583 | 0 | } |
1584 | 0 | break; |
1585 | | |
1586 | 207 | default: |
1587 | 207 | break; |
1588 | 207 | } |
1589 | 207 | } |
1590 | | |
1591 | | static int mode_select_pages(SCSIDiskReq *r, uint8_t *p, int len, bool change) |
1592 | 7.30k | { |
1593 | 7.30k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
1594 | | |
1595 | 8.73k | while (len > 0) { |
1596 | 7.29k | int page, subpage, page_len; |
1597 | | |
1598 | | /* Parse both possible formats for the mode page headers. */ |
1599 | 7.29k | page = p[0] & 0x3f; |
1600 | 7.29k | if (p[0] & 0x40) { |
1601 | 1.41k | if (len < 4) { |
1602 | 259 | goto invalid_param_len; |
1603 | 259 | } |
1604 | 1.16k | subpage = p[1]; |
1605 | 1.16k | page_len = lduw_be_p(&p[2]); |
1606 | 1.16k | p += 4; |
1607 | 1.16k | len -= 4; |
1608 | 5.87k | } else { |
1609 | 5.87k | if (len < 2) { |
1610 | 295 | goto invalid_param_len; |
1611 | 295 | } |
1612 | 5.58k | subpage = 0; |
1613 | 5.58k | page_len = p[1]; |
1614 | 5.58k | p += 2; |
1615 | 5.58k | len -= 2; |
1616 | 5.58k | } |
1617 | | |
1618 | 6.74k | if (subpage) { |
1619 | 735 | goto invalid_param; |
1620 | 735 | } |
1621 | 6.00k | if (page_len > len) { |
1622 | 704 | if (!(s->quirks & SCSI_DISK_QUIRK_MODE_PAGE_TRUNCATED)) { |
1623 | 704 | goto invalid_param_len; |
1624 | 704 | } |
1625 | 0 | trace_scsi_disk_mode_select_page_truncated(page, page_len, len); |
1626 | 0 | page_len = len; |
1627 | 0 | } |
1628 | | |
1629 | 5.30k | if (!change) { |
1630 | 5.09k | if (scsi_disk_check_mode_select(s, page, p, page_len) < 0) { |
1631 | 3.86k | goto invalid_param; |
1632 | 3.86k | } |
1633 | 5.09k | } else { |
1634 | 207 | scsi_disk_apply_mode_select(s, page, p, page_len); |
1635 | 207 | } |
1636 | | |
1637 | 1.43k | p += page_len; |
1638 | 1.43k | len -= page_len; |
1639 | 1.43k | } |
1640 | 1.44k | return 0; |
1641 | | |
1642 | 4.60k | invalid_param: |
1643 | 4.60k | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM)); |
1644 | 4.60k | return -1; |
1645 | | |
1646 | 1.25k | invalid_param_len: |
1647 | 1.25k | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); |
1648 | 1.25k | return -1; |
1649 | 7.30k | } |
1650 | | |
1651 | | static void scsi_disk_emulate_mode_select(SCSIDiskReq *r, uint8_t *inbuf) |
1652 | 9.49k | { |
1653 | 9.49k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
1654 | 9.49k | uint8_t *p = inbuf; |
1655 | 9.49k | int cmd = r->req.cmd.buf[0]; |
1656 | 9.49k | int len = r->req.cmd.xfer; |
1657 | 9.49k | int hdr_len = (cmd == MODE_SELECT ? 4 : 8); |
1658 | 9.49k | int bd_len, bs; |
1659 | 9.49k | int pass; |
1660 | | |
1661 | 9.49k | if ((r->req.cmd.buf[1] & 0x11) != 0x10) { |
1662 | 809 | if (!(s->quirks & |
1663 | 809 | (1 << SCSI_DISK_QUIRK_MODE_PAGE_VENDOR_SPECIFIC_APPLE))) { |
1664 | | /* We only support PF=1, SP=0. */ |
1665 | 809 | goto invalid_field; |
1666 | 809 | } |
1667 | 809 | } |
1668 | | |
1669 | 8.68k | if (len < hdr_len) { |
1670 | 507 | goto invalid_param_len; |
1671 | 507 | } |
1672 | | |
1673 | 8.17k | bd_len = (cmd == MODE_SELECT ? p[3] : lduw_be_p(&p[6])); |
1674 | 8.17k | len -= hdr_len; |
1675 | 8.17k | p += hdr_len; |
1676 | 8.17k | if (len < bd_len) { |
1677 | 735 | goto invalid_param_len; |
1678 | 735 | } |
1679 | 7.44k | if (bd_len != 0 && bd_len != 8) { |
1680 | 860 | goto invalid_param; |
1681 | 860 | } |
1682 | | |
1683 | | /* |
1684 | | * Allow changing the block size only if the quirk is enabled for it. |
1685 | | * Writing s->qdev.blocksize is not thread safe! |
1686 | | */ |
1687 | 6.58k | if (bd_len && (s->quirks & |
1688 | 2.19k | (1 << SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE))) { |
1689 | 0 | bs = p[5] << 16 | p[6] << 8 | p[7]; |
1690 | | |
1691 | | /* |
1692 | | * Since the existing code only checks/updates bits 8-15 of the block |
1693 | | * size, restrict ourselves to the same requirement for now to ensure |
1694 | | * that a block size set by a block descriptor and then read back by |
1695 | | * a subsequent SCSI command will be the same. Also disallow a block |
1696 | | * size of 256 since we cannot handle anything below BDRV_SECTOR_SIZE. |
1697 | | */ |
1698 | 0 | if (bs && !(bs & ~0xfe00) && bs != s->qdev.blocksize) { |
1699 | 0 | s->qdev.blocksize = bs; |
1700 | 0 | trace_scsi_disk_mode_select_set_blocksize(s->qdev.blocksize); |
1701 | 0 | } |
1702 | 0 | } |
1703 | | |
1704 | 6.58k | len -= bd_len; |
1705 | 6.58k | p += bd_len; |
1706 | | |
1707 | | /* Ensure no change is made if there is an error! */ |
1708 | 8.02k | for (pass = 0; pass < 2; pass++) { |
1709 | 7.30k | if (mode_select_pages(r, p, len, pass == 1) < 0) { |
1710 | 5.86k | assert(pass == 0); |
1711 | 5.86k | return; |
1712 | 5.86k | } |
1713 | 7.30k | } |
1714 | 721 | if (!blk_enable_write_cache(s->qdev.conf.blk)) { |
1715 | | /* The request is used as the AIO opaque value, so add a ref. */ |
1716 | 0 | scsi_req_ref(&r->req); |
1717 | 0 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0, |
1718 | 0 | BLOCK_ACCT_FLUSH); |
1719 | 0 | r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_aio_complete, r); |
1720 | 0 | return; |
1721 | 0 | } |
1722 | | |
1723 | 721 | scsi_req_complete(&r->req, GOOD); |
1724 | 721 | return; |
1725 | | |
1726 | 860 | invalid_param: |
1727 | 860 | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM)); |
1728 | 860 | return; |
1729 | | |
1730 | 1.24k | invalid_param_len: |
1731 | 1.24k | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); |
1732 | 1.24k | return; |
1733 | | |
1734 | 809 | invalid_field: |
1735 | 809 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
1736 | 809 | } |
1737 | | |
1738 | | /* sector_num and nb_sectors expected to be in qdev blocksize */ |
1739 | | static inline bool check_lba_range(SCSIDiskState *s, |
1740 | | uint64_t sector_num, uint32_t nb_sectors) |
1741 | 616k | { |
1742 | | /* |
1743 | | * The first line tests that no overflow happens when computing the last |
1744 | | * sector. The second line tests that the last accessed sector is in |
1745 | | * range. |
1746 | | * |
1747 | | * Careful, the computations should not underflow for nb_sectors == 0, |
1748 | | * and a 0-block read to the first LBA beyond the end of device is |
1749 | | * valid. |
1750 | | */ |
1751 | 616k | return (sector_num <= sector_num + nb_sectors && |
1752 | 615k | sector_num + nb_sectors <= s->qdev.max_lba + 1); |
1753 | 616k | } |
1754 | | |
1755 | | typedef struct UnmapCBData { |
1756 | | SCSIDiskReq *r; |
1757 | | uint8_t *inbuf; |
1758 | | int count; |
1759 | | } UnmapCBData; |
1760 | | |
1761 | | static void scsi_unmap_complete(void *opaque, int ret); |
1762 | | |
1763 | | static void scsi_unmap_complete_noio(UnmapCBData *data, int ret) |
1764 | 600k | { |
1765 | 600k | SCSIDiskReq *r = data->r; |
1766 | 600k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
1767 | | |
1768 | 600k | assert(r->req.aiocb == NULL); |
1769 | | |
1770 | 600k | if (data->count > 0) { |
1771 | 595k | uint64_t sector_num = ldq_be_p(&data->inbuf[0]); |
1772 | 595k | uint32_t nb_sectors = ldl_be_p(&data->inbuf[8]) & 0xffffffffULL; |
1773 | 595k | r->sector = sector_num * (s->qdev.blocksize / BDRV_SECTOR_SIZE); |
1774 | 595k | r->sector_count = nb_sectors * (s->qdev.blocksize / BDRV_SECTOR_SIZE); |
1775 | | |
1776 | 595k | if (!check_lba_range(s, sector_num, nb_sectors)) { |
1777 | 29.4k | block_acct_invalid(blk_get_stats(s->qdev.conf.blk), |
1778 | 29.4k | BLOCK_ACCT_UNMAP); |
1779 | 29.4k | scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); |
1780 | 29.4k | goto done; |
1781 | 29.4k | } |
1782 | | |
1783 | 566k | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, |
1784 | 566k | r->sector_count * BDRV_SECTOR_SIZE, |
1785 | 566k | BLOCK_ACCT_UNMAP); |
1786 | | |
1787 | 566k | r->req.aiocb = blk_aio_pdiscard(s->qdev.conf.blk, |
1788 | 566k | r->sector * BDRV_SECTOR_SIZE, |
1789 | 566k | r->sector_count * BDRV_SECTOR_SIZE, |
1790 | 566k | scsi_unmap_complete, data); |
1791 | 566k | data->count--; |
1792 | 566k | data->inbuf += 16; |
1793 | 566k | return; |
1794 | 595k | } |
1795 | | |
1796 | 4.52k | scsi_req_complete(&r->req, GOOD); |
1797 | | |
1798 | 34.0k | done: |
1799 | 34.0k | scsi_req_unref(&r->req); |
1800 | 34.0k | g_free(data); |
1801 | 34.0k | } |
1802 | | |
1803 | | static void scsi_unmap_complete(void *opaque, int ret) |
1804 | 566k | { |
1805 | 566k | UnmapCBData *data = opaque; |
1806 | 566k | SCSIDiskReq *r = data->r; |
1807 | 566k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
1808 | | |
1809 | 566k | assert(r->req.aiocb != NULL); |
1810 | 566k | r->req.aiocb = NULL; |
1811 | | |
1812 | 566k | if (scsi_disk_req_check_error(r, ret, true)) { |
1813 | 2.64k | scsi_req_unref(&r->req); |
1814 | 2.64k | g_free(data); |
1815 | 563k | } else { |
1816 | 563k | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); |
1817 | 563k | scsi_unmap_complete_noio(data, ret); |
1818 | 563k | } |
1819 | 566k | } |
1820 | | |
1821 | | static void scsi_disk_emulate_unmap(SCSIDiskReq *r, uint8_t *inbuf) |
1822 | 42.7k | { |
1823 | 42.7k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
1824 | 42.7k | uint8_t *p = inbuf; |
1825 | 42.7k | int len = r->req.cmd.xfer; |
1826 | 42.7k | UnmapCBData *data; |
1827 | | |
1828 | | /* Reject ANCHOR=1. */ |
1829 | 42.7k | if (r->req.cmd.buf[1] & 0x1) { |
1830 | 754 | goto invalid_field; |
1831 | 754 | } |
1832 | | |
1833 | 42.0k | if (len < 8) { |
1834 | 1.56k | goto invalid_param_len; |
1835 | 1.56k | } |
1836 | 40.4k | if (len < lduw_be_p(&p[0]) + 2) { |
1837 | 952 | goto invalid_param_len; |
1838 | 952 | } |
1839 | 39.4k | if (len < lduw_be_p(&p[2]) + 8) { |
1840 | 1.07k | goto invalid_param_len; |
1841 | 1.07k | } |
1842 | 38.4k | if (lduw_be_p(&p[2]) & 15) { |
1843 | 1.76k | goto invalid_param_len; |
1844 | 1.76k | } |
1845 | | |
1846 | 36.6k | if (!blk_is_writable(s->qdev.conf.blk)) { |
1847 | 0 | block_acct_invalid(blk_get_stats(s->qdev.conf.blk), BLOCK_ACCT_UNMAP); |
1848 | 0 | scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED)); |
1849 | 0 | return; |
1850 | 0 | } |
1851 | | |
1852 | 36.6k | data = g_new0(UnmapCBData, 1); |
1853 | 36.6k | data->r = r; |
1854 | 36.6k | data->inbuf = &p[8]; |
1855 | 36.6k | data->count = lduw_be_p(&p[2]) >> 4; |
1856 | | |
1857 | | /* The matching unref is in scsi_unmap_complete, before data is freed. */ |
1858 | 36.6k | scsi_req_ref(&r->req); |
1859 | 36.6k | scsi_unmap_complete_noio(data, 0); |
1860 | 36.6k | return; |
1861 | | |
1862 | 5.35k | invalid_param_len: |
1863 | 5.35k | block_acct_invalid(blk_get_stats(s->qdev.conf.blk), BLOCK_ACCT_UNMAP); |
1864 | 5.35k | scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); |
1865 | 5.35k | return; |
1866 | | |
1867 | 754 | invalid_field: |
1868 | 754 | block_acct_invalid(blk_get_stats(s->qdev.conf.blk), BLOCK_ACCT_UNMAP); |
1869 | 754 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
1870 | 754 | } |
1871 | | |
1872 | | typedef struct WriteSameCBData { |
1873 | | SCSIDiskReq *r; |
1874 | | int64_t sector; |
1875 | | int nb_sectors; |
1876 | | QEMUIOVector qiov; |
1877 | | struct iovec iov; |
1878 | | } WriteSameCBData; |
1879 | | |
1880 | | static void scsi_write_same_complete(void *opaque, int ret) |
1881 | 12.5k | { |
1882 | 12.5k | WriteSameCBData *data = opaque; |
1883 | 12.5k | SCSIDiskReq *r = data->r; |
1884 | 12.5k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
1885 | | |
1886 | 12.5k | assert(r->req.aiocb != NULL); |
1887 | 12.5k | r->req.aiocb = NULL; |
1888 | | |
1889 | 12.5k | if (scsi_disk_req_check_error(r, ret, true)) { |
1890 | 89 | goto done; |
1891 | 89 | } |
1892 | | |
1893 | 12.4k | block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); |
1894 | | |
1895 | 12.4k | data->nb_sectors -= data->iov.iov_len / BDRV_SECTOR_SIZE; |
1896 | 12.4k | data->sector += data->iov.iov_len / BDRV_SECTOR_SIZE; |
1897 | 12.4k | data->iov.iov_len = MIN(data->nb_sectors * BDRV_SECTOR_SIZE, |
1898 | 12.4k | data->iov.iov_len); |
1899 | 12.4k | if (data->iov.iov_len) { |
1900 | 12.0k | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, |
1901 | 12.0k | data->iov.iov_len, BLOCK_ACCT_WRITE); |
1902 | | /* Reinitialize qiov, to handle unaligned WRITE SAME request |
1903 | | * where final qiov may need smaller size */ |
1904 | 12.0k | qemu_iovec_init_external(&data->qiov, &data->iov, 1); |
1905 | 12.0k | r->req.aiocb = blk_aio_pwritev(s->qdev.conf.blk, |
1906 | 12.0k | data->sector << BDRV_SECTOR_BITS, |
1907 | 12.0k | &data->qiov, 0, |
1908 | 12.0k | scsi_write_same_complete, data); |
1909 | 12.0k | return; |
1910 | 12.0k | } |
1911 | | |
1912 | 392 | scsi_req_complete(&r->req, GOOD); |
1913 | | |
1914 | 481 | done: |
1915 | 481 | scsi_req_unref(&r->req); |
1916 | 481 | qemu_vfree(data->iov.iov_base); |
1917 | 481 | g_free(data); |
1918 | 481 | } |
1919 | | |
1920 | | static void scsi_disk_emulate_write_same(SCSIDiskReq *r, uint8_t *inbuf) |
1921 | 7.37k | { |
1922 | 7.37k | SCSIRequest *req = &r->req; |
1923 | 7.37k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
1924 | 7.37k | uint32_t nb_sectors = scsi_data_cdb_xfer(r->req.cmd.buf); |
1925 | 7.37k | uint32_t buflen = MIN(s->qdev.blocksize, r->buflen); |
1926 | 7.37k | WriteSameCBData *data; |
1927 | 7.37k | uint8_t *buf; |
1928 | 7.37k | int i, l; |
1929 | | |
1930 | | /* Fail if PBDATA=1 or LBDATA=1 or ANCHOR=1. */ |
1931 | 7.37k | if (nb_sectors == 0 || (req->cmd.buf[1] & 0x16)) { |
1932 | 1.60k | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
1933 | 1.60k | return; |
1934 | 1.60k | } |
1935 | | |
1936 | 5.76k | if (!blk_is_writable(s->qdev.conf.blk)) { |
1937 | 0 | scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED)); |
1938 | 0 | return; |
1939 | 0 | } |
1940 | 5.76k | if (!check_lba_range(s, r->req.cmd.lba, nb_sectors)) { |
1941 | 5.16k | scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); |
1942 | 5.16k | return; |
1943 | 5.16k | } |
1944 | | |
1945 | 595 | if ((req->cmd.buf[1] & 0x1) || buffer_is_zero(inbuf, buflen)) { |
1946 | 114 | int flags = (req->cmd.buf[1] & 0x8) ? BDRV_REQ_MAY_UNMAP : 0; |
1947 | | |
1948 | | /* The request is used as the AIO opaque value, so add a ref. */ |
1949 | 114 | scsi_req_ref(&r->req); |
1950 | 114 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, |
1951 | 114 | nb_sectors * s->qdev.blocksize, |
1952 | 114 | BLOCK_ACCT_WRITE); |
1953 | 114 | r->req.aiocb = blk_aio_pwrite_zeroes(s->qdev.conf.blk, |
1954 | 114 | r->req.cmd.lba * s->qdev.blocksize, |
1955 | 114 | nb_sectors * s->qdev.blocksize, |
1956 | 114 | flags, scsi_aio_complete, r); |
1957 | 114 | return; |
1958 | 114 | } |
1959 | | |
1960 | 481 | data = g_new0(WriteSameCBData, 1); |
1961 | 481 | data->r = r; |
1962 | 481 | data->sector = r->req.cmd.lba * (s->qdev.blocksize / BDRV_SECTOR_SIZE); |
1963 | 481 | data->nb_sectors = nb_sectors * (s->qdev.blocksize / BDRV_SECTOR_SIZE); |
1964 | 481 | data->iov.iov_len = MIN(data->nb_sectors * BDRV_SECTOR_SIZE, |
1965 | 481 | SCSI_WRITE_SAME_MAX); |
1966 | 481 | data->iov.iov_base = buf = blk_blockalign(s->qdev.conf.blk, |
1967 | 481 | data->iov.iov_len); |
1968 | 481 | qemu_iovec_init_external(&data->qiov, &data->iov, 1); |
1969 | | |
1970 | 448k | for (i = 0; i < data->iov.iov_len; i += l) { |
1971 | 447k | l = MIN(buflen, data->iov.iov_len - i); |
1972 | 447k | memcpy(&buf[i], inbuf, l); |
1973 | 447k | } |
1974 | | |
1975 | 481 | scsi_req_ref(&r->req); |
1976 | 481 | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, |
1977 | 481 | data->iov.iov_len, BLOCK_ACCT_WRITE); |
1978 | 481 | r->req.aiocb = blk_aio_pwritev(s->qdev.conf.blk, |
1979 | 481 | data->sector << BDRV_SECTOR_BITS, |
1980 | 481 | &data->qiov, 0, |
1981 | 481 | scsi_write_same_complete, data); |
1982 | 481 | } |
1983 | | |
1984 | | static void scsi_disk_emulate_write_data(SCSIRequest *req) |
1985 | 124k | { |
1986 | 124k | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
1987 | | |
1988 | 124k | if (r->iov.iov_len) { |
1989 | 63.2k | int buflen = r->iov.iov_len; |
1990 | 63.2k | trace_scsi_disk_emulate_write_data(buflen); |
1991 | 63.2k | r->iov.iov_len = 0; |
1992 | 63.2k | scsi_req_data(&r->req, buflen); |
1993 | 63.2k | return; |
1994 | 63.2k | } |
1995 | | |
1996 | 61.0k | switch (req->cmd.buf[0]) { |
1997 | 1.44k | case MODE_SELECT: |
1998 | 9.49k | case MODE_SELECT_10: |
1999 | | /* This also clears the sense buffer for REQUEST SENSE. */ |
2000 | 9.49k | scsi_disk_emulate_mode_select(r, r->iov.iov_base); |
2001 | 9.49k | break; |
2002 | | |
2003 | 42.7k | case UNMAP: |
2004 | 42.7k | scsi_disk_emulate_unmap(r, r->iov.iov_base); |
2005 | 42.7k | break; |
2006 | | |
2007 | 0 | case VERIFY_10: |
2008 | 0 | case VERIFY_12: |
2009 | 0 | case VERIFY_16: |
2010 | 0 | if (r->req.status == -1) { |
2011 | 0 | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
2012 | 0 | } |
2013 | 0 | break; |
2014 | | |
2015 | 1.20k | case WRITE_SAME_10: |
2016 | 7.37k | case WRITE_SAME_16: |
2017 | 7.37k | scsi_disk_emulate_write_same(r, r->iov.iov_base); |
2018 | 7.37k | break; |
2019 | | |
2020 | 1.41k | case FORMAT_UNIT: |
2021 | 1.41k | scsi_req_complete(&r->req, GOOD); |
2022 | 1.41k | break; |
2023 | | |
2024 | 0 | default: |
2025 | 0 | abort(); |
2026 | 61.0k | } |
2027 | 61.0k | } |
2028 | | |
2029 | | static int32_t scsi_disk_emulate_command(SCSIRequest *req, uint8_t *buf) |
2030 | 144k | { |
2031 | 144k | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
2032 | 144k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
2033 | 144k | uint64_t nb_sectors; |
2034 | 144k | uint8_t *outbuf; |
2035 | 144k | int buflen; |
2036 | | |
2037 | 144k | switch (req->cmd.buf[0]) { |
2038 | 4.31k | case INQUIRY: |
2039 | 6.61k | case MODE_SENSE: |
2040 | 8.48k | case MODE_SENSE_10: |
2041 | 9.36k | case RESERVE: |
2042 | 9.82k | case RESERVE_10: |
2043 | 10.5k | case RELEASE: |
2044 | 12.6k | case RELEASE_10: |
2045 | 13.9k | case START_STOP: |
2046 | 14.9k | case ALLOW_MEDIUM_REMOVAL: |
2047 | 16.5k | case GET_CONFIGURATION: |
2048 | 17.1k | case GET_EVENT_STATUS_NOTIFICATION: |
2049 | 17.5k | case MECHANISM_STATUS: |
2050 | 18.3k | case REQUEST_SENSE: |
2051 | 18.3k | break; |
2052 | | |
2053 | 126k | default: |
2054 | 126k | if (!blk_is_available(s->qdev.conf.blk)) { |
2055 | 0 | scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); |
2056 | 0 | return 0; |
2057 | 0 | } |
2058 | 126k | break; |
2059 | 144k | } |
2060 | | |
2061 | | /* |
2062 | | * FIXME: we shouldn't return anything bigger than 4k, but the code |
2063 | | * requires the buffer to be as big as req->cmd.xfer in several |
2064 | | * places. So, do not allow CDBs with a very large ALLOCATION |
2065 | | * LENGTH. The real fix would be to modify scsi_read_data and |
2066 | | * dma_buf_read, so that they return data beyond the buflen |
2067 | | * as all zeros. |
2068 | | */ |
2069 | 144k | if (req->cmd.xfer > 65536) { |
2070 | 2.01k | goto illegal_request; |
2071 | 2.01k | } |
2072 | 142k | r->buflen = MAX(4096, req->cmd.xfer); |
2073 | | |
2074 | 142k | if (!r->iov.iov_base) { |
2075 | 142k | r->iov.iov_base = blk_blockalign(s->qdev.conf.blk, r->buflen); |
2076 | 142k | } |
2077 | | |
2078 | 142k | outbuf = r->iov.iov_base; |
2079 | 142k | memset(outbuf, 0, r->buflen); |
2080 | 142k | switch (req->cmd.buf[0]) { |
2081 | 14.5k | case TEST_UNIT_READY: |
2082 | 14.5k | assert(blk_is_available(s->qdev.conf.blk)); |
2083 | 14.5k | break; |
2084 | 14.5k | case INQUIRY: |
2085 | 4.31k | buflen = scsi_disk_emulate_inquiry(req, outbuf); |
2086 | 4.31k | if (buflen < 0) { |
2087 | 1.30k | goto illegal_request; |
2088 | 1.30k | } |
2089 | 3.00k | break; |
2090 | 3.00k | case MODE_SENSE: |
2091 | 4.17k | case MODE_SENSE_10: |
2092 | 4.17k | buflen = scsi_disk_emulate_mode_sense(r, outbuf); |
2093 | 4.17k | if (buflen < 0) { |
2094 | 3.39k | goto illegal_request; |
2095 | 3.39k | } |
2096 | 779 | break; |
2097 | 2.31k | case READ_TOC: |
2098 | 2.31k | buflen = scsi_disk_emulate_read_toc(req, outbuf); |
2099 | 2.31k | if (buflen < 0) { |
2100 | 824 | goto illegal_request; |
2101 | 824 | } |
2102 | 1.49k | break; |
2103 | 1.49k | case RESERVE: |
2104 | 885 | if (req->cmd.buf[1] & 1) { |
2105 | 412 | goto illegal_request; |
2106 | 412 | } |
2107 | 473 | break; |
2108 | 473 | case RESERVE_10: |
2109 | 460 | if (req->cmd.buf[1] & 3) { |
2110 | 347 | goto illegal_request; |
2111 | 347 | } |
2112 | 113 | break; |
2113 | 752 | case RELEASE: |
2114 | 752 | if (req->cmd.buf[1] & 1) { |
2115 | 404 | goto illegal_request; |
2116 | 404 | } |
2117 | 348 | break; |
2118 | 2.05k | case RELEASE_10: |
2119 | 2.05k | if (req->cmd.buf[1] & 3) { |
2120 | 1.44k | goto illegal_request; |
2121 | 1.44k | } |
2122 | 616 | break; |
2123 | 1.26k | case START_STOP: |
2124 | 1.26k | if (scsi_disk_emulate_start_stop(r) < 0) { |
2125 | 0 | return 0; |
2126 | 0 | } |
2127 | 1.26k | break; |
2128 | 1.26k | case ALLOW_MEDIUM_REMOVAL: |
2129 | 996 | s->tray_locked = req->cmd.buf[4] & 1; |
2130 | 996 | blk_lock_medium(s->qdev.conf.blk, req->cmd.buf[4] & 1); |
2131 | 996 | break; |
2132 | 2.96k | case READ_CAPACITY_10: |
2133 | | /* The normal LEN field for this command is zero. */ |
2134 | 2.96k | memset(outbuf, 0, 8); |
2135 | 2.96k | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
2136 | 2.96k | if (!nb_sectors) { |
2137 | 0 | scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY)); |
2138 | 0 | return 0; |
2139 | 0 | } |
2140 | 2.96k | if ((req->cmd.buf[8] & 1) == 0 && req->cmd.lba) { |
2141 | 1.10k | goto illegal_request; |
2142 | 1.10k | } |
2143 | 1.86k | nb_sectors /= s->qdev.blocksize / BDRV_SECTOR_SIZE; |
2144 | | /* Returned value is the address of the last sector. */ |
2145 | 1.86k | nb_sectors--; |
2146 | | /* Remember the new size for read/write sanity checking. */ |
2147 | 1.86k | s->qdev.max_lba = nb_sectors; |
2148 | | /* Clip to 2TB, instead of returning capacity modulo 2TB. */ |
2149 | 1.86k | if (nb_sectors > UINT32_MAX) { |
2150 | 0 | nb_sectors = UINT32_MAX; |
2151 | 0 | } |
2152 | 1.86k | outbuf[0] = (nb_sectors >> 24) & 0xff; |
2153 | 1.86k | outbuf[1] = (nb_sectors >> 16) & 0xff; |
2154 | 1.86k | outbuf[2] = (nb_sectors >> 8) & 0xff; |
2155 | 1.86k | outbuf[3] = nb_sectors & 0xff; |
2156 | 1.86k | outbuf[4] = 0; |
2157 | 1.86k | outbuf[5] = 0; |
2158 | 1.86k | outbuf[6] = s->qdev.blocksize >> 8; |
2159 | 1.86k | outbuf[7] = 0; |
2160 | 1.86k | break; |
2161 | 771 | case REQUEST_SENSE: |
2162 | | /* Just return "NO SENSE". */ |
2163 | 771 | buflen = scsi_convert_sense(NULL, 0, outbuf, r->buflen, |
2164 | 771 | (req->cmd.buf[1] & 1) == 0); |
2165 | 771 | if (buflen < 0) { |
2166 | 0 | goto illegal_request; |
2167 | 0 | } |
2168 | 771 | break; |
2169 | 771 | case MECHANISM_STATUS: |
2170 | 409 | buflen = scsi_emulate_mechanism_status(s, outbuf); |
2171 | 409 | if (buflen < 0) { |
2172 | 409 | goto illegal_request; |
2173 | 409 | } |
2174 | 0 | break; |
2175 | 1.69k | case GET_CONFIGURATION: |
2176 | 1.69k | buflen = scsi_get_configuration(s, outbuf); |
2177 | 1.69k | if (buflen < 0) { |
2178 | 1.69k | goto illegal_request; |
2179 | 1.69k | } |
2180 | 0 | break; |
2181 | 530 | case GET_EVENT_STATUS_NOTIFICATION: |
2182 | 530 | buflen = scsi_get_event_status_notification(s, r, outbuf); |
2183 | 530 | if (buflen < 0) { |
2184 | 530 | goto illegal_request; |
2185 | 530 | } |
2186 | 0 | break; |
2187 | 594 | case READ_DISC_INFORMATION: |
2188 | 594 | buflen = scsi_read_disc_information(s, r, outbuf); |
2189 | 594 | if (buflen < 0) { |
2190 | 594 | goto illegal_request; |
2191 | 594 | } |
2192 | 0 | break; |
2193 | 27 | case READ_DVD_STRUCTURE: |
2194 | 27 | buflen = scsi_read_dvd_structure(s, r, outbuf); |
2195 | 27 | if (buflen < 0) { |
2196 | 27 | goto illegal_request; |
2197 | 27 | } |
2198 | 0 | break; |
2199 | 5.56k | case SERVICE_ACTION_IN_16: |
2200 | | /* Service Action In subcommands. */ |
2201 | 5.56k | if ((req->cmd.buf[1] & 31) == SAI_READ_CAPACITY_16) { |
2202 | 5.24k | trace_scsi_disk_emulate_command_SAI_16(); |
2203 | 5.24k | memset(outbuf, 0, req->cmd.xfer); |
2204 | 5.24k | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
2205 | 5.24k | if (!nb_sectors) { |
2206 | 0 | scsi_check_condition(r, SENSE_CODE(LUN_NOT_READY)); |
2207 | 0 | return 0; |
2208 | 0 | } |
2209 | 5.24k | if ((req->cmd.buf[14] & 1) == 0 && req->cmd.lba) { |
2210 | 1.20k | goto illegal_request; |
2211 | 1.20k | } |
2212 | 4.04k | nb_sectors /= s->qdev.blocksize / BDRV_SECTOR_SIZE; |
2213 | | /* Returned value is the address of the last sector. */ |
2214 | 4.04k | nb_sectors--; |
2215 | | /* Remember the new size for read/write sanity checking. */ |
2216 | 4.04k | s->qdev.max_lba = nb_sectors; |
2217 | 4.04k | outbuf[0] = (nb_sectors >> 56) & 0xff; |
2218 | 4.04k | outbuf[1] = (nb_sectors >> 48) & 0xff; |
2219 | 4.04k | outbuf[2] = (nb_sectors >> 40) & 0xff; |
2220 | 4.04k | outbuf[3] = (nb_sectors >> 32) & 0xff; |
2221 | 4.04k | outbuf[4] = (nb_sectors >> 24) & 0xff; |
2222 | 4.04k | outbuf[5] = (nb_sectors >> 16) & 0xff; |
2223 | 4.04k | outbuf[6] = (nb_sectors >> 8) & 0xff; |
2224 | 4.04k | outbuf[7] = nb_sectors & 0xff; |
2225 | 4.04k | outbuf[8] = 0; |
2226 | 4.04k | outbuf[9] = 0; |
2227 | 4.04k | outbuf[10] = s->qdev.blocksize >> 8; |
2228 | 4.04k | outbuf[11] = 0; |
2229 | 4.04k | outbuf[12] = 0; |
2230 | 4.04k | outbuf[13] = get_physical_block_exp(&s->qdev.conf); |
2231 | | |
2232 | | /* set TPE bit if the format supports discard */ |
2233 | 4.04k | if (s->qdev.conf.discard_granularity) { |
2234 | 4.04k | outbuf[14] = 0x80; |
2235 | 4.04k | } |
2236 | | |
2237 | | /* Protection, exponent and lowest lba field left blank. */ |
2238 | 4.04k | break; |
2239 | 5.24k | } |
2240 | 315 | trace_scsi_disk_emulate_command_SAI_unsupported(); |
2241 | 315 | goto illegal_request; |
2242 | 1.14k | case SYNCHRONIZE_CACHE: |
2243 | | /* The request is used as the AIO opaque value, so add a ref. */ |
2244 | 1.14k | scsi_req_ref(&r->req); |
2245 | 1.14k | block_acct_start(blk_get_stats(s->qdev.conf.blk), &r->acct, 0, |
2246 | 1.14k | BLOCK_ACCT_FLUSH); |
2247 | 1.14k | r->req.aiocb = blk_aio_flush(s->qdev.conf.blk, scsi_aio_complete, r); |
2248 | 1.14k | return 0; |
2249 | 1.54k | case SEEK_10: |
2250 | 1.54k | trace_scsi_disk_emulate_command_SEEK_10(r->req.cmd.lba); |
2251 | 1.54k | if (r->req.cmd.lba > s->qdev.max_lba) { |
2252 | 1.15k | goto illegal_lba; |
2253 | 1.15k | } |
2254 | 393 | break; |
2255 | 1.93k | case MODE_SELECT: |
2256 | 1.93k | trace_scsi_disk_emulate_command_MODE_SELECT(r->req.cmd.xfer); |
2257 | 1.93k | break; |
2258 | 8.48k | case MODE_SELECT_10: |
2259 | 8.48k | trace_scsi_disk_emulate_command_MODE_SELECT_10(r->req.cmd.xfer); |
2260 | 8.48k | break; |
2261 | 44.0k | case UNMAP: |
2262 | 44.0k | trace_scsi_disk_emulate_command_UNMAP(r->req.cmd.xfer); |
2263 | 44.0k | break; |
2264 | 1.12k | case VERIFY_10: |
2265 | 1.34k | case VERIFY_12: |
2266 | 2.43k | case VERIFY_16: |
2267 | 2.43k | trace_scsi_disk_emulate_command_VERIFY((req->cmd.buf[1] >> 1) & 3); |
2268 | 2.43k | if (req->cmd.buf[1] & 6) { |
2269 | 1.67k | goto illegal_request; |
2270 | 1.67k | } |
2271 | 753 | break; |
2272 | 1.51k | case WRITE_SAME_10: |
2273 | 7.97k | case WRITE_SAME_16: |
2274 | 7.97k | trace_scsi_disk_emulate_command_WRITE_SAME( |
2275 | 7.97k | req->cmd.buf[0] == WRITE_SAME_10 ? 10 : 16, r->req.cmd.xfer); |
2276 | 7.97k | break; |
2277 | 2.58k | case FORMAT_UNIT: |
2278 | 2.58k | trace_scsi_disk_emulate_command_FORMAT_UNIT(r->req.cmd.xfer); |
2279 | 2.58k | break; |
2280 | 27.8k | default: |
2281 | 27.8k | trace_scsi_disk_emulate_command_UNKNOWN(buf[0], |
2282 | 27.8k | scsi_command_name(buf[0])); |
2283 | 27.8k | scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE)); |
2284 | 27.8k | return 0; |
2285 | 142k | } |
2286 | 142k | assert(!r->req.aiocb); |
2287 | 96.5k | r->iov.iov_len = MIN(r->buflen, req->cmd.xfer); |
2288 | 96.5k | if (r->iov.iov_len == 0) { |
2289 | 28.2k | scsi_req_complete(&r->req, GOOD); |
2290 | 28.2k | } |
2291 | 96.5k | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
2292 | 63.2k | assert(r->iov.iov_len == req->cmd.xfer); |
2293 | 63.2k | return -r->iov.iov_len; |
2294 | 63.2k | } else { |
2295 | 33.2k | return r->iov.iov_len; |
2296 | 33.2k | } |
2297 | | |
2298 | 17.6k | illegal_request: |
2299 | 17.6k | if (r->req.status == -1) { |
2300 | 17.2k | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
2301 | 17.2k | } |
2302 | 17.6k | return 0; |
2303 | | |
2304 | 1.15k | illegal_lba: |
2305 | 1.15k | scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); |
2306 | 1.15k | return 0; |
2307 | 96.5k | } |
2308 | | |
2309 | | /* Execute a scsi command. Returns the length of the data expected by the |
2310 | | command. This will be Positive for data transfers from the device |
2311 | | (eg. disk reads), negative for transfers to the device (eg. disk writes), |
2312 | | and zero if the command does not transfer any data. */ |
2313 | | |
2314 | | static int32_t scsi_disk_dma_command(SCSIRequest *req, uint8_t *buf) |
2315 | 18.2k | { |
2316 | 18.2k | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
2317 | 18.2k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
2318 | 18.2k | SCSIDiskClass *sdc = (SCSIDiskClass *) object_get_class(OBJECT(s)); |
2319 | 18.2k | uint32_t len; |
2320 | 18.2k | uint8_t command; |
2321 | | |
2322 | 18.2k | command = buf[0]; |
2323 | | |
2324 | 18.2k | if (!blk_is_available(s->qdev.conf.blk)) { |
2325 | 0 | scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); |
2326 | 0 | return 0; |
2327 | 0 | } |
2328 | | |
2329 | 18.2k | len = scsi_data_cdb_xfer(r->req.cmd.buf); |
2330 | 18.2k | switch (command) { |
2331 | 883 | case READ_6: |
2332 | 2.74k | case READ_10: |
2333 | 4.70k | case READ_12: |
2334 | 5.34k | case READ_16: |
2335 | 5.34k | trace_scsi_disk_dma_command_READ(r->req.cmd.lba, len); |
2336 | | /* Protection information is not supported. For SCSI versions 2 and |
2337 | | * older (as determined by snooping the guest's INQUIRY commands), |
2338 | | * there is no RD/WR/VRPROTECT, so skip this check in these versions. |
2339 | | */ |
2340 | 5.34k | if (s->qdev.scsi_version > 2 && (r->req.cmd.buf[1] & 0xe0)) { |
2341 | 700 | goto illegal_request; |
2342 | 700 | } |
2343 | 4.64k | if (!check_lba_range(s, r->req.cmd.lba, len)) { |
2344 | 1.36k | goto illegal_lba; |
2345 | 1.36k | } |
2346 | 3.28k | r->sector = r->req.cmd.lba * (s->qdev.blocksize / BDRV_SECTOR_SIZE); |
2347 | 3.28k | r->sector_count = len * (s->qdev.blocksize / BDRV_SECTOR_SIZE); |
2348 | 3.28k | break; |
2349 | 2.38k | case WRITE_6: |
2350 | 6.96k | case WRITE_10: |
2351 | 7.41k | case WRITE_12: |
2352 | 8.86k | case WRITE_16: |
2353 | 10.3k | case WRITE_VERIFY_10: |
2354 | 12.5k | case WRITE_VERIFY_12: |
2355 | 12.8k | case WRITE_VERIFY_16: |
2356 | 12.8k | if (!blk_is_writable(s->qdev.conf.blk)) { |
2357 | 0 | scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED)); |
2358 | 0 | return 0; |
2359 | 0 | } |
2360 | 12.8k | trace_scsi_disk_dma_command_WRITE( |
2361 | 12.8k | (command & 0xe) == 0xe ? "And Verify " : "", |
2362 | 12.8k | r->req.cmd.lba, len); |
2363 | | /* fall through */ |
2364 | 12.8k | case VERIFY_10: |
2365 | 12.8k | case VERIFY_12: |
2366 | 12.8k | case VERIFY_16: |
2367 | | /* We get here only for BYTCHK == 0x01 and only for scsi-block. |
2368 | | * As far as DMA is concerned, we can treat it the same as a write; |
2369 | | * scsi_block_do_sgio will send VERIFY commands. |
2370 | | */ |
2371 | 12.8k | if (s->qdev.scsi_version > 2 && (r->req.cmd.buf[1] & 0xe0)) { |
2372 | 2.00k | goto illegal_request; |
2373 | 2.00k | } |
2374 | 10.8k | if (!check_lba_range(s, r->req.cmd.lba, len)) { |
2375 | 2.61k | goto illegal_lba; |
2376 | 2.61k | } |
2377 | 8.23k | r->sector = r->req.cmd.lba * (s->qdev.blocksize / BDRV_SECTOR_SIZE); |
2378 | 8.23k | r->sector_count = len * (s->qdev.blocksize / BDRV_SECTOR_SIZE); |
2379 | 8.23k | break; |
2380 | 0 | default: |
2381 | 0 | abort(); |
2382 | 2.70k | illegal_request: |
2383 | 2.70k | scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); |
2384 | 2.70k | return 0; |
2385 | 3.97k | illegal_lba: |
2386 | 3.97k | scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); |
2387 | 3.97k | return 0; |
2388 | 18.2k | } |
2389 | 11.5k | r->need_fua = sdc->need_fua(&r->req.cmd); |
2390 | 11.5k | if (r->sector_count == 0) { |
2391 | 2.63k | scsi_req_complete(&r->req, GOOD); |
2392 | 2.63k | } |
2393 | 11.5k | assert(r->iov.iov_len == 0); |
2394 | 11.5k | if (r->req.cmd.mode == SCSI_XFER_TO_DEV) { |
2395 | 7.01k | return -r->sector_count * BDRV_SECTOR_SIZE; |
2396 | 7.01k | } else { |
2397 | 4.50k | return r->sector_count * BDRV_SECTOR_SIZE; |
2398 | 4.50k | } |
2399 | 11.5k | } |
2400 | | |
2401 | | static void scsi_disk_reset(DeviceState *dev) |
2402 | 163k | { |
2403 | 163k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev.qdev, dev); |
2404 | 163k | uint64_t nb_sectors; |
2405 | | |
2406 | 163k | scsi_device_purge_requests(&s->qdev, SENSE_CODE(RESET)); |
2407 | | |
2408 | 163k | blk_get_geometry(s->qdev.conf.blk, &nb_sectors); |
2409 | | |
2410 | 163k | nb_sectors /= s->qdev.blocksize / BDRV_SECTOR_SIZE; |
2411 | 163k | if (nb_sectors) { |
2412 | 163k | nb_sectors--; |
2413 | 163k | } |
2414 | 163k | s->qdev.max_lba = nb_sectors; |
2415 | | /* reset tray statuses */ |
2416 | 163k | s->tray_locked = 0; |
2417 | 163k | s->tray_open = 0; |
2418 | | |
2419 | 163k | s->qdev.scsi_version = s->qdev.default_scsi_version; |
2420 | 163k | } |
2421 | | |
2422 | | static void scsi_disk_drained_begin(void *opaque) |
2423 | 277k | { |
2424 | 277k | SCSIDiskState *s = opaque; |
2425 | | |
2426 | 277k | scsi_device_drained_begin(&s->qdev); |
2427 | 277k | } |
2428 | | |
2429 | | static void scsi_disk_drained_end(void *opaque) |
2430 | 277k | { |
2431 | 277k | SCSIDiskState *s = opaque; |
2432 | | |
2433 | 277k | scsi_device_drained_end(&s->qdev); |
2434 | 277k | } |
2435 | | |
2436 | | static void scsi_disk_resize_cb(void *opaque) |
2437 | 0 | { |
2438 | 0 | SCSIDiskState *s = opaque; |
2439 | | |
2440 | | /* SPC lists this sense code as available only for |
2441 | | * direct-access devices. |
2442 | | */ |
2443 | 0 | if (s->qdev.type == TYPE_DISK) { |
2444 | 0 | scsi_device_report_change(&s->qdev, SENSE_CODE(CAPACITY_CHANGED)); |
2445 | 0 | } |
2446 | 0 | } |
2447 | | |
2448 | | static void scsi_cd_change_media_cb(void *opaque, bool load, Error **errp) |
2449 | 0 | { |
2450 | 0 | SCSIDiskState *s = opaque; |
2451 | | |
2452 | | /* |
2453 | | * When a CD gets changed, we have to report an ejected state and |
2454 | | * then a loaded state to guests so that they detect tray |
2455 | | * open/close and media change events. Guests that do not use |
2456 | | * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close |
2457 | | * states rely on this behavior. |
2458 | | * |
2459 | | * media_changed governs the state machine used for unit attention |
2460 | | * report. media_event is used by GET EVENT STATUS NOTIFICATION. |
2461 | | */ |
2462 | 0 | s->media_changed = load; |
2463 | 0 | s->tray_open = !load; |
2464 | 0 | scsi_device_set_ua(&s->qdev, SENSE_CODE(UNIT_ATTENTION_NO_MEDIUM)); |
2465 | 0 | s->media_event = true; |
2466 | 0 | s->eject_request = false; |
2467 | 0 | } |
2468 | | |
2469 | | static void scsi_cd_eject_request_cb(void *opaque, bool force) |
2470 | 0 | { |
2471 | 0 | SCSIDiskState *s = opaque; |
2472 | |
|
2473 | 0 | s->eject_request = true; |
2474 | 0 | if (force) { |
2475 | 0 | s->tray_locked = false; |
2476 | 0 | } |
2477 | 0 | } |
2478 | | |
2479 | | static bool scsi_cd_is_tray_open(void *opaque) |
2480 | 0 | { |
2481 | 0 | return ((SCSIDiskState *)opaque)->tray_open; |
2482 | 0 | } |
2483 | | |
2484 | | static bool scsi_cd_is_medium_locked(void *opaque) |
2485 | 0 | { |
2486 | 0 | return ((SCSIDiskState *)opaque)->tray_locked; |
2487 | 0 | } |
2488 | | |
2489 | | static const BlockDevOps scsi_disk_removable_block_ops = { |
2490 | | .change_media_cb = scsi_cd_change_media_cb, |
2491 | | .drained_begin = scsi_disk_drained_begin, |
2492 | | .drained_end = scsi_disk_drained_end, |
2493 | | .eject_request_cb = scsi_cd_eject_request_cb, |
2494 | | .is_medium_locked = scsi_cd_is_medium_locked, |
2495 | | .is_tray_open = scsi_cd_is_tray_open, |
2496 | | .resize_cb = scsi_disk_resize_cb, |
2497 | | }; |
2498 | | |
2499 | | static const BlockDevOps scsi_disk_block_ops = { |
2500 | | .drained_begin = scsi_disk_drained_begin, |
2501 | | .drained_end = scsi_disk_drained_end, |
2502 | | .resize_cb = scsi_disk_resize_cb, |
2503 | | }; |
2504 | | |
2505 | | static void scsi_disk_unit_attention_reported(SCSIDevice *dev) |
2506 | 13.0k | { |
2507 | 13.0k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
2508 | 13.0k | if (s->media_changed) { |
2509 | 0 | s->media_changed = false; |
2510 | 0 | scsi_device_set_ua(&s->qdev, SENSE_CODE(MEDIUM_CHANGED)); |
2511 | 0 | } |
2512 | 13.0k | } |
2513 | | |
2514 | | static void scsi_realize(SCSIDevice *dev, Error **errp) |
2515 | 9 | { |
2516 | 9 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
2517 | 9 | bool read_only; |
2518 | | |
2519 | 9 | if (!s->qdev.conf.blk) { |
2520 | 0 | error_setg(errp, "drive property not set"); |
2521 | 0 | return; |
2522 | 0 | } |
2523 | | |
2524 | 9 | if (!(s->features & (1 << SCSI_DISK_F_REMOVABLE)) && |
2525 | 6 | !blk_is_inserted(s->qdev.conf.blk)) { |
2526 | 0 | error_setg(errp, "Device needs media, but drive is empty"); |
2527 | 0 | return; |
2528 | 0 | } |
2529 | | |
2530 | 9 | if (!blkconf_blocksizes(&s->qdev.conf, errp)) { |
2531 | 0 | return; |
2532 | 0 | } |
2533 | | |
2534 | 9 | if (blk_get_aio_context(s->qdev.conf.blk) != qemu_get_aio_context() && |
2535 | 0 | !s->qdev.hba_supports_iothread) |
2536 | 0 | { |
2537 | 0 | error_setg(errp, "HBA does not support iothreads"); |
2538 | 0 | return; |
2539 | 0 | } |
2540 | | |
2541 | 9 | if (dev->type == TYPE_DISK) { |
2542 | 6 | if (!blkconf_geometry(&dev->conf, NULL, 65535, 255, 255, errp)) { |
2543 | 0 | return; |
2544 | 0 | } |
2545 | 6 | } |
2546 | | |
2547 | 9 | read_only = !blk_supports_write_perm(s->qdev.conf.blk); |
2548 | 9 | if (dev->type == TYPE_ROM) { |
2549 | 3 | read_only = true; |
2550 | 3 | } |
2551 | | |
2552 | 9 | if (!blkconf_apply_backend_options(&dev->conf, read_only, |
2553 | 9 | dev->type == TYPE_DISK, errp)) { |
2554 | 0 | return; |
2555 | 0 | } |
2556 | | |
2557 | 9 | if (s->qdev.conf.discard_granularity == -1) { |
2558 | 9 | s->qdev.conf.discard_granularity = |
2559 | 9 | MAX(s->qdev.conf.logical_block_size, DEFAULT_DISCARD_GRANULARITY); |
2560 | 9 | } |
2561 | | |
2562 | 9 | if (!s->version) { |
2563 | 9 | s->version = g_strdup(QEMU_HW_VERSION); |
2564 | 9 | } |
2565 | 9 | if (!s->vendor) { |
2566 | 9 | s->vendor = g_strdup("QEMU"); |
2567 | 9 | } |
2568 | 9 | if (s->serial && strlen(s->serial) > MAX_SERIAL_LEN) { |
2569 | 0 | error_setg(errp, "The serial number can't be longer than %d characters", |
2570 | 0 | MAX_SERIAL_LEN); |
2571 | 0 | return; |
2572 | 0 | } |
2573 | 9 | if (!s->device_id) { |
2574 | 9 | if (s->serial) { |
2575 | 0 | if (strlen(s->serial) > MAX_SERIAL_LEN_FOR_DEVID) { |
2576 | 0 | error_setg(errp, "The serial number can't be longer than %d " |
2577 | 0 | "characters when it is also used as the default for " |
2578 | 0 | "device_id", MAX_SERIAL_LEN_FOR_DEVID); |
2579 | 0 | return; |
2580 | 0 | } |
2581 | 0 | s->device_id = g_strdup(s->serial); |
2582 | 9 | } else { |
2583 | 9 | const char *str = blk_name(s->qdev.conf.blk); |
2584 | 9 | if (str && *str) { |
2585 | 8 | s->device_id = g_strdup(str); |
2586 | 8 | } |
2587 | 9 | } |
2588 | 9 | } |
2589 | | |
2590 | 9 | if (blk_is_sg(s->qdev.conf.blk)) { |
2591 | 0 | error_setg(errp, "unwanted /dev/sg*"); |
2592 | 0 | return; |
2593 | 0 | } |
2594 | | |
2595 | 9 | if ((s->features & (1 << SCSI_DISK_F_REMOVABLE)) && |
2596 | 3 | !(s->features & (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS))) { |
2597 | 3 | blk_set_dev_ops(s->qdev.conf.blk, &scsi_disk_removable_block_ops, s); |
2598 | 6 | } else { |
2599 | 6 | blk_set_dev_ops(s->qdev.conf.blk, &scsi_disk_block_ops, s); |
2600 | 6 | } |
2601 | | |
2602 | 9 | blk_iostatus_enable(s->qdev.conf.blk); |
2603 | | |
2604 | 9 | add_boot_device_lchs(&dev->qdev, NULL, |
2605 | 9 | dev->conf.lcyls, |
2606 | 9 | dev->conf.lheads, |
2607 | 9 | dev->conf.lsecs); |
2608 | 9 | } |
2609 | | |
2610 | | static void scsi_unrealize(SCSIDevice *dev) |
2611 | 0 | { |
2612 | 0 | del_boot_device_lchs(&dev->qdev, NULL); |
2613 | 0 | } |
2614 | | |
2615 | | static void scsi_hd_realize(SCSIDevice *dev, Error **errp) |
2616 | 6 | { |
2617 | 6 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
2618 | | |
2619 | | /* can happen for devices without drive. The error message for missing |
2620 | | * backend will be issued in scsi_realize |
2621 | | */ |
2622 | 6 | if (s->qdev.conf.blk) { |
2623 | 6 | if (!blkconf_blocksizes(&s->qdev.conf, errp)) { |
2624 | 0 | return; |
2625 | 0 | } |
2626 | 6 | } |
2627 | 6 | s->qdev.blocksize = s->qdev.conf.logical_block_size; |
2628 | 6 | s->qdev.type = TYPE_DISK; |
2629 | 6 | if (!s->product) { |
2630 | 6 | s->product = g_strdup("QEMU HARDDISK"); |
2631 | 6 | } |
2632 | 6 | scsi_realize(&s->qdev, errp); |
2633 | 6 | } |
2634 | | |
2635 | | static void scsi_cd_realize(SCSIDevice *dev, Error **errp) |
2636 | 3 | { |
2637 | 3 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
2638 | 3 | int ret; |
2639 | 3 | uint32_t blocksize = 2048; |
2640 | | |
2641 | 3 | if (!dev->conf.blk) { |
2642 | | /* Anonymous BlockBackend for an empty drive. As we put it into |
2643 | | * dev->conf, qdev takes care of detaching on unplug. */ |
2644 | 0 | dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); |
2645 | 0 | ret = blk_attach_dev(dev->conf.blk, &dev->qdev); |
2646 | 0 | assert(ret == 0); |
2647 | 0 | } |
2648 | | |
2649 | 3 | if (dev->conf.physical_block_size != 0) { |
2650 | 0 | blocksize = dev->conf.physical_block_size; |
2651 | 0 | } |
2652 | | |
2653 | 3 | s->qdev.blocksize = blocksize; |
2654 | 3 | s->qdev.type = TYPE_ROM; |
2655 | 3 | s->features |= 1 << SCSI_DISK_F_REMOVABLE; |
2656 | 3 | if (!s->product) { |
2657 | 3 | s->product = g_strdup("QEMU CD-ROM"); |
2658 | 3 | } |
2659 | 3 | scsi_realize(&s->qdev, errp); |
2660 | 3 | } |
2661 | | |
2662 | | |
2663 | | static const SCSIReqOps scsi_disk_emulate_reqops = { |
2664 | | .size = sizeof(SCSIDiskReq), |
2665 | | .free_req = scsi_free_request, |
2666 | | .send_command = scsi_disk_emulate_command, |
2667 | | .read_data = scsi_disk_emulate_read_data, |
2668 | | .write_data = scsi_disk_emulate_write_data, |
2669 | | .get_buf = scsi_get_buf, |
2670 | | .load_request = scsi_disk_emulate_load_request, |
2671 | | .save_request = scsi_disk_emulate_save_request, |
2672 | | }; |
2673 | | |
2674 | | static const SCSIReqOps scsi_disk_dma_reqops = { |
2675 | | .size = sizeof(SCSIDiskReq), |
2676 | | .free_req = scsi_free_request, |
2677 | | .send_command = scsi_disk_dma_command, |
2678 | | .read_data = scsi_read_data, |
2679 | | .write_data = scsi_write_data, |
2680 | | .get_buf = scsi_get_buf, |
2681 | | .load_request = scsi_disk_load_request, |
2682 | | .save_request = scsi_disk_save_request, |
2683 | | }; |
2684 | | |
2685 | | static const SCSIReqOps *const scsi_disk_reqops_dispatch[256] = { |
2686 | | [TEST_UNIT_READY] = &scsi_disk_emulate_reqops, |
2687 | | [INQUIRY] = &scsi_disk_emulate_reqops, |
2688 | | [MODE_SENSE] = &scsi_disk_emulate_reqops, |
2689 | | [MODE_SENSE_10] = &scsi_disk_emulate_reqops, |
2690 | | [START_STOP] = &scsi_disk_emulate_reqops, |
2691 | | [ALLOW_MEDIUM_REMOVAL] = &scsi_disk_emulate_reqops, |
2692 | | [READ_CAPACITY_10] = &scsi_disk_emulate_reqops, |
2693 | | [READ_TOC] = &scsi_disk_emulate_reqops, |
2694 | | [READ_DVD_STRUCTURE] = &scsi_disk_emulate_reqops, |
2695 | | [READ_DISC_INFORMATION] = &scsi_disk_emulate_reqops, |
2696 | | [GET_CONFIGURATION] = &scsi_disk_emulate_reqops, |
2697 | | [GET_EVENT_STATUS_NOTIFICATION] = &scsi_disk_emulate_reqops, |
2698 | | [MECHANISM_STATUS] = &scsi_disk_emulate_reqops, |
2699 | | [SERVICE_ACTION_IN_16] = &scsi_disk_emulate_reqops, |
2700 | | [REQUEST_SENSE] = &scsi_disk_emulate_reqops, |
2701 | | [SYNCHRONIZE_CACHE] = &scsi_disk_emulate_reqops, |
2702 | | [SEEK_10] = &scsi_disk_emulate_reqops, |
2703 | | [MODE_SELECT] = &scsi_disk_emulate_reqops, |
2704 | | [MODE_SELECT_10] = &scsi_disk_emulate_reqops, |
2705 | | [UNMAP] = &scsi_disk_emulate_reqops, |
2706 | | [WRITE_SAME_10] = &scsi_disk_emulate_reqops, |
2707 | | [WRITE_SAME_16] = &scsi_disk_emulate_reqops, |
2708 | | [VERIFY_10] = &scsi_disk_emulate_reqops, |
2709 | | [VERIFY_12] = &scsi_disk_emulate_reqops, |
2710 | | [VERIFY_16] = &scsi_disk_emulate_reqops, |
2711 | | [FORMAT_UNIT] = &scsi_disk_emulate_reqops, |
2712 | | |
2713 | | [READ_6] = &scsi_disk_dma_reqops, |
2714 | | [READ_10] = &scsi_disk_dma_reqops, |
2715 | | [READ_12] = &scsi_disk_dma_reqops, |
2716 | | [READ_16] = &scsi_disk_dma_reqops, |
2717 | | [WRITE_6] = &scsi_disk_dma_reqops, |
2718 | | [WRITE_10] = &scsi_disk_dma_reqops, |
2719 | | [WRITE_12] = &scsi_disk_dma_reqops, |
2720 | | [WRITE_16] = &scsi_disk_dma_reqops, |
2721 | | [WRITE_VERIFY_10] = &scsi_disk_dma_reqops, |
2722 | | [WRITE_VERIFY_12] = &scsi_disk_dma_reqops, |
2723 | | [WRITE_VERIFY_16] = &scsi_disk_dma_reqops, |
2724 | | }; |
2725 | | |
2726 | | static void scsi_disk_new_request_dump(uint32_t lun, uint32_t tag, uint8_t *buf) |
2727 | 0 | { |
2728 | 0 | int len = scsi_cdb_length(buf); |
2729 | 0 | g_autoptr(GString) str = NULL; |
2730 | |
|
2731 | 0 | assert(len > 0 && len <= 16); |
2732 | 0 | str = qemu_hexdump_line(NULL, buf, len, 1, 0); |
2733 | 0 | trace_scsi_disk_new_request(lun, tag, str->str); |
2734 | 0 | } |
2735 | | |
2736 | | static SCSIRequest *scsi_new_request(SCSIDevice *d, uint32_t tag, uint32_t lun, |
2737 | | uint8_t *buf, void *hba_private) |
2738 | 196k | { |
2739 | 196k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); |
2740 | 196k | SCSIRequest *req; |
2741 | 196k | const SCSIReqOps *ops; |
2742 | 196k | uint8_t command; |
2743 | | |
2744 | 196k | command = buf[0]; |
2745 | 196k | ops = scsi_disk_reqops_dispatch[command]; |
2746 | 196k | if (!ops) { |
2747 | 44.0k | ops = &scsi_disk_emulate_reqops; |
2748 | 44.0k | } |
2749 | 196k | req = scsi_req_alloc(ops, &s->qdev, tag, lun, hba_private); |
2750 | | |
2751 | 196k | if (trace_event_get_state_backends(TRACE_SCSI_DISK_NEW_REQUEST)) { |
2752 | 0 | scsi_disk_new_request_dump(lun, tag, buf); |
2753 | 0 | } |
2754 | | |
2755 | 196k | return req; |
2756 | 196k | } |
2757 | | |
2758 | | #ifdef __linux__ |
2759 | | /* |
2760 | | * Preempt on the SCSI Persistent Reservation on the source when migration |
2761 | | * fails because the destination may have already preempted and we need to get |
2762 | | * the reservation back. |
2763 | | */ |
2764 | | static void scsi_block_migration_failed(SCSIDiskState *s) |
2765 | 0 | { |
2766 | 0 | SCSIDevice *d = &s->qdev; |
2767 | 0 | Error *local_err = NULL; |
2768 | |
|
2769 | 0 | if (!scsi_generic_pr_state_preempt(d, &local_err)) { |
2770 | | /* MIG_EVENT_FAILED cannot fail, so just warn */ |
2771 | 0 | error_prepend(&local_err, "scsi-block migration rollback: "); |
2772 | 0 | warn_report_err(local_err); |
2773 | 0 | } |
2774 | 0 | } |
2775 | | |
2776 | | /* |
2777 | | * Unregister keys on the source after migration since the destination has |
2778 | | * preempted and taken over. This cleanup is not necessary at the SCSI level |
2779 | | * but multipathd tracks registered keys and must be informed that this host no |
2780 | | * longer wishes to be registered. |
2781 | | */ |
2782 | | static void scsi_block_migration_done(SCSIDiskState *s) |
2783 | 0 | { |
2784 | 0 | SCSIDevice *d = &s->qdev; |
2785 | 0 | Error *local_err = NULL; |
2786 | |
|
2787 | 0 | if (!scsi_generic_pr_state_cleanup(d, &local_err)) { |
2788 | 0 | error_prepend(&local_err, "scsi-block migration done: "); |
2789 | 0 | warn_report_err(local_err); |
2790 | 0 | } |
2791 | 0 | } |
2792 | | |
2793 | | static int scsi_block_migration_notifier(NotifierWithReturn *notifier, |
2794 | | MigrationEvent *e, Error **errp) |
2795 | 0 | { |
2796 | 0 | SCSIDiskState *s = |
2797 | 0 | container_of(notifier, SCSIDiskState, migration_notifier); |
2798 | |
|
2799 | 0 | if (e->type == MIG_EVENT_DONE) { |
2800 | 0 | scsi_block_migration_done(s); |
2801 | 0 | } else if (e->type == MIG_EVENT_FAILED) { |
2802 | 0 | scsi_block_migration_failed(s); |
2803 | 0 | } |
2804 | 0 | return 0; |
2805 | 0 | } |
2806 | | |
2807 | | static int get_device_type(SCSIDiskState *s) |
2808 | 0 | { |
2809 | 0 | uint8_t cmd[16]; |
2810 | 0 | uint8_t buf[36]; |
2811 | 0 | int ret; |
2812 | |
|
2813 | 0 | memset(cmd, 0, sizeof(cmd)); |
2814 | 0 | memset(buf, 0, sizeof(buf)); |
2815 | 0 | cmd[0] = INQUIRY; |
2816 | 0 | cmd[4] = sizeof(buf); |
2817 | |
|
2818 | 0 | ret = scsi_SG_IO(s->qdev.conf.blk, SG_DXFER_FROM_DEV, cmd, sizeof(cmd), |
2819 | 0 | buf, sizeof(buf), s->qdev.io_timeout, NULL); |
2820 | 0 | if (ret < 0) { |
2821 | 0 | return -1; |
2822 | 0 | } |
2823 | 0 | s->qdev.type = buf[0]; |
2824 | 0 | if (buf[1] & 0x80) { |
2825 | 0 | s->features |= 1 << SCSI_DISK_F_REMOVABLE; |
2826 | 0 | } |
2827 | 0 | return 0; |
2828 | 0 | } |
2829 | | |
2830 | | static void scsi_block_realize(SCSIDevice *dev, Error **errp) |
2831 | 0 | { |
2832 | 0 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
2833 | 0 | int sg_version; |
2834 | 0 | int rc; |
2835 | |
|
2836 | 0 | if (!s->qdev.conf.blk) { |
2837 | 0 | error_setg(errp, "drive property not set"); |
2838 | 0 | return; |
2839 | 0 | } |
2840 | | |
2841 | 0 | if (s->rotation_rate) { |
2842 | 0 | error_report_once("rotation_rate is specified for scsi-block but is " |
2843 | 0 | "not implemented. This option is deprecated and will " |
2844 | 0 | "be removed in a future version"); |
2845 | 0 | } |
2846 | | |
2847 | | /* check we are using a driver managing SG_IO (version 3 and after) */ |
2848 | 0 | rc = blk_ioctl(s->qdev.conf.blk, SG_GET_VERSION_NUM, &sg_version); |
2849 | 0 | if (rc < 0) { |
2850 | 0 | error_setg_errno(errp, -rc, "cannot get SG_IO version number"); |
2851 | 0 | if (rc != -EPERM) { |
2852 | 0 | error_append_hint(errp, "Is this a SCSI device?\n"); |
2853 | 0 | } |
2854 | 0 | return; |
2855 | 0 | } |
2856 | 0 | if (sg_version < 30000) { |
2857 | 0 | error_setg(errp, "scsi generic interface too old"); |
2858 | 0 | return; |
2859 | 0 | } |
2860 | | |
2861 | | /* get device type from INQUIRY data */ |
2862 | 0 | rc = get_device_type(s); |
2863 | 0 | if (rc < 0) { |
2864 | 0 | error_setg(errp, "INQUIRY failed"); |
2865 | 0 | return; |
2866 | 0 | } |
2867 | | |
2868 | | /* Make a guess for the block size, we'll fix it when the guest sends. |
2869 | | * READ CAPACITY. If they don't, they likely would assume these sizes |
2870 | | * anyway. (TODO: check in /sys). |
2871 | | */ |
2872 | 0 | if (s->qdev.type == TYPE_ROM || s->qdev.type == TYPE_WORM) { |
2873 | 0 | s->qdev.blocksize = 2048; |
2874 | 0 | } else { |
2875 | 0 | s->qdev.blocksize = 512; |
2876 | 0 | } |
2877 | | |
2878 | | /* Makes the scsi-block device not removable by using HMP and QMP eject |
2879 | | * command. |
2880 | | */ |
2881 | 0 | s->features |= (1 << SCSI_DISK_F_NO_REMOVABLE_DEVOPS); |
2882 | |
|
2883 | 0 | scsi_realize(&s->qdev, errp); |
2884 | 0 | scsi_generic_read_device_inquiry(&s->qdev); |
2885 | |
|
2886 | 0 | migration_add_notifier(&s->migration_notifier, |
2887 | 0 | scsi_block_migration_notifier); |
2888 | 0 | } |
2889 | | |
2890 | | static void scsi_block_unrealize(SCSIDevice *dev) |
2891 | 0 | { |
2892 | 0 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev); |
2893 | |
|
2894 | 0 | migration_remove_notifier(&s->migration_notifier); |
2895 | 0 | } |
2896 | | |
2897 | | typedef struct SCSIBlockReq { |
2898 | | SCSIDiskReq req; |
2899 | | sg_io_hdr_t io_header; |
2900 | | |
2901 | | /* Selected bytes of the original CDB, copied into our own CDB. */ |
2902 | | uint8_t cmd, cdb1, group_number; |
2903 | | |
2904 | | /* CDB passed to SG_IO. */ |
2905 | | uint8_t cdb[16]; |
2906 | | BlockCompletionFunc *cb; |
2907 | | void *cb_opaque; |
2908 | | } SCSIBlockReq; |
2909 | | |
2910 | | static void scsi_block_sgio_complete(void *opaque, int ret) |
2911 | 0 | { |
2912 | 0 | SCSIBlockReq *req = (SCSIBlockReq *)opaque; |
2913 | 0 | SCSIDiskReq *r = &req->req; |
2914 | 0 | sg_io_hdr_t *io_hdr = &req->io_header; |
2915 | |
|
2916 | 0 | if (ret == 0) { |
2917 | 0 | if (io_hdr->host_status != SCSI_HOST_OK) { |
2918 | 0 | r->req.host_status = io_hdr->host_status; |
2919 | 0 | ret = -ENODEV; |
2920 | 0 | } else if (io_hdr->driver_status & SG_ERR_DRIVER_TIMEOUT) { |
2921 | 0 | ret = BUSY; |
2922 | 0 | } else { |
2923 | 0 | ret = io_hdr->status; |
2924 | 0 | } |
2925 | 0 | } |
2926 | |
|
2927 | 0 | req->cb(req->cb_opaque, ret); |
2928 | 0 | } |
2929 | | |
2930 | | static BlockAIOCB *scsi_block_do_sgio(SCSIBlockReq *req, |
2931 | | int64_t offset, QEMUIOVector *iov, |
2932 | | int direction, |
2933 | | BlockCompletionFunc *cb, void *opaque) |
2934 | 0 | { |
2935 | 0 | sg_io_hdr_t *io_header = &req->io_header; |
2936 | 0 | SCSIDiskReq *r = &req->req; |
2937 | 0 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
2938 | 0 | int nb_logical_blocks; |
2939 | 0 | uint64_t lba; |
2940 | 0 | BlockAIOCB *aiocb; |
2941 | | |
2942 | | /* This is not supported yet. It can only happen if the guest does |
2943 | | * reads and writes that are not aligned to one logical sectors |
2944 | | * _and_ cover multiple MemoryRegions. |
2945 | | */ |
2946 | 0 | assert(offset % s->qdev.blocksize == 0); |
2947 | 0 | assert(iov->size % s->qdev.blocksize == 0); |
2948 | |
|
2949 | 0 | io_header->interface_id = 'S'; |
2950 | | |
2951 | | /* The data transfer comes from the QEMUIOVector. */ |
2952 | 0 | io_header->dxfer_direction = direction; |
2953 | 0 | io_header->dxfer_len = iov->size; |
2954 | 0 | io_header->dxferp = (void *)iov->iov; |
2955 | 0 | io_header->iovec_count = iov->niov; |
2956 | 0 | assert(io_header->iovec_count == iov->niov); /* no overflow! */ |
2957 | | |
2958 | | /* Build a new CDB with the LBA and length patched in, in case |
2959 | | * DMA helpers split the transfer in multiple segments. Do not |
2960 | | * build a CDB smaller than what the guest wanted, and only build |
2961 | | * a larger one if strictly necessary. |
2962 | | */ |
2963 | 0 | io_header->cmdp = req->cdb; |
2964 | 0 | lba = offset / s->qdev.blocksize; |
2965 | 0 | nb_logical_blocks = io_header->dxfer_len / s->qdev.blocksize; |
2966 | |
|
2967 | 0 | if ((req->cmd >> 5) == 0 && lba <= 0x1ffff) { |
2968 | | /* 6-byte CDB */ |
2969 | 0 | stl_be_p(&req->cdb[0], lba | (req->cmd << 24)); |
2970 | 0 | req->cdb[4] = nb_logical_blocks; |
2971 | 0 | req->cdb[5] = 0; |
2972 | 0 | io_header->cmd_len = 6; |
2973 | 0 | } else if ((req->cmd >> 5) <= 1 && lba <= 0xffffffffULL) { |
2974 | | /* 10-byte CDB */ |
2975 | 0 | req->cdb[0] = (req->cmd & 0x1f) | 0x20; |
2976 | 0 | req->cdb[1] = req->cdb1; |
2977 | 0 | stl_be_p(&req->cdb[2], lba); |
2978 | 0 | req->cdb[6] = req->group_number; |
2979 | 0 | stw_be_p(&req->cdb[7], nb_logical_blocks); |
2980 | 0 | req->cdb[9] = 0; |
2981 | 0 | io_header->cmd_len = 10; |
2982 | 0 | } else if ((req->cmd >> 5) != 4 && lba <= 0xffffffffULL) { |
2983 | | /* 12-byte CDB */ |
2984 | 0 | req->cdb[0] = (req->cmd & 0x1f) | 0xA0; |
2985 | 0 | req->cdb[1] = req->cdb1; |
2986 | 0 | stl_be_p(&req->cdb[2], lba); |
2987 | 0 | stl_be_p(&req->cdb[6], nb_logical_blocks); |
2988 | 0 | req->cdb[10] = req->group_number; |
2989 | 0 | req->cdb[11] = 0; |
2990 | 0 | io_header->cmd_len = 12; |
2991 | 0 | } else { |
2992 | | /* 16-byte CDB */ |
2993 | 0 | req->cdb[0] = (req->cmd & 0x1f) | 0x80; |
2994 | 0 | req->cdb[1] = req->cdb1; |
2995 | 0 | stq_be_p(&req->cdb[2], lba); |
2996 | 0 | stl_be_p(&req->cdb[10], nb_logical_blocks); |
2997 | 0 | req->cdb[14] = req->group_number; |
2998 | 0 | req->cdb[15] = 0; |
2999 | 0 | io_header->cmd_len = 16; |
3000 | 0 | } |
3001 | | |
3002 | | /* The rest is as in scsi-generic.c. */ |
3003 | 0 | io_header->mx_sb_len = sizeof(r->req.sense); |
3004 | 0 | io_header->sbp = r->req.sense; |
3005 | 0 | io_header->timeout = s->qdev.io_timeout * 1000; |
3006 | 0 | io_header->usr_ptr = r; |
3007 | 0 | io_header->flags |= SG_FLAG_DIRECT_IO; |
3008 | 0 | req->cb = cb; |
3009 | 0 | req->cb_opaque = opaque; |
3010 | 0 | trace_scsi_disk_aio_sgio_command(r->req.tag, req->cdb[0], lba, |
3011 | 0 | nb_logical_blocks, io_header->timeout); |
3012 | 0 | aiocb = blk_aio_ioctl(s->qdev.conf.blk, SG_IO, io_header, scsi_block_sgio_complete, req); |
3013 | 0 | assert(aiocb != NULL); |
3014 | 0 | return aiocb; |
3015 | 0 | } |
3016 | | |
3017 | | static bool scsi_block_no_fua(SCSICommand *cmd) |
3018 | 0 | { |
3019 | 0 | return false; |
3020 | 0 | } |
3021 | | |
3022 | | static BlockAIOCB *scsi_block_dma_readv(int64_t offset, |
3023 | | QEMUIOVector *iov, |
3024 | | BlockCompletionFunc *cb, void *cb_opaque, |
3025 | | void *opaque) |
3026 | 0 | { |
3027 | 0 | SCSIBlockReq *r = opaque; |
3028 | 0 | return scsi_block_do_sgio(r, offset, iov, |
3029 | 0 | SG_DXFER_FROM_DEV, cb, cb_opaque); |
3030 | 0 | } |
3031 | | |
3032 | | static BlockAIOCB *scsi_block_dma_writev(int64_t offset, |
3033 | | QEMUIOVector *iov, |
3034 | | BlockCompletionFunc *cb, void *cb_opaque, |
3035 | | void *opaque) |
3036 | 0 | { |
3037 | 0 | SCSIBlockReq *r = opaque; |
3038 | 0 | return scsi_block_do_sgio(r, offset, iov, |
3039 | 0 | SG_DXFER_TO_DEV, cb, cb_opaque); |
3040 | 0 | } |
3041 | | |
3042 | | static bool scsi_block_is_passthrough(SCSIDiskState *s, uint8_t *buf) |
3043 | 0 | { |
3044 | 0 | switch (buf[0]) { |
3045 | 0 | case VERIFY_10: |
3046 | 0 | case VERIFY_12: |
3047 | 0 | case VERIFY_16: |
3048 | | /* Check if BYTCHK == 0x01 (data-out buffer contains data |
3049 | | * for the number of logical blocks specified in the length |
3050 | | * field). For other modes, do not use scatter/gather operation. |
3051 | | */ |
3052 | 0 | if ((buf[1] & 6) == 2) { |
3053 | 0 | return false; |
3054 | 0 | } |
3055 | 0 | break; |
3056 | | |
3057 | 0 | case READ_6: |
3058 | 0 | case READ_10: |
3059 | 0 | case READ_12: |
3060 | 0 | case READ_16: |
3061 | 0 | case WRITE_6: |
3062 | 0 | case WRITE_10: |
3063 | 0 | case WRITE_12: |
3064 | 0 | case WRITE_16: |
3065 | 0 | case WRITE_VERIFY_10: |
3066 | 0 | case WRITE_VERIFY_12: |
3067 | 0 | case WRITE_VERIFY_16: |
3068 | | /* MMC writing cannot be done via DMA helpers, because it sometimes |
3069 | | * involves writing beyond the maximum LBA or to negative LBA (lead-in). |
3070 | | * We might use scsi_block_dma_reqops as long as no writing commands are |
3071 | | * seen, but performance usually isn't paramount on optical media. So, |
3072 | | * just make scsi-block operate the same as scsi-generic for them. |
3073 | | */ |
3074 | 0 | if (s->qdev.type != TYPE_ROM) { |
3075 | 0 | return false; |
3076 | 0 | } |
3077 | 0 | break; |
3078 | | |
3079 | 0 | default: |
3080 | 0 | break; |
3081 | 0 | } |
3082 | | |
3083 | 0 | return true; |
3084 | 0 | } |
3085 | | |
3086 | | |
3087 | | static int32_t scsi_block_dma_command(SCSIRequest *req, uint8_t *buf) |
3088 | 0 | { |
3089 | 0 | SCSIBlockReq *r = (SCSIBlockReq *)req; |
3090 | 0 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev); |
3091 | |
|
3092 | 0 | r->cmd = req->cmd.buf[0]; |
3093 | 0 | switch (r->cmd >> 5) { |
3094 | 0 | case 0: |
3095 | | /* 6-byte CDB. */ |
3096 | 0 | r->cdb1 = r->group_number = 0; |
3097 | 0 | break; |
3098 | 0 | case 1: |
3099 | | /* 10-byte CDB. */ |
3100 | 0 | r->cdb1 = req->cmd.buf[1]; |
3101 | 0 | r->group_number = req->cmd.buf[6]; |
3102 | 0 | break; |
3103 | 0 | case 4: |
3104 | | /* 12-byte CDB. */ |
3105 | 0 | r->cdb1 = req->cmd.buf[1]; |
3106 | 0 | r->group_number = req->cmd.buf[10]; |
3107 | 0 | break; |
3108 | 0 | case 5: |
3109 | | /* 16-byte CDB. */ |
3110 | 0 | r->cdb1 = req->cmd.buf[1]; |
3111 | 0 | r->group_number = req->cmd.buf[14]; |
3112 | 0 | break; |
3113 | 0 | default: |
3114 | 0 | abort(); |
3115 | 0 | } |
3116 | | |
3117 | | /* Protection information is not supported. For SCSI versions 2 and |
3118 | | * older (as determined by snooping the guest's INQUIRY commands), |
3119 | | * there is no RD/WR/VRPROTECT, so skip this check in these versions. |
3120 | | */ |
3121 | 0 | if (s->qdev.scsi_version > 2 && (req->cmd.buf[1] & 0xe0)) { |
3122 | 0 | scsi_check_condition(&r->req, SENSE_CODE(INVALID_FIELD)); |
3123 | 0 | return 0; |
3124 | 0 | } |
3125 | | |
3126 | 0 | return scsi_disk_dma_command(req, buf); |
3127 | 0 | } |
3128 | | |
3129 | | static const SCSIReqOps scsi_block_dma_reqops = { |
3130 | | .size = sizeof(SCSIBlockReq), |
3131 | | .free_req = scsi_free_request, |
3132 | | .send_command = scsi_block_dma_command, |
3133 | | .read_data = scsi_read_data, |
3134 | | .write_data = scsi_write_data, |
3135 | | .get_buf = scsi_get_buf, |
3136 | | .load_request = scsi_disk_load_request, |
3137 | | .save_request = scsi_disk_save_request, |
3138 | | }; |
3139 | | |
3140 | | static SCSIRequest *scsi_block_new_request(SCSIDevice *d, uint32_t tag, |
3141 | | uint32_t lun, uint8_t *buf, |
3142 | | void *hba_private) |
3143 | 0 | { |
3144 | 0 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); |
3145 | |
|
3146 | 0 | if (scsi_block_is_passthrough(s, buf)) { |
3147 | 0 | return scsi_req_alloc(&scsi_generic_req_ops, &s->qdev, tag, lun, |
3148 | 0 | hba_private); |
3149 | 0 | } else { |
3150 | 0 | return scsi_req_alloc(&scsi_block_dma_reqops, &s->qdev, tag, lun, |
3151 | 0 | hba_private); |
3152 | 0 | } |
3153 | 0 | } |
3154 | | |
3155 | | static int scsi_block_parse_cdb(SCSIDevice *d, SCSICommand *cmd, |
3156 | | uint8_t *buf, size_t buf_len, |
3157 | | void *hba_private) |
3158 | 0 | { |
3159 | 0 | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, d); |
3160 | |
|
3161 | 0 | if (scsi_block_is_passthrough(s, buf)) { |
3162 | 0 | return scsi_bus_parse_cdb(&s->qdev, cmd, buf, buf_len, hba_private); |
3163 | 0 | } else { |
3164 | 0 | return scsi_req_parse_cdb(&s->qdev, cmd, buf, buf_len); |
3165 | 0 | } |
3166 | 0 | } |
3167 | | |
3168 | | static void scsi_block_update_sense(SCSIRequest *req) |
3169 | 0 | { |
3170 | 0 | SCSIDiskReq *r = DO_UPCAST(SCSIDiskReq, req, req); |
3171 | 0 | SCSIBlockReq *br = DO_UPCAST(SCSIBlockReq, req, r); |
3172 | 0 | r->req.sense_len = MIN(br->io_header.sb_len_wr, sizeof(r->req.sense)); |
3173 | 0 | } |
3174 | | #endif |
3175 | | |
3176 | | static |
3177 | | BlockAIOCB *scsi_dma_readv(int64_t offset, QEMUIOVector *iov, |
3178 | | BlockCompletionFunc *cb, void *cb_opaque, |
3179 | | void *opaque) |
3180 | 17.3k | { |
3181 | 17.3k | SCSIDiskReq *r = opaque; |
3182 | 17.3k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
3183 | 17.3k | return blk_aio_preadv(s->qdev.conf.blk, offset, iov, 0, cb, cb_opaque); |
3184 | 17.3k | } |
3185 | | |
3186 | | static |
3187 | | BlockAIOCB *scsi_dma_writev(int64_t offset, QEMUIOVector *iov, |
3188 | | BlockCompletionFunc *cb, void *cb_opaque, |
3189 | | void *opaque) |
3190 | 38.2k | { |
3191 | 38.2k | SCSIDiskReq *r = opaque; |
3192 | 38.2k | SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, r->req.dev); |
3193 | 38.2k | int flags = r->need_fua ? BDRV_REQ_FUA : 0; |
3194 | 38.2k | return blk_aio_pwritev(s->qdev.conf.blk, offset, iov, flags, cb, cb_opaque); |
3195 | 38.2k | } |
3196 | | |
3197 | | static char *scsi_property_get_loadparm(Object *obj, Error **errp) |
3198 | 0 | { |
3199 | 0 | return g_strdup(SCSI_DISK_BASE(obj)->loadparm); |
3200 | 0 | } |
3201 | | |
3202 | | static void scsi_property_set_loadparm(Object *obj, const char *value, |
3203 | | Error **errp) |
3204 | 0 | { |
3205 | 0 | void *lp_str; |
3206 | |
|
3207 | 0 | if (object_property_get_int(obj, "bootindex", NULL) < 0) { |
3208 | 0 | error_setg(errp, "'loadparm' is only valid for boot devices"); |
3209 | 0 | return; |
3210 | 0 | } |
3211 | | |
3212 | 0 | lp_str = g_malloc0(strlen(value) + 1); |
3213 | 0 | if (!qdev_prop_sanitize_s390x_loadparm(lp_str, value, errp)) { |
3214 | 0 | g_free(lp_str); |
3215 | 0 | return; |
3216 | 0 | } |
3217 | 0 | SCSI_DISK_BASE(obj)->loadparm = lp_str; |
3218 | 0 | } |
3219 | | |
3220 | | static void scsi_property_add_specifics(DeviceClass *dc) |
3221 | 122 | { |
3222 | 122 | ObjectClass *oc = OBJECT_CLASS(dc); |
3223 | | |
3224 | | /* The loadparm property is only supported on s390x */ |
3225 | 122 | if (target_s390x()) { |
3226 | 0 | object_class_property_add_str(oc, "loadparm", |
3227 | 0 | scsi_property_get_loadparm, |
3228 | 0 | scsi_property_set_loadparm); |
3229 | 0 | object_class_property_set_description(oc, "loadparm", |
3230 | 0 | "load parameter (s390x only)"); |
3231 | 0 | } |
3232 | 122 | } |
3233 | | |
3234 | | static void scsi_disk_base_class_initfn(ObjectClass *klass, const void *data) |
3235 | 61 | { |
3236 | 61 | DeviceClass *dc = DEVICE_CLASS(klass); |
3237 | 61 | SCSIDiskClass *sdc = SCSI_DISK_BASE_CLASS(klass); |
3238 | | |
3239 | 61 | dc->fw_name = "disk"; |
3240 | 61 | device_class_set_legacy_reset(dc, scsi_disk_reset); |
3241 | 61 | sdc->dma_readv = scsi_dma_readv; |
3242 | 61 | sdc->dma_writev = scsi_dma_writev; |
3243 | 61 | sdc->need_fua = scsi_is_cmd_fua; |
3244 | 61 | } |
3245 | | |
3246 | | static const TypeInfo scsi_disk_base_info = { |
3247 | | .name = TYPE_SCSI_DISK_BASE, |
3248 | | .parent = TYPE_SCSI_DEVICE, |
3249 | | .class_init = scsi_disk_base_class_initfn, |
3250 | | .instance_size = sizeof(SCSIDiskState), |
3251 | | .class_size = sizeof(SCSIDiskClass), |
3252 | | .abstract = true, |
3253 | | }; |
3254 | | |
3255 | | #define DEFINE_SCSI_DISK_PROPERTIES() \ |
3256 | | DEFINE_PROP_DRIVE_IOTHREAD("drive", SCSIDiskState, qdev.conf.blk), \ |
3257 | | DEFINE_BLOCK_PROPERTIES_BASE(SCSIDiskState, qdev.conf), \ |
3258 | | DEFINE_BLOCK_ERROR_PROPERTIES(SCSIDiskState, qdev.conf), \ |
3259 | | DEFINE_PROP_STRING("ver", SCSIDiskState, version), \ |
3260 | | DEFINE_PROP_STRING("serial", SCSIDiskState, serial), \ |
3261 | | DEFINE_PROP_STRING("vendor", SCSIDiskState, vendor), \ |
3262 | | DEFINE_PROP_STRING("product", SCSIDiskState, product), \ |
3263 | | DEFINE_PROP_STRING("device_id", SCSIDiskState, device_id), \ |
3264 | | DEFINE_PROP_BOOL("migrate-emulated-scsi-request", SCSIDiskState, migrate_emulated_scsi_request, true) |
3265 | | |
3266 | | |
3267 | | static const Property scsi_hd_properties[] = { |
3268 | | DEFINE_SCSI_DISK_PROPERTIES(), |
3269 | | DEFINE_PROP_BIT("removable", SCSIDiskState, features, |
3270 | | SCSI_DISK_F_REMOVABLE, false), |
3271 | | DEFINE_PROP_BIT("dpofua", SCSIDiskState, features, |
3272 | | SCSI_DISK_F_DPOFUA, true), |
3273 | | DEFINE_PROP_UINT64("wwn", SCSIDiskState, qdev.wwn, 0), |
3274 | | DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, qdev.port_wwn, 0), |
3275 | | DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0), |
3276 | | DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size, |
3277 | | DEFAULT_MAX_UNMAP_SIZE), |
3278 | | DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size, |
3279 | | DEFAULT_MAX_IO_SIZE), |
3280 | | DEFINE_PROP_UINT16("rotation_rate", SCSIDiskState, rotation_rate, 0), |
3281 | | DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version, |
3282 | | 5), |
3283 | | DEFINE_PROP_BIT("quirk_mode_page_vendor_specific_apple", SCSIDiskState, |
3284 | | quirks, SCSI_DISK_QUIRK_MODE_PAGE_VENDOR_SPECIFIC_APPLE, |
3285 | | 0), |
3286 | | DEFINE_PROP_BIT("quirk_mode_page_set_block_size", SCSIDiskState, |
3287 | | quirks, SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE, 0), |
3288 | | DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState, qdev.conf), |
3289 | | }; |
3290 | | |
3291 | | #ifdef __linux__ |
3292 | | static bool scsi_disk_pr_state_post_load_errp(void *opaque, int version_id, |
3293 | | Error **errp) |
3294 | 0 | { |
3295 | 0 | SCSIDiskState *s = opaque; |
3296 | 0 | SCSIDevice *dev = &s->qdev; |
3297 | |
|
3298 | 0 | return scsi_generic_pr_state_preempt(dev, errp); |
3299 | 0 | } |
3300 | | |
3301 | | static bool scsi_disk_pr_state_needed(void *opaque) |
3302 | 0 | { |
3303 | 0 | SCSIDiskState *s = opaque; |
3304 | 0 | SCSIPRState *pr_state = &s->qdev.pr_state; |
3305 | 0 | bool ret; |
3306 | |
|
3307 | 0 | if (!s->qdev.migrate_pr) { |
3308 | 0 | return false; |
3309 | 0 | } |
3310 | | |
3311 | | /* A reservation requires a key, so checking this field is enough */ |
3312 | 0 | WITH_QEMU_LOCK_GUARD(&pr_state->mutex) { |
3313 | 0 | ret = pr_state->key; |
3314 | 0 | } |
3315 | 0 | return ret; |
3316 | 0 | } |
3317 | | |
3318 | | static const VMStateDescription vmstate_scsi_disk_pr_state = { |
3319 | | .name = "scsi-disk/pr", |
3320 | | .version_id = 1, |
3321 | | .minimum_version_id = 1, |
3322 | | .post_load_errp = scsi_disk_pr_state_post_load_errp, |
3323 | | .needed = scsi_disk_pr_state_needed, |
3324 | | .fields = (const VMStateField[]) { |
3325 | | VMSTATE_UINT64(qdev.pr_state.key, SCSIDiskState), |
3326 | | VMSTATE_UINT8(qdev.pr_state.resv_type, SCSIDiskState), |
3327 | | VMSTATE_END_OF_LIST() |
3328 | | } |
3329 | | }; |
3330 | | #endif /* __linux__ */ |
3331 | | |
3332 | | static const VMStateDescription vmstate_scsi_disk_state = { |
3333 | | .name = "scsi-disk", |
3334 | | .version_id = 1, |
3335 | | .minimum_version_id = 1, |
3336 | | .fields = (const VMStateField[]) { |
3337 | | VMSTATE_SCSI_DEVICE(qdev, SCSIDiskState), |
3338 | | VMSTATE_BOOL(media_changed, SCSIDiskState), |
3339 | | VMSTATE_BOOL(media_event, SCSIDiskState), |
3340 | | VMSTATE_BOOL(eject_request, SCSIDiskState), |
3341 | | VMSTATE_BOOL(tray_open, SCSIDiskState), |
3342 | | VMSTATE_BOOL(tray_locked, SCSIDiskState), |
3343 | | VMSTATE_END_OF_LIST() |
3344 | | }, |
3345 | | .subsections = (const VMStateDescription * const []) { |
3346 | | #ifdef __linux__ |
3347 | | &vmstate_scsi_disk_pr_state, |
3348 | | #endif |
3349 | | NULL |
3350 | | }, |
3351 | | }; |
3352 | | |
3353 | | static void scsi_hd_class_initfn(ObjectClass *klass, const void *data) |
3354 | 61 | { |
3355 | 61 | DeviceClass *dc = DEVICE_CLASS(klass); |
3356 | 61 | SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); |
3357 | | |
3358 | 61 | sc->realize = scsi_hd_realize; |
3359 | 61 | sc->unrealize = scsi_unrealize; |
3360 | 61 | sc->alloc_req = scsi_new_request; |
3361 | 61 | sc->unit_attention_reported = scsi_disk_unit_attention_reported; |
3362 | 61 | dc->desc = "virtual SCSI disk"; |
3363 | 61 | device_class_set_props(dc, scsi_hd_properties); |
3364 | 61 | dc->vmsd = &vmstate_scsi_disk_state; |
3365 | | |
3366 | 61 | scsi_property_add_specifics(dc); |
3367 | 61 | } |
3368 | | |
3369 | | static const TypeInfo scsi_hd_info = { |
3370 | | .name = "scsi-hd", |
3371 | | .parent = TYPE_SCSI_DISK_BASE, |
3372 | | .class_init = scsi_hd_class_initfn, |
3373 | | }; |
3374 | | |
3375 | | static const Property scsi_cd_properties[] = { |
3376 | | DEFINE_SCSI_DISK_PROPERTIES(), |
3377 | | DEFINE_PROP_UINT64("wwn", SCSIDiskState, qdev.wwn, 0), |
3378 | | DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, qdev.port_wwn, 0), |
3379 | | DEFINE_PROP_UINT16("port_index", SCSIDiskState, port_index, 0), |
3380 | | DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size, |
3381 | | DEFAULT_MAX_IO_SIZE), |
3382 | | DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version, |
3383 | | 5), |
3384 | | DEFINE_PROP_BIT("quirk_mode_page_apple_vendor", SCSIDiskState, quirks, |
3385 | | SCSI_DISK_QUIRK_MODE_PAGE_APPLE_VENDOR, 0), |
3386 | | DEFINE_PROP_BIT("quirk_mode_sense_rom_use_dbd", SCSIDiskState, quirks, |
3387 | | SCSI_DISK_QUIRK_MODE_SENSE_ROM_USE_DBD, 0), |
3388 | | DEFINE_PROP_BIT("quirk_mode_page_vendor_specific_apple", SCSIDiskState, |
3389 | | quirks, SCSI_DISK_QUIRK_MODE_PAGE_VENDOR_SPECIFIC_APPLE, |
3390 | | 0), |
3391 | | DEFINE_PROP_BIT("quirk_mode_page_truncated", SCSIDiskState, quirks, |
3392 | | SCSI_DISK_QUIRK_MODE_PAGE_TRUNCATED, 0), |
3393 | | DEFINE_PROP_BIT("quirk_mode_page_set_block_size", SCSIDiskState, |
3394 | | quirks, SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE, 0), |
3395 | | }; |
3396 | | |
3397 | | static void scsi_cd_class_initfn(ObjectClass *klass, const void *data) |
3398 | 61 | { |
3399 | 61 | DeviceClass *dc = DEVICE_CLASS(klass); |
3400 | 61 | SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); |
3401 | | |
3402 | 61 | sc->realize = scsi_cd_realize; |
3403 | 61 | sc->alloc_req = scsi_new_request; |
3404 | 61 | sc->unit_attention_reported = scsi_disk_unit_attention_reported; |
3405 | 61 | dc->desc = "virtual SCSI CD-ROM"; |
3406 | 61 | device_class_set_props(dc, scsi_cd_properties); |
3407 | 61 | dc->vmsd = &vmstate_scsi_disk_state; |
3408 | | |
3409 | 61 | scsi_property_add_specifics(dc); |
3410 | 61 | } |
3411 | | |
3412 | | static const TypeInfo scsi_cd_info = { |
3413 | | .name = "scsi-cd", |
3414 | | .parent = TYPE_SCSI_DISK_BASE, |
3415 | | .class_init = scsi_cd_class_initfn, |
3416 | | }; |
3417 | | |
3418 | | #ifdef __linux__ |
3419 | | static const Property scsi_block_properties[] = { |
3420 | | DEFINE_BLOCK_ERROR_PROPERTIES(SCSIDiskState, qdev.conf), |
3421 | | DEFINE_PROP_DRIVE("drive", SCSIDiskState, qdev.conf.blk), |
3422 | | DEFINE_PROP_BOOL("share-rw", SCSIDiskState, qdev.conf.share_rw, false), |
3423 | | DEFINE_PROP_UINT16("rotation_rate", SCSIDiskState, rotation_rate, 0), |
3424 | | DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size, |
3425 | | DEFAULT_MAX_UNMAP_SIZE), |
3426 | | DEFINE_PROP_UINT64("max_io_size", SCSIDiskState, max_io_size, |
3427 | | DEFAULT_MAX_IO_SIZE), |
3428 | | DEFINE_PROP_INT32("scsi_version", SCSIDiskState, qdev.default_scsi_version, |
3429 | | -1), |
3430 | | DEFINE_PROP_UINT32("io_timeout", SCSIDiskState, qdev.io_timeout, |
3431 | | DEFAULT_IO_TIMEOUT), |
3432 | | DEFINE_PROP_BOOL("migrate-pr", SCSIDiskState, qdev.migrate_pr, true), |
3433 | | }; |
3434 | | |
3435 | | static void scsi_block_class_initfn(ObjectClass *klass, const void *data) |
3436 | 61 | { |
3437 | 61 | DeviceClass *dc = DEVICE_CLASS(klass); |
3438 | 61 | SCSIDeviceClass *sc = SCSI_DEVICE_CLASS(klass); |
3439 | 61 | SCSIDiskClass *sdc = SCSI_DISK_BASE_CLASS(klass); |
3440 | | |
3441 | 61 | sc->realize = scsi_block_realize; |
3442 | 61 | sc->unrealize = scsi_block_unrealize; |
3443 | 61 | sc->alloc_req = scsi_block_new_request; |
3444 | 61 | sc->parse_cdb = scsi_block_parse_cdb; |
3445 | 61 | sdc->dma_readv = scsi_block_dma_readv; |
3446 | 61 | sdc->dma_writev = scsi_block_dma_writev; |
3447 | 61 | sdc->update_sense = scsi_block_update_sense; |
3448 | 61 | sdc->need_fua = scsi_block_no_fua; |
3449 | 61 | dc->desc = "SCSI block device passthrough"; |
3450 | 61 | device_class_set_props(dc, scsi_block_properties); |
3451 | 61 | dc->vmsd = &vmstate_scsi_disk_state; |
3452 | 61 | } |
3453 | | |
3454 | | static const TypeInfo scsi_block_info = { |
3455 | | .name = "scsi-block", |
3456 | | .parent = TYPE_SCSI_DISK_BASE, |
3457 | | .class_init = scsi_block_class_initfn, |
3458 | | }; |
3459 | | #endif |
3460 | | |
3461 | | static void scsi_disk_register_types(void) |
3462 | 61 | { |
3463 | 61 | type_register_static(&scsi_disk_base_info); |
3464 | 61 | type_register_static(&scsi_hd_info); |
3465 | 61 | type_register_static(&scsi_cd_info); |
3466 | 61 | #ifdef __linux__ |
3467 | 61 | type_register_static(&scsi_block_info); |
3468 | 61 | #endif |
3469 | 61 | } |
3470 | | |
3471 | | type_init(scsi_disk_register_types) |