xref: /openbmc/linux/drivers/usb/storage/uas.c (revision 95e9fd10)
1 /*
2  * USB Attached SCSI
3  * Note that this is not the same as the USB Mass Storage driver
4  *
5  * Copyright Matthew Wilcox for Intel Corp, 2010
6  * Copyright Sarah Sharp for Intel Corp, 2010
7  *
8  * Distributed under the terms of the GNU GPL, version two.
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/hcd.h>
17 #include <linux/usb/storage.h>
18 #include <linux/usb/uas.h>
19 
20 #include <scsi/scsi.h>
21 #include <scsi/scsi_dbg.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <scsi/scsi_device.h>
24 #include <scsi/scsi_host.h>
25 #include <scsi/scsi_tcq.h>
26 
27 /*
28  * The r00-r01c specs define this version of the SENSE IU data structure.
29  * It's still in use by several different firmware releases.
30  */
31 struct sense_iu_old {
32 	__u8 iu_id;
33 	__u8 rsvd1;
34 	__be16 tag;
35 	__be16 len;
36 	__u8 status;
37 	__u8 service_response;
38 	__u8 sense[SCSI_SENSE_BUFFERSIZE];
39 };
40 
41 struct uas_dev_info {
42 	struct usb_interface *intf;
43 	struct usb_device *udev;
44 	struct usb_anchor sense_urbs;
45 	struct usb_anchor data_urbs;
46 	int qdepth, resetting;
47 	struct response_ui response;
48 	unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
49 	unsigned use_streams:1;
50 	unsigned uas_sense_old:1;
51 	struct scsi_cmnd *cmnd;
52 };
53 
54 enum {
55 	SUBMIT_STATUS_URB	= (1 << 1),
56 	ALLOC_DATA_IN_URB	= (1 << 2),
57 	SUBMIT_DATA_IN_URB	= (1 << 3),
58 	ALLOC_DATA_OUT_URB	= (1 << 4),
59 	SUBMIT_DATA_OUT_URB	= (1 << 5),
60 	ALLOC_CMD_URB		= (1 << 6),
61 	SUBMIT_CMD_URB		= (1 << 7),
62 	COMMAND_INFLIGHT        = (1 << 8),
63 	DATA_IN_URB_INFLIGHT    = (1 << 9),
64 	DATA_OUT_URB_INFLIGHT   = (1 << 10),
65 	COMMAND_COMPLETED       = (1 << 11),
66 };
67 
68 /* Overrides scsi_pointer */
69 struct uas_cmd_info {
70 	unsigned int state;
71 	unsigned int stream;
72 	unsigned int aborted;
73 	struct urb *cmd_urb;
74 	struct urb *data_in_urb;
75 	struct urb *data_out_urb;
76 	struct list_head list;
77 };
78 
79 /* I hate forward declarations, but I actually have a loop */
80 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
81 				struct uas_dev_info *devinfo, gfp_t gfp);
82 static void uas_do_work(struct work_struct *work);
83 
84 static DECLARE_WORK(uas_work, uas_do_work);
85 static DEFINE_SPINLOCK(uas_work_lock);
86 static LIST_HEAD(uas_work_list);
87 
88 static void uas_do_work(struct work_struct *work)
89 {
90 	struct uas_cmd_info *cmdinfo;
91 	struct uas_cmd_info *temp;
92 	struct list_head list;
93 	int err;
94 
95 	spin_lock_irq(&uas_work_lock);
96 	list_replace_init(&uas_work_list, &list);
97 	spin_unlock_irq(&uas_work_lock);
98 
99 	list_for_each_entry_safe(cmdinfo, temp, &list, list) {
100 		struct scsi_pointer *scp = (void *)cmdinfo;
101 		struct scsi_cmnd *cmnd = container_of(scp,
102 							struct scsi_cmnd, SCp);
103 		err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
104 		if (err) {
105 			list_del(&cmdinfo->list);
106 			spin_lock_irq(&uas_work_lock);
107 			list_add_tail(&cmdinfo->list, &uas_work_list);
108 			spin_unlock_irq(&uas_work_lock);
109 			schedule_work(&uas_work);
110 		}
111 	}
112 }
113 
114 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
115 {
116 	struct sense_iu *sense_iu = urb->transfer_buffer;
117 	struct scsi_device *sdev = cmnd->device;
118 
119 	if (urb->actual_length > 16) {
120 		unsigned len = be16_to_cpup(&sense_iu->len);
121 		if (len + 16 != urb->actual_length) {
122 			int newlen = min(len + 16, urb->actual_length) - 16;
123 			if (newlen < 0)
124 				newlen = 0;
125 			sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
126 				"disagrees with IU sense data length %d, "
127 				"using %d bytes of sense data\n", __func__,
128 					urb->actual_length, len, newlen);
129 			len = newlen;
130 		}
131 		memcpy(cmnd->sense_buffer, sense_iu->sense, len);
132 	}
133 
134 	cmnd->result = sense_iu->status;
135 }
136 
137 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
138 {
139 	struct sense_iu_old *sense_iu = urb->transfer_buffer;
140 	struct scsi_device *sdev = cmnd->device;
141 
142 	if (urb->actual_length > 8) {
143 		unsigned len = be16_to_cpup(&sense_iu->len) - 2;
144 		if (len + 8 != urb->actual_length) {
145 			int newlen = min(len + 8, urb->actual_length) - 8;
146 			if (newlen < 0)
147 				newlen = 0;
148 			sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
149 				"disagrees with IU sense data length %d, "
150 				"using %d bytes of sense data\n", __func__,
151 					urb->actual_length, len, newlen);
152 			len = newlen;
153 		}
154 		memcpy(cmnd->sense_buffer, sense_iu->sense, len);
155 	}
156 
157 	cmnd->result = sense_iu->status;
158 }
159 
160 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
161 {
162 	struct uas_cmd_info *ci = (void *)&cmnd->SCp;
163 
164 	scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
165 		    "%s%s%s%s%s%s%s%s%s%s%s\n",
166 		    caller, cmnd, cmnd->request->tag,
167 		    (ci->state & SUBMIT_STATUS_URB)     ? " s-st"  : "",
168 		    (ci->state & ALLOC_DATA_IN_URB)     ? " a-in"  : "",
169 		    (ci->state & SUBMIT_DATA_IN_URB)    ? " s-in"  : "",
170 		    (ci->state & ALLOC_DATA_OUT_URB)    ? " a-out" : "",
171 		    (ci->state & SUBMIT_DATA_OUT_URB)   ? " s-out" : "",
172 		    (ci->state & ALLOC_CMD_URB)         ? " a-cmd" : "",
173 		    (ci->state & SUBMIT_CMD_URB)        ? " s-cmd" : "",
174 		    (ci->state & COMMAND_INFLIGHT)      ? " CMD"   : "",
175 		    (ci->state & DATA_IN_URB_INFLIGHT)  ? " IN"    : "",
176 		    (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT"   : "",
177 		    (ci->state & COMMAND_COMPLETED)     ? " done"  : "");
178 }
179 
180 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
181 {
182 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
183 
184 	if (cmdinfo->state & (COMMAND_INFLIGHT |
185 			      DATA_IN_URB_INFLIGHT |
186 			      DATA_OUT_URB_INFLIGHT))
187 		return -EBUSY;
188 	BUG_ON(cmdinfo->state & COMMAND_COMPLETED);
189 	cmdinfo->state |= COMMAND_COMPLETED;
190 	usb_free_urb(cmdinfo->data_in_urb);
191 	usb_free_urb(cmdinfo->data_out_urb);
192 	cmnd->scsi_done(cmnd);
193 	return 0;
194 }
195 
196 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
197 			  unsigned direction)
198 {
199 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
200 	int err;
201 
202 	cmdinfo->state |= direction | SUBMIT_STATUS_URB;
203 	err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
204 	if (err) {
205 		spin_lock(&uas_work_lock);
206 		list_add_tail(&cmdinfo->list, &uas_work_list);
207 		spin_unlock(&uas_work_lock);
208 		schedule_work(&uas_work);
209 	}
210 }
211 
212 static void uas_stat_cmplt(struct urb *urb)
213 {
214 	struct iu *iu = urb->transfer_buffer;
215 	struct Scsi_Host *shost = urb->context;
216 	struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
217 	struct scsi_cmnd *cmnd;
218 	struct uas_cmd_info *cmdinfo;
219 	u16 tag;
220 
221 	if (urb->status) {
222 		dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
223 		usb_free_urb(urb);
224 		return;
225 	}
226 
227 	if (devinfo->resetting) {
228 		usb_free_urb(urb);
229 		return;
230 	}
231 
232 	tag = be16_to_cpup(&iu->tag) - 1;
233 	if (tag == 0)
234 		cmnd = devinfo->cmnd;
235 	else
236 		cmnd = scsi_host_find_tag(shost, tag - 1);
237 	if (!cmnd) {
238 		if (iu->iu_id != IU_ID_RESPONSE) {
239 			usb_free_urb(urb);
240 			return;
241 		}
242 	} else {
243 		cmdinfo = (void *)&cmnd->SCp;
244 	}
245 
246 	switch (iu->iu_id) {
247 	case IU_ID_STATUS:
248 		if (devinfo->cmnd == cmnd)
249 			devinfo->cmnd = NULL;
250 
251 		if (urb->actual_length < 16)
252 			devinfo->uas_sense_old = 1;
253 		if (devinfo->uas_sense_old)
254 			uas_sense_old(urb, cmnd);
255 		else
256 			uas_sense(urb, cmnd);
257 		if (cmnd->result != 0) {
258 			/* cancel data transfers on error */
259 			if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
260 				usb_unlink_urb(cmdinfo->data_in_urb);
261 			if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
262 				usb_unlink_urb(cmdinfo->data_out_urb);
263 		}
264 		cmdinfo->state &= ~COMMAND_INFLIGHT;
265 		uas_try_complete(cmnd, __func__);
266 		break;
267 	case IU_ID_READ_READY:
268 		uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
269 		break;
270 	case IU_ID_WRITE_READY:
271 		uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
272 		break;
273 	case IU_ID_RESPONSE:
274 		/* store results for uas_eh_task_mgmt() */
275 		memcpy(&devinfo->response, iu, sizeof(devinfo->response));
276 		break;
277 	default:
278 		scmd_printk(KERN_ERR, cmnd,
279 			"Bogus IU (%d) received on status pipe\n", iu->iu_id);
280 	}
281 	usb_free_urb(urb);
282 }
283 
284 static void uas_data_cmplt(struct urb *urb)
285 {
286 	struct scsi_cmnd *cmnd = urb->context;
287 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
288 	struct scsi_data_buffer *sdb = NULL;
289 
290 	if (cmdinfo->data_in_urb == urb) {
291 		sdb = scsi_in(cmnd);
292 		cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
293 	} else if (cmdinfo->data_out_urb == urb) {
294 		sdb = scsi_out(cmnd);
295 		cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
296 	}
297 	BUG_ON(sdb == NULL);
298 	if (urb->status) {
299 		/* error: no data transfered */
300 		sdb->resid = sdb->length;
301 	} else {
302 		sdb->resid = sdb->length - urb->actual_length;
303 	}
304 	if (cmdinfo->aborted) {
305 		return;
306 	}
307 	uas_try_complete(cmnd, __func__);
308 }
309 
310 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
311 				      unsigned int pipe, u16 stream_id,
312 				      struct scsi_cmnd *cmnd,
313 				      enum dma_data_direction dir)
314 {
315 	struct usb_device *udev = devinfo->udev;
316 	struct urb *urb = usb_alloc_urb(0, gfp);
317 	struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
318 		? scsi_in(cmnd) : scsi_out(cmnd);
319 
320 	if (!urb)
321 		goto out;
322 	usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
323 			  uas_data_cmplt, cmnd);
324 	if (devinfo->use_streams)
325 		urb->stream_id = stream_id;
326 	urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
327 	urb->sg = sdb->table.sgl;
328  out:
329 	return urb;
330 }
331 
332 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
333 				       struct Scsi_Host *shost, u16 stream_id)
334 {
335 	struct usb_device *udev = devinfo->udev;
336 	struct urb *urb = usb_alloc_urb(0, gfp);
337 	struct sense_iu *iu;
338 
339 	if (!urb)
340 		goto out;
341 
342 	iu = kzalloc(sizeof(*iu), gfp);
343 	if (!iu)
344 		goto free;
345 
346 	usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
347 						uas_stat_cmplt, shost);
348 	urb->stream_id = stream_id;
349 	urb->transfer_flags |= URB_FREE_BUFFER;
350  out:
351 	return urb;
352  free:
353 	usb_free_urb(urb);
354 	return NULL;
355 }
356 
357 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
358 					struct scsi_cmnd *cmnd, u16 stream_id)
359 {
360 	struct usb_device *udev = devinfo->udev;
361 	struct scsi_device *sdev = cmnd->device;
362 	struct urb *urb = usb_alloc_urb(0, gfp);
363 	struct command_iu *iu;
364 	int len;
365 
366 	if (!urb)
367 		goto out;
368 
369 	len = cmnd->cmd_len - 16;
370 	if (len < 0)
371 		len = 0;
372 	len = ALIGN(len, 4);
373 	iu = kzalloc(sizeof(*iu) + len, gfp);
374 	if (!iu)
375 		goto free;
376 
377 	iu->iu_id = IU_ID_COMMAND;
378 	if (blk_rq_tagged(cmnd->request))
379 		iu->tag = cpu_to_be16(cmnd->request->tag + 2);
380 	else
381 		iu->tag = cpu_to_be16(1);
382 	iu->prio_attr = UAS_SIMPLE_TAG;
383 	iu->len = len;
384 	int_to_scsilun(sdev->lun, &iu->lun);
385 	memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
386 
387 	usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
388 							usb_free_urb, NULL);
389 	urb->transfer_flags |= URB_FREE_BUFFER;
390  out:
391 	return urb;
392  free:
393 	usb_free_urb(urb);
394 	return NULL;
395 }
396 
397 static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
398 			       u8 function, u16 stream_id)
399 {
400 	struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
401 	struct usb_device *udev = devinfo->udev;
402 	struct urb *urb = usb_alloc_urb(0, gfp);
403 	struct task_mgmt_iu *iu;
404 	int err = -ENOMEM;
405 
406 	if (!urb)
407 		goto err;
408 
409 	iu = kzalloc(sizeof(*iu), gfp);
410 	if (!iu)
411 		goto err;
412 
413 	iu->iu_id = IU_ID_TASK_MGMT;
414 	iu->tag = cpu_to_be16(stream_id);
415 	int_to_scsilun(cmnd->device->lun, &iu->lun);
416 
417 	iu->function = function;
418 	switch (function) {
419 	case TMF_ABORT_TASK:
420 		if (blk_rq_tagged(cmnd->request))
421 			iu->task_tag = cpu_to_be16(cmnd->request->tag + 2);
422 		else
423 			iu->task_tag = cpu_to_be16(1);
424 		break;
425 	}
426 
427 	usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
428 			  usb_free_urb, NULL);
429 	urb->transfer_flags |= URB_FREE_BUFFER;
430 
431 	err = usb_submit_urb(urb, gfp);
432 	if (err)
433 		goto err;
434 
435 	return 0;
436 
437 err:
438 	usb_free_urb(urb);
439 	return err;
440 }
441 
442 /*
443  * Why should I request the Status IU before sending the Command IU?  Spec
444  * says to, but also says the device may receive them in any order.  Seems
445  * daft to me.
446  */
447 
448 static int uas_submit_sense_urb(struct Scsi_Host *shost,
449 				gfp_t gfp, unsigned int stream)
450 {
451 	struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
452 	struct urb *urb;
453 
454 	urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
455 	if (!urb)
456 		return SCSI_MLQUEUE_DEVICE_BUSY;
457 	if (usb_submit_urb(urb, gfp)) {
458 		shost_printk(KERN_INFO, shost,
459 			     "sense urb submission failure\n");
460 		usb_free_urb(urb);
461 		return SCSI_MLQUEUE_DEVICE_BUSY;
462 	}
463 	usb_anchor_urb(urb, &devinfo->sense_urbs);
464 	return 0;
465 }
466 
467 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
468 			   struct uas_dev_info *devinfo, gfp_t gfp)
469 {
470 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
471 	int err;
472 
473 	if (cmdinfo->state & SUBMIT_STATUS_URB) {
474 		err = uas_submit_sense_urb(cmnd->device->host, gfp,
475 					   cmdinfo->stream);
476 		if (err) {
477 			return err;
478 		}
479 		cmdinfo->state &= ~SUBMIT_STATUS_URB;
480 	}
481 
482 	if (cmdinfo->state & ALLOC_DATA_IN_URB) {
483 		cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
484 					devinfo->data_in_pipe, cmdinfo->stream,
485 					cmnd, DMA_FROM_DEVICE);
486 		if (!cmdinfo->data_in_urb)
487 			return SCSI_MLQUEUE_DEVICE_BUSY;
488 		cmdinfo->state &= ~ALLOC_DATA_IN_URB;
489 	}
490 
491 	if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
492 		if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
493 			scmd_printk(KERN_INFO, cmnd,
494 					"data in urb submission failure\n");
495 			return SCSI_MLQUEUE_DEVICE_BUSY;
496 		}
497 		cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
498 		cmdinfo->state |= DATA_IN_URB_INFLIGHT;
499 		usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
500 	}
501 
502 	if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
503 		cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
504 					devinfo->data_out_pipe, cmdinfo->stream,
505 					cmnd, DMA_TO_DEVICE);
506 		if (!cmdinfo->data_out_urb)
507 			return SCSI_MLQUEUE_DEVICE_BUSY;
508 		cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
509 	}
510 
511 	if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
512 		if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
513 			scmd_printk(KERN_INFO, cmnd,
514 					"data out urb submission failure\n");
515 			return SCSI_MLQUEUE_DEVICE_BUSY;
516 		}
517 		cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
518 		cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
519 		usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
520 	}
521 
522 	if (cmdinfo->state & ALLOC_CMD_URB) {
523 		cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
524 							cmdinfo->stream);
525 		if (!cmdinfo->cmd_urb)
526 			return SCSI_MLQUEUE_DEVICE_BUSY;
527 		cmdinfo->state &= ~ALLOC_CMD_URB;
528 	}
529 
530 	if (cmdinfo->state & SUBMIT_CMD_URB) {
531 		if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
532 			scmd_printk(KERN_INFO, cmnd,
533 					"cmd urb submission failure\n");
534 			return SCSI_MLQUEUE_DEVICE_BUSY;
535 		}
536 		cmdinfo->state &= ~SUBMIT_CMD_URB;
537 		cmdinfo->state |= COMMAND_INFLIGHT;
538 	}
539 
540 	return 0;
541 }
542 
543 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
544 					void (*done)(struct scsi_cmnd *))
545 {
546 	struct scsi_device *sdev = cmnd->device;
547 	struct uas_dev_info *devinfo = sdev->hostdata;
548 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
549 	int err;
550 
551 	BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
552 
553 	if (devinfo->cmnd)
554 		return SCSI_MLQUEUE_DEVICE_BUSY;
555 
556 	if (blk_rq_tagged(cmnd->request)) {
557 		cmdinfo->stream = cmnd->request->tag + 2;
558 	} else {
559 		devinfo->cmnd = cmnd;
560 		cmdinfo->stream = 1;
561 	}
562 
563 	cmnd->scsi_done = done;
564 
565 	cmdinfo->state = SUBMIT_STATUS_URB |
566 			ALLOC_CMD_URB | SUBMIT_CMD_URB;
567 	cmdinfo->aborted = 0;
568 
569 	switch (cmnd->sc_data_direction) {
570 	case DMA_FROM_DEVICE:
571 		cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
572 		break;
573 	case DMA_BIDIRECTIONAL:
574 		cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
575 	case DMA_TO_DEVICE:
576 		cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
577 	case DMA_NONE:
578 		break;
579 	}
580 
581 	if (!devinfo->use_streams) {
582 		cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
583 		cmdinfo->stream = 0;
584 	}
585 
586 	err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
587 	if (err) {
588 		/* If we did nothing, give up now */
589 		if (cmdinfo->state & SUBMIT_STATUS_URB) {
590 			return SCSI_MLQUEUE_DEVICE_BUSY;
591 		}
592 		spin_lock(&uas_work_lock);
593 		list_add_tail(&cmdinfo->list, &uas_work_list);
594 		spin_unlock(&uas_work_lock);
595 		schedule_work(&uas_work);
596 	}
597 
598 	return 0;
599 }
600 
601 static DEF_SCSI_QCMD(uas_queuecommand)
602 
603 static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
604 			    const char *fname, u8 function)
605 {
606 	struct Scsi_Host *shost = cmnd->device->host;
607 	struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
608 	u16 tag = 9999; /* FIXME */
609 
610 	memset(&devinfo->response, 0, sizeof(devinfo->response));
611 	if (uas_submit_sense_urb(shost, GFP_NOIO, tag)) {
612 		shost_printk(KERN_INFO, shost,
613 			     "%s: %s: submit sense urb failed\n",
614 			     __func__, fname);
615 		return FAILED;
616 	}
617 	if (uas_submit_task_urb(cmnd, GFP_NOIO, function, tag)) {
618 		shost_printk(KERN_INFO, shost,
619 			     "%s: %s: submit task mgmt urb failed\n",
620 			     __func__, fname);
621 		return FAILED;
622 	}
623 	if (0 == usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000)) {
624 		shost_printk(KERN_INFO, shost,
625 			     "%s: %s timed out\n", __func__, fname);
626 		return FAILED;
627 	}
628 	if (be16_to_cpu(devinfo->response.tag) != tag) {
629 		shost_printk(KERN_INFO, shost,
630 			     "%s: %s failed (wrong tag %d/%d)\n", __func__,
631 			     fname, be16_to_cpu(devinfo->response.tag), tag);
632 		return FAILED;
633 	}
634 	if (devinfo->response.response_code != RC_TMF_COMPLETE) {
635 		shost_printk(KERN_INFO, shost,
636 			     "%s: %s failed (rc 0x%x)\n", __func__,
637 			     fname, devinfo->response.response_code);
638 		return FAILED;
639 	}
640 	return SUCCESS;
641 }
642 
643 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
644 {
645 	struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
646 	int ret;
647 
648 	uas_log_cmd_state(cmnd, __func__);
649 	cmdinfo->aborted = 1;
650 	ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
651 	if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
652 		usb_kill_urb(cmdinfo->data_in_urb);
653 	if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
654 		usb_kill_urb(cmdinfo->data_out_urb);
655 	return ret;
656 }
657 
658 static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
659 {
660 	sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__);
661 	return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET",
662 				TMF_LOGICAL_UNIT_RESET);
663 }
664 
665 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
666 {
667 	struct scsi_device *sdev = cmnd->device;
668 	struct uas_dev_info *devinfo = sdev->hostdata;
669 	struct usb_device *udev = devinfo->udev;
670 	int err;
671 
672 	devinfo->resetting = 1;
673 	usb_kill_anchored_urbs(&devinfo->sense_urbs);
674 	usb_kill_anchored_urbs(&devinfo->data_urbs);
675 	err = usb_reset_device(udev);
676 	devinfo->resetting = 0;
677 
678 	if (err) {
679 		shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
680 		return FAILED;
681 	}
682 
683 	shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
684 	return SUCCESS;
685 }
686 
687 static int uas_slave_alloc(struct scsi_device *sdev)
688 {
689 	sdev->hostdata = (void *)sdev->host->hostdata[0];
690 	return 0;
691 }
692 
693 static int uas_slave_configure(struct scsi_device *sdev)
694 {
695 	struct uas_dev_info *devinfo = sdev->hostdata;
696 	scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
697 	scsi_activate_tcq(sdev, devinfo->qdepth - 2);
698 	return 0;
699 }
700 
701 static struct scsi_host_template uas_host_template = {
702 	.module = THIS_MODULE,
703 	.name = "uas",
704 	.queuecommand = uas_queuecommand,
705 	.slave_alloc = uas_slave_alloc,
706 	.slave_configure = uas_slave_configure,
707 	.eh_abort_handler = uas_eh_abort_handler,
708 	.eh_device_reset_handler = uas_eh_device_reset_handler,
709 	.eh_bus_reset_handler = uas_eh_bus_reset_handler,
710 	.can_queue = 65536,	/* Is there a limit on the _host_ ? */
711 	.this_id = -1,
712 	.sg_tablesize = SG_NONE,
713 	.cmd_per_lun = 1,	/* until we override it */
714 	.skip_settle_delay = 1,
715 	.ordered_tag = 1,
716 };
717 
718 static struct usb_device_id uas_usb_ids[] = {
719 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
720 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
721 	/* 0xaa is a prototype device I happen to have access to */
722 	{ USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
723 	{ }
724 };
725 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
726 
727 static int uas_is_interface(struct usb_host_interface *intf)
728 {
729 	return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
730 		intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
731 		intf->desc.bInterfaceProtocol == USB_PR_UAS);
732 }
733 
734 static int uas_isnt_supported(struct usb_device *udev)
735 {
736 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
737 
738 	dev_warn(&udev->dev, "The driver for the USB controller %s does not "
739 			"support scatter-gather which is\n",
740 			hcd->driver->description);
741 	dev_warn(&udev->dev, "required by the UAS driver. Please try an"
742 			"alternative USB controller if you wish to use UAS.\n");
743 	return -ENODEV;
744 }
745 
746 static int uas_switch_interface(struct usb_device *udev,
747 						struct usb_interface *intf)
748 {
749 	int i;
750 	int sg_supported = udev->bus->sg_tablesize != 0;
751 
752 	for (i = 0; i < intf->num_altsetting; i++) {
753 		struct usb_host_interface *alt = &intf->altsetting[i];
754 
755 		if (uas_is_interface(alt)) {
756 			if (!sg_supported)
757 				return uas_isnt_supported(udev);
758 			return usb_set_interface(udev,
759 						alt->desc.bInterfaceNumber,
760 						alt->desc.bAlternateSetting);
761 		}
762 	}
763 
764 	return -ENODEV;
765 }
766 
767 static void uas_configure_endpoints(struct uas_dev_info *devinfo)
768 {
769 	struct usb_host_endpoint *eps[4] = { };
770 	struct usb_interface *intf = devinfo->intf;
771 	struct usb_device *udev = devinfo->udev;
772 	struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
773 	unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
774 
775 	devinfo->uas_sense_old = 0;
776 	devinfo->cmnd = NULL;
777 
778 	for (i = 0; i < n_endpoints; i++) {
779 		unsigned char *extra = endpoint[i].extra;
780 		int len = endpoint[i].extralen;
781 		while (len > 1) {
782 			if (extra[1] == USB_DT_PIPE_USAGE) {
783 				unsigned pipe_id = extra[2];
784 				if (pipe_id > 0 && pipe_id < 5)
785 					eps[pipe_id - 1] = &endpoint[i];
786 				break;
787 			}
788 			len -= extra[0];
789 			extra += extra[0];
790 		}
791 	}
792 
793 	/*
794 	 * Assume that if we didn't find a control pipe descriptor, we're
795 	 * using a device with old firmware that happens to be set up like
796 	 * this.
797 	 */
798 	if (!eps[0]) {
799 		devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
800 		devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
801 		devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
802 		devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
803 
804 		eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
805 		eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
806 		eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
807 	} else {
808 		devinfo->cmd_pipe = usb_sndbulkpipe(udev,
809 						eps[0]->desc.bEndpointAddress);
810 		devinfo->status_pipe = usb_rcvbulkpipe(udev,
811 						eps[1]->desc.bEndpointAddress);
812 		devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
813 						eps[2]->desc.bEndpointAddress);
814 		devinfo->data_out_pipe = usb_sndbulkpipe(udev,
815 						eps[3]->desc.bEndpointAddress);
816 	}
817 
818 	devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
819 								GFP_KERNEL);
820 	if (devinfo->qdepth < 0) {
821 		devinfo->qdepth = 256;
822 		devinfo->use_streams = 0;
823 	} else {
824 		devinfo->use_streams = 1;
825 	}
826 }
827 
828 static void uas_free_streams(struct uas_dev_info *devinfo)
829 {
830 	struct usb_device *udev = devinfo->udev;
831 	struct usb_host_endpoint *eps[3];
832 
833 	eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
834 	eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
835 	eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
836 	usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
837 }
838 
839 /*
840  * XXX: What I'd like to do here is register a SCSI host for each USB host in
841  * the system.  Follow usb-storage's design of registering a SCSI host for
842  * each USB device for the moment.  Can implement this by walking up the
843  * USB hierarchy until we find a USB host.
844  */
845 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
846 {
847 	int result;
848 	struct Scsi_Host *shost;
849 	struct uas_dev_info *devinfo;
850 	struct usb_device *udev = interface_to_usbdev(intf);
851 
852 	if (uas_switch_interface(udev, intf))
853 		return -ENODEV;
854 
855 	devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
856 	if (!devinfo)
857 		return -ENOMEM;
858 
859 	result = -ENOMEM;
860 	shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
861 	if (!shost)
862 		goto free;
863 
864 	shost->max_cmd_len = 16 + 252;
865 	shost->max_id = 1;
866 	shost->sg_tablesize = udev->bus->sg_tablesize;
867 
868 	devinfo->intf = intf;
869 	devinfo->udev = udev;
870 	devinfo->resetting = 0;
871 	init_usb_anchor(&devinfo->sense_urbs);
872 	init_usb_anchor(&devinfo->data_urbs);
873 	uas_configure_endpoints(devinfo);
874 
875 	result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
876 	if (result)
877 		goto free;
878 
879 	result = scsi_add_host(shost, &intf->dev);
880 	if (result)
881 		goto deconfig_eps;
882 
883 	shost->hostdata[0] = (unsigned long)devinfo;
884 
885 	scsi_scan_host(shost);
886 	usb_set_intfdata(intf, shost);
887 	return result;
888 
889 deconfig_eps:
890 	uas_free_streams(devinfo);
891  free:
892 	kfree(devinfo);
893 	if (shost)
894 		scsi_host_put(shost);
895 	return result;
896 }
897 
898 static int uas_pre_reset(struct usb_interface *intf)
899 {
900 /* XXX: Need to return 1 if it's not our device in error handling */
901 	return 0;
902 }
903 
904 static int uas_post_reset(struct usb_interface *intf)
905 {
906 /* XXX: Need to return 1 if it's not our device in error handling */
907 	return 0;
908 }
909 
910 static void uas_disconnect(struct usb_interface *intf)
911 {
912 	struct Scsi_Host *shost = usb_get_intfdata(intf);
913 	struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
914 
915 	scsi_remove_host(shost);
916 	usb_kill_anchored_urbs(&devinfo->sense_urbs);
917 	usb_kill_anchored_urbs(&devinfo->data_urbs);
918 	uas_free_streams(devinfo);
919 	kfree(devinfo);
920 }
921 
922 /*
923  * XXX: Should this plug into libusual so we can auto-upgrade devices from
924  * Bulk-Only to UAS?
925  */
926 static struct usb_driver uas_driver = {
927 	.name = "uas",
928 	.probe = uas_probe,
929 	.disconnect = uas_disconnect,
930 	.pre_reset = uas_pre_reset,
931 	.post_reset = uas_post_reset,
932 	.id_table = uas_usb_ids,
933 };
934 
935 module_usb_driver(uas_driver);
936 
937 MODULE_LICENSE("GPL");
938 MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");
939