uas: Do not use scsi_host_find_tag
[deliverable/linux.git] / drivers / usb / storage / uas.c
CommitLineData
115bb1ff
MW
1/*
2 * USB Attached SCSI
3 * Note that this is not the same as the USB Mass Storage driver
4 *
5df2be63 5 * Copyright Hans de Goede <hdegoede@redhat.com> for Red Hat, Inc. 2013 - 2014
115bb1ff
MW
6 * Copyright Matthew Wilcox for Intel Corp, 2010
7 * Copyright Sarah Sharp for Intel Corp, 2010
8 *
9 * Distributed under the terms of the GNU GPL, version two.
10 */
11
12#include <linux/blkdev.h>
13#include <linux/slab.h>
14#include <linux/types.h>
6eb0de82 15#include <linux/module.h>
115bb1ff 16#include <linux/usb.h>
79b4c061 17#include <linux/usb_usual.h>
c898add5 18#include <linux/usb/hcd.h>
115bb1ff 19#include <linux/usb/storage.h>
348748b0 20#include <linux/usb/uas.h>
115bb1ff
MW
21
22#include <scsi/scsi.h>
4de7a373 23#include <scsi/scsi_eh.h>
115bb1ff
MW
24#include <scsi/scsi_dbg.h>
25#include <scsi/scsi_cmnd.h>
26#include <scsi/scsi_device.h>
27#include <scsi/scsi_host.h>
28#include <scsi/scsi_tcq.h>
29
82aa0387 30#include "uas-detect.h"
59307852 31#include "scsiglue.h"
82aa0387 32
5e61aede
HG
33#define MAX_CMNDS 256
34
115bb1ff
MW
35/*
36 * The r00-r01c specs define this version of the SENSE IU data structure.
37 * It's still in use by several different firmware releases.
38 */
39struct sense_iu_old {
40 __u8 iu_id;
41 __u8 rsvd1;
42 __be16 tag;
43 __be16 len;
44 __u8 status;
45 __u8 service_response;
46 __u8 sense[SCSI_SENSE_BUFFERSIZE];
47};
48
115bb1ff
MW
49struct uas_dev_info {
50 struct usb_interface *intf;
51 struct usb_device *udev;
a0e39e34 52 struct usb_anchor cmd_urbs;
bdd000fb
GH
53 struct usb_anchor sense_urbs;
54 struct usb_anchor data_urbs;
59307852 55 unsigned long flags;
023b515e 56 int qdepth, resetting;
115bb1ff
MW
57 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
58 unsigned use_streams:1;
59 unsigned uas_sense_old:1;
da65c2bb 60 unsigned shutdown:1;
5e61aede 61 struct scsi_cmnd *cmnd[MAX_CMNDS];
e0648520 62 spinlock_t lock;
1bf8198e 63 struct work_struct work;
61c09ce5 64 struct list_head inflight_list;
326349f8 65 struct list_head dead_list;
115bb1ff
MW
66};
67
68enum {
92a3f767 69 SUBMIT_STATUS_URB = (1 << 1),
115bb1ff
MW
70 ALLOC_DATA_IN_URB = (1 << 2),
71 SUBMIT_DATA_IN_URB = (1 << 3),
72 ALLOC_DATA_OUT_URB = (1 << 4),
73 SUBMIT_DATA_OUT_URB = (1 << 5),
74 ALLOC_CMD_URB = (1 << 6),
75 SUBMIT_CMD_URB = (1 << 7),
b1d67693
GH
76 COMMAND_INFLIGHT = (1 << 8),
77 DATA_IN_URB_INFLIGHT = (1 << 9),
78 DATA_OUT_URB_INFLIGHT = (1 << 10),
79 COMMAND_COMPLETED = (1 << 11),
ef018cc9 80 COMMAND_ABORTED = (1 << 12),
b06e48af 81 UNLINK_DATA_URBS = (1 << 13),
efefecf3 82 IS_IN_WORK_LIST = (1 << 14),
115bb1ff
MW
83};
84
85/* Overrides scsi_pointer */
86struct uas_cmd_info {
87 unsigned int state;
88 unsigned int stream;
89 struct urb *cmd_urb;
115bb1ff
MW
90 struct urb *data_in_urb;
91 struct urb *data_out_urb;
040d1a8f 92 struct list_head list;
115bb1ff
MW
93};
94
95/* I hate forward declarations, but I actually have a loop */
96static int uas_submit_urbs(struct scsi_cmnd *cmnd,
97 struct uas_dev_info *devinfo, gfp_t gfp);
ea9da1c7 98static void uas_do_work(struct work_struct *work);
4c456971 99static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
d89bd835 100static void uas_free_streams(struct uas_dev_info *devinfo);
326349f8 101static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller);
115bb1ff 102
7e50e0be 103/* Must be called with devinfo->lock held, will temporary unlock the lock */
aa8f6123 104static void uas_unlink_data_urbs(struct uas_dev_info *devinfo,
7e50e0be
HG
105 struct uas_cmd_info *cmdinfo,
106 unsigned long *lock_flags)
aa8f6123 107{
b06e48af
GH
108 /*
109 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
110 * (called by urb completion) doesn't release cmdinfo
111 * underneath us.
112 */
b06e48af 113 cmdinfo->state |= UNLINK_DATA_URBS;
7e50e0be 114 spin_unlock_irqrestore(&devinfo->lock, *lock_flags);
b06e48af 115
aa8f6123
GH
116 if (cmdinfo->data_in_urb)
117 usb_unlink_urb(cmdinfo->data_in_urb);
118 if (cmdinfo->data_out_urb)
119 usb_unlink_urb(cmdinfo->data_out_urb);
b06e48af 120
7e50e0be 121 spin_lock_irqsave(&devinfo->lock, *lock_flags);
b06e48af 122 cmdinfo->state &= ~UNLINK_DATA_URBS;
aa8f6123
GH
123}
124
115bb1ff
MW
125static void uas_do_work(struct work_struct *work)
126{
1bf8198e
GH
127 struct uas_dev_info *devinfo =
128 container_of(work, struct uas_dev_info, work);
115bb1ff 129 struct uas_cmd_info *cmdinfo;
e0648520 130 unsigned long flags;
ea9da1c7 131 int err;
115bb1ff 132
1bf8198e 133 spin_lock_irqsave(&devinfo->lock, flags);
b7b5d11f
HG
134
135 if (devinfo->resetting)
136 goto out;
137
040d1a8f 138 list_for_each_entry(cmdinfo, &devinfo->inflight_list, list) {
115bb1ff 139 struct scsi_pointer *scp = (void *)cmdinfo;
1bf8198e
GH
140 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
141 SCp);
61c09ce5
HG
142
143 if (!(cmdinfo->state & IS_IN_WORK_LIST))
144 continue;
145
e7eda932 146 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
61c09ce5 147 if (!err)
efefecf3 148 cmdinfo->state &= ~IS_IN_WORK_LIST;
61c09ce5 149 else
1bf8198e 150 schedule_work(&devinfo->work);
115bb1ff 151 }
b7b5d11f 152out:
1bf8198e 153 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff
MW
154}
155
da3033ea 156static void uas_mark_cmd_dead(struct uas_dev_info *devinfo,
673331c8
HG
157 struct uas_cmd_info *cmdinfo,
158 int result, const char *caller)
da3033ea
HG
159{
160 struct scsi_pointer *scp = (void *)cmdinfo;
161 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
162
163 uas_log_cmd_state(cmnd, caller);
ab945eff 164 lockdep_assert_held(&devinfo->lock);
da3033ea
HG
165 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
166 cmdinfo->state |= COMMAND_ABORTED;
167 cmdinfo->state &= ~IS_IN_WORK_LIST;
673331c8 168 cmnd->result = result << 16;
040d1a8f 169 list_move_tail(&cmdinfo->list, &devinfo->dead_list);
da3033ea
HG
170}
171
673331c8
HG
172static void uas_abort_inflight(struct uas_dev_info *devinfo, int result,
173 const char *caller)
4c456971
GH
174{
175 struct uas_cmd_info *cmdinfo;
176 struct uas_cmd_info *temp;
4c456971
GH
177 unsigned long flags;
178
4c456971 179 spin_lock_irqsave(&devinfo->lock, flags);
040d1a8f 180 list_for_each_entry_safe(cmdinfo, temp, &devinfo->inflight_list, list)
673331c8 181 uas_mark_cmd_dead(devinfo, cmdinfo, result, caller);
4c456971
GH
182 spin_unlock_irqrestore(&devinfo->lock, flags);
183}
184
1bf8198e
GH
185static void uas_add_work(struct uas_cmd_info *cmdinfo)
186{
187 struct scsi_pointer *scp = (void *)cmdinfo;
188 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
189 struct uas_dev_info *devinfo = cmnd->device->hostdata;
190
ab945eff 191 lockdep_assert_held(&devinfo->lock);
1bf8198e
GH
192 cmdinfo->state |= IS_IN_WORK_LIST;
193 schedule_work(&devinfo->work);
194}
195
326349f8
GH
196static void uas_zap_dead(struct uas_dev_info *devinfo)
197{
198 struct uas_cmd_info *cmdinfo;
199 struct uas_cmd_info *temp;
200 unsigned long flags;
201
202 spin_lock_irqsave(&devinfo->lock, flags);
040d1a8f 203 list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, list) {
326349f8
GH
204 struct scsi_pointer *scp = (void *)cmdinfo;
205 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
206 SCp);
207 uas_log_cmd_state(cmnd, __func__);
f491ecbb 208 WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED));
326349f8
GH
209 /* all urbs are killed, clear inflight bits */
210 cmdinfo->state &= ~(COMMAND_INFLIGHT |
211 DATA_IN_URB_INFLIGHT |
212 DATA_OUT_URB_INFLIGHT);
213 uas_try_complete(cmnd, __func__);
214 }
215 spin_unlock_irqrestore(&devinfo->lock, flags);
216}
217
115bb1ff
MW
218static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
219{
220 struct sense_iu *sense_iu = urb->transfer_buffer;
221 struct scsi_device *sdev = cmnd->device;
222
223 if (urb->actual_length > 16) {
224 unsigned len = be16_to_cpup(&sense_iu->len);
225 if (len + 16 != urb->actual_length) {
226 int newlen = min(len + 16, urb->actual_length) - 16;
227 if (newlen < 0)
228 newlen = 0;
229 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
230 "disagrees with IU sense data length %d, "
231 "using %d bytes of sense data\n", __func__,
232 urb->actual_length, len, newlen);
233 len = newlen;
234 }
235 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
236 }
237
238 cmnd->result = sense_iu->status;
115bb1ff
MW
239}
240
241static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
242{
243 struct sense_iu_old *sense_iu = urb->transfer_buffer;
244 struct scsi_device *sdev = cmnd->device;
245
246 if (urb->actual_length > 8) {
247 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
248 if (len + 8 != urb->actual_length) {
249 int newlen = min(len + 8, urb->actual_length) - 8;
250 if (newlen < 0)
251 newlen = 0;
252 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
253 "disagrees with IU sense data length %d, "
254 "using %d bytes of sense data\n", __func__,
255 urb->actual_length, len, newlen);
256 len = newlen;
257 }
258 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
259 }
260
261 cmnd->result = sense_iu->status;
b1d67693
GH
262}
263
e0620001
HG
264/*
265 * scsi-tags go from 0 - (nr_tags - 1), uas tags need to match stream-ids,
266 * which go from 1 - nr_streams. And we use 1 for untagged commands.
267 */
268static int uas_get_tag(struct scsi_cmnd *cmnd)
269{
270 int tag;
271
272 if (blk_rq_tagged(cmnd->request))
273 tag = cmnd->request->tag + 2;
274 else
275 tag = 1;
276
277 return tag;
278}
279
b1d67693
GH
280static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
281{
282 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
283
284 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
efefecf3 285 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
e0620001 286 caller, cmnd, uas_get_tag(cmnd),
b1d67693
GH
287 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
288 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
289 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
290 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
291 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
292 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
293 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
294 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
295 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
296 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
ef018cc9 297 (ci->state & COMMAND_COMPLETED) ? " done" : "",
b06e48af 298 (ci->state & COMMAND_ABORTED) ? " abort" : "",
efefecf3
GH
299 (ci->state & UNLINK_DATA_URBS) ? " unlink": "",
300 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
b1d67693
GH
301}
302
303static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
304{
305 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
e0648520 306 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
b1d67693 307
ab945eff 308 lockdep_assert_held(&devinfo->lock);
b1d67693
GH
309 if (cmdinfo->state & (COMMAND_INFLIGHT |
310 DATA_IN_URB_INFLIGHT |
b06e48af
GH
311 DATA_OUT_URB_INFLIGHT |
312 UNLINK_DATA_URBS))
b1d67693 313 return -EBUSY;
f491ecbb 314 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
b1d67693
GH
315 cmdinfo->state |= COMMAND_COMPLETED;
316 usb_free_urb(cmdinfo->data_in_urb);
317 usb_free_urb(cmdinfo->data_out_urb);
673331c8 318 if (cmdinfo->state & COMMAND_ABORTED)
0871d7d8 319 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
040d1a8f 320 list_del(&cmdinfo->list);
5e61aede 321 devinfo->cmnd[uas_get_tag(cmnd) - 1] = NULL;
c621a81e 322 cmnd->scsi_done(cmnd);
b1d67693 323 return 0;
115bb1ff
MW
324}
325
326static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
b1d67693 327 unsigned direction)
115bb1ff
MW
328{
329 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
330 int err;
331
b1d67693 332 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
115bb1ff
MW
333 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
334 if (err) {
1bf8198e 335 uas_add_work(cmdinfo);
115bb1ff
MW
336 }
337}
338
339static void uas_stat_cmplt(struct urb *urb)
340{
341 struct iu *iu = urb->transfer_buffer;
22188f4a 342 struct Scsi_Host *shost = urb->context;
21fc05b6 343 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
115bb1ff 344 struct scsi_cmnd *cmnd;
b1d67693 345 struct uas_cmd_info *cmdinfo;
e0648520 346 unsigned long flags;
5e61aede 347 unsigned int idx;
115bb1ff 348
b7b5d11f
HG
349 spin_lock_irqsave(&devinfo->lock, flags);
350
351 if (devinfo->resetting)
352 goto out;
353
115bb1ff 354 if (urb->status) {
326349f8
GH
355 if (urb->status == -ENOENT) {
356 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
357 urb->stream_id);
358 } else {
359 dev_err(&urb->dev->dev, "stat urb: status %d\n",
360 urb->status);
361 }
b7b5d11f 362 goto out;
023b515e
GH
363 }
364
5e61aede
HG
365 idx = be16_to_cpup(&iu->tag) - 1;
366 if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
367 dev_err(&urb->dev->dev,
368 "stat urb: no pending cmd for tag %d\n", idx + 1);
b7b5d11f 369 goto out;
5e61aede 370 }
115bb1ff 371
5e61aede 372 cmnd = devinfo->cmnd[idx];
e0423dee 373 cmdinfo = (void *)&cmnd->SCp;
115bb1ff
MW
374 switch (iu->iu_id) {
375 case IU_ID_STATUS:
376 if (urb->actual_length < 16)
377 devinfo->uas_sense_old = 1;
378 if (devinfo->uas_sense_old)
379 uas_sense_old(urb, cmnd);
380 else
381 uas_sense(urb, cmnd);
8aac863e
GH
382 if (cmnd->result != 0) {
383 /* cancel data transfers on error */
7e50e0be 384 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
8aac863e 385 }
b1d67693
GH
386 cmdinfo->state &= ~COMMAND_INFLIGHT;
387 uas_try_complete(cmnd, __func__);
115bb1ff
MW
388 break;
389 case IU_ID_READ_READY:
8e453155
HG
390 if (!cmdinfo->data_in_urb ||
391 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
392 scmd_printk(KERN_ERR, cmnd, "unexpected read rdy\n");
393 break;
394 }
115bb1ff
MW
395 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
396 break;
397 case IU_ID_WRITE_READY:
8e453155
HG
398 if (!cmdinfo->data_out_urb ||
399 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
400 scmd_printk(KERN_ERR, cmnd, "unexpected write rdy\n");
401 break;
402 }
115bb1ff
MW
403 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
404 break;
405 default:
406 scmd_printk(KERN_ERR, cmnd,
407 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
408 }
b7b5d11f 409out:
e9bd7e1a 410 usb_free_urb(urb);
e0648520 411 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff
MW
412}
413
c621a81e 414static void uas_data_cmplt(struct urb *urb)
115bb1ff 415{
b1d67693
GH
416 struct scsi_cmnd *cmnd = urb->context;
417 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
e0648520 418 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
b1d67693 419 struct scsi_data_buffer *sdb = NULL;
e0648520 420 unsigned long flags;
b1d67693 421
e0648520 422 spin_lock_irqsave(&devinfo->lock, flags);
b7b5d11f 423
b1d67693
GH
424 if (cmdinfo->data_in_urb == urb) {
425 sdb = scsi_in(cmnd);
426 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
427 } else if (cmdinfo->data_out_urb == urb) {
428 sdb = scsi_out(cmnd);
429 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
430 }
f491ecbb
GH
431 if (sdb == NULL) {
432 WARN_ON_ONCE(1);
b7b5d11f
HG
433 goto out;
434 }
435
436 if (devinfo->resetting)
437 goto out;
438
439 if (urb->status) {
876285cc
HG
440 if (urb->status != -ECONNRESET) {
441 uas_log_cmd_state(cmnd, __func__);
442 scmd_printk(KERN_ERR, cmnd,
443 "data cmplt err %d stream %d\n",
444 urb->status, urb->stream_id);
445 }
8aac863e
GH
446 /* error: no data transfered */
447 sdb->resid = sdb->length;
448 } else {
449 sdb->resid = sdb->length - urb->actual_length;
450 }
b1d67693 451 uas_try_complete(cmnd, __func__);
b7b5d11f 452out:
e0648520 453 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff
MW
454}
455
876285cc
HG
456static void uas_cmd_cmplt(struct urb *urb)
457{
458 struct scsi_cmnd *cmnd = urb->context;
459
460 if (urb->status) {
461 uas_log_cmd_state(cmnd, __func__);
462 scmd_printk(KERN_ERR, cmnd, "cmd cmplt err %d\n", urb->status);
463 }
464 usb_free_urb(urb);
465}
466
115bb1ff 467static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
b1d67693
GH
468 unsigned int pipe, u16 stream_id,
469 struct scsi_cmnd *cmnd,
470 enum dma_data_direction dir)
115bb1ff
MW
471{
472 struct usb_device *udev = devinfo->udev;
473 struct urb *urb = usb_alloc_urb(0, gfp);
b1d67693
GH
474 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
475 ? scsi_in(cmnd) : scsi_out(cmnd);
115bb1ff
MW
476
477 if (!urb)
478 goto out;
b1d67693
GH
479 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
480 uas_data_cmplt, cmnd);
c6d4579d 481 urb->stream_id = stream_id;
115bb1ff
MW
482 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
483 urb->sg = sdb->table.sgl;
484 out:
485 return urb;
486}
487
488static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
e9bd7e1a 489 struct Scsi_Host *shost, u16 stream_id)
115bb1ff
MW
490{
491 struct usb_device *udev = devinfo->udev;
492 struct urb *urb = usb_alloc_urb(0, gfp);
493 struct sense_iu *iu;
494
495 if (!urb)
496 goto out;
497
ac563cfd 498 iu = kzalloc(sizeof(*iu), gfp);
115bb1ff
MW
499 if (!iu)
500 goto free;
501
502 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
e9bd7e1a 503 uas_stat_cmplt, shost);
115bb1ff
MW
504 urb->stream_id = stream_id;
505 urb->transfer_flags |= URB_FREE_BUFFER;
506 out:
507 return urb;
508 free:
509 usb_free_urb(urb);
510 return NULL;
511}
512
513static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
a887cd36 514 struct scsi_cmnd *cmnd)
115bb1ff
MW
515{
516 struct usb_device *udev = devinfo->udev;
517 struct scsi_device *sdev = cmnd->device;
518 struct urb *urb = usb_alloc_urb(0, gfp);
519 struct command_iu *iu;
520 int len;
521
522 if (!urb)
523 goto out;
524
525 len = cmnd->cmd_len - 16;
526 if (len < 0)
527 len = 0;
528 len = ALIGN(len, 4);
ac563cfd 529 iu = kzalloc(sizeof(*iu) + len, gfp);
115bb1ff
MW
530 if (!iu)
531 goto free;
532
533 iu->iu_id = IU_ID_COMMAND;
e0620001 534 iu->tag = cpu_to_be16(uas_get_tag(cmnd));
02e031cb 535 iu->prio_attr = UAS_SIMPLE_TAG;
115bb1ff
MW
536 iu->len = len;
537 int_to_scsilun(sdev->lun, &iu->lun);
538 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
539
540 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
876285cc 541 uas_cmd_cmplt, cmnd);
115bb1ff
MW
542 urb->transfer_flags |= URB_FREE_BUFFER;
543 out:
544 return urb;
545 free:
546 usb_free_urb(urb);
547 return NULL;
548}
549
550/*
551 * Why should I request the Status IU before sending the Command IU? Spec
552 * says to, but also says the device may receive them in any order. Seems
553 * daft to me.
554 */
555
876285cc 556static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd,
70cf0fba 557 gfp_t gfp, unsigned int stream)
115bb1ff 558{
876285cc 559 struct Scsi_Host *shost = cmnd->device->host;
21fc05b6 560 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
e9bd7e1a 561 struct urb *urb;
876285cc 562 int err;
115bb1ff 563
e9bd7e1a
GH
564 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
565 if (!urb)
70cf0fba 566 return NULL;
d5f808d3 567 usb_anchor_urb(urb, &devinfo->sense_urbs);
876285cc
HG
568 err = usb_submit_urb(urb, gfp);
569 if (err) {
d5f808d3 570 usb_unanchor_urb(urb);
876285cc 571 uas_log_cmd_state(cmnd, __func__);
e9bd7e1a 572 shost_printk(KERN_INFO, shost,
876285cc
HG
573 "sense urb submission error %d stream %d\n",
574 err, stream);
e9bd7e1a 575 usb_free_urb(urb);
70cf0fba 576 return NULL;
115bb1ff 577 }
70cf0fba 578 return urb;
e9bd7e1a
GH
579}
580
581static int uas_submit_urbs(struct scsi_cmnd *cmnd,
582 struct uas_dev_info *devinfo, gfp_t gfp)
583{
584 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
70cf0fba 585 struct urb *urb;
876285cc 586 int err;
115bb1ff 587
ab945eff 588 lockdep_assert_held(&devinfo->lock);
92a3f767 589 if (cmdinfo->state & SUBMIT_STATUS_URB) {
876285cc 590 urb = uas_submit_sense_urb(cmnd, gfp, cmdinfo->stream);
70cf0fba
HG
591 if (!urb)
592 return SCSI_MLQUEUE_DEVICE_BUSY;
92a3f767 593 cmdinfo->state &= ~SUBMIT_STATUS_URB;
115bb1ff
MW
594 }
595
596 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
597 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
c621a81e 598 devinfo->data_in_pipe, cmdinfo->stream,
b1d67693 599 cmnd, DMA_FROM_DEVICE);
115bb1ff
MW
600 if (!cmdinfo->data_in_urb)
601 return SCSI_MLQUEUE_DEVICE_BUSY;
602 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
603 }
604
605 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
d5f808d3 606 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
876285cc
HG
607 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
608 if (err) {
d5f808d3 609 usb_unanchor_urb(cmdinfo->data_in_urb);
876285cc 610 uas_log_cmd_state(cmnd, __func__);
115bb1ff 611 scmd_printk(KERN_INFO, cmnd,
876285cc
HG
612 "data in urb submission error %d stream %d\n",
613 err, cmdinfo->data_in_urb->stream_id);
115bb1ff
MW
614 return SCSI_MLQUEUE_DEVICE_BUSY;
615 }
616 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
b1d67693 617 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
115bb1ff
MW
618 }
619
620 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
621 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
c621a81e 622 devinfo->data_out_pipe, cmdinfo->stream,
b1d67693 623 cmnd, DMA_TO_DEVICE);
115bb1ff
MW
624 if (!cmdinfo->data_out_urb)
625 return SCSI_MLQUEUE_DEVICE_BUSY;
626 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
627 }
628
629 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
d5f808d3 630 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
876285cc
HG
631 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
632 if (err) {
d5f808d3 633 usb_unanchor_urb(cmdinfo->data_out_urb);
876285cc 634 uas_log_cmd_state(cmnd, __func__);
115bb1ff 635 scmd_printk(KERN_INFO, cmnd,
876285cc
HG
636 "data out urb submission error %d stream %d\n",
637 err, cmdinfo->data_out_urb->stream_id);
115bb1ff
MW
638 return SCSI_MLQUEUE_DEVICE_BUSY;
639 }
640 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
b1d67693 641 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
115bb1ff
MW
642 }
643
644 if (cmdinfo->state & ALLOC_CMD_URB) {
a887cd36 645 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
115bb1ff
MW
646 if (!cmdinfo->cmd_urb)
647 return SCSI_MLQUEUE_DEVICE_BUSY;
648 cmdinfo->state &= ~ALLOC_CMD_URB;
649 }
650
651 if (cmdinfo->state & SUBMIT_CMD_URB) {
d5f808d3 652 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
876285cc
HG
653 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
654 if (err) {
d5f808d3 655 usb_unanchor_urb(cmdinfo->cmd_urb);
876285cc 656 uas_log_cmd_state(cmnd, __func__);
115bb1ff 657 scmd_printk(KERN_INFO, cmnd,
876285cc 658 "cmd urb submission error %d\n", err);
115bb1ff
MW
659 return SCSI_MLQUEUE_DEVICE_BUSY;
660 }
a0e39e34 661 cmdinfo->cmd_urb = NULL;
115bb1ff 662 cmdinfo->state &= ~SUBMIT_CMD_URB;
b1d67693 663 cmdinfo->state |= COMMAND_INFLIGHT;
115bb1ff
MW
664 }
665
666 return 0;
667}
668
f281233d 669static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
115bb1ff
MW
670 void (*done)(struct scsi_cmnd *))
671{
672 struct scsi_device *sdev = cmnd->device;
673 struct uas_dev_info *devinfo = sdev->hostdata;
674 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
e0648520 675 unsigned long flags;
5e61aede 676 unsigned int stream;
115bb1ff
MW
677 int err;
678
679 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
680
59307852
HG
681 if ((devinfo->flags & US_FL_NO_ATA_1X) &&
682 (cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) {
683 memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB,
684 sizeof(usb_stor_sense_invalidCDB));
685 cmnd->result = SAM_STAT_CHECK_CONDITION;
686 cmnd->scsi_done(cmnd);
687 return 0;
688 }
689
c6f63207
HG
690 spin_lock_irqsave(&devinfo->lock, flags);
691
f8be6bfc
GH
692 if (devinfo->resetting) {
693 cmnd->result = DID_ERROR << 16;
694 cmnd->scsi_done(cmnd);
c6f63207 695 spin_unlock_irqrestore(&devinfo->lock, flags);
f8be6bfc
GH
696 return 0;
697 }
698
5e61aede
HG
699 stream = uas_get_tag(cmnd);
700 if (devinfo->cmnd[stream - 1]) {
e0648520 701 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff 702 return SCSI_MLQUEUE_DEVICE_BUSY;
e0648520 703 }
115bb1ff 704
115bb1ff
MW
705 cmnd->scsi_done = done;
706
5e61aede
HG
707 memset(cmdinfo, 0, sizeof(*cmdinfo));
708 cmdinfo->stream = stream;
e0620001 709 cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
115bb1ff
MW
710
711 switch (cmnd->sc_data_direction) {
712 case DMA_FROM_DEVICE:
713 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
714 break;
715 case DMA_BIDIRECTIONAL:
716 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
717 case DMA_TO_DEVICE:
718 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
719 case DMA_NONE:
720 break;
721 }
722
723 if (!devinfo->use_streams) {
db32de11 724 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
115bb1ff
MW
725 cmdinfo->stream = 0;
726 }
727
728 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
729 if (err) {
730 /* If we did nothing, give up now */
92a3f767 731 if (cmdinfo->state & SUBMIT_STATUS_URB) {
e0648520 732 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff
MW
733 return SCSI_MLQUEUE_DEVICE_BUSY;
734 }
1bf8198e 735 uas_add_work(cmdinfo);
115bb1ff
MW
736 }
737
5e61aede 738 devinfo->cmnd[stream - 1] = cmnd;
040d1a8f 739 list_add_tail(&cmdinfo->list, &devinfo->inflight_list);
e0648520 740 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff
MW
741 return 0;
742}
743
f281233d
JG
744static DEF_SCSI_QCMD(uas_queuecommand)
745
115bb1ff
MW
746static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
747{
748 struct scsi_device *sdev = cmnd->device;
749 struct uas_dev_info *devinfo = sdev->hostdata;
750 struct usb_device *udev = devinfo->udev;
b7b5d11f 751 unsigned long flags;
023b515e 752 int err;
115bb1ff 753
be326f4c
HG
754 err = usb_lock_device_for_reset(udev, devinfo->intf);
755 if (err) {
756 shost_printk(KERN_ERR, sdev->host,
757 "%s FAILED to get lock err %d\n", __func__, err);
758 return FAILED;
759 }
760
326349f8 761 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
b7b5d11f
HG
762
763 spin_lock_irqsave(&devinfo->lock, flags);
023b515e 764 devinfo->resetting = 1;
b7b5d11f
HG
765 spin_unlock_irqrestore(&devinfo->lock, flags);
766
673331c8 767 uas_abort_inflight(devinfo, DID_RESET, __func__);
a0e39e34 768 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
023b515e
GH
769 usb_kill_anchored_urbs(&devinfo->sense_urbs);
770 usb_kill_anchored_urbs(&devinfo->data_urbs);
326349f8 771 uas_zap_dead(devinfo);
023b515e 772 err = usb_reset_device(udev);
b7b5d11f
HG
773
774 spin_lock_irqsave(&devinfo->lock, flags);
023b515e 775 devinfo->resetting = 0;
b7b5d11f 776 spin_unlock_irqrestore(&devinfo->lock, flags);
115bb1ff 777
be326f4c
HG
778 usb_unlock_device(udev);
779
023b515e
GH
780 if (err) {
781 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
782 return FAILED;
783 }
115bb1ff 784
023b515e
GH
785 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
786 return SUCCESS;
115bb1ff
MW
787}
788
789static int uas_slave_alloc(struct scsi_device *sdev)
790{
21fc05b6 791 sdev->hostdata = (void *)sdev->host->hostdata;
37599f96
HG
792
793 /* USB has unusual DMA-alignment requirements: Although the
794 * starting address of each scatter-gather element doesn't matter,
795 * the length of each element except the last must be divisible
796 * by the Bulk maxpacket value. There's currently no way to
797 * express this by block-layer constraints, so we'll cop out
798 * and simply require addresses to be aligned at 512-byte
799 * boundaries. This is okay since most block I/O involves
800 * hardware sectors that are multiples of 512 bytes in length,
801 * and since host controllers up through USB 2.0 have maxpacket
802 * values no larger than 512.
803 *
804 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
805 * values can be as large as 2048. To make that work properly
806 * will require changes to the block layer.
807 */
808 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
809
115bb1ff
MW
810 return 0;
811}
812
813static int uas_slave_configure(struct scsi_device *sdev)
814{
815 struct uas_dev_info *devinfo = sdev->hostdata;
734016b0
HG
816
817 if (devinfo->flags & US_FL_NO_REPORT_OPCODES)
818 sdev->no_report_opcodes = 1;
819
115bb1ff 820 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
d3f7c156 821 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
115bb1ff
MW
822 return 0;
823}
824
825static struct scsi_host_template uas_host_template = {
826 .module = THIS_MODULE,
827 .name = "uas",
828 .queuecommand = uas_queuecommand,
829 .slave_alloc = uas_slave_alloc,
830 .slave_configure = uas_slave_configure,
115bb1ff
MW
831 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
832 .can_queue = 65536, /* Is there a limit on the _host_ ? */
833 .this_id = -1,
834 .sg_tablesize = SG_NONE,
835 .cmd_per_lun = 1, /* until we override it */
836 .skip_settle_delay = 1,
837 .ordered_tag = 1,
838};
839
79b4c061
HG
840#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
841 vendorName, productName, useProtocol, useTransport, \
842 initFunction, flags) \
843{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
844 .driver_info = (flags) }
845
115bb1ff 846static struct usb_device_id uas_usb_ids[] = {
79b4c061 847# include "unusual_uas.h"
115bb1ff
MW
848 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
849 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
850 /* 0xaa is a prototype device I happen to have access to */
851 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
852 { }
853};
854MODULE_DEVICE_TABLE(usb, uas_usb_ids);
855
79b4c061
HG
856#undef UNUSUAL_DEV
857
e1be067b
HG
858static int uas_switch_interface(struct usb_device *udev,
859 struct usb_interface *intf)
860{
861 int alt;
862
863 alt = uas_find_uas_alt_setting(intf);
864 if (alt < 0)
865 return alt;
866
867 return usb_set_interface(udev,
868 intf->altsetting[0].desc.bInterfaceNumber, alt);
869}
870
58d51444 871static int uas_configure_endpoints(struct uas_dev_info *devinfo)
34f11e59
HG
872{
873 struct usb_host_endpoint *eps[4] = { };
874 struct usb_device *udev = devinfo->udev;
875 int r;
876
877 devinfo->uas_sense_old = 0;
34f11e59
HG
878
879 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
74d71aec
HG
880 if (r)
881 return r;
882
883 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
884 usb_endpoint_num(&eps[0]->desc));
885 devinfo->status_pipe = usb_rcvbulkpipe(udev,
886 usb_endpoint_num(&eps[1]->desc));
887 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
888 usb_endpoint_num(&eps[2]->desc));
889 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
890 usb_endpoint_num(&eps[3]->desc));
115bb1ff 891
58d51444 892 if (udev->speed != USB_SPEED_SUPER) {
e2875c33 893 devinfo->qdepth = 32;
115bb1ff
MW
894 devinfo->use_streams = 0;
895 } else {
58d51444 896 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
5e61aede 897 3, MAX_CMNDS, GFP_NOIO);
58d51444
HG
898 if (devinfo->qdepth < 0)
899 return devinfo->qdepth;
115bb1ff
MW
900 devinfo->use_streams = 1;
901 }
58d51444
HG
902
903 return 0;
115bb1ff
MW
904}
905
dae51546
SAS
906static void uas_free_streams(struct uas_dev_info *devinfo)
907{
908 struct usb_device *udev = devinfo->udev;
909 struct usb_host_endpoint *eps[3];
910
911 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
912 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
913 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
94d72f00 914 usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
dae51546
SAS
915}
916
115bb1ff
MW
917static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
918{
6ce8213b
HG
919 int result = -ENOMEM;
920 struct Scsi_Host *shost = NULL;
115bb1ff
MW
921 struct uas_dev_info *devinfo;
922 struct usb_device *udev = interface_to_usbdev(intf);
923
79b4c061
HG
924 if (!uas_use_uas_driver(intf, id))
925 return -ENODEV;
926
89dc2905
MW
927 if (uas_switch_interface(udev, intf))
928 return -ENODEV;
115bb1ff 929
21fc05b6
HG
930 shost = scsi_host_alloc(&uas_host_template,
931 sizeof(struct uas_dev_info));
115bb1ff 932 if (!shost)
6ce8213b 933 goto set_alt0;
115bb1ff
MW
934
935 shost->max_cmd_len = 16 + 252;
936 shost->max_id = 1;
bde027b4
GH
937 shost->max_lun = 256;
938 shost->max_channel = 0;
115bb1ff
MW
939 shost->sg_tablesize = udev->bus->sg_tablesize;
940
21fc05b6 941 devinfo = (struct uas_dev_info *)shost->hostdata;
115bb1ff
MW
942 devinfo->intf = intf;
943 devinfo->udev = udev;
023b515e 944 devinfo->resetting = 0;
da65c2bb 945 devinfo->shutdown = 0;
59307852
HG
946 devinfo->flags = id->driver_info;
947 usb_stor_adjust_quirks(udev, &devinfo->flags);
a0e39e34 948 init_usb_anchor(&devinfo->cmd_urbs);
bdd000fb
GH
949 init_usb_anchor(&devinfo->sense_urbs);
950 init_usb_anchor(&devinfo->data_urbs);
e0648520 951 spin_lock_init(&devinfo->lock);
1bf8198e 952 INIT_WORK(&devinfo->work, uas_do_work);
61c09ce5 953 INIT_LIST_HEAD(&devinfo->inflight_list);
326349f8 954 INIT_LIST_HEAD(&devinfo->dead_list);
58d51444
HG
955
956 result = uas_configure_endpoints(devinfo);
957 if (result)
958 goto set_alt0;
115bb1ff 959
d3f7c156 960 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
115bb1ff 961 if (result)
6ce8213b 962 goto free_streams;
dae51546 963
c637f1fa 964 usb_set_intfdata(intf, shost);
dae51546
SAS
965 result = scsi_add_host(shost, &intf->dev);
966 if (result)
6ce8213b 967 goto free_streams;
dae51546 968
115bb1ff 969 scsi_scan_host(shost);
115bb1ff 970 return result;
dae51546 971
6ce8213b 972free_streams:
dae51546 973 uas_free_streams(devinfo);
c637f1fa 974 usb_set_intfdata(intf, NULL);
6ce8213b
HG
975set_alt0:
976 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
115bb1ff
MW
977 if (shost)
978 scsi_host_put(shost);
979 return result;
980}
981
982static int uas_pre_reset(struct usb_interface *intf)
983{
4de7a373 984 struct Scsi_Host *shost = usb_get_intfdata(intf);
21fc05b6 985 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
4de7a373
HG
986 unsigned long flags;
987
da65c2bb
HG
988 if (devinfo->shutdown)
989 return 0;
990
4de7a373
HG
991 /* Block new requests */
992 spin_lock_irqsave(shost->host_lock, flags);
993 scsi_block_requests(shost);
994 spin_unlock_irqrestore(shost->host_lock, flags);
995
996 /* Wait for any pending requests to complete */
997 flush_work(&devinfo->work);
998 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
999 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1000 return 1;
1001 }
1002
1003 uas_free_streams(devinfo);
1004
115bb1ff
MW
1005 return 0;
1006}
1007
1008static int uas_post_reset(struct usb_interface *intf)
1009{
4de7a373 1010 struct Scsi_Host *shost = usb_get_intfdata(intf);
21fc05b6 1011 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
4de7a373
HG
1012 unsigned long flags;
1013
da65c2bb
HG
1014 if (devinfo->shutdown)
1015 return 0;
1016
58d51444
HG
1017 if (uas_configure_endpoints(devinfo) != 0) {
1018 shost_printk(KERN_ERR, shost,
1019 "%s: alloc streams error after reset", __func__);
1020 return 1;
1021 }
4de7a373
HG
1022
1023 spin_lock_irqsave(shost->host_lock, flags);
1024 scsi_report_bus_reset(shost, 0);
1025 spin_unlock_irqrestore(shost->host_lock, flags);
1026
1027 scsi_unblock_requests(shost);
1028
115bb1ff
MW
1029 return 0;
1030}
1031
0df1f663
HG
1032static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1033{
1034 struct Scsi_Host *shost = usb_get_intfdata(intf);
21fc05b6 1035 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
0df1f663
HG
1036
1037 /* Wait for any pending requests to complete */
1038 flush_work(&devinfo->work);
1039 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1040 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1041 return -ETIME;
1042 }
1043
1044 return 0;
1045}
1046
1047static int uas_resume(struct usb_interface *intf)
1048{
1049 return 0;
1050}
1051
1052static int uas_reset_resume(struct usb_interface *intf)
1053{
1054 struct Scsi_Host *shost = usb_get_intfdata(intf);
21fc05b6 1055 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
0df1f663
HG
1056 unsigned long flags;
1057
1058 if (uas_configure_endpoints(devinfo) != 0) {
1059 shost_printk(KERN_ERR, shost,
1060 "%s: alloc streams error after reset", __func__);
1061 return -EIO;
1062 }
1063
1064 spin_lock_irqsave(shost->host_lock, flags);
1065 scsi_report_bus_reset(shost, 0);
1066 spin_unlock_irqrestore(shost->host_lock, flags);
1067
1068 return 0;
1069}
1070
115bb1ff
MW
1071static void uas_disconnect(struct usb_interface *intf)
1072{
115bb1ff 1073 struct Scsi_Host *shost = usb_get_intfdata(intf);
21fc05b6 1074 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
b7b5d11f 1075 unsigned long flags;
115bb1ff 1076
b7b5d11f 1077 spin_lock_irqsave(&devinfo->lock, flags);
4c456971 1078 devinfo->resetting = 1;
b7b5d11f
HG
1079 spin_unlock_irqrestore(&devinfo->lock, flags);
1080
1bf8198e 1081 cancel_work_sync(&devinfo->work);
673331c8 1082 uas_abort_inflight(devinfo, DID_NO_CONNECT, __func__);
a0e39e34 1083 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
bdd000fb
GH
1084 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1085 usb_kill_anchored_urbs(&devinfo->data_urbs);
326349f8 1086 uas_zap_dead(devinfo);
4c456971 1087 scsi_remove_host(shost);
dae51546 1088 uas_free_streams(devinfo);
21fc05b6 1089 scsi_host_put(shost);
115bb1ff
MW
1090}
1091
da65c2bb
HG
1092/*
1093 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1094 * hang on reboot when the device is still in uas mode. Note the reset is
1095 * necessary as some devices won't revert to usb-storage mode without it.
1096 */
1097static void uas_shutdown(struct device *dev)
1098{
1099 struct usb_interface *intf = to_usb_interface(dev);
1100 struct usb_device *udev = interface_to_usbdev(intf);
1101 struct Scsi_Host *shost = usb_get_intfdata(intf);
21fc05b6 1102 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
da65c2bb
HG
1103
1104 if (system_state != SYSTEM_RESTART)
1105 return;
1106
1107 devinfo->shutdown = 1;
1108 uas_free_streams(devinfo);
1109 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1110 usb_reset_device(udev);
1111}
1112
115bb1ff
MW
1113static struct usb_driver uas_driver = {
1114 .name = "uas",
1115 .probe = uas_probe,
1116 .disconnect = uas_disconnect,
1117 .pre_reset = uas_pre_reset,
1118 .post_reset = uas_post_reset,
0df1f663
HG
1119 .suspend = uas_suspend,
1120 .resume = uas_resume,
1121 .reset_resume = uas_reset_resume,
da65c2bb 1122 .drvwrap.driver.shutdown = uas_shutdown,
115bb1ff
MW
1123 .id_table = uas_usb_ids,
1124};
1125
65db4305 1126module_usb_driver(uas_driver);
115bb1ff
MW
1127
1128MODULE_LICENSE("GPL");
f50a4968
HG
1129MODULE_AUTHOR(
1130 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");
This page took 0.303421 seconds and 5 git commands to generate.