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