blob: 036e969009561cbd8c15f55c1e175ad9f16c3925 [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;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100101 struct scsi_cmnd *cmnd;
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100102 struct urb *status_urb; /* used only if stream support is available */
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200103};
104
105enum {
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500106 ALLOC_STATUS_URB = (1 << 0),
107 SUBMIT_STATUS_URB = (1 << 1),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200108 ALLOC_DATA_IN_URB = (1 << 2),
109 SUBMIT_DATA_IN_URB = (1 << 3),
110 ALLOC_DATA_OUT_URB = (1 << 4),
111 SUBMIT_DATA_OUT_URB = (1 << 5),
112 ALLOC_CMD_URB = (1 << 6),
113 SUBMIT_CMD_URB = (1 << 7),
114};
115
116/* Overrides scsi_pointer */
117struct uas_cmd_info {
118 unsigned int state;
119 unsigned int stream;
120 struct urb *cmd_urb;
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100121 /* status_urb is used only if stream support isn't available */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500122 struct urb *status_urb;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200123 struct urb *data_in_urb;
124 struct urb *data_out_urb;
125 struct list_head list;
126};
127
128/* I hate forward declarations, but I actually have a loop */
129static int uas_submit_urbs(struct scsi_cmnd *cmnd,
130 struct uas_dev_info *devinfo, gfp_t gfp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800131static void uas_do_work(struct work_struct *work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200132
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800133static DECLARE_WORK(uas_work, uas_do_work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200134static DEFINE_SPINLOCK(uas_work_lock);
135static LIST_HEAD(uas_work_list);
136
137static void uas_do_work(struct work_struct *work)
138{
139 struct uas_cmd_info *cmdinfo;
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800140 struct uas_cmd_info *temp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200141 struct list_head list;
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800142 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200143
144 spin_lock_irq(&uas_work_lock);
145 list_replace_init(&uas_work_list, &list);
146 spin_unlock_irq(&uas_work_lock);
147
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800148 list_for_each_entry_safe(cmdinfo, temp, &list, list) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200149 struct scsi_pointer *scp = (void *)cmdinfo;
150 struct scsi_cmnd *cmnd = container_of(scp,
151 struct scsi_cmnd, SCp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800152 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
153 if (err) {
154 list_del(&cmdinfo->list);
155 spin_lock_irq(&uas_work_lock);
156 list_add_tail(&cmdinfo->list, &uas_work_list);
157 spin_unlock_irq(&uas_work_lock);
158 schedule_work(&uas_work);
159 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200160 }
161}
162
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200163static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
164{
165 struct sense_iu *sense_iu = urb->transfer_buffer;
166 struct scsi_device *sdev = cmnd->device;
167
168 if (urb->actual_length > 16) {
169 unsigned len = be16_to_cpup(&sense_iu->len);
170 if (len + 16 != urb->actual_length) {
171 int newlen = min(len + 16, urb->actual_length) - 16;
172 if (newlen < 0)
173 newlen = 0;
174 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
175 "disagrees with IU sense data length %d, "
176 "using %d bytes of sense data\n", __func__,
177 urb->actual_length, len, newlen);
178 len = newlen;
179 }
180 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
181 }
182
183 cmnd->result = sense_iu->status;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200184 cmnd->scsi_done(cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200185}
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;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200208 cmnd->scsi_done(cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200209}
210
211static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
212 unsigned direction)
213{
214 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
215 int err;
216
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100217 cmdinfo->state = direction;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200218 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
219 if (err) {
220 spin_lock(&uas_work_lock);
221 list_add_tail(&cmdinfo->list, &uas_work_list);
222 spin_unlock(&uas_work_lock);
223 schedule_work(&uas_work);
224 }
225}
226
227static void uas_stat_cmplt(struct urb *urb)
228{
229 struct iu *iu = urb->transfer_buffer;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100230 struct Scsi_Host *shost = urb->context;
231 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200232 struct scsi_cmnd *cmnd;
233 u16 tag;
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100234 int ret;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200235
236 if (urb->status) {
237 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100238 if (devinfo->use_streams)
239 usb_free_urb(urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200240 return;
241 }
242
243 tag = be16_to_cpup(&iu->tag) - 1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100244 if (tag == 0)
245 cmnd = devinfo->cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200246 else
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100247 cmnd = scsi_host_find_tag(shost, tag - 1);
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800248 if (!cmnd) {
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100249 if (devinfo->use_streams) {
250 usb_free_urb(urb);
251 return;
252 }
253 ret = usb_submit_urb(urb, GFP_ATOMIC);
254 if (ret)
255 dev_err(&urb->dev->dev, "failed submit status urb\n");
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200256 return;
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800257 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200258
259 switch (iu->iu_id) {
260 case IU_ID_STATUS:
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100261 if (devinfo->cmnd == cmnd)
262 devinfo->cmnd = NULL;
263
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200264 if (urb->actual_length < 16)
265 devinfo->uas_sense_old = 1;
266 if (devinfo->uas_sense_old)
267 uas_sense_old(urb, cmnd);
268 else
269 uas_sense(urb, cmnd);
270 break;
271 case IU_ID_READ_READY:
272 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
273 break;
274 case IU_ID_WRITE_READY:
275 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
276 break;
277 default:
278 scmd_printk(KERN_ERR, cmnd,
279 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
280 }
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100281
282 if (devinfo->use_streams) {
283 usb_free_urb(urb);
284 return;
285 }
286
287 ret = usb_submit_urb(urb, GFP_ATOMIC);
288 if (ret)
289 dev_err(&urb->dev->dev, "failed submit status urb\n");
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200290}
291
292static void uas_data_cmplt(struct urb *urb)
293{
294 struct scsi_data_buffer *sdb = urb->context;
295 sdb->resid = sdb->length - urb->actual_length;
296 usb_free_urb(urb);
297}
298
299static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
300 unsigned int pipe, u16 stream_id,
301 struct scsi_data_buffer *sdb,
302 enum dma_data_direction dir)
303{
304 struct usb_device *udev = devinfo->udev;
305 struct urb *urb = usb_alloc_urb(0, gfp);
306
307 if (!urb)
308 goto out;
309 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, uas_data_cmplt,
310 sdb);
311 if (devinfo->use_streams)
312 urb->stream_id = stream_id;
313 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
314 urb->sg = sdb->table.sgl;
315 out:
316 return urb;
317}
318
319static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100320 struct Scsi_Host *shost, u16 stream_id)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200321{
322 struct usb_device *udev = devinfo->udev;
323 struct urb *urb = usb_alloc_urb(0, gfp);
324 struct sense_iu *iu;
325
326 if (!urb)
327 goto out;
328
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500329 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200330 if (!iu)
331 goto free;
332
333 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100334 uas_stat_cmplt, shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200335 urb->stream_id = stream_id;
336 urb->transfer_flags |= URB_FREE_BUFFER;
337 out:
338 return urb;
339 free:
340 usb_free_urb(urb);
341 return NULL;
342}
343
344static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
345 struct scsi_cmnd *cmnd, u16 stream_id)
346{
347 struct usb_device *udev = devinfo->udev;
348 struct scsi_device *sdev = cmnd->device;
349 struct urb *urb = usb_alloc_urb(0, gfp);
350 struct command_iu *iu;
351 int len;
352
353 if (!urb)
354 goto out;
355
356 len = cmnd->cmd_len - 16;
357 if (len < 0)
358 len = 0;
359 len = ALIGN(len, 4);
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500360 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200361 if (!iu)
362 goto free;
363
364 iu->iu_id = IU_ID_COMMAND;
Sarah Sharp9eb44542011-12-02 11:55:46 -0800365 if (blk_rq_tagged(cmnd->request))
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100366 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
Sarah Sharp9eb44542011-12-02 11:55:46 -0800367 else
368 iu->tag = cpu_to_be16(1);
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100369 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200370 iu->len = len;
371 int_to_scsilun(sdev->lun, &iu->lun);
372 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
373
374 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
375 usb_free_urb, NULL);
376 urb->transfer_flags |= URB_FREE_BUFFER;
377 out:
378 return urb;
379 free:
380 usb_free_urb(urb);
381 return NULL;
382}
383
384/*
385 * Why should I request the Status IU before sending the Command IU? Spec
386 * says to, but also says the device may receive them in any order. Seems
387 * daft to me.
388 */
389
390static int uas_submit_urbs(struct scsi_cmnd *cmnd,
391 struct uas_dev_info *devinfo, gfp_t gfp)
392{
393 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
394
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500395 if (cmdinfo->state & ALLOC_STATUS_URB) {
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100396 cmdinfo->status_urb = uas_alloc_sense_urb(devinfo, gfp,
397 cmnd->device->host, cmdinfo->stream);
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500398 if (!cmdinfo->status_urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200399 return SCSI_MLQUEUE_DEVICE_BUSY;
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500400 cmdinfo->state &= ~ALLOC_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200401 }
402
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500403 if (cmdinfo->state & SUBMIT_STATUS_URB) {
404 if (usb_submit_urb(cmdinfo->status_urb, gfp)) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200405 scmd_printk(KERN_INFO, cmnd,
406 "sense urb submission failure\n");
407 return SCSI_MLQUEUE_DEVICE_BUSY;
408 }
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500409 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200410 }
411
412 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
413 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
414 devinfo->data_in_pipe, cmdinfo->stream,
415 scsi_in(cmnd), DMA_FROM_DEVICE);
416 if (!cmdinfo->data_in_urb)
417 return SCSI_MLQUEUE_DEVICE_BUSY;
418 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
419 }
420
421 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
422 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
423 scmd_printk(KERN_INFO, cmnd,
424 "data in urb submission failure\n");
425 return SCSI_MLQUEUE_DEVICE_BUSY;
426 }
427 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
428 }
429
430 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
431 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
432 devinfo->data_out_pipe, cmdinfo->stream,
433 scsi_out(cmnd), DMA_TO_DEVICE);
434 if (!cmdinfo->data_out_urb)
435 return SCSI_MLQUEUE_DEVICE_BUSY;
436 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
437 }
438
439 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
440 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
441 scmd_printk(KERN_INFO, cmnd,
442 "data out urb submission failure\n");
443 return SCSI_MLQUEUE_DEVICE_BUSY;
444 }
445 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
446 }
447
448 if (cmdinfo->state & ALLOC_CMD_URB) {
449 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
450 cmdinfo->stream);
451 if (!cmdinfo->cmd_urb)
452 return SCSI_MLQUEUE_DEVICE_BUSY;
453 cmdinfo->state &= ~ALLOC_CMD_URB;
454 }
455
456 if (cmdinfo->state & SUBMIT_CMD_URB) {
457 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
458 scmd_printk(KERN_INFO, cmnd,
459 "cmd urb submission failure\n");
460 return SCSI_MLQUEUE_DEVICE_BUSY;
461 }
462 cmdinfo->state &= ~SUBMIT_CMD_URB;
463 }
464
465 return 0;
466}
467
Jeff Garzikf2812332010-11-16 02:10:29 -0500468static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200469 void (*done)(struct scsi_cmnd *))
470{
471 struct scsi_device *sdev = cmnd->device;
472 struct uas_dev_info *devinfo = sdev->hostdata;
473 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
474 int err;
475
476 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
477
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100478 if (devinfo->cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200479 return SCSI_MLQUEUE_DEVICE_BUSY;
480
481 if (blk_rq_tagged(cmnd->request)) {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100482 cmdinfo->stream = cmnd->request->tag + 2;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200483 } else {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100484 devinfo->cmnd = cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200485 cmdinfo->stream = 1;
486 }
487
488 cmnd->scsi_done = done;
489
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500490 cmdinfo->state = ALLOC_STATUS_URB | SUBMIT_STATUS_URB |
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200491 ALLOC_CMD_URB | SUBMIT_CMD_URB;
492
493 switch (cmnd->sc_data_direction) {
494 case DMA_FROM_DEVICE:
495 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
496 break;
497 case DMA_BIDIRECTIONAL:
498 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
499 case DMA_TO_DEVICE:
500 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
501 case DMA_NONE:
502 break;
503 }
504
505 if (!devinfo->use_streams) {
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100506 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB |
507 ALLOC_STATUS_URB | SUBMIT_STATUS_URB);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200508 cmdinfo->stream = 0;
509 }
510
511 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
512 if (err) {
513 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500514 if (cmdinfo->state & SUBMIT_STATUS_URB) {
515 usb_free_urb(cmdinfo->status_urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200516 return SCSI_MLQUEUE_DEVICE_BUSY;
517 }
518 spin_lock(&uas_work_lock);
519 list_add_tail(&cmdinfo->list, &uas_work_list);
520 spin_unlock(&uas_work_lock);
521 schedule_work(&uas_work);
522 }
523
524 return 0;
525}
526
Jeff Garzikf2812332010-11-16 02:10:29 -0500527static DEF_SCSI_QCMD(uas_queuecommand)
528
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200529static int uas_eh_abort_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: Send ABORT TASK Task Management command */
536 return FAILED;
537}
538
539static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
540{
541 struct scsi_device *sdev = cmnd->device;
542 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
543 cmnd->request->tag);
544
545/* XXX: Send LOGICAL UNIT RESET Task Management command */
546 return FAILED;
547}
548
549static int uas_eh_target_reset_handler(struct scsi_cmnd *cmnd)
550{
551 struct scsi_device *sdev = cmnd->device;
552 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
553 cmnd->request->tag);
554
555/* XXX: Can we reset just the one USB interface?
556 * Would calling usb_set_interface() have the right effect?
557 */
558 return FAILED;
559}
560
561static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
562{
563 struct scsi_device *sdev = cmnd->device;
564 struct uas_dev_info *devinfo = sdev->hostdata;
565 struct usb_device *udev = devinfo->udev;
566
567 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
568 cmnd->request->tag);
569
570 if (usb_reset_device(udev))
571 return SUCCESS;
572
573 return FAILED;
574}
575
576static int uas_slave_alloc(struct scsi_device *sdev)
577{
578 sdev->hostdata = (void *)sdev->host->hostdata[0];
579 return 0;
580}
581
582static int uas_slave_configure(struct scsi_device *sdev)
583{
584 struct uas_dev_info *devinfo = sdev->hostdata;
585 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100586 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200587 return 0;
588}
589
590static struct scsi_host_template uas_host_template = {
591 .module = THIS_MODULE,
592 .name = "uas",
593 .queuecommand = uas_queuecommand,
594 .slave_alloc = uas_slave_alloc,
595 .slave_configure = uas_slave_configure,
596 .eh_abort_handler = uas_eh_abort_handler,
597 .eh_device_reset_handler = uas_eh_device_reset_handler,
598 .eh_target_reset_handler = uas_eh_target_reset_handler,
599 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
600 .can_queue = 65536, /* Is there a limit on the _host_ ? */
601 .this_id = -1,
602 .sg_tablesize = SG_NONE,
603 .cmd_per_lun = 1, /* until we override it */
604 .skip_settle_delay = 1,
605 .ordered_tag = 1,
606};
607
608static struct usb_device_id uas_usb_ids[] = {
609 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
610 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
611 /* 0xaa is a prototype device I happen to have access to */
612 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
613 { }
614};
615MODULE_DEVICE_TABLE(usb, uas_usb_ids);
616
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500617static int uas_is_interface(struct usb_host_interface *intf)
618{
619 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
620 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
621 intf->desc.bInterfaceProtocol == USB_PR_UAS);
622}
623
624static int uas_switch_interface(struct usb_device *udev,
625 struct usb_interface *intf)
626{
627 int i;
628
629 if (uas_is_interface(intf->cur_altsetting))
630 return 0;
631
632 for (i = 0; i < intf->num_altsetting; i++) {
633 struct usb_host_interface *alt = &intf->altsetting[i];
634 if (alt == intf->cur_altsetting)
635 continue;
636 if (uas_is_interface(alt))
637 return usb_set_interface(udev,
638 alt->desc.bInterfaceNumber,
639 alt->desc.bAlternateSetting);
640 }
641
642 return -ENODEV;
643}
644
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200645static void uas_configure_endpoints(struct uas_dev_info *devinfo)
646{
647 struct usb_host_endpoint *eps[4] = { };
648 struct usb_interface *intf = devinfo->intf;
649 struct usb_device *udev = devinfo->udev;
650 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
651 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
652
653 devinfo->uas_sense_old = 0;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100654 devinfo->cmnd = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200655
656 for (i = 0; i < n_endpoints; i++) {
657 unsigned char *extra = endpoint[i].extra;
658 int len = endpoint[i].extralen;
659 while (len > 1) {
660 if (extra[1] == USB_DT_PIPE_USAGE) {
661 unsigned pipe_id = extra[2];
662 if (pipe_id > 0 && pipe_id < 5)
663 eps[pipe_id - 1] = &endpoint[i];
664 break;
665 }
666 len -= extra[0];
667 extra += extra[0];
668 }
669 }
670
671 /*
672 * Assume that if we didn't find a control pipe descriptor, we're
673 * using a device with old firmware that happens to be set up like
674 * this.
675 */
676 if (!eps[0]) {
677 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
678 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
679 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
680 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
681
682 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
683 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
684 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
685 } else {
686 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
687 eps[0]->desc.bEndpointAddress);
688 devinfo->status_pipe = usb_rcvbulkpipe(udev,
689 eps[1]->desc.bEndpointAddress);
690 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
691 eps[2]->desc.bEndpointAddress);
692 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
693 eps[3]->desc.bEndpointAddress);
694 }
695
696 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
697 GFP_KERNEL);
698 if (devinfo->qdepth < 0) {
699 devinfo->qdepth = 256;
700 devinfo->use_streams = 0;
701 } else {
702 devinfo->use_streams = 1;
703 }
704}
705
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100706static int uas_alloc_status_urb(struct uas_dev_info *devinfo,
707 struct Scsi_Host *shost)
708{
709 if (devinfo->use_streams) {
710 devinfo->status_urb = NULL;
711 return 0;
712 }
713
714 devinfo->status_urb = uas_alloc_sense_urb(devinfo, GFP_KERNEL,
715 shost, 0);
716 if (!devinfo->status_urb)
717 goto err_s_urb;
718
719 if (usb_submit_urb(devinfo->status_urb, GFP_KERNEL))
720 goto err_submit_urb;
721
722 return 0;
723err_submit_urb:
724 usb_free_urb(devinfo->status_urb);
725err_s_urb:
726 return -ENOMEM;
727}
728
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100729static void uas_free_streams(struct uas_dev_info *devinfo)
730{
731 struct usb_device *udev = devinfo->udev;
732 struct usb_host_endpoint *eps[3];
733
734 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
735 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
736 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
737 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
738}
739
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200740/*
741 * XXX: What I'd like to do here is register a SCSI host for each USB host in
742 * the system. Follow usb-storage's design of registering a SCSI host for
743 * each USB device for the moment. Can implement this by walking up the
744 * USB hierarchy until we find a USB host.
745 */
746static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
747{
748 int result;
749 struct Scsi_Host *shost;
750 struct uas_dev_info *devinfo;
751 struct usb_device *udev = interface_to_usbdev(intf);
752
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500753 if (uas_switch_interface(udev, intf))
754 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200755
756 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
757 if (!devinfo)
758 return -ENOMEM;
759
760 result = -ENOMEM;
761 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
762 if (!shost)
763 goto free;
764
765 shost->max_cmd_len = 16 + 252;
766 shost->max_id = 1;
767 shost->sg_tablesize = udev->bus->sg_tablesize;
768
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200769 devinfo->intf = intf;
770 devinfo->udev = udev;
771 uas_configure_endpoints(devinfo);
772
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100773 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100774 if (result)
775 goto free;
776
777 result = scsi_add_host(shost, &intf->dev);
778 if (result)
779 goto deconfig_eps;
780
781 shost->hostdata[0] = (unsigned long)devinfo;
782
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100783 result = uas_alloc_status_urb(devinfo, shost);
784 if (result)
785 goto err_alloc_status;
786
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200787 scsi_scan_host(shost);
788 usb_set_intfdata(intf, shost);
789 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100790
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100791err_alloc_status:
792 scsi_remove_host(shost);
793 shost = NULL;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100794deconfig_eps:
795 uas_free_streams(devinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200796 free:
797 kfree(devinfo);
798 if (shost)
799 scsi_host_put(shost);
800 return result;
801}
802
803static int uas_pre_reset(struct usb_interface *intf)
804{
805/* XXX: Need to return 1 if it's not our device in error handling */
806 return 0;
807}
808
809static int uas_post_reset(struct usb_interface *intf)
810{
811/* XXX: Need to return 1 if it's not our device in error handling */
812 return 0;
813}
814
815static void uas_disconnect(struct usb_interface *intf)
816{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200817 struct Scsi_Host *shost = usb_get_intfdata(intf);
818 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
819
820 scsi_remove_host(shost);
Sebastian Andrzej Siewiorceb3f912011-12-20 14:50:26 +0100821 usb_kill_urb(devinfo->status_urb);
822 usb_free_urb(devinfo->status_urb);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100823 uas_free_streams(devinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200824 kfree(devinfo);
825}
826
827/*
828 * XXX: Should this plug into libusual so we can auto-upgrade devices from
829 * Bulk-Only to UAS?
830 */
831static struct usb_driver uas_driver = {
832 .name = "uas",
833 .probe = uas_probe,
834 .disconnect = uas_disconnect,
835 .pre_reset = uas_pre_reset,
836 .post_reset = uas_post_reset,
837 .id_table = uas_usb_ids,
838};
839
840static int uas_init(void)
841{
842 return usb_register(&uas_driver);
843}
844
845static void uas_exit(void)
846{
847 usb_deregister(&uas_driver);
848}
849
850module_init(uas_init);
851module_exit(uas_exit);
852
853MODULE_LICENSE("GPL");
854MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");