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