blob: b589b2e0e928984cedc451b9718fd36bb7e61730 [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>
Sebastian Andrzej Siewior348748b2012-01-11 12:45:56 +010018#include <linux/usb/uas.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020019
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
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020027/*
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 */
31struct 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
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020041struct uas_dev_info {
42 struct usb_interface *intf;
43 struct usb_device *udev;
44 int qdepth;
45 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
46 unsigned use_streams:1;
47 unsigned uas_sense_old:1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +010048 struct scsi_cmnd *cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020049};
50
51enum {
Matthew Wilcox92a3f762010-12-15 15:44:04 -050052 SUBMIT_STATUS_URB = (1 << 1),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020053 ALLOC_DATA_IN_URB = (1 << 2),
54 SUBMIT_DATA_IN_URB = (1 << 3),
55 ALLOC_DATA_OUT_URB = (1 << 4),
56 SUBMIT_DATA_OUT_URB = (1 << 5),
57 ALLOC_CMD_URB = (1 << 6),
58 SUBMIT_CMD_URB = (1 << 7),
Gerd Hoffmannb1d67692012-06-19 09:54:51 +020059 COMMAND_INFLIGHT = (1 << 8),
60 DATA_IN_URB_INFLIGHT = (1 << 9),
61 DATA_OUT_URB_INFLIGHT = (1 << 10),
62 COMMAND_COMPLETED = (1 << 11),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020063};
64
65/* Overrides scsi_pointer */
66struct uas_cmd_info {
67 unsigned int state;
68 unsigned int stream;
69 struct urb *cmd_urb;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020070 struct urb *data_in_urb;
71 struct urb *data_out_urb;
72 struct list_head list;
73};
74
75/* I hate forward declarations, but I actually have a loop */
76static int uas_submit_urbs(struct scsi_cmnd *cmnd,
77 struct uas_dev_info *devinfo, gfp_t gfp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -080078static void uas_do_work(struct work_struct *work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020079
Sarah Sharpea9da1c2011-12-02 11:55:44 -080080static DECLARE_WORK(uas_work, uas_do_work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020081static DEFINE_SPINLOCK(uas_work_lock);
82static LIST_HEAD(uas_work_list);
83
84static void uas_do_work(struct work_struct *work)
85{
86 struct uas_cmd_info *cmdinfo;
Sarah Sharpea9da1c2011-12-02 11:55:44 -080087 struct uas_cmd_info *temp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020088 struct list_head list;
Sarah Sharpea9da1c2011-12-02 11:55:44 -080089 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020090
91 spin_lock_irq(&uas_work_lock);
92 list_replace_init(&uas_work_list, &list);
93 spin_unlock_irq(&uas_work_lock);
94
Sarah Sharpea9da1c2011-12-02 11:55:44 -080095 list_for_each_entry_safe(cmdinfo, temp, &list, list) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020096 struct scsi_pointer *scp = (void *)cmdinfo;
97 struct scsi_cmnd *cmnd = container_of(scp,
98 struct scsi_cmnd, SCp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -080099 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
100 if (err) {
101 list_del(&cmdinfo->list);
102 spin_lock_irq(&uas_work_lock);
103 list_add_tail(&cmdinfo->list, &uas_work_list);
104 spin_unlock_irq(&uas_work_lock);
105 schedule_work(&uas_work);
106 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200107 }
108}
109
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200110static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
111{
112 struct sense_iu *sense_iu = urb->transfer_buffer;
113 struct scsi_device *sdev = cmnd->device;
114
115 if (urb->actual_length > 16) {
116 unsigned len = be16_to_cpup(&sense_iu->len);
117 if (len + 16 != urb->actual_length) {
118 int newlen = min(len + 16, urb->actual_length) - 16;
119 if (newlen < 0)
120 newlen = 0;
121 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
122 "disagrees with IU sense data length %d, "
123 "using %d bytes of sense data\n", __func__,
124 urb->actual_length, len, newlen);
125 len = newlen;
126 }
127 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
128 }
129
130 cmnd->result = sense_iu->status;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200131}
132
133static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
134{
135 struct sense_iu_old *sense_iu = urb->transfer_buffer;
136 struct scsi_device *sdev = cmnd->device;
137
138 if (urb->actual_length > 8) {
139 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
140 if (len + 8 != urb->actual_length) {
141 int newlen = min(len + 8, urb->actual_length) - 8;
142 if (newlen < 0)
143 newlen = 0;
144 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
145 "disagrees with IU sense data length %d, "
146 "using %d bytes of sense data\n", __func__,
147 urb->actual_length, len, newlen);
148 len = newlen;
149 }
150 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
151 }
152
153 cmnd->result = sense_iu->status;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200154}
155
156static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
157{
158 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
159
160 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
161 "%s%s%s%s%s%s%s%s%s%s%s\n",
162 caller, cmnd, cmnd->request->tag,
163 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
164 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
165 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
166 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
167 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
168 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
169 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
170 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
171 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
172 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
173 (ci->state & COMMAND_COMPLETED) ? " done" : "");
174}
175
176static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
177{
178 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
179
180 if (cmdinfo->state & (COMMAND_INFLIGHT |
181 DATA_IN_URB_INFLIGHT |
182 DATA_OUT_URB_INFLIGHT))
183 return -EBUSY;
184 BUG_ON(cmdinfo->state & COMMAND_COMPLETED);
185 cmdinfo->state |= COMMAND_COMPLETED;
186 usb_free_urb(cmdinfo->data_in_urb);
187 usb_free_urb(cmdinfo->data_out_urb);
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200188 cmnd->scsi_done(cmnd);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200189 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200190}
191
192static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200193 unsigned direction)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200194{
195 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
196 int err;
197
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200198 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200199 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
200 if (err) {
201 spin_lock(&uas_work_lock);
202 list_add_tail(&cmdinfo->list, &uas_work_list);
203 spin_unlock(&uas_work_lock);
204 schedule_work(&uas_work);
205 }
206}
207
208static void uas_stat_cmplt(struct urb *urb)
209{
210 struct iu *iu = urb->transfer_buffer;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100211 struct Scsi_Host *shost = urb->context;
212 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200213 struct scsi_cmnd *cmnd;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200214 struct uas_cmd_info *cmdinfo;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200215 u16 tag;
216
217 if (urb->status) {
218 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200219 usb_free_urb(urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200220 return;
221 }
222
223 tag = be16_to_cpup(&iu->tag) - 1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100224 if (tag == 0)
225 cmnd = devinfo->cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200226 else
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100227 cmnd = scsi_host_find_tag(shost, tag - 1);
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800228 if (!cmnd) {
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200229 usb_free_urb(urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200230 return;
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800231 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200232 cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200233
234 switch (iu->iu_id) {
235 case IU_ID_STATUS:
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100236 if (devinfo->cmnd == cmnd)
237 devinfo->cmnd = NULL;
238
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200239 if (urb->actual_length < 16)
240 devinfo->uas_sense_old = 1;
241 if (devinfo->uas_sense_old)
242 uas_sense_old(urb, cmnd);
243 else
244 uas_sense(urb, cmnd);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200245 cmdinfo->state &= ~COMMAND_INFLIGHT;
246 uas_try_complete(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200247 break;
248 case IU_ID_READ_READY:
249 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
250 break;
251 case IU_ID_WRITE_READY:
252 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
253 break;
254 default:
255 scmd_printk(KERN_ERR, cmnd,
256 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
257 }
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200258 usb_free_urb(urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200259}
260
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200261static void uas_data_cmplt(struct urb *urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200262{
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200263 struct scsi_cmnd *cmnd = urb->context;
264 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
265 struct scsi_data_buffer *sdb = NULL;
266
267 if (cmdinfo->data_in_urb == urb) {
268 sdb = scsi_in(cmnd);
269 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
270 } else if (cmdinfo->data_out_urb == urb) {
271 sdb = scsi_out(cmnd);
272 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
273 }
274 BUG_ON(sdb == NULL);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200275 sdb->resid = sdb->length - urb->actual_length;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200276 uas_try_complete(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200277}
278
279static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200280 unsigned int pipe, u16 stream_id,
281 struct scsi_cmnd *cmnd,
282 enum dma_data_direction dir)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200283{
284 struct usb_device *udev = devinfo->udev;
285 struct urb *urb = usb_alloc_urb(0, gfp);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200286 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
287 ? scsi_in(cmnd) : scsi_out(cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200288
289 if (!urb)
290 goto out;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200291 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
292 uas_data_cmplt, cmnd);
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200293 if (devinfo->use_streams)
294 urb->stream_id = stream_id;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200295 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
296 urb->sg = sdb->table.sgl;
297 out:
298 return urb;
299}
300
301static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200302 struct Scsi_Host *shost, u16 stream_id)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200303{
304 struct usb_device *udev = devinfo->udev;
305 struct urb *urb = usb_alloc_urb(0, gfp);
306 struct sense_iu *iu;
307
308 if (!urb)
309 goto out;
310
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500311 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200312 if (!iu)
313 goto free;
314
315 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200316 uas_stat_cmplt, shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200317 urb->stream_id = stream_id;
318 urb->transfer_flags |= URB_FREE_BUFFER;
319 out:
320 return urb;
321 free:
322 usb_free_urb(urb);
323 return NULL;
324}
325
326static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
327 struct scsi_cmnd *cmnd, u16 stream_id)
328{
329 struct usb_device *udev = devinfo->udev;
330 struct scsi_device *sdev = cmnd->device;
331 struct urb *urb = usb_alloc_urb(0, gfp);
332 struct command_iu *iu;
333 int len;
334
335 if (!urb)
336 goto out;
337
338 len = cmnd->cmd_len - 16;
339 if (len < 0)
340 len = 0;
341 len = ALIGN(len, 4);
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500342 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200343 if (!iu)
344 goto free;
345
346 iu->iu_id = IU_ID_COMMAND;
Sarah Sharp9eb44542011-12-02 11:55:46 -0800347 if (blk_rq_tagged(cmnd->request))
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100348 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
Sarah Sharp9eb44542011-12-02 11:55:46 -0800349 else
350 iu->tag = cpu_to_be16(1);
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100351 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200352 iu->len = len;
353 int_to_scsilun(sdev->lun, &iu->lun);
354 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
355
356 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
357 usb_free_urb, NULL);
358 urb->transfer_flags |= URB_FREE_BUFFER;
359 out:
360 return urb;
361 free:
362 usb_free_urb(urb);
363 return NULL;
364}
365
366/*
367 * Why should I request the Status IU before sending the Command IU? Spec
368 * says to, but also says the device may receive them in any order. Seems
369 * daft to me.
370 */
371
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200372static int uas_submit_sense_urb(struct Scsi_Host *shost,
373 gfp_t gfp, unsigned int stream)
374{
375 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
376 struct urb *urb;
377
378 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
379 if (!urb)
380 return SCSI_MLQUEUE_DEVICE_BUSY;
381 if (usb_submit_urb(urb, gfp)) {
382 shost_printk(KERN_INFO, shost,
383 "sense urb submission failure\n");
384 usb_free_urb(urb);
385 return SCSI_MLQUEUE_DEVICE_BUSY;
386 }
387 return 0;
388}
389
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200390static int uas_submit_urbs(struct scsi_cmnd *cmnd,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200391 struct uas_dev_info *devinfo, gfp_t gfp)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200392{
393 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200394 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200395
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500396 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200397 err = uas_submit_sense_urb(cmnd->device->host, gfp,
398 cmdinfo->stream);
399 if (err) {
400 return err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200401 }
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500402 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200403 }
404
405 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
406 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200407 devinfo->data_in_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200408 cmnd, DMA_FROM_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200409 if (!cmdinfo->data_in_urb)
410 return SCSI_MLQUEUE_DEVICE_BUSY;
411 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
412 }
413
414 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
415 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
416 scmd_printk(KERN_INFO, cmnd,
417 "data in urb submission failure\n");
418 return SCSI_MLQUEUE_DEVICE_BUSY;
419 }
420 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200421 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200422 }
423
424 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
425 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200426 devinfo->data_out_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200427 cmnd, DMA_TO_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200428 if (!cmdinfo->data_out_urb)
429 return SCSI_MLQUEUE_DEVICE_BUSY;
430 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
431 }
432
433 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
434 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
435 scmd_printk(KERN_INFO, cmnd,
436 "data out urb submission failure\n");
437 return SCSI_MLQUEUE_DEVICE_BUSY;
438 }
439 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200440 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200441 }
442
443 if (cmdinfo->state & ALLOC_CMD_URB) {
444 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
445 cmdinfo->stream);
446 if (!cmdinfo->cmd_urb)
447 return SCSI_MLQUEUE_DEVICE_BUSY;
448 cmdinfo->state &= ~ALLOC_CMD_URB;
449 }
450
451 if (cmdinfo->state & SUBMIT_CMD_URB) {
452 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
453 scmd_printk(KERN_INFO, cmnd,
454 "cmd urb submission failure\n");
455 return SCSI_MLQUEUE_DEVICE_BUSY;
456 }
457 cmdinfo->state &= ~SUBMIT_CMD_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200458 cmdinfo->state |= COMMAND_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200459 }
460
461 return 0;
462}
463
Jeff Garzikf2812332010-11-16 02:10:29 -0500464static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200465 void (*done)(struct scsi_cmnd *))
466{
467 struct scsi_device *sdev = cmnd->device;
468 struct uas_dev_info *devinfo = sdev->hostdata;
469 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
470 int err;
471
472 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
473
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100474 if (devinfo->cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200475 return SCSI_MLQUEUE_DEVICE_BUSY;
476
477 if (blk_rq_tagged(cmnd->request)) {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100478 cmdinfo->stream = cmnd->request->tag + 2;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200479 } else {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100480 devinfo->cmnd = cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200481 cmdinfo->stream = 1;
482 }
483
484 cmnd->scsi_done = done;
485
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200486 cmdinfo->state = SUBMIT_STATUS_URB |
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200487 ALLOC_CMD_URB | SUBMIT_CMD_URB;
488
489 switch (cmnd->sc_data_direction) {
490 case DMA_FROM_DEVICE:
491 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
492 break;
493 case DMA_BIDIRECTIONAL:
494 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
495 case DMA_TO_DEVICE:
496 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
497 case DMA_NONE:
498 break;
499 }
500
501 if (!devinfo->use_streams) {
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200502 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200503 cmdinfo->stream = 0;
504 }
505
506 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
507 if (err) {
508 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500509 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200510 return SCSI_MLQUEUE_DEVICE_BUSY;
511 }
512 spin_lock(&uas_work_lock);
513 list_add_tail(&cmdinfo->list, &uas_work_list);
514 spin_unlock(&uas_work_lock);
515 schedule_work(&uas_work);
516 }
517
518 return 0;
519}
520
Jeff Garzikf2812332010-11-16 02:10:29 -0500521static DEF_SCSI_QCMD(uas_queuecommand)
522
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200523static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
524{
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200525 uas_log_cmd_state(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200526
527/* XXX: Send ABORT TASK Task Management command */
528 return FAILED;
529}
530
531static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
532{
533 struct scsi_device *sdev = cmnd->device;
534 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
535 cmnd->request->tag);
536
537/* XXX: Send LOGICAL UNIT RESET Task Management command */
538 return FAILED;
539}
540
541static int uas_eh_target_reset_handler(struct scsi_cmnd *cmnd)
542{
543 struct scsi_device *sdev = cmnd->device;
544 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
545 cmnd->request->tag);
546
547/* XXX: Can we reset just the one USB interface?
548 * Would calling usb_set_interface() have the right effect?
549 */
550 return FAILED;
551}
552
553static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
554{
555 struct scsi_device *sdev = cmnd->device;
556 struct uas_dev_info *devinfo = sdev->hostdata;
557 struct usb_device *udev = devinfo->udev;
558
559 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
560 cmnd->request->tag);
561
562 if (usb_reset_device(udev))
563 return SUCCESS;
564
565 return FAILED;
566}
567
568static int uas_slave_alloc(struct scsi_device *sdev)
569{
570 sdev->hostdata = (void *)sdev->host->hostdata[0];
571 return 0;
572}
573
574static int uas_slave_configure(struct scsi_device *sdev)
575{
576 struct uas_dev_info *devinfo = sdev->hostdata;
577 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100578 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200579 return 0;
580}
581
582static struct scsi_host_template uas_host_template = {
583 .module = THIS_MODULE,
584 .name = "uas",
585 .queuecommand = uas_queuecommand,
586 .slave_alloc = uas_slave_alloc,
587 .slave_configure = uas_slave_configure,
588 .eh_abort_handler = uas_eh_abort_handler,
589 .eh_device_reset_handler = uas_eh_device_reset_handler,
590 .eh_target_reset_handler = uas_eh_target_reset_handler,
591 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
592 .can_queue = 65536, /* Is there a limit on the _host_ ? */
593 .this_id = -1,
594 .sg_tablesize = SG_NONE,
595 .cmd_per_lun = 1, /* until we override it */
596 .skip_settle_delay = 1,
597 .ordered_tag = 1,
598};
599
600static struct usb_device_id uas_usb_ids[] = {
601 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
602 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
603 /* 0xaa is a prototype device I happen to have access to */
604 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
605 { }
606};
607MODULE_DEVICE_TABLE(usb, uas_usb_ids);
608
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500609static int uas_is_interface(struct usb_host_interface *intf)
610{
611 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
612 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
613 intf->desc.bInterfaceProtocol == USB_PR_UAS);
614}
615
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100616static int uas_isnt_supported(struct usb_device *udev)
617{
618 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
619
620 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
621 "support scatter-gather which is\n",
622 hcd->driver->description);
623 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
624 "alternative USB controller if you wish to use UAS.\n");
625 return -ENODEV;
626}
627
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500628static int uas_switch_interface(struct usb_device *udev,
629 struct usb_interface *intf)
630{
631 int i;
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100632 int sg_supported = udev->bus->sg_tablesize != 0;
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500633
634 for (i = 0; i < intf->num_altsetting; i++) {
635 struct usb_host_interface *alt = &intf->altsetting[i];
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100636
637 if (uas_is_interface(alt)) {
638 if (!sg_supported)
639 return uas_isnt_supported(udev);
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500640 return usb_set_interface(udev,
641 alt->desc.bInterfaceNumber,
642 alt->desc.bAlternateSetting);
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100643 }
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500644 }
645
646 return -ENODEV;
647}
648
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200649static void uas_configure_endpoints(struct uas_dev_info *devinfo)
650{
651 struct usb_host_endpoint *eps[4] = { };
652 struct usb_interface *intf = devinfo->intf;
653 struct usb_device *udev = devinfo->udev;
654 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
655 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
656
657 devinfo->uas_sense_old = 0;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100658 devinfo->cmnd = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200659
660 for (i = 0; i < n_endpoints; i++) {
661 unsigned char *extra = endpoint[i].extra;
662 int len = endpoint[i].extralen;
663 while (len > 1) {
664 if (extra[1] == USB_DT_PIPE_USAGE) {
665 unsigned pipe_id = extra[2];
666 if (pipe_id > 0 && pipe_id < 5)
667 eps[pipe_id - 1] = &endpoint[i];
668 break;
669 }
670 len -= extra[0];
671 extra += extra[0];
672 }
673 }
674
675 /*
676 * Assume that if we didn't find a control pipe descriptor, we're
677 * using a device with old firmware that happens to be set up like
678 * this.
679 */
680 if (!eps[0]) {
681 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
682 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
683 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
684 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
685
686 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
687 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
688 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
689 } else {
690 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
691 eps[0]->desc.bEndpointAddress);
692 devinfo->status_pipe = usb_rcvbulkpipe(udev,
693 eps[1]->desc.bEndpointAddress);
694 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
695 eps[2]->desc.bEndpointAddress);
696 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
697 eps[3]->desc.bEndpointAddress);
698 }
699
700 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
701 GFP_KERNEL);
702 if (devinfo->qdepth < 0) {
703 devinfo->qdepth = 256;
704 devinfo->use_streams = 0;
705 } else {
706 devinfo->use_streams = 1;
707 }
708}
709
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100710static void uas_free_streams(struct uas_dev_info *devinfo)
711{
712 struct usb_device *udev = devinfo->udev;
713 struct usb_host_endpoint *eps[3];
714
715 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
716 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
717 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
718 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
719}
720
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200721/*
722 * XXX: What I'd like to do here is register a SCSI host for each USB host in
723 * the system. Follow usb-storage's design of registering a SCSI host for
724 * each USB device for the moment. Can implement this by walking up the
725 * USB hierarchy until we find a USB host.
726 */
727static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
728{
729 int result;
730 struct Scsi_Host *shost;
731 struct uas_dev_info *devinfo;
732 struct usb_device *udev = interface_to_usbdev(intf);
733
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500734 if (uas_switch_interface(udev, intf))
735 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200736
737 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
738 if (!devinfo)
739 return -ENOMEM;
740
741 result = -ENOMEM;
742 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
743 if (!shost)
744 goto free;
745
746 shost->max_cmd_len = 16 + 252;
747 shost->max_id = 1;
748 shost->sg_tablesize = udev->bus->sg_tablesize;
749
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200750 devinfo->intf = intf;
751 devinfo->udev = udev;
752 uas_configure_endpoints(devinfo);
753
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100754 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100755 if (result)
756 goto free;
757
758 result = scsi_add_host(shost, &intf->dev);
759 if (result)
760 goto deconfig_eps;
761
762 shost->hostdata[0] = (unsigned long)devinfo;
763
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200764 scsi_scan_host(shost);
765 usb_set_intfdata(intf, shost);
766 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100767
768deconfig_eps:
769 uas_free_streams(devinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200770 free:
771 kfree(devinfo);
772 if (shost)
773 scsi_host_put(shost);
774 return result;
775}
776
777static int uas_pre_reset(struct usb_interface *intf)
778{
779/* XXX: Need to return 1 if it's not our device in error handling */
780 return 0;
781}
782
783static int uas_post_reset(struct usb_interface *intf)
784{
785/* XXX: Need to return 1 if it's not our device in error handling */
786 return 0;
787}
788
789static void uas_disconnect(struct usb_interface *intf)
790{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200791 struct Scsi_Host *shost = usb_get_intfdata(intf);
792 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
793
794 scsi_remove_host(shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100795 uas_free_streams(devinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200796 kfree(devinfo);
797}
798
799/*
800 * XXX: Should this plug into libusual so we can auto-upgrade devices from
801 * Bulk-Only to UAS?
802 */
803static struct usb_driver uas_driver = {
804 .name = "uas",
805 .probe = uas_probe,
806 .disconnect = uas_disconnect,
807 .pre_reset = uas_pre_reset,
808 .post_reset = uas_post_reset,
809 .id_table = uas_usb_ids,
810};
811
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800812module_usb_driver(uas_driver);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200813
814MODULE_LICENSE("GPL");
815MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");