blob: e34c75970ed1c9d9746eff3ed56bd2cb144bdf82 [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>
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +010016#include <linux/usb/hcd.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020017#include <linux/usb/storage.h>
18
19#include <scsi/scsi.h>
20#include <scsi/scsi_dbg.h>
21#include <scsi/scsi_cmnd.h>
22#include <scsi/scsi_device.h>
23#include <scsi/scsi_host.h>
24#include <scsi/scsi_tcq.h>
25
26/* Common header for all IUs */
27struct iu {
28 __u8 iu_id;
29 __u8 rsvd1;
30 __be16 tag;
31};
32
33enum {
34 IU_ID_COMMAND = 0x01,
35 IU_ID_STATUS = 0x03,
36 IU_ID_RESPONSE = 0x04,
37 IU_ID_TASK_MGMT = 0x05,
38 IU_ID_READ_READY = 0x06,
39 IU_ID_WRITE_READY = 0x07,
40};
41
42struct command_iu {
43 __u8 iu_id;
44 __u8 rsvd1;
45 __be16 tag;
46 __u8 prio_attr;
47 __u8 rsvd5;
48 __u8 len;
49 __u8 rsvd7;
50 struct scsi_lun lun;
51 __u8 cdb[16]; /* XXX: Overflow-checking tools may misunderstand */
52};
53
Matthew Wilcox4400ef32010-12-15 15:44:02 -050054/*
55 * Also used for the Read Ready and Write Ready IUs since they have the
56 * same first four bytes
57 */
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020058struct sense_iu {
59 __u8 iu_id;
60 __u8 rsvd1;
61 __be16 tag;
62 __be16 status_qual;
63 __u8 status;
Matthew Wilcox4400ef32010-12-15 15:44:02 -050064 __u8 rsvd7[7];
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020065 __be16 len;
66 __u8 sense[SCSI_SENSE_BUFFERSIZE];
67};
68
69/*
70 * The r00-r01c specs define this version of the SENSE IU data structure.
71 * It's still in use by several different firmware releases.
72 */
73struct sense_iu_old {
74 __u8 iu_id;
75 __u8 rsvd1;
76 __be16 tag;
77 __be16 len;
78 __u8 status;
79 __u8 service_response;
80 __u8 sense[SCSI_SENSE_BUFFERSIZE];
81};
82
83enum {
84 CMD_PIPE_ID = 1,
85 STATUS_PIPE_ID = 2,
86 DATA_IN_PIPE_ID = 3,
87 DATA_OUT_PIPE_ID = 4,
88
89 UAS_SIMPLE_TAG = 0,
90 UAS_HEAD_TAG = 1,
91 UAS_ORDERED_TAG = 2,
92 UAS_ACA = 4,
93};
94
95struct uas_dev_info {
96 struct usb_interface *intf;
97 struct usb_device *udev;
98 int qdepth;
99 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
100 unsigned use_streams:1;
101 unsigned uas_sense_old:1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100102 struct scsi_cmnd *cmnd;
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100103 struct urb *status_urb; /* used only if stream support is available */
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200104};
105
106enum {
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500107 ALLOC_STATUS_URB = (1 << 0),
108 SUBMIT_STATUS_URB = (1 << 1),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200109 ALLOC_DATA_IN_URB = (1 << 2),
110 SUBMIT_DATA_IN_URB = (1 << 3),
111 ALLOC_DATA_OUT_URB = (1 << 4),
112 SUBMIT_DATA_OUT_URB = (1 << 5),
113 ALLOC_CMD_URB = (1 << 6),
114 SUBMIT_CMD_URB = (1 << 7),
115};
116
117/* Overrides scsi_pointer */
118struct uas_cmd_info {
119 unsigned int state;
120 unsigned int stream;
121 struct urb *cmd_urb;
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100122 /* status_urb is used only if stream support isn't available */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500123 struct urb *status_urb;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200124 struct urb *data_in_urb;
125 struct urb *data_out_urb;
126 struct list_head list;
127};
128
129/* I hate forward declarations, but I actually have a loop */
130static int uas_submit_urbs(struct scsi_cmnd *cmnd,
131 struct uas_dev_info *devinfo, gfp_t gfp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800132static void uas_do_work(struct work_struct *work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200133
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800134static DECLARE_WORK(uas_work, uas_do_work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200135static DEFINE_SPINLOCK(uas_work_lock);
136static LIST_HEAD(uas_work_list);
137
138static void uas_do_work(struct work_struct *work)
139{
140 struct uas_cmd_info *cmdinfo;
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800141 struct uas_cmd_info *temp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200142 struct list_head list;
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800143 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200144
145 spin_lock_irq(&uas_work_lock);
146 list_replace_init(&uas_work_list, &list);
147 spin_unlock_irq(&uas_work_lock);
148
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800149 list_for_each_entry_safe(cmdinfo, temp, &list, list) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200150 struct scsi_pointer *scp = (void *)cmdinfo;
151 struct scsi_cmnd *cmnd = container_of(scp,
152 struct scsi_cmnd, SCp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800153 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
154 if (err) {
155 list_del(&cmdinfo->list);
156 spin_lock_irq(&uas_work_lock);
157 list_add_tail(&cmdinfo->list, &uas_work_list);
158 spin_unlock_irq(&uas_work_lock);
159 schedule_work(&uas_work);
160 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200161 }
162}
163
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200164static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
165{
166 struct sense_iu *sense_iu = urb->transfer_buffer;
167 struct scsi_device *sdev = cmnd->device;
168
169 if (urb->actual_length > 16) {
170 unsigned len = be16_to_cpup(&sense_iu->len);
171 if (len + 16 != urb->actual_length) {
172 int newlen = min(len + 16, urb->actual_length) - 16;
173 if (newlen < 0)
174 newlen = 0;
175 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
176 "disagrees with IU sense data length %d, "
177 "using %d bytes of sense data\n", __func__,
178 urb->actual_length, len, newlen);
179 len = newlen;
180 }
181 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
182 }
183
184 cmnd->result = sense_iu->status;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200185 cmnd->scsi_done(cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200186}
187
188static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
189{
190 struct sense_iu_old *sense_iu = urb->transfer_buffer;
191 struct scsi_device *sdev = cmnd->device;
192
193 if (urb->actual_length > 8) {
194 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
195 if (len + 8 != urb->actual_length) {
196 int newlen = min(len + 8, urb->actual_length) - 8;
197 if (newlen < 0)
198 newlen = 0;
199 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
200 "disagrees with IU sense data length %d, "
201 "using %d bytes of sense data\n", __func__,
202 urb->actual_length, len, newlen);
203 len = newlen;
204 }
205 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
206 }
207
208 cmnd->result = sense_iu->status;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200209 cmnd->scsi_done(cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200210}
211
212static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
213 unsigned direction)
214{
215 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
216 int err;
217
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100218 cmdinfo->state = direction;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200219 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
220 if (err) {
221 spin_lock(&uas_work_lock);
222 list_add_tail(&cmdinfo->list, &uas_work_list);
223 spin_unlock(&uas_work_lock);
224 schedule_work(&uas_work);
225 }
226}
227
228static void uas_stat_cmplt(struct urb *urb)
229{
230 struct iu *iu = urb->transfer_buffer;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100231 struct Scsi_Host *shost = urb->context;
232 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200233 struct scsi_cmnd *cmnd;
234 u16 tag;
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100235 int ret;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200236
237 if (urb->status) {
238 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100239 if (devinfo->use_streams)
240 usb_free_urb(urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200241 return;
242 }
243
244 tag = be16_to_cpup(&iu->tag) - 1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100245 if (tag == 0)
246 cmnd = devinfo->cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200247 else
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100248 cmnd = scsi_host_find_tag(shost, tag - 1);
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800249 if (!cmnd) {
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100250 if (devinfo->use_streams) {
251 usb_free_urb(urb);
252 return;
253 }
254 ret = usb_submit_urb(urb, GFP_ATOMIC);
255 if (ret)
256 dev_err(&urb->dev->dev, "failed submit status urb\n");
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200257 return;
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800258 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200259
260 switch (iu->iu_id) {
261 case IU_ID_STATUS:
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100262 if (devinfo->cmnd == cmnd)
263 devinfo->cmnd = NULL;
264
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200265 if (urb->actual_length < 16)
266 devinfo->uas_sense_old = 1;
267 if (devinfo->uas_sense_old)
268 uas_sense_old(urb, cmnd);
269 else
270 uas_sense(urb, cmnd);
271 break;
272 case IU_ID_READ_READY:
273 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
274 break;
275 case IU_ID_WRITE_READY:
276 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
277 break;
278 default:
279 scmd_printk(KERN_ERR, cmnd,
280 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
281 }
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100282
283 if (devinfo->use_streams) {
284 usb_free_urb(urb);
285 return;
286 }
287
288 ret = usb_submit_urb(urb, GFP_ATOMIC);
289 if (ret)
290 dev_err(&urb->dev->dev, "failed submit status urb\n");
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200291}
292
293static void uas_data_cmplt(struct urb *urb)
294{
295 struct scsi_data_buffer *sdb = urb->context;
296 sdb->resid = sdb->length - urb->actual_length;
297 usb_free_urb(urb);
298}
299
300static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
301 unsigned int pipe, u16 stream_id,
302 struct scsi_data_buffer *sdb,
303 enum dma_data_direction dir)
304{
305 struct usb_device *udev = devinfo->udev;
306 struct urb *urb = usb_alloc_urb(0, gfp);
307
308 if (!urb)
309 goto out;
310 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, uas_data_cmplt,
311 sdb);
312 if (devinfo->use_streams)
313 urb->stream_id = stream_id;
314 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
315 urb->sg = sdb->table.sgl;
316 out:
317 return urb;
318}
319
320static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100321 struct Scsi_Host *shost, u16 stream_id)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200322{
323 struct usb_device *udev = devinfo->udev;
324 struct urb *urb = usb_alloc_urb(0, gfp);
325 struct sense_iu *iu;
326
327 if (!urb)
328 goto out;
329
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500330 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200331 if (!iu)
332 goto free;
333
334 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100335 uas_stat_cmplt, shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200336 urb->stream_id = stream_id;
337 urb->transfer_flags |= URB_FREE_BUFFER;
338 out:
339 return urb;
340 free:
341 usb_free_urb(urb);
342 return NULL;
343}
344
345static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
346 struct scsi_cmnd *cmnd, u16 stream_id)
347{
348 struct usb_device *udev = devinfo->udev;
349 struct scsi_device *sdev = cmnd->device;
350 struct urb *urb = usb_alloc_urb(0, gfp);
351 struct command_iu *iu;
352 int len;
353
354 if (!urb)
355 goto out;
356
357 len = cmnd->cmd_len - 16;
358 if (len < 0)
359 len = 0;
360 len = ALIGN(len, 4);
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500361 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200362 if (!iu)
363 goto free;
364
365 iu->iu_id = IU_ID_COMMAND;
Sarah Sharp9eb44542011-12-02 11:55:46 -0800366 if (blk_rq_tagged(cmnd->request))
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100367 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
Sarah Sharp9eb44542011-12-02 11:55:46 -0800368 else
369 iu->tag = cpu_to_be16(1);
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100370 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200371 iu->len = len;
372 int_to_scsilun(sdev->lun, &iu->lun);
373 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
374
375 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
376 usb_free_urb, NULL);
377 urb->transfer_flags |= URB_FREE_BUFFER;
378 out:
379 return urb;
380 free:
381 usb_free_urb(urb);
382 return NULL;
383}
384
385/*
386 * Why should I request the Status IU before sending the Command IU? Spec
387 * says to, but also says the device may receive them in any order. Seems
388 * daft to me.
389 */
390
391static int uas_submit_urbs(struct scsi_cmnd *cmnd,
392 struct uas_dev_info *devinfo, gfp_t gfp)
393{
394 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
395
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500396 if (cmdinfo->state & ALLOC_STATUS_URB) {
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100397 cmdinfo->status_urb = uas_alloc_sense_urb(devinfo, gfp,
398 cmnd->device->host, cmdinfo->stream);
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500399 if (!cmdinfo->status_urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200400 return SCSI_MLQUEUE_DEVICE_BUSY;
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500401 cmdinfo->state &= ~ALLOC_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200402 }
403
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500404 if (cmdinfo->state & SUBMIT_STATUS_URB) {
405 if (usb_submit_urb(cmdinfo->status_urb, gfp)) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200406 scmd_printk(KERN_INFO, cmnd,
407 "sense urb submission failure\n");
408 return SCSI_MLQUEUE_DEVICE_BUSY;
409 }
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500410 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200411 }
412
413 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
414 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
415 devinfo->data_in_pipe, cmdinfo->stream,
416 scsi_in(cmnd), DMA_FROM_DEVICE);
417 if (!cmdinfo->data_in_urb)
418 return SCSI_MLQUEUE_DEVICE_BUSY;
419 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
420 }
421
422 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
423 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
424 scmd_printk(KERN_INFO, cmnd,
425 "data in urb submission failure\n");
426 return SCSI_MLQUEUE_DEVICE_BUSY;
427 }
428 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
429 }
430
431 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
432 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
433 devinfo->data_out_pipe, cmdinfo->stream,
434 scsi_out(cmnd), DMA_TO_DEVICE);
435 if (!cmdinfo->data_out_urb)
436 return SCSI_MLQUEUE_DEVICE_BUSY;
437 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
438 }
439
440 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
441 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
442 scmd_printk(KERN_INFO, cmnd,
443 "data out urb submission failure\n");
444 return SCSI_MLQUEUE_DEVICE_BUSY;
445 }
446 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
447 }
448
449 if (cmdinfo->state & ALLOC_CMD_URB) {
450 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
451 cmdinfo->stream);
452 if (!cmdinfo->cmd_urb)
453 return SCSI_MLQUEUE_DEVICE_BUSY;
454 cmdinfo->state &= ~ALLOC_CMD_URB;
455 }
456
457 if (cmdinfo->state & SUBMIT_CMD_URB) {
458 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
459 scmd_printk(KERN_INFO, cmnd,
460 "cmd urb submission failure\n");
461 return SCSI_MLQUEUE_DEVICE_BUSY;
462 }
463 cmdinfo->state &= ~SUBMIT_CMD_URB;
464 }
465
466 return 0;
467}
468
Jeff Garzikf2812332010-11-16 02:10:29 -0500469static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200470 void (*done)(struct scsi_cmnd *))
471{
472 struct scsi_device *sdev = cmnd->device;
473 struct uas_dev_info *devinfo = sdev->hostdata;
474 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
475 int err;
476
477 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
478
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100479 if (devinfo->cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200480 return SCSI_MLQUEUE_DEVICE_BUSY;
481
482 if (blk_rq_tagged(cmnd->request)) {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100483 cmdinfo->stream = cmnd->request->tag + 2;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200484 } else {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100485 devinfo->cmnd = cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200486 cmdinfo->stream = 1;
487 }
488
489 cmnd->scsi_done = done;
490
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500491 cmdinfo->state = ALLOC_STATUS_URB | SUBMIT_STATUS_URB |
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200492 ALLOC_CMD_URB | SUBMIT_CMD_URB;
493
494 switch (cmnd->sc_data_direction) {
495 case DMA_FROM_DEVICE:
496 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
497 break;
498 case DMA_BIDIRECTIONAL:
499 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
500 case DMA_TO_DEVICE:
501 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
502 case DMA_NONE:
503 break;
504 }
505
506 if (!devinfo->use_streams) {
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100507 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB |
508 ALLOC_STATUS_URB | SUBMIT_STATUS_URB);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200509 cmdinfo->stream = 0;
510 }
511
512 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
513 if (err) {
514 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500515 if (cmdinfo->state & SUBMIT_STATUS_URB) {
516 usb_free_urb(cmdinfo->status_urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200517 return SCSI_MLQUEUE_DEVICE_BUSY;
518 }
519 spin_lock(&uas_work_lock);
520 list_add_tail(&cmdinfo->list, &uas_work_list);
521 spin_unlock(&uas_work_lock);
522 schedule_work(&uas_work);
523 }
524
525 return 0;
526}
527
Jeff Garzikf2812332010-11-16 02:10:29 -0500528static DEF_SCSI_QCMD(uas_queuecommand)
529
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200530static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
531{
532 struct scsi_device *sdev = cmnd->device;
533 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
534 cmnd->request->tag);
535
536/* XXX: Send ABORT TASK Task Management command */
537 return FAILED;
538}
539
540static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
541{
542 struct scsi_device *sdev = cmnd->device;
543 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
544 cmnd->request->tag);
545
546/* XXX: Send LOGICAL UNIT RESET Task Management command */
547 return FAILED;
548}
549
550static int uas_eh_target_reset_handler(struct scsi_cmnd *cmnd)
551{
552 struct scsi_device *sdev = cmnd->device;
553 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
554 cmnd->request->tag);
555
556/* XXX: Can we reset just the one USB interface?
557 * Would calling usb_set_interface() have the right effect?
558 */
559 return FAILED;
560}
561
562static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
563{
564 struct scsi_device *sdev = cmnd->device;
565 struct uas_dev_info *devinfo = sdev->hostdata;
566 struct usb_device *udev = devinfo->udev;
567
568 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
569 cmnd->request->tag);
570
571 if (usb_reset_device(udev))
572 return SUCCESS;
573
574 return FAILED;
575}
576
577static int uas_slave_alloc(struct scsi_device *sdev)
578{
579 sdev->hostdata = (void *)sdev->host->hostdata[0];
580 return 0;
581}
582
583static int uas_slave_configure(struct scsi_device *sdev)
584{
585 struct uas_dev_info *devinfo = sdev->hostdata;
586 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100587 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200588 return 0;
589}
590
591static struct scsi_host_template uas_host_template = {
592 .module = THIS_MODULE,
593 .name = "uas",
594 .queuecommand = uas_queuecommand,
595 .slave_alloc = uas_slave_alloc,
596 .slave_configure = uas_slave_configure,
597 .eh_abort_handler = uas_eh_abort_handler,
598 .eh_device_reset_handler = uas_eh_device_reset_handler,
599 .eh_target_reset_handler = uas_eh_target_reset_handler,
600 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
601 .can_queue = 65536, /* Is there a limit on the _host_ ? */
602 .this_id = -1,
603 .sg_tablesize = SG_NONE,
604 .cmd_per_lun = 1, /* until we override it */
605 .skip_settle_delay = 1,
606 .ordered_tag = 1,
607};
608
609static struct usb_device_id uas_usb_ids[] = {
610 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
611 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
612 /* 0xaa is a prototype device I happen to have access to */
613 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
614 { }
615};
616MODULE_DEVICE_TABLE(usb, uas_usb_ids);
617
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500618static int uas_is_interface(struct usb_host_interface *intf)
619{
620 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
621 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
622 intf->desc.bInterfaceProtocol == USB_PR_UAS);
623}
624
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100625static int uas_isnt_supported(struct usb_device *udev)
626{
627 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
628
629 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
630 "support scatter-gather which is\n",
631 hcd->driver->description);
632 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
633 "alternative USB controller if you wish to use UAS.\n");
634 return -ENODEV;
635}
636
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500637static int uas_switch_interface(struct usb_device *udev,
638 struct usb_interface *intf)
639{
640 int i;
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100641 int sg_supported = udev->bus->sg_tablesize != 0;
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500642
643 for (i = 0; i < intf->num_altsetting; i++) {
644 struct usb_host_interface *alt = &intf->altsetting[i];
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100645
646 if (uas_is_interface(alt)) {
647 if (!sg_supported)
648 return uas_isnt_supported(udev);
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500649 return usb_set_interface(udev,
650 alt->desc.bInterfaceNumber,
651 alt->desc.bAlternateSetting);
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100652 }
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500653 }
654
655 return -ENODEV;
656}
657
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200658static void uas_configure_endpoints(struct uas_dev_info *devinfo)
659{
660 struct usb_host_endpoint *eps[4] = { };
661 struct usb_interface *intf = devinfo->intf;
662 struct usb_device *udev = devinfo->udev;
663 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
664 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
665
666 devinfo->uas_sense_old = 0;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100667 devinfo->cmnd = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200668
669 for (i = 0; i < n_endpoints; i++) {
670 unsigned char *extra = endpoint[i].extra;
671 int len = endpoint[i].extralen;
672 while (len > 1) {
673 if (extra[1] == USB_DT_PIPE_USAGE) {
674 unsigned pipe_id = extra[2];
675 if (pipe_id > 0 && pipe_id < 5)
676 eps[pipe_id - 1] = &endpoint[i];
677 break;
678 }
679 len -= extra[0];
680 extra += extra[0];
681 }
682 }
683
684 /*
685 * Assume that if we didn't find a control pipe descriptor, we're
686 * using a device with old firmware that happens to be set up like
687 * this.
688 */
689 if (!eps[0]) {
690 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
691 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
692 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
693 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
694
695 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
696 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
697 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
698 } else {
699 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
700 eps[0]->desc.bEndpointAddress);
701 devinfo->status_pipe = usb_rcvbulkpipe(udev,
702 eps[1]->desc.bEndpointAddress);
703 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
704 eps[2]->desc.bEndpointAddress);
705 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
706 eps[3]->desc.bEndpointAddress);
707 }
708
709 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
710 GFP_KERNEL);
711 if (devinfo->qdepth < 0) {
712 devinfo->qdepth = 256;
713 devinfo->use_streams = 0;
714 } else {
715 devinfo->use_streams = 1;
716 }
717}
718
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100719static int uas_alloc_status_urb(struct uas_dev_info *devinfo,
720 struct Scsi_Host *shost)
721{
722 if (devinfo->use_streams) {
723 devinfo->status_urb = NULL;
724 return 0;
725 }
726
727 devinfo->status_urb = uas_alloc_sense_urb(devinfo, GFP_KERNEL,
728 shost, 0);
729 if (!devinfo->status_urb)
730 goto err_s_urb;
731
732 if (usb_submit_urb(devinfo->status_urb, GFP_KERNEL))
733 goto err_submit_urb;
734
735 return 0;
736err_submit_urb:
737 usb_free_urb(devinfo->status_urb);
738err_s_urb:
739 return -ENOMEM;
740}
741
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100742static void uas_free_streams(struct uas_dev_info *devinfo)
743{
744 struct usb_device *udev = devinfo->udev;
745 struct usb_host_endpoint *eps[3];
746
747 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
748 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
749 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
750 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
751}
752
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200753/*
754 * XXX: What I'd like to do here is register a SCSI host for each USB host in
755 * the system. Follow usb-storage's design of registering a SCSI host for
756 * each USB device for the moment. Can implement this by walking up the
757 * USB hierarchy until we find a USB host.
758 */
759static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
760{
761 int result;
762 struct Scsi_Host *shost;
763 struct uas_dev_info *devinfo;
764 struct usb_device *udev = interface_to_usbdev(intf);
765
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500766 if (uas_switch_interface(udev, intf))
767 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200768
769 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
770 if (!devinfo)
771 return -ENOMEM;
772
773 result = -ENOMEM;
774 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
775 if (!shost)
776 goto free;
777
778 shost->max_cmd_len = 16 + 252;
779 shost->max_id = 1;
780 shost->sg_tablesize = udev->bus->sg_tablesize;
781
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200782 devinfo->intf = intf;
783 devinfo->udev = udev;
784 uas_configure_endpoints(devinfo);
785
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100786 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100787 if (result)
788 goto free;
789
790 result = scsi_add_host(shost, &intf->dev);
791 if (result)
792 goto deconfig_eps;
793
794 shost->hostdata[0] = (unsigned long)devinfo;
795
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100796 result = uas_alloc_status_urb(devinfo, shost);
797 if (result)
798 goto err_alloc_status;
799
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200800 scsi_scan_host(shost);
801 usb_set_intfdata(intf, shost);
802 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100803
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100804err_alloc_status:
805 scsi_remove_host(shost);
806 shost = NULL;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100807deconfig_eps:
808 uas_free_streams(devinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200809 free:
810 kfree(devinfo);
811 if (shost)
812 scsi_host_put(shost);
813 return result;
814}
815
816static int uas_pre_reset(struct usb_interface *intf)
817{
818/* XXX: Need to return 1 if it's not our device in error handling */
819 return 0;
820}
821
822static int uas_post_reset(struct usb_interface *intf)
823{
824/* XXX: Need to return 1 if it's not our device in error handling */
825 return 0;
826}
827
828static void uas_disconnect(struct usb_interface *intf)
829{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200830 struct Scsi_Host *shost = usb_get_intfdata(intf);
831 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
832
833 scsi_remove_host(shost);
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100834 usb_kill_urb(devinfo->status_urb);
835 usb_free_urb(devinfo->status_urb);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100836 uas_free_streams(devinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200837 kfree(devinfo);
838}
839
840/*
841 * XXX: Should this plug into libusual so we can auto-upgrade devices from
842 * Bulk-Only to UAS?
843 */
844static struct usb_driver uas_driver = {
845 .name = "uas",
846 .probe = uas_probe,
847 .disconnect = uas_disconnect,
848 .pre_reset = uas_pre_reset,
849 .post_reset = uas_post_reset,
850 .id_table = uas_usb_ids,
851};
852
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800853module_usb_driver(uas_driver);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200854
855MODULE_LICENSE("GPL");
856MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");