3 * Note that this is not the same as the USB Mass Storage driver
5 * Copyright Matthew Wilcox for Intel Corp, 2010
6 * Copyright Sarah Sharp for Intel Corp, 2010
8 * Distributed under the terms of the GNU GPL, version two.
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/storage.h>
18 #include <scsi/scsi.h>
19 #include <scsi/scsi_dbg.h>
20 #include <scsi/scsi_cmnd.h>
21 #include <scsi/scsi_device.h>
22 #include <scsi/scsi_host.h>
23 #include <scsi/scsi_tcq.h>
25 /* Common header for all IUs */
35 IU_ID_RESPONSE = 0x04,
36 IU_ID_TASK_MGMT = 0x05,
37 IU_ID_READ_READY = 0x06,
38 IU_ID_WRITE_READY = 0x07,
50 __u8 cdb[16]; /* XXX: Overflow-checking tools may misunderstand */
54 * Also used for the Read Ready and Write Ready IUs since they have the
55 * same first four bytes
65 __u8 sense[SCSI_SENSE_BUFFERSIZE];
69 * The r00-r01c specs define this version of the SENSE IU data structure.
70 * It's still in use by several different firmware releases.
78 __u8 service_response;
79 __u8 sense[SCSI_SENSE_BUFFERSIZE];
95 struct usb_interface *intf;
96 struct usb_device *udev;
98 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
99 unsigned use_streams:1;
100 unsigned uas_sense_old:1;
104 ALLOC_STATUS_URB = (1 << 0),
105 SUBMIT_STATUS_URB = (1 << 1),
106 ALLOC_DATA_IN_URB = (1 << 2),
107 SUBMIT_DATA_IN_URB = (1 << 3),
108 ALLOC_DATA_OUT_URB = (1 << 4),
109 SUBMIT_DATA_OUT_URB = (1 << 5),
110 ALLOC_CMD_URB = (1 << 6),
111 SUBMIT_CMD_URB = (1 << 7),
114 /* Overrides scsi_pointer */
115 struct uas_cmd_info {
119 struct urb *status_urb;
120 struct urb *data_in_urb;
121 struct urb *data_out_urb;
122 struct list_head list;
125 /* I hate forward declarations, but I actually have a loop */
126 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
127 struct uas_dev_info *devinfo, gfp_t gfp);
129 static DEFINE_SPINLOCK(uas_work_lock);
130 static LIST_HEAD(uas_work_list);
132 static void uas_do_work(struct work_struct *work)
134 struct uas_cmd_info *cmdinfo;
135 struct list_head list;
137 spin_lock_irq(&uas_work_lock);
138 list_replace_init(&uas_work_list, &list);
139 spin_unlock_irq(&uas_work_lock);
141 list_for_each_entry(cmdinfo, &list, list) {
142 struct scsi_pointer *scp = (void *)cmdinfo;
143 struct scsi_cmnd *cmnd = container_of(scp,
144 struct scsi_cmnd, SCp);
145 uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
149 static DECLARE_WORK(uas_work, uas_do_work);
151 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
153 struct sense_iu *sense_iu = urb->transfer_buffer;
154 struct scsi_device *sdev = cmnd->device;
156 if (urb->actual_length > 16) {
157 unsigned len = be16_to_cpup(&sense_iu->len);
158 if (len + 16 != urb->actual_length) {
159 int newlen = min(len + 16, urb->actual_length) - 16;
162 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
163 "disagrees with IU sense data length %d, "
164 "using %d bytes of sense data\n", __func__,
165 urb->actual_length, len, newlen);
168 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
171 cmnd->result = sense_iu->status;
172 if (sdev->current_cmnd)
173 sdev->current_cmnd = NULL;
174 cmnd->scsi_done(cmnd);
178 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
180 struct sense_iu_old *sense_iu = urb->transfer_buffer;
181 struct scsi_device *sdev = cmnd->device;
183 if (urb->actual_length > 8) {
184 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
185 if (len + 8 != urb->actual_length) {
186 int newlen = min(len + 8, urb->actual_length) - 8;
189 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
190 "disagrees with IU sense data length %d, "
191 "using %d bytes of sense data\n", __func__,
192 urb->actual_length, len, newlen);
195 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
198 cmnd->result = sense_iu->status;
199 if (sdev->current_cmnd)
200 sdev->current_cmnd = NULL;
201 cmnd->scsi_done(cmnd);
205 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
208 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
211 cmdinfo->state = direction | SUBMIT_STATUS_URB;
212 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
214 spin_lock(&uas_work_lock);
215 list_add_tail(&cmdinfo->list, &uas_work_list);
216 spin_unlock(&uas_work_lock);
217 schedule_work(&uas_work);
221 static void uas_stat_cmplt(struct urb *urb)
223 struct iu *iu = urb->transfer_buffer;
224 struct scsi_device *sdev = urb->context;
225 struct uas_dev_info *devinfo = sdev->hostdata;
226 struct scsi_cmnd *cmnd;
230 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
235 tag = be16_to_cpup(&iu->tag) - 1;
236 if (sdev->current_cmnd)
237 cmnd = sdev->current_cmnd;
239 cmnd = scsi_find_tag(sdev, tag);
245 if (urb->actual_length < 16)
246 devinfo->uas_sense_old = 1;
247 if (devinfo->uas_sense_old)
248 uas_sense_old(urb, cmnd);
250 uas_sense(urb, cmnd);
252 case IU_ID_READ_READY:
253 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
255 case IU_ID_WRITE_READY:
256 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
259 scmd_printk(KERN_ERR, cmnd,
260 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
264 static void uas_data_cmplt(struct urb *urb)
266 struct scsi_data_buffer *sdb = urb->context;
267 sdb->resid = sdb->length - urb->actual_length;
271 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
272 unsigned int pipe, u16 stream_id,
273 struct scsi_data_buffer *sdb,
274 enum dma_data_direction dir)
276 struct usb_device *udev = devinfo->udev;
277 struct urb *urb = usb_alloc_urb(0, gfp);
281 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, uas_data_cmplt,
283 if (devinfo->use_streams)
284 urb->stream_id = stream_id;
285 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
286 urb->sg = sdb->table.sgl;
291 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
292 struct scsi_cmnd *cmnd, u16 stream_id)
294 struct usb_device *udev = devinfo->udev;
295 struct urb *urb = usb_alloc_urb(0, gfp);
301 iu = kzalloc(sizeof(*iu), gfp);
305 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
306 uas_stat_cmplt, cmnd->device);
307 urb->stream_id = stream_id;
308 urb->transfer_flags |= URB_FREE_BUFFER;
316 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
317 struct scsi_cmnd *cmnd, u16 stream_id)
319 struct usb_device *udev = devinfo->udev;
320 struct scsi_device *sdev = cmnd->device;
321 struct urb *urb = usb_alloc_urb(0, gfp);
322 struct command_iu *iu;
328 len = cmnd->cmd_len - 16;
332 iu = kzalloc(sizeof(*iu) + len, gfp);
336 iu->iu_id = IU_ID_COMMAND;
337 iu->tag = cpu_to_be16(stream_id);
338 iu->prio_attr = UAS_SIMPLE_TAG;
340 int_to_scsilun(sdev->lun, &iu->lun);
341 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
343 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
345 urb->transfer_flags |= URB_FREE_BUFFER;
354 * Why should I request the Status IU before sending the Command IU? Spec
355 * says to, but also says the device may receive them in any order. Seems
359 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
360 struct uas_dev_info *devinfo, gfp_t gfp)
362 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
364 if (cmdinfo->state & ALLOC_STATUS_URB) {
365 cmdinfo->status_urb = uas_alloc_sense_urb(devinfo, gfp, cmnd,
367 if (!cmdinfo->status_urb)
368 return SCSI_MLQUEUE_DEVICE_BUSY;
369 cmdinfo->state &= ~ALLOC_STATUS_URB;
372 if (cmdinfo->state & SUBMIT_STATUS_URB) {
373 if (usb_submit_urb(cmdinfo->status_urb, gfp)) {
374 scmd_printk(KERN_INFO, cmnd,
375 "sense urb submission failure\n");
376 return SCSI_MLQUEUE_DEVICE_BUSY;
378 cmdinfo->state &= ~SUBMIT_STATUS_URB;
381 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
382 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
383 devinfo->data_in_pipe, cmdinfo->stream,
384 scsi_in(cmnd), DMA_FROM_DEVICE);
385 if (!cmdinfo->data_in_urb)
386 return SCSI_MLQUEUE_DEVICE_BUSY;
387 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
390 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
391 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
392 scmd_printk(KERN_INFO, cmnd,
393 "data in urb submission failure\n");
394 return SCSI_MLQUEUE_DEVICE_BUSY;
396 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
399 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
400 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
401 devinfo->data_out_pipe, cmdinfo->stream,
402 scsi_out(cmnd), DMA_TO_DEVICE);
403 if (!cmdinfo->data_out_urb)
404 return SCSI_MLQUEUE_DEVICE_BUSY;
405 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
408 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
409 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
410 scmd_printk(KERN_INFO, cmnd,
411 "data out urb submission failure\n");
412 return SCSI_MLQUEUE_DEVICE_BUSY;
414 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
417 if (cmdinfo->state & ALLOC_CMD_URB) {
418 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
420 if (!cmdinfo->cmd_urb)
421 return SCSI_MLQUEUE_DEVICE_BUSY;
422 cmdinfo->state &= ~ALLOC_CMD_URB;
425 if (cmdinfo->state & SUBMIT_CMD_URB) {
426 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
427 scmd_printk(KERN_INFO, cmnd,
428 "cmd urb submission failure\n");
429 return SCSI_MLQUEUE_DEVICE_BUSY;
431 cmdinfo->state &= ~SUBMIT_CMD_URB;
437 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
438 void (*done)(struct scsi_cmnd *))
440 struct scsi_device *sdev = cmnd->device;
441 struct uas_dev_info *devinfo = sdev->hostdata;
442 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
445 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
447 if (!cmdinfo->status_urb && sdev->current_cmnd)
448 return SCSI_MLQUEUE_DEVICE_BUSY;
450 if (blk_rq_tagged(cmnd->request)) {
451 cmdinfo->stream = cmnd->request->tag + 1;
453 sdev->current_cmnd = cmnd;
457 cmnd->scsi_done = done;
459 cmdinfo->state = ALLOC_STATUS_URB | SUBMIT_STATUS_URB |
460 ALLOC_CMD_URB | SUBMIT_CMD_URB;
462 switch (cmnd->sc_data_direction) {
463 case DMA_FROM_DEVICE:
464 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
466 case DMA_BIDIRECTIONAL:
467 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
469 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
474 if (!devinfo->use_streams) {
475 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
479 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
481 /* If we did nothing, give up now */
482 if (cmdinfo->state & SUBMIT_STATUS_URB) {
483 usb_free_urb(cmdinfo->status_urb);
484 return SCSI_MLQUEUE_DEVICE_BUSY;
486 spin_lock(&uas_work_lock);
487 list_add_tail(&cmdinfo->list, &uas_work_list);
488 spin_unlock(&uas_work_lock);
489 schedule_work(&uas_work);
495 static DEF_SCSI_QCMD(uas_queuecommand)
497 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
499 struct scsi_device *sdev = cmnd->device;
500 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
503 /* XXX: Send ABORT TASK Task Management command */
507 static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
509 struct scsi_device *sdev = cmnd->device;
510 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
513 /* XXX: Send LOGICAL UNIT RESET Task Management command */
517 static int uas_eh_target_reset_handler(struct scsi_cmnd *cmnd)
519 struct scsi_device *sdev = cmnd->device;
520 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
523 /* XXX: Can we reset just the one USB interface?
524 * Would calling usb_set_interface() have the right effect?
529 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
531 struct scsi_device *sdev = cmnd->device;
532 struct uas_dev_info *devinfo = sdev->hostdata;
533 struct usb_device *udev = devinfo->udev;
535 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
538 if (usb_reset_device(udev))
544 static int uas_slave_alloc(struct scsi_device *sdev)
546 sdev->hostdata = (void *)sdev->host->hostdata[0];
550 static int uas_slave_configure(struct scsi_device *sdev)
552 struct uas_dev_info *devinfo = sdev->hostdata;
553 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
554 scsi_activate_tcq(sdev, devinfo->qdepth - 1);
558 static struct scsi_host_template uas_host_template = {
559 .module = THIS_MODULE,
561 .queuecommand = uas_queuecommand,
562 .slave_alloc = uas_slave_alloc,
563 .slave_configure = uas_slave_configure,
564 .eh_abort_handler = uas_eh_abort_handler,
565 .eh_device_reset_handler = uas_eh_device_reset_handler,
566 .eh_target_reset_handler = uas_eh_target_reset_handler,
567 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
568 .can_queue = 65536, /* Is there a limit on the _host_ ? */
570 .sg_tablesize = SG_NONE,
571 .cmd_per_lun = 1, /* until we override it */
572 .skip_settle_delay = 1,
576 static struct usb_device_id uas_usb_ids[] = {
577 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
578 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
579 /* 0xaa is a prototype device I happen to have access to */
580 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
583 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
585 static int uas_is_interface(struct usb_host_interface *intf)
587 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
588 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
589 intf->desc.bInterfaceProtocol == USB_PR_UAS);
592 static int uas_switch_interface(struct usb_device *udev,
593 struct usb_interface *intf)
597 if (uas_is_interface(intf->cur_altsetting))
600 for (i = 0; i < intf->num_altsetting; i++) {
601 struct usb_host_interface *alt = &intf->altsetting[i];
602 if (alt == intf->cur_altsetting)
604 if (uas_is_interface(alt))
605 return usb_set_interface(udev,
606 alt->desc.bInterfaceNumber,
607 alt->desc.bAlternateSetting);
613 static void uas_configure_endpoints(struct uas_dev_info *devinfo)
615 struct usb_host_endpoint *eps[4] = { };
616 struct usb_interface *intf = devinfo->intf;
617 struct usb_device *udev = devinfo->udev;
618 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
619 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
621 devinfo->uas_sense_old = 0;
623 for (i = 0; i < n_endpoints; i++) {
624 unsigned char *extra = endpoint[i].extra;
625 int len = endpoint[i].extralen;
627 if (extra[1] == USB_DT_PIPE_USAGE) {
628 unsigned pipe_id = extra[2];
629 if (pipe_id > 0 && pipe_id < 5)
630 eps[pipe_id - 1] = &endpoint[i];
639 * Assume that if we didn't find a control pipe descriptor, we're
640 * using a device with old firmware that happens to be set up like
644 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
645 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
646 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
647 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
649 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
650 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
651 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
653 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
654 eps[0]->desc.bEndpointAddress);
655 devinfo->status_pipe = usb_rcvbulkpipe(udev,
656 eps[1]->desc.bEndpointAddress);
657 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
658 eps[2]->desc.bEndpointAddress);
659 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
660 eps[3]->desc.bEndpointAddress);
663 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
665 if (devinfo->qdepth < 0) {
666 devinfo->qdepth = 256;
667 devinfo->use_streams = 0;
669 devinfo->use_streams = 1;
674 * XXX: What I'd like to do here is register a SCSI host for each USB host in
675 * the system. Follow usb-storage's design of registering a SCSI host for
676 * each USB device for the moment. Can implement this by walking up the
677 * USB hierarchy until we find a USB host.
679 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
682 struct Scsi_Host *shost;
683 struct uas_dev_info *devinfo;
684 struct usb_device *udev = interface_to_usbdev(intf);
686 if (uas_switch_interface(udev, intf))
689 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
694 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
698 shost->max_cmd_len = 16 + 252;
700 shost->sg_tablesize = udev->bus->sg_tablesize;
702 result = scsi_add_host(shost, &intf->dev);
705 shost->hostdata[0] = (unsigned long)devinfo;
707 devinfo->intf = intf;
708 devinfo->udev = udev;
709 uas_configure_endpoints(devinfo);
711 scsi_scan_host(shost);
712 usb_set_intfdata(intf, shost);
717 scsi_host_put(shost);
721 static int uas_pre_reset(struct usb_interface *intf)
723 /* XXX: Need to return 1 if it's not our device in error handling */
727 static int uas_post_reset(struct usb_interface *intf)
729 /* XXX: Need to return 1 if it's not our device in error handling */
733 static void uas_disconnect(struct usb_interface *intf)
735 struct usb_device *udev = interface_to_usbdev(intf);
736 struct usb_host_endpoint *eps[3];
737 struct Scsi_Host *shost = usb_get_intfdata(intf);
738 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
740 scsi_remove_host(shost);
742 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
743 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
744 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
745 usb_free_streams(intf, eps, 3, GFP_KERNEL);
751 * XXX: Should this plug into libusual so we can auto-upgrade devices from
754 static struct usb_driver uas_driver = {
757 .disconnect = uas_disconnect,
758 .pre_reset = uas_pre_reset,
759 .post_reset = uas_post_reset,
760 .id_table = uas_usb_ids,
763 static int uas_init(void)
765 return usb_register(&uas_driver);
768 static void uas_exit(void)
770 usb_deregister(&uas_driver);
773 module_init(uas_init);
774 module_exit(uas_exit);
776 MODULE_LICENSE("GPL");
777 MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");