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