]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/storage/scsiglue.c
USB: add SPDX identifiers to all remaining files in drivers/usb/
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / storage / scsiglue.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
f0183a33
FB
2/*
3 * Driver for USB Mass Storage compliant devices
1da177e4
LT
4 * SCSI layer glue code
5 *
1da177e4
LT
6 * Current development and maintenance by:
7 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 *
9 * Developed with the assistance of:
10 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
11 * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
12 *
13 * Initial work by:
14 * (c) 1999 Michael Gee (michael@linuxspecific.com)
15 *
16 * This driver is based on the 'USB Mass Storage Class' document. This
17 * describes in detail the protocol used to communicate with such
18 * devices. Clearly, the designers had SCSI and ATAPI commands in
19 * mind when they created this document. The commands are all very
20 * similar to commands in the SCSI-II and ATAPI specifications.
21 *
22 * It is important to note that in a number of cases this class
23 * exhibits class-specific exemptions from the USB specification.
24 * Notably the usage of NAK, STALL and ACK differs from the norm, in
25 * that they are used to communicate wait, failed and OK on commands.
26 *
27 * Also, for certain devices, the interrupt endpoint is used to convey
28 * status of a command.
29 *
30 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
31 * information about this driver.
32 *
33 * This program is free software; you can redistribute it and/or modify it
34 * under the terms of the GNU General Public License as published by the
35 * Free Software Foundation; either version 2, or (at your option) any
36 * later version.
37 *
38 * This program is distributed in the hope that it will be useful, but
39 * WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41 * General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 675 Mass Ave, Cambridge, MA 02139, USA.
46 */
47
1da177e4 48#include <linux/module.h>
4186ecf8 49#include <linux/mutex.h>
1da177e4
LT
50
51#include <scsi/scsi.h>
52#include <scsi/scsi_cmnd.h>
53#include <scsi/scsi_devinfo.h>
54#include <scsi/scsi_device.h>
55#include <scsi/scsi_eh.h>
56
57#include "usb.h"
58#include "scsiglue.h"
59#include "debug.h"
60#include "transport.h"
61#include "protocol.h"
62
f0183a33
FB
63/*
64 * Vendor IDs for companies that seem to include the READ CAPACITY bug
a81a81a2
AS
65 * in all their devices
66 */
67#define VENDOR_ID_NOKIA 0x0421
68#define VENDOR_ID_NIKON 0x04b0
506e9469 69#define VENDOR_ID_PENTAX 0x0a17
a81a81a2
AS
70#define VENDOR_ID_MOTOROLA 0x22b8
71
1da177e4
LT
72/***********************************************************************
73 * Host functions
74 ***********************************************************************/
75
76static const char* host_info(struct Scsi_Host *host)
77{
00fa43ef
MW
78 struct us_data *us = host_to_us(host);
79 return us->scsi_name;
1da177e4
LT
80}
81
82static int slave_alloc (struct scsi_device *sdev)
83{
823d12c9
AS
84 struct us_data *us = host_to_us(sdev->host);
85
1da177e4
LT
86 /*
87 * Set the INQUIRY transfer length to 36. We don't use any of
88 * the extra data and many devices choke if asked for more or
89 * less than 36 bytes.
90 */
91 sdev->inquiry_len = 36;
3a3416b1 92
f0183a33
FB
93 /*
94 * USB has unusual DMA-alignment requirements: Although the
f756cbd4
AS
95 * starting address of each scatter-gather element doesn't matter,
96 * the length of each element except the last must be divisible
97 * by the Bulk maxpacket value. There's currently no way to
98 * express this by block-layer constraints, so we'll cop out
99 * and simply require addresses to be aligned at 512-byte
100 * boundaries. This is okay since most block I/O involves
101 * hardware sectors that are multiples of 512 bytes in length,
102 * and since host controllers up through USB 2.0 have maxpacket
103 * values no larger than 512.
104 *
105 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
106 * values can be as large as 2048. To make that work properly
107 * will require changes to the block layer.
148d9fe4 108 */
f756cbd4 109 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
465ff318 110
823d12c9
AS
111 /* Tell the SCSI layer if we know there is more than one LUN */
112 if (us->protocol == USB_PR_BULK && us->max_lun > 0)
113 sdev->sdev_bflags |= BLIST_FORCELUN;
114
1da177e4
LT
115 return 0;
116}
117
118static int slave_configure(struct scsi_device *sdev)
119{
120 struct us_data *us = host_to_us(sdev->host);
121
f0183a33
FB
122 /*
123 * Many devices have trouble transferring more than 32KB at a time,
883d989a
PD
124 * while others have trouble with more than 64K. At this time we
125 * are limiting both to 32K (64 sectores).
126 */
7e4d6c38 127 if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
33abc04f
DM
128 unsigned int max_sectors = 64;
129
7e4d6c38 130 if (us->fflags & US_FL_MAX_SECTORS_MIN)
09cbfeaf 131 max_sectors = PAGE_SIZE >> 9;
49d6271b 132 if (queue_max_hw_sectors(sdev->request_queue) > max_sectors)
086fa5ff 133 blk_queue_max_hw_sectors(sdev->request_queue,
33abc04f 134 max_sectors);
5c16034d 135 } else if (sdev->type == TYPE_TAPE) {
f0183a33
FB
136 /*
137 * Tapes need much higher max_sector limits, so just
5c16034d
AS
138 * raise it to the maximum possible (4 GB / 512) and
139 * let the queue segment size sort out the real limit.
140 */
086fa5ff 141 blk_queue_max_hw_sectors(sdev->request_queue, 0x7FFFFF);
5b91dfe1 142 } else if (us->pusb_dev->speed >= USB_SPEED_SUPER) {
f0183a33
FB
143 /*
144 * USB3 devices will be limited to 2048 sectors. This gives us
5b91dfe1
FB
145 * better throughput on most devices.
146 */
147 blk_queue_max_hw_sectors(sdev->request_queue, 2048);
33abc04f 148 }
1da177e4 149
f0183a33
FB
150 /*
151 * Some USB host controllers can't do DMA; they have to use PIO.
96983d2d
AS
152 * They indicate this by setting their dma_mask to NULL. For
153 * such controllers we need to make sure the block layer sets
154 * up bounce buffers in addressable memory.
155 */
156 if (!us->pusb_dev->bus->controller->dma_mask)
157 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_HIGH);
158
f0183a33
FB
159 /*
160 * We can't put these settings in slave_alloc() because that gets
1da177e4 161 * called before the device type is known. Consequently these
f0183a33
FB
162 * settings can't be overridden via the scsi devinfo mechanism.
163 */
1da177e4
LT
164 if (sdev->type == TYPE_DISK) {
165
f0183a33
FB
166 /*
167 * Some vendors seem to put the READ CAPACITY bug into
a81a81a2
AS
168 * all their devices -- primarily makers of cell phones
169 * and digital cameras. Since these devices always use
170 * flash media and can be expected to have an even number
171 * of sectors, we will always enable the CAPACITY_HEURISTICS
f0183a33
FB
172 * flag unless told otherwise.
173 */
a81a81a2
AS
174 switch (le16_to_cpu(us->pusb_dev->descriptor.idVendor)) {
175 case VENDOR_ID_NOKIA:
176 case VENDOR_ID_NIKON:
506e9469 177 case VENDOR_ID_PENTAX:
a81a81a2
AS
178 case VENDOR_ID_MOTOROLA:
179 if (!(us->fflags & (US_FL_FIX_CAPACITY |
180 US_FL_CAPACITY_OK)))
181 us->fflags |= US_FL_CAPACITY_HEURISTICS;
182 break;
183 }
184
f0183a33
FB
185 /*
186 * Disk-type devices use MODE SENSE(6) if the protocol
1da177e4 187 * (SubClass) is Transparent SCSI, otherwise they use
f0183a33
FB
188 * MODE SENSE(10).
189 */
8fa7fd74 190 if (us->subclass != USB_SC_SCSI && us->subclass != USB_SC_CYP_ATACB)
1da177e4
LT
191 sdev->use_10_for_ms = 1;
192
f0183a33
FB
193 /*
194 *Many disks only accept MODE SENSE transfer lengths of
195 * 192 bytes (that's what Windows uses).
196 */
1da177e4
LT
197 sdev->use_192_bytes_for_3f = 1;
198
f0183a33
FB
199 /*
200 * Some devices don't like MODE SENSE with page=0x3f,
1da177e4
LT
201 * which is the command used for checking if a device
202 * is write-protected. Now that we tell the sd driver
203 * to do a 192-byte transfer with this command the
204 * majority of devices work fine, but a few still can't
205 * handle it. The sd driver will simply assume those
f0183a33
FB
206 * devices are write-enabled.
207 */
7e4d6c38 208 if (us->fflags & US_FL_NO_WP_DETECT)
1da177e4
LT
209 sdev->skip_ms_page_3f = 1;
210
f0183a33
FB
211 /*
212 * A number of devices have problems with MODE SENSE for
213 * page x08, so we will skip it.
214 */
1da177e4
LT
215 sdev->skip_ms_page_8 = 1;
216
09b6b51b
AS
217 /* Some devices don't handle VPD pages correctly */
218 sdev->skip_vpd_pages = 1;
219
3c6bdaea
MP
220 /* Do not attempt to use REPORT SUPPORTED OPERATION CODES */
221 sdev->no_report_opcodes = 1;
222
5db44863
MP
223 /* Do not attempt to use WRITE SAME */
224 sdev->no_write_same = 1;
225
f0183a33
FB
226 /*
227 * Some disks return the total number of blocks in response
1da177e4 228 * to READ CAPACITY rather than the highest block number.
f0183a33
FB
229 * If this device makes that mistake, tell the sd driver.
230 */
7e4d6c38 231 if (us->fflags & US_FL_FIX_CAPACITY)
1da177e4 232 sdev->fix_capacity = 1;
86dbde9c 233
f0183a33
FB
234 /*
235 * A few disks have two indistinguishable version, one of
61bf54b7 236 * which reports the correct capacity and the other does not.
f0183a33
FB
237 * The sd driver has to guess which is the case.
238 */
7e4d6c38 239 if (us->fflags & US_FL_CAPACITY_HEURISTICS)
61bf54b7
ON
240 sdev->guess_capacity = 1;
241
00914025
HG
242 /* Some devices cannot handle READ_CAPACITY_16 */
243 if (us->fflags & US_FL_NO_READ_CAPACITY_16)
244 sdev->no_read_capacity_16 = 1;
245
6a0bdffa
AS
246 /*
247 * Many devices do not respond properly to READ_CAPACITY_16.
248 * Tell the SCSI layer to try READ_CAPACITY_10 first.
32c37fc3
ON
249 * However some USB 3.0 drive enclosures return capacity
250 * modulo 2TB. Those must use READ_CAPACITY_16
6a0bdffa 251 */
32c37fc3
ON
252 if (!(us->fflags & US_FL_NEEDS_CAP16))
253 sdev->try_rc_10_first = 1;
6a0bdffa 254
1537e0ad
BE
255 /* assume SPC3 or latter devices support sense size > 18 */
256 if (sdev->scsi_level > SCSI_SPC_2)
257 us->fflags |= US_FL_SANE_SENSE;
258
f0183a33
FB
259 /*
260 * USB-IDE bridges tend to report SK = 0x04 (Non-recoverable
86dbde9c
MD
261 * Hardware Error) when any low-level error occurs,
262 * recoverable or not. Setting this flag tells the SCSI
263 * midlayer to retry such commands, which frequently will
264 * succeed and fix the error. The worst this can lead to
f0183a33
FB
265 * is an occasional series of retries that will all fail.
266 */
86dbde9c
MD
267 sdev->retry_hwerror = 1;
268
f0183a33
FB
269 /*
270 * USB disks should allow restart. Some drives spin down
271 * automatically, requiring a START-STOP UNIT command.
272 */
f09e495d
MCC
273 sdev->allow_restart = 1;
274
f0183a33
FB
275 /*
276 * Some USB cardreaders have trouble reading an sdcard's last
23c3e290 277 * sector in a larger then 1 sector read, since the performance
f0183a33
FB
278 * impact is negligible we set this flag for all USB disks
279 */
23c3e290 280 sdev->last_sector_bug = 1;
25ff1c31 281
f0183a33
FB
282 /*
283 * Enable last-sector hacks for single-target devices using
25ff1c31 284 * the Bulk-only transport, unless we already know the
f0183a33
FB
285 * capacity will be decremented or is correct.
286 */
25ff1c31
AS
287 if (!(us->fflags & (US_FL_FIX_CAPACITY | US_FL_CAPACITY_OK |
288 US_FL_SCM_MULT_TARG)) &&
8fa7fd74 289 us->protocol == USB_PR_BULK)
25ff1c31 290 us->use_last_sector_hacks = 1;
eaa05dfc
NJ
291
292 /* Check if write cache default on flag is set or not */
293 if (us->fflags & US_FL_WRITE_CACHE)
294 sdev->wce_default_on = 1;
295
b14bf2d0
AS
296 /* A few buggy USB-ATA bridges don't understand FUA */
297 if (us->fflags & US_FL_BROKEN_FUA)
298 sdev->broken_fua = 1;
299
050bc4e8
ON
300 /* Some even totally fail to indicate a cache */
301 if (us->fflags & US_FL_ALWAYS_SYNC) {
302 /* don't read caching information */
303 sdev->skip_ms_page_8 = 1;
304 sdev->skip_ms_page_3f = 1;
305 /* assume sync is needed */
306 sdev->wce_default_on = 1;
307 }
1da177e4
LT
308 } else {
309
f0183a33
FB
310 /*
311 * Non-disk-type devices don't need to blacklist any pages
1da177e4 312 * or to force 192-byte transfer lengths for MODE SENSE.
f0183a33
FB
313 * But they do need to use MODE SENSE(10).
314 */
1da177e4 315 sdev->use_10_for_ms = 1;
ae38c78a
HG
316
317 /* Some (fake) usb cdrom devices don't like READ_DISC_INFO */
318 if (us->fflags & US_FL_NO_READ_DISC_INFO)
319 sdev->no_read_disc_info = 1;
1da177e4
LT
320 }
321
f0183a33
FB
322 /*
323 * The CB and CBI transports have no way to pass LUN values
f3f49065
AS
324 * other than the bits in the second byte of a CDB. But those
325 * bits don't get set to the LUN value if the device reports
326 * scsi_level == 0 (UNKNOWN). Hence such devices must necessarily
327 * be single-LUN.
328 */
8fa7fd74 329 if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
f3f49065
AS
330 sdev->scsi_level == SCSI_UNKNOWN)
331 us->max_lun = 0;
332
f0183a33
FB
333 /*
334 * Some devices choke when they receive a PREVENT-ALLOW MEDIUM
335 * REMOVAL command, so suppress those commands.
336 */
7e4d6c38 337 if (us->fflags & US_FL_NOT_LOCKABLE)
1da177e4
LT
338 sdev->lockable = 0;
339
f0183a33
FB
340 /*
341 * this is to satisfy the compiler, tho I don't think the
342 * return code is ever checked anywhere.
343 */
1da177e4
LT
344 return 0;
345}
346
09b6b51b
AS
347static int target_alloc(struct scsi_target *starget)
348{
af74d2da
AS
349 struct us_data *us = host_to_us(dev_to_shost(starget->dev.parent));
350
09b6b51b
AS
351 /*
352 * Some USB drives don't support REPORT LUNS, even though they
353 * report a SCSI revision level above 2. Tell the SCSI layer
354 * not to issue that command; it will perform a normal sequential
355 * scan instead.
356 */
357 starget->no_report_luns = 1;
af74d2da
AS
358
359 /*
360 * The UFI spec treats the Peripheral Qualifier bits in an
361 * INQUIRY result as reserved and requires devices to set them
362 * to 0. However the SCSI spec requires these bits to be set
363 * to 3 to indicate when a LUN is not present.
364 *
365 * Let the scanning code know if this target merely sets
366 * Peripheral Device Type to 0x1f to indicate no LUN.
367 */
368 if (us->subclass == USB_SC_UFI)
369 starget->pdt_1f_for_no_lun = 1;
370
09b6b51b
AS
371 return 0;
372}
373
1da177e4
LT
374/* queue a command */
375/* This is always called with scsi_lock(host) held */
f281233d 376static int queuecommand_lck(struct scsi_cmnd *srb,
1da177e4
LT
377 void (*done)(struct scsi_cmnd *))
378{
379 struct us_data *us = host_to_us(srb->device->host);
380
1da177e4
LT
381 /* check for state-transition errors */
382 if (us->srb != NULL) {
383 printk(KERN_ERR USB_STORAGE "Error in %s: us->srb = %p\n",
441b62c1 384 __func__, us->srb);
1da177e4
LT
385 return SCSI_MLQUEUE_HOST_BUSY;
386 }
387
388 /* fail the command if we are disconnecting */
7e4d6c38 389 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
191648d0 390 usb_stor_dbg(us, "Fail command during disconnect\n");
1da177e4
LT
391 srb->result = DID_NO_CONNECT << 16;
392 done(srb);
393 return 0;
394 }
395
396 /* enqueue the command and wake up the control thread */
397 srb->scsi_done = done;
398 us->srb = srb;
7119e3c3 399 complete(&us->cmnd_ready);
1da177e4
LT
400
401 return 0;
402}
403
f281233d
JG
404static DEF_SCSI_QCMD(queuecommand)
405
1da177e4
LT
406/***********************************************************************
407 * Error handling functions
408 ***********************************************************************/
409
410/* Command timeout and abort */
1da177e4
LT
411static int command_abort(struct scsi_cmnd *srb)
412{
413 struct us_data *us = host_to_us(srb->device->host);
414
191648d0 415 usb_stor_dbg(us, "%s called\n", __func__);
1da177e4 416
f0183a33
FB
417 /*
418 * us->srb together with the TIMED_OUT, RESETTING, and ABORTING
419 * bits are protected by the host lock.
420 */
226173ed
MD
421 scsi_lock(us_to_host(us));
422
1da177e4
LT
423 /* Is this command still active? */
424 if (us->srb != srb) {
226173ed 425 scsi_unlock(us_to_host(us));
191648d0 426 usb_stor_dbg(us, "-- nothing to abort\n");
1da177e4
LT
427 return FAILED;
428 }
429
f0183a33
FB
430 /*
431 * Set the TIMED_OUT bit. Also set the ABORTING bit, but only if
1da177e4 432 * a device reset isn't already in progress (to avoid interfering
226173ed
MD
433 * with the reset). Note that we must retain the host lock while
434 * calling usb_stor_stop_transport(); otherwise it might interfere
f0183a33
FB
435 * with an auto-reset that begins as soon as we release the lock.
436 */
7e4d6c38
AS
437 set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
438 if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
439 set_bit(US_FLIDX_ABORTING, &us->dflags);
1da177e4
LT
440 usb_stor_stop_transport(us);
441 }
226173ed 442 scsi_unlock(us_to_host(us));
1da177e4
LT
443
444 /* Wait for the aborted command to finish */
445 wait_for_completion(&us->notify);
1da177e4
LT
446 return SUCCESS;
447}
448
f0183a33
FB
449/*
450 * This invokes the transport reset mechanism to reset the state of the
451 * device
452 */
1da177e4
LT
453static int device_reset(struct scsi_cmnd *srb)
454{
455 struct us_data *us = host_to_us(srb->device->host);
456 int result;
457
191648d0 458 usb_stor_dbg(us, "%s called\n", __func__);
1da177e4 459
d526875d
GKH
460 /* lock the device pointers and do the reset */
461 mutex_lock(&(us->dev_mutex));
462 result = us->transport_reset(us);
463 mutex_unlock(&us->dev_mutex);
1da177e4 464
4d07ef76 465 return result < 0 ? FAILED : SUCCESS;
1da177e4
LT
466}
467
4d07ef76 468/* Simulate a SCSI bus reset by resetting the device's USB port. */
1da177e4
LT
469static int bus_reset(struct scsi_cmnd *srb)
470{
471 struct us_data *us = host_to_us(srb->device->host);
4d07ef76 472 int result;
1da177e4 473
191648d0
JP
474 usb_stor_dbg(us, "%s called\n", __func__);
475
4d07ef76 476 result = usb_stor_port_reset(us);
1da177e4
LT
477 return result < 0 ? FAILED : SUCCESS;
478}
479
f0183a33
FB
480/*
481 * Report a driver-initiated device reset to the SCSI layer.
1da177e4 482 * Calling this for a SCSI-initiated reset is unnecessary but harmless.
f0183a33
FB
483 * The caller must own the SCSI host lock.
484 */
1da177e4
LT
485void usb_stor_report_device_reset(struct us_data *us)
486{
487 int i;
488 struct Scsi_Host *host = us_to_host(us);
489
490 scsi_report_device_reset(host, 0, 0);
7e4d6c38 491 if (us->fflags & US_FL_SCM_MULT_TARG) {
1da177e4
LT
492 for (i = 1; i < host->max_id; ++i)
493 scsi_report_device_reset(host, 0, i);
494 }
495}
496
f0183a33
FB
497/*
498 * Report a driver-initiated bus reset to the SCSI layer.
4d07ef76 499 * Calling this for a SCSI-initiated reset is unnecessary but harmless.
f0183a33
FB
500 * The caller must not own the SCSI host lock.
501 */
4d07ef76
MD
502void usb_stor_report_bus_reset(struct us_data *us)
503{
f07600cf
AS
504 struct Scsi_Host *host = us_to_host(us);
505
506 scsi_lock(host);
507 scsi_report_bus_reset(host, 0);
508 scsi_unlock(host);
4d07ef76
MD
509}
510
1da177e4
LT
511/***********************************************************************
512 * /proc/scsi/ functions
513 ***********************************************************************/
514
09dae7fc
AV
515static int write_info(struct Scsi_Host *host, char *buffer, int length)
516{
517 /* if someone is sending us data, just throw it away */
518 return length;
519}
520
09dae7fc 521static int show_info (struct seq_file *m, struct Scsi_Host *host)
1da177e4
LT
522{
523 struct us_data *us = host_to_us(host);
1da177e4
LT
524 const char *string;
525
1da177e4 526 /* print the controller name */
7d203a9e 527 seq_printf(m, " Host scsi%d: usb-storage\n", host->host_no);
1da177e4
LT
528
529 /* print product, vendor, and serial number strings */
530 if (us->pusb_dev->manufacturer)
531 string = us->pusb_dev->manufacturer;
532 else if (us->unusual_dev->vendorName)
533 string = us->unusual_dev->vendorName;
534 else
535 string = "Unknown";
7d203a9e 536 seq_printf(m, " Vendor: %s\n", string);
1da177e4
LT
537 if (us->pusb_dev->product)
538 string = us->pusb_dev->product;
539 else if (us->unusual_dev->productName)
540 string = us->unusual_dev->productName;
541 else
542 string = "Unknown";
7d203a9e 543 seq_printf(m, " Product: %s\n", string);
1da177e4
LT
544 if (us->pusb_dev->serial)
545 string = us->pusb_dev->serial;
546 else
547 string = "None";
7d203a9e 548 seq_printf(m, "Serial Number: %s\n", string);
1da177e4
LT
549
550 /* show the protocol and transport */
7d203a9e
JP
551 seq_printf(m, " Protocol: %s\n", us->protocol_name);
552 seq_printf(m, " Transport: %s\n", us->transport_name);
1da177e4
LT
553
554 /* show the device flags */
7d203a9e 555 seq_printf(m, " Quirks:");
1da177e4
LT
556
557#define US_FLAG(name, value) \
09dae7fc 558 if (us->fflags & value) seq_printf(m, " " #name);
1da177e4
LT
559US_DO_ALL_FLAGS
560#undef US_FLAG
09dae7fc
AV
561 seq_putc(m, '\n');
562 return 0;
1da177e4
LT
563}
564
565/***********************************************************************
566 * Sysfs interface
567 ***********************************************************************/
568
569/* Output routine for the sysfs max_sectors file */
f2e0ae93 570static ssize_t max_sectors_show(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
571{
572 struct scsi_device *sdev = to_scsi_device(dev);
573
49d6271b 574 return sprintf(buf, "%u\n", queue_max_hw_sectors(sdev->request_queue));
1da177e4
LT
575}
576
577/* Input routine for the sysfs max_sectors file */
f2e0ae93 578static ssize_t max_sectors_store(struct device *dev, struct device_attribute *attr, const char *buf,
1da177e4
LT
579 size_t count)
580{
581 struct scsi_device *sdev = to_scsi_device(dev);
582 unsigned short ms;
583
49d6271b 584 if (sscanf(buf, "%hu", &ms) > 0) {
086fa5ff 585 blk_queue_max_hw_sectors(sdev->request_queue, ms);
49d6271b 586 return count;
1da177e4 587 }
f2e0ae93 588 return -EINVAL;
1da177e4 589}
f2e0ae93 590static DEVICE_ATTR_RW(max_sectors);
1da177e4
LT
591
592static struct device_attribute *sysfs_device_attr_list[] = {
f2e0ae93
GKH
593 &dev_attr_max_sectors,
594 NULL,
595};
1da177e4
LT
596
597/*
598 * this defines our host template, with which we'll allocate hosts
599 */
600
aa519be3 601static const struct scsi_host_template usb_stor_host_template = {
1da177e4
LT
602 /* basic userland interface stuff */
603 .name = "usb-storage",
604 .proc_name = "usb-storage",
09dae7fc
AV
605 .show_info = show_info,
606 .write_info = write_info,
1da177e4
LT
607 .info = host_info,
608
609 /* command interface -- queued only */
610 .queuecommand = queuecommand,
611
612 /* error and abort handlers */
613 .eh_abort_handler = command_abort,
614 .eh_device_reset_handler = device_reset,
615 .eh_bus_reset_handler = bus_reset,
616
617 /* queue commands only, only one command per LUN */
618 .can_queue = 1,
1da177e4
LT
619
620 /* unknown initiator id */
621 .this_id = -1,
622
623 .slave_alloc = slave_alloc,
624 .slave_configure = slave_configure,
09b6b51b 625 .target_alloc = target_alloc,
1da177e4
LT
626
627 /* lots of sg segments can be handled */
65e8617f 628 .sg_tablesize = SG_MAX_SEGMENTS,
1da177e4 629
779b457f
FB
630
631 /*
632 * Limit the total size of a transfer to 120 KB.
633 *
634 * Some devices are known to choke with anything larger. It seems like
635 * the problem stems from the fact that original IDE controllers had
636 * only an 8-bit register to hold the number of sectors in one transfer
637 * and even those couldn't handle a full 256 sectors.
638 *
639 * Because we want to make sure we interoperate with as many devices as
640 * possible, we will maintain a 240 sector transfer size limit for USB
641 * Mass Storage devices.
642 *
643 * Tests show that other operating have similar limits with Microsoft
644 * Windows 7 limiting transfers to 128 sectors for both USB2 and USB3
645 * and Apple Mac OS X 10.11 limiting transfers to 256 sectors for USB2
646 * and 2048 for USB3 devices.
647 */
1da177e4
LT
648 .max_sectors = 240,
649
f0183a33
FB
650 /*
651 * merge commands... this seems to help performance, but
1da177e4
LT
652 * periodically someone should test to see which setting is more
653 * optimal.
654 */
655 .use_clustering = 1,
656
657 /* emulated HBA */
658 .emulated = 1,
659
660 /* we do our own delay after a device or bus reset */
661 .skip_settle_delay = 1,
662
663 /* sysfs device attributes */
664 .sdev_attrs = sysfs_device_attr_list,
665
666 /* module management */
667 .module = THIS_MODULE
668};
669
aa519be3
AM
670void usb_stor_host_template_init(struct scsi_host_template *sht,
671 const char *name, struct module *owner)
672{
673 *sht = usb_stor_host_template;
674 sht->name = name;
675 sht->proc_name = name;
676 sht->module = owner;
677}
678EXPORT_SYMBOL_GPL(usb_stor_host_template_init);
679
1da177e4
LT
680/* To Report "Illegal Request: Invalid Field in CDB */
681unsigned char usb_stor_sense_invalidCDB[18] = {
682 [0] = 0x70, /* current error */
683 [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */
684 [7] = 0x0a, /* additional length */
685 [12] = 0x24 /* Invalid Field in CDB */
686};
e6e244b6 687EXPORT_SYMBOL_GPL(usb_stor_sense_invalidCDB);