• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * USB Attached SCSI
3  * Note that this is not the same as the USB Mass Storage driver
4  *
5  * Copyright Hans de Goede <hdegoede@redhat.com> for Red Hat, Inc. 2013 - 2016
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>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 #include <linux/usb_usual.h>
18 #include <linux/usb/hcd.h>
19 #include <linux/usb/storage.h>
20 #include <linux/usb/uas.h>
21 
22 #include <scsi/scsi.h>
23 #include <scsi/scsi_eh.h>
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 
30 #include "uas-detect.h"
31 #include "scsiglue.h"
32 
33 #define MAX_CMNDS 256
34 
35 struct uas_dev_info {
36 	struct usb_interface *intf;
37 	struct usb_device *udev;
38 	struct usb_anchor cmd_urbs;
39 	struct usb_anchor sense_urbs;
40 	struct usb_anchor data_urbs;
41 	unsigned long flags;
42 	int qdepth, resetting;
43 	unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
44 	unsigned use_streams:1;
45 	unsigned shutdown:1;
46 	struct scsi_cmnd *cmnd[MAX_CMNDS];
47 	spinlock_t lock;
48 	struct work_struct work;
49 	struct work_struct scan_work;      /* for async scanning */
50 };
51 
52 enum {
53 	SUBMIT_STATUS_URB	= (1 << 1),
54 	ALLOC_DATA_IN_URB	= (1 << 2),
55 	SUBMIT_DATA_IN_URB	= (1 << 3),
56 	ALLOC_DATA_OUT_URB	= (1 << 4),
57 	SUBMIT_DATA_OUT_URB	= (1 << 5),
58 	ALLOC_CMD_URB		= (1 << 6),
59 	SUBMIT_CMD_URB		= (1 << 7),
60 	COMMAND_INFLIGHT        = (1 << 8),
61 	DATA_IN_URB_INFLIGHT    = (1 << 9),
62 	DATA_OUT_URB_INFLIGHT   = (1 << 10),
63 	COMMAND_ABORTED         = (1 << 11),
64 	IS_IN_WORK_LIST         = (1 << 12),
65 };
66 
67 /* Overrides scsi_pointer */
68 struct uas_cmd_info {
69 	unsigned int state;
70 	unsigned int uas_tag;
71 	struct urb *cmd_urb;
72 	struct urb *data_in_urb;
73 	struct urb *data_out_urb;
74 };
75 
76 /* I hate forward declarations, but I actually have a loop */
77 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
78 				struct uas_dev_info *devinfo, gfp_t gfp);
79 static void uas_do_work(struct work_struct *work);
80 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
81 static void uas_free_streams(struct uas_dev_info *devinfo);
82 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
83 				int status);
84 
85 /*
86  * This driver needs its own workqueue, as we need to control memory allocation.
87  *
88  * In the course of error handling and power management uas_wait_for_pending_cmnds()
89  * needs to flush pending work items. In these contexts we cannot allocate memory
90  * by doing block IO as we would deadlock. For the same reason we cannot wait
91  * for anything allocating memory not heeding these constraints.
92  *
93  * So we have to control all work items that can be on the workqueue we flush.
94  * Hence we cannot share a queue and need our own.
95  */
96 static struct workqueue_struct *workqueue;
97 
uas_do_work(struct work_struct * work)98 static void uas_do_work(struct work_struct *work)
99 {
100 	struct uas_dev_info *devinfo =
101 		container_of(work, struct uas_dev_info, work);
102 	struct uas_cmd_info *cmdinfo;
103 	struct scsi_cmnd *cmnd;
104 	unsigned long flags;
105 	int i, err;
106 
107 	spin_lock_irqsave(&devinfo->lock, flags);
108 
109 	if (devinfo->resetting)
110 		goto out;
111 
112 	for (i = 0; i < devinfo->qdepth; i++) {
113 		if (!devinfo->cmnd[i])
114 			continue;
115 
116 		cmnd = devinfo->cmnd[i];
117 		cmdinfo = (void *)&cmnd->SCp;
118 
119 		if (!(cmdinfo->state & IS_IN_WORK_LIST))
120 			continue;
121 
122 		err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
123 		if (!err)
124 			cmdinfo->state &= ~IS_IN_WORK_LIST;
125 		else
126 			queue_work(workqueue, &devinfo->work);
127 	}
128 out:
129 	spin_unlock_irqrestore(&devinfo->lock, flags);
130 }
131 
uas_scan_work(struct work_struct * work)132 static void uas_scan_work(struct work_struct *work)
133 {
134 	struct uas_dev_info *devinfo =
135 		container_of(work, struct uas_dev_info, scan_work);
136 	struct Scsi_Host *shost = usb_get_intfdata(devinfo->intf);
137 
138 	dev_dbg(&devinfo->intf->dev, "starting scan\n");
139 	scsi_scan_host(shost);
140 	dev_dbg(&devinfo->intf->dev, "scan complete\n");
141 }
142 
uas_add_work(struct uas_cmd_info * cmdinfo)143 static void uas_add_work(struct uas_cmd_info *cmdinfo)
144 {
145 	struct scsi_pointer *scp = (void *)cmdinfo;
146 	struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
147 	struct uas_dev_info *devinfo = cmnd->device->hostdata;
148 
149 	lockdep_assert_held(&devinfo->lock);
150 	cmdinfo->state |= IS_IN_WORK_LIST;
151 	queue_work(workqueue, &devinfo->work);
152 }
153 
uas_zap_pending(struct uas_dev_info * devinfo,int result)154 static void uas_zap_pending(struct uas_dev_info *devinfo, int result)
155 {
156 	struct uas_cmd_info *cmdinfo;
157 	struct scsi_cmnd *cmnd;
158 	unsigned long flags;
159 	int i, err;
160 
161 	spin_lock_irqsave(&devinfo->lock, flags);
162 	for (i = 0; i < devinfo->qdepth; i++) {
163 		if (!devinfo->cmnd[i])
164 			continue;
165 
166 		cmnd = devinfo->cmnd[i];
167 		cmdinfo = (void *)&cmnd->SCp;
168 		uas_log_cmd_state(cmnd, __func__, 0);
169 		/* Sense urbs were killed, clear COMMAND_INFLIGHT manually */
170 		cmdinfo->state &= ~COMMAND_INFLIGHT;
171 		cmnd->result = result << 16;
172 		err = uas_try_complete(cmnd, __func__);
173 		WARN_ON(err != 0);
174 	}
175 	spin_unlock_irqrestore(&devinfo->lock, flags);
176 }
177 
uas_sense(struct urb * urb,struct scsi_cmnd * cmnd)178 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
179 {
180 	struct sense_iu *sense_iu = urb->transfer_buffer;
181 	struct scsi_device *sdev = cmnd->device;
182 
183 	if (urb->actual_length > 16) {
184 		unsigned len = be16_to_cpup(&sense_iu->len);
185 		if (len + 16 != urb->actual_length) {
186 			int newlen = min(len + 16, urb->actual_length) - 16;
187 			if (newlen < 0)
188 				newlen = 0;
189 			sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
190 				"disagrees with IU sense data length %d, "
191 				"using %d bytes of sense data\n", __func__,
192 					urb->actual_length, len, newlen);
193 			len = newlen;
194 		}
195 		memcpy(cmnd->sense_buffer, sense_iu->sense, len);
196 	}
197 
198 	cmnd->result = sense_iu->status;
199 }
200 
uas_log_cmd_state(struct scsi_cmnd * cmnd,const char * prefix,int status)201 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
202 			      int status)
203 {
204 	struct uas_cmd_info *ci = (void *)&cmnd->SCp;
205 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
206 
207 	if (status == -ENODEV) /* too late */
208 		return;
209 
210 	scmd_printk(KERN_INFO, cmnd,
211 		    "%s %d uas-tag %d inflight:%s%s%s%s%s%s%s%s%s%s%s%s ",
212 		    prefix, status, cmdinfo->uas_tag,
213 		    (ci->state & SUBMIT_STATUS_URB)     ? " s-st"  : "",
214 		    (ci->state & ALLOC_DATA_IN_URB)     ? " a-in"  : "",
215 		    (ci->state & SUBMIT_DATA_IN_URB)    ? " s-in"  : "",
216 		    (ci->state & ALLOC_DATA_OUT_URB)    ? " a-out" : "",
217 		    (ci->state & SUBMIT_DATA_OUT_URB)   ? " s-out" : "",
218 		    (ci->state & ALLOC_CMD_URB)         ? " a-cmd" : "",
219 		    (ci->state & SUBMIT_CMD_URB)        ? " s-cmd" : "",
220 		    (ci->state & COMMAND_INFLIGHT)      ? " CMD"   : "",
221 		    (ci->state & DATA_IN_URB_INFLIGHT)  ? " IN"    : "",
222 		    (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT"   : "",
223 		    (ci->state & COMMAND_ABORTED)       ? " abort" : "",
224 		    (ci->state & IS_IN_WORK_LIST)       ? " work"  : "");
225 	scsi_print_command(cmnd);
226 }
227 
uas_free_unsubmitted_urbs(struct scsi_cmnd * cmnd)228 static void uas_free_unsubmitted_urbs(struct scsi_cmnd *cmnd)
229 {
230 	struct uas_cmd_info *cmdinfo;
231 
232 	if (!cmnd)
233 		return;
234 
235 	cmdinfo = (void *)&cmnd->SCp;
236 
237 	if (cmdinfo->state & SUBMIT_CMD_URB)
238 		usb_free_urb(cmdinfo->cmd_urb);
239 
240 	/* data urbs may have never gotten their submit flag set */
241 	if (!(cmdinfo->state & DATA_IN_URB_INFLIGHT))
242 		usb_free_urb(cmdinfo->data_in_urb);
243 	if (!(cmdinfo->state & DATA_OUT_URB_INFLIGHT))
244 		usb_free_urb(cmdinfo->data_out_urb);
245 }
246 
uas_try_complete(struct scsi_cmnd * cmnd,const char * caller)247 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
248 {
249 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
250 	struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
251 
252 	lockdep_assert_held(&devinfo->lock);
253 	if (cmdinfo->state & (COMMAND_INFLIGHT |
254 			      DATA_IN_URB_INFLIGHT |
255 			      DATA_OUT_URB_INFLIGHT |
256 			      COMMAND_ABORTED))
257 		return -EBUSY;
258 	devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
259 	uas_free_unsubmitted_urbs(cmnd);
260 	cmnd->scsi_done(cmnd);
261 	return 0;
262 }
263 
uas_xfer_data(struct urb * urb,struct scsi_cmnd * cmnd,unsigned direction)264 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
265 			  unsigned direction)
266 {
267 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
268 	int err;
269 
270 	cmdinfo->state |= direction | SUBMIT_STATUS_URB;
271 	err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
272 	if (err) {
273 		uas_add_work(cmdinfo);
274 	}
275 }
276 
uas_stat_cmplt(struct urb * urb)277 static void uas_stat_cmplt(struct urb *urb)
278 {
279 	struct iu *iu = urb->transfer_buffer;
280 	struct Scsi_Host *shost = urb->context;
281 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
282 	struct urb *data_in_urb = NULL;
283 	struct urb *data_out_urb = NULL;
284 	struct scsi_cmnd *cmnd;
285 	struct uas_cmd_info *cmdinfo;
286 	unsigned long flags;
287 	unsigned int idx;
288 	int status = urb->status;
289 
290 	spin_lock_irqsave(&devinfo->lock, flags);
291 
292 	if (devinfo->resetting)
293 		goto out;
294 
295 	if (status) {
296 		if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
297 			dev_err(&urb->dev->dev, "stat urb: status %d\n", status);
298 		goto out;
299 	}
300 
301 	idx = be16_to_cpup(&iu->tag) - 1;
302 	if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
303 		dev_err(&urb->dev->dev,
304 			"stat urb: no pending cmd for uas-tag %d\n", idx + 1);
305 		goto out;
306 	}
307 
308 	cmnd = devinfo->cmnd[idx];
309 	cmdinfo = (void *)&cmnd->SCp;
310 
311 	if (!(cmdinfo->state & COMMAND_INFLIGHT)) {
312 		uas_log_cmd_state(cmnd, "unexpected status cmplt", 0);
313 		goto out;
314 	}
315 
316 	switch (iu->iu_id) {
317 	case IU_ID_STATUS:
318 		uas_sense(urb, cmnd);
319 		if (cmnd->result != 0) {
320 			/* cancel data transfers on error */
321 			data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
322 			data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
323 		}
324 		cmdinfo->state &= ~COMMAND_INFLIGHT;
325 		uas_try_complete(cmnd, __func__);
326 		break;
327 	case IU_ID_READ_READY:
328 		if (!cmdinfo->data_in_urb ||
329 				(cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
330 			uas_log_cmd_state(cmnd, "unexpected read rdy", 0);
331 			break;
332 		}
333 		uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
334 		break;
335 	case IU_ID_WRITE_READY:
336 		if (!cmdinfo->data_out_urb ||
337 				(cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
338 			uas_log_cmd_state(cmnd, "unexpected write rdy", 0);
339 			break;
340 		}
341 		uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
342 		break;
343 	case IU_ID_RESPONSE:
344 		uas_log_cmd_state(cmnd, "unexpected response iu",
345 				  ((struct response_iu *)iu)->response_code);
346 		/* Error, cancel data transfers */
347 		data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
348 		data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
349 		cmdinfo->state &= ~COMMAND_INFLIGHT;
350 		cmnd->result = DID_ERROR << 16;
351 		uas_try_complete(cmnd, __func__);
352 		break;
353 	default:
354 		uas_log_cmd_state(cmnd, "bogus IU", iu->iu_id);
355 	}
356 out:
357 	usb_free_urb(urb);
358 	spin_unlock_irqrestore(&devinfo->lock, flags);
359 
360 	/* Unlinking of data urbs must be done without holding the lock */
361 	if (data_in_urb) {
362 		usb_unlink_urb(data_in_urb);
363 		usb_put_urb(data_in_urb);
364 	}
365 	if (data_out_urb) {
366 		usb_unlink_urb(data_out_urb);
367 		usb_put_urb(data_out_urb);
368 	}
369 }
370 
uas_data_cmplt(struct urb * urb)371 static void uas_data_cmplt(struct urb *urb)
372 {
373 	struct scsi_cmnd *cmnd = urb->context;
374 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
375 	struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
376 	struct scsi_data_buffer *sdb = NULL;
377 	unsigned long flags;
378 	int status = urb->status;
379 
380 	spin_lock_irqsave(&devinfo->lock, flags);
381 
382 	if (cmdinfo->data_in_urb == urb) {
383 		sdb = scsi_in(cmnd);
384 		cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
385 		cmdinfo->data_in_urb = NULL;
386 	} else if (cmdinfo->data_out_urb == urb) {
387 		sdb = scsi_out(cmnd);
388 		cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
389 		cmdinfo->data_out_urb = NULL;
390 	}
391 	if (sdb == NULL) {
392 		WARN_ON_ONCE(1);
393 		goto out;
394 	}
395 
396 	if (devinfo->resetting)
397 		goto out;
398 
399 	/* Data urbs should not complete before the cmd urb is submitted */
400 	if (cmdinfo->state & SUBMIT_CMD_URB) {
401 		uas_log_cmd_state(cmnd, "unexpected data cmplt", 0);
402 		goto out;
403 	}
404 
405 	if (status) {
406 		if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
407 			uas_log_cmd_state(cmnd, "data cmplt err", status);
408 		/* error: no data transfered */
409 		sdb->resid = sdb->length;
410 	} else {
411 		sdb->resid = sdb->length - urb->actual_length;
412 	}
413 	uas_try_complete(cmnd, __func__);
414 out:
415 	usb_free_urb(urb);
416 	spin_unlock_irqrestore(&devinfo->lock, flags);
417 }
418 
uas_cmd_cmplt(struct urb * urb)419 static void uas_cmd_cmplt(struct urb *urb)
420 {
421 	if (urb->status)
422 		dev_err(&urb->dev->dev, "cmd cmplt err %d\n", urb->status);
423 
424 	usb_free_urb(urb);
425 }
426 
uas_alloc_data_urb(struct uas_dev_info * devinfo,gfp_t gfp,struct scsi_cmnd * cmnd,enum dma_data_direction dir)427 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
428 				      struct scsi_cmnd *cmnd,
429 				      enum dma_data_direction dir)
430 {
431 	struct usb_device *udev = devinfo->udev;
432 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
433 	struct urb *urb = usb_alloc_urb(0, gfp);
434 	struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
435 		? scsi_in(cmnd) : scsi_out(cmnd);
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 
uas_alloc_sense_urb(struct uas_dev_info * devinfo,gfp_t gfp,struct scsi_cmnd * cmnd)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 
uas_alloc_cmd_urb(struct uas_dev_info * devinfo,gfp_t gfp,struct scsi_cmnd * cmnd)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 
uas_submit_sense_urb(struct scsi_cmnd * cmnd,gfp_t gfp)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 
uas_submit_urbs(struct scsi_cmnd * cmnd,struct uas_dev_info * devinfo,gfp_t gfp)542 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
543 			   struct uas_dev_info *devinfo, gfp_t gfp)
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);
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,
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);
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,
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);
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, 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);
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 
uas_queuecommand_lck(struct scsi_cmnd * cmnd,void (* done)(struct scsi_cmnd *))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 		goto zombie;
650 	}
651 
652 	/* Find a free uas-tag */
653 	for (idx = 0; idx < devinfo->qdepth; idx++) {
654 		if (!devinfo->cmnd[idx])
655 			break;
656 	}
657 	if (idx == devinfo->qdepth) {
658 		spin_unlock_irqrestore(&devinfo->lock, flags);
659 		return SCSI_MLQUEUE_DEVICE_BUSY;
660 	}
661 
662 	cmnd->scsi_done = done;
663 
664 	memset(cmdinfo, 0, sizeof(*cmdinfo));
665 	cmdinfo->uas_tag = idx + 1; /* uas-tag == usb-stream-id, so 1 based */
666 	cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
667 
668 	switch (cmnd->sc_data_direction) {
669 	case DMA_FROM_DEVICE:
670 		cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
671 		break;
672 	case DMA_BIDIRECTIONAL:
673 		cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
674 	case DMA_TO_DEVICE:
675 		cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
676 	case DMA_NONE:
677 		break;
678 	}
679 
680 	if (!devinfo->use_streams)
681 		cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
682 
683 	err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
684 	/*
685 	 * in case of fatal errors the SCSI layer is peculiar
686 	 * a command that has finished is a success for the purpose
687 	 * of queueing, no matter how fatal the error
688 	 */
689 	if (err == -ENODEV) {
690 		cmnd->result = DID_ERROR << 16;
691 		cmnd->scsi_done(cmnd);
692 		goto zombie;
693 	}
694 	if (err) {
695 		/* If we did nothing, give up now */
696 		if (cmdinfo->state & SUBMIT_STATUS_URB) {
697 			spin_unlock_irqrestore(&devinfo->lock, flags);
698 			return SCSI_MLQUEUE_DEVICE_BUSY;
699 		}
700 		uas_add_work(cmdinfo);
701 	}
702 
703 	devinfo->cmnd[idx] = cmnd;
704 zombie:
705 	spin_unlock_irqrestore(&devinfo->lock, flags);
706 	return 0;
707 }
708 
DEF_SCSI_QCMD(uas_queuecommand)709 static DEF_SCSI_QCMD(uas_queuecommand)
710 
711 /*
712  * For now we do not support actually sending an abort to the device, so
713  * this eh always fails. Still we must define it to make sure that we've
714  * dropped all references to the cmnd in question once this function exits.
715  */
716 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
717 {
718 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
719 	struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
720 	struct urb *data_in_urb = NULL;
721 	struct urb *data_out_urb = NULL;
722 	unsigned long flags;
723 
724 	spin_lock_irqsave(&devinfo->lock, flags);
725 
726 	uas_log_cmd_state(cmnd, __func__, 0);
727 
728 	/* Ensure that try_complete does not call scsi_done */
729 	cmdinfo->state |= COMMAND_ABORTED;
730 
731 	/* Drop all refs to this cmnd, kill data urbs to break their ref */
732 	devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
733 	if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
734 		data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
735 	if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
736 		data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
737 
738 	uas_free_unsubmitted_urbs(cmnd);
739 
740 	spin_unlock_irqrestore(&devinfo->lock, flags);
741 
742 	if (data_in_urb) {
743 		usb_kill_urb(data_in_urb);
744 		usb_put_urb(data_in_urb);
745 	}
746 	if (data_out_urb) {
747 		usb_kill_urb(data_out_urb);
748 		usb_put_urb(data_out_urb);
749 	}
750 
751 	return FAILED;
752 }
753 
uas_eh_bus_reset_handler(struct scsi_cmnd * cmnd)754 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
755 {
756 	struct scsi_device *sdev = cmnd->device;
757 	struct uas_dev_info *devinfo = sdev->hostdata;
758 	struct usb_device *udev = devinfo->udev;
759 	unsigned long flags;
760 	int err;
761 
762 	err = usb_lock_device_for_reset(udev, devinfo->intf);
763 	if (err) {
764 		shost_printk(KERN_ERR, sdev->host,
765 			     "%s FAILED to get lock err %d\n", __func__, err);
766 		return FAILED;
767 	}
768 
769 	shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
770 
771 	spin_lock_irqsave(&devinfo->lock, flags);
772 	devinfo->resetting = 1;
773 	spin_unlock_irqrestore(&devinfo->lock, flags);
774 
775 	usb_kill_anchored_urbs(&devinfo->cmd_urbs);
776 	usb_kill_anchored_urbs(&devinfo->sense_urbs);
777 	usb_kill_anchored_urbs(&devinfo->data_urbs);
778 	uas_zap_pending(devinfo, DID_RESET);
779 
780 	err = usb_reset_device(udev);
781 
782 	spin_lock_irqsave(&devinfo->lock, flags);
783 	devinfo->resetting = 0;
784 	spin_unlock_irqrestore(&devinfo->lock, flags);
785 
786 	usb_unlock_device(udev);
787 
788 	if (err) {
789 		shost_printk(KERN_INFO, sdev->host, "%s FAILED err %d\n",
790 			     __func__, err);
791 		return FAILED;
792 	}
793 
794 	shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
795 	return SUCCESS;
796 }
797 
uas_target_alloc(struct scsi_target * starget)798 static int uas_target_alloc(struct scsi_target *starget)
799 {
800 	struct uas_dev_info *devinfo = (struct uas_dev_info *)
801 			dev_to_shost(starget->dev.parent)->hostdata;
802 
803 	if (devinfo->flags & US_FL_NO_REPORT_LUNS)
804 		starget->no_report_luns = 1;
805 
806 	return 0;
807 }
808 
uas_slave_alloc(struct scsi_device * sdev)809 static int uas_slave_alloc(struct scsi_device *sdev)
810 {
811 	struct uas_dev_info *devinfo =
812 		(struct uas_dev_info *)sdev->host->hostdata;
813 
814 	sdev->hostdata = devinfo;
815 
816 	/*
817 	 * The protocol has no requirements on alignment in the strict sense.
818 	 * Controllers may or may not have alignment restrictions.
819 	 * As this is not exported, we use an extremely conservative guess.
820 	 */
821 	blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
822 
823 	if (devinfo->flags & US_FL_MAX_SECTORS_64)
824 		blk_queue_max_hw_sectors(sdev->request_queue, 64);
825 	else if (devinfo->flags & US_FL_MAX_SECTORS_240)
826 		blk_queue_max_hw_sectors(sdev->request_queue, 240);
827 
828 	return 0;
829 }
830 
uas_slave_configure(struct scsi_device * sdev)831 static int uas_slave_configure(struct scsi_device *sdev)
832 {
833 	struct uas_dev_info *devinfo = sdev->hostdata;
834 
835 	if (devinfo->flags & US_FL_NO_REPORT_OPCODES)
836 		sdev->no_report_opcodes = 1;
837 
838 	/* A few buggy USB-ATA bridges don't understand FUA */
839 	if (devinfo->flags & US_FL_BROKEN_FUA)
840 		sdev->broken_fua = 1;
841 
842 	scsi_change_queue_depth(sdev, devinfo->qdepth - 2);
843 	return 0;
844 }
845 
846 static struct scsi_host_template uas_host_template = {
847 	.module = THIS_MODULE,
848 	.name = "uas",
849 	.queuecommand = uas_queuecommand,
850 	.target_alloc = uas_target_alloc,
851 	.slave_alloc = uas_slave_alloc,
852 	.slave_configure = uas_slave_configure,
853 	.eh_abort_handler = uas_eh_abort_handler,
854 	.eh_bus_reset_handler = uas_eh_bus_reset_handler,
855 	.can_queue = MAX_CMNDS,
856 	.this_id = -1,
857 	.sg_tablesize = SG_NONE,
858 	.skip_settle_delay = 1,
859 };
860 
861 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
862 		    vendorName, productName, useProtocol, useTransport, \
863 		    initFunction, flags) \
864 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
865 	.driver_info = (flags) }
866 
867 static struct usb_device_id uas_usb_ids[] = {
868 #	include "unusual_uas.h"
869 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
870 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
871 	{ }
872 };
873 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
874 
875 #undef UNUSUAL_DEV
876 
uas_switch_interface(struct usb_device * udev,struct usb_interface * intf)877 static int uas_switch_interface(struct usb_device *udev,
878 				struct usb_interface *intf)
879 {
880 	struct usb_host_interface *alt;
881 
882 	alt = uas_find_uas_alt_setting(intf);
883 	if (!alt)
884 		return -ENODEV;
885 
886 	return usb_set_interface(udev, alt->desc.bInterfaceNumber,
887 			alt->desc.bAlternateSetting);
888 }
889 
uas_configure_endpoints(struct uas_dev_info * devinfo)890 static int uas_configure_endpoints(struct uas_dev_info *devinfo)
891 {
892 	struct usb_host_endpoint *eps[4] = { };
893 	struct usb_device *udev = devinfo->udev;
894 	int r;
895 
896 	r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
897 	if (r)
898 		return r;
899 
900 	devinfo->cmd_pipe = usb_sndbulkpipe(udev,
901 					    usb_endpoint_num(&eps[0]->desc));
902 	devinfo->status_pipe = usb_rcvbulkpipe(udev,
903 					    usb_endpoint_num(&eps[1]->desc));
904 	devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
905 					    usb_endpoint_num(&eps[2]->desc));
906 	devinfo->data_out_pipe = usb_sndbulkpipe(udev,
907 					    usb_endpoint_num(&eps[3]->desc));
908 
909 	if (udev->speed < USB_SPEED_SUPER) {
910 		devinfo->qdepth = 32;
911 		devinfo->use_streams = 0;
912 	} else {
913 		devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
914 						    3, MAX_CMNDS, GFP_NOIO);
915 		if (devinfo->qdepth < 0)
916 			return devinfo->qdepth;
917 		devinfo->use_streams = 1;
918 	}
919 
920 	return 0;
921 }
922 
uas_free_streams(struct uas_dev_info * devinfo)923 static void uas_free_streams(struct uas_dev_info *devinfo)
924 {
925 	struct usb_device *udev = devinfo->udev;
926 	struct usb_host_endpoint *eps[3];
927 
928 	eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
929 	eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
930 	eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
931 	usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
932 }
933 
uas_probe(struct usb_interface * intf,const struct usb_device_id * id)934 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
935 {
936 	int result = -ENOMEM;
937 	struct Scsi_Host *shost = NULL;
938 	struct uas_dev_info *devinfo;
939 	struct usb_device *udev = interface_to_usbdev(intf);
940 	unsigned long dev_flags;
941 
942 	if (!uas_use_uas_driver(intf, id, &dev_flags))
943 		return -ENODEV;
944 
945 	if (uas_switch_interface(udev, intf))
946 		return -ENODEV;
947 
948 	shost = scsi_host_alloc(&uas_host_template,
949 				sizeof(struct uas_dev_info));
950 	if (!shost)
951 		goto set_alt0;
952 
953 	shost->max_cmd_len = 16 + 252;
954 	shost->max_id = 1;
955 	shost->max_lun = 256;
956 	shost->max_channel = 0;
957 	shost->sg_tablesize = udev->bus->sg_tablesize;
958 
959 	devinfo = (struct uas_dev_info *)shost->hostdata;
960 	devinfo->intf = intf;
961 	devinfo->udev = udev;
962 	devinfo->resetting = 0;
963 	devinfo->shutdown = 0;
964 	devinfo->flags = dev_flags;
965 	init_usb_anchor(&devinfo->cmd_urbs);
966 	init_usb_anchor(&devinfo->sense_urbs);
967 	init_usb_anchor(&devinfo->data_urbs);
968 	spin_lock_init(&devinfo->lock);
969 	INIT_WORK(&devinfo->work, uas_do_work);
970 	INIT_WORK(&devinfo->scan_work, uas_scan_work);
971 
972 	result = uas_configure_endpoints(devinfo);
973 	if (result)
974 		goto set_alt0;
975 
976 	/*
977 	 * 1 tag is reserved for untagged commands +
978 	 * 1 tag to avoid off by one errors in some bridge firmwares
979 	 */
980 	shost->can_queue = devinfo->qdepth - 2;
981 
982 	usb_set_intfdata(intf, shost);
983 	result = scsi_add_host(shost, &intf->dev);
984 	if (result)
985 		goto free_streams;
986 
987 	/* Submit the delayed_work for SCSI-device scanning */
988 	schedule_work(&devinfo->scan_work);
989 
990 	return result;
991 
992 free_streams:
993 	uas_free_streams(devinfo);
994 	usb_set_intfdata(intf, NULL);
995 set_alt0:
996 	usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
997 	if (shost)
998 		scsi_host_put(shost);
999 	return result;
1000 }
1001 
uas_cmnd_list_empty(struct uas_dev_info * devinfo)1002 static int uas_cmnd_list_empty(struct uas_dev_info *devinfo)
1003 {
1004 	unsigned long flags;
1005 	int i, r = 1;
1006 
1007 	spin_lock_irqsave(&devinfo->lock, flags);
1008 
1009 	for (i = 0; i < devinfo->qdepth; i++) {
1010 		if (devinfo->cmnd[i]) {
1011 			r = 0; /* Not empty */
1012 			break;
1013 		}
1014 	}
1015 
1016 	spin_unlock_irqrestore(&devinfo->lock, flags);
1017 
1018 	return r;
1019 }
1020 
1021 /*
1022  * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily
1023  * get empty while there still is more work to do due to sense-urbs completing
1024  * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty.
1025  */
uas_wait_for_pending_cmnds(struct uas_dev_info * devinfo)1026 static int uas_wait_for_pending_cmnds(struct uas_dev_info *devinfo)
1027 {
1028 	unsigned long start_time;
1029 	int r;
1030 
1031 	start_time = jiffies;
1032 	do {
1033 		flush_work(&devinfo->work);
1034 
1035 		r = usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000);
1036 		if (r == 0)
1037 			return -ETIME;
1038 
1039 		r = usb_wait_anchor_empty_timeout(&devinfo->data_urbs, 500);
1040 		if (r == 0)
1041 			return -ETIME;
1042 
1043 		if (time_after(jiffies, start_time + 5 * HZ))
1044 			return -ETIME;
1045 	} while (!uas_cmnd_list_empty(devinfo));
1046 
1047 	return 0;
1048 }
1049 
uas_pre_reset(struct usb_interface * intf)1050 static int uas_pre_reset(struct usb_interface *intf)
1051 {
1052 	struct Scsi_Host *shost = usb_get_intfdata(intf);
1053 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1054 	unsigned long flags;
1055 
1056 	if (devinfo->shutdown)
1057 		return 0;
1058 
1059 	/* Block new requests */
1060 	spin_lock_irqsave(shost->host_lock, flags);
1061 	scsi_block_requests(shost);
1062 	spin_unlock_irqrestore(shost->host_lock, flags);
1063 
1064 	if (uas_wait_for_pending_cmnds(devinfo) != 0) {
1065 		shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1066 		scsi_unblock_requests(shost);
1067 		return 1;
1068 	}
1069 
1070 	uas_free_streams(devinfo);
1071 
1072 	return 0;
1073 }
1074 
uas_post_reset(struct usb_interface * intf)1075 static int uas_post_reset(struct usb_interface *intf)
1076 {
1077 	struct Scsi_Host *shost = usb_get_intfdata(intf);
1078 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1079 	unsigned long flags;
1080 	int err;
1081 
1082 	if (devinfo->shutdown)
1083 		return 0;
1084 
1085 	err = uas_configure_endpoints(devinfo);
1086 	if (err && err != -ENODEV)
1087 		shost_printk(KERN_ERR, shost,
1088 			     "%s: alloc streams error %d after reset",
1089 			     __func__, err);
1090 
1091 	/* we must unblock the host in every case lest we deadlock */
1092 	spin_lock_irqsave(shost->host_lock, flags);
1093 	scsi_report_bus_reset(shost, 0);
1094 	spin_unlock_irqrestore(shost->host_lock, flags);
1095 
1096 	scsi_unblock_requests(shost);
1097 
1098 	return err ? 1 : 0;
1099 }
1100 
uas_suspend(struct usb_interface * intf,pm_message_t message)1101 static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1102 {
1103 	struct Scsi_Host *shost = usb_get_intfdata(intf);
1104 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1105 
1106 	if (uas_wait_for_pending_cmnds(devinfo) != 0) {
1107 		shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1108 		return -ETIME;
1109 	}
1110 
1111 	return 0;
1112 }
1113 
uas_resume(struct usb_interface * intf)1114 static int uas_resume(struct usb_interface *intf)
1115 {
1116 	return 0;
1117 }
1118 
uas_reset_resume(struct usb_interface * intf)1119 static int uas_reset_resume(struct usb_interface *intf)
1120 {
1121 	struct Scsi_Host *shost = usb_get_intfdata(intf);
1122 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1123 	unsigned long flags;
1124 	int err;
1125 
1126 	err = uas_configure_endpoints(devinfo);
1127 	if (err) {
1128 		shost_printk(KERN_ERR, shost,
1129 			     "%s: alloc streams error %d after reset",
1130 			     __func__, err);
1131 		return -EIO;
1132 	}
1133 
1134 	spin_lock_irqsave(shost->host_lock, flags);
1135 	scsi_report_bus_reset(shost, 0);
1136 	spin_unlock_irqrestore(shost->host_lock, flags);
1137 
1138 	return 0;
1139 }
1140 
uas_disconnect(struct usb_interface * intf)1141 static void uas_disconnect(struct usb_interface *intf)
1142 {
1143 	struct Scsi_Host *shost = usb_get_intfdata(intf);
1144 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1145 	unsigned long flags;
1146 
1147 	spin_lock_irqsave(&devinfo->lock, flags);
1148 	devinfo->resetting = 1;
1149 	spin_unlock_irqrestore(&devinfo->lock, flags);
1150 
1151 	cancel_work_sync(&devinfo->work);
1152 	usb_kill_anchored_urbs(&devinfo->cmd_urbs);
1153 	usb_kill_anchored_urbs(&devinfo->sense_urbs);
1154 	usb_kill_anchored_urbs(&devinfo->data_urbs);
1155 	uas_zap_pending(devinfo, DID_NO_CONNECT);
1156 
1157 	/*
1158 	 * Prevent SCSI scanning (if it hasn't started yet)
1159 	 * or wait for the SCSI-scanning routine to stop.
1160 	 */
1161 	cancel_work_sync(&devinfo->scan_work);
1162 
1163 	scsi_remove_host(shost);
1164 	uas_free_streams(devinfo);
1165 	scsi_host_put(shost);
1166 }
1167 
1168 /*
1169  * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1170  * hang on reboot when the device is still in uas mode. Note the reset is
1171  * necessary as some devices won't revert to usb-storage mode without it.
1172  */
uas_shutdown(struct device * dev)1173 static void uas_shutdown(struct device *dev)
1174 {
1175 	struct usb_interface *intf = to_usb_interface(dev);
1176 	struct usb_device *udev = interface_to_usbdev(intf);
1177 	struct Scsi_Host *shost = usb_get_intfdata(intf);
1178 	struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1179 
1180 	if (system_state != SYSTEM_RESTART)
1181 		return;
1182 
1183 	devinfo->shutdown = 1;
1184 	uas_free_streams(devinfo);
1185 	usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1186 	usb_reset_device(udev);
1187 }
1188 
1189 static struct usb_driver uas_driver = {
1190 	.name = "uas",
1191 	.probe = uas_probe,
1192 	.disconnect = uas_disconnect,
1193 	.pre_reset = uas_pre_reset,
1194 	.post_reset = uas_post_reset,
1195 	.suspend = uas_suspend,
1196 	.resume = uas_resume,
1197 	.reset_resume = uas_reset_resume,
1198 	.drvwrap.driver.shutdown = uas_shutdown,
1199 	.id_table = uas_usb_ids,
1200 };
1201 
uas_init(void)1202 static int __init uas_init(void)
1203 {
1204 	int rv;
1205 
1206 	workqueue = alloc_workqueue("uas", WQ_MEM_RECLAIM, 0);
1207 	if (!workqueue)
1208 		return -ENOMEM;
1209 
1210 	rv = usb_register(&uas_driver);
1211 	if (rv) {
1212 		destroy_workqueue(workqueue);
1213 		return -ENOMEM;
1214 	}
1215 
1216 	return 0;
1217 }
1218 
uas_exit(void)1219 static void __exit uas_exit(void)
1220 {
1221 	usb_deregister(&uas_driver);
1222 	destroy_workqueue(workqueue);
1223 }
1224 
1225 module_init(uas_init);
1226 module_exit(uas_exit);
1227 
1228 MODULE_LICENSE("GPL");
1229 MODULE_AUTHOR(
1230 	"Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");
1231