blob: 28d9b1909389d4197c6e26712cf113359b21ea1b [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>
16#include <linux/usb/storage.h>
17
18#include <scsi/scsi.h>
19#include <scsi/scsi_dbg.h>
20#include <scsi/scsi_cmnd.h>
21#include <scsi/scsi_device.h>
22#include <scsi/scsi_host.h>
23#include <scsi/scsi_tcq.h>
24
25/* Common header for all IUs */
26struct iu {
27 __u8 iu_id;
28 __u8 rsvd1;
29 __be16 tag;
30};
31
32enum {
33 IU_ID_COMMAND = 0x01,
34 IU_ID_STATUS = 0x03,
35 IU_ID_RESPONSE = 0x04,
36 IU_ID_TASK_MGMT = 0x05,
37 IU_ID_READ_READY = 0x06,
38 IU_ID_WRITE_READY = 0x07,
39};
40
41struct command_iu {
42 __u8 iu_id;
43 __u8 rsvd1;
44 __be16 tag;
45 __u8 prio_attr;
46 __u8 rsvd5;
47 __u8 len;
48 __u8 rsvd7;
49 struct scsi_lun lun;
50 __u8 cdb[16]; /* XXX: Overflow-checking tools may misunderstand */
51};
52
Matthew Wilcox4400ef32010-12-15 15:44:02 -050053/*
54 * Also used for the Read Ready and Write Ready IUs since they have the
55 * same first four bytes
56 */
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020057struct sense_iu {
58 __u8 iu_id;
59 __u8 rsvd1;
60 __be16 tag;
61 __be16 status_qual;
62 __u8 status;
Matthew Wilcox4400ef32010-12-15 15:44:02 -050063 __u8 rsvd7[7];
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020064 __be16 len;
65 __u8 sense[SCSI_SENSE_BUFFERSIZE];
66};
67
68/*
69 * The r00-r01c specs define this version of the SENSE IU data structure.
70 * It's still in use by several different firmware releases.
71 */
72struct sense_iu_old {
73 __u8 iu_id;
74 __u8 rsvd1;
75 __be16 tag;
76 __be16 len;
77 __u8 status;
78 __u8 service_response;
79 __u8 sense[SCSI_SENSE_BUFFERSIZE];
80};
81
82enum {
83 CMD_PIPE_ID = 1,
84 STATUS_PIPE_ID = 2,
85 DATA_IN_PIPE_ID = 3,
86 DATA_OUT_PIPE_ID = 4,
87
88 UAS_SIMPLE_TAG = 0,
89 UAS_HEAD_TAG = 1,
90 UAS_ORDERED_TAG = 2,
91 UAS_ACA = 4,
92};
93
94struct uas_dev_info {
95 struct usb_interface *intf;
96 struct usb_device *udev;
97 int qdepth;
98 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
99 unsigned use_streams:1;
100 unsigned uas_sense_old:1;
101};
102
103enum {
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500104 ALLOC_STATUS_URB = (1 << 0),
105 SUBMIT_STATUS_URB = (1 << 1),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200106 ALLOC_DATA_IN_URB = (1 << 2),
107 SUBMIT_DATA_IN_URB = (1 << 3),
108 ALLOC_DATA_OUT_URB = (1 << 4),
109 SUBMIT_DATA_OUT_URB = (1 << 5),
110 ALLOC_CMD_URB = (1 << 6),
111 SUBMIT_CMD_URB = (1 << 7),
112};
113
114/* Overrides scsi_pointer */
115struct uas_cmd_info {
116 unsigned int state;
117 unsigned int stream;
118 struct urb *cmd_urb;
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500119 struct urb *status_urb;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200120 struct urb *data_in_urb;
121 struct urb *data_out_urb;
122 struct list_head list;
123};
124
125/* I hate forward declarations, but I actually have a loop */
126static int uas_submit_urbs(struct scsi_cmnd *cmnd,
127 struct uas_dev_info *devinfo, gfp_t gfp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800128static void uas_do_work(struct work_struct *work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200129
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800130static DECLARE_WORK(uas_work, uas_do_work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200131static DEFINE_SPINLOCK(uas_work_lock);
132static LIST_HEAD(uas_work_list);
133
134static void uas_do_work(struct work_struct *work)
135{
136 struct uas_cmd_info *cmdinfo;
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800137 struct uas_cmd_info *temp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200138 struct list_head list;
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800139 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200140
141 spin_lock_irq(&uas_work_lock);
142 list_replace_init(&uas_work_list, &list);
143 spin_unlock_irq(&uas_work_lock);
144
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800145 list_for_each_entry_safe(cmdinfo, temp, &list, list) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200146 struct scsi_pointer *scp = (void *)cmdinfo;
147 struct scsi_cmnd *cmnd = container_of(scp,
148 struct scsi_cmnd, SCp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800149 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
150 if (err) {
151 list_del(&cmdinfo->list);
152 spin_lock_irq(&uas_work_lock);
153 list_add_tail(&cmdinfo->list, &uas_work_list);
154 spin_unlock_irq(&uas_work_lock);
155 schedule_work(&uas_work);
156 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200157 }
158}
159
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200160static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
161{
162 struct sense_iu *sense_iu = urb->transfer_buffer;
163 struct scsi_device *sdev = cmnd->device;
164
165 if (urb->actual_length > 16) {
166 unsigned len = be16_to_cpup(&sense_iu->len);
167 if (len + 16 != urb->actual_length) {
168 int newlen = min(len + 16, urb->actual_length) - 16;
169 if (newlen < 0)
170 newlen = 0;
171 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
172 "disagrees with IU sense data length %d, "
173 "using %d bytes of sense data\n", __func__,
174 urb->actual_length, len, newlen);
175 len = newlen;
176 }
177 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
178 }
179
180 cmnd->result = sense_iu->status;
181 if (sdev->current_cmnd)
182 sdev->current_cmnd = NULL;
183 cmnd->scsi_done(cmnd);
184 usb_free_urb(urb);
185}
186
187static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
188{
189 struct sense_iu_old *sense_iu = urb->transfer_buffer;
190 struct scsi_device *sdev = cmnd->device;
191
192 if (urb->actual_length > 8) {
193 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
194 if (len + 8 != urb->actual_length) {
195 int newlen = min(len + 8, urb->actual_length) - 8;
196 if (newlen < 0)
197 newlen = 0;
198 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
199 "disagrees with IU sense data length %d, "
200 "using %d bytes of sense data\n", __func__,
201 urb->actual_length, len, newlen);
202 len = newlen;
203 }
204 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
205 }
206
207 cmnd->result = sense_iu->status;
208 if (sdev->current_cmnd)
209 sdev->current_cmnd = NULL;
210 cmnd->scsi_done(cmnd);
211 usb_free_urb(urb);
212}
213
214static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
215 unsigned direction)
216{
217 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
218 int err;
219
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500220 cmdinfo->state = direction | SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200221 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
222 if (err) {
223 spin_lock(&uas_work_lock);
224 list_add_tail(&cmdinfo->list, &uas_work_list);
225 spin_unlock(&uas_work_lock);
226 schedule_work(&uas_work);
227 }
228}
229
230static void uas_stat_cmplt(struct urb *urb)
231{
232 struct iu *iu = urb->transfer_buffer;
233 struct scsi_device *sdev = urb->context;
234 struct uas_dev_info *devinfo = sdev->hostdata;
235 struct scsi_cmnd *cmnd;
236 u16 tag;
237
238 if (urb->status) {
239 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
240 usb_free_urb(urb);
241 return;
242 }
243
244 tag = be16_to_cpup(&iu->tag) - 1;
245 if (sdev->current_cmnd)
246 cmnd = sdev->current_cmnd;
247 else
248 cmnd = scsi_find_tag(sdev, tag);
249 if (!cmnd)
250 return;
251
252 switch (iu->iu_id) {
253 case IU_ID_STATUS:
254 if (urb->actual_length < 16)
255 devinfo->uas_sense_old = 1;
256 if (devinfo->uas_sense_old)
257 uas_sense_old(urb, cmnd);
258 else
259 uas_sense(urb, cmnd);
260 break;
261 case IU_ID_READ_READY:
262 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
263 break;
264 case IU_ID_WRITE_READY:
265 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
266 break;
267 default:
268 scmd_printk(KERN_ERR, cmnd,
269 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
270 }
271}
272
273static void uas_data_cmplt(struct urb *urb)
274{
275 struct scsi_data_buffer *sdb = urb->context;
276 sdb->resid = sdb->length - urb->actual_length;
277 usb_free_urb(urb);
278}
279
280static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
281 unsigned int pipe, u16 stream_id,
282 struct scsi_data_buffer *sdb,
283 enum dma_data_direction dir)
284{
285 struct usb_device *udev = devinfo->udev;
286 struct urb *urb = usb_alloc_urb(0, gfp);
287
288 if (!urb)
289 goto out;
290 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, uas_data_cmplt,
291 sdb);
292 if (devinfo->use_streams)
293 urb->stream_id = stream_id;
294 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
295 urb->sg = sdb->table.sgl;
296 out:
297 return urb;
298}
299
300static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
301 struct scsi_cmnd *cmnd, u16 stream_id)
302{
303 struct usb_device *udev = devinfo->udev;
304 struct urb *urb = usb_alloc_urb(0, gfp);
305 struct sense_iu *iu;
306
307 if (!urb)
308 goto out;
309
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500310 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200311 if (!iu)
312 goto free;
313
314 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
315 uas_stat_cmplt, cmnd->device);
316 urb->stream_id = stream_id;
317 urb->transfer_flags |= URB_FREE_BUFFER;
318 out:
319 return urb;
320 free:
321 usb_free_urb(urb);
322 return NULL;
323}
324
325static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
326 struct scsi_cmnd *cmnd, u16 stream_id)
327{
328 struct usb_device *udev = devinfo->udev;
329 struct scsi_device *sdev = cmnd->device;
330 struct urb *urb = usb_alloc_urb(0, gfp);
331 struct command_iu *iu;
332 int len;
333
334 if (!urb)
335 goto out;
336
337 len = cmnd->cmd_len - 16;
338 if (len < 0)
339 len = 0;
340 len = ALIGN(len, 4);
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500341 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200342 if (!iu)
343 goto free;
344
345 iu->iu_id = IU_ID_COMMAND;
Sarah Sharp9eb44542011-12-02 11:55:46 -0800346 if (blk_rq_tagged(cmnd->request))
347 iu->tag = cpu_to_be16(cmnd->request->tag + 1);
348 else
349 iu->tag = cpu_to_be16(1);
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100350 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200351 iu->len = len;
352 int_to_scsilun(sdev->lun, &iu->lun);
353 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
354
355 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
356 usb_free_urb, NULL);
357 urb->transfer_flags |= URB_FREE_BUFFER;
358 out:
359 return urb;
360 free:
361 usb_free_urb(urb);
362 return NULL;
363}
364
365/*
366 * Why should I request the Status IU before sending the Command IU? Spec
367 * says to, but also says the device may receive them in any order. Seems
368 * daft to me.
369 */
370
371static int uas_submit_urbs(struct scsi_cmnd *cmnd,
372 struct uas_dev_info *devinfo, gfp_t gfp)
373{
374 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
375
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500376 if (cmdinfo->state & ALLOC_STATUS_URB) {
377 cmdinfo->status_urb = uas_alloc_sense_urb(devinfo, gfp, cmnd,
378 cmdinfo->stream);
379 if (!cmdinfo->status_urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200380 return SCSI_MLQUEUE_DEVICE_BUSY;
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500381 cmdinfo->state &= ~ALLOC_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200382 }
383
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500384 if (cmdinfo->state & SUBMIT_STATUS_URB) {
385 if (usb_submit_urb(cmdinfo->status_urb, gfp)) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200386 scmd_printk(KERN_INFO, cmnd,
387 "sense urb submission failure\n");
388 return SCSI_MLQUEUE_DEVICE_BUSY;
389 }
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500390 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200391 }
392
393 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
394 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
395 devinfo->data_in_pipe, cmdinfo->stream,
396 scsi_in(cmnd), DMA_FROM_DEVICE);
397 if (!cmdinfo->data_in_urb)
398 return SCSI_MLQUEUE_DEVICE_BUSY;
399 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
400 }
401
402 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
403 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
404 scmd_printk(KERN_INFO, cmnd,
405 "data in urb submission failure\n");
406 return SCSI_MLQUEUE_DEVICE_BUSY;
407 }
408 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
409 }
410
411 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
412 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
413 devinfo->data_out_pipe, cmdinfo->stream,
414 scsi_out(cmnd), DMA_TO_DEVICE);
415 if (!cmdinfo->data_out_urb)
416 return SCSI_MLQUEUE_DEVICE_BUSY;
417 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
418 }
419
420 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
421 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
422 scmd_printk(KERN_INFO, cmnd,
423 "data out urb submission failure\n");
424 return SCSI_MLQUEUE_DEVICE_BUSY;
425 }
426 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
427 }
428
429 if (cmdinfo->state & ALLOC_CMD_URB) {
430 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
431 cmdinfo->stream);
432 if (!cmdinfo->cmd_urb)
433 return SCSI_MLQUEUE_DEVICE_BUSY;
434 cmdinfo->state &= ~ALLOC_CMD_URB;
435 }
436
437 if (cmdinfo->state & SUBMIT_CMD_URB) {
438 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
439 scmd_printk(KERN_INFO, cmnd,
440 "cmd urb submission failure\n");
441 return SCSI_MLQUEUE_DEVICE_BUSY;
442 }
443 cmdinfo->state &= ~SUBMIT_CMD_URB;
444 }
445
446 return 0;
447}
448
Jeff Garzikf2812332010-11-16 02:10:29 -0500449static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200450 void (*done)(struct scsi_cmnd *))
451{
452 struct scsi_device *sdev = cmnd->device;
453 struct uas_dev_info *devinfo = sdev->hostdata;
454 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
455 int err;
456
457 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
458
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500459 if (!cmdinfo->status_urb && sdev->current_cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200460 return SCSI_MLQUEUE_DEVICE_BUSY;
461
462 if (blk_rq_tagged(cmnd->request)) {
463 cmdinfo->stream = cmnd->request->tag + 1;
464 } else {
465 sdev->current_cmnd = cmnd;
466 cmdinfo->stream = 1;
467 }
468
469 cmnd->scsi_done = done;
470
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500471 cmdinfo->state = ALLOC_STATUS_URB | SUBMIT_STATUS_URB |
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200472 ALLOC_CMD_URB | SUBMIT_CMD_URB;
473
474 switch (cmnd->sc_data_direction) {
475 case DMA_FROM_DEVICE:
476 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
477 break;
478 case DMA_BIDIRECTIONAL:
479 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
480 case DMA_TO_DEVICE:
481 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
482 case DMA_NONE:
483 break;
484 }
485
486 if (!devinfo->use_streams) {
487 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
488 cmdinfo->stream = 0;
489 }
490
491 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
492 if (err) {
493 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500494 if (cmdinfo->state & SUBMIT_STATUS_URB) {
495 usb_free_urb(cmdinfo->status_urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200496 return SCSI_MLQUEUE_DEVICE_BUSY;
497 }
498 spin_lock(&uas_work_lock);
499 list_add_tail(&cmdinfo->list, &uas_work_list);
500 spin_unlock(&uas_work_lock);
501 schedule_work(&uas_work);
502 }
503
504 return 0;
505}
506
Jeff Garzikf2812332010-11-16 02:10:29 -0500507static DEF_SCSI_QCMD(uas_queuecommand)
508
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200509static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
510{
511 struct scsi_device *sdev = cmnd->device;
512 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
513 cmnd->request->tag);
514
515/* XXX: Send ABORT TASK Task Management command */
516 return FAILED;
517}
518
519static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
520{
521 struct scsi_device *sdev = cmnd->device;
522 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
523 cmnd->request->tag);
524
525/* XXX: Send LOGICAL UNIT RESET Task Management command */
526 return FAILED;
527}
528
529static int uas_eh_target_reset_handler(struct scsi_cmnd *cmnd)
530{
531 struct scsi_device *sdev = cmnd->device;
532 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
533 cmnd->request->tag);
534
535/* XXX: Can we reset just the one USB interface?
536 * Would calling usb_set_interface() have the right effect?
537 */
538 return FAILED;
539}
540
541static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
542{
543 struct scsi_device *sdev = cmnd->device;
544 struct uas_dev_info *devinfo = sdev->hostdata;
545 struct usb_device *udev = devinfo->udev;
546
547 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
548 cmnd->request->tag);
549
550 if (usb_reset_device(udev))
551 return SUCCESS;
552
553 return FAILED;
554}
555
556static int uas_slave_alloc(struct scsi_device *sdev)
557{
558 sdev->hostdata = (void *)sdev->host->hostdata[0];
559 return 0;
560}
561
562static int uas_slave_configure(struct scsi_device *sdev)
563{
564 struct uas_dev_info *devinfo = sdev->hostdata;
565 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
566 scsi_activate_tcq(sdev, devinfo->qdepth - 1);
567 return 0;
568}
569
570static struct scsi_host_template uas_host_template = {
571 .module = THIS_MODULE,
572 .name = "uas",
573 .queuecommand = uas_queuecommand,
574 .slave_alloc = uas_slave_alloc,
575 .slave_configure = uas_slave_configure,
576 .eh_abort_handler = uas_eh_abort_handler,
577 .eh_device_reset_handler = uas_eh_device_reset_handler,
578 .eh_target_reset_handler = uas_eh_target_reset_handler,
579 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
580 .can_queue = 65536, /* Is there a limit on the _host_ ? */
581 .this_id = -1,
582 .sg_tablesize = SG_NONE,
583 .cmd_per_lun = 1, /* until we override it */
584 .skip_settle_delay = 1,
585 .ordered_tag = 1,
586};
587
588static struct usb_device_id uas_usb_ids[] = {
589 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
590 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
591 /* 0xaa is a prototype device I happen to have access to */
592 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
593 { }
594};
595MODULE_DEVICE_TABLE(usb, uas_usb_ids);
596
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500597static int uas_is_interface(struct usb_host_interface *intf)
598{
599 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
600 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
601 intf->desc.bInterfaceProtocol == USB_PR_UAS);
602}
603
604static int uas_switch_interface(struct usb_device *udev,
605 struct usb_interface *intf)
606{
607 int i;
608
609 if (uas_is_interface(intf->cur_altsetting))
610 return 0;
611
612 for (i = 0; i < intf->num_altsetting; i++) {
613 struct usb_host_interface *alt = &intf->altsetting[i];
614 if (alt == intf->cur_altsetting)
615 continue;
616 if (uas_is_interface(alt))
617 return usb_set_interface(udev,
618 alt->desc.bInterfaceNumber,
619 alt->desc.bAlternateSetting);
620 }
621
622 return -ENODEV;
623}
624
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200625static void uas_configure_endpoints(struct uas_dev_info *devinfo)
626{
627 struct usb_host_endpoint *eps[4] = { };
628 struct usb_interface *intf = devinfo->intf;
629 struct usb_device *udev = devinfo->udev;
630 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
631 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
632
633 devinfo->uas_sense_old = 0;
634
635 for (i = 0; i < n_endpoints; i++) {
636 unsigned char *extra = endpoint[i].extra;
637 int len = endpoint[i].extralen;
638 while (len > 1) {
639 if (extra[1] == USB_DT_PIPE_USAGE) {
640 unsigned pipe_id = extra[2];
641 if (pipe_id > 0 && pipe_id < 5)
642 eps[pipe_id - 1] = &endpoint[i];
643 break;
644 }
645 len -= extra[0];
646 extra += extra[0];
647 }
648 }
649
650 /*
651 * Assume that if we didn't find a control pipe descriptor, we're
652 * using a device with old firmware that happens to be set up like
653 * this.
654 */
655 if (!eps[0]) {
656 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
657 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
658 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
659 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
660
661 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
662 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
663 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
664 } else {
665 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
666 eps[0]->desc.bEndpointAddress);
667 devinfo->status_pipe = usb_rcvbulkpipe(udev,
668 eps[1]->desc.bEndpointAddress);
669 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
670 eps[2]->desc.bEndpointAddress);
671 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
672 eps[3]->desc.bEndpointAddress);
673 }
674
675 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
676 GFP_KERNEL);
677 if (devinfo->qdepth < 0) {
678 devinfo->qdepth = 256;
679 devinfo->use_streams = 0;
680 } else {
681 devinfo->use_streams = 1;
682 }
683}
684
685/*
686 * XXX: What I'd like to do here is register a SCSI host for each USB host in
687 * the system. Follow usb-storage's design of registering a SCSI host for
688 * each USB device for the moment. Can implement this by walking up the
689 * USB hierarchy until we find a USB host.
690 */
691static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
692{
693 int result;
694 struct Scsi_Host *shost;
695 struct uas_dev_info *devinfo;
696 struct usb_device *udev = interface_to_usbdev(intf);
697
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500698 if (uas_switch_interface(udev, intf))
699 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200700
701 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
702 if (!devinfo)
703 return -ENOMEM;
704
705 result = -ENOMEM;
706 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
707 if (!shost)
708 goto free;
709
710 shost->max_cmd_len = 16 + 252;
711 shost->max_id = 1;
712 shost->sg_tablesize = udev->bus->sg_tablesize;
713
714 result = scsi_add_host(shost, &intf->dev);
715 if (result)
716 goto free;
717 shost->hostdata[0] = (unsigned long)devinfo;
718
719 devinfo->intf = intf;
720 devinfo->udev = udev;
721 uas_configure_endpoints(devinfo);
722
723 scsi_scan_host(shost);
724 usb_set_intfdata(intf, shost);
725 return result;
726 free:
727 kfree(devinfo);
728 if (shost)
729 scsi_host_put(shost);
730 return result;
731}
732
733static int uas_pre_reset(struct usb_interface *intf)
734{
735/* XXX: Need to return 1 if it's not our device in error handling */
736 return 0;
737}
738
739static int uas_post_reset(struct usb_interface *intf)
740{
741/* XXX: Need to return 1 if it's not our device in error handling */
742 return 0;
743}
744
745static void uas_disconnect(struct usb_interface *intf)
746{
747 struct usb_device *udev = interface_to_usbdev(intf);
748 struct usb_host_endpoint *eps[3];
749 struct Scsi_Host *shost = usb_get_intfdata(intf);
750 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
751
752 scsi_remove_host(shost);
753
754 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
755 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
756 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
757 usb_free_streams(intf, eps, 3, GFP_KERNEL);
758
759 kfree(devinfo);
760}
761
762/*
763 * XXX: Should this plug into libusual so we can auto-upgrade devices from
764 * Bulk-Only to UAS?
765 */
766static struct usb_driver uas_driver = {
767 .name = "uas",
768 .probe = uas_probe,
769 .disconnect = uas_disconnect,
770 .pre_reset = uas_pre_reset,
771 .post_reset = uas_post_reset,
772 .id_table = uas_usb_ids,
773};
774
775static int uas_init(void)
776{
777 return usb_register(&uas_driver);
778}
779
780static void uas_exit(void)
781{
782 usb_deregister(&uas_driver);
783}
784
785module_init(uas_init);
786module_exit(uas_exit);
787
788MODULE_LICENSE("GPL");
789MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");