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