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