blob: 004ebc12bc21faab4d36a0ac32feb848a65cce51 [file] [log] [blame]
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001/*
2 * USB Attached SCSI
3 * Note that this is not the same as the USB Mass Storage driver
4 *
Hans de Goede5df2be62014-09-13 12:26:29 +02005 * Copyright Hans de Goede <hdegoede@redhat.com> for Red Hat, Inc. 2013 - 2014
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02006 * 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 Gortmaker6eb0de82011-07-03 16:09:31 -040015#include <linux/module.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020016#include <linux/usb.h>
Hans de Goede79b4c062013-10-25 17:04:33 +010017#include <linux/usb_usual.h>
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +010018#include <linux/usb/hcd.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020019#include <linux/usb/storage.h>
Sebastian Andrzej Siewior348748b2012-01-11 12:45:56 +010020#include <linux/usb/uas.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020021
22#include <scsi/scsi.h>
Hans de Goede4de7a3732013-10-22 16:10:44 +010023#include <scsi/scsi_eh.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020024#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 Goede82aa0382013-10-21 08:53:31 +010030#include "uas-detect.h"
Hans de Goede59307852014-09-15 16:04:12 +020031#include "scsiglue.h"
Hans de Goede82aa0382013-10-21 08:53:31 +010032
Hans de Goede5e61aed2014-09-13 12:26:32 +020033#define MAX_CMNDS 256
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020034
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020035struct uas_dev_info {
36 struct usb_interface *intf;
37 struct usb_device *udev;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +020038 struct usb_anchor cmd_urbs;
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +020039 struct usb_anchor sense_urbs;
40 struct usb_anchor data_urbs;
Hans de Goede59307852014-09-15 16:04:12 +020041 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +020042 int qdepth, resetting;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020043 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
44 unsigned use_streams:1;
Hans de Goededa65c2b2013-11-11 11:51:42 +010045 unsigned shutdown:1;
Hans de Goede5e61aed2014-09-13 12:26:32 +020046 struct scsi_cmnd *cmnd[MAX_CMNDS];
Gerd Hoffmanne0648522012-09-25 10:47:08 +020047 spinlock_t lock;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +020048 struct work_struct work;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020049};
50
51enum {
Matthew Wilcox92a3f762010-12-15 15:44:04 -050052 SUBMIT_STATUS_URB = (1 << 1),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020053 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 Hoffmannb1d67692012-06-19 09:54:51 +020059 COMMAND_INFLIGHT = (1 << 8),
60 DATA_IN_URB_INFLIGHT = (1 << 9),
61 DATA_OUT_URB_INFLIGHT = (1 << 10),
Hans de Goedeeb7d6642014-09-13 12:26:46 +020062 COMMAND_ABORTED = (1 << 11),
63 IS_IN_WORK_LIST = (1 << 12),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020064};
65
66/* Overrides scsi_pointer */
67struct uas_cmd_info {
68 unsigned int state;
Hans de Goedee28e2f2f2014-10-12 12:19:40 +020069 unsigned int uas_tag;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020070 struct urb *cmd_urb;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020071 struct urb *data_in_urb;
72 struct urb *data_out_urb;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020073};
74
75/* I hate forward declarations, but I actually have a loop */
76static int uas_submit_urbs(struct scsi_cmnd *cmnd,
77 struct uas_dev_info *devinfo, gfp_t gfp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -080078static void uas_do_work(struct work_struct *work);
Gerd Hoffmann4c456972012-11-30 11:54:44 +010079static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
Gerd Hoffmannd89bd832013-09-13 13:27:11 +020080static void uas_free_streams(struct uas_dev_info *devinfo);
Hans de Goede1ad7ed52014-09-13 12:26:49 +020081static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
82 int status);
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +010083
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020084static void uas_do_work(struct work_struct *work)
85{
Gerd Hoffmann1bf81982013-09-13 13:27:12 +020086 struct uas_dev_info *devinfo =
87 container_of(work, struct uas_dev_info, work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020088 struct uas_cmd_info *cmdinfo;
Hans de Goede43cd99c2014-09-13 12:26:38 +020089 struct scsi_cmnd *cmnd;
Gerd Hoffmanne0648522012-09-25 10:47:08 +020090 unsigned long flags;
Hans de Goede43cd99c2014-09-13 12:26:38 +020091 int i, err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020092
Gerd Hoffmann1bf81982013-09-13 13:27:12 +020093 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goedeb7b5d112014-09-13 12:26:30 +020094
95 if (devinfo->resetting)
96 goto out;
97
Hans de Goede43cd99c2014-09-13 12:26:38 +020098 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 Goede61c09ce2013-11-12 13:44:20 +0100104
105 if (!(cmdinfo->state & IS_IN_WORK_LIST))
106 continue;
107
Oliver Neukume7eda932014-03-28 11:10:30 +0100108 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
Hans de Goede61c09ce2013-11-12 13:44:20 +0100109 if (!err)
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100110 cmdinfo->state &= ~IS_IN_WORK_LIST;
Hans de Goede61c09ce2013-11-12 13:44:20 +0100111 else
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200112 schedule_work(&devinfo->work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200113 }
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200114out:
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100115 spin_unlock_irqrestore(&devinfo->lock, flags);
116}
117
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200118static 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 Sharmaab945ef2014-08-12 12:10:21 +0530124 lockdep_assert_held(&devinfo->lock);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200125 cmdinfo->state |= IS_IN_WORK_LIST;
126 schedule_work(&devinfo->work);
127}
128
Hans de Goede15893492014-09-13 12:26:36 +0200129static void uas_zap_pending(struct uas_dev_info *devinfo, int result)
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200130{
131 struct uas_cmd_info *cmdinfo;
Hans de Goede43cd99c2014-09-13 12:26:38 +0200132 struct scsi_cmnd *cmnd;
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200133 unsigned long flags;
Hans de Goede43cd99c2014-09-13 12:26:38 +0200134 int i, err;
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200135
136 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goede43cd99c2014-09-13 12:26:38 +0200137 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 Goede1ad7ed52014-09-13 12:26:49 +0200143 uas_log_cmd_state(cmnd, __func__, 0);
Hans de Goede9c15c572014-09-13 12:26:37 +0200144 /* Sense urbs were killed, clear COMMAND_INFLIGHT manually */
145 cmdinfo->state &= ~COMMAND_INFLIGHT;
Hans de Goede15893492014-09-13 12:26:36 +0200146 cmnd->result = result << 16;
Hans de Goede9c15c572014-09-13 12:26:37 +0200147 err = uas_try_complete(cmnd, __func__);
148 WARN_ON(err != 0);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200149 }
150 spin_unlock_irqrestore(&devinfo->lock, flags);
151}
152
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200153static 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 Wilcox115bb1f2010-10-07 13:05:23 +0200174}
175
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200176static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
177 int status)
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200178{
179 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200180 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200181
Hans de Goede60d9f672014-09-13 12:26:34 +0200182 scmd_printk(KERN_INFO, cmnd,
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200183 "%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 Hoffmannb1d67692012-06-19 09:54:51 +0200185 (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 Hoffmannb06e48a2012-11-30 11:54:41 +0100195 (ci->state & COMMAND_ABORTED) ? " abort" : "",
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100196 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
Hans de Goede6dcd8ec2014-09-13 12:26:45 +0200197 scsi_print_command(cmnd);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200198}
199
Hans de Goede4c5481e2014-09-13 12:26:41 +0200200static 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 Hoffmannb1d67692012-06-19 09:54:51 +0200217}
218
219static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
220{
221 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200222 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200223
Sanjeev Sharmaab945ef2014-08-12 12:10:21 +0530224 lockdep_assert_held(&devinfo->lock);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200225 if (cmdinfo->state & (COMMAND_INFLIGHT |
226 DATA_IN_URB_INFLIGHT |
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100227 DATA_OUT_URB_INFLIGHT |
Hans de Goede616f0e62014-09-13 12:26:40 +0200228 COMMAND_ABORTED))
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200229 return -EBUSY;
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200230 devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
Hans de Goede4c5481e2014-09-13 12:26:41 +0200231 uas_free_unsubmitted_urbs(cmnd);
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200232 cmnd->scsi_done(cmnd);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200233 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200234}
235
236static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200237 unsigned direction)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200238{
239 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
240 int err;
241
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200242 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200243 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
244 if (err) {
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200245 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200246 }
247}
248
249static void uas_stat_cmplt(struct urb *urb)
250{
251 struct iu *iu = urb->transfer_buffer;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100252 struct Scsi_Host *shost = urb->context;
Hans de Goede21fc05b2013-11-13 09:32:22 +0100253 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede60d9f672014-09-13 12:26:34 +0200254 struct urb *data_in_urb = NULL;
255 struct urb *data_out_urb = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200256 struct scsi_cmnd *cmnd;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200257 struct uas_cmd_info *cmdinfo;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200258 unsigned long flags;
Hans de Goede5e61aed2014-09-13 12:26:32 +0200259 unsigned int idx;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200260
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200261 spin_lock_irqsave(&devinfo->lock, flags);
262
263 if (devinfo->resetting)
264 goto out;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200265
266 if (urb->status) {
Hans de Goede51b36172014-09-13 12:26:44 +0200267 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200268 dev_err(&urb->dev->dev, "stat urb: status %d\n",
269 urb->status);
270 }
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200271 goto out;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200272 }
273
Hans de Goede5e61aed2014-09-13 12:26:32 +0200274 idx = be16_to_cpup(&iu->tag) - 1;
275 if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
276 dev_err(&urb->dev->dev,
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200277 "stat urb: no pending cmd for uas-tag %d\n", idx + 1);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200278 goto out;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200279 }
280
Hans de Goede5e61aed2014-09-13 12:26:32 +0200281 cmnd = devinfo->cmnd[idx];
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200282 cmdinfo = (void *)&cmnd->SCp;
Hans de Goeded89da032014-09-13 12:26:33 +0200283
284 if (!(cmdinfo->state & COMMAND_INFLIGHT)) {
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200285 uas_log_cmd_state(cmnd, "unexpected status cmplt", 0);
Hans de Goeded89da032014-09-13 12:26:33 +0200286 goto out;
287 }
288
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200289 switch (iu->iu_id) {
290 case IU_ID_STATUS:
Hans de Goede5ad22cf2014-09-13 12:26:47 +0200291 uas_sense(urb, cmnd);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200292 if (cmnd->result != 0) {
293 /* cancel data transfers on error */
Hans de Goede60d9f672014-09-13 12:26:34 +0200294 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
295 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200296 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200297 cmdinfo->state &= ~COMMAND_INFLIGHT;
298 uas_try_complete(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200299 break;
300 case IU_ID_READ_READY:
Hans de Goede8e453152013-11-15 10:04:11 +0100301 if (!cmdinfo->data_in_urb ||
302 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200303 uas_log_cmd_state(cmnd, "unexpected read rdy", 0);
Hans de Goede8e453152013-11-15 10:04:11 +0100304 break;
305 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200306 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
307 break;
308 case IU_ID_WRITE_READY:
Hans de Goede8e453152013-11-15 10:04:11 +0100309 if (!cmdinfo->data_out_urb ||
310 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200311 uas_log_cmd_state(cmnd, "unexpected write rdy", 0);
Hans de Goede8e453152013-11-15 10:04:11 +0100312 break;
313 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200314 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
315 break;
Hans de Goedefac1f482014-09-13 12:26:51 +0200316 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 Wilcox115bb1f2010-10-07 13:05:23 +0200326 default:
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200327 uas_log_cmd_state(cmnd, "bogus IU", iu->iu_id);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200328 }
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200329out:
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200330 usb_free_urb(urb);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200331 spin_unlock_irqrestore(&devinfo->lock, flags);
Hans de Goede60d9f672014-09-13 12:26:34 +0200332
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 Wilcox115bb1f2010-10-07 13:05:23 +0200342}
343
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200344static void uas_data_cmplt(struct urb *urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200345{
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200346 struct scsi_cmnd *cmnd = urb->context;
347 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200348 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200349 struct scsi_data_buffer *sdb = NULL;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200350 unsigned long flags;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200351
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200352 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200353
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200354 if (cmdinfo->data_in_urb == urb) {
355 sdb = scsi_in(cmnd);
356 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
Hans de Goede85fea822014-09-13 12:26:35 +0200357 cmdinfo->data_in_urb = NULL;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200358 } else if (cmdinfo->data_out_urb == urb) {
359 sdb = scsi_out(cmnd);
360 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
Hans de Goede85fea822014-09-13 12:26:35 +0200361 cmdinfo->data_out_urb = NULL;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200362 }
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200363 if (sdb == NULL) {
364 WARN_ON_ONCE(1);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200365 goto out;
366 }
367
368 if (devinfo->resetting)
369 goto out;
370
Hans de Goeded89da032014-09-13 12:26:33 +0200371 /* Data urbs should not complete before the cmd urb is submitted */
372 if (cmdinfo->state & SUBMIT_CMD_URB) {
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200373 uas_log_cmd_state(cmnd, "unexpected data cmplt", 0);
Hans de Goeded89da032014-09-13 12:26:33 +0200374 goto out;
375 }
376
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200377 if (urb->status) {
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200378 if (urb->status != -ENOENT && urb->status != -ECONNRESET)
379 uas_log_cmd_state(cmnd, "data cmplt err", urb->status);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200380 /* error: no data transfered */
381 sdb->resid = sdb->length;
382 } else {
383 sdb->resid = sdb->length - urb->actual_length;
384 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200385 uas_try_complete(cmnd, __func__);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200386out:
Hans de Goede85fea822014-09-13 12:26:35 +0200387 usb_free_urb(urb);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200388 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200389}
390
Hans de Goede876285c2013-11-07 08:52:42 +0100391static void uas_cmd_cmplt(struct urb *urb)
392{
Hans de Goedeb6823c52014-09-13 12:26:39 +0200393 if (urb->status)
394 dev_err(&urb->dev->dev, "cmd cmplt err %d\n", urb->status);
Hans de Goede876285c2013-11-07 08:52:42 +0100395
Hans de Goede876285c2013-11-07 08:52:42 +0100396 usb_free_urb(urb);
397}
398
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200399static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200400 struct scsi_cmnd *cmnd,
401 enum dma_data_direction dir)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200402{
403 struct usb_device *udev = devinfo->udev;
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200404 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200405 struct urb *urb = usb_alloc_urb(0, gfp);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200406 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
407 ? scsi_in(cmnd) : scsi_out(cmnd);
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200408 unsigned int pipe = (dir == DMA_FROM_DEVICE)
409 ? devinfo->data_in_pipe : devinfo->data_out_pipe;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200410
411 if (!urb)
412 goto out;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200413 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
414 uas_data_cmplt, cmnd);
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200415 if (devinfo->use_streams)
416 urb->stream_id = cmdinfo->uas_tag;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200417 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
418 urb->sg = sdb->table.sgl;
419 out:
420 return urb;
421}
422
423static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200424 struct scsi_cmnd *cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200425{
426 struct usb_device *udev = devinfo->udev;
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200427 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200428 struct urb *urb = usb_alloc_urb(0, gfp);
429 struct sense_iu *iu;
430
431 if (!urb)
432 goto out;
433
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500434 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200435 if (!iu)
436 goto free;
437
438 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200439 uas_stat_cmplt, cmnd->device->host);
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200440 if (devinfo->use_streams)
441 urb->stream_id = cmdinfo->uas_tag;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200442 urb->transfer_flags |= URB_FREE_BUFFER;
443 out:
444 return urb;
445 free:
446 usb_free_urb(urb);
447 return NULL;
448}
449
450static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Hans de Goedea887cd32013-10-17 19:47:28 +0200451 struct scsi_cmnd *cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200452{
453 struct usb_device *udev = devinfo->udev;
454 struct scsi_device *sdev = cmnd->device;
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200455 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200456 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 Wilcoxac563cf2010-12-15 15:44:03 -0500467 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200468 if (!iu)
469 goto free;
470
471 iu->iu_id = IU_ID_COMMAND;
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200472 iu->tag = cpu_to_be16(cmdinfo->uas_tag);
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100473 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200474 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 Goedeb6823c52014-09-13 12:26:39 +0200479 uas_cmd_cmplt, NULL);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200480 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 Goede2d75b9c2014-10-03 12:08:56 +0200494static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd, gfp_t gfp)
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200495{
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200496 struct uas_dev_info *devinfo = cmnd->device->hostdata;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200497 struct urb *urb;
Hans de Goede876285c2013-11-07 08:52:42 +0100498 int err;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200499
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200500 urb = uas_alloc_sense_urb(devinfo, gfp, cmnd);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200501 if (!urb)
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100502 return NULL;
Hans de Goeded5f808d2013-10-17 19:30:26 +0200503 usb_anchor_urb(urb, &devinfo->sense_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100504 err = usb_submit_urb(urb, gfp);
505 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200506 usb_unanchor_urb(urb);
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200507 uas_log_cmd_state(cmnd, "sense submit err", err);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200508 usb_free_urb(urb);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100509 return NULL;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200510 }
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100511 return urb;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200512}
513
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200514static int uas_submit_urbs(struct scsi_cmnd *cmnd,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200515 struct uas_dev_info *devinfo, gfp_t gfp)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200516{
517 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100518 struct urb *urb;
Hans de Goede876285c2013-11-07 08:52:42 +0100519 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200520
Sanjeev Sharmaab945ef2014-08-12 12:10:21 +0530521 lockdep_assert_held(&devinfo->lock);
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500522 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200523 urb = uas_submit_sense_urb(cmnd, gfp);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100524 if (!urb)
525 return SCSI_MLQUEUE_DEVICE_BUSY;
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500526 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200527 }
528
529 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
530 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200531 cmnd, DMA_FROM_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200532 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 Goeded5f808d2013-10-17 19:30:26 +0200538 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100539 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
540 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200541 usb_unanchor_urb(cmdinfo->data_in_urb);
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200542 uas_log_cmd_state(cmnd, "data in submit err", err);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200543 return SCSI_MLQUEUE_DEVICE_BUSY;
544 }
545 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200546 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200547 }
548
549 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
550 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
Hans de Goede2d75b9c2014-10-03 12:08:56 +0200551 cmnd, DMA_TO_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200552 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 Goeded5f808d2013-10-17 19:30:26 +0200558 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100559 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
560 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200561 usb_unanchor_urb(cmdinfo->data_out_urb);
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200562 uas_log_cmd_state(cmnd, "data out submit err", err);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200563 return SCSI_MLQUEUE_DEVICE_BUSY;
564 }
565 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200566 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200567 }
568
569 if (cmdinfo->state & ALLOC_CMD_URB) {
Hans de Goedea887cd32013-10-17 19:47:28 +0200570 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200571 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 Goeded5f808d2013-10-17 19:30:26 +0200577 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100578 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
579 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200580 usb_unanchor_urb(cmdinfo->cmd_urb);
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200581 uas_log_cmd_state(cmnd, "cmd submit err", err);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200582 return SCSI_MLQUEUE_DEVICE_BUSY;
583 }
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200584 cmdinfo->cmd_urb = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200585 cmdinfo->state &= ~SUBMIT_CMD_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200586 cmdinfo->state |= COMMAND_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200587 }
588
589 return 0;
590}
591
Jeff Garzikf2812332010-11-16 02:10:29 -0500592static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200593 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 Hoffmanne0648522012-09-25 10:47:08 +0200598 unsigned long flags;
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200599 int idx, err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200600
601 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
602
Hans de Goedef9dc0242014-09-13 12:26:42 +0200603 /* 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 Goede59307852014-09-15 16:04:12 +0200607 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 Goedec6f63202013-11-13 09:24:15 +0100616 spin_lock_irqsave(&devinfo->lock, flags);
617
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100618 if (devinfo->resetting) {
619 cmnd->result = DID_ERROR << 16;
620 cmnd->scsi_done(cmnd);
Hans de Goedec6f63202013-11-13 09:24:15 +0100621 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100622 return 0;
623 }
624
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200625 /* 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 Hoffmanne0648522012-09-25 10:47:08 +0200631 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200632 return SCSI_MLQUEUE_DEVICE_BUSY;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200633 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200634
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200635 cmnd->scsi_done = done;
636
Hans de Goede5e61aed2014-09-13 12:26:32 +0200637 memset(cmdinfo, 0, sizeof(*cmdinfo));
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200638 cmdinfo->uas_tag = idx + 1; /* uas-tag == usb-stream-id, so 1 based */
Hans de Goedee0620002014-09-13 12:26:31 +0200639 cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200640
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 Goedee28e2f2f2014-10-12 12:19:40 +0200653 if (!devinfo->use_streams)
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200654 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200655
656 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
657 if (err) {
658 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500659 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200660 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200661 return SCSI_MLQUEUE_DEVICE_BUSY;
662 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200663 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200664 }
665
Hans de Goedee28e2f2f2014-10-12 12:19:40 +0200666 devinfo->cmnd[idx] = cmnd;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200667 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200668 return 0;
669}
670
Jeff Garzikf2812332010-11-16 02:10:29 -0500671static DEF_SCSI_QCMD(uas_queuecommand)
672
Hans de Goede616f0e62014-09-13 12:26:40 +0200673/*
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 Wilcox115bb1f2010-10-07 13:05:23 +0200678static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
679{
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200680 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200681 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Hans de Goede616f0e62014-09-13 12:26:40 +0200682 struct urb *data_in_urb = NULL;
683 struct urb *data_out_urb = NULL;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200684 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200685
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200686 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goedec6f63202013-11-13 09:24:15 +0100687
Hans de Goede1ad7ed52014-09-13 12:26:49 +0200688 uas_log_cmd_state(cmnd, __func__, 0);
Hans de Goede616f0e62014-09-13 12:26:40 +0200689
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 Goedee28e2f2f2014-10-12 12:19:40 +0200694 devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
Hans de Goede616f0e62014-09-13 12:26:40 +0200695 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 Goede4c5481e2014-09-13 12:26:41 +0200700 uas_free_unsubmitted_urbs(cmnd);
701
Hans de Goede616f0e62014-09-13 12:26:40 +0200702 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 Goedec6f63202013-11-13 09:24:15 +0100711 }
712
Hans de Goede616f0e62014-09-13 12:26:40 +0200713 return FAILED;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200714}
715
716static 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 Goedeb7b5d112014-09-13 12:26:30 +0200721 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200722 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200723
Hans de Goedebe326f42013-09-22 16:27:02 +0200724 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 Hoffmann326349f2013-09-13 13:27:13 +0200731 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200732
733 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200734 devinfo->resetting = 1;
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200735 spin_unlock_irqrestore(&devinfo->lock, flags);
736
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200737 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200738 usb_kill_anchored_urbs(&devinfo->sense_urbs);
739 usb_kill_anchored_urbs(&devinfo->data_urbs);
Hans de Goede15893492014-09-13 12:26:36 +0200740 uas_zap_pending(devinfo, DID_RESET);
741
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200742 err = usb_reset_device(udev);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200743
744 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200745 devinfo->resetting = 0;
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200746 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200747
Hans de Goedebe326f42013-09-22 16:27:02 +0200748 usb_unlock_device(udev);
749
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200750 if (err) {
Hans de Goedece39fe62014-09-13 12:26:50 +0200751 shost_printk(KERN_INFO, sdev->host, "%s FAILED err %d\n",
752 __func__, err);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200753 return FAILED;
754 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200755
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200756 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
757 return SUCCESS;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200758}
759
760static int uas_slave_alloc(struct scsi_device *sdev)
761{
Hans de Goede21fc05b2013-11-13 09:32:22 +0100762 sdev->hostdata = (void *)sdev->host->hostdata;
Hans de Goede37599f92013-11-15 10:04:31 +0100763
764 /* USB has unusual DMA-alignment requirements: Although the
765 * starting address of each scatter-gather element doesn't matter,
766 * the length of each element except the last must be divisible
767 * by the Bulk maxpacket value. There's currently no way to
768 * express this by block-layer constraints, so we'll cop out
769 * and simply require addresses to be aligned at 512-byte
770 * boundaries. This is okay since most block I/O involves
771 * hardware sectors that are multiples of 512 bytes in length,
772 * and since host controllers up through USB 2.0 have maxpacket
773 * values no larger than 512.
774 *
775 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
776 * values can be as large as 2048. To make that work properly
777 * will require changes to the block layer.
778 */
779 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
780
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200781 return 0;
782}
783
784static int uas_slave_configure(struct scsi_device *sdev)
785{
786 struct uas_dev_info *devinfo = sdev->hostdata;
Hans de Goede734016b2014-09-16 18:36:52 +0200787
788 if (devinfo->flags & US_FL_NO_REPORT_OPCODES)
789 sdev->no_report_opcodes = 1;
790
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200791 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
Hans de Goeded3f7c152013-10-23 17:46:17 +0100792 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200793 return 0;
794}
795
796static struct scsi_host_template uas_host_template = {
797 .module = THIS_MODULE,
798 .name = "uas",
799 .queuecommand = uas_queuecommand,
800 .slave_alloc = uas_slave_alloc,
801 .slave_configure = uas_slave_configure,
802 .eh_abort_handler = uas_eh_abort_handler,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200803 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
804 .can_queue = 65536, /* Is there a limit on the _host_ ? */
805 .this_id = -1,
806 .sg_tablesize = SG_NONE,
807 .cmd_per_lun = 1, /* until we override it */
808 .skip_settle_delay = 1,
809 .ordered_tag = 1,
810};
811
Hans de Goede79b4c062013-10-25 17:04:33 +0100812#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
813 vendorName, productName, useProtocol, useTransport, \
814 initFunction, flags) \
815{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
816 .driver_info = (flags) }
817
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200818static struct usb_device_id uas_usb_ids[] = {
Hans de Goede79b4c062013-10-25 17:04:33 +0100819# include "unusual_uas.h"
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200820 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
821 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200822 { }
823};
824MODULE_DEVICE_TABLE(usb, uas_usb_ids);
825
Hans de Goede79b4c062013-10-25 17:04:33 +0100826#undef UNUSUAL_DEV
827
Hans de Goedee1be0672013-10-21 08:00:58 +0100828static int uas_switch_interface(struct usb_device *udev,
829 struct usb_interface *intf)
830{
831 int alt;
832
833 alt = uas_find_uas_alt_setting(intf);
834 if (alt < 0)
835 return alt;
836
837 return usb_set_interface(udev,
838 intf->altsetting[0].desc.bInterfaceNumber, alt);
839}
840
Hans de Goede58d51442013-10-29 10:23:26 +0100841static int uas_configure_endpoints(struct uas_dev_info *devinfo)
Hans de Goede34f11e52013-10-29 08:54:48 +0100842{
843 struct usb_host_endpoint *eps[4] = { };
844 struct usb_device *udev = devinfo->udev;
845 int r;
846
Hans de Goede34f11e52013-10-29 08:54:48 +0100847 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
Hans de Goede74d71ae2013-10-29 10:10:36 +0100848 if (r)
849 return r;
Hans de Goede34f11e52013-10-29 08:54:48 +0100850
Hans de Goede74d71ae2013-10-29 10:10:36 +0100851 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
852 usb_endpoint_num(&eps[0]->desc));
853 devinfo->status_pipe = usb_rcvbulkpipe(udev,
854 usb_endpoint_num(&eps[1]->desc));
855 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
856 usb_endpoint_num(&eps[2]->desc));
857 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
858 usb_endpoint_num(&eps[3]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200859
Hans de Goedee5e55812014-09-13 12:26:43 +0200860 if (udev->speed < USB_SPEED_SUPER) {
Hans de Goedee2875c32014-08-01 17:33:08 +0200861 devinfo->qdepth = 32;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200862 devinfo->use_streams = 0;
863 } else {
Hans de Goede58d51442013-10-29 10:23:26 +0100864 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
Hans de Goede5e61aed2014-09-13 12:26:32 +0200865 3, MAX_CMNDS, GFP_NOIO);
Hans de Goede58d51442013-10-29 10:23:26 +0100866 if (devinfo->qdepth < 0)
867 return devinfo->qdepth;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200868 devinfo->use_streams = 1;
869 }
Hans de Goede58d51442013-10-29 10:23:26 +0100870
871 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200872}
873
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100874static void uas_free_streams(struct uas_dev_info *devinfo)
875{
876 struct usb_device *udev = devinfo->udev;
877 struct usb_host_endpoint *eps[3];
878
879 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
880 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
881 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
Oliver Neukum94d72f02014-03-28 11:25:50 +0100882 usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100883}
884
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200885static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
886{
Hans de Goede6ce82132013-10-17 19:00:45 +0200887 int result = -ENOMEM;
888 struct Scsi_Host *shost = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200889 struct uas_dev_info *devinfo;
890 struct usb_device *udev = interface_to_usbdev(intf);
891
Hans de Goede79b4c062013-10-25 17:04:33 +0100892 if (!uas_use_uas_driver(intf, id))
893 return -ENODEV;
894
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500895 if (uas_switch_interface(udev, intf))
896 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200897
Hans de Goede21fc05b2013-11-13 09:32:22 +0100898 shost = scsi_host_alloc(&uas_host_template,
899 sizeof(struct uas_dev_info));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200900 if (!shost)
Hans de Goede6ce82132013-10-17 19:00:45 +0200901 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200902
903 shost->max_cmd_len = 16 + 252;
904 shost->max_id = 1;
Gerd Hoffmannbde027b2013-01-25 15:03:36 +0100905 shost->max_lun = 256;
906 shost->max_channel = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200907 shost->sg_tablesize = udev->bus->sg_tablesize;
908
Hans de Goede21fc05b2013-11-13 09:32:22 +0100909 devinfo = (struct uas_dev_info *)shost->hostdata;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200910 devinfo->intf = intf;
911 devinfo->udev = udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200912 devinfo->resetting = 0;
Hans de Goededa65c2b2013-11-11 11:51:42 +0100913 devinfo->shutdown = 0;
Hans de Goede59307852014-09-15 16:04:12 +0200914 devinfo->flags = id->driver_info;
915 usb_stor_adjust_quirks(udev, &devinfo->flags);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200916 init_usb_anchor(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +0200917 init_usb_anchor(&devinfo->sense_urbs);
918 init_usb_anchor(&devinfo->data_urbs);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200919 spin_lock_init(&devinfo->lock);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200920 INIT_WORK(&devinfo->work, uas_do_work);
Hans de Goede58d51442013-10-29 10:23:26 +0100921
922 result = uas_configure_endpoints(devinfo);
923 if (result)
924 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200925
Hans de Goeded3f7c152013-10-23 17:46:17 +0100926 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100927 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +0200928 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100929
Oliver Neukumc637f1f2014-03-28 11:29:25 +0100930 usb_set_intfdata(intf, shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100931 result = scsi_add_host(shost, &intf->dev);
932 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +0200933 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100934
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200935 scsi_scan_host(shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200936 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100937
Hans de Goede6ce82132013-10-17 19:00:45 +0200938free_streams:
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100939 uas_free_streams(devinfo);
Oliver Neukumc637f1f2014-03-28 11:29:25 +0100940 usb_set_intfdata(intf, NULL);
Hans de Goede6ce82132013-10-17 19:00:45 +0200941set_alt0:
942 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200943 if (shost)
944 scsi_host_put(shost);
945 return result;
946}
947
Hans de Goedef9dc0242014-09-13 12:26:42 +0200948static int uas_cmnd_list_empty(struct uas_dev_info *devinfo)
949{
950 unsigned long flags;
951 int i, r = 1;
952
953 spin_lock_irqsave(&devinfo->lock, flags);
954
955 for (i = 0; i < devinfo->qdepth; i++) {
956 if (devinfo->cmnd[i]) {
957 r = 0; /* Not empty */
958 break;
959 }
960 }
961
962 spin_unlock_irqrestore(&devinfo->lock, flags);
963
964 return r;
965}
966
967/*
968 * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily
969 * get empty while there still is more work to do due to sense-urbs completing
970 * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty.
971 */
972static int uas_wait_for_pending_cmnds(struct uas_dev_info *devinfo)
973{
974 unsigned long start_time;
975 int r;
976
977 start_time = jiffies;
978 do {
979 flush_work(&devinfo->work);
980
981 r = usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000);
982 if (r == 0)
983 return -ETIME;
984
985 r = usb_wait_anchor_empty_timeout(&devinfo->data_urbs, 500);
986 if (r == 0)
987 return -ETIME;
988
989 if (time_after(jiffies, start_time + 5 * HZ))
990 return -ETIME;
991 } while (!uas_cmnd_list_empty(devinfo));
992
993 return 0;
994}
995
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200996static int uas_pre_reset(struct usb_interface *intf)
997{
Hans de Goede4de7a3732013-10-22 16:10:44 +0100998 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +0100999 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001000 unsigned long flags;
1001
Hans de Goededa65c2b2013-11-11 11:51:42 +01001002 if (devinfo->shutdown)
1003 return 0;
1004
Hans de Goede4de7a3732013-10-22 16:10:44 +01001005 /* Block new requests */
1006 spin_lock_irqsave(shost->host_lock, flags);
1007 scsi_block_requests(shost);
1008 spin_unlock_irqrestore(shost->host_lock, flags);
1009
Hans de Goedef9dc0242014-09-13 12:26:42 +02001010 if (uas_wait_for_pending_cmnds(devinfo) != 0) {
Hans de Goede4de7a3732013-10-22 16:10:44 +01001011 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
Hans de Goedef9dc0242014-09-13 12:26:42 +02001012 scsi_unblock_requests(shost);
Hans de Goede4de7a3732013-10-22 16:10:44 +01001013 return 1;
1014 }
1015
1016 uas_free_streams(devinfo);
1017
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001018 return 0;
1019}
1020
1021static int uas_post_reset(struct usb_interface *intf)
1022{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001023 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001024 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001025 unsigned long flags;
Hans de Goedece39fe62014-09-13 12:26:50 +02001026 int err;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001027
Hans de Goededa65c2b2013-11-11 11:51:42 +01001028 if (devinfo->shutdown)
1029 return 0;
1030
Hans de Goedece39fe62014-09-13 12:26:50 +02001031 err = uas_configure_endpoints(devinfo);
1032 if (err) {
Hans de Goede58d51442013-10-29 10:23:26 +01001033 shost_printk(KERN_ERR, shost,
Hans de Goedece39fe62014-09-13 12:26:50 +02001034 "%s: alloc streams error %d after reset",
1035 __func__, err);
Hans de Goede58d51442013-10-29 10:23:26 +01001036 return 1;
1037 }
Hans de Goede4de7a3732013-10-22 16:10:44 +01001038
1039 spin_lock_irqsave(shost->host_lock, flags);
1040 scsi_report_bus_reset(shost, 0);
1041 spin_unlock_irqrestore(shost->host_lock, flags);
1042
1043 scsi_unblock_requests(shost);
1044
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001045 return 0;
1046}
1047
Hans de Goede0df1f662013-11-07 08:47:05 +01001048static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1049{
1050 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001051 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede0df1f662013-11-07 08:47:05 +01001052
Hans de Goedef9dc0242014-09-13 12:26:42 +02001053 if (uas_wait_for_pending_cmnds(devinfo) != 0) {
Hans de Goede0df1f662013-11-07 08:47:05 +01001054 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1055 return -ETIME;
1056 }
1057
1058 return 0;
1059}
1060
1061static int uas_resume(struct usb_interface *intf)
1062{
1063 return 0;
1064}
1065
1066static int uas_reset_resume(struct usb_interface *intf)
1067{
1068 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001069 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede0df1f662013-11-07 08:47:05 +01001070 unsigned long flags;
Hans de Goedece39fe62014-09-13 12:26:50 +02001071 int err;
Hans de Goede0df1f662013-11-07 08:47:05 +01001072
Hans de Goedece39fe62014-09-13 12:26:50 +02001073 err = uas_configure_endpoints(devinfo);
1074 if (err) {
Hans de Goede0df1f662013-11-07 08:47:05 +01001075 shost_printk(KERN_ERR, shost,
Hans de Goedece39fe62014-09-13 12:26:50 +02001076 "%s: alloc streams error %d after reset",
1077 __func__, err);
Hans de Goede0df1f662013-11-07 08:47:05 +01001078 return -EIO;
1079 }
1080
1081 spin_lock_irqsave(shost->host_lock, flags);
1082 scsi_report_bus_reset(shost, 0);
1083 spin_unlock_irqrestore(shost->host_lock, flags);
1084
1085 return 0;
1086}
1087
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001088static void uas_disconnect(struct usb_interface *intf)
1089{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001090 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001091 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goedeb7b5d112014-09-13 12:26:30 +02001092 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001093
Hans de Goedeb7b5d112014-09-13 12:26:30 +02001094 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001095 devinfo->resetting = 1;
Hans de Goedeb7b5d112014-09-13 12:26:30 +02001096 spin_unlock_irqrestore(&devinfo->lock, flags);
1097
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001098 cancel_work_sync(&devinfo->work);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001099 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001100 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1101 usb_kill_anchored_urbs(&devinfo->data_urbs);
Hans de Goede15893492014-09-13 12:26:36 +02001102 uas_zap_pending(devinfo, DID_NO_CONNECT);
1103
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001104 scsi_remove_host(shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001105 uas_free_streams(devinfo);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001106 scsi_host_put(shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001107}
1108
Hans de Goededa65c2b2013-11-11 11:51:42 +01001109/*
1110 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1111 * hang on reboot when the device is still in uas mode. Note the reset is
1112 * necessary as some devices won't revert to usb-storage mode without it.
1113 */
1114static void uas_shutdown(struct device *dev)
1115{
1116 struct usb_interface *intf = to_usb_interface(dev);
1117 struct usb_device *udev = interface_to_usbdev(intf);
1118 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001119 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goededa65c2b2013-11-11 11:51:42 +01001120
1121 if (system_state != SYSTEM_RESTART)
1122 return;
1123
1124 devinfo->shutdown = 1;
1125 uas_free_streams(devinfo);
1126 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1127 usb_reset_device(udev);
1128}
1129
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001130static struct usb_driver uas_driver = {
1131 .name = "uas",
1132 .probe = uas_probe,
1133 .disconnect = uas_disconnect,
1134 .pre_reset = uas_pre_reset,
1135 .post_reset = uas_post_reset,
Hans de Goede0df1f662013-11-07 08:47:05 +01001136 .suspend = uas_suspend,
1137 .resume = uas_resume,
1138 .reset_resume = uas_reset_resume,
Hans de Goededa65c2b2013-11-11 11:51:42 +01001139 .drvwrap.driver.shutdown = uas_shutdown,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001140 .id_table = uas_usb_ids,
1141};
1142
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001143module_usb_driver(uas_driver);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001144
1145MODULE_LICENSE("GPL");
Hans de Goedef50a4962013-10-28 10:48:04 +00001146MODULE_AUTHOR(
1147 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");