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