source: mainline/uspace/drv/bus/usb/usbmast/scsi_ms.c@ 495547d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 495547d was ce25903, checked in by Jan Vesely <jano.vesely@…>, 10 years ago

ubsmast: Add Test Unit Ready command

Fixes my two flash drive

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