2 * sd.c Copyright (C) 1992 Drew Eckhardt
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
5 * Linux scsi disk driver
6 * Initial versions: Drew Eckhardt
7 * Subsequent revisions: Eric Youngdale
8 * Modification history:
9 * - Drew Eckhardt <drew@colorado.edu> original
10 * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple
11 * outstanding request, and other enhancements.
12 * Support loadable low-level scsi drivers.
13 * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using
14 * eight major numbers.
15 * - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
16 * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in
17 * sd_init and cleanups.
18 * - Alex Davis <letmein@erols.com> Fix problem where partition info
19 * not being read in sd_open. Fix problem where removable media
20 * could be ejected after sd_open.
21 * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
22 * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox
23 * <willy@debian.org>, Kurt Garloff <garloff@suse.de>:
24 * Support 32k/1M disks.
26 * Logging policy (needs CONFIG_SCSI_LOGGING defined):
27 * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
28 * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
29 * - entering sd_ioctl: SCSI_LOG_IOCTL level 1
30 * - entering other commands: SCSI_LOG_HLQUEUE level 3
31 * Note: when the logging level is set by the user, it must be greater
32 * than the level indicated above to trigger output.
35 #include <linux/module.h>
37 #include <linux/kernel.h>
39 #include <linux/bio.h>
40 #include <linux/genhd.h>
41 #include <linux/hdreg.h>
42 #include <linux/errno.h>
43 #include <linux/idr.h>
44 #include <linux/interrupt.h>
45 #include <linux/init.h>
46 #include <linux/blkdev.h>
47 #include <linux/blkpg.h>
48 #include <linux/delay.h>
49 #include <linux/mutex.h>
50 #include <linux/string_helpers.h>
51 #include <linux/async.h>
52 #include <linux/slab.h>
53 #include <linux/pm_runtime.h>
55 #include <asm/uaccess.h>
56 #include <asm/unaligned.h>
58 #include <scsi/scsi.h>
59 #include <scsi/scsi_cmnd.h>
60 #include <scsi/scsi_dbg.h>
61 #include <scsi/scsi_device.h>
62 #include <scsi/scsi_driver.h>
63 #include <scsi/scsi_eh.h>
64 #include <scsi/scsi_host.h>
65 #include <scsi/scsi_ioctl.h>
66 #include <scsi/scsicam.h>
69 #include "scsi_priv.h"
70 #include "scsi_logging.h"
72 MODULE_AUTHOR("Eric Youngdale");
73 MODULE_DESCRIPTION("SCSI disk (sd) driver");
74 MODULE_LICENSE("GPL");
76 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR
);
77 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR
);
78 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR
);
79 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR
);
80 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR
);
81 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR
);
82 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR
);
83 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR
);
84 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR
);
85 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR
);
86 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR
);
87 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR
);
88 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR
);
89 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR
);
90 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR
);
91 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR
);
92 MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK
);
93 MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD
);
94 MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC
);
96 #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
102 static void sd_config_discard(struct scsi_disk
*, unsigned int);
103 static void sd_config_write_same(struct scsi_disk
*);
104 static int sd_revalidate_disk(struct gendisk
*);
105 static void sd_unlock_native_capacity(struct gendisk
*disk
);
106 static int sd_probe(struct device
*);
107 static int sd_remove(struct device
*);
108 static void sd_shutdown(struct device
*);
109 static int sd_suspend_system(struct device
*);
110 static int sd_suspend_runtime(struct device
*);
111 static int sd_resume(struct device
*);
112 static void sd_rescan(struct device
*);
113 static int sd_init_command(struct scsi_cmnd
*SCpnt
);
114 static void sd_uninit_command(struct scsi_cmnd
*SCpnt
);
115 static int sd_done(struct scsi_cmnd
*);
116 static int sd_eh_action(struct scsi_cmnd
*, int);
117 static void sd_read_capacity(struct scsi_disk
*sdkp
, unsigned char *buffer
);
118 static void scsi_disk_release(struct device
*cdev
);
119 static void sd_print_sense_hdr(struct scsi_disk
*, struct scsi_sense_hdr
*);
120 static void sd_print_result(const struct scsi_disk
*, const char *, int);
122 static DEFINE_SPINLOCK(sd_index_lock
);
123 static DEFINE_IDA(sd_index_ida
);
125 /* This semaphore is used to mediate the 0->1 reference get in the
126 * face of object destruction (i.e. we can't allow a get on an
127 * object after last put) */
128 static DEFINE_MUTEX(sd_ref_mutex
);
130 static struct kmem_cache
*sd_cdb_cache
;
131 static mempool_t
*sd_cdb_pool
;
133 static const char *sd_cache_types
[] = {
134 "write through", "none", "write back",
135 "write back, no read (daft)"
138 static void sd_set_flush_flag(struct scsi_disk
*sdkp
)
148 blk_queue_flush(sdkp
->disk
->queue
, flush
);
152 cache_type_store(struct device
*dev
, struct device_attribute
*attr
,
153 const char *buf
, size_t count
)
155 int i
, ct
= -1, rcd
, wce
, sp
;
156 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
157 struct scsi_device
*sdp
= sdkp
->device
;
160 struct scsi_mode_data data
;
161 struct scsi_sense_hdr sshdr
;
162 static const char temp
[] = "temporary ";
165 if (sdp
->type
!= TYPE_DISK
)
166 /* no cache control on RBC devices; theoretically they
167 * can do it, but there's probably so many exceptions
168 * it's not worth the risk */
171 if (strncmp(buf
, temp
, sizeof(temp
) - 1) == 0) {
172 buf
+= sizeof(temp
) - 1;
173 sdkp
->cache_override
= 1;
175 sdkp
->cache_override
= 0;
178 for (i
= 0; i
< ARRAY_SIZE(sd_cache_types
); i
++) {
179 len
= strlen(sd_cache_types
[i
]);
180 if (strncmp(sd_cache_types
[i
], buf
, len
) == 0 &&
188 rcd
= ct
& 0x01 ? 1 : 0;
189 wce
= (ct
& 0x02) && !sdkp
->write_prot
? 1 : 0;
191 if (sdkp
->cache_override
) {
194 sd_set_flush_flag(sdkp
);
198 if (scsi_mode_sense(sdp
, 0x08, 8, buffer
, sizeof(buffer
), SD_TIMEOUT
,
199 SD_MAX_RETRIES
, &data
, NULL
))
201 len
= min_t(size_t, sizeof(buffer
), data
.length
- data
.header_length
-
202 data
.block_descriptor_length
);
203 buffer_data
= buffer
+ data
.header_length
+
204 data
.block_descriptor_length
;
205 buffer_data
[2] &= ~0x05;
206 buffer_data
[2] |= wce
<< 2 | rcd
;
207 sp
= buffer_data
[0] & 0x80 ? 1 : 0;
208 buffer_data
[0] &= ~0x80;
210 if (scsi_mode_select(sdp
, 1, sp
, 8, buffer_data
, len
, SD_TIMEOUT
,
211 SD_MAX_RETRIES
, &data
, &sshdr
)) {
212 if (scsi_sense_valid(&sshdr
))
213 sd_print_sense_hdr(sdkp
, &sshdr
);
216 revalidate_disk(sdkp
->disk
);
221 manage_start_stop_show(struct device
*dev
, struct device_attribute
*attr
,
224 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
225 struct scsi_device
*sdp
= sdkp
->device
;
227 return snprintf(buf
, 20, "%u\n", sdp
->manage_start_stop
);
231 manage_start_stop_store(struct device
*dev
, struct device_attribute
*attr
,
232 const char *buf
, size_t count
)
234 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
235 struct scsi_device
*sdp
= sdkp
->device
;
238 if (!capable(CAP_SYS_ADMIN
))
241 if (kstrtobool(buf
, &v
))
244 sdp
->manage_start_stop
= v
;
248 static DEVICE_ATTR_RW(manage_start_stop
);
251 allow_restart_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
253 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
255 return snprintf(buf
, 40, "%d\n", sdkp
->device
->allow_restart
);
259 allow_restart_store(struct device
*dev
, struct device_attribute
*attr
,
260 const char *buf
, size_t count
)
263 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
264 struct scsi_device
*sdp
= sdkp
->device
;
266 if (!capable(CAP_SYS_ADMIN
))
269 if (sdp
->type
!= TYPE_DISK
)
272 if (kstrtobool(buf
, &v
))
275 sdp
->allow_restart
= v
;
279 static DEVICE_ATTR_RW(allow_restart
);
282 cache_type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
284 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
285 int ct
= sdkp
->RCD
+ 2*sdkp
->WCE
;
287 return snprintf(buf
, 40, "%s\n", sd_cache_types
[ct
]);
289 static DEVICE_ATTR_RW(cache_type
);
292 FUA_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
294 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
296 return snprintf(buf
, 20, "%u\n", sdkp
->DPOFUA
);
298 static DEVICE_ATTR_RO(FUA
);
301 protection_type_show(struct device
*dev
, struct device_attribute
*attr
,
304 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
306 return snprintf(buf
, 20, "%u\n", sdkp
->protection_type
);
310 protection_type_store(struct device
*dev
, struct device_attribute
*attr
,
311 const char *buf
, size_t count
)
313 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
317 if (!capable(CAP_SYS_ADMIN
))
320 err
= kstrtouint(buf
, 10, &val
);
325 if (val
>= 0 && val
<= SD_DIF_TYPE3_PROTECTION
)
326 sdkp
->protection_type
= val
;
330 static DEVICE_ATTR_RW(protection_type
);
333 protection_mode_show(struct device
*dev
, struct device_attribute
*attr
,
336 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
337 struct scsi_device
*sdp
= sdkp
->device
;
338 unsigned int dif
, dix
;
340 dif
= scsi_host_dif_capable(sdp
->host
, sdkp
->protection_type
);
341 dix
= scsi_host_dix_capable(sdp
->host
, sdkp
->protection_type
);
343 if (!dix
&& scsi_host_dix_capable(sdp
->host
, SD_DIF_TYPE0_PROTECTION
)) {
349 return snprintf(buf
, 20, "none\n");
351 return snprintf(buf
, 20, "%s%u\n", dix
? "dix" : "dif", dif
);
353 static DEVICE_ATTR_RO(protection_mode
);
356 app_tag_own_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
358 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
360 return snprintf(buf
, 20, "%u\n", sdkp
->ATO
);
362 static DEVICE_ATTR_RO(app_tag_own
);
365 thin_provisioning_show(struct device
*dev
, struct device_attribute
*attr
,
368 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
370 return snprintf(buf
, 20, "%u\n", sdkp
->lbpme
);
372 static DEVICE_ATTR_RO(thin_provisioning
);
374 static const char *lbp_mode
[] = {
375 [SD_LBP_FULL
] = "full",
376 [SD_LBP_UNMAP
] = "unmap",
377 [SD_LBP_WS16
] = "writesame_16",
378 [SD_LBP_WS10
] = "writesame_10",
379 [SD_LBP_ZERO
] = "writesame_zero",
380 [SD_LBP_DISABLE
] = "disabled",
384 provisioning_mode_show(struct device
*dev
, struct device_attribute
*attr
,
387 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
389 return snprintf(buf
, 20, "%s\n", lbp_mode
[sdkp
->provisioning_mode
]);
393 provisioning_mode_store(struct device
*dev
, struct device_attribute
*attr
,
394 const char *buf
, size_t count
)
396 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
397 struct scsi_device
*sdp
= sdkp
->device
;
399 if (!capable(CAP_SYS_ADMIN
))
402 if (sdp
->type
!= TYPE_DISK
)
405 if (!strncmp(buf
, lbp_mode
[SD_LBP_UNMAP
], 20))
406 sd_config_discard(sdkp
, SD_LBP_UNMAP
);
407 else if (!strncmp(buf
, lbp_mode
[SD_LBP_WS16
], 20))
408 sd_config_discard(sdkp
, SD_LBP_WS16
);
409 else if (!strncmp(buf
, lbp_mode
[SD_LBP_WS10
], 20))
410 sd_config_discard(sdkp
, SD_LBP_WS10
);
411 else if (!strncmp(buf
, lbp_mode
[SD_LBP_ZERO
], 20))
412 sd_config_discard(sdkp
, SD_LBP_ZERO
);
413 else if (!strncmp(buf
, lbp_mode
[SD_LBP_DISABLE
], 20))
414 sd_config_discard(sdkp
, SD_LBP_DISABLE
);
420 static DEVICE_ATTR_RW(provisioning_mode
);
423 max_medium_access_timeouts_show(struct device
*dev
,
424 struct device_attribute
*attr
, char *buf
)
426 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
428 return snprintf(buf
, 20, "%u\n", sdkp
->max_medium_access_timeouts
);
432 max_medium_access_timeouts_store(struct device
*dev
,
433 struct device_attribute
*attr
, const char *buf
,
436 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
439 if (!capable(CAP_SYS_ADMIN
))
442 err
= kstrtouint(buf
, 10, &sdkp
->max_medium_access_timeouts
);
444 return err
? err
: count
;
446 static DEVICE_ATTR_RW(max_medium_access_timeouts
);
449 max_write_same_blocks_show(struct device
*dev
, struct device_attribute
*attr
,
452 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
454 return snprintf(buf
, 20, "%u\n", sdkp
->max_ws_blocks
);
458 max_write_same_blocks_store(struct device
*dev
, struct device_attribute
*attr
,
459 const char *buf
, size_t count
)
461 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
462 struct scsi_device
*sdp
= sdkp
->device
;
466 if (!capable(CAP_SYS_ADMIN
))
469 if (sdp
->type
!= TYPE_DISK
)
472 err
= kstrtoul(buf
, 10, &max
);
478 sdp
->no_write_same
= 1;
479 else if (max
<= SD_MAX_WS16_BLOCKS
) {
480 sdp
->no_write_same
= 0;
481 sdkp
->max_ws_blocks
= max
;
484 sd_config_write_same(sdkp
);
488 static DEVICE_ATTR_RW(max_write_same_blocks
);
490 static struct attribute
*sd_disk_attrs
[] = {
491 &dev_attr_cache_type
.attr
,
493 &dev_attr_allow_restart
.attr
,
494 &dev_attr_manage_start_stop
.attr
,
495 &dev_attr_protection_type
.attr
,
496 &dev_attr_protection_mode
.attr
,
497 &dev_attr_app_tag_own
.attr
,
498 &dev_attr_thin_provisioning
.attr
,
499 &dev_attr_provisioning_mode
.attr
,
500 &dev_attr_max_write_same_blocks
.attr
,
501 &dev_attr_max_medium_access_timeouts
.attr
,
504 ATTRIBUTE_GROUPS(sd_disk
);
506 static struct class sd_disk_class
= {
508 .owner
= THIS_MODULE
,
509 .dev_release
= scsi_disk_release
,
510 .dev_groups
= sd_disk_groups
,
513 static const struct dev_pm_ops sd_pm_ops
= {
514 .suspend
= sd_suspend_system
,
516 .poweroff
= sd_suspend_system
,
517 .restore
= sd_resume
,
518 .runtime_suspend
= sd_suspend_runtime
,
519 .runtime_resume
= sd_resume
,
522 static struct scsi_driver sd_template
= {
525 .owner
= THIS_MODULE
,
528 .shutdown
= sd_shutdown
,
532 .init_command
= sd_init_command
,
533 .uninit_command
= sd_uninit_command
,
535 .eh_action
= sd_eh_action
,
539 * Dummy kobj_map->probe function.
540 * The default ->probe function will call modprobe, which is
541 * pointless as this module is already loaded.
543 static struct kobject
*sd_default_probe(dev_t devt
, int *partno
, void *data
)
549 * Device no to disk mapping:
551 * major disc2 disc p1
552 * |............|.............|....|....| <- dev_t
555 * Inside a major, we have 16k disks, however mapped non-
556 * contiguously. The first 16 disks are for major0, the next
557 * ones with major1, ... Disk 256 is for major0 again, disk 272
559 * As we stay compatible with our numbering scheme, we can reuse
560 * the well-know SCSI majors 8, 65--71, 136--143.
562 static int sd_major(int major_idx
)
566 return SCSI_DISK0_MAJOR
;
568 return SCSI_DISK1_MAJOR
+ major_idx
- 1;
570 return SCSI_DISK8_MAJOR
+ major_idx
- 8;
573 return 0; /* shut up gcc */
577 static struct scsi_disk
*scsi_disk_get(struct gendisk
*disk
)
579 struct scsi_disk
*sdkp
= NULL
;
581 mutex_lock(&sd_ref_mutex
);
583 if (disk
->private_data
) {
584 sdkp
= scsi_disk(disk
);
585 if (scsi_device_get(sdkp
->device
) == 0)
586 get_device(&sdkp
->dev
);
590 mutex_unlock(&sd_ref_mutex
);
594 static void scsi_disk_put(struct scsi_disk
*sdkp
)
596 struct scsi_device
*sdev
= sdkp
->device
;
598 mutex_lock(&sd_ref_mutex
);
599 put_device(&sdkp
->dev
);
600 scsi_device_put(sdev
);
601 mutex_unlock(&sd_ref_mutex
);
604 static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd
*scmd
,
605 unsigned int dix
, unsigned int dif
)
607 struct bio
*bio
= scmd
->request
->bio
;
608 unsigned int prot_op
= sd_prot_op(rq_data_dir(scmd
->request
), dix
, dif
);
609 unsigned int protect
= 0;
611 if (dix
) { /* DIX Type 0, 1, 2, 3 */
612 if (bio_integrity_flagged(bio
, BIP_IP_CHECKSUM
))
613 scmd
->prot_flags
|= SCSI_PROT_IP_CHECKSUM
;
615 if (bio_integrity_flagged(bio
, BIP_CTRL_NOCHECK
) == false)
616 scmd
->prot_flags
|= SCSI_PROT_GUARD_CHECK
;
619 if (dif
!= SD_DIF_TYPE3_PROTECTION
) { /* DIX/DIF Type 0, 1, 2 */
620 scmd
->prot_flags
|= SCSI_PROT_REF_INCREMENT
;
622 if (bio_integrity_flagged(bio
, BIP_CTRL_NOCHECK
) == false)
623 scmd
->prot_flags
|= SCSI_PROT_REF_CHECK
;
626 if (dif
) { /* DIX/DIF Type 1, 2, 3 */
627 scmd
->prot_flags
|= SCSI_PROT_TRANSFER_PI
;
629 if (bio_integrity_flagged(bio
, BIP_DISK_NOCHECK
))
630 protect
= 3 << 5; /* Disable target PI checking */
632 protect
= 1 << 5; /* Enable target PI checking */
635 scsi_set_prot_op(scmd
, prot_op
);
636 scsi_set_prot_type(scmd
, dif
);
637 scmd
->prot_flags
&= sd_prot_flag_mask(prot_op
);
642 static void sd_config_discard(struct scsi_disk
*sdkp
, unsigned int mode
)
644 struct request_queue
*q
= sdkp
->disk
->queue
;
645 unsigned int logical_block_size
= sdkp
->device
->sector_size
;
646 unsigned int max_blocks
= 0;
648 q
->limits
.discard_zeroes_data
= 0;
651 * When LBPRZ is reported, discard alignment and granularity
652 * must be fixed to the logical block size. Otherwise the block
653 * layer will drop misaligned portions of the request which can
654 * lead to data corruption. If LBPRZ is not set, we honor the
658 q
->limits
.discard_alignment
= 0;
659 q
->limits
.discard_granularity
= logical_block_size
;
661 q
->limits
.discard_alignment
= sdkp
->unmap_alignment
*
663 q
->limits
.discard_granularity
=
664 max(sdkp
->physical_block_size
,
665 sdkp
->unmap_granularity
* logical_block_size
);
668 sdkp
->provisioning_mode
= mode
;
673 blk_queue_max_discard_sectors(q
, 0);
674 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
, q
);
678 max_blocks
= min_not_zero(sdkp
->max_unmap_blocks
,
679 (u32
)SD_MAX_WS16_BLOCKS
);
683 max_blocks
= min_not_zero(sdkp
->max_ws_blocks
,
684 (u32
)SD_MAX_WS16_BLOCKS
);
685 q
->limits
.discard_zeroes_data
= sdkp
->lbprz
;
689 max_blocks
= min_not_zero(sdkp
->max_ws_blocks
,
690 (u32
)SD_MAX_WS10_BLOCKS
);
691 q
->limits
.discard_zeroes_data
= sdkp
->lbprz
;
695 max_blocks
= min_not_zero(sdkp
->max_ws_blocks
,
696 (u32
)SD_MAX_WS10_BLOCKS
);
697 q
->limits
.discard_zeroes_data
= 1;
701 blk_queue_max_discard_sectors(q
, max_blocks
* (logical_block_size
>> 9));
702 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, q
);
706 * sd_setup_discard_cmnd - unmap blocks on thinly provisioned device
707 * @sdp: scsi device to operate one
708 * @rq: Request to prepare
710 * Will issue either UNMAP or WRITE SAME(16) depending on preference
711 * indicated by target device.
713 static int sd_setup_discard_cmnd(struct scsi_cmnd
*cmd
)
715 struct request
*rq
= cmd
->request
;
716 struct scsi_device
*sdp
= cmd
->device
;
717 struct scsi_disk
*sdkp
= scsi_disk(rq
->rq_disk
);
718 sector_t sector
= blk_rq_pos(rq
);
719 unsigned int nr_sectors
= blk_rq_sectors(rq
);
720 unsigned int nr_bytes
= blk_rq_bytes(rq
);
726 sector
>>= ilog2(sdp
->sector_size
) - 9;
727 nr_sectors
>>= ilog2(sdp
->sector_size
) - 9;
729 page
= alloc_page(GFP_ATOMIC
| __GFP_ZERO
);
731 return BLKPREP_DEFER
;
733 switch (sdkp
->provisioning_mode
) {
735 buf
= page_address(page
);
738 cmd
->cmnd
[0] = UNMAP
;
741 put_unaligned_be16(6 + 16, &buf
[0]);
742 put_unaligned_be16(16, &buf
[2]);
743 put_unaligned_be64(sector
, &buf
[8]);
744 put_unaligned_be32(nr_sectors
, &buf
[16]);
751 cmd
->cmnd
[0] = WRITE_SAME_16
;
752 cmd
->cmnd
[1] = 0x8; /* UNMAP */
753 put_unaligned_be64(sector
, &cmd
->cmnd
[2]);
754 put_unaligned_be32(nr_sectors
, &cmd
->cmnd
[10]);
756 len
= sdkp
->device
->sector_size
;
762 cmd
->cmnd
[0] = WRITE_SAME
;
763 if (sdkp
->provisioning_mode
== SD_LBP_WS10
)
764 cmd
->cmnd
[1] = 0x8; /* UNMAP */
765 put_unaligned_be32(sector
, &cmd
->cmnd
[2]);
766 put_unaligned_be16(nr_sectors
, &cmd
->cmnd
[7]);
768 len
= sdkp
->device
->sector_size
;
776 rq
->completion_data
= page
;
777 rq
->timeout
= SD_TIMEOUT
;
779 cmd
->transfersize
= len
;
780 cmd
->allowed
= SD_MAX_RETRIES
;
783 * Initially __data_len is set to the amount of data that needs to be
784 * transferred to the target. This amount depends on whether WRITE SAME
785 * or UNMAP is being used. After the scatterlist has been mapped by
786 * scsi_init_io() we set __data_len to the size of the area to be
787 * discarded on disk. This allows us to report completion on the full
788 * amount of blocks described by the request.
790 blk_add_request_payload(rq
, page
, len
);
791 ret
= scsi_init_io(cmd
);
792 rq
->__data_len
= nr_bytes
;
795 if (ret
!= BLKPREP_OK
)
800 static void sd_config_write_same(struct scsi_disk
*sdkp
)
802 struct request_queue
*q
= sdkp
->disk
->queue
;
803 unsigned int logical_block_size
= sdkp
->device
->sector_size
;
805 if (sdkp
->device
->no_write_same
) {
806 sdkp
->max_ws_blocks
= 0;
810 /* Some devices can not handle block counts above 0xffff despite
811 * supporting WRITE SAME(16). Consequently we default to 64k
812 * blocks per I/O unless the device explicitly advertises a
815 if (sdkp
->max_ws_blocks
> SD_MAX_WS10_BLOCKS
)
816 sdkp
->max_ws_blocks
= min_not_zero(sdkp
->max_ws_blocks
,
817 (u32
)SD_MAX_WS16_BLOCKS
);
818 else if (sdkp
->ws16
|| sdkp
->ws10
|| sdkp
->device
->no_report_opcodes
)
819 sdkp
->max_ws_blocks
= min_not_zero(sdkp
->max_ws_blocks
,
820 (u32
)SD_MAX_WS10_BLOCKS
);
822 sdkp
->device
->no_write_same
= 1;
823 sdkp
->max_ws_blocks
= 0;
827 blk_queue_max_write_same_sectors(q
, sdkp
->max_ws_blocks
*
828 (logical_block_size
>> 9));
832 * sd_setup_write_same_cmnd - write the same data to multiple blocks
833 * @cmd: command to prepare
835 * Will issue either WRITE SAME(10) or WRITE SAME(16) depending on
836 * preference indicated by target device.
838 static int sd_setup_write_same_cmnd(struct scsi_cmnd
*cmd
)
840 struct request
*rq
= cmd
->request
;
841 struct scsi_device
*sdp
= cmd
->device
;
842 struct scsi_disk
*sdkp
= scsi_disk(rq
->rq_disk
);
843 struct bio
*bio
= rq
->bio
;
844 sector_t sector
= blk_rq_pos(rq
);
845 unsigned int nr_sectors
= blk_rq_sectors(rq
);
846 unsigned int nr_bytes
= blk_rq_bytes(rq
);
849 if (sdkp
->device
->no_write_same
)
852 BUG_ON(bio_offset(bio
) || bio_iovec(bio
).bv_len
!= sdp
->sector_size
);
854 sector
>>= ilog2(sdp
->sector_size
) - 9;
855 nr_sectors
>>= ilog2(sdp
->sector_size
) - 9;
857 rq
->timeout
= SD_WRITE_SAME_TIMEOUT
;
859 if (sdkp
->ws16
|| sector
> 0xffffffff || nr_sectors
> 0xffff) {
861 cmd
->cmnd
[0] = WRITE_SAME_16
;
862 put_unaligned_be64(sector
, &cmd
->cmnd
[2]);
863 put_unaligned_be32(nr_sectors
, &cmd
->cmnd
[10]);
866 cmd
->cmnd
[0] = WRITE_SAME
;
867 put_unaligned_be32(sector
, &cmd
->cmnd
[2]);
868 put_unaligned_be16(nr_sectors
, &cmd
->cmnd
[7]);
871 cmd
->transfersize
= sdp
->sector_size
;
872 cmd
->allowed
= SD_MAX_RETRIES
;
875 * For WRITE_SAME the data transferred in the DATA IN buffer is
876 * different from the amount of data actually written to the target.
878 * We set up __data_len to the amount of data transferred from the
879 * DATA IN buffer so that blk_rq_map_sg set up the proper S/G list
880 * to transfer a single sector of data first, but then reset it to
881 * the amount of data to be written right after so that the I/O path
882 * knows how much to actually write.
884 rq
->__data_len
= sdp
->sector_size
;
885 ret
= scsi_init_io(cmd
);
886 rq
->__data_len
= nr_bytes
;
890 static int sd_setup_flush_cmnd(struct scsi_cmnd
*cmd
)
892 struct request
*rq
= cmd
->request
;
894 /* flush requests don't perform I/O, zero the S/G table */
895 memset(&cmd
->sdb
, 0, sizeof(cmd
->sdb
));
897 cmd
->cmnd
[0] = SYNCHRONIZE_CACHE
;
899 cmd
->transfersize
= 0;
900 cmd
->allowed
= SD_MAX_RETRIES
;
902 rq
->timeout
= rq
->q
->rq_timeout
* SD_FLUSH_TIMEOUT_MULTIPLIER
;
906 static int sd_setup_read_write_cmnd(struct scsi_cmnd
*SCpnt
)
908 struct request
*rq
= SCpnt
->request
;
909 struct scsi_device
*sdp
= SCpnt
->device
;
910 struct gendisk
*disk
= rq
->rq_disk
;
911 struct scsi_disk
*sdkp
;
912 sector_t block
= blk_rq_pos(rq
);
914 unsigned int this_count
= blk_rq_sectors(rq
);
915 unsigned int dif
, dix
;
917 unsigned char protect
;
919 ret
= scsi_init_io(SCpnt
);
920 if (ret
!= BLKPREP_OK
)
923 sdkp
= scsi_disk(disk
);
925 /* from here on until we're complete, any goto out
926 * is used for a killable error condition */
930 scmd_printk(KERN_INFO
, SCpnt
,
931 "%s: block=%llu, count=%d\n",
932 __func__
, (unsigned long long)block
, this_count
));
934 if (!sdp
|| !scsi_device_online(sdp
) ||
935 block
+ blk_rq_sectors(rq
) > get_capacity(disk
)) {
936 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO
, SCpnt
,
937 "Finishing %u sectors\n",
938 blk_rq_sectors(rq
)));
939 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO
, SCpnt
,
940 "Retry with 0x%p\n", SCpnt
));
946 * quietly refuse to do anything to a changed disc until
947 * the changed bit has been reset
949 /* printk("SCSI disk has been changed or is not present. Prohibiting further I/O.\n"); */
954 * Some SD card readers can't handle multi-sector accesses which touch
955 * the last one or two hardware sectors. Split accesses as needed.
957 threshold
= get_capacity(disk
) - SD_LAST_BUGGY_SECTORS
*
958 (sdp
->sector_size
/ 512);
960 if (unlikely(sdp
->last_sector_bug
&& block
+ this_count
> threshold
)) {
961 if (block
< threshold
) {
962 /* Access up to the threshold but not beyond */
963 this_count
= threshold
- block
;
965 /* Access only a single hardware sector */
966 this_count
= sdp
->sector_size
/ 512;
970 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO
, SCpnt
, "block=%llu\n",
971 (unsigned long long)block
));
974 * If we have a 1K hardware sectorsize, prevent access to single
975 * 512 byte sectors. In theory we could handle this - in fact
976 * the scsi cdrom driver must be able to handle this because
977 * we typically use 1K blocksizes, and cdroms typically have
978 * 2K hardware sectorsizes. Of course, things are simpler
979 * with the cdrom, since it is read-only. For performance
980 * reasons, the filesystems should be able to handle this
981 * and not force the scsi disk driver to use bounce buffers
984 if (sdp
->sector_size
== 1024) {
985 if ((block
& 1) || (blk_rq_sectors(rq
) & 1)) {
986 scmd_printk(KERN_ERR
, SCpnt
,
987 "Bad block number requested\n");
991 this_count
= this_count
>> 1;
994 if (sdp
->sector_size
== 2048) {
995 if ((block
& 3) || (blk_rq_sectors(rq
) & 3)) {
996 scmd_printk(KERN_ERR
, SCpnt
,
997 "Bad block number requested\n");
1001 this_count
= this_count
>> 2;
1004 if (sdp
->sector_size
== 4096) {
1005 if ((block
& 7) || (blk_rq_sectors(rq
) & 7)) {
1006 scmd_printk(KERN_ERR
, SCpnt
,
1007 "Bad block number requested\n");
1011 this_count
= this_count
>> 3;
1014 if (rq_data_dir(rq
) == WRITE
) {
1015 SCpnt
->cmnd
[0] = WRITE_6
;
1017 if (blk_integrity_rq(rq
))
1018 sd_dif_prepare(SCpnt
);
1020 } else if (rq_data_dir(rq
) == READ
) {
1021 SCpnt
->cmnd
[0] = READ_6
;
1023 scmd_printk(KERN_ERR
, SCpnt
, "Unknown command %llx\n", (unsigned long long) rq
->cmd_flags
);
1027 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO
, SCpnt
,
1028 "%s %d/%u 512 byte blocks.\n",
1029 (rq_data_dir(rq
) == WRITE
) ?
1030 "writing" : "reading", this_count
,
1031 blk_rq_sectors(rq
)));
1033 dix
= scsi_prot_sg_count(SCpnt
);
1034 dif
= scsi_host_dif_capable(SCpnt
->device
->host
, sdkp
->protection_type
);
1037 protect
= sd_setup_protect_cmnd(SCpnt
, dix
, dif
);
1041 if (protect
&& sdkp
->protection_type
== SD_DIF_TYPE2_PROTECTION
) {
1042 SCpnt
->cmnd
= mempool_alloc(sd_cdb_pool
, GFP_ATOMIC
);
1044 if (unlikely(SCpnt
->cmnd
== NULL
)) {
1045 ret
= BLKPREP_DEFER
;
1049 SCpnt
->cmd_len
= SD_EXT_CDB_SIZE
;
1050 memset(SCpnt
->cmnd
, 0, SCpnt
->cmd_len
);
1051 SCpnt
->cmnd
[0] = VARIABLE_LENGTH_CMD
;
1052 SCpnt
->cmnd
[7] = 0x18;
1053 SCpnt
->cmnd
[9] = (rq_data_dir(rq
) == READ
) ? READ_32
: WRITE_32
;
1054 SCpnt
->cmnd
[10] = protect
| ((rq
->cmd_flags
& REQ_FUA
) ? 0x8 : 0);
1057 SCpnt
->cmnd
[12] = sizeof(block
) > 4 ? (unsigned char) (block
>> 56) & 0xff : 0;
1058 SCpnt
->cmnd
[13] = sizeof(block
) > 4 ? (unsigned char) (block
>> 48) & 0xff : 0;
1059 SCpnt
->cmnd
[14] = sizeof(block
) > 4 ? (unsigned char) (block
>> 40) & 0xff : 0;
1060 SCpnt
->cmnd
[15] = sizeof(block
) > 4 ? (unsigned char) (block
>> 32) & 0xff : 0;
1061 SCpnt
->cmnd
[16] = (unsigned char) (block
>> 24) & 0xff;
1062 SCpnt
->cmnd
[17] = (unsigned char) (block
>> 16) & 0xff;
1063 SCpnt
->cmnd
[18] = (unsigned char) (block
>> 8) & 0xff;
1064 SCpnt
->cmnd
[19] = (unsigned char) block
& 0xff;
1066 /* Expected Indirect LBA */
1067 SCpnt
->cmnd
[20] = (unsigned char) (block
>> 24) & 0xff;
1068 SCpnt
->cmnd
[21] = (unsigned char) (block
>> 16) & 0xff;
1069 SCpnt
->cmnd
[22] = (unsigned char) (block
>> 8) & 0xff;
1070 SCpnt
->cmnd
[23] = (unsigned char) block
& 0xff;
1072 /* Transfer length */
1073 SCpnt
->cmnd
[28] = (unsigned char) (this_count
>> 24) & 0xff;
1074 SCpnt
->cmnd
[29] = (unsigned char) (this_count
>> 16) & 0xff;
1075 SCpnt
->cmnd
[30] = (unsigned char) (this_count
>> 8) & 0xff;
1076 SCpnt
->cmnd
[31] = (unsigned char) this_count
& 0xff;
1077 } else if (sdp
->use_16_for_rw
|| (this_count
> 0xffff)) {
1078 SCpnt
->cmnd
[0] += READ_16
- READ_6
;
1079 SCpnt
->cmnd
[1] = protect
| ((rq
->cmd_flags
& REQ_FUA
) ? 0x8 : 0);
1080 SCpnt
->cmnd
[2] = sizeof(block
) > 4 ? (unsigned char) (block
>> 56) & 0xff : 0;
1081 SCpnt
->cmnd
[3] = sizeof(block
) > 4 ? (unsigned char) (block
>> 48) & 0xff : 0;
1082 SCpnt
->cmnd
[4] = sizeof(block
) > 4 ? (unsigned char) (block
>> 40) & 0xff : 0;
1083 SCpnt
->cmnd
[5] = sizeof(block
) > 4 ? (unsigned char) (block
>> 32) & 0xff : 0;
1084 SCpnt
->cmnd
[6] = (unsigned char) (block
>> 24) & 0xff;
1085 SCpnt
->cmnd
[7] = (unsigned char) (block
>> 16) & 0xff;
1086 SCpnt
->cmnd
[8] = (unsigned char) (block
>> 8) & 0xff;
1087 SCpnt
->cmnd
[9] = (unsigned char) block
& 0xff;
1088 SCpnt
->cmnd
[10] = (unsigned char) (this_count
>> 24) & 0xff;
1089 SCpnt
->cmnd
[11] = (unsigned char) (this_count
>> 16) & 0xff;
1090 SCpnt
->cmnd
[12] = (unsigned char) (this_count
>> 8) & 0xff;
1091 SCpnt
->cmnd
[13] = (unsigned char) this_count
& 0xff;
1092 SCpnt
->cmnd
[14] = SCpnt
->cmnd
[15] = 0;
1093 } else if ((this_count
> 0xff) || (block
> 0x1fffff) ||
1094 scsi_device_protection(SCpnt
->device
) ||
1095 SCpnt
->device
->use_10_for_rw
) {
1096 SCpnt
->cmnd
[0] += READ_10
- READ_6
;
1097 SCpnt
->cmnd
[1] = protect
| ((rq
->cmd_flags
& REQ_FUA
) ? 0x8 : 0);
1098 SCpnt
->cmnd
[2] = (unsigned char) (block
>> 24) & 0xff;
1099 SCpnt
->cmnd
[3] = (unsigned char) (block
>> 16) & 0xff;
1100 SCpnt
->cmnd
[4] = (unsigned char) (block
>> 8) & 0xff;
1101 SCpnt
->cmnd
[5] = (unsigned char) block
& 0xff;
1102 SCpnt
->cmnd
[6] = SCpnt
->cmnd
[9] = 0;
1103 SCpnt
->cmnd
[7] = (unsigned char) (this_count
>> 8) & 0xff;
1104 SCpnt
->cmnd
[8] = (unsigned char) this_count
& 0xff;
1106 if (unlikely(rq
->cmd_flags
& REQ_FUA
)) {
1108 * This happens only if this drive failed
1109 * 10byte rw command with ILLEGAL_REQUEST
1110 * during operation and thus turned off
1113 scmd_printk(KERN_ERR
, SCpnt
,
1114 "FUA write on READ/WRITE(6) drive\n");
1118 SCpnt
->cmnd
[1] |= (unsigned char) ((block
>> 16) & 0x1f);
1119 SCpnt
->cmnd
[2] = (unsigned char) ((block
>> 8) & 0xff);
1120 SCpnt
->cmnd
[3] = (unsigned char) block
& 0xff;
1121 SCpnt
->cmnd
[4] = (unsigned char) this_count
;
1124 SCpnt
->sdb
.length
= this_count
* sdp
->sector_size
;
1127 * We shouldn't disconnect in the middle of a sector, so with a dumb
1128 * host adapter, it's safe to assume that we can at least transfer
1129 * this many bytes between each connect / disconnect.
1131 SCpnt
->transfersize
= sdp
->sector_size
;
1132 SCpnt
->underflow
= this_count
<< 9;
1133 SCpnt
->allowed
= SD_MAX_RETRIES
;
1136 * This indicates that the command is ready from our end to be
1144 static int sd_init_command(struct scsi_cmnd
*cmd
)
1146 struct request
*rq
= cmd
->request
;
1148 if (rq
->cmd_flags
& REQ_DISCARD
)
1149 return sd_setup_discard_cmnd(cmd
);
1150 else if (rq
->cmd_flags
& REQ_WRITE_SAME
)
1151 return sd_setup_write_same_cmnd(cmd
);
1152 else if (rq
->cmd_flags
& REQ_FLUSH
)
1153 return sd_setup_flush_cmnd(cmd
);
1155 return sd_setup_read_write_cmnd(cmd
);
1158 static void sd_uninit_command(struct scsi_cmnd
*SCpnt
)
1160 struct request
*rq
= SCpnt
->request
;
1162 if (rq
->cmd_flags
& REQ_DISCARD
)
1163 __free_page(rq
->completion_data
);
1165 if (SCpnt
->cmnd
!= rq
->cmd
) {
1166 mempool_free(SCpnt
->cmnd
, sd_cdb_pool
);
1173 * sd_open - open a scsi disk device
1174 * @inode: only i_rdev member may be used
1175 * @filp: only f_mode and f_flags may be used
1177 * Returns 0 if successful. Returns a negated errno value in case
1180 * Note: This can be called from a user context (e.g. fsck(1) )
1181 * or from within the kernel (e.g. as a result of a mount(1) ).
1182 * In the latter case @inode and @filp carry an abridged amount
1183 * of information as noted above.
1185 * Locking: called with bdev->bd_mutex held.
1187 static int sd_open(struct block_device
*bdev
, fmode_t mode
)
1189 struct scsi_disk
*sdkp
= scsi_disk_get(bdev
->bd_disk
);
1190 struct scsi_device
*sdev
;
1196 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO
, sdkp
, "sd_open\n"));
1198 sdev
= sdkp
->device
;
1201 * If the device is in error recovery, wait until it is done.
1202 * If the device is offline, then disallow any access to it.
1205 if (!scsi_block_when_processing_errors(sdev
))
1208 if (sdev
->removable
|| sdkp
->write_prot
)
1209 check_disk_change(bdev
);
1212 * If the drive is empty, just let the open fail.
1214 retval
= -ENOMEDIUM
;
1215 if (sdev
->removable
&& !sdkp
->media_present
&& !(mode
& FMODE_NDELAY
))
1219 * If the device has the write protect tab set, have the open fail
1220 * if the user expects to be able to write to the thing.
1223 if (sdkp
->write_prot
&& (mode
& FMODE_WRITE
))
1227 * It is possible that the disk changing stuff resulted in
1228 * the device being taken offline. If this is the case,
1229 * report this to the user, and don't pretend that the
1230 * open actually succeeded.
1233 if (!scsi_device_online(sdev
))
1236 if ((atomic_inc_return(&sdkp
->openers
) == 1) && sdev
->removable
) {
1237 if (scsi_block_when_processing_errors(sdev
))
1238 scsi_set_medium_removal(sdev
, SCSI_REMOVAL_PREVENT
);
1244 scsi_disk_put(sdkp
);
1249 * sd_release - invoked when the (last) close(2) is called on this
1251 * @inode: only i_rdev member may be used
1252 * @filp: only f_mode and f_flags may be used
1256 * Note: may block (uninterruptible) if error recovery is underway
1259 * Locking: called with bdev->bd_mutex held.
1261 static void sd_release(struct gendisk
*disk
, fmode_t mode
)
1263 struct scsi_disk
*sdkp
= scsi_disk(disk
);
1264 struct scsi_device
*sdev
= sdkp
->device
;
1266 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO
, sdkp
, "sd_release\n"));
1268 if (atomic_dec_return(&sdkp
->openers
) == 0 && sdev
->removable
) {
1269 if (scsi_block_when_processing_errors(sdev
))
1270 scsi_set_medium_removal(sdev
, SCSI_REMOVAL_ALLOW
);
1274 * XXX and what if there are packets in flight and this close()
1275 * XXX is followed by a "rmmod sd_mod"?
1278 scsi_disk_put(sdkp
);
1281 static int sd_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
1283 struct scsi_disk
*sdkp
= scsi_disk(bdev
->bd_disk
);
1284 struct scsi_device
*sdp
= sdkp
->device
;
1285 struct Scsi_Host
*host
= sdp
->host
;
1286 sector_t capacity
= logical_to_sectors(sdp
, sdkp
->capacity
);
1289 /* default to most commonly used values */
1290 diskinfo
[0] = 0x40; /* 1 << 6 */
1291 diskinfo
[1] = 0x20; /* 1 << 5 */
1292 diskinfo
[2] = capacity
>> 11;
1294 /* override with calculated, extended default, or driver values */
1295 if (host
->hostt
->bios_param
)
1296 host
->hostt
->bios_param(sdp
, bdev
, capacity
, diskinfo
);
1298 scsicam_bios_param(bdev
, capacity
, diskinfo
);
1300 geo
->heads
= diskinfo
[0];
1301 geo
->sectors
= diskinfo
[1];
1302 geo
->cylinders
= diskinfo
[2];
1307 * sd_ioctl - process an ioctl
1308 * @inode: only i_rdev/i_bdev members may be used
1309 * @filp: only f_mode and f_flags may be used
1310 * @cmd: ioctl command number
1311 * @arg: this is third argument given to ioctl(2) system call.
1312 * Often contains a pointer.
1314 * Returns 0 if successful (some ioctls return positive numbers on
1315 * success as well). Returns a negated errno value in case of error.
1317 * Note: most ioctls are forward onto the block subsystem or further
1318 * down in the scsi subsystem.
1320 static int sd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1321 unsigned int cmd
, unsigned long arg
)
1323 struct gendisk
*disk
= bdev
->bd_disk
;
1324 struct scsi_disk
*sdkp
= scsi_disk(disk
);
1325 struct scsi_device
*sdp
= sdkp
->device
;
1326 void __user
*p
= (void __user
*)arg
;
1329 SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO
, sdkp
, "sd_ioctl: disk=%s, "
1330 "cmd=0x%x\n", disk
->disk_name
, cmd
));
1332 error
= scsi_verify_blk_ioctl(bdev
, cmd
);
1337 * If we are in the middle of error recovery, don't let anyone
1338 * else try and use this device. Also, if error recovery fails, it
1339 * may try and take the device offline, in which case all further
1340 * access to the device is prohibited.
1342 error
= scsi_ioctl_block_when_processing_errors(sdp
, cmd
,
1343 (mode
& FMODE_NDELAY
) != 0);
1348 * Send SCSI addressing ioctls directly to mid level, send other
1349 * ioctls to block level and then onto mid level if they can't be
1353 case SCSI_IOCTL_GET_IDLUN
:
1354 case SCSI_IOCTL_GET_BUS_NUMBER
:
1355 error
= scsi_ioctl(sdp
, cmd
, p
);
1358 error
= scsi_cmd_blk_ioctl(bdev
, mode
, cmd
, p
);
1359 if (error
!= -ENOTTY
)
1361 error
= scsi_ioctl(sdp
, cmd
, p
);
1368 static void set_media_not_present(struct scsi_disk
*sdkp
)
1370 if (sdkp
->media_present
)
1371 sdkp
->device
->changed
= 1;
1373 if (sdkp
->device
->removable
) {
1374 sdkp
->media_present
= 0;
1379 static int media_not_present(struct scsi_disk
*sdkp
,
1380 struct scsi_sense_hdr
*sshdr
)
1382 if (!scsi_sense_valid(sshdr
))
1385 /* not invoked for commands that could return deferred errors */
1386 switch (sshdr
->sense_key
) {
1387 case UNIT_ATTENTION
:
1389 /* medium not present */
1390 if (sshdr
->asc
== 0x3A) {
1391 set_media_not_present(sdkp
);
1399 * sd_check_events - check media events
1400 * @disk: kernel device descriptor
1401 * @clearing: disk events currently being cleared
1403 * Returns mask of DISK_EVENT_*.
1405 * Note: this function is invoked from the block subsystem.
1407 static unsigned int sd_check_events(struct gendisk
*disk
, unsigned int clearing
)
1409 struct scsi_disk
*sdkp
= scsi_disk_get(disk
);
1410 struct scsi_device
*sdp
;
1411 struct scsi_sense_hdr
*sshdr
= NULL
;
1418 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO
, sdkp
, "sd_check_events\n"));
1421 * If the device is offline, don't send any commands - just pretend as
1422 * if the command failed. If the device ever comes back online, we
1423 * can deal with it then. It is only because of unrecoverable errors
1424 * that we would ever take a device offline in the first place.
1426 if (!scsi_device_online(sdp
)) {
1427 set_media_not_present(sdkp
);
1432 * Using TEST_UNIT_READY enables differentiation between drive with
1433 * no cartridge loaded - NOT READY, drive with changed cartridge -
1434 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
1436 * Drives that auto spin down. eg iomega jaz 1G, will be started
1437 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
1438 * sd_revalidate() is called.
1442 if (scsi_block_when_processing_errors(sdp
)) {
1443 sshdr
= kzalloc(sizeof(*sshdr
), GFP_KERNEL
);
1444 retval
= scsi_test_unit_ready(sdp
, SD_TIMEOUT
, SD_MAX_RETRIES
,
1448 /* failed to execute TUR, assume media not present */
1449 if (host_byte(retval
)) {
1450 set_media_not_present(sdkp
);
1454 if (media_not_present(sdkp
, sshdr
))
1458 * For removable scsi disk we have to recognise the presence
1459 * of a disk in the drive.
1461 if (!sdkp
->media_present
)
1463 sdkp
->media_present
= 1;
1466 * sdp->changed is set under the following conditions:
1468 * Medium present state has changed in either direction.
1469 * Device has indicated UNIT_ATTENTION.
1472 retval
= sdp
->changed
? DISK_EVENT_MEDIA_CHANGE
: 0;
1474 scsi_disk_put(sdkp
);
1478 static int sd_sync_cache(struct scsi_disk
*sdkp
)
1481 struct scsi_device
*sdp
= sdkp
->device
;
1482 const int timeout
= sdp
->request_queue
->rq_timeout
1483 * SD_FLUSH_TIMEOUT_MULTIPLIER
;
1484 struct scsi_sense_hdr sshdr
;
1486 if (!scsi_device_online(sdp
))
1489 for (retries
= 3; retries
> 0; --retries
) {
1490 unsigned char cmd
[10] = { 0 };
1492 cmd
[0] = SYNCHRONIZE_CACHE
;
1494 * Leave the rest of the command zero to indicate
1497 res
= scsi_execute_req_flags(sdp
, cmd
, DMA_NONE
, NULL
, 0,
1498 &sshdr
, timeout
, SD_MAX_RETRIES
,
1505 sd_print_result(sdkp
, "Synchronize Cache(10) failed", res
);
1507 if (driver_byte(res
) & DRIVER_SENSE
)
1508 sd_print_sense_hdr(sdkp
, &sshdr
);
1509 /* we need to evaluate the error return */
1510 if (scsi_sense_valid(&sshdr
) &&
1511 (sshdr
.asc
== 0x3a || /* medium not present */
1512 sshdr
.asc
== 0x20)) /* invalid command */
1513 /* this is no error here */
1516 switch (host_byte(res
)) {
1517 /* ignore errors due to racing a disconnection */
1518 case DID_BAD_TARGET
:
1519 case DID_NO_CONNECT
:
1521 /* signal the upper layer it might try again */
1525 case DID_SOFT_ERROR
:
1534 static void sd_rescan(struct device
*dev
)
1536 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
1538 revalidate_disk(sdkp
->disk
);
1542 #ifdef CONFIG_COMPAT
1544 * This gets directly called from VFS. When the ioctl
1545 * is not recognized we go back to the other translation paths.
1547 static int sd_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
1548 unsigned int cmd
, unsigned long arg
)
1550 struct scsi_device
*sdev
= scsi_disk(bdev
->bd_disk
)->device
;
1553 error
= scsi_ioctl_block_when_processing_errors(sdev
, cmd
,
1554 (mode
& FMODE_NDELAY
) != 0);
1559 * Let the static ioctl translation table take care of it.
1561 if (!sdev
->host
->hostt
->compat_ioctl
)
1562 return -ENOIOCTLCMD
;
1563 return sdev
->host
->hostt
->compat_ioctl(sdev
, cmd
, (void __user
*)arg
);
1567 static char sd_pr_type(enum pr_type type
)
1570 case PR_WRITE_EXCLUSIVE
:
1572 case PR_EXCLUSIVE_ACCESS
:
1574 case PR_WRITE_EXCLUSIVE_REG_ONLY
:
1576 case PR_EXCLUSIVE_ACCESS_REG_ONLY
:
1578 case PR_WRITE_EXCLUSIVE_ALL_REGS
:
1580 case PR_EXCLUSIVE_ACCESS_ALL_REGS
:
1587 static int sd_pr_command(struct block_device
*bdev
, u8 sa
,
1588 u64 key
, u64 sa_key
, u8 type
, u8 flags
)
1590 struct scsi_device
*sdev
= scsi_disk(bdev
->bd_disk
)->device
;
1591 struct scsi_sense_hdr sshdr
;
1593 u8 cmd
[16] = { 0, };
1594 u8 data
[24] = { 0, };
1596 cmd
[0] = PERSISTENT_RESERVE_OUT
;
1599 put_unaligned_be32(sizeof(data
), &cmd
[5]);
1601 put_unaligned_be64(key
, &data
[0]);
1602 put_unaligned_be64(sa_key
, &data
[8]);
1605 result
= scsi_execute_req(sdev
, cmd
, DMA_TO_DEVICE
, &data
, sizeof(data
),
1606 &sshdr
, SD_TIMEOUT
, SD_MAX_RETRIES
, NULL
);
1608 if ((driver_byte(result
) & DRIVER_SENSE
) &&
1609 (scsi_sense_valid(&sshdr
))) {
1610 sdev_printk(KERN_INFO
, sdev
, "PR command failed: %d\n", result
);
1611 scsi_print_sense_hdr(sdev
, NULL
, &sshdr
);
1617 static int sd_pr_register(struct block_device
*bdev
, u64 old_key
, u64 new_key
,
1620 if (flags
& ~PR_FL_IGNORE_KEY
)
1622 return sd_pr_command(bdev
, (flags
& PR_FL_IGNORE_KEY
) ? 0x06 : 0x00,
1623 old_key
, new_key
, 0,
1624 (1 << 0) /* APTPL */ |
1625 (1 << 2) /* ALL_TG_PT */);
1628 static int sd_pr_reserve(struct block_device
*bdev
, u64 key
, enum pr_type type
,
1633 return sd_pr_command(bdev
, 0x01, key
, 0, sd_pr_type(type
), 0);
1636 static int sd_pr_release(struct block_device
*bdev
, u64 key
, enum pr_type type
)
1638 return sd_pr_command(bdev
, 0x02, key
, 0, sd_pr_type(type
), 0);
1641 static int sd_pr_preempt(struct block_device
*bdev
, u64 old_key
, u64 new_key
,
1642 enum pr_type type
, bool abort
)
1644 return sd_pr_command(bdev
, abort
? 0x05 : 0x04, old_key
, new_key
,
1645 sd_pr_type(type
), 0);
1648 static int sd_pr_clear(struct block_device
*bdev
, u64 key
)
1650 return sd_pr_command(bdev
, 0x03, key
, 0, 0, 0);
1653 static const struct pr_ops sd_pr_ops
= {
1654 .pr_register
= sd_pr_register
,
1655 .pr_reserve
= sd_pr_reserve
,
1656 .pr_release
= sd_pr_release
,
1657 .pr_preempt
= sd_pr_preempt
,
1658 .pr_clear
= sd_pr_clear
,
1661 static const struct block_device_operations sd_fops
= {
1662 .owner
= THIS_MODULE
,
1664 .release
= sd_release
,
1666 .getgeo
= sd_getgeo
,
1667 #ifdef CONFIG_COMPAT
1668 .compat_ioctl
= sd_compat_ioctl
,
1670 .check_events
= sd_check_events
,
1671 .revalidate_disk
= sd_revalidate_disk
,
1672 .unlock_native_capacity
= sd_unlock_native_capacity
,
1673 .pr_ops
= &sd_pr_ops
,
1677 * sd_eh_action - error handling callback
1678 * @scmd: sd-issued command that has failed
1679 * @eh_disp: The recovery disposition suggested by the midlayer
1681 * This function is called by the SCSI midlayer upon completion of an
1682 * error test command (currently TEST UNIT READY). The result of sending
1683 * the eh command is passed in eh_disp. We're looking for devices that
1684 * fail medium access commands but are OK with non access commands like
1685 * test unit ready (so wrongly see the device as having a successful
1688 static int sd_eh_action(struct scsi_cmnd
*scmd
, int eh_disp
)
1690 struct scsi_disk
*sdkp
= scsi_disk(scmd
->request
->rq_disk
);
1692 if (!scsi_device_online(scmd
->device
) ||
1693 !scsi_medium_access_command(scmd
) ||
1694 host_byte(scmd
->result
) != DID_TIME_OUT
||
1699 * The device has timed out executing a medium access command.
1700 * However, the TEST UNIT READY command sent during error
1701 * handling completed successfully. Either the device is in the
1702 * process of recovering or has it suffered an internal failure
1703 * that prevents access to the storage medium.
1705 sdkp
->medium_access_timed_out
++;
1708 * If the device keeps failing read/write commands but TEST UNIT
1709 * READY always completes successfully we assume that medium
1710 * access is no longer possible and take the device offline.
1712 if (sdkp
->medium_access_timed_out
>= sdkp
->max_medium_access_timeouts
) {
1713 scmd_printk(KERN_ERR
, scmd
,
1714 "Medium access timeout failure. Offlining disk!\n");
1715 scsi_device_set_state(scmd
->device
, SDEV_OFFLINE
);
1723 static unsigned int sd_completed_bytes(struct scsi_cmnd
*scmd
)
1725 u64 start_lba
= blk_rq_pos(scmd
->request
);
1726 u64 end_lba
= blk_rq_pos(scmd
->request
) + (scsi_bufflen(scmd
) / 512);
1727 u64 factor
= scmd
->device
->sector_size
/ 512;
1731 * resid is optional but mostly filled in. When it's unused,
1732 * its value is zero, so we assume the whole buffer transferred
1734 unsigned int transferred
= scsi_bufflen(scmd
) - scsi_get_resid(scmd
);
1735 unsigned int good_bytes
;
1737 if (scmd
->request
->cmd_type
!= REQ_TYPE_FS
)
1740 info_valid
= scsi_get_sense_info_fld(scmd
->sense_buffer
,
1741 SCSI_SENSE_BUFFERSIZE
,
1746 if (scsi_bufflen(scmd
) <= scmd
->device
->sector_size
)
1749 /* be careful ... don't want any overflows */
1750 do_div(start_lba
, factor
);
1751 do_div(end_lba
, factor
);
1753 /* The bad lba was reported incorrectly, we have no idea where
1756 if (bad_lba
< start_lba
|| bad_lba
>= end_lba
)
1759 /* This computation should always be done in terms of
1760 * the resolution of the device's medium.
1762 good_bytes
= (bad_lba
- start_lba
) * scmd
->device
->sector_size
;
1763 return min(good_bytes
, transferred
);
1767 * sd_done - bottom half handler: called when the lower level
1768 * driver has completed (successfully or otherwise) a scsi command.
1769 * @SCpnt: mid-level's per command structure.
1771 * Note: potentially run from within an ISR. Must not block.
1773 static int sd_done(struct scsi_cmnd
*SCpnt
)
1775 int result
= SCpnt
->result
;
1776 unsigned int good_bytes
= result
? 0 : scsi_bufflen(SCpnt
);
1777 struct scsi_sense_hdr sshdr
;
1778 struct scsi_disk
*sdkp
= scsi_disk(SCpnt
->request
->rq_disk
);
1779 struct request
*req
= SCpnt
->request
;
1780 int sense_valid
= 0;
1781 int sense_deferred
= 0;
1782 unsigned char op
= SCpnt
->cmnd
[0];
1783 unsigned char unmap
= SCpnt
->cmnd
[1] & 8;
1785 if (req
->cmd_flags
& REQ_DISCARD
|| req
->cmd_flags
& REQ_WRITE_SAME
) {
1787 good_bytes
= blk_rq_bytes(req
);
1788 scsi_set_resid(SCpnt
, 0);
1791 scsi_set_resid(SCpnt
, blk_rq_bytes(req
));
1796 sense_valid
= scsi_command_normalize_sense(SCpnt
, &sshdr
);
1798 sense_deferred
= scsi_sense_is_deferred(&sshdr
);
1800 sdkp
->medium_access_timed_out
= 0;
1802 if (driver_byte(result
) != DRIVER_SENSE
&&
1803 (!sense_valid
|| sense_deferred
))
1806 switch (sshdr
.sense_key
) {
1807 case HARDWARE_ERROR
:
1809 good_bytes
= sd_completed_bytes(SCpnt
);
1811 case RECOVERED_ERROR
:
1812 good_bytes
= scsi_bufflen(SCpnt
);
1815 /* This indicates a false check condition, so ignore it. An
1816 * unknown amount of data was transferred so treat it as an
1820 memset(SCpnt
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
1822 case ABORTED_COMMAND
:
1823 if (sshdr
.asc
== 0x10) /* DIF: Target detected corruption */
1824 good_bytes
= sd_completed_bytes(SCpnt
);
1826 case ILLEGAL_REQUEST
:
1827 if (sshdr
.asc
== 0x10) /* DIX: Host detected corruption */
1828 good_bytes
= sd_completed_bytes(SCpnt
);
1829 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
1830 if (sshdr
.asc
== 0x20 || sshdr
.asc
== 0x24) {
1833 sd_config_discard(sdkp
, SD_LBP_DISABLE
);
1838 sd_config_discard(sdkp
, SD_LBP_DISABLE
);
1840 sdkp
->device
->no_write_same
= 1;
1841 sd_config_write_same(sdkp
);
1844 req
->__data_len
= blk_rq_bytes(req
);
1845 req
->cmd_flags
|= REQ_QUIET
;
1854 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO
, SCpnt
,
1855 "sd_done: completed %d of %d bytes\n",
1856 good_bytes
, scsi_bufflen(SCpnt
)));
1858 if (rq_data_dir(SCpnt
->request
) == READ
&& scsi_prot_sg_count(SCpnt
))
1859 sd_dif_complete(SCpnt
, good_bytes
);
1865 * spinup disk - called only in sd_revalidate_disk()
1868 sd_spinup_disk(struct scsi_disk
*sdkp
)
1870 unsigned char cmd
[10];
1871 unsigned long spintime_expire
= 0;
1872 int retries
, spintime
;
1873 unsigned int the_result
;
1874 struct scsi_sense_hdr sshdr
;
1875 int sense_valid
= 0;
1879 /* Spin up drives, as required. Only do this at boot time */
1880 /* Spinup needs to be done for module loads too. */
1885 cmd
[0] = TEST_UNIT_READY
;
1886 memset((void *) &cmd
[1], 0, 9);
1888 the_result
= scsi_execute_req(sdkp
->device
, cmd
,
1891 SD_MAX_RETRIES
, NULL
);
1894 * If the drive has indicated to us that it
1895 * doesn't have any media in it, don't bother
1896 * with any more polling.
1898 if (media_not_present(sdkp
, &sshdr
))
1902 sense_valid
= scsi_sense_valid(&sshdr
);
1904 } while (retries
< 3 &&
1905 (!scsi_status_is_good(the_result
) ||
1906 ((driver_byte(the_result
) & DRIVER_SENSE
) &&
1907 sense_valid
&& sshdr
.sense_key
== UNIT_ATTENTION
)));
1909 if ((driver_byte(the_result
) & DRIVER_SENSE
) == 0) {
1910 /* no sense, TUR either succeeded or failed
1911 * with a status error */
1912 if(!spintime
&& !scsi_status_is_good(the_result
)) {
1913 sd_print_result(sdkp
, "Test Unit Ready failed",
1920 * The device does not want the automatic start to be issued.
1922 if (sdkp
->device
->no_start_on_add
)
1925 if (sense_valid
&& sshdr
.sense_key
== NOT_READY
) {
1926 if (sshdr
.asc
== 4 && sshdr
.ascq
== 3)
1927 break; /* manual intervention required */
1928 if (sshdr
.asc
== 4 && sshdr
.ascq
== 0xb)
1929 break; /* standby */
1930 if (sshdr
.asc
== 4 && sshdr
.ascq
== 0xc)
1931 break; /* unavailable */
1932 if (sshdr
.asc
== 4 && sshdr
.ascq
== 0x1b)
1933 break; /* sanitize in progress */
1935 * Issue command to spin up drive when not ready
1938 sd_printk(KERN_NOTICE
, sdkp
, "Spinning up disk...");
1939 cmd
[0] = START_STOP
;
1940 cmd
[1] = 1; /* Return immediately */
1941 memset((void *) &cmd
[2], 0, 8);
1942 cmd
[4] = 1; /* Start spin cycle */
1943 if (sdkp
->device
->start_stop_pwr_cond
)
1945 scsi_execute_req(sdkp
->device
, cmd
, DMA_NONE
,
1947 SD_TIMEOUT
, SD_MAX_RETRIES
,
1949 spintime_expire
= jiffies
+ 100 * HZ
;
1952 /* Wait 1 second for next try */
1957 * Wait for USB flash devices with slow firmware.
1958 * Yes, this sense key/ASC combination shouldn't
1959 * occur here. It's characteristic of these devices.
1961 } else if (sense_valid
&&
1962 sshdr
.sense_key
== UNIT_ATTENTION
&&
1963 sshdr
.asc
== 0x28) {
1965 spintime_expire
= jiffies
+ 5 * HZ
;
1968 /* Wait 1 second for next try */
1971 /* we don't understand the sense code, so it's
1972 * probably pointless to loop */
1974 sd_printk(KERN_NOTICE
, sdkp
, "Unit Not Ready\n");
1975 sd_print_sense_hdr(sdkp
, &sshdr
);
1980 } while (spintime
&& time_before_eq(jiffies
, spintime_expire
));
1983 if (scsi_status_is_good(the_result
))
1986 printk("not responding...\n");
1992 * Determine whether disk supports Data Integrity Field.
1994 static int sd_read_protection_type(struct scsi_disk
*sdkp
, unsigned char *buffer
)
1996 struct scsi_device
*sdp
= sdkp
->device
;
2000 if (scsi_device_protection(sdp
) == 0 || (buffer
[12] & 1) == 0)
2003 type
= ((buffer
[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
2005 if (type
> SD_DIF_TYPE3_PROTECTION
)
2007 else if (scsi_host_dif_capable(sdp
->host
, type
))
2010 if (sdkp
->first_scan
|| type
!= sdkp
->protection_type
)
2013 sd_printk(KERN_ERR
, sdkp
, "formatted with unsupported" \
2014 " protection type %u. Disabling disk!\n",
2018 sd_printk(KERN_NOTICE
, sdkp
,
2019 "Enabling DIF Type %u protection\n", type
);
2022 sd_printk(KERN_NOTICE
, sdkp
,
2023 "Disabling DIF Type %u protection\n", type
);
2027 sdkp
->protection_type
= type
;
2032 static void read_capacity_error(struct scsi_disk
*sdkp
, struct scsi_device
*sdp
,
2033 struct scsi_sense_hdr
*sshdr
, int sense_valid
,
2036 if (driver_byte(the_result
) & DRIVER_SENSE
)
2037 sd_print_sense_hdr(sdkp
, sshdr
);
2039 sd_printk(KERN_NOTICE
, sdkp
, "Sense not available.\n");
2042 * Set dirty bit for removable devices if not ready -
2043 * sometimes drives will not report this properly.
2045 if (sdp
->removable
&&
2046 sense_valid
&& sshdr
->sense_key
== NOT_READY
)
2047 set_media_not_present(sdkp
);
2050 * We used to set media_present to 0 here to indicate no media
2051 * in the drive, but some drives fail read capacity even with
2052 * media present, so we can't do that.
2054 sdkp
->capacity
= 0; /* unknown mapped to zero - as usual */
2058 #if RC16_LEN > SD_BUF_SIZE
2059 #error RC16_LEN must not be more than SD_BUF_SIZE
2062 #define READ_CAPACITY_RETRIES_ON_RESET 10
2065 * Ensure that we don't overflow sector_t when CONFIG_LBDAF is not set
2066 * and the reported logical block size is bigger than 512 bytes. Note
2067 * that last_sector is a u64 and therefore logical_to_sectors() is not
2070 static bool sd_addressable_capacity(u64 lba
, unsigned int sector_size
)
2072 u64 last_sector
= (lba
+ 1ULL) << (ilog2(sector_size
) - 9);
2074 if (sizeof(sector_t
) == 4 && last_sector
> U32_MAX
)
2080 static int read_capacity_16(struct scsi_disk
*sdkp
, struct scsi_device
*sdp
,
2081 unsigned char *buffer
)
2083 unsigned char cmd
[16];
2084 struct scsi_sense_hdr sshdr
;
2085 int sense_valid
= 0;
2087 int retries
= 3, reset_retries
= READ_CAPACITY_RETRIES_ON_RESET
;
2088 unsigned int alignment
;
2089 unsigned long long lba
;
2090 unsigned sector_size
;
2092 if (sdp
->no_read_capacity_16
)
2097 cmd
[0] = SERVICE_ACTION_IN_16
;
2098 cmd
[1] = SAI_READ_CAPACITY_16
;
2100 memset(buffer
, 0, RC16_LEN
);
2102 the_result
= scsi_execute_req(sdp
, cmd
, DMA_FROM_DEVICE
,
2103 buffer
, RC16_LEN
, &sshdr
,
2104 SD_TIMEOUT
, SD_MAX_RETRIES
, NULL
);
2106 if (media_not_present(sdkp
, &sshdr
))
2110 sense_valid
= scsi_sense_valid(&sshdr
);
2112 sshdr
.sense_key
== ILLEGAL_REQUEST
&&
2113 (sshdr
.asc
== 0x20 || sshdr
.asc
== 0x24) &&
2115 /* Invalid Command Operation Code or
2116 * Invalid Field in CDB, just retry
2117 * silently with RC10 */
2120 sshdr
.sense_key
== UNIT_ATTENTION
&&
2121 sshdr
.asc
== 0x29 && sshdr
.ascq
== 0x00)
2122 /* Device reset might occur several times,
2123 * give it one more chance */
2124 if (--reset_retries
> 0)
2129 } while (the_result
&& retries
);
2132 sd_print_result(sdkp
, "Read Capacity(16) failed", the_result
);
2133 read_capacity_error(sdkp
, sdp
, &sshdr
, sense_valid
, the_result
);
2137 sector_size
= get_unaligned_be32(&buffer
[8]);
2138 lba
= get_unaligned_be64(&buffer
[0]);
2140 if (sd_read_protection_type(sdkp
, buffer
) < 0) {
2145 if (!sd_addressable_capacity(lba
, sector_size
)) {
2146 sd_printk(KERN_ERR
, sdkp
, "Too big for this kernel. Use a "
2147 "kernel compiled with support for large block "
2153 /* Logical blocks per physical block exponent */
2154 sdkp
->physical_block_size
= (1 << (buffer
[13] & 0xf)) * sector_size
;
2156 /* Lowest aligned logical block */
2157 alignment
= ((buffer
[14] & 0x3f) << 8 | buffer
[15]) * sector_size
;
2158 blk_queue_alignment_offset(sdp
->request_queue
, alignment
);
2159 if (alignment
&& sdkp
->first_scan
)
2160 sd_printk(KERN_NOTICE
, sdkp
,
2161 "physical block alignment offset: %u\n", alignment
);
2163 if (buffer
[14] & 0x80) { /* LBPME */
2166 if (buffer
[14] & 0x40) /* LBPRZ */
2169 sd_config_discard(sdkp
, SD_LBP_WS16
);
2172 sdkp
->capacity
= lba
+ 1;
2176 static int read_capacity_10(struct scsi_disk
*sdkp
, struct scsi_device
*sdp
,
2177 unsigned char *buffer
)
2179 unsigned char cmd
[16];
2180 struct scsi_sense_hdr sshdr
;
2181 int sense_valid
= 0;
2183 int retries
= 3, reset_retries
= READ_CAPACITY_RETRIES_ON_RESET
;
2185 unsigned sector_size
;
2188 cmd
[0] = READ_CAPACITY
;
2189 memset(&cmd
[1], 0, 9);
2190 memset(buffer
, 0, 8);
2192 the_result
= scsi_execute_req(sdp
, cmd
, DMA_FROM_DEVICE
,
2194 SD_TIMEOUT
, SD_MAX_RETRIES
, NULL
);
2196 if (media_not_present(sdkp
, &sshdr
))
2200 sense_valid
= scsi_sense_valid(&sshdr
);
2202 sshdr
.sense_key
== UNIT_ATTENTION
&&
2203 sshdr
.asc
== 0x29 && sshdr
.ascq
== 0x00)
2204 /* Device reset might occur several times,
2205 * give it one more chance */
2206 if (--reset_retries
> 0)
2211 } while (the_result
&& retries
);
2214 sd_print_result(sdkp
, "Read Capacity(10) failed", the_result
);
2215 read_capacity_error(sdkp
, sdp
, &sshdr
, sense_valid
, the_result
);
2219 sector_size
= get_unaligned_be32(&buffer
[4]);
2220 lba
= get_unaligned_be32(&buffer
[0]);
2222 if (sdp
->no_read_capacity_16
&& (lba
== 0xffffffff)) {
2223 /* Some buggy (usb cardreader) devices return an lba of
2224 0xffffffff when the want to report a size of 0 (with
2225 which they really mean no media is present) */
2227 sdkp
->physical_block_size
= sector_size
;
2231 if (!sd_addressable_capacity(lba
, sector_size
)) {
2232 sd_printk(KERN_ERR
, sdkp
, "Too big for this kernel. Use a "
2233 "kernel compiled with support for large block "
2239 sdkp
->capacity
= lba
+ 1;
2240 sdkp
->physical_block_size
= sector_size
;
2244 static int sd_try_rc16_first(struct scsi_device
*sdp
)
2246 if (sdp
->host
->max_cmd_len
< 16)
2248 if (sdp
->try_rc_10_first
)
2250 if (sdp
->scsi_level
> SCSI_SPC_2
)
2252 if (scsi_device_protection(sdp
))
2258 * read disk capacity
2261 sd_read_capacity(struct scsi_disk
*sdkp
, unsigned char *buffer
)
2264 struct scsi_device
*sdp
= sdkp
->device
;
2265 sector_t old_capacity
= sdkp
->capacity
;
2267 if (sd_try_rc16_first(sdp
)) {
2268 sector_size
= read_capacity_16(sdkp
, sdp
, buffer
);
2269 if (sector_size
== -EOVERFLOW
)
2271 if (sector_size
== -ENODEV
)
2273 if (sector_size
< 0)
2274 sector_size
= read_capacity_10(sdkp
, sdp
, buffer
);
2275 if (sector_size
< 0)
2278 sector_size
= read_capacity_10(sdkp
, sdp
, buffer
);
2279 if (sector_size
== -EOVERFLOW
)
2281 if (sector_size
< 0)
2283 if ((sizeof(sdkp
->capacity
) > 4) &&
2284 (sdkp
->capacity
> 0xffffffffULL
)) {
2285 int old_sector_size
= sector_size
;
2286 sd_printk(KERN_NOTICE
, sdkp
, "Very big device. "
2287 "Trying to use READ CAPACITY(16).\n");
2288 sector_size
= read_capacity_16(sdkp
, sdp
, buffer
);
2289 if (sector_size
< 0) {
2290 sd_printk(KERN_NOTICE
, sdkp
,
2291 "Using 0xffffffff as device size\n");
2292 sdkp
->capacity
= 1 + (sector_t
) 0xffffffff;
2293 sector_size
= old_sector_size
;
2299 /* Some devices are known to return the total number of blocks,
2300 * not the highest block number. Some devices have versions
2301 * which do this and others which do not. Some devices we might
2302 * suspect of doing this but we don't know for certain.
2304 * If we know the reported capacity is wrong, decrement it. If
2305 * we can only guess, then assume the number of blocks is even
2306 * (usually true but not always) and err on the side of lowering
2309 if (sdp
->fix_capacity
||
2310 (sdp
->guess_capacity
&& (sdkp
->capacity
& 0x01))) {
2311 sd_printk(KERN_INFO
, sdkp
, "Adjusting the sector count "
2312 "from its reported value: %llu\n",
2313 (unsigned long long) sdkp
->capacity
);
2318 if (sector_size
== 0) {
2320 sd_printk(KERN_NOTICE
, sdkp
, "Sector size 0 reported, "
2324 if (sector_size
!= 512 &&
2325 sector_size
!= 1024 &&
2326 sector_size
!= 2048 &&
2327 sector_size
!= 4096) {
2328 sd_printk(KERN_NOTICE
, sdkp
, "Unsupported sector size %d.\n",
2331 * The user might want to re-format the drive with
2332 * a supported sectorsize. Once this happens, it
2333 * would be relatively trivial to set the thing up.
2334 * For this reason, we leave the thing in the table.
2338 * set a bogus sector size so the normal read/write
2339 * logic in the block layer will eventually refuse any
2340 * request on this device without tripping over power
2341 * of two sector size assumptions
2345 blk_queue_logical_block_size(sdp
->request_queue
, sector_size
);
2348 char cap_str_2
[10], cap_str_10
[10];
2350 string_get_size(sdkp
->capacity
, sector_size
,
2351 STRING_UNITS_2
, cap_str_2
, sizeof(cap_str_2
));
2352 string_get_size(sdkp
->capacity
, sector_size
,
2353 STRING_UNITS_10
, cap_str_10
,
2354 sizeof(cap_str_10
));
2356 if (sdkp
->first_scan
|| old_capacity
!= sdkp
->capacity
) {
2357 sd_printk(KERN_NOTICE
, sdkp
,
2358 "%llu %d-byte logical blocks: (%s/%s)\n",
2359 (unsigned long long)sdkp
->capacity
,
2360 sector_size
, cap_str_10
, cap_str_2
);
2362 if (sdkp
->physical_block_size
!= sector_size
)
2363 sd_printk(KERN_NOTICE
, sdkp
,
2364 "%u-byte physical blocks\n",
2365 sdkp
->physical_block_size
);
2369 if (sdkp
->capacity
> 0xffffffff)
2370 sdp
->use_16_for_rw
= 1;
2372 blk_queue_physical_block_size(sdp
->request_queue
,
2373 sdkp
->physical_block_size
);
2374 sdkp
->device
->sector_size
= sector_size
;
2377 /* called with buffer of length 512 */
2379 sd_do_mode_sense(struct scsi_device
*sdp
, int dbd
, int modepage
,
2380 unsigned char *buffer
, int len
, struct scsi_mode_data
*data
,
2381 struct scsi_sense_hdr
*sshdr
)
2383 return scsi_mode_sense(sdp
, dbd
, modepage
, buffer
, len
,
2384 SD_TIMEOUT
, SD_MAX_RETRIES
, data
,
2389 * read write protect setting, if possible - called only in sd_revalidate_disk()
2390 * called with buffer of length SD_BUF_SIZE
2393 sd_read_write_protect_flag(struct scsi_disk
*sdkp
, unsigned char *buffer
)
2396 struct scsi_device
*sdp
= sdkp
->device
;
2397 struct scsi_mode_data data
;
2398 int disk_ro
= get_disk_ro(sdkp
->disk
);
2399 int old_wp
= sdkp
->write_prot
;
2401 set_disk_ro(sdkp
->disk
, 0);
2402 if (sdp
->skip_ms_page_3f
) {
2403 sd_first_printk(KERN_NOTICE
, sdkp
, "Assuming Write Enabled\n");
2407 if (sdp
->use_192_bytes_for_3f
) {
2408 res
= sd_do_mode_sense(sdp
, 0, 0x3F, buffer
, 192, &data
, NULL
);
2411 * First attempt: ask for all pages (0x3F), but only 4 bytes.
2412 * We have to start carefully: some devices hang if we ask
2413 * for more than is available.
2415 res
= sd_do_mode_sense(sdp
, 0, 0x3F, buffer
, 4, &data
, NULL
);
2418 * Second attempt: ask for page 0 When only page 0 is
2419 * implemented, a request for page 3F may return Sense Key
2420 * 5: Illegal Request, Sense Code 24: Invalid field in
2423 if (!scsi_status_is_good(res
))
2424 res
= sd_do_mode_sense(sdp
, 0, 0, buffer
, 4, &data
, NULL
);
2427 * Third attempt: ask 255 bytes, as we did earlier.
2429 if (!scsi_status_is_good(res
))
2430 res
= sd_do_mode_sense(sdp
, 0, 0x3F, buffer
, 255,
2434 if (!scsi_status_is_good(res
)) {
2435 sd_first_printk(KERN_WARNING
, sdkp
,
2436 "Test WP failed, assume Write Enabled\n");
2438 sdkp
->write_prot
= ((data
.device_specific
& 0x80) != 0);
2439 set_disk_ro(sdkp
->disk
, sdkp
->write_prot
|| disk_ro
);
2440 if (sdkp
->first_scan
|| old_wp
!= sdkp
->write_prot
) {
2441 sd_printk(KERN_NOTICE
, sdkp
, "Write Protect is %s\n",
2442 sdkp
->write_prot
? "on" : "off");
2443 sd_printk(KERN_DEBUG
, sdkp
,
2444 "Mode Sense: %02x %02x %02x %02x\n",
2445 buffer
[0], buffer
[1], buffer
[2], buffer
[3]);
2451 * sd_read_cache_type - called only from sd_revalidate_disk()
2452 * called with buffer of length SD_BUF_SIZE
2455 sd_read_cache_type(struct scsi_disk
*sdkp
, unsigned char *buffer
)
2458 struct scsi_device
*sdp
= sdkp
->device
;
2463 struct scsi_mode_data data
;
2464 struct scsi_sense_hdr sshdr
;
2465 int old_wce
= sdkp
->WCE
;
2466 int old_rcd
= sdkp
->RCD
;
2467 int old_dpofua
= sdkp
->DPOFUA
;
2470 if (sdkp
->cache_override
)
2474 if (sdp
->skip_ms_page_8
) {
2475 if (sdp
->type
== TYPE_RBC
)
2478 if (sdp
->skip_ms_page_3f
)
2481 if (sdp
->use_192_bytes_for_3f
)
2485 } else if (sdp
->type
== TYPE_RBC
) {
2493 /* cautiously ask */
2494 res
= sd_do_mode_sense(sdp
, dbd
, modepage
, buffer
, first_len
,
2497 if (!scsi_status_is_good(res
))
2500 if (!data
.header_length
) {
2503 sd_first_printk(KERN_ERR
, sdkp
,
2504 "Missing header in MODE_SENSE response\n");
2507 /* that went OK, now ask for the proper length */
2511 * We're only interested in the first three bytes, actually.
2512 * But the data cache page is defined for the first 20.
2516 else if (len
> SD_BUF_SIZE
) {
2517 sd_first_printk(KERN_NOTICE
, sdkp
, "Truncating mode parameter "
2518 "data from %d to %d bytes\n", len
, SD_BUF_SIZE
);
2521 if (modepage
== 0x3F && sdp
->use_192_bytes_for_3f
)
2525 if (len
> first_len
)
2526 res
= sd_do_mode_sense(sdp
, dbd
, modepage
, buffer
, len
,
2529 if (scsi_status_is_good(res
)) {
2530 int offset
= data
.header_length
+ data
.block_descriptor_length
;
2532 while (offset
< len
) {
2533 u8 page_code
= buffer
[offset
] & 0x3F;
2534 u8 spf
= buffer
[offset
] & 0x40;
2536 if (page_code
== 8 || page_code
== 6) {
2537 /* We're interested only in the first 3 bytes.
2539 if (len
- offset
<= 2) {
2540 sd_first_printk(KERN_ERR
, sdkp
,
2541 "Incomplete mode parameter "
2545 modepage
= page_code
;
2549 /* Go to the next page */
2550 if (spf
&& len
- offset
> 3)
2551 offset
+= 4 + (buffer
[offset
+2] << 8) +
2553 else if (!spf
&& len
- offset
> 1)
2554 offset
+= 2 + buffer
[offset
+1];
2556 sd_first_printk(KERN_ERR
, sdkp
,
2558 "parameter data\n");
2564 sd_first_printk(KERN_ERR
, sdkp
, "No Caching mode page found\n");
2568 if (modepage
== 8) {
2569 sdkp
->WCE
= ((buffer
[offset
+ 2] & 0x04) != 0);
2570 sdkp
->RCD
= ((buffer
[offset
+ 2] & 0x01) != 0);
2572 sdkp
->WCE
= ((buffer
[offset
+ 2] & 0x01) == 0);
2576 sdkp
->DPOFUA
= (data
.device_specific
& 0x10) != 0;
2577 if (sdp
->broken_fua
) {
2578 sd_first_printk(KERN_NOTICE
, sdkp
, "Disabling FUA\n");
2580 } else if (sdkp
->DPOFUA
&& !sdkp
->device
->use_10_for_rw
&&
2581 !sdkp
->device
->use_16_for_rw
) {
2582 sd_first_printk(KERN_NOTICE
, sdkp
,
2583 "Uses READ/WRITE(6), disabling FUA\n");
2587 /* No cache flush allowed for write protected devices */
2588 if (sdkp
->WCE
&& sdkp
->write_prot
)
2591 if (sdkp
->first_scan
|| old_wce
!= sdkp
->WCE
||
2592 old_rcd
!= sdkp
->RCD
|| old_dpofua
!= sdkp
->DPOFUA
)
2593 sd_printk(KERN_NOTICE
, sdkp
,
2594 "Write cache: %s, read cache: %s, %s\n",
2595 sdkp
->WCE
? "enabled" : "disabled",
2596 sdkp
->RCD
? "disabled" : "enabled",
2597 sdkp
->DPOFUA
? "supports DPO and FUA"
2598 : "doesn't support DPO or FUA");
2604 if (scsi_sense_valid(&sshdr
) &&
2605 sshdr
.sense_key
== ILLEGAL_REQUEST
&&
2606 sshdr
.asc
== 0x24 && sshdr
.ascq
== 0x0)
2607 /* Invalid field in CDB */
2608 sd_first_printk(KERN_NOTICE
, sdkp
, "Cache data unavailable\n");
2610 sd_first_printk(KERN_ERR
, sdkp
,
2611 "Asking for cache data failed\n");
2614 if (sdp
->wce_default_on
) {
2615 sd_first_printk(KERN_NOTICE
, sdkp
,
2616 "Assuming drive cache: write back\n");
2619 sd_first_printk(KERN_ERR
, sdkp
,
2620 "Assuming drive cache: write through\n");
2628 * The ATO bit indicates whether the DIF application tag is available
2629 * for use by the operating system.
2631 static void sd_read_app_tag_own(struct scsi_disk
*sdkp
, unsigned char *buffer
)
2634 struct scsi_device
*sdp
= sdkp
->device
;
2635 struct scsi_mode_data data
;
2636 struct scsi_sense_hdr sshdr
;
2638 if (sdp
->type
!= TYPE_DISK
)
2641 if (sdkp
->protection_type
== 0)
2644 res
= scsi_mode_sense(sdp
, 1, 0x0a, buffer
, 36, SD_TIMEOUT
,
2645 SD_MAX_RETRIES
, &data
, &sshdr
);
2647 if (!scsi_status_is_good(res
) || !data
.header_length
||
2649 sd_first_printk(KERN_WARNING
, sdkp
,
2650 "getting Control mode page failed, assume no ATO\n");
2652 if (scsi_sense_valid(&sshdr
))
2653 sd_print_sense_hdr(sdkp
, &sshdr
);
2658 offset
= data
.header_length
+ data
.block_descriptor_length
;
2660 if ((buffer
[offset
] & 0x3f) != 0x0a) {
2661 sd_first_printk(KERN_ERR
, sdkp
, "ATO Got wrong page\n");
2665 if ((buffer
[offset
+ 5] & 0x80) == 0)
2674 * sd_read_block_limits - Query disk device for preferred I/O sizes.
2675 * @disk: disk to query
2677 static void sd_read_block_limits(struct scsi_disk
*sdkp
)
2679 unsigned int sector_sz
= sdkp
->device
->sector_size
;
2680 const int vpd_len
= 64;
2681 unsigned char *buffer
= kmalloc(vpd_len
, GFP_KERNEL
);
2684 /* Block Limits VPD */
2685 scsi_get_vpd_page(sdkp
->device
, 0xb0, buffer
, vpd_len
))
2688 blk_queue_io_min(sdkp
->disk
->queue
,
2689 get_unaligned_be16(&buffer
[6]) * sector_sz
);
2691 sdkp
->max_xfer_blocks
= get_unaligned_be32(&buffer
[8]);
2692 sdkp
->opt_xfer_blocks
= get_unaligned_be32(&buffer
[12]);
2694 if (buffer
[3] == 0x3c) {
2695 unsigned int lba_count
, desc_count
;
2697 sdkp
->max_ws_blocks
= (u32
)get_unaligned_be64(&buffer
[36]);
2702 lba_count
= get_unaligned_be32(&buffer
[20]);
2703 desc_count
= get_unaligned_be32(&buffer
[24]);
2705 if (lba_count
&& desc_count
)
2706 sdkp
->max_unmap_blocks
= lba_count
;
2708 sdkp
->unmap_granularity
= get_unaligned_be32(&buffer
[28]);
2710 if (buffer
[32] & 0x80)
2711 sdkp
->unmap_alignment
=
2712 get_unaligned_be32(&buffer
[32]) & ~(1 << 31);
2714 if (!sdkp
->lbpvpd
) { /* LBP VPD page not provided */
2716 if (sdkp
->max_unmap_blocks
)
2717 sd_config_discard(sdkp
, SD_LBP_UNMAP
);
2719 sd_config_discard(sdkp
, SD_LBP_WS16
);
2721 } else { /* LBP VPD page tells us what to use */
2722 if (sdkp
->lbpu
&& sdkp
->max_unmap_blocks
&& !sdkp
->lbprz
)
2723 sd_config_discard(sdkp
, SD_LBP_UNMAP
);
2724 else if (sdkp
->lbpws
)
2725 sd_config_discard(sdkp
, SD_LBP_WS16
);
2726 else if (sdkp
->lbpws10
)
2727 sd_config_discard(sdkp
, SD_LBP_WS10
);
2728 else if (sdkp
->lbpu
&& sdkp
->max_unmap_blocks
)
2729 sd_config_discard(sdkp
, SD_LBP_UNMAP
);
2731 sd_config_discard(sdkp
, SD_LBP_DISABLE
);
2740 * sd_read_block_characteristics - Query block dev. characteristics
2741 * @disk: disk to query
2743 static void sd_read_block_characteristics(struct scsi_disk
*sdkp
)
2745 unsigned char *buffer
;
2747 const int vpd_len
= 64;
2749 buffer
= kmalloc(vpd_len
, GFP_KERNEL
);
2752 /* Block Device Characteristics VPD */
2753 scsi_get_vpd_page(sdkp
->device
, 0xb1, buffer
, vpd_len
))
2756 rot
= get_unaligned_be16(&buffer
[4]);
2759 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, sdkp
->disk
->queue
);
2760 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM
, sdkp
->disk
->queue
);
2768 * sd_read_block_provisioning - Query provisioning VPD page
2769 * @disk: disk to query
2771 static void sd_read_block_provisioning(struct scsi_disk
*sdkp
)
2773 unsigned char *buffer
;
2774 const int vpd_len
= 8;
2776 if (sdkp
->lbpme
== 0)
2779 buffer
= kmalloc(vpd_len
, GFP_KERNEL
);
2781 if (!buffer
|| scsi_get_vpd_page(sdkp
->device
, 0xb2, buffer
, vpd_len
))
2785 sdkp
->lbpu
= (buffer
[5] >> 7) & 1; /* UNMAP */
2786 sdkp
->lbpws
= (buffer
[5] >> 6) & 1; /* WRITE SAME(16) with UNMAP */
2787 sdkp
->lbpws10
= (buffer
[5] >> 5) & 1; /* WRITE SAME(10) with UNMAP */
2793 static void sd_read_write_same(struct scsi_disk
*sdkp
, unsigned char *buffer
)
2795 struct scsi_device
*sdev
= sdkp
->device
;
2797 if (sdev
->host
->no_write_same
) {
2798 sdev
->no_write_same
= 1;
2803 if (scsi_report_opcode(sdev
, buffer
, SD_BUF_SIZE
, INQUIRY
) < 0) {
2804 /* too large values might cause issues with arcmsr */
2805 int vpd_buf_len
= 64;
2807 sdev
->no_report_opcodes
= 1;
2809 /* Disable WRITE SAME if REPORT SUPPORTED OPERATION
2810 * CODES is unsupported and the device has an ATA
2811 * Information VPD page (SAT).
2813 if (!scsi_get_vpd_page(sdev
, 0x89, buffer
, vpd_buf_len
))
2814 sdev
->no_write_same
= 1;
2817 if (scsi_report_opcode(sdev
, buffer
, SD_BUF_SIZE
, WRITE_SAME_16
) == 1)
2820 if (scsi_report_opcode(sdev
, buffer
, SD_BUF_SIZE
, WRITE_SAME
) == 1)
2824 static int sd_try_extended_inquiry(struct scsi_device
*sdp
)
2826 /* Attempt VPD inquiry if the device blacklist explicitly calls
2829 if (sdp
->try_vpd_pages
)
2832 * Although VPD inquiries can go to SCSI-2 type devices,
2833 * some USB ones crash on receiving them, and the pages
2834 * we currently ask for are for SPC-3 and beyond
2836 if (sdp
->scsi_level
> SCSI_SPC_2
&& !sdp
->skip_vpd_pages
)
2842 * sd_revalidate_disk - called the first time a new disk is seen,
2843 * performs disk spin up, read_capacity, etc.
2844 * @disk: struct gendisk we care about
2846 static int sd_revalidate_disk(struct gendisk
*disk
)
2848 struct scsi_disk
*sdkp
= scsi_disk(disk
);
2849 struct scsi_device
*sdp
= sdkp
->device
;
2850 struct request_queue
*q
= sdkp
->disk
->queue
;
2851 unsigned char *buffer
;
2852 unsigned int dev_max
, rw_max
;
2854 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO
, sdkp
,
2855 "sd_revalidate_disk\n"));
2858 * If the device is offline, don't try and read capacity or any
2859 * of the other niceties.
2861 if (!scsi_device_online(sdp
))
2864 buffer
= kmalloc(SD_BUF_SIZE
, GFP_KERNEL
);
2866 sd_printk(KERN_WARNING
, sdkp
, "sd_revalidate_disk: Memory "
2867 "allocation failure.\n");
2871 sd_spinup_disk(sdkp
);
2874 * Without media there is no reason to ask; moreover, some devices
2875 * react badly if we do.
2877 if (sdkp
->media_present
) {
2878 sd_read_capacity(sdkp
, buffer
);
2880 if (sd_try_extended_inquiry(sdp
)) {
2881 sd_read_block_provisioning(sdkp
);
2882 sd_read_block_limits(sdkp
);
2883 sd_read_block_characteristics(sdkp
);
2886 sd_read_write_protect_flag(sdkp
, buffer
);
2887 sd_read_cache_type(sdkp
, buffer
);
2888 sd_read_app_tag_own(sdkp
, buffer
);
2889 sd_read_write_same(sdkp
, buffer
);
2893 * We now have all cache related info, determine how we deal
2894 * with flush requests.
2896 sd_set_flush_flag(sdkp
);
2898 /* Initial block count limit based on CDB TRANSFER LENGTH field size. */
2899 dev_max
= sdp
->use_16_for_rw
? SD_MAX_XFER_BLOCKS
: SD_DEF_XFER_BLOCKS
;
2901 /* Some devices report a maximum block count for READ/WRITE requests. */
2902 dev_max
= min_not_zero(dev_max
, sdkp
->max_xfer_blocks
);
2903 q
->limits
.max_dev_sectors
= logical_to_sectors(sdp
, dev_max
);
2906 * Determine the device's preferred I/O size for reads and writes
2907 * unless the reported value is unreasonably small, large, or
2910 if (sdkp
->opt_xfer_blocks
&&
2911 sdkp
->opt_xfer_blocks
<= dev_max
&&
2912 sdkp
->opt_xfer_blocks
<= SD_DEF_XFER_BLOCKS
&&
2913 logical_to_bytes(sdp
, sdkp
->opt_xfer_blocks
) >= PAGE_CACHE_SIZE
) {
2914 q
->limits
.io_opt
= logical_to_bytes(sdp
, sdkp
->opt_xfer_blocks
);
2915 rw_max
= logical_to_sectors(sdp
, sdkp
->opt_xfer_blocks
);
2917 rw_max
= min_not_zero(logical_to_sectors(sdp
, dev_max
),
2918 (sector_t
)BLK_DEF_MAX_SECTORS
);
2920 /* Do not exceed controller limit */
2921 rw_max
= min(rw_max
, queue_max_hw_sectors(q
));
2924 * Only update max_sectors if previously unset or if the current value
2925 * exceeds the capabilities of the hardware.
2927 if (sdkp
->first_scan
||
2928 q
->limits
.max_sectors
> q
->limits
.max_dev_sectors
||
2929 q
->limits
.max_sectors
> q
->limits
.max_hw_sectors
)
2930 q
->limits
.max_sectors
= rw_max
;
2932 sdkp
->first_scan
= 0;
2934 set_capacity(disk
, logical_to_sectors(sdp
, sdkp
->capacity
));
2935 sd_config_write_same(sdkp
);
2943 * sd_unlock_native_capacity - unlock native capacity
2944 * @disk: struct gendisk to set capacity for
2946 * Block layer calls this function if it detects that partitions
2947 * on @disk reach beyond the end of the device. If the SCSI host
2948 * implements ->unlock_native_capacity() method, it's invoked to
2949 * give it a chance to adjust the device capacity.
2952 * Defined by block layer. Might sleep.
2954 static void sd_unlock_native_capacity(struct gendisk
*disk
)
2956 struct scsi_device
*sdev
= scsi_disk(disk
)->device
;
2958 if (sdev
->host
->hostt
->unlock_native_capacity
)
2959 sdev
->host
->hostt
->unlock_native_capacity(sdev
);
2963 * sd_format_disk_name - format disk name
2964 * @prefix: name prefix - ie. "sd" for SCSI disks
2965 * @index: index of the disk to format name for
2966 * @buf: output buffer
2967 * @buflen: length of the output buffer
2969 * SCSI disk names starts at sda. The 26th device is sdz and the
2970 * 27th is sdaa. The last one for two lettered suffix is sdzz
2971 * which is followed by sdaaa.
2973 * This is basically 26 base counting with one extra 'nil' entry
2974 * at the beginning from the second digit on and can be
2975 * determined using similar method as 26 base conversion with the
2976 * index shifted -1 after each digit is computed.
2982 * 0 on success, -errno on failure.
2984 static int sd_format_disk_name(char *prefix
, int index
, char *buf
, int buflen
)
2986 const int base
= 'z' - 'a' + 1;
2987 char *begin
= buf
+ strlen(prefix
);
2988 char *end
= buf
+ buflen
;
2998 *--p
= 'a' + (index
% unit
);
2999 index
= (index
/ unit
) - 1;
3000 } while (index
>= 0);
3002 memmove(begin
, p
, end
- p
);
3003 memcpy(buf
, prefix
, strlen(prefix
));
3009 * The asynchronous part of sd_probe
3011 static void sd_probe_async(void *data
, async_cookie_t cookie
)
3013 struct scsi_disk
*sdkp
= data
;
3014 struct scsi_device
*sdp
;
3021 index
= sdkp
->index
;
3022 dev
= &sdp
->sdev_gendev
;
3024 gd
->major
= sd_major((index
& 0xf0) >> 4);
3025 gd
->first_minor
= ((index
& 0xf) << 4) | (index
& 0xfff00);
3026 gd
->minors
= SD_MINORS
;
3028 gd
->fops
= &sd_fops
;
3029 gd
->private_data
= &sdkp
->driver
;
3030 gd
->queue
= sdkp
->device
->request_queue
;
3032 /* defaults, until the device tells us otherwise */
3033 sdp
->sector_size
= 512;
3035 sdkp
->media_present
= 1;
3036 sdkp
->write_prot
= 0;
3037 sdkp
->cache_override
= 0;
3041 sdkp
->first_scan
= 1;
3042 sdkp
->max_medium_access_timeouts
= SD_MAX_MEDIUM_TIMEOUTS
;
3044 sd_revalidate_disk(gd
);
3046 gd
->driverfs_dev
= &sdp
->sdev_gendev
;
3047 gd
->flags
= GENHD_FL_EXT_DEVT
;
3048 if (sdp
->removable
) {
3049 gd
->flags
|= GENHD_FL_REMOVABLE
;
3050 gd
->events
|= DISK_EVENT_MEDIA_CHANGE
;
3053 blk_pm_runtime_init(sdp
->request_queue
, dev
);
3056 sd_dif_config_host(sdkp
);
3058 sd_revalidate_disk(gd
);
3060 sd_printk(KERN_NOTICE
, sdkp
, "Attached SCSI %sdisk\n",
3061 sdp
->removable
? "removable " : "");
3062 scsi_autopm_put_device(sdp
);
3063 put_device(&sdkp
->dev
);
3067 * sd_probe - called during driver initialization and whenever a
3068 * new scsi device is attached to the system. It is called once
3069 * for each scsi device (not just disks) present.
3070 * @dev: pointer to device object
3072 * Returns 0 if successful (or not interested in this scsi device
3073 * (e.g. scanner)); 1 when there is an error.
3075 * Note: this function is invoked from the scsi mid-level.
3076 * This function sets up the mapping between a given
3077 * <host,channel,id,lun> (found in sdp) and new device name
3078 * (e.g. /dev/sda). More precisely it is the block device major
3079 * and minor number that is chosen here.
3081 * Assume sd_probe is not re-entrant (for time being)
3082 * Also think about sd_probe() and sd_remove() running coincidentally.
3084 static int sd_probe(struct device
*dev
)
3086 struct scsi_device
*sdp
= to_scsi_device(dev
);
3087 struct scsi_disk
*sdkp
;
3092 scsi_autopm_get_device(sdp
);
3094 if (sdp
->type
!= TYPE_DISK
&& sdp
->type
!= TYPE_MOD
&& sdp
->type
!= TYPE_RBC
)
3097 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO
, sdp
,
3101 sdkp
= kzalloc(sizeof(*sdkp
), GFP_KERNEL
);
3105 gd
= alloc_disk(SD_MINORS
);
3110 if (!ida_pre_get(&sd_index_ida
, GFP_KERNEL
))
3113 spin_lock(&sd_index_lock
);
3114 error
= ida_get_new(&sd_index_ida
, &index
);
3115 spin_unlock(&sd_index_lock
);
3116 } while (error
== -EAGAIN
);
3119 sdev_printk(KERN_WARNING
, sdp
, "sd_probe: memory exhausted.\n");
3123 error
= sd_format_disk_name("sd", index
, gd
->disk_name
, DISK_NAME_LEN
);
3125 sdev_printk(KERN_WARNING
, sdp
, "SCSI disk (sd) name length exceeded.\n");
3126 goto out_free_index
;
3130 sdkp
->driver
= &sd_template
;
3132 sdkp
->index
= index
;
3133 atomic_set(&sdkp
->openers
, 0);
3134 atomic_set(&sdkp
->device
->ioerr_cnt
, 0);
3136 if (!sdp
->request_queue
->rq_timeout
) {
3137 if (sdp
->type
!= TYPE_MOD
)
3138 blk_queue_rq_timeout(sdp
->request_queue
, SD_TIMEOUT
);
3140 blk_queue_rq_timeout(sdp
->request_queue
,
3144 device_initialize(&sdkp
->dev
);
3145 sdkp
->dev
.parent
= dev
;
3146 sdkp
->dev
.class = &sd_disk_class
;
3147 dev_set_name(&sdkp
->dev
, "%s", dev_name(dev
));
3149 error
= device_add(&sdkp
->dev
);
3151 goto out_free_index
;
3154 dev_set_drvdata(dev
, sdkp
);
3156 get_device(&sdkp
->dev
); /* prevent release before async_schedule */
3157 async_schedule_domain(sd_probe_async
, sdkp
, &scsi_sd_probe_domain
);
3162 spin_lock(&sd_index_lock
);
3163 ida_remove(&sd_index_ida
, index
);
3164 spin_unlock(&sd_index_lock
);
3170 scsi_autopm_put_device(sdp
);
3175 * sd_remove - called whenever a scsi disk (previously recognized by
3176 * sd_probe) is detached from the system. It is called (potentially
3177 * multiple times) during sd module unload.
3178 * @sdp: pointer to mid level scsi device object
3180 * Note: this function is invoked from the scsi mid-level.
3181 * This function potentially frees up a device name (e.g. /dev/sdc)
3182 * that could be re-used by a subsequent sd_probe().
3183 * This function is not called when the built-in sd driver is "exit-ed".
3185 static int sd_remove(struct device
*dev
)
3187 struct scsi_disk
*sdkp
;
3190 sdkp
= dev_get_drvdata(dev
);
3191 devt
= disk_devt(sdkp
->disk
);
3192 scsi_autopm_get_device(sdkp
->device
);
3194 async_synchronize_full_domain(&scsi_sd_pm_domain
);
3195 async_synchronize_full_domain(&scsi_sd_probe_domain
);
3196 device_del(&sdkp
->dev
);
3197 del_gendisk(sdkp
->disk
);
3200 blk_register_region(devt
, SD_MINORS
, NULL
,
3201 sd_default_probe
, NULL
, NULL
);
3203 mutex_lock(&sd_ref_mutex
);
3204 dev_set_drvdata(dev
, NULL
);
3205 put_device(&sdkp
->dev
);
3206 mutex_unlock(&sd_ref_mutex
);
3212 * scsi_disk_release - Called to free the scsi_disk structure
3213 * @dev: pointer to embedded class device
3215 * sd_ref_mutex must be held entering this routine. Because it is
3216 * called on last put, you should always use the scsi_disk_get()
3217 * scsi_disk_put() helpers which manipulate the semaphore directly
3218 * and never do a direct put_device.
3220 static void scsi_disk_release(struct device
*dev
)
3222 struct scsi_disk
*sdkp
= to_scsi_disk(dev
);
3223 struct gendisk
*disk
= sdkp
->disk
;
3225 spin_lock(&sd_index_lock
);
3226 ida_remove(&sd_index_ida
, sdkp
->index
);
3227 spin_unlock(&sd_index_lock
);
3229 disk
->private_data
= NULL
;
3231 put_device(&sdkp
->device
->sdev_gendev
);
3236 static int sd_start_stop_device(struct scsi_disk
*sdkp
, int start
)
3238 unsigned char cmd
[6] = { START_STOP
}; /* START_VALID */
3239 struct scsi_sense_hdr sshdr
;
3240 struct scsi_device
*sdp
= sdkp
->device
;
3244 cmd
[4] |= 1; /* START */
3246 if (sdp
->start_stop_pwr_cond
)
3247 cmd
[4] |= start
? 1 << 4 : 3 << 4; /* Active or Standby */
3249 if (!scsi_device_online(sdp
))
3252 res
= scsi_execute_req_flags(sdp
, cmd
, DMA_NONE
, NULL
, 0, &sshdr
,
3253 SD_TIMEOUT
, SD_MAX_RETRIES
, NULL
, REQ_PM
);
3255 sd_print_result(sdkp
, "Start/Stop Unit failed", res
);
3256 if (driver_byte(res
) & DRIVER_SENSE
)
3257 sd_print_sense_hdr(sdkp
, &sshdr
);
3258 if (scsi_sense_valid(&sshdr
) &&
3259 /* 0x3a is medium not present */
3264 /* SCSI error codes must not go to the generic layer */
3272 * Send a SYNCHRONIZE CACHE instruction down to the device through
3273 * the normal SCSI command structure. Wait for the command to
3276 static void sd_shutdown(struct device
*dev
)
3278 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
3281 return; /* this can happen */
3283 if (pm_runtime_suspended(dev
))
3286 if (sdkp
->WCE
&& sdkp
->media_present
) {
3287 sd_printk(KERN_NOTICE
, sdkp
, "Synchronizing SCSI cache\n");
3288 sd_sync_cache(sdkp
);
3291 if (system_state
!= SYSTEM_RESTART
&& sdkp
->device
->manage_start_stop
) {
3292 sd_printk(KERN_NOTICE
, sdkp
, "Stopping disk\n");
3293 sd_start_stop_device(sdkp
, 0);
3297 static int sd_suspend_common(struct device
*dev
, bool ignore_stop_errors
)
3299 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
3302 if (!sdkp
) /* E.g.: runtime suspend following sd_remove() */
3305 if (sdkp
->WCE
&& sdkp
->media_present
) {
3306 sd_printk(KERN_NOTICE
, sdkp
, "Synchronizing SCSI cache\n");
3307 ret
= sd_sync_cache(sdkp
);
3309 /* ignore OFFLINE device */
3316 if (sdkp
->device
->manage_start_stop
) {
3317 sd_printk(KERN_NOTICE
, sdkp
, "Stopping disk\n");
3318 /* an error is not worth aborting a system sleep */
3319 ret
= sd_start_stop_device(sdkp
, 0);
3320 if (ignore_stop_errors
)
3328 static int sd_suspend_system(struct device
*dev
)
3330 return sd_suspend_common(dev
, true);
3333 static int sd_suspend_runtime(struct device
*dev
)
3335 return sd_suspend_common(dev
, false);
3338 static int sd_resume(struct device
*dev
)
3340 struct scsi_disk
*sdkp
= dev_get_drvdata(dev
);
3342 if (!sdkp
) /* E.g.: runtime resume at the start of sd_probe() */
3345 if (!sdkp
->device
->manage_start_stop
)
3348 sd_printk(KERN_NOTICE
, sdkp
, "Starting disk\n");
3349 return sd_start_stop_device(sdkp
, 1);
3353 * init_sd - entry point for this driver (both when built in or when
3356 * Note: this function registers this driver with the scsi mid-level.
3358 static int __init
init_sd(void)
3360 int majors
= 0, i
, err
;
3362 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
3364 for (i
= 0; i
< SD_MAJORS
; i
++) {
3365 if (register_blkdev(sd_major(i
), "sd") != 0)
3368 blk_register_region(sd_major(i
), SD_MINORS
, NULL
,
3369 sd_default_probe
, NULL
, NULL
);
3375 err
= class_register(&sd_disk_class
);
3379 sd_cdb_cache
= kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE
,
3381 if (!sd_cdb_cache
) {
3382 printk(KERN_ERR
"sd: can't init extended cdb cache\n");
3387 sd_cdb_pool
= mempool_create_slab_pool(SD_MEMPOOL_SIZE
, sd_cdb_cache
);
3389 printk(KERN_ERR
"sd: can't init extended cdb pool\n");
3394 err
= scsi_register_driver(&sd_template
.gendrv
);
3396 goto err_out_driver
;
3401 mempool_destroy(sd_cdb_pool
);
3404 kmem_cache_destroy(sd_cdb_cache
);
3407 class_unregister(&sd_disk_class
);
3409 for (i
= 0; i
< SD_MAJORS
; i
++)
3410 unregister_blkdev(sd_major(i
), "sd");
3415 * exit_sd - exit point for this driver (when it is a module).
3417 * Note: this function unregisters this driver from the scsi mid-level.
3419 static void __exit
exit_sd(void)
3423 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
3425 scsi_unregister_driver(&sd_template
.gendrv
);
3426 mempool_destroy(sd_cdb_pool
);
3427 kmem_cache_destroy(sd_cdb_cache
);
3429 class_unregister(&sd_disk_class
);
3431 for (i
= 0; i
< SD_MAJORS
; i
++) {
3432 blk_unregister_region(sd_major(i
), SD_MINORS
);
3433 unregister_blkdev(sd_major(i
), "sd");
3437 module_init(init_sd
);
3438 module_exit(exit_sd
);
3440 static void sd_print_sense_hdr(struct scsi_disk
*sdkp
,
3441 struct scsi_sense_hdr
*sshdr
)
3443 scsi_print_sense_hdr(sdkp
->device
,
3444 sdkp
->disk
? sdkp
->disk
->disk_name
: NULL
, sshdr
);
3447 static void sd_print_result(const struct scsi_disk
*sdkp
, const char *msg
,
3450 const char *hb_string
= scsi_hostbyte_string(result
);
3451 const char *db_string
= scsi_driverbyte_string(result
);
3453 if (hb_string
|| db_string
)
3454 sd_printk(KERN_INFO
, sdkp
,
3455 "%s: Result: hostbyte=%s driverbyte=%s\n", msg
,
3456 hb_string
? hb_string
: "invalid",
3457 db_string
? db_string
: "invalid");
3459 sd_printk(KERN_INFO
, sdkp
,
3460 "%s: Result: hostbyte=0x%02x driverbyte=0x%02x\n",
3461 msg
, host_byte(result
), driver_byte(result
));