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