xref: /openbmc/linux/drivers/usb/storage/uas.c (revision f9834f18)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * USB Attached SCSI
4  * Note that this is not the same as the USB Mass Storage driver
5  *
6  * Copyright Hans de Goede <hdegoede@redhat.com> for Red Hat, Inc. 2013 - 2016
7  * Copyright Matthew Wilcox for Intel Corp, 2010
8  * Copyright Sarah Sharp for Intel Corp, 2010
9  */
10 
11 #include <linux/blkdev.h>
12 #include <linux/slab.h>
13 #include <linux/types.h>
14 #include <linux/module.h>
15 #include <linux/usb.h>
16 #include <linux/usb_usual.h>
17 #include <linux/usb/hcd.h>
18 #include <linux/usb/storage.h>
19 #include <linux/usb/uas.h>
20 
21 #include <scsi/scsi.h>
22 #include <scsi/scsi_eh.h>
23 #include <scsi/scsi_dbg.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_device.h>
26 #include <scsi/scsi_host.h>
27 #include <scsi/scsi_tcq.h>
28 
29 #include "uas-detect.h"
30 #include "scsiglue.h"
31 
32 #define MAX_CMNDS 256
33 
34 struct uas_dev_info {
35 	struct usb_interface *intf;
36 	struct usb_device *udev;
37 	struct usb_anchor cmd_urbs;
38 	struct usb_anchor sense_urbs;
39 	struct usb_anchor data_urbs;
40 	unsigned long flags;
41 	int qdepth, resetting;
42 	unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
43 	unsigned use_streams:1;
44 	unsigned shutdown:1;
45 	struct scsi_cmnd *cmnd[MAX_CMNDS];
46 	spinlock_t lock;
47 	struct work_struct work;
48 	struct work_struct scan_work;      /* for async scanning */
49 };
50 
51 enum {
52 	SUBMIT_STATUS_URB	= BIT(1),
53 	ALLOC_DATA_IN_URB	= BIT(2),
54 	SUBMIT_DATA_IN_URB	= BIT(3),
55 	ALLOC_DATA_OUT_URB	= BIT(4),
56 	SUBMIT_DATA_OUT_URB	= BIT(5),
57 	ALLOC_CMD_URB		= BIT(6),
58 	SUBMIT_CMD_URB		= BIT(7),
59 	COMMAND_INFLIGHT        = BIT(8),
60 	DATA_IN_URB_INFLIGHT    = BIT(9),
61 	DATA_OUT_URB_INFLIGHT   = BIT(10),
62 	COMMAND_ABORTED         = BIT(11),
63 	IS_IN_WORK_LIST         = BIT(12),
64 };
65 
66 /* Overrides scsi_pointer */
67 struct uas_cmd_info {
68 	unsigned int state;
69 	unsigned int uas_tag;
70 	struct urb *cmd_urb;
71 	struct urb *data_in_urb;
72 	struct urb *data_out_urb;
73 };
74 
75 /* I hate forward declarations, but I actually have a loop */
76 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
77 				struct uas_dev_info *devinfo);
78 static void uas_do_work(struct work_struct *work);
79 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
80 static void uas_free_streams(struct uas_dev_info *devinfo);
81 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
82 				int status);
83 
84 static void uas_do_work(struct work_struct *work)
85 {
86 	struct uas_dev_info *devinfo =
87 		container_of(work, struct uas_dev_info, work);
88 	struct uas_cmd_info *cmdinfo;
89 	struct scsi_cmnd *cmnd;
90 	unsigned long flags;
91 	int i, err;
92 
93 	spin_lock_irqsave(&devinfo->lock, flags);
94 
95 	if (devinfo->resetting)
96 		goto out;
97 
98 	for (i = 0; i < devinfo->qdepth; i++) {
99 		if (!devinfo->cmnd[i])
100 			continue;
101 
102 		cmnd = devinfo->cmnd[i];
103 		cmdinfo = (void *)&cmnd->SCp;
104 
105 		if (!(cmdinfo->state & IS_IN_WORK_LIST))
106 			continue;
107 
108 		err = uas_submit_urbs(cmnd, cmnd->device->hostdata);
109 		if (!err)
110 			cmdinfo->state &= ~IS_IN_WORK_LIST;
111 		else
112 			schedule_work(&devinfo->work);
113 	}
114 out:
115 	spin_unlock_irqrestore(&devinfo->lock, flags);
116 }
117 
118 static void uas_scan_work(struct work_struct *work)
119 {
120 	struct uas_dev_info *devinfo =
121 		container_of(work, struct uas_dev_info, scan_work);
122 	struct Scsi_Host *shost = usb_get_intfdata(devinfo->intf);
123 
124 	dev_dbg(&devinfo->intf->dev, "starting scan\n");
125 	scsi_scan_host(shost);
126 	dev_dbg(&devinfo->intf->dev, "scan complete\n");
127 }
128 
129 static void uas_add_work(struct uas_cmd_info *cmdinfo)
130 {
131 	struct scsi_pointer *scp = (void *)cmdinfo;
132 	struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
133 	struct uas_dev_info *devinfo = cmnd->device->hostdata;
134 
135 	lockdep_assert_held(&devinfo->lock);
136 	cmdinfo->state |= IS_IN_WORK_LIST;
137 	schedule_work(&devinfo->work);
138 }
139 
140 static void uas_zap_pending(struct uas_dev_info *devinfo, int result)
141 {
142 	struct uas_cmd_info *cmdinfo;
143 	struct scsi_cmnd *cmnd;
144 	unsigned long flags;
145 	int i, err;
146 
147 	spin_lock_irqsave(&devinfo->lock, flags);
148 	for (i = 0; i < devinfo->qdepth; i++) {
149 		if (!devinfo->cmnd[i])
150 			continue;
151 
152 		cmnd = devinfo->cmnd[i];
153 		cmdinfo = (void *)&cmnd->SCp;
154 		uas_log_cmd_state(cmnd, __func__, 0);
155 		/* Sense urbs were killed, clear COMMAND_INFLIGHT manually */
156 		cmdinfo->state &= ~COMMAND_INFLIGHT;
157 		cmnd->result = result << 16;
158 		err = uas_try_complete(cmnd, __func__);
159 		WARN_ON(err != 0);
160 	}
161 	spin_unlock_irqrestore(&devinfo->lock, flags);
162 }
163 
164 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
165 {
166 	struct sense_iu *sense_iu = urb->transfer_buffer;
167 	struct scsi_device *sdev = cmnd->device;
168 
169 	if (urb->actual_length > 16) {
170 		unsigned len = be16_to_cpup(&sense_iu->len);
171 		if (len + 16 != urb->actual_length) {
172 			int newlen = min(len + 16, urb->actual_length) - 16;
173 			if (newlen < 0)
174 				newlen = 0;
175 			sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
176 				"disagrees with IU sense data length %d, "
177 				"using %d bytes of sense data\n", __func__,
178 					urb->actual_length, len, newlen);
179 			len = newlen;
180 		}
181 		memcpy(cmnd->sense_buffer, sense_iu->sense, len);
182 	}
183 
184 	cmnd->result = sense_iu->status;
185 }
186 
187 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
188 			      int status)
189 {
190 	struct uas_cmd_info *ci = (void *)&cmnd->SCp;
191 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
192 
193 	scmd_printk(KERN_INFO, cmnd,
194 		    "%s %d uas-tag %d inflight:%s%s%s%s%s%s%s%s%s%s%s%s ",
195 		    prefix, status, cmdinfo->uas_tag,
196 		    (ci->state & SUBMIT_STATUS_URB)     ? " s-st"  : "",
197 		    (ci->state & ALLOC_DATA_IN_URB)     ? " a-in"  : "",
198 		    (ci->state & SUBMIT_DATA_IN_URB)    ? " s-in"  : "",
199 		    (ci->state & ALLOC_DATA_OUT_URB)    ? " a-out" : "",
200 		    (ci->state & SUBMIT_DATA_OUT_URB)   ? " s-out" : "",
201 		    (ci->state & ALLOC_CMD_URB)         ? " a-cmd" : "",
202 		    (ci->state & SUBMIT_CMD_URB)        ? " s-cmd" : "",
203 		    (ci->state & COMMAND_INFLIGHT)      ? " CMD"   : "",
204 		    (ci->state & DATA_IN_URB_INFLIGHT)  ? " IN"    : "",
205 		    (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT"   : "",
206 		    (ci->state & COMMAND_ABORTED)       ? " abort" : "",
207 		    (ci->state & IS_IN_WORK_LIST)       ? " work"  : "");
208 	scsi_print_command(cmnd);
209 }
210 
211 static void uas_free_unsubmitted_urbs(struct scsi_cmnd *cmnd)
212 {
213 	struct uas_cmd_info *cmdinfo;
214 
215 	if (!cmnd)
216 		return;
217 
218 	cmdinfo = (void *)&cmnd->SCp;
219 
220 	if (cmdinfo->state & SUBMIT_CMD_URB)
221 		usb_free_urb(cmdinfo->cmd_urb);
222 
223 	/* data urbs may have never gotten their submit flag set */
224 	if (!(cmdinfo->state & DATA_IN_URB_INFLIGHT))
225 		usb_free_urb(cmdinfo->data_in_urb);
226 	if (!(cmdinfo->state & DATA_OUT_URB_INFLIGHT))
227 		usb_free_urb(cmdinfo->data_out_urb);
228 }
229 
230 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
231 {
232 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
233 	struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
234 
235 	lockdep_assert_held(&devinfo->lock);
236 	if (cmdinfo->state & (COMMAND_INFLIGHT |
237 			      DATA_IN_URB_INFLIGHT |
238 			      DATA_OUT_URB_INFLIGHT |
239 			      COMMAND_ABORTED))
240 		return -EBUSY;
241 	devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
242 	uas_free_unsubmitted_urbs(cmnd);
243 	cmnd->scsi_done(cmnd);
244 	return 0;
245 }
246 
247 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
248 			  unsigned direction)
249 {
250 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
251 	int err;
252 
253 	cmdinfo->state |= direction | SUBMIT_STATUS_URB;
254 	err = uas_submit_urbs(cmnd, cmnd->device->hostdata);
255 	if (err) {
256 		uas_add_work(cmdinfo);
257 	}
258 }
259 
260 static bool uas_evaluate_response_iu(struct response_iu *riu, struct scsi_cmnd *cmnd)
261 {
262 	u8 response_code = riu->response_code;
263 
264 	switch (response_code) {
265 	case RC_INCORRECT_LUN:
266 		cmnd->result = DID_BAD_TARGET << 16;
267 		break;
268 	case RC_TMF_SUCCEEDED:
269 		cmnd->result = DID_OK << 16;
270 		break;
271 	case RC_TMF_NOT_SUPPORTED:
272 		cmnd->result = DID_TARGET_FAILURE << 16;
273 		break;
274 	default:
275 		uas_log_cmd_state(cmnd, "response iu", response_code);
276 		cmnd->result = DID_ERROR << 16;
277 		break;
278 	}
279 
280 	return response_code == RC_TMF_SUCCEEDED;
281 }
282 
283 static void uas_stat_cmplt(struct urb *urb)
284 {
285 	struct iu *iu = urb->transfer_buffer;
286 	struct Scsi_Host *shost = urb->context;
287 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
288 	struct urb *data_in_urb = NULL;
289 	struct urb *data_out_urb = NULL;
290 	struct scsi_cmnd *cmnd;
291 	struct uas_cmd_info *cmdinfo;
292 	unsigned long flags;
293 	unsigned int idx;
294 	int status = urb->status;
295 	bool success;
296 
297 	spin_lock_irqsave(&devinfo->lock, flags);
298 
299 	if (devinfo->resetting)
300 		goto out;
301 
302 	if (status) {
303 		if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
304 			dev_err(&urb->dev->dev, "stat urb: status %d\n", status);
305 		goto out;
306 	}
307 
308 	idx = be16_to_cpup(&iu->tag) - 1;
309 	if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
310 		dev_err(&urb->dev->dev,
311 			"stat urb: no pending cmd for uas-tag %d\n", idx + 1);
312 		goto out;
313 	}
314 
315 	cmnd = devinfo->cmnd[idx];
316 	cmdinfo = (void *)&cmnd->SCp;
317 
318 	if (!(cmdinfo->state & COMMAND_INFLIGHT)) {
319 		uas_log_cmd_state(cmnd, "unexpected status cmplt", 0);
320 		goto out;
321 	}
322 
323 	switch (iu->iu_id) {
324 	case IU_ID_STATUS:
325 		uas_sense(urb, cmnd);
326 		if (cmnd->result != 0) {
327 			/* cancel data transfers on error */
328 			data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
329 			data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
330 		}
331 		cmdinfo->state &= ~COMMAND_INFLIGHT;
332 		uas_try_complete(cmnd, __func__);
333 		break;
334 	case IU_ID_READ_READY:
335 		if (!cmdinfo->data_in_urb ||
336 				(cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
337 			uas_log_cmd_state(cmnd, "unexpected read rdy", 0);
338 			break;
339 		}
340 		uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
341 		break;
342 	case IU_ID_WRITE_READY:
343 		if (!cmdinfo->data_out_urb ||
344 				(cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
345 			uas_log_cmd_state(cmnd, "unexpected write rdy", 0);
346 			break;
347 		}
348 		uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
349 		break;
350 	case IU_ID_RESPONSE:
351 		cmdinfo->state &= ~COMMAND_INFLIGHT;
352 		success = uas_evaluate_response_iu((struct response_iu *)iu, cmnd);
353 		if (!success) {
354 			/* Error, cancel data transfers */
355 			data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
356 			data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
357 		}
358 		uas_try_complete(cmnd, __func__);
359 		break;
360 	default:
361 		uas_log_cmd_state(cmnd, "bogus IU", iu->iu_id);
362 	}
363 out:
364 	usb_free_urb(urb);
365 	spin_unlock_irqrestore(&devinfo->lock, flags);
366 
367 	/* Unlinking of data urbs must be done without holding the lock */
368 	if (data_in_urb) {
369 		usb_unlink_urb(data_in_urb);
370 		usb_put_urb(data_in_urb);
371 	}
372 	if (data_out_urb) {
373 		usb_unlink_urb(data_out_urb);
374 		usb_put_urb(data_out_urb);
375 	}
376 }
377 
378 static void uas_data_cmplt(struct urb *urb)
379 {
380 	struct scsi_cmnd *cmnd = urb->context;
381 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
382 	struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
383 	struct scsi_data_buffer *sdb = &cmnd->sdb;
384 	unsigned long flags;
385 	int status = urb->status;
386 
387 	spin_lock_irqsave(&devinfo->lock, flags);
388 
389 	if (cmdinfo->data_in_urb == urb) {
390 		cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
391 		cmdinfo->data_in_urb = NULL;
392 	} else if (cmdinfo->data_out_urb == urb) {
393 		cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
394 		cmdinfo->data_out_urb = NULL;
395 	}
396 
397 	if (devinfo->resetting)
398 		goto out;
399 
400 	/* Data urbs should not complete before the cmd urb is submitted */
401 	if (cmdinfo->state & SUBMIT_CMD_URB) {
402 		uas_log_cmd_state(cmnd, "unexpected data cmplt", 0);
403 		goto out;
404 	}
405 
406 	if (status) {
407 		if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
408 			uas_log_cmd_state(cmnd, "data cmplt err", status);
409 		/* error: no data transfered */
410 		scsi_set_resid(cmnd, sdb->length);
411 	} else {
412 		scsi_set_resid(cmnd, sdb->length - urb->actual_length);
413 	}
414 	uas_try_complete(cmnd, __func__);
415 out:
416 	usb_free_urb(urb);
417 	spin_unlock_irqrestore(&devinfo->lock, flags);
418 }
419 
420 static void uas_cmd_cmplt(struct urb *urb)
421 {
422 	if (urb->status)
423 		dev_err(&urb->dev->dev, "cmd cmplt err %d\n", urb->status);
424 
425 	usb_free_urb(urb);
426 }
427 
428 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
429 				      struct scsi_cmnd *cmnd,
430 				      enum dma_data_direction dir)
431 {
432 	struct usb_device *udev = devinfo->udev;
433 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
434 	struct urb *urb = usb_alloc_urb(0, gfp);
435 	struct scsi_data_buffer *sdb = &cmnd->sdb;
436 	unsigned int pipe = (dir == DMA_FROM_DEVICE)
437 		? devinfo->data_in_pipe : devinfo->data_out_pipe;
438 
439 	if (!urb)
440 		goto out;
441 	usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
442 			  uas_data_cmplt, cmnd);
443 	if (devinfo->use_streams)
444 		urb->stream_id = cmdinfo->uas_tag;
445 	urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
446 	urb->sg = sdb->table.sgl;
447  out:
448 	return urb;
449 }
450 
451 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
452 				       struct scsi_cmnd *cmnd)
453 {
454 	struct usb_device *udev = devinfo->udev;
455 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
456 	struct urb *urb = usb_alloc_urb(0, gfp);
457 	struct sense_iu *iu;
458 
459 	if (!urb)
460 		goto out;
461 
462 	iu = kzalloc(sizeof(*iu), gfp);
463 	if (!iu)
464 		goto free;
465 
466 	usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
467 			  uas_stat_cmplt, cmnd->device->host);
468 	if (devinfo->use_streams)
469 		urb->stream_id = cmdinfo->uas_tag;
470 	urb->transfer_flags |= URB_FREE_BUFFER;
471  out:
472 	return urb;
473  free:
474 	usb_free_urb(urb);
475 	return NULL;
476 }
477 
478 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
479 					struct scsi_cmnd *cmnd)
480 {
481 	struct usb_device *udev = devinfo->udev;
482 	struct scsi_device *sdev = cmnd->device;
483 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
484 	struct urb *urb = usb_alloc_urb(0, gfp);
485 	struct command_iu *iu;
486 	int len;
487 
488 	if (!urb)
489 		goto out;
490 
491 	len = cmnd->cmd_len - 16;
492 	if (len < 0)
493 		len = 0;
494 	len = ALIGN(len, 4);
495 	iu = kzalloc(sizeof(*iu) + len, gfp);
496 	if (!iu)
497 		goto free;
498 
499 	iu->iu_id = IU_ID_COMMAND;
500 	iu->tag = cpu_to_be16(cmdinfo->uas_tag);
501 	iu->prio_attr = UAS_SIMPLE_TAG;
502 	iu->len = len;
503 	int_to_scsilun(sdev->lun, &iu->lun);
504 	memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
505 
506 	usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
507 							uas_cmd_cmplt, NULL);
508 	urb->transfer_flags |= URB_FREE_BUFFER;
509  out:
510 	return urb;
511  free:
512 	usb_free_urb(urb);
513 	return NULL;
514 }
515 
516 /*
517  * Why should I request the Status IU before sending the Command IU?  Spec
518  * says to, but also says the device may receive them in any order.  Seems
519  * daft to me.
520  */
521 
522 static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd, gfp_t gfp)
523 {
524 	struct uas_dev_info *devinfo = cmnd->device->hostdata;
525 	struct urb *urb;
526 	int err;
527 
528 	urb = uas_alloc_sense_urb(devinfo, gfp, cmnd);
529 	if (!urb)
530 		return NULL;
531 	usb_anchor_urb(urb, &devinfo->sense_urbs);
532 	err = usb_submit_urb(urb, gfp);
533 	if (err) {
534 		usb_unanchor_urb(urb);
535 		uas_log_cmd_state(cmnd, "sense submit err", err);
536 		usb_free_urb(urb);
537 		return NULL;
538 	}
539 	return urb;
540 }
541 
542 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
543 			   struct uas_dev_info *devinfo)
544 {
545 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
546 	struct urb *urb;
547 	int err;
548 
549 	lockdep_assert_held(&devinfo->lock);
550 	if (cmdinfo->state & SUBMIT_STATUS_URB) {
551 		urb = uas_submit_sense_urb(cmnd, GFP_ATOMIC);
552 		if (!urb)
553 			return SCSI_MLQUEUE_DEVICE_BUSY;
554 		cmdinfo->state &= ~SUBMIT_STATUS_URB;
555 	}
556 
557 	if (cmdinfo->state & ALLOC_DATA_IN_URB) {
558 		cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, GFP_ATOMIC,
559 							cmnd, DMA_FROM_DEVICE);
560 		if (!cmdinfo->data_in_urb)
561 			return SCSI_MLQUEUE_DEVICE_BUSY;
562 		cmdinfo->state &= ~ALLOC_DATA_IN_URB;
563 	}
564 
565 	if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
566 		usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
567 		err = usb_submit_urb(cmdinfo->data_in_urb, GFP_ATOMIC);
568 		if (err) {
569 			usb_unanchor_urb(cmdinfo->data_in_urb);
570 			uas_log_cmd_state(cmnd, "data in submit err", err);
571 			return SCSI_MLQUEUE_DEVICE_BUSY;
572 		}
573 		cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
574 		cmdinfo->state |= DATA_IN_URB_INFLIGHT;
575 	}
576 
577 	if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
578 		cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, GFP_ATOMIC,
579 							cmnd, DMA_TO_DEVICE);
580 		if (!cmdinfo->data_out_urb)
581 			return SCSI_MLQUEUE_DEVICE_BUSY;
582 		cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
583 	}
584 
585 	if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
586 		usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
587 		err = usb_submit_urb(cmdinfo->data_out_urb, GFP_ATOMIC);
588 		if (err) {
589 			usb_unanchor_urb(cmdinfo->data_out_urb);
590 			uas_log_cmd_state(cmnd, "data out submit err", err);
591 			return SCSI_MLQUEUE_DEVICE_BUSY;
592 		}
593 		cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
594 		cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
595 	}
596 
597 	if (cmdinfo->state & ALLOC_CMD_URB) {
598 		cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, GFP_ATOMIC, cmnd);
599 		if (!cmdinfo->cmd_urb)
600 			return SCSI_MLQUEUE_DEVICE_BUSY;
601 		cmdinfo->state &= ~ALLOC_CMD_URB;
602 	}
603 
604 	if (cmdinfo->state & SUBMIT_CMD_URB) {
605 		usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
606 		err = usb_submit_urb(cmdinfo->cmd_urb, GFP_ATOMIC);
607 		if (err) {
608 			usb_unanchor_urb(cmdinfo->cmd_urb);
609 			uas_log_cmd_state(cmnd, "cmd submit err", err);
610 			return SCSI_MLQUEUE_DEVICE_BUSY;
611 		}
612 		cmdinfo->cmd_urb = NULL;
613 		cmdinfo->state &= ~SUBMIT_CMD_URB;
614 		cmdinfo->state |= COMMAND_INFLIGHT;
615 	}
616 
617 	return 0;
618 }
619 
620 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
621 					void (*done)(struct scsi_cmnd *))
622 {
623 	struct scsi_device *sdev = cmnd->device;
624 	struct uas_dev_info *devinfo = sdev->hostdata;
625 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
626 	unsigned long flags;
627 	int idx, err;
628 
629 	BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
630 
631 	/* Re-check scsi_block_requests now that we've the host-lock */
632 	if (cmnd->device->host->host_self_blocked)
633 		return SCSI_MLQUEUE_DEVICE_BUSY;
634 
635 	if ((devinfo->flags & US_FL_NO_ATA_1X) &&
636 			(cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) {
637 		memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB,
638 		       sizeof(usb_stor_sense_invalidCDB));
639 		cmnd->result = SAM_STAT_CHECK_CONDITION;
640 		cmnd->scsi_done(cmnd);
641 		return 0;
642 	}
643 
644 	spin_lock_irqsave(&devinfo->lock, flags);
645 
646 	if (devinfo->resetting) {
647 		cmnd->result = DID_ERROR << 16;
648 		cmnd->scsi_done(cmnd);
649 		spin_unlock_irqrestore(&devinfo->lock, flags);
650 		return 0;
651 	}
652 
653 	/* Find a free uas-tag */
654 	for (idx = 0; idx < devinfo->qdepth; idx++) {
655 		if (!devinfo->cmnd[idx])
656 			break;
657 	}
658 	if (idx == devinfo->qdepth) {
659 		spin_unlock_irqrestore(&devinfo->lock, flags);
660 		return SCSI_MLQUEUE_DEVICE_BUSY;
661 	}
662 
663 	cmnd->scsi_done = done;
664 
665 	memset(cmdinfo, 0, sizeof(*cmdinfo));
666 	cmdinfo->uas_tag = idx + 1; /* uas-tag == usb-stream-id, so 1 based */
667 	cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
668 
669 	switch (cmnd->sc_data_direction) {
670 	case DMA_FROM_DEVICE:
671 		cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
672 		break;
673 	case DMA_BIDIRECTIONAL:
674 		cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
675 		/* fall through */
676 	case DMA_TO_DEVICE:
677 		cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
678 	case DMA_NONE:
679 		break;
680 	}
681 
682 	if (!devinfo->use_streams)
683 		cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
684 
685 	err = uas_submit_urbs(cmnd, devinfo);
686 	if (err) {
687 		/* If we did nothing, give up now */
688 		if (cmdinfo->state & SUBMIT_STATUS_URB) {
689 			spin_unlock_irqrestore(&devinfo->lock, flags);
690 			return SCSI_MLQUEUE_DEVICE_BUSY;
691 		}
692 		uas_add_work(cmdinfo);
693 	}
694 
695 	devinfo->cmnd[idx] = cmnd;
696 	spin_unlock_irqrestore(&devinfo->lock, flags);
697 	return 0;
698 }
699 
700 static DEF_SCSI_QCMD(uas_queuecommand)
701 
702 /*
703  * For now we do not support actually sending an abort to the device, so
704  * this eh always fails. Still we must define it to make sure that we've
705  * dropped all references to the cmnd in question once this function exits.
706  */
707 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
708 {
709 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
710 	struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
711 	struct urb *data_in_urb = NULL;
712 	struct urb *data_out_urb = NULL;
713 	unsigned long flags;
714 
715 	spin_lock_irqsave(&devinfo->lock, flags);
716 
717 	uas_log_cmd_state(cmnd, __func__, 0);
718 
719 	/* Ensure that try_complete does not call scsi_done */
720 	cmdinfo->state |= COMMAND_ABORTED;
721 
722 	/* Drop all refs to this cmnd, kill data urbs to break their ref */
723 	devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
724 	if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
725 		data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
726 	if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
727 		data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
728 
729 	uas_free_unsubmitted_urbs(cmnd);
730 
731 	spin_unlock_irqrestore(&devinfo->lock, flags);
732 
733 	if (data_in_urb) {
734 		usb_kill_urb(data_in_urb);
735 		usb_put_urb(data_in_urb);
736 	}
737 	if (data_out_urb) {
738 		usb_kill_urb(data_out_urb);
739 		usb_put_urb(data_out_urb);
740 	}
741 
742 	return FAILED;
743 }
744 
745 static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
746 {
747 	struct scsi_device *sdev = cmnd->device;
748 	struct uas_dev_info *devinfo = sdev->hostdata;
749 	struct usb_device *udev = devinfo->udev;
750 	unsigned long flags;
751 	int err;
752 
753 	err = usb_lock_device_for_reset(udev, devinfo->intf);
754 	if (err) {
755 		shost_printk(KERN_ERR, sdev->host,
756 			     "%s FAILED to get lock err %d\n", __func__, err);
757 		return FAILED;
758 	}
759 
760 	shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
761 
762 	spin_lock_irqsave(&devinfo->lock, flags);
763 	devinfo->resetting = 1;
764 	spin_unlock_irqrestore(&devinfo->lock, flags);
765 
766 	usb_kill_anchored_urbs(&devinfo->cmd_urbs);
767 	usb_kill_anchored_urbs(&devinfo->sense_urbs);
768 	usb_kill_anchored_urbs(&devinfo->data_urbs);
769 	uas_zap_pending(devinfo, DID_RESET);
770 
771 	err = usb_reset_device(udev);
772 
773 	spin_lock_irqsave(&devinfo->lock, flags);
774 	devinfo->resetting = 0;
775 	spin_unlock_irqrestore(&devinfo->lock, flags);
776 
777 	usb_unlock_device(udev);
778 
779 	if (err) {
780 		shost_printk(KERN_INFO, sdev->host, "%s FAILED err %d\n",
781 			     __func__, err);
782 		return FAILED;
783 	}
784 
785 	shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
786 	return SUCCESS;
787 }
788 
789 static int uas_target_alloc(struct scsi_target *starget)
790 {
791 	struct uas_dev_info *devinfo = (struct uas_dev_info *)
792 			dev_to_shost(starget->dev.parent)->hostdata;
793 
794 	if (devinfo->flags & US_FL_NO_REPORT_LUNS)
795 		starget->no_report_luns = 1;
796 
797 	return 0;
798 }
799 
800 static int uas_slave_alloc(struct scsi_device *sdev)
801 {
802 	struct uas_dev_info *devinfo =
803 		(struct uas_dev_info *)sdev->host->hostdata;
804 
805 	sdev->hostdata = devinfo;
806 
807 	/*
808 	 * The protocol has no requirements on alignment in the strict sense.
809 	 * Controllers may or may not have alignment restrictions.
810 	 * As this is not exported, we use an extremely conservative guess.
811 	 */
812 	blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
813 
814 	if (devinfo->flags & US_FL_MAX_SECTORS_64)
815 		blk_queue_max_hw_sectors(sdev->request_queue, 64);
816 	else if (devinfo->flags & US_FL_MAX_SECTORS_240)
817 		blk_queue_max_hw_sectors(sdev->request_queue, 240);
818 
819 	return 0;
820 }
821 
822 static int uas_slave_configure(struct scsi_device *sdev)
823 {
824 	struct uas_dev_info *devinfo = sdev->hostdata;
825 
826 	if (devinfo->flags & US_FL_NO_REPORT_OPCODES)
827 		sdev->no_report_opcodes = 1;
828 
829 	/* A few buggy USB-ATA bridges don't understand FUA */
830 	if (devinfo->flags & US_FL_BROKEN_FUA)
831 		sdev->broken_fua = 1;
832 
833 	/* UAS also needs to support FL_ALWAYS_SYNC */
834 	if (devinfo->flags & US_FL_ALWAYS_SYNC) {
835 		sdev->skip_ms_page_3f = 1;
836 		sdev->skip_ms_page_8 = 1;
837 		sdev->wce_default_on = 1;
838 	}
839 
840 	/* Some disks cannot handle READ_CAPACITY_16 */
841 	if (devinfo->flags & US_FL_NO_READ_CAPACITY_16)
842 		sdev->no_read_capacity_16 = 1;
843 
844 	/*
845 	 * Some disks return the total number of blocks in response
846 	 * to READ CAPACITY rather than the highest block number.
847 	 * If this device makes that mistake, tell the sd driver.
848 	 */
849 	if (devinfo->flags & US_FL_FIX_CAPACITY)
850 		sdev->fix_capacity = 1;
851 
852 	/*
853 	 * in some cases we have to guess
854 	 */
855 	if (devinfo->flags & US_FL_CAPACITY_HEURISTICS)
856 		sdev->guess_capacity = 1;
857 
858 	/*
859 	 * Some devices don't like MODE SENSE with page=0x3f,
860 	 * which is the command used for checking if a device
861 	 * is write-protected.  Now that we tell the sd driver
862 	 * to do a 192-byte transfer with this command the
863 	 * majority of devices work fine, but a few still can't
864 	 * handle it.  The sd driver will simply assume those
865 	 * devices are write-enabled.
866 	 */
867 	if (devinfo->flags & US_FL_NO_WP_DETECT)
868 		sdev->skip_ms_page_3f = 1;
869 
870 	scsi_change_queue_depth(sdev, devinfo->qdepth - 2);
871 	return 0;
872 }
873 
874 static struct scsi_host_template uas_host_template = {
875 	.module = THIS_MODULE,
876 	.name = "uas",
877 	.queuecommand = uas_queuecommand,
878 	.target_alloc = uas_target_alloc,
879 	.slave_alloc = uas_slave_alloc,
880 	.slave_configure = uas_slave_configure,
881 	.eh_abort_handler = uas_eh_abort_handler,
882 	.eh_device_reset_handler = uas_eh_device_reset_handler,
883 	.this_id = -1,
884 	.skip_settle_delay = 1,
885 	.dma_boundary = PAGE_SIZE - 1,
886 };
887 
888 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
889 		    vendorName, productName, useProtocol, useTransport, \
890 		    initFunction, flags) \
891 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
892 	.driver_info = (flags) }
893 
894 static struct usb_device_id uas_usb_ids[] = {
895 #	include "unusual_uas.h"
896 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
897 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
898 	{ }
899 };
900 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
901 
902 #undef UNUSUAL_DEV
903 
904 static int uas_switch_interface(struct usb_device *udev,
905 				struct usb_interface *intf)
906 {
907 	struct usb_host_interface *alt;
908 
909 	alt = uas_find_uas_alt_setting(intf);
910 	if (!alt)
911 		return -ENODEV;
912 
913 	return usb_set_interface(udev, alt->desc.bInterfaceNumber,
914 			alt->desc.bAlternateSetting);
915 }
916 
917 static int uas_configure_endpoints(struct uas_dev_info *devinfo)
918 {
919 	struct usb_host_endpoint *eps[4] = { };
920 	struct usb_device *udev = devinfo->udev;
921 	int r;
922 
923 	r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
924 	if (r)
925 		return r;
926 
927 	devinfo->cmd_pipe = usb_sndbulkpipe(udev,
928 					    usb_endpoint_num(&eps[0]->desc));
929 	devinfo->status_pipe = usb_rcvbulkpipe(udev,
930 					    usb_endpoint_num(&eps[1]->desc));
931 	devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
932 					    usb_endpoint_num(&eps[2]->desc));
933 	devinfo->data_out_pipe = usb_sndbulkpipe(udev,
934 					    usb_endpoint_num(&eps[3]->desc));
935 
936 	if (udev->speed < USB_SPEED_SUPER) {
937 		devinfo->qdepth = 32;
938 		devinfo->use_streams = 0;
939 	} else {
940 		devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
941 						    3, MAX_CMNDS, GFP_NOIO);
942 		if (devinfo->qdepth < 0)
943 			return devinfo->qdepth;
944 		devinfo->use_streams = 1;
945 	}
946 
947 	return 0;
948 }
949 
950 static void uas_free_streams(struct uas_dev_info *devinfo)
951 {
952 	struct usb_device *udev = devinfo->udev;
953 	struct usb_host_endpoint *eps[3];
954 
955 	eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
956 	eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
957 	eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
958 	usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
959 }
960 
961 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
962 {
963 	int result = -ENOMEM;
964 	struct Scsi_Host *shost = NULL;
965 	struct uas_dev_info *devinfo;
966 	struct usb_device *udev = interface_to_usbdev(intf);
967 	unsigned long dev_flags;
968 
969 	if (!uas_use_uas_driver(intf, id, &dev_flags))
970 		return -ENODEV;
971 
972 	if (uas_switch_interface(udev, intf))
973 		return -ENODEV;
974 
975 	shost = scsi_host_alloc(&uas_host_template,
976 				sizeof(struct uas_dev_info));
977 	if (!shost)
978 		goto set_alt0;
979 
980 	shost->max_cmd_len = 16 + 252;
981 	shost->max_id = 1;
982 	shost->max_lun = 256;
983 	shost->max_channel = 0;
984 	shost->sg_tablesize = udev->bus->sg_tablesize;
985 
986 	devinfo = (struct uas_dev_info *)shost->hostdata;
987 	devinfo->intf = intf;
988 	devinfo->udev = udev;
989 	devinfo->resetting = 0;
990 	devinfo->shutdown = 0;
991 	devinfo->flags = dev_flags;
992 	init_usb_anchor(&devinfo->cmd_urbs);
993 	init_usb_anchor(&devinfo->sense_urbs);
994 	init_usb_anchor(&devinfo->data_urbs);
995 	spin_lock_init(&devinfo->lock);
996 	INIT_WORK(&devinfo->work, uas_do_work);
997 	INIT_WORK(&devinfo->scan_work, uas_scan_work);
998 
999 	result = uas_configure_endpoints(devinfo);
1000 	if (result)
1001 		goto set_alt0;
1002 
1003 	/*
1004 	 * 1 tag is reserved for untagged commands +
1005 	 * 1 tag to avoid off by one errors in some bridge firmwares
1006 	 */
1007 	shost->can_queue = devinfo->qdepth - 2;
1008 
1009 	usb_set_intfdata(intf, shost);
1010 	result = scsi_add_host(shost, &intf->dev);
1011 	if (result)
1012 		goto free_streams;
1013 
1014 	/* Submit the delayed_work for SCSI-device scanning */
1015 	schedule_work(&devinfo->scan_work);
1016 
1017 	return result;
1018 
1019 free_streams:
1020 	uas_free_streams(devinfo);
1021 	usb_set_intfdata(intf, NULL);
1022 set_alt0:
1023 	usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1024 	if (shost)
1025 		scsi_host_put(shost);
1026 	return result;
1027 }
1028 
1029 static int uas_cmnd_list_empty(struct uas_dev_info *devinfo)
1030 {
1031 	unsigned long flags;
1032 	int i, r = 1;
1033 
1034 	spin_lock_irqsave(&devinfo->lock, flags);
1035 
1036 	for (i = 0; i < devinfo->qdepth; i++) {
1037 		if (devinfo->cmnd[i]) {
1038 			r = 0; /* Not empty */
1039 			break;
1040 		}
1041 	}
1042 
1043 	spin_unlock_irqrestore(&devinfo->lock, flags);
1044 
1045 	return r;
1046 }
1047 
1048 /*
1049  * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily
1050  * get empty while there still is more work to do due to sense-urbs completing
1051  * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty.
1052  */
1053 static int uas_wait_for_pending_cmnds(struct uas_dev_info *devinfo)
1054 {
1055 	unsigned long start_time;
1056 	int r;
1057 
1058 	start_time = jiffies;
1059 	do {
1060 		flush_work(&devinfo->work);
1061 
1062 		r = usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000);
1063 		if (r == 0)
1064 			return -ETIME;
1065 
1066 		r = usb_wait_anchor_empty_timeout(&devinfo->data_urbs, 500);
1067 		if (r == 0)
1068 			return -ETIME;
1069 
1070 		if (time_after(jiffies, start_time + 5 * HZ))
1071 			return -ETIME;
1072 	} while (!uas_cmnd_list_empty(devinfo));
1073 
1074 	return 0;
1075 }
1076 
1077 static int uas_pre_reset(struct usb_interface *intf)
1078 {
1079 	struct Scsi_Host *shost = usb_get_intfdata(intf);
1080 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1081 	unsigned long flags;
1082 
1083 	if (devinfo->shutdown)
1084 		return 0;
1085 
1086 	/* Block new requests */
1087 	spin_lock_irqsave(shost->host_lock, flags);
1088 	scsi_block_requests(shost);
1089 	spin_unlock_irqrestore(shost->host_lock, flags);
1090 
1091 	if (uas_wait_for_pending_cmnds(devinfo) != 0) {
1092 		shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1093 		scsi_unblock_requests(shost);
1094 		return 1;
1095 	}
1096 
1097 	uas_free_streams(devinfo);
1098 
1099 	return 0;
1100 }
1101 
1102 static int uas_post_reset(struct usb_interface *intf)
1103 {
1104 	struct Scsi_Host *shost = usb_get_intfdata(intf);
1105 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1106 	unsigned long flags;
1107 	int err;
1108 
1109 	if (devinfo->shutdown)
1110 		return 0;
1111 
1112 	err = uas_configure_endpoints(devinfo);
1113 	if (err && err != -ENODEV)
1114 		shost_printk(KERN_ERR, shost,
1115 			     "%s: alloc streams error %d after reset",
1116 			     __func__, err);
1117 
1118 	/* we must unblock the host in every case lest we deadlock */
1119 	spin_lock_irqsave(shost->host_lock, flags);
1120 	scsi_report_bus_reset(shost, 0);
1121 	spin_unlock_irqrestore(shost->host_lock, flags);
1122 
1123 	scsi_unblock_requests(shost);
1124 
1125 	return err ? 1 : 0;
1126 }
1127 
1128 static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1129 {
1130 	struct Scsi_Host *shost = usb_get_intfdata(intf);
1131 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1132 
1133 	if (uas_wait_for_pending_cmnds(devinfo) != 0) {
1134 		shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1135 		return -ETIME;
1136 	}
1137 
1138 	return 0;
1139 }
1140 
1141 static int uas_resume(struct usb_interface *intf)
1142 {
1143 	return 0;
1144 }
1145 
1146 static int uas_reset_resume(struct usb_interface *intf)
1147 {
1148 	struct Scsi_Host *shost = usb_get_intfdata(intf);
1149 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1150 	unsigned long flags;
1151 	int err;
1152 
1153 	err = uas_configure_endpoints(devinfo);
1154 	if (err) {
1155 		shost_printk(KERN_ERR, shost,
1156 			     "%s: alloc streams error %d after reset",
1157 			     __func__, err);
1158 		return -EIO;
1159 	}
1160 
1161 	spin_lock_irqsave(shost->host_lock, flags);
1162 	scsi_report_bus_reset(shost, 0);
1163 	spin_unlock_irqrestore(shost->host_lock, flags);
1164 
1165 	return 0;
1166 }
1167 
1168 static void uas_disconnect(struct usb_interface *intf)
1169 {
1170 	struct Scsi_Host *shost = usb_get_intfdata(intf);
1171 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1172 	unsigned long flags;
1173 
1174 	spin_lock_irqsave(&devinfo->lock, flags);
1175 	devinfo->resetting = 1;
1176 	spin_unlock_irqrestore(&devinfo->lock, flags);
1177 
1178 	cancel_work_sync(&devinfo->work);
1179 	usb_kill_anchored_urbs(&devinfo->cmd_urbs);
1180 	usb_kill_anchored_urbs(&devinfo->sense_urbs);
1181 	usb_kill_anchored_urbs(&devinfo->data_urbs);
1182 	uas_zap_pending(devinfo, DID_NO_CONNECT);
1183 
1184 	/*
1185 	 * Prevent SCSI scanning (if it hasn't started yet)
1186 	 * or wait for the SCSI-scanning routine to stop.
1187 	 */
1188 	cancel_work_sync(&devinfo->scan_work);
1189 
1190 	scsi_remove_host(shost);
1191 	uas_free_streams(devinfo);
1192 	scsi_host_put(shost);
1193 }
1194 
1195 /*
1196  * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1197  * hang on reboot when the device is still in uas mode. Note the reset is
1198  * necessary as some devices won't revert to usb-storage mode without it.
1199  */
1200 static void uas_shutdown(struct device *dev)
1201 {
1202 	struct usb_interface *intf = to_usb_interface(dev);
1203 	struct usb_device *udev = interface_to_usbdev(intf);
1204 	struct Scsi_Host *shost = usb_get_intfdata(intf);
1205 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1206 
1207 	if (system_state != SYSTEM_RESTART)
1208 		return;
1209 
1210 	devinfo->shutdown = 1;
1211 	uas_free_streams(devinfo);
1212 	usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1213 	usb_reset_device(udev);
1214 }
1215 
1216 static struct usb_driver uas_driver = {
1217 	.name = "uas",
1218 	.probe = uas_probe,
1219 	.disconnect = uas_disconnect,
1220 	.pre_reset = uas_pre_reset,
1221 	.post_reset = uas_post_reset,
1222 	.suspend = uas_suspend,
1223 	.resume = uas_resume,
1224 	.reset_resume = uas_reset_resume,
1225 	.drvwrap.driver.shutdown = uas_shutdown,
1226 	.id_table = uas_usb_ids,
1227 };
1228 
1229 module_usb_driver(uas_driver);
1230 
1231 MODULE_LICENSE("GPL");
1232 MODULE_IMPORT_NS(USB_STORAGE);
1233 MODULE_AUTHOR(
1234 	"Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");
1235