blob: d8b7bc6ea141525b6ad76872c383f2de957d4ea6 [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 Hoffmann8aac8632012-06-19 09:54:52 +0200245 if (cmnd->result != 0) {
246 /* cancel data transfers on error */
247 if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
248 usb_unlink_urb(cmdinfo->data_in_urb);
249 if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
250 usb_unlink_urb(cmdinfo->data_out_urb);
251 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200252 cmdinfo->state &= ~COMMAND_INFLIGHT;
253 uas_try_complete(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200254 break;
255 case IU_ID_READ_READY:
256 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
257 break;
258 case IU_ID_WRITE_READY:
259 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
260 break;
261 default:
262 scmd_printk(KERN_ERR, cmnd,
263 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
264 }
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200265 usb_free_urb(urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200266}
267
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200268static void uas_data_cmplt(struct urb *urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200269{
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200270 struct scsi_cmnd *cmnd = urb->context;
271 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
272 struct scsi_data_buffer *sdb = NULL;
273
274 if (cmdinfo->data_in_urb == urb) {
275 sdb = scsi_in(cmnd);
276 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
277 } else if (cmdinfo->data_out_urb == urb) {
278 sdb = scsi_out(cmnd);
279 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
280 }
281 BUG_ON(sdb == NULL);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200282 if (urb->status) {
283 /* error: no data transfered */
284 sdb->resid = sdb->length;
285 } else {
286 sdb->resid = sdb->length - urb->actual_length;
287 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200288 uas_try_complete(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200289}
290
291static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200292 unsigned int pipe, u16 stream_id,
293 struct scsi_cmnd *cmnd,
294 enum dma_data_direction dir)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200295{
296 struct usb_device *udev = devinfo->udev;
297 struct urb *urb = usb_alloc_urb(0, gfp);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200298 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
299 ? scsi_in(cmnd) : scsi_out(cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200300
301 if (!urb)
302 goto out;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200303 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
304 uas_data_cmplt, cmnd);
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200305 if (devinfo->use_streams)
306 urb->stream_id = stream_id;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200307 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
308 urb->sg = sdb->table.sgl;
309 out:
310 return urb;
311}
312
313static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200314 struct Scsi_Host *shost, u16 stream_id)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200315{
316 struct usb_device *udev = devinfo->udev;
317 struct urb *urb = usb_alloc_urb(0, gfp);
318 struct sense_iu *iu;
319
320 if (!urb)
321 goto out;
322
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500323 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200324 if (!iu)
325 goto free;
326
327 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200328 uas_stat_cmplt, shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200329 urb->stream_id = stream_id;
330 urb->transfer_flags |= URB_FREE_BUFFER;
331 out:
332 return urb;
333 free:
334 usb_free_urb(urb);
335 return NULL;
336}
337
338static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
339 struct scsi_cmnd *cmnd, u16 stream_id)
340{
341 struct usb_device *udev = devinfo->udev;
342 struct scsi_device *sdev = cmnd->device;
343 struct urb *urb = usb_alloc_urb(0, gfp);
344 struct command_iu *iu;
345 int len;
346
347 if (!urb)
348 goto out;
349
350 len = cmnd->cmd_len - 16;
351 if (len < 0)
352 len = 0;
353 len = ALIGN(len, 4);
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500354 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200355 if (!iu)
356 goto free;
357
358 iu->iu_id = IU_ID_COMMAND;
Sarah Sharp9eb44542011-12-02 11:55:46 -0800359 if (blk_rq_tagged(cmnd->request))
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100360 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
Sarah Sharp9eb44542011-12-02 11:55:46 -0800361 else
362 iu->tag = cpu_to_be16(1);
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100363 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200364 iu->len = len;
365 int_to_scsilun(sdev->lun, &iu->lun);
366 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
367
368 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
369 usb_free_urb, NULL);
370 urb->transfer_flags |= URB_FREE_BUFFER;
371 out:
372 return urb;
373 free:
374 usb_free_urb(urb);
375 return NULL;
376}
377
378/*
379 * Why should I request the Status IU before sending the Command IU? Spec
380 * says to, but also says the device may receive them in any order. Seems
381 * daft to me.
382 */
383
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200384static int uas_submit_sense_urb(struct Scsi_Host *shost,
385 gfp_t gfp, unsigned int stream)
386{
387 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
388 struct urb *urb;
389
390 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
391 if (!urb)
392 return SCSI_MLQUEUE_DEVICE_BUSY;
393 if (usb_submit_urb(urb, gfp)) {
394 shost_printk(KERN_INFO, shost,
395 "sense urb submission failure\n");
396 usb_free_urb(urb);
397 return SCSI_MLQUEUE_DEVICE_BUSY;
398 }
399 return 0;
400}
401
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200402static int uas_submit_urbs(struct scsi_cmnd *cmnd,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200403 struct uas_dev_info *devinfo, gfp_t gfp)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200404{
405 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200406 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200407
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500408 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200409 err = uas_submit_sense_urb(cmnd->device->host, gfp,
410 cmdinfo->stream);
411 if (err) {
412 return err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200413 }
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500414 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200415 }
416
417 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
418 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200419 devinfo->data_in_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200420 cmnd, DMA_FROM_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200421 if (!cmdinfo->data_in_urb)
422 return SCSI_MLQUEUE_DEVICE_BUSY;
423 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
424 }
425
426 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
427 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
428 scmd_printk(KERN_INFO, cmnd,
429 "data in urb submission failure\n");
430 return SCSI_MLQUEUE_DEVICE_BUSY;
431 }
432 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200433 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200434 }
435
436 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
437 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200438 devinfo->data_out_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200439 cmnd, DMA_TO_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200440 if (!cmdinfo->data_out_urb)
441 return SCSI_MLQUEUE_DEVICE_BUSY;
442 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
443 }
444
445 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
446 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
447 scmd_printk(KERN_INFO, cmnd,
448 "data out urb submission failure\n");
449 return SCSI_MLQUEUE_DEVICE_BUSY;
450 }
451 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200452 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200453 }
454
455 if (cmdinfo->state & ALLOC_CMD_URB) {
456 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
457 cmdinfo->stream);
458 if (!cmdinfo->cmd_urb)
459 return SCSI_MLQUEUE_DEVICE_BUSY;
460 cmdinfo->state &= ~ALLOC_CMD_URB;
461 }
462
463 if (cmdinfo->state & SUBMIT_CMD_URB) {
464 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
465 scmd_printk(KERN_INFO, cmnd,
466 "cmd urb submission failure\n");
467 return SCSI_MLQUEUE_DEVICE_BUSY;
468 }
469 cmdinfo->state &= ~SUBMIT_CMD_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200470 cmdinfo->state |= COMMAND_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200471 }
472
473 return 0;
474}
475
Jeff Garzikf2812332010-11-16 02:10:29 -0500476static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200477 void (*done)(struct scsi_cmnd *))
478{
479 struct scsi_device *sdev = cmnd->device;
480 struct uas_dev_info *devinfo = sdev->hostdata;
481 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
482 int err;
483
484 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
485
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100486 if (devinfo->cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200487 return SCSI_MLQUEUE_DEVICE_BUSY;
488
489 if (blk_rq_tagged(cmnd->request)) {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100490 cmdinfo->stream = cmnd->request->tag + 2;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200491 } else {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100492 devinfo->cmnd = cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200493 cmdinfo->stream = 1;
494 }
495
496 cmnd->scsi_done = done;
497
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200498 cmdinfo->state = SUBMIT_STATUS_URB |
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200499 ALLOC_CMD_URB | SUBMIT_CMD_URB;
500
501 switch (cmnd->sc_data_direction) {
502 case DMA_FROM_DEVICE:
503 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
504 break;
505 case DMA_BIDIRECTIONAL:
506 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
507 case DMA_TO_DEVICE:
508 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
509 case DMA_NONE:
510 break;
511 }
512
513 if (!devinfo->use_streams) {
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200514 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200515 cmdinfo->stream = 0;
516 }
517
518 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
519 if (err) {
520 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500521 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200522 return SCSI_MLQUEUE_DEVICE_BUSY;
523 }
524 spin_lock(&uas_work_lock);
525 list_add_tail(&cmdinfo->list, &uas_work_list);
526 spin_unlock(&uas_work_lock);
527 schedule_work(&uas_work);
528 }
529
530 return 0;
531}
532
Jeff Garzikf2812332010-11-16 02:10:29 -0500533static DEF_SCSI_QCMD(uas_queuecommand)
534
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200535static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
536{
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200537 uas_log_cmd_state(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200538
539/* XXX: Send ABORT TASK Task Management command */
540 return FAILED;
541}
542
543static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
544{
545 struct scsi_device *sdev = cmnd->device;
546 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
547 cmnd->request->tag);
548
549/* XXX: Send LOGICAL UNIT RESET Task Management command */
550 return FAILED;
551}
552
553static int uas_eh_target_reset_handler(struct scsi_cmnd *cmnd)
554{
555 struct scsi_device *sdev = cmnd->device;
556 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
557 cmnd->request->tag);
558
559/* XXX: Can we reset just the one USB interface?
560 * Would calling usb_set_interface() have the right effect?
561 */
562 return FAILED;
563}
564
565static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
566{
567 struct scsi_device *sdev = cmnd->device;
568 struct uas_dev_info *devinfo = sdev->hostdata;
569 struct usb_device *udev = devinfo->udev;
570
571 sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
572 cmnd->request->tag);
573
574 if (usb_reset_device(udev))
575 return SUCCESS;
576
577 return FAILED;
578}
579
580static int uas_slave_alloc(struct scsi_device *sdev)
581{
582 sdev->hostdata = (void *)sdev->host->hostdata[0];
583 return 0;
584}
585
586static int uas_slave_configure(struct scsi_device *sdev)
587{
588 struct uas_dev_info *devinfo = sdev->hostdata;
589 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100590 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200591 return 0;
592}
593
594static struct scsi_host_template uas_host_template = {
595 .module = THIS_MODULE,
596 .name = "uas",
597 .queuecommand = uas_queuecommand,
598 .slave_alloc = uas_slave_alloc,
599 .slave_configure = uas_slave_configure,
600 .eh_abort_handler = uas_eh_abort_handler,
601 .eh_device_reset_handler = uas_eh_device_reset_handler,
602 .eh_target_reset_handler = uas_eh_target_reset_handler,
603 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
604 .can_queue = 65536, /* Is there a limit on the _host_ ? */
605 .this_id = -1,
606 .sg_tablesize = SG_NONE,
607 .cmd_per_lun = 1, /* until we override it */
608 .skip_settle_delay = 1,
609 .ordered_tag = 1,
610};
611
612static struct usb_device_id uas_usb_ids[] = {
613 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
614 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
615 /* 0xaa is a prototype device I happen to have access to */
616 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
617 { }
618};
619MODULE_DEVICE_TABLE(usb, uas_usb_ids);
620
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500621static int uas_is_interface(struct usb_host_interface *intf)
622{
623 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
624 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
625 intf->desc.bInterfaceProtocol == USB_PR_UAS);
626}
627
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100628static int uas_isnt_supported(struct usb_device *udev)
629{
630 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
631
632 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
633 "support scatter-gather which is\n",
634 hcd->driver->description);
635 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
636 "alternative USB controller if you wish to use UAS.\n");
637 return -ENODEV;
638}
639
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500640static int uas_switch_interface(struct usb_device *udev,
641 struct usb_interface *intf)
642{
643 int i;
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100644 int sg_supported = udev->bus->sg_tablesize != 0;
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500645
646 for (i = 0; i < intf->num_altsetting; i++) {
647 struct usb_host_interface *alt = &intf->altsetting[i];
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100648
649 if (uas_is_interface(alt)) {
650 if (!sg_supported)
651 return uas_isnt_supported(udev);
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500652 return usb_set_interface(udev,
653 alt->desc.bInterfaceNumber,
654 alt->desc.bAlternateSetting);
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100655 }
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500656 }
657
658 return -ENODEV;
659}
660
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200661static void uas_configure_endpoints(struct uas_dev_info *devinfo)
662{
663 struct usb_host_endpoint *eps[4] = { };
664 struct usb_interface *intf = devinfo->intf;
665 struct usb_device *udev = devinfo->udev;
666 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
667 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
668
669 devinfo->uas_sense_old = 0;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100670 devinfo->cmnd = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200671
672 for (i = 0; i < n_endpoints; i++) {
673 unsigned char *extra = endpoint[i].extra;
674 int len = endpoint[i].extralen;
675 while (len > 1) {
676 if (extra[1] == USB_DT_PIPE_USAGE) {
677 unsigned pipe_id = extra[2];
678 if (pipe_id > 0 && pipe_id < 5)
679 eps[pipe_id - 1] = &endpoint[i];
680 break;
681 }
682 len -= extra[0];
683 extra += extra[0];
684 }
685 }
686
687 /*
688 * Assume that if we didn't find a control pipe descriptor, we're
689 * using a device with old firmware that happens to be set up like
690 * this.
691 */
692 if (!eps[0]) {
693 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
694 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
695 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
696 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
697
698 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
699 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
700 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
701 } else {
702 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
703 eps[0]->desc.bEndpointAddress);
704 devinfo->status_pipe = usb_rcvbulkpipe(udev,
705 eps[1]->desc.bEndpointAddress);
706 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
707 eps[2]->desc.bEndpointAddress);
708 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
709 eps[3]->desc.bEndpointAddress);
710 }
711
712 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
713 GFP_KERNEL);
714 if (devinfo->qdepth < 0) {
715 devinfo->qdepth = 256;
716 devinfo->use_streams = 0;
717 } else {
718 devinfo->use_streams = 1;
719 }
720}
721
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100722static void uas_free_streams(struct uas_dev_info *devinfo)
723{
724 struct usb_device *udev = devinfo->udev;
725 struct usb_host_endpoint *eps[3];
726
727 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
728 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
729 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
730 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
731}
732
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200733/*
734 * XXX: What I'd like to do here is register a SCSI host for each USB host in
735 * the system. Follow usb-storage's design of registering a SCSI host for
736 * each USB device for the moment. Can implement this by walking up the
737 * USB hierarchy until we find a USB host.
738 */
739static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
740{
741 int result;
742 struct Scsi_Host *shost;
743 struct uas_dev_info *devinfo;
744 struct usb_device *udev = interface_to_usbdev(intf);
745
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500746 if (uas_switch_interface(udev, intf))
747 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200748
749 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
750 if (!devinfo)
751 return -ENOMEM;
752
753 result = -ENOMEM;
754 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
755 if (!shost)
756 goto free;
757
758 shost->max_cmd_len = 16 + 252;
759 shost->max_id = 1;
760 shost->sg_tablesize = udev->bus->sg_tablesize;
761
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200762 devinfo->intf = intf;
763 devinfo->udev = udev;
764 uas_configure_endpoints(devinfo);
765
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100766 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100767 if (result)
768 goto free;
769
770 result = scsi_add_host(shost, &intf->dev);
771 if (result)
772 goto deconfig_eps;
773
774 shost->hostdata[0] = (unsigned long)devinfo;
775
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200776 scsi_scan_host(shost);
777 usb_set_intfdata(intf, shost);
778 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100779
780deconfig_eps:
781 uas_free_streams(devinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200782 free:
783 kfree(devinfo);
784 if (shost)
785 scsi_host_put(shost);
786 return result;
787}
788
789static int uas_pre_reset(struct usb_interface *intf)
790{
791/* XXX: Need to return 1 if it's not our device in error handling */
792 return 0;
793}
794
795static int uas_post_reset(struct usb_interface *intf)
796{
797/* XXX: Need to return 1 if it's not our device in error handling */
798 return 0;
799}
800
801static void uas_disconnect(struct usb_interface *intf)
802{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200803 struct Scsi_Host *shost = usb_get_intfdata(intf);
804 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
805
806 scsi_remove_host(shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100807 uas_free_streams(devinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200808 kfree(devinfo);
809}
810
811/*
812 * XXX: Should this plug into libusual so we can auto-upgrade devices from
813 * Bulk-Only to UAS?
814 */
815static struct usb_driver uas_driver = {
816 .name = "uas",
817 .probe = uas_probe,
818 .disconnect = uas_disconnect,
819 .pre_reset = uas_pre_reset,
820 .post_reset = uas_post_reset,
821 .id_table = uas_usb_ids,
822};
823
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800824module_usb_driver(uas_driver);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200825
826MODULE_LICENSE("GPL");
827MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");