aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/storage/uas.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/storage/uas.c')
-rw-r--r--drivers/usb/storage/uas.c687
1 files changed, 437 insertions, 250 deletions
diff --git a/drivers/usb/storage/uas.c b/drivers/usb/storage/uas.c
index d966b59f7d7b..a7ac97cc5949 100644
--- a/drivers/usb/storage/uas.c
+++ b/drivers/usb/storage/uas.c
@@ -2,6 +2,7 @@
2 * USB Attached SCSI 2 * USB Attached SCSI
3 * Note that this is not the same as the USB Mass Storage driver 3 * Note that this is not the same as the USB Mass Storage driver
4 * 4 *
5 * Copyright Hans de Goede <hdegoede@redhat.com> for Red Hat, Inc. 2013
5 * Copyright Matthew Wilcox for Intel Corp, 2010 6 * Copyright Matthew Wilcox for Intel Corp, 2010
6 * Copyright Sarah Sharp for Intel Corp, 2010 7 * Copyright Sarah Sharp for Intel Corp, 2010
7 * 8 *
@@ -13,17 +14,21 @@
13#include <linux/types.h> 14#include <linux/types.h>
14#include <linux/module.h> 15#include <linux/module.h>
15#include <linux/usb.h> 16#include <linux/usb.h>
17#include <linux/usb_usual.h>
16#include <linux/usb/hcd.h> 18#include <linux/usb/hcd.h>
17#include <linux/usb/storage.h> 19#include <linux/usb/storage.h>
18#include <linux/usb/uas.h> 20#include <linux/usb/uas.h>
19 21
20#include <scsi/scsi.h> 22#include <scsi/scsi.h>
23#include <scsi/scsi_eh.h>
21#include <scsi/scsi_dbg.h> 24#include <scsi/scsi_dbg.h>
22#include <scsi/scsi_cmnd.h> 25#include <scsi/scsi_cmnd.h>
23#include <scsi/scsi_device.h> 26#include <scsi/scsi_device.h>
24#include <scsi/scsi_host.h> 27#include <scsi/scsi_host.h>
25#include <scsi/scsi_tcq.h> 28#include <scsi/scsi_tcq.h>
26 29
30#include "uas-detect.h"
31
27/* 32/*
28 * The r00-r01c specs define this version of the SENSE IU data structure. 33 * The r00-r01c specs define this version of the SENSE IU data structure.
29 * It's still in use by several different firmware releases. 34 * It's still in use by several different firmware releases.
@@ -45,12 +50,17 @@ struct uas_dev_info {
45 struct usb_anchor sense_urbs; 50 struct usb_anchor sense_urbs;
46 struct usb_anchor data_urbs; 51 struct usb_anchor data_urbs;
47 int qdepth, resetting; 52 int qdepth, resetting;
48 struct response_ui response; 53 struct response_iu response;
49 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe; 54 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
50 unsigned use_streams:1; 55 unsigned use_streams:1;
51 unsigned uas_sense_old:1; 56 unsigned uas_sense_old:1;
57 unsigned running_task:1;
58 unsigned shutdown:1;
52 struct scsi_cmnd *cmnd; 59 struct scsi_cmnd *cmnd;
53 spinlock_t lock; 60 spinlock_t lock;
61 struct work_struct work;
62 struct list_head inflight_list;
63 struct list_head dead_list;
54}; 64};
55 65
56enum { 66enum {
@@ -85,103 +95,117 @@ static int uas_submit_urbs(struct scsi_cmnd *cmnd,
85 struct uas_dev_info *devinfo, gfp_t gfp); 95 struct uas_dev_info *devinfo, gfp_t gfp);
86static void uas_do_work(struct work_struct *work); 96static void uas_do_work(struct work_struct *work);
87static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller); 97static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
98static void uas_free_streams(struct uas_dev_info *devinfo);
99static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller);
88 100
89static DECLARE_WORK(uas_work, uas_do_work); 101/* Must be called with devinfo->lock held, will temporary unlock the lock */
90static DEFINE_SPINLOCK(uas_work_lock);
91static LIST_HEAD(uas_work_list);
92
93static void uas_unlink_data_urbs(struct uas_dev_info *devinfo, 102static void uas_unlink_data_urbs(struct uas_dev_info *devinfo,
94 struct uas_cmd_info *cmdinfo) 103 struct uas_cmd_info *cmdinfo,
104 unsigned long *lock_flags)
95{ 105{
96 unsigned long flags;
97
98 /* 106 /*
99 * The UNLINK_DATA_URBS flag makes sure uas_try_complete 107 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
100 * (called by urb completion) doesn't release cmdinfo 108 * (called by urb completion) doesn't release cmdinfo
101 * underneath us. 109 * underneath us.
102 */ 110 */
103 spin_lock_irqsave(&devinfo->lock, flags);
104 cmdinfo->state |= UNLINK_DATA_URBS; 111 cmdinfo->state |= UNLINK_DATA_URBS;
105 spin_unlock_irqrestore(&devinfo->lock, flags); 112 spin_unlock_irqrestore(&devinfo->lock, *lock_flags);
106 113
107 if (cmdinfo->data_in_urb) 114 if (cmdinfo->data_in_urb)
108 usb_unlink_urb(cmdinfo->data_in_urb); 115 usb_unlink_urb(cmdinfo->data_in_urb);
109 if (cmdinfo->data_out_urb) 116 if (cmdinfo->data_out_urb)
110 usb_unlink_urb(cmdinfo->data_out_urb); 117 usb_unlink_urb(cmdinfo->data_out_urb);
111 118
112 spin_lock_irqsave(&devinfo->lock, flags); 119 spin_lock_irqsave(&devinfo->lock, *lock_flags);
113 cmdinfo->state &= ~UNLINK_DATA_URBS; 120 cmdinfo->state &= ~UNLINK_DATA_URBS;
114 spin_unlock_irqrestore(&devinfo->lock, flags);
115} 121}
116 122
117static void uas_do_work(struct work_struct *work) 123static void uas_do_work(struct work_struct *work)
118{ 124{
125 struct uas_dev_info *devinfo =
126 container_of(work, struct uas_dev_info, work);
119 struct uas_cmd_info *cmdinfo; 127 struct uas_cmd_info *cmdinfo;
120 struct uas_cmd_info *temp;
121 struct list_head list;
122 unsigned long flags; 128 unsigned long flags;
123 int err; 129 int err;
124 130
125 spin_lock_irq(&uas_work_lock); 131 spin_lock_irqsave(&devinfo->lock, flags);
126 list_replace_init(&uas_work_list, &list); 132 list_for_each_entry(cmdinfo, &devinfo->inflight_list, list) {
127 spin_unlock_irq(&uas_work_lock);
128
129 list_for_each_entry_safe(cmdinfo, temp, &list, list) {
130 struct scsi_pointer *scp = (void *)cmdinfo; 133 struct scsi_pointer *scp = (void *)cmdinfo;
131 struct scsi_cmnd *cmnd = container_of(scp, 134 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
132 struct scsi_cmnd, SCp); 135 SCp);
133 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; 136
134 spin_lock_irqsave(&devinfo->lock, flags); 137 if (!(cmdinfo->state & IS_IN_WORK_LIST))
135 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC); 138 continue;
139
140 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
136 if (!err) 141 if (!err)
137 cmdinfo->state &= ~IS_IN_WORK_LIST; 142 cmdinfo->state &= ~IS_IN_WORK_LIST;
138 spin_unlock_irqrestore(&devinfo->lock, flags); 143 else
139 if (err) { 144 schedule_work(&devinfo->work);
140 list_del(&cmdinfo->list);
141 spin_lock_irq(&uas_work_lock);
142 list_add_tail(&cmdinfo->list, &uas_work_list);
143 spin_unlock_irq(&uas_work_lock);
144 schedule_work(&uas_work);
145 }
146 } 145 }
146 spin_unlock_irqrestore(&devinfo->lock, flags);
147} 147}
148 148
149static void uas_abort_work(struct uas_dev_info *devinfo) 149static void uas_mark_cmd_dead(struct uas_dev_info *devinfo,
150 struct uas_cmd_info *cmdinfo,
151 int result, const char *caller)
152{
153 struct scsi_pointer *scp = (void *)cmdinfo;
154 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
155
156 uas_log_cmd_state(cmnd, caller);
157 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
158 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
159 cmdinfo->state |= COMMAND_ABORTED;
160 cmdinfo->state &= ~IS_IN_WORK_LIST;
161 cmnd->result = result << 16;
162 list_move_tail(&cmdinfo->list, &devinfo->dead_list);
163}
164
165static void uas_abort_inflight(struct uas_dev_info *devinfo, int result,
166 const char *caller)
150{ 167{
151 struct uas_cmd_info *cmdinfo; 168 struct uas_cmd_info *cmdinfo;
152 struct uas_cmd_info *temp; 169 struct uas_cmd_info *temp;
153 struct list_head list;
154 unsigned long flags; 170 unsigned long flags;
155 171
156 spin_lock_irq(&uas_work_lock); 172 spin_lock_irqsave(&devinfo->lock, flags);
157 list_replace_init(&uas_work_list, &list); 173 list_for_each_entry_safe(cmdinfo, temp, &devinfo->inflight_list, list)
158 spin_unlock_irq(&uas_work_lock); 174 uas_mark_cmd_dead(devinfo, cmdinfo, result, caller);
175 spin_unlock_irqrestore(&devinfo->lock, flags);
176}
177
178static void uas_add_work(struct uas_cmd_info *cmdinfo)
179{
180 struct scsi_pointer *scp = (void *)cmdinfo;
181 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
182 struct uas_dev_info *devinfo = cmnd->device->hostdata;
183
184 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
185 cmdinfo->state |= IS_IN_WORK_LIST;
186 schedule_work(&devinfo->work);
187}
188
189static void uas_zap_dead(struct uas_dev_info *devinfo)
190{
191 struct uas_cmd_info *cmdinfo;
192 struct uas_cmd_info *temp;
193 unsigned long flags;
159 194
160 spin_lock_irqsave(&devinfo->lock, flags); 195 spin_lock_irqsave(&devinfo->lock, flags);
161 list_for_each_entry_safe(cmdinfo, temp, &list, list) { 196 list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, list) {
162 struct scsi_pointer *scp = (void *)cmdinfo; 197 struct scsi_pointer *scp = (void *)cmdinfo;
163 struct scsi_cmnd *cmnd = container_of(scp, 198 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
164 struct scsi_cmnd, SCp); 199 SCp);
165 struct uas_dev_info *di = (void *)cmnd->device->hostdata; 200 uas_log_cmd_state(cmnd, __func__);
166 201 WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED));
167 if (di == devinfo) { 202 /* all urbs are killed, clear inflight bits */
168 cmdinfo->state |= COMMAND_ABORTED; 203 cmdinfo->state &= ~(COMMAND_INFLIGHT |
169 cmdinfo->state &= ~IS_IN_WORK_LIST; 204 DATA_IN_URB_INFLIGHT |
170 if (devinfo->resetting) { 205 DATA_OUT_URB_INFLIGHT);
171 /* uas_stat_cmplt() will not do that 206 uas_try_complete(cmnd, __func__);
172 * when a device reset is in
173 * progress */
174 cmdinfo->state &= ~COMMAND_INFLIGHT;
175 }
176 uas_try_complete(cmnd, __func__);
177 } else {
178 /* not our uas device, relink into list */
179 list_del(&cmdinfo->list);
180 spin_lock_irq(&uas_work_lock);
181 list_add_tail(&cmdinfo->list, &uas_work_list);
182 spin_unlock_irq(&uas_work_lock);
183 }
184 } 207 }
208 devinfo->running_task = 0;
185 spin_unlock_irqrestore(&devinfo->lock, flags); 209 spin_unlock_irqrestore(&devinfo->lock, flags);
186} 210}
187 211
@@ -259,20 +283,19 @@ static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
259 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 283 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
260 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; 284 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
261 285
262 WARN_ON(!spin_is_locked(&devinfo->lock)); 286 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
263 if (cmdinfo->state & (COMMAND_INFLIGHT | 287 if (cmdinfo->state & (COMMAND_INFLIGHT |
264 DATA_IN_URB_INFLIGHT | 288 DATA_IN_URB_INFLIGHT |
265 DATA_OUT_URB_INFLIGHT | 289 DATA_OUT_URB_INFLIGHT |
266 UNLINK_DATA_URBS)) 290 UNLINK_DATA_URBS))
267 return -EBUSY; 291 return -EBUSY;
268 BUG_ON(cmdinfo->state & COMMAND_COMPLETED); 292 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
269 cmdinfo->state |= COMMAND_COMPLETED; 293 cmdinfo->state |= COMMAND_COMPLETED;
270 usb_free_urb(cmdinfo->data_in_urb); 294 usb_free_urb(cmdinfo->data_in_urb);
271 usb_free_urb(cmdinfo->data_out_urb); 295 usb_free_urb(cmdinfo->data_out_urb);
272 if (cmdinfo->state & COMMAND_ABORTED) { 296 if (cmdinfo->state & COMMAND_ABORTED)
273 scmd_printk(KERN_INFO, cmnd, "abort completed\n"); 297 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
274 cmnd->result = DID_ABORT << 16; 298 list_del(&cmdinfo->list);
275 }
276 cmnd->scsi_done(cmnd); 299 cmnd->scsi_done(cmnd);
277 return 0; 300 return 0;
278} 301}
@@ -286,11 +309,7 @@ static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
286 cmdinfo->state |= direction | SUBMIT_STATUS_URB; 309 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
287 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC); 310 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
288 if (err) { 311 if (err) {
289 spin_lock(&uas_work_lock); 312 uas_add_work(cmdinfo);
290 list_add_tail(&cmdinfo->list, &uas_work_list);
291 cmdinfo->state |= IS_IN_WORK_LIST;
292 spin_unlock(&uas_work_lock);
293 schedule_work(&uas_work);
294 } 313 }
295} 314}
296 315
@@ -298,14 +317,20 @@ static void uas_stat_cmplt(struct urb *urb)
298{ 317{
299 struct iu *iu = urb->transfer_buffer; 318 struct iu *iu = urb->transfer_buffer;
300 struct Scsi_Host *shost = urb->context; 319 struct Scsi_Host *shost = urb->context;
301 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 320 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
302 struct scsi_cmnd *cmnd; 321 struct scsi_cmnd *cmnd;
303 struct uas_cmd_info *cmdinfo; 322 struct uas_cmd_info *cmdinfo;
304 unsigned long flags; 323 unsigned long flags;
305 u16 tag; 324 u16 tag;
306 325
307 if (urb->status) { 326 if (urb->status) {
308 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status); 327 if (urb->status == -ENOENT) {
328 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
329 urb->stream_id);
330 } else {
331 dev_err(&urb->dev->dev, "stat urb: status %d\n",
332 urb->status);
333 }
309 usb_free_urb(urb); 334 usb_free_urb(urb);
310 return; 335 return;
311 } 336 }
@@ -324,6 +349,9 @@ static void uas_stat_cmplt(struct urb *urb)
324 349
325 if (!cmnd) { 350 if (!cmnd) {
326 if (iu->iu_id == IU_ID_RESPONSE) { 351 if (iu->iu_id == IU_ID_RESPONSE) {
352 if (!devinfo->running_task)
353 dev_warn(&urb->dev->dev,
354 "stat urb: recv unexpected response iu\n");
327 /* store results for uas_eh_task_mgmt() */ 355 /* store results for uas_eh_task_mgmt() */
328 memcpy(&devinfo->response, iu, sizeof(devinfo->response)); 356 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
329 } 357 }
@@ -346,17 +374,25 @@ static void uas_stat_cmplt(struct urb *urb)
346 uas_sense(urb, cmnd); 374 uas_sense(urb, cmnd);
347 if (cmnd->result != 0) { 375 if (cmnd->result != 0) {
348 /* cancel data transfers on error */ 376 /* cancel data transfers on error */
349 spin_unlock_irqrestore(&devinfo->lock, flags); 377 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
350 uas_unlink_data_urbs(devinfo, cmdinfo);
351 spin_lock_irqsave(&devinfo->lock, flags);
352 } 378 }
353 cmdinfo->state &= ~COMMAND_INFLIGHT; 379 cmdinfo->state &= ~COMMAND_INFLIGHT;
354 uas_try_complete(cmnd, __func__); 380 uas_try_complete(cmnd, __func__);
355 break; 381 break;
356 case IU_ID_READ_READY: 382 case IU_ID_READ_READY:
383 if (!cmdinfo->data_in_urb ||
384 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
385 scmd_printk(KERN_ERR, cmnd, "unexpected read rdy\n");
386 break;
387 }
357 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB); 388 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
358 break; 389 break;
359 case IU_ID_WRITE_READY: 390 case IU_ID_WRITE_READY:
391 if (!cmdinfo->data_out_urb ||
392 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
393 scmd_printk(KERN_ERR, cmnd, "unexpected write rdy\n");
394 break;
395 }
360 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB); 396 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
361 break; 397 break;
362 default: 398 default:
@@ -383,8 +419,15 @@ static void uas_data_cmplt(struct urb *urb)
383 sdb = scsi_out(cmnd); 419 sdb = scsi_out(cmnd);
384 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT; 420 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
385 } 421 }
386 BUG_ON(sdb == NULL); 422 if (sdb == NULL) {
387 if (urb->status) { 423 WARN_ON_ONCE(1);
424 } else if (urb->status) {
425 if (urb->status != -ECONNRESET) {
426 uas_log_cmd_state(cmnd, __func__);
427 scmd_printk(KERN_ERR, cmnd,
428 "data cmplt err %d stream %d\n",
429 urb->status, urb->stream_id);
430 }
388 /* error: no data transfered */ 431 /* error: no data transfered */
389 sdb->resid = sdb->length; 432 sdb->resid = sdb->length;
390 } else { 433 } else {
@@ -394,6 +437,17 @@ static void uas_data_cmplt(struct urb *urb)
394 spin_unlock_irqrestore(&devinfo->lock, flags); 437 spin_unlock_irqrestore(&devinfo->lock, flags);
395} 438}
396 439
440static void uas_cmd_cmplt(struct urb *urb)
441{
442 struct scsi_cmnd *cmnd = urb->context;
443
444 if (urb->status) {
445 uas_log_cmd_state(cmnd, __func__);
446 scmd_printk(KERN_ERR, cmnd, "cmd cmplt err %d\n", urb->status);
447 }
448 usb_free_urb(urb);
449}
450
397static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp, 451static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
398 unsigned int pipe, u16 stream_id, 452 unsigned int pipe, u16 stream_id,
399 struct scsi_cmnd *cmnd, 453 struct scsi_cmnd *cmnd,
@@ -408,8 +462,7 @@ static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
408 goto out; 462 goto out;
409 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, 463 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
410 uas_data_cmplt, cmnd); 464 uas_data_cmplt, cmnd);
411 if (devinfo->use_streams) 465 urb->stream_id = stream_id;
412 urb->stream_id = stream_id;
413 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0; 466 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
414 urb->sg = sdb->table.sgl; 467 urb->sg = sdb->table.sgl;
415 out: 468 out:
@@ -442,7 +495,7 @@ static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
442} 495}
443 496
444static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp, 497static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
445 struct scsi_cmnd *cmnd, u16 stream_id) 498 struct scsi_cmnd *cmnd)
446{ 499{
447 struct usb_device *udev = devinfo->udev; 500 struct usb_device *udev = devinfo->udev;
448 struct scsi_device *sdev = cmnd->device; 501 struct scsi_device *sdev = cmnd->device;
@@ -472,7 +525,7 @@ static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
472 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len); 525 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
473 526
474 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len, 527 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
475 usb_free_urb, NULL); 528 uas_cmd_cmplt, cmnd);
476 urb->transfer_flags |= URB_FREE_BUFFER; 529 urb->transfer_flags |= URB_FREE_BUFFER;
477 out: 530 out:
478 return urb; 531 return urb;
@@ -512,13 +565,17 @@ static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
512 } 565 }
513 566
514 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu), 567 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
515 usb_free_urb, NULL); 568 uas_cmd_cmplt, cmnd);
516 urb->transfer_flags |= URB_FREE_BUFFER; 569 urb->transfer_flags |= URB_FREE_BUFFER;
517 570
571 usb_anchor_urb(urb, &devinfo->cmd_urbs);
518 err = usb_submit_urb(urb, gfp); 572 err = usb_submit_urb(urb, gfp);
519 if (err) 573 if (err) {
574 usb_unanchor_urb(urb);
575 uas_log_cmd_state(cmnd, __func__);
576 scmd_printk(KERN_ERR, cmnd, "task submission err %d\n", err);
520 goto err; 577 goto err;
521 usb_anchor_urb(urb, &devinfo->cmd_urbs); 578 }
522 579
523 return 0; 580 return 0;
524 581
@@ -533,38 +590,43 @@ err:
533 * daft to me. 590 * daft to me.
534 */ 591 */
535 592
536static int uas_submit_sense_urb(struct Scsi_Host *shost, 593static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd,
537 gfp_t gfp, unsigned int stream) 594 gfp_t gfp, unsigned int stream)
538{ 595{
539 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 596 struct Scsi_Host *shost = cmnd->device->host;
597 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
540 struct urb *urb; 598 struct urb *urb;
599 int err;
541 600
542 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream); 601 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
543 if (!urb) 602 if (!urb)
544 return SCSI_MLQUEUE_DEVICE_BUSY; 603 return NULL;
545 if (usb_submit_urb(urb, gfp)) { 604 usb_anchor_urb(urb, &devinfo->sense_urbs);
605 err = usb_submit_urb(urb, gfp);
606 if (err) {
607 usb_unanchor_urb(urb);
608 uas_log_cmd_state(cmnd, __func__);
546 shost_printk(KERN_INFO, shost, 609 shost_printk(KERN_INFO, shost,
547 "sense urb submission failure\n"); 610 "sense urb submission error %d stream %d\n",
611 err, stream);
548 usb_free_urb(urb); 612 usb_free_urb(urb);
549 return SCSI_MLQUEUE_DEVICE_BUSY; 613 return NULL;
550 } 614 }
551 usb_anchor_urb(urb, &devinfo->sense_urbs); 615 return urb;
552 return 0;
553} 616}
554 617
555static int uas_submit_urbs(struct scsi_cmnd *cmnd, 618static int uas_submit_urbs(struct scsi_cmnd *cmnd,
556 struct uas_dev_info *devinfo, gfp_t gfp) 619 struct uas_dev_info *devinfo, gfp_t gfp)
557{ 620{
558 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; 621 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
622 struct urb *urb;
559 int err; 623 int err;
560 624
561 WARN_ON(!spin_is_locked(&devinfo->lock)); 625 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
562 if (cmdinfo->state & SUBMIT_STATUS_URB) { 626 if (cmdinfo->state & SUBMIT_STATUS_URB) {
563 err = uas_submit_sense_urb(cmnd->device->host, gfp, 627 urb = uas_submit_sense_urb(cmnd, gfp, cmdinfo->stream);
564 cmdinfo->stream); 628 if (!urb)
565 if (err) { 629 return SCSI_MLQUEUE_DEVICE_BUSY;
566 return err;
567 }
568 cmdinfo->state &= ~SUBMIT_STATUS_URB; 630 cmdinfo->state &= ~SUBMIT_STATUS_URB;
569 } 631 }
570 632
@@ -578,14 +640,18 @@ static int uas_submit_urbs(struct scsi_cmnd *cmnd,
578 } 640 }
579 641
580 if (cmdinfo->state & SUBMIT_DATA_IN_URB) { 642 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
581 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) { 643 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
644 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
645 if (err) {
646 usb_unanchor_urb(cmdinfo->data_in_urb);
647 uas_log_cmd_state(cmnd, __func__);
582 scmd_printk(KERN_INFO, cmnd, 648 scmd_printk(KERN_INFO, cmnd,
583 "data in urb submission failure\n"); 649 "data in urb submission error %d stream %d\n",
650 err, cmdinfo->data_in_urb->stream_id);
584 return SCSI_MLQUEUE_DEVICE_BUSY; 651 return SCSI_MLQUEUE_DEVICE_BUSY;
585 } 652 }
586 cmdinfo->state &= ~SUBMIT_DATA_IN_URB; 653 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
587 cmdinfo->state |= DATA_IN_URB_INFLIGHT; 654 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
588 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
589 } 655 }
590 656
591 if (cmdinfo->state & ALLOC_DATA_OUT_URB) { 657 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
@@ -598,33 +664,37 @@ static int uas_submit_urbs(struct scsi_cmnd *cmnd,
598 } 664 }
599 665
600 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) { 666 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
601 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) { 667 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
668 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
669 if (err) {
670 usb_unanchor_urb(cmdinfo->data_out_urb);
671 uas_log_cmd_state(cmnd, __func__);
602 scmd_printk(KERN_INFO, cmnd, 672 scmd_printk(KERN_INFO, cmnd,
603 "data out urb submission failure\n"); 673 "data out urb submission error %d stream %d\n",
674 err, cmdinfo->data_out_urb->stream_id);
604 return SCSI_MLQUEUE_DEVICE_BUSY; 675 return SCSI_MLQUEUE_DEVICE_BUSY;
605 } 676 }
606 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB; 677 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
607 cmdinfo->state |= DATA_OUT_URB_INFLIGHT; 678 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
608 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
609 } 679 }
610 680
611 if (cmdinfo->state & ALLOC_CMD_URB) { 681 if (cmdinfo->state & ALLOC_CMD_URB) {
612 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd, 682 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
613 cmdinfo->stream);
614 if (!cmdinfo->cmd_urb) 683 if (!cmdinfo->cmd_urb)
615 return SCSI_MLQUEUE_DEVICE_BUSY; 684 return SCSI_MLQUEUE_DEVICE_BUSY;
616 cmdinfo->state &= ~ALLOC_CMD_URB; 685 cmdinfo->state &= ~ALLOC_CMD_URB;
617 } 686 }
618 687
619 if (cmdinfo->state & SUBMIT_CMD_URB) { 688 if (cmdinfo->state & SUBMIT_CMD_URB) {
620 usb_get_urb(cmdinfo->cmd_urb); 689 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
621 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) { 690 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
691 if (err) {
692 usb_unanchor_urb(cmdinfo->cmd_urb);
693 uas_log_cmd_state(cmnd, __func__);
622 scmd_printk(KERN_INFO, cmnd, 694 scmd_printk(KERN_INFO, cmnd,
623 "cmd urb submission failure\n"); 695 "cmd urb submission error %d\n", err);
624 return SCSI_MLQUEUE_DEVICE_BUSY; 696 return SCSI_MLQUEUE_DEVICE_BUSY;
625 } 697 }
626 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
627 usb_put_urb(cmdinfo->cmd_urb);
628 cmdinfo->cmd_urb = NULL; 698 cmdinfo->cmd_urb = NULL;
629 cmdinfo->state &= ~SUBMIT_CMD_URB; 699 cmdinfo->state &= ~SUBMIT_CMD_URB;
630 cmdinfo->state |= COMMAND_INFLIGHT; 700 cmdinfo->state |= COMMAND_INFLIGHT;
@@ -644,18 +714,22 @@ static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
644 714
645 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer)); 715 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
646 716
717 spin_lock_irqsave(&devinfo->lock, flags);
718
647 if (devinfo->resetting) { 719 if (devinfo->resetting) {
648 cmnd->result = DID_ERROR << 16; 720 cmnd->result = DID_ERROR << 16;
649 cmnd->scsi_done(cmnd); 721 cmnd->scsi_done(cmnd);
722 spin_unlock_irqrestore(&devinfo->lock, flags);
650 return 0; 723 return 0;
651 } 724 }
652 725
653 spin_lock_irqsave(&devinfo->lock, flags);
654 if (devinfo->cmnd) { 726 if (devinfo->cmnd) {
655 spin_unlock_irqrestore(&devinfo->lock, flags); 727 spin_unlock_irqrestore(&devinfo->lock, flags);
656 return SCSI_MLQUEUE_DEVICE_BUSY; 728 return SCSI_MLQUEUE_DEVICE_BUSY;
657 } 729 }
658 730
731 memset(cmdinfo, 0, sizeof(*cmdinfo));
732
659 if (blk_rq_tagged(cmnd->request)) { 733 if (blk_rq_tagged(cmnd->request)) {
660 cmdinfo->stream = cmnd->request->tag + 2; 734 cmdinfo->stream = cmnd->request->tag + 2;
661 } else { 735 } else {
@@ -692,13 +766,10 @@ static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
692 spin_unlock_irqrestore(&devinfo->lock, flags); 766 spin_unlock_irqrestore(&devinfo->lock, flags);
693 return SCSI_MLQUEUE_DEVICE_BUSY; 767 return SCSI_MLQUEUE_DEVICE_BUSY;
694 } 768 }
695 spin_lock(&uas_work_lock); 769 uas_add_work(cmdinfo);
696 list_add_tail(&cmdinfo->list, &uas_work_list);
697 cmdinfo->state |= IS_IN_WORK_LIST;
698 spin_unlock(&uas_work_lock);
699 schedule_work(&uas_work);
700 } 770 }
701 771
772 list_add_tail(&cmdinfo->list, &devinfo->inflight_list);
702 spin_unlock_irqrestore(&devinfo->lock, flags); 773 spin_unlock_irqrestore(&devinfo->lock, flags);
703 return 0; 774 return 0;
704} 775}
@@ -709,46 +780,78 @@ static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
709 const char *fname, u8 function) 780 const char *fname, u8 function)
710{ 781{
711 struct Scsi_Host *shost = cmnd->device->host; 782 struct Scsi_Host *shost = cmnd->device->host;
712 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 783 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
713 u16 tag = devinfo->qdepth - 1; 784 u16 tag = devinfo->qdepth;
714 unsigned long flags; 785 unsigned long flags;
786 struct urb *sense_urb;
787 int result = SUCCESS;
715 788
716 spin_lock_irqsave(&devinfo->lock, flags); 789 spin_lock_irqsave(&devinfo->lock, flags);
790
791 if (devinfo->resetting) {
792 spin_unlock_irqrestore(&devinfo->lock, flags);
793 return FAILED;
794 }
795
796 if (devinfo->running_task) {
797 shost_printk(KERN_INFO, shost,
798 "%s: %s: error already running a task\n",
799 __func__, fname);
800 spin_unlock_irqrestore(&devinfo->lock, flags);
801 return FAILED;
802 }
803
804 devinfo->running_task = 1;
717 memset(&devinfo->response, 0, sizeof(devinfo->response)); 805 memset(&devinfo->response, 0, sizeof(devinfo->response));
718 if (uas_submit_sense_urb(shost, GFP_ATOMIC, tag)) { 806 sense_urb = uas_submit_sense_urb(cmnd, GFP_NOIO,
807 devinfo->use_streams ? tag : 0);
808 if (!sense_urb) {
719 shost_printk(KERN_INFO, shost, 809 shost_printk(KERN_INFO, shost,
720 "%s: %s: submit sense urb failed\n", 810 "%s: %s: submit sense urb failed\n",
721 __func__, fname); 811 __func__, fname);
812 devinfo->running_task = 0;
722 spin_unlock_irqrestore(&devinfo->lock, flags); 813 spin_unlock_irqrestore(&devinfo->lock, flags);
723 return FAILED; 814 return FAILED;
724 } 815 }
725 if (uas_submit_task_urb(cmnd, GFP_ATOMIC, function, tag)) { 816 if (uas_submit_task_urb(cmnd, GFP_NOIO, function, tag)) {
726 shost_printk(KERN_INFO, shost, 817 shost_printk(KERN_INFO, shost,
727 "%s: %s: submit task mgmt urb failed\n", 818 "%s: %s: submit task mgmt urb failed\n",
728 __func__, fname); 819 __func__, fname);
820 devinfo->running_task = 0;
729 spin_unlock_irqrestore(&devinfo->lock, flags); 821 spin_unlock_irqrestore(&devinfo->lock, flags);
822 usb_kill_urb(sense_urb);
730 return FAILED; 823 return FAILED;
731 } 824 }
732 spin_unlock_irqrestore(&devinfo->lock, flags); 825 spin_unlock_irqrestore(&devinfo->lock, flags);
733 826
734 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) { 827 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) {
828 /*
829 * Note we deliberately do not clear running_task here. If we
830 * allow new tasks to be submitted, there is no way to figure
831 * out if a received response_iu is for the failed task or for
832 * the new one. A bus-reset will eventually clear running_task.
833 */
735 shost_printk(KERN_INFO, shost, 834 shost_printk(KERN_INFO, shost,
736 "%s: %s timed out\n", __func__, fname); 835 "%s: %s timed out\n", __func__, fname);
737 return FAILED; 836 return FAILED;
738 } 837 }
838
839 spin_lock_irqsave(&devinfo->lock, flags);
840 devinfo->running_task = 0;
739 if (be16_to_cpu(devinfo->response.tag) != tag) { 841 if (be16_to_cpu(devinfo->response.tag) != tag) {
740 shost_printk(KERN_INFO, shost, 842 shost_printk(KERN_INFO, shost,
741 "%s: %s failed (wrong tag %d/%d)\n", __func__, 843 "%s: %s failed (wrong tag %d/%d)\n", __func__,
742 fname, be16_to_cpu(devinfo->response.tag), tag); 844 fname, be16_to_cpu(devinfo->response.tag), tag);
743 return FAILED; 845 result = FAILED;
744 } 846 } else if (devinfo->response.response_code != RC_TMF_COMPLETE) {
745 if (devinfo->response.response_code != RC_TMF_COMPLETE) {
746 shost_printk(KERN_INFO, shost, 847 shost_printk(KERN_INFO, shost,
747 "%s: %s failed (rc 0x%x)\n", __func__, 848 "%s: %s failed (rc 0x%x)\n", __func__,
748 fname, devinfo->response.response_code); 849 fname, devinfo->response.response_code);
749 return FAILED; 850 result = FAILED;
750 } 851 }
751 return SUCCESS; 852 spin_unlock_irqrestore(&devinfo->lock, flags);
853
854 return result;
752} 855}
753 856
754static int uas_eh_abort_handler(struct scsi_cmnd *cmnd) 857static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
@@ -758,22 +861,19 @@ static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
758 unsigned long flags; 861 unsigned long flags;
759 int ret; 862 int ret;
760 863
761 uas_log_cmd_state(cmnd, __func__);
762 spin_lock_irqsave(&devinfo->lock, flags); 864 spin_lock_irqsave(&devinfo->lock, flags);
763 cmdinfo->state |= COMMAND_ABORTED; 865
764 if (cmdinfo->state & IS_IN_WORK_LIST) { 866 if (devinfo->resetting) {
765 spin_lock(&uas_work_lock); 867 spin_unlock_irqrestore(&devinfo->lock, flags);
766 list_del(&cmdinfo->list); 868 return FAILED;
767 cmdinfo->state &= ~IS_IN_WORK_LIST;
768 spin_unlock(&uas_work_lock);
769 } 869 }
870
871 uas_mark_cmd_dead(devinfo, cmdinfo, DID_ABORT, __func__);
770 if (cmdinfo->state & COMMAND_INFLIGHT) { 872 if (cmdinfo->state & COMMAND_INFLIGHT) {
771 spin_unlock_irqrestore(&devinfo->lock, flags); 873 spin_unlock_irqrestore(&devinfo->lock, flags);
772 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK); 874 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
773 } else { 875 } else {
774 spin_unlock_irqrestore(&devinfo->lock, flags); 876 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
775 uas_unlink_data_urbs(devinfo, cmdinfo);
776 spin_lock_irqsave(&devinfo->lock, flags);
777 uas_try_complete(cmnd, __func__); 877 uas_try_complete(cmnd, __func__);
778 spin_unlock_irqrestore(&devinfo->lock, flags); 878 spin_unlock_irqrestore(&devinfo->lock, flags);
779 ret = SUCCESS; 879 ret = SUCCESS;
@@ -795,14 +895,25 @@ static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
795 struct usb_device *udev = devinfo->udev; 895 struct usb_device *udev = devinfo->udev;
796 int err; 896 int err;
797 897
898 err = usb_lock_device_for_reset(udev, devinfo->intf);
899 if (err) {
900 shost_printk(KERN_ERR, sdev->host,
901 "%s FAILED to get lock err %d\n", __func__, err);
902 return FAILED;
903 }
904
905 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
798 devinfo->resetting = 1; 906 devinfo->resetting = 1;
799 uas_abort_work(devinfo); 907 uas_abort_inflight(devinfo, DID_RESET, __func__);
800 usb_kill_anchored_urbs(&devinfo->cmd_urbs); 908 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
801 usb_kill_anchored_urbs(&devinfo->sense_urbs); 909 usb_kill_anchored_urbs(&devinfo->sense_urbs);
802 usb_kill_anchored_urbs(&devinfo->data_urbs); 910 usb_kill_anchored_urbs(&devinfo->data_urbs);
911 uas_zap_dead(devinfo);
803 err = usb_reset_device(udev); 912 err = usb_reset_device(udev);
804 devinfo->resetting = 0; 913 devinfo->resetting = 0;
805 914
915 usb_unlock_device(udev);
916
806 if (err) { 917 if (err) {
807 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__); 918 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
808 return FAILED; 919 return FAILED;
@@ -814,7 +925,25 @@ static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
814 925
815static int uas_slave_alloc(struct scsi_device *sdev) 926static int uas_slave_alloc(struct scsi_device *sdev)
816{ 927{
817 sdev->hostdata = (void *)sdev->host->hostdata[0]; 928 sdev->hostdata = (void *)sdev->host->hostdata;
929
930 /* USB has unusual DMA-alignment requirements: Although the
931 * starting address of each scatter-gather element doesn't matter,
932 * the length of each element except the last must be divisible
933 * by the Bulk maxpacket value. There's currently no way to
934 * express this by block-layer constraints, so we'll cop out
935 * and simply require addresses to be aligned at 512-byte
936 * boundaries. This is okay since most block I/O involves
937 * hardware sectors that are multiples of 512 bytes in length,
938 * and since host controllers up through USB 2.0 have maxpacket
939 * values no larger than 512.
940 *
941 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
942 * values can be as large as 2048. To make that work properly
943 * will require changes to the block layer.
944 */
945 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
946
818 return 0; 947 return 0;
819} 948}
820 949
@@ -822,7 +951,7 @@ static int uas_slave_configure(struct scsi_device *sdev)
822{ 951{
823 struct uas_dev_info *devinfo = sdev->hostdata; 952 struct uas_dev_info *devinfo = sdev->hostdata;
824 scsi_set_tag_type(sdev, MSG_ORDERED_TAG); 953 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
825 scsi_activate_tcq(sdev, devinfo->qdepth - 3); 954 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
826 return 0; 955 return 0;
827} 956}
828 957
@@ -843,7 +972,14 @@ static struct scsi_host_template uas_host_template = {
843 .ordered_tag = 1, 972 .ordered_tag = 1,
844}; 973};
845 974
975#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
976 vendorName, productName, useProtocol, useTransport, \
977 initFunction, flags) \
978{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
979 .driver_info = (flags) }
980
846static struct usb_device_id uas_usb_ids[] = { 981static struct usb_device_id uas_usb_ids[] = {
982# include "unusual_uas.h"
847 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) }, 983 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
848 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) }, 984 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
849 /* 0xaa is a prototype device I happen to have access to */ 985 /* 0xaa is a prototype device I happen to have access to */
@@ -852,105 +988,55 @@ static struct usb_device_id uas_usb_ids[] = {
852}; 988};
853MODULE_DEVICE_TABLE(usb, uas_usb_ids); 989MODULE_DEVICE_TABLE(usb, uas_usb_ids);
854 990
855static int uas_is_interface(struct usb_host_interface *intf) 991#undef UNUSUAL_DEV
856{
857 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
858 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
859 intf->desc.bInterfaceProtocol == USB_PR_UAS);
860}
861
862static int uas_isnt_supported(struct usb_device *udev)
863{
864 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
865
866 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
867 "support scatter-gather which is\n",
868 hcd->driver->description);
869 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
870 "alternative USB controller if you wish to use UAS.\n");
871 return -ENODEV;
872}
873 992
874static int uas_switch_interface(struct usb_device *udev, 993static int uas_switch_interface(struct usb_device *udev,
875 struct usb_interface *intf) 994 struct usb_interface *intf)
876{ 995{
877 int i; 996 int alt;
878 int sg_supported = udev->bus->sg_tablesize != 0; 997
879 998 alt = uas_find_uas_alt_setting(intf);
880 for (i = 0; i < intf->num_altsetting; i++) { 999 if (alt < 0)
881 struct usb_host_interface *alt = &intf->altsetting[i]; 1000 return alt;
882
883 if (uas_is_interface(alt)) {
884 if (!sg_supported)
885 return uas_isnt_supported(udev);
886 return usb_set_interface(udev,
887 alt->desc.bInterfaceNumber,
888 alt->desc.bAlternateSetting);
889 }
890 }
891 1001
892 return -ENODEV; 1002 return usb_set_interface(udev,
1003 intf->altsetting[0].desc.bInterfaceNumber, alt);
893} 1004}
894 1005
895static void uas_configure_endpoints(struct uas_dev_info *devinfo) 1006static int uas_configure_endpoints(struct uas_dev_info *devinfo)
896{ 1007{
897 struct usb_host_endpoint *eps[4] = { }; 1008 struct usb_host_endpoint *eps[4] = { };
898 struct usb_interface *intf = devinfo->intf;
899 struct usb_device *udev = devinfo->udev; 1009 struct usb_device *udev = devinfo->udev;
900 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint; 1010 int r;
901 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
902 1011
903 devinfo->uas_sense_old = 0; 1012 devinfo->uas_sense_old = 0;
904 devinfo->cmnd = NULL; 1013 devinfo->cmnd = NULL;
905 1014
906 for (i = 0; i < n_endpoints; i++) { 1015 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
907 unsigned char *extra = endpoint[i].extra; 1016 if (r)
908 int len = endpoint[i].extralen; 1017 return r;
909 while (len > 1) {
910 if (extra[1] == USB_DT_PIPE_USAGE) {
911 unsigned pipe_id = extra[2];
912 if (pipe_id > 0 && pipe_id < 5)
913 eps[pipe_id - 1] = &endpoint[i];
914 break;
915 }
916 len -= extra[0];
917 extra += extra[0];
918 }
919 }
920 1018
921 /* 1019 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
922 * Assume that if we didn't find a control pipe descriptor, we're 1020 usb_endpoint_num(&eps[0]->desc));
923 * using a device with old firmware that happens to be set up like 1021 devinfo->status_pipe = usb_rcvbulkpipe(udev,
924 * this. 1022 usb_endpoint_num(&eps[1]->desc));
925 */ 1023 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
926 if (!eps[0]) { 1024 usb_endpoint_num(&eps[2]->desc));
927 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1); 1025 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
928 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1); 1026 usb_endpoint_num(&eps[3]->desc));
929 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
930 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
931
932 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
933 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
934 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
935 } else {
936 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
937 eps[0]->desc.bEndpointAddress);
938 devinfo->status_pipe = usb_rcvbulkpipe(udev,
939 eps[1]->desc.bEndpointAddress);
940 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
941 eps[2]->desc.bEndpointAddress);
942 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
943 eps[3]->desc.bEndpointAddress);
944 }
945 1027
946 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256, 1028 if (udev->speed != USB_SPEED_SUPER) {
947 GFP_KERNEL);
948 if (devinfo->qdepth < 0) {
949 devinfo->qdepth = 256; 1029 devinfo->qdepth = 256;
950 devinfo->use_streams = 0; 1030 devinfo->use_streams = 0;
951 } else { 1031 } else {
1032 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
1033 3, 256, GFP_KERNEL);
1034 if (devinfo->qdepth < 0)
1035 return devinfo->qdepth;
952 devinfo->use_streams = 1; 1036 devinfo->use_streams = 1;
953 } 1037 }
1038
1039 return 0;
954} 1040}
955 1041
956static void uas_free_streams(struct uas_dev_info *devinfo) 1042static void uas_free_streams(struct uas_dev_info *devinfo)
@@ -964,30 +1050,23 @@ static void uas_free_streams(struct uas_dev_info *devinfo)
964 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL); 1050 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
965} 1051}
966 1052
967/*
968 * XXX: What I'd like to do here is register a SCSI host for each USB host in
969 * the system. Follow usb-storage's design of registering a SCSI host for
970 * each USB device for the moment. Can implement this by walking up the
971 * USB hierarchy until we find a USB host.
972 */
973static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id) 1053static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
974{ 1054{
975 int result; 1055 int result = -ENOMEM;
976 struct Scsi_Host *shost; 1056 struct Scsi_Host *shost = NULL;
977 struct uas_dev_info *devinfo; 1057 struct uas_dev_info *devinfo;
978 struct usb_device *udev = interface_to_usbdev(intf); 1058 struct usb_device *udev = interface_to_usbdev(intf);
979 1059
980 if (uas_switch_interface(udev, intf)) 1060 if (!uas_use_uas_driver(intf, id))
981 return -ENODEV; 1061 return -ENODEV;
982 1062
983 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL); 1063 if (uas_switch_interface(udev, intf))
984 if (!devinfo) 1064 return -ENODEV;
985 return -ENOMEM;
986 1065
987 result = -ENOMEM; 1066 shost = scsi_host_alloc(&uas_host_template,
988 shost = scsi_host_alloc(&uas_host_template, sizeof(void *)); 1067 sizeof(struct uas_dev_info));
989 if (!shost) 1068 if (!shost)
990 goto free; 1069 goto set_alt0;
991 1070
992 shost->max_cmd_len = 16 + 252; 1071 shost->max_cmd_len = 16 + 252;
993 shost->max_id = 1; 1072 shost->max_id = 1;
@@ -995,33 +1074,40 @@ static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
995 shost->max_channel = 0; 1074 shost->max_channel = 0;
996 shost->sg_tablesize = udev->bus->sg_tablesize; 1075 shost->sg_tablesize = udev->bus->sg_tablesize;
997 1076
1077 devinfo = (struct uas_dev_info *)shost->hostdata;
998 devinfo->intf = intf; 1078 devinfo->intf = intf;
999 devinfo->udev = udev; 1079 devinfo->udev = udev;
1000 devinfo->resetting = 0; 1080 devinfo->resetting = 0;
1081 devinfo->running_task = 0;
1082 devinfo->shutdown = 0;
1001 init_usb_anchor(&devinfo->cmd_urbs); 1083 init_usb_anchor(&devinfo->cmd_urbs);
1002 init_usb_anchor(&devinfo->sense_urbs); 1084 init_usb_anchor(&devinfo->sense_urbs);
1003 init_usb_anchor(&devinfo->data_urbs); 1085 init_usb_anchor(&devinfo->data_urbs);
1004 spin_lock_init(&devinfo->lock); 1086 spin_lock_init(&devinfo->lock);
1005 uas_configure_endpoints(devinfo); 1087 INIT_WORK(&devinfo->work, uas_do_work);
1088 INIT_LIST_HEAD(&devinfo->inflight_list);
1089 INIT_LIST_HEAD(&devinfo->dead_list);
1006 1090
1007 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 3); 1091 result = uas_configure_endpoints(devinfo);
1008 if (result) 1092 if (result)
1009 goto free; 1093 goto set_alt0;
1010 1094
1011 result = scsi_add_host(shost, &intf->dev); 1095 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
1012 if (result) 1096 if (result)
1013 goto deconfig_eps; 1097 goto free_streams;
1014 1098
1015 shost->hostdata[0] = (unsigned long)devinfo; 1099 result = scsi_add_host(shost, &intf->dev);
1100 if (result)
1101 goto free_streams;
1016 1102
1017 scsi_scan_host(shost); 1103 scsi_scan_host(shost);
1018 usb_set_intfdata(intf, shost); 1104 usb_set_intfdata(intf, shost);
1019 return result; 1105 return result;
1020 1106
1021deconfig_eps: 1107free_streams:
1022 uas_free_streams(devinfo); 1108 uas_free_streams(devinfo);
1023 free: 1109set_alt0:
1024 kfree(devinfo); 1110 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1025 if (shost) 1111 if (shost)
1026 scsi_host_put(shost); 1112 scsi_host_put(shost);
1027 return result; 1113 return result;
@@ -1029,45 +1115,146 @@ deconfig_eps:
1029 1115
1030static int uas_pre_reset(struct usb_interface *intf) 1116static int uas_pre_reset(struct usb_interface *intf)
1031{ 1117{
1032/* XXX: Need to return 1 if it's not our device in error handling */ 1118 struct Scsi_Host *shost = usb_get_intfdata(intf);
1119 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1120 unsigned long flags;
1121
1122 if (devinfo->shutdown)
1123 return 0;
1124
1125 /* Block new requests */
1126 spin_lock_irqsave(shost->host_lock, flags);
1127 scsi_block_requests(shost);
1128 spin_unlock_irqrestore(shost->host_lock, flags);
1129
1130 /* Wait for any pending requests to complete */
1131 flush_work(&devinfo->work);
1132 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1133 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1134 return 1;
1135 }
1136
1137 uas_free_streams(devinfo);
1138
1033 return 0; 1139 return 0;
1034} 1140}
1035 1141
1036static int uas_post_reset(struct usb_interface *intf) 1142static int uas_post_reset(struct usb_interface *intf)
1037{ 1143{
1038/* XXX: Need to return 1 if it's not our device in error handling */ 1144 struct Scsi_Host *shost = usb_get_intfdata(intf);
1145 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1146 unsigned long flags;
1147
1148 if (devinfo->shutdown)
1149 return 0;
1150
1151 if (uas_configure_endpoints(devinfo) != 0) {
1152 shost_printk(KERN_ERR, shost,
1153 "%s: alloc streams error after reset", __func__);
1154 return 1;
1155 }
1156
1157 spin_lock_irqsave(shost->host_lock, flags);
1158 scsi_report_bus_reset(shost, 0);
1159 spin_unlock_irqrestore(shost->host_lock, flags);
1160
1161 scsi_unblock_requests(shost);
1162
1163 return 0;
1164}
1165
1166static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1167{
1168 struct Scsi_Host *shost = usb_get_intfdata(intf);
1169 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1170
1171 /* Wait for any pending requests to complete */
1172 flush_work(&devinfo->work);
1173 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1174 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1175 return -ETIME;
1176 }
1177
1178 return 0;
1179}
1180
1181static int uas_resume(struct usb_interface *intf)
1182{
1183 return 0;
1184}
1185
1186static int uas_reset_resume(struct usb_interface *intf)
1187{
1188 struct Scsi_Host *shost = usb_get_intfdata(intf);
1189 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1190 unsigned long flags;
1191
1192 if (uas_configure_endpoints(devinfo) != 0) {
1193 shost_printk(KERN_ERR, shost,
1194 "%s: alloc streams error after reset", __func__);
1195 return -EIO;
1196 }
1197
1198 spin_lock_irqsave(shost->host_lock, flags);
1199 scsi_report_bus_reset(shost, 0);
1200 spin_unlock_irqrestore(shost->host_lock, flags);
1201
1039 return 0; 1202 return 0;
1040} 1203}
1041 1204
1042static void uas_disconnect(struct usb_interface *intf) 1205static void uas_disconnect(struct usb_interface *intf)
1043{ 1206{
1044 struct Scsi_Host *shost = usb_get_intfdata(intf); 1207 struct Scsi_Host *shost = usb_get_intfdata(intf);
1045 struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; 1208 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1046 1209
1047 devinfo->resetting = 1; 1210 devinfo->resetting = 1;
1048 uas_abort_work(devinfo); 1211 cancel_work_sync(&devinfo->work);
1212 uas_abort_inflight(devinfo, DID_NO_CONNECT, __func__);
1049 usb_kill_anchored_urbs(&devinfo->cmd_urbs); 1213 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
1050 usb_kill_anchored_urbs(&devinfo->sense_urbs); 1214 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1051 usb_kill_anchored_urbs(&devinfo->data_urbs); 1215 usb_kill_anchored_urbs(&devinfo->data_urbs);
1216 uas_zap_dead(devinfo);
1052 scsi_remove_host(shost); 1217 scsi_remove_host(shost);
1053 uas_free_streams(devinfo); 1218 uas_free_streams(devinfo);
1054 kfree(devinfo); 1219 scsi_host_put(shost);
1055} 1220}
1056 1221
1057/* 1222/*
1058 * XXX: Should this plug into libusual so we can auto-upgrade devices from 1223 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1059 * Bulk-Only to UAS? 1224 * hang on reboot when the device is still in uas mode. Note the reset is
1225 * necessary as some devices won't revert to usb-storage mode without it.
1060 */ 1226 */
1227static void uas_shutdown(struct device *dev)
1228{
1229 struct usb_interface *intf = to_usb_interface(dev);
1230 struct usb_device *udev = interface_to_usbdev(intf);
1231 struct Scsi_Host *shost = usb_get_intfdata(intf);
1232 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1233
1234 if (system_state != SYSTEM_RESTART)
1235 return;
1236
1237 devinfo->shutdown = 1;
1238 uas_free_streams(devinfo);
1239 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1240 usb_reset_device(udev);
1241}
1242
1061static struct usb_driver uas_driver = { 1243static struct usb_driver uas_driver = {
1062 .name = "uas", 1244 .name = "uas",
1063 .probe = uas_probe, 1245 .probe = uas_probe,
1064 .disconnect = uas_disconnect, 1246 .disconnect = uas_disconnect,
1065 .pre_reset = uas_pre_reset, 1247 .pre_reset = uas_pre_reset,
1066 .post_reset = uas_post_reset, 1248 .post_reset = uas_post_reset,
1249 .suspend = uas_suspend,
1250 .resume = uas_resume,
1251 .reset_resume = uas_reset_resume,
1252 .drvwrap.driver.shutdown = uas_shutdown,
1067 .id_table = uas_usb_ids, 1253 .id_table = uas_usb_ids,
1068}; 1254};
1069 1255
1070module_usb_driver(uas_driver); 1256module_usb_driver(uas_driver);
1071 1257
1072MODULE_LICENSE("GPL"); 1258MODULE_LICENSE("GPL");
1073MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp"); 1259MODULE_AUTHOR(
1260 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");