1 | /*
|
---|
2 | * SPDX-FileCopyrightText: 2011 Vojtech Horky
|
---|
3 | * SPDX-FileCopyrightText: 2011 Jiri Svoboda
|
---|
4 | * SPDX-FileCopyrightText: 2018 Ondrej Hlavaty
|
---|
5 | *
|
---|
6 | * SPDX-License-Identifier: BSD-3-Clause
|
---|
7 | */
|
---|
8 |
|
---|
9 | /** @addtogroup drvusbmast
|
---|
10 | * @{
|
---|
11 | */
|
---|
12 | /**
|
---|
13 | * @file
|
---|
14 | * SCSI functions for USB mass storage driver.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #include <bitops.h>
|
---|
18 | #include <byteorder.h>
|
---|
19 | #include <inttypes.h>
|
---|
20 | #include <macros.h>
|
---|
21 | #include <usb/dev/driver.h>
|
---|
22 | #include <usb/debug.h>
|
---|
23 | #include <errno.h>
|
---|
24 | #include <str_error.h>
|
---|
25 | #include <str.h>
|
---|
26 | #include <scsi/sbc.h>
|
---|
27 | #include <scsi/spc.h>
|
---|
28 | #include "cmdw.h"
|
---|
29 | #include "bo_trans.h"
|
---|
30 | #include "scsi_ms.h"
|
---|
31 | #include "usbmast.h"
|
---|
32 |
|
---|
33 | /** Get string representation for SCSI peripheral device type.
|
---|
34 | *
|
---|
35 | * @param type SCSI peripheral device type code.
|
---|
36 | * @return String representation.
|
---|
37 | */
|
---|
38 | const char *usbmast_scsi_dev_type_str(unsigned type)
|
---|
39 | {
|
---|
40 | return scsi_get_dev_type_str(type);
|
---|
41 | }
|
---|
42 |
|
---|
43 | static void usbmast_dump_sense(scsi_sense_data_t *sense_buf)
|
---|
44 | {
|
---|
45 | const unsigned sense_key = sense_buf->flags_key & 0x0f;
|
---|
46 | printf("Got sense data. Sense key: 0x%x (%s), ASC 0x%02x, "
|
---|
47 | "ASCQ 0x%02x.\n", sense_key,
|
---|
48 | scsi_get_sense_key_str(sense_key),
|
---|
49 | sense_buf->additional_code,
|
---|
50 | sense_buf->additional_cqual);
|
---|
51 | }
|
---|
52 |
|
---|
53 | static errno_t usb_massstor_unit_ready(usbmast_fun_t *mfun)
|
---|
54 | {
|
---|
55 | scsi_cmd_t cmd;
|
---|
56 | scsi_cdb_test_unit_ready_t cdb;
|
---|
57 | errno_t rc;
|
---|
58 |
|
---|
59 | memset(&cdb, 0, sizeof(cdb));
|
---|
60 | cdb.op_code = SCSI_CMD_TEST_UNIT_READY;
|
---|
61 |
|
---|
62 | memset(&cmd, 0, sizeof(cmd));
|
---|
63 | cmd.cdb = &cdb;
|
---|
64 | cmd.cdb_size = sizeof(cdb);
|
---|
65 |
|
---|
66 | rc = usb_massstor_cmd(mfun, 0xDEADBEEF, &cmd);
|
---|
67 |
|
---|
68 | if (rc != EOK) {
|
---|
69 | usb_log_error("Test Unit Ready failed on device %s: %s.",
|
---|
70 | usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
|
---|
71 | return rc;
|
---|
72 | }
|
---|
73 | /*
|
---|
74 | * Ignore command error here. If there's something wrong
|
---|
75 | * with the device the following commands will fail too.
|
---|
76 | */
|
---|
77 | if (cmd.status != CMDS_GOOD)
|
---|
78 | usb_log_warning("Test Unit Ready command failed on device %s.",
|
---|
79 | usb_device_get_name(mfun->mdev->usb_dev));
|
---|
80 |
|
---|
81 | return EOK;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Run SCSI command.
|
---|
85 | *
|
---|
86 | * Run command and repeat in case of unit attention.
|
---|
87 | * XXX This is too simplified.
|
---|
88 | */
|
---|
89 | static errno_t usbmast_run_cmd(usbmast_fun_t *mfun, scsi_cmd_t *cmd)
|
---|
90 | {
|
---|
91 | uint8_t sense_key;
|
---|
92 | scsi_sense_data_t sense_buf;
|
---|
93 | errno_t rc;
|
---|
94 |
|
---|
95 | do {
|
---|
96 | rc = usb_massstor_unit_ready(mfun);
|
---|
97 | if (rc != EOK) {
|
---|
98 | usb_log_error("Inquiry transport failed, device %s: %s.",
|
---|
99 | usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
|
---|
100 | return rc;
|
---|
101 | }
|
---|
102 |
|
---|
103 | rc = usb_massstor_cmd(mfun, 0xDEADBEEF, cmd);
|
---|
104 | if (rc != EOK) {
|
---|
105 | usb_log_error("Inquiry transport failed, device %s: %s.",
|
---|
106 | usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
|
---|
107 | return rc;
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (cmd->status == CMDS_GOOD)
|
---|
111 | return EOK;
|
---|
112 |
|
---|
113 | usb_log_error("SCSI command failed, device %s.",
|
---|
114 | usb_device_get_name(mfun->mdev->usb_dev));
|
---|
115 |
|
---|
116 | rc = usbmast_request_sense(mfun, &sense_buf, sizeof(sense_buf));
|
---|
117 | if (rc != EOK) {
|
---|
118 | usb_log_error("Failed to read sense data.");
|
---|
119 | return EIO;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /* Dump sense data to log */
|
---|
123 | usbmast_dump_sense(&sense_buf);
|
---|
124 |
|
---|
125 | /* Get sense key */
|
---|
126 | sense_key = sense_buf.flags_key & 0x0f;
|
---|
127 |
|
---|
128 | if (sense_key == SCSI_SK_UNIT_ATTENTION) {
|
---|
129 | printf("Got unit attention. Re-trying command.\n");
|
---|
130 | }
|
---|
131 |
|
---|
132 | } while (sense_key == SCSI_SK_UNIT_ATTENTION);
|
---|
133 |
|
---|
134 | /* Command status is not good, nevertheless transport succeeded. */
|
---|
135 | return EOK;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** Perform SCSI Inquiry command on USB mass storage device.
|
---|
139 | *
|
---|
140 | * @param mfun Mass storage function
|
---|
141 | * @param inquiry_result Where to store parsed inquiry result
|
---|
142 | * @return Error code
|
---|
143 | */
|
---|
144 | errno_t usbmast_inquiry(usbmast_fun_t *mfun, usbmast_inquiry_data_t *inq_res)
|
---|
145 | {
|
---|
146 | scsi_std_inquiry_data_t inq_data;
|
---|
147 | scsi_cmd_t cmd;
|
---|
148 | scsi_cdb_inquiry_t cdb;
|
---|
149 | errno_t rc;
|
---|
150 |
|
---|
151 | memset(&cdb, 0, sizeof(cdb));
|
---|
152 | cdb.op_code = SCSI_CMD_INQUIRY;
|
---|
153 | cdb.alloc_len = host2uint16_t_be(sizeof(inq_data));
|
---|
154 |
|
---|
155 | memset(&cmd, 0, sizeof(cmd));
|
---|
156 | cmd.cdb = &cdb;
|
---|
157 | cmd.cdb_size = sizeof(cdb);
|
---|
158 | cmd.data_in = &inq_data;
|
---|
159 | cmd.data_in_size = sizeof(inq_data);
|
---|
160 |
|
---|
161 | rc = usb_massstor_cmd(mfun, 0xDEADBEEF, &cmd);
|
---|
162 |
|
---|
163 | if (rc != EOK) {
|
---|
164 | usb_log_error("Inquiry transport failed, device %s: %s.",
|
---|
165 | usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
|
---|
166 | return rc;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (cmd.status != CMDS_GOOD) {
|
---|
170 | usb_log_error("Inquiry command failed, device %s.",
|
---|
171 | usb_device_get_name(mfun->mdev->usb_dev));
|
---|
172 | return EIO;
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (cmd.rcvd_size < SCSI_STD_INQUIRY_DATA_MIN_SIZE) {
|
---|
176 | usb_log_error("SCSI Inquiry response too short (%zu).",
|
---|
177 | cmd.rcvd_size);
|
---|
178 | return EIO;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * Parse inquiry data and fill in the result structure.
|
---|
183 | */
|
---|
184 |
|
---|
185 | memset(inq_res, 0, sizeof(*inq_res));
|
---|
186 |
|
---|
187 | inq_res->device_type = BIT_RANGE_EXTRACT(uint8_t,
|
---|
188 | inq_data.pqual_devtype, SCSI_PQDT_DEV_TYPE_h, SCSI_PQDT_DEV_TYPE_l);
|
---|
189 |
|
---|
190 | inq_res->removable = BIT_RANGE_EXTRACT(uint8_t,
|
---|
191 | inq_data.rmb, SCSI_RMB_RMB, SCSI_RMB_RMB);
|
---|
192 |
|
---|
193 | spascii_to_str(inq_res->vendor, SCSI_INQ_VENDOR_STR_BUFSIZE,
|
---|
194 | inq_data.vendor, sizeof(inq_data.vendor));
|
---|
195 |
|
---|
196 | spascii_to_str(inq_res->product, SCSI_INQ_PRODUCT_STR_BUFSIZE,
|
---|
197 | inq_data.product, sizeof(inq_data.product));
|
---|
198 |
|
---|
199 | spascii_to_str(inq_res->revision, SCSI_INQ_REVISION_STR_BUFSIZE,
|
---|
200 | inq_data.revision, sizeof(inq_data.revision));
|
---|
201 |
|
---|
202 | return EOK;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /** Perform SCSI Request Sense command on USB mass storage device.
|
---|
206 | *
|
---|
207 | * @param mfun Mass storage function
|
---|
208 | * @param buf Destination buffer
|
---|
209 | * @param size Size of @a buf
|
---|
210 | *
|
---|
211 | * @return Error code.
|
---|
212 | */
|
---|
213 | errno_t usbmast_request_sense(usbmast_fun_t *mfun, void *buf, size_t size)
|
---|
214 | {
|
---|
215 | scsi_cmd_t cmd;
|
---|
216 | scsi_cdb_request_sense_t cdb;
|
---|
217 | errno_t rc;
|
---|
218 |
|
---|
219 | memset(&cdb, 0, sizeof(cdb));
|
---|
220 | cdb.op_code = SCSI_CMD_REQUEST_SENSE;
|
---|
221 | cdb.alloc_len = min(size, SCSI_SENSE_DATA_MAX_SIZE);
|
---|
222 |
|
---|
223 | memset(&cmd, 0, sizeof(cmd));
|
---|
224 | cmd.cdb = &cdb;
|
---|
225 | cmd.cdb_size = sizeof(cdb);
|
---|
226 | cmd.data_in = buf;
|
---|
227 | cmd.data_in_size = size;
|
---|
228 |
|
---|
229 | rc = usb_massstor_cmd(mfun, 0xDEADBEEF, &cmd);
|
---|
230 |
|
---|
231 | if (rc != EOK || cmd.status != CMDS_GOOD) {
|
---|
232 | usb_log_error("Request Sense failed, device %s: %s.",
|
---|
233 | usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
|
---|
234 | return rc;
|
---|
235 | }
|
---|
236 |
|
---|
237 | if (cmd.rcvd_size < SCSI_SENSE_DATA_MIN_SIZE) {
|
---|
238 | /* The missing bytes should be considered to be zeroes. */
|
---|
239 | memset((uint8_t *)buf + cmd.rcvd_size, 0,
|
---|
240 | SCSI_SENSE_DATA_MIN_SIZE - cmd.rcvd_size);
|
---|
241 | }
|
---|
242 |
|
---|
243 | return EOK;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /** Perform SCSI Read Capacity command on USB mass storage device.
|
---|
247 | *
|
---|
248 | * @param mfun Mass storage function
|
---|
249 | * @param nblocks Output, number of blocks
|
---|
250 | * @param block_size Output, block size in bytes
|
---|
251 | *
|
---|
252 | * @return Error code.
|
---|
253 | */
|
---|
254 | errno_t usbmast_read_capacity(usbmast_fun_t *mfun, uint32_t *nblocks,
|
---|
255 | uint32_t *block_size)
|
---|
256 | {
|
---|
257 | scsi_cmd_t cmd;
|
---|
258 | scsi_cdb_read_capacity_10_t cdb;
|
---|
259 | scsi_read_capacity_10_data_t data;
|
---|
260 | errno_t rc;
|
---|
261 |
|
---|
262 | memset(&cdb, 0, sizeof(cdb));
|
---|
263 | cdb.op_code = SCSI_CMD_READ_CAPACITY_10;
|
---|
264 |
|
---|
265 | memset(&cmd, 0, sizeof(cmd));
|
---|
266 | cmd.cdb = &cdb;
|
---|
267 | cmd.cdb_size = sizeof(cdb);
|
---|
268 | cmd.data_in = &data;
|
---|
269 | cmd.data_in_size = sizeof(data);
|
---|
270 |
|
---|
271 | rc = usbmast_run_cmd(mfun, &cmd);
|
---|
272 |
|
---|
273 | if (rc != EOK) {
|
---|
274 | usb_log_error("Read Capacity (10) transport failed, device %s: %s.",
|
---|
275 | usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
|
---|
276 | return rc;
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (cmd.status != CMDS_GOOD) {
|
---|
280 | usb_log_error("Read Capacity (10) command failed, device %s.",
|
---|
281 | usb_device_get_name(mfun->mdev->usb_dev));
|
---|
282 | return EIO;
|
---|
283 | }
|
---|
284 |
|
---|
285 | if (cmd.rcvd_size < sizeof(data)) {
|
---|
286 | usb_log_error("SCSI Read Capacity response too short (%zu).",
|
---|
287 | cmd.rcvd_size);
|
---|
288 | return EIO;
|
---|
289 | }
|
---|
290 |
|
---|
291 | *nblocks = uint32_t_be2host(data.last_lba) + 1;
|
---|
292 | *block_size = uint32_t_be2host(data.block_size);
|
---|
293 |
|
---|
294 | return EOK;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /** Perform SCSI Read command on USB mass storage device.
|
---|
298 | *
|
---|
299 | * @param mfun Mass storage function
|
---|
300 | * @param ba Address of first block
|
---|
301 | * @param nblocks Number of blocks to read
|
---|
302 | *
|
---|
303 | * @return Error code
|
---|
304 | */
|
---|
305 | errno_t usbmast_read(usbmast_fun_t *mfun, uint64_t ba, size_t nblocks, void *buf)
|
---|
306 | {
|
---|
307 | scsi_cmd_t cmd;
|
---|
308 | scsi_cdb_read_10_t cdb;
|
---|
309 | errno_t rc;
|
---|
310 |
|
---|
311 | if (ba > UINT32_MAX)
|
---|
312 | return ELIMIT;
|
---|
313 |
|
---|
314 | if (nblocks > UINT16_MAX)
|
---|
315 | return ELIMIT;
|
---|
316 |
|
---|
317 | memset(&cdb, 0, sizeof(cdb));
|
---|
318 | cdb.op_code = SCSI_CMD_READ_10;
|
---|
319 | cdb.lba = host2uint32_t_be(ba);
|
---|
320 | cdb.xfer_len = host2uint16_t_be(nblocks);
|
---|
321 |
|
---|
322 | memset(&cmd, 0, sizeof(cmd));
|
---|
323 | cmd.cdb = &cdb;
|
---|
324 | cmd.cdb_size = sizeof(cdb);
|
---|
325 | cmd.data_in = buf;
|
---|
326 | cmd.data_in_size = nblocks * mfun->block_size;
|
---|
327 |
|
---|
328 | rc = usbmast_run_cmd(mfun, &cmd);
|
---|
329 |
|
---|
330 | if (rc != EOK) {
|
---|
331 | usb_log_error("Read (10) transport failed, device %s: %s.",
|
---|
332 | usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
|
---|
333 | return rc;
|
---|
334 | }
|
---|
335 |
|
---|
336 | if (cmd.status != CMDS_GOOD) {
|
---|
337 | usb_log_error("Read (10) command failed, device %s.",
|
---|
338 | usb_device_get_name(mfun->mdev->usb_dev));
|
---|
339 | return EIO;
|
---|
340 | }
|
---|
341 |
|
---|
342 | if (cmd.rcvd_size < nblocks * mfun->block_size) {
|
---|
343 | usb_log_error("SCSI Read response too short (%zu).",
|
---|
344 | cmd.rcvd_size);
|
---|
345 | return EIO;
|
---|
346 | }
|
---|
347 |
|
---|
348 | return EOK;
|
---|
349 | }
|
---|
350 |
|
---|
351 | /** Perform SCSI Write command on USB mass storage device.
|
---|
352 | *
|
---|
353 | * @param mfun Mass storage function
|
---|
354 | * @param ba Address of first block
|
---|
355 | * @param nblocks Number of blocks to read
|
---|
356 | * @param data Data to write
|
---|
357 | *
|
---|
358 | * @return Error code
|
---|
359 | */
|
---|
360 | errno_t usbmast_write(usbmast_fun_t *mfun, uint64_t ba, size_t nblocks,
|
---|
361 | const void *data)
|
---|
362 | {
|
---|
363 | scsi_cmd_t cmd;
|
---|
364 | scsi_cdb_write_10_t cdb;
|
---|
365 | errno_t rc;
|
---|
366 |
|
---|
367 | if (ba > UINT32_MAX)
|
---|
368 | return ELIMIT;
|
---|
369 |
|
---|
370 | if (nblocks > UINT16_MAX)
|
---|
371 | return ELIMIT;
|
---|
372 |
|
---|
373 | memset(&cdb, 0, sizeof(cdb));
|
---|
374 | cdb.op_code = SCSI_CMD_WRITE_10;
|
---|
375 | cdb.lba = host2uint32_t_be(ba);
|
---|
376 | cdb.xfer_len = host2uint16_t_be(nblocks);
|
---|
377 |
|
---|
378 | memset(&cmd, 0, sizeof(cmd));
|
---|
379 | cmd.cdb = &cdb;
|
---|
380 | cmd.cdb_size = sizeof(cdb);
|
---|
381 | cmd.data_out = data;
|
---|
382 | cmd.data_out_size = nblocks * mfun->block_size;
|
---|
383 |
|
---|
384 | rc = usbmast_run_cmd(mfun, &cmd);
|
---|
385 |
|
---|
386 | if (rc != EOK) {
|
---|
387 | usb_log_error("Write (10) transport failed, device %s: %s.",
|
---|
388 | usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
|
---|
389 | return rc;
|
---|
390 | }
|
---|
391 |
|
---|
392 | if (cmd.status != CMDS_GOOD) {
|
---|
393 | usb_log_error("Write (10) command failed, device %s.",
|
---|
394 | usb_device_get_name(mfun->mdev->usb_dev));
|
---|
395 | return EIO;
|
---|
396 | }
|
---|
397 |
|
---|
398 | return EOK;
|
---|
399 | }
|
---|
400 |
|
---|
401 | /** Perform SCSI Synchronize Cache command on USB mass storage device.
|
---|
402 | *
|
---|
403 | * @param mfun Mass storage function
|
---|
404 | * @param ba Address of first block
|
---|
405 | * @param nblocks Number of blocks to read
|
---|
406 | * @param data Data to write
|
---|
407 | *
|
---|
408 | * @return Error code
|
---|
409 | */
|
---|
410 | errno_t usbmast_sync_cache(usbmast_fun_t *mfun, uint64_t ba, size_t nblocks)
|
---|
411 | {
|
---|
412 | if (ba > UINT32_MAX)
|
---|
413 | return ELIMIT;
|
---|
414 |
|
---|
415 | if (nblocks > UINT16_MAX)
|
---|
416 | return ELIMIT;
|
---|
417 |
|
---|
418 | const scsi_cdb_sync_cache_10_t cdb = {
|
---|
419 | .op_code = SCSI_CMD_SYNC_CACHE_10,
|
---|
420 | .lba = host2uint32_t_be(ba),
|
---|
421 | .numlb = host2uint16_t_be(nblocks),
|
---|
422 | };
|
---|
423 |
|
---|
424 | scsi_cmd_t cmd = {
|
---|
425 | .cdb = &cdb,
|
---|
426 | .cdb_size = sizeof(cdb),
|
---|
427 | };
|
---|
428 |
|
---|
429 | const errno_t rc = usbmast_run_cmd(mfun, &cmd);
|
---|
430 |
|
---|
431 | if (rc != EOK) {
|
---|
432 | usb_log_error("Synchronize Cache (10) transport failed, device %s: %s.",
|
---|
433 | usb_device_get_name(mfun->mdev->usb_dev), str_error(rc));
|
---|
434 | return rc;
|
---|
435 | }
|
---|
436 |
|
---|
437 | if (cmd.status != CMDS_GOOD) {
|
---|
438 | usb_log_error("Synchronize Cache (10) command failed, device %s.",
|
---|
439 | usb_device_get_name(mfun->mdev->usb_dev));
|
---|
440 | return EIO;
|
---|
441 | }
|
---|
442 |
|
---|
443 | return EOK;
|
---|
444 | }
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * @}
|
---|
448 | */
|
---|