scsiglue.c 20.5 KB
Newer Older
1 2
/*
 * Driver for USB Mass Storage compliant devices
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 * SCSI layer glue code
 *
 * Current development and maintenance by:
 *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
 *
 * Developed with the assistance of:
 *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
 *   (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
 *
 * Initial work by:
 *   (c) 1999 Michael Gee (michael@linuxspecific.com)
 *
 * This driver is based on the 'USB Mass Storage Class' document. This
 * describes in detail the protocol used to communicate with such
 * devices.  Clearly, the designers had SCSI and ATAPI commands in
 * mind when they created this document.  The commands are all very
 * similar to commands in the SCSI-II and ATAPI specifications.
 *
 * It is important to note that in a number of cases this class
 * exhibits class-specific exemptions from the USB specification.
 * Notably the usage of NAK, STALL and ACK differs from the norm, in
 * that they are used to communicate wait, failed and OK on commands.
 *
 * Also, for certain devices, the interrupt endpoint is used to convey
 * status of a command.
 *
 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
 * information about this driver.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
48
#include <linux/mutex.h>
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_devinfo.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_eh.h>

#include "usb.h"
#include "scsiglue.h"
#include "debug.h"
#include "transport.h"
#include "protocol.h"

62 63
/*
 * Vendor IDs for companies that seem to include the READ CAPACITY bug
64 65 66 67
 * in all their devices
 */
#define VENDOR_ID_NOKIA		0x0421
#define VENDOR_ID_NIKON		0x04b0
68
#define VENDOR_ID_PENTAX	0x0a17
69 70
#define VENDOR_ID_MOTOROLA	0x22b8

L
Linus Torvalds 已提交
71 72 73 74 75 76
/***********************************************************************
 * Host functions 
 ***********************************************************************/

static const char* host_info(struct Scsi_Host *host)
{
77 78
	struct us_data *us = host_to_us(host);
	return us->scsi_name;
L
Linus Torvalds 已提交
79 80 81 82
}

static int slave_alloc (struct scsi_device *sdev)
{
83 84
	struct us_data *us = host_to_us(sdev->host);

L
Linus Torvalds 已提交
85 86 87 88 89 90
	/*
	 * Set the INQUIRY transfer length to 36.  We don't use any of
	 * the extra data and many devices choke if asked for more or
	 * less than 36 bytes.
	 */
	sdev->inquiry_len = 36;
91

92 93
	/*
	 * USB has unusual DMA-alignment requirements: Although the
94 95 96 97 98 99 100 101 102 103 104 105 106
	 * starting address of each scatter-gather element doesn't matter,
	 * the length of each element except the last must be divisible
	 * by the Bulk maxpacket value.  There's currently no way to
	 * express this by block-layer constraints, so we'll cop out
	 * and simply require addresses to be aligned at 512-byte
	 * boundaries.  This is okay since most block I/O involves
	 * hardware sectors that are multiples of 512 bytes in length,
	 * and since host controllers up through USB 2.0 have maxpacket
	 * values no larger than 512.
	 *
	 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
	 * values can be as large as 2048.  To make that work properly
	 * will require changes to the block layer.
107
	 */
108
	blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
109

110 111 112 113
	/* Tell the SCSI layer if we know there is more than one LUN */
	if (us->protocol == USB_PR_BULK && us->max_lun > 0)
		sdev->sdev_bflags |= BLIST_FORCELUN;

L
Linus Torvalds 已提交
114 115 116 117 118 119 120
	return 0;
}

static int slave_configure(struct scsi_device *sdev)
{
	struct us_data *us = host_to_us(sdev->host);

121 122
	/*
	 * Many devices have trouble transferring more than 32KB at a time,
123 124 125
	 * while others have trouble with more than 64K. At this time we
	 * are limiting both to 32K (64 sectores).
	 */
126
	if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
127 128
		unsigned int max_sectors = 64;

129
		if (us->fflags & US_FL_MAX_SECTORS_MIN)
130
			max_sectors = PAGE_SIZE >> 9;
131
		if (queue_max_hw_sectors(sdev->request_queue) > max_sectors)
132
			blk_queue_max_hw_sectors(sdev->request_queue,
133
					      max_sectors);
134
	} else if (sdev->type == TYPE_TAPE) {
135 136
		/*
		 * Tapes need much higher max_sector limits, so just
137 138 139
		 * raise it to the maximum possible (4 GB / 512) and
		 * let the queue segment size sort out the real limit.
		 */
140
		blk_queue_max_hw_sectors(sdev->request_queue, 0x7FFFFF);
141
	} else if (us->pusb_dev->speed >= USB_SPEED_SUPER) {
142 143
		/*
		 * USB3 devices will be limited to 2048 sectors. This gives us
144 145 146
		 * better throughput on most devices.
		 */
		blk_queue_max_hw_sectors(sdev->request_queue, 2048);
147
	}
L
Linus Torvalds 已提交
148

149 150
	/*
	 * Some USB host controllers can't do DMA; they have to use PIO.
151 152 153 154 155 156 157
	 * They indicate this by setting their dma_mask to NULL.  For
	 * such controllers we need to make sure the block layer sets
	 * up bounce buffers in addressable memory.
	 */
	if (!us->pusb_dev->bus->controller->dma_mask)
		blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH);

158 159
	/*
	 * We can't put these settings in slave_alloc() because that gets
L
Linus Torvalds 已提交
160
	 * called before the device type is known.  Consequently these
161 162
	 * settings can't be overridden via the scsi devinfo mechanism.
	 */
L
Linus Torvalds 已提交
163 164
	if (sdev->type == TYPE_DISK) {

165 166
		/*
		 * Some vendors seem to put the READ CAPACITY bug into
167 168 169 170
		 * all their devices -- primarily makers of cell phones
		 * and digital cameras.  Since these devices always use
		 * flash media and can be expected to have an even number
		 * of sectors, we will always enable the CAPACITY_HEURISTICS
171 172
		 * flag unless told otherwise.
		 */
173 174 175
		switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) {
		case VENDOR_ID_NOKIA:
		case VENDOR_ID_NIKON:
176
		case VENDOR_ID_PENTAX:
177 178 179 180 181 182 183
		case VENDOR_ID_MOTOROLA:
			if (!(us->fflags & (US_FL_FIX_CAPACITY |
					US_FL_CAPACITY_OK)))
				us->fflags |= US_FL_CAPACITY_HEURISTICS;
			break;
		}

184 185
		/*
		 * Disk-type devices use MODE SENSE(6) if the protocol
L
Linus Torvalds 已提交
186
		 * (SubClass) is Transparent SCSI, otherwise they use
187 188
		 * MODE SENSE(10).
		 */
189
		if (us->subclass != USB_SC_SCSI && us->subclass != USB_SC_CYP_ATACB)
L
Linus Torvalds 已提交
190 191
			sdev->use_10_for_ms = 1;

192 193 194 195
		/*
		 *Many disks only accept MODE SENSE transfer lengths of
		 * 192 bytes (that's what Windows uses).
		 */
L
Linus Torvalds 已提交
196 197
		sdev->use_192_bytes_for_3f = 1;

198 199
		/*
		 * Some devices don't like MODE SENSE with page=0x3f,
L
Linus Torvalds 已提交
200 201 202 203 204
		 * which is the command used for checking if a device
		 * is write-protected.  Now that we tell the sd driver
		 * to do a 192-byte transfer with this command the
		 * majority of devices work fine, but a few still can't
		 * handle it.  The sd driver will simply assume those
205 206
		 * devices are write-enabled.
		 */
207
		if (us->fflags & US_FL_NO_WP_DETECT)
L
Linus Torvalds 已提交
208 209
			sdev->skip_ms_page_3f = 1;

210 211 212 213
		/*
		 * A number of devices have problems with MODE SENSE for
		 * page x08, so we will skip it.
		 */
L
Linus Torvalds 已提交
214 215
		sdev->skip_ms_page_8 = 1;

216 217 218
		/* Some devices don't handle VPD pages correctly */
		sdev->skip_vpd_pages = 1;

219 220 221
		/* Do not attempt to use REPORT SUPPORTED OPERATION CODES */
		sdev->no_report_opcodes = 1;

222 223 224
		/* Do not attempt to use WRITE SAME */
		sdev->no_write_same = 1;

225 226
		/*
		 * Some disks return the total number of blocks in response
L
Linus Torvalds 已提交
227
		 * to READ CAPACITY rather than the highest block number.
228 229
		 * If this device makes that mistake, tell the sd driver.
		 */
230
		if (us->fflags & US_FL_FIX_CAPACITY)
L
Linus Torvalds 已提交
231
			sdev->fix_capacity = 1;
232

233 234
		/*
		 * A few disks have two indistinguishable version, one of
235
		 * which reports the correct capacity and the other does not.
236 237
		 * The sd driver has to guess which is the case.
		 */
238
		if (us->fflags & US_FL_CAPACITY_HEURISTICS)
239 240
			sdev->guess_capacity = 1;

241 242 243 244
		/* Some devices cannot handle READ_CAPACITY_16 */
		if (us->fflags & US_FL_NO_READ_CAPACITY_16)
			sdev->no_read_capacity_16 = 1;

245 246 247
		/*
		 * Many devices do not respond properly to READ_CAPACITY_16.
		 * Tell the SCSI layer to try READ_CAPACITY_10 first.
248 249
		 * However some USB 3.0 drive enclosures return capacity
		 * modulo 2TB. Those must use READ_CAPACITY_16
250
		 */
251 252
		if (!(us->fflags & US_FL_NEEDS_CAP16))
			sdev->try_rc_10_first = 1;
253

B
Ben Efros 已提交
254 255 256 257
		/* assume SPC3 or latter devices support sense size > 18 */
		if (sdev->scsi_level > SCSI_SPC_2)
			us->fflags |= US_FL_SANE_SENSE;

258 259
		/*
		 * USB-IDE bridges tend to report SK = 0x04 (Non-recoverable
260 261 262 263
		 * Hardware Error) when any low-level error occurs,
		 * recoverable or not.  Setting this flag tells the SCSI
		 * midlayer to retry such commands, which frequently will
		 * succeed and fix the error.  The worst this can lead to
264 265
		 * is an occasional series of retries that will all fail.
		 */
266 267
		sdev->retry_hwerror = 1;

268 269 270 271
		/*
		 * USB disks should allow restart.  Some drives spin down
		 * automatically, requiring a START-STOP UNIT command.
		 */
272 273
		sdev->allow_restart = 1;

274 275
		/*
		 * Some USB cardreaders have trouble reading an sdcard's last
276
		 * sector in a larger then 1 sector read, since the performance
277 278
		 * impact is negligible we set this flag for all USB disks
		 */
279
		sdev->last_sector_bug = 1;
A
Alan Stern 已提交
280

281 282
		/*
		 * Enable last-sector hacks for single-target devices using
A
Alan Stern 已提交
283
		 * the Bulk-only transport, unless we already know the
284 285
		 * capacity will be decremented or is correct.
		 */
A
Alan Stern 已提交
286 287
		if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK |
					US_FL_SCM_MULT_TARG)) &&
288
				us->protocol == USB_PR_BULK)
A
Alan Stern 已提交
289
			us->use_last_sector_hacks = 1;
290 291 292 293 294

		/* Check if write cache default on flag is set or not */
		if (us->fflags & US_FL_WRITE_CACHE)
			sdev->wce_default_on = 1;

295 296 297 298
		/* A few buggy USB-ATA bridges don't understand FUA */
		if (us->fflags & US_FL_BROKEN_FUA)
			sdev->broken_fua = 1;

299 300 301 302 303 304 305 306
		/* Some even totally fail to indicate a cache */
		if (us->fflags & US_FL_ALWAYS_SYNC) {
			/* don't read caching information */
			sdev->skip_ms_page_8 = 1;
			sdev->skip_ms_page_3f = 1;
			/* assume sync is needed */
			sdev->wce_default_on = 1;
		}
L
Linus Torvalds 已提交
307 308
	} else {

309 310
		/*
		 * Non-disk-type devices don't need to blacklist any pages
L
Linus Torvalds 已提交
311
		 * or to force 192-byte transfer lengths for MODE SENSE.
312 313
		 * But they do need to use MODE SENSE(10).
		 */
L
Linus Torvalds 已提交
314
		sdev->use_10_for_ms = 1;
315 316 317 318

		/* Some (fake) usb cdrom devices don't like READ_DISC_INFO */
		if (us->fflags & US_FL_NO_READ_DISC_INFO)
			sdev->no_read_disc_info = 1;
L
Linus Torvalds 已提交
319 320
	}

321 322
	/*
	 * The CB and CBI transports have no way to pass LUN values
A
Alan Stern 已提交
323 324 325 326 327
	 * other than the bits in the second byte of a CDB.  But those
	 * bits don't get set to the LUN value if the device reports
	 * scsi_level == 0 (UNKNOWN).  Hence such devices must necessarily
	 * be single-LUN.
	 */
328
	if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
A
Alan Stern 已提交
329 330 331
			sdev->scsi_level == SCSI_UNKNOWN)
		us->max_lun = 0;

332 333 334 335
	/*
	 * Some devices choke when they receive a PREVENT-ALLOW MEDIUM
	 * REMOVAL command, so suppress those commands.
	 */
336
	if (us->fflags & US_FL_NOT_LOCKABLE)
L
Linus Torvalds 已提交
337 338
		sdev->lockable = 0;

339 340 341 342
	/*
	 * this is to satisfy the compiler, tho I don't think the 
	 * return code is ever checked anywhere.
	 */
L
Linus Torvalds 已提交
343 344 345
	return 0;
}

346 347
static int target_alloc(struct scsi_target *starget)
{
348 349
	struct us_data *us = host_to_us(dev_to_shost(starget->dev.parent));

350 351 352 353 354 355 356
	/*
	 * Some USB drives don't support REPORT LUNS, even though they
	 * report a SCSI revision level above 2.  Tell the SCSI layer
	 * not to issue that command; it will perform a normal sequential
	 * scan instead.
	 */
	starget->no_report_luns = 1;
357 358 359 360 361 362 363 364 365 366 367 368 369

	/*
	 * The UFI spec treats the Peripheral Qualifier bits in an
	 * INQUIRY result as reserved and requires devices to set them
	 * to 0.  However the SCSI spec requires these bits to be set
	 * to 3 to indicate when a LUN is not present.
	 *
	 * Let the scanning code know if this target merely sets
	 * Peripheral Device Type to 0x1f to indicate no LUN.
	 */
	if (us->subclass == USB_SC_UFI)
		starget->pdt_1f_for_no_lun = 1;

370 371 372
	return 0;
}

L
Linus Torvalds 已提交
373 374
/* queue a command */
/* This is always called with scsi_lock(host) held */
J
Jeff Garzik 已提交
375
static int queuecommand_lck(struct scsi_cmnd *srb,
L
Linus Torvalds 已提交
376 377 378 379 380 381 382
			void (*done)(struct scsi_cmnd *))
{
	struct us_data *us = host_to_us(srb->device->host);

	/* check for state-transition errors */
	if (us->srb != NULL) {
		printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
383
			__func__, us->srb);
L
Linus Torvalds 已提交
384 385 386 387
		return SCSI_MLQUEUE_HOST_BUSY;
	}

	/* fail the command if we are disconnecting */
388
	if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
389
		usb_stor_dbg(us, "Fail command during disconnect\n");
L
Linus Torvalds 已提交
390 391 392 393 394 395 396 397
		srb->result = DID_NO_CONNECT << 16;
		done(srb);
		return 0;
	}

	/* enqueue the command and wake up the control thread */
	srb->scsi_done = done;
	us->srb = srb;
398
	complete(&us->cmnd_ready);
L
Linus Torvalds 已提交
399 400 401 402

	return 0;
}

J
Jeff Garzik 已提交
403 404
static DEF_SCSI_QCMD(queuecommand)

L
Linus Torvalds 已提交
405 406 407 408 409 410 411 412 413
/***********************************************************************
 * Error handling functions
 ***********************************************************************/

/* Command timeout and abort */
static int command_abort(struct scsi_cmnd *srb)
{
	struct us_data *us = host_to_us(srb->device->host);

414
	usb_stor_dbg(us, "%s called\n", __func__);
L
Linus Torvalds 已提交
415

416 417 418 419
	/*
	 * us->srb together with the TIMED_OUT, RESETTING, and ABORTING
	 * bits are protected by the host lock.
	 */
420 421
	scsi_lock(us_to_host(us));

L
Linus Torvalds 已提交
422 423
	/* Is this command still active? */
	if (us->srb != srb) {
424
		scsi_unlock(us_to_host(us));
425
		usb_stor_dbg(us, "-- nothing to abort\n");
L
Linus Torvalds 已提交
426 427 428
		return FAILED;
	}

429 430
	/*
	 * Set the TIMED_OUT bit.  Also set the ABORTING bit, but only if
L
Linus Torvalds 已提交
431
	 * a device reset isn't already in progress (to avoid interfering
432 433
	 * with the reset).  Note that we must retain the host lock while
	 * calling usb_stor_stop_transport(); otherwise it might interfere
434 435
	 * with an auto-reset that begins as soon as we release the lock.
	 */
436 437 438
	set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
	if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
		set_bit(US_FLIDX_ABORTING, &us->dflags);
L
Linus Torvalds 已提交
439 440
		usb_stor_stop_transport(us);
	}
441
	scsi_unlock(us_to_host(us));
L
Linus Torvalds 已提交
442 443 444 445 446 447

	/* Wait for the aborted command to finish */
	wait_for_completion(&us->notify);
	return SUCCESS;
}

448 449 450 451
/*
 * This invokes the transport reset mechanism to reset the state of the
 * device
 */
L
Linus Torvalds 已提交
452 453 454 455 456
static int device_reset(struct scsi_cmnd *srb)
{
	struct us_data *us = host_to_us(srb->device->host);
	int result;

457
	usb_stor_dbg(us, "%s called\n", __func__);
L
Linus Torvalds 已提交
458

459 460 461 462
	/* lock the device pointers and do the reset */
	mutex_lock(&(us->dev_mutex));
	result = us->transport_reset(us);
	mutex_unlock(&us->dev_mutex);
L
Linus Torvalds 已提交
463

464
	return result < 0 ? FAILED : SUCCESS;
L
Linus Torvalds 已提交
465 466
}

467
/* Simulate a SCSI bus reset by resetting the device's USB port. */
L
Linus Torvalds 已提交
468 469 470
static int bus_reset(struct scsi_cmnd *srb)
{
	struct us_data *us = host_to_us(srb->device->host);
471
	int result;
L
Linus Torvalds 已提交
472

473 474
	usb_stor_dbg(us, "%s called\n", __func__);

475
	result = usb_stor_port_reset(us);
L
Linus Torvalds 已提交
476 477 478
	return result < 0 ? FAILED : SUCCESS;
}

479 480
/*
 * Report a driver-initiated device reset to the SCSI layer.
L
Linus Torvalds 已提交
481
 * Calling this for a SCSI-initiated reset is unnecessary but harmless.
482 483
 * The caller must own the SCSI host lock.
 */
L
Linus Torvalds 已提交
484 485 486 487 488 489
void usb_stor_report_device_reset(struct us_data *us)
{
	int i;
	struct Scsi_Host *host = us_to_host(us);

	scsi_report_device_reset(host, 0, 0);
490
	if (us->fflags & US_FL_SCM_MULT_TARG) {
L
Linus Torvalds 已提交
491 492 493 494 495
		for (i = 1; i < host->max_id; ++i)
			scsi_report_device_reset(host, 0, i);
	}
}

496 497
/*
 * Report a driver-initiated bus reset to the SCSI layer.
498
 * Calling this for a SCSI-initiated reset is unnecessary but harmless.
499 500
 * The caller must not own the SCSI host lock.
 */
501 502
void usb_stor_report_bus_reset(struct us_data *us)
{
A
Alan Stern 已提交
503 504 505 506 507
	struct Scsi_Host *host = us_to_host(us);

	scsi_lock(host);
	scsi_report_bus_reset(host, 0);
	scsi_unlock(host);
508 509
}

L
Linus Torvalds 已提交
510 511 512 513
/***********************************************************************
 * /proc/scsi/ functions
 ***********************************************************************/

A
Al Viro 已提交
514 515 516 517 518 519 520
static int write_info(struct Scsi_Host *host, char *buffer, int length)
{
	/* if someone is sending us data, just throw it away */
	return length;
}

static int show_info (struct seq_file *m, struct Scsi_Host *host)
L
Linus Torvalds 已提交
521 522 523 524 525
{
	struct us_data *us = host_to_us(host);
	const char *string;

	/* print the controller name */
526
	seq_printf(m, "   Host scsi%d: usb-storage\n", host->host_no);
L
Linus Torvalds 已提交
527 528 529 530 531 532 533 534

	/* print product, vendor, and serial number strings */
	if (us->pusb_dev->manufacturer)
		string = us->pusb_dev->manufacturer;
	else if (us->unusual_dev->vendorName)
		string = us->unusual_dev->vendorName;
	else
		string = "Unknown";
535
	seq_printf(m, "       Vendor: %s\n", string);
L
Linus Torvalds 已提交
536 537 538 539 540 541
	if (us->pusb_dev->product)
		string = us->pusb_dev->product;
	else if (us->unusual_dev->productName)
		string = us->unusual_dev->productName;
	else
		string = "Unknown";
542
	seq_printf(m, "      Product: %s\n", string);
L
Linus Torvalds 已提交
543 544 545 546
	if (us->pusb_dev->serial)
		string = us->pusb_dev->serial;
	else
		string = "None";
547
	seq_printf(m, "Serial Number: %s\n", string);
L
Linus Torvalds 已提交
548 549

	/* show the protocol and transport */
550 551
	seq_printf(m, "     Protocol: %s\n", us->protocol_name);
	seq_printf(m, "    Transport: %s\n", us->transport_name);
L
Linus Torvalds 已提交
552 553

	/* show the device flags */
554
	seq_printf(m, "       Quirks:");
L
Linus Torvalds 已提交
555 556

#define US_FLAG(name, value) \
A
Al Viro 已提交
557
	if (us->fflags & value) seq_printf(m, " " #name);
L
Linus Torvalds 已提交
558 559
US_DO_ALL_FLAGS
#undef US_FLAG
A
Al Viro 已提交
560 561
	seq_putc(m, '\n');
	return 0;
L
Linus Torvalds 已提交
562 563 564 565 566 567 568
}

/***********************************************************************
 * Sysfs interface
 ***********************************************************************/

/* Output routine for the sysfs max_sectors file */
569
static ssize_t max_sectors_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
570 571 572
{
	struct scsi_device *sdev = to_scsi_device(dev);

573
	return sprintf(buf, "%u\n", queue_max_hw_sectors(sdev->request_queue));
L
Linus Torvalds 已提交
574 575 576
}

/* Input routine for the sysfs max_sectors file */
577
static ssize_t max_sectors_store(struct device *dev, struct device_attribute *attr, const char *buf,
L
Linus Torvalds 已提交
578 579 580 581 582
		size_t count)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	unsigned short ms;

583
	if (sscanf(buf, "%hu", &ms) > 0) {
584
		blk_queue_max_hw_sectors(sdev->request_queue, ms);
585
		return count;
L
Linus Torvalds 已提交
586
	}
587
	return -EINVAL;
L
Linus Torvalds 已提交
588
}
589
static DEVICE_ATTR_RW(max_sectors);
L
Linus Torvalds 已提交
590 591

static struct device_attribute *sysfs_device_attr_list[] = {
592 593 594
	&dev_attr_max_sectors,
	NULL,
};
L
Linus Torvalds 已提交
595 596 597 598 599

/*
 * this defines our host template, with which we'll allocate hosts
 */

600
static const struct scsi_host_template usb_stor_host_template = {
L
Linus Torvalds 已提交
601 602 603
	/* basic userland interface stuff */
	.name =				"usb-storage",
	.proc_name =			"usb-storage",
A
Al Viro 已提交
604 605
	.show_info =			show_info,
	.write_info =			write_info,
L
Linus Torvalds 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
	.info =				host_info,

	/* command interface -- queued only */
	.queuecommand =			queuecommand,

	/* error and abort handlers */
	.eh_abort_handler =		command_abort,
	.eh_device_reset_handler =	device_reset,
	.eh_bus_reset_handler =		bus_reset,

	/* queue commands only, only one command per LUN */
	.can_queue =			1,

	/* unknown initiator id */
	.this_id =			-1,

	.slave_alloc =			slave_alloc,
	.slave_configure =		slave_configure,
624
	.target_alloc =			target_alloc,
L
Linus Torvalds 已提交
625 626

	/* lots of sg segments can be handled */
627
	.sg_tablesize =			SG_MAX_SEGMENTS,
L
Linus Torvalds 已提交
628

629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

	/*
	 * Limit the total size of a transfer to 120 KB.
	 *
	 * Some devices are known to choke with anything larger. It seems like
	 * the problem stems from the fact that original IDE controllers had
	 * only an 8-bit register to hold the number of sectors in one transfer
	 * and even those couldn't handle a full 256 sectors.
	 *
	 * Because we want to make sure we interoperate with as many devices as
	 * possible, we will maintain a 240 sector transfer size limit for USB
	 * Mass Storage devices.
	 *
	 * Tests show that other operating have similar limits with Microsoft
	 * Windows 7 limiting transfers to 128 sectors for both USB2 and USB3
	 * and Apple Mac OS X 10.11 limiting transfers to 256 sectors for USB2
	 * and 2048 for USB3 devices.
	 */
L
Linus Torvalds 已提交
647 648
	.max_sectors =                  240,

649 650
	/*
	 * merge commands... this seems to help performance, but
L
Linus Torvalds 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	 * periodically someone should test to see which setting is more
	 * optimal.
	 */
	.use_clustering =		1,

	/* emulated HBA */
	.emulated =			1,

	/* we do our own delay after a device or bus reset */
	.skip_settle_delay =		1,

	/* sysfs device attributes */
	.sdev_attrs =			sysfs_device_attr_list,

	/* module management */
	.module =			THIS_MODULE
};

669 670 671 672 673 674 675 676 677 678
void usb_stor_host_template_init(struct scsi_host_template *sht,
				 const char *name, struct module *owner)
{
	*sht = usb_stor_host_template;
	sht->name = name;
	sht->proc_name = name;
	sht->module = owner;
}
EXPORT_SYMBOL_GPL(usb_stor_host_template_init);

L
Linus Torvalds 已提交
679 680 681 682 683 684 685
/* To Report "Illegal Request: Invalid Field in CDB */
unsigned char usb_stor_sense_invalidCDB[18] = {
	[0]	= 0x70,			    /* current error */
	[2]	= ILLEGAL_REQUEST,	    /* Illegal Request = 0x05 */
	[7]	= 0x0a,			    /* additional length */
	[12]	= 0x24			    /* Invalid Field in CDB */
};
686
EXPORT_SYMBOL_GPL(usb_stor_sense_invalidCDB);