]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/usb/storage/uas.c
fc08ee91943927e2294a886f30e59423d6a883bf
[mirror_ubuntu-artful-kernel.git] / drivers / usb / storage / uas.c
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>
14 #include <linux/module.h>
15 #include <linux/usb.h>
16 #include <linux/usb/hcd.h>
17 #include <linux/usb/storage.h>
18 #include <linux/usb/uas.h>
19
20 #include <scsi/scsi.h>
21 #include <scsi/scsi_dbg.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <scsi/scsi_device.h>
24 #include <scsi/scsi_host.h>
25 #include <scsi/scsi_tcq.h>
26
27 /*
28 * The r00-r01c specs define this version of the SENSE IU data structure.
29 * It's still in use by several different firmware releases.
30 */
31 struct sense_iu_old {
32 __u8 iu_id;
33 __u8 rsvd1;
34 __be16 tag;
35 __be16 len;
36 __u8 status;
37 __u8 service_response;
38 __u8 sense[SCSI_SENSE_BUFFERSIZE];
39 };
40
41 struct uas_dev_info {
42 struct usb_interface *intf;
43 struct usb_device *udev;
44 struct usb_anchor cmd_urbs;
45 struct usb_anchor sense_urbs;
46 struct usb_anchor data_urbs;
47 int qdepth, resetting;
48 struct response_ui response;
49 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
50 unsigned use_streams:1;
51 unsigned uas_sense_old:1;
52 struct scsi_cmnd *cmnd;
53 spinlock_t lock;
54 };
55
56 enum {
57 SUBMIT_STATUS_URB = (1 << 1),
58 ALLOC_DATA_IN_URB = (1 << 2),
59 SUBMIT_DATA_IN_URB = (1 << 3),
60 ALLOC_DATA_OUT_URB = (1 << 4),
61 SUBMIT_DATA_OUT_URB = (1 << 5),
62 ALLOC_CMD_URB = (1 << 6),
63 SUBMIT_CMD_URB = (1 << 7),
64 COMMAND_INFLIGHT = (1 << 8),
65 DATA_IN_URB_INFLIGHT = (1 << 9),
66 DATA_OUT_URB_INFLIGHT = (1 << 10),
67 COMMAND_COMPLETED = (1 << 11),
68 COMMAND_ABORTED = (1 << 12),
69 UNLINK_DATA_URBS = (1 << 13),
70 IS_IN_WORK_LIST = (1 << 14),
71 };
72
73 /* Overrides scsi_pointer */
74 struct uas_cmd_info {
75 unsigned int state;
76 unsigned int stream;
77 struct urb *cmd_urb;
78 struct urb *data_in_urb;
79 struct urb *data_out_urb;
80 struct list_head list;
81 };
82
83 /* I hate forward declarations, but I actually have a loop */
84 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
85 struct uas_dev_info *devinfo, gfp_t gfp);
86 static void uas_do_work(struct work_struct *work);
87 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
88 static void uas_configure_endpoints(struct uas_dev_info *devinfo);
89 static void uas_free_streams(struct uas_dev_info *devinfo);
90
91 static DECLARE_WORK(uas_work, uas_do_work);
92 static DEFINE_SPINLOCK(uas_work_lock);
93 static LIST_HEAD(uas_work_list);
94
95 static void uas_unlink_data_urbs(struct uas_dev_info *devinfo,
96 struct uas_cmd_info *cmdinfo)
97 {
98 unsigned long flags;
99
100 /*
101 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
102 * (called by urb completion) doesn't release cmdinfo
103 * underneath us.
104 */
105 spin_lock_irqsave(&devinfo->lock, flags);
106 cmdinfo->state |= UNLINK_DATA_URBS;
107 spin_unlock_irqrestore(&devinfo->lock, flags);
108
109 if (cmdinfo->data_in_urb)
110 usb_unlink_urb(cmdinfo->data_in_urb);
111 if (cmdinfo->data_out_urb)
112 usb_unlink_urb(cmdinfo->data_out_urb);
113
114 spin_lock_irqsave(&devinfo->lock, flags);
115 cmdinfo->state &= ~UNLINK_DATA_URBS;
116 spin_unlock_irqrestore(&devinfo->lock, flags);
117 }
118
119 static void uas_do_work(struct work_struct *work)
120 {
121 struct uas_cmd_info *cmdinfo;
122 struct uas_cmd_info *temp;
123 struct list_head list;
124 unsigned long flags;
125 int err;
126
127 spin_lock_irq(&uas_work_lock);
128 list_replace_init(&uas_work_list, &list);
129 spin_unlock_irq(&uas_work_lock);
130
131 list_for_each_entry_safe(cmdinfo, temp, &list, list) {
132 struct scsi_pointer *scp = (void *)cmdinfo;
133 struct scsi_cmnd *cmnd = container_of(scp,
134 struct scsi_cmnd, SCp);
135 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
136 spin_lock_irqsave(&devinfo->lock, flags);
137 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
138 if (!err)
139 cmdinfo->state &= ~IS_IN_WORK_LIST;
140 spin_unlock_irqrestore(&devinfo->lock, flags);
141 if (err) {
142 list_del(&cmdinfo->list);
143 spin_lock_irq(&uas_work_lock);
144 list_add_tail(&cmdinfo->list, &uas_work_list);
145 spin_unlock_irq(&uas_work_lock);
146 schedule_work(&uas_work);
147 }
148 }
149 }
150
151 static void uas_abort_work(struct uas_dev_info *devinfo)
152 {
153 struct uas_cmd_info *cmdinfo;
154 struct uas_cmd_info *temp;
155 struct list_head list;
156 unsigned long flags;
157
158 spin_lock_irq(&uas_work_lock);
159 list_replace_init(&uas_work_list, &list);
160 spin_unlock_irq(&uas_work_lock);
161
162 spin_lock_irqsave(&devinfo->lock, flags);
163 list_for_each_entry_safe(cmdinfo, temp, &list, list) {
164 struct scsi_pointer *scp = (void *)cmdinfo;
165 struct scsi_cmnd *cmnd = container_of(scp,
166 struct scsi_cmnd, SCp);
167 struct uas_dev_info *di = (void *)cmnd->device->hostdata;
168
169 if (di == devinfo) {
170 cmdinfo->state |= COMMAND_ABORTED;
171 cmdinfo->state &= ~IS_IN_WORK_LIST;
172 if (devinfo->resetting) {
173 /* uas_stat_cmplt() will not do that
174 * when a device reset is in
175 * progress */
176 cmdinfo->state &= ~COMMAND_INFLIGHT;
177 }
178 uas_try_complete(cmnd, __func__);
179 } else {
180 /* not our uas device, relink into list */
181 list_del(&cmdinfo->list);
182 spin_lock_irq(&uas_work_lock);
183 list_add_tail(&cmdinfo->list, &uas_work_list);
184 spin_unlock_irq(&uas_work_lock);
185 }
186 }
187 spin_unlock_irqrestore(&devinfo->lock, flags);
188 }
189
190 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
191 {
192 struct sense_iu *sense_iu = urb->transfer_buffer;
193 struct scsi_device *sdev = cmnd->device;
194
195 if (urb->actual_length > 16) {
196 unsigned len = be16_to_cpup(&sense_iu->len);
197 if (len + 16 != urb->actual_length) {
198 int newlen = min(len + 16, urb->actual_length) - 16;
199 if (newlen < 0)
200 newlen = 0;
201 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
202 "disagrees with IU sense data length %d, "
203 "using %d bytes of sense data\n", __func__,
204 urb->actual_length, len, newlen);
205 len = newlen;
206 }
207 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
208 }
209
210 cmnd->result = sense_iu->status;
211 }
212
213 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
214 {
215 struct sense_iu_old *sense_iu = urb->transfer_buffer;
216 struct scsi_device *sdev = cmnd->device;
217
218 if (urb->actual_length > 8) {
219 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
220 if (len + 8 != urb->actual_length) {
221 int newlen = min(len + 8, urb->actual_length) - 8;
222 if (newlen < 0)
223 newlen = 0;
224 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
225 "disagrees with IU sense data length %d, "
226 "using %d bytes of sense data\n", __func__,
227 urb->actual_length, len, newlen);
228 len = newlen;
229 }
230 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
231 }
232
233 cmnd->result = sense_iu->status;
234 }
235
236 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
237 {
238 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
239
240 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
241 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
242 caller, cmnd, cmnd->request->tag,
243 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
244 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
245 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
246 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
247 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
248 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
249 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
250 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
251 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
252 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
253 (ci->state & COMMAND_COMPLETED) ? " done" : "",
254 (ci->state & COMMAND_ABORTED) ? " abort" : "",
255 (ci->state & UNLINK_DATA_URBS) ? " unlink": "",
256 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
257 }
258
259 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
260 {
261 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
262 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
263
264 WARN_ON(!spin_is_locked(&devinfo->lock));
265 if (cmdinfo->state & (COMMAND_INFLIGHT |
266 DATA_IN_URB_INFLIGHT |
267 DATA_OUT_URB_INFLIGHT |
268 UNLINK_DATA_URBS))
269 return -EBUSY;
270 BUG_ON(cmdinfo->state & COMMAND_COMPLETED);
271 cmdinfo->state |= COMMAND_COMPLETED;
272 usb_free_urb(cmdinfo->data_in_urb);
273 usb_free_urb(cmdinfo->data_out_urb);
274 if (cmdinfo->state & COMMAND_ABORTED) {
275 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
276 cmnd->result = DID_ABORT << 16;
277 }
278 cmnd->scsi_done(cmnd);
279 return 0;
280 }
281
282 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
283 unsigned direction)
284 {
285 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
286 int err;
287
288 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
289 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
290 if (err) {
291 spin_lock(&uas_work_lock);
292 list_add_tail(&cmdinfo->list, &uas_work_list);
293 cmdinfo->state |= IS_IN_WORK_LIST;
294 spin_unlock(&uas_work_lock);
295 schedule_work(&uas_work);
296 }
297 }
298
299 static void uas_stat_cmplt(struct urb *urb)
300 {
301 struct iu *iu = urb->transfer_buffer;
302 struct Scsi_Host *shost = urb->context;
303 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
304 struct scsi_cmnd *cmnd;
305 struct uas_cmd_info *cmdinfo;
306 unsigned long flags;
307 u16 tag;
308
309 if (urb->status) {
310 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
311 usb_free_urb(urb);
312 return;
313 }
314
315 if (devinfo->resetting) {
316 usb_free_urb(urb);
317 return;
318 }
319
320 spin_lock_irqsave(&devinfo->lock, flags);
321 tag = be16_to_cpup(&iu->tag) - 1;
322 if (tag == 0)
323 cmnd = devinfo->cmnd;
324 else
325 cmnd = scsi_host_find_tag(shost, tag - 1);
326
327 if (!cmnd) {
328 if (iu->iu_id == IU_ID_RESPONSE) {
329 /* store results for uas_eh_task_mgmt() */
330 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
331 }
332 usb_free_urb(urb);
333 spin_unlock_irqrestore(&devinfo->lock, flags);
334 return;
335 }
336
337 cmdinfo = (void *)&cmnd->SCp;
338 switch (iu->iu_id) {
339 case IU_ID_STATUS:
340 if (devinfo->cmnd == cmnd)
341 devinfo->cmnd = NULL;
342
343 if (urb->actual_length < 16)
344 devinfo->uas_sense_old = 1;
345 if (devinfo->uas_sense_old)
346 uas_sense_old(urb, cmnd);
347 else
348 uas_sense(urb, cmnd);
349 if (cmnd->result != 0) {
350 /* cancel data transfers on error */
351 spin_unlock_irqrestore(&devinfo->lock, flags);
352 uas_unlink_data_urbs(devinfo, cmdinfo);
353 spin_lock_irqsave(&devinfo->lock, flags);
354 }
355 cmdinfo->state &= ~COMMAND_INFLIGHT;
356 uas_try_complete(cmnd, __func__);
357 break;
358 case IU_ID_READ_READY:
359 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
360 break;
361 case IU_ID_WRITE_READY:
362 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
363 break;
364 default:
365 scmd_printk(KERN_ERR, cmnd,
366 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
367 }
368 usb_free_urb(urb);
369 spin_unlock_irqrestore(&devinfo->lock, flags);
370 }
371
372 static void uas_data_cmplt(struct urb *urb)
373 {
374 struct scsi_cmnd *cmnd = urb->context;
375 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
376 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
377 struct scsi_data_buffer *sdb = NULL;
378 unsigned long flags;
379
380 spin_lock_irqsave(&devinfo->lock, flags);
381 if (cmdinfo->data_in_urb == urb) {
382 sdb = scsi_in(cmnd);
383 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
384 } else if (cmdinfo->data_out_urb == urb) {
385 sdb = scsi_out(cmnd);
386 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
387 }
388 BUG_ON(sdb == NULL);
389 if (urb->status) {
390 /* error: no data transfered */
391 sdb->resid = sdb->length;
392 } else {
393 sdb->resid = sdb->length - urb->actual_length;
394 }
395 uas_try_complete(cmnd, __func__);
396 spin_unlock_irqrestore(&devinfo->lock, flags);
397 }
398
399 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
400 unsigned int pipe, u16 stream_id,
401 struct scsi_cmnd *cmnd,
402 enum dma_data_direction dir)
403 {
404 struct usb_device *udev = devinfo->udev;
405 struct urb *urb = usb_alloc_urb(0, gfp);
406 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
407 ? scsi_in(cmnd) : scsi_out(cmnd);
408
409 if (!urb)
410 goto out;
411 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
412 uas_data_cmplt, cmnd);
413 if (devinfo->use_streams)
414 urb->stream_id = stream_id;
415 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
416 urb->sg = sdb->table.sgl;
417 out:
418 return urb;
419 }
420
421 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
422 struct Scsi_Host *shost, u16 stream_id)
423 {
424 struct usb_device *udev = devinfo->udev;
425 struct urb *urb = usb_alloc_urb(0, gfp);
426 struct sense_iu *iu;
427
428 if (!urb)
429 goto out;
430
431 iu = kzalloc(sizeof(*iu), gfp);
432 if (!iu)
433 goto free;
434
435 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
436 uas_stat_cmplt, shost);
437 urb->stream_id = stream_id;
438 urb->transfer_flags |= URB_FREE_BUFFER;
439 out:
440 return urb;
441 free:
442 usb_free_urb(urb);
443 return NULL;
444 }
445
446 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
447 struct scsi_cmnd *cmnd, u16 stream_id)
448 {
449 struct usb_device *udev = devinfo->udev;
450 struct scsi_device *sdev = cmnd->device;
451 struct urb *urb = usb_alloc_urb(0, gfp);
452 struct command_iu *iu;
453 int len;
454
455 if (!urb)
456 goto out;
457
458 len = cmnd->cmd_len - 16;
459 if (len < 0)
460 len = 0;
461 len = ALIGN(len, 4);
462 iu = kzalloc(sizeof(*iu) + len, gfp);
463 if (!iu)
464 goto free;
465
466 iu->iu_id = IU_ID_COMMAND;
467 if (blk_rq_tagged(cmnd->request))
468 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
469 else
470 iu->tag = cpu_to_be16(1);
471 iu->prio_attr = UAS_SIMPLE_TAG;
472 iu->len = len;
473 int_to_scsilun(sdev->lun, &iu->lun);
474 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
475
476 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
477 usb_free_urb, NULL);
478 urb->transfer_flags |= URB_FREE_BUFFER;
479 out:
480 return urb;
481 free:
482 usb_free_urb(urb);
483 return NULL;
484 }
485
486 static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
487 u8 function, u16 stream_id)
488 {
489 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
490 struct usb_device *udev = devinfo->udev;
491 struct urb *urb = usb_alloc_urb(0, gfp);
492 struct task_mgmt_iu *iu;
493 int err = -ENOMEM;
494
495 if (!urb)
496 goto err;
497
498 iu = kzalloc(sizeof(*iu), gfp);
499 if (!iu)
500 goto err;
501
502 iu->iu_id = IU_ID_TASK_MGMT;
503 iu->tag = cpu_to_be16(stream_id);
504 int_to_scsilun(cmnd->device->lun, &iu->lun);
505
506 iu->function = function;
507 switch (function) {
508 case TMF_ABORT_TASK:
509 if (blk_rq_tagged(cmnd->request))
510 iu->task_tag = cpu_to_be16(cmnd->request->tag + 2);
511 else
512 iu->task_tag = cpu_to_be16(1);
513 break;
514 }
515
516 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
517 usb_free_urb, NULL);
518 urb->transfer_flags |= URB_FREE_BUFFER;
519
520 err = usb_submit_urb(urb, gfp);
521 if (err)
522 goto err;
523 usb_anchor_urb(urb, &devinfo->cmd_urbs);
524
525 return 0;
526
527 err:
528 usb_free_urb(urb);
529 return err;
530 }
531
532 /*
533 * Why should I request the Status IU before sending the Command IU? Spec
534 * says to, but also says the device may receive them in any order. Seems
535 * daft to me.
536 */
537
538 static int uas_submit_sense_urb(struct Scsi_Host *shost,
539 gfp_t gfp, unsigned int stream)
540 {
541 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
542 struct urb *urb;
543
544 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
545 if (!urb)
546 return SCSI_MLQUEUE_DEVICE_BUSY;
547 if (usb_submit_urb(urb, gfp)) {
548 shost_printk(KERN_INFO, shost,
549 "sense urb submission failure\n");
550 usb_free_urb(urb);
551 return SCSI_MLQUEUE_DEVICE_BUSY;
552 }
553 usb_anchor_urb(urb, &devinfo->sense_urbs);
554 return 0;
555 }
556
557 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
558 struct uas_dev_info *devinfo, gfp_t gfp)
559 {
560 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
561 int err;
562
563 WARN_ON(!spin_is_locked(&devinfo->lock));
564 if (cmdinfo->state & SUBMIT_STATUS_URB) {
565 err = uas_submit_sense_urb(cmnd->device->host, gfp,
566 cmdinfo->stream);
567 if (err) {
568 return err;
569 }
570 cmdinfo->state &= ~SUBMIT_STATUS_URB;
571 }
572
573 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
574 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
575 devinfo->data_in_pipe, cmdinfo->stream,
576 cmnd, DMA_FROM_DEVICE);
577 if (!cmdinfo->data_in_urb)
578 return SCSI_MLQUEUE_DEVICE_BUSY;
579 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
580 }
581
582 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
583 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
584 scmd_printk(KERN_INFO, cmnd,
585 "data in urb submission failure\n");
586 return SCSI_MLQUEUE_DEVICE_BUSY;
587 }
588 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
589 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
590 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
591 }
592
593 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
594 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
595 devinfo->data_out_pipe, cmdinfo->stream,
596 cmnd, DMA_TO_DEVICE);
597 if (!cmdinfo->data_out_urb)
598 return SCSI_MLQUEUE_DEVICE_BUSY;
599 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
600 }
601
602 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
603 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
604 scmd_printk(KERN_INFO, cmnd,
605 "data out urb submission failure\n");
606 return SCSI_MLQUEUE_DEVICE_BUSY;
607 }
608 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
609 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
610 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
611 }
612
613 if (cmdinfo->state & ALLOC_CMD_URB) {
614 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
615 cmdinfo->stream);
616 if (!cmdinfo->cmd_urb)
617 return SCSI_MLQUEUE_DEVICE_BUSY;
618 cmdinfo->state &= ~ALLOC_CMD_URB;
619 }
620
621 if (cmdinfo->state & SUBMIT_CMD_URB) {
622 usb_get_urb(cmdinfo->cmd_urb);
623 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
624 scmd_printk(KERN_INFO, cmnd,
625 "cmd urb submission failure\n");
626 return SCSI_MLQUEUE_DEVICE_BUSY;
627 }
628 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
629 usb_put_urb(cmdinfo->cmd_urb);
630 cmdinfo->cmd_urb = NULL;
631 cmdinfo->state &= ~SUBMIT_CMD_URB;
632 cmdinfo->state |= COMMAND_INFLIGHT;
633 }
634
635 return 0;
636 }
637
638 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
639 void (*done)(struct scsi_cmnd *))
640 {
641 struct scsi_device *sdev = cmnd->device;
642 struct uas_dev_info *devinfo = sdev->hostdata;
643 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
644 unsigned long flags;
645 int err;
646
647 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
648
649 if (devinfo->resetting) {
650 cmnd->result = DID_ERROR << 16;
651 cmnd->scsi_done(cmnd);
652 return 0;
653 }
654
655 spin_lock_irqsave(&devinfo->lock, flags);
656 if (devinfo->cmnd) {
657 spin_unlock_irqrestore(&devinfo->lock, flags);
658 return SCSI_MLQUEUE_DEVICE_BUSY;
659 }
660
661 if (blk_rq_tagged(cmnd->request)) {
662 cmdinfo->stream = cmnd->request->tag + 2;
663 } else {
664 devinfo->cmnd = cmnd;
665 cmdinfo->stream = 1;
666 }
667
668 cmnd->scsi_done = done;
669
670 cmdinfo->state = SUBMIT_STATUS_URB |
671 ALLOC_CMD_URB | SUBMIT_CMD_URB;
672
673 switch (cmnd->sc_data_direction) {
674 case DMA_FROM_DEVICE:
675 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
676 break;
677 case DMA_BIDIRECTIONAL:
678 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
679 case DMA_TO_DEVICE:
680 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
681 case DMA_NONE:
682 break;
683 }
684
685 if (!devinfo->use_streams) {
686 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
687 cmdinfo->stream = 0;
688 }
689
690 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
691 if (err) {
692 /* If we did nothing, give up now */
693 if (cmdinfo->state & SUBMIT_STATUS_URB) {
694 spin_unlock_irqrestore(&devinfo->lock, flags);
695 return SCSI_MLQUEUE_DEVICE_BUSY;
696 }
697 spin_lock(&uas_work_lock);
698 list_add_tail(&cmdinfo->list, &uas_work_list);
699 cmdinfo->state |= IS_IN_WORK_LIST;
700 spin_unlock(&uas_work_lock);
701 schedule_work(&uas_work);
702 }
703
704 spin_unlock_irqrestore(&devinfo->lock, flags);
705 return 0;
706 }
707
708 static DEF_SCSI_QCMD(uas_queuecommand)
709
710 static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
711 const char *fname, u8 function)
712 {
713 struct Scsi_Host *shost = cmnd->device->host;
714 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
715 u16 tag = devinfo->qdepth - 1;
716 unsigned long flags;
717
718 spin_lock_irqsave(&devinfo->lock, flags);
719 memset(&devinfo->response, 0, sizeof(devinfo->response));
720 if (uas_submit_sense_urb(shost, GFP_ATOMIC, tag)) {
721 shost_printk(KERN_INFO, shost,
722 "%s: %s: submit sense urb failed\n",
723 __func__, fname);
724 spin_unlock_irqrestore(&devinfo->lock, flags);
725 return FAILED;
726 }
727 if (uas_submit_task_urb(cmnd, GFP_ATOMIC, function, tag)) {
728 shost_printk(KERN_INFO, shost,
729 "%s: %s: submit task mgmt urb failed\n",
730 __func__, fname);
731 spin_unlock_irqrestore(&devinfo->lock, flags);
732 return FAILED;
733 }
734 spin_unlock_irqrestore(&devinfo->lock, flags);
735
736 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) {
737 shost_printk(KERN_INFO, shost,
738 "%s: %s timed out\n", __func__, fname);
739 return FAILED;
740 }
741 if (be16_to_cpu(devinfo->response.tag) != tag) {
742 shost_printk(KERN_INFO, shost,
743 "%s: %s failed (wrong tag %d/%d)\n", __func__,
744 fname, be16_to_cpu(devinfo->response.tag), tag);
745 return FAILED;
746 }
747 if (devinfo->response.response_code != RC_TMF_COMPLETE) {
748 shost_printk(KERN_INFO, shost,
749 "%s: %s failed (rc 0x%x)\n", __func__,
750 fname, devinfo->response.response_code);
751 return FAILED;
752 }
753 return SUCCESS;
754 }
755
756 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
757 {
758 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
759 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
760 unsigned long flags;
761 int ret;
762
763 uas_log_cmd_state(cmnd, __func__);
764 spin_lock_irqsave(&devinfo->lock, flags);
765 cmdinfo->state |= COMMAND_ABORTED;
766 if (cmdinfo->state & IS_IN_WORK_LIST) {
767 spin_lock(&uas_work_lock);
768 list_del(&cmdinfo->list);
769 cmdinfo->state &= ~IS_IN_WORK_LIST;
770 spin_unlock(&uas_work_lock);
771 }
772 if (cmdinfo->state & COMMAND_INFLIGHT) {
773 spin_unlock_irqrestore(&devinfo->lock, flags);
774 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
775 } else {
776 spin_unlock_irqrestore(&devinfo->lock, flags);
777 uas_unlink_data_urbs(devinfo, cmdinfo);
778 spin_lock_irqsave(&devinfo->lock, flags);
779 uas_try_complete(cmnd, __func__);
780 spin_unlock_irqrestore(&devinfo->lock, flags);
781 ret = SUCCESS;
782 }
783 return ret;
784 }
785
786 static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
787 {
788 sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__);
789 return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET",
790 TMF_LOGICAL_UNIT_RESET);
791 }
792
793 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
794 {
795 struct scsi_device *sdev = cmnd->device;
796 struct uas_dev_info *devinfo = sdev->hostdata;
797 struct usb_device *udev = devinfo->udev;
798 int err;
799
800 devinfo->resetting = 1;
801 uas_abort_work(devinfo);
802 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
803 usb_kill_anchored_urbs(&devinfo->sense_urbs);
804 usb_kill_anchored_urbs(&devinfo->data_urbs);
805 uas_free_streams(devinfo);
806 err = usb_reset_device(udev);
807 if (!err)
808 uas_configure_endpoints(devinfo);
809 devinfo->resetting = 0;
810
811 if (err) {
812 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
813 return FAILED;
814 }
815
816 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
817 return SUCCESS;
818 }
819
820 static int uas_slave_alloc(struct scsi_device *sdev)
821 {
822 sdev->hostdata = (void *)sdev->host->hostdata[0];
823 return 0;
824 }
825
826 static int uas_slave_configure(struct scsi_device *sdev)
827 {
828 struct uas_dev_info *devinfo = sdev->hostdata;
829 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
830 scsi_activate_tcq(sdev, devinfo->qdepth - 3);
831 return 0;
832 }
833
834 static struct scsi_host_template uas_host_template = {
835 .module = THIS_MODULE,
836 .name = "uas",
837 .queuecommand = uas_queuecommand,
838 .slave_alloc = uas_slave_alloc,
839 .slave_configure = uas_slave_configure,
840 .eh_abort_handler = uas_eh_abort_handler,
841 .eh_device_reset_handler = uas_eh_device_reset_handler,
842 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
843 .can_queue = 65536, /* Is there a limit on the _host_ ? */
844 .this_id = -1,
845 .sg_tablesize = SG_NONE,
846 .cmd_per_lun = 1, /* until we override it */
847 .skip_settle_delay = 1,
848 .ordered_tag = 1,
849 };
850
851 static struct usb_device_id uas_usb_ids[] = {
852 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
853 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
854 /* 0xaa is a prototype device I happen to have access to */
855 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
856 { }
857 };
858 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
859
860 static int uas_is_interface(struct usb_host_interface *intf)
861 {
862 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
863 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
864 intf->desc.bInterfaceProtocol == USB_PR_UAS);
865 }
866
867 static int uas_isnt_supported(struct usb_device *udev)
868 {
869 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
870
871 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
872 "support scatter-gather which is\n",
873 hcd->driver->description);
874 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
875 "alternative USB controller if you wish to use UAS.\n");
876 return -ENODEV;
877 }
878
879 static int uas_switch_interface(struct usb_device *udev,
880 struct usb_interface *intf)
881 {
882 int i;
883 int sg_supported = udev->bus->sg_tablesize != 0;
884
885 for (i = 0; i < intf->num_altsetting; i++) {
886 struct usb_host_interface *alt = &intf->altsetting[i];
887
888 if (uas_is_interface(alt)) {
889 if (!sg_supported)
890 return uas_isnt_supported(udev);
891 return usb_set_interface(udev,
892 alt->desc.bInterfaceNumber,
893 alt->desc.bAlternateSetting);
894 }
895 }
896
897 return -ENODEV;
898 }
899
900 static void uas_configure_endpoints(struct uas_dev_info *devinfo)
901 {
902 struct usb_host_endpoint *eps[4] = { };
903 struct usb_interface *intf = devinfo->intf;
904 struct usb_device *udev = devinfo->udev;
905 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
906 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
907
908 devinfo->uas_sense_old = 0;
909 devinfo->cmnd = NULL;
910
911 for (i = 0; i < n_endpoints; i++) {
912 unsigned char *extra = endpoint[i].extra;
913 int len = endpoint[i].extralen;
914 while (len > 1) {
915 if (extra[1] == USB_DT_PIPE_USAGE) {
916 unsigned pipe_id = extra[2];
917 if (pipe_id > 0 && pipe_id < 5)
918 eps[pipe_id - 1] = &endpoint[i];
919 break;
920 }
921 len -= extra[0];
922 extra += extra[0];
923 }
924 }
925
926 /*
927 * Assume that if we didn't find a control pipe descriptor, we're
928 * using a device with old firmware that happens to be set up like
929 * this.
930 */
931 if (!eps[0]) {
932 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
933 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
934 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
935 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
936
937 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
938 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
939 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
940 } else {
941 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
942 eps[0]->desc.bEndpointAddress);
943 devinfo->status_pipe = usb_rcvbulkpipe(udev,
944 eps[1]->desc.bEndpointAddress);
945 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
946 eps[2]->desc.bEndpointAddress);
947 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
948 eps[3]->desc.bEndpointAddress);
949 }
950
951 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
952 GFP_KERNEL);
953 if (devinfo->qdepth < 0) {
954 devinfo->qdepth = 256;
955 devinfo->use_streams = 0;
956 } else {
957 devinfo->use_streams = 1;
958 }
959 }
960
961 static void uas_free_streams(struct uas_dev_info *devinfo)
962 {
963 struct usb_device *udev = devinfo->udev;
964 struct usb_host_endpoint *eps[3];
965
966 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
967 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
968 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
969 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
970 }
971
972 /*
973 * XXX: What I'd like to do here is register a SCSI host for each USB host in
974 * the system. Follow usb-storage's design of registering a SCSI host for
975 * each USB device for the moment. Can implement this by walking up the
976 * USB hierarchy until we find a USB host.
977 */
978 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
979 {
980 int result;
981 struct Scsi_Host *shost;
982 struct uas_dev_info *devinfo;
983 struct usb_device *udev = interface_to_usbdev(intf);
984
985 if (uas_switch_interface(udev, intf))
986 return -ENODEV;
987
988 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
989 if (!devinfo)
990 return -ENOMEM;
991
992 result = -ENOMEM;
993 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
994 if (!shost)
995 goto free;
996
997 shost->max_cmd_len = 16 + 252;
998 shost->max_id = 1;
999 shost->max_lun = 256;
1000 shost->max_channel = 0;
1001 shost->sg_tablesize = udev->bus->sg_tablesize;
1002
1003 devinfo->intf = intf;
1004 devinfo->udev = udev;
1005 devinfo->resetting = 0;
1006 init_usb_anchor(&devinfo->cmd_urbs);
1007 init_usb_anchor(&devinfo->sense_urbs);
1008 init_usb_anchor(&devinfo->data_urbs);
1009 spin_lock_init(&devinfo->lock);
1010 uas_configure_endpoints(devinfo);
1011
1012 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 3);
1013 if (result)
1014 goto free;
1015
1016 result = scsi_add_host(shost, &intf->dev);
1017 if (result)
1018 goto deconfig_eps;
1019
1020 shost->hostdata[0] = (unsigned long)devinfo;
1021
1022 scsi_scan_host(shost);
1023 usb_set_intfdata(intf, shost);
1024 return result;
1025
1026 deconfig_eps:
1027 uas_free_streams(devinfo);
1028 free:
1029 kfree(devinfo);
1030 if (shost)
1031 scsi_host_put(shost);
1032 return result;
1033 }
1034
1035 static int uas_pre_reset(struct usb_interface *intf)
1036 {
1037 /* XXX: Need to return 1 if it's not our device in error handling */
1038 return 0;
1039 }
1040
1041 static int uas_post_reset(struct usb_interface *intf)
1042 {
1043 /* XXX: Need to return 1 if it's not our device in error handling */
1044 return 0;
1045 }
1046
1047 static void uas_disconnect(struct usb_interface *intf)
1048 {
1049 struct Scsi_Host *shost = usb_get_intfdata(intf);
1050 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
1051
1052 devinfo->resetting = 1;
1053 uas_abort_work(devinfo);
1054 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
1055 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1056 usb_kill_anchored_urbs(&devinfo->data_urbs);
1057 scsi_remove_host(shost);
1058 uas_free_streams(devinfo);
1059 kfree(devinfo);
1060 }
1061
1062 /*
1063 * XXX: Should this plug into libusual so we can auto-upgrade devices from
1064 * Bulk-Only to UAS?
1065 */
1066 static struct usb_driver uas_driver = {
1067 .name = "uas",
1068 .probe = uas_probe,
1069 .disconnect = uas_disconnect,
1070 .pre_reset = uas_pre_reset,
1071 .post_reset = uas_post_reset,
1072 .id_table = uas_usb_ids,
1073 };
1074
1075 module_usb_driver(uas_driver);
1076
1077 MODULE_LICENSE("GPL");
1078 MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");