uas: Improve error reporting
[linux-2.6-block.git] / drivers / usb / storage / uas.c
CommitLineData
115bb1ff
MW
1/*
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>
6eb0de82 14#include <linux/module.h>
115bb1ff 15#include <linux/usb.h>
79b4c061 16#include <linux/usb_usual.h>
c898add5 17#include <linux/usb/hcd.h>
115bb1ff 18#include <linux/usb/storage.h>
348748b0 19#include <linux/usb/uas.h>
115bb1ff
MW
20
21#include <scsi/scsi.h>
4de7a373 22#include <scsi/scsi_eh.h>
115bb1ff
MW
23#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
82aa0387
HG
29#include "uas-detect.h"
30
115bb1ff
MW
31/*
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
115bb1ff
MW
45struct uas_dev_info {
46 struct usb_interface *intf;
47 struct usb_device *udev;
a0e39e34 48 struct usb_anchor cmd_urbs;
bdd000fb
GH
49 struct usb_anchor sense_urbs;
50 struct usb_anchor data_urbs;
023b515e 51 int qdepth, resetting;
e52e0314 52 struct response_iu response;
115bb1ff
MW
53 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
54 unsigned use_streams:1;
55 unsigned uas_sense_old:1;
b83b86a3 56 unsigned running_task:1;
da65c2bb 57 unsigned shutdown:1;
22188f4a 58 struct scsi_cmnd *cmnd;
e0648520 59 spinlock_t lock;
1bf8198e 60 struct work_struct work;
61c09ce5 61 struct list_head inflight_list;
326349f8 62 struct list_head dead_list;
115bb1ff
MW
63};
64
65enum {
92a3f767 66 SUBMIT_STATUS_URB = (1 << 1),
115bb1ff
MW
67 ALLOC_DATA_IN_URB = (1 << 2),
68 SUBMIT_DATA_IN_URB = (1 << 3),
69 ALLOC_DATA_OUT_URB = (1 << 4),
70 SUBMIT_DATA_OUT_URB = (1 << 5),
71 ALLOC_CMD_URB = (1 << 6),
72 SUBMIT_CMD_URB = (1 << 7),
b1d67693
GH
73 COMMAND_INFLIGHT = (1 << 8),
74 DATA_IN_URB_INFLIGHT = (1 << 9),
75 DATA_OUT_URB_INFLIGHT = (1 << 10),
76 COMMAND_COMPLETED = (1 << 11),
ef018cc9 77 COMMAND_ABORTED = (1 << 12),
b06e48af 78 UNLINK_DATA_URBS = (1 << 13),
efefecf3 79 IS_IN_WORK_LIST = (1 << 14),
115bb1ff
MW
80};
81
82/* Overrides scsi_pointer */
83struct uas_cmd_info {
84 unsigned int state;
85 unsigned int stream;
86 struct urb *cmd_urb;
115bb1ff
MW
87 struct urb *data_in_urb;
88 struct urb *data_out_urb;
040d1a8f 89 struct list_head list;
115bb1ff
MW
90};
91
92/* I hate forward declarations, but I actually have a loop */
93static int uas_submit_urbs(struct scsi_cmnd *cmnd,
94 struct uas_dev_info *devinfo, gfp_t gfp);
ea9da1c7 95static void uas_do_work(struct work_struct *work);
4c456971 96static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
d89bd835 97static void uas_free_streams(struct uas_dev_info *devinfo);
326349f8 98static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller);
115bb1ff 99
7e50e0be 100/* Must be called with devinfo->lock held, will temporary unlock the lock */
aa8f6123 101static void uas_unlink_data_urbs(struct uas_dev_info *devinfo,
7e50e0be
HG
102 struct uas_cmd_info *cmdinfo,
103 unsigned long *lock_flags)
aa8f6123 104{
b06e48af
GH
105 /*
106 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
107 * (called by urb completion) doesn't release cmdinfo
108 * underneath us.
109 */
b06e48af 110 cmdinfo->state |= UNLINK_DATA_URBS;
7e50e0be 111 spin_unlock_irqrestore(&devinfo->lock, *lock_flags);
b06e48af 112
aa8f6123
GH
113 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);
b06e48af 117
7e50e0be 118 spin_lock_irqsave(&devinfo->lock, *lock_flags);
b06e48af 119 cmdinfo->state &= ~UNLINK_DATA_URBS;
aa8f6123
GH
120}
121
115bb1ff
MW
122static void uas_do_work(struct work_struct *work)
123{
1bf8198e
GH
124 struct uas_dev_info *devinfo =
125 container_of(work, struct uas_dev_info, work);
115bb1ff 126 struct uas_cmd_info *cmdinfo;
e0648520 127 unsigned long flags;
ea9da1c7 128 int err;
115bb1ff 129
1bf8198e 130 spin_lock_irqsave(&devinfo->lock, flags);
040d1a8f 131 list_for_each_entry(cmdinfo, &devinfo->inflight_list, list) {
115bb1ff 132 struct scsi_pointer *scp = (void *)cmdinfo;
1bf8198e
GH
133 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
134 SCp);
61c09ce5
HG
135
136 if (!(cmdinfo->state & IS_IN_WORK_LIST))
137 continue;
138
e36e6493 139 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
61c09ce5 140 if (!err)
efefecf3 141 cmdinfo->state &= ~IS_IN_WORK_LIST;
61c09ce5 142 else
1bf8198e 143 schedule_work(&devinfo->work);
115bb1ff 144 }
1bf8198e 145 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff
MW
146}
147
da3033ea 148static void uas_mark_cmd_dead(struct uas_dev_info *devinfo,
673331c8
HG
149 struct uas_cmd_info *cmdinfo,
150 int result, const char *caller)
da3033ea
HG
151{
152 struct scsi_pointer *scp = (void *)cmdinfo;
153 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
154
155 uas_log_cmd_state(cmnd, caller);
156 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
157 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
158 cmdinfo->state |= COMMAND_ABORTED;
159 cmdinfo->state &= ~IS_IN_WORK_LIST;
673331c8 160 cmnd->result = result << 16;
040d1a8f 161 list_move_tail(&cmdinfo->list, &devinfo->dead_list);
da3033ea
HG
162}
163
673331c8
HG
164static void uas_abort_inflight(struct uas_dev_info *devinfo, int result,
165 const char *caller)
4c456971
GH
166{
167 struct uas_cmd_info *cmdinfo;
168 struct uas_cmd_info *temp;
4c456971
GH
169 unsigned long flags;
170
4c456971 171 spin_lock_irqsave(&devinfo->lock, flags);
040d1a8f 172 list_for_each_entry_safe(cmdinfo, temp, &devinfo->inflight_list, list)
673331c8 173 uas_mark_cmd_dead(devinfo, cmdinfo, result, caller);
4c456971
GH
174 spin_unlock_irqrestore(&devinfo->lock, flags);
175}
176
1bf8198e
GH
177static void uas_add_work(struct uas_cmd_info *cmdinfo)
178{
179 struct scsi_pointer *scp = (void *)cmdinfo;
180 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
181 struct uas_dev_info *devinfo = cmnd->device->hostdata;
182
f491ecbb 183 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
1bf8198e
GH
184 cmdinfo->state |= IS_IN_WORK_LIST;
185 schedule_work(&devinfo->work);
186}
187
326349f8
GH
188static void uas_zap_dead(struct uas_dev_info *devinfo)
189{
190 struct uas_cmd_info *cmdinfo;
191 struct uas_cmd_info *temp;
192 unsigned long flags;
193
194 spin_lock_irqsave(&devinfo->lock, flags);
040d1a8f 195 list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, list) {
326349f8
GH
196 struct scsi_pointer *scp = (void *)cmdinfo;
197 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
198 SCp);
199 uas_log_cmd_state(cmnd, __func__);
f491ecbb 200 WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED));
326349f8
GH
201 /* all urbs are killed, clear inflight bits */
202 cmdinfo->state &= ~(COMMAND_INFLIGHT |
203 DATA_IN_URB_INFLIGHT |
204 DATA_OUT_URB_INFLIGHT);
205 uas_try_complete(cmnd, __func__);
206 }
b83b86a3 207 devinfo->running_task = 0;
326349f8
GH
208 spin_unlock_irqrestore(&devinfo->lock, flags);
209}
210
115bb1ff
MW
211static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
212{
213 struct sense_iu *sense_iu = urb->transfer_buffer;
214 struct scsi_device *sdev = cmnd->device;
215
216 if (urb->actual_length > 16) {
217 unsigned len = be16_to_cpup(&sense_iu->len);
218 if (len + 16 != urb->actual_length) {
219 int newlen = min(len + 16, urb->actual_length) - 16;
220 if (newlen < 0)
221 newlen = 0;
222 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
223 "disagrees with IU sense data length %d, "
224 "using %d bytes of sense data\n", __func__,
225 urb->actual_length, len, newlen);
226 len = newlen;
227 }
228 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
229 }
230
231 cmnd->result = sense_iu->status;
115bb1ff
MW
232}
233
234static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
235{
236 struct sense_iu_old *sense_iu = urb->transfer_buffer;
237 struct scsi_device *sdev = cmnd->device;
238
239 if (urb->actual_length > 8) {
240 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
241 if (len + 8 != urb->actual_length) {
242 int newlen = min(len + 8, urb->actual_length) - 8;
243 if (newlen < 0)
244 newlen = 0;
245 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
246 "disagrees with IU sense data length %d, "
247 "using %d bytes of sense data\n", __func__,
248 urb->actual_length, len, newlen);
249 len = newlen;
250 }
251 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
252 }
253
254 cmnd->result = sense_iu->status;
b1d67693
GH
255}
256
257static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
258{
259 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
260
261 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
efefecf3 262 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
b1d67693
GH
263 caller, cmnd, cmnd->request->tag,
264 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
265 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
266 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
267 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
268 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
269 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
270 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
271 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
272 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
273 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
ef018cc9 274 (ci->state & COMMAND_COMPLETED) ? " done" : "",
b06e48af 275 (ci->state & COMMAND_ABORTED) ? " abort" : "",
efefecf3
GH
276 (ci->state & UNLINK_DATA_URBS) ? " unlink": "",
277 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
b1d67693
GH
278}
279
280static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
281{
282 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
e0648520 283 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
b1d67693 284
f491ecbb 285 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
b1d67693
GH
286 if (cmdinfo->state & (COMMAND_INFLIGHT |
287 DATA_IN_URB_INFLIGHT |
b06e48af
GH
288 DATA_OUT_URB_INFLIGHT |
289 UNLINK_DATA_URBS))
b1d67693 290 return -EBUSY;
f491ecbb 291 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
b1d67693
GH
292 cmdinfo->state |= COMMAND_COMPLETED;
293 usb_free_urb(cmdinfo->data_in_urb);
294 usb_free_urb(cmdinfo->data_out_urb);
673331c8 295 if (cmdinfo->state & COMMAND_ABORTED)
0871d7d8 296 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
040d1a8f 297 list_del(&cmdinfo->list);
c621a81e 298 cmnd->scsi_done(cmnd);
b1d67693 299 return 0;
115bb1ff
MW
300}
301
302static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
b1d67693 303 unsigned direction)
115bb1ff
MW
304{
305 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
306 int err;
307
b1d67693 308 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
115bb1ff
MW
309 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
310 if (err) {
1bf8198e 311 uas_add_work(cmdinfo);
115bb1ff
MW
312 }
313}
314
315static void uas_stat_cmplt(struct urb *urb)
316{
317 struct iu *iu = urb->transfer_buffer;
22188f4a 318 struct Scsi_Host *shost = urb->context;
21fc05b6 319 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
115bb1ff 320 struct scsi_cmnd *cmnd;
b1d67693 321 struct uas_cmd_info *cmdinfo;
e0648520 322 unsigned long flags;
115bb1ff
MW
323 u16 tag;
324
325 if (urb->status) {
326349f8
GH
326 if (urb->status == -ENOENT) {
327 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
328 urb->stream_id);
329 } else {
330 dev_err(&urb->dev->dev, "stat urb: status %d\n",
331 urb->status);
332 }
db32de11 333 usb_free_urb(urb);
115bb1ff
MW
334 return;
335 }
336
023b515e
GH
337 if (devinfo->resetting) {
338 usb_free_urb(urb);
339 return;
340 }
341
e0648520 342 spin_lock_irqsave(&devinfo->lock, flags);
115bb1ff 343 tag = be16_to_cpup(&iu->tag) - 1;
22188f4a
SAS
344 if (tag == 0)
345 cmnd = devinfo->cmnd;
115bb1ff 346 else
22188f4a 347 cmnd = scsi_host_find_tag(shost, tag - 1);
e0423dee 348
96c1eb98 349 if (!cmnd) {
e0423dee 350 if (iu->iu_id == IU_ID_RESPONSE) {
b83b86a3
HG
351 if (!devinfo->running_task)
352 dev_warn(&urb->dev->dev,
353 "stat urb: recv unexpected response iu\n");
e0423dee
GH
354 /* store results for uas_eh_task_mgmt() */
355 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
023b515e 356 }
e0423dee
GH
357 usb_free_urb(urb);
358 spin_unlock_irqrestore(&devinfo->lock, flags);
359 return;
96c1eb98 360 }
115bb1ff 361
e0423dee 362 cmdinfo = (void *)&cmnd->SCp;
115bb1ff
MW
363 switch (iu->iu_id) {
364 case IU_ID_STATUS:
22188f4a
SAS
365 if (devinfo->cmnd == cmnd)
366 devinfo->cmnd = NULL;
367
115bb1ff
MW
368 if (urb->actual_length < 16)
369 devinfo->uas_sense_old = 1;
370 if (devinfo->uas_sense_old)
371 uas_sense_old(urb, cmnd);
372 else
373 uas_sense(urb, cmnd);
8aac863e
GH
374 if (cmnd->result != 0) {
375 /* cancel data transfers on error */
7e50e0be 376 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
8aac863e 377 }
b1d67693
GH
378 cmdinfo->state &= ~COMMAND_INFLIGHT;
379 uas_try_complete(cmnd, __func__);
115bb1ff
MW
380 break;
381 case IU_ID_READ_READY:
382 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
383 break;
384 case IU_ID_WRITE_READY:
385 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
386 break;
387 default:
388 scmd_printk(KERN_ERR, cmnd,
389 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
390 }
e9bd7e1a 391 usb_free_urb(urb);
e0648520 392 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff
MW
393}
394
c621a81e 395static void uas_data_cmplt(struct urb *urb)
115bb1ff 396{
b1d67693
GH
397 struct scsi_cmnd *cmnd = urb->context;
398 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
e0648520 399 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
b1d67693 400 struct scsi_data_buffer *sdb = NULL;
e0648520 401 unsigned long flags;
b1d67693 402
e0648520 403 spin_lock_irqsave(&devinfo->lock, flags);
b1d67693
GH
404 if (cmdinfo->data_in_urb == urb) {
405 sdb = scsi_in(cmnd);
406 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
407 } else if (cmdinfo->data_out_urb == urb) {
408 sdb = scsi_out(cmnd);
409 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
410 }
f491ecbb
GH
411 if (sdb == NULL) {
412 WARN_ON_ONCE(1);
413 } else if (urb->status) {
876285cc
HG
414 if (urb->status != -ECONNRESET) {
415 uas_log_cmd_state(cmnd, __func__);
416 scmd_printk(KERN_ERR, cmnd,
417 "data cmplt err %d stream %d\n",
418 urb->status, urb->stream_id);
419 }
8aac863e
GH
420 /* error: no data transfered */
421 sdb->resid = sdb->length;
422 } else {
423 sdb->resid = sdb->length - urb->actual_length;
424 }
b1d67693 425 uas_try_complete(cmnd, __func__);
e0648520 426 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff
MW
427}
428
876285cc
HG
429static void uas_cmd_cmplt(struct urb *urb)
430{
431 struct scsi_cmnd *cmnd = urb->context;
432
433 if (urb->status) {
434 uas_log_cmd_state(cmnd, __func__);
435 scmd_printk(KERN_ERR, cmnd, "cmd cmplt err %d\n", urb->status);
436 }
437 usb_free_urb(urb);
438}
439
115bb1ff 440static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
b1d67693
GH
441 unsigned int pipe, u16 stream_id,
442 struct scsi_cmnd *cmnd,
443 enum dma_data_direction dir)
115bb1ff
MW
444{
445 struct usb_device *udev = devinfo->udev;
446 struct urb *urb = usb_alloc_urb(0, gfp);
b1d67693
GH
447 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
448 ? scsi_in(cmnd) : scsi_out(cmnd);
115bb1ff
MW
449
450 if (!urb)
451 goto out;
b1d67693
GH
452 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
453 uas_data_cmplt, cmnd);
c6d4579d 454 urb->stream_id = stream_id;
115bb1ff
MW
455 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
456 urb->sg = sdb->table.sgl;
457 out:
458 return urb;
459}
460
461static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
e9bd7e1a 462 struct Scsi_Host *shost, u16 stream_id)
115bb1ff
MW
463{
464 struct usb_device *udev = devinfo->udev;
465 struct urb *urb = usb_alloc_urb(0, gfp);
466 struct sense_iu *iu;
467
468 if (!urb)
469 goto out;
470
ac563cfd 471 iu = kzalloc(sizeof(*iu), gfp);
115bb1ff
MW
472 if (!iu)
473 goto free;
474
475 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
e9bd7e1a 476 uas_stat_cmplt, shost);
115bb1ff
MW
477 urb->stream_id = stream_id;
478 urb->transfer_flags |= URB_FREE_BUFFER;
479 out:
480 return urb;
481 free:
482 usb_free_urb(urb);
483 return NULL;
484}
485
486static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
a887cd36 487 struct scsi_cmnd *cmnd)
115bb1ff
MW
488{
489 struct usb_device *udev = devinfo->udev;
490 struct scsi_device *sdev = cmnd->device;
491 struct urb *urb = usb_alloc_urb(0, gfp);
492 struct command_iu *iu;
493 int len;
494
495 if (!urb)
496 goto out;
497
498 len = cmnd->cmd_len - 16;
499 if (len < 0)
500 len = 0;
501 len = ALIGN(len, 4);
ac563cfd 502 iu = kzalloc(sizeof(*iu) + len, gfp);
115bb1ff
MW
503 if (!iu)
504 goto free;
505
506 iu->iu_id = IU_ID_COMMAND;
9eb44541 507 if (blk_rq_tagged(cmnd->request))
22188f4a 508 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
9eb44541
SS
509 else
510 iu->tag = cpu_to_be16(1);
02e031cb 511 iu->prio_attr = UAS_SIMPLE_TAG;
115bb1ff
MW
512 iu->len = len;
513 int_to_scsilun(sdev->lun, &iu->lun);
514 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
515
516 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
876285cc 517 uas_cmd_cmplt, cmnd);
115bb1ff
MW
518 urb->transfer_flags |= URB_FREE_BUFFER;
519 out:
520 return urb;
521 free:
522 usb_free_urb(urb);
523 return NULL;
524}
525
023b515e
GH
526static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
527 u8 function, u16 stream_id)
528{
529 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
530 struct usb_device *udev = devinfo->udev;
531 struct urb *urb = usb_alloc_urb(0, gfp);
532 struct task_mgmt_iu *iu;
533 int err = -ENOMEM;
534
535 if (!urb)
536 goto err;
537
538 iu = kzalloc(sizeof(*iu), gfp);
539 if (!iu)
540 goto err;
541
542 iu->iu_id = IU_ID_TASK_MGMT;
543 iu->tag = cpu_to_be16(stream_id);
544 int_to_scsilun(cmnd->device->lun, &iu->lun);
545
546 iu->function = function;
547 switch (function) {
548 case TMF_ABORT_TASK:
549 if (blk_rq_tagged(cmnd->request))
550 iu->task_tag = cpu_to_be16(cmnd->request->tag + 2);
551 else
552 iu->task_tag = cpu_to_be16(1);
553 break;
554 }
555
556 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
876285cc 557 uas_cmd_cmplt, cmnd);
023b515e
GH
558 urb->transfer_flags |= URB_FREE_BUFFER;
559
d5f808d3 560 usb_anchor_urb(urb, &devinfo->cmd_urbs);
023b515e 561 err = usb_submit_urb(urb, gfp);
d5f808d3
HG
562 if (err) {
563 usb_unanchor_urb(urb);
876285cc
HG
564 uas_log_cmd_state(cmnd, __func__);
565 scmd_printk(KERN_ERR, cmnd, "task submission err %d\n", err);
023b515e 566 goto err;
d5f808d3 567 }
023b515e
GH
568
569 return 0;
570
571err:
572 usb_free_urb(urb);
573 return err;
574}
575
115bb1ff
MW
576/*
577 * Why should I request the Status IU before sending the Command IU? Spec
578 * says to, but also says the device may receive them in any order. Seems
579 * daft to me.
580 */
581
876285cc 582static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd,
70cf0fba 583 gfp_t gfp, unsigned int stream)
115bb1ff 584{
876285cc 585 struct Scsi_Host *shost = cmnd->device->host;
21fc05b6 586 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
e9bd7e1a 587 struct urb *urb;
876285cc 588 int err;
115bb1ff 589
e9bd7e1a
GH
590 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
591 if (!urb)
70cf0fba 592 return NULL;
d5f808d3 593 usb_anchor_urb(urb, &devinfo->sense_urbs);
876285cc
HG
594 err = usb_submit_urb(urb, gfp);
595 if (err) {
d5f808d3 596 usb_unanchor_urb(urb);
876285cc 597 uas_log_cmd_state(cmnd, __func__);
e9bd7e1a 598 shost_printk(KERN_INFO, shost,
876285cc
HG
599 "sense urb submission error %d stream %d\n",
600 err, stream);
e9bd7e1a 601 usb_free_urb(urb);
70cf0fba 602 return NULL;
115bb1ff 603 }
70cf0fba 604 return urb;
e9bd7e1a
GH
605}
606
607static int uas_submit_urbs(struct scsi_cmnd *cmnd,
608 struct uas_dev_info *devinfo, gfp_t gfp)
609{
610 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
70cf0fba 611 struct urb *urb;
876285cc 612 int err;
115bb1ff 613
f491ecbb 614 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
92a3f767 615 if (cmdinfo->state & SUBMIT_STATUS_URB) {
876285cc 616 urb = uas_submit_sense_urb(cmnd, gfp, cmdinfo->stream);
70cf0fba
HG
617 if (!urb)
618 return SCSI_MLQUEUE_DEVICE_BUSY;
92a3f767 619 cmdinfo->state &= ~SUBMIT_STATUS_URB;
115bb1ff
MW
620 }
621
622 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
623 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
c621a81e 624 devinfo->data_in_pipe, cmdinfo->stream,
b1d67693 625 cmnd, DMA_FROM_DEVICE);
115bb1ff
MW
626 if (!cmdinfo->data_in_urb)
627 return SCSI_MLQUEUE_DEVICE_BUSY;
628 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
629 }
630
631 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
d5f808d3 632 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
876285cc
HG
633 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
634 if (err) {
d5f808d3 635 usb_unanchor_urb(cmdinfo->data_in_urb);
876285cc 636 uas_log_cmd_state(cmnd, __func__);
115bb1ff 637 scmd_printk(KERN_INFO, cmnd,
876285cc
HG
638 "data in urb submission error %d stream %d\n",
639 err, cmdinfo->data_in_urb->stream_id);
115bb1ff
MW
640 return SCSI_MLQUEUE_DEVICE_BUSY;
641 }
642 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
b1d67693 643 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
115bb1ff
MW
644 }
645
646 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
647 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
c621a81e 648 devinfo->data_out_pipe, cmdinfo->stream,
b1d67693 649 cmnd, DMA_TO_DEVICE);
115bb1ff
MW
650 if (!cmdinfo->data_out_urb)
651 return SCSI_MLQUEUE_DEVICE_BUSY;
652 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
653 }
654
655 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
d5f808d3 656 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
876285cc
HG
657 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
658 if (err) {
d5f808d3 659 usb_unanchor_urb(cmdinfo->data_out_urb);
876285cc 660 uas_log_cmd_state(cmnd, __func__);
115bb1ff 661 scmd_printk(KERN_INFO, cmnd,
876285cc
HG
662 "data out urb submission error %d stream %d\n",
663 err, cmdinfo->data_out_urb->stream_id);
115bb1ff
MW
664 return SCSI_MLQUEUE_DEVICE_BUSY;
665 }
666 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
b1d67693 667 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
115bb1ff
MW
668 }
669
670 if (cmdinfo->state & ALLOC_CMD_URB) {
a887cd36 671 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
115bb1ff
MW
672 if (!cmdinfo->cmd_urb)
673 return SCSI_MLQUEUE_DEVICE_BUSY;
674 cmdinfo->state &= ~ALLOC_CMD_URB;
675 }
676
677 if (cmdinfo->state & SUBMIT_CMD_URB) {
d5f808d3 678 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
876285cc
HG
679 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
680 if (err) {
d5f808d3 681 usb_unanchor_urb(cmdinfo->cmd_urb);
876285cc 682 uas_log_cmd_state(cmnd, __func__);
115bb1ff 683 scmd_printk(KERN_INFO, cmnd,
876285cc 684 "cmd urb submission error %d\n", err);
115bb1ff
MW
685 return SCSI_MLQUEUE_DEVICE_BUSY;
686 }
a0e39e34 687 cmdinfo->cmd_urb = NULL;
115bb1ff 688 cmdinfo->state &= ~SUBMIT_CMD_URB;
b1d67693 689 cmdinfo->state |= COMMAND_INFLIGHT;
115bb1ff
MW
690 }
691
692 return 0;
693}
694
f281233d 695static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
115bb1ff
MW
696 void (*done)(struct scsi_cmnd *))
697{
698 struct scsi_device *sdev = cmnd->device;
699 struct uas_dev_info *devinfo = sdev->hostdata;
700 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
e0648520 701 unsigned long flags;
115bb1ff
MW
702 int err;
703
704 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
705
c6f63207
HG
706 spin_lock_irqsave(&devinfo->lock, flags);
707
f8be6bfc
GH
708 if (devinfo->resetting) {
709 cmnd->result = DID_ERROR << 16;
710 cmnd->scsi_done(cmnd);
c6f63207 711 spin_unlock_irqrestore(&devinfo->lock, flags);
f8be6bfc
GH
712 return 0;
713 }
714
e0648520
GH
715 if (devinfo->cmnd) {
716 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff 717 return SCSI_MLQUEUE_DEVICE_BUSY;
e0648520 718 }
115bb1ff 719
3a4462e0
HG
720 memset(cmdinfo, 0, sizeof(*cmdinfo));
721
115bb1ff 722 if (blk_rq_tagged(cmnd->request)) {
22188f4a 723 cmdinfo->stream = cmnd->request->tag + 2;
115bb1ff 724 } else {
22188f4a 725 devinfo->cmnd = cmnd;
115bb1ff
MW
726 cmdinfo->stream = 1;
727 }
728
729 cmnd->scsi_done = done;
730
e9bd7e1a 731 cmdinfo->state = SUBMIT_STATUS_URB |
115bb1ff
MW
732 ALLOC_CMD_URB | SUBMIT_CMD_URB;
733
734 switch (cmnd->sc_data_direction) {
735 case DMA_FROM_DEVICE:
736 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
737 break;
738 case DMA_BIDIRECTIONAL:
739 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
740 case DMA_TO_DEVICE:
741 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
742 case DMA_NONE:
743 break;
744 }
745
746 if (!devinfo->use_streams) {
db32de11 747 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
115bb1ff
MW
748 cmdinfo->stream = 0;
749 }
750
751 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
752 if (err) {
753 /* If we did nothing, give up now */
92a3f767 754 if (cmdinfo->state & SUBMIT_STATUS_URB) {
e0648520 755 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff
MW
756 return SCSI_MLQUEUE_DEVICE_BUSY;
757 }
1bf8198e 758 uas_add_work(cmdinfo);
115bb1ff
MW
759 }
760
040d1a8f 761 list_add_tail(&cmdinfo->list, &devinfo->inflight_list);
e0648520 762 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff
MW
763 return 0;
764}
765
f281233d
JG
766static DEF_SCSI_QCMD(uas_queuecommand)
767
023b515e
GH
768static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
769 const char *fname, u8 function)
115bb1ff 770{
023b515e 771 struct Scsi_Host *shost = cmnd->device->host;
21fc05b6 772 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
d3f7c156 773 u16 tag = devinfo->qdepth;
e0648520 774 unsigned long flags;
70cf0fba 775 struct urb *sense_urb;
b83b86a3 776 int result = SUCCESS;
115bb1ff 777
e0648520 778 spin_lock_irqsave(&devinfo->lock, flags);
b83b86a3 779
c6f63207
HG
780 if (devinfo->resetting) {
781 spin_unlock_irqrestore(&devinfo->lock, flags);
782 return FAILED;
783 }
784
b83b86a3
HG
785 if (devinfo->running_task) {
786 shost_printk(KERN_INFO, shost,
787 "%s: %s: error already running a task\n",
788 __func__, fname);
789 spin_unlock_irqrestore(&devinfo->lock, flags);
790 return FAILED;
791 }
792
793 devinfo->running_task = 1;
023b515e 794 memset(&devinfo->response, 0, sizeof(devinfo->response));
876285cc 795 sense_urb = uas_submit_sense_urb(cmnd, GFP_NOIO,
f323abcd 796 devinfo->use_streams ? tag : 0);
70cf0fba 797 if (!sense_urb) {
023b515e
GH
798 shost_printk(KERN_INFO, shost,
799 "%s: %s: submit sense urb failed\n",
800 __func__, fname);
b83b86a3 801 devinfo->running_task = 0;
1994ff40 802 spin_unlock_irqrestore(&devinfo->lock, flags);
023b515e
GH
803 return FAILED;
804 }
e36e6493 805 if (uas_submit_task_urb(cmnd, GFP_NOIO, function, tag)) {
023b515e
GH
806 shost_printk(KERN_INFO, shost,
807 "%s: %s: submit task mgmt urb failed\n",
808 __func__, fname);
b83b86a3 809 devinfo->running_task = 0;
1994ff40 810 spin_unlock_irqrestore(&devinfo->lock, flags);
70cf0fba 811 usb_kill_urb(sense_urb);
023b515e
GH
812 return FAILED;
813 }
e0648520
GH
814 spin_unlock_irqrestore(&devinfo->lock, flags);
815
816 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) {
b83b86a3
HG
817 /*
818 * Note we deliberately do not clear running_task here. If we
819 * allow new tasks to be submitted, there is no way to figure
820 * out if a received response_iu is for the failed task or for
821 * the new one. A bus-reset will eventually clear running_task.
822 */
023b515e
GH
823 shost_printk(KERN_INFO, shost,
824 "%s: %s timed out\n", __func__, fname);
825 return FAILED;
826 }
b83b86a3
HG
827
828 spin_lock_irqsave(&devinfo->lock, flags);
829 devinfo->running_task = 0;
023b515e
GH
830 if (be16_to_cpu(devinfo->response.tag) != tag) {
831 shost_printk(KERN_INFO, shost,
832 "%s: %s failed (wrong tag %d/%d)\n", __func__,
833 fname, be16_to_cpu(devinfo->response.tag), tag);
b83b86a3
HG
834 result = FAILED;
835 } else if (devinfo->response.response_code != RC_TMF_COMPLETE) {
023b515e
GH
836 shost_printk(KERN_INFO, shost,
837 "%s: %s failed (rc 0x%x)\n", __func__,
838 fname, devinfo->response.response_code);
b83b86a3 839 result = FAILED;
023b515e 840 }
b83b86a3
HG
841 spin_unlock_irqrestore(&devinfo->lock, flags);
842
843 return result;
115bb1ff
MW
844}
845
023b515e 846static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
115bb1ff 847{
023b515e 848 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
e0648520
GH
849 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
850 unsigned long flags;
023b515e 851 int ret;
115bb1ff 852
e0648520 853 spin_lock_irqsave(&devinfo->lock, flags);
c6f63207
HG
854
855 if (devinfo->resetting) {
856 spin_unlock_irqrestore(&devinfo->lock, flags);
857 return FAILED;
858 }
859
673331c8 860 uas_mark_cmd_dead(devinfo, cmdinfo, DID_ABORT, __func__);
5d390403
GH
861 if (cmdinfo->state & COMMAND_INFLIGHT) {
862 spin_unlock_irqrestore(&devinfo->lock, flags);
863 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
864 } else {
7e50e0be 865 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
5d390403
GH
866 uas_try_complete(cmnd, __func__);
867 spin_unlock_irqrestore(&devinfo->lock, flags);
868 ret = SUCCESS;
869 }
023b515e 870 return ret;
115bb1ff
MW
871}
872
023b515e 873static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
115bb1ff 874{
023b515e
GH
875 sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__);
876 return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET",
877 TMF_LOGICAL_UNIT_RESET);
115bb1ff
MW
878}
879
880static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
881{
882 struct scsi_device *sdev = cmnd->device;
883 struct uas_dev_info *devinfo = sdev->hostdata;
884 struct usb_device *udev = devinfo->udev;
023b515e 885 int err;
115bb1ff 886
be326f4c
HG
887 err = usb_lock_device_for_reset(udev, devinfo->intf);
888 if (err) {
889 shost_printk(KERN_ERR, sdev->host,
890 "%s FAILED to get lock err %d\n", __func__, err);
891 return FAILED;
892 }
893
326349f8 894 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
023b515e 895 devinfo->resetting = 1;
673331c8 896 uas_abort_inflight(devinfo, DID_RESET, __func__);
a0e39e34 897 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
023b515e
GH
898 usb_kill_anchored_urbs(&devinfo->sense_urbs);
899 usb_kill_anchored_urbs(&devinfo->data_urbs);
326349f8 900 uas_zap_dead(devinfo);
023b515e
GH
901 err = usb_reset_device(udev);
902 devinfo->resetting = 0;
115bb1ff 903
be326f4c
HG
904 usb_unlock_device(udev);
905
023b515e
GH
906 if (err) {
907 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
908 return FAILED;
909 }
115bb1ff 910
023b515e
GH
911 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
912 return SUCCESS;
115bb1ff
MW
913}
914
915static int uas_slave_alloc(struct scsi_device *sdev)
916{
21fc05b6 917 sdev->hostdata = (void *)sdev->host->hostdata;
115bb1ff
MW
918 return 0;
919}
920
921static int uas_slave_configure(struct scsi_device *sdev)
922{
923 struct uas_dev_info *devinfo = sdev->hostdata;
924 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
d3f7c156 925 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
115bb1ff
MW
926 return 0;
927}
928
929static struct scsi_host_template uas_host_template = {
930 .module = THIS_MODULE,
931 .name = "uas",
932 .queuecommand = uas_queuecommand,
933 .slave_alloc = uas_slave_alloc,
934 .slave_configure = uas_slave_configure,
935 .eh_abort_handler = uas_eh_abort_handler,
936 .eh_device_reset_handler = uas_eh_device_reset_handler,
115bb1ff
MW
937 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
938 .can_queue = 65536, /* Is there a limit on the _host_ ? */
939 .this_id = -1,
940 .sg_tablesize = SG_NONE,
941 .cmd_per_lun = 1, /* until we override it */
942 .skip_settle_delay = 1,
943 .ordered_tag = 1,
944};
945
79b4c061
HG
946#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
947 vendorName, productName, useProtocol, useTransport, \
948 initFunction, flags) \
949{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
950 .driver_info = (flags) }
951
115bb1ff 952static struct usb_device_id uas_usb_ids[] = {
79b4c061 953# include "unusual_uas.h"
115bb1ff
MW
954 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
955 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
956 /* 0xaa is a prototype device I happen to have access to */
957 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
958 { }
959};
960MODULE_DEVICE_TABLE(usb, uas_usb_ids);
961
79b4c061
HG
962#undef UNUSUAL_DEV
963
e1be067b
HG
964static int uas_switch_interface(struct usb_device *udev,
965 struct usb_interface *intf)
966{
967 int alt;
968
969 alt = uas_find_uas_alt_setting(intf);
970 if (alt < 0)
971 return alt;
972
973 return usb_set_interface(udev,
974 intf->altsetting[0].desc.bInterfaceNumber, alt);
975}
976
58d51444 977static int uas_configure_endpoints(struct uas_dev_info *devinfo)
34f11e59
HG
978{
979 struct usb_host_endpoint *eps[4] = { };
980 struct usb_device *udev = devinfo->udev;
981 int r;
982
983 devinfo->uas_sense_old = 0;
984 devinfo->cmnd = NULL;
985
986 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
74d71aec
HG
987 if (r)
988 return r;
989
990 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
991 usb_endpoint_num(&eps[0]->desc));
992 devinfo->status_pipe = usb_rcvbulkpipe(udev,
993 usb_endpoint_num(&eps[1]->desc));
994 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
995 usb_endpoint_num(&eps[2]->desc));
996 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
997 usb_endpoint_num(&eps[3]->desc));
115bb1ff 998
58d51444 999 if (udev->speed != USB_SPEED_SUPER) {
115bb1ff
MW
1000 devinfo->qdepth = 256;
1001 devinfo->use_streams = 0;
1002 } else {
58d51444
HG
1003 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
1004 3, 256, GFP_KERNEL);
1005 if (devinfo->qdepth < 0)
1006 return devinfo->qdepth;
115bb1ff
MW
1007 devinfo->use_streams = 1;
1008 }
58d51444
HG
1009
1010 return 0;
115bb1ff
MW
1011}
1012
dae51546
SAS
1013static void uas_free_streams(struct uas_dev_info *devinfo)
1014{
1015 struct usb_device *udev = devinfo->udev;
1016 struct usb_host_endpoint *eps[3];
1017
1018 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
1019 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
1020 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
1021 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
1022}
1023
115bb1ff
MW
1024/*
1025 * XXX: What I'd like to do here is register a SCSI host for each USB host in
1026 * the system. Follow usb-storage's design of registering a SCSI host for
1027 * each USB device for the moment. Can implement this by walking up the
1028 * USB hierarchy until we find a USB host.
1029 */
1030static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
1031{
6ce8213b
HG
1032 int result = -ENOMEM;
1033 struct Scsi_Host *shost = NULL;
115bb1ff
MW
1034 struct uas_dev_info *devinfo;
1035 struct usb_device *udev = interface_to_usbdev(intf);
1036
79b4c061
HG
1037 if (!uas_use_uas_driver(intf, id))
1038 return -ENODEV;
1039
89dc2905
MW
1040 if (uas_switch_interface(udev, intf))
1041 return -ENODEV;
115bb1ff 1042
21fc05b6
HG
1043 shost = scsi_host_alloc(&uas_host_template,
1044 sizeof(struct uas_dev_info));
115bb1ff 1045 if (!shost)
6ce8213b 1046 goto set_alt0;
115bb1ff
MW
1047
1048 shost->max_cmd_len = 16 + 252;
1049 shost->max_id = 1;
bde027b4
GH
1050 shost->max_lun = 256;
1051 shost->max_channel = 0;
115bb1ff
MW
1052 shost->sg_tablesize = udev->bus->sg_tablesize;
1053
21fc05b6 1054 devinfo = (struct uas_dev_info *)shost->hostdata;
115bb1ff
MW
1055 devinfo->intf = intf;
1056 devinfo->udev = udev;
023b515e 1057 devinfo->resetting = 0;
b83b86a3 1058 devinfo->running_task = 0;
da65c2bb 1059 devinfo->shutdown = 0;
a0e39e34 1060 init_usb_anchor(&devinfo->cmd_urbs);
bdd000fb
GH
1061 init_usb_anchor(&devinfo->sense_urbs);
1062 init_usb_anchor(&devinfo->data_urbs);
e0648520 1063 spin_lock_init(&devinfo->lock);
1bf8198e 1064 INIT_WORK(&devinfo->work, uas_do_work);
61c09ce5 1065 INIT_LIST_HEAD(&devinfo->inflight_list);
326349f8 1066 INIT_LIST_HEAD(&devinfo->dead_list);
58d51444
HG
1067
1068 result = uas_configure_endpoints(devinfo);
1069 if (result)
1070 goto set_alt0;
115bb1ff 1071
d3f7c156 1072 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
115bb1ff 1073 if (result)
6ce8213b 1074 goto free_streams;
dae51546
SAS
1075
1076 result = scsi_add_host(shost, &intf->dev);
1077 if (result)
6ce8213b 1078 goto free_streams;
dae51546 1079
115bb1ff
MW
1080 scsi_scan_host(shost);
1081 usb_set_intfdata(intf, shost);
1082 return result;
dae51546 1083
6ce8213b 1084free_streams:
dae51546 1085 uas_free_streams(devinfo);
6ce8213b
HG
1086set_alt0:
1087 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
115bb1ff
MW
1088 if (shost)
1089 scsi_host_put(shost);
1090 return result;
1091}
1092
1093static int uas_pre_reset(struct usb_interface *intf)
1094{
4de7a373 1095 struct Scsi_Host *shost = usb_get_intfdata(intf);
21fc05b6 1096 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
4de7a373
HG
1097 unsigned long flags;
1098
da65c2bb
HG
1099 if (devinfo->shutdown)
1100 return 0;
1101
4de7a373
HG
1102 /* Block new requests */
1103 spin_lock_irqsave(shost->host_lock, flags);
1104 scsi_block_requests(shost);
1105 spin_unlock_irqrestore(shost->host_lock, flags);
1106
1107 /* Wait for any pending requests to complete */
1108 flush_work(&devinfo->work);
1109 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1110 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1111 return 1;
1112 }
1113
1114 uas_free_streams(devinfo);
1115
115bb1ff
MW
1116 return 0;
1117}
1118
1119static int uas_post_reset(struct usb_interface *intf)
1120{
4de7a373 1121 struct Scsi_Host *shost = usb_get_intfdata(intf);
21fc05b6 1122 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
4de7a373
HG
1123 unsigned long flags;
1124
da65c2bb
HG
1125 if (devinfo->shutdown)
1126 return 0;
1127
58d51444
HG
1128 if (uas_configure_endpoints(devinfo) != 0) {
1129 shost_printk(KERN_ERR, shost,
1130 "%s: alloc streams error after reset", __func__);
1131 return 1;
1132 }
4de7a373
HG
1133
1134 spin_lock_irqsave(shost->host_lock, flags);
1135 scsi_report_bus_reset(shost, 0);
1136 spin_unlock_irqrestore(shost->host_lock, flags);
1137
1138 scsi_unblock_requests(shost);
1139
115bb1ff
MW
1140 return 0;
1141}
1142
0df1f663
HG
1143static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1144{
1145 struct Scsi_Host *shost = usb_get_intfdata(intf);
21fc05b6 1146 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
0df1f663
HG
1147
1148 /* Wait for any pending requests to complete */
1149 flush_work(&devinfo->work);
1150 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1151 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1152 return -ETIME;
1153 }
1154
1155 return 0;
1156}
1157
1158static int uas_resume(struct usb_interface *intf)
1159{
1160 return 0;
1161}
1162
1163static int uas_reset_resume(struct usb_interface *intf)
1164{
1165 struct Scsi_Host *shost = usb_get_intfdata(intf);
21fc05b6 1166 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
0df1f663
HG
1167 unsigned long flags;
1168
1169 if (uas_configure_endpoints(devinfo) != 0) {
1170 shost_printk(KERN_ERR, shost,
1171 "%s: alloc streams error after reset", __func__);
1172 return -EIO;
1173 }
1174
1175 spin_lock_irqsave(shost->host_lock, flags);
1176 scsi_report_bus_reset(shost, 0);
1177 spin_unlock_irqrestore(shost->host_lock, flags);
1178
1179 return 0;
1180}
1181
115bb1ff
MW
1182static void uas_disconnect(struct usb_interface *intf)
1183{
115bb1ff 1184 struct Scsi_Host *shost = usb_get_intfdata(intf);
21fc05b6 1185 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
115bb1ff 1186
4c456971 1187 devinfo->resetting = 1;
1bf8198e 1188 cancel_work_sync(&devinfo->work);
673331c8 1189 uas_abort_inflight(devinfo, DID_NO_CONNECT, __func__);
a0e39e34 1190 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
bdd000fb
GH
1191 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1192 usb_kill_anchored_urbs(&devinfo->data_urbs);
326349f8 1193 uas_zap_dead(devinfo);
4c456971 1194 scsi_remove_host(shost);
dae51546 1195 uas_free_streams(devinfo);
21fc05b6 1196 scsi_host_put(shost);
115bb1ff
MW
1197}
1198
da65c2bb
HG
1199/*
1200 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1201 * hang on reboot when the device is still in uas mode. Note the reset is
1202 * necessary as some devices won't revert to usb-storage mode without it.
1203 */
1204static void uas_shutdown(struct device *dev)
1205{
1206 struct usb_interface *intf = to_usb_interface(dev);
1207 struct usb_device *udev = interface_to_usbdev(intf);
1208 struct Scsi_Host *shost = usb_get_intfdata(intf);
21fc05b6 1209 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
da65c2bb
HG
1210
1211 if (system_state != SYSTEM_RESTART)
1212 return;
1213
1214 devinfo->shutdown = 1;
1215 uas_free_streams(devinfo);
1216 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1217 usb_reset_device(udev);
1218}
1219
115bb1ff
MW
1220static struct usb_driver uas_driver = {
1221 .name = "uas",
1222 .probe = uas_probe,
1223 .disconnect = uas_disconnect,
1224 .pre_reset = uas_pre_reset,
1225 .post_reset = uas_post_reset,
0df1f663
HG
1226 .suspend = uas_suspend,
1227 .resume = uas_resume,
1228 .reset_resume = uas_reset_resume,
da65c2bb 1229 .drvwrap.driver.shutdown = uas_shutdown,
115bb1ff
MW
1230 .id_table = uas_usb_ids,
1231};
1232
65db4305 1233module_usb_driver(uas_driver);
115bb1ff
MW
1234
1235MODULE_LICENSE("GPL");
1236MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");