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 | |
Alexander Kappner | 187941e | 2018-05-18 21:50:15 -0700 | [diff] [blame] | 839 | /* UAS also needs to support FL_ALWAYS_SYNC */ |
| 840 | if (devinfo->flags & US_FL_ALWAYS_SYNC) { |
| 841 | sdev->skip_ms_page_3f = 1; |
| 842 | sdev->skip_ms_page_8 = 1; |
| 843 | sdev->wce_default_on = 1; |
| 844 | } |
Hans de Goede | 593224e | 2016-05-31 09:18:03 +0200 | [diff] [blame] | 845 | scsi_change_queue_depth(sdev, devinfo->qdepth - 2); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | static struct scsi_host_template uas_host_template = { |
| 850 | .module = THIS_MODULE, |
| 851 | .name = "uas", |
| 852 | .queuecommand = uas_queuecommand, |
Hans de Goede | 1363074 | 2016-04-12 12:27:09 +0200 | [diff] [blame] | 853 | .target_alloc = uas_target_alloc, |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 854 | .slave_alloc = uas_slave_alloc, |
| 855 | .slave_configure = uas_slave_configure, |
| 856 | .eh_abort_handler = uas_eh_abort_handler, |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 857 | .eh_bus_reset_handler = uas_eh_bus_reset_handler, |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 858 | .this_id = -1, |
| 859 | .sg_tablesize = SG_NONE, |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 860 | .skip_settle_delay = 1, |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 861 | }; |
| 862 | |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 863 | #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ |
| 864 | vendorName, productName, useProtocol, useTransport, \ |
| 865 | initFunction, flags) \ |
| 866 | { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ |
| 867 | .driver_info = (flags) } |
| 868 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 869 | static struct usb_device_id uas_usb_ids[] = { |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 870 | # include "unusual_uas.h" |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 871 | { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) }, |
| 872 | { 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] | 873 | { } |
| 874 | }; |
| 875 | MODULE_DEVICE_TABLE(usb, uas_usb_ids); |
| 876 | |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 877 | #undef UNUSUAL_DEV |
| 878 | |
Hans de Goede | e1be067 | 2013-10-21 08:00:58 +0100 | [diff] [blame] | 879 | static int uas_switch_interface(struct usb_device *udev, |
| 880 | struct usb_interface *intf) |
| 881 | { |
Alan Stern | d77606e | 2017-09-22 11:56:49 -0400 | [diff] [blame] | 882 | struct usb_host_interface *alt; |
Hans de Goede | e1be067 | 2013-10-21 08:00:58 +0100 | [diff] [blame] | 883 | |
| 884 | alt = uas_find_uas_alt_setting(intf); |
Alan Stern | d77606e | 2017-09-22 11:56:49 -0400 | [diff] [blame] | 885 | if (!alt) |
| 886 | return -ENODEV; |
Hans de Goede | e1be067 | 2013-10-21 08:00:58 +0100 | [diff] [blame] | 887 | |
Alan Stern | d77606e | 2017-09-22 11:56:49 -0400 | [diff] [blame] | 888 | return usb_set_interface(udev, alt->desc.bInterfaceNumber, |
| 889 | alt->desc.bAlternateSetting); |
Hans de Goede | e1be067 | 2013-10-21 08:00:58 +0100 | [diff] [blame] | 890 | } |
| 891 | |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 892 | static int uas_configure_endpoints(struct uas_dev_info *devinfo) |
Hans de Goede | 34f11e5 | 2013-10-29 08:54:48 +0100 | [diff] [blame] | 893 | { |
| 894 | struct usb_host_endpoint *eps[4] = { }; |
| 895 | struct usb_device *udev = devinfo->udev; |
| 896 | int r; |
| 897 | |
Hans de Goede | 34f11e5 | 2013-10-29 08:54:48 +0100 | [diff] [blame] | 898 | r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps); |
Hans de Goede | 74d71ae | 2013-10-29 10:10:36 +0100 | [diff] [blame] | 899 | if (r) |
| 900 | return r; |
Hans de Goede | 34f11e5 | 2013-10-29 08:54:48 +0100 | [diff] [blame] | 901 | |
Hans de Goede | 74d71ae | 2013-10-29 10:10:36 +0100 | [diff] [blame] | 902 | devinfo->cmd_pipe = usb_sndbulkpipe(udev, |
| 903 | usb_endpoint_num(&eps[0]->desc)); |
| 904 | devinfo->status_pipe = usb_rcvbulkpipe(udev, |
| 905 | usb_endpoint_num(&eps[1]->desc)); |
| 906 | devinfo->data_in_pipe = usb_rcvbulkpipe(udev, |
| 907 | usb_endpoint_num(&eps[2]->desc)); |
| 908 | devinfo->data_out_pipe = usb_sndbulkpipe(udev, |
| 909 | usb_endpoint_num(&eps[3]->desc)); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 910 | |
Hans de Goede | e5e5581 | 2014-09-13 12:26:43 +0200 | [diff] [blame] | 911 | if (udev->speed < USB_SPEED_SUPER) { |
Hans de Goede | e2875c3 | 2014-08-01 17:33:08 +0200 | [diff] [blame] | 912 | devinfo->qdepth = 32; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 913 | devinfo->use_streams = 0; |
| 914 | } else { |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 915 | devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, |
Hans de Goede | 5e61aed | 2014-09-13 12:26:32 +0200 | [diff] [blame] | 916 | 3, MAX_CMNDS, GFP_NOIO); |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 917 | if (devinfo->qdepth < 0) |
| 918 | return devinfo->qdepth; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 919 | devinfo->use_streams = 1; |
| 920 | } |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 921 | |
| 922 | return 0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 925 | static void uas_free_streams(struct uas_dev_info *devinfo) |
| 926 | { |
| 927 | struct usb_device *udev = devinfo->udev; |
| 928 | struct usb_host_endpoint *eps[3]; |
| 929 | |
| 930 | eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe); |
| 931 | eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe); |
| 932 | eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe); |
Oliver Neukum | 94d72f0 | 2014-03-28 11:25:50 +0100 | [diff] [blame] | 933 | usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO); |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 934 | } |
| 935 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 936 | static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 937 | { |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 938 | int result = -ENOMEM; |
| 939 | struct Scsi_Host *shost = NULL; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 940 | struct uas_dev_info *devinfo; |
| 941 | struct usb_device *udev = interface_to_usbdev(intf); |
Hans de Goede | a5011d4 | 2015-04-21 11:20:30 +0200 | [diff] [blame] | 942 | unsigned long dev_flags; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 943 | |
Hans de Goede | a5011d4 | 2015-04-21 11:20:30 +0200 | [diff] [blame] | 944 | if (!uas_use_uas_driver(intf, id, &dev_flags)) |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 945 | return -ENODEV; |
| 946 | |
Matthew Wilcox | 89dc290 | 2010-12-15 15:44:05 -0500 | [diff] [blame] | 947 | if (uas_switch_interface(udev, intf)) |
| 948 | return -ENODEV; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 949 | |
Hans de Goede | 21fc05b | 2013-11-13 09:32:22 +0100 | [diff] [blame] | 950 | shost = scsi_host_alloc(&uas_host_template, |
| 951 | sizeof(struct uas_dev_info)); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 952 | if (!shost) |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 953 | goto set_alt0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 954 | |
| 955 | shost->max_cmd_len = 16 + 252; |
| 956 | shost->max_id = 1; |
Gerd Hoffmann | bde027b | 2013-01-25 15:03:36 +0100 | [diff] [blame] | 957 | shost->max_lun = 256; |
| 958 | shost->max_channel = 0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 959 | shost->sg_tablesize = udev->bus->sg_tablesize; |
| 960 | |
Hans de Goede | 21fc05b | 2013-11-13 09:32:22 +0100 | [diff] [blame] | 961 | devinfo = (struct uas_dev_info *)shost->hostdata; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 962 | devinfo->intf = intf; |
| 963 | devinfo->udev = udev; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 964 | devinfo->resetting = 0; |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 965 | devinfo->shutdown = 0; |
Hans de Goede | a5011d4 | 2015-04-21 11:20:30 +0200 | [diff] [blame] | 966 | devinfo->flags = dev_flags; |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 967 | init_usb_anchor(&devinfo->cmd_urbs); |
Gerd Hoffmann | bdd000f | 2012-06-19 09:54:53 +0200 | [diff] [blame] | 968 | init_usb_anchor(&devinfo->sense_urbs); |
| 969 | init_usb_anchor(&devinfo->data_urbs); |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 970 | spin_lock_init(&devinfo->lock); |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 971 | INIT_WORK(&devinfo->work, uas_do_work); |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 972 | |
| 973 | result = uas_configure_endpoints(devinfo); |
| 974 | if (result) |
| 975 | goto set_alt0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 976 | |
Hans de Goede | 198de51 | 2016-04-12 12:27:08 +0200 | [diff] [blame] | 977 | /* |
| 978 | * 1 tag is reserved for untagged commands + |
| 979 | * 1 tag to avoid off by one errors in some bridge firmwares |
| 980 | */ |
| 981 | shost->can_queue = devinfo->qdepth - 2; |
| 982 | |
Oliver Neukum | c637f1f | 2014-03-28 11:29:25 +0100 | [diff] [blame] | 983 | usb_set_intfdata(intf, shost); |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 984 | result = scsi_add_host(shost, &intf->dev); |
| 985 | if (result) |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 986 | goto free_streams; |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 987 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 988 | scsi_scan_host(shost); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 989 | return result; |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 990 | |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 991 | free_streams: |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 992 | uas_free_streams(devinfo); |
Oliver Neukum | c637f1f | 2014-03-28 11:29:25 +0100 | [diff] [blame] | 993 | usb_set_intfdata(intf, NULL); |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 994 | set_alt0: |
| 995 | usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 996 | if (shost) |
| 997 | scsi_host_put(shost); |
| 998 | return result; |
| 999 | } |
| 1000 | |
Hans de Goede | f9dc024 | 2014-09-13 12:26:42 +0200 | [diff] [blame] | 1001 | static int uas_cmnd_list_empty(struct uas_dev_info *devinfo) |
| 1002 | { |
| 1003 | unsigned long flags; |
| 1004 | int i, r = 1; |
| 1005 | |
| 1006 | spin_lock_irqsave(&devinfo->lock, flags); |
| 1007 | |
| 1008 | for (i = 0; i < devinfo->qdepth; i++) { |
| 1009 | if (devinfo->cmnd[i]) { |
| 1010 | r = 0; /* Not empty */ |
| 1011 | break; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 1016 | |
| 1017 | return r; |
| 1018 | } |
| 1019 | |
| 1020 | /* |
| 1021 | * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily |
| 1022 | * get empty while there still is more work to do due to sense-urbs completing |
| 1023 | * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty. |
| 1024 | */ |
| 1025 | static int uas_wait_for_pending_cmnds(struct uas_dev_info *devinfo) |
| 1026 | { |
| 1027 | unsigned long start_time; |
| 1028 | int r; |
| 1029 | |
| 1030 | start_time = jiffies; |
| 1031 | do { |
| 1032 | flush_work(&devinfo->work); |
| 1033 | |
| 1034 | r = usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000); |
| 1035 | if (r == 0) |
| 1036 | return -ETIME; |
| 1037 | |
| 1038 | r = usb_wait_anchor_empty_timeout(&devinfo->data_urbs, 500); |
| 1039 | if (r == 0) |
| 1040 | return -ETIME; |
| 1041 | |
| 1042 | if (time_after(jiffies, start_time + 5 * HZ)) |
| 1043 | return -ETIME; |
| 1044 | } while (!uas_cmnd_list_empty(devinfo)); |
| 1045 | |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1049 | static int uas_pre_reset(struct usb_interface *intf) |
| 1050 | { |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1051 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
Hans de Goede | 21fc05b | 2013-11-13 09:32:22 +0100 | [diff] [blame] | 1052 | struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1053 | unsigned long flags; |
| 1054 | |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1055 | if (devinfo->shutdown) |
| 1056 | return 0; |
| 1057 | |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1058 | /* Block new requests */ |
| 1059 | spin_lock_irqsave(shost->host_lock, flags); |
| 1060 | scsi_block_requests(shost); |
| 1061 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1062 | |
Hans de Goede | f9dc024 | 2014-09-13 12:26:42 +0200 | [diff] [blame] | 1063 | if (uas_wait_for_pending_cmnds(devinfo) != 0) { |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1064 | shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__); |
Hans de Goede | f9dc024 | 2014-09-13 12:26:42 +0200 | [diff] [blame] | 1065 | scsi_unblock_requests(shost); |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1066 | return 1; |
| 1067 | } |
| 1068 | |
| 1069 | uas_free_streams(devinfo); |
| 1070 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | static int uas_post_reset(struct usb_interface *intf) |
| 1075 | { |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1076 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
Hans de Goede | 21fc05b | 2013-11-13 09:32:22 +0100 | [diff] [blame] | 1077 | struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1078 | unsigned long flags; |
Hans de Goede | ce39fe6 | 2014-09-13 12:26:50 +0200 | [diff] [blame] | 1079 | int err; |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1080 | |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1081 | if (devinfo->shutdown) |
| 1082 | return 0; |
| 1083 | |
Hans de Goede | ce39fe6 | 2014-09-13 12:26:50 +0200 | [diff] [blame] | 1084 | err = uas_configure_endpoints(devinfo); |
Oliver Neukum | 438fd54 | 2018-03-06 15:04:24 +0100 | [diff] [blame] | 1085 | if (err && err != -ENODEV) |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 1086 | shost_printk(KERN_ERR, shost, |
Hans de Goede | ce39fe6 | 2014-09-13 12:26:50 +0200 | [diff] [blame] | 1087 | "%s: alloc streams error %d after reset", |
| 1088 | __func__, err); |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1089 | |
Oliver Neukum | 92e64a1 | 2018-01-11 13:10:16 +0100 | [diff] [blame] | 1090 | /* we must unblock the host in every case lest we deadlock */ |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1091 | spin_lock_irqsave(shost->host_lock, flags); |
| 1092 | scsi_report_bus_reset(shost, 0); |
| 1093 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1094 | |
| 1095 | scsi_unblock_requests(shost); |
| 1096 | |
Oliver Neukum | 92e64a1 | 2018-01-11 13:10:16 +0100 | [diff] [blame] | 1097 | return err ? 1 : 0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1098 | } |
| 1099 | |
Hans de Goede | 0df1f66 | 2013-11-07 08:47:05 +0100 | [diff] [blame] | 1100 | static int uas_suspend(struct usb_interface *intf, pm_message_t message) |
| 1101 | { |
| 1102 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
Hans de Goede | 21fc05b | 2013-11-13 09:32:22 +0100 | [diff] [blame] | 1103 | struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; |
Hans de Goede | 0df1f66 | 2013-11-07 08:47:05 +0100 | [diff] [blame] | 1104 | |
Hans de Goede | f9dc024 | 2014-09-13 12:26:42 +0200 | [diff] [blame] | 1105 | if (uas_wait_for_pending_cmnds(devinfo) != 0) { |
Hans de Goede | 0df1f66 | 2013-11-07 08:47:05 +0100 | [diff] [blame] | 1106 | shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__); |
| 1107 | return -ETIME; |
| 1108 | } |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | static int uas_resume(struct usb_interface *intf) |
| 1114 | { |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | static int uas_reset_resume(struct usb_interface *intf) |
| 1119 | { |
| 1120 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
Hans de Goede | 21fc05b | 2013-11-13 09:32:22 +0100 | [diff] [blame] | 1121 | struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; |
Hans de Goede | 0df1f66 | 2013-11-07 08:47:05 +0100 | [diff] [blame] | 1122 | unsigned long flags; |
Hans de Goede | ce39fe6 | 2014-09-13 12:26:50 +0200 | [diff] [blame] | 1123 | int err; |
Hans de Goede | 0df1f66 | 2013-11-07 08:47:05 +0100 | [diff] [blame] | 1124 | |
Hans de Goede | ce39fe6 | 2014-09-13 12:26:50 +0200 | [diff] [blame] | 1125 | err = uas_configure_endpoints(devinfo); |
| 1126 | if (err) { |
Hans de Goede | 0df1f66 | 2013-11-07 08:47:05 +0100 | [diff] [blame] | 1127 | shost_printk(KERN_ERR, shost, |
Hans de Goede | ce39fe6 | 2014-09-13 12:26:50 +0200 | [diff] [blame] | 1128 | "%s: alloc streams error %d after reset", |
| 1129 | __func__, err); |
Hans de Goede | 0df1f66 | 2013-11-07 08:47:05 +0100 | [diff] [blame] | 1130 | return -EIO; |
| 1131 | } |
| 1132 | |
| 1133 | spin_lock_irqsave(shost->host_lock, flags); |
| 1134 | scsi_report_bus_reset(shost, 0); |
| 1135 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1136 | |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1140 | static void uas_disconnect(struct usb_interface *intf) |
| 1141 | { |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1142 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
Hans de Goede | 21fc05b | 2013-11-13 09:32:22 +0100 | [diff] [blame] | 1143 | struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; |
Hans de Goede | b7b5d11 | 2014-09-13 12:26:30 +0200 | [diff] [blame] | 1144 | unsigned long flags; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1145 | |
Hans de Goede | b7b5d11 | 2014-09-13 12:26:30 +0200 | [diff] [blame] | 1146 | spin_lock_irqsave(&devinfo->lock, flags); |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 1147 | devinfo->resetting = 1; |
Hans de Goede | b7b5d11 | 2014-09-13 12:26:30 +0200 | [diff] [blame] | 1148 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 1149 | |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 1150 | cancel_work_sync(&devinfo->work); |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 1151 | usb_kill_anchored_urbs(&devinfo->cmd_urbs); |
Gerd Hoffmann | bdd000f | 2012-06-19 09:54:53 +0200 | [diff] [blame] | 1152 | usb_kill_anchored_urbs(&devinfo->sense_urbs); |
| 1153 | usb_kill_anchored_urbs(&devinfo->data_urbs); |
Hans de Goede | 1589349 | 2014-09-13 12:26:36 +0200 | [diff] [blame] | 1154 | uas_zap_pending(devinfo, DID_NO_CONNECT); |
| 1155 | |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 1156 | scsi_remove_host(shost); |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1157 | uas_free_streams(devinfo); |
Hans de Goede | 21fc05b | 2013-11-13 09:32:22 +0100 | [diff] [blame] | 1158 | scsi_host_put(shost); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1159 | } |
| 1160 | |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1161 | /* |
| 1162 | * Put the device back in usb-storage mode on shutdown, as some BIOS-es |
| 1163 | * hang on reboot when the device is still in uas mode. Note the reset is |
| 1164 | * necessary as some devices won't revert to usb-storage mode without it. |
| 1165 | */ |
| 1166 | static void uas_shutdown(struct device *dev) |
| 1167 | { |
| 1168 | struct usb_interface *intf = to_usb_interface(dev); |
| 1169 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1170 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
Hans de Goede | 21fc05b | 2013-11-13 09:32:22 +0100 | [diff] [blame] | 1171 | struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata; |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1172 | |
| 1173 | if (system_state != SYSTEM_RESTART) |
| 1174 | return; |
| 1175 | |
| 1176 | devinfo->shutdown = 1; |
| 1177 | uas_free_streams(devinfo); |
| 1178 | usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0); |
| 1179 | usb_reset_device(udev); |
| 1180 | } |
| 1181 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1182 | static struct usb_driver uas_driver = { |
| 1183 | .name = "uas", |
| 1184 | .probe = uas_probe, |
| 1185 | .disconnect = uas_disconnect, |
| 1186 | .pre_reset = uas_pre_reset, |
| 1187 | .post_reset = uas_post_reset, |
Hans de Goede | 0df1f66 | 2013-11-07 08:47:05 +0100 | [diff] [blame] | 1188 | .suspend = uas_suspend, |
| 1189 | .resume = uas_resume, |
| 1190 | .reset_resume = uas_reset_resume, |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1191 | .drvwrap.driver.shutdown = uas_shutdown, |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1192 | .id_table = uas_usb_ids, |
| 1193 | }; |
| 1194 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 1195 | module_usb_driver(uas_driver); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1196 | |
| 1197 | MODULE_LICENSE("GPL"); |
Hans de Goede | f50a496 | 2013-10-28 10:48:04 +0000 | [diff] [blame] | 1198 | MODULE_AUTHOR( |
| 1199 | "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp"); |