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