blob: a19b99121b1c4d0c3757d9ef6425e39c8ffdda0f [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * sd.c Copyright (C) 1992 Drew Eckhardt
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4 *
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.
25 *
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.
33 */
34
35#include <linux/module.h>
36#include <linux/fs.h>
37#include <linux/kernel.h>
38#include <linux/mm.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/sed-opal.h>
54#include <linux/pm_runtime.h>
55#include <linux/pr.h>
56#include <linux/t10-pi.h>
57#include <linux/uaccess.h>
58#include <asm/unaligned.h>
59
60#include <scsi/scsi.h>
61#include <scsi/scsi_cmnd.h>
62#include <scsi/scsi_dbg.h>
63#include <scsi/scsi_device.h>
64#include <scsi/scsi_driver.h>
65#include <scsi/scsi_eh.h>
66#include <scsi/scsi_host.h>
67#include <scsi/scsi_ioctl.h>
68#include <scsi/scsicam.h>
69
70#include "sd.h"
71#include "scsi_priv.h"
72#include "scsi_logging.h"
73
74MODULE_AUTHOR("Eric Youngdale");
75MODULE_DESCRIPTION("SCSI disk (sd) driver");
76MODULE_LICENSE("GPL");
77
78MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
79MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
80MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
81MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
82MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
83MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
84MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
85MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
86MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
87MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
88MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
89MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
90MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
91MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
92MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
93MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
94MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
95MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
96MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
97MODULE_ALIAS_SCSI_DEVICE(TYPE_ZBC);
98
99#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
100#define SD_MINORS 16
101#else
102#define SD_MINORS 0
103#endif
104
105static void sd_config_discard(struct scsi_disk *, unsigned int);
106static void sd_config_write_same(struct scsi_disk *);
107static int sd_revalidate_disk(struct gendisk *);
108static void sd_unlock_native_capacity(struct gendisk *disk);
109static int sd_probe(struct device *);
110static int sd_remove(struct device *);
111static void sd_shutdown(struct device *);
112static int sd_suspend_system(struct device *);
113static int sd_suspend_runtime(struct device *);
114static int sd_resume(struct device *);
115static void sd_rescan(struct device *);
116static int sd_init_command(struct scsi_cmnd *SCpnt);
117static void sd_uninit_command(struct scsi_cmnd *SCpnt);
118static int sd_done(struct scsi_cmnd *);
119static void sd_eh_reset(struct scsi_cmnd *);
120static int sd_eh_action(struct scsi_cmnd *, int);
121static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
122static void scsi_disk_release(struct device *cdev);
123static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
124static void sd_print_result(const struct scsi_disk *, const char *, int);
125
126static DEFINE_IDA(sd_index_ida);
127
128/* This semaphore is used to mediate the 0->1 reference get in the
129 * face of object destruction (i.e. we can't allow a get on an
130 * object after last put) */
131static DEFINE_MUTEX(sd_ref_mutex);
132
133static struct kmem_cache *sd_cdb_cache;
134static mempool_t *sd_cdb_pool;
135static mempool_t *sd_page_pool;
136
137static const char *sd_cache_types[] = {
138 "write through", "none", "write back",
139 "write back, no read (daft)"
140};
141
142static void sd_set_flush_flag(struct scsi_disk *sdkp)
143{
144 bool wc = false, fua = false;
145
146 if (sdkp->WCE) {
147 wc = true;
148 if (sdkp->DPOFUA)
149 fua = true;
150 }
151
152 blk_queue_write_cache(sdkp->disk->queue, wc, fua);
153}
154
155static ssize_t
156cache_type_store(struct device *dev, struct device_attribute *attr,
157 const char *buf, size_t count)
158{
159 int ct, rcd, wce, sp;
160 struct scsi_disk *sdkp = to_scsi_disk(dev);
161 struct scsi_device *sdp = sdkp->device;
162 char buffer[64];
163 char *buffer_data;
164 struct scsi_mode_data data;
165 struct scsi_sense_hdr sshdr;
166 static const char temp[] = "temporary ";
167 int len;
168
169 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
170 /* no cache control on RBC devices; theoretically they
171 * can do it, but there's probably so many exceptions
172 * it's not worth the risk */
173 return -EINVAL;
174
175 if (strncmp(buf, temp, sizeof(temp) - 1) == 0) {
176 buf += sizeof(temp) - 1;
177 sdkp->cache_override = 1;
178 } else {
179 sdkp->cache_override = 0;
180 }
181
182 ct = sysfs_match_string(sd_cache_types, buf);
183 if (ct < 0)
184 return -EINVAL;
185
186 rcd = ct & 0x01 ? 1 : 0;
187 wce = (ct & 0x02) && !sdkp->write_prot ? 1 : 0;
188
189 if (sdkp->cache_override) {
190 sdkp->WCE = wce;
191 sdkp->RCD = rcd;
192 sd_set_flush_flag(sdkp);
193 return count;
194 }
195
196 if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
197 SD_MAX_RETRIES, &data, NULL))
198 return -EINVAL;
199 len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
200 data.block_descriptor_length);
201 buffer_data = buffer + data.header_length +
202 data.block_descriptor_length;
203 buffer_data[2] &= ~0x05;
204 buffer_data[2] |= wce << 2 | rcd;
205 sp = buffer_data[0] & 0x80 ? 1 : 0;
206 buffer_data[0] &= ~0x80;
207
208 /*
209 * Ensure WP, DPOFUA, and RESERVED fields are cleared in
210 * received mode parameter buffer before doing MODE SELECT.
211 */
212 data.device_specific = 0;
213
214 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
215 SD_MAX_RETRIES, &data, &sshdr)) {
216 if (scsi_sense_valid(&sshdr))
217 sd_print_sense_hdr(sdkp, &sshdr);
218 return -EINVAL;
219 }
220 revalidate_disk(sdkp->disk);
221 return count;
222}
223
224static ssize_t
225manage_start_stop_show(struct device *dev, struct device_attribute *attr,
226 char *buf)
227{
228 struct scsi_disk *sdkp = to_scsi_disk(dev);
229 struct scsi_device *sdp = sdkp->device;
230
231 return sprintf(buf, "%u\n", sdp->manage_start_stop);
232}
233
234static ssize_t
235manage_start_stop_store(struct device *dev, struct device_attribute *attr,
236 const char *buf, size_t count)
237{
238 struct scsi_disk *sdkp = to_scsi_disk(dev);
239 struct scsi_device *sdp = sdkp->device;
240 bool v;
241
242 if (!capable(CAP_SYS_ADMIN))
243 return -EACCES;
244
245 if (kstrtobool(buf, &v))
246 return -EINVAL;
247
248 sdp->manage_start_stop = v;
249
250 return count;
251}
252static DEVICE_ATTR_RW(manage_start_stop);
253
254static ssize_t
255allow_restart_show(struct device *dev, struct device_attribute *attr, char *buf)
256{
257 struct scsi_disk *sdkp = to_scsi_disk(dev);
258
259 return sprintf(buf, "%u\n", sdkp->device->allow_restart);
260}
261
262static ssize_t
263allow_restart_store(struct device *dev, struct device_attribute *attr,
264 const char *buf, size_t count)
265{
266 bool v;
267 struct scsi_disk *sdkp = to_scsi_disk(dev);
268 struct scsi_device *sdp = sdkp->device;
269
270 if (!capable(CAP_SYS_ADMIN))
271 return -EACCES;
272
273 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
274 return -EINVAL;
275
276 if (kstrtobool(buf, &v))
277 return -EINVAL;
278
279 sdp->allow_restart = v;
280
281 return count;
282}
283static DEVICE_ATTR_RW(allow_restart);
284
285static ssize_t
286cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
287{
288 struct scsi_disk *sdkp = to_scsi_disk(dev);
289 int ct = sdkp->RCD + 2*sdkp->WCE;
290
291 return sprintf(buf, "%s\n", sd_cache_types[ct]);
292}
293static DEVICE_ATTR_RW(cache_type);
294
295static ssize_t
296FUA_show(struct device *dev, struct device_attribute *attr, char *buf)
297{
298 struct scsi_disk *sdkp = to_scsi_disk(dev);
299
300 return sprintf(buf, "%u\n", sdkp->DPOFUA);
301}
302static DEVICE_ATTR_RO(FUA);
303
304static ssize_t
305protection_type_show(struct device *dev, struct device_attribute *attr,
306 char *buf)
307{
308 struct scsi_disk *sdkp = to_scsi_disk(dev);
309
310 return sprintf(buf, "%u\n", sdkp->protection_type);
311}
312
313static ssize_t
314protection_type_store(struct device *dev, struct device_attribute *attr,
315 const char *buf, size_t count)
316{
317 struct scsi_disk *sdkp = to_scsi_disk(dev);
318 unsigned int val;
319 int err;
320
321 if (!capable(CAP_SYS_ADMIN))
322 return -EACCES;
323
324 err = kstrtouint(buf, 10, &val);
325
326 if (err)
327 return err;
328
329 if (val <= T10_PI_TYPE3_PROTECTION)
330 sdkp->protection_type = val;
331
332 return count;
333}
334static DEVICE_ATTR_RW(protection_type);
335
336static ssize_t
337protection_mode_show(struct device *dev, struct device_attribute *attr,
338 char *buf)
339{
340 struct scsi_disk *sdkp = to_scsi_disk(dev);
341 struct scsi_device *sdp = sdkp->device;
342 unsigned int dif, dix;
343
344 dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
345 dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type);
346
347 if (!dix && scsi_host_dix_capable(sdp->host, T10_PI_TYPE0_PROTECTION)) {
348 dif = 0;
349 dix = 1;
350 }
351
352 if (!dif && !dix)
353 return sprintf(buf, "none\n");
354
355 return sprintf(buf, "%s%u\n", dix ? "dix" : "dif", dif);
356}
357static DEVICE_ATTR_RO(protection_mode);
358
359static ssize_t
360app_tag_own_show(struct device *dev, struct device_attribute *attr, char *buf)
361{
362 struct scsi_disk *sdkp = to_scsi_disk(dev);
363
364 return sprintf(buf, "%u\n", sdkp->ATO);
365}
366static DEVICE_ATTR_RO(app_tag_own);
367
368static ssize_t
369thin_provisioning_show(struct device *dev, struct device_attribute *attr,
370 char *buf)
371{
372 struct scsi_disk *sdkp = to_scsi_disk(dev);
373
374 return sprintf(buf, "%u\n", sdkp->lbpme);
375}
376static DEVICE_ATTR_RO(thin_provisioning);
377
378/* sysfs_match_string() requires dense arrays */
379static const char *lbp_mode[] = {
380 [SD_LBP_FULL] = "full",
381 [SD_LBP_UNMAP] = "unmap",
382 [SD_LBP_WS16] = "writesame_16",
383 [SD_LBP_WS10] = "writesame_10",
384 [SD_LBP_ZERO] = "writesame_zero",
385 [SD_LBP_DISABLE] = "disabled",
386};
387
388static ssize_t
389provisioning_mode_show(struct device *dev, struct device_attribute *attr,
390 char *buf)
391{
392 struct scsi_disk *sdkp = to_scsi_disk(dev);
393
394 return sprintf(buf, "%s\n", lbp_mode[sdkp->provisioning_mode]);
395}
396
397static ssize_t
398provisioning_mode_store(struct device *dev, struct device_attribute *attr,
399 const char *buf, size_t count)
400{
401 struct scsi_disk *sdkp = to_scsi_disk(dev);
402 struct scsi_device *sdp = sdkp->device;
403 int mode;
404
405 if (!capable(CAP_SYS_ADMIN))
406 return -EACCES;
407
408 if (sd_is_zoned(sdkp)) {
409 sd_config_discard(sdkp, SD_LBP_DISABLE);
410 return count;
411 }
412
413 if (sdp->type != TYPE_DISK)
414 return -EINVAL;
415
416 mode = sysfs_match_string(lbp_mode, buf);
417 if (mode < 0)
418 return -EINVAL;
419
420 sd_config_discard(sdkp, mode);
421
422 return count;
423}
424static DEVICE_ATTR_RW(provisioning_mode);
425
426/* sysfs_match_string() requires dense arrays */
427static const char *zeroing_mode[] = {
428 [SD_ZERO_WRITE] = "write",
429 [SD_ZERO_WS] = "writesame",
430 [SD_ZERO_WS16_UNMAP] = "writesame_16_unmap",
431 [SD_ZERO_WS10_UNMAP] = "writesame_10_unmap",
432};
433
434static ssize_t
435zeroing_mode_show(struct device *dev, struct device_attribute *attr,
436 char *buf)
437{
438 struct scsi_disk *sdkp = to_scsi_disk(dev);
439
440 return sprintf(buf, "%s\n", zeroing_mode[sdkp->zeroing_mode]);
441}
442
443static ssize_t
444zeroing_mode_store(struct device *dev, struct device_attribute *attr,
445 const char *buf, size_t count)
446{
447 struct scsi_disk *sdkp = to_scsi_disk(dev);
448 int mode;
449
450 if (!capable(CAP_SYS_ADMIN))
451 return -EACCES;
452
453 mode = sysfs_match_string(zeroing_mode, buf);
454 if (mode < 0)
455 return -EINVAL;
456
457 sdkp->zeroing_mode = mode;
458
459 return count;
460}
461static DEVICE_ATTR_RW(zeroing_mode);
462
463static ssize_t
464max_medium_access_timeouts_show(struct device *dev,
465 struct device_attribute *attr, char *buf)
466{
467 struct scsi_disk *sdkp = to_scsi_disk(dev);
468
469 return sprintf(buf, "%u\n", sdkp->max_medium_access_timeouts);
470}
471
472static ssize_t
473max_medium_access_timeouts_store(struct device *dev,
474 struct device_attribute *attr, const char *buf,
475 size_t count)
476{
477 struct scsi_disk *sdkp = to_scsi_disk(dev);
478 int err;
479
480 if (!capable(CAP_SYS_ADMIN))
481 return -EACCES;
482
483 err = kstrtouint(buf, 10, &sdkp->max_medium_access_timeouts);
484
485 return err ? err : count;
486}
487static DEVICE_ATTR_RW(max_medium_access_timeouts);
488
489static ssize_t
490max_write_same_blocks_show(struct device *dev, struct device_attribute *attr,
491 char *buf)
492{
493 struct scsi_disk *sdkp = to_scsi_disk(dev);
494
495 return sprintf(buf, "%u\n", sdkp->max_ws_blocks);
496}
497
498static ssize_t
499max_write_same_blocks_store(struct device *dev, struct device_attribute *attr,
500 const char *buf, size_t count)
501{
502 struct scsi_disk *sdkp = to_scsi_disk(dev);
503 struct scsi_device *sdp = sdkp->device;
504 unsigned long max;
505 int err;
506
507 if (!capable(CAP_SYS_ADMIN))
508 return -EACCES;
509
510 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
511 return -EINVAL;
512
513 err = kstrtoul(buf, 10, &max);
514
515 if (err)
516 return err;
517
518 if (max == 0)
519 sdp->no_write_same = 1;
520 else if (max <= SD_MAX_WS16_BLOCKS) {
521 sdp->no_write_same = 0;
522 sdkp->max_ws_blocks = max;
523 }
524
525 sd_config_write_same(sdkp);
526
527 return count;
528}
529static DEVICE_ATTR_RW(max_write_same_blocks);
530
531static struct attribute *sd_disk_attrs[] = {
532 &dev_attr_cache_type.attr,
533 &dev_attr_FUA.attr,
534 &dev_attr_allow_restart.attr,
535 &dev_attr_manage_start_stop.attr,
536 &dev_attr_protection_type.attr,
537 &dev_attr_protection_mode.attr,
538 &dev_attr_app_tag_own.attr,
539 &dev_attr_thin_provisioning.attr,
540 &dev_attr_provisioning_mode.attr,
541 &dev_attr_zeroing_mode.attr,
542 &dev_attr_max_write_same_blocks.attr,
543 &dev_attr_max_medium_access_timeouts.attr,
544 NULL,
545};
546ATTRIBUTE_GROUPS(sd_disk);
547
548static struct class sd_disk_class = {
549 .name = "scsi_disk",
550 .owner = THIS_MODULE,
551 .dev_release = scsi_disk_release,
552 .dev_groups = sd_disk_groups,
553};
554
555static const struct dev_pm_ops sd_pm_ops = {
556 .suspend = sd_suspend_system,
557 .resume = sd_resume,
558 .poweroff = sd_suspend_system,
559 .restore = sd_resume,
560 .runtime_suspend = sd_suspend_runtime,
561 .runtime_resume = sd_resume,
562};
563
564static struct scsi_driver sd_template = {
565 .gendrv = {
566 .name = "sd",
567 .owner = THIS_MODULE,
568 .probe = sd_probe,
569 .remove = sd_remove,
570 .shutdown = sd_shutdown,
571 .pm = &sd_pm_ops,
572 },
573 .rescan = sd_rescan,
574 .init_command = sd_init_command,
575 .uninit_command = sd_uninit_command,
576 .done = sd_done,
577 .eh_action = sd_eh_action,
578 .eh_reset = sd_eh_reset,
579};
580
581/*
582 * Dummy kobj_map->probe function.
583 * The default ->probe function will call modprobe, which is
584 * pointless as this module is already loaded.
585 */
586static struct kobject *sd_default_probe(dev_t devt, int *partno, void *data)
587{
588 return NULL;
589}
590
591/*
592 * Device no to disk mapping:
593 *
594 * major disc2 disc p1
595 * |............|.............|....|....| <- dev_t
596 * 31 20 19 8 7 4 3 0
597 *
598 * Inside a major, we have 16k disks, however mapped non-
599 * contiguously. The first 16 disks are for major0, the next
600 * ones with major1, ... Disk 256 is for major0 again, disk 272
601 * for major1, ...
602 * As we stay compatible with our numbering scheme, we can reuse
603 * the well-know SCSI majors 8, 65--71, 136--143.
604 */
605static int sd_major(int major_idx)
606{
607 switch (major_idx) {
608 case 0:
609 return SCSI_DISK0_MAJOR;
610 case 1 ... 7:
611 return SCSI_DISK1_MAJOR + major_idx - 1;
612 case 8 ... 15:
613 return SCSI_DISK8_MAJOR + major_idx - 8;
614 default:
615 BUG();
616 return 0; /* shut up gcc */
617 }
618}
619
620static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
621{
622 struct scsi_disk *sdkp = NULL;
623
624 mutex_lock(&sd_ref_mutex);
625
626 if (disk->private_data) {
627 sdkp = scsi_disk(disk);
628 if (scsi_device_get(sdkp->device) == 0)
629 get_device(&sdkp->dev);
630 else
631 sdkp = NULL;
632 }
633 mutex_unlock(&sd_ref_mutex);
634 return sdkp;
635}
636
637static void scsi_disk_put(struct scsi_disk *sdkp)
638{
639 struct scsi_device *sdev = sdkp->device;
640
641 mutex_lock(&sd_ref_mutex);
642 put_device(&sdkp->dev);
643 scsi_device_put(sdev);
644 mutex_unlock(&sd_ref_mutex);
645}
646
647#ifdef CONFIG_BLK_SED_OPAL
648static int sd_sec_submit(void *data, u16 spsp, u8 secp, void *buffer,
649 size_t len, bool send)
650{
651 struct scsi_device *sdev = data;
652 u8 cdb[12] = { 0, };
653 int ret;
654
655 cdb[0] = send ? SECURITY_PROTOCOL_OUT : SECURITY_PROTOCOL_IN;
656 cdb[1] = secp;
657 put_unaligned_be16(spsp, &cdb[2]);
658 put_unaligned_be32(len, &cdb[6]);
659
660 ret = scsi_execute_req(sdev, cdb,
661 send ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
662 buffer, len, NULL, SD_TIMEOUT, SD_MAX_RETRIES, NULL);
663 return ret <= 0 ? ret : -EIO;
664}
665#endif /* CONFIG_BLK_SED_OPAL */
666
667static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd,
668 unsigned int dix, unsigned int dif)
669{
670 struct bio *bio = scmd->request->bio;
671 unsigned int prot_op = sd_prot_op(rq_data_dir(scmd->request), dix, dif);
672 unsigned int protect = 0;
673
674 if (dix) { /* DIX Type 0, 1, 2, 3 */
675 if (bio_integrity_flagged(bio, BIP_IP_CHECKSUM))
676 scmd->prot_flags |= SCSI_PROT_IP_CHECKSUM;
677
678 if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
679 scmd->prot_flags |= SCSI_PROT_GUARD_CHECK;
680 }
681
682 if (dif != T10_PI_TYPE3_PROTECTION) { /* DIX/DIF Type 0, 1, 2 */
683 scmd->prot_flags |= SCSI_PROT_REF_INCREMENT;
684
685 if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
686 scmd->prot_flags |= SCSI_PROT_REF_CHECK;
687 }
688
689 if (dif) { /* DIX/DIF Type 1, 2, 3 */
690 scmd->prot_flags |= SCSI_PROT_TRANSFER_PI;
691
692 if (bio_integrity_flagged(bio, BIP_DISK_NOCHECK))
693 protect = 3 << 5; /* Disable target PI checking */
694 else
695 protect = 1 << 5; /* Enable target PI checking */
696 }
697
698 scsi_set_prot_op(scmd, prot_op);
699 scsi_set_prot_type(scmd, dif);
700 scmd->prot_flags &= sd_prot_flag_mask(prot_op);
701
702 return protect;
703}
704
705static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode)
706{
707 struct request_queue *q = sdkp->disk->queue;
708 unsigned int logical_block_size = sdkp->device->sector_size;
709 unsigned int max_blocks = 0;
710
711 q->limits.discard_alignment =
712 sdkp->unmap_alignment * logical_block_size;
713 q->limits.discard_granularity =
714 max(sdkp->physical_block_size,
715 sdkp->unmap_granularity * logical_block_size);
716 sdkp->provisioning_mode = mode;
717
718 switch (mode) {
719
720 case SD_LBP_FULL:
721 case SD_LBP_DISABLE:
722 blk_queue_max_discard_sectors(q, 0);
723 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
724 return;
725
726 case SD_LBP_UNMAP:
727 max_blocks = min_not_zero(sdkp->max_unmap_blocks,
728 (u32)SD_MAX_WS16_BLOCKS);
729 break;
730
731 case SD_LBP_WS16:
732 if (sdkp->device->unmap_limit_for_ws)
733 max_blocks = sdkp->max_unmap_blocks;
734 else
735 max_blocks = sdkp->max_ws_blocks;
736
737 max_blocks = min_not_zero(max_blocks, (u32)SD_MAX_WS16_BLOCKS);
738 break;
739
740 case SD_LBP_WS10:
741 if (sdkp->device->unmap_limit_for_ws)
742 max_blocks = sdkp->max_unmap_blocks;
743 else
744 max_blocks = sdkp->max_ws_blocks;
745
746 max_blocks = min_not_zero(max_blocks, (u32)SD_MAX_WS10_BLOCKS);
747 break;
748
749 case SD_LBP_ZERO:
750 max_blocks = min_not_zero(sdkp->max_ws_blocks,
751 (u32)SD_MAX_WS10_BLOCKS);
752 break;
753 }
754
755 blk_queue_max_discard_sectors(q, max_blocks * (logical_block_size >> 9));
756 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
757}
758
759static int sd_setup_unmap_cmnd(struct scsi_cmnd *cmd)
760{
761 struct scsi_device *sdp = cmd->device;
762 struct request *rq = cmd->request;
763 u64 sector = blk_rq_pos(rq) >> (ilog2(sdp->sector_size) - 9);
764 u32 nr_sectors = blk_rq_sectors(rq) >> (ilog2(sdp->sector_size) - 9);
765 unsigned int data_len = 24;
766 char *buf;
767
768 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
769 if (!rq->special_vec.bv_page)
770 return BLKPREP_DEFER;
771 clear_highpage(rq->special_vec.bv_page);
772 rq->special_vec.bv_offset = 0;
773 rq->special_vec.bv_len = data_len;
774 rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
775
776 cmd->cmd_len = 10;
777 cmd->cmnd[0] = UNMAP;
778 cmd->cmnd[8] = 24;
779
780 buf = page_address(rq->special_vec.bv_page);
781 put_unaligned_be16(6 + 16, &buf[0]);
782 put_unaligned_be16(16, &buf[2]);
783 put_unaligned_be64(sector, &buf[8]);
784 put_unaligned_be32(nr_sectors, &buf[16]);
785
786 cmd->allowed = SD_MAX_RETRIES;
787 cmd->transfersize = data_len;
788 rq->timeout = SD_TIMEOUT;
789 scsi_req(rq)->resid_len = data_len;
790
791 return scsi_init_io(cmd);
792}
793
794static int sd_setup_write_same16_cmnd(struct scsi_cmnd *cmd, bool unmap)
795{
796 struct scsi_device *sdp = cmd->device;
797 struct request *rq = cmd->request;
798 u64 sector = blk_rq_pos(rq) >> (ilog2(sdp->sector_size) - 9);
799 u32 nr_sectors = blk_rq_sectors(rq) >> (ilog2(sdp->sector_size) - 9);
800 u32 data_len = sdp->sector_size;
801
802 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
803 if (!rq->special_vec.bv_page)
804 return BLKPREP_DEFER;
805 clear_highpage(rq->special_vec.bv_page);
806 rq->special_vec.bv_offset = 0;
807 rq->special_vec.bv_len = data_len;
808 rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
809
810 cmd->cmd_len = 16;
811 cmd->cmnd[0] = WRITE_SAME_16;
812 if (unmap)
813 cmd->cmnd[1] = 0x8; /* UNMAP */
814 put_unaligned_be64(sector, &cmd->cmnd[2]);
815 put_unaligned_be32(nr_sectors, &cmd->cmnd[10]);
816
817 cmd->allowed = SD_MAX_RETRIES;
818 cmd->transfersize = data_len;
819 rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT;
820 scsi_req(rq)->resid_len = data_len;
821
822 return scsi_init_io(cmd);
823}
824
825static int sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd, bool unmap)
826{
827 struct scsi_device *sdp = cmd->device;
828 struct request *rq = cmd->request;
829 u64 sector = blk_rq_pos(rq) >> (ilog2(sdp->sector_size) - 9);
830 u32 nr_sectors = blk_rq_sectors(rq) >> (ilog2(sdp->sector_size) - 9);
831 u32 data_len = sdp->sector_size;
832
833 rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
834 if (!rq->special_vec.bv_page)
835 return BLKPREP_DEFER;
836 clear_highpage(rq->special_vec.bv_page);
837 rq->special_vec.bv_offset = 0;
838 rq->special_vec.bv_len = data_len;
839 rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
840
841 cmd->cmd_len = 10;
842 cmd->cmnd[0] = WRITE_SAME;
843 if (unmap)
844 cmd->cmnd[1] = 0x8; /* UNMAP */
845 put_unaligned_be32(sector, &cmd->cmnd[2]);
846 put_unaligned_be16(nr_sectors, &cmd->cmnd[7]);
847
848 cmd->allowed = SD_MAX_RETRIES;
849 cmd->transfersize = data_len;
850 rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT;
851 scsi_req(rq)->resid_len = data_len;
852
853 return scsi_init_io(cmd);
854}
855
856static int sd_setup_write_zeroes_cmnd(struct scsi_cmnd *cmd)
857{
858 struct request *rq = cmd->request;
859 struct scsi_device *sdp = cmd->device;
860 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
861 u64 sector = blk_rq_pos(rq) >> (ilog2(sdp->sector_size) - 9);
862 u32 nr_sectors = blk_rq_sectors(rq) >> (ilog2(sdp->sector_size) - 9);
863
864 if (!(rq->cmd_flags & REQ_NOUNMAP)) {
865 switch (sdkp->zeroing_mode) {
866 case SD_ZERO_WS16_UNMAP:
867 return sd_setup_write_same16_cmnd(cmd, true);
868 case SD_ZERO_WS10_UNMAP:
869 return sd_setup_write_same10_cmnd(cmd, true);
870 }
871 }
872
873 if (sdp->no_write_same)
874 return BLKPREP_INVALID;
875
876 if (sdkp->ws16 || sector > 0xffffffff || nr_sectors > 0xffff)
877 return sd_setup_write_same16_cmnd(cmd, false);
878
879 return sd_setup_write_same10_cmnd(cmd, false);
880}
881
882static void sd_config_write_same(struct scsi_disk *sdkp)
883{
884 struct request_queue *q = sdkp->disk->queue;
885 unsigned int logical_block_size = sdkp->device->sector_size;
886
887 if (sdkp->device->no_write_same) {
888 sdkp->max_ws_blocks = 0;
889 goto out;
890 }
891
892 /* Some devices can not handle block counts above 0xffff despite
893 * supporting WRITE SAME(16). Consequently we default to 64k
894 * blocks per I/O unless the device explicitly advertises a
895 * bigger limit.
896 */
897 if (sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS)
898 sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
899 (u32)SD_MAX_WS16_BLOCKS);
900 else if (sdkp->ws16 || sdkp->ws10 || sdkp->device->no_report_opcodes)
901 sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
902 (u32)SD_MAX_WS10_BLOCKS);
903 else {
904 sdkp->device->no_write_same = 1;
905 sdkp->max_ws_blocks = 0;
906 }
907
908 if (sdkp->lbprz && sdkp->lbpws)
909 sdkp->zeroing_mode = SD_ZERO_WS16_UNMAP;
910 else if (sdkp->lbprz && sdkp->lbpws10)
911 sdkp->zeroing_mode = SD_ZERO_WS10_UNMAP;
912 else if (sdkp->max_ws_blocks)
913 sdkp->zeroing_mode = SD_ZERO_WS;
914 else
915 sdkp->zeroing_mode = SD_ZERO_WRITE;
916
917 if (sdkp->max_ws_blocks &&
918 sdkp->physical_block_size > logical_block_size) {
919 /*
920 * Reporting a maximum number of blocks that is not aligned
921 * on the device physical size would cause a large write same
922 * request to be split into physically unaligned chunks by
923 * __blkdev_issue_write_zeroes() and __blkdev_issue_write_same()
924 * even if the caller of these functions took care to align the
925 * large request. So make sure the maximum reported is aligned
926 * to the device physical block size. This is only an optional
927 * optimization for regular disks, but this is mandatory to
928 * avoid failure of large write same requests directed at
929 * sequential write required zones of host-managed ZBC disks.
930 */
931 sdkp->max_ws_blocks =
932 round_down(sdkp->max_ws_blocks,
933 bytes_to_logical(sdkp->device,
934 sdkp->physical_block_size));
935 }
936
937out:
938 blk_queue_max_write_same_sectors(q, sdkp->max_ws_blocks *
939 (logical_block_size >> 9));
940 blk_queue_max_write_zeroes_sectors(q, sdkp->max_ws_blocks *
941 (logical_block_size >> 9));
942}
943
944/**
945 * sd_setup_write_same_cmnd - write the same data to multiple blocks
946 * @cmd: command to prepare
947 *
948 * Will set up either WRITE SAME(10) or WRITE SAME(16) depending on
949 * the preference indicated by the target device.
950 **/
951static int sd_setup_write_same_cmnd(struct scsi_cmnd *cmd)
952{
953 struct request *rq = cmd->request;
954 struct scsi_device *sdp = cmd->device;
955 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
956 struct bio *bio = rq->bio;
957 sector_t sector = blk_rq_pos(rq);
958 unsigned int nr_sectors = blk_rq_sectors(rq);
959 unsigned int nr_bytes = blk_rq_bytes(rq);
960 int ret;
961
962 if (sdkp->device->no_write_same)
963 return BLKPREP_INVALID;
964
965 BUG_ON(bio_offset(bio) || bio_iovec(bio).bv_len != sdp->sector_size);
966
967 sector >>= ilog2(sdp->sector_size) - 9;
968 nr_sectors >>= ilog2(sdp->sector_size) - 9;
969
970 rq->timeout = SD_WRITE_SAME_TIMEOUT;
971
972 if (sdkp->ws16 || sector > 0xffffffff || nr_sectors > 0xffff) {
973 cmd->cmd_len = 16;
974 cmd->cmnd[0] = WRITE_SAME_16;
975 put_unaligned_be64(sector, &cmd->cmnd[2]);
976 put_unaligned_be32(nr_sectors, &cmd->cmnd[10]);
977 } else {
978 cmd->cmd_len = 10;
979 cmd->cmnd[0] = WRITE_SAME;
980 put_unaligned_be32(sector, &cmd->cmnd[2]);
981 put_unaligned_be16(nr_sectors, &cmd->cmnd[7]);
982 }
983
984 cmd->transfersize = sdp->sector_size;
985 cmd->allowed = SD_MAX_RETRIES;
986
987 /*
988 * For WRITE SAME the data transferred via the DATA OUT buffer is
989 * different from the amount of data actually written to the target.
990 *
991 * We set up __data_len to the amount of data transferred via the
992 * DATA OUT buffer so that blk_rq_map_sg sets up the proper S/G list
993 * to transfer a single sector of data first, but then reset it to
994 * the amount of data to be written right after so that the I/O path
995 * knows how much to actually write.
996 */
997 rq->__data_len = sdp->sector_size;
998 ret = scsi_init_io(cmd);
999 rq->__data_len = nr_bytes;
1000
1001 return ret;
1002}
1003
1004static int sd_setup_flush_cmnd(struct scsi_cmnd *cmd)
1005{
1006 struct request *rq = cmd->request;
1007
1008 /* flush requests don't perform I/O, zero the S/G table */
1009 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
1010
1011 cmd->cmnd[0] = SYNCHRONIZE_CACHE;
1012 cmd->cmd_len = 10;
1013 cmd->transfersize = 0;
1014 cmd->allowed = SD_MAX_RETRIES;
1015
1016 rq->timeout = rq->q->rq_timeout * SD_FLUSH_TIMEOUT_MULTIPLIER;
1017 return BLKPREP_OK;
1018}
1019
1020static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt)
1021{
1022 struct request *rq = SCpnt->request;
1023 struct scsi_device *sdp = SCpnt->device;
1024 struct gendisk *disk = rq->rq_disk;
1025 struct scsi_disk *sdkp = scsi_disk(disk);
1026 sector_t block = blk_rq_pos(rq);
1027 sector_t threshold;
1028 unsigned int this_count = blk_rq_sectors(rq);
1029 unsigned int dif, dix;
1030 int ret;
1031 unsigned char protect;
1032
1033 ret = scsi_init_io(SCpnt);
1034 if (ret != BLKPREP_OK)
1035 return ret;
1036 WARN_ON_ONCE(SCpnt != rq->special);
1037
1038 /* from here on until we're complete, any goto out
1039 * is used for a killable error condition */
1040 ret = BLKPREP_KILL;
1041
1042 SCSI_LOG_HLQUEUE(1,
1043 scmd_printk(KERN_INFO, SCpnt,
1044 "%s: block=%llu, count=%d\n",
1045 __func__, (unsigned long long)block, this_count));
1046
1047 if (!sdp || !scsi_device_online(sdp) ||
1048 block + blk_rq_sectors(rq) > get_capacity(disk)) {
1049 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
1050 "Finishing %u sectors\n",
1051 blk_rq_sectors(rq)));
1052 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
1053 "Retry with 0x%p\n", SCpnt));
1054 goto out;
1055 }
1056
1057 if (sdp->changed) {
1058 /*
1059 * quietly refuse to do anything to a changed disc until
1060 * the changed bit has been reset
1061 */
1062 /* printk("SCSI disk has been changed or is not present. Prohibiting further I/O.\n"); */
1063 goto out;
1064 }
1065
1066 /*
1067 * Some SD card readers can't handle multi-sector accesses which touch
1068 * the last one or two hardware sectors. Split accesses as needed.
1069 */
1070 threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
1071 (sdp->sector_size / 512);
1072
1073 if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
1074 if (block < threshold) {
1075 /* Access up to the threshold but not beyond */
1076 this_count = threshold - block;
1077 } else {
1078 /* Access only a single hardware sector */
1079 this_count = sdp->sector_size / 512;
1080 }
1081 }
1082
1083 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
1084 (unsigned long long)block));
1085
1086 /*
1087 * If we have a 1K hardware sectorsize, prevent access to single
1088 * 512 byte sectors. In theory we could handle this - in fact
1089 * the scsi cdrom driver must be able to handle this because
1090 * we typically use 1K blocksizes, and cdroms typically have
1091 * 2K hardware sectorsizes. Of course, things are simpler
1092 * with the cdrom, since it is read-only. For performance
1093 * reasons, the filesystems should be able to handle this
1094 * and not force the scsi disk driver to use bounce buffers
1095 * for this.
1096 */
1097 if (sdp->sector_size == 1024) {
1098 if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
1099 scmd_printk(KERN_ERR, SCpnt,
1100 "Bad block number requested\n");
1101 goto out;
1102 } else {
1103 block = block >> 1;
1104 this_count = this_count >> 1;
1105 }
1106 }
1107 if (sdp->sector_size == 2048) {
1108 if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
1109 scmd_printk(KERN_ERR, SCpnt,
1110 "Bad block number requested\n");
1111 goto out;
1112 } else {
1113 block = block >> 2;
1114 this_count = this_count >> 2;
1115 }
1116 }
1117 if (sdp->sector_size == 4096) {
1118 if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
1119 scmd_printk(KERN_ERR, SCpnt,
1120 "Bad block number requested\n");
1121 goto out;
1122 } else {
1123 block = block >> 3;
1124 this_count = this_count >> 3;
1125 }
1126 }
1127 if (rq_data_dir(rq) == WRITE) {
1128 SCpnt->cmnd[0] = WRITE_6;
1129
1130 if (blk_integrity_rq(rq))
1131 t10_pi_prepare(SCpnt->request, sdkp->protection_type);
1132
1133 } else if (rq_data_dir(rq) == READ) {
1134 SCpnt->cmnd[0] = READ_6;
1135 } else {
1136 scmd_printk(KERN_ERR, SCpnt, "Unknown command %d\n", req_op(rq));
1137 goto out;
1138 }
1139
1140 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
1141 "%s %d/%u 512 byte blocks.\n",
1142 (rq_data_dir(rq) == WRITE) ?
1143 "writing" : "reading", this_count,
1144 blk_rq_sectors(rq)));
1145
1146 dix = scsi_prot_sg_count(SCpnt);
1147 dif = scsi_host_dif_capable(SCpnt->device->host, sdkp->protection_type);
1148
1149 if (dif || dix)
1150 protect = sd_setup_protect_cmnd(SCpnt, dix, dif);
1151 else
1152 protect = 0;
1153
1154 if (protect && sdkp->protection_type == T10_PI_TYPE2_PROTECTION) {
1155 SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
1156
1157 if (unlikely(SCpnt->cmnd == NULL)) {
1158 ret = BLKPREP_DEFER;
1159 goto out;
1160 }
1161
1162 SCpnt->cmd_len = SD_EXT_CDB_SIZE;
1163 memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
1164 SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
1165 SCpnt->cmnd[7] = 0x18;
1166 SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
1167 SCpnt->cmnd[10] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
1168
1169 /* LBA */
1170 SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
1171 SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
1172 SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
1173 SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
1174 SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
1175 SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
1176 SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
1177 SCpnt->cmnd[19] = (unsigned char) block & 0xff;
1178
1179 /* Expected Indirect LBA */
1180 SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
1181 SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
1182 SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
1183 SCpnt->cmnd[23] = (unsigned char) block & 0xff;
1184
1185 /* Transfer length */
1186 SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
1187 SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
1188 SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
1189 SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
1190 } else if (sdp->use_16_for_rw || (this_count > 0xffff)) {
1191 SCpnt->cmnd[0] += READ_16 - READ_6;
1192 SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
1193 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
1194 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
1195 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
1196 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
1197 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
1198 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
1199 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
1200 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
1201 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
1202 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
1203 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
1204 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
1205 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
1206 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
1207 scsi_device_protection(SCpnt->device) ||
1208 SCpnt->device->use_10_for_rw) {
1209 SCpnt->cmnd[0] += READ_10 - READ_6;
1210 SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
1211 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
1212 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
1213 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
1214 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
1215 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
1216 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
1217 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
1218 } else {
1219 if (unlikely(rq->cmd_flags & REQ_FUA)) {
1220 /*
1221 * This happens only if this drive failed
1222 * 10byte rw command with ILLEGAL_REQUEST
1223 * during operation and thus turned off
1224 * use_10_for_rw.
1225 */
1226 scmd_printk(KERN_ERR, SCpnt,
1227 "FUA write on READ/WRITE(6) drive\n");
1228 goto out;
1229 }
1230
1231 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
1232 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
1233 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
1234 SCpnt->cmnd[4] = (unsigned char) this_count;
1235 SCpnt->cmnd[5] = 0;
1236 }
1237 SCpnt->sdb.length = this_count * sdp->sector_size;
1238
1239 /*
1240 * We shouldn't disconnect in the middle of a sector, so with a dumb
1241 * host adapter, it's safe to assume that we can at least transfer
1242 * this many bytes between each connect / disconnect.
1243 */
1244 SCpnt->transfersize = sdp->sector_size;
1245 SCpnt->underflow = this_count << 9;
1246 SCpnt->allowed = SD_MAX_RETRIES;
1247
1248 /*
1249 * This indicates that the command is ready from our end to be
1250 * queued.
1251 */
1252 ret = BLKPREP_OK;
1253 out:
1254 return ret;
1255}
1256
1257static int sd_init_command(struct scsi_cmnd *cmd)
1258{
1259 struct request *rq = cmd->request;
1260
1261 switch (req_op(rq)) {
1262 case REQ_OP_DISCARD:
1263 switch (scsi_disk(rq->rq_disk)->provisioning_mode) {
1264 case SD_LBP_UNMAP:
1265 return sd_setup_unmap_cmnd(cmd);
1266 case SD_LBP_WS16:
1267 return sd_setup_write_same16_cmnd(cmd, true);
1268 case SD_LBP_WS10:
1269 return sd_setup_write_same10_cmnd(cmd, true);
1270 case SD_LBP_ZERO:
1271 return sd_setup_write_same10_cmnd(cmd, false);
1272 default:
1273 return BLKPREP_INVALID;
1274 }
1275 case REQ_OP_WRITE_ZEROES:
1276 return sd_setup_write_zeroes_cmnd(cmd);
1277 case REQ_OP_WRITE_SAME:
1278 return sd_setup_write_same_cmnd(cmd);
1279 case REQ_OP_FLUSH:
1280 return sd_setup_flush_cmnd(cmd);
1281 case REQ_OP_READ:
1282 case REQ_OP_WRITE:
1283 return sd_setup_read_write_cmnd(cmd);
1284 case REQ_OP_ZONE_REPORT:
1285 return sd_zbc_setup_report_cmnd(cmd);
1286 case REQ_OP_ZONE_RESET:
1287 return sd_zbc_setup_reset_cmnd(cmd);
1288 default:
1289 WARN_ON_ONCE(1);
1290 return BLKPREP_KILL;
1291 }
1292}
1293
1294static void sd_uninit_command(struct scsi_cmnd *SCpnt)
1295{
1296 struct request *rq = SCpnt->request;
1297 u8 *cmnd;
1298
1299 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
1300 mempool_free(rq->special_vec.bv_page, sd_page_pool);
1301
1302 if (SCpnt->cmnd != scsi_req(rq)->cmd) {
1303 cmnd = SCpnt->cmnd;
1304 SCpnt->cmnd = NULL;
1305 SCpnt->cmd_len = 0;
1306 mempool_free(cmnd, sd_cdb_pool);
1307 }
1308}
1309
1310/**
1311 * sd_open - open a scsi disk device
1312 * @bdev: Block device of the scsi disk to open
1313 * @mode: FMODE_* mask
1314 *
1315 * Returns 0 if successful. Returns a negated errno value in case
1316 * of error.
1317 *
1318 * Note: This can be called from a user context (e.g. fsck(1) )
1319 * or from within the kernel (e.g. as a result of a mount(1) ).
1320 * In the latter case @inode and @filp carry an abridged amount
1321 * of information as noted above.
1322 *
1323 * Locking: called with bdev->bd_mutex held.
1324 **/
1325static int sd_open(struct block_device *bdev, fmode_t mode)
1326{
1327 struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
1328 struct scsi_device *sdev;
1329 int retval;
1330
1331 if (!sdkp)
1332 return -ENXIO;
1333
1334 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
1335
1336 sdev = sdkp->device;
1337
1338 /*
1339 * If the device is in error recovery, wait until it is done.
1340 * If the device is offline, then disallow any access to it.
1341 */
1342 retval = -ENXIO;
1343 if (!scsi_block_when_processing_errors(sdev))
1344 goto error_out;
1345
1346 if (sdev->removable || sdkp->write_prot)
1347 check_disk_change(bdev);
1348
1349 /*
1350 * If the drive is empty, just let the open fail.
1351 */
1352 retval = -ENOMEDIUM;
1353 if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
1354 goto error_out;
1355
1356 /*
1357 * If the device has the write protect tab set, have the open fail
1358 * if the user expects to be able to write to the thing.
1359 */
1360 retval = -EROFS;
1361 if (sdkp->write_prot && (mode & FMODE_WRITE))
1362 goto error_out;
1363
1364 /*
1365 * It is possible that the disk changing stuff resulted in
1366 * the device being taken offline. If this is the case,
1367 * report this to the user, and don't pretend that the
1368 * open actually succeeded.
1369 */
1370 retval = -ENXIO;
1371 if (!scsi_device_online(sdev))
1372 goto error_out;
1373
1374 if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) {
1375 if (scsi_block_when_processing_errors(sdev))
1376 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
1377 }
1378
1379 return 0;
1380
1381error_out:
1382 scsi_disk_put(sdkp);
1383 return retval;
1384}
1385
1386/**
1387 * sd_release - invoked when the (last) close(2) is called on this
1388 * scsi disk.
1389 * @disk: disk to release
1390 * @mode: FMODE_* mask
1391 *
1392 * Returns 0.
1393 *
1394 * Note: may block (uninterruptible) if error recovery is underway
1395 * on this disk.
1396 *
1397 * Locking: called with bdev->bd_mutex held.
1398 **/
1399static void sd_release(struct gendisk *disk, fmode_t mode)
1400{
1401 struct scsi_disk *sdkp = scsi_disk(disk);
1402 struct scsi_device *sdev = sdkp->device;
1403
1404 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
1405
1406 if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) {
1407 if (scsi_block_when_processing_errors(sdev))
1408 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
1409 }
1410
1411 scsi_disk_put(sdkp);
1412}
1413
1414static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1415{
1416 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
1417 struct scsi_device *sdp = sdkp->device;
1418 struct Scsi_Host *host = sdp->host;
1419 sector_t capacity = logical_to_sectors(sdp, sdkp->capacity);
1420 int diskinfo[4];
1421
1422 /* default to most commonly used values */
1423 diskinfo[0] = 0x40; /* 1 << 6 */
1424 diskinfo[1] = 0x20; /* 1 << 5 */
1425 diskinfo[2] = capacity >> 11;
1426
1427 /* override with calculated, extended default, or driver values */
1428 if (host->hostt->bios_param)
1429 host->hostt->bios_param(sdp, bdev, capacity, diskinfo);
1430 else
1431 scsicam_bios_param(bdev, capacity, diskinfo);
1432
1433 geo->heads = diskinfo[0];
1434 geo->sectors = diskinfo[1];
1435 geo->cylinders = diskinfo[2];
1436 return 0;
1437}
1438
1439/**
1440 * sd_ioctl - process an ioctl
1441 * @bdev: target block device
1442 * @mode: FMODE_* mask
1443 * @cmd: ioctl command number
1444 * @arg: this is third argument given to ioctl(2) system call.
1445 * Often contains a pointer.
1446 *
1447 * Returns 0 if successful (some ioctls return positive numbers on
1448 * success as well). Returns a negated errno value in case of error.
1449 *
1450 * Note: most ioctls are forward onto the block subsystem or further
1451 * down in the scsi subsystem.
1452 **/
1453static int sd_ioctl(struct block_device *bdev, fmode_t mode,
1454 unsigned int cmd, unsigned long arg)
1455{
1456 struct gendisk *disk = bdev->bd_disk;
1457 struct scsi_disk *sdkp = scsi_disk(disk);
1458 struct scsi_device *sdp = sdkp->device;
1459 void __user *p = (void __user *)arg;
1460 int error;
1461
1462 SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, "
1463 "cmd=0x%x\n", disk->disk_name, cmd));
1464
1465 error = scsi_verify_blk_ioctl(bdev, cmd);
1466 if (error < 0)
1467 return error;
1468
1469 /*
1470 * If we are in the middle of error recovery, don't let anyone
1471 * else try and use this device. Also, if error recovery fails, it
1472 * may try and take the device offline, in which case all further
1473 * access to the device is prohibited.
1474 */
1475 error = scsi_ioctl_block_when_processing_errors(sdp, cmd,
1476 (mode & FMODE_NDELAY) != 0);
1477 if (error)
1478 goto out;
1479
1480 if (is_sed_ioctl(cmd))
1481 return sed_ioctl(sdkp->opal_dev, cmd, p);
1482
1483 /*
1484 * Send SCSI addressing ioctls directly to mid level, send other
1485 * ioctls to block level and then onto mid level if they can't be
1486 * resolved.
1487 */
1488 switch (cmd) {
1489 case SCSI_IOCTL_GET_IDLUN:
1490 case SCSI_IOCTL_GET_BUS_NUMBER:
1491 error = scsi_ioctl(sdp, cmd, p);
1492 break;
1493 default:
1494 error = scsi_cmd_blk_ioctl(bdev, mode, cmd, p);
1495 if (error != -ENOTTY)
1496 break;
1497 error = scsi_ioctl(sdp, cmd, p);
1498 break;
1499 }
1500out:
1501 return error;
1502}
1503
1504static void set_media_not_present(struct scsi_disk *sdkp)
1505{
1506 if (sdkp->media_present)
1507 sdkp->device->changed = 1;
1508
1509 if (sdkp->device->removable) {
1510 sdkp->media_present = 0;
1511 sdkp->capacity = 0;
1512 }
1513}
1514
1515static int media_not_present(struct scsi_disk *sdkp,
1516 struct scsi_sense_hdr *sshdr)
1517{
1518 if (!scsi_sense_valid(sshdr))
1519 return 0;
1520
1521 /* not invoked for commands that could return deferred errors */
1522 switch (sshdr->sense_key) {
1523 case UNIT_ATTENTION:
1524 case NOT_READY:
1525 /* medium not present */
1526 if (sshdr->asc == 0x3A) {
1527 set_media_not_present(sdkp);
1528 return 1;
1529 }
1530 }
1531 return 0;
1532}
1533
1534/**
1535 * sd_check_events - check media events
1536 * @disk: kernel device descriptor
1537 * @clearing: disk events currently being cleared
1538 *
1539 * Returns mask of DISK_EVENT_*.
1540 *
1541 * Note: this function is invoked from the block subsystem.
1542 **/
1543static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing)
1544{
1545 struct scsi_disk *sdkp = scsi_disk_get(disk);
1546 struct scsi_device *sdp;
1547 int retval;
1548
1549 if (!sdkp)
1550 return 0;
1551
1552 sdp = sdkp->device;
1553 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_check_events\n"));
1554
1555 /*
1556 * If the device is offline, don't send any commands - just pretend as
1557 * if the command failed. If the device ever comes back online, we
1558 * can deal with it then. It is only because of unrecoverable errors
1559 * that we would ever take a device offline in the first place.
1560 */
1561 if (!scsi_device_online(sdp)) {
1562 set_media_not_present(sdkp);
1563 goto out;
1564 }
1565
1566 /*
1567 * Using TEST_UNIT_READY enables differentiation between drive with
1568 * no cartridge loaded - NOT READY, drive with changed cartridge -
1569 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
1570 *
1571 * Drives that auto spin down. eg iomega jaz 1G, will be started
1572 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
1573 * sd_revalidate() is called.
1574 */
1575 if (scsi_block_when_processing_errors(sdp)) {
1576 struct scsi_sense_hdr sshdr = { 0, };
1577
1578 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
1579 &sshdr);
1580
1581 /* failed to execute TUR, assume media not present */
1582 if (host_byte(retval)) {
1583 set_media_not_present(sdkp);
1584 goto out;
1585 }
1586
1587 if (media_not_present(sdkp, &sshdr))
1588 goto out;
1589 }
1590
1591 /*
1592 * For removable scsi disk we have to recognise the presence
1593 * of a disk in the drive.
1594 */
1595 if (!sdkp->media_present)
1596 sdp->changed = 1;
1597 sdkp->media_present = 1;
1598out:
1599 /*
1600 * sdp->changed is set under the following conditions:
1601 *
1602 * Medium present state has changed in either direction.
1603 * Device has indicated UNIT_ATTENTION.
1604 */
1605 retval = sdp->changed ? DISK_EVENT_MEDIA_CHANGE : 0;
1606 sdp->changed = 0;
1607 scsi_disk_put(sdkp);
1608 return retval;
1609}
1610
1611static int sd_sync_cache(struct scsi_disk *sdkp, struct scsi_sense_hdr *sshdr)
1612{
1613 int retries, res;
1614 struct scsi_device *sdp = sdkp->device;
1615 const int timeout = sdp->request_queue->rq_timeout
1616 * SD_FLUSH_TIMEOUT_MULTIPLIER;
1617 struct scsi_sense_hdr my_sshdr;
1618
1619 if (!scsi_device_online(sdp))
1620 return -ENODEV;
1621
1622 /* caller might not be interested in sense, but we need it */
1623 if (!sshdr)
1624 sshdr = &my_sshdr;
1625
1626 for (retries = 3; retries > 0; --retries) {
1627 unsigned char cmd[10] = { 0 };
1628
1629 cmd[0] = SYNCHRONIZE_CACHE;
1630 /*
1631 * Leave the rest of the command zero to indicate
1632 * flush everything.
1633 */
1634 res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, sshdr,
1635 timeout, SD_MAX_RETRIES, 0, RQF_PM, NULL);
1636 if (res == 0)
1637 break;
1638 }
1639
1640 if (res) {
1641 sd_print_result(sdkp, "Synchronize Cache(10) failed", res);
1642
1643 if (driver_byte(res) == DRIVER_SENSE)
1644 sd_print_sense_hdr(sdkp, sshdr);
1645
1646 /* we need to evaluate the error return */
1647 if (scsi_sense_valid(sshdr) &&
1648 (sshdr->asc == 0x3a || /* medium not present */
1649 sshdr->asc == 0x20 || /* invalid command */
1650 (sshdr->asc == 0x74 && sshdr->ascq == 0x71))) /* drive is password locked */
1651 /* this is no error here */
1652 return 0;
1653
1654 switch (host_byte(res)) {
1655 /* ignore errors due to racing a disconnection */
1656 case DID_BAD_TARGET:
1657 case DID_NO_CONNECT:
1658 return 0;
1659 /* signal the upper layer it might try again */
1660 case DID_BUS_BUSY:
1661 case DID_IMM_RETRY:
1662 case DID_REQUEUE:
1663 case DID_SOFT_ERROR:
1664 return -EBUSY;
1665 default:
1666 return -EIO;
1667 }
1668 }
1669 return 0;
1670}
1671
1672static void sd_rescan(struct device *dev)
1673{
1674 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1675
1676 revalidate_disk(sdkp->disk);
1677}
1678
1679
1680#ifdef CONFIG_COMPAT
1681/*
1682 * This gets directly called from VFS. When the ioctl
1683 * is not recognized we go back to the other translation paths.
1684 */
1685static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
1686 unsigned int cmd, unsigned long arg)
1687{
1688 struct gendisk *disk = bdev->bd_disk;
1689 struct scsi_disk *sdkp = scsi_disk(disk);
1690 struct scsi_device *sdev = sdkp->device;
1691 void __user *p = compat_ptr(arg);
1692 int error;
1693
1694 error = scsi_verify_blk_ioctl(bdev, cmd);
1695 if (error < 0)
1696 return error;
1697
1698 error = scsi_ioctl_block_when_processing_errors(sdev, cmd,
1699 (mode & FMODE_NDELAY) != 0);
1700 if (error)
1701 return error;
1702
1703 if (is_sed_ioctl(cmd))
1704 return sed_ioctl(sdkp->opal_dev, cmd, p);
1705
1706 /*
1707 * Let the static ioctl translation table take care of it.
1708 */
1709 if (!sdev->host->hostt->compat_ioctl)
1710 return -ENOIOCTLCMD;
1711 return sdev->host->hostt->compat_ioctl(sdev, cmd, p);
1712}
1713#endif
1714
1715static char sd_pr_type(enum pr_type type)
1716{
1717 switch (type) {
1718 case PR_WRITE_EXCLUSIVE:
1719 return 0x01;
1720 case PR_EXCLUSIVE_ACCESS:
1721 return 0x03;
1722 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1723 return 0x05;
1724 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1725 return 0x06;
1726 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1727 return 0x07;
1728 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1729 return 0x08;
1730 default:
1731 return 0;
1732 }
1733};
1734
1735static int sd_pr_command(struct block_device *bdev, u8 sa,
1736 u64 key, u64 sa_key, u8 type, u8 flags)
1737{
1738 struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
1739 struct scsi_sense_hdr sshdr;
1740 int result;
1741 u8 cmd[16] = { 0, };
1742 u8 data[24] = { 0, };
1743
1744 cmd[0] = PERSISTENT_RESERVE_OUT;
1745 cmd[1] = sa;
1746 cmd[2] = type;
1747 put_unaligned_be32(sizeof(data), &cmd[5]);
1748
1749 put_unaligned_be64(key, &data[0]);
1750 put_unaligned_be64(sa_key, &data[8]);
1751 data[20] = flags;
1752
1753 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, &data, sizeof(data),
1754 &sshdr, SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1755
1756 if (driver_byte(result) == DRIVER_SENSE &&
1757 scsi_sense_valid(&sshdr)) {
1758 sdev_printk(KERN_INFO, sdev, "PR command failed: %d\n", result);
1759 scsi_print_sense_hdr(sdev, NULL, &sshdr);
1760 }
1761
1762 return result;
1763}
1764
1765static int sd_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
1766 u32 flags)
1767{
1768 if (flags & ~PR_FL_IGNORE_KEY)
1769 return -EOPNOTSUPP;
1770 return sd_pr_command(bdev, (flags & PR_FL_IGNORE_KEY) ? 0x06 : 0x00,
1771 old_key, new_key, 0,
1772 (1 << 0) /* APTPL */);
1773}
1774
1775static int sd_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
1776 u32 flags)
1777{
1778 if (flags)
1779 return -EOPNOTSUPP;
1780 return sd_pr_command(bdev, 0x01, key, 0, sd_pr_type(type), 0);
1781}
1782
1783static int sd_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1784{
1785 return sd_pr_command(bdev, 0x02, key, 0, sd_pr_type(type), 0);
1786}
1787
1788static int sd_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
1789 enum pr_type type, bool abort)
1790{
1791 return sd_pr_command(bdev, abort ? 0x05 : 0x04, old_key, new_key,
1792 sd_pr_type(type), 0);
1793}
1794
1795static int sd_pr_clear(struct block_device *bdev, u64 key)
1796{
1797 return sd_pr_command(bdev, 0x03, key, 0, 0, 0);
1798}
1799
1800static const struct pr_ops sd_pr_ops = {
1801 .pr_register = sd_pr_register,
1802 .pr_reserve = sd_pr_reserve,
1803 .pr_release = sd_pr_release,
1804 .pr_preempt = sd_pr_preempt,
1805 .pr_clear = sd_pr_clear,
1806};
1807
1808static const struct block_device_operations sd_fops = {
1809 .owner = THIS_MODULE,
1810 .open = sd_open,
1811 .release = sd_release,
1812 .ioctl = sd_ioctl,
1813 .getgeo = sd_getgeo,
1814#ifdef CONFIG_COMPAT
1815 .compat_ioctl = sd_compat_ioctl,
1816#endif
1817 .check_events = sd_check_events,
1818 .revalidate_disk = sd_revalidate_disk,
1819 .unlock_native_capacity = sd_unlock_native_capacity,
1820 .pr_ops = &sd_pr_ops,
1821};
1822
1823/**
1824 * sd_eh_reset - reset error handling callback
1825 * @scmd: sd-issued command that has failed
1826 *
1827 * This function is called by the SCSI midlayer before starting
1828 * SCSI EH. When counting medium access failures we have to be
1829 * careful to register it only only once per device and SCSI EH run;
1830 * there might be several timed out commands which will cause the
1831 * 'max_medium_access_timeouts' counter to trigger after the first
1832 * SCSI EH run already and set the device to offline.
1833 * So this function resets the internal counter before starting SCSI EH.
1834 **/
1835static void sd_eh_reset(struct scsi_cmnd *scmd)
1836{
1837 struct scsi_disk *sdkp = scsi_disk(scmd->request->rq_disk);
1838
1839 /* New SCSI EH run, reset gate variable */
1840 sdkp->ignore_medium_access_errors = false;
1841}
1842
1843/**
1844 * sd_eh_action - error handling callback
1845 * @scmd: sd-issued command that has failed
1846 * @eh_disp: The recovery disposition suggested by the midlayer
1847 *
1848 * This function is called by the SCSI midlayer upon completion of an
1849 * error test command (currently TEST UNIT READY). The result of sending
1850 * the eh command is passed in eh_disp. We're looking for devices that
1851 * fail medium access commands but are OK with non access commands like
1852 * test unit ready (so wrongly see the device as having a successful
1853 * recovery)
1854 **/
1855static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp)
1856{
1857 struct scsi_disk *sdkp = scsi_disk(scmd->request->rq_disk);
1858 struct scsi_device *sdev = scmd->device;
1859
1860 if (!scsi_device_online(sdev) ||
1861 !scsi_medium_access_command(scmd) ||
1862 host_byte(scmd->result) != DID_TIME_OUT ||
1863 eh_disp != SUCCESS)
1864 return eh_disp;
1865
1866 /*
1867 * The device has timed out executing a medium access command.
1868 * However, the TEST UNIT READY command sent during error
1869 * handling completed successfully. Either the device is in the
1870 * process of recovering or has it suffered an internal failure
1871 * that prevents access to the storage medium.
1872 */
1873 if (!sdkp->ignore_medium_access_errors) {
1874 sdkp->medium_access_timed_out++;
1875 sdkp->ignore_medium_access_errors = true;
1876 }
1877
1878 /*
1879 * If the device keeps failing read/write commands but TEST UNIT
1880 * READY always completes successfully we assume that medium
1881 * access is no longer possible and take the device offline.
1882 */
1883 if (sdkp->medium_access_timed_out >= sdkp->max_medium_access_timeouts) {
1884 scmd_printk(KERN_ERR, scmd,
1885 "Medium access timeout failure. Offlining disk!\n");
1886 mutex_lock(&sdev->state_mutex);
1887 scsi_device_set_state(sdev, SDEV_OFFLINE);
1888 mutex_unlock(&sdev->state_mutex);
1889
1890 return SUCCESS;
1891 }
1892
1893 return eh_disp;
1894}
1895
1896static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1897{
1898 struct request *req = scmd->request;
1899 struct scsi_device *sdev = scmd->device;
1900 unsigned int transferred, good_bytes;
1901 u64 start_lba, end_lba, bad_lba;
1902
1903 /*
1904 * Some commands have a payload smaller than the device logical
1905 * block size (e.g. INQUIRY on a 4K disk).
1906 */
1907 if (scsi_bufflen(scmd) <= sdev->sector_size)
1908 return 0;
1909
1910 /* Check if we have a 'bad_lba' information */
1911 if (!scsi_get_sense_info_fld(scmd->sense_buffer,
1912 SCSI_SENSE_BUFFERSIZE,
1913 &bad_lba))
1914 return 0;
1915
1916 /*
1917 * If the bad lba was reported incorrectly, we have no idea where
1918 * the error is.
1919 */
1920 start_lba = sectors_to_logical(sdev, blk_rq_pos(req));
1921 end_lba = start_lba + bytes_to_logical(sdev, scsi_bufflen(scmd));
1922 if (bad_lba < start_lba || bad_lba >= end_lba)
1923 return 0;
1924
1925 /*
1926 * resid is optional but mostly filled in. When it's unused,
1927 * its value is zero, so we assume the whole buffer transferred
1928 */
1929 transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd);
1930
1931 /* This computation should always be done in terms of the
1932 * resolution of the device's medium.
1933 */
1934 good_bytes = logical_to_bytes(sdev, bad_lba - start_lba);
1935
1936 return min(good_bytes, transferred);
1937}
1938
1939/**
1940 * sd_done - bottom half handler: called when the lower level
1941 * driver has completed (successfully or otherwise) a scsi command.
1942 * @SCpnt: mid-level's per command structure.
1943 *
1944 * Note: potentially run from within an ISR. Must not block.
1945 **/
1946static int sd_done(struct scsi_cmnd *SCpnt)
1947{
1948 int result = SCpnt->result;
1949 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
1950 unsigned int sector_size = SCpnt->device->sector_size;
1951 unsigned int resid;
1952 struct scsi_sense_hdr sshdr;
1953 struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
1954 struct request *req = SCpnt->request;
1955 int sense_valid = 0;
1956 int sense_deferred = 0;
1957
1958 switch (req_op(req)) {
1959 case REQ_OP_DISCARD:
1960 case REQ_OP_WRITE_ZEROES:
1961 case REQ_OP_WRITE_SAME:
1962 case REQ_OP_ZONE_RESET:
1963 if (!result) {
1964 good_bytes = blk_rq_bytes(req);
1965 scsi_set_resid(SCpnt, 0);
1966 } else {
1967 good_bytes = 0;
1968 scsi_set_resid(SCpnt, blk_rq_bytes(req));
1969 }
1970 break;
1971 case REQ_OP_ZONE_REPORT:
1972 if (!result) {
1973 good_bytes = scsi_bufflen(SCpnt)
1974 - scsi_get_resid(SCpnt);
1975 scsi_set_resid(SCpnt, 0);
1976 } else {
1977 good_bytes = 0;
1978 scsi_set_resid(SCpnt, blk_rq_bytes(req));
1979 }
1980 break;
1981 default:
1982 /*
1983 * In case of bogus fw or device, we could end up having
1984 * an unaligned partial completion. Check this here and force
1985 * alignment.
1986 */
1987 resid = scsi_get_resid(SCpnt);
1988 if (resid & (sector_size - 1)) {
1989 sd_printk(KERN_INFO, sdkp,
1990 "Unaligned partial completion (resid=%u, sector_sz=%u)\n",
1991 resid, sector_size);
1992 resid = min(scsi_bufflen(SCpnt),
1993 round_up(resid, sector_size));
1994 scsi_set_resid(SCpnt, resid);
1995 }
1996 }
1997
1998 if (result) {
1999 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
2000 if (sense_valid)
2001 sense_deferred = scsi_sense_is_deferred(&sshdr);
2002 }
2003 sdkp->medium_access_timed_out = 0;
2004
2005 if (driver_byte(result) != DRIVER_SENSE &&
2006 (!sense_valid || sense_deferred))
2007 goto out;
2008
2009 switch (sshdr.sense_key) {
2010 case HARDWARE_ERROR:
2011 case MEDIUM_ERROR:
2012 good_bytes = sd_completed_bytes(SCpnt);
2013 break;
2014 case RECOVERED_ERROR:
2015 good_bytes = scsi_bufflen(SCpnt);
2016 break;
2017 case NO_SENSE:
2018 /* This indicates a false check condition, so ignore it. An
2019 * unknown amount of data was transferred so treat it as an
2020 * error.
2021 */
2022 SCpnt->result = 0;
2023 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2024 break;
2025 case ABORTED_COMMAND:
2026 if (sshdr.asc == 0x10) /* DIF: Target detected corruption */
2027 good_bytes = sd_completed_bytes(SCpnt);
2028 break;
2029 case ILLEGAL_REQUEST:
2030 switch (sshdr.asc) {
2031 case 0x10: /* DIX: Host detected corruption */
2032 good_bytes = sd_completed_bytes(SCpnt);
2033 break;
2034 case 0x20: /* INVALID COMMAND OPCODE */
2035 case 0x24: /* INVALID FIELD IN CDB */
2036 switch (SCpnt->cmnd[0]) {
2037 case UNMAP:
2038 sd_config_discard(sdkp, SD_LBP_DISABLE);
2039 break;
2040 case WRITE_SAME_16:
2041 case WRITE_SAME:
2042 if (SCpnt->cmnd[1] & 8) { /* UNMAP */
2043 sd_config_discard(sdkp, SD_LBP_DISABLE);
2044 } else {
2045 sdkp->device->no_write_same = 1;
2046 sd_config_write_same(sdkp);
2047 req->rq_flags |= RQF_QUIET;
2048 }
2049 break;
2050 }
2051 }
2052 break;
2053 default:
2054 break;
2055 }
2056
2057 out:
2058 if (sd_is_zoned(sdkp))
2059 sd_zbc_complete(SCpnt, good_bytes, &sshdr);
2060
2061 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
2062 "sd_done: completed %d of %d bytes\n",
2063 good_bytes, scsi_bufflen(SCpnt)));
2064
2065 if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt) &&
2066 good_bytes)
2067 t10_pi_complete(SCpnt->request, sdkp->protection_type,
2068 good_bytes / scsi_prot_interval(SCpnt));
2069
2070 return good_bytes;
2071}
2072
2073/*
2074 * spinup disk - called only in sd_revalidate_disk()
2075 */
2076static void
2077sd_spinup_disk(struct scsi_disk *sdkp)
2078{
2079 unsigned char cmd[10];
2080 unsigned long spintime_expire = 0;
2081 int retries, spintime;
2082 unsigned int the_result;
2083 struct scsi_sense_hdr sshdr;
2084 int sense_valid = 0;
2085
2086 spintime = 0;
2087
2088 /* Spin up drives, as required. Only do this at boot time */
2089 /* Spinup needs to be done for module loads too. */
2090 do {
2091 retries = 0;
2092
2093 do {
2094 cmd[0] = TEST_UNIT_READY;
2095 memset((void *) &cmd[1], 0, 9);
2096
2097 the_result = scsi_execute_req(sdkp->device, cmd,
2098 DMA_NONE, NULL, 0,
2099 &sshdr, SD_TIMEOUT,
2100 SD_MAX_RETRIES, NULL);
2101
2102 /*
2103 * If the drive has indicated to us that it
2104 * doesn't have any media in it, don't bother
2105 * with any more polling.
2106 */
2107 if (media_not_present(sdkp, &sshdr))
2108 return;
2109
2110 if (the_result)
2111 sense_valid = scsi_sense_valid(&sshdr);
2112 retries++;
2113 } while (retries < 3 &&
2114 (!scsi_status_is_good(the_result) ||
2115 ((driver_byte(the_result) == DRIVER_SENSE) &&
2116 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
2117
2118 if (driver_byte(the_result) != DRIVER_SENSE) {
2119 /* no sense, TUR either succeeded or failed
2120 * with a status error */
2121 if(!spintime && !scsi_status_is_good(the_result)) {
2122 sd_print_result(sdkp, "Test Unit Ready failed",
2123 the_result);
2124 }
2125 break;
2126 }
2127
2128 /*
2129 * The device does not want the automatic start to be issued.
2130 */
2131 if (sdkp->device->no_start_on_add)
2132 break;
2133
2134 if (sense_valid && sshdr.sense_key == NOT_READY) {
2135 if (sshdr.asc == 4 && sshdr.ascq == 3)
2136 break; /* manual intervention required */
2137 if (sshdr.asc == 4 && sshdr.ascq == 0xb)
2138 break; /* standby */
2139 if (sshdr.asc == 4 && sshdr.ascq == 0xc)
2140 break; /* unavailable */
2141 if (sshdr.asc == 4 && sshdr.ascq == 0x1b)
2142 break; /* sanitize in progress */
2143 /*
2144 * Issue command to spin up drive when not ready
2145 */
2146 if (!spintime) {
2147 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
2148 cmd[0] = START_STOP;
2149 cmd[1] = 1; /* Return immediately */
2150 memset((void *) &cmd[2], 0, 8);
2151 cmd[4] = 1; /* Start spin cycle */
2152 if (sdkp->device->start_stop_pwr_cond)
2153 cmd[4] |= 1 << 4;
2154 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
2155 NULL, 0, &sshdr,
2156 SD_TIMEOUT, SD_MAX_RETRIES,
2157 NULL);
2158 spintime_expire = jiffies + 100 * HZ;
2159 spintime = 1;
2160 }
2161 /* Wait 1 second for next try */
2162 msleep(1000);
2163 printk(KERN_CONT ".");
2164
2165 /*
2166 * Wait for USB flash devices with slow firmware.
2167 * Yes, this sense key/ASC combination shouldn't
2168 * occur here. It's characteristic of these devices.
2169 */
2170 } else if (sense_valid &&
2171 sshdr.sense_key == UNIT_ATTENTION &&
2172 sshdr.asc == 0x28) {
2173 if (!spintime) {
2174 spintime_expire = jiffies + 5 * HZ;
2175 spintime = 1;
2176 }
2177 /* Wait 1 second for next try */
2178 msleep(1000);
2179 } else {
2180 /* we don't understand the sense code, so it's
2181 * probably pointless to loop */
2182 if(!spintime) {
2183 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
2184 sd_print_sense_hdr(sdkp, &sshdr);
2185 }
2186 break;
2187 }
2188
2189 } while (spintime && time_before_eq(jiffies, spintime_expire));
2190
2191 if (spintime) {
2192 if (scsi_status_is_good(the_result))
2193 printk(KERN_CONT "ready\n");
2194 else
2195 printk(KERN_CONT "not responding...\n");
2196 }
2197}
2198
2199/*
2200 * Determine whether disk supports Data Integrity Field.
2201 */
2202static int sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
2203{
2204 struct scsi_device *sdp = sdkp->device;
2205 u8 type;
2206 int ret = 0;
2207
2208 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0) {
2209 sdkp->protection_type = 0;
2210 return ret;
2211 }
2212
2213 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
2214
2215 if (type > T10_PI_TYPE3_PROTECTION)
2216 ret = -ENODEV;
2217 else if (scsi_host_dif_capable(sdp->host, type))
2218 ret = 1;
2219
2220 if (sdkp->first_scan || type != sdkp->protection_type)
2221 switch (ret) {
2222 case -ENODEV:
2223 sd_printk(KERN_ERR, sdkp, "formatted with unsupported" \
2224 " protection type %u. Disabling disk!\n",
2225 type);
2226 break;
2227 case 1:
2228 sd_printk(KERN_NOTICE, sdkp,
2229 "Enabling DIF Type %u protection\n", type);
2230 break;
2231 case 0:
2232 sd_printk(KERN_NOTICE, sdkp,
2233 "Disabling DIF Type %u protection\n", type);
2234 break;
2235 }
2236
2237 sdkp->protection_type = type;
2238
2239 return ret;
2240}
2241
2242static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
2243 struct scsi_sense_hdr *sshdr, int sense_valid,
2244 int the_result)
2245{
2246 if (driver_byte(the_result) == DRIVER_SENSE)
2247 sd_print_sense_hdr(sdkp, sshdr);
2248 else
2249 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
2250
2251 /*
2252 * Set dirty bit for removable devices if not ready -
2253 * sometimes drives will not report this properly.
2254 */
2255 if (sdp->removable &&
2256 sense_valid && sshdr->sense_key == NOT_READY)
2257 set_media_not_present(sdkp);
2258
2259 /*
2260 * We used to set media_present to 0 here to indicate no media
2261 * in the drive, but some drives fail read capacity even with
2262 * media present, so we can't do that.
2263 */
2264 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
2265}
2266
2267#define RC16_LEN 32
2268#if RC16_LEN > SD_BUF_SIZE
2269#error RC16_LEN must not be more than SD_BUF_SIZE
2270#endif
2271
2272#define READ_CAPACITY_RETRIES_ON_RESET 10
2273
2274/*
2275 * Ensure that we don't overflow sector_t when CONFIG_LBDAF is not set
2276 * and the reported logical block size is bigger than 512 bytes. Note
2277 * that last_sector is a u64 and therefore logical_to_sectors() is not
2278 * applicable.
2279 */
2280static bool sd_addressable_capacity(u64 lba, unsigned int sector_size)
2281{
2282 u64 last_sector = (lba + 1ULL) << (ilog2(sector_size) - 9);
2283
2284 if (sizeof(sector_t) == 4 && last_sector > U32_MAX)
2285 return false;
2286
2287 return true;
2288}
2289
2290static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
2291 unsigned char *buffer)
2292{
2293 unsigned char cmd[16];
2294 struct scsi_sense_hdr sshdr;
2295 int sense_valid = 0;
2296 int the_result;
2297 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
2298 unsigned int alignment;
2299 unsigned long long lba;
2300 unsigned sector_size;
2301
2302 if (sdp->no_read_capacity_16)
2303 return -EINVAL;
2304
2305 do {
2306 memset(cmd, 0, 16);
2307 cmd[0] = SERVICE_ACTION_IN_16;
2308 cmd[1] = SAI_READ_CAPACITY_16;
2309 cmd[13] = RC16_LEN;
2310 memset(buffer, 0, RC16_LEN);
2311
2312 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
2313 buffer, RC16_LEN, &sshdr,
2314 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
2315
2316 if (media_not_present(sdkp, &sshdr))
2317 return -ENODEV;
2318
2319 if (the_result) {
2320 sense_valid = scsi_sense_valid(&sshdr);
2321 if (sense_valid &&
2322 sshdr.sense_key == ILLEGAL_REQUEST &&
2323 (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
2324 sshdr.ascq == 0x00)
2325 /* Invalid Command Operation Code or
2326 * Invalid Field in CDB, just retry
2327 * silently with RC10 */
2328 return -EINVAL;
2329 if (sense_valid &&
2330 sshdr.sense_key == UNIT_ATTENTION &&
2331 sshdr.asc == 0x29 && sshdr.ascq == 0x00)
2332 /* Device reset might occur several times,
2333 * give it one more chance */
2334 if (--reset_retries > 0)
2335 continue;
2336 }
2337 retries--;
2338
2339 } while (the_result && retries);
2340
2341 if (the_result) {
2342 sd_print_result(sdkp, "Read Capacity(16) failed", the_result);
2343 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
2344 return -EINVAL;
2345 }
2346
2347 sector_size = get_unaligned_be32(&buffer[8]);
2348 lba = get_unaligned_be64(&buffer[0]);
2349
2350 if (sd_read_protection_type(sdkp, buffer) < 0) {
2351 sdkp->capacity = 0;
2352 return -ENODEV;
2353 }
2354
2355 if (!sd_addressable_capacity(lba, sector_size)) {
2356 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
2357 "kernel compiled with support for large block "
2358 "devices.\n");
2359 sdkp->capacity = 0;
2360 return -EOVERFLOW;
2361 }
2362
2363 /* Logical blocks per physical block exponent */
2364 sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size;
2365
2366 /* RC basis */
2367 sdkp->rc_basis = (buffer[12] >> 4) & 0x3;
2368
2369 /* Lowest aligned logical block */
2370 alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
2371 blk_queue_alignment_offset(sdp->request_queue, alignment);
2372 if (alignment && sdkp->first_scan)
2373 sd_printk(KERN_NOTICE, sdkp,
2374 "physical block alignment offset: %u\n", alignment);
2375
2376 if (buffer[14] & 0x80) { /* LBPME */
2377 sdkp->lbpme = 1;
2378
2379 if (buffer[14] & 0x40) /* LBPRZ */
2380 sdkp->lbprz = 1;
2381
2382 sd_config_discard(sdkp, SD_LBP_WS16);
2383 }
2384
2385 sdkp->capacity = lba + 1;
2386 return sector_size;
2387}
2388
2389static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
2390 unsigned char *buffer)
2391{
2392 unsigned char cmd[16];
2393 struct scsi_sense_hdr sshdr;
2394 int sense_valid = 0;
2395 int the_result;
2396 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
2397 sector_t lba;
2398 unsigned sector_size;
2399
2400 do {
2401 cmd[0] = READ_CAPACITY;
2402 memset(&cmd[1], 0, 9);
2403 memset(buffer, 0, 8);
2404
2405 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
2406 buffer, 8, &sshdr,
2407 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
2408
2409 if (media_not_present(sdkp, &sshdr))
2410 return -ENODEV;
2411
2412 if (the_result) {
2413 sense_valid = scsi_sense_valid(&sshdr);
2414 if (sense_valid &&
2415 sshdr.sense_key == UNIT_ATTENTION &&
2416 sshdr.asc == 0x29 && sshdr.ascq == 0x00)
2417 /* Device reset might occur several times,
2418 * give it one more chance */
2419 if (--reset_retries > 0)
2420 continue;
2421 }
2422 retries--;
2423
2424 } while (the_result && retries);
2425
2426 if (the_result) {
2427 sd_print_result(sdkp, "Read Capacity(10) failed", the_result);
2428 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
2429 return -EINVAL;
2430 }
2431
2432 sector_size = get_unaligned_be32(&buffer[4]);
2433 lba = get_unaligned_be32(&buffer[0]);
2434
2435 if (sdp->no_read_capacity_16 && (lba == 0xffffffff)) {
2436 /* Some buggy (usb cardreader) devices return an lba of
2437 0xffffffff when the want to report a size of 0 (with
2438 which they really mean no media is present) */
2439 sdkp->capacity = 0;
2440 sdkp->physical_block_size = sector_size;
2441 return sector_size;
2442 }
2443
2444 if (!sd_addressable_capacity(lba, sector_size)) {
2445 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
2446 "kernel compiled with support for large block "
2447 "devices.\n");
2448 sdkp->capacity = 0;
2449 return -EOVERFLOW;
2450 }
2451
2452 sdkp->capacity = lba + 1;
2453 sdkp->physical_block_size = sector_size;
2454 return sector_size;
2455}
2456
2457static int sd_try_rc16_first(struct scsi_device *sdp)
2458{
2459 if (sdp->host->max_cmd_len < 16)
2460 return 0;
2461 if (sdp->try_rc_10_first)
2462 return 0;
2463 if (sdp->scsi_level > SCSI_SPC_2)
2464 return 1;
2465 if (scsi_device_protection(sdp))
2466 return 1;
2467 return 0;
2468}
2469
2470/*
2471 * read disk capacity
2472 */
2473static void
2474sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
2475{
2476 int sector_size;
2477 struct scsi_device *sdp = sdkp->device;
2478
2479 if (sd_try_rc16_first(sdp)) {
2480 sector_size = read_capacity_16(sdkp, sdp, buffer);
2481 if (sector_size == -EOVERFLOW)
2482 goto got_data;
2483 if (sector_size == -ENODEV)
2484 return;
2485 if (sector_size < 0)
2486 sector_size = read_capacity_10(sdkp, sdp, buffer);
2487 if (sector_size < 0)
2488 return;
2489 } else {
2490 sector_size = read_capacity_10(sdkp, sdp, buffer);
2491 if (sector_size == -EOVERFLOW)
2492 goto got_data;
2493 if (sector_size < 0)
2494 return;
2495 if ((sizeof(sdkp->capacity) > 4) &&
2496 (sdkp->capacity > 0xffffffffULL)) {
2497 int old_sector_size = sector_size;
2498 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
2499 "Trying to use READ CAPACITY(16).\n");
2500 sector_size = read_capacity_16(sdkp, sdp, buffer);
2501 if (sector_size < 0) {
2502 sd_printk(KERN_NOTICE, sdkp,
2503 "Using 0xffffffff as device size\n");
2504 sdkp->capacity = 1 + (sector_t) 0xffffffff;
2505 sector_size = old_sector_size;
2506 goto got_data;
2507 }
2508 /* Remember that READ CAPACITY(16) succeeded */
2509 sdp->try_rc_10_first = 0;
2510 }
2511 }
2512
2513 /* Some devices are known to return the total number of blocks,
2514 * not the highest block number. Some devices have versions
2515 * which do this and others which do not. Some devices we might
2516 * suspect of doing this but we don't know for certain.
2517 *
2518 * If we know the reported capacity is wrong, decrement it. If
2519 * we can only guess, then assume the number of blocks is even
2520 * (usually true but not always) and err on the side of lowering
2521 * the capacity.
2522 */
2523 if (sdp->fix_capacity ||
2524 (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
2525 sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
2526 "from its reported value: %llu\n",
2527 (unsigned long long) sdkp->capacity);
2528 --sdkp->capacity;
2529 }
2530
2531got_data:
2532 if (sector_size == 0) {
2533 sector_size = 512;
2534 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
2535 "assuming 512.\n");
2536 }
2537
2538 if (sector_size != 512 &&
2539 sector_size != 1024 &&
2540 sector_size != 2048 &&
2541 sector_size != 4096) {
2542 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
2543 sector_size);
2544 /*
2545 * The user might want to re-format the drive with
2546 * a supported sectorsize. Once this happens, it
2547 * would be relatively trivial to set the thing up.
2548 * For this reason, we leave the thing in the table.
2549 */
2550 sdkp->capacity = 0;
2551 /*
2552 * set a bogus sector size so the normal read/write
2553 * logic in the block layer will eventually refuse any
2554 * request on this device without tripping over power
2555 * of two sector size assumptions
2556 */
2557 sector_size = 512;
2558 }
2559 blk_queue_logical_block_size(sdp->request_queue, sector_size);
2560 blk_queue_physical_block_size(sdp->request_queue,
2561 sdkp->physical_block_size);
2562 sdkp->device->sector_size = sector_size;
2563
2564 if (sdkp->capacity > 0xffffffff)
2565 sdp->use_16_for_rw = 1;
2566
2567}
2568
2569/*
2570 * Print disk capacity
2571 */
2572static void
2573sd_print_capacity(struct scsi_disk *sdkp,
2574 sector_t old_capacity)
2575{
2576 int sector_size = sdkp->device->sector_size;
2577 char cap_str_2[10], cap_str_10[10];
2578
2579 string_get_size(sdkp->capacity, sector_size,
2580 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
2581 string_get_size(sdkp->capacity, sector_size,
2582 STRING_UNITS_10, cap_str_10,
2583 sizeof(cap_str_10));
2584
2585 if (sdkp->first_scan || old_capacity != sdkp->capacity) {
2586 sd_printk(KERN_NOTICE, sdkp,
2587 "%llu %d-byte logical blocks: (%s/%s)\n",
2588 (unsigned long long)sdkp->capacity,
2589 sector_size, cap_str_10, cap_str_2);
2590
2591 if (sdkp->physical_block_size != sector_size)
2592 sd_printk(KERN_NOTICE, sdkp,
2593 "%u-byte physical blocks\n",
2594 sdkp->physical_block_size);
2595
2596 sd_zbc_print_zones(sdkp);
2597 }
2598}
2599
2600/* called with buffer of length 512 */
2601static inline int
2602sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
2603 unsigned char *buffer, int len, struct scsi_mode_data *data,
2604 struct scsi_sense_hdr *sshdr)
2605{
2606 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
2607 SD_TIMEOUT, SD_MAX_RETRIES, data,
2608 sshdr);
2609}
2610
2611/*
2612 * read write protect setting, if possible - called only in sd_revalidate_disk()
2613 * called with buffer of length SD_BUF_SIZE
2614 */
2615static void
2616sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
2617{
2618 int res;
2619 struct scsi_device *sdp = sdkp->device;
2620 struct scsi_mode_data data;
2621 int old_wp = sdkp->write_prot;
2622
2623 set_disk_ro(sdkp->disk, 0);
2624 if (sdp->skip_ms_page_3f) {
2625 sd_first_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
2626 return;
2627 }
2628
2629 if (sdp->use_192_bytes_for_3f) {
2630 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
2631 } else {
2632 /*
2633 * First attempt: ask for all pages (0x3F), but only 4 bytes.
2634 * We have to start carefully: some devices hang if we ask
2635 * for more than is available.
2636 */
2637 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
2638
2639 /*
2640 * Second attempt: ask for page 0 When only page 0 is
2641 * implemented, a request for page 3F may return Sense Key
2642 * 5: Illegal Request, Sense Code 24: Invalid field in
2643 * CDB.
2644 */
2645 if (!scsi_status_is_good(res))
2646 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
2647
2648 /*
2649 * Third attempt: ask 255 bytes, as we did earlier.
2650 */
2651 if (!scsi_status_is_good(res))
2652 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
2653 &data, NULL);
2654 }
2655
2656 if (!scsi_status_is_good(res)) {
2657 sd_first_printk(KERN_WARNING, sdkp,
2658 "Test WP failed, assume Write Enabled\n");
2659 } else {
2660 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
2661 set_disk_ro(sdkp->disk, sdkp->write_prot);
2662 if (sdkp->first_scan || old_wp != sdkp->write_prot) {
2663 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
2664 sdkp->write_prot ? "on" : "off");
2665 sd_printk(KERN_DEBUG, sdkp, "Mode Sense: %4ph\n", buffer);
2666 }
2667 }
2668}
2669
2670/*
2671 * sd_read_cache_type - called only from sd_revalidate_disk()
2672 * called with buffer of length SD_BUF_SIZE
2673 */
2674static void
2675sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
2676{
2677 int len = 0, res;
2678 struct scsi_device *sdp = sdkp->device;
2679
2680 int dbd;
2681 int modepage;
2682 int first_len;
2683 struct scsi_mode_data data;
2684 struct scsi_sense_hdr sshdr;
2685 int old_wce = sdkp->WCE;
2686 int old_rcd = sdkp->RCD;
2687 int old_dpofua = sdkp->DPOFUA;
2688
2689
2690 if (sdkp->cache_override)
2691 return;
2692
2693 first_len = 4;
2694 if (sdp->skip_ms_page_8) {
2695 if (sdp->type == TYPE_RBC)
2696 goto defaults;
2697 else {
2698 if (sdp->skip_ms_page_3f)
2699 goto defaults;
2700 modepage = 0x3F;
2701 if (sdp->use_192_bytes_for_3f)
2702 first_len = 192;
2703 dbd = 0;
2704 }
2705 } else if (sdp->type == TYPE_RBC) {
2706 modepage = 6;
2707 dbd = 8;
2708 } else {
2709 modepage = 8;
2710 dbd = 0;
2711 }
2712
2713 /* cautiously ask */
2714 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, first_len,
2715 &data, &sshdr);
2716
2717 if (!scsi_status_is_good(res))
2718 goto bad_sense;
2719
2720 if (!data.header_length) {
2721 modepage = 6;
2722 first_len = 0;
2723 sd_first_printk(KERN_ERR, sdkp,
2724 "Missing header in MODE_SENSE response\n");
2725 }
2726
2727 /* that went OK, now ask for the proper length */
2728 len = data.length;
2729
2730 /*
2731 * We're only interested in the first three bytes, actually.
2732 * But the data cache page is defined for the first 20.
2733 */
2734 if (len < 3)
2735 goto bad_sense;
2736 else if (len > SD_BUF_SIZE) {
2737 sd_first_printk(KERN_NOTICE, sdkp, "Truncating mode parameter "
2738 "data from %d to %d bytes\n", len, SD_BUF_SIZE);
2739 len = SD_BUF_SIZE;
2740 }
2741 if (modepage == 0x3F && sdp->use_192_bytes_for_3f)
2742 len = 192;
2743
2744 /* Get the data */
2745 if (len > first_len)
2746 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len,
2747 &data, &sshdr);
2748
2749 if (scsi_status_is_good(res)) {
2750 int offset = data.header_length + data.block_descriptor_length;
2751
2752 while (offset < len) {
2753 u8 page_code = buffer[offset] & 0x3F;
2754 u8 spf = buffer[offset] & 0x40;
2755
2756 if (page_code == 8 || page_code == 6) {
2757 /* We're interested only in the first 3 bytes.
2758 */
2759 if (len - offset <= 2) {
2760 sd_first_printk(KERN_ERR, sdkp,
2761 "Incomplete mode parameter "
2762 "data\n");
2763 goto defaults;
2764 } else {
2765 modepage = page_code;
2766 goto Page_found;
2767 }
2768 } else {
2769 /* Go to the next page */
2770 if (spf && len - offset > 3)
2771 offset += 4 + (buffer[offset+2] << 8) +
2772 buffer[offset+3];
2773 else if (!spf && len - offset > 1)
2774 offset += 2 + buffer[offset+1];
2775 else {
2776 sd_first_printk(KERN_ERR, sdkp,
2777 "Incomplete mode "
2778 "parameter data\n");
2779 goto defaults;
2780 }
2781 }
2782 }
2783
2784 sd_first_printk(KERN_ERR, sdkp, "No Caching mode page found\n");
2785 goto defaults;
2786
2787 Page_found:
2788 if (modepage == 8) {
2789 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
2790 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
2791 } else {
2792 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
2793 sdkp->RCD = 0;
2794 }
2795
2796 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
2797 if (sdp->broken_fua) {
2798 sd_first_printk(KERN_NOTICE, sdkp, "Disabling FUA\n");
2799 sdkp->DPOFUA = 0;
2800 } else if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw &&
2801 !sdkp->device->use_16_for_rw) {
2802 sd_first_printk(KERN_NOTICE, sdkp,
2803 "Uses READ/WRITE(6), disabling FUA\n");
2804 sdkp->DPOFUA = 0;
2805 }
2806
2807 /* No cache flush allowed for write protected devices */
2808 if (sdkp->WCE && sdkp->write_prot)
2809 sdkp->WCE = 0;
2810
2811 if (sdkp->first_scan || old_wce != sdkp->WCE ||
2812 old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
2813 sd_printk(KERN_NOTICE, sdkp,
2814 "Write cache: %s, read cache: %s, %s\n",
2815 sdkp->WCE ? "enabled" : "disabled",
2816 sdkp->RCD ? "disabled" : "enabled",
2817 sdkp->DPOFUA ? "supports DPO and FUA"
2818 : "doesn't support DPO or FUA");
2819
2820 return;
2821 }
2822
2823bad_sense:
2824 if (scsi_sense_valid(&sshdr) &&
2825 sshdr.sense_key == ILLEGAL_REQUEST &&
2826 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
2827 /* Invalid field in CDB */
2828 sd_first_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
2829 else
2830 sd_first_printk(KERN_ERR, sdkp,
2831 "Asking for cache data failed\n");
2832
2833defaults:
2834 if (sdp->wce_default_on) {
2835 sd_first_printk(KERN_NOTICE, sdkp,
2836 "Assuming drive cache: write back\n");
2837 sdkp->WCE = 1;
2838 } else {
2839 sd_first_printk(KERN_ERR, sdkp,
2840 "Assuming drive cache: write through\n");
2841 sdkp->WCE = 0;
2842 }
2843 sdkp->RCD = 0;
2844 sdkp->DPOFUA = 0;
2845}
2846
2847/*
2848 * The ATO bit indicates whether the DIF application tag is available
2849 * for use by the operating system.
2850 */
2851static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
2852{
2853 int res, offset;
2854 struct scsi_device *sdp = sdkp->device;
2855 struct scsi_mode_data data;
2856 struct scsi_sense_hdr sshdr;
2857
2858 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
2859 return;
2860
2861 if (sdkp->protection_type == 0)
2862 return;
2863
2864 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
2865 SD_MAX_RETRIES, &data, &sshdr);
2866
2867 if (!scsi_status_is_good(res) || !data.header_length ||
2868 data.length < 6) {
2869 sd_first_printk(KERN_WARNING, sdkp,
2870 "getting Control mode page failed, assume no ATO\n");
2871
2872 if (scsi_sense_valid(&sshdr))
2873 sd_print_sense_hdr(sdkp, &sshdr);
2874
2875 return;
2876 }
2877
2878 offset = data.header_length + data.block_descriptor_length;
2879
2880 if ((buffer[offset] & 0x3f) != 0x0a) {
2881 sd_first_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
2882 return;
2883 }
2884
2885 if ((buffer[offset + 5] & 0x80) == 0)
2886 return;
2887
2888 sdkp->ATO = 1;
2889
2890 return;
2891}
2892
2893/**
2894 * sd_read_block_limits - Query disk device for preferred I/O sizes.
2895 * @sdkp: disk to query
2896 */
2897static void sd_read_block_limits(struct scsi_disk *sdkp)
2898{
2899 unsigned int sector_sz = sdkp->device->sector_size;
2900 const int vpd_len = 64;
2901 unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
2902
2903 if (!buffer ||
2904 /* Block Limits VPD */
2905 scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
2906 goto out;
2907
2908 blk_queue_io_min(sdkp->disk->queue,
2909 get_unaligned_be16(&buffer[6]) * sector_sz);
2910
2911 sdkp->max_xfer_blocks = get_unaligned_be32(&buffer[8]);
2912 sdkp->opt_xfer_blocks = get_unaligned_be32(&buffer[12]);
2913
2914 if (buffer[3] == 0x3c) {
2915 unsigned int lba_count, desc_count;
2916
2917 sdkp->max_ws_blocks = (u32)get_unaligned_be64(&buffer[36]);
2918
2919 if (!sdkp->lbpme)
2920 goto out;
2921
2922 lba_count = get_unaligned_be32(&buffer[20]);
2923 desc_count = get_unaligned_be32(&buffer[24]);
2924
2925 if (lba_count && desc_count)
2926 sdkp->max_unmap_blocks = lba_count;
2927
2928 sdkp->unmap_granularity = get_unaligned_be32(&buffer[28]);
2929
2930 if (buffer[32] & 0x80)
2931 sdkp->unmap_alignment =
2932 get_unaligned_be32(&buffer[32]) & ~(1 << 31);
2933
2934 if (!sdkp->lbpvpd) { /* LBP VPD page not provided */
2935
2936 if (sdkp->max_unmap_blocks)
2937 sd_config_discard(sdkp, SD_LBP_UNMAP);
2938 else
2939 sd_config_discard(sdkp, SD_LBP_WS16);
2940
2941 } else { /* LBP VPD page tells us what to use */
2942 if (sdkp->lbpu && sdkp->max_unmap_blocks)
2943 sd_config_discard(sdkp, SD_LBP_UNMAP);
2944 else if (sdkp->lbpws)
2945 sd_config_discard(sdkp, SD_LBP_WS16);
2946 else if (sdkp->lbpws10)
2947 sd_config_discard(sdkp, SD_LBP_WS10);
2948 else
2949 sd_config_discard(sdkp, SD_LBP_DISABLE);
2950 }
2951 }
2952
2953 out:
2954 kfree(buffer);
2955}
2956
2957/**
2958 * sd_read_block_characteristics - Query block dev. characteristics
2959 * @sdkp: disk to query
2960 */
2961static void sd_read_block_characteristics(struct scsi_disk *sdkp)
2962{
2963 struct request_queue *q = sdkp->disk->queue;
2964 unsigned char *buffer;
2965 u16 rot;
2966 const int vpd_len = 64;
2967
2968 buffer = kmalloc(vpd_len, GFP_KERNEL);
2969
2970 if (!buffer ||
2971 /* Block Device Characteristics VPD */
2972 scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len))
2973 goto out;
2974
2975 rot = get_unaligned_be16(&buffer[4]);
2976
2977 if (rot == 1) {
2978 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
2979 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, q);
2980 }
2981
2982 if (sdkp->device->type == TYPE_ZBC) {
2983 /* Host-managed */
2984 q->limits.zoned = BLK_ZONED_HM;
2985 } else {
2986 sdkp->zoned = (buffer[8] >> 4) & 3;
2987 if (sdkp->zoned == 1)
2988 /* Host-aware */
2989 q->limits.zoned = BLK_ZONED_HA;
2990 else
2991 /*
2992 * Treat drive-managed devices as
2993 * regular block devices.
2994 */
2995 q->limits.zoned = BLK_ZONED_NONE;
2996 }
2997 if (blk_queue_is_zoned(q) && sdkp->first_scan)
2998 sd_printk(KERN_NOTICE, sdkp, "Host-%s zoned block device\n",
2999 q->limits.zoned == BLK_ZONED_HM ? "managed" : "aware");
3000
3001 out:
3002 kfree(buffer);
3003}
3004
3005/**
3006 * sd_read_block_provisioning - Query provisioning VPD page
3007 * @sdkp: disk to query
3008 */
3009static void sd_read_block_provisioning(struct scsi_disk *sdkp)
3010{
3011 unsigned char *buffer;
3012 const int vpd_len = 8;
3013
3014 if (sdkp->lbpme == 0)
3015 return;
3016
3017 buffer = kmalloc(vpd_len, GFP_KERNEL);
3018
3019 if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb2, buffer, vpd_len))
3020 goto out;
3021
3022 sdkp->lbpvpd = 1;
3023 sdkp->lbpu = (buffer[5] >> 7) & 1; /* UNMAP */
3024 sdkp->lbpws = (buffer[5] >> 6) & 1; /* WRITE SAME(16) with UNMAP */
3025 sdkp->lbpws10 = (buffer[5] >> 5) & 1; /* WRITE SAME(10) with UNMAP */
3026
3027 out:
3028 kfree(buffer);
3029}
3030
3031static void sd_read_write_same(struct scsi_disk *sdkp, unsigned char *buffer)
3032{
3033 struct scsi_device *sdev = sdkp->device;
3034
3035 if (sdev->host->no_write_same) {
3036 sdev->no_write_same = 1;
3037
3038 return;
3039 }
3040
3041 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, INQUIRY) < 0) {
3042 /* too large values might cause issues with arcmsr */
3043 int vpd_buf_len = 64;
3044
3045 sdev->no_report_opcodes = 1;
3046
3047 /* Disable WRITE SAME if REPORT SUPPORTED OPERATION
3048 * CODES is unsupported and the device has an ATA
3049 * Information VPD page (SAT).
3050 */
3051 if (!scsi_get_vpd_page(sdev, 0x89, buffer, vpd_buf_len))
3052 sdev->no_write_same = 1;
3053 }
3054
3055 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME_16) == 1)
3056 sdkp->ws16 = 1;
3057
3058 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME) == 1)
3059 sdkp->ws10 = 1;
3060}
3061
3062static void sd_read_security(struct scsi_disk *sdkp, unsigned char *buffer)
3063{
3064 struct scsi_device *sdev = sdkp->device;
3065
3066 if (!sdev->security_supported)
3067 return;
3068
3069 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE,
3070 SECURITY_PROTOCOL_IN) == 1 &&
3071 scsi_report_opcode(sdev, buffer, SD_BUF_SIZE,
3072 SECURITY_PROTOCOL_OUT) == 1)
3073 sdkp->security = 1;
3074}
3075
3076/*
3077 * Determine the device's preferred I/O size for reads and writes
3078 * unless the reported value is unreasonably small, large, not a
3079 * multiple of the physical block size, or simply garbage.
3080 */
3081static bool sd_validate_opt_xfer_size(struct scsi_disk *sdkp,
3082 unsigned int dev_max)
3083{
3084 struct scsi_device *sdp = sdkp->device;
3085 unsigned int opt_xfer_bytes =
3086 logical_to_bytes(sdp, sdkp->opt_xfer_blocks);
3087
3088 if (sdkp->opt_xfer_blocks == 0)
3089 return false;
3090
3091 if (sdkp->opt_xfer_blocks > dev_max) {
3092 sd_first_printk(KERN_WARNING, sdkp,
3093 "Optimal transfer size %u logical blocks " \
3094 "> dev_max (%u logical blocks)\n",
3095 sdkp->opt_xfer_blocks, dev_max);
3096 return false;
3097 }
3098
3099 if (sdkp->opt_xfer_blocks > SD_DEF_XFER_BLOCKS) {
3100 sd_first_printk(KERN_WARNING, sdkp,
3101 "Optimal transfer size %u logical blocks " \
3102 "> sd driver limit (%u logical blocks)\n",
3103 sdkp->opt_xfer_blocks, SD_DEF_XFER_BLOCKS);
3104 return false;
3105 }
3106
3107 if (opt_xfer_bytes < PAGE_SIZE) {
3108 sd_first_printk(KERN_WARNING, sdkp,
3109 "Optimal transfer size %u bytes < " \
3110 "PAGE_SIZE (%u bytes)\n",
3111 opt_xfer_bytes, (unsigned int)PAGE_SIZE);
3112 return false;
3113 }
3114
3115 if (opt_xfer_bytes & (sdkp->physical_block_size - 1)) {
3116 sd_first_printk(KERN_WARNING, sdkp,
3117 "Optimal transfer size %u bytes not a " \
3118 "multiple of physical block size (%u bytes)\n",
3119 opt_xfer_bytes, sdkp->physical_block_size);
3120 return false;
3121 }
3122
3123 sd_first_printk(KERN_INFO, sdkp, "Optimal transfer size %u bytes\n",
3124 opt_xfer_bytes);
3125 return true;
3126}
3127
3128/**
3129 * sd_revalidate_disk - called the first time a new disk is seen,
3130 * performs disk spin up, read_capacity, etc.
3131 * @disk: struct gendisk we care about
3132 **/
3133static int sd_revalidate_disk(struct gendisk *disk)
3134{
3135 struct scsi_disk *sdkp = scsi_disk(disk);
3136 struct scsi_device *sdp = sdkp->device;
3137 struct request_queue *q = sdkp->disk->queue;
3138 sector_t old_capacity = sdkp->capacity;
3139 unsigned char *buffer;
3140 unsigned int dev_max, rw_max;
3141
3142 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
3143 "sd_revalidate_disk\n"));
3144
3145 /*
3146 * If the device is offline, don't try and read capacity or any
3147 * of the other niceties.
3148 */
3149 if (!scsi_device_online(sdp))
3150 goto out;
3151
3152 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
3153 if (!buffer) {
3154 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
3155 "allocation failure.\n");
3156 goto out;
3157 }
3158
3159 sd_spinup_disk(sdkp);
3160
3161 /*
3162 * Without media there is no reason to ask; moreover, some devices
3163 * react badly if we do.
3164 */
3165 if (sdkp->media_present) {
3166 sd_read_capacity(sdkp, buffer);
3167
3168 /*
3169 * set the default to rotational. All non-rotational devices
3170 * support the block characteristics VPD page, which will
3171 * cause this to be updated correctly and any device which
3172 * doesn't support it should be treated as rotational.
3173 */
3174 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
3175 blk_queue_flag_set(QUEUE_FLAG_ADD_RANDOM, q);
3176
3177 if (scsi_device_supports_vpd(sdp)) {
3178 sd_read_block_provisioning(sdkp);
3179 sd_read_block_limits(sdkp);
3180 sd_read_block_characteristics(sdkp);
3181 sd_zbc_read_zones(sdkp, buffer);
3182 }
3183
3184 sd_print_capacity(sdkp, old_capacity);
3185
3186 sd_read_write_protect_flag(sdkp, buffer);
3187 sd_read_cache_type(sdkp, buffer);
3188 sd_read_app_tag_own(sdkp, buffer);
3189 sd_read_write_same(sdkp, buffer);
3190 sd_read_security(sdkp, buffer);
3191 }
3192
3193 /*
3194 * We now have all cache related info, determine how we deal
3195 * with flush requests.
3196 */
3197 sd_set_flush_flag(sdkp);
3198
3199 /* Initial block count limit based on CDB TRANSFER LENGTH field size. */
3200 dev_max = sdp->use_16_for_rw ? SD_MAX_XFER_BLOCKS : SD_DEF_XFER_BLOCKS;
3201
3202 /* Some devices report a maximum block count for READ/WRITE requests. */
3203 dev_max = min_not_zero(dev_max, sdkp->max_xfer_blocks);
3204 q->limits.max_dev_sectors = logical_to_sectors(sdp, dev_max);
3205
3206 if (sd_validate_opt_xfer_size(sdkp, dev_max)) {
3207 rw_max = q->limits.io_opt =
3208 logical_to_bytes(sdp, sdkp->opt_xfer_blocks);
3209 } else
3210 rw_max = min_not_zero(logical_to_sectors(sdp, dev_max),
3211 (sector_t)BLK_DEF_MAX_SECTORS);
3212
3213 /* Do not exceed controller limit */
3214 rw_max = min(rw_max, queue_max_hw_sectors(q));
3215
3216 /*
3217 * Only update max_sectors if previously unset or if the current value
3218 * exceeds the capabilities of the hardware.
3219 */
3220 if (sdkp->first_scan ||
3221 q->limits.max_sectors > q->limits.max_dev_sectors ||
3222 q->limits.max_sectors > q->limits.max_hw_sectors)
3223 q->limits.max_sectors = rw_max;
3224
3225 sdkp->first_scan = 0;
3226
3227 set_capacity(disk, logical_to_sectors(sdp, sdkp->capacity));
3228 sd_config_write_same(sdkp);
3229 kfree(buffer);
3230
3231 out:
3232 return 0;
3233}
3234
3235/**
3236 * sd_unlock_native_capacity - unlock native capacity
3237 * @disk: struct gendisk to set capacity for
3238 *
3239 * Block layer calls this function if it detects that partitions
3240 * on @disk reach beyond the end of the device. If the SCSI host
3241 * implements ->unlock_native_capacity() method, it's invoked to
3242 * give it a chance to adjust the device capacity.
3243 *
3244 * CONTEXT:
3245 * Defined by block layer. Might sleep.
3246 */
3247static void sd_unlock_native_capacity(struct gendisk *disk)
3248{
3249 struct scsi_device *sdev = scsi_disk(disk)->device;
3250
3251 if (sdev->host->hostt->unlock_native_capacity)
3252 sdev->host->hostt->unlock_native_capacity(sdev);
3253}
3254
3255/**
3256 * sd_format_disk_name - format disk name
3257 * @prefix: name prefix - ie. "sd" for SCSI disks
3258 * @index: index of the disk to format name for
3259 * @buf: output buffer
3260 * @buflen: length of the output buffer
3261 *
3262 * SCSI disk names starts at sda. The 26th device is sdz and the
3263 * 27th is sdaa. The last one for two lettered suffix is sdzz
3264 * which is followed by sdaaa.
3265 *
3266 * This is basically 26 base counting with one extra 'nil' entry
3267 * at the beginning from the second digit on and can be
3268 * determined using similar method as 26 base conversion with the
3269 * index shifted -1 after each digit is computed.
3270 *
3271 * CONTEXT:
3272 * Don't care.
3273 *
3274 * RETURNS:
3275 * 0 on success, -errno on failure.
3276 */
3277static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
3278{
3279 const int base = 'z' - 'a' + 1;
3280 char *begin = buf + strlen(prefix);
3281 char *end = buf + buflen;
3282 char *p;
3283 int unit;
3284
3285 p = end - 1;
3286 *p = '\0';
3287 unit = base;
3288 do {
3289 if (p == begin)
3290 return -EINVAL;
3291 *--p = 'a' + (index % unit);
3292 index = (index / unit) - 1;
3293 } while (index >= 0);
3294
3295 memmove(begin, p, end - p);
3296 memcpy(buf, prefix, strlen(prefix));
3297
3298 return 0;
3299}
3300
3301/*
3302 * The asynchronous part of sd_probe
3303 */
3304static void sd_probe_async(void *data, async_cookie_t cookie)
3305{
3306 struct scsi_disk *sdkp = data;
3307 struct scsi_device *sdp;
3308 struct gendisk *gd;
3309 u32 index;
3310 struct device *dev;
3311
3312 sdp = sdkp->device;
3313 gd = sdkp->disk;
3314 index = sdkp->index;
3315 dev = &sdp->sdev_gendev;
3316
3317 gd->major = sd_major((index & 0xf0) >> 4);
3318 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
3319
3320 gd->fops = &sd_fops;
3321 gd->private_data = &sdkp->driver;
3322 gd->queue = sdkp->device->request_queue;
3323
3324 /* defaults, until the device tells us otherwise */
3325 sdp->sector_size = 512;
3326 sdkp->capacity = 0;
3327 sdkp->media_present = 1;
3328 sdkp->write_prot = 0;
3329 sdkp->cache_override = 0;
3330 sdkp->WCE = 0;
3331 sdkp->RCD = 0;
3332 sdkp->ATO = 0;
3333 sdkp->first_scan = 1;
3334 sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS;
3335
3336 sd_revalidate_disk(gd);
3337
3338 gd->flags = GENHD_FL_EXT_DEVT;
3339 if (sdp->removable) {
3340 gd->flags |= GENHD_FL_REMOVABLE;
3341 gd->events |= DISK_EVENT_MEDIA_CHANGE;
3342 }
3343
3344 blk_pm_runtime_init(sdp->request_queue, dev);
3345 if (sdp->rpm_autosuspend) {
3346 pm_runtime_set_autosuspend_delay(dev,
3347 sdp->host->hostt->rpm_autosuspend_delay);
3348 }
3349
3350 device_add_disk(dev, gd);
3351 if (sdkp->capacity)
3352 sd_dif_config_host(sdkp);
3353
3354 sd_revalidate_disk(gd);
3355
3356 if (sdkp->security) {
3357 sdkp->opal_dev = init_opal_dev(sdp, &sd_sec_submit);
3358 if (sdkp->opal_dev)
3359 sd_printk(KERN_NOTICE, sdkp, "supports TCG Opal\n");
3360 }
3361
3362 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
3363 sdp->removable ? "removable " : "");
3364 scsi_autopm_put_device(sdp);
3365 put_device(&sdkp->dev);
3366}
3367
3368/**
3369 * sd_probe - called during driver initialization and whenever a
3370 * new scsi device is attached to the system. It is called once
3371 * for each scsi device (not just disks) present.
3372 * @dev: pointer to device object
3373 *
3374 * Returns 0 if successful (or not interested in this scsi device
3375 * (e.g. scanner)); 1 when there is an error.
3376 *
3377 * Note: this function is invoked from the scsi mid-level.
3378 * This function sets up the mapping between a given
3379 * <host,channel,id,lun> (found in sdp) and new device name
3380 * (e.g. /dev/sda). More precisely it is the block device major
3381 * and minor number that is chosen here.
3382 *
3383 * Assume sd_probe is not re-entrant (for time being)
3384 * Also think about sd_probe() and sd_remove() running coincidentally.
3385 **/
3386static int sd_probe(struct device *dev)
3387{
3388 struct scsi_device *sdp = to_scsi_device(dev);
3389 struct scsi_disk *sdkp;
3390 struct gendisk *gd;
3391 int index;
3392 int error;
3393
3394 scsi_autopm_get_device(sdp);
3395 error = -ENODEV;
3396 if (sdp->type != TYPE_DISK &&
3397 sdp->type != TYPE_ZBC &&
3398 sdp->type != TYPE_MOD &&
3399 sdp->type != TYPE_RBC)
3400 goto out;
3401
3402#ifndef CONFIG_BLK_DEV_ZONED
3403 if (sdp->type == TYPE_ZBC)
3404 goto out;
3405#endif
3406 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
3407 "sd_probe\n"));
3408
3409 error = -ENOMEM;
3410 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
3411 if (!sdkp)
3412 goto out;
3413
3414 gd = alloc_disk(SD_MINORS);
3415 if (!gd)
3416 goto out_free;
3417
3418 index = ida_alloc(&sd_index_ida, GFP_KERNEL);
3419 if (index < 0) {
3420 sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n");
3421 goto out_put;
3422 }
3423
3424 error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
3425 if (error) {
3426 sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n");
3427 goto out_free_index;
3428 }
3429
3430 sdkp->device = sdp;
3431 sdkp->driver = &sd_template;
3432 sdkp->disk = gd;
3433 sdkp->index = index;
3434 atomic_set(&sdkp->openers, 0);
3435 atomic_set(&sdkp->device->ioerr_cnt, 0);
3436
3437 if (!sdp->request_queue->rq_timeout) {
3438 if (sdp->type != TYPE_MOD)
3439 blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
3440 else
3441 blk_queue_rq_timeout(sdp->request_queue,
3442 SD_MOD_TIMEOUT);
3443 }
3444
3445 device_initialize(&sdkp->dev);
3446 sdkp->dev.parent = dev;
3447 sdkp->dev.class = &sd_disk_class;
3448 dev_set_name(&sdkp->dev, "%s", dev_name(dev));
3449
3450 error = device_add(&sdkp->dev);
3451 if (error)
3452 goto out_free_index;
3453
3454 get_device(dev);
3455 dev_set_drvdata(dev, sdkp);
3456
3457 get_device(&sdkp->dev); /* prevent release before async_schedule */
3458 async_schedule_domain(sd_probe_async, sdkp, &scsi_sd_probe_domain);
3459
3460 return 0;
3461
3462 out_free_index:
3463 ida_free(&sd_index_ida, index);
3464 out_put:
3465 put_disk(gd);
3466 out_free:
3467 kfree(sdkp);
3468 out:
3469 scsi_autopm_put_device(sdp);
3470 return error;
3471}
3472
3473/**
3474 * sd_remove - called whenever a scsi disk (previously recognized by
3475 * sd_probe) is detached from the system. It is called (potentially
3476 * multiple times) during sd module unload.
3477 * @dev: pointer to device object
3478 *
3479 * Note: this function is invoked from the scsi mid-level.
3480 * This function potentially frees up a device name (e.g. /dev/sdc)
3481 * that could be re-used by a subsequent sd_probe().
3482 * This function is not called when the built-in sd driver is "exit-ed".
3483 **/
3484static int sd_remove(struct device *dev)
3485{
3486 struct scsi_disk *sdkp;
3487 dev_t devt;
3488
3489 sdkp = dev_get_drvdata(dev);
3490 devt = disk_devt(sdkp->disk);
3491 scsi_autopm_get_device(sdkp->device);
3492
3493 async_synchronize_full_domain(&scsi_sd_pm_domain);
3494 async_synchronize_full_domain(&scsi_sd_probe_domain);
3495 device_del(&sdkp->dev);
3496 del_gendisk(sdkp->disk);
3497 sd_shutdown(dev);
3498
3499 sd_zbc_remove(sdkp);
3500
3501 free_opal_dev(sdkp->opal_dev);
3502
3503 blk_register_region(devt, SD_MINORS, NULL,
3504 sd_default_probe, NULL, NULL);
3505
3506 mutex_lock(&sd_ref_mutex);
3507 dev_set_drvdata(dev, NULL);
3508 put_device(&sdkp->dev);
3509 mutex_unlock(&sd_ref_mutex);
3510
3511 return 0;
3512}
3513
3514/**
3515 * scsi_disk_release - Called to free the scsi_disk structure
3516 * @dev: pointer to embedded class device
3517 *
3518 * sd_ref_mutex must be held entering this routine. Because it is
3519 * called on last put, you should always use the scsi_disk_get()
3520 * scsi_disk_put() helpers which manipulate the semaphore directly
3521 * and never do a direct put_device.
3522 **/
3523static void scsi_disk_release(struct device *dev)
3524{
3525 struct scsi_disk *sdkp = to_scsi_disk(dev);
3526 struct gendisk *disk = sdkp->disk;
3527 struct request_queue *q = disk->queue;
3528
3529 ida_free(&sd_index_ida, sdkp->index);
3530
3531 /*
3532 * Wait until all requests that are in progress have completed.
3533 * This is necessary to avoid that e.g. scsi_end_request() crashes
3534 * due to clearing the disk->private_data pointer. Wait from inside
3535 * scsi_disk_release() instead of from sd_release() to avoid that
3536 * freezing and unfreezing the request queue affects user space I/O
3537 * in case multiple processes open a /dev/sd... node concurrently.
3538 */
3539 blk_mq_freeze_queue(q);
3540 blk_mq_unfreeze_queue(q);
3541
3542 disk->private_data = NULL;
3543 put_disk(disk);
3544 put_device(&sdkp->device->sdev_gendev);
3545
3546 kfree(sdkp);
3547}
3548
3549static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
3550{
3551 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
3552 struct scsi_sense_hdr sshdr;
3553 struct scsi_device *sdp = sdkp->device;
3554 int res;
3555
3556 if (start)
3557 cmd[4] |= 1; /* START */
3558
3559 if (sdp->start_stop_pwr_cond)
3560 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
3561
3562 if (!scsi_device_online(sdp))
3563 return -ENODEV;
3564
3565 res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
3566 SD_TIMEOUT, SD_MAX_RETRIES, 0, RQF_PM, NULL);
3567 if (res) {
3568 sd_print_result(sdkp, "Start/Stop Unit failed", res);
3569 if (driver_byte(res) == DRIVER_SENSE)
3570 sd_print_sense_hdr(sdkp, &sshdr);
3571 if (scsi_sense_valid(&sshdr) &&
3572 /* 0x3a is medium not present */
3573 sshdr.asc == 0x3a)
3574 res = 0;
3575 }
3576
3577 /* SCSI error codes must not go to the generic layer */
3578 if (res)
3579 return -EIO;
3580
3581 return 0;
3582}
3583
3584/*
3585 * Send a SYNCHRONIZE CACHE instruction down to the device through
3586 * the normal SCSI command structure. Wait for the command to
3587 * complete.
3588 */
3589static void sd_shutdown(struct device *dev)
3590{
3591 struct scsi_disk *sdkp = dev_get_drvdata(dev);
3592
3593 if (!sdkp)
3594 return; /* this can happen */
3595
3596 if (pm_runtime_suspended(dev))
3597 return;
3598
3599 if (sdkp->WCE && sdkp->media_present) {
3600 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
3601 sd_sync_cache(sdkp, NULL);
3602 }
3603
3604 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
3605 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
3606 sd_start_stop_device(sdkp, 0);
3607 }
3608}
3609
3610static int sd_suspend_common(struct device *dev, bool ignore_stop_errors)
3611{
3612 struct scsi_disk *sdkp = dev_get_drvdata(dev);
3613 struct scsi_sense_hdr sshdr;
3614 int ret = 0;
3615
3616 if (!sdkp) /* E.g.: runtime suspend following sd_remove() */
3617 return 0;
3618
3619 if (sdkp->WCE && sdkp->media_present) {
3620 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
3621 ret = sd_sync_cache(sdkp, &sshdr);
3622
3623 if (ret) {
3624 /* ignore OFFLINE device */
3625 if (ret == -ENODEV)
3626 return 0;
3627
3628 if (!scsi_sense_valid(&sshdr) ||
3629 sshdr.sense_key != ILLEGAL_REQUEST)
3630 return ret;
3631
3632 /*
3633 * sshdr.sense_key == ILLEGAL_REQUEST means this drive
3634 * doesn't support sync. There's not much to do and
3635 * suspend shouldn't fail.
3636 */
3637 ret = 0;
3638 }
3639 }
3640
3641 if (sdkp->device->manage_start_stop) {
3642 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
3643 /* an error is not worth aborting a system sleep */
3644 ret = sd_start_stop_device(sdkp, 0);
3645 if (ignore_stop_errors)
3646 ret = 0;
3647 }
3648
3649 return ret;
3650}
3651
3652static int sd_suspend_system(struct device *dev)
3653{
3654 return sd_suspend_common(dev, true);
3655}
3656
3657static int sd_suspend_runtime(struct device *dev)
3658{
3659 return sd_suspend_common(dev, false);
3660}
3661
3662static int sd_resume(struct device *dev)
3663{
3664 struct scsi_disk *sdkp = dev_get_drvdata(dev);
3665 int ret;
3666
3667 if (!sdkp) /* E.g.: runtime resume at the start of sd_probe() */
3668 return 0;
3669
3670 if (!sdkp->device->manage_start_stop)
3671 return 0;
3672
3673 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
3674 ret = sd_start_stop_device(sdkp, 1);
3675 if (!ret)
3676 opal_unlock_from_suspend(sdkp->opal_dev);
3677 return ret;
3678}
3679
3680/**
3681 * init_sd - entry point for this driver (both when built in or when
3682 * a module).
3683 *
3684 * Note: this function registers this driver with the scsi mid-level.
3685 **/
3686static int __init init_sd(void)
3687{
3688 int majors = 0, i, err;
3689
3690 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
3691
3692 for (i = 0; i < SD_MAJORS; i++) {
3693 if (register_blkdev(sd_major(i), "sd") != 0)
3694 continue;
3695 majors++;
3696 blk_register_region(sd_major(i), SD_MINORS, NULL,
3697 sd_default_probe, NULL, NULL);
3698 }
3699
3700 if (!majors)
3701 return -ENODEV;
3702
3703 err = class_register(&sd_disk_class);
3704 if (err)
3705 goto err_out;
3706
3707 sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
3708 0, 0, NULL);
3709 if (!sd_cdb_cache) {
3710 printk(KERN_ERR "sd: can't init extended cdb cache\n");
3711 err = -ENOMEM;
3712 goto err_out_class;
3713 }
3714
3715 sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
3716 if (!sd_cdb_pool) {
3717 printk(KERN_ERR "sd: can't init extended cdb pool\n");
3718 err = -ENOMEM;
3719 goto err_out_cache;
3720 }
3721
3722 sd_page_pool = mempool_create_page_pool(SD_MEMPOOL_SIZE, 0);
3723 if (!sd_page_pool) {
3724 printk(KERN_ERR "sd: can't init discard page pool\n");
3725 err = -ENOMEM;
3726 goto err_out_ppool;
3727 }
3728
3729 err = scsi_register_driver(&sd_template.gendrv);
3730 if (err)
3731 goto err_out_driver;
3732
3733 return 0;
3734
3735err_out_driver:
3736 mempool_destroy(sd_page_pool);
3737
3738err_out_ppool:
3739 mempool_destroy(sd_cdb_pool);
3740
3741err_out_cache:
3742 kmem_cache_destroy(sd_cdb_cache);
3743
3744err_out_class:
3745 class_unregister(&sd_disk_class);
3746err_out:
3747 for (i = 0; i < SD_MAJORS; i++)
3748 unregister_blkdev(sd_major(i), "sd");
3749 return err;
3750}
3751
3752/**
3753 * exit_sd - exit point for this driver (when it is a module).
3754 *
3755 * Note: this function unregisters this driver from the scsi mid-level.
3756 **/
3757static void __exit exit_sd(void)
3758{
3759 int i;
3760
3761 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
3762
3763 scsi_unregister_driver(&sd_template.gendrv);
3764 mempool_destroy(sd_cdb_pool);
3765 mempool_destroy(sd_page_pool);
3766 kmem_cache_destroy(sd_cdb_cache);
3767
3768 class_unregister(&sd_disk_class);
3769
3770 for (i = 0; i < SD_MAJORS; i++) {
3771 blk_unregister_region(sd_major(i), SD_MINORS);
3772 unregister_blkdev(sd_major(i), "sd");
3773 }
3774}
3775
3776module_init(init_sd);
3777module_exit(exit_sd);
3778
3779static void sd_print_sense_hdr(struct scsi_disk *sdkp,
3780 struct scsi_sense_hdr *sshdr)
3781{
3782 scsi_print_sense_hdr(sdkp->device,
3783 sdkp->disk ? sdkp->disk->disk_name : NULL, sshdr);
3784}
3785
3786static void sd_print_result(const struct scsi_disk *sdkp, const char *msg,
3787 int result)
3788{
3789 const char *hb_string = scsi_hostbyte_string(result);
3790 const char *db_string = scsi_driverbyte_string(result);
3791
3792 if (hb_string || db_string)
3793 sd_printk(KERN_INFO, sdkp,
3794 "%s: Result: hostbyte=%s driverbyte=%s\n", msg,
3795 hb_string ? hb_string : "invalid",
3796 db_string ? db_string : "invalid");
3797 else
3798 sd_printk(KERN_INFO, sdkp,
3799 "%s: Result: hostbyte=0x%02x driverbyte=0x%02x\n",
3800 msg, host_byte(result), driver_byte(result));
3801}
3802