sd.c 98.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds's avatar
Linus Torvalds committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
 *      sd.c Copyright (C) 1992 Drew Eckhardt
 *           Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
 *
 *      Linux scsi disk driver
 *              Initial versions: Drew Eckhardt
 *              Subsequent revisions: Eric Youngdale
 *	Modification history:
 *       - Drew Eckhardt <drew@colorado.edu> original
 *       - Eric Youngdale <eric@andante.org> add scatter-gather, multiple 
 *         outstanding request, and other enhancements.
 *         Support loadable low-level scsi drivers.
 *       - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using 
 *         eight major numbers.
 *       - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
 *	 - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in 
 *	   sd_init and cleanups.
 *	 - Alex Davis <letmein@erols.com> Fix problem where partition info
 *	   not being read in sd_open. Fix problem where removable media 
 *	   could be ejected after sd_open.
 *	 - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
 *	 - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox 
 *	   <willy@debian.org>, Kurt Garloff <garloff@suse.de>: 
 *	   Support 32k/1M disks.
 *
 *	Logging policy (needs CONFIG_SCSI_LOGGING defined):
 *	 - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
 *	 - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
 *	 - entering sd_ioctl: SCSI_LOG_IOCTL level 1
 *	 - entering other commands: SCSI_LOG_HLQUEUE level 3
 *	Note: when the logging level is set by the user, it must be greater
 *	than the level indicated above to trigger output.	
 */

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/bio.h>
#include <linux/genhd.h>
#include <linux/hdreg.h>
#include <linux/errno.h>
#include <linux/idr.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/blkpg.h>
49
#include <linux/blk-pm.h>
Linus Torvalds's avatar
Linus Torvalds committed
50
#include <linux/delay.h>
51
#include <linux/mutex.h>
52
#include <linux/string_helpers.h>
53
#include <linux/async.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 <asm/unaligned.h>
Linus Torvalds's avatar
Linus Torvalds committed
61
62
63
64
65
66
67
68
69
70
71

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_dbg.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_driver.h>
#include <scsi/scsi_eh.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_ioctl.h>
#include <scsi/scsicam.h>

72
#include "sd.h"
73
#include "scsi_priv.h"
Linus Torvalds's avatar
Linus Torvalds committed
74
75
#include "scsi_logging.h"

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
MODULE_AUTHOR("Eric Youngdale");
MODULE_DESCRIPTION("SCSI disk (sd) driver");
MODULE_LICENSE("GPL");

MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
96
97
98
MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
99
MODULE_ALIAS_SCSI_DEVICE(TYPE_ZBC);
100

101
#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
102
#define SD_MINORS	16
103
#else
104
#define SD_MINORS	0
105
106
#endif

107
static void sd_config_discard(struct scsi_disk *, unsigned int);
108
static void sd_config_write_same(struct scsi_disk *);
109
static int  sd_revalidate_disk(struct gendisk *);
110
static void sd_unlock_native_capacity(struct gendisk *disk);
111
112
113
static int  sd_probe(struct device *);
static int  sd_remove(struct device *);
static void sd_shutdown(struct device *);
114
115
static int sd_suspend_system(struct device *);
static int sd_suspend_runtime(struct device *);
116
117
static int sd_resume(struct device *);
static void sd_rescan(struct device *);
118
static blk_status_t sd_init_command(struct scsi_cmnd *SCpnt);
119
static void sd_uninit_command(struct scsi_cmnd *SCpnt);
120
static int sd_done(struct scsi_cmnd *);
121
static void sd_eh_reset(struct scsi_cmnd *);
122
static int sd_eh_action(struct scsi_cmnd *, int);
123
static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
124
static void scsi_disk_release(struct device *cdev);
125

126
static DEFINE_IDA(sd_index_ida);
Linus Torvalds's avatar
Linus Torvalds committed
127
128
129
130

/* This semaphore is used to mediate the 0->1 reference get in the
 * face of object destruction (i.e. we can't allow a get on an
 * object after last put) */
131
static DEFINE_MUTEX(sd_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
132

133
134
static struct kmem_cache *sd_cdb_cache;
static mempool_t *sd_cdb_pool;
135
static mempool_t *sd_page_pool;
136

137
138
139
140
141
static const char *sd_cache_types[] = {
	"write through", "none", "write back",
	"write back, no read (daft)"
};

142
143
static void sd_set_flush_flag(struct scsi_disk *sdkp)
{
144
	bool wc = false, fua = false;
145
146

	if (sdkp->WCE) {
147
		wc = true;
148
		if (sdkp->DPOFUA)
149
			fua = true;
150
151
	}

152
	blk_queue_write_cache(sdkp->disk->queue, wc, fua);
153
154
}

155
static ssize_t
156
157
cache_type_store(struct device *dev, struct device_attribute *attr,
		 const char *buf, size_t count)
158
{
159
	int ct, rcd, wce, sp;
160
	struct scsi_disk *sdkp = to_scsi_disk(dev);
161
162
163
164
165
	struct scsi_device *sdp = sdkp->device;
	char buffer[64];
	char *buffer_data;
	struct scsi_mode_data data;
	struct scsi_sense_hdr sshdr;
166
	static const char temp[] = "temporary ";
167
168
	int len;

169
	if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
170
171
172
173
174
		/* no cache control on RBC devices; theoretically they
		 * can do it, but there's probably so many exceptions
		 * it's not worth the risk */
		return -EINVAL;

175
176
177
178
179
180
181
	if (strncmp(buf, temp, sizeof(temp) - 1) == 0) {
		buf += sizeof(temp) - 1;
		sdkp->cache_override = 1;
	} else {
		sdkp->cache_override = 0;
	}

182
	ct = sysfs_match_string(sd_cache_types, buf);
183
184
	if (ct < 0)
		return -EINVAL;
185

186
	rcd = ct & 0x01 ? 1 : 0;
187
	wce = (ct & 0x02) && !sdkp->write_prot ? 1 : 0;
188
189
190
191

	if (sdkp->cache_override) {
		sdkp->WCE = wce;
		sdkp->RCD = rcd;
192
		sd_set_flush_flag(sdkp);
193
194
195
		return count;
	}

196
197
198
	if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
			    SD_MAX_RETRIES, &data, NULL))
		return -EINVAL;
199
	len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
200
201
202
203
204
205
		  data.block_descriptor_length);
	buffer_data = buffer + data.header_length +
		data.block_descriptor_length;
	buffer_data[2] &= ~0x05;
	buffer_data[2] |= wce << 2 | rcd;
	sp = buffer_data[0] & 0x80 ? 1 : 0;
206
	buffer_data[0] &= ~0x80;
207

208
209
210
211
212
213
	/*
	 * Ensure WP, DPOFUA, and RESERVED fields are cleared in
	 * received mode parameter buffer before doing MODE SELECT.
	 */
	data.device_specific = 0;

214
215
216
	if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
			     SD_MAX_RETRIES, &data, &sshdr)) {
		if (scsi_sense_valid(&sshdr))
217
			sd_print_sense_hdr(sdkp, &sshdr);
218
219
		return -EINVAL;
	}
220
	revalidate_disk(sdkp->disk);
221
222
223
	return count;
}

224
static ssize_t
225
226
227
228
229
230
manage_start_stop_show(struct device *dev, struct device_attribute *attr,
		       char *buf)
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);
	struct scsi_device *sdp = sdkp->device;

231
	return sprintf(buf, "%u\n", sdp->manage_start_stop);
232
233
234
235
236
}

static ssize_t
manage_start_stop_store(struct device *dev, struct device_attribute *attr,
			const char *buf, size_t count)
237
{
238
	struct scsi_disk *sdkp = to_scsi_disk(dev);
239
	struct scsi_device *sdp = sdkp->device;
240
	bool v;
241
242
243
244

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

245
246
247
248
	if (kstrtobool(buf, &v))
		return -EINVAL;

	sdp->manage_start_stop = v;
249
250
251

	return count;
}
252
static DEVICE_ATTR_RW(manage_start_stop);
253

254
static ssize_t
255
256
257
258
allow_restart_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);

259
	return sprintf(buf, "%u\n", sdkp->device->allow_restart);
260
261
262
263
264
}

static ssize_t
allow_restart_store(struct device *dev, struct device_attribute *attr,
		    const char *buf, size_t count)
265
{
266
	bool v;
267
	struct scsi_disk *sdkp = to_scsi_disk(dev);
268
269
270
271
272
	struct scsi_device *sdp = sdkp->device;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

273
	if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
274
275
		return -EINVAL;

276
277
278
279
	if (kstrtobool(buf, &v))
		return -EINVAL;

	sdp->allow_restart = v;
280
281
282

	return count;
}
283
static DEVICE_ATTR_RW(allow_restart);
284

285
static ssize_t
286
cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
287
{
288
	struct scsi_disk *sdkp = to_scsi_disk(dev);
289
290
	int ct = sdkp->RCD + 2*sdkp->WCE;

291
	return sprintf(buf, "%s\n", sd_cache_types[ct]);
292
}
293
static DEVICE_ATTR_RW(cache_type);
294

295
static ssize_t
296
FUA_show(struct device *dev, struct device_attribute *attr, char *buf)
297
{
298
	struct scsi_disk *sdkp = to_scsi_disk(dev);
299

300
	return sprintf(buf, "%u\n", sdkp->DPOFUA);
301
}
302
static DEVICE_ATTR_RO(FUA);
303

304
static ssize_t
305
306
protection_type_show(struct device *dev, struct device_attribute *attr,
		     char *buf)
307
308
309
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);

310
	return sprintf(buf, "%u\n", sdkp->protection_type);
311
312
}

313
static ssize_t
314
315
protection_type_store(struct device *dev, struct device_attribute *attr,
		      const char *buf, size_t count)
316
317
318
319
320
321
322
323
324
325
326
327
328
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);
	unsigned int val;
	int err;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	err = kstrtouint(buf, 10, &val);

	if (err)
		return err;

329
	if (val <= T10_PI_TYPE3_PROTECTION)
330
331
332
333
		sdkp->protection_type = val;

	return count;
}
334
static DEVICE_ATTR_RW(protection_type);
335

336
static ssize_t
337
338
protection_mode_show(struct device *dev, struct device_attribute *attr,
		     char *buf)
339
340
341
342
343
344
345
346
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);
	struct scsi_device *sdp = sdkp->device;
	unsigned int dif, dix;

	dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
	dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type);

347
	if (!dix && scsi_host_dix_capable(sdp->host, T10_PI_TYPE0_PROTECTION)) {
348
349
350
351
352
		dif = 0;
		dix = 1;
	}

	if (!dif && !dix)
353
		return sprintf(buf, "none\n");
354

355
	return sprintf(buf, "%s%u\n", dix ? "dix" : "dif", dif);
356
}
357
static DEVICE_ATTR_RO(protection_mode);
358

359
static ssize_t
360
app_tag_own_show(struct device *dev, struct device_attribute *attr, char *buf)
361
362
363
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);

364
	return sprintf(buf, "%u\n", sdkp->ATO);
365
}
366
static DEVICE_ATTR_RO(app_tag_own);
367

368
static ssize_t
369
370
thin_provisioning_show(struct device *dev, struct device_attribute *attr,
		       char *buf)
371
372
373
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);

374
	return sprintf(buf, "%u\n", sdkp->lbpme);
375
}
376
static DEVICE_ATTR_RO(thin_provisioning);
377

378
/* sysfs_match_string() requires dense arrays */
379
380
381
382
383
384
385
386
387
388
static const char *lbp_mode[] = {
	[SD_LBP_FULL]		= "full",
	[SD_LBP_UNMAP]		= "unmap",
	[SD_LBP_WS16]		= "writesame_16",
	[SD_LBP_WS10]		= "writesame_10",
	[SD_LBP_ZERO]		= "writesame_zero",
	[SD_LBP_DISABLE]	= "disabled",
};

static ssize_t
389
390
provisioning_mode_show(struct device *dev, struct device_attribute *attr,
		       char *buf)
391
392
393
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);

394
	return sprintf(buf, "%s\n", lbp_mode[sdkp->provisioning_mode]);
395
396
397
}

static ssize_t
398
399
provisioning_mode_store(struct device *dev, struct device_attribute *attr,
			const char *buf, size_t count)
400
401
402
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);
	struct scsi_device *sdp = sdkp->device;
403
	int mode;
404
405
406
407

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

408
409
410
411
412
	if (sd_is_zoned(sdkp)) {
		sd_config_discard(sdkp, SD_LBP_DISABLE);
		return count;
	}

413
414
415
	if (sdp->type != TYPE_DISK)
		return -EINVAL;

416
417
	mode = sysfs_match_string(lbp_mode, buf);
	if (mode < 0)
418
419
		return -EINVAL;

420
421
	sd_config_discard(sdkp, mode);

422
	return count;
423
}
424
static DEVICE_ATTR_RW(provisioning_mode);
425

426
/* sysfs_match_string() requires dense arrays */
427
428
429
430
431
432
433
434
435
436
437
438
439
static const char *zeroing_mode[] = {
	[SD_ZERO_WRITE]		= "write",
	[SD_ZERO_WS]		= "writesame",
	[SD_ZERO_WS16_UNMAP]	= "writesame_16_unmap",
	[SD_ZERO_WS10_UNMAP]	= "writesame_10_unmap",
};

static ssize_t
zeroing_mode_show(struct device *dev, struct device_attribute *attr,
		  char *buf)
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);

440
	return sprintf(buf, "%s\n", zeroing_mode[sdkp->zeroing_mode]);
441
442
443
444
445
446
447
}

static ssize_t
zeroing_mode_store(struct device *dev, struct device_attribute *attr,
		   const char *buf, size_t count)
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);
448
	int mode;
449
450
451
452

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

453
454
	mode = sysfs_match_string(zeroing_mode, buf);
	if (mode < 0)
455
456
		return -EINVAL;

457
458
	sdkp->zeroing_mode = mode;

459
460
461
462
	return count;
}
static DEVICE_ATTR_RW(zeroing_mode);

463
static ssize_t
464
465
max_medium_access_timeouts_show(struct device *dev,
				struct device_attribute *attr, char *buf)
466
467
468
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);

469
	return sprintf(buf, "%u\n", sdkp->max_medium_access_timeouts);
470
471
472
}

static ssize_t
473
474
475
max_medium_access_timeouts_store(struct device *dev,
				 struct device_attribute *attr, const char *buf,
				 size_t count)
476
477
478
479
480
481
482
483
484
485
486
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);
	int err;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

	err = kstrtouint(buf, 10, &sdkp->max_medium_access_timeouts);

	return err ? err : count;
}
487
static DEVICE_ATTR_RW(max_medium_access_timeouts);
488

489
static ssize_t
490
491
max_write_same_blocks_show(struct device *dev, struct device_attribute *attr,
			   char *buf)
492
493
494
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);

495
	return sprintf(buf, "%u\n", sdkp->max_ws_blocks);
496
497
498
}

static ssize_t
499
500
max_write_same_blocks_store(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
501
502
503
504
505
506
507
508
509
{
	struct scsi_disk *sdkp = to_scsi_disk(dev);
	struct scsi_device *sdp = sdkp->device;
	unsigned long max;
	int err;

	if (!capable(CAP_SYS_ADMIN))
		return -EACCES;

510
	if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
511
512
513
514
515
516
517
518
519
		return -EINVAL;

	err = kstrtoul(buf, 10, &max);

	if (err)
		return err;

	if (max == 0)
		sdp->no_write_same = 1;
520
521
	else if (max <= SD_MAX_WS16_BLOCKS) {
		sdp->no_write_same = 0;
522
		sdkp->max_ws_blocks = max;
523
	}
524
525
526
527
528

	sd_config_write_same(sdkp);

	return count;
}
529
530
531
532
533
534
535
536
537
538
539
540
static DEVICE_ATTR_RW(max_write_same_blocks);

static struct attribute *sd_disk_attrs[] = {
	&dev_attr_cache_type.attr,
	&dev_attr_FUA.attr,
	&dev_attr_allow_restart.attr,
	&dev_attr_manage_start_stop.attr,
	&dev_attr_protection_type.attr,
	&dev_attr_protection_mode.attr,
	&dev_attr_app_tag_own.attr,
	&dev_attr_thin_provisioning.attr,
	&dev_attr_provisioning_mode.attr,
541
	&dev_attr_zeroing_mode.attr,
542
543
544
	&dev_attr_max_write_same_blocks.attr,
	&dev_attr_max_medium_access_timeouts.attr,
	NULL,
545
};
546
ATTRIBUTE_GROUPS(sd_disk);
547
548
549
550

static struct class sd_disk_class = {
	.name		= "scsi_disk",
	.owner		= THIS_MODULE,
551
	.dev_release	= scsi_disk_release,
552
	.dev_groups	= sd_disk_groups,
553
};
Linus Torvalds's avatar
Linus Torvalds committed
554

555
static const struct dev_pm_ops sd_pm_ops = {
556
	.suspend		= sd_suspend_system,
557
	.resume			= sd_resume,
558
	.poweroff		= sd_suspend_system,
559
	.restore		= sd_resume,
560
	.runtime_suspend	= sd_suspend_runtime,
561
562
563
	.runtime_resume		= sd_resume,
};

Linus Torvalds's avatar
Linus Torvalds committed
564
565
566
static struct scsi_driver sd_template = {
	.gendrv = {
		.name		= "sd",
567
		.owner		= THIS_MODULE,
Linus Torvalds's avatar
Linus Torvalds committed
568
		.probe		= sd_probe,
569
		.probe_type	= PROBE_PREFER_ASYNCHRONOUS,
Linus Torvalds's avatar
Linus Torvalds committed
570
571
		.remove		= sd_remove,
		.shutdown	= sd_shutdown,
572
		.pm		= &sd_pm_ops,
Linus Torvalds's avatar
Linus Torvalds committed
573
574
	},
	.rescan			= sd_rescan,
575
576
	.init_command		= sd_init_command,
	.uninit_command		= sd_uninit_command,
577
	.done			= sd_done,
578
	.eh_action		= sd_eh_action,
579
	.eh_reset		= sd_eh_reset,
Linus Torvalds's avatar
Linus Torvalds committed
580
581
};

582
583
584
585
586
587
588
589
590
591
/*
 * Dummy kobj_map->probe function.
 * The default ->probe function will call modprobe, which is
 * pointless as this module is already loaded.
 */
static struct kobject *sd_default_probe(dev_t devt, int *partno, void *data)
{
	return NULL;
}

Linus Torvalds's avatar
Linus Torvalds committed
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*
 * Device no to disk mapping:
 * 
 *       major         disc2     disc  p1
 *   |............|.............|....|....| <- dev_t
 *    31        20 19          8 7  4 3  0
 * 
 * Inside a major, we have 16k disks, however mapped non-
 * contiguously. The first 16 disks are for major0, the next
 * ones with major1, ... Disk 256 is for major0 again, disk 272 
 * for major1, ... 
 * As we stay compatible with our numbering scheme, we can reuse 
 * the well-know SCSI majors 8, 65--71, 136--143.
 */
static int sd_major(int major_idx)
{
	switch (major_idx) {
	case 0:
		return SCSI_DISK0_MAJOR;
	case 1 ... 7:
		return SCSI_DISK1_MAJOR + major_idx - 1;
	case 8 ... 15:
		return SCSI_DISK8_MAJOR + major_idx - 8;
	default:
		BUG();
		return 0;	/* shut up gcc */
	}
}

621
static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
Linus Torvalds's avatar
Linus Torvalds committed
622
623
624
{
	struct scsi_disk *sdkp = NULL;

625
626
	mutex_lock(&sd_ref_mutex);

Alan Stern's avatar
Alan Stern committed
627
628
629
	if (disk->private_data) {
		sdkp = scsi_disk(disk);
		if (scsi_device_get(sdkp->device) == 0)
630
			get_device(&sdkp->dev);
Alan Stern's avatar
Alan Stern committed
631
632
633
		else
			sdkp = NULL;
	}
634
	mutex_unlock(&sd_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
635
636
637
638
639
640
641
	return sdkp;
}

static void scsi_disk_put(struct scsi_disk *sdkp)
{
	struct scsi_device *sdev = sdkp->device;

642
	mutex_lock(&sd_ref_mutex);
643
	put_device(&sdkp->dev);
Linus Torvalds's avatar
Linus Torvalds committed
644
	scsi_device_put(sdev);
645
	mutex_unlock(&sd_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
646
647
}

648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#ifdef CONFIG_BLK_SED_OPAL
static int sd_sec_submit(void *data, u16 spsp, u8 secp, void *buffer,
		size_t len, bool send)
{
	struct scsi_device *sdev = data;
	u8 cdb[12] = { 0, };
	int ret;

	cdb[0] = send ? SECURITY_PROTOCOL_OUT : SECURITY_PROTOCOL_IN;
	cdb[1] = secp;
	put_unaligned_be16(spsp, &cdb[2]);
	put_unaligned_be32(len, &cdb[6]);

	ret = scsi_execute_req(sdev, cdb,
			send ? DMA_TO_DEVICE : DMA_FROM_DEVICE,
			buffer, len, NULL, SD_TIMEOUT, SD_MAX_RETRIES, NULL);
	return ret <= 0 ? ret : -EIO;
}
#endif /* CONFIG_BLK_SED_OPAL */

668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/*
 * Look up the DIX operation based on whether the command is read or
 * write and whether dix and dif are enabled.
 */
static unsigned int sd_prot_op(bool write, bool dix, bool dif)
{
	/* Lookup table: bit 2 (write), bit 1 (dix), bit 0 (dif) */
	static const unsigned int ops[] = {	/* wrt dix dif */
		SCSI_PROT_NORMAL,		/*  0	0   0  */
		SCSI_PROT_READ_STRIP,		/*  0	0   1  */
		SCSI_PROT_READ_INSERT,		/*  0	1   0  */
		SCSI_PROT_READ_PASS,		/*  0	1   1  */
		SCSI_PROT_NORMAL,		/*  1	0   0  */
		SCSI_PROT_WRITE_INSERT,		/*  1	0   1  */
		SCSI_PROT_WRITE_STRIP,		/*  1	1   0  */
		SCSI_PROT_WRITE_PASS,		/*  1	1   1  */
	};

	return ops[write << 2 | dix << 1 | dif];
}

/*
 * Returns a mask of the protection flags that are valid for a given DIX
 * operation.
 */
static unsigned int sd_prot_flag_mask(unsigned int prot_op)
{
	static const unsigned int flag_mask[] = {
		[SCSI_PROT_NORMAL]		= 0,

		[SCSI_PROT_READ_STRIP]		= SCSI_PROT_TRANSFER_PI |
						  SCSI_PROT_GUARD_CHECK |
						  SCSI_PROT_REF_CHECK |
						  SCSI_PROT_REF_INCREMENT,

		[SCSI_PROT_READ_INSERT]		= SCSI_PROT_REF_INCREMENT |
						  SCSI_PROT_IP_CHECKSUM,

		[SCSI_PROT_READ_PASS]		= SCSI_PROT_TRANSFER_PI |
						  SCSI_PROT_GUARD_CHECK |
						  SCSI_PROT_REF_CHECK |
						  SCSI_PROT_REF_INCREMENT |
						  SCSI_PROT_IP_CHECKSUM,

		[SCSI_PROT_WRITE_INSERT]	= SCSI_PROT_TRANSFER_PI |
						  SCSI_PROT_REF_INCREMENT,

		[SCSI_PROT_WRITE_STRIP]		= SCSI_PROT_GUARD_CHECK |
						  SCSI_PROT_REF_CHECK |
						  SCSI_PROT_REF_INCREMENT |
						  SCSI_PROT_IP_CHECKSUM,

		[SCSI_PROT_WRITE_PASS]		= SCSI_PROT_TRANSFER_PI |
						  SCSI_PROT_GUARD_CHECK |
						  SCSI_PROT_REF_CHECK |
						  SCSI_PROT_REF_INCREMENT |
						  SCSI_PROT_IP_CHECKSUM,
	};

	return flag_mask[prot_op];
}

730
731
static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd,
					   unsigned int dix, unsigned int dif)
732
{
733
734
735
736
737
738
739
740
741
742
743
744
	struct bio *bio = scmd->request->bio;
	unsigned int prot_op = sd_prot_op(rq_data_dir(scmd->request), dix, dif);
	unsigned int protect = 0;

	if (dix) {				/* DIX Type 0, 1, 2, 3 */
		if (bio_integrity_flagged(bio, BIP_IP_CHECKSUM))
			scmd->prot_flags |= SCSI_PROT_IP_CHECKSUM;

		if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
			scmd->prot_flags |= SCSI_PROT_GUARD_CHECK;
	}

745
	if (dif != T10_PI_TYPE3_PROTECTION) {	/* DIX/DIF Type 0, 1, 2 */
746
747
748
749
750
751
752
753
754
755
756
757
758
		scmd->prot_flags |= SCSI_PROT_REF_INCREMENT;

		if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
			scmd->prot_flags |= SCSI_PROT_REF_CHECK;
	}

	if (dif) {				/* DIX/DIF Type 1, 2, 3 */
		scmd->prot_flags |= SCSI_PROT_TRANSFER_PI;

		if (bio_integrity_flagged(bio, BIP_DISK_NOCHECK))
			protect = 3 << 5;	/* Disable target PI checking */
		else
			protect = 1 << 5;	/* Enable target PI checking */
759
760
761
762
	}

	scsi_set_prot_op(scmd, prot_op);
	scsi_set_prot_type(scmd, dif);
763
764
765
	scmd->prot_flags &= sd_prot_flag_mask(prot_op);

	return protect;
766
767
}

768
769
770
771
772
773
static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode)
{
	struct request_queue *q = sdkp->disk->queue;
	unsigned int logical_block_size = sdkp->device->sector_size;
	unsigned int max_blocks = 0;

774
775
776
777
778
	q->limits.discard_alignment =
		sdkp->unmap_alignment * logical_block_size;
	q->limits.discard_granularity =
		max(sdkp->physical_block_size,
		    sdkp->unmap_granularity * logical_block_size);
779
780
	sdkp->provisioning_mode = mode;

781
782
	switch (mode) {

783
	case SD_LBP_FULL:
784
	case SD_LBP_DISABLE:
785
		blk_queue_max_discard_sectors(q, 0);
786
		blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
787
788
789
		return;

	case SD_LBP_UNMAP:
790
791
		max_blocks = min_not_zero(sdkp->max_unmap_blocks,
					  (u32)SD_MAX_WS16_BLOCKS);
792
793
794
		break;

	case SD_LBP_WS16:
795
796
797
798
799
800
		if (sdkp->device->unmap_limit_for_ws)
			max_blocks = sdkp->max_unmap_blocks;
		else
			max_blocks = sdkp->max_ws_blocks;

		max_blocks = min_not_zero(max_blocks, (u32)SD_MAX_WS16_BLOCKS);
801
802
803
		break;

	case SD_LBP_WS10:
804
805
806
807
808
809
		if (sdkp->device->unmap_limit_for_ws)
			max_blocks = sdkp->max_unmap_blocks;
		else
			max_blocks = sdkp->max_ws_blocks;

		max_blocks = min_not_zero(max_blocks, (u32)SD_MAX_WS10_BLOCKS);
810
811
812
		break;

	case SD_LBP_ZERO:
813
814
		max_blocks = min_not_zero(sdkp->max_ws_blocks,
					  (u32)SD_MAX_WS10_BLOCKS);
815
816
817
		break;
	}

818
	blk_queue_max_discard_sectors(q, max_blocks * (logical_block_size >> 9));
819
	blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
820
821
}

822
static blk_status_t sd_setup_unmap_cmnd(struct scsi_cmnd *cmd)
823
{
824
	struct scsi_device *sdp = cmd->device;
825
	struct request *rq = cmd->request;
826
827
	u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
	u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
828
	unsigned int data_len = 24;
829
	char *buf;
830

831
	rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
832
	if (!rq->special_vec.bv_page)
833
		return BLK_STS_RESOURCE;
834
	clear_highpage(rq->special_vec.bv_page);
835
836
837
	rq->special_vec.bv_offset = 0;
	rq->special_vec.bv_len = data_len;
	rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
838

839
840
841
	cmd->cmd_len = 10;
	cmd->cmnd[0] = UNMAP;
	cmd->cmnd[8] = 24;
842

843
844
845
	buf = page_address(rq->special_vec.bv_page);
	put_unaligned_be16(6 + 16, &buf[0]);
	put_unaligned_be16(16, &buf[2]);
846
847
	put_unaligned_be64(lba, &buf[8]);
	put_unaligned_be32(nr_blocks, &buf[16]);
848

849
850
851
	cmd->allowed = SD_MAX_RETRIES;
	cmd->transfersize = data_len;
	rq->timeout = SD_TIMEOUT;
852

853
854
	return scsi_init_io(cmd);
}
855

856
857
static blk_status_t sd_setup_write_same16_cmnd(struct scsi_cmnd *cmd,
		bool unmap)
858
859
860
{
	struct scsi_device *sdp = cmd->device;
	struct request *rq = cmd->request;
861
862
	u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
	u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
863
	u32 data_len = sdp->sector_size;
864

865
	rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
866
	if (!rq->special_vec.bv_page)
867
		return BLK_STS_RESOURCE;
868
	clear_highpage(rq->special_vec.bv_page);
869
870
871
	rq->special_vec.bv_offset = 0;
	rq->special_vec.bv_len = data_len;
	rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
872

873
874
	cmd->cmd_len = 16;
	cmd->cmnd[0] = WRITE_SAME_16;
875
	if (unmap)
876
		cmd->cmnd[1] = 0x8; /* UNMAP */
877
878
	put_unaligned_be64(lba, &cmd->cmnd[2]);
	put_unaligned_be32(nr_blocks, &cmd->cmnd[10]);
879

880
881
	cmd->allowed = SD_MAX_RETRIES;
	cmd->transfersize = data_len;
882
	rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT;
883

884
885
	return scsi_init_io(cmd);
}
886

887
888
static blk_status_t sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd,
		bool unmap)
889
890
891
{
	struct scsi_device *sdp = cmd->device;
	struct request *rq = cmd->request;
892
893
	u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
	u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
894
	u32 data_len = sdp->sector_size;
895

896
	rq->special_vec.bv_page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
897
	if (!rq->special_vec.bv_page)
898
		return BLK_STS_RESOURCE;
899
	clear_highpage(rq->special_vec.bv_page);
900
901
	rq->special_vec.bv_offset = 0;
	rq->special_vec.bv_len = data_len;
902
	rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
903

904
905
906
907
	cmd->cmd_len = 10;
	cmd->cmnd[0] = WRITE_SAME;
	if (unmap)
		cmd->cmnd[1] = 0x8; /* UNMAP */
908
909
	put_unaligned_be32(lba, &cmd->cmnd[2]);
	put_unaligned_be16(nr_blocks, &cmd->cmnd[7]);
910

Christoph Hellwig's avatar
Christoph Hellwig committed
911
	cmd->allowed = SD_MAX_RETRIES;
912
	cmd->transfersize = data_len;
913
	rq->timeout = unmap ? SD_TIMEOUT : SD_WRITE_SAME_TIMEOUT;
914

915
	return scsi_init_io(cmd);
916
}
917

918
static blk_status_t sd_setup_write_zeroes_cmnd(struct scsi_cmnd *cmd)
919
920
921
922
{
	struct request *rq = cmd->request;
	struct scsi_device *sdp = cmd->device;
	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
923
924
	u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
	u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
925

926
	if (!(rq->cmd_flags & REQ_NOUNMAP)) {
927
928
		switch (sdkp->zeroing_mode) {
		case SD_ZERO_WS16_UNMAP:
929
			return sd_setup_write_same16_cmnd(cmd, true);
930
		case SD_ZERO_WS10_UNMAP:
931
			return sd_setup_write_same10_cmnd(cmd, true);
932
933
		}
	}
934

935
	if (sdp->no_write_same)
936
		return BLK_STS_TARGET;
937

938
	if (sdkp->ws16 || lba > 0xffffffff || nr_blocks > 0xffff)
939
		return sd_setup_write_same16_cmnd(cmd, false);
940

941
	return sd_setup_write_same10_cmnd(cmd, false);
942
943
}

944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
static void sd_config_write_same(struct scsi_disk *sdkp)
{
	struct request_queue *q = sdkp->disk->queue;
	unsigned int logical_block_size = sdkp->device->sector_size;

	if (sdkp->device->no_write_same) {
		sdkp->max_ws_blocks = 0;
		goto out;
	}

	/* Some devices can not handle block counts above 0xffff despite
	 * supporting WRITE SAME(16). Consequently we default to 64k
	 * blocks per I/O unless the device explicitly advertises a
	 * bigger limit.
	 */
959
960
961
962
963
964
965
966
967
968
	if (sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS)
		sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
						   (u32)SD_MAX_WS16_BLOCKS);
	else if (sdkp->ws16 || sdkp->ws10 || sdkp->device->no_report_opcodes)
		sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
						   (u32)SD_MAX_WS10_BLOCKS);
	else {
		sdkp->device->no_write_same = 1;
		sdkp->max_ws_blocks = 0;
	}
969

970
971
972
973
974
975
976
977
978
	if (sdkp->lbprz && sdkp->lbpws)
		sdkp->zeroing_mode = SD_ZERO_WS16_UNMAP;
	else if (sdkp->lbprz && sdkp->lbpws10)
		sdkp->zeroing_mode = SD_ZERO_WS10_UNMAP;
	else if (sdkp->max_ws_blocks)
		sdkp->zeroing_mode = SD_ZERO_WS;
	else
		sdkp->zeroing_mode = SD_ZERO_WRITE;

979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
	if (sdkp->max_ws_blocks &&
	    sdkp->physical_block_size > logical_block_size) {
		/*
		 * Reporting a maximum number of blocks that is not aligned
		 * on the device physical size would cause a large write same
		 * request to be split into physically unaligned chunks by
		 * __blkdev_issue_write_zeroes() and __blkdev_issue_write_same()
		 * even if the caller of these functions took care to align the
		 * large request. So make sure the maximum reported is aligned
		 * to the device physical block size. This is only an optional
		 * optimization for regular disks, but this is mandatory to
		 * avoid failure of large write same requests directed at
		 * sequential write required zones of host-managed ZBC disks.
		 */
		sdkp->max_ws_blocks =
			round_down(sdkp->max_ws_blocks,
				   bytes_to_logical(sdkp->device,
						    sdkp->physical_block_size));
	}

999
out:
1000
1001
	blk_queue_max_write_same_sectors(q, sdkp->max_ws_blocks *
					 (logical_block_size >> 9));
1002
1003
	blk_queue_max_write_zeroes_sectors(q, sdkp->max_ws_blocks *
					 (logical_block_size >> 9));
1004
1005
1006
1007
}

/**
 * sd_setup_write_same_cmnd - write the same data to multiple blocks
1008
 * @cmd: command to prepare
1009
 *
1010
1011
 * Will set up either WRITE SAME(10) or WRITE SAME(16) depending on
 * the preference indicated by the target device.
1012
 **/
1013
static blk_status_t sd_setup_write_same_cmnd(struct scsi_cmnd *cmd)
1014
{
1015
1016
	struct request *rq = cmd->request;
	struct scsi_device *sdp = cmd->device;
1017
1018
	struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
	struct bio *bio = rq->bio;
1019
1020
	u64 lba = sectors_to_logical(sdp, blk_rq_pos(rq));
	u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
1021
	blk_status_t ret;
1022
1023

	if (sdkp->device->no_write_same)
1024
		return BLK_STS_TARGET;
1025

1026
	BUG_ON(bio_offset(bio) || bio_iovec(bio).bv_len != sdp->sector_size);
1027
1028
1029

	rq->timeout = SD_WRITE_SAME_TIMEOUT;

1030
	if (sdkp->ws16 || lba > 0xffffffff || nr_blocks > 0xffff) {
1031
1032
		cmd->cmd_len = 16;
		cmd->cmnd[0] = WRITE_SAME_16;
1033
1034
		put_unaligned_be64(lba, &cmd->cmnd[2]);
		put_unaligned_be32(nr_blocks, &cmd->cmnd[10]);
1035
	} else {
1036
1037
		cmd->cmd_len = 10;
		cmd->cmnd[0] = WRITE_SAME;