blob: 3debefbd858f33217a84242501931a7d4e3f444e [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
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020033/*
34 * The r00-r01c specs define this version of the SENSE IU data structure.
35 * It's still in use by several different firmware releases.
36 */
37struct sense_iu_old {
38 __u8 iu_id;
39 __u8 rsvd1;
40 __be16 tag;
41 __be16 len;
42 __u8 status;
43 __u8 service_response;
44 __u8 sense[SCSI_SENSE_BUFFERSIZE];
45};
46
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020047struct uas_dev_info {
48 struct usb_interface *intf;
49 struct usb_device *udev;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +020050 struct usb_anchor cmd_urbs;
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +020051 struct usb_anchor sense_urbs;
52 struct usb_anchor data_urbs;
Hans de Goede59307852014-09-15 16:04:12 +020053 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +020054 int qdepth, resetting;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020055 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
56 unsigned use_streams:1;
57 unsigned uas_sense_old:1;
Hans de Goededa65c2b2013-11-11 11:51:42 +010058 unsigned shutdown:1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +010059 struct scsi_cmnd *cmnd;
Gerd Hoffmanne0648522012-09-25 10:47:08 +020060 spinlock_t lock;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +020061 struct work_struct work;
Hans de Goede61c09ce2013-11-12 13:44:20 +010062 struct list_head inflight_list;
Gerd Hoffmann326349f2013-09-13 13:27:13 +020063 struct list_head dead_list;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020064};
65
66enum {
Matthew Wilcox92a3f762010-12-15 15:44:04 -050067 SUBMIT_STATUS_URB = (1 << 1),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020068 ALLOC_DATA_IN_URB = (1 << 2),
69 SUBMIT_DATA_IN_URB = (1 << 3),
70 ALLOC_DATA_OUT_URB = (1 << 4),
71 SUBMIT_DATA_OUT_URB = (1 << 5),
72 ALLOC_CMD_URB = (1 << 6),
73 SUBMIT_CMD_URB = (1 << 7),
Gerd Hoffmannb1d67692012-06-19 09:54:51 +020074 COMMAND_INFLIGHT = (1 << 8),
75 DATA_IN_URB_INFLIGHT = (1 << 9),
76 DATA_OUT_URB_INFLIGHT = (1 << 10),
77 COMMAND_COMPLETED = (1 << 11),
Gerd Hoffmannef018cc92012-09-25 10:47:06 +020078 COMMAND_ABORTED = (1 << 12),
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +010079 UNLINK_DATA_URBS = (1 << 13),
Gerd Hoffmannefefecf2012-11-30 11:54:42 +010080 IS_IN_WORK_LIST = (1 << 14),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020081};
82
83/* Overrides scsi_pointer */
84struct uas_cmd_info {
85 unsigned int state;
86 unsigned int stream;
87 struct urb *cmd_urb;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020088 struct urb *data_in_urb;
89 struct urb *data_out_urb;
Hans de Goede040d1a82013-11-12 14:02:12 +010090 struct list_head list;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020091};
92
93/* I hate forward declarations, but I actually have a loop */
94static int uas_submit_urbs(struct scsi_cmnd *cmnd,
95 struct uas_dev_info *devinfo, gfp_t gfp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -080096static void uas_do_work(struct work_struct *work);
Gerd Hoffmann4c456972012-11-30 11:54:44 +010097static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
Gerd Hoffmannd89bd832013-09-13 13:27:11 +020098static void uas_free_streams(struct uas_dev_info *devinfo);
Gerd Hoffmann326349f2013-09-13 13:27:13 +020099static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200100
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200101/* Must be called with devinfo->lock held, will temporary unlock the lock */
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100102static void uas_unlink_data_urbs(struct uas_dev_info *devinfo,
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200103 struct uas_cmd_info *cmdinfo,
104 unsigned long *lock_flags)
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100105{
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100106 /*
107 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
108 * (called by urb completion) doesn't release cmdinfo
109 * underneath us.
110 */
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100111 cmdinfo->state |= UNLINK_DATA_URBS;
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200112 spin_unlock_irqrestore(&devinfo->lock, *lock_flags);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100113
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100114 if (cmdinfo->data_in_urb)
115 usb_unlink_urb(cmdinfo->data_in_urb);
116 if (cmdinfo->data_out_urb)
117 usb_unlink_urb(cmdinfo->data_out_urb);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100118
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200119 spin_lock_irqsave(&devinfo->lock, *lock_flags);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100120 cmdinfo->state &= ~UNLINK_DATA_URBS;
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100121}
122
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200123static void uas_do_work(struct work_struct *work)
124{
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200125 struct uas_dev_info *devinfo =
126 container_of(work, struct uas_dev_info, work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200127 struct uas_cmd_info *cmdinfo;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200128 unsigned long flags;
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800129 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200130
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200131 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200132
133 if (devinfo->resetting)
134 goto out;
135
Hans de Goede040d1a82013-11-12 14:02:12 +0100136 list_for_each_entry(cmdinfo, &devinfo->inflight_list, list) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200137 struct scsi_pointer *scp = (void *)cmdinfo;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200138 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
139 SCp);
Hans de Goede61c09ce2013-11-12 13:44:20 +0100140
141 if (!(cmdinfo->state & IS_IN_WORK_LIST))
142 continue;
143
Oliver Neukume7eda932014-03-28 11:10:30 +0100144 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
Hans de Goede61c09ce2013-11-12 13:44:20 +0100145 if (!err)
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100146 cmdinfo->state &= ~IS_IN_WORK_LIST;
Hans de Goede61c09ce2013-11-12 13:44:20 +0100147 else
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200148 schedule_work(&devinfo->work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200149 }
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200150out:
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200151 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200152}
153
Hans de Goededa3033e2013-11-12 13:57:24 +0100154static void uas_mark_cmd_dead(struct uas_dev_info *devinfo,
Hans de Goede673331c2013-11-14 14:27:27 +0100155 struct uas_cmd_info *cmdinfo,
156 int result, const char *caller)
Hans de Goededa3033e2013-11-12 13:57:24 +0100157{
158 struct scsi_pointer *scp = (void *)cmdinfo;
159 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
160
161 uas_log_cmd_state(cmnd, caller);
Sanjeev Sharmaab945ef2014-08-12 12:10:21 +0530162 lockdep_assert_held(&devinfo->lock);
Hans de Goededa3033e2013-11-12 13:57:24 +0100163 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
164 cmdinfo->state |= COMMAND_ABORTED;
165 cmdinfo->state &= ~IS_IN_WORK_LIST;
Hans de Goede673331c2013-11-14 14:27:27 +0100166 cmnd->result = result << 16;
Hans de Goede040d1a82013-11-12 14:02:12 +0100167 list_move_tail(&cmdinfo->list, &devinfo->dead_list);
Hans de Goededa3033e2013-11-12 13:57:24 +0100168}
169
Hans de Goede673331c2013-11-14 14:27:27 +0100170static void uas_abort_inflight(struct uas_dev_info *devinfo, int result,
171 const char *caller)
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100172{
173 struct uas_cmd_info *cmdinfo;
174 struct uas_cmd_info *temp;
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100175 unsigned long flags;
176
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100177 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goede040d1a82013-11-12 14:02:12 +0100178 list_for_each_entry_safe(cmdinfo, temp, &devinfo->inflight_list, list)
Hans de Goede673331c2013-11-14 14:27:27 +0100179 uas_mark_cmd_dead(devinfo, cmdinfo, result, caller);
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100180 spin_unlock_irqrestore(&devinfo->lock, flags);
181}
182
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200183static void uas_add_work(struct uas_cmd_info *cmdinfo)
184{
185 struct scsi_pointer *scp = (void *)cmdinfo;
186 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
187 struct uas_dev_info *devinfo = cmnd->device->hostdata;
188
Sanjeev Sharmaab945ef2014-08-12 12:10:21 +0530189 lockdep_assert_held(&devinfo->lock);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200190 cmdinfo->state |= IS_IN_WORK_LIST;
191 schedule_work(&devinfo->work);
192}
193
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200194static void uas_zap_dead(struct uas_dev_info *devinfo)
195{
196 struct uas_cmd_info *cmdinfo;
197 struct uas_cmd_info *temp;
198 unsigned long flags;
199
200 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goede040d1a82013-11-12 14:02:12 +0100201 list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, list) {
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200202 struct scsi_pointer *scp = (void *)cmdinfo;
203 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
204 SCp);
205 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200206 WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED));
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200207 /* all urbs are killed, clear inflight bits */
208 cmdinfo->state &= ~(COMMAND_INFLIGHT |
209 DATA_IN_URB_INFLIGHT |
210 DATA_OUT_URB_INFLIGHT);
211 uas_try_complete(cmnd, __func__);
212 }
213 spin_unlock_irqrestore(&devinfo->lock, flags);
214}
215
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200216static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
217{
218 struct sense_iu *sense_iu = urb->transfer_buffer;
219 struct scsi_device *sdev = cmnd->device;
220
221 if (urb->actual_length > 16) {
222 unsigned len = be16_to_cpup(&sense_iu->len);
223 if (len + 16 != urb->actual_length) {
224 int newlen = min(len + 16, urb->actual_length) - 16;
225 if (newlen < 0)
226 newlen = 0;
227 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
228 "disagrees with IU sense data length %d, "
229 "using %d bytes of sense data\n", __func__,
230 urb->actual_length, len, newlen);
231 len = newlen;
232 }
233 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
234 }
235
236 cmnd->result = sense_iu->status;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200237}
238
239static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
240{
241 struct sense_iu_old *sense_iu = urb->transfer_buffer;
242 struct scsi_device *sdev = cmnd->device;
243
244 if (urb->actual_length > 8) {
245 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
246 if (len + 8 != urb->actual_length) {
247 int newlen = min(len + 8, urb->actual_length) - 8;
248 if (newlen < 0)
249 newlen = 0;
250 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
251 "disagrees with IU sense data length %d, "
252 "using %d bytes of sense data\n", __func__,
253 urb->actual_length, len, newlen);
254 len = newlen;
255 }
256 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
257 }
258
259 cmnd->result = sense_iu->status;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200260}
261
Hans de Goedee0620002014-09-13 12:26:31 +0200262/*
263 * scsi-tags go from 0 - (nr_tags - 1), uas tags need to match stream-ids,
264 * which go from 1 - nr_streams. And we use 1 for untagged commands.
265 */
266static int uas_get_tag(struct scsi_cmnd *cmnd)
267{
268 int tag;
269
270 if (blk_rq_tagged(cmnd->request))
271 tag = cmnd->request->tag + 2;
272 else
273 tag = 1;
274
275 return tag;
276}
277
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200278static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
279{
280 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
281
282 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100283 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
Hans de Goedee0620002014-09-13 12:26:31 +0200284 caller, cmnd, uas_get_tag(cmnd),
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200285 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
286 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
287 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
288 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
289 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
290 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
291 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
292 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
293 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
294 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
Gerd Hoffmannef018cc92012-09-25 10:47:06 +0200295 (ci->state & COMMAND_COMPLETED) ? " done" : "",
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100296 (ci->state & COMMAND_ABORTED) ? " abort" : "",
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100297 (ci->state & UNLINK_DATA_URBS) ? " unlink": "",
298 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200299}
300
301static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
302{
303 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200304 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200305
Sanjeev Sharmaab945ef2014-08-12 12:10:21 +0530306 lockdep_assert_held(&devinfo->lock);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200307 if (cmdinfo->state & (COMMAND_INFLIGHT |
308 DATA_IN_URB_INFLIGHT |
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100309 DATA_OUT_URB_INFLIGHT |
310 UNLINK_DATA_URBS))
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200311 return -EBUSY;
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200312 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200313 cmdinfo->state |= COMMAND_COMPLETED;
314 usb_free_urb(cmdinfo->data_in_urb);
315 usb_free_urb(cmdinfo->data_out_urb);
Hans de Goede673331c2013-11-14 14:27:27 +0100316 if (cmdinfo->state & COMMAND_ABORTED)
Gerd Hoffmann0871d7d2012-09-25 10:47:07 +0200317 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
Hans de Goede040d1a82013-11-12 14:02:12 +0100318 list_del(&cmdinfo->list);
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200319 cmnd->scsi_done(cmnd);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200320 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200321}
322
323static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200324 unsigned direction)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200325{
326 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
327 int err;
328
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200329 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200330 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
331 if (err) {
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200332 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200333 }
334}
335
336static void uas_stat_cmplt(struct urb *urb)
337{
338 struct iu *iu = urb->transfer_buffer;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100339 struct Scsi_Host *shost = urb->context;
Hans de Goede21fc05b2013-11-13 09:32:22 +0100340 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200341 struct scsi_cmnd *cmnd;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200342 struct uas_cmd_info *cmdinfo;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200343 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200344 u16 tag;
345
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200346 spin_lock_irqsave(&devinfo->lock, flags);
347
348 if (devinfo->resetting)
349 goto out;
350
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200351 if (urb->status) {
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200352 if (urb->status == -ENOENT) {
353 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
354 urb->stream_id);
355 } else {
356 dev_err(&urb->dev->dev, "stat urb: status %d\n",
357 urb->status);
358 }
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200359 goto out;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200360 }
361
362 tag = be16_to_cpup(&iu->tag) - 1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100363 if (tag == 0)
364 cmnd = devinfo->cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200365 else
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100366 cmnd = scsi_host_find_tag(shost, tag - 1);
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200367
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200368 if (!cmnd)
369 goto out;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200370
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200371 cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200372 switch (iu->iu_id) {
373 case IU_ID_STATUS:
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100374 if (devinfo->cmnd == cmnd)
375 devinfo->cmnd = NULL;
376
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200377 if (urb->actual_length < 16)
378 devinfo->uas_sense_old = 1;
379 if (devinfo->uas_sense_old)
380 uas_sense_old(urb, cmnd);
381 else
382 uas_sense(urb, cmnd);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200383 if (cmnd->result != 0) {
384 /* cancel data transfers on error */
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200385 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200386 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200387 cmdinfo->state &= ~COMMAND_INFLIGHT;
388 uas_try_complete(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200389 break;
390 case IU_ID_READ_READY:
Hans de Goede8e453152013-11-15 10:04:11 +0100391 if (!cmdinfo->data_in_urb ||
392 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
393 scmd_printk(KERN_ERR, cmnd, "unexpected read rdy\n");
394 break;
395 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200396 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
397 break;
398 case IU_ID_WRITE_READY:
Hans de Goede8e453152013-11-15 10:04:11 +0100399 if (!cmdinfo->data_out_urb ||
400 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
401 scmd_printk(KERN_ERR, cmnd, "unexpected write rdy\n");
402 break;
403 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200404 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
405 break;
406 default:
407 scmd_printk(KERN_ERR, cmnd,
408 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
409 }
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200410out:
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200411 usb_free_urb(urb);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200412 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200413}
414
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200415static void uas_data_cmplt(struct urb *urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200416{
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200417 struct scsi_cmnd *cmnd = urb->context;
418 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200419 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200420 struct scsi_data_buffer *sdb = NULL;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200421 unsigned long flags;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200422
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200423 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200424
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200425 if (cmdinfo->data_in_urb == urb) {
426 sdb = scsi_in(cmnd);
427 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
428 } else if (cmdinfo->data_out_urb == urb) {
429 sdb = scsi_out(cmnd);
430 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
431 }
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200432 if (sdb == NULL) {
433 WARN_ON_ONCE(1);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200434 goto out;
435 }
436
437 if (devinfo->resetting)
438 goto out;
439
440 if (urb->status) {
Hans de Goede876285c2013-11-07 08:52:42 +0100441 if (urb->status != -ECONNRESET) {
442 uas_log_cmd_state(cmnd, __func__);
443 scmd_printk(KERN_ERR, cmnd,
444 "data cmplt err %d stream %d\n",
445 urb->status, urb->stream_id);
446 }
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200447 /* error: no data transfered */
448 sdb->resid = sdb->length;
449 } else {
450 sdb->resid = sdb->length - urb->actual_length;
451 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200452 uas_try_complete(cmnd, __func__);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200453out:
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200454 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200455}
456
Hans de Goede876285c2013-11-07 08:52:42 +0100457static void uas_cmd_cmplt(struct urb *urb)
458{
459 struct scsi_cmnd *cmnd = urb->context;
460
461 if (urb->status) {
462 uas_log_cmd_state(cmnd, __func__);
463 scmd_printk(KERN_ERR, cmnd, "cmd cmplt err %d\n", urb->status);
464 }
465 usb_free_urb(urb);
466}
467
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200468static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200469 unsigned int pipe, u16 stream_id,
470 struct scsi_cmnd *cmnd,
471 enum dma_data_direction dir)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200472{
473 struct usb_device *udev = devinfo->udev;
474 struct urb *urb = usb_alloc_urb(0, gfp);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200475 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
476 ? scsi_in(cmnd) : scsi_out(cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200477
478 if (!urb)
479 goto out;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200480 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
481 uas_data_cmplt, cmnd);
Hans de Goedec6d45792013-11-12 10:53:57 +0100482 urb->stream_id = stream_id;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200483 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
484 urb->sg = sdb->table.sgl;
485 out:
486 return urb;
487}
488
489static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200490 struct Scsi_Host *shost, u16 stream_id)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200491{
492 struct usb_device *udev = devinfo->udev;
493 struct urb *urb = usb_alloc_urb(0, gfp);
494 struct sense_iu *iu;
495
496 if (!urb)
497 goto out;
498
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500499 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200500 if (!iu)
501 goto free;
502
503 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200504 uas_stat_cmplt, shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200505 urb->stream_id = stream_id;
506 urb->transfer_flags |= URB_FREE_BUFFER;
507 out:
508 return urb;
509 free:
510 usb_free_urb(urb);
511 return NULL;
512}
513
514static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Hans de Goedea887cd32013-10-17 19:47:28 +0200515 struct scsi_cmnd *cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200516{
517 struct usb_device *udev = devinfo->udev;
518 struct scsi_device *sdev = cmnd->device;
519 struct urb *urb = usb_alloc_urb(0, gfp);
520 struct command_iu *iu;
521 int len;
522
523 if (!urb)
524 goto out;
525
526 len = cmnd->cmd_len - 16;
527 if (len < 0)
528 len = 0;
529 len = ALIGN(len, 4);
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500530 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200531 if (!iu)
532 goto free;
533
534 iu->iu_id = IU_ID_COMMAND;
Hans de Goedee0620002014-09-13 12:26:31 +0200535 iu->tag = cpu_to_be16(uas_get_tag(cmnd));
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100536 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200537 iu->len = len;
538 int_to_scsilun(sdev->lun, &iu->lun);
539 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
540
541 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
Hans de Goede876285c2013-11-07 08:52:42 +0100542 uas_cmd_cmplt, cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200543 urb->transfer_flags |= URB_FREE_BUFFER;
544 out:
545 return urb;
546 free:
547 usb_free_urb(urb);
548 return NULL;
549}
550
551/*
552 * Why should I request the Status IU before sending the Command IU? Spec
553 * says to, but also says the device may receive them in any order. Seems
554 * daft to me.
555 */
556
Hans de Goede876285c2013-11-07 08:52:42 +0100557static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd,
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100558 gfp_t gfp, unsigned int stream)
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200559{
Hans de Goede876285c2013-11-07 08:52:42 +0100560 struct Scsi_Host *shost = cmnd->device->host;
Hans de Goede21fc05b2013-11-13 09:32:22 +0100561 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200562 struct urb *urb;
Hans de Goede876285c2013-11-07 08:52:42 +0100563 int err;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200564
565 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
566 if (!urb)
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100567 return NULL;
Hans de Goeded5f808d2013-10-17 19:30:26 +0200568 usb_anchor_urb(urb, &devinfo->sense_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100569 err = usb_submit_urb(urb, gfp);
570 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200571 usb_unanchor_urb(urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100572 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200573 shost_printk(KERN_INFO, shost,
Hans de Goede876285c2013-11-07 08:52:42 +0100574 "sense urb submission error %d stream %d\n",
575 err, stream);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200576 usb_free_urb(urb);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100577 return NULL;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200578 }
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100579 return urb;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200580}
581
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200582static int uas_submit_urbs(struct scsi_cmnd *cmnd,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200583 struct uas_dev_info *devinfo, gfp_t gfp)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200584{
585 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100586 struct urb *urb;
Hans de Goede876285c2013-11-07 08:52:42 +0100587 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200588
Sanjeev Sharmaab945ef2014-08-12 12:10:21 +0530589 lockdep_assert_held(&devinfo->lock);
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500590 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Hans de Goede876285c2013-11-07 08:52:42 +0100591 urb = uas_submit_sense_urb(cmnd, gfp, cmdinfo->stream);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100592 if (!urb)
593 return SCSI_MLQUEUE_DEVICE_BUSY;
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500594 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200595 }
596
597 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
598 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200599 devinfo->data_in_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200600 cmnd, DMA_FROM_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200601 if (!cmdinfo->data_in_urb)
602 return SCSI_MLQUEUE_DEVICE_BUSY;
603 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
604 }
605
606 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200607 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100608 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
609 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200610 usb_unanchor_urb(cmdinfo->data_in_urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100611 uas_log_cmd_state(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200612 scmd_printk(KERN_INFO, cmnd,
Hans de Goede876285c2013-11-07 08:52:42 +0100613 "data in urb submission error %d stream %d\n",
614 err, cmdinfo->data_in_urb->stream_id);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200615 return SCSI_MLQUEUE_DEVICE_BUSY;
616 }
617 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200618 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200619 }
620
621 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
622 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200623 devinfo->data_out_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200624 cmnd, DMA_TO_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200625 if (!cmdinfo->data_out_urb)
626 return SCSI_MLQUEUE_DEVICE_BUSY;
627 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
628 }
629
630 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200631 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100632 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
633 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200634 usb_unanchor_urb(cmdinfo->data_out_urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100635 uas_log_cmd_state(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200636 scmd_printk(KERN_INFO, cmnd,
Hans de Goede876285c2013-11-07 08:52:42 +0100637 "data out urb submission error %d stream %d\n",
638 err, cmdinfo->data_out_urb->stream_id);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200639 return SCSI_MLQUEUE_DEVICE_BUSY;
640 }
641 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200642 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200643 }
644
645 if (cmdinfo->state & ALLOC_CMD_URB) {
Hans de Goedea887cd32013-10-17 19:47:28 +0200646 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200647 if (!cmdinfo->cmd_urb)
648 return SCSI_MLQUEUE_DEVICE_BUSY;
649 cmdinfo->state &= ~ALLOC_CMD_URB;
650 }
651
652 if (cmdinfo->state & SUBMIT_CMD_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200653 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100654 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
655 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200656 usb_unanchor_urb(cmdinfo->cmd_urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100657 uas_log_cmd_state(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200658 scmd_printk(KERN_INFO, cmnd,
Hans de Goede876285c2013-11-07 08:52:42 +0100659 "cmd urb submission error %d\n", err);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200660 return SCSI_MLQUEUE_DEVICE_BUSY;
661 }
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200662 cmdinfo->cmd_urb = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200663 cmdinfo->state &= ~SUBMIT_CMD_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200664 cmdinfo->state |= COMMAND_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200665 }
666
667 return 0;
668}
669
Jeff Garzikf2812332010-11-16 02:10:29 -0500670static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200671 void (*done)(struct scsi_cmnd *))
672{
673 struct scsi_device *sdev = cmnd->device;
674 struct uas_dev_info *devinfo = sdev->hostdata;
675 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200676 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200677 int err;
678
679 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
680
Hans de Goede59307852014-09-15 16:04:12 +0200681 if ((devinfo->flags & US_FL_NO_ATA_1X) &&
682 (cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) {
683 memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB,
684 sizeof(usb_stor_sense_invalidCDB));
685 cmnd->result = SAM_STAT_CHECK_CONDITION;
686 cmnd->scsi_done(cmnd);
687 return 0;
688 }
689
Hans de Goedec6f63202013-11-13 09:24:15 +0100690 spin_lock_irqsave(&devinfo->lock, flags);
691
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100692 if (devinfo->resetting) {
693 cmnd->result = DID_ERROR << 16;
694 cmnd->scsi_done(cmnd);
Hans de Goedec6f63202013-11-13 09:24:15 +0100695 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100696 return 0;
697 }
698
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200699 if (devinfo->cmnd) {
700 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200701 return SCSI_MLQUEUE_DEVICE_BUSY;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200702 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200703
Hans de Goede3a4462e2013-11-14 11:06:13 +0100704 memset(cmdinfo, 0, sizeof(*cmdinfo));
705
Hans de Goedee0620002014-09-13 12:26:31 +0200706 if (!blk_rq_tagged(cmnd->request))
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100707 devinfo->cmnd = cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200708
709 cmnd->scsi_done = done;
710
Hans de Goedee0620002014-09-13 12:26:31 +0200711 cmdinfo->stream = uas_get_tag(cmnd);
712 cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200713
714 switch (cmnd->sc_data_direction) {
715 case DMA_FROM_DEVICE:
716 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
717 break;
718 case DMA_BIDIRECTIONAL:
719 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
720 case DMA_TO_DEVICE:
721 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
722 case DMA_NONE:
723 break;
724 }
725
726 if (!devinfo->use_streams) {
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200727 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200728 cmdinfo->stream = 0;
729 }
730
731 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
732 if (err) {
733 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500734 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200735 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200736 return SCSI_MLQUEUE_DEVICE_BUSY;
737 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200738 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200739 }
740
Hans de Goede040d1a82013-11-12 14:02:12 +0100741 list_add_tail(&cmdinfo->list, &devinfo->inflight_list);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200742 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200743 return 0;
744}
745
Jeff Garzikf2812332010-11-16 02:10:29 -0500746static DEF_SCSI_QCMD(uas_queuecommand)
747
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200748static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
749{
750 struct scsi_device *sdev = cmnd->device;
751 struct uas_dev_info *devinfo = sdev->hostdata;
752 struct usb_device *udev = devinfo->udev;
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200753 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200754 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200755
Hans de Goedebe326f42013-09-22 16:27:02 +0200756 err = usb_lock_device_for_reset(udev, devinfo->intf);
757 if (err) {
758 shost_printk(KERN_ERR, sdev->host,
759 "%s FAILED to get lock err %d\n", __func__, err);
760 return FAILED;
761 }
762
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200763 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200764
765 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200766 devinfo->resetting = 1;
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200767 spin_unlock_irqrestore(&devinfo->lock, flags);
768
Hans de Goede673331c2013-11-14 14:27:27 +0100769 uas_abort_inflight(devinfo, DID_RESET, __func__);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200770 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200771 usb_kill_anchored_urbs(&devinfo->sense_urbs);
772 usb_kill_anchored_urbs(&devinfo->data_urbs);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200773 uas_zap_dead(devinfo);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200774 err = usb_reset_device(udev);
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200775
776 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200777 devinfo->resetting = 0;
Hans de Goedeb7b5d112014-09-13 12:26:30 +0200778 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200779
Hans de Goedebe326f42013-09-22 16:27:02 +0200780 usb_unlock_device(udev);
781
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200782 if (err) {
783 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
784 return FAILED;
785 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200786
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200787 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
788 return SUCCESS;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200789}
790
791static int uas_slave_alloc(struct scsi_device *sdev)
792{
Hans de Goede21fc05b2013-11-13 09:32:22 +0100793 sdev->hostdata = (void *)sdev->host->hostdata;
Hans de Goede37599f92013-11-15 10:04:31 +0100794
795 /* USB has unusual DMA-alignment requirements: Although the
796 * starting address of each scatter-gather element doesn't matter,
797 * the length of each element except the last must be divisible
798 * by the Bulk maxpacket value. There's currently no way to
799 * express this by block-layer constraints, so we'll cop out
800 * and simply require addresses to be aligned at 512-byte
801 * boundaries. This is okay since most block I/O involves
802 * hardware sectors that are multiples of 512 bytes in length,
803 * and since host controllers up through USB 2.0 have maxpacket
804 * values no larger than 512.
805 *
806 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
807 * values can be as large as 2048. To make that work properly
808 * will require changes to the block layer.
809 */
810 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
811
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200812 return 0;
813}
814
815static int uas_slave_configure(struct scsi_device *sdev)
816{
817 struct uas_dev_info *devinfo = sdev->hostdata;
Hans de Goede734016b2014-09-16 18:36:52 +0200818
819 if (devinfo->flags & US_FL_NO_REPORT_OPCODES)
820 sdev->no_report_opcodes = 1;
821
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200822 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
Hans de Goeded3f7c152013-10-23 17:46:17 +0100823 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200824 return 0;
825}
826
827static struct scsi_host_template uas_host_template = {
828 .module = THIS_MODULE,
829 .name = "uas",
830 .queuecommand = uas_queuecommand,
831 .slave_alloc = uas_slave_alloc,
832 .slave_configure = uas_slave_configure,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200833 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
834 .can_queue = 65536, /* Is there a limit on the _host_ ? */
835 .this_id = -1,
836 .sg_tablesize = SG_NONE,
837 .cmd_per_lun = 1, /* until we override it */
838 .skip_settle_delay = 1,
839 .ordered_tag = 1,
840};
841
Hans de Goede79b4c062013-10-25 17:04:33 +0100842#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
843 vendorName, productName, useProtocol, useTransport, \
844 initFunction, flags) \
845{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
846 .driver_info = (flags) }
847
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200848static struct usb_device_id uas_usb_ids[] = {
Hans de Goede79b4c062013-10-25 17:04:33 +0100849# include "unusual_uas.h"
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200850 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
851 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
852 /* 0xaa is a prototype device I happen to have access to */
853 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
854 { }
855};
856MODULE_DEVICE_TABLE(usb, uas_usb_ids);
857
Hans de Goede79b4c062013-10-25 17:04:33 +0100858#undef UNUSUAL_DEV
859
Hans de Goedee1be0672013-10-21 08:00:58 +0100860static int uas_switch_interface(struct usb_device *udev,
861 struct usb_interface *intf)
862{
863 int alt;
864
865 alt = uas_find_uas_alt_setting(intf);
866 if (alt < 0)
867 return alt;
868
869 return usb_set_interface(udev,
870 intf->altsetting[0].desc.bInterfaceNumber, alt);
871}
872
Hans de Goede58d51442013-10-29 10:23:26 +0100873static int uas_configure_endpoints(struct uas_dev_info *devinfo)
Hans de Goede34f11e52013-10-29 08:54:48 +0100874{
875 struct usb_host_endpoint *eps[4] = { };
876 struct usb_device *udev = devinfo->udev;
877 int r;
878
879 devinfo->uas_sense_old = 0;
880 devinfo->cmnd = NULL;
881
882 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
Hans de Goede74d71ae2013-10-29 10:10:36 +0100883 if (r)
884 return r;
Hans de Goede34f11e52013-10-29 08:54:48 +0100885
Hans de Goede74d71ae2013-10-29 10:10:36 +0100886 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
887 usb_endpoint_num(&eps[0]->desc));
888 devinfo->status_pipe = usb_rcvbulkpipe(udev,
889 usb_endpoint_num(&eps[1]->desc));
890 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
891 usb_endpoint_num(&eps[2]->desc));
892 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
893 usb_endpoint_num(&eps[3]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200894
Hans de Goede58d51442013-10-29 10:23:26 +0100895 if (udev->speed != USB_SPEED_SUPER) {
Hans de Goedee2875c32014-08-01 17:33:08 +0200896 devinfo->qdepth = 32;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200897 devinfo->use_streams = 0;
898 } else {
Hans de Goede58d51442013-10-29 10:23:26 +0100899 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
Oliver Neukum94d72f02014-03-28 11:25:50 +0100900 3, 256, GFP_NOIO);
Hans de Goede58d51442013-10-29 10:23:26 +0100901 if (devinfo->qdepth < 0)
902 return devinfo->qdepth;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200903 devinfo->use_streams = 1;
904 }
Hans de Goede58d51442013-10-29 10:23:26 +0100905
906 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200907}
908
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100909static void uas_free_streams(struct uas_dev_info *devinfo)
910{
911 struct usb_device *udev = devinfo->udev;
912 struct usb_host_endpoint *eps[3];
913
914 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
915 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
916 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
Oliver Neukum94d72f02014-03-28 11:25:50 +0100917 usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100918}
919
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200920static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
921{
Hans de Goede6ce82132013-10-17 19:00:45 +0200922 int result = -ENOMEM;
923 struct Scsi_Host *shost = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200924 struct uas_dev_info *devinfo;
925 struct usb_device *udev = interface_to_usbdev(intf);
926
Hans de Goede79b4c062013-10-25 17:04:33 +0100927 if (!uas_use_uas_driver(intf, id))
928 return -ENODEV;
929
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500930 if (uas_switch_interface(udev, intf))
931 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200932
Hans de Goede21fc05b2013-11-13 09:32:22 +0100933 shost = scsi_host_alloc(&uas_host_template,
934 sizeof(struct uas_dev_info));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200935 if (!shost)
Hans de Goede6ce82132013-10-17 19:00:45 +0200936 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200937
938 shost->max_cmd_len = 16 + 252;
939 shost->max_id = 1;
Gerd Hoffmannbde027b2013-01-25 15:03:36 +0100940 shost->max_lun = 256;
941 shost->max_channel = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200942 shost->sg_tablesize = udev->bus->sg_tablesize;
943
Hans de Goede21fc05b2013-11-13 09:32:22 +0100944 devinfo = (struct uas_dev_info *)shost->hostdata;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200945 devinfo->intf = intf;
946 devinfo->udev = udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200947 devinfo->resetting = 0;
Hans de Goededa65c2b2013-11-11 11:51:42 +0100948 devinfo->shutdown = 0;
Hans de Goede59307852014-09-15 16:04:12 +0200949 devinfo->flags = id->driver_info;
950 usb_stor_adjust_quirks(udev, &devinfo->flags);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200951 init_usb_anchor(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +0200952 init_usb_anchor(&devinfo->sense_urbs);
953 init_usb_anchor(&devinfo->data_urbs);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200954 spin_lock_init(&devinfo->lock);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200955 INIT_WORK(&devinfo->work, uas_do_work);
Hans de Goede61c09ce2013-11-12 13:44:20 +0100956 INIT_LIST_HEAD(&devinfo->inflight_list);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200957 INIT_LIST_HEAD(&devinfo->dead_list);
Hans de Goede58d51442013-10-29 10:23:26 +0100958
959 result = uas_configure_endpoints(devinfo);
960 if (result)
961 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200962
Hans de Goeded3f7c152013-10-23 17:46:17 +0100963 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100964 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +0200965 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100966
Oliver Neukumc637f1fa2014-03-28 11:29:25 +0100967 usb_set_intfdata(intf, shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100968 result = scsi_add_host(shost, &intf->dev);
969 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +0200970 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100971
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200972 scsi_scan_host(shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200973 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100974
Hans de Goede6ce82132013-10-17 19:00:45 +0200975free_streams:
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100976 uas_free_streams(devinfo);
Oliver Neukumc637f1fa2014-03-28 11:29:25 +0100977 usb_set_intfdata(intf, NULL);
Hans de Goede6ce82132013-10-17 19:00:45 +0200978set_alt0:
979 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200980 if (shost)
981 scsi_host_put(shost);
982 return result;
983}
984
985static int uas_pre_reset(struct usb_interface *intf)
986{
Hans de Goede4de7a3732013-10-22 16:10:44 +0100987 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +0100988 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede4de7a3732013-10-22 16:10:44 +0100989 unsigned long flags;
990
Hans de Goededa65c2b2013-11-11 11:51:42 +0100991 if (devinfo->shutdown)
992 return 0;
993
Hans de Goede4de7a3732013-10-22 16:10:44 +0100994 /* Block new requests */
995 spin_lock_irqsave(shost->host_lock, flags);
996 scsi_block_requests(shost);
997 spin_unlock_irqrestore(shost->host_lock, flags);
998
999 /* Wait for any pending requests to complete */
1000 flush_work(&devinfo->work);
1001 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1002 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1003 return 1;
1004 }
1005
1006 uas_free_streams(devinfo);
1007
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001008 return 0;
1009}
1010
1011static int uas_post_reset(struct usb_interface *intf)
1012{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001013 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001014 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001015 unsigned long flags;
1016
Hans de Goededa65c2b2013-11-11 11:51:42 +01001017 if (devinfo->shutdown)
1018 return 0;
1019
Hans de Goede58d51442013-10-29 10:23:26 +01001020 if (uas_configure_endpoints(devinfo) != 0) {
1021 shost_printk(KERN_ERR, shost,
1022 "%s: alloc streams error after reset", __func__);
1023 return 1;
1024 }
Hans de Goede4de7a3732013-10-22 16:10:44 +01001025
1026 spin_lock_irqsave(shost->host_lock, flags);
1027 scsi_report_bus_reset(shost, 0);
1028 spin_unlock_irqrestore(shost->host_lock, flags);
1029
1030 scsi_unblock_requests(shost);
1031
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001032 return 0;
1033}
1034
Hans de Goede0df1f662013-11-07 08:47:05 +01001035static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1036{
1037 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001038 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede0df1f662013-11-07 08:47:05 +01001039
1040 /* Wait for any pending requests to complete */
1041 flush_work(&devinfo->work);
1042 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1043 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1044 return -ETIME;
1045 }
1046
1047 return 0;
1048}
1049
1050static int uas_resume(struct usb_interface *intf)
1051{
1052 return 0;
1053}
1054
1055static int uas_reset_resume(struct usb_interface *intf)
1056{
1057 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001058 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede0df1f662013-11-07 08:47:05 +01001059 unsigned long flags;
1060
1061 if (uas_configure_endpoints(devinfo) != 0) {
1062 shost_printk(KERN_ERR, shost,
1063 "%s: alloc streams error after reset", __func__);
1064 return -EIO;
1065 }
1066
1067 spin_lock_irqsave(shost->host_lock, flags);
1068 scsi_report_bus_reset(shost, 0);
1069 spin_unlock_irqrestore(shost->host_lock, flags);
1070
1071 return 0;
1072}
1073
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001074static void uas_disconnect(struct usb_interface *intf)
1075{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001076 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001077 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goedeb7b5d112014-09-13 12:26:30 +02001078 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001079
Hans de Goedeb7b5d112014-09-13 12:26:30 +02001080 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001081 devinfo->resetting = 1;
Hans de Goedeb7b5d112014-09-13 12:26:30 +02001082 spin_unlock_irqrestore(&devinfo->lock, flags);
1083
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001084 cancel_work_sync(&devinfo->work);
Hans de Goede673331c2013-11-14 14:27:27 +01001085 uas_abort_inflight(devinfo, DID_NO_CONNECT, __func__);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001086 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001087 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1088 usb_kill_anchored_urbs(&devinfo->data_urbs);
Gerd Hoffmann326349f2013-09-13 13:27:13 +02001089 uas_zap_dead(devinfo);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001090 scsi_remove_host(shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001091 uas_free_streams(devinfo);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001092 scsi_host_put(shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001093}
1094
Hans de Goededa65c2b2013-11-11 11:51:42 +01001095/*
1096 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1097 * hang on reboot when the device is still in uas mode. Note the reset is
1098 * necessary as some devices won't revert to usb-storage mode without it.
1099 */
1100static void uas_shutdown(struct device *dev)
1101{
1102 struct usb_interface *intf = to_usb_interface(dev);
1103 struct usb_device *udev = interface_to_usbdev(intf);
1104 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001105 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goededa65c2b2013-11-11 11:51:42 +01001106
1107 if (system_state != SYSTEM_RESTART)
1108 return;
1109
1110 devinfo->shutdown = 1;
1111 uas_free_streams(devinfo);
1112 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1113 usb_reset_device(udev);
1114}
1115
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001116static struct usb_driver uas_driver = {
1117 .name = "uas",
1118 .probe = uas_probe,
1119 .disconnect = uas_disconnect,
1120 .pre_reset = uas_pre_reset,
1121 .post_reset = uas_post_reset,
Hans de Goede0df1f662013-11-07 08:47:05 +01001122 .suspend = uas_suspend,
1123 .resume = uas_resume,
1124 .reset_resume = uas_reset_resume,
Hans de Goededa65c2b2013-11-11 11:51:42 +01001125 .drvwrap.driver.shutdown = uas_shutdown,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001126 .id_table = uas_usb_ids,
1127};
1128
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001129module_usb_driver(uas_driver);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001130
1131MODULE_LICENSE("GPL");
Hans de Goedef50a4962013-10-28 10:48:04 +00001132MODULE_AUTHOR(
1133 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");