blob: 511b22953167bd99a611e7e5f7fc13b45e574e8c [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 Goedef50a4962013-10-28 10:48:04 +00005 * Copyright Hans de Goede <hdegoede@redhat.com> for Red Hat, Inc. 2013
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"
31
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020032/*
33 * The r00-r01c specs define this version of the SENSE IU data structure.
34 * It's still in use by several different firmware releases.
35 */
36struct sense_iu_old {
37 __u8 iu_id;
38 __u8 rsvd1;
39 __be16 tag;
40 __be16 len;
41 __u8 status;
42 __u8 service_response;
43 __u8 sense[SCSI_SENSE_BUFFERSIZE];
44};
45
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020046struct uas_dev_info {
47 struct usb_interface *intf;
48 struct usb_device *udev;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +020049 struct usb_anchor cmd_urbs;
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +020050 struct usb_anchor sense_urbs;
51 struct usb_anchor data_urbs;
Gerd Hoffmann023b5152012-06-19 09:54:54 +020052 int qdepth, resetting;
Hans de Goedee52e0312013-10-23 14:27:09 +010053 struct response_iu response;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020054 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
55 unsigned use_streams:1;
56 unsigned uas_sense_old:1;
Hans de Goedeb83b86a2013-10-29 10:51:00 +010057 unsigned running_task: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 Goede040d1a82013-11-12 14:02:12 +0100132 list_for_each_entry(cmdinfo, &devinfo->inflight_list, list) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200133 struct scsi_pointer *scp = (void *)cmdinfo;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200134 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
135 SCp);
Hans de Goede61c09ce2013-11-12 13:44:20 +0100136
137 if (!(cmdinfo->state & IS_IN_WORK_LIST))
138 continue;
139
Oliver Neukume7eda932014-03-28 11:10:30 +0100140 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
Hans de Goede61c09ce2013-11-12 13:44:20 +0100141 if (!err)
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100142 cmdinfo->state &= ~IS_IN_WORK_LIST;
Hans de Goede61c09ce2013-11-12 13:44:20 +0100143 else
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200144 schedule_work(&devinfo->work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200145 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200146 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200147}
148
Hans de Goededa3033e2013-11-12 13:57:24 +0100149static void uas_mark_cmd_dead(struct uas_dev_info *devinfo,
Hans de Goede673331c2013-11-14 14:27:27 +0100150 struct uas_cmd_info *cmdinfo,
151 int result, const char *caller)
Hans de Goededa3033e2013-11-12 13:57:24 +0100152{
153 struct scsi_pointer *scp = (void *)cmdinfo;
154 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
155
156 uas_log_cmd_state(cmnd, caller);
157 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
158 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
159 cmdinfo->state |= COMMAND_ABORTED;
160 cmdinfo->state &= ~IS_IN_WORK_LIST;
Hans de Goede673331c2013-11-14 14:27:27 +0100161 cmnd->result = result << 16;
Hans de Goede040d1a82013-11-12 14:02:12 +0100162 list_move_tail(&cmdinfo->list, &devinfo->dead_list);
Hans de Goededa3033e2013-11-12 13:57:24 +0100163}
164
Hans de Goede673331c2013-11-14 14:27:27 +0100165static void uas_abort_inflight(struct uas_dev_info *devinfo, int result,
166 const char *caller)
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100167{
168 struct uas_cmd_info *cmdinfo;
169 struct uas_cmd_info *temp;
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100170 unsigned long flags;
171
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100172 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goede040d1a82013-11-12 14:02:12 +0100173 list_for_each_entry_safe(cmdinfo, temp, &devinfo->inflight_list, list)
Hans de Goede673331c2013-11-14 14:27:27 +0100174 uas_mark_cmd_dead(devinfo, cmdinfo, result, caller);
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100175 spin_unlock_irqrestore(&devinfo->lock, flags);
176}
177
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200178static void uas_add_work(struct uas_cmd_info *cmdinfo)
179{
180 struct scsi_pointer *scp = (void *)cmdinfo;
181 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
182 struct uas_dev_info *devinfo = cmnd->device->hostdata;
183
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200184 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200185 cmdinfo->state |= IS_IN_WORK_LIST;
186 schedule_work(&devinfo->work);
187}
188
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200189static void uas_zap_dead(struct uas_dev_info *devinfo)
190{
191 struct uas_cmd_info *cmdinfo;
192 struct uas_cmd_info *temp;
193 unsigned long flags;
194
195 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goede040d1a82013-11-12 14:02:12 +0100196 list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, list) {
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200197 struct scsi_pointer *scp = (void *)cmdinfo;
198 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
199 SCp);
200 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200201 WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED));
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200202 /* all urbs are killed, clear inflight bits */
203 cmdinfo->state &= ~(COMMAND_INFLIGHT |
204 DATA_IN_URB_INFLIGHT |
205 DATA_OUT_URB_INFLIGHT);
206 uas_try_complete(cmnd, __func__);
207 }
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100208 devinfo->running_task = 0;
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200209 spin_unlock_irqrestore(&devinfo->lock, flags);
210}
211
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200212static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
213{
214 struct sense_iu *sense_iu = urb->transfer_buffer;
215 struct scsi_device *sdev = cmnd->device;
216
217 if (urb->actual_length > 16) {
218 unsigned len = be16_to_cpup(&sense_iu->len);
219 if (len + 16 != urb->actual_length) {
220 int newlen = min(len + 16, urb->actual_length) - 16;
221 if (newlen < 0)
222 newlen = 0;
223 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
224 "disagrees with IU sense data length %d, "
225 "using %d bytes of sense data\n", __func__,
226 urb->actual_length, len, newlen);
227 len = newlen;
228 }
229 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
230 }
231
232 cmnd->result = sense_iu->status;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200233}
234
235static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
236{
237 struct sense_iu_old *sense_iu = urb->transfer_buffer;
238 struct scsi_device *sdev = cmnd->device;
239
240 if (urb->actual_length > 8) {
241 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
242 if (len + 8 != urb->actual_length) {
243 int newlen = min(len + 8, urb->actual_length) - 8;
244 if (newlen < 0)
245 newlen = 0;
246 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
247 "disagrees with IU sense data length %d, "
248 "using %d bytes of sense data\n", __func__,
249 urb->actual_length, len, newlen);
250 len = newlen;
251 }
252 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
253 }
254
255 cmnd->result = sense_iu->status;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200256}
257
258static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
259{
260 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
261
262 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100263 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200264 caller, cmnd, cmnd->request->tag,
265 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
266 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
267 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
268 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
269 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
270 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
271 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
272 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
273 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
274 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
Gerd Hoffmannef018cc92012-09-25 10:47:06 +0200275 (ci->state & COMMAND_COMPLETED) ? " done" : "",
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100276 (ci->state & COMMAND_ABORTED) ? " abort" : "",
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100277 (ci->state & UNLINK_DATA_URBS) ? " unlink": "",
278 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200279}
280
281static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
282{
283 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200284 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200285
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200286 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200287 if (cmdinfo->state & (COMMAND_INFLIGHT |
288 DATA_IN_URB_INFLIGHT |
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100289 DATA_OUT_URB_INFLIGHT |
290 UNLINK_DATA_URBS))
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200291 return -EBUSY;
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200292 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200293 cmdinfo->state |= COMMAND_COMPLETED;
294 usb_free_urb(cmdinfo->data_in_urb);
295 usb_free_urb(cmdinfo->data_out_urb);
Hans de Goede673331c2013-11-14 14:27:27 +0100296 if (cmdinfo->state & COMMAND_ABORTED)
Gerd Hoffmann0871d7d2012-09-25 10:47:07 +0200297 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
Hans de Goede040d1a82013-11-12 14:02:12 +0100298 list_del(&cmdinfo->list);
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200299 cmnd->scsi_done(cmnd);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200300 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200301}
302
303static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200304 unsigned direction)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200305{
306 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
307 int err;
308
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200309 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200310 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
311 if (err) {
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200312 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200313 }
314}
315
316static void uas_stat_cmplt(struct urb *urb)
317{
318 struct iu *iu = urb->transfer_buffer;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100319 struct Scsi_Host *shost = urb->context;
Hans de Goede21fc05b2013-11-13 09:32:22 +0100320 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200321 struct scsi_cmnd *cmnd;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200322 struct uas_cmd_info *cmdinfo;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200323 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200324 u16 tag;
325
326 if (urb->status) {
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200327 if (urb->status == -ENOENT) {
328 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
329 urb->stream_id);
330 } else {
331 dev_err(&urb->dev->dev, "stat urb: status %d\n",
332 urb->status);
333 }
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200334 usb_free_urb(urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200335 return;
336 }
337
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200338 if (devinfo->resetting) {
339 usb_free_urb(urb);
340 return;
341 }
342
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200343 spin_lock_irqsave(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200344 tag = be16_to_cpup(&iu->tag) - 1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100345 if (tag == 0)
346 cmnd = devinfo->cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200347 else
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100348 cmnd = scsi_host_find_tag(shost, tag - 1);
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200349
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800350 if (!cmnd) {
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200351 if (iu->iu_id == IU_ID_RESPONSE) {
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100352 if (!devinfo->running_task)
353 dev_warn(&urb->dev->dev,
354 "stat urb: recv unexpected response iu\n");
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200355 /* store results for uas_eh_task_mgmt() */
356 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200357 }
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200358 usb_free_urb(urb);
359 spin_unlock_irqrestore(&devinfo->lock, flags);
360 return;
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800361 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200362
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200363 cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200364 switch (iu->iu_id) {
365 case IU_ID_STATUS:
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100366 if (devinfo->cmnd == cmnd)
367 devinfo->cmnd = NULL;
368
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200369 if (urb->actual_length < 16)
370 devinfo->uas_sense_old = 1;
371 if (devinfo->uas_sense_old)
372 uas_sense_old(urb, cmnd);
373 else
374 uas_sense(urb, cmnd);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200375 if (cmnd->result != 0) {
376 /* cancel data transfers on error */
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200377 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200378 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200379 cmdinfo->state &= ~COMMAND_INFLIGHT;
380 uas_try_complete(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200381 break;
382 case IU_ID_READ_READY:
Hans de Goede8e453152013-11-15 10:04:11 +0100383 if (!cmdinfo->data_in_urb ||
384 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
385 scmd_printk(KERN_ERR, cmnd, "unexpected read rdy\n");
386 break;
387 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200388 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
389 break;
390 case IU_ID_WRITE_READY:
Hans de Goede8e453152013-11-15 10:04:11 +0100391 if (!cmdinfo->data_out_urb ||
392 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
393 scmd_printk(KERN_ERR, cmnd, "unexpected write rdy\n");
394 break;
395 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200396 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
397 break;
398 default:
399 scmd_printk(KERN_ERR, cmnd,
400 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
401 }
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200402 usb_free_urb(urb);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200403 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200404}
405
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200406static void uas_data_cmplt(struct urb *urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200407{
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200408 struct scsi_cmnd *cmnd = urb->context;
409 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200410 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200411 struct scsi_data_buffer *sdb = NULL;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200412 unsigned long flags;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200413
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200414 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200415 if (cmdinfo->data_in_urb == urb) {
416 sdb = scsi_in(cmnd);
417 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
418 } else if (cmdinfo->data_out_urb == urb) {
419 sdb = scsi_out(cmnd);
420 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
421 }
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200422 if (sdb == NULL) {
423 WARN_ON_ONCE(1);
424 } else if (urb->status) {
Hans de Goede876285c2013-11-07 08:52:42 +0100425 if (urb->status != -ECONNRESET) {
426 uas_log_cmd_state(cmnd, __func__);
427 scmd_printk(KERN_ERR, cmnd,
428 "data cmplt err %d stream %d\n",
429 urb->status, urb->stream_id);
430 }
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200431 /* error: no data transfered */
432 sdb->resid = sdb->length;
433 } else {
434 sdb->resid = sdb->length - urb->actual_length;
435 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200436 uas_try_complete(cmnd, __func__);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200437 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200438}
439
Hans de Goede876285c2013-11-07 08:52:42 +0100440static void uas_cmd_cmplt(struct urb *urb)
441{
442 struct scsi_cmnd *cmnd = urb->context;
443
444 if (urb->status) {
445 uas_log_cmd_state(cmnd, __func__);
446 scmd_printk(KERN_ERR, cmnd, "cmd cmplt err %d\n", urb->status);
447 }
448 usb_free_urb(urb);
449}
450
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200451static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200452 unsigned int pipe, u16 stream_id,
453 struct scsi_cmnd *cmnd,
454 enum dma_data_direction dir)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200455{
456 struct usb_device *udev = devinfo->udev;
457 struct urb *urb = usb_alloc_urb(0, gfp);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200458 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
459 ? scsi_in(cmnd) : scsi_out(cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200460
461 if (!urb)
462 goto out;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200463 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
464 uas_data_cmplt, cmnd);
Hans de Goedec6d45792013-11-12 10:53:57 +0100465 urb->stream_id = stream_id;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200466 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
467 urb->sg = sdb->table.sgl;
468 out:
469 return urb;
470}
471
472static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200473 struct Scsi_Host *shost, u16 stream_id)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200474{
475 struct usb_device *udev = devinfo->udev;
476 struct urb *urb = usb_alloc_urb(0, gfp);
477 struct sense_iu *iu;
478
479 if (!urb)
480 goto out;
481
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500482 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200483 if (!iu)
484 goto free;
485
486 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200487 uas_stat_cmplt, shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200488 urb->stream_id = stream_id;
489 urb->transfer_flags |= URB_FREE_BUFFER;
490 out:
491 return urb;
492 free:
493 usb_free_urb(urb);
494 return NULL;
495}
496
497static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Hans de Goedea887cd32013-10-17 19:47:28 +0200498 struct scsi_cmnd *cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200499{
500 struct usb_device *udev = devinfo->udev;
501 struct scsi_device *sdev = cmnd->device;
502 struct urb *urb = usb_alloc_urb(0, gfp);
503 struct command_iu *iu;
504 int len;
505
506 if (!urb)
507 goto out;
508
509 len = cmnd->cmd_len - 16;
510 if (len < 0)
511 len = 0;
512 len = ALIGN(len, 4);
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500513 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200514 if (!iu)
515 goto free;
516
517 iu->iu_id = IU_ID_COMMAND;
Sarah Sharp9eb44542011-12-02 11:55:46 -0800518 if (blk_rq_tagged(cmnd->request))
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100519 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
Sarah Sharp9eb44542011-12-02 11:55:46 -0800520 else
521 iu->tag = cpu_to_be16(1);
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100522 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200523 iu->len = len;
524 int_to_scsilun(sdev->lun, &iu->lun);
525 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
526
527 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
Hans de Goede876285c2013-11-07 08:52:42 +0100528 uas_cmd_cmplt, cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200529 urb->transfer_flags |= URB_FREE_BUFFER;
530 out:
531 return urb;
532 free:
533 usb_free_urb(urb);
534 return NULL;
535}
536
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200537static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
538 u8 function, u16 stream_id)
539{
540 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
541 struct usb_device *udev = devinfo->udev;
542 struct urb *urb = usb_alloc_urb(0, gfp);
543 struct task_mgmt_iu *iu;
544 int err = -ENOMEM;
545
546 if (!urb)
547 goto err;
548
549 iu = kzalloc(sizeof(*iu), gfp);
550 if (!iu)
551 goto err;
552
553 iu->iu_id = IU_ID_TASK_MGMT;
554 iu->tag = cpu_to_be16(stream_id);
555 int_to_scsilun(cmnd->device->lun, &iu->lun);
556
557 iu->function = function;
558 switch (function) {
559 case TMF_ABORT_TASK:
560 if (blk_rq_tagged(cmnd->request))
561 iu->task_tag = cpu_to_be16(cmnd->request->tag + 2);
562 else
563 iu->task_tag = cpu_to_be16(1);
564 break;
565 }
566
567 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
Hans de Goede876285c2013-11-07 08:52:42 +0100568 uas_cmd_cmplt, cmnd);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200569 urb->transfer_flags |= URB_FREE_BUFFER;
570
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200571 usb_anchor_urb(urb, &devinfo->cmd_urbs);
Hans de Goeded5f808d2013-10-17 19:30:26 +0200572 err = usb_submit_urb(urb, gfp);
573 if (err) {
574 usb_unanchor_urb(urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100575 uas_log_cmd_state(cmnd, __func__);
576 scmd_printk(KERN_ERR, cmnd, "task submission err %d\n", err);
Hans de Goeded5f808d2013-10-17 19:30:26 +0200577 goto err;
578 }
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200579
580 return 0;
581
582err:
583 usb_free_urb(urb);
584 return err;
585}
586
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200587/*
588 * Why should I request the Status IU before sending the Command IU? Spec
589 * says to, but also says the device may receive them in any order. Seems
590 * daft to me.
591 */
592
Hans de Goede876285c2013-11-07 08:52:42 +0100593static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd,
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100594 gfp_t gfp, unsigned int stream)
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200595{
Hans de Goede876285c2013-11-07 08:52:42 +0100596 struct Scsi_Host *shost = cmnd->device->host;
Hans de Goede21fc05b2013-11-13 09:32:22 +0100597 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200598 struct urb *urb;
Hans de Goede876285c2013-11-07 08:52:42 +0100599 int err;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200600
601 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
602 if (!urb)
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100603 return NULL;
Hans de Goeded5f808d2013-10-17 19:30:26 +0200604 usb_anchor_urb(urb, &devinfo->sense_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100605 err = usb_submit_urb(urb, gfp);
606 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200607 usb_unanchor_urb(urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100608 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200609 shost_printk(KERN_INFO, shost,
Hans de Goede876285c2013-11-07 08:52:42 +0100610 "sense urb submission error %d stream %d\n",
611 err, stream);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200612 usb_free_urb(urb);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100613 return NULL;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200614 }
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100615 return urb;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200616}
617
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200618static int uas_submit_urbs(struct scsi_cmnd *cmnd,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200619 struct uas_dev_info *devinfo, gfp_t gfp)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200620{
621 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100622 struct urb *urb;
Hans de Goede876285c2013-11-07 08:52:42 +0100623 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200624
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200625 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500626 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Hans de Goede876285c2013-11-07 08:52:42 +0100627 urb = uas_submit_sense_urb(cmnd, gfp, cmdinfo->stream);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100628 if (!urb)
629 return SCSI_MLQUEUE_DEVICE_BUSY;
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500630 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200631 }
632
633 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
634 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200635 devinfo->data_in_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200636 cmnd, DMA_FROM_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200637 if (!cmdinfo->data_in_urb)
638 return SCSI_MLQUEUE_DEVICE_BUSY;
639 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
640 }
641
642 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200643 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100644 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
645 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200646 usb_unanchor_urb(cmdinfo->data_in_urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100647 uas_log_cmd_state(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200648 scmd_printk(KERN_INFO, cmnd,
Hans de Goede876285c2013-11-07 08:52:42 +0100649 "data in urb submission error %d stream %d\n",
650 err, cmdinfo->data_in_urb->stream_id);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200651 return SCSI_MLQUEUE_DEVICE_BUSY;
652 }
653 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200654 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200655 }
656
657 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
658 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200659 devinfo->data_out_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200660 cmnd, DMA_TO_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200661 if (!cmdinfo->data_out_urb)
662 return SCSI_MLQUEUE_DEVICE_BUSY;
663 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
664 }
665
666 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200667 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100668 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
669 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200670 usb_unanchor_urb(cmdinfo->data_out_urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100671 uas_log_cmd_state(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200672 scmd_printk(KERN_INFO, cmnd,
Hans de Goede876285c2013-11-07 08:52:42 +0100673 "data out urb submission error %d stream %d\n",
674 err, cmdinfo->data_out_urb->stream_id);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200675 return SCSI_MLQUEUE_DEVICE_BUSY;
676 }
677 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200678 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200679 }
680
681 if (cmdinfo->state & ALLOC_CMD_URB) {
Hans de Goedea887cd32013-10-17 19:47:28 +0200682 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200683 if (!cmdinfo->cmd_urb)
684 return SCSI_MLQUEUE_DEVICE_BUSY;
685 cmdinfo->state &= ~ALLOC_CMD_URB;
686 }
687
688 if (cmdinfo->state & SUBMIT_CMD_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200689 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
Hans de Goede876285c2013-11-07 08:52:42 +0100690 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
691 if (err) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200692 usb_unanchor_urb(cmdinfo->cmd_urb);
Hans de Goede876285c2013-11-07 08:52:42 +0100693 uas_log_cmd_state(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200694 scmd_printk(KERN_INFO, cmnd,
Hans de Goede876285c2013-11-07 08:52:42 +0100695 "cmd urb submission error %d\n", err);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200696 return SCSI_MLQUEUE_DEVICE_BUSY;
697 }
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200698 cmdinfo->cmd_urb = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200699 cmdinfo->state &= ~SUBMIT_CMD_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200700 cmdinfo->state |= COMMAND_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200701 }
702
703 return 0;
704}
705
Jeff Garzikf2812332010-11-16 02:10:29 -0500706static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200707 void (*done)(struct scsi_cmnd *))
708{
709 struct scsi_device *sdev = cmnd->device;
710 struct uas_dev_info *devinfo = sdev->hostdata;
711 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200712 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200713 int err;
714
715 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
716
Hans de Goedec6f63202013-11-13 09:24:15 +0100717 spin_lock_irqsave(&devinfo->lock, flags);
718
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100719 if (devinfo->resetting) {
720 cmnd->result = DID_ERROR << 16;
721 cmnd->scsi_done(cmnd);
Hans de Goedec6f63202013-11-13 09:24:15 +0100722 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100723 return 0;
724 }
725
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200726 if (devinfo->cmnd) {
727 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200728 return SCSI_MLQUEUE_DEVICE_BUSY;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200729 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200730
Hans de Goede3a4462e2013-11-14 11:06:13 +0100731 memset(cmdinfo, 0, sizeof(*cmdinfo));
732
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200733 if (blk_rq_tagged(cmnd->request)) {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100734 cmdinfo->stream = cmnd->request->tag + 2;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200735 } else {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100736 devinfo->cmnd = cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200737 cmdinfo->stream = 1;
738 }
739
740 cmnd->scsi_done = done;
741
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200742 cmdinfo->state = SUBMIT_STATUS_URB |
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200743 ALLOC_CMD_URB | SUBMIT_CMD_URB;
744
745 switch (cmnd->sc_data_direction) {
746 case DMA_FROM_DEVICE:
747 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
748 break;
749 case DMA_BIDIRECTIONAL:
750 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
751 case DMA_TO_DEVICE:
752 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
753 case DMA_NONE:
754 break;
755 }
756
757 if (!devinfo->use_streams) {
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200758 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200759 cmdinfo->stream = 0;
760 }
761
762 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
763 if (err) {
764 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500765 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200766 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200767 return SCSI_MLQUEUE_DEVICE_BUSY;
768 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200769 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200770 }
771
Hans de Goede040d1a82013-11-12 14:02:12 +0100772 list_add_tail(&cmdinfo->list, &devinfo->inflight_list);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200773 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200774 return 0;
775}
776
Jeff Garzikf2812332010-11-16 02:10:29 -0500777static DEF_SCSI_QCMD(uas_queuecommand)
778
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200779static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
780 const char *fname, u8 function)
781{
782 struct Scsi_Host *shost = cmnd->device->host;
Hans de Goede21fc05b2013-11-13 09:32:22 +0100783 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goeded3f7c152013-10-23 17:46:17 +0100784 u16 tag = devinfo->qdepth;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200785 unsigned long flags;
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100786 struct urb *sense_urb;
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100787 int result = SUCCESS;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200788
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200789 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100790
Hans de Goedec6f63202013-11-13 09:24:15 +0100791 if (devinfo->resetting) {
792 spin_unlock_irqrestore(&devinfo->lock, flags);
793 return FAILED;
794 }
795
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100796 if (devinfo->running_task) {
797 shost_printk(KERN_INFO, shost,
798 "%s: %s: error already running a task\n",
799 __func__, fname);
800 spin_unlock_irqrestore(&devinfo->lock, flags);
801 return FAILED;
802 }
803
804 devinfo->running_task = 1;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200805 memset(&devinfo->response, 0, sizeof(devinfo->response));
Oliver Neukume7eda932014-03-28 11:10:30 +0100806 sense_urb = uas_submit_sense_urb(cmnd, GFP_ATOMIC,
Hans de Goedef323abc2013-11-12 10:51:33 +0100807 devinfo->use_streams ? tag : 0);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100808 if (!sense_urb) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200809 shost_printk(KERN_INFO, shost,
810 "%s: %s: submit sense urb failed\n",
811 __func__, fname);
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100812 devinfo->running_task = 0;
Gerd Hoffmann1994ff42012-09-26 10:28:58 +0200813 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200814 return FAILED;
815 }
Oliver Neukume7eda932014-03-28 11:10:30 +0100816 if (uas_submit_task_urb(cmnd, GFP_ATOMIC, function, tag)) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200817 shost_printk(KERN_INFO, shost,
818 "%s: %s: submit task mgmt urb failed\n",
819 __func__, fname);
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100820 devinfo->running_task = 0;
Gerd Hoffmann1994ff42012-09-26 10:28:58 +0200821 spin_unlock_irqrestore(&devinfo->lock, flags);
Hans de Goede70cf0fb2013-10-29 10:37:23 +0100822 usb_kill_urb(sense_urb);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200823 return FAILED;
824 }
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200825 spin_unlock_irqrestore(&devinfo->lock, flags);
826
827 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) {
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100828 /*
829 * Note we deliberately do not clear running_task here. If we
830 * allow new tasks to be submitted, there is no way to figure
831 * out if a received response_iu is for the failed task or for
832 * the new one. A bus-reset will eventually clear running_task.
833 */
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200834 shost_printk(KERN_INFO, shost,
835 "%s: %s timed out\n", __func__, fname);
836 return FAILED;
837 }
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100838
839 spin_lock_irqsave(&devinfo->lock, flags);
840 devinfo->running_task = 0;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200841 if (be16_to_cpu(devinfo->response.tag) != tag) {
842 shost_printk(KERN_INFO, shost,
843 "%s: %s failed (wrong tag %d/%d)\n", __func__,
844 fname, be16_to_cpu(devinfo->response.tag), tag);
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100845 result = FAILED;
846 } else if (devinfo->response.response_code != RC_TMF_COMPLETE) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200847 shost_printk(KERN_INFO, shost,
848 "%s: %s failed (rc 0x%x)\n", __func__,
849 fname, devinfo->response.response_code);
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100850 result = FAILED;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200851 }
Hans de Goedeb83b86a2013-10-29 10:51:00 +0100852 spin_unlock_irqrestore(&devinfo->lock, flags);
853
854 return result;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200855}
856
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200857static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
858{
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200859 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200860 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
861 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200862 int ret;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200863
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200864 spin_lock_irqsave(&devinfo->lock, flags);
Hans de Goedec6f63202013-11-13 09:24:15 +0100865
866 if (devinfo->resetting) {
867 spin_unlock_irqrestore(&devinfo->lock, flags);
868 return FAILED;
869 }
870
Hans de Goede673331c2013-11-14 14:27:27 +0100871 uas_mark_cmd_dead(devinfo, cmdinfo, DID_ABORT, __func__);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100872 if (cmdinfo->state & COMMAND_INFLIGHT) {
873 spin_unlock_irqrestore(&devinfo->lock, flags);
874 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
875 } else {
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200876 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100877 uas_try_complete(cmnd, __func__);
878 spin_unlock_irqrestore(&devinfo->lock, flags);
879 ret = SUCCESS;
880 }
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200881 return ret;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200882}
883
884static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
885{
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200886 sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__);
887 return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET",
888 TMF_LOGICAL_UNIT_RESET);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200889}
890
891static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
892{
893 struct scsi_device *sdev = cmnd->device;
894 struct uas_dev_info *devinfo = sdev->hostdata;
895 struct usb_device *udev = devinfo->udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200896 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200897
Hans de Goedebe326f42013-09-22 16:27:02 +0200898 err = usb_lock_device_for_reset(udev, devinfo->intf);
899 if (err) {
900 shost_printk(KERN_ERR, sdev->host,
901 "%s FAILED to get lock err %d\n", __func__, err);
902 return FAILED;
903 }
904
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200905 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200906 devinfo->resetting = 1;
Hans de Goede673331c2013-11-14 14:27:27 +0100907 uas_abort_inflight(devinfo, DID_RESET, __func__);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200908 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200909 usb_kill_anchored_urbs(&devinfo->sense_urbs);
910 usb_kill_anchored_urbs(&devinfo->data_urbs);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200911 uas_zap_dead(devinfo);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200912 err = usb_reset_device(udev);
913 devinfo->resetting = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200914
Hans de Goedebe326f42013-09-22 16:27:02 +0200915 usb_unlock_device(udev);
916
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200917 if (err) {
918 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
919 return FAILED;
920 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200921
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200922 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
923 return SUCCESS;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200924}
925
926static int uas_slave_alloc(struct scsi_device *sdev)
927{
Hans de Goede21fc05b2013-11-13 09:32:22 +0100928 sdev->hostdata = (void *)sdev->host->hostdata;
Hans de Goede37599f92013-11-15 10:04:31 +0100929
930 /* USB has unusual DMA-alignment requirements: Although the
931 * starting address of each scatter-gather element doesn't matter,
932 * the length of each element except the last must be divisible
933 * by the Bulk maxpacket value. There's currently no way to
934 * express this by block-layer constraints, so we'll cop out
935 * and simply require addresses to be aligned at 512-byte
936 * boundaries. This is okay since most block I/O involves
937 * hardware sectors that are multiples of 512 bytes in length,
938 * and since host controllers up through USB 2.0 have maxpacket
939 * values no larger than 512.
940 *
941 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
942 * values can be as large as 2048. To make that work properly
943 * will require changes to the block layer.
944 */
945 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
946
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200947 return 0;
948}
949
950static int uas_slave_configure(struct scsi_device *sdev)
951{
952 struct uas_dev_info *devinfo = sdev->hostdata;
953 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
Hans de Goeded3f7c152013-10-23 17:46:17 +0100954 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200955 return 0;
956}
957
958static struct scsi_host_template uas_host_template = {
959 .module = THIS_MODULE,
960 .name = "uas",
961 .queuecommand = uas_queuecommand,
962 .slave_alloc = uas_slave_alloc,
963 .slave_configure = uas_slave_configure,
964 .eh_abort_handler = uas_eh_abort_handler,
965 .eh_device_reset_handler = uas_eh_device_reset_handler,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200966 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
967 .can_queue = 65536, /* Is there a limit on the _host_ ? */
968 .this_id = -1,
969 .sg_tablesize = SG_NONE,
970 .cmd_per_lun = 1, /* until we override it */
971 .skip_settle_delay = 1,
972 .ordered_tag = 1,
973};
974
Hans de Goede79b4c062013-10-25 17:04:33 +0100975#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
976 vendorName, productName, useProtocol, useTransport, \
977 initFunction, flags) \
978{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
979 .driver_info = (flags) }
980
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200981static struct usb_device_id uas_usb_ids[] = {
Hans de Goede79b4c062013-10-25 17:04:33 +0100982# include "unusual_uas.h"
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200983 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
984 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
985 /* 0xaa is a prototype device I happen to have access to */
986 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
987 { }
988};
989MODULE_DEVICE_TABLE(usb, uas_usb_ids);
990
Hans de Goede79b4c062013-10-25 17:04:33 +0100991#undef UNUSUAL_DEV
992
Hans de Goedee1be0672013-10-21 08:00:58 +0100993static int uas_switch_interface(struct usb_device *udev,
994 struct usb_interface *intf)
995{
996 int alt;
997
998 alt = uas_find_uas_alt_setting(intf);
999 if (alt < 0)
1000 return alt;
1001
1002 return usb_set_interface(udev,
1003 intf->altsetting[0].desc.bInterfaceNumber, alt);
1004}
1005
Hans de Goede58d51442013-10-29 10:23:26 +01001006static int uas_configure_endpoints(struct uas_dev_info *devinfo)
Hans de Goede34f11e52013-10-29 08:54:48 +01001007{
1008 struct usb_host_endpoint *eps[4] = { };
1009 struct usb_device *udev = devinfo->udev;
1010 int r;
1011
1012 devinfo->uas_sense_old = 0;
1013 devinfo->cmnd = NULL;
1014
1015 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
Hans de Goede74d71ae2013-10-29 10:10:36 +01001016 if (r)
1017 return r;
Hans de Goede34f11e52013-10-29 08:54:48 +01001018
Hans de Goede74d71ae2013-10-29 10:10:36 +01001019 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
1020 usb_endpoint_num(&eps[0]->desc));
1021 devinfo->status_pipe = usb_rcvbulkpipe(udev,
1022 usb_endpoint_num(&eps[1]->desc));
1023 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
1024 usb_endpoint_num(&eps[2]->desc));
1025 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
1026 usb_endpoint_num(&eps[3]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001027
Hans de Goede58d51442013-10-29 10:23:26 +01001028 if (udev->speed != USB_SPEED_SUPER) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001029 devinfo->qdepth = 256;
1030 devinfo->use_streams = 0;
1031 } else {
Hans de Goede58d51442013-10-29 10:23:26 +01001032 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
Oliver Neukum94d72f02014-03-28 11:25:50 +01001033 3, 256, GFP_NOIO);
Hans de Goede58d51442013-10-29 10:23:26 +01001034 if (devinfo->qdepth < 0)
1035 return devinfo->qdepth;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001036 devinfo->use_streams = 1;
1037 }
Hans de Goede58d51442013-10-29 10:23:26 +01001038
1039 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001040}
1041
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001042static void uas_free_streams(struct uas_dev_info *devinfo)
1043{
1044 struct usb_device *udev = devinfo->udev;
1045 struct usb_host_endpoint *eps[3];
1046
1047 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
1048 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
1049 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
Oliver Neukum94d72f02014-03-28 11:25:50 +01001050 usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001051}
1052
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001053static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
1054{
Hans de Goede6ce82132013-10-17 19:00:45 +02001055 int result = -ENOMEM;
1056 struct Scsi_Host *shost = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001057 struct uas_dev_info *devinfo;
1058 struct usb_device *udev = interface_to_usbdev(intf);
1059
Hans de Goede79b4c062013-10-25 17:04:33 +01001060 if (!uas_use_uas_driver(intf, id))
1061 return -ENODEV;
1062
Matthew Wilcox89dc2902010-12-15 15:44:05 -05001063 if (uas_switch_interface(udev, intf))
1064 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001065
Hans de Goede21fc05b2013-11-13 09:32:22 +01001066 shost = scsi_host_alloc(&uas_host_template,
1067 sizeof(struct uas_dev_info));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001068 if (!shost)
Hans de Goede6ce82132013-10-17 19:00:45 +02001069 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001070
1071 shost->max_cmd_len = 16 + 252;
1072 shost->max_id = 1;
Gerd Hoffmannbde027b2013-01-25 15:03:36 +01001073 shost->max_lun = 256;
1074 shost->max_channel = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001075 shost->sg_tablesize = udev->bus->sg_tablesize;
1076
Hans de Goede21fc05b2013-11-13 09:32:22 +01001077 devinfo = (struct uas_dev_info *)shost->hostdata;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001078 devinfo->intf = intf;
1079 devinfo->udev = udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +02001080 devinfo->resetting = 0;
Hans de Goedeb83b86a2013-10-29 10:51:00 +01001081 devinfo->running_task = 0;
Hans de Goededa65c2b2013-11-11 11:51:42 +01001082 devinfo->shutdown = 0;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001083 init_usb_anchor(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001084 init_usb_anchor(&devinfo->sense_urbs);
1085 init_usb_anchor(&devinfo->data_urbs);
Gerd Hoffmanne0648522012-09-25 10:47:08 +02001086 spin_lock_init(&devinfo->lock);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001087 INIT_WORK(&devinfo->work, uas_do_work);
Hans de Goede61c09ce2013-11-12 13:44:20 +01001088 INIT_LIST_HEAD(&devinfo->inflight_list);
Gerd Hoffmann326349f2013-09-13 13:27:13 +02001089 INIT_LIST_HEAD(&devinfo->dead_list);
Hans de Goede58d51442013-10-29 10:23:26 +01001090
1091 result = uas_configure_endpoints(devinfo);
1092 if (result)
1093 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001094
Hans de Goeded3f7c152013-10-23 17:46:17 +01001095 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001096 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +02001097 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001098
Oliver Neukumc637f1f2014-03-28 11:29:25 +01001099 usb_set_intfdata(intf, shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001100 result = scsi_add_host(shost, &intf->dev);
1101 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +02001102 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001103
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001104 scsi_scan_host(shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001105 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001106
Hans de Goede6ce82132013-10-17 19:00:45 +02001107free_streams:
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001108 uas_free_streams(devinfo);
Oliver Neukumc637f1f2014-03-28 11:29:25 +01001109 usb_set_intfdata(intf, NULL);
Hans de Goede6ce82132013-10-17 19:00:45 +02001110set_alt0:
1111 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001112 if (shost)
1113 scsi_host_put(shost);
1114 return result;
1115}
1116
1117static int uas_pre_reset(struct usb_interface *intf)
1118{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001119 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001120 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede4de7a3732013-10-22 16:10:44 +01001121 unsigned long flags;
1122
Hans de Goededa65c2b2013-11-11 11:51:42 +01001123 if (devinfo->shutdown)
1124 return 0;
1125
Hans de Goede4de7a3732013-10-22 16:10:44 +01001126 /* Block new requests */
1127 spin_lock_irqsave(shost->host_lock, flags);
1128 scsi_block_requests(shost);
1129 spin_unlock_irqrestore(shost->host_lock, flags);
1130
1131 /* Wait for any pending requests to complete */
1132 flush_work(&devinfo->work);
1133 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1134 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1135 return 1;
1136 }
1137
1138 uas_free_streams(devinfo);
1139
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001140 return 0;
1141}
1142
1143static int uas_post_reset(struct usb_interface *intf)
1144{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001145 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 Goede4de7a3732013-10-22 16:10:44 +01001147 unsigned long flags;
1148
Hans de Goededa65c2b2013-11-11 11:51:42 +01001149 if (devinfo->shutdown)
1150 return 0;
1151
Hans de Goede58d51442013-10-29 10:23:26 +01001152 if (uas_configure_endpoints(devinfo) != 0) {
1153 shost_printk(KERN_ERR, shost,
1154 "%s: alloc streams error after reset", __func__);
1155 return 1;
1156 }
Hans de Goede4de7a3732013-10-22 16:10:44 +01001157
1158 spin_lock_irqsave(shost->host_lock, flags);
1159 scsi_report_bus_reset(shost, 0);
1160 spin_unlock_irqrestore(shost->host_lock, flags);
1161
1162 scsi_unblock_requests(shost);
1163
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001164 return 0;
1165}
1166
Hans de Goede0df1f662013-11-07 08:47:05 +01001167static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1168{
1169 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001170 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede0df1f662013-11-07 08:47:05 +01001171
1172 /* Wait for any pending requests to complete */
1173 flush_work(&devinfo->work);
1174 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1175 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1176 return -ETIME;
1177 }
1178
1179 return 0;
1180}
1181
1182static int uas_resume(struct usb_interface *intf)
1183{
1184 return 0;
1185}
1186
1187static int uas_reset_resume(struct usb_interface *intf)
1188{
1189 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001190 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goede0df1f662013-11-07 08:47:05 +01001191 unsigned long flags;
1192
1193 if (uas_configure_endpoints(devinfo) != 0) {
1194 shost_printk(KERN_ERR, shost,
1195 "%s: alloc streams error after reset", __func__);
1196 return -EIO;
1197 }
1198
1199 spin_lock_irqsave(shost->host_lock, flags);
1200 scsi_report_bus_reset(shost, 0);
1201 spin_unlock_irqrestore(shost->host_lock, flags);
1202
1203 return 0;
1204}
1205
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001206static void uas_disconnect(struct usb_interface *intf)
1207{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001208 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;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001210
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001211 devinfo->resetting = 1;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001212 cancel_work_sync(&devinfo->work);
Hans de Goede673331c2013-11-14 14:27:27 +01001213 uas_abort_inflight(devinfo, DID_NO_CONNECT, __func__);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001214 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001215 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1216 usb_kill_anchored_urbs(&devinfo->data_urbs);
Gerd Hoffmann326349f2013-09-13 13:27:13 +02001217 uas_zap_dead(devinfo);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001218 scsi_remove_host(shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001219 uas_free_streams(devinfo);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001220 scsi_host_put(shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001221}
1222
Hans de Goededa65c2b2013-11-11 11:51:42 +01001223/*
1224 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1225 * hang on reboot when the device is still in uas mode. Note the reset is
1226 * necessary as some devices won't revert to usb-storage mode without it.
1227 */
1228static void uas_shutdown(struct device *dev)
1229{
1230 struct usb_interface *intf = to_usb_interface(dev);
1231 struct usb_device *udev = interface_to_usbdev(intf);
1232 struct Scsi_Host *shost = usb_get_intfdata(intf);
Hans de Goede21fc05b2013-11-13 09:32:22 +01001233 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
Hans de Goededa65c2b2013-11-11 11:51:42 +01001234
1235 if (system_state != SYSTEM_RESTART)
1236 return;
1237
1238 devinfo->shutdown = 1;
1239 uas_free_streams(devinfo);
1240 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1241 usb_reset_device(udev);
1242}
1243
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001244static struct usb_driver uas_driver = {
1245 .name = "uas",
1246 .probe = uas_probe,
1247 .disconnect = uas_disconnect,
1248 .pre_reset = uas_pre_reset,
1249 .post_reset = uas_post_reset,
Hans de Goede0df1f662013-11-07 08:47:05 +01001250 .suspend = uas_suspend,
1251 .resume = uas_resume,
1252 .reset_resume = uas_reset_resume,
Hans de Goededa65c2b2013-11-11 11:51:42 +01001253 .drvwrap.driver.shutdown = uas_shutdown,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001254 .id_table = uas_usb_ids,
1255};
1256
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001257module_usb_driver(uas_driver);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001258
1259MODULE_LICENSE("GPL");
Hans de Goedef50a4962013-10-28 10:48:04 +00001260MODULE_AUTHOR(
1261 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");