1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * USB Attached SCSI 4 * Note that this is not the same as the USB Mass Storage driver 5 * 6 * Copyright Hans de Goede <hdegoede@redhat.com> for Red Hat, Inc. 2013 - 2016 7 * Copyright Matthew Wilcox for Intel Corp, 2010 8 * Copyright Sarah Sharp for Intel Corp, 2010 9 * 10 * Distributed under the terms of the GNU GPL, version two. 11 */ 12 13 #include <linux/blkdev.h> 14 #include <linux/slab.h> 15 #include <linux/types.h> 16 #include <linux/module.h> 17 #include <linux/usb.h> 18 #include <linux/usb_usual.h> 19 #include <linux/usb/hcd.h> 20 #include <linux/usb/storage.h> 21 #include <linux/usb/uas.h> 22 23 #include <scsi/scsi.h> 24 #include <scsi/scsi_eh.h> 25 #include <scsi/scsi_dbg.h> 26 #include <scsi/scsi_cmnd.h> 27 #include <scsi/scsi_device.h> 28 #include <scsi/scsi_host.h> 29 #include <scsi/scsi_tcq.h> 30 31 #include "uas-detect.h" 32 #include "scsiglue.h" 33 34 #define MAX_CMNDS 256 35 36 struct uas_dev_info { 37 struct usb_interface *intf; 38 struct usb_device *udev; 39 struct usb_anchor cmd_urbs; 40 struct usb_anchor sense_urbs; 41 struct usb_anchor data_urbs; 42 unsigned long flags; 43 int qdepth, resetting; 44 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe; 45 unsigned use_streams:1; 46 unsigned shutdown:1; 47 struct scsi_cmnd *cmnd[MAX_CMNDS]; 48 spinlock_t lock; 49 struct work_struct work; 50 }; 51 52 enum { 53 SUBMIT_STATUS_URB = BIT(1), 54 ALLOC_DATA_IN_URB = BIT(2), 55 SUBMIT_DATA_IN_URB = BIT(3), 56 ALLOC_DATA_OUT_URB = BIT(4), 57 SUBMIT_DATA_OUT_URB = BIT(5), 58 ALLOC_CMD_URB = BIT(6), 59 SUBMIT_CMD_URB = BIT(7), 60 COMMAND_INFLIGHT = BIT(8), 61 DATA_IN_URB_INFLIGHT = BIT(9), 62 DATA_OUT_URB_INFLIGHT = BIT(10), 63 COMMAND_ABORTED = BIT(11), 64 IS_IN_WORK_LIST = BIT(12), 65 }; 66 67 /* Overrides scsi_pointer */ 68 struct uas_cmd_info { 69 unsigned int state; 70 unsigned int uas_tag; 71 struct urb *cmd_urb; 72 struct urb *data_in_urb; 73 struct urb *data_out_urb; 74 }; 75 76 /* I hate forward declarations, but I actually have a loop */ 77 static int uas_submit_urbs(struct scsi_cmnd *cmnd, 78 struct uas_dev_info *devinfo); 79 static void uas_do_work(struct work_struct *work); 80 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller); 81 static void uas_free_streams(struct uas_dev_info *devinfo); 82 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix, 83 int status); 84 85 static void uas_do_work(struct work_struct *work) 86 { 87 struct uas_dev_info *devinfo = 88 container_of(work, struct uas_dev_info, work); 89 struct uas_cmd_info *cmdinfo; 90 struct scsi_cmnd *cmnd; 91 unsigned long flags; 92 int i, err; 93 94 spin_lock_irqsave(&devinfo->lock, flags); 95 96 if (devinfo->resetting) 97 goto out; 98 99 for (i = 0; i < devinfo->qdepth; i++) { 100 if (!devinfo->cmnd[i]) 101 continue; 102 103 cmnd = devinfo->cmnd[i]; 104 cmdinfo = (void *)&cmnd->SCp; 105 106 if (!(cmdinfo->state & IS_IN_WORK_LIST)) 107 continue; 108 109 err = uas_submit_urbs(cmnd, cmnd->device->hostdata); 110 if (!err) 111 cmdinfo->state &= ~IS_IN_WORK_LIST; 112 else 113 schedule_work(&devinfo->work); 114 } 115 out: 116 spin_unlock_irqrestore(&devinfo->lock, flags); 117 } 118 119 static void uas_add_work(struct uas_cmd_info *cmdinfo) 120 { 121 struct scsi_pointer *scp = (void *)cmdinfo; 122 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp); 123 struct uas_dev_info *devinfo = cmnd->device->hostdata; 124 125 lockdep_assert_held(&devinfo->lock); 126 cmdinfo->state |= IS_IN_WORK_LIST; 127 schedule_work(&devinfo->work); 128 } 129 130 static void uas_zap_pending(struct uas_dev_info *devinfo, int result) 131 { 132 struct uas_cmd_info *cmdinfo; 133 struct scsi_cmnd *cmnd; 134 unsigned long flags; 135 int i, err; 136 137 spin_lock_irqsave(&devinfo->lock, flags); 138 for (i = 0; i < devinfo->qdepth; i++) { 139 if (!devinfo->cmnd[i]) 140 continue; 141 142 cmnd = devinfo->cmnd[i]; 143 cmdinfo = (void *)&cmnd->SCp; 144 uas_log_cmd_state(cmnd, __func__, 0); 145 /* Sense urbs were killed, clear COMMAND_INFLIGHT manually */ 146 cmdinfo->state &= ~COMMAND_INFLIGHT; 147 cmnd->result = result << 16; 148 err = uas_try_complete(cmnd, __func__); 149 WARN_ON(err != 0); 150 } 151 spin_unlock_irqrestore(&devinfo->lock, flags); 152 } 153 154 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd) 155 { 156 struct sense_iu *sense_iu = urb->transfer_buffer; 157 struct scsi_device *sdev = cmnd->device; 158 159 if (urb->actual_length > 16) { 160 unsigned len = be16_to_cpup(&sense_iu->len); 161 if (len + 16 != urb->actual_length) { 162 int newlen = min(len + 16, urb->actual_length) - 16; 163 if (newlen < 0) 164 newlen = 0; 165 sdev_printk(KERN_INFO, sdev, "%s: urb length %d " 166 "disagrees with IU sense data length %d, " 167 "using %d bytes of sense data\n", __func__, 168 urb->actual_length, len, newlen); 169 len = newlen; 170 } 171 memcpy(cmnd->sense_buffer, sense_iu->sense, len); 172 } 173 174 cmnd->result = sense_iu->status; 175 } 176 177 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix, 178 int status) 179 { 180 struct uas_cmd_info *ci = (void *)&cmnd->SCp; 181 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 182 183 scmd_printk(KERN_INFO, cmnd, 184 "%s %d uas-tag %d inflight:%s%s%s%s%s%s%s%s%s%s%s%s ", 185 prefix, status, cmdinfo->uas_tag, 186 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "", 187 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "", 188 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "", 189 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "", 190 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "", 191 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "", 192 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "", 193 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "", 194 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "", 195 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "", 196 (ci->state & COMMAND_ABORTED) ? " abort" : "", 197 (ci->state & IS_IN_WORK_LIST) ? " work" : ""); 198 scsi_print_command(cmnd); 199 } 200 201 static void uas_free_unsubmitted_urbs(struct scsi_cmnd *cmnd) 202 { 203 struct uas_cmd_info *cmdinfo; 204 205 if (!cmnd) 206 return; 207 208 cmdinfo = (void *)&cmnd->SCp; 209 210 if (cmdinfo->state & SUBMIT_CMD_URB) 211 usb_free_urb(cmdinfo->cmd_urb); 212 213 /* data urbs may have never gotten their submit flag set */ 214 if (!(cmdinfo->state & DATA_IN_URB_INFLIGHT)) 215 usb_free_urb(cmdinfo->data_in_urb); 216 if (!(cmdinfo->state & DATA_OUT_URB_INFLIGHT)) 217 usb_free_urb(cmdinfo->data_out_urb); 218 } 219 220 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller) 221 { 222 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 223 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; 224 225 lockdep_assert_held(&devinfo->lock); 226 if (cmdinfo->state & (COMMAND_INFLIGHT | 227 DATA_IN_URB_INFLIGHT | 228 DATA_OUT_URB_INFLIGHT | 229 COMMAND_ABORTED)) 230 return -EBUSY; 231 devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL; 232 uas_free_unsubmitted_urbs(cmnd); 233 cmnd->scsi_done(cmnd); 234 return 0; 235 } 236 237 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd, 238 unsigned direction) 239 { 240 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 241 int err; 242 243 cmdinfo->state |= direction | SUBMIT_STATUS_URB; 244 err = uas_submit_urbs(cmnd, cmnd->device->hostdata); 245 if (err) { 246 uas_add_work(cmdinfo); 247 } 248 } 249 250 static bool uas_evaluate_response_iu(struct response_iu *riu, struct scsi_cmnd *cmnd) 251 { 252 u8 response_code = riu->response_code; 253 254 switch (response_code) { 255 case RC_INCORRECT_LUN: 256 cmnd->result = DID_BAD_TARGET << 16; 257 break; 258 case RC_TMF_SUCCEEDED: 259 cmnd->result = DID_OK << 16; 260 break; 261 case RC_TMF_NOT_SUPPORTED: 262 cmnd->result = DID_TARGET_FAILURE << 16; 263 break; 264 default: 265 uas_log_cmd_state(cmnd, "response iu", response_code); 266 cmnd->result = DID_ERROR << 16; 267 break; 268 } 269 270 return response_code == RC_TMF_SUCCEEDED; 271 } 272 273 static void uas_stat_cmplt(struct urb *urb) 274 { 275 struct iu *iu = urb->transfer_buffer; 276 struct Scsi_Host *shost = urb->context; 277 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; 278 struct urb *data_in_urb = NULL; 279 struct urb *data_out_urb = NULL; 280 struct scsi_cmnd *cmnd; 281 struct uas_cmd_info *cmdinfo; 282 unsigned long flags; 283 unsigned int idx; 284 int status = urb->status; 285 bool success; 286 287 spin_lock_irqsave(&devinfo->lock, flags); 288 289 if (devinfo->resetting) 290 goto out; 291 292 if (status) { 293 if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN) 294 dev_err(&urb->dev->dev, "stat urb: status %d\n", status); 295 goto out; 296 } 297 298 idx = be16_to_cpup(&iu->tag) - 1; 299 if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) { 300 dev_err(&urb->dev->dev, 301 "stat urb: no pending cmd for uas-tag %d\n", idx + 1); 302 goto out; 303 } 304 305 cmnd = devinfo->cmnd[idx]; 306 cmdinfo = (void *)&cmnd->SCp; 307 308 if (!(cmdinfo->state & COMMAND_INFLIGHT)) { 309 uas_log_cmd_state(cmnd, "unexpected status cmplt", 0); 310 goto out; 311 } 312 313 switch (iu->iu_id) { 314 case IU_ID_STATUS: 315 uas_sense(urb, cmnd); 316 if (cmnd->result != 0) { 317 /* cancel data transfers on error */ 318 data_in_urb = usb_get_urb(cmdinfo->data_in_urb); 319 data_out_urb = usb_get_urb(cmdinfo->data_out_urb); 320 } 321 cmdinfo->state &= ~COMMAND_INFLIGHT; 322 uas_try_complete(cmnd, __func__); 323 break; 324 case IU_ID_READ_READY: 325 if (!cmdinfo->data_in_urb || 326 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) { 327 uas_log_cmd_state(cmnd, "unexpected read rdy", 0); 328 break; 329 } 330 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB); 331 break; 332 case IU_ID_WRITE_READY: 333 if (!cmdinfo->data_out_urb || 334 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) { 335 uas_log_cmd_state(cmnd, "unexpected write rdy", 0); 336 break; 337 } 338 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB); 339 break; 340 case IU_ID_RESPONSE: 341 cmdinfo->state &= ~COMMAND_INFLIGHT; 342 success = uas_evaluate_response_iu((struct response_iu *)iu, cmnd); 343 if (!success) { 344 /* Error, cancel data transfers */ 345 data_in_urb = usb_get_urb(cmdinfo->data_in_urb); 346 data_out_urb = usb_get_urb(cmdinfo->data_out_urb); 347 } 348 uas_try_complete(cmnd, __func__); 349 break; 350 default: 351 uas_log_cmd_state(cmnd, "bogus IU", iu->iu_id); 352 } 353 out: 354 usb_free_urb(urb); 355 spin_unlock_irqrestore(&devinfo->lock, flags); 356 357 /* Unlinking of data urbs must be done without holding the lock */ 358 if (data_in_urb) { 359 usb_unlink_urb(data_in_urb); 360 usb_put_urb(data_in_urb); 361 } 362 if (data_out_urb) { 363 usb_unlink_urb(data_out_urb); 364 usb_put_urb(data_out_urb); 365 } 366 } 367 368 static void uas_data_cmplt(struct urb *urb) 369 { 370 struct scsi_cmnd *cmnd = urb->context; 371 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 372 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; 373 struct scsi_data_buffer *sdb = NULL; 374 unsigned long flags; 375 int status = urb->status; 376 377 spin_lock_irqsave(&devinfo->lock, flags); 378 379 if (cmdinfo->data_in_urb == urb) { 380 sdb = scsi_in(cmnd); 381 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT; 382 cmdinfo->data_in_urb = NULL; 383 } else if (cmdinfo->data_out_urb == urb) { 384 sdb = scsi_out(cmnd); 385 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT; 386 cmdinfo->data_out_urb = NULL; 387 } 388 if (sdb == NULL) { 389 WARN_ON_ONCE(1); 390 goto out; 391 } 392 393 if (devinfo->resetting) 394 goto out; 395 396 /* Data urbs should not complete before the cmd urb is submitted */ 397 if (cmdinfo->state & SUBMIT_CMD_URB) { 398 uas_log_cmd_state(cmnd, "unexpected data cmplt", 0); 399 goto out; 400 } 401 402 if (status) { 403 if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN) 404 uas_log_cmd_state(cmnd, "data cmplt err", status); 405 /* error: no data transfered */ 406 sdb->resid = sdb->length; 407 } else { 408 sdb->resid = sdb->length - urb->actual_length; 409 } 410 uas_try_complete(cmnd, __func__); 411 out: 412 usb_free_urb(urb); 413 spin_unlock_irqrestore(&devinfo->lock, flags); 414 } 415 416 static void uas_cmd_cmplt(struct urb *urb) 417 { 418 if (urb->status) 419 dev_err(&urb->dev->dev, "cmd cmplt err %d\n", urb->status); 420 421 usb_free_urb(urb); 422 } 423 424 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp, 425 struct scsi_cmnd *cmnd, 426 enum dma_data_direction dir) 427 { 428 struct usb_device *udev = devinfo->udev; 429 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 430 struct urb *urb = usb_alloc_urb(0, gfp); 431 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE) 432 ? scsi_in(cmnd) : scsi_out(cmnd); 433 unsigned int pipe = (dir == DMA_FROM_DEVICE) 434 ? devinfo->data_in_pipe : devinfo->data_out_pipe; 435 436 if (!urb) 437 goto out; 438 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, 439 uas_data_cmplt, cmnd); 440 if (devinfo->use_streams) 441 urb->stream_id = cmdinfo->uas_tag; 442 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0; 443 urb->sg = sdb->table.sgl; 444 out: 445 return urb; 446 } 447 448 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp, 449 struct scsi_cmnd *cmnd) 450 { 451 struct usb_device *udev = devinfo->udev; 452 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 453 struct urb *urb = usb_alloc_urb(0, gfp); 454 struct sense_iu *iu; 455 456 if (!urb) 457 goto out; 458 459 iu = kzalloc(sizeof(*iu), gfp); 460 if (!iu) 461 goto free; 462 463 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu), 464 uas_stat_cmplt, cmnd->device->host); 465 if (devinfo->use_streams) 466 urb->stream_id = cmdinfo->uas_tag; 467 urb->transfer_flags |= URB_FREE_BUFFER; 468 out: 469 return urb; 470 free: 471 usb_free_urb(urb); 472 return NULL; 473 } 474 475 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp, 476 struct scsi_cmnd *cmnd) 477 { 478 struct usb_device *udev = devinfo->udev; 479 struct scsi_device *sdev = cmnd->device; 480 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 481 struct urb *urb = usb_alloc_urb(0, gfp); 482 struct command_iu *iu; 483 int len; 484 485 if (!urb) 486 goto out; 487 488 len = cmnd->cmd_len - 16; 489 if (len < 0) 490 len = 0; 491 len = ALIGN(len, 4); 492 iu = kzalloc(sizeof(*iu) + len, gfp); 493 if (!iu) 494 goto free; 495 496 iu->iu_id = IU_ID_COMMAND; 497 iu->tag = cpu_to_be16(cmdinfo->uas_tag); 498 iu->prio_attr = UAS_SIMPLE_TAG; 499 iu->len = len; 500 int_to_scsilun(sdev->lun, &iu->lun); 501 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len); 502 503 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len, 504 uas_cmd_cmplt, NULL); 505 urb->transfer_flags |= URB_FREE_BUFFER; 506 out: 507 return urb; 508 free: 509 usb_free_urb(urb); 510 return NULL; 511 } 512 513 /* 514 * Why should I request the Status IU before sending the Command IU? Spec 515 * says to, but also says the device may receive them in any order. Seems 516 * daft to me. 517 */ 518 519 static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd, gfp_t gfp) 520 { 521 struct uas_dev_info *devinfo = cmnd->device->hostdata; 522 struct urb *urb; 523 int err; 524 525 urb = uas_alloc_sense_urb(devinfo, gfp, cmnd); 526 if (!urb) 527 return NULL; 528 usb_anchor_urb(urb, &devinfo->sense_urbs); 529 err = usb_submit_urb(urb, gfp); 530 if (err) { 531 usb_unanchor_urb(urb); 532 uas_log_cmd_state(cmnd, "sense submit err", err); 533 usb_free_urb(urb); 534 return NULL; 535 } 536 return urb; 537 } 538 539 static int uas_submit_urbs(struct scsi_cmnd *cmnd, 540 struct uas_dev_info *devinfo) 541 { 542 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 543 struct urb *urb; 544 int err; 545 546 lockdep_assert_held(&devinfo->lock); 547 if (cmdinfo->state & SUBMIT_STATUS_URB) { 548 urb = uas_submit_sense_urb(cmnd, GFP_ATOMIC); 549 if (!urb) 550 return SCSI_MLQUEUE_DEVICE_BUSY; 551 cmdinfo->state &= ~SUBMIT_STATUS_URB; 552 } 553 554 if (cmdinfo->state & ALLOC_DATA_IN_URB) { 555 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, GFP_ATOMIC, 556 cmnd, DMA_FROM_DEVICE); 557 if (!cmdinfo->data_in_urb) 558 return SCSI_MLQUEUE_DEVICE_BUSY; 559 cmdinfo->state &= ~ALLOC_DATA_IN_URB; 560 } 561 562 if (cmdinfo->state & SUBMIT_DATA_IN_URB) { 563 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs); 564 err = usb_submit_urb(cmdinfo->data_in_urb, GFP_ATOMIC); 565 if (err) { 566 usb_unanchor_urb(cmdinfo->data_in_urb); 567 uas_log_cmd_state(cmnd, "data in submit err", err); 568 return SCSI_MLQUEUE_DEVICE_BUSY; 569 } 570 cmdinfo->state &= ~SUBMIT_DATA_IN_URB; 571 cmdinfo->state |= DATA_IN_URB_INFLIGHT; 572 } 573 574 if (cmdinfo->state & ALLOC_DATA_OUT_URB) { 575 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, GFP_ATOMIC, 576 cmnd, DMA_TO_DEVICE); 577 if (!cmdinfo->data_out_urb) 578 return SCSI_MLQUEUE_DEVICE_BUSY; 579 cmdinfo->state &= ~ALLOC_DATA_OUT_URB; 580 } 581 582 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) { 583 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs); 584 err = usb_submit_urb(cmdinfo->data_out_urb, GFP_ATOMIC); 585 if (err) { 586 usb_unanchor_urb(cmdinfo->data_out_urb); 587 uas_log_cmd_state(cmnd, "data out submit err", err); 588 return SCSI_MLQUEUE_DEVICE_BUSY; 589 } 590 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB; 591 cmdinfo->state |= DATA_OUT_URB_INFLIGHT; 592 } 593 594 if (cmdinfo->state & ALLOC_CMD_URB) { 595 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, GFP_ATOMIC, cmnd); 596 if (!cmdinfo->cmd_urb) 597 return SCSI_MLQUEUE_DEVICE_BUSY; 598 cmdinfo->state &= ~ALLOC_CMD_URB; 599 } 600 601 if (cmdinfo->state & SUBMIT_CMD_URB) { 602 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs); 603 err = usb_submit_urb(cmdinfo->cmd_urb, GFP_ATOMIC); 604 if (err) { 605 usb_unanchor_urb(cmdinfo->cmd_urb); 606 uas_log_cmd_state(cmnd, "cmd submit err", err); 607 return SCSI_MLQUEUE_DEVICE_BUSY; 608 } 609 cmdinfo->cmd_urb = NULL; 610 cmdinfo->state &= ~SUBMIT_CMD_URB; 611 cmdinfo->state |= COMMAND_INFLIGHT; 612 } 613 614 return 0; 615 } 616 617 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd, 618 void (*done)(struct scsi_cmnd *)) 619 { 620 struct scsi_device *sdev = cmnd->device; 621 struct uas_dev_info *devinfo = sdev->hostdata; 622 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 623 unsigned long flags; 624 int idx, err; 625 626 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer)); 627 628 /* Re-check scsi_block_requests now that we've the host-lock */ 629 if (cmnd->device->host->host_self_blocked) 630 return SCSI_MLQUEUE_DEVICE_BUSY; 631 632 if ((devinfo->flags & US_FL_NO_ATA_1X) && 633 (cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) { 634 memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB, 635 sizeof(usb_stor_sense_invalidCDB)); 636 cmnd->result = SAM_STAT_CHECK_CONDITION; 637 cmnd->scsi_done(cmnd); 638 return 0; 639 } 640 641 spin_lock_irqsave(&devinfo->lock, flags); 642 643 if (devinfo->resetting) { 644 cmnd->result = DID_ERROR << 16; 645 cmnd->scsi_done(cmnd); 646 spin_unlock_irqrestore(&devinfo->lock, flags); 647 return 0; 648 } 649 650 /* Find a free uas-tag */ 651 for (idx = 0; idx < devinfo->qdepth; idx++) { 652 if (!devinfo->cmnd[idx]) 653 break; 654 } 655 if (idx == devinfo->qdepth) { 656 spin_unlock_irqrestore(&devinfo->lock, flags); 657 return SCSI_MLQUEUE_DEVICE_BUSY; 658 } 659 660 cmnd->scsi_done = done; 661 662 memset(cmdinfo, 0, sizeof(*cmdinfo)); 663 cmdinfo->uas_tag = idx + 1; /* uas-tag == usb-stream-id, so 1 based */ 664 cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB; 665 666 switch (cmnd->sc_data_direction) { 667 case DMA_FROM_DEVICE: 668 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB; 669 break; 670 case DMA_BIDIRECTIONAL: 671 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB; 672 /* fall through */ 673 case DMA_TO_DEVICE: 674 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB; 675 case DMA_NONE: 676 break; 677 } 678 679 if (!devinfo->use_streams) 680 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB); 681 682 err = uas_submit_urbs(cmnd, devinfo); 683 if (err) { 684 /* If we did nothing, give up now */ 685 if (cmdinfo->state & SUBMIT_STATUS_URB) { 686 spin_unlock_irqrestore(&devinfo->lock, flags); 687 return SCSI_MLQUEUE_DEVICE_BUSY; 688 } 689 uas_add_work(cmdinfo); 690 } 691 692 devinfo->cmnd[idx] = cmnd; 693 spin_unlock_irqrestore(&devinfo->lock, flags); 694 return 0; 695 } 696 697 static DEF_SCSI_QCMD(uas_queuecommand) 698 699 /* 700 * For now we do not support actually sending an abort to the device, so 701 * this eh always fails. Still we must define it to make sure that we've 702 * dropped all references to the cmnd in question once this function exits. 703 */ 704 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd) 705 { 706 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 707 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; 708 struct urb *data_in_urb = NULL; 709 struct urb *data_out_urb = NULL; 710 unsigned long flags; 711 712 spin_lock_irqsave(&devinfo->lock, flags); 713 714 uas_log_cmd_state(cmnd, __func__, 0); 715 716 /* Ensure that try_complete does not call scsi_done */ 717 cmdinfo->state |= COMMAND_ABORTED; 718 719 /* Drop all refs to this cmnd, kill data urbs to break their ref */ 720 devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL; 721 if (cmdinfo->state & DATA_IN_URB_INFLIGHT) 722 data_in_urb = usb_get_urb(cmdinfo->data_in_urb); 723 if (cmdinfo->state & DATA_OUT_URB_INFLIGHT) 724 data_out_urb = usb_get_urb(cmdinfo->data_out_urb); 725 726 uas_free_unsubmitted_urbs(cmnd); 727 728 spin_unlock_irqrestore(&devinfo->lock, flags); 729 730 if (data_in_urb) { 731 usb_kill_urb(data_in_urb); 732 usb_put_urb(data_in_urb); 733 } 734 if (data_out_urb) { 735 usb_kill_urb(data_out_urb); 736 usb_put_urb(data_out_urb); 737 } 738 739 return FAILED; 740 } 741 742 static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd) 743 { 744 struct scsi_device *sdev = cmnd->device; 745 struct uas_dev_info *devinfo = sdev->hostdata; 746 struct usb_device *udev = devinfo->udev; 747 unsigned long flags; 748 int err; 749 750 err = usb_lock_device_for_reset(udev, devinfo->intf); 751 if (err) { 752 shost_printk(KERN_ERR, sdev->host, 753 "%s FAILED to get lock err %d\n", __func__, err); 754 return FAILED; 755 } 756 757 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__); 758 759 spin_lock_irqsave(&devinfo->lock, flags); 760 devinfo->resetting = 1; 761 spin_unlock_irqrestore(&devinfo->lock, flags); 762 763 usb_kill_anchored_urbs(&devinfo->cmd_urbs); 764 usb_kill_anchored_urbs(&devinfo->sense_urbs); 765 usb_kill_anchored_urbs(&devinfo->data_urbs); 766 uas_zap_pending(devinfo, DID_RESET); 767 768 err = usb_reset_device(udev); 769 770 spin_lock_irqsave(&devinfo->lock, flags); 771 devinfo->resetting = 0; 772 spin_unlock_irqrestore(&devinfo->lock, flags); 773 774 usb_unlock_device(udev); 775 776 if (err) { 777 shost_printk(KERN_INFO, sdev->host, "%s FAILED err %d\n", 778 __func__, err); 779 return FAILED; 780 } 781 782 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__); 783 return SUCCESS; 784 } 785 786 static int uas_target_alloc(struct scsi_target *starget) 787 { 788 struct uas_dev_info *devinfo = (struct uas_dev_info *) 789 dev_to_shost(starget->dev.parent)->hostdata; 790 791 if (devinfo->flags & US_FL_NO_REPORT_LUNS) 792 starget->no_report_luns = 1; 793 794 return 0; 795 } 796 797 static int uas_slave_alloc(struct scsi_device *sdev) 798 { 799 struct uas_dev_info *devinfo = 800 (struct uas_dev_info *)sdev->host->hostdata; 801 802 sdev->hostdata = devinfo; 803 804 /* 805 * USB has unusual DMA-alignment requirements: Although the 806 * starting address of each scatter-gather element doesn't matter, 807 * the length of each element except the last must be divisible 808 * by the Bulk maxpacket value. There's currently no way to 809 * express this by block-layer constraints, so we'll cop out 810 * and simply require addresses to be aligned at 512-byte 811 * boundaries. This is okay since most block I/O involves 812 * hardware sectors that are multiples of 512 bytes in length, 813 * and since host controllers up through USB 2.0 have maxpacket 814 * values no larger than 512. 815 * 816 * But it doesn't suffice for Wireless USB, where Bulk maxpacket 817 * values can be as large as 2048. To make that work properly 818 * will require changes to the block layer. 819 */ 820 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1)); 821 822 if (devinfo->flags & US_FL_MAX_SECTORS_64) 823 blk_queue_max_hw_sectors(sdev->request_queue, 64); 824 else if (devinfo->flags & US_FL_MAX_SECTORS_240) 825 blk_queue_max_hw_sectors(sdev->request_queue, 240); 826 827 return 0; 828 } 829 830 static int uas_slave_configure(struct scsi_device *sdev) 831 { 832 struct uas_dev_info *devinfo = sdev->hostdata; 833 834 if (devinfo->flags & US_FL_NO_REPORT_OPCODES) 835 sdev->no_report_opcodes = 1; 836 837 /* A few buggy USB-ATA bridges don't understand FUA */ 838 if (devinfo->flags & US_FL_BROKEN_FUA) 839 sdev->broken_fua = 1; 840 841 scsi_change_queue_depth(sdev, devinfo->qdepth - 2); 842 return 0; 843 } 844 845 static struct scsi_host_template uas_host_template = { 846 .module = THIS_MODULE, 847 .name = "uas", 848 .queuecommand = uas_queuecommand, 849 .target_alloc = uas_target_alloc, 850 .slave_alloc = uas_slave_alloc, 851 .slave_configure = uas_slave_configure, 852 .eh_abort_handler = uas_eh_abort_handler, 853 .eh_device_reset_handler = uas_eh_device_reset_handler, 854 .this_id = -1, 855 .sg_tablesize = SG_NONE, 856 .skip_settle_delay = 1, 857 }; 858 859 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ 860 vendorName, productName, useProtocol, useTransport, \ 861 initFunction, flags) \ 862 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ 863 .driver_info = (flags) } 864 865 static struct usb_device_id uas_usb_ids[] = { 866 # include "unusual_uas.h" 867 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) }, 868 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) }, 869 { } 870 }; 871 MODULE_DEVICE_TABLE(usb, uas_usb_ids); 872 873 #undef UNUSUAL_DEV 874 875 static int uas_switch_interface(struct usb_device *udev, 876 struct usb_interface *intf) 877 { 878 struct usb_host_interface *alt; 879 880 alt = uas_find_uas_alt_setting(intf); 881 if (!alt) 882 return -ENODEV; 883 884 return usb_set_interface(udev, alt->desc.bInterfaceNumber, 885 alt->desc.bAlternateSetting); 886 } 887 888 static int uas_configure_endpoints(struct uas_dev_info *devinfo) 889 { 890 struct usb_host_endpoint *eps[4] = { }; 891 struct usb_device *udev = devinfo->udev; 892 int r; 893 894 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps); 895 if (r) 896 return r; 897 898 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 899 usb_endpoint_num(&eps[0]->desc)); 900 devinfo->status_pipe = usb_rcvbulkpipe(udev, 901 usb_endpoint_num(&eps[1]->desc)); 902 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 903 usb_endpoint_num(&eps[2]->desc)); 904 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 905 usb_endpoint_num(&eps[3]->desc)); 906 907 if (udev->speed < USB_SPEED_SUPER) { 908 devinfo->qdepth = 32; 909 devinfo->use_streams = 0; 910 } else { 911 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 912 3, MAX_CMNDS, GFP_NOIO); 913 if (devinfo->qdepth < 0) 914 return devinfo->qdepth; 915 devinfo->use_streams = 1; 916 } 917 918 return 0; 919 } 920 921 static void uas_free_streams(struct uas_dev_info *devinfo) 922 { 923 struct usb_device *udev = devinfo->udev; 924 struct usb_host_endpoint *eps[3]; 925 926 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe); 927 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe); 928 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe); 929 usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO); 930 } 931 932 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id) 933 { 934 int result = -ENOMEM; 935 struct Scsi_Host *shost = NULL; 936 struct uas_dev_info *devinfo; 937 struct usb_device *udev = interface_to_usbdev(intf); 938 unsigned long dev_flags; 939 940 if (!uas_use_uas_driver(intf, id, &dev_flags)) 941 return -ENODEV; 942 943 if (uas_switch_interface(udev, intf)) 944 return -ENODEV; 945 946 shost = scsi_host_alloc(&uas_host_template, 947 sizeof(struct uas_dev_info)); 948 if (!shost) 949 goto set_alt0; 950 951 shost->max_cmd_len = 16 + 252; 952 shost->max_id = 1; 953 shost->max_lun = 256; 954 shost->max_channel = 0; 955 shost->sg_tablesize = udev->bus->sg_tablesize; 956 957 devinfo = (struct uas_dev_info *)shost->hostdata; 958 devinfo->intf = intf; 959 devinfo->udev = udev; 960 devinfo->resetting = 0; 961 devinfo->shutdown = 0; 962 devinfo->flags = dev_flags; 963 init_usb_anchor(&devinfo->cmd_urbs); 964 init_usb_anchor(&devinfo->sense_urbs); 965 init_usb_anchor(&devinfo->data_urbs); 966 spin_lock_init(&devinfo->lock); 967 INIT_WORK(&devinfo->work, uas_do_work); 968 969 result = uas_configure_endpoints(devinfo); 970 if (result) 971 goto set_alt0; 972 973 /* 974 * 1 tag is reserved for untagged commands + 975 * 1 tag to avoid off by one errors in some bridge firmwares 976 */ 977 shost->can_queue = devinfo->qdepth - 2; 978 979 usb_set_intfdata(intf, shost); 980 result = scsi_add_host(shost, &intf->dev); 981 if (result) 982 goto free_streams; 983 984 scsi_scan_host(shost); 985 return result; 986 987 free_streams: 988 uas_free_streams(devinfo); 989 usb_set_intfdata(intf, NULL); 990 set_alt0: 991 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0); 992 if (shost) 993 scsi_host_put(shost); 994 return result; 995 } 996 997 static int uas_cmnd_list_empty(struct uas_dev_info *devinfo) 998 { 999 unsigned long flags; 1000 int i, r = 1; 1001 1002 spin_lock_irqsave(&devinfo->lock, flags); 1003 1004 for (i = 0; i < devinfo->qdepth; i++) { 1005 if (devinfo->cmnd[i]) { 1006 r = 0; /* Not empty */ 1007 break; 1008 } 1009 } 1010 1011 spin_unlock_irqrestore(&devinfo->lock, flags); 1012 1013 return r; 1014 } 1015 1016 /* 1017 * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily 1018 * get empty while there still is more work to do due to sense-urbs completing 1019 * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty. 1020 */ 1021 static int uas_wait_for_pending_cmnds(struct uas_dev_info *devinfo) 1022 { 1023 unsigned long start_time; 1024 int r; 1025 1026 start_time = jiffies; 1027 do { 1028 flush_work(&devinfo->work); 1029 1030 r = usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000); 1031 if (r == 0) 1032 return -ETIME; 1033 1034 r = usb_wait_anchor_empty_timeout(&devinfo->data_urbs, 500); 1035 if (r == 0) 1036 return -ETIME; 1037 1038 if (time_after(jiffies, start_time + 5 * HZ)) 1039 return -ETIME; 1040 } while (!uas_cmnd_list_empty(devinfo)); 1041 1042 return 0; 1043 } 1044 1045 static int uas_pre_reset(struct usb_interface *intf) 1046 { 1047 struct Scsi_Host *shost = usb_get_intfdata(intf); 1048 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; 1049 unsigned long flags; 1050 1051 if (devinfo->shutdown) 1052 return 0; 1053 1054 /* Block new requests */ 1055 spin_lock_irqsave(shost->host_lock, flags); 1056 scsi_block_requests(shost); 1057 spin_unlock_irqrestore(shost->host_lock, flags); 1058 1059 if (uas_wait_for_pending_cmnds(devinfo) != 0) { 1060 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__); 1061 scsi_unblock_requests(shost); 1062 return 1; 1063 } 1064 1065 uas_free_streams(devinfo); 1066 1067 return 0; 1068 } 1069 1070 static int uas_post_reset(struct usb_interface *intf) 1071 { 1072 struct Scsi_Host *shost = usb_get_intfdata(intf); 1073 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; 1074 unsigned long flags; 1075 int err; 1076 1077 if (devinfo->shutdown) 1078 return 0; 1079 1080 err = uas_configure_endpoints(devinfo); 1081 if (err) { 1082 shost_printk(KERN_ERR, shost, 1083 "%s: alloc streams error %d after reset", 1084 __func__, err); 1085 return 1; 1086 } 1087 1088 spin_lock_irqsave(shost->host_lock, flags); 1089 scsi_report_bus_reset(shost, 0); 1090 spin_unlock_irqrestore(shost->host_lock, flags); 1091 1092 scsi_unblock_requests(shost); 1093 1094 return 0; 1095 } 1096 1097 static int uas_suspend(struct usb_interface *intf, pm_message_t message) 1098 { 1099 struct Scsi_Host *shost = usb_get_intfdata(intf); 1100 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; 1101 1102 if (uas_wait_for_pending_cmnds(devinfo) != 0) { 1103 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__); 1104 return -ETIME; 1105 } 1106 1107 return 0; 1108 } 1109 1110 static int uas_resume(struct usb_interface *intf) 1111 { 1112 return 0; 1113 } 1114 1115 static int uas_reset_resume(struct usb_interface *intf) 1116 { 1117 struct Scsi_Host *shost = usb_get_intfdata(intf); 1118 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; 1119 unsigned long flags; 1120 int err; 1121 1122 err = uas_configure_endpoints(devinfo); 1123 if (err) { 1124 shost_printk(KERN_ERR, shost, 1125 "%s: alloc streams error %d after reset", 1126 __func__, err); 1127 return -EIO; 1128 } 1129 1130 spin_lock_irqsave(shost->host_lock, flags); 1131 scsi_report_bus_reset(shost, 0); 1132 spin_unlock_irqrestore(shost->host_lock, flags); 1133 1134 return 0; 1135 } 1136 1137 static void uas_disconnect(struct usb_interface *intf) 1138 { 1139 struct Scsi_Host *shost = usb_get_intfdata(intf); 1140 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; 1141 unsigned long flags; 1142 1143 spin_lock_irqsave(&devinfo->lock, flags); 1144 devinfo->resetting = 1; 1145 spin_unlock_irqrestore(&devinfo->lock, flags); 1146 1147 cancel_work_sync(&devinfo->work); 1148 usb_kill_anchored_urbs(&devinfo->cmd_urbs); 1149 usb_kill_anchored_urbs(&devinfo->sense_urbs); 1150 usb_kill_anchored_urbs(&devinfo->data_urbs); 1151 uas_zap_pending(devinfo, DID_NO_CONNECT); 1152 1153 scsi_remove_host(shost); 1154 uas_free_streams(devinfo); 1155 scsi_host_put(shost); 1156 } 1157 1158 /* 1159 * Put the device back in usb-storage mode on shutdown, as some BIOS-es 1160 * hang on reboot when the device is still in uas mode. Note the reset is 1161 * necessary as some devices won't revert to usb-storage mode without it. 1162 */ 1163 static void uas_shutdown(struct device *dev) 1164 { 1165 struct usb_interface *intf = to_usb_interface(dev); 1166 struct usb_device *udev = interface_to_usbdev(intf); 1167 struct Scsi_Host *shost = usb_get_intfdata(intf); 1168 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; 1169 1170 if (system_state != SYSTEM_RESTART) 1171 return; 1172 1173 devinfo->shutdown = 1; 1174 uas_free_streams(devinfo); 1175 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0); 1176 usb_reset_device(udev); 1177 } 1178 1179 static struct usb_driver uas_driver = { 1180 .name = "uas", 1181 .probe = uas_probe, 1182 .disconnect = uas_disconnect, 1183 .pre_reset = uas_pre_reset, 1184 .post_reset = uas_post_reset, 1185 .suspend = uas_suspend, 1186 .resume = uas_resume, 1187 .reset_resume = uas_reset_resume, 1188 .drvwrap.driver.shutdown = uas_shutdown, 1189 .id_table = uas_usb_ids, 1190 }; 1191 1192 module_usb_driver(uas_driver); 1193 1194 MODULE_LICENSE("GPL"); 1195 MODULE_AUTHOR( 1196 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp"); 1197