aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>2012-02-02 19:12:14 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2012-02-02 19:12:14 -0500
commitc39c654fa2a830e1f73a2c118bf0c59ee18f3248 (patch)
treeff7fd6fda4ad80fcf35c199e27550b3e3239152e
parentfec67b45bf045582c3172101970090d640cd56d9 (diff)
parente4d8318a85779b25b880187b1b1c44e797bd7d4b (diff)
Merge tag 'for-uas-next-2012-02-02' of git://git.kernel.org/pub/scm/linux/kernel/git/sarah/xhci into usb-next
UAS bug fixes for 3.4. This includes the merge of the uas_for_sarah signed tag from Sebastian Andrzej Siewior. These patches should be merged into usb-next, and queued for 3.4. The UAS driver error handling has been broken for over a year now, and the (future) changes that are needed to completely fix will be too big to go into stable, so there's no point in queueing this set for stable.
-rw-r--r--drivers/usb/storage/uas.c328
-rw-r--r--include/linux/usb/uas.h69
2 files changed, 281 insertions, 116 deletions
diff --git a/drivers/usb/storage/uas.c b/drivers/usb/storage/uas.c
index a33ead5dce20..8ec8a6e66f50 100644
--- a/drivers/usb/storage/uas.c
+++ b/drivers/usb/storage/uas.c
@@ -13,7 +13,9 @@
13#include <linux/types.h> 13#include <linux/types.h>
14#include <linux/module.h> 14#include <linux/module.h>
15#include <linux/usb.h> 15#include <linux/usb.h>
16#include <linux/usb/hcd.h>
16#include <linux/usb/storage.h> 17#include <linux/usb/storage.h>
18#include <linux/usb/uas.h>
17 19
18#include <scsi/scsi.h> 20#include <scsi/scsi.h>
19#include <scsi/scsi_dbg.h> 21#include <scsi/scsi_dbg.h>
@@ -22,49 +24,6 @@
22#include <scsi/scsi_host.h> 24#include <scsi/scsi_host.h>
23#include <scsi/scsi_tcq.h> 25#include <scsi/scsi_tcq.h>
24 26
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
53/*
54 * Also used for the Read Ready and Write Ready IUs since they have the
55 * same first four bytes
56 */
57struct sense_iu {
58 __u8 iu_id;
59 __u8 rsvd1;
60 __be16 tag;
61 __be16 status_qual;
62 __u8 status;
63 __u8 rsvd7[7];
64 __be16 len;
65 __u8 sense[SCSI_SENSE_BUFFERSIZE];
66};
67
68/* 27/*
69 * The r00-r01c specs define this version of the SENSE IU data structure. 28 * The r00-r01c specs define this version of the SENSE IU data structure.
70 * It's still in use by several different firmware releases. 29 * It's still in use by several different firmware releases.
@@ -79,18 +38,6 @@ struct sense_iu_old {
79 __u8 sense[SCSI_SENSE_BUFFERSIZE]; 38 __u8 sense[SCSI_SENSE_BUFFERSIZE];
80}; 39};
81 40
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 { 41struct uas_dev_info {
95 struct usb_interface *intf; 42 struct usb_interface *intf;
96 struct usb_device *udev; 43 struct usb_device *udev;
@@ -98,6 +45,8 @@ struct uas_dev_info {
98 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe; 45 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
99 unsigned use_streams:1; 46 unsigned use_streams:1;
100 unsigned uas_sense_old:1; 47 unsigned uas_sense_old:1;
48 struct scsi_cmnd *cmnd;
49 struct urb *status_urb; /* used only if stream support is available */
101}; 50};
102 51
103enum { 52enum {
@@ -109,6 +58,9 @@ enum {
109 SUBMIT_DATA_OUT_URB = (1 << 5), 58 SUBMIT_DATA_OUT_URB = (1 << 5),
110 ALLOC_CMD_URB = (1 << 6), 59 ALLOC_CMD_URB = (1 << 6),
111 SUBMIT_CMD_URB = (1 << 7), 60 SUBMIT_CMD_URB = (1 << 7),
61 COMPLETED_DATA_IN = (1 << 8),
62 COMPLETED_DATA_OUT = (1 << 9),
63 DATA_COMPLETES_CMD = (1 << 10),
112}; 64};
113 65
114/* Overrides scsi_pointer */ 66/* Overrides scsi_pointer */
@@ -116,6 +68,7 @@ struct uas_cmd_info {
116 unsigned int state; 68 unsigned int state;
117 unsigned int stream; 69 unsigned int stream;
118 struct urb *cmd_urb; 70 struct urb *cmd_urb;
71 /* status_urb is used only if stream support isn't available */
119 struct urb *status_urb; 72 struct urb *status_urb;
120 struct urb *data_in_urb; 73 struct urb *data_in_urb;
121 struct urb *data_out_urb; 74 struct urb *data_out_urb;
@@ -125,33 +78,43 @@ struct uas_cmd_info {
125/* I hate forward declarations, but I actually have a loop */ 78/* I hate forward declarations, but I actually have a loop */
126static int uas_submit_urbs(struct scsi_cmnd *cmnd, 79static int uas_submit_urbs(struct scsi_cmnd *cmnd,
127 struct uas_dev_info *devinfo, gfp_t gfp); 80 struct uas_dev_info *devinfo, gfp_t gfp);
81static void uas_do_work(struct work_struct *work);
128 82
83static DECLARE_WORK(uas_work, uas_do_work);
129static DEFINE_SPINLOCK(uas_work_lock); 84static DEFINE_SPINLOCK(uas_work_lock);
130static LIST_HEAD(uas_work_list); 85static LIST_HEAD(uas_work_list);
131 86
132static void uas_do_work(struct work_struct *work) 87static void uas_do_work(struct work_struct *work)
133{ 88{
134 struct uas_cmd_info *cmdinfo; 89 struct uas_cmd_info *cmdinfo;
90 struct uas_cmd_info *temp;
135 struct list_head list; 91 struct list_head list;
92 int err;
136 93
137 spin_lock_irq(&uas_work_lock); 94 spin_lock_irq(&uas_work_lock);
138 list_replace_init(&uas_work_list, &list); 95 list_replace_init(&uas_work_list, &list);
139 spin_unlock_irq(&uas_work_lock); 96 spin_unlock_irq(&uas_work_lock);
140 97
141 list_for_each_entry(cmdinfo, &list, list) { 98 list_for_each_entry_safe(cmdinfo, temp, &list, list) {
142 struct scsi_pointer *scp = (void *)cmdinfo; 99 struct scsi_pointer *scp = (void *)cmdinfo;
143 struct scsi_cmnd *cmnd = container_of(scp, 100 struct scsi_cmnd *cmnd = container_of(scp,
144 struct scsi_cmnd, SCp); 101 struct scsi_cmnd, SCp);
145 uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO); 102 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
103 if (err) {
104 list_del(&cmdinfo->list);
105 spin_lock_irq(&uas_work_lock);
106 list_add_tail(&cmdinfo->list, &uas_work_list);
107 spin_unlock_irq(&uas_work_lock);
108 schedule_work(&uas_work);
109 }
146 } 110 }
147} 111}
148 112
149static DECLARE_WORK(uas_work, uas_do_work);
150
151static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd) 113static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
152{ 114{
153 struct sense_iu *sense_iu = urb->transfer_buffer; 115 struct sense_iu *sense_iu = urb->transfer_buffer;
154 struct scsi_device *sdev = cmnd->device; 116 struct scsi_device *sdev = cmnd->device;
117 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
155 118
156 if (urb->actual_length > 16) { 119 if (urb->actual_length > 16) {
157 unsigned len = be16_to_cpup(&sense_iu->len); 120 unsigned len = be16_to_cpup(&sense_iu->len);
@@ -169,16 +132,15 @@ static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
169 } 132 }
170 133
171 cmnd->result = sense_iu->status; 134 cmnd->result = sense_iu->status;
172 if (sdev->current_cmnd) 135 if (!(cmdinfo->state & DATA_COMPLETES_CMD))
173 sdev->current_cmnd = NULL; 136 cmnd->scsi_done(cmnd);
174 cmnd->scsi_done(cmnd);
175 usb_free_urb(urb);
176} 137}
177 138
178static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd) 139static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
179{ 140{
180 struct sense_iu_old *sense_iu = urb->transfer_buffer; 141 struct sense_iu_old *sense_iu = urb->transfer_buffer;
181 struct scsi_device *sdev = cmnd->device; 142 struct scsi_device *sdev = cmnd->device;
143 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
182 144
183 if (urb->actual_length > 8) { 145 if (urb->actual_length > 8) {
184 unsigned len = be16_to_cpup(&sense_iu->len) - 2; 146 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
@@ -196,10 +158,8 @@ static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
196 } 158 }
197 159
198 cmnd->result = sense_iu->status; 160 cmnd->result = sense_iu->status;
199 if (sdev->current_cmnd) 161 if (!(cmdinfo->state & DATA_COMPLETES_CMD))
200 sdev->current_cmnd = NULL; 162 cmnd->scsi_done(cmnd);
201 cmnd->scsi_done(cmnd);
202 usb_free_urb(urb);
203} 163}
204 164
205static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd, 165static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
@@ -208,7 +168,7 @@ static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
208 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 168 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
209 int err; 169 int err;
210 170
211 cmdinfo->state = direction | SUBMIT_STATUS_URB; 171 cmdinfo->state = direction;
212 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC); 172 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
213 if (err) { 173 if (err) {
214 spin_lock(&uas_work_lock); 174 spin_lock(&uas_work_lock);
@@ -221,27 +181,61 @@ static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
221static void uas_stat_cmplt(struct urb *urb) 181static void uas_stat_cmplt(struct urb *urb)
222{ 182{
223 struct iu *iu = urb->transfer_buffer; 183 struct iu *iu = urb->transfer_buffer;
224 struct scsi_device *sdev = urb->context; 184 struct Scsi_Host *shost = urb->context;
225 struct uas_dev_info *devinfo = sdev->hostdata; 185 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
226 struct scsi_cmnd *cmnd; 186 struct scsi_cmnd *cmnd;
187 struct uas_cmd_info *cmdinfo;
227 u16 tag; 188 u16 tag;
189 int ret;
228 190
229 if (urb->status) { 191 if (urb->status) {
230 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status); 192 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
231 usb_free_urb(urb); 193 if (devinfo->use_streams)
194 usb_free_urb(urb);
232 return; 195 return;
233 } 196 }
234 197
235 tag = be16_to_cpup(&iu->tag) - 1; 198 tag = be16_to_cpup(&iu->tag) - 1;
236 if (sdev->current_cmnd) 199 if (tag == 0)
237 cmnd = sdev->current_cmnd; 200 cmnd = devinfo->cmnd;
238 else 201 else
239 cmnd = scsi_find_tag(sdev, tag); 202 cmnd = scsi_host_find_tag(shost, tag - 1);
240 if (!cmnd) 203 if (!cmnd) {
204 if (devinfo->use_streams) {
205 usb_free_urb(urb);
206 return;
207 }
208 ret = usb_submit_urb(urb, GFP_ATOMIC);
209 if (ret)
210 dev_err(&urb->dev->dev, "failed submit status urb\n");
241 return; 211 return;
212 }
213 cmdinfo = (void *)&cmnd->SCp;
242 214
243 switch (iu->iu_id) { 215 switch (iu->iu_id) {
244 case IU_ID_STATUS: 216 case IU_ID_STATUS:
217 if (devinfo->cmnd == cmnd)
218 devinfo->cmnd = NULL;
219
220 if (!(cmdinfo->state & COMPLETED_DATA_IN) &&
221 cmdinfo->data_in_urb) {
222 if (devinfo->use_streams) {
223 cmdinfo->state |= DATA_COMPLETES_CMD;
224 usb_unlink_urb(cmdinfo->data_in_urb);
225 } else {
226 usb_free_urb(cmdinfo->data_in_urb);
227 }
228 }
229 if (!(cmdinfo->state & COMPLETED_DATA_OUT) &&
230 cmdinfo->data_out_urb) {
231 if (devinfo->use_streams) {
232 cmdinfo->state |= DATA_COMPLETES_CMD;
233 usb_unlink_urb(cmdinfo->data_in_urb);
234 } else {
235 usb_free_urb(cmdinfo->data_out_urb);
236 }
237 }
238
245 if (urb->actual_length < 16) 239 if (urb->actual_length < 16)
246 devinfo->uas_sense_old = 1; 240 devinfo->uas_sense_old = 1;
247 if (devinfo->uas_sense_old) 241 if (devinfo->uas_sense_old)
@@ -259,29 +253,70 @@ static void uas_stat_cmplt(struct urb *urb)
259 scmd_printk(KERN_ERR, cmnd, 253 scmd_printk(KERN_ERR, cmnd,
260 "Bogus IU (%d) received on status pipe\n", iu->iu_id); 254 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
261 } 255 }
256
257 if (devinfo->use_streams) {
258 usb_free_urb(urb);
259 return;
260 }
261
262 ret = usb_submit_urb(urb, GFP_ATOMIC);
263 if (ret)
264 dev_err(&urb->dev->dev, "failed submit status urb\n");
262} 265}
263 266
264static void uas_data_cmplt(struct urb *urb) 267static void uas_data_out_cmplt(struct urb *urb)
265{ 268{
266 struct scsi_data_buffer *sdb = urb->context; 269 struct scsi_cmnd *cmnd = urb->context;
270 struct scsi_data_buffer *sdb = scsi_out(cmnd);
271 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
272
273 cmdinfo->state |= COMPLETED_DATA_OUT;
274
267 sdb->resid = sdb->length - urb->actual_length; 275 sdb->resid = sdb->length - urb->actual_length;
268 usb_free_urb(urb); 276 usb_free_urb(urb);
277
278 if (cmdinfo->state & DATA_COMPLETES_CMD)
279 cmnd->scsi_done(cmnd);
280}
281
282static void uas_data_in_cmplt(struct urb *urb)
283{
284 struct scsi_cmnd *cmnd = urb->context;
285 struct scsi_data_buffer *sdb = scsi_in(cmnd);
286 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
287
288 cmdinfo->state |= COMPLETED_DATA_IN;
289
290 sdb->resid = sdb->length - urb->actual_length;
291 usb_free_urb(urb);
292
293 if (cmdinfo->state & DATA_COMPLETES_CMD)
294 cmnd->scsi_done(cmnd);
269} 295}
270 296
271static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp, 297static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
272 unsigned int pipe, u16 stream_id, 298 unsigned int pipe, struct scsi_cmnd *cmnd,
273 struct scsi_data_buffer *sdb, 299 enum dma_data_direction dir)
274 enum dma_data_direction dir)
275{ 300{
301 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
276 struct usb_device *udev = devinfo->udev; 302 struct usb_device *udev = devinfo->udev;
277 struct urb *urb = usb_alloc_urb(0, gfp); 303 struct urb *urb = usb_alloc_urb(0, gfp);
304 struct scsi_data_buffer *sdb;
305 usb_complete_t complete_fn;
306 u16 stream_id = cmdinfo->stream;
278 307
279 if (!urb) 308 if (!urb)
280 goto out; 309 goto out;
281 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, uas_data_cmplt, 310 if (dir == DMA_FROM_DEVICE) {
282 sdb); 311 sdb = scsi_in(cmnd);
283 if (devinfo->use_streams) 312 complete_fn = uas_data_in_cmplt;
284 urb->stream_id = stream_id; 313 } else {
314 sdb = scsi_out(cmnd);
315 complete_fn = uas_data_out_cmplt;
316 }
317 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
318 complete_fn, cmnd);
319 urb->stream_id = stream_id;
285 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0; 320 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
286 urb->sg = sdb->table.sgl; 321 urb->sg = sdb->table.sgl;
287 out: 322 out:
@@ -289,7 +324,7 @@ static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
289} 324}
290 325
291static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp, 326static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
292 struct scsi_cmnd *cmnd, u16 stream_id) 327 struct Scsi_Host *shost, u16 stream_id)
293{ 328{
294 struct usb_device *udev = devinfo->udev; 329 struct usb_device *udev = devinfo->udev;
295 struct urb *urb = usb_alloc_urb(0, gfp); 330 struct urb *urb = usb_alloc_urb(0, gfp);
@@ -303,7 +338,7 @@ static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
303 goto free; 338 goto free;
304 339
305 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu), 340 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
306 uas_stat_cmplt, cmnd->device); 341 uas_stat_cmplt, shost);
307 urb->stream_id = stream_id; 342 urb->stream_id = stream_id;
308 urb->transfer_flags |= URB_FREE_BUFFER; 343 urb->transfer_flags |= URB_FREE_BUFFER;
309 out: 344 out:
@@ -334,7 +369,10 @@ static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
334 goto free; 369 goto free;
335 370
336 iu->iu_id = IU_ID_COMMAND; 371 iu->iu_id = IU_ID_COMMAND;
337 iu->tag = cpu_to_be16(stream_id); 372 if (blk_rq_tagged(cmnd->request))
373 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
374 else
375 iu->tag = cpu_to_be16(1);
338 iu->prio_attr = UAS_SIMPLE_TAG; 376 iu->prio_attr = UAS_SIMPLE_TAG;
339 iu->len = len; 377 iu->len = len;
340 int_to_scsilun(sdev->lun, &iu->lun); 378 int_to_scsilun(sdev->lun, &iu->lun);
@@ -362,8 +400,8 @@ static int uas_submit_urbs(struct scsi_cmnd *cmnd,
362 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 400 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
363 401
364 if (cmdinfo->state & ALLOC_STATUS_URB) { 402 if (cmdinfo->state & ALLOC_STATUS_URB) {
365 cmdinfo->status_urb = uas_alloc_sense_urb(devinfo, gfp, cmnd, 403 cmdinfo->status_urb = uas_alloc_sense_urb(devinfo, gfp,
366 cmdinfo->stream); 404 cmnd->device->host, cmdinfo->stream);
367 if (!cmdinfo->status_urb) 405 if (!cmdinfo->status_urb)
368 return SCSI_MLQUEUE_DEVICE_BUSY; 406 return SCSI_MLQUEUE_DEVICE_BUSY;
369 cmdinfo->state &= ~ALLOC_STATUS_URB; 407 cmdinfo->state &= ~ALLOC_STATUS_URB;
@@ -380,8 +418,8 @@ static int uas_submit_urbs(struct scsi_cmnd *cmnd,
380 418
381 if (cmdinfo->state & ALLOC_DATA_IN_URB) { 419 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
382 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp, 420 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
383 devinfo->data_in_pipe, cmdinfo->stream, 421 devinfo->data_in_pipe, cmnd,
384 scsi_in(cmnd), DMA_FROM_DEVICE); 422 DMA_FROM_DEVICE);
385 if (!cmdinfo->data_in_urb) 423 if (!cmdinfo->data_in_urb)
386 return SCSI_MLQUEUE_DEVICE_BUSY; 424 return SCSI_MLQUEUE_DEVICE_BUSY;
387 cmdinfo->state &= ~ALLOC_DATA_IN_URB; 425 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
@@ -398,8 +436,8 @@ static int uas_submit_urbs(struct scsi_cmnd *cmnd,
398 436
399 if (cmdinfo->state & ALLOC_DATA_OUT_URB) { 437 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
400 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp, 438 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
401 devinfo->data_out_pipe, cmdinfo->stream, 439 devinfo->data_out_pipe, cmnd,
402 scsi_out(cmnd), DMA_TO_DEVICE); 440 DMA_TO_DEVICE);
403 if (!cmdinfo->data_out_urb) 441 if (!cmdinfo->data_out_urb)
404 return SCSI_MLQUEUE_DEVICE_BUSY; 442 return SCSI_MLQUEUE_DEVICE_BUSY;
405 cmdinfo->state &= ~ALLOC_DATA_OUT_URB; 443 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
@@ -444,13 +482,13 @@ static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
444 482
445 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer)); 483 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
446 484
447 if (!cmdinfo->status_urb && sdev->current_cmnd) 485 if (devinfo->cmnd)
448 return SCSI_MLQUEUE_DEVICE_BUSY; 486 return SCSI_MLQUEUE_DEVICE_BUSY;
449 487
450 if (blk_rq_tagged(cmnd->request)) { 488 if (blk_rq_tagged(cmnd->request)) {
451 cmdinfo->stream = cmnd->request->tag + 1; 489 cmdinfo->stream = cmnd->request->tag + 2;
452 } else { 490 } else {
453 sdev->current_cmnd = cmnd; 491 devinfo->cmnd = cmnd;
454 cmdinfo->stream = 1; 492 cmdinfo->stream = 1;
455 } 493 }
456 494
@@ -472,7 +510,8 @@ static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
472 } 510 }
473 511
474 if (!devinfo->use_streams) { 512 if (!devinfo->use_streams) {
475 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB); 513 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB |
514 ALLOC_STATUS_URB | SUBMIT_STATUS_URB);
476 cmdinfo->stream = 0; 515 cmdinfo->stream = 0;
477 } 516 }
478 517
@@ -551,7 +590,7 @@ static int uas_slave_configure(struct scsi_device *sdev)
551{ 590{
552 struct uas_dev_info *devinfo = sdev->hostdata; 591 struct uas_dev_info *devinfo = sdev->hostdata;
553 scsi_set_tag_type(sdev, MSG_ORDERED_TAG); 592 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
554 scsi_activate_tcq(sdev, devinfo->qdepth - 1); 593 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
555 return 0; 594 return 0;
556} 595}
557 596
@@ -589,22 +628,34 @@ static int uas_is_interface(struct usb_host_interface *intf)
589 intf->desc.bInterfaceProtocol == USB_PR_UAS); 628 intf->desc.bInterfaceProtocol == USB_PR_UAS);
590} 629}
591 630
631static int uas_isnt_supported(struct usb_device *udev)
632{
633 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
634
635 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
636 "support scatter-gather which is\n",
637 hcd->driver->description);
638 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
639 "alternative USB controller if you wish to use UAS.\n");
640 return -ENODEV;
641}
642
592static int uas_switch_interface(struct usb_device *udev, 643static int uas_switch_interface(struct usb_device *udev,
593 struct usb_interface *intf) 644 struct usb_interface *intf)
594{ 645{
595 int i; 646 int i;
596 647 int sg_supported = udev->bus->sg_tablesize != 0;
597 if (uas_is_interface(intf->cur_altsetting))
598 return 0;
599 648
600 for (i = 0; i < intf->num_altsetting; i++) { 649 for (i = 0; i < intf->num_altsetting; i++) {
601 struct usb_host_interface *alt = &intf->altsetting[i]; 650 struct usb_host_interface *alt = &intf->altsetting[i];
602 if (alt == intf->cur_altsetting) 651
603 continue; 652 if (uas_is_interface(alt)) {
604 if (uas_is_interface(alt)) 653 if (!sg_supported)
654 return uas_isnt_supported(udev);
605 return usb_set_interface(udev, 655 return usb_set_interface(udev,
606 alt->desc.bInterfaceNumber, 656 alt->desc.bInterfaceNumber,
607 alt->desc.bAlternateSetting); 657 alt->desc.bAlternateSetting);
658 }
608 } 659 }
609 660
610 return -ENODEV; 661 return -ENODEV;
@@ -619,6 +670,7 @@ static void uas_configure_endpoints(struct uas_dev_info *devinfo)
619 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints; 670 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
620 671
621 devinfo->uas_sense_old = 0; 672 devinfo->uas_sense_old = 0;
673 devinfo->cmnd = NULL;
622 674
623 for (i = 0; i < n_endpoints; i++) { 675 for (i = 0; i < n_endpoints; i++) {
624 unsigned char *extra = endpoint[i].extra; 676 unsigned char *extra = endpoint[i].extra;
@@ -670,6 +722,40 @@ static void uas_configure_endpoints(struct uas_dev_info *devinfo)
670 } 722 }
671} 723}
672 724
725static int uas_alloc_status_urb(struct uas_dev_info *devinfo,
726 struct Scsi_Host *shost)
727{
728 if (devinfo->use_streams) {
729 devinfo->status_urb = NULL;
730 return 0;
731 }
732
733 devinfo->status_urb = uas_alloc_sense_urb(devinfo, GFP_KERNEL,
734 shost, 0);
735 if (!devinfo->status_urb)
736 goto err_s_urb;
737
738 if (usb_submit_urb(devinfo->status_urb, GFP_KERNEL))
739 goto err_submit_urb;
740
741 return 0;
742err_submit_urb:
743 usb_free_urb(devinfo->status_urb);
744err_s_urb:
745 return -ENOMEM;
746}
747
748static void uas_free_streams(struct uas_dev_info *devinfo)
749{
750 struct usb_device *udev = devinfo->udev;
751 struct usb_host_endpoint *eps[3];
752
753 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
754 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
755 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
756 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
757}
758
673/* 759/*
674 * XXX: What I'd like to do here is register a SCSI host for each USB host in 760 * XXX: What I'd like to do here is register a SCSI host for each USB host in
675 * the system. Follow usb-storage's design of registering a SCSI host for 761 * the system. Follow usb-storage's design of registering a SCSI host for
@@ -699,18 +785,33 @@ static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
699 shost->max_id = 1; 785 shost->max_id = 1;
700 shost->sg_tablesize = udev->bus->sg_tablesize; 786 shost->sg_tablesize = udev->bus->sg_tablesize;
701 787
702 result = scsi_add_host(shost, &intf->dev); 788 devinfo->intf = intf;
789 devinfo->udev = udev;
790 uas_configure_endpoints(devinfo);
791
792 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
703 if (result) 793 if (result)
704 goto free; 794 goto free;
795
796 result = scsi_add_host(shost, &intf->dev);
797 if (result)
798 goto deconfig_eps;
799
705 shost->hostdata[0] = (unsigned long)devinfo; 800 shost->hostdata[0] = (unsigned long)devinfo;
706 801
707 devinfo->intf = intf; 802 result = uas_alloc_status_urb(devinfo, shost);
708 devinfo->udev = udev; 803 if (result)
709 uas_configure_endpoints(devinfo); 804 goto err_alloc_status;
710 805
711 scsi_scan_host(shost); 806 scsi_scan_host(shost);
712 usb_set_intfdata(intf, shost); 807 usb_set_intfdata(intf, shost);
713 return result; 808 return result;
809
810err_alloc_status:
811 scsi_remove_host(shost);
812 shost = NULL;
813deconfig_eps:
814 uas_free_streams(devinfo);
714 free: 815 free:
715 kfree(devinfo); 816 kfree(devinfo);
716 if (shost) 817 if (shost)
@@ -732,18 +833,13 @@ static int uas_post_reset(struct usb_interface *intf)
732 833
733static void uas_disconnect(struct usb_interface *intf) 834static void uas_disconnect(struct usb_interface *intf)
734{ 835{
735 struct usb_device *udev = interface_to_usbdev(intf);
736 struct usb_host_endpoint *eps[3];
737 struct Scsi_Host *shost = usb_get_intfdata(intf); 836 struct Scsi_Host *shost = usb_get_intfdata(intf);
738 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 837 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
739 838
740 scsi_remove_host(shost); 839 scsi_remove_host(shost);
741 840 usb_kill_urb(devinfo->status_urb);
742 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe); 841 usb_free_urb(devinfo->status_urb);
743 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe); 842 uas_free_streams(devinfo);
744 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
745 usb_free_streams(intf, eps, 3, GFP_KERNEL);
746
747 kfree(devinfo); 843 kfree(devinfo);
748} 844}
749 845
diff --git a/include/linux/usb/uas.h b/include/linux/usb/uas.h
new file mode 100644
index 000000000000..9a988e413694
--- /dev/null
+++ b/include/linux/usb/uas.h
@@ -0,0 +1,69 @@
1#ifndef __USB_UAS_H__
2#define __USB_UAS_H__
3
4#include <scsi/scsi.h>
5#include <scsi/scsi_cmnd.h>
6
7/* Common header for all IUs */
8struct iu {
9 __u8 iu_id;
10 __u8 rsvd1;
11 __be16 tag;
12};
13
14enum {
15 IU_ID_COMMAND = 0x01,
16 IU_ID_STATUS = 0x03,
17 IU_ID_RESPONSE = 0x04,
18 IU_ID_TASK_MGMT = 0x05,
19 IU_ID_READ_READY = 0x06,
20 IU_ID_WRITE_READY = 0x07,
21};
22
23struct command_iu {
24 __u8 iu_id;
25 __u8 rsvd1;
26 __be16 tag;
27 __u8 prio_attr;
28 __u8 rsvd5;
29 __u8 len;
30 __u8 rsvd7;
31 struct scsi_lun lun;
32 __u8 cdb[16]; /* XXX: Overflow-checking tools may misunderstand */
33};
34
35/*
36 * Also used for the Read Ready and Write Ready IUs since they have the
37 * same first four bytes
38 */
39struct sense_iu {
40 __u8 iu_id;
41 __u8 rsvd1;
42 __be16 tag;
43 __be16 status_qual;
44 __u8 status;
45 __u8 rsvd7[7];
46 __be16 len;
47 __u8 sense[SCSI_SENSE_BUFFERSIZE];
48};
49
50struct usb_pipe_usage_descriptor {
51 __u8 bLength;
52 __u8 bDescriptorType;
53
54 __u8 bPipeID;
55 __u8 Reserved;
56} __attribute__((__packed__));
57
58enum {
59 CMD_PIPE_ID = 1,
60 STATUS_PIPE_ID = 2,
61 DATA_IN_PIPE_ID = 3,
62 DATA_OUT_PIPE_ID = 4,
63
64 UAS_SIMPLE_TAG = 0,
65 UAS_HEAD_TAG = 1,
66 UAS_ORDERED_TAG = 2,
67 UAS_ACA = 4,
68};
69#endif