blob: d758faef866426c578702b648c0518b616f09e76 [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;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +010056 struct scsi_cmnd *cmnd;
Gerd Hoffmanne0648522012-09-25 10:47:08 +020057 spinlock_t lock;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +020058 struct work_struct work;
59 struct list_head work_list;
Gerd Hoffmann326349f2013-09-13 13:27:13 +020060 struct list_head dead_list;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020061};
62
63enum {
Matthew Wilcox92a3f762010-12-15 15:44:04 -050064 SUBMIT_STATUS_URB = (1 << 1),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020065 ALLOC_DATA_IN_URB = (1 << 2),
66 SUBMIT_DATA_IN_URB = (1 << 3),
67 ALLOC_DATA_OUT_URB = (1 << 4),
68 SUBMIT_DATA_OUT_URB = (1 << 5),
69 ALLOC_CMD_URB = (1 << 6),
70 SUBMIT_CMD_URB = (1 << 7),
Gerd Hoffmannb1d67692012-06-19 09:54:51 +020071 COMMAND_INFLIGHT = (1 << 8),
72 DATA_IN_URB_INFLIGHT = (1 << 9),
73 DATA_OUT_URB_INFLIGHT = (1 << 10),
74 COMMAND_COMPLETED = (1 << 11),
Gerd Hoffmannef018cc92012-09-25 10:47:06 +020075 COMMAND_ABORTED = (1 << 12),
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +010076 UNLINK_DATA_URBS = (1 << 13),
Gerd Hoffmannefefecf2012-11-30 11:54:42 +010077 IS_IN_WORK_LIST = (1 << 14),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020078};
79
80/* Overrides scsi_pointer */
81struct uas_cmd_info {
82 unsigned int state;
83 unsigned int stream;
84 struct urb *cmd_urb;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020085 struct urb *data_in_urb;
86 struct urb *data_out_urb;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +020087 struct list_head work;
Gerd Hoffmann326349f2013-09-13 13:27:13 +020088 struct list_head dead;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020089};
90
91/* I hate forward declarations, but I actually have a loop */
92static int uas_submit_urbs(struct scsi_cmnd *cmnd,
93 struct uas_dev_info *devinfo, gfp_t gfp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -080094static void uas_do_work(struct work_struct *work);
Gerd Hoffmann4c456972012-11-30 11:54:44 +010095static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
Gerd Hoffmannd89bd832013-09-13 13:27:11 +020096static void uas_free_streams(struct uas_dev_info *devinfo);
Gerd Hoffmann326349f2013-09-13 13:27:13 +020097static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020098
Hans de Goede7e50e0b2013-10-17 19:19:04 +020099/* Must be called with devinfo->lock held, will temporary unlock the lock */
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100100static void uas_unlink_data_urbs(struct uas_dev_info *devinfo,
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200101 struct uas_cmd_info *cmdinfo,
102 unsigned long *lock_flags)
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100103{
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100104 /*
105 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
106 * (called by urb completion) doesn't release cmdinfo
107 * underneath us.
108 */
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100109 cmdinfo->state |= UNLINK_DATA_URBS;
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200110 spin_unlock_irqrestore(&devinfo->lock, *lock_flags);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100111
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100112 if (cmdinfo->data_in_urb)
113 usb_unlink_urb(cmdinfo->data_in_urb);
114 if (cmdinfo->data_out_urb)
115 usb_unlink_urb(cmdinfo->data_out_urb);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100116
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200117 spin_lock_irqsave(&devinfo->lock, *lock_flags);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100118 cmdinfo->state &= ~UNLINK_DATA_URBS;
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100119}
120
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200121static void uas_do_work(struct work_struct *work)
122{
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200123 struct uas_dev_info *devinfo =
124 container_of(work, struct uas_dev_info, work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200125 struct uas_cmd_info *cmdinfo;
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800126 struct uas_cmd_info *temp;
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);
131 list_for_each_entry_safe(cmdinfo, temp, &devinfo->work_list, work) {
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);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200135 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200136 if (!err) {
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100137 cmdinfo->state &= ~IS_IN_WORK_LIST;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200138 list_del(&cmdinfo->work);
139 } else {
140 schedule_work(&devinfo->work);
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800141 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200142 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200143 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200144}
145
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100146static void uas_abort_work(struct uas_dev_info *devinfo)
147{
148 struct uas_cmd_info *cmdinfo;
149 struct uas_cmd_info *temp;
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100150 unsigned long flags;
151
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100152 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200153 list_for_each_entry_safe(cmdinfo, temp, &devinfo->work_list, work) {
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100154 struct scsi_pointer *scp = (void *)cmdinfo;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200155 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
156 SCp);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200157 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200158 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200159 cmdinfo->state |= COMMAND_ABORTED;
160 cmdinfo->state &= ~IS_IN_WORK_LIST;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200161 list_del(&cmdinfo->work);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200162 list_add_tail(&cmdinfo->dead, &devinfo->dead_list);
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100163 }
164 spin_unlock_irqrestore(&devinfo->lock, flags);
165}
166
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200167static void uas_add_work(struct uas_cmd_info *cmdinfo)
168{
169 struct scsi_pointer *scp = (void *)cmdinfo;
170 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
171 struct uas_dev_info *devinfo = cmnd->device->hostdata;
172
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200173 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200174 list_add_tail(&cmdinfo->work, &devinfo->work_list);
175 cmdinfo->state |= IS_IN_WORK_LIST;
176 schedule_work(&devinfo->work);
177}
178
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200179static void uas_zap_dead(struct uas_dev_info *devinfo)
180{
181 struct uas_cmd_info *cmdinfo;
182 struct uas_cmd_info *temp;
183 unsigned long flags;
184
185 spin_lock_irqsave(&devinfo->lock, flags);
186 list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, dead) {
187 struct scsi_pointer *scp = (void *)cmdinfo;
188 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
189 SCp);
190 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200191 WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED));
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200192 /* all urbs are killed, clear inflight bits */
193 cmdinfo->state &= ~(COMMAND_INFLIGHT |
194 DATA_IN_URB_INFLIGHT |
195 DATA_OUT_URB_INFLIGHT);
196 uas_try_complete(cmnd, __func__);
197 }
198 spin_unlock_irqrestore(&devinfo->lock, flags);
199}
200
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200201static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
202{
203 struct sense_iu *sense_iu = urb->transfer_buffer;
204 struct scsi_device *sdev = cmnd->device;
205
206 if (urb->actual_length > 16) {
207 unsigned len = be16_to_cpup(&sense_iu->len);
208 if (len + 16 != urb->actual_length) {
209 int newlen = min(len + 16, urb->actual_length) - 16;
210 if (newlen < 0)
211 newlen = 0;
212 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
213 "disagrees with IU sense data length %d, "
214 "using %d bytes of sense data\n", __func__,
215 urb->actual_length, len, newlen);
216 len = newlen;
217 }
218 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
219 }
220
221 cmnd->result = sense_iu->status;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200222}
223
224static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
225{
226 struct sense_iu_old *sense_iu = urb->transfer_buffer;
227 struct scsi_device *sdev = cmnd->device;
228
229 if (urb->actual_length > 8) {
230 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
231 if (len + 8 != urb->actual_length) {
232 int newlen = min(len + 8, urb->actual_length) - 8;
233 if (newlen < 0)
234 newlen = 0;
235 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
236 "disagrees with IU sense data length %d, "
237 "using %d bytes of sense data\n", __func__,
238 urb->actual_length, len, newlen);
239 len = newlen;
240 }
241 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
242 }
243
244 cmnd->result = sense_iu->status;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200245}
246
247static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
248{
249 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
250
251 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100252 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200253 caller, cmnd, cmnd->request->tag,
254 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
255 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
256 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
257 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
258 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
259 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
260 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
261 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
262 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
263 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
Gerd Hoffmannef018cc92012-09-25 10:47:06 +0200264 (ci->state & COMMAND_COMPLETED) ? " done" : "",
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100265 (ci->state & COMMAND_ABORTED) ? " abort" : "",
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100266 (ci->state & UNLINK_DATA_URBS) ? " unlink": "",
267 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200268}
269
270static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
271{
272 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200273 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200274
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200275 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200276 if (cmdinfo->state & (COMMAND_INFLIGHT |
277 DATA_IN_URB_INFLIGHT |
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100278 DATA_OUT_URB_INFLIGHT |
279 UNLINK_DATA_URBS))
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200280 return -EBUSY;
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200281 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200282 cmdinfo->state |= COMMAND_COMPLETED;
283 usb_free_urb(cmdinfo->data_in_urb);
284 usb_free_urb(cmdinfo->data_out_urb);
Gerd Hoffmann0871d7d2012-09-25 10:47:07 +0200285 if (cmdinfo->state & COMMAND_ABORTED) {
286 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
287 cmnd->result = DID_ABORT << 16;
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200288 list_del(&cmdinfo->dead);
Gerd Hoffmann0871d7d2012-09-25 10:47:07 +0200289 }
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200290 cmnd->scsi_done(cmnd);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200291 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200292}
293
294static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200295 unsigned direction)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200296{
297 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
298 int err;
299
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200300 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200301 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
302 if (err) {
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200303 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200304 }
305}
306
307static void uas_stat_cmplt(struct urb *urb)
308{
309 struct iu *iu = urb->transfer_buffer;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100310 struct Scsi_Host *shost = urb->context;
311 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200312 struct scsi_cmnd *cmnd;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200313 struct uas_cmd_info *cmdinfo;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200314 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200315 u16 tag;
316
317 if (urb->status) {
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200318 if (urb->status == -ENOENT) {
319 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
320 urb->stream_id);
321 } else {
322 dev_err(&urb->dev->dev, "stat urb: status %d\n",
323 urb->status);
324 }
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200325 usb_free_urb(urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200326 return;
327 }
328
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200329 if (devinfo->resetting) {
330 usb_free_urb(urb);
331 return;
332 }
333
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200334 spin_lock_irqsave(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200335 tag = be16_to_cpup(&iu->tag) - 1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100336 if (tag == 0)
337 cmnd = devinfo->cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200338 else
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100339 cmnd = scsi_host_find_tag(shost, tag - 1);
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200340
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800341 if (!cmnd) {
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200342 if (iu->iu_id == IU_ID_RESPONSE) {
343 /* store results for uas_eh_task_mgmt() */
344 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200345 }
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200346 usb_free_urb(urb);
347 spin_unlock_irqrestore(&devinfo->lock, flags);
348 return;
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800349 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200350
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200351 cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200352 switch (iu->iu_id) {
353 case IU_ID_STATUS:
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100354 if (devinfo->cmnd == cmnd)
355 devinfo->cmnd = NULL;
356
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200357 if (urb->actual_length < 16)
358 devinfo->uas_sense_old = 1;
359 if (devinfo->uas_sense_old)
360 uas_sense_old(urb, cmnd);
361 else
362 uas_sense(urb, cmnd);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200363 if (cmnd->result != 0) {
364 /* cancel data transfers on error */
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200365 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200366 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200367 cmdinfo->state &= ~COMMAND_INFLIGHT;
368 uas_try_complete(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200369 break;
370 case IU_ID_READ_READY:
371 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
372 break;
373 case IU_ID_WRITE_READY:
374 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
375 break;
376 default:
377 scmd_printk(KERN_ERR, cmnd,
378 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
379 }
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200380 usb_free_urb(urb);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200381 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200382}
383
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200384static void uas_data_cmplt(struct urb *urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200385{
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200386 struct scsi_cmnd *cmnd = urb->context;
387 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200388 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200389 struct scsi_data_buffer *sdb = NULL;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200390 unsigned long flags;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200391
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200392 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200393 if (cmdinfo->data_in_urb == urb) {
394 sdb = scsi_in(cmnd);
395 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
396 } else if (cmdinfo->data_out_urb == urb) {
397 sdb = scsi_out(cmnd);
398 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
399 }
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200400 if (sdb == NULL) {
401 WARN_ON_ONCE(1);
402 } else if (urb->status) {
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200403 /* error: no data transfered */
404 sdb->resid = sdb->length;
405 } else {
406 sdb->resid = sdb->length - urb->actual_length;
407 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200408 uas_try_complete(cmnd, __func__);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200409 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200410}
411
412static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200413 unsigned int pipe, u16 stream_id,
414 struct scsi_cmnd *cmnd,
415 enum dma_data_direction dir)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200416{
417 struct usb_device *udev = devinfo->udev;
418 struct urb *urb = usb_alloc_urb(0, gfp);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200419 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
420 ? scsi_in(cmnd) : scsi_out(cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200421
422 if (!urb)
423 goto out;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200424 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
425 uas_data_cmplt, cmnd);
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200426 if (devinfo->use_streams)
427 urb->stream_id = stream_id;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200428 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
429 urb->sg = sdb->table.sgl;
430 out:
431 return urb;
432}
433
434static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200435 struct Scsi_Host *shost, u16 stream_id)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200436{
437 struct usb_device *udev = devinfo->udev;
438 struct urb *urb = usb_alloc_urb(0, gfp);
439 struct sense_iu *iu;
440
441 if (!urb)
442 goto out;
443
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500444 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200445 if (!iu)
446 goto free;
447
448 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200449 uas_stat_cmplt, shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200450 urb->stream_id = stream_id;
451 urb->transfer_flags |= URB_FREE_BUFFER;
452 out:
453 return urb;
454 free:
455 usb_free_urb(urb);
456 return NULL;
457}
458
459static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Hans de Goedea887cd32013-10-17 19:47:28 +0200460 struct scsi_cmnd *cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200461{
462 struct usb_device *udev = devinfo->udev;
463 struct scsi_device *sdev = cmnd->device;
464 struct urb *urb = usb_alloc_urb(0, gfp);
465 struct command_iu *iu;
466 int len;
467
468 if (!urb)
469 goto out;
470
471 len = cmnd->cmd_len - 16;
472 if (len < 0)
473 len = 0;
474 len = ALIGN(len, 4);
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500475 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200476 if (!iu)
477 goto free;
478
479 iu->iu_id = IU_ID_COMMAND;
Sarah Sharp9eb44542011-12-02 11:55:46 -0800480 if (blk_rq_tagged(cmnd->request))
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100481 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
Sarah Sharp9eb44542011-12-02 11:55:46 -0800482 else
483 iu->tag = cpu_to_be16(1);
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100484 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200485 iu->len = len;
486 int_to_scsilun(sdev->lun, &iu->lun);
487 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
488
489 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
490 usb_free_urb, NULL);
491 urb->transfer_flags |= URB_FREE_BUFFER;
492 out:
493 return urb;
494 free:
495 usb_free_urb(urb);
496 return NULL;
497}
498
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200499static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
500 u8 function, u16 stream_id)
501{
502 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
503 struct usb_device *udev = devinfo->udev;
504 struct urb *urb = usb_alloc_urb(0, gfp);
505 struct task_mgmt_iu *iu;
506 int err = -ENOMEM;
507
508 if (!urb)
509 goto err;
510
511 iu = kzalloc(sizeof(*iu), gfp);
512 if (!iu)
513 goto err;
514
515 iu->iu_id = IU_ID_TASK_MGMT;
516 iu->tag = cpu_to_be16(stream_id);
517 int_to_scsilun(cmnd->device->lun, &iu->lun);
518
519 iu->function = function;
520 switch (function) {
521 case TMF_ABORT_TASK:
522 if (blk_rq_tagged(cmnd->request))
523 iu->task_tag = cpu_to_be16(cmnd->request->tag + 2);
524 else
525 iu->task_tag = cpu_to_be16(1);
526 break;
527 }
528
529 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
530 usb_free_urb, NULL);
531 urb->transfer_flags |= URB_FREE_BUFFER;
532
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200533 usb_anchor_urb(urb, &devinfo->cmd_urbs);
Hans de Goeded5f808d2013-10-17 19:30:26 +0200534 err = usb_submit_urb(urb, gfp);
535 if (err) {
536 usb_unanchor_urb(urb);
537 goto err;
538 }
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200539
540 return 0;
541
542err:
543 usb_free_urb(urb);
544 return err;
545}
546
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200547/*
548 * Why should I request the Status IU before sending the Command IU? Spec
549 * says to, but also says the device may receive them in any order. Seems
550 * daft to me.
551 */
552
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200553static int uas_submit_sense_urb(struct Scsi_Host *shost,
554 gfp_t gfp, unsigned int stream)
555{
556 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
557 struct urb *urb;
558
559 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
560 if (!urb)
561 return SCSI_MLQUEUE_DEVICE_BUSY;
Hans de Goeded5f808d2013-10-17 19:30:26 +0200562 usb_anchor_urb(urb, &devinfo->sense_urbs);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200563 if (usb_submit_urb(urb, gfp)) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200564 usb_unanchor_urb(urb);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200565 shost_printk(KERN_INFO, shost,
566 "sense urb submission failure\n");
567 usb_free_urb(urb);
568 return SCSI_MLQUEUE_DEVICE_BUSY;
569 }
570 return 0;
571}
572
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200573static int uas_submit_urbs(struct scsi_cmnd *cmnd,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200574 struct uas_dev_info *devinfo, gfp_t gfp)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200575{
576 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200577 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200578
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200579 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500580 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200581 err = uas_submit_sense_urb(cmnd->device->host, gfp,
582 cmdinfo->stream);
583 if (err) {
584 return err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200585 }
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500586 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200587 }
588
589 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
590 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200591 devinfo->data_in_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200592 cmnd, DMA_FROM_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200593 if (!cmdinfo->data_in_urb)
594 return SCSI_MLQUEUE_DEVICE_BUSY;
595 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
596 }
597
598 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200599 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200600 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200601 usb_unanchor_urb(cmdinfo->data_in_urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200602 scmd_printk(KERN_INFO, cmnd,
603 "data in urb submission failure\n");
604 return SCSI_MLQUEUE_DEVICE_BUSY;
605 }
606 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200607 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200608 }
609
610 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
611 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200612 devinfo->data_out_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200613 cmnd, DMA_TO_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200614 if (!cmdinfo->data_out_urb)
615 return SCSI_MLQUEUE_DEVICE_BUSY;
616 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
617 }
618
619 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200620 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200621 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200622 usb_unanchor_urb(cmdinfo->data_out_urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200623 scmd_printk(KERN_INFO, cmnd,
624 "data out urb submission failure\n");
625 return SCSI_MLQUEUE_DEVICE_BUSY;
626 }
627 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200628 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200629 }
630
631 if (cmdinfo->state & ALLOC_CMD_URB) {
Hans de Goedea887cd32013-10-17 19:47:28 +0200632 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200633 if (!cmdinfo->cmd_urb)
634 return SCSI_MLQUEUE_DEVICE_BUSY;
635 cmdinfo->state &= ~ALLOC_CMD_URB;
636 }
637
638 if (cmdinfo->state & SUBMIT_CMD_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200639 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200640 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200641 usb_unanchor_urb(cmdinfo->cmd_urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200642 scmd_printk(KERN_INFO, cmnd,
643 "cmd urb submission failure\n");
644 return SCSI_MLQUEUE_DEVICE_BUSY;
645 }
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200646 cmdinfo->cmd_urb = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200647 cmdinfo->state &= ~SUBMIT_CMD_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200648 cmdinfo->state |= COMMAND_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200649 }
650
651 return 0;
652}
653
Jeff Garzikf2812332010-11-16 02:10:29 -0500654static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200655 void (*done)(struct scsi_cmnd *))
656{
657 struct scsi_device *sdev = cmnd->device;
658 struct uas_dev_info *devinfo = sdev->hostdata;
659 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200660 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200661 int err;
662
663 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
664
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100665 if (devinfo->resetting) {
666 cmnd->result = DID_ERROR << 16;
667 cmnd->scsi_done(cmnd);
668 return 0;
669 }
670
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200671 spin_lock_irqsave(&devinfo->lock, flags);
672 if (devinfo->cmnd) {
673 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200674 return SCSI_MLQUEUE_DEVICE_BUSY;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200675 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200676
677 if (blk_rq_tagged(cmnd->request)) {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100678 cmdinfo->stream = cmnd->request->tag + 2;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200679 } else {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100680 devinfo->cmnd = cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200681 cmdinfo->stream = 1;
682 }
683
684 cmnd->scsi_done = done;
685
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200686 cmdinfo->state = SUBMIT_STATUS_URB |
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200687 ALLOC_CMD_URB | SUBMIT_CMD_URB;
688
689 switch (cmnd->sc_data_direction) {
690 case DMA_FROM_DEVICE:
691 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
692 break;
693 case DMA_BIDIRECTIONAL:
694 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
695 case DMA_TO_DEVICE:
696 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
697 case DMA_NONE:
698 break;
699 }
700
701 if (!devinfo->use_streams) {
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200702 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200703 cmdinfo->stream = 0;
704 }
705
706 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
707 if (err) {
708 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500709 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200710 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200711 return SCSI_MLQUEUE_DEVICE_BUSY;
712 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200713 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200714 }
715
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200716 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200717 return 0;
718}
719
Jeff Garzikf2812332010-11-16 02:10:29 -0500720static DEF_SCSI_QCMD(uas_queuecommand)
721
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200722static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
723 const char *fname, u8 function)
724{
725 struct Scsi_Host *shost = cmnd->device->host;
726 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
Hans de Goeded3f7c152013-10-23 17:46:17 +0100727 u16 tag = devinfo->qdepth;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200728 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200729
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200730 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200731 memset(&devinfo->response, 0, sizeof(devinfo->response));
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200732 if (uas_submit_sense_urb(shost, GFP_ATOMIC, tag)) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200733 shost_printk(KERN_INFO, shost,
734 "%s: %s: submit sense urb failed\n",
735 __func__, fname);
Gerd Hoffmann1994ff42012-09-26 10:28:58 +0200736 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200737 return FAILED;
738 }
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200739 if (uas_submit_task_urb(cmnd, GFP_ATOMIC, function, tag)) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200740 shost_printk(KERN_INFO, shost,
741 "%s: %s: submit task mgmt urb failed\n",
742 __func__, fname);
Gerd Hoffmann1994ff42012-09-26 10:28:58 +0200743 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200744 return FAILED;
745 }
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200746 spin_unlock_irqrestore(&devinfo->lock, flags);
747
748 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200749 shost_printk(KERN_INFO, shost,
750 "%s: %s timed out\n", __func__, fname);
751 return FAILED;
752 }
753 if (be16_to_cpu(devinfo->response.tag) != tag) {
754 shost_printk(KERN_INFO, shost,
755 "%s: %s failed (wrong tag %d/%d)\n", __func__,
756 fname, be16_to_cpu(devinfo->response.tag), tag);
757 return FAILED;
758 }
759 if (devinfo->response.response_code != RC_TMF_COMPLETE) {
760 shost_printk(KERN_INFO, shost,
761 "%s: %s failed (rc 0x%x)\n", __func__,
762 fname, devinfo->response.response_code);
763 return FAILED;
764 }
765 return SUCCESS;
766}
767
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200768static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
769{
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200770 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200771 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
772 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200773 int ret;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200774
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200775 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200776 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200777 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
Gerd Hoffmannef018cc92012-09-25 10:47:06 +0200778 cmdinfo->state |= COMMAND_ABORTED;
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200779 list_add_tail(&cmdinfo->dead, &devinfo->dead_list);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100780 if (cmdinfo->state & IS_IN_WORK_LIST) {
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200781 list_del(&cmdinfo->work);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100782 cmdinfo->state &= ~IS_IN_WORK_LIST;
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100783 }
784 if (cmdinfo->state & COMMAND_INFLIGHT) {
785 spin_unlock_irqrestore(&devinfo->lock, flags);
786 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
787 } else {
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200788 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100789 uas_try_complete(cmnd, __func__);
790 spin_unlock_irqrestore(&devinfo->lock, flags);
791 ret = SUCCESS;
792 }
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200793 return ret;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200794}
795
796static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
797{
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200798 sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__);
799 return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET",
800 TMF_LOGICAL_UNIT_RESET);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200801}
802
803static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
804{
805 struct scsi_device *sdev = cmnd->device;
806 struct uas_dev_info *devinfo = sdev->hostdata;
807 struct usb_device *udev = devinfo->udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200808 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200809
Hans de Goedebe326f42013-09-22 16:27:02 +0200810 err = usb_lock_device_for_reset(udev, devinfo->intf);
811 if (err) {
812 shost_printk(KERN_ERR, sdev->host,
813 "%s FAILED to get lock err %d\n", __func__, err);
814 return FAILED;
815 }
816
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200817 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200818 devinfo->resetting = 1;
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100819 uas_abort_work(devinfo);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200820 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200821 usb_kill_anchored_urbs(&devinfo->sense_urbs);
822 usb_kill_anchored_urbs(&devinfo->data_urbs);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200823 uas_zap_dead(devinfo);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200824 err = usb_reset_device(udev);
825 devinfo->resetting = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200826
Hans de Goedebe326f42013-09-22 16:27:02 +0200827 usb_unlock_device(udev);
828
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200829 if (err) {
830 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
831 return FAILED;
832 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200833
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200834 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
835 return SUCCESS;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200836}
837
838static int uas_slave_alloc(struct scsi_device *sdev)
839{
840 sdev->hostdata = (void *)sdev->host->hostdata[0];
841 return 0;
842}
843
844static int uas_slave_configure(struct scsi_device *sdev)
845{
846 struct uas_dev_info *devinfo = sdev->hostdata;
847 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
Hans de Goeded3f7c152013-10-23 17:46:17 +0100848 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200849 return 0;
850}
851
852static struct scsi_host_template uas_host_template = {
853 .module = THIS_MODULE,
854 .name = "uas",
855 .queuecommand = uas_queuecommand,
856 .slave_alloc = uas_slave_alloc,
857 .slave_configure = uas_slave_configure,
858 .eh_abort_handler = uas_eh_abort_handler,
859 .eh_device_reset_handler = uas_eh_device_reset_handler,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200860 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
861 .can_queue = 65536, /* Is there a limit on the _host_ ? */
862 .this_id = -1,
863 .sg_tablesize = SG_NONE,
864 .cmd_per_lun = 1, /* until we override it */
865 .skip_settle_delay = 1,
866 .ordered_tag = 1,
867};
868
Hans de Goede79b4c062013-10-25 17:04:33 +0100869#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
870 vendorName, productName, useProtocol, useTransport, \
871 initFunction, flags) \
872{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
873 .driver_info = (flags) }
874
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200875static struct usb_device_id uas_usb_ids[] = {
Hans de Goede79b4c062013-10-25 17:04:33 +0100876# include "unusual_uas.h"
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200877 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
878 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
879 /* 0xaa is a prototype device I happen to have access to */
880 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
881 { }
882};
883MODULE_DEVICE_TABLE(usb, uas_usb_ids);
884
Hans de Goede79b4c062013-10-25 17:04:33 +0100885#undef UNUSUAL_DEV
886
Hans de Goedee1be0672013-10-21 08:00:58 +0100887static int uas_switch_interface(struct usb_device *udev,
888 struct usb_interface *intf)
889{
890 int alt;
891
892 alt = uas_find_uas_alt_setting(intf);
893 if (alt < 0)
894 return alt;
895
896 return usb_set_interface(udev,
897 intf->altsetting[0].desc.bInterfaceNumber, alt);
898}
899
Hans de Goede58d51442013-10-29 10:23:26 +0100900static int uas_configure_endpoints(struct uas_dev_info *devinfo)
Hans de Goede34f11e52013-10-29 08:54:48 +0100901{
902 struct usb_host_endpoint *eps[4] = { };
903 struct usb_device *udev = devinfo->udev;
904 int r;
905
906 devinfo->uas_sense_old = 0;
907 devinfo->cmnd = NULL;
908
909 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
Hans de Goede74d71ae2013-10-29 10:10:36 +0100910 if (r)
911 return r;
Hans de Goede34f11e52013-10-29 08:54:48 +0100912
Hans de Goede74d71ae2013-10-29 10:10:36 +0100913 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
914 usb_endpoint_num(&eps[0]->desc));
915 devinfo->status_pipe = usb_rcvbulkpipe(udev,
916 usb_endpoint_num(&eps[1]->desc));
917 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
918 usb_endpoint_num(&eps[2]->desc));
919 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
920 usb_endpoint_num(&eps[3]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200921
Hans de Goede58d51442013-10-29 10:23:26 +0100922 if (udev->speed != USB_SPEED_SUPER) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200923 devinfo->qdepth = 256;
924 devinfo->use_streams = 0;
925 } else {
Hans de Goede58d51442013-10-29 10:23:26 +0100926 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
927 3, 256, GFP_KERNEL);
928 if (devinfo->qdepth < 0)
929 return devinfo->qdepth;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200930 devinfo->use_streams = 1;
931 }
Hans de Goede58d51442013-10-29 10:23:26 +0100932
933 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200934}
935
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100936static void uas_free_streams(struct uas_dev_info *devinfo)
937{
938 struct usb_device *udev = devinfo->udev;
939 struct usb_host_endpoint *eps[3];
940
941 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
942 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
943 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
944 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
945}
946
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200947/*
948 * XXX: What I'd like to do here is register a SCSI host for each USB host in
949 * the system. Follow usb-storage's design of registering a SCSI host for
950 * each USB device for the moment. Can implement this by walking up the
951 * USB hierarchy until we find a USB host.
952 */
953static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
954{
Hans de Goede6ce82132013-10-17 19:00:45 +0200955 int result = -ENOMEM;
956 struct Scsi_Host *shost = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200957 struct uas_dev_info *devinfo;
958 struct usb_device *udev = interface_to_usbdev(intf);
959
Hans de Goede79b4c062013-10-25 17:04:33 +0100960 if (!uas_use_uas_driver(intf, id))
961 return -ENODEV;
962
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500963 if (uas_switch_interface(udev, intf))
964 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200965
966 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
967 if (!devinfo)
Hans de Goede6ce82132013-10-17 19:00:45 +0200968 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200969
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200970 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
971 if (!shost)
Hans de Goede6ce82132013-10-17 19:00:45 +0200972 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200973
974 shost->max_cmd_len = 16 + 252;
975 shost->max_id = 1;
Gerd Hoffmannbde027b2013-01-25 15:03:36 +0100976 shost->max_lun = 256;
977 shost->max_channel = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200978 shost->sg_tablesize = udev->bus->sg_tablesize;
979
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200980 devinfo->intf = intf;
981 devinfo->udev = udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200982 devinfo->resetting = 0;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200983 init_usb_anchor(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +0200984 init_usb_anchor(&devinfo->sense_urbs);
985 init_usb_anchor(&devinfo->data_urbs);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200986 spin_lock_init(&devinfo->lock);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200987 INIT_WORK(&devinfo->work, uas_do_work);
988 INIT_LIST_HEAD(&devinfo->work_list);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200989 INIT_LIST_HEAD(&devinfo->dead_list);
Hans de Goede58d51442013-10-29 10:23:26 +0100990
991 result = uas_configure_endpoints(devinfo);
992 if (result)
993 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200994
Hans de Goeded3f7c152013-10-23 17:46:17 +0100995 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100996 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +0200997 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100998
999 result = scsi_add_host(shost, &intf->dev);
1000 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +02001001 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001002
1003 shost->hostdata[0] = (unsigned long)devinfo;
1004
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001005 scsi_scan_host(shost);
1006 usb_set_intfdata(intf, shost);
1007 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001008
Hans de Goede6ce82132013-10-17 19:00:45 +02001009free_streams:
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001010 uas_free_streams(devinfo);
Hans de Goede6ce82132013-10-17 19:00:45 +02001011set_alt0:
1012 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001013 kfree(devinfo);
1014 if (shost)
1015 scsi_host_put(shost);
1016 return result;
1017}
1018
1019static int uas_pre_reset(struct usb_interface *intf)
1020{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001021 struct Scsi_Host *shost = usb_get_intfdata(intf);
1022 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
1023 unsigned long flags;
1024
1025 /* Block new requests */
1026 spin_lock_irqsave(shost->host_lock, flags);
1027 scsi_block_requests(shost);
1028 spin_unlock_irqrestore(shost->host_lock, flags);
1029
1030 /* Wait for any pending requests to complete */
1031 flush_work(&devinfo->work);
1032 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1033 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1034 return 1;
1035 }
1036
1037 uas_free_streams(devinfo);
1038
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001039 return 0;
1040}
1041
1042static int uas_post_reset(struct usb_interface *intf)
1043{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001044 struct Scsi_Host *shost = usb_get_intfdata(intf);
1045 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
1046 unsigned long flags;
1047
Hans de Goede58d51442013-10-29 10:23:26 +01001048 if (uas_configure_endpoints(devinfo) != 0) {
1049 shost_printk(KERN_ERR, shost,
1050 "%s: alloc streams error after reset", __func__);
1051 return 1;
1052 }
Hans de Goede4de7a3732013-10-22 16:10:44 +01001053
1054 spin_lock_irqsave(shost->host_lock, flags);
1055 scsi_report_bus_reset(shost, 0);
1056 spin_unlock_irqrestore(shost->host_lock, flags);
1057
1058 scsi_unblock_requests(shost);
1059
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001060 return 0;
1061}
1062
1063static void uas_disconnect(struct usb_interface *intf)
1064{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001065 struct Scsi_Host *shost = usb_get_intfdata(intf);
1066 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
1067
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001068 devinfo->resetting = 1;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001069 cancel_work_sync(&devinfo->work);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001070 uas_abort_work(devinfo);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001071 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001072 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1073 usb_kill_anchored_urbs(&devinfo->data_urbs);
Gerd Hoffmann326349f2013-09-13 13:27:13 +02001074 uas_zap_dead(devinfo);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001075 scsi_remove_host(shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001076 uas_free_streams(devinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001077 kfree(devinfo);
1078}
1079
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001080static struct usb_driver uas_driver = {
1081 .name = "uas",
1082 .probe = uas_probe,
1083 .disconnect = uas_disconnect,
1084 .pre_reset = uas_pre_reset,
1085 .post_reset = uas_post_reset,
1086 .id_table = uas_usb_ids,
1087};
1088
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001089module_usb_driver(uas_driver);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001090
1091MODULE_LICENSE("GPL");
1092MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");