12 #include <linux/slab.h>
13 #include <linux/types.h>
14 #include <linux/module.h>
20 #include <scsi/scsi.h>
82 static int uas_submit_urbs(
struct scsi_cmnd *cmnd,
98 spin_lock_irq(&uas_work_lock);
99 list_replace_init(&uas_work_list, &
list);
100 spin_unlock_irq(&uas_work_lock);
109 spin_unlock_irqrestore(&devinfo->
lock, flags);
112 spin_lock_irq(&uas_work_lock);
114 spin_unlock_irq(&uas_work_lock);
125 if (urb->actual_length > 16) {
127 if (len + 16 != urb->actual_length) {
128 int newlen =
min(len + 16, urb->actual_length) - 16;
132 "disagrees with IU sense data length %d, "
133 "using %d bytes of sense data\n", __func__,
134 urb->actual_length, len, newlen);
143 static void uas_sense_old(
struct urb *urb,
struct scsi_cmnd *cmnd)
148 if (urb->actual_length > 8) {
150 if (len + 8 != urb->actual_length) {
151 int newlen =
min(len + 8, urb->actual_length) - 8;
155 "disagrees with IU sense data length %d, "
156 "using %d bytes of sense data\n", __func__,
157 urb->actual_length, len, newlen);
166 static void uas_log_cmd_state(
struct scsi_cmnd *cmnd,
const char *
caller)
171 "%s%s%s%s%s%s%s%s%s%s%s%s\n",
172 caller, cmnd, cmnd->
request->tag,
187 static int uas_try_complete(
struct scsi_cmnd *cmnd,
const char *caller)
209 static void uas_xfer_data(
struct urb *urb,
struct scsi_cmnd *cmnd,
218 spin_lock(&uas_work_lock);
220 spin_unlock(&uas_work_lock);
225 static void uas_stat_cmplt(
struct urb *urb)
227 struct iu *
iu = urb->transfer_buffer;
229 struct uas_dev_info *devinfo = (
void *)shost->hostdata[0];
236 dev_err(&urb->dev->dev,
"URB BAD STATUS %d\n", urb->status);
249 cmnd = devinfo->
cmnd;
251 cmnd = scsi_host_find_tag(shost,
tag - 1);
259 spin_unlock_irqrestore(&devinfo->
lock, flags);
263 cmdinfo = (
void *)&cmnd->
SCp;
266 if (devinfo->
cmnd == cmnd)
269 if (urb->actual_length < 16)
274 uas_sense(urb, cmnd);
278 spin_unlock_irqrestore(&devinfo->
lock, flags);
283 spin_unlock_irqrestore(&devinfo->
lock, flags);
289 uas_try_complete(cmnd, __func__);
299 "Bogus IU (%d) received on status pipe\n", iu->
iu_id);
302 spin_unlock_irqrestore(&devinfo->
lock, flags);
305 static void uas_data_cmplt(
struct urb *urb)
318 sdb = scsi_out(cmnd);
328 uas_try_complete(cmnd, __func__);
329 spin_unlock_irqrestore(&devinfo->
lock, flags);
333 unsigned int pipe,
u16 stream_id,
337 struct usb_device *
udev = devinfo->
udev;
340 ? scsi_in(cmnd) : scsi_out(cmnd);
344 usb_fill_bulk_urb(urb, udev, pipe,
NULL, sdb->
length,
345 uas_data_cmplt, cmnd);
347 urb->stream_id = stream_id;
348 urb->num_sgs = udev->bus->sg_tablesize ? sdb->
table.nents : 0;
349 urb->sg = sdb->
table.sgl;
357 struct usb_device *udev = devinfo->
udev;
364 iu = kzalloc(
sizeof(*iu), gfp);
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;
382 struct usb_device *udev = devinfo->
udev;
395 iu = kzalloc(
sizeof(*iu) + len, gfp);
400 if (blk_rq_tagged(cmnd->
request))
409 usb_fill_bulk_urb(urb, udev, devinfo->
cmd_pipe, iu,
sizeof(*iu) + len,
411 urb->transfer_flags |= URB_FREE_BUFFER;
420 u8 function,
u16 stream_id)
423 struct usb_device *udev = devinfo->
udev;
431 iu = kzalloc(
sizeof(*iu), gfp);
442 if (blk_rq_tagged(cmnd->
request))
449 usb_fill_bulk_urb(urb, udev, devinfo->
cmd_pipe, iu,
sizeof(*iu),
451 urb->transfer_flags |= URB_FREE_BUFFER;
471 static int uas_submit_sense_urb(
struct Scsi_Host *shost,
472 gfp_t gfp,
unsigned int stream)
474 struct uas_dev_info *devinfo = (
void *)shost->hostdata[0];
477 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
482 "sense urb submission failure\n");
490 static int uas_submit_urbs(
struct scsi_cmnd *cmnd,
498 err = uas_submit_sense_urb(cmnd->
device->host, gfp,
507 cmdinfo->
data_in_urb = uas_alloc_data_urb(devinfo, gfp,
518 "data in urb submission failure\n");
527 cmdinfo->
data_out_urb = uas_alloc_data_urb(devinfo, gfp,
538 "data out urb submission failure\n");
547 cmdinfo->
cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
558 "cmd urb submission failure\n");
571 static int uas_queuecommand_lck(
struct scsi_cmnd *cmnd,
584 spin_unlock_irqrestore(&devinfo->
lock, flags);
588 if (blk_rq_tagged(cmnd->
request)) {
591 devinfo->
cmnd = cmnd;
617 err = uas_submit_urbs(cmnd, devinfo,
GFP_ATOMIC);
621 spin_unlock_irqrestore(&devinfo->
lock, flags);
624 spin_lock(&uas_work_lock);
626 spin_unlock(&uas_work_lock);
630 spin_unlock_irqrestore(&devinfo->
lock, flags);
639 struct Scsi_Host *shost = cmnd->device->host;
640 struct uas_dev_info *devinfo = (
void *)shost->hostdata[0];
648 "%s: %s: submit sense urb failed\n",
650 spin_unlock_irqrestore(&devinfo->
lock, flags);
655 "%s: %s: submit task mgmt urb failed\n",
657 spin_unlock_irqrestore(&devinfo->
lock, flags);
660 spin_unlock_irqrestore(&devinfo->
lock, flags);
664 "%s: %s timed out\n", __func__, fname);
669 "%s: %s failed (wrong tag %d/%d)\n", __func__,
675 "%s: %s failed (rc 0x%x)\n", __func__,
676 fname, devinfo->
response.response_code);
682 static int uas_eh_abort_handler(
struct scsi_cmnd *cmnd)
689 uas_log_cmd_state(cmnd, __func__);
692 spin_unlock_irqrestore(&devinfo->
lock, flags);
697 static int uas_eh_device_reset_handler(
struct scsi_cmnd *cmnd)
700 return uas_eh_task_mgmt(cmnd,
"LOGICAL UNIT RESET",
704 static int uas_eh_bus_reset_handler(
struct scsi_cmnd *cmnd)
708 struct usb_device *udev = devinfo->
udev;
727 static int uas_slave_alloc(
struct scsi_device *sdev)
733 static int uas_slave_configure(
struct scsi_device *sdev)
737 scsi_activate_tcq(sdev, devinfo->
qdepth - 3);
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,
754 .skip_settle_delay = 1,
767 static int uas_is_interface(
struct usb_host_interface *
intf)
774 static int uas_isnt_supported(
struct usb_device *udev)
776 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
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");
786 static int uas_switch_interface(
struct usb_device *udev,
790 int sg_supported = udev->bus->sg_tablesize != 0;
792 for (i = 0; i < intf->num_altsetting; i++) {
793 struct usb_host_interface *alt = &intf->altsetting[
i];
795 if (uas_is_interface(alt)) {
797 return uas_isnt_supported(udev);
799 alt->desc.bInterfaceNumber,
800 alt->desc.bAlternateSetting);
807 static void uas_configure_endpoints(
struct uas_dev_info *devinfo)
809 struct usb_host_endpoint *eps[4] = { };
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;
818 for (i = 0; i < n_endpoints; i++) {
819 unsigned char *
extra = endpoint[
i].extra;
820 int len = endpoint[
i].extralen;
823 unsigned pipe_id = extra[2];
824 if (pipe_id > 0 && pipe_id < 5)
825 eps[pipe_id - 1] = &endpoint[
i];
839 devinfo->
cmd_pipe = usb_sndbulkpipe(udev, 1);
844 eps[1] = usb_pipe_endpoint(udev, devinfo->
status_pipe);
845 eps[2] = usb_pipe_endpoint(udev, devinfo->
data_in_pipe);
848 devinfo->
cmd_pipe = usb_sndbulkpipe(udev,
849 eps[0]->
desc.bEndpointAddress);
851 eps[1]->
desc.bEndpointAddress);
853 eps[2]->
desc.bEndpointAddress);
855 eps[3]->
desc.bEndpointAddress);
860 if (devinfo->
qdepth < 0) {
868 static void uas_free_streams(
struct uas_dev_info *devinfo)
870 struct usb_device *udev = devinfo->
udev;
871 struct usb_host_endpoint *eps[3];
873 eps[0] = usb_pipe_endpoint(udev, devinfo->
status_pipe);
874 eps[1] = usb_pipe_endpoint(udev, devinfo->
data_in_pipe);
890 struct usb_device *udev = interface_to_usbdev(intf);
892 if (uas_switch_interface(udev, intf))
911 init_usb_anchor(&devinfo->
cmd_urbs);
915 uas_configure_endpoints(devinfo);
917 result = scsi_init_shared_tag_map(shost, devinfo->
qdepth - 3);
921 result = scsi_add_host(shost, &intf->dev);
925 shost->hostdata[0] = (
unsigned long)devinfo;
928 usb_set_intfdata(intf, shost);
932 uas_free_streams(devinfo);
954 struct Scsi_Host *shost = usb_get_intfdata(intf);
955 struct uas_dev_info *devinfo = (
void *)shost->hostdata[0];
961 uas_free_streams(devinfo);
969 static struct usb_driver uas_driver = {
972 .disconnect = uas_disconnect,
973 .pre_reset = uas_pre_reset,
974 .post_reset = uas_post_reset,
975 .id_table = uas_usb_ids,