source: mainline/uspace/drv/bus/usb/usbmast/scsi_ms.c@ 45cae6b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 45cae6b was 45cae6b, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Work on USB mass storage:

  • handle pipe stalls when transferring data
  • distinguish between transport failure and failed command (i.e. check condition)
  • when a command fails, read sense data and log sense key, ASC, ASCQ
  • Property mode set to 100644
File size: 8.9 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(usbmast_fun_t *mfun)
64{
65 scsi_sense_data_t sense_buf;
66 int rc;
67
68 rc = usbmast_request_sense(mfun, &sense_buf, sizeof(sense_buf));
69 if (rc == EOK) {
70 printf("Got sense data (sense_key=0x%x, asc=0x%02x, "
71 "ascq=0x%02x.\n", sense_buf.flags_key & 0x0f,
72 sense_buf.additional_code,
73 sense_buf.additional_cqual);
74 } else {
75 printf("Failed to read sense data.\n");
76 }
77}
78
79/** Perform SCSI Inquiry command on USB mass storage device.
80 *
81 * @param mfun Mass storage function
82 * @param inquiry_result Where to store parsed inquiry result
83 * @return Error code
84 */
85int usbmast_inquiry(usbmast_fun_t *mfun, usbmast_inquiry_data_t *inq_res)
86{
87 scsi_std_inquiry_data_t inq_data;
88 size_t response_len;
89 scsi_cdb_inquiry_t cdb;
90 cmd_status_t status;
91 int rc;
92
93 memset(&cdb, 0, sizeof(cdb));
94 cdb.op_code = SCSI_CMD_INQUIRY;
95 cdb.alloc_len = host2uint16_t_be(sizeof(inq_data));
96
97 rc = usb_massstor_data_in(mfun, 0xDEADBEEF, (uint8_t *) &cdb,
98 sizeof(cdb), &inq_data, sizeof(inq_data), &response_len, &status);
99
100 if (rc != EOK) {
101 usb_log_error("Inquiry transport failed, device %s: %s.\n",
102 mfun->mdev->ddf_dev->name, str_error(rc));
103 return rc;
104 }
105
106 if (status != CMDS_GOOD) {
107 usb_log_error("Inquiry command failed, device %s.\n",
108 mfun->mdev->ddf_dev->name);
109 usbmast_dump_sense(mfun);
110 return EIO;
111 }
112
113 if (response_len < SCSI_STD_INQUIRY_DATA_MIN_SIZE) {
114 usb_log_error("SCSI Inquiry response too short (%zu).\n",
115 response_len);
116 return EIO;
117 }
118
119 /*
120 * Parse inquiry data and fill in the result structure.
121 */
122
123 bzero(inq_res, sizeof(*inq_res));
124
125 inq_res->device_type = BIT_RANGE_EXTRACT(uint8_t,
126 inq_data.pqual_devtype, SCSI_PQDT_DEV_TYPE_h, SCSI_PQDT_DEV_TYPE_l);
127
128 inq_res->removable = BIT_RANGE_EXTRACT(uint8_t,
129 inq_data.rmb, SCSI_RMB_RMB, SCSI_RMB_RMB);
130
131 spascii_to_str(inq_res->vendor, SCSI_INQ_VENDOR_STR_BUFSIZE,
132 inq_data.vendor, sizeof(inq_data.vendor));
133
134 spascii_to_str(inq_res->product, SCSI_INQ_PRODUCT_STR_BUFSIZE,
135 inq_data.product, sizeof(inq_data.product));
136
137 spascii_to_str(inq_res->revision, SCSI_INQ_REVISION_STR_BUFSIZE,
138 inq_data.revision, sizeof(inq_data.revision));
139
140 return EOK;
141}
142
143/** Perform SCSI Request Sense command on USB mass storage device.
144 *
145 * @param mfun Mass storage function
146 * @param buf Destination buffer
147 * @param size Size of @a buf
148 *
149 * @return Error code.
150 */
151int usbmast_request_sense(usbmast_fun_t *mfun, void *buf, size_t size)
152{
153 scsi_cdb_request_sense_t cdb;
154 size_t data_len;
155 cmd_status_t status;
156 int rc;
157
158 memset(&cdb, 0, sizeof(cdb));
159 cdb.op_code = SCSI_CMD_REQUEST_SENSE;
160 cdb.alloc_len = min(size, SCSI_SENSE_DATA_MAX_SIZE);
161
162 rc = usb_massstor_data_in(mfun, 0xDEADBEEF, (uint8_t *) &cdb,
163 sizeof(cdb), buf, size, &data_len, &status);
164
165 if (rc != EOK || status != CMDS_GOOD) {
166 usb_log_error("Request Sense failed, device %s: %s.\n",
167 mfun->mdev->ddf_dev->name, str_error(rc));
168 return rc;
169 }
170
171 if (data_len < SCSI_SENSE_DATA_MIN_SIZE) {
172 /* The missing bytes should be considered to be zeroes. */
173 memset((uint8_t *)buf + data_len, 0,
174 SCSI_SENSE_DATA_MIN_SIZE - data_len);
175 }
176
177 return EOK;
178}
179
180/** Perform SCSI Read Capacity command on USB mass storage device.
181 *
182 * @param mfun Mass storage function
183 * @param nblocks Output, number of blocks
184 * @param block_size Output, block size in bytes
185 *
186 * @return Error code.
187 */
188int usbmast_read_capacity(usbmast_fun_t *mfun, uint32_t *nblocks,
189 uint32_t *block_size)
190{
191 scsi_cdb_read_capacity_10_t cdb;
192 scsi_read_capacity_10_data_t data;
193 size_t data_len;
194 cmd_status_t status;
195 int rc;
196
197 memset(&cdb, 0, sizeof(cdb));
198 cdb.op_code = SCSI_CMD_READ_CAPACITY_10;
199
200 rc = usb_massstor_data_in(mfun, 0xDEADBEEF, (uint8_t *) &cdb,
201 sizeof(cdb), &data, sizeof(data), &data_len, &status);
202
203 if (rc != EOK) {
204 usb_log_error("Read Capacity (10) transport failed, device %s: %s.\n",
205 mfun->mdev->ddf_dev->name, str_error(rc));
206 return rc;
207 }
208
209 if (status != CMDS_GOOD) {
210 usb_log_error("Read Capacity (10) command failed, device %s.\n",
211 mfun->mdev->ddf_dev->name);
212 usbmast_dump_sense(mfun);
213 return EIO;
214 }
215
216 if (data_len < sizeof(data)) {
217 usb_log_error("SCSI Read Capacity response too short (%zu).\n",
218 data_len);
219 return EIO;
220 }
221
222 *nblocks = uint32_t_be2host(data.last_lba) + 1;
223 *block_size = uint32_t_be2host(data.block_size);
224
225 return EOK;
226}
227
228/** Perform SCSI Read command on USB mass storage device.
229 *
230 * @param mfun Mass storage function
231 * @param ba Address of first block
232 * @param nblocks Number of blocks to read
233 *
234 * @return Error code
235 */
236int usbmast_read(usbmast_fun_t *mfun, uint64_t ba, size_t nblocks, void *buf)
237{
238 scsi_cdb_read_12_t cdb;
239 size_t data_len;
240 cmd_status_t status;
241 int rc;
242
243 /* XXX Need softstate to store block size. */
244
245 if (ba > UINT32_MAX)
246 return ELIMIT;
247
248 if ((uint64_t)nblocks * mfun->block_size > UINT32_MAX)
249 return ELIMIT;
250
251 memset(&cdb, 0, sizeof(cdb));
252 cdb.op_code = SCSI_CMD_READ_12;
253 cdb.lba = host2uint32_t_be(ba);
254 cdb.xfer_len = host2uint32_t_be(nblocks);
255
256 rc = usb_massstor_data_in(mfun, 0xDEADBEEF, (uint8_t *) &cdb,
257 sizeof(cdb), buf, nblocks * mfun->block_size, &data_len, &status);
258
259 if (rc != EOK) {
260 usb_log_error("Read (12) transport failed, device %s: %s.\n",
261 mfun->mdev->ddf_dev->name, str_error(rc));
262 return rc;
263 }
264
265 if (status != CMDS_GOOD) {
266 usb_log_error("Read (12) command failed, device %s.\n",
267 mfun->mdev->ddf_dev->name);
268 usbmast_dump_sense(mfun);
269 return EIO;
270 }
271
272 if (data_len < nblocks * mfun->block_size) {
273 usb_log_error("SCSI Read response too short (%zu).\n",
274 data_len);
275 return EIO;
276 }
277
278 return EOK;
279}
280
281/** Perform SCSI Write command on USB mass storage device.
282 *
283 * @param mfun Mass storage function
284 * @param ba Address of first block
285 * @param nblocks Number of blocks to read
286 * @param data Data to write
287 *
288 * @return Error code
289 */
290int usbmast_write(usbmast_fun_t *mfun, uint64_t ba, size_t nblocks,
291 const void *data)
292{
293 scsi_cdb_write_12_t cdb;
294 size_t sent_len;
295 cmd_status_t status;
296 int rc;
297
298 if (ba > UINT32_MAX)
299 return ELIMIT;
300
301 if ((uint64_t)nblocks * mfun->block_size > UINT32_MAX)
302 return ELIMIT;
303
304 memset(&cdb, 0, sizeof(cdb));
305 cdb.op_code = SCSI_CMD_WRITE_12;
306 cdb.lba = host2uint32_t_be(ba);
307 cdb.xfer_len = host2uint32_t_be(nblocks);
308
309 rc = usb_massstor_data_out(mfun, 0xDEADBEEF, (uint8_t *) &cdb,
310 sizeof(cdb), data, nblocks * mfun->block_size, &sent_len, &status);
311
312 if (rc != EOK) {
313 usb_log_error("Write (12) transport failed, device %s: %s.\n",
314 mfun->mdev->ddf_dev->name, str_error(rc));
315 return rc;
316 }
317
318 if (status != CMDS_GOOD) {
319 usb_log_error("Write (12) command failed, device %s.\n",
320 mfun->mdev->ddf_dev->name);
321 usbmast_dump_sense(mfun);
322 return EIO;
323 }
324
325 if (sent_len < nblocks * mfun->block_size) {
326 usb_log_error("SCSI Write not all bytes transferred (%zu).\n",
327 sent_len);
328 return EIO;
329 }
330
331 return EOK;
332}
333
334/**
335 * @}
336 */
Note: See TracBrowser for help on using the repository browser.