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