blob: f09205b162e49ce69d9de409320ef46396538e89 [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 *
5 * Copyright Matthew Wilcox for Intel Corp, 2010
6 * Copyright Sarah Sharp for Intel Corp, 2010
7 *
8 * Distributed under the terms of the GNU GPL, version two.
9 */
10
11#include <linux/blkdev.h>
12#include <linux/slab.h>
13#include <linux/types.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040014#include <linux/module.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020015#include <linux/usb.h>
Hans de Goede79b4c062013-10-25 17:04:33 +010016#include <linux/usb_usual.h>
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +010017#include <linux/usb/hcd.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020018#include <linux/usb/storage.h>
Sebastian Andrzej Siewior348748b2012-01-11 12:45:56 +010019#include <linux/usb/uas.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020020
21#include <scsi/scsi.h>
Hans de Goede4de7a3732013-10-22 16:10:44 +010022#include <scsi/scsi_eh.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020023#include <scsi/scsi_dbg.h>
24#include <scsi/scsi_cmnd.h>
25#include <scsi/scsi_device.h>
26#include <scsi/scsi_host.h>
27#include <scsi/scsi_tcq.h>
28
Hans de Goede82aa0382013-10-21 08:53:31 +010029#include "uas-detect.h"
30
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020031/*
32 * The r00-r01c specs define this version of the SENSE IU data structure.
33 * It's still in use by several different firmware releases.
34 */
35struct sense_iu_old {
36 __u8 iu_id;
37 __u8 rsvd1;
38 __be16 tag;
39 __be16 len;
40 __u8 status;
41 __u8 service_response;
42 __u8 sense[SCSI_SENSE_BUFFERSIZE];
43};
44
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020045struct uas_dev_info {
46 struct usb_interface *intf;
47 struct usb_device *udev;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +020048 struct usb_anchor cmd_urbs;
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +020049 struct usb_anchor sense_urbs;
50 struct usb_anchor data_urbs;
Gerd Hoffmann023b5152012-06-19 09:54:54 +020051 int qdepth, resetting;
Hans de Goedee52e0312013-10-23 14:27:09 +010052 struct response_iu response;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020053 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
54 unsigned use_streams:1;
55 unsigned uas_sense_old:1;
Hans de Goedeb83b86a2013-10-29 10:51:00 +010056 unsigned running_task:1;
Hans de Goededa65c2b2013-11-11 11:51:42 +010057 unsigned shutdown:1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +010058 struct scsi_cmnd *cmnd;
Gerd Hoffmanne0648522012-09-25 10:47:08 +020059 spinlock_t lock;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +020060 struct work_struct work;
Hans de Goede61c09ce2013-11-12 13:44:20 +010061 struct list_head inflight_list;
Gerd Hoffmann326349f2013-09-13 13:27:13 +020062 struct list_head dead_list;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020063};
64
65enum {
Matthew Wilcox92a3f762010-12-15 15:44:04 -050066 SUBMIT_STATUS_URB = (1 << 1),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020067 ALLOC_DATA_IN_URB = (1 << 2),
68 SUBMIT_DATA_IN_URB = (1 << 3),
69 ALLOC_DATA_OUT_URB = (1 << 4),
70 SUBMIT_DATA_OUT_URB = (1 << 5),
71 ALLOC_CMD_URB = (1 << 6),
72 SUBMIT_CMD_URB = (1 << 7),
Gerd Hoffmannb1d67692012-06-19 09:54:51 +020073 COMMAND_INFLIGHT = (1 << 8),
74 DATA_IN_URB_INFLIGHT = (1 << 9),
75 DATA_OUT_URB_INFLIGHT = (1 << 10),
76 COMMAND_COMPLETED = (1 << 11),
Gerd Hoffmannef018cc92012-09-25 10:47:06 +020077 COMMAND_ABORTED = (1 << 12),
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +010078 UNLINK_DATA_URBS = (1 << 13),
Gerd Hoffmannefefecf2012-11-30 11:54:42 +010079 IS_IN_WORK_LIST = (1 << 14),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020080};
81
82/* Overrides scsi_pointer */
83struct uas_cmd_info {
84 unsigned int state;
85 unsigned int stream;
86 struct urb *cmd_urb;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020087 struct urb *data_in_urb;
88 struct urb *data_out_urb;
Hans de Goede040d1a82013-11-12 14:02:12 +010089 struct list_head list;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020090};
91
92/* I hate forward declarations, but I actually have a loop */
93static int uas_submit_urbs(struct scsi_cmnd *cmnd,
94 struct uas_dev_info *devinfo, gfp_t gfp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -080095static void uas_do_work(struct work_struct *work);
Gerd Hoffmann4c456972012-11-30 11:54:44 +010096static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
Gerd Hoffmannd89bd832013-09-13 13:27:11 +020097static void uas_free_streams(struct uas_dev_info *devinfo);
Gerd Hoffmann326349f2013-09-13 13:27:13 +020098static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020099
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200100/* Must be called with devinfo->lock held, will temporary unlock the lock */
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100101static void uas_unlink_data_urbs(struct uas_dev_info *devinfo,
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200102 struct uas_cmd_info *cmdinfo,
103 unsigned long *lock_flags)
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100104{
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100105 /*
106 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
107 * (called by urb completion) doesn't release cmdinfo
108 * underneath us.
109 */
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100110 cmdinfo->state |= UNLINK_DATA_URBS;
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200111 spin_unlock_irqrestore(&devinfo->lock, *lock_flags);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100112
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100113 if (cmdinfo->data_in_urb)
114 usb_unlink_urb(cmdinfo->data_in_urb);
115 if (cmdinfo->data_out_urb)
116 usb_unlink_urb(cmdinfo->data_out_urb);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100117
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200118 spin_lock_irqsave(&devinfo->lock, *lock_flags);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100119 cmdinfo->state &= ~UNLINK_DATA_URBS;
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100120}
121
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200122static void uas_do_work(struct work_struct *work)
123{
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200124 struct uas_dev_info *devinfo =
125 container_of(work, struct uas_dev_info, work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200126 struct uas_cmd_info *cmdinfo;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200127 unsigned long flags;
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800128 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200129
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200130 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goede040d1a82013-11-12 14:02:12 +0100131 list_for_each_entry(cmdinfo, &devinfo->inflight_list, list) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200132 struct scsi_pointer *scp = (void *)cmdinfo;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200133 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
134 SCp);
Hans de Goede61c09ce2013-11-12 13:44:20 +0100135
136 if (!(cmdinfo->state & IS_IN_WORK_LIST))
137 continue;
138
Hans de Goedee36e6492013-11-07 08:35:55 +0100139 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
Hans de Goede61c09ce2013-11-12 13:44:20 +0100140 if (!err)
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100141 cmdinfo->state &= ~IS_IN_WORK_LIST;
Hans de Goede61c09ce2013-11-12 13:44:20 +0100142 else
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200143 schedule_work(&devinfo->work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200144 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200145 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200146}
147
Hans de Goededa3033e2013-11-12 13:57:24 +0100148static void uas_mark_cmd_dead(struct uas_dev_info *devinfo,
Hans de Goede673331c2013-11-14 14:27:27 +0100149 struct uas_cmd_info *cmdinfo,
150 int result, const char *caller)
Hans de Goededa3033e2013-11-12 13:57:24 +0100151{
152 struct scsi_pointer *scp = (void *)cmdinfo;
153 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
154
155 uas_log_cmd_state(cmnd, caller);
156 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
157 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
158 cmdinfo->state |= COMMAND_ABORTED;
159 cmdinfo->state &= ~IS_IN_WORK_LIST;
Hans de Goede673331c2013-11-14 14:27:27 +0100160 cmnd->result = result << 16;
Hans de Goede040d1a82013-11-12 14:02:12 +0100161 list_move_tail(&cmdinfo->list, &devinfo->dead_list);
Hans de Goededa3033e2013-11-12 13:57:24 +0100162}
163
Hans de Goede673331c2013-11-14 14:27:27 +0100164static void uas_abort_inflight(struct uas_dev_info *devinfo, int result,
165 const char *caller)
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100166{
167 struct uas_cmd_info *cmdinfo;
168 struct uas_cmd_info *temp;
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100169 unsigned long flags;
170
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100171 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goede040d1a82013-11-12 14:02:12 +0100172 list_for_each_entry_safe(cmdinfo, temp, &devinfo->inflight_list, list)
Hans de Goede673331c2013-11-14 14:27:27 +0100173 uas_mark_cmd_dead(devinfo, cmdinfo, result, caller);
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100174 spin_unlock_irqrestore(&devinfo->lock, flags);
175}
176
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200177static void uas_add_work(struct uas_cmd_info *cmdinfo)
178{
179 struct scsi_pointer *scp = (void *)cmdinfo;
180 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
181 struct uas_dev_info *devinfo = cmnd->device->hostdata;
182
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200183 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200184 cmdinfo->state |= IS_IN_WORK_LIST;
185 schedule_work(&devinfo->work);
186}
187
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200188static void uas_zap_dead(struct uas_dev_info *devinfo)
189{
190 struct uas_cmd_info *cmdinfo;
191 struct uas_cmd_info *temp;
192 unsigned long flags;
193
194 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goede040d1a82013-11-12 14:02:12 +0100195 list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, list) {
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200196 struct scsi_pointer *scp = (void *)cmdinfo;
197 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
198 SCp);
199 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200200 WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED));
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200201 /* all urbs are killed, clear inflight bits */
202 cmdinfo->state &= ~(COMMAND_INFLIGHT |
203 DATA_IN_URB_INFLIGHT |
204 DATA_OUT_URB_INFLIGHT);
205 uas_try_complete(cmnd, __func__);
206 }
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100207 devinfo->running_task = 0;
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200208 spin_unlock_irqrestore(&devinfo->lock, flags);
209}
210
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200211static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
212{
213 struct sense_iu *sense_iu = urb->transfer_buffer;
214 struct scsi_device *sdev = cmnd->device;
215
216 if (urb->actual_length > 16) {
217 unsigned len = be16_to_cpup(&sense_iu->len);
218 if (len + 16 != urb->actual_length) {
219 int newlen = min(len + 16, urb->actual_length) - 16;
220 if (newlen < 0)
221 newlen = 0;
222 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
223 "disagrees with IU sense data length %d, "
224 "using %d bytes of sense data\n", __func__,
225 urb->actual_length, len, newlen);
226 len = newlen;
227 }
228 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
229 }
230
231 cmnd->result = sense_iu->status;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200232}
233
234static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
235{
236 struct sense_iu_old *sense_iu = urb->transfer_buffer;
237 struct scsi_device *sdev = cmnd->device;
238
239 if (urb->actual_length > 8) {
240 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
241 if (len + 8 != urb->actual_length) {
242 int newlen = min(len + 8, urb->actual_length) - 8;
243 if (newlen < 0)
244 newlen = 0;
245 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
246 "disagrees with IU sense data length %d, "
247 "using %d bytes of sense data\n", __func__,
248 urb->actual_length, len, newlen);
249 len = newlen;
250 }
251 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
252 }
253
254 cmnd->result = sense_iu->status;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200255}
256
257static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
258{
259 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
260
261 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100262 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200263 caller, cmnd, cmnd->request->tag,
264 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
265 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
266 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
267 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
268 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
269 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
270 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
271 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
272 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
273 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
Gerd Hoffmannef018cc92012-09-25 10:47:06 +0200274 (ci->state & COMMAND_COMPLETED) ? " done" : "",
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100275 (ci->state & COMMAND_ABORTED) ? " abort" : "",
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100276 (ci->state & UNLINK_DATA_URBS) ? " unlink": "",
277 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200278}
279
280static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
281{
282 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200283 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200284
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200285 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200286 if (cmdinfo->state & (COMMAND_INFLIGHT |
287 DATA_IN_URB_INFLIGHT |
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100288 DATA_OUT_URB_INFLIGHT |
289 UNLINK_DATA_URBS))
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200290 return -EBUSY;
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200291 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200292 cmdinfo->state |= COMMAND_COMPLETED;
293 usb_free_urb(cmdinfo->data_in_urb);
294 usb_free_urb(cmdinfo->data_out_urb);
Hans de Goede673331c2013-11-14 14:27:27 +0100295 if (cmdinfo->state & COMMAND_ABORTED)
Gerd Hoffmann0871d7d2012-09-25 10:47:07 +0200296 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
Hans de Goede040d1a82013-11-12 14:02:12 +0100297 list_del(&cmdinfo->list);
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200298 cmnd->scsi_done(cmnd);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200299 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200300}
301
302static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200303 unsigned direction)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200304{
305 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
306 int err;
307
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200308 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200309 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
310 if (err) {
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200311 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200312 }
313}
314
315static void uas_stat_cmplt(struct urb *urb)
316{
317 struct iu *iu = urb->transfer_buffer;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100318 struct Scsi_Host *shost = urb->context;
Hans de Goede21fc05b2013-11-13 09:32:22 +0100319 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200320 struct scsi_cmnd *cmnd;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200321 struct uas_cmd_info *cmdinfo;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200322 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200323 u16 tag;
324
325 if (urb->status) {
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200326 if (urb->status == -ENOENT) {
327 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
328 urb->stream_id);
329 } else {
330 dev_err(&urb->dev->dev, "stat urb: status %d\n",
331 urb->status);
332 }
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200333 usb_free_urb(urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200334 return;
335 }
336
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200337 if (devinfo->resetting) {
338 usb_free_urb(urb);
339 return;
340 }
341
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200342 spin_lock_irqsave(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200343 tag = be16_to_cpup(&iu->tag) - 1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100344 if (tag == 0)
345 cmnd = devinfo->cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200346 else
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100347 cmnd = scsi_host_find_tag(shost, tag - 1);
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200348
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800349 if (!cmnd) {
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200350 if (iu->iu_id == IU_ID_RESPONSE) {
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100351 if (!devinfo->running_task)
352 dev_warn(&urb->dev->dev,
353 "stat urb: recv unexpected response iu\n");
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200354 /* store results for uas_eh_task_mgmt() */
355 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200356 }
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200357 usb_free_urb(urb);
358 spin_unlock_irqrestore(&devinfo->lock, flags);
359 return;
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800360 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200361
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200362 cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200363 switch (iu->iu_id) {
364 case IU_ID_STATUS:
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100365 if (devinfo->cmnd == cmnd)
366 devinfo->cmnd = NULL;
367
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200368 if (urb->actual_length < 16)
369 devinfo->uas_sense_old = 1;
370 if (devinfo->uas_sense_old)
371 uas_sense_old(urb, cmnd);
372 else
373 uas_sense(urb, cmnd);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200374 if (cmnd->result != 0) {
375 /* cancel data transfers on error */
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200376 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200377 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200378 cmdinfo->state &= ~COMMAND_INFLIGHT;
379 uas_try_complete(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200380 break;
381 case IU_ID_READ_READY:
382 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
383 break;
384 case IU_ID_WRITE_READY:
385 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
386 break;
387 default:
388 scmd_printk(KERN_ERR, cmnd,
389 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
390 }
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200391 usb_free_urb(urb);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200392 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200393}
394
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200395static void uas_data_cmplt(struct urb *urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200396{
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200397 struct scsi_cmnd *cmnd = urb->context;
398 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200399 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200400 struct scsi_data_buffer *sdb = NULL;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200401 unsigned long flags;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200402
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200403 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200404 if (cmdinfo->data_in_urb == urb) {
405 sdb = scsi_in(cmnd);
406 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
407 } else if (cmdinfo->data_out_urb == urb) {
408 sdb = scsi_out(cmnd);
409 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
410 }
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200411 if (sdb == NULL) {
412 WARN_ON_ONCE(1);
413 } else if (urb->status) {
Hans de Goede876285c2013-11-07 08:52:42 +0100414 if (urb->status != -ECONNRESET) {
415 uas_log_cmd_state(cmnd, __func__);
416 scmd_printk(KERN_ERR, cmnd,
417 "data cmplt err %d stream %d\n",
418 urb->status, urb->stream_id);
419 }
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200420 /* error: no data transfered */
421 sdb->resid = sdb->length;
422 } else {
423 sdb->resid = sdb->length - urb->actual_length;
424 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200425 uas_try_complete(cmnd, __func__);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200426 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200427}
428
Hans de Goede876285c2013-11-07 08:52:42 +0100429static void uas_cmd_cmplt(struct urb *urb)
430{
431 struct scsi_cmnd *cmnd = urb->context;
432
433 if (urb->status) {
434 uas_log_cmd_state(cmnd, __func__);
435 scmd_printk(KERN_ERR, cmnd, "cmd cmplt err %d\n", urb->status);
436 }
437 usb_free_urb(urb);
438}
439
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200440static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200441 unsigned int pipe, u16 stream_id,
442 struct scsi_cmnd *cmnd,
443 enum dma_data_direction dir)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200444{
445 struct usb_device *udev = devinfo->udev;
446 struct urb *urb = usb_alloc_urb(0, gfp);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200447 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
448 ? scsi_in(cmnd) : scsi_out(cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200449
450 if (!urb)
451 goto out;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200452 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
453 uas_data_cmplt, cmnd);
Hans de Goedec6d45792013-11-12 10:53:57 +0100454 urb->stream_id = stream_id;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200455 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
456 urb->sg = sdb->table.sgl;
457 out:
458 return urb;
459}
460
461static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200462 struct Scsi_Host *shost, u16 stream_id)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200463{
464 struct usb_device *udev = devinfo->udev;
465 struct urb *urb = usb_alloc_urb(0, gfp);
466 struct sense_iu *iu;
467
468 if (!urb)
469 goto out;
470
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500471 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200472 if (!iu)
473 goto free;
474
475 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200476 uas_stat_cmplt, shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200477 urb->stream_id = stream_id;
478 urb->transfer_flags |= URB_FREE_BUFFER;
479 out:
480 return urb;
481 free:
482 usb_free_urb(urb);
483 return NULL;
484}
485
486static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Hans de Goedea887cd32013-10-17 19:47:28 +0200487 struct scsi_cmnd *cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200488{
489 struct usb_device *udev = devinfo->udev;
490 struct scsi_device *sdev = cmnd->device;
491 struct urb *urb = usb_alloc_urb(0, gfp);
492 struct command_iu *iu;
493 int len;
494
495 if (!urb)
496 goto out;
497
498 len = cmnd->cmd_len - 16;
499 if (len < 0)
500 len = 0;
501 len = ALIGN(len, 4);
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500502 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200503 if (!iu)
504 goto free;
505
506 iu->iu_id = IU_ID_COMMAND;
Sarah Sharp9eb44542011-12-02 11:55:46 -0800507 if (blk_rq_tagged(cmnd->request))
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100508 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
Sarah Sharp9eb44542011-12-02 11:55:46 -0800509 else
510 iu->tag = cpu_to_be16(1);
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100511 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200512 iu->len = len;
513 int_to_scsilun(sdev->lun, &iu->lun);
514 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
515
516 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
Hans de Goede876285c2013-11-07 08:52:42 +0100517 uas_cmd_cmplt, cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200518 urb->transfer_flags |= URB_FREE_BUFFER;
519 out:
520 return urb;
521 free:
522 usb_free_urb(urb);
523 return NULL;
524}
525
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200526static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
527 u8 function, u16 stream_id)
528{
529 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
530 struct usb_device *udev = devinfo->udev;
531 struct urb *urb = usb_alloc_urb(0, gfp);
532 struct task_mgmt_iu *iu;
533 int err = -ENOMEM;
534
535 if (!urb)
536 goto err;
537
538 iu = kzalloc(sizeof(*iu), gfp);
539 if (!iu)
540 goto err;
541
542 iu->iu_id = IU_ID_TASK_MGMT;
543 iu->tag = cpu_to_be16(stream_id);
544 int_to_scsilun(cmnd->device->lun, &iu->lun);
545
546 iu->function = function;
547 switch (function) {
548 case TMF_ABORT_TASK:
549 if (blk_rq_tagged(cmnd->request))
550 iu->task_tag = cpu_to_be16(cmnd->request->tag + 2);
551 else
552 iu->task_tag = cpu_to_be16(1);
553 break;
554 }
555
556 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
Hans de Goede876285c2013-11-07 08:52:42 +0100557 uas_cmd_cmplt, cmnd);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200558 urb->transfer_flags |= URB_FREE_BUFFER;
559
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200560 usb_anchor_urb(urb, &devinfo->cmd_urbs);
Hans de Goeded5f808d2013-10-17 19:30:26 +0200561 err = usb_submit_urb(urb, gfp);
562 if (err) {
563 usb_unanchor_urb(urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100564 uas_log_cmd_state(cmnd, __func__);
565 scmd_printk(KERN_ERR, cmnd, "task submission err %d\n", err);
Hans de Goeded5f808d2013-10-17 19:30:26 +0200566 goto err;
567 }
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200568
569 return 0;
570
571err:
572 usb_free_urb(urb);
573 return err;
574}
575
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200576/*
577 * Why should I request the Status IU before sending the Command IU? Spec
578 * says to, but also says the device may receive them in any order. Seems
579 * daft to me.
580 */
581
Hans de Goede876285c2013-11-07 08:52:42 +0100582static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd,
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100583 gfp_t gfp, unsigned int stream)
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200584{
Hans de Goede876285c2013-11-07 08:52:42 +0100585 struct Scsi_Host *shost = cmnd->device->host;
Hans de Goede21fc05b2013-11-13 09:32:22 +0100586 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200587 struct urb *urb;
Hans de Goede876285c2013-11-07 08:52:42 +0100588 int err;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200589
590 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
591 if (!urb)
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100592 return NULL;
Hans de Goeded5f808d2013-10-17 19:30:26 +0200593 usb_anchor_urb(urb, &devinfo->sense_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100594 err = usb_submit_urb(urb, gfp);
595 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200596 usb_unanchor_urb(urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100597 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200598 shost_printk(KERN_INFO, shost,
Hans de Goede876285c2013-11-07 08:52:42 +0100599 "sense urb submission error %d stream %d\n",
600 err, stream);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200601 usb_free_urb(urb);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100602 return NULL;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200603 }
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100604 return urb;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200605}
606
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200607static int uas_submit_urbs(struct scsi_cmnd *cmnd,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200608 struct uas_dev_info *devinfo, gfp_t gfp)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200609{
610 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100611 struct urb *urb;
Hans de Goede876285c2013-11-07 08:52:42 +0100612 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200613
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200614 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500615 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Hans de Goede876285c2013-11-07 08:52:42 +0100616 urb = uas_submit_sense_urb(cmnd, gfp, cmdinfo->stream);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100617 if (!urb)
618 return SCSI_MLQUEUE_DEVICE_BUSY;
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500619 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200620 }
621
622 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
623 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200624 devinfo->data_in_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200625 cmnd, DMA_FROM_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200626 if (!cmdinfo->data_in_urb)
627 return SCSI_MLQUEUE_DEVICE_BUSY;
628 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
629 }
630
631 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200632 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100633 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
634 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200635 usb_unanchor_urb(cmdinfo->data_in_urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100636 uas_log_cmd_state(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200637 scmd_printk(KERN_INFO, cmnd,
Hans de Goede876285c2013-11-07 08:52:42 +0100638 "data in urb submission error %d stream %d\n",
639 err, cmdinfo->data_in_urb->stream_id);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200640 return SCSI_MLQUEUE_DEVICE_BUSY;
641 }
642 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200643 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200644 }
645
646 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
647 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200648 devinfo->data_out_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200649 cmnd, DMA_TO_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200650 if (!cmdinfo->data_out_urb)
651 return SCSI_MLQUEUE_DEVICE_BUSY;
652 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
653 }
654
655 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200656 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100657 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
658 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200659 usb_unanchor_urb(cmdinfo->data_out_urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100660 uas_log_cmd_state(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200661 scmd_printk(KERN_INFO, cmnd,
Hans de Goede876285c2013-11-07 08:52:42 +0100662 "data out urb submission error %d stream %d\n",
663 err, cmdinfo->data_out_urb->stream_id);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200664 return SCSI_MLQUEUE_DEVICE_BUSY;
665 }
666 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200667 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200668 }
669
670 if (cmdinfo->state & ALLOC_CMD_URB) {
Hans de Goedea887cd32013-10-17 19:47:28 +0200671 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200672 if (!cmdinfo->cmd_urb)
673 return SCSI_MLQUEUE_DEVICE_BUSY;
674 cmdinfo->state &= ~ALLOC_CMD_URB;
675 }
676
677 if (cmdinfo->state & SUBMIT_CMD_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200678 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100679 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
680 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200681 usb_unanchor_urb(cmdinfo->cmd_urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100682 uas_log_cmd_state(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200683 scmd_printk(KERN_INFO, cmnd,
Hans de Goede876285c2013-11-07 08:52:42 +0100684 "cmd urb submission error %d\n", err);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200685 return SCSI_MLQUEUE_DEVICE_BUSY;
686 }
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200687 cmdinfo->cmd_urb = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200688 cmdinfo->state &= ~SUBMIT_CMD_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200689 cmdinfo->state |= COMMAND_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200690 }
691
692 return 0;
693}
694
Jeff Garzikf2812332010-11-16 02:10:29 -0500695static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200696 void (*done)(struct scsi_cmnd *))
697{
698 struct scsi_device *sdev = cmnd->device;
699 struct uas_dev_info *devinfo = sdev->hostdata;
700 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200701 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200702 int err;
703
704 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
705
Hans de Goedec6f63202013-11-13 09:24:15 +0100706 spin_lock_irqsave(&devinfo->lock, flags);
707
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100708 if (devinfo->resetting) {
709 cmnd->result = DID_ERROR << 16;
710 cmnd->scsi_done(cmnd);
Hans de Goedec6f63202013-11-13 09:24:15 +0100711 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100712 return 0;
713 }
714
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200715 if (devinfo->cmnd) {
716 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200717 return SCSI_MLQUEUE_DEVICE_BUSY;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200718 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200719
Hans de Goede3a4462e2013-11-14 11:06:13 +0100720 memset(cmdinfo, 0, sizeof(*cmdinfo));
721
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200722 if (blk_rq_tagged(cmnd->request)) {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100723 cmdinfo->stream = cmnd->request->tag + 2;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200724 } else {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100725 devinfo->cmnd = cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200726 cmdinfo->stream = 1;
727 }
728
729 cmnd->scsi_done = done;
730
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200731 cmdinfo->state = SUBMIT_STATUS_URB |
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200732 ALLOC_CMD_URB | SUBMIT_CMD_URB;
733
734 switch (cmnd->sc_data_direction) {
735 case DMA_FROM_DEVICE:
736 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
737 break;
738 case DMA_BIDIRECTIONAL:
739 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
740 case DMA_TO_DEVICE:
741 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
742 case DMA_NONE:
743 break;
744 }
745
746 if (!devinfo->use_streams) {
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200747 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200748 cmdinfo->stream = 0;
749 }
750
751 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
752 if (err) {
753 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500754 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200755 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200756 return SCSI_MLQUEUE_DEVICE_BUSY;
757 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200758 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200759 }
760
Hans de Goede040d1a82013-11-12 14:02:12 +0100761 list_add_tail(&cmdinfo->list, &devinfo->inflight_list);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200762 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200763 return 0;
764}
765
Jeff Garzikf2812332010-11-16 02:10:29 -0500766static DEF_SCSI_QCMD(uas_queuecommand)
767
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200768static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
769 const char *fname, u8 function)
770{
771 struct Scsi_Host *shost = cmnd->device->host;
Hans de Goede21fc05b2013-11-13 09:32:22 +0100772 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goeded3f7c152013-10-23 17:46:17 +0100773 u16 tag = devinfo->qdepth;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200774 unsigned long flags;
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100775 struct urb *sense_urb;
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100776 int result = SUCCESS;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200777
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200778 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100779
Hans de Goedec6f63202013-11-13 09:24:15 +0100780 if (devinfo->resetting) {
781 spin_unlock_irqrestore(&devinfo->lock, flags);
782 return FAILED;
783 }
784
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100785 if (devinfo->running_task) {
786 shost_printk(KERN_INFO, shost,
787 "%s: %s: error already running a task\n",
788 __func__, fname);
789 spin_unlock_irqrestore(&devinfo->lock, flags);
790 return FAILED;
791 }
792
793 devinfo->running_task = 1;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200794 memset(&devinfo->response, 0, sizeof(devinfo->response));
Hans de Goede876285c2013-11-07 08:52:42 +0100795 sense_urb = uas_submit_sense_urb(cmnd, GFP_NOIO,
Hans de Goedef323abc2013-11-12 10:51:33 +0100796 devinfo->use_streams ? tag : 0);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100797 if (!sense_urb) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200798 shost_printk(KERN_INFO, shost,
799 "%s: %s: submit sense urb failed\n",
800 __func__, fname);
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100801 devinfo->running_task = 0;
Gerd Hoffmann1994ff42012-09-26 10:28:58 +0200802 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200803 return FAILED;
804 }
Hans de Goedee36e6492013-11-07 08:35:55 +0100805 if (uas_submit_task_urb(cmnd, GFP_NOIO, function, tag)) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200806 shost_printk(KERN_INFO, shost,
807 "%s: %s: submit task mgmt urb failed\n",
808 __func__, fname);
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100809 devinfo->running_task = 0;
Gerd Hoffmann1994ff42012-09-26 10:28:58 +0200810 spin_unlock_irqrestore(&devinfo->lock, flags);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100811 usb_kill_urb(sense_urb);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200812 return FAILED;
813 }
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200814 spin_unlock_irqrestore(&devinfo->lock, flags);
815
816 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) {
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100817 /*
818 * Note we deliberately do not clear running_task here. If we
819 * allow new tasks to be submitted, there is no way to figure
820 * out if a received response_iu is for the failed task or for
821 * the new one. A bus-reset will eventually clear running_task.
822 */
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200823 shost_printk(KERN_INFO, shost,
824 "%s: %s timed out\n", __func__, fname);
825 return FAILED;
826 }
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100827
828 spin_lock_irqsave(&devinfo->lock, flags);
829 devinfo->running_task = 0;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200830 if (be16_to_cpu(devinfo->response.tag) != tag) {
831 shost_printk(KERN_INFO, shost,
832 "%s: %s failed (wrong tag %d/%d)\n", __func__,
833 fname, be16_to_cpu(devinfo->response.tag), tag);
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100834 result = FAILED;
835 } else if (devinfo->response.response_code != RC_TMF_COMPLETE) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200836 shost_printk(KERN_INFO, shost,
837 "%s: %s failed (rc 0x%x)\n", __func__,
838 fname, devinfo->response.response_code);
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100839 result = FAILED;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200840 }
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100841 spin_unlock_irqrestore(&devinfo->lock, flags);
842
843 return result;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200844}
845
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200846static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
847{
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200848 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200849 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
850 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200851 int ret;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200852
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200853 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goedec6f63202013-11-13 09:24:15 +0100854
855 if (devinfo->resetting) {
856 spin_unlock_irqrestore(&devinfo->lock, flags);
857 return FAILED;
858 }
859
Hans de Goede673331c2013-11-14 14:27:27 +0100860 uas_mark_cmd_dead(devinfo, cmdinfo, DID_ABORT, __func__);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100861 if (cmdinfo->state & COMMAND_INFLIGHT) {
862 spin_unlock_irqrestore(&devinfo->lock, flags);
863 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
864 } else {
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200865 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100866 uas_try_complete(cmnd, __func__);
867 spin_unlock_irqrestore(&devinfo->lock, flags);
868 ret = SUCCESS;
869 }
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200870 return ret;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200871}
872
873static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
874{
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200875 sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__);
876 return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET",
877 TMF_LOGICAL_UNIT_RESET);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200878}
879
880static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
881{
882 struct scsi_device *sdev = cmnd->device;
883 struct uas_dev_info *devinfo = sdev->hostdata;
884 struct usb_device *udev = devinfo->udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200885 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200886
Hans de Goedebe326f42013-09-22 16:27:02 +0200887 err = usb_lock_device_for_reset(udev, devinfo->intf);
888 if (err) {
889 shost_printk(KERN_ERR, sdev->host,
890 "%s FAILED to get lock err %d\n", __func__, err);
891 return FAILED;
892 }
893
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200894 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200895 devinfo->resetting = 1;
Hans de Goede673331c2013-11-14 14:27:27 +0100896 uas_abort_inflight(devinfo, DID_RESET, __func__);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200897 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200898 usb_kill_anchored_urbs(&devinfo->sense_urbs);
899 usb_kill_anchored_urbs(&devinfo->data_urbs);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200900 uas_zap_dead(devinfo);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200901 err = usb_reset_device(udev);
902 devinfo->resetting = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200903
Hans de Goedebe326f42013-09-22 16:27:02 +0200904 usb_unlock_device(udev);
905
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200906 if (err) {
907 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
908 return FAILED;
909 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200910
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200911 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
912 return SUCCESS;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200913}
914
915static int uas_slave_alloc(struct scsi_device *sdev)
916{
Hans de Goede21fc05b2013-11-13 09:32:22 +0100917 sdev->hostdata = (void *)sdev->host->hostdata;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200918 return 0;
919}
920
921static int uas_slave_configure(struct scsi_device *sdev)
922{
923 struct uas_dev_info *devinfo = sdev->hostdata;
924 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
Hans de Goeded3f7c152013-10-23 17:46:17 +0100925 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200926 return 0;
927}
928
929static struct scsi_host_template uas_host_template = {
930 .module = THIS_MODULE,
931 .name = "uas",
932 .queuecommand = uas_queuecommand,
933 .slave_alloc = uas_slave_alloc,
934 .slave_configure = uas_slave_configure,
935 .eh_abort_handler = uas_eh_abort_handler,
936 .eh_device_reset_handler = uas_eh_device_reset_handler,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200937 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
938 .can_queue = 65536, /* Is there a limit on the _host_ ? */
939 .this_id = -1,
940 .sg_tablesize = SG_NONE,
941 .cmd_per_lun = 1, /* until we override it */
942 .skip_settle_delay = 1,
943 .ordered_tag = 1,
944};
945
Hans de Goede79b4c062013-10-25 17:04:33 +0100946#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
947 vendorName, productName, useProtocol, useTransport, \
948 initFunction, flags) \
949{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
950 .driver_info = (flags) }
951
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200952static struct usb_device_id uas_usb_ids[] = {
Hans de Goede79b4c062013-10-25 17:04:33 +0100953# include "unusual_uas.h"
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200954 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
955 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
956 /* 0xaa is a prototype device I happen to have access to */
957 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
958 { }
959};
960MODULE_DEVICE_TABLE(usb, uas_usb_ids);
961
Hans de Goede79b4c062013-10-25 17:04:33 +0100962#undef UNUSUAL_DEV
963
Hans de Goedee1be0672013-10-21 08:00:58 +0100964static int uas_switch_interface(struct usb_device *udev,
965 struct usb_interface *intf)
966{
967 int alt;
968
969 alt = uas_find_uas_alt_setting(intf);
970 if (alt < 0)
971 return alt;
972
973 return usb_set_interface(udev,
974 intf->altsetting[0].desc.bInterfaceNumber, alt);
975}
976
Hans de Goede58d51442013-10-29 10:23:26 +0100977static int uas_configure_endpoints(struct uas_dev_info *devinfo)
Hans de Goede34f11e52013-10-29 08:54:48 +0100978{
979 struct usb_host_endpoint *eps[4] = { };
980 struct usb_device *udev = devinfo->udev;
981 int r;
982
983 devinfo->uas_sense_old = 0;
984 devinfo->cmnd = NULL;
985
986 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
Hans de Goede74d71ae2013-10-29 10:10:36 +0100987 if (r)
988 return r;
Hans de Goede34f11e52013-10-29 08:54:48 +0100989
Hans de Goede74d71ae2013-10-29 10:10:36 +0100990 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
991 usb_endpoint_num(&eps[0]->desc));
992 devinfo->status_pipe = usb_rcvbulkpipe(udev,
993 usb_endpoint_num(&eps[1]->desc));
994 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
995 usb_endpoint_num(&eps[2]->desc));
996 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
997 usb_endpoint_num(&eps[3]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200998
Hans de Goede58d51442013-10-29 10:23:26 +0100999 if (udev->speed != USB_SPEED_SUPER) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001000 devinfo->qdepth = 256;
1001 devinfo->use_streams = 0;
1002 } else {
Hans de Goede58d51442013-10-29 10:23:26 +01001003 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
1004 3, 256, GFP_KERNEL);
1005 if (devinfo->qdepth < 0)
1006 return devinfo->qdepth;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001007 devinfo->use_streams = 1;
1008 }
Hans de Goede58d51442013-10-29 10:23:26 +01001009
1010 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001011}
1012
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001013static void uas_free_streams(struct uas_dev_info *devinfo)
1014{
1015 struct usb_device *udev = devinfo->udev;
1016 struct usb_host_endpoint *eps[3];
1017
1018 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
1019 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
1020 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
1021 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
1022}
1023
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001024/*
1025 * XXX: What I'd like to do here is register a SCSI host for each USB host in
1026 * the system. Follow usb-storage's design of registering a SCSI host for
1027 * each USB device for the moment. Can implement this by walking up the
1028 * USB hierarchy until we find a USB host.
1029 */
1030static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
1031{
Hans de Goede6ce82132013-10-17 19:00:45 +02001032 int result = -ENOMEM;
1033 struct Scsi_Host *shost = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001034 struct uas_dev_info *devinfo;
1035 struct usb_device *udev = interface_to_usbdev(intf);
1036
Hans de Goede79b4c062013-10-25 17:04:33 +01001037 if (!uas_use_uas_driver(intf, id))
1038 return -ENODEV;
1039
Matthew Wilcox89dc2902010-12-15 15:44:05 -05001040 if (uas_switch_interface(udev, intf))
1041 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001042
Hans de Goede21fc05b2013-11-13 09:32:22 +01001043 shost = scsi_host_alloc(&uas_host_template,
1044 sizeof(struct uas_dev_info));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001045 if (!shost)
Hans de Goede6ce82132013-10-17 19:00:45 +02001046 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001047
1048 shost->max_cmd_len = 16 + 252;
1049 shost->max_id = 1;
Gerd Hoffmannbde027b2013-01-25 15:03:36 +01001050 shost->max_lun = 256;
1051 shost->max_channel = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001052 shost->sg_tablesize = udev->bus->sg_tablesize;
1053
Hans de Goede21fc05b2013-11-13 09:32:22 +01001054 devinfo = (struct uas_dev_info *)shost->hostdata;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001055 devinfo->intf = intf;
1056 devinfo->udev = udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +02001057 devinfo->resetting = 0;
Hans de Goedeb83b86a2013-10-29 10:51:00 +01001058 devinfo->running_task = 0;
Hans de Goededa65c2b2013-11-11 11:51:42 +01001059 devinfo->shutdown = 0;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001060 init_usb_anchor(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001061 init_usb_anchor(&devinfo->sense_urbs);
1062 init_usb_anchor(&devinfo->data_urbs);
Gerd Hoffmanne0648522012-09-25 10:47:08 +02001063 spin_lock_init(&devinfo->lock);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001064 INIT_WORK(&devinfo->work, uas_do_work);
Hans de Goede61c09ce2013-11-12 13:44:20 +01001065 INIT_LIST_HEAD(&devinfo->inflight_list);
Gerd Hoffmann326349f2013-09-13 13:27:13 +02001066 INIT_LIST_HEAD(&devinfo->dead_list);
Hans de Goede58d51442013-10-29 10:23:26 +01001067
1068 result = uas_configure_endpoints(devinfo);
1069 if (result)
1070 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001071
Hans de Goeded3f7c152013-10-23 17:46:17 +01001072 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001073 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +02001074 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001075
1076 result = scsi_add_host(shost, &intf->dev);
1077 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +02001078 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001079
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001080 scsi_scan_host(shost);
1081 usb_set_intfdata(intf, shost);
1082 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001083
Hans de Goede6ce82132013-10-17 19:00:45 +02001084free_streams:
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001085 uas_free_streams(devinfo);
Hans de Goede6ce82132013-10-17 19:00:45 +02001086set_alt0:
1087 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001088 if (shost)
1089 scsi_host_put(shost);
1090 return result;
1091}
1092
1093static int uas_pre_reset(struct usb_interface *intf)
1094{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001095 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001096 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001097 unsigned long flags;
1098
Hans de Goededa65c2b2013-11-11 11:51:42 +01001099 if (devinfo->shutdown)
1100 return 0;
1101
Hans de Goede4de7a3732013-10-22 16:10:44 +01001102 /* Block new requests */
1103 spin_lock_irqsave(shost->host_lock, flags);
1104 scsi_block_requests(shost);
1105 spin_unlock_irqrestore(shost->host_lock, flags);
1106
1107 /* Wait for any pending requests to complete */
1108 flush_work(&devinfo->work);
1109 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1110 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1111 return 1;
1112 }
1113
1114 uas_free_streams(devinfo);
1115
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001116 return 0;
1117}
1118
1119static int uas_post_reset(struct usb_interface *intf)
1120{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001121 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001122 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001123 unsigned long flags;
1124
Hans de Goededa65c2b2013-11-11 11:51:42 +01001125 if (devinfo->shutdown)
1126 return 0;
1127
Hans de Goede58d51442013-10-29 10:23:26 +01001128 if (uas_configure_endpoints(devinfo) != 0) {
1129 shost_printk(KERN_ERR, shost,
1130 "%s: alloc streams error after reset", __func__);
1131 return 1;
1132 }
Hans de Goede4de7a3732013-10-22 16:10:44 +01001133
1134 spin_lock_irqsave(shost->host_lock, flags);
1135 scsi_report_bus_reset(shost, 0);
1136 spin_unlock_irqrestore(shost->host_lock, flags);
1137
1138 scsi_unblock_requests(shost);
1139
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001140 return 0;
1141}
1142
Hans de Goede0df1f662013-11-07 08:47:05 +01001143static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1144{
1145 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001146 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede0df1f662013-11-07 08:47:05 +01001147
1148 /* Wait for any pending requests to complete */
1149 flush_work(&devinfo->work);
1150 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1151 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1152 return -ETIME;
1153 }
1154
1155 return 0;
1156}
1157
1158static int uas_resume(struct usb_interface *intf)
1159{
1160 return 0;
1161}
1162
1163static int uas_reset_resume(struct usb_interface *intf)
1164{
1165 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001166 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede0df1f662013-11-07 08:47:05 +01001167 unsigned long flags;
1168
1169 if (uas_configure_endpoints(devinfo) != 0) {
1170 shost_printk(KERN_ERR, shost,
1171 "%s: alloc streams error after reset", __func__);
1172 return -EIO;
1173 }
1174
1175 spin_lock_irqsave(shost->host_lock, flags);
1176 scsi_report_bus_reset(shost, 0);
1177 spin_unlock_irqrestore(shost->host_lock, flags);
1178
1179 return 0;
1180}
1181
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001182static void uas_disconnect(struct usb_interface *intf)
1183{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001184 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001185 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001186
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001187 devinfo->resetting = 1;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001188 cancel_work_sync(&devinfo->work);
Hans de Goede673331c2013-11-14 14:27:27 +01001189 uas_abort_inflight(devinfo, DID_NO_CONNECT, __func__);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001190 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001191 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1192 usb_kill_anchored_urbs(&devinfo->data_urbs);
Gerd Hoffmann326349f2013-09-13 13:27:13 +02001193 uas_zap_dead(devinfo);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001194 scsi_remove_host(shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001195 uas_free_streams(devinfo);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001196 scsi_host_put(shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001197}
1198
Hans de Goededa65c2b2013-11-11 11:51:42 +01001199/*
1200 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1201 * hang on reboot when the device is still in uas mode. Note the reset is
1202 * necessary as some devices won't revert to usb-storage mode without it.
1203 */
1204static void uas_shutdown(struct device *dev)
1205{
1206 struct usb_interface *intf = to_usb_interface(dev);
1207 struct usb_device *udev = interface_to_usbdev(intf);
1208 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001209 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goededa65c2b2013-11-11 11:51:42 +01001210
1211 if (system_state != SYSTEM_RESTART)
1212 return;
1213
1214 devinfo->shutdown = 1;
1215 uas_free_streams(devinfo);
1216 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1217 usb_reset_device(udev);
1218}
1219
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001220static struct usb_driver uas_driver = {
1221 .name = "uas",
1222 .probe = uas_probe,
1223 .disconnect = uas_disconnect,
1224 .pre_reset = uas_pre_reset,
1225 .post_reset = uas_post_reset,
Hans de Goede0df1f662013-11-07 08:47:05 +01001226 .suspend = uas_suspend,
1227 .resume = uas_resume,
1228 .reset_resume = uas_reset_resume,
Hans de Goededa65c2b2013-11-11 11:51:42 +01001229 .drvwrap.driver.shutdown = uas_shutdown,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001230 .id_table = uas_usb_ids,
1231};
1232
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001233module_usb_driver(uas_driver);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001234
1235MODULE_LICENSE("GPL");
1236MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");