blob: e817e72d86732faf490033d6af67371ecbbfd4e9 [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_configure_endpoints(struct uas_dev_info *devinfo);
97static void uas_free_streams(struct uas_dev_info *devinfo);
Gerd Hoffmann326349f2013-09-13 13:27:13 +020098static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020099
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200100/* Must be called with devinfo->lock held, will temporary unlock the lock */
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100101static void uas_unlink_data_urbs(struct uas_dev_info *devinfo,
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200102 struct uas_cmd_info *cmdinfo,
103 unsigned long *lock_flags)
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100104{
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100105 /*
106 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
107 * (called by urb completion) doesn't release cmdinfo
108 * underneath us.
109 */
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100110 cmdinfo->state |= UNLINK_DATA_URBS;
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200111 spin_unlock_irqrestore(&devinfo->lock, *lock_flags);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100112
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100113 if (cmdinfo->data_in_urb)
114 usb_unlink_urb(cmdinfo->data_in_urb);
115 if (cmdinfo->data_out_urb)
116 usb_unlink_urb(cmdinfo->data_out_urb);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100117
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200118 spin_lock_irqsave(&devinfo->lock, *lock_flags);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100119 cmdinfo->state &= ~UNLINK_DATA_URBS;
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100120}
121
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200122static void uas_do_work(struct work_struct *work)
123{
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200124 struct uas_dev_info *devinfo =
125 container_of(work, struct uas_dev_info, work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200126 struct uas_cmd_info *cmdinfo;
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800127 struct uas_cmd_info *temp;
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);
132 list_for_each_entry_safe(cmdinfo, temp, &devinfo->work_list, work) {
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);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200136 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200137 if (!err) {
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100138 cmdinfo->state &= ~IS_IN_WORK_LIST;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200139 list_del(&cmdinfo->work);
140 } else {
141 schedule_work(&devinfo->work);
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800142 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200143 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200144 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200145}
146
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100147static void uas_abort_work(struct uas_dev_info *devinfo)
148{
149 struct uas_cmd_info *cmdinfo;
150 struct uas_cmd_info *temp;
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100151 unsigned long flags;
152
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100153 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200154 list_for_each_entry_safe(cmdinfo, temp, &devinfo->work_list, work) {
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100155 struct scsi_pointer *scp = (void *)cmdinfo;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200156 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
157 SCp);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200158 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200159 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200160 cmdinfo->state |= COMMAND_ABORTED;
161 cmdinfo->state &= ~IS_IN_WORK_LIST;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200162 list_del(&cmdinfo->work);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200163 list_add_tail(&cmdinfo->dead, &devinfo->dead_list);
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100164 }
165 spin_unlock_irqrestore(&devinfo->lock, flags);
166}
167
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200168static void uas_add_work(struct uas_cmd_info *cmdinfo)
169{
170 struct scsi_pointer *scp = (void *)cmdinfo;
171 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
172 struct uas_dev_info *devinfo = cmnd->device->hostdata;
173
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200174 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200175 list_add_tail(&cmdinfo->work, &devinfo->work_list);
176 cmdinfo->state |= IS_IN_WORK_LIST;
177 schedule_work(&devinfo->work);
178}
179
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200180static void uas_zap_dead(struct uas_dev_info *devinfo)
181{
182 struct uas_cmd_info *cmdinfo;
183 struct uas_cmd_info *temp;
184 unsigned long flags;
185
186 spin_lock_irqsave(&devinfo->lock, flags);
187 list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, dead) {
188 struct scsi_pointer *scp = (void *)cmdinfo;
189 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
190 SCp);
191 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200192 WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED));
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200193 /* all urbs are killed, clear inflight bits */
194 cmdinfo->state &= ~(COMMAND_INFLIGHT |
195 DATA_IN_URB_INFLIGHT |
196 DATA_OUT_URB_INFLIGHT);
197 uas_try_complete(cmnd, __func__);
198 }
199 spin_unlock_irqrestore(&devinfo->lock, flags);
200}
201
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200202static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
203{
204 struct sense_iu *sense_iu = urb->transfer_buffer;
205 struct scsi_device *sdev = cmnd->device;
206
207 if (urb->actual_length > 16) {
208 unsigned len = be16_to_cpup(&sense_iu->len);
209 if (len + 16 != urb->actual_length) {
210 int newlen = min(len + 16, urb->actual_length) - 16;
211 if (newlen < 0)
212 newlen = 0;
213 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
214 "disagrees with IU sense data length %d, "
215 "using %d bytes of sense data\n", __func__,
216 urb->actual_length, len, newlen);
217 len = newlen;
218 }
219 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
220 }
221
222 cmnd->result = sense_iu->status;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200223}
224
225static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
226{
227 struct sense_iu_old *sense_iu = urb->transfer_buffer;
228 struct scsi_device *sdev = cmnd->device;
229
230 if (urb->actual_length > 8) {
231 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
232 if (len + 8 != urb->actual_length) {
233 int newlen = min(len + 8, urb->actual_length) - 8;
234 if (newlen < 0)
235 newlen = 0;
236 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
237 "disagrees with IU sense data length %d, "
238 "using %d bytes of sense data\n", __func__,
239 urb->actual_length, len, newlen);
240 len = newlen;
241 }
242 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
243 }
244
245 cmnd->result = sense_iu->status;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200246}
247
248static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
249{
250 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
251
252 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100253 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200254 caller, cmnd, cmnd->request->tag,
255 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
256 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
257 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
258 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
259 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
260 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
261 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
262 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
263 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
264 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
Gerd Hoffmannef018cc92012-09-25 10:47:06 +0200265 (ci->state & COMMAND_COMPLETED) ? " done" : "",
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100266 (ci->state & COMMAND_ABORTED) ? " abort" : "",
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100267 (ci->state & UNLINK_DATA_URBS) ? " unlink": "",
268 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200269}
270
271static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
272{
273 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200274 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200275
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200276 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200277 if (cmdinfo->state & (COMMAND_INFLIGHT |
278 DATA_IN_URB_INFLIGHT |
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100279 DATA_OUT_URB_INFLIGHT |
280 UNLINK_DATA_URBS))
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200281 return -EBUSY;
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200282 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200283 cmdinfo->state |= COMMAND_COMPLETED;
284 usb_free_urb(cmdinfo->data_in_urb);
285 usb_free_urb(cmdinfo->data_out_urb);
Gerd Hoffmann0871d7d2012-09-25 10:47:07 +0200286 if (cmdinfo->state & COMMAND_ABORTED) {
287 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
288 cmnd->result = DID_ABORT << 16;
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200289 list_del(&cmdinfo->dead);
Gerd Hoffmann0871d7d2012-09-25 10:47:07 +0200290 }
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200291 cmnd->scsi_done(cmnd);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200292 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200293}
294
295static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200296 unsigned direction)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200297{
298 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
299 int err;
300
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200301 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200302 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
303 if (err) {
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200304 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200305 }
306}
307
308static void uas_stat_cmplt(struct urb *urb)
309{
310 struct iu *iu = urb->transfer_buffer;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100311 struct Scsi_Host *shost = urb->context;
312 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200313 struct scsi_cmnd *cmnd;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200314 struct uas_cmd_info *cmdinfo;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200315 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200316 u16 tag;
317
318 if (urb->status) {
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200319 if (urb->status == -ENOENT) {
320 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
321 urb->stream_id);
322 } else {
323 dev_err(&urb->dev->dev, "stat urb: status %d\n",
324 urb->status);
325 }
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200326 usb_free_urb(urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200327 return;
328 }
329
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200330 if (devinfo->resetting) {
331 usb_free_urb(urb);
332 return;
333 }
334
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200335 spin_lock_irqsave(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200336 tag = be16_to_cpup(&iu->tag) - 1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100337 if (tag == 0)
338 cmnd = devinfo->cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200339 else
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100340 cmnd = scsi_host_find_tag(shost, tag - 1);
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200341
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800342 if (!cmnd) {
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200343 if (iu->iu_id == IU_ID_RESPONSE) {
344 /* store results for uas_eh_task_mgmt() */
345 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200346 }
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200347 usb_free_urb(urb);
348 spin_unlock_irqrestore(&devinfo->lock, flags);
349 return;
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800350 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200351
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200352 cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200353 switch (iu->iu_id) {
354 case IU_ID_STATUS:
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100355 if (devinfo->cmnd == cmnd)
356 devinfo->cmnd = NULL;
357
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200358 if (urb->actual_length < 16)
359 devinfo->uas_sense_old = 1;
360 if (devinfo->uas_sense_old)
361 uas_sense_old(urb, cmnd);
362 else
363 uas_sense(urb, cmnd);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200364 if (cmnd->result != 0) {
365 /* cancel data transfers on error */
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200366 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200367 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200368 cmdinfo->state &= ~COMMAND_INFLIGHT;
369 uas_try_complete(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200370 break;
371 case IU_ID_READ_READY:
372 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
373 break;
374 case IU_ID_WRITE_READY:
375 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
376 break;
377 default:
378 scmd_printk(KERN_ERR, cmnd,
379 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
380 }
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200381 usb_free_urb(urb);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200382 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200383}
384
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200385static void uas_data_cmplt(struct urb *urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200386{
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200387 struct scsi_cmnd *cmnd = urb->context;
388 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200389 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200390 struct scsi_data_buffer *sdb = NULL;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200391 unsigned long flags;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200392
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200393 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200394 if (cmdinfo->data_in_urb == urb) {
395 sdb = scsi_in(cmnd);
396 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
397 } else if (cmdinfo->data_out_urb == urb) {
398 sdb = scsi_out(cmnd);
399 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
400 }
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200401 if (sdb == NULL) {
402 WARN_ON_ONCE(1);
403 } else if (urb->status) {
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200404 /* error: no data transfered */
405 sdb->resid = sdb->length;
406 } else {
407 sdb->resid = sdb->length - urb->actual_length;
408 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200409 uas_try_complete(cmnd, __func__);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200410 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200411}
412
413static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200414 unsigned int pipe, u16 stream_id,
415 struct scsi_cmnd *cmnd,
416 enum dma_data_direction dir)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200417{
418 struct usb_device *udev = devinfo->udev;
419 struct urb *urb = usb_alloc_urb(0, gfp);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200420 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
421 ? scsi_in(cmnd) : scsi_out(cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200422
423 if (!urb)
424 goto out;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200425 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
426 uas_data_cmplt, cmnd);
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200427 if (devinfo->use_streams)
428 urb->stream_id = stream_id;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200429 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
430 urb->sg = sdb->table.sgl;
431 out:
432 return urb;
433}
434
435static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200436 struct Scsi_Host *shost, u16 stream_id)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200437{
438 struct usb_device *udev = devinfo->udev;
439 struct urb *urb = usb_alloc_urb(0, gfp);
440 struct sense_iu *iu;
441
442 if (!urb)
443 goto out;
444
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500445 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200446 if (!iu)
447 goto free;
448
449 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200450 uas_stat_cmplt, shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200451 urb->stream_id = stream_id;
452 urb->transfer_flags |= URB_FREE_BUFFER;
453 out:
454 return urb;
455 free:
456 usb_free_urb(urb);
457 return NULL;
458}
459
460static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Hans de Goedea887cd32013-10-17 19:47:28 +0200461 struct scsi_cmnd *cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200462{
463 struct usb_device *udev = devinfo->udev;
464 struct scsi_device *sdev = cmnd->device;
465 struct urb *urb = usb_alloc_urb(0, gfp);
466 struct command_iu *iu;
467 int len;
468
469 if (!urb)
470 goto out;
471
472 len = cmnd->cmd_len - 16;
473 if (len < 0)
474 len = 0;
475 len = ALIGN(len, 4);
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500476 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200477 if (!iu)
478 goto free;
479
480 iu->iu_id = IU_ID_COMMAND;
Sarah Sharp9eb44542011-12-02 11:55:46 -0800481 if (blk_rq_tagged(cmnd->request))
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100482 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
Sarah Sharp9eb44542011-12-02 11:55:46 -0800483 else
484 iu->tag = cpu_to_be16(1);
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100485 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200486 iu->len = len;
487 int_to_scsilun(sdev->lun, &iu->lun);
488 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
489
490 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
491 usb_free_urb, NULL);
492 urb->transfer_flags |= URB_FREE_BUFFER;
493 out:
494 return urb;
495 free:
496 usb_free_urb(urb);
497 return NULL;
498}
499
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200500static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
501 u8 function, u16 stream_id)
502{
503 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
504 struct usb_device *udev = devinfo->udev;
505 struct urb *urb = usb_alloc_urb(0, gfp);
506 struct task_mgmt_iu *iu;
507 int err = -ENOMEM;
508
509 if (!urb)
510 goto err;
511
512 iu = kzalloc(sizeof(*iu), gfp);
513 if (!iu)
514 goto err;
515
516 iu->iu_id = IU_ID_TASK_MGMT;
517 iu->tag = cpu_to_be16(stream_id);
518 int_to_scsilun(cmnd->device->lun, &iu->lun);
519
520 iu->function = function;
521 switch (function) {
522 case TMF_ABORT_TASK:
523 if (blk_rq_tagged(cmnd->request))
524 iu->task_tag = cpu_to_be16(cmnd->request->tag + 2);
525 else
526 iu->task_tag = cpu_to_be16(1);
527 break;
528 }
529
530 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
531 usb_free_urb, NULL);
532 urb->transfer_flags |= URB_FREE_BUFFER;
533
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200534 usb_anchor_urb(urb, &devinfo->cmd_urbs);
Hans de Goeded5f808d2013-10-17 19:30:26 +0200535 err = usb_submit_urb(urb, gfp);
536 if (err) {
537 usb_unanchor_urb(urb);
538 goto err;
539 }
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200540
541 return 0;
542
543err:
544 usb_free_urb(urb);
545 return err;
546}
547
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200548/*
549 * Why should I request the Status IU before sending the Command IU? Spec
550 * says to, but also says the device may receive them in any order. Seems
551 * daft to me.
552 */
553
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200554static int uas_submit_sense_urb(struct Scsi_Host *shost,
555 gfp_t gfp, unsigned int stream)
556{
557 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
558 struct urb *urb;
559
560 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
561 if (!urb)
562 return SCSI_MLQUEUE_DEVICE_BUSY;
Hans de Goeded5f808d2013-10-17 19:30:26 +0200563 usb_anchor_urb(urb, &devinfo->sense_urbs);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200564 if (usb_submit_urb(urb, gfp)) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200565 usb_unanchor_urb(urb);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200566 shost_printk(KERN_INFO, shost,
567 "sense urb submission failure\n");
568 usb_free_urb(urb);
569 return SCSI_MLQUEUE_DEVICE_BUSY;
570 }
571 return 0;
572}
573
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200574static int uas_submit_urbs(struct scsi_cmnd *cmnd,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200575 struct uas_dev_info *devinfo, gfp_t gfp)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200576{
577 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200578 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200579
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200580 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500581 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200582 err = uas_submit_sense_urb(cmnd->device->host, gfp,
583 cmdinfo->stream);
584 if (err) {
585 return err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200586 }
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500587 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200588 }
589
590 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
591 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200592 devinfo->data_in_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200593 cmnd, DMA_FROM_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200594 if (!cmdinfo->data_in_urb)
595 return SCSI_MLQUEUE_DEVICE_BUSY;
596 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
597 }
598
599 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200600 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200601 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200602 usb_unanchor_urb(cmdinfo->data_in_urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200603 scmd_printk(KERN_INFO, cmnd,
604 "data in urb submission failure\n");
605 return SCSI_MLQUEUE_DEVICE_BUSY;
606 }
607 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200608 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200609 }
610
611 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
612 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200613 devinfo->data_out_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200614 cmnd, DMA_TO_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200615 if (!cmdinfo->data_out_urb)
616 return SCSI_MLQUEUE_DEVICE_BUSY;
617 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
618 }
619
620 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200621 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200622 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200623 usb_unanchor_urb(cmdinfo->data_out_urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200624 scmd_printk(KERN_INFO, cmnd,
625 "data out urb submission failure\n");
626 return SCSI_MLQUEUE_DEVICE_BUSY;
627 }
628 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200629 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200630 }
631
632 if (cmdinfo->state & ALLOC_CMD_URB) {
Hans de Goedea887cd32013-10-17 19:47:28 +0200633 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200634 if (!cmdinfo->cmd_urb)
635 return SCSI_MLQUEUE_DEVICE_BUSY;
636 cmdinfo->state &= ~ALLOC_CMD_URB;
637 }
638
639 if (cmdinfo->state & SUBMIT_CMD_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200640 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200641 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200642 usb_unanchor_urb(cmdinfo->cmd_urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200643 scmd_printk(KERN_INFO, cmnd,
644 "cmd urb submission failure\n");
645 return SCSI_MLQUEUE_DEVICE_BUSY;
646 }
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200647 cmdinfo->cmd_urb = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200648 cmdinfo->state &= ~SUBMIT_CMD_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200649 cmdinfo->state |= COMMAND_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200650 }
651
652 return 0;
653}
654
Jeff Garzikf2812332010-11-16 02:10:29 -0500655static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200656 void (*done)(struct scsi_cmnd *))
657{
658 struct scsi_device *sdev = cmnd->device;
659 struct uas_dev_info *devinfo = sdev->hostdata;
660 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200661 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200662 int err;
663
664 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
665
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100666 if (devinfo->resetting) {
667 cmnd->result = DID_ERROR << 16;
668 cmnd->scsi_done(cmnd);
669 return 0;
670 }
671
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200672 spin_lock_irqsave(&devinfo->lock, flags);
673 if (devinfo->cmnd) {
674 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200675 return SCSI_MLQUEUE_DEVICE_BUSY;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200676 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200677
678 if (blk_rq_tagged(cmnd->request)) {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100679 cmdinfo->stream = cmnd->request->tag + 2;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200680 } else {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100681 devinfo->cmnd = cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200682 cmdinfo->stream = 1;
683 }
684
685 cmnd->scsi_done = done;
686
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200687 cmdinfo->state = SUBMIT_STATUS_URB |
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200688 ALLOC_CMD_URB | SUBMIT_CMD_URB;
689
690 switch (cmnd->sc_data_direction) {
691 case DMA_FROM_DEVICE:
692 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
693 break;
694 case DMA_BIDIRECTIONAL:
695 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
696 case DMA_TO_DEVICE:
697 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
698 case DMA_NONE:
699 break;
700 }
701
702 if (!devinfo->use_streams) {
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200703 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200704 cmdinfo->stream = 0;
705 }
706
707 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
708 if (err) {
709 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500710 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200711 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200712 return SCSI_MLQUEUE_DEVICE_BUSY;
713 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200714 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200715 }
716
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200717 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200718 return 0;
719}
720
Jeff Garzikf2812332010-11-16 02:10:29 -0500721static DEF_SCSI_QCMD(uas_queuecommand)
722
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200723static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
724 const char *fname, u8 function)
725{
726 struct Scsi_Host *shost = cmnd->device->host;
727 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
Hans de Goeded3f7c152013-10-23 17:46:17 +0100728 u16 tag = devinfo->qdepth;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200729 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200730
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200731 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200732 memset(&devinfo->response, 0, sizeof(devinfo->response));
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200733 if (uas_submit_sense_urb(shost, GFP_ATOMIC, tag)) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200734 shost_printk(KERN_INFO, shost,
735 "%s: %s: submit sense urb failed\n",
736 __func__, fname);
Gerd Hoffmann1994ff42012-09-26 10:28:58 +0200737 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200738 return FAILED;
739 }
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200740 if (uas_submit_task_urb(cmnd, GFP_ATOMIC, function, tag)) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200741 shost_printk(KERN_INFO, shost,
742 "%s: %s: submit task mgmt urb failed\n",
743 __func__, fname);
Gerd Hoffmann1994ff42012-09-26 10:28:58 +0200744 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200745 return FAILED;
746 }
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200747 spin_unlock_irqrestore(&devinfo->lock, flags);
748
749 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200750 shost_printk(KERN_INFO, shost,
751 "%s: %s timed out\n", __func__, fname);
752 return FAILED;
753 }
754 if (be16_to_cpu(devinfo->response.tag) != tag) {
755 shost_printk(KERN_INFO, shost,
756 "%s: %s failed (wrong tag %d/%d)\n", __func__,
757 fname, be16_to_cpu(devinfo->response.tag), tag);
758 return FAILED;
759 }
760 if (devinfo->response.response_code != RC_TMF_COMPLETE) {
761 shost_printk(KERN_INFO, shost,
762 "%s: %s failed (rc 0x%x)\n", __func__,
763 fname, devinfo->response.response_code);
764 return FAILED;
765 }
766 return SUCCESS;
767}
768
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200769static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
770{
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200771 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200772 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
773 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200774 int ret;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200775
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200776 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200777 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200778 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
Gerd Hoffmannef018cc92012-09-25 10:47:06 +0200779 cmdinfo->state |= COMMAND_ABORTED;
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200780 list_add_tail(&cmdinfo->dead, &devinfo->dead_list);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100781 if (cmdinfo->state & IS_IN_WORK_LIST) {
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200782 list_del(&cmdinfo->work);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100783 cmdinfo->state &= ~IS_IN_WORK_LIST;
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100784 }
785 if (cmdinfo->state & COMMAND_INFLIGHT) {
786 spin_unlock_irqrestore(&devinfo->lock, flags);
787 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
788 } else {
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200789 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100790 uas_try_complete(cmnd, __func__);
791 spin_unlock_irqrestore(&devinfo->lock, flags);
792 ret = SUCCESS;
793 }
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200794 return ret;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200795}
796
797static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
798{
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200799 sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__);
800 return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET",
801 TMF_LOGICAL_UNIT_RESET);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200802}
803
804static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
805{
806 struct scsi_device *sdev = cmnd->device;
807 struct uas_dev_info *devinfo = sdev->hostdata;
808 struct usb_device *udev = devinfo->udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200809 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200810
Hans de Goedebe326f42013-09-22 16:27:02 +0200811 err = usb_lock_device_for_reset(udev, devinfo->intf);
812 if (err) {
813 shost_printk(KERN_ERR, sdev->host,
814 "%s FAILED to get lock err %d\n", __func__, err);
815 return FAILED;
816 }
817
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200818 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200819 devinfo->resetting = 1;
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100820 uas_abort_work(devinfo);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200821 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200822 usb_kill_anchored_urbs(&devinfo->sense_urbs);
823 usb_kill_anchored_urbs(&devinfo->data_urbs);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200824 uas_zap_dead(devinfo);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200825 err = usb_reset_device(udev);
826 devinfo->resetting = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200827
Hans de Goedebe326f42013-09-22 16:27:02 +0200828 usb_unlock_device(udev);
829
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200830 if (err) {
831 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
832 return FAILED;
833 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200834
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200835 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
836 return SUCCESS;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200837}
838
839static int uas_slave_alloc(struct scsi_device *sdev)
840{
841 sdev->hostdata = (void *)sdev->host->hostdata[0];
842 return 0;
843}
844
845static int uas_slave_configure(struct scsi_device *sdev)
846{
847 struct uas_dev_info *devinfo = sdev->hostdata;
848 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
Hans de Goeded3f7c152013-10-23 17:46:17 +0100849 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200850 return 0;
851}
852
853static struct scsi_host_template uas_host_template = {
854 .module = THIS_MODULE,
855 .name = "uas",
856 .queuecommand = uas_queuecommand,
857 .slave_alloc = uas_slave_alloc,
858 .slave_configure = uas_slave_configure,
859 .eh_abort_handler = uas_eh_abort_handler,
860 .eh_device_reset_handler = uas_eh_device_reset_handler,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200861 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
862 .can_queue = 65536, /* Is there a limit on the _host_ ? */
863 .this_id = -1,
864 .sg_tablesize = SG_NONE,
865 .cmd_per_lun = 1, /* until we override it */
866 .skip_settle_delay = 1,
867 .ordered_tag = 1,
868};
869
Hans de Goede79b4c062013-10-25 17:04:33 +0100870#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
871 vendorName, productName, useProtocol, useTransport, \
872 initFunction, flags) \
873{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
874 .driver_info = (flags) }
875
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200876static struct usb_device_id uas_usb_ids[] = {
Hans de Goede79b4c062013-10-25 17:04:33 +0100877# include "unusual_uas.h"
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200878 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
879 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
880 /* 0xaa is a prototype device I happen to have access to */
881 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
882 { }
883};
884MODULE_DEVICE_TABLE(usb, uas_usb_ids);
885
Hans de Goede79b4c062013-10-25 17:04:33 +0100886#undef UNUSUAL_DEV
887
Hans de Goedee1be0672013-10-21 08:00:58 +0100888static int uas_switch_interface(struct usb_device *udev,
889 struct usb_interface *intf)
890{
891 int alt;
892
893 alt = uas_find_uas_alt_setting(intf);
894 if (alt < 0)
895 return alt;
896
897 return usb_set_interface(udev,
898 intf->altsetting[0].desc.bInterfaceNumber, alt);
899}
900
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200901static void uas_configure_endpoints(struct uas_dev_info *devinfo)
902{
903 struct usb_host_endpoint *eps[4] = { };
904 struct usb_interface *intf = devinfo->intf;
905 struct usb_device *udev = devinfo->udev;
906 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
907 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
908
909 devinfo->uas_sense_old = 0;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100910 devinfo->cmnd = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200911
912 for (i = 0; i < n_endpoints; i++) {
913 unsigned char *extra = endpoint[i].extra;
914 int len = endpoint[i].extralen;
915 while (len > 1) {
916 if (extra[1] == USB_DT_PIPE_USAGE) {
917 unsigned pipe_id = extra[2];
918 if (pipe_id > 0 && pipe_id < 5)
919 eps[pipe_id - 1] = &endpoint[i];
920 break;
921 }
922 len -= extra[0];
923 extra += extra[0];
924 }
925 }
926
927 /*
928 * Assume that if we didn't find a control pipe descriptor, we're
929 * using a device with old firmware that happens to be set up like
930 * this.
931 */
932 if (!eps[0]) {
933 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
934 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
935 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
936 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
937
938 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
939 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
940 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
941 } else {
942 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
Hans de Goede6c2334e2013-10-17 22:20:54 +0200943 usb_endpoint_num(&eps[0]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200944 devinfo->status_pipe = usb_rcvbulkpipe(udev,
Hans de Goede6c2334e2013-10-17 22:20:54 +0200945 usb_endpoint_num(&eps[1]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200946 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
Hans de Goede6c2334e2013-10-17 22:20:54 +0200947 usb_endpoint_num(&eps[2]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200948 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
Hans de Goede6c2334e2013-10-17 22:20:54 +0200949 usb_endpoint_num(&eps[3]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200950 }
951
952 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
953 GFP_KERNEL);
954 if (devinfo->qdepth < 0) {
955 devinfo->qdepth = 256;
956 devinfo->use_streams = 0;
957 } else {
958 devinfo->use_streams = 1;
959 }
960}
961
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100962static void uas_free_streams(struct uas_dev_info *devinfo)
963{
964 struct usb_device *udev = devinfo->udev;
965 struct usb_host_endpoint *eps[3];
966
967 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
968 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
969 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
970 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
971}
972
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200973/*
974 * XXX: What I'd like to do here is register a SCSI host for each USB host in
975 * the system. Follow usb-storage's design of registering a SCSI host for
976 * each USB device for the moment. Can implement this by walking up the
977 * USB hierarchy until we find a USB host.
978 */
979static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
980{
Hans de Goede6ce82132013-10-17 19:00:45 +0200981 int result = -ENOMEM;
982 struct Scsi_Host *shost = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200983 struct uas_dev_info *devinfo;
984 struct usb_device *udev = interface_to_usbdev(intf);
985
Hans de Goede79b4c062013-10-25 17:04:33 +0100986 if (!uas_use_uas_driver(intf, id))
987 return -ENODEV;
988
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500989 if (uas_switch_interface(udev, intf))
990 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200991
992 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
993 if (!devinfo)
Hans de Goede6ce82132013-10-17 19:00:45 +0200994 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200995
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200996 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
997 if (!shost)
Hans de Goede6ce82132013-10-17 19:00:45 +0200998 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200999
1000 shost->max_cmd_len = 16 + 252;
1001 shost->max_id = 1;
Gerd Hoffmannbde027b2013-01-25 15:03:36 +01001002 shost->max_lun = 256;
1003 shost->max_channel = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001004 shost->sg_tablesize = udev->bus->sg_tablesize;
1005
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001006 devinfo->intf = intf;
1007 devinfo->udev = udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +02001008 devinfo->resetting = 0;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001009 init_usb_anchor(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001010 init_usb_anchor(&devinfo->sense_urbs);
1011 init_usb_anchor(&devinfo->data_urbs);
Gerd Hoffmanne0648522012-09-25 10:47:08 +02001012 spin_lock_init(&devinfo->lock);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001013 INIT_WORK(&devinfo->work, uas_do_work);
1014 INIT_LIST_HEAD(&devinfo->work_list);
Gerd Hoffmann326349f2013-09-13 13:27:13 +02001015 INIT_LIST_HEAD(&devinfo->dead_list);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001016 uas_configure_endpoints(devinfo);
1017
Hans de Goeded3f7c152013-10-23 17:46:17 +01001018 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001019 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +02001020 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001021
1022 result = scsi_add_host(shost, &intf->dev);
1023 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +02001024 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001025
1026 shost->hostdata[0] = (unsigned long)devinfo;
1027
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001028 scsi_scan_host(shost);
1029 usb_set_intfdata(intf, shost);
1030 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001031
Hans de Goede6ce82132013-10-17 19:00:45 +02001032free_streams:
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001033 uas_free_streams(devinfo);
Hans de Goede6ce82132013-10-17 19:00:45 +02001034set_alt0:
1035 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001036 kfree(devinfo);
1037 if (shost)
1038 scsi_host_put(shost);
1039 return result;
1040}
1041
1042static int uas_pre_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
1048 /* Block new requests */
1049 spin_lock_irqsave(shost->host_lock, flags);
1050 scsi_block_requests(shost);
1051 spin_unlock_irqrestore(shost->host_lock, flags);
1052
1053 /* Wait for any pending requests to complete */
1054 flush_work(&devinfo->work);
1055 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1056 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1057 return 1;
1058 }
1059
1060 uas_free_streams(devinfo);
1061
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001062 return 0;
1063}
1064
1065static int uas_post_reset(struct usb_interface *intf)
1066{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001067 struct Scsi_Host *shost = usb_get_intfdata(intf);
1068 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
1069 unsigned long flags;
1070
1071 uas_configure_endpoints(devinfo);
1072
1073 spin_lock_irqsave(shost->host_lock, flags);
1074 scsi_report_bus_reset(shost, 0);
1075 spin_unlock_irqrestore(shost->host_lock, flags);
1076
1077 scsi_unblock_requests(shost);
1078
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001079 return 0;
1080}
1081
1082static void uas_disconnect(struct usb_interface *intf)
1083{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001084 struct Scsi_Host *shost = usb_get_intfdata(intf);
1085 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
1086
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001087 devinfo->resetting = 1;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001088 cancel_work_sync(&devinfo->work);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001089 uas_abort_work(devinfo);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001090 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001091 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1092 usb_kill_anchored_urbs(&devinfo->data_urbs);
Gerd Hoffmann326349f2013-09-13 13:27:13 +02001093 uas_zap_dead(devinfo);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001094 scsi_remove_host(shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001095 uas_free_streams(devinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001096 kfree(devinfo);
1097}
1098
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001099static struct usb_driver uas_driver = {
1100 .name = "uas",
1101 .probe = uas_probe,
1102 .disconnect = uas_disconnect,
1103 .pre_reset = uas_pre_reset,
1104 .post_reset = uas_post_reset,
1105 .id_table = uas_usb_ids,
1106};
1107
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001108module_usb_driver(uas_driver);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001109
1110MODULE_LICENSE("GPL");
1111MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");