1 /* 2 * USB Attached SCSI 3 * Note that this is not the same as the USB Mass Storage driver 4 * 5 * Copyright Matthew Wilcox for Intel Corp, 2010 6 * Copyright Sarah Sharp for Intel Corp, 2010 7 * 8 * Distributed under the terms of the GNU GPL, version two. 9 */ 10 11 #include <linux/blkdev.h> 12 #include <linux/slab.h> 13 #include <linux/types.h> 14 #include <linux/module.h> 15 #include <linux/usb.h> 16 #include <linux/usb/hcd.h> 17 #include <linux/usb/storage.h> 18 #include <linux/usb/uas.h> 19 20 #include <scsi/scsi.h> 21 #include <scsi/scsi_dbg.h> 22 #include <scsi/scsi_cmnd.h> 23 #include <scsi/scsi_device.h> 24 #include <scsi/scsi_host.h> 25 #include <scsi/scsi_tcq.h> 26 27 /* 28 * The r00-r01c specs define this version of the SENSE IU data structure. 29 * It's still in use by several different firmware releases. 30 */ 31 struct sense_iu_old { 32 __u8 iu_id; 33 __u8 rsvd1; 34 __be16 tag; 35 __be16 len; 36 __u8 status; 37 __u8 service_response; 38 __u8 sense[SCSI_SENSE_BUFFERSIZE]; 39 }; 40 41 struct uas_dev_info { 42 struct usb_interface *intf; 43 struct usb_device *udev; 44 struct usb_anchor cmd_urbs; 45 struct usb_anchor sense_urbs; 46 struct usb_anchor data_urbs; 47 int qdepth, resetting; 48 struct response_ui response; 49 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe; 50 unsigned use_streams:1; 51 unsigned uas_sense_old:1; 52 struct scsi_cmnd *cmnd; 53 spinlock_t lock; 54 }; 55 56 enum { 57 SUBMIT_STATUS_URB = (1 << 1), 58 ALLOC_DATA_IN_URB = (1 << 2), 59 SUBMIT_DATA_IN_URB = (1 << 3), 60 ALLOC_DATA_OUT_URB = (1 << 4), 61 SUBMIT_DATA_OUT_URB = (1 << 5), 62 ALLOC_CMD_URB = (1 << 6), 63 SUBMIT_CMD_URB = (1 << 7), 64 COMMAND_INFLIGHT = (1 << 8), 65 DATA_IN_URB_INFLIGHT = (1 << 9), 66 DATA_OUT_URB_INFLIGHT = (1 << 10), 67 COMMAND_COMPLETED = (1 << 11), 68 COMMAND_ABORTED = (1 << 12), 69 }; 70 71 /* Overrides scsi_pointer */ 72 struct uas_cmd_info { 73 unsigned int state; 74 unsigned int stream; 75 struct urb *cmd_urb; 76 struct urb *data_in_urb; 77 struct urb *data_out_urb; 78 struct list_head list; 79 }; 80 81 /* I hate forward declarations, but I actually have a loop */ 82 static int uas_submit_urbs(struct scsi_cmnd *cmnd, 83 struct uas_dev_info *devinfo, gfp_t gfp); 84 static void uas_do_work(struct work_struct *work); 85 86 static DECLARE_WORK(uas_work, uas_do_work); 87 static DEFINE_SPINLOCK(uas_work_lock); 88 static LIST_HEAD(uas_work_list); 89 90 static void uas_do_work(struct work_struct *work) 91 { 92 struct uas_cmd_info *cmdinfo; 93 struct uas_cmd_info *temp; 94 struct list_head list; 95 unsigned long flags; 96 int err; 97 98 spin_lock_irq(&uas_work_lock); 99 list_replace_init(&uas_work_list, &list); 100 spin_unlock_irq(&uas_work_lock); 101 102 list_for_each_entry_safe(cmdinfo, temp, &list, list) { 103 struct scsi_pointer *scp = (void *)cmdinfo; 104 struct scsi_cmnd *cmnd = container_of(scp, 105 struct scsi_cmnd, SCp); 106 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; 107 spin_lock_irqsave(&devinfo->lock, flags); 108 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC); 109 spin_unlock_irqrestore(&devinfo->lock, flags); 110 if (err) { 111 list_del(&cmdinfo->list); 112 spin_lock_irq(&uas_work_lock); 113 list_add_tail(&cmdinfo->list, &uas_work_list); 114 spin_unlock_irq(&uas_work_lock); 115 schedule_work(&uas_work); 116 } 117 } 118 } 119 120 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd) 121 { 122 struct sense_iu *sense_iu = urb->transfer_buffer; 123 struct scsi_device *sdev = cmnd->device; 124 125 if (urb->actual_length > 16) { 126 unsigned len = be16_to_cpup(&sense_iu->len); 127 if (len + 16 != urb->actual_length) { 128 int newlen = min(len + 16, urb->actual_length) - 16; 129 if (newlen < 0) 130 newlen = 0; 131 sdev_printk(KERN_INFO, sdev, "%s: urb length %d " 132 "disagrees with IU sense data length %d, " 133 "using %d bytes of sense data\n", __func__, 134 urb->actual_length, len, newlen); 135 len = newlen; 136 } 137 memcpy(cmnd->sense_buffer, sense_iu->sense, len); 138 } 139 140 cmnd->result = sense_iu->status; 141 } 142 143 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd) 144 { 145 struct sense_iu_old *sense_iu = urb->transfer_buffer; 146 struct scsi_device *sdev = cmnd->device; 147 148 if (urb->actual_length > 8) { 149 unsigned len = be16_to_cpup(&sense_iu->len) - 2; 150 if (len + 8 != urb->actual_length) { 151 int newlen = min(len + 8, urb->actual_length) - 8; 152 if (newlen < 0) 153 newlen = 0; 154 sdev_printk(KERN_INFO, sdev, "%s: urb length %d " 155 "disagrees with IU sense data length %d, " 156 "using %d bytes of sense data\n", __func__, 157 urb->actual_length, len, newlen); 158 len = newlen; 159 } 160 memcpy(cmnd->sense_buffer, sense_iu->sense, len); 161 } 162 163 cmnd->result = sense_iu->status; 164 } 165 166 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller) 167 { 168 struct uas_cmd_info *ci = (void *)&cmnd->SCp; 169 170 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:" 171 "%s%s%s%s%s%s%s%s%s%s%s%s\n", 172 caller, cmnd, cmnd->request->tag, 173 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "", 174 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "", 175 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "", 176 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "", 177 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "", 178 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "", 179 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "", 180 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "", 181 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "", 182 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "", 183 (ci->state & COMMAND_COMPLETED) ? " done" : "", 184 (ci->state & COMMAND_ABORTED) ? " abort" : ""); 185 } 186 187 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller) 188 { 189 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 190 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; 191 192 WARN_ON(!spin_is_locked(&devinfo->lock)); 193 if (cmdinfo->state & (COMMAND_INFLIGHT | 194 DATA_IN_URB_INFLIGHT | 195 DATA_OUT_URB_INFLIGHT)) 196 return -EBUSY; 197 BUG_ON(cmdinfo->state & COMMAND_COMPLETED); 198 cmdinfo->state |= COMMAND_COMPLETED; 199 usb_free_urb(cmdinfo->data_in_urb); 200 usb_free_urb(cmdinfo->data_out_urb); 201 if (cmdinfo->state & COMMAND_ABORTED) { 202 scmd_printk(KERN_INFO, cmnd, "abort completed\n"); 203 cmnd->result = DID_ABORT << 16; 204 } 205 cmnd->scsi_done(cmnd); 206 return 0; 207 } 208 209 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd, 210 unsigned direction) 211 { 212 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 213 int err; 214 215 cmdinfo->state |= direction | SUBMIT_STATUS_URB; 216 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC); 217 if (err) { 218 spin_lock(&uas_work_lock); 219 list_add_tail(&cmdinfo->list, &uas_work_list); 220 spin_unlock(&uas_work_lock); 221 schedule_work(&uas_work); 222 } 223 } 224 225 static void uas_stat_cmplt(struct urb *urb) 226 { 227 struct iu *iu = urb->transfer_buffer; 228 struct Scsi_Host *shost = urb->context; 229 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 230 struct scsi_cmnd *cmnd; 231 struct uas_cmd_info *cmdinfo; 232 unsigned long flags; 233 u16 tag; 234 235 if (urb->status) { 236 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status); 237 usb_free_urb(urb); 238 return; 239 } 240 241 if (devinfo->resetting) { 242 usb_free_urb(urb); 243 return; 244 } 245 246 spin_lock_irqsave(&devinfo->lock, flags); 247 tag = be16_to_cpup(&iu->tag) - 1; 248 if (tag == 0) 249 cmnd = devinfo->cmnd; 250 else 251 cmnd = scsi_host_find_tag(shost, tag - 1); 252 253 if (!cmnd) { 254 if (iu->iu_id == IU_ID_RESPONSE) { 255 /* store results for uas_eh_task_mgmt() */ 256 memcpy(&devinfo->response, iu, sizeof(devinfo->response)); 257 } 258 usb_free_urb(urb); 259 spin_unlock_irqrestore(&devinfo->lock, flags); 260 return; 261 } 262 263 cmdinfo = (void *)&cmnd->SCp; 264 switch (iu->iu_id) { 265 case IU_ID_STATUS: 266 if (devinfo->cmnd == cmnd) 267 devinfo->cmnd = NULL; 268 269 if (urb->actual_length < 16) 270 devinfo->uas_sense_old = 1; 271 if (devinfo->uas_sense_old) 272 uas_sense_old(urb, cmnd); 273 else 274 uas_sense(urb, cmnd); 275 if (cmnd->result != 0) { 276 /* cancel data transfers on error */ 277 if (cmdinfo->state & DATA_IN_URB_INFLIGHT) { 278 spin_unlock_irqrestore(&devinfo->lock, flags); 279 usb_unlink_urb(cmdinfo->data_in_urb); 280 spin_lock_irqsave(&devinfo->lock, flags); 281 } 282 if (cmdinfo->state & DATA_OUT_URB_INFLIGHT) { 283 spin_unlock_irqrestore(&devinfo->lock, flags); 284 usb_unlink_urb(cmdinfo->data_out_urb); 285 spin_lock_irqsave(&devinfo->lock, flags); 286 } 287 } 288 cmdinfo->state &= ~COMMAND_INFLIGHT; 289 uas_try_complete(cmnd, __func__); 290 break; 291 case IU_ID_READ_READY: 292 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB); 293 break; 294 case IU_ID_WRITE_READY: 295 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB); 296 break; 297 default: 298 scmd_printk(KERN_ERR, cmnd, 299 "Bogus IU (%d) received on status pipe\n", iu->iu_id); 300 } 301 usb_free_urb(urb); 302 spin_unlock_irqrestore(&devinfo->lock, flags); 303 } 304 305 static void uas_data_cmplt(struct urb *urb) 306 { 307 struct scsi_cmnd *cmnd = urb->context; 308 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 309 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; 310 struct scsi_data_buffer *sdb = NULL; 311 unsigned long flags; 312 313 spin_lock_irqsave(&devinfo->lock, flags); 314 if (cmdinfo->data_in_urb == urb) { 315 sdb = scsi_in(cmnd); 316 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT; 317 } else if (cmdinfo->data_out_urb == urb) { 318 sdb = scsi_out(cmnd); 319 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT; 320 } 321 BUG_ON(sdb == NULL); 322 if (urb->status) { 323 /* error: no data transfered */ 324 sdb->resid = sdb->length; 325 } else { 326 sdb->resid = sdb->length - urb->actual_length; 327 } 328 uas_try_complete(cmnd, __func__); 329 spin_unlock_irqrestore(&devinfo->lock, flags); 330 } 331 332 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp, 333 unsigned int pipe, u16 stream_id, 334 struct scsi_cmnd *cmnd, 335 enum dma_data_direction dir) 336 { 337 struct usb_device *udev = devinfo->udev; 338 struct urb *urb = usb_alloc_urb(0, gfp); 339 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE) 340 ? scsi_in(cmnd) : scsi_out(cmnd); 341 342 if (!urb) 343 goto out; 344 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, 345 uas_data_cmplt, cmnd); 346 if (devinfo->use_streams) 347 urb->stream_id = stream_id; 348 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0; 349 urb->sg = sdb->table.sgl; 350 out: 351 return urb; 352 } 353 354 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp, 355 struct Scsi_Host *shost, u16 stream_id) 356 { 357 struct usb_device *udev = devinfo->udev; 358 struct urb *urb = usb_alloc_urb(0, gfp); 359 struct sense_iu *iu; 360 361 if (!urb) 362 goto out; 363 364 iu = kzalloc(sizeof(*iu), gfp); 365 if (!iu) 366 goto free; 367 368 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu), 369 uas_stat_cmplt, shost); 370 urb->stream_id = stream_id; 371 urb->transfer_flags |= URB_FREE_BUFFER; 372 out: 373 return urb; 374 free: 375 usb_free_urb(urb); 376 return NULL; 377 } 378 379 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp, 380 struct scsi_cmnd *cmnd, u16 stream_id) 381 { 382 struct usb_device *udev = devinfo->udev; 383 struct scsi_device *sdev = cmnd->device; 384 struct urb *urb = usb_alloc_urb(0, gfp); 385 struct command_iu *iu; 386 int len; 387 388 if (!urb) 389 goto out; 390 391 len = cmnd->cmd_len - 16; 392 if (len < 0) 393 len = 0; 394 len = ALIGN(len, 4); 395 iu = kzalloc(sizeof(*iu) + len, gfp); 396 if (!iu) 397 goto free; 398 399 iu->iu_id = IU_ID_COMMAND; 400 if (blk_rq_tagged(cmnd->request)) 401 iu->tag = cpu_to_be16(cmnd->request->tag + 2); 402 else 403 iu->tag = cpu_to_be16(1); 404 iu->prio_attr = UAS_SIMPLE_TAG; 405 iu->len = len; 406 int_to_scsilun(sdev->lun, &iu->lun); 407 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len); 408 409 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len, 410 usb_free_urb, NULL); 411 urb->transfer_flags |= URB_FREE_BUFFER; 412 out: 413 return urb; 414 free: 415 usb_free_urb(urb); 416 return NULL; 417 } 418 419 static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp, 420 u8 function, u16 stream_id) 421 { 422 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; 423 struct usb_device *udev = devinfo->udev; 424 struct urb *urb = usb_alloc_urb(0, gfp); 425 struct task_mgmt_iu *iu; 426 int err = -ENOMEM; 427 428 if (!urb) 429 goto err; 430 431 iu = kzalloc(sizeof(*iu), gfp); 432 if (!iu) 433 goto err; 434 435 iu->iu_id = IU_ID_TASK_MGMT; 436 iu->tag = cpu_to_be16(stream_id); 437 int_to_scsilun(cmnd->device->lun, &iu->lun); 438 439 iu->function = function; 440 switch (function) { 441 case TMF_ABORT_TASK: 442 if (blk_rq_tagged(cmnd->request)) 443 iu->task_tag = cpu_to_be16(cmnd->request->tag + 2); 444 else 445 iu->task_tag = cpu_to_be16(1); 446 break; 447 } 448 449 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu), 450 usb_free_urb, NULL); 451 urb->transfer_flags |= URB_FREE_BUFFER; 452 453 err = usb_submit_urb(urb, gfp); 454 if (err) 455 goto err; 456 usb_anchor_urb(urb, &devinfo->cmd_urbs); 457 458 return 0; 459 460 err: 461 usb_free_urb(urb); 462 return err; 463 } 464 465 /* 466 * Why should I request the Status IU before sending the Command IU? Spec 467 * says to, but also says the device may receive them in any order. Seems 468 * daft to me. 469 */ 470 471 static int uas_submit_sense_urb(struct Scsi_Host *shost, 472 gfp_t gfp, unsigned int stream) 473 { 474 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 475 struct urb *urb; 476 477 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream); 478 if (!urb) 479 return SCSI_MLQUEUE_DEVICE_BUSY; 480 if (usb_submit_urb(urb, gfp)) { 481 shost_printk(KERN_INFO, shost, 482 "sense urb submission failure\n"); 483 usb_free_urb(urb); 484 return SCSI_MLQUEUE_DEVICE_BUSY; 485 } 486 usb_anchor_urb(urb, &devinfo->sense_urbs); 487 return 0; 488 } 489 490 static int uas_submit_urbs(struct scsi_cmnd *cmnd, 491 struct uas_dev_info *devinfo, gfp_t gfp) 492 { 493 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 494 int err; 495 496 WARN_ON(!spin_is_locked(&devinfo->lock)); 497 if (cmdinfo->state & SUBMIT_STATUS_URB) { 498 err = uas_submit_sense_urb(cmnd->device->host, gfp, 499 cmdinfo->stream); 500 if (err) { 501 return err; 502 } 503 cmdinfo->state &= ~SUBMIT_STATUS_URB; 504 } 505 506 if (cmdinfo->state & ALLOC_DATA_IN_URB) { 507 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp, 508 devinfo->data_in_pipe, cmdinfo->stream, 509 cmnd, DMA_FROM_DEVICE); 510 if (!cmdinfo->data_in_urb) 511 return SCSI_MLQUEUE_DEVICE_BUSY; 512 cmdinfo->state &= ~ALLOC_DATA_IN_URB; 513 } 514 515 if (cmdinfo->state & SUBMIT_DATA_IN_URB) { 516 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) { 517 scmd_printk(KERN_INFO, cmnd, 518 "data in urb submission failure\n"); 519 return SCSI_MLQUEUE_DEVICE_BUSY; 520 } 521 cmdinfo->state &= ~SUBMIT_DATA_IN_URB; 522 cmdinfo->state |= DATA_IN_URB_INFLIGHT; 523 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs); 524 } 525 526 if (cmdinfo->state & ALLOC_DATA_OUT_URB) { 527 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp, 528 devinfo->data_out_pipe, cmdinfo->stream, 529 cmnd, DMA_TO_DEVICE); 530 if (!cmdinfo->data_out_urb) 531 return SCSI_MLQUEUE_DEVICE_BUSY; 532 cmdinfo->state &= ~ALLOC_DATA_OUT_URB; 533 } 534 535 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) { 536 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) { 537 scmd_printk(KERN_INFO, cmnd, 538 "data out urb submission failure\n"); 539 return SCSI_MLQUEUE_DEVICE_BUSY; 540 } 541 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB; 542 cmdinfo->state |= DATA_OUT_URB_INFLIGHT; 543 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs); 544 } 545 546 if (cmdinfo->state & ALLOC_CMD_URB) { 547 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd, 548 cmdinfo->stream); 549 if (!cmdinfo->cmd_urb) 550 return SCSI_MLQUEUE_DEVICE_BUSY; 551 cmdinfo->state &= ~ALLOC_CMD_URB; 552 } 553 554 if (cmdinfo->state & SUBMIT_CMD_URB) { 555 usb_get_urb(cmdinfo->cmd_urb); 556 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) { 557 scmd_printk(KERN_INFO, cmnd, 558 "cmd urb submission failure\n"); 559 return SCSI_MLQUEUE_DEVICE_BUSY; 560 } 561 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs); 562 usb_put_urb(cmdinfo->cmd_urb); 563 cmdinfo->cmd_urb = NULL; 564 cmdinfo->state &= ~SUBMIT_CMD_URB; 565 cmdinfo->state |= COMMAND_INFLIGHT; 566 } 567 568 return 0; 569 } 570 571 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd, 572 void (*done)(struct scsi_cmnd *)) 573 { 574 struct scsi_device *sdev = cmnd->device; 575 struct uas_dev_info *devinfo = sdev->hostdata; 576 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 577 unsigned long flags; 578 int err; 579 580 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer)); 581 582 spin_lock_irqsave(&devinfo->lock, flags); 583 if (devinfo->cmnd) { 584 spin_unlock_irqrestore(&devinfo->lock, flags); 585 return SCSI_MLQUEUE_DEVICE_BUSY; 586 } 587 588 if (blk_rq_tagged(cmnd->request)) { 589 cmdinfo->stream = cmnd->request->tag + 2; 590 } else { 591 devinfo->cmnd = cmnd; 592 cmdinfo->stream = 1; 593 } 594 595 cmnd->scsi_done = done; 596 597 cmdinfo->state = SUBMIT_STATUS_URB | 598 ALLOC_CMD_URB | SUBMIT_CMD_URB; 599 600 switch (cmnd->sc_data_direction) { 601 case DMA_FROM_DEVICE: 602 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB; 603 break; 604 case DMA_BIDIRECTIONAL: 605 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB; 606 case DMA_TO_DEVICE: 607 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB; 608 case DMA_NONE: 609 break; 610 } 611 612 if (!devinfo->use_streams) { 613 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB); 614 cmdinfo->stream = 0; 615 } 616 617 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC); 618 if (err) { 619 /* If we did nothing, give up now */ 620 if (cmdinfo->state & SUBMIT_STATUS_URB) { 621 spin_unlock_irqrestore(&devinfo->lock, flags); 622 return SCSI_MLQUEUE_DEVICE_BUSY; 623 } 624 spin_lock(&uas_work_lock); 625 list_add_tail(&cmdinfo->list, &uas_work_list); 626 spin_unlock(&uas_work_lock); 627 schedule_work(&uas_work); 628 } 629 630 spin_unlock_irqrestore(&devinfo->lock, flags); 631 return 0; 632 } 633 634 static DEF_SCSI_QCMD(uas_queuecommand) 635 636 static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd, 637 const char *fname, u8 function) 638 { 639 struct Scsi_Host *shost = cmnd->device->host; 640 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 641 u16 tag = devinfo->qdepth - 1; 642 unsigned long flags; 643 644 spin_lock_irqsave(&devinfo->lock, flags); 645 memset(&devinfo->response, 0, sizeof(devinfo->response)); 646 if (uas_submit_sense_urb(shost, GFP_ATOMIC, tag)) { 647 shost_printk(KERN_INFO, shost, 648 "%s: %s: submit sense urb failed\n", 649 __func__, fname); 650 spin_unlock_irqrestore(&devinfo->lock, flags); 651 return FAILED; 652 } 653 if (uas_submit_task_urb(cmnd, GFP_ATOMIC, function, tag)) { 654 shost_printk(KERN_INFO, shost, 655 "%s: %s: submit task mgmt urb failed\n", 656 __func__, fname); 657 spin_unlock_irqrestore(&devinfo->lock, flags); 658 return FAILED; 659 } 660 spin_unlock_irqrestore(&devinfo->lock, flags); 661 662 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) { 663 shost_printk(KERN_INFO, shost, 664 "%s: %s timed out\n", __func__, fname); 665 return FAILED; 666 } 667 if (be16_to_cpu(devinfo->response.tag) != tag) { 668 shost_printk(KERN_INFO, shost, 669 "%s: %s failed (wrong tag %d/%d)\n", __func__, 670 fname, be16_to_cpu(devinfo->response.tag), tag); 671 return FAILED; 672 } 673 if (devinfo->response.response_code != RC_TMF_COMPLETE) { 674 shost_printk(KERN_INFO, shost, 675 "%s: %s failed (rc 0x%x)\n", __func__, 676 fname, devinfo->response.response_code); 677 return FAILED; 678 } 679 return SUCCESS; 680 } 681 682 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd) 683 { 684 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 685 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; 686 unsigned long flags; 687 int ret; 688 689 uas_log_cmd_state(cmnd, __func__); 690 spin_lock_irqsave(&devinfo->lock, flags); 691 cmdinfo->state |= COMMAND_ABORTED; 692 spin_unlock_irqrestore(&devinfo->lock, flags); 693 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK); 694 return ret; 695 } 696 697 static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd) 698 { 699 sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__); 700 return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET", 701 TMF_LOGICAL_UNIT_RESET); 702 } 703 704 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd) 705 { 706 struct scsi_device *sdev = cmnd->device; 707 struct uas_dev_info *devinfo = sdev->hostdata; 708 struct usb_device *udev = devinfo->udev; 709 int err; 710 711 devinfo->resetting = 1; 712 usb_kill_anchored_urbs(&devinfo->cmd_urbs); 713 usb_kill_anchored_urbs(&devinfo->sense_urbs); 714 usb_kill_anchored_urbs(&devinfo->data_urbs); 715 err = usb_reset_device(udev); 716 devinfo->resetting = 0; 717 718 if (err) { 719 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__); 720 return FAILED; 721 } 722 723 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__); 724 return SUCCESS; 725 } 726 727 static int uas_slave_alloc(struct scsi_device *sdev) 728 { 729 sdev->hostdata = (void *)sdev->host->hostdata[0]; 730 return 0; 731 } 732 733 static int uas_slave_configure(struct scsi_device *sdev) 734 { 735 struct uas_dev_info *devinfo = sdev->hostdata; 736 scsi_set_tag_type(sdev, MSG_ORDERED_TAG); 737 scsi_activate_tcq(sdev, devinfo->qdepth - 3); 738 return 0; 739 } 740 741 static struct scsi_host_template uas_host_template = { 742 .module = THIS_MODULE, 743 .name = "uas", 744 .queuecommand = uas_queuecommand, 745 .slave_alloc = uas_slave_alloc, 746 .slave_configure = uas_slave_configure, 747 .eh_abort_handler = uas_eh_abort_handler, 748 .eh_device_reset_handler = uas_eh_device_reset_handler, 749 .eh_bus_reset_handler = uas_eh_bus_reset_handler, 750 .can_queue = 65536, /* Is there a limit on the _host_ ? */ 751 .this_id = -1, 752 .sg_tablesize = SG_NONE, 753 .cmd_per_lun = 1, /* until we override it */ 754 .skip_settle_delay = 1, 755 .ordered_tag = 1, 756 }; 757 758 static struct usb_device_id uas_usb_ids[] = { 759 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) }, 760 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) }, 761 /* 0xaa is a prototype device I happen to have access to */ 762 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) }, 763 { } 764 }; 765 MODULE_DEVICE_TABLE(usb, uas_usb_ids); 766 767 static int uas_is_interface(struct usb_host_interface *intf) 768 { 769 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE && 770 intf->desc.bInterfaceSubClass == USB_SC_SCSI && 771 intf->desc.bInterfaceProtocol == USB_PR_UAS); 772 } 773 774 static int uas_isnt_supported(struct usb_device *udev) 775 { 776 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 777 778 dev_warn(&udev->dev, "The driver for the USB controller %s does not " 779 "support scatter-gather which is\n", 780 hcd->driver->description); 781 dev_warn(&udev->dev, "required by the UAS driver. Please try an" 782 "alternative USB controller if you wish to use UAS.\n"); 783 return -ENODEV; 784 } 785 786 static int uas_switch_interface(struct usb_device *udev, 787 struct usb_interface *intf) 788 { 789 int i; 790 int sg_supported = udev->bus->sg_tablesize != 0; 791 792 for (i = 0; i < intf->num_altsetting; i++) { 793 struct usb_host_interface *alt = &intf->altsetting[i]; 794 795 if (uas_is_interface(alt)) { 796 if (!sg_supported) 797 return uas_isnt_supported(udev); 798 return usb_set_interface(udev, 799 alt->desc.bInterfaceNumber, 800 alt->desc.bAlternateSetting); 801 } 802 } 803 804 return -ENODEV; 805 } 806 807 static void uas_configure_endpoints(struct uas_dev_info *devinfo) 808 { 809 struct usb_host_endpoint *eps[4] = { }; 810 struct usb_interface *intf = devinfo->intf; 811 struct usb_device *udev = devinfo->udev; 812 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint; 813 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints; 814 815 devinfo->uas_sense_old = 0; 816 devinfo->cmnd = NULL; 817 818 for (i = 0; i < n_endpoints; i++) { 819 unsigned char *extra = endpoint[i].extra; 820 int len = endpoint[i].extralen; 821 while (len > 1) { 822 if (extra[1] == USB_DT_PIPE_USAGE) { 823 unsigned pipe_id = extra[2]; 824 if (pipe_id > 0 && pipe_id < 5) 825 eps[pipe_id - 1] = &endpoint[i]; 826 break; 827 } 828 len -= extra[0]; 829 extra += extra[0]; 830 } 831 } 832 833 /* 834 * Assume that if we didn't find a control pipe descriptor, we're 835 * using a device with old firmware that happens to be set up like 836 * this. 837 */ 838 if (!eps[0]) { 839 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1); 840 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1); 841 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2); 842 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2); 843 844 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe); 845 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe); 846 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe); 847 } else { 848 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 849 eps[0]->desc.bEndpointAddress); 850 devinfo->status_pipe = usb_rcvbulkpipe(udev, 851 eps[1]->desc.bEndpointAddress); 852 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 853 eps[2]->desc.bEndpointAddress); 854 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 855 eps[3]->desc.bEndpointAddress); 856 } 857 858 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256, 859 GFP_KERNEL); 860 if (devinfo->qdepth < 0) { 861 devinfo->qdepth = 256; 862 devinfo->use_streams = 0; 863 } else { 864 devinfo->use_streams = 1; 865 } 866 } 867 868 static void uas_free_streams(struct uas_dev_info *devinfo) 869 { 870 struct usb_device *udev = devinfo->udev; 871 struct usb_host_endpoint *eps[3]; 872 873 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe); 874 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe); 875 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe); 876 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL); 877 } 878 879 /* 880 * XXX: What I'd like to do here is register a SCSI host for each USB host in 881 * the system. Follow usb-storage's design of registering a SCSI host for 882 * each USB device for the moment. Can implement this by walking up the 883 * USB hierarchy until we find a USB host. 884 */ 885 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id) 886 { 887 int result; 888 struct Scsi_Host *shost; 889 struct uas_dev_info *devinfo; 890 struct usb_device *udev = interface_to_usbdev(intf); 891 892 if (uas_switch_interface(udev, intf)) 893 return -ENODEV; 894 895 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL); 896 if (!devinfo) 897 return -ENOMEM; 898 899 result = -ENOMEM; 900 shost = scsi_host_alloc(&uas_host_template, sizeof(void *)); 901 if (!shost) 902 goto free; 903 904 shost->max_cmd_len = 16 + 252; 905 shost->max_id = 1; 906 shost->sg_tablesize = udev->bus->sg_tablesize; 907 908 devinfo->intf = intf; 909 devinfo->udev = udev; 910 devinfo->resetting = 0; 911 init_usb_anchor(&devinfo->cmd_urbs); 912 init_usb_anchor(&devinfo->sense_urbs); 913 init_usb_anchor(&devinfo->data_urbs); 914 spin_lock_init(&devinfo->lock); 915 uas_configure_endpoints(devinfo); 916 917 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 3); 918 if (result) 919 goto free; 920 921 result = scsi_add_host(shost, &intf->dev); 922 if (result) 923 goto deconfig_eps; 924 925 shost->hostdata[0] = (unsigned long)devinfo; 926 927 scsi_scan_host(shost); 928 usb_set_intfdata(intf, shost); 929 return result; 930 931 deconfig_eps: 932 uas_free_streams(devinfo); 933 free: 934 kfree(devinfo); 935 if (shost) 936 scsi_host_put(shost); 937 return result; 938 } 939 940 static int uas_pre_reset(struct usb_interface *intf) 941 { 942 /* XXX: Need to return 1 if it's not our device in error handling */ 943 return 0; 944 } 945 946 static int uas_post_reset(struct usb_interface *intf) 947 { 948 /* XXX: Need to return 1 if it's not our device in error handling */ 949 return 0; 950 } 951 952 static void uas_disconnect(struct usb_interface *intf) 953 { 954 struct Scsi_Host *shost = usb_get_intfdata(intf); 955 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 956 957 scsi_remove_host(shost); 958 usb_kill_anchored_urbs(&devinfo->cmd_urbs); 959 usb_kill_anchored_urbs(&devinfo->sense_urbs); 960 usb_kill_anchored_urbs(&devinfo->data_urbs); 961 uas_free_streams(devinfo); 962 kfree(devinfo); 963 } 964 965 /* 966 * XXX: Should this plug into libusual so we can auto-upgrade devices from 967 * Bulk-Only to UAS? 968 */ 969 static struct usb_driver uas_driver = { 970 .name = "uas", 971 .probe = uas_probe, 972 .disconnect = uas_disconnect, 973 .pre_reset = uas_pre_reset, 974 .post_reset = uas_post_reset, 975 .id_table = uas_usb_ids, 976 }; 977 978 module_usb_driver(uas_driver); 979 980 MODULE_LICENSE("GPL"); 981 MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp"); 982