Linux Audio

Check our new training course

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