blob: 65738b0aad367d1e0c3f850b8d5f7d019bbbfa0f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sd.c Copyright (C) 1992 Drew Eckhardt
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4 *
5 * Linux scsi disk driver
6 * Initial versions: Drew Eckhardt
7 * Subsequent revisions: Eric Youngdale
8 * Modification history:
9 * - Drew Eckhardt <drew@colorado.edu> original
10 * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple
11 * outstanding request, and other enhancements.
12 * Support loadable low-level scsi drivers.
13 * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using
14 * eight major numbers.
15 * - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
16 * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in
17 * sd_init and cleanups.
18 * - Alex Davis <letmein@erols.com> Fix problem where partition info
19 * not being read in sd_open. Fix problem where removable media
20 * could be ejected after sd_open.
21 * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
22 * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox
23 * <willy@debian.org>, Kurt Garloff <garloff@suse.de>:
24 * Support 32k/1M disks.
25 *
26 * Logging policy (needs CONFIG_SCSI_LOGGING defined):
27 * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
28 * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
29 * - entering sd_ioctl: SCSI_LOG_IOCTL level 1
30 * - entering other commands: SCSI_LOG_HLQUEUE level 3
31 * Note: when the logging level is set by the user, it must be greater
32 * than the level indicated above to trigger output.
33 */
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/fs.h>
37#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/mm.h>
39#include <linux/bio.h>
40#include <linux/genhd.h>
41#include <linux/hdreg.h>
42#include <linux/errno.h>
43#include <linux/idr.h>
44#include <linux/interrupt.h>
45#include <linux/init.h>
46#include <linux/blkdev.h>
47#include <linux/blkpg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/delay.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010049#include <linux/mutex.h>
James Bottomley7404ad3b2008-08-31 10:41:52 -050050#include <linux/string_helpers.h>
Arjan van de Ven4ace92f2009-01-04 05:32:28 -080051#include <linux/async.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Lin Ming54f575882011-12-05 09:20:26 +080053#include <linux/pm_runtime.h>
Christoph Hellwig924d55b2015-10-15 14:10:49 +020054#include <linux/pr.h>
Christoph Hellwig8475c812016-09-11 19:35:41 +020055#include <linux/t10-pi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/uaccess.h>
Dave Hansen8f76d152009-04-21 16:43:27 -070057#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#include <scsi/scsi.h>
60#include <scsi/scsi_cmnd.h>
61#include <scsi/scsi_dbg.h>
62#include <scsi/scsi_device.h>
63#include <scsi/scsi_driver.h>
64#include <scsi/scsi_eh.h>
65#include <scsi/scsi_host.h>
66#include <scsi/scsi_ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <scsi/scsicam.h>
68
Martin K. Petersenaa916962008-06-17 12:47:32 -040069#include "sd.h"
Dan Williamsa7a20d12012-03-22 17:05:11 -070070#include "scsi_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include "scsi_logging.h"
72
Rene Hermanf018fa52006-03-08 00:14:20 -080073MODULE_AUTHOR("Eric Youngdale");
74MODULE_DESCRIPTION("SCSI disk (sd) driver");
75MODULE_LICENSE("GPL");
76
77MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
78MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
79MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
80MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
81MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
82MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
83MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
84MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
85MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
86MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
87MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
88MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
89MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
90MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
91MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
92MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
Michael Tokarevd7b8bcb02006-10-27 16:02:37 +040093MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
94MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
95MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
Hannes Reinecke89d94752016-10-18 15:40:34 +090096MODULE_ALIAS_SCSI_DEVICE(TYPE_ZBC);
Rene Hermanf018fa52006-03-08 00:14:20 -080097
Tejun Heo870d6652008-08-25 19:47:25 +090098#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
Tejun Heof615b482008-08-25 19:47:24 +090099#define SD_MINORS 16
Tejun Heo870d6652008-08-25 19:47:25 +0900100#else
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900101#define SD_MINORS 0
Tejun Heo870d6652008-08-25 19:47:25 +0900102#endif
103
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500104static void sd_config_discard(struct scsi_disk *, unsigned int);
Martin K. Petersen5db44862012-09-18 12:19:32 -0400105static void sd_config_write_same(struct scsi_disk *);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800106static int sd_revalidate_disk(struct gendisk *);
Tejun Heo72ec24b2010-05-15 20:09:32 +0200107static void sd_unlock_native_capacity(struct gendisk *disk);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800108static int sd_probe(struct device *);
109static int sd_remove(struct device *);
110static void sd_shutdown(struct device *);
Oliver Neukum95897912013-09-16 13:28:15 +0200111static int sd_suspend_system(struct device *);
112static int sd_suspend_runtime(struct device *);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800113static int sd_resume(struct device *);
114static void sd_rescan(struct device *);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200115static int sd_init_command(struct scsi_cmnd *SCpnt);
116static void sd_uninit_command(struct scsi_cmnd *SCpnt);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800117static int sd_done(struct scsi_cmnd *);
James Bottomley24510792013-11-11 13:44:53 +0100118static int sd_eh_action(struct scsi_cmnd *, int);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800119static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
Tony Jonesee959b02008-02-22 00:13:36 +0100120static void scsi_disk_release(struct device *cdev);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800121static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
Hannes Reineckeef613292014-10-24 14:27:00 +0200122static void sd_print_result(const struct scsi_disk *, const char *, int);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800123
Tejun Heo4034cc62009-02-21 11:04:45 +0900124static DEFINE_SPINLOCK(sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +0900125static DEFINE_IDA(sd_index_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* This semaphore is used to mediate the 0->1 reference get in the
128 * face of object destruction (i.e. we can't allow a get on an
129 * object after last put) */
Arjan van de Ven0b950672006-01-11 13:16:10 +0100130static DEFINE_MUTEX(sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
H Hartley Sweeten439d77f2010-08-10 18:01:20 -0700132static struct kmem_cache *sd_cdb_cache;
133static mempool_t *sd_cdb_pool;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400134
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600135static const char *sd_cache_types[] = {
136 "write through", "none", "write back",
137 "write back, no read (daft)"
138};
139
Vaughan Caocb2fb682014-06-03 17:37:30 +0800140static void sd_set_flush_flag(struct scsi_disk *sdkp)
141{
Jens Axboeeb310e22016-03-30 10:06:11 -0600142 bool wc = false, fua = false;
Vaughan Caocb2fb682014-06-03 17:37:30 +0800143
144 if (sdkp->WCE) {
Jens Axboeeb310e22016-03-30 10:06:11 -0600145 wc = true;
Vaughan Caocb2fb682014-06-03 17:37:30 +0800146 if (sdkp->DPOFUA)
Jens Axboeeb310e22016-03-30 10:06:11 -0600147 fua = true;
Vaughan Caocb2fb682014-06-03 17:37:30 +0800148 }
149
Jens Axboeeb310e22016-03-30 10:06:11 -0600150 blk_queue_write_cache(sdkp->disk->queue, wc, fua);
Vaughan Caocb2fb682014-06-03 17:37:30 +0800151}
152
Tony Jonesee959b02008-02-22 00:13:36 +0100153static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700154cache_type_store(struct device *dev, struct device_attribute *attr,
155 const char *buf, size_t count)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600156{
157 int i, ct = -1, rcd, wce, sp;
Tony Jonesee959b02008-02-22 00:13:36 +0100158 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600159 struct scsi_device *sdp = sdkp->device;
160 char buffer[64];
161 char *buffer_data;
162 struct scsi_mode_data data;
163 struct scsi_sense_hdr sshdr;
Ben Hutchings2ee3e262013-05-27 19:07:19 +0100164 static const char temp[] = "temporary ";
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600165 int len;
166
Hannes Reinecke89d94752016-10-18 15:40:34 +0900167 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600168 /* no cache control on RBC devices; theoretically they
169 * can do it, but there's probably so many exceptions
170 * it's not worth the risk */
171 return -EINVAL;
172
James Bottomley39c60a02013-04-24 14:02:53 -0700173 if (strncmp(buf, temp, sizeof(temp) - 1) == 0) {
174 buf += sizeof(temp) - 1;
175 sdkp->cache_override = 1;
176 } else {
177 sdkp->cache_override = 0;
178 }
179
Tobias Klauser6391a112006-06-08 22:23:48 -0700180 for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
H Hartley Sweeten439d77f2010-08-10 18:01:20 -0700181 len = strlen(sd_cache_types[i]);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600182 if (strncmp(sd_cache_types[i], buf, len) == 0 &&
183 buf[len] == '\n') {
184 ct = i;
185 break;
186 }
187 }
188 if (ct < 0)
189 return -EINVAL;
190 rcd = ct & 0x01 ? 1 : 0;
Sujit Reddy Thumma2eefd572014-08-11 15:40:37 +0300191 wce = (ct & 0x02) && !sdkp->write_prot ? 1 : 0;
James Bottomley39c60a02013-04-24 14:02:53 -0700192
193 if (sdkp->cache_override) {
194 sdkp->WCE = wce;
195 sdkp->RCD = rcd;
Vaughan Caocb2fb682014-06-03 17:37:30 +0800196 sd_set_flush_flag(sdkp);
James Bottomley39c60a02013-04-24 14:02:53 -0700197 return count;
198 }
199
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600200 if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
201 SD_MAX_RETRIES, &data, NULL))
202 return -EINVAL;
Andrew Mortona9312fb2006-03-25 03:08:30 -0800203 len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600204 data.block_descriptor_length);
205 buffer_data = buffer + data.header_length +
206 data.block_descriptor_length;
207 buffer_data[2] &= ~0x05;
208 buffer_data[2] |= wce << 2 | rcd;
209 sp = buffer_data[0] & 0x80 ? 1 : 0;
Gabriel Krisman Bertazi2c5d16d2015-10-30 16:04:43 -0200210 buffer_data[0] &= ~0x80;
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600211
212 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
213 SD_MAX_RETRIES, &data, &sshdr)) {
214 if (scsi_sense_valid(&sshdr))
Martin K. Petersene73aec82007-02-27 22:40:55 -0500215 sd_print_sense_hdr(sdkp, &sshdr);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600216 return -EINVAL;
217 }
Andrew Pattersonf98a8ca2008-09-04 14:27:35 -0600218 revalidate_disk(sdkp->disk);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600219 return count;
220}
221
Tony Jonesee959b02008-02-22 00:13:36 +0100222static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700223manage_start_stop_show(struct device *dev, struct device_attribute *attr,
224 char *buf)
225{
226 struct scsi_disk *sdkp = to_scsi_disk(dev);
227 struct scsi_device *sdp = sdkp->device;
228
229 return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
230}
231
232static ssize_t
233manage_start_stop_store(struct device *dev, struct device_attribute *attr,
234 const char *buf, size_t count)
Tejun Heoc3c94c5a2007-03-21 00:13:59 +0900235{
Tony Jonesee959b02008-02-22 00:13:36 +0100236 struct scsi_disk *sdkp = to_scsi_disk(dev);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +0900237 struct scsi_device *sdp = sdkp->device;
238
239 if (!capable(CAP_SYS_ADMIN))
240 return -EACCES;
241
242 sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
243
244 return count;
245}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700246static DEVICE_ATTR_RW(manage_start_stop);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +0900247
Tony Jonesee959b02008-02-22 00:13:36 +0100248static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700249allow_restart_show(struct device *dev, struct device_attribute *attr, char *buf)
250{
251 struct scsi_disk *sdkp = to_scsi_disk(dev);
252
253 return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
254}
255
256static ssize_t
257allow_restart_store(struct device *dev, struct device_attribute *attr,
258 const char *buf, size_t count)
Brian Kinga144c5a2006-06-27 11:10:31 -0500259{
Tony Jonesee959b02008-02-22 00:13:36 +0100260 struct scsi_disk *sdkp = to_scsi_disk(dev);
Brian Kinga144c5a2006-06-27 11:10:31 -0500261 struct scsi_device *sdp = sdkp->device;
262
263 if (!capable(CAP_SYS_ADMIN))
264 return -EACCES;
265
Hannes Reinecke89d94752016-10-18 15:40:34 +0900266 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
Brian Kinga144c5a2006-06-27 11:10:31 -0500267 return -EINVAL;
268
269 sdp->allow_restart = simple_strtoul(buf, NULL, 10);
270
271 return count;
272}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700273static DEVICE_ATTR_RW(allow_restart);
Brian Kinga144c5a2006-06-27 11:10:31 -0500274
Tony Jonesee959b02008-02-22 00:13:36 +0100275static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700276cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600277{
Tony Jonesee959b02008-02-22 00:13:36 +0100278 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600279 int ct = sdkp->RCD + 2*sdkp->WCE;
280
281 return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
282}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700283static DEVICE_ATTR_RW(cache_type);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600284
Tony Jonesee959b02008-02-22 00:13:36 +0100285static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700286FUA_show(struct device *dev, struct device_attribute *attr, char *buf)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600287{
Tony Jonesee959b02008-02-22 00:13:36 +0100288 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600289
290 return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
291}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700292static DEVICE_ATTR_RO(FUA);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600293
Tony Jonesee959b02008-02-22 00:13:36 +0100294static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700295protection_type_show(struct device *dev, struct device_attribute *attr,
296 char *buf)
Martin K. Petersene0597d72008-07-17 04:28:34 -0400297{
298 struct scsi_disk *sdkp = to_scsi_disk(dev);
299
300 return snprintf(buf, 20, "%u\n", sdkp->protection_type);
301}
302
303static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700304protection_type_store(struct device *dev, struct device_attribute *attr,
305 const char *buf, size_t count)
Martin K. Petersen81724992012-08-28 14:29:34 -0400306{
307 struct scsi_disk *sdkp = to_scsi_disk(dev);
308 unsigned int val;
309 int err;
310
311 if (!capable(CAP_SYS_ADMIN))
312 return -EACCES;
313
314 err = kstrtouint(buf, 10, &val);
315
316 if (err)
317 return err;
318
Christoph Hellwig8475c812016-09-11 19:35:41 +0200319 if (val >= 0 && val <= T10_PI_TYPE3_PROTECTION)
Martin K. Petersen81724992012-08-28 14:29:34 -0400320 sdkp->protection_type = val;
321
322 return count;
323}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700324static DEVICE_ATTR_RW(protection_type);
Martin K. Petersen81724992012-08-28 14:29:34 -0400325
326static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700327protection_mode_show(struct device *dev, struct device_attribute *attr,
328 char *buf)
Martin K. Petersen518fa8e2010-10-08 01:36:24 -0400329{
330 struct scsi_disk *sdkp = to_scsi_disk(dev);
331 struct scsi_device *sdp = sdkp->device;
332 unsigned int dif, dix;
333
334 dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
335 dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type);
336
Christoph Hellwig8475c812016-09-11 19:35:41 +0200337 if (!dix && scsi_host_dix_capable(sdp->host, T10_PI_TYPE0_PROTECTION)) {
Martin K. Petersen518fa8e2010-10-08 01:36:24 -0400338 dif = 0;
339 dix = 1;
340 }
341
342 if (!dif && !dix)
343 return snprintf(buf, 20, "none\n");
344
345 return snprintf(buf, 20, "%s%u\n", dix ? "dix" : "dif", dif);
346}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700347static DEVICE_ATTR_RO(protection_mode);
Martin K. Petersen518fa8e2010-10-08 01:36:24 -0400348
349static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700350app_tag_own_show(struct device *dev, struct device_attribute *attr, char *buf)
Martin K. Petersene0597d72008-07-17 04:28:34 -0400351{
352 struct scsi_disk *sdkp = to_scsi_disk(dev);
353
354 return snprintf(buf, 20, "%u\n", sdkp->ATO);
355}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700356static DEVICE_ATTR_RO(app_tag_own);
Martin K. Petersene0597d72008-07-17 04:28:34 -0400357
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500358static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700359thin_provisioning_show(struct device *dev, struct device_attribute *attr,
360 char *buf)
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500361{
362 struct scsi_disk *sdkp = to_scsi_disk(dev);
363
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500364 return snprintf(buf, 20, "%u\n", sdkp->lbpme);
365}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700366static DEVICE_ATTR_RO(thin_provisioning);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500367
368static const char *lbp_mode[] = {
369 [SD_LBP_FULL] = "full",
370 [SD_LBP_UNMAP] = "unmap",
371 [SD_LBP_WS16] = "writesame_16",
372 [SD_LBP_WS10] = "writesame_10",
373 [SD_LBP_ZERO] = "writesame_zero",
374 [SD_LBP_DISABLE] = "disabled",
375};
376
377static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700378provisioning_mode_show(struct device *dev, struct device_attribute *attr,
379 char *buf)
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500380{
381 struct scsi_disk *sdkp = to_scsi_disk(dev);
382
383 return snprintf(buf, 20, "%s\n", lbp_mode[sdkp->provisioning_mode]);
384}
385
386static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700387provisioning_mode_store(struct device *dev, struct device_attribute *attr,
388 const char *buf, size_t count)
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500389{
390 struct scsi_disk *sdkp = to_scsi_disk(dev);
391 struct scsi_device *sdp = sdkp->device;
392
393 if (!capable(CAP_SYS_ADMIN))
394 return -EACCES;
395
Hannes Reinecke89d94752016-10-18 15:40:34 +0900396 if (sd_is_zoned(sdkp)) {
397 sd_config_discard(sdkp, SD_LBP_DISABLE);
398 return count;
399 }
400
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500401 if (sdp->type != TYPE_DISK)
402 return -EINVAL;
403
404 if (!strncmp(buf, lbp_mode[SD_LBP_UNMAP], 20))
405 sd_config_discard(sdkp, SD_LBP_UNMAP);
406 else if (!strncmp(buf, lbp_mode[SD_LBP_WS16], 20))
407 sd_config_discard(sdkp, SD_LBP_WS16);
408 else if (!strncmp(buf, lbp_mode[SD_LBP_WS10], 20))
409 sd_config_discard(sdkp, SD_LBP_WS10);
410 else if (!strncmp(buf, lbp_mode[SD_LBP_ZERO], 20))
411 sd_config_discard(sdkp, SD_LBP_ZERO);
412 else if (!strncmp(buf, lbp_mode[SD_LBP_DISABLE], 20))
413 sd_config_discard(sdkp, SD_LBP_DISABLE);
414 else
415 return -EINVAL;
416
417 return count;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500418}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700419static DEVICE_ATTR_RW(provisioning_mode);
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500420
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -0500421static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700422max_medium_access_timeouts_show(struct device *dev,
423 struct device_attribute *attr, char *buf)
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -0500424{
425 struct scsi_disk *sdkp = to_scsi_disk(dev);
426
427 return snprintf(buf, 20, "%u\n", sdkp->max_medium_access_timeouts);
428}
429
430static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700431max_medium_access_timeouts_store(struct device *dev,
432 struct device_attribute *attr, const char *buf,
433 size_t count)
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -0500434{
435 struct scsi_disk *sdkp = to_scsi_disk(dev);
436 int err;
437
438 if (!capable(CAP_SYS_ADMIN))
439 return -EACCES;
440
441 err = kstrtouint(buf, 10, &sdkp->max_medium_access_timeouts);
442
443 return err ? err : count;
444}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700445static DEVICE_ATTR_RW(max_medium_access_timeouts);
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -0500446
Martin K. Petersen5db44862012-09-18 12:19:32 -0400447static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700448max_write_same_blocks_show(struct device *dev, struct device_attribute *attr,
449 char *buf)
Martin K. Petersen5db44862012-09-18 12:19:32 -0400450{
451 struct scsi_disk *sdkp = to_scsi_disk(dev);
452
453 return snprintf(buf, 20, "%u\n", sdkp->max_ws_blocks);
454}
455
456static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700457max_write_same_blocks_store(struct device *dev, struct device_attribute *attr,
458 const char *buf, size_t count)
Martin K. Petersen5db44862012-09-18 12:19:32 -0400459{
460 struct scsi_disk *sdkp = to_scsi_disk(dev);
461 struct scsi_device *sdp = sdkp->device;
462 unsigned long max;
463 int err;
464
465 if (!capable(CAP_SYS_ADMIN))
466 return -EACCES;
467
Hannes Reinecke89d94752016-10-18 15:40:34 +0900468 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
Martin K. Petersen5db44862012-09-18 12:19:32 -0400469 return -EINVAL;
470
471 err = kstrtoul(buf, 10, &max);
472
473 if (err)
474 return err;
475
476 if (max == 0)
477 sdp->no_write_same = 1;
Martin K. Petersen66c28f92013-06-06 22:15:55 -0400478 else if (max <= SD_MAX_WS16_BLOCKS) {
479 sdp->no_write_same = 0;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400480 sdkp->max_ws_blocks = max;
Martin K. Petersen66c28f92013-06-06 22:15:55 -0400481 }
Martin K. Petersen5db44862012-09-18 12:19:32 -0400482
483 sd_config_write_same(sdkp);
484
485 return count;
486}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700487static DEVICE_ATTR_RW(max_write_same_blocks);
Martin K. Petersen5db44862012-09-18 12:19:32 -0400488
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700489static struct attribute *sd_disk_attrs[] = {
490 &dev_attr_cache_type.attr,
491 &dev_attr_FUA.attr,
492 &dev_attr_allow_restart.attr,
493 &dev_attr_manage_start_stop.attr,
494 &dev_attr_protection_type.attr,
495 &dev_attr_protection_mode.attr,
496 &dev_attr_app_tag_own.attr,
497 &dev_attr_thin_provisioning.attr,
498 &dev_attr_provisioning_mode.attr,
499 &dev_attr_max_write_same_blocks.attr,
500 &dev_attr_max_medium_access_timeouts.attr,
501 NULL,
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600502};
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700503ATTRIBUTE_GROUPS(sd_disk);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600504
505static struct class sd_disk_class = {
506 .name = "scsi_disk",
507 .owner = THIS_MODULE,
Tony Jonesee959b02008-02-22 00:13:36 +0100508 .dev_release = scsi_disk_release,
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700509 .dev_groups = sd_disk_groups,
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600510};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Aaron Lu691e3d32012-11-09 15:27:55 +0800512static const struct dev_pm_ops sd_pm_ops = {
Oliver Neukum95897912013-09-16 13:28:15 +0200513 .suspend = sd_suspend_system,
Aaron Lu691e3d32012-11-09 15:27:55 +0800514 .resume = sd_resume,
Oliver Neukum95897912013-09-16 13:28:15 +0200515 .poweroff = sd_suspend_system,
Aaron Lu691e3d32012-11-09 15:27:55 +0800516 .restore = sd_resume,
Oliver Neukum95897912013-09-16 13:28:15 +0200517 .runtime_suspend = sd_suspend_runtime,
Aaron Lu691e3d32012-11-09 15:27:55 +0800518 .runtime_resume = sd_resume,
519};
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521static struct scsi_driver sd_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 .gendrv = {
523 .name = "sd",
Christoph Hellwig3af6b352014-11-12 18:34:51 +0100524 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 .probe = sd_probe,
526 .remove = sd_remove,
527 .shutdown = sd_shutdown,
Aaron Lu691e3d32012-11-09 15:27:55 +0800528 .pm = &sd_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 },
530 .rescan = sd_rescan,
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200531 .init_command = sd_init_command,
532 .uninit_command = sd_uninit_command,
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800533 .done = sd_done,
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -0500534 .eh_action = sd_eh_action,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535};
536
537/*
Hannes Reinecke0761df92013-05-10 11:06:16 +0200538 * Dummy kobj_map->probe function.
539 * The default ->probe function will call modprobe, which is
540 * pointless as this module is already loaded.
541 */
542static struct kobject *sd_default_probe(dev_t devt, int *partno, void *data)
543{
544 return NULL;
545}
546
547/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * Device no to disk mapping:
549 *
550 * major disc2 disc p1
551 * |............|.............|....|....| <- dev_t
552 * 31 20 19 8 7 4 3 0
553 *
554 * Inside a major, we have 16k disks, however mapped non-
555 * contiguously. The first 16 disks are for major0, the next
556 * ones with major1, ... Disk 256 is for major0 again, disk 272
557 * for major1, ...
558 * As we stay compatible with our numbering scheme, we can reuse
559 * the well-know SCSI majors 8, 65--71, 136--143.
560 */
561static int sd_major(int major_idx)
562{
563 switch (major_idx) {
564 case 0:
565 return SCSI_DISK0_MAJOR;
566 case 1 ... 7:
567 return SCSI_DISK1_MAJOR + major_idx - 1;
568 case 8 ... 15:
569 return SCSI_DISK8_MAJOR + major_idx - 8;
570 default:
571 BUG();
572 return 0; /* shut up gcc */
573 }
574}
575
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +0100576static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 struct scsi_disk *sdkp = NULL;
579
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +0100580 mutex_lock(&sd_ref_mutex);
581
Alan Stern39b7f1e2005-11-04 14:44:41 -0500582 if (disk->private_data) {
583 sdkp = scsi_disk(disk);
584 if (scsi_device_get(sdkp->device) == 0)
Tony Jonesee959b02008-02-22 00:13:36 +0100585 get_device(&sdkp->dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500586 else
587 sdkp = NULL;
588 }
Arjan van de Ven0b950672006-01-11 13:16:10 +0100589 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return sdkp;
591}
592
593static void scsi_disk_put(struct scsi_disk *sdkp)
594{
595 struct scsi_device *sdev = sdkp->device;
596
Arjan van de Ven0b950672006-01-11 13:16:10 +0100597 mutex_lock(&sd_ref_mutex);
Tony Jonesee959b02008-02-22 00:13:36 +0100598 put_device(&sdkp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 scsi_device_put(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100600 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Martin K. Petersenc6115292014-09-26 19:20:08 -0400603static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd,
604 unsigned int dix, unsigned int dif)
605{
606 struct bio *bio = scmd->request->bio;
607 unsigned int prot_op = sd_prot_op(rq_data_dir(scmd->request), dix, dif);
608 unsigned int protect = 0;
609
610 if (dix) { /* DIX Type 0, 1, 2, 3 */
611 if (bio_integrity_flagged(bio, BIP_IP_CHECKSUM))
612 scmd->prot_flags |= SCSI_PROT_IP_CHECKSUM;
613
614 if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
615 scmd->prot_flags |= SCSI_PROT_GUARD_CHECK;
616 }
617
Christoph Hellwig8475c812016-09-11 19:35:41 +0200618 if (dif != T10_PI_TYPE3_PROTECTION) { /* DIX/DIF Type 0, 1, 2 */
Martin K. Petersenc6115292014-09-26 19:20:08 -0400619 scmd->prot_flags |= SCSI_PROT_REF_INCREMENT;
620
621 if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
622 scmd->prot_flags |= SCSI_PROT_REF_CHECK;
623 }
624
625 if (dif) { /* DIX/DIF Type 1, 2, 3 */
626 scmd->prot_flags |= SCSI_PROT_TRANSFER_PI;
627
628 if (bio_integrity_flagged(bio, BIP_DISK_NOCHECK))
629 protect = 3 << 5; /* Disable target PI checking */
630 else
631 protect = 1 << 5; /* Enable target PI checking */
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -0400632 }
633
634 scsi_set_prot_op(scmd, prot_op);
635 scsi_set_prot_type(scmd, dif);
Martin K. Petersenc6115292014-09-26 19:20:08 -0400636 scmd->prot_flags &= sd_prot_flag_mask(prot_op);
637
638 return protect;
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -0400639}
640
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500641static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode)
642{
643 struct request_queue *q = sdkp->disk->queue;
644 unsigned int logical_block_size = sdkp->device->sector_size;
645 unsigned int max_blocks = 0;
646
Martin K. Petersen79850902014-11-07 00:08:13 -0500647 q->limits.discard_zeroes_data = 0;
Martin K. Petersen39773722015-11-13 16:46:47 -0500648
649 /*
650 * When LBPRZ is reported, discard alignment and granularity
651 * must be fixed to the logical block size. Otherwise the block
652 * layer will drop misaligned portions of the request which can
653 * lead to data corruption. If LBPRZ is not set, we honor the
654 * device preference.
655 */
656 if (sdkp->lbprz) {
657 q->limits.discard_alignment = 0;
Martin K. Petersen6540a652016-03-05 17:52:02 -0500658 q->limits.discard_granularity = logical_block_size;
Martin K. Petersen39773722015-11-13 16:46:47 -0500659 } else {
660 q->limits.discard_alignment = sdkp->unmap_alignment *
661 logical_block_size;
662 q->limits.discard_granularity =
663 max(sdkp->physical_block_size,
664 sdkp->unmap_granularity * logical_block_size);
665 }
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500666
Martin K. Petersen89730392012-02-13 15:39:00 -0500667 sdkp->provisioning_mode = mode;
668
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500669 switch (mode) {
670
671 case SD_LBP_DISABLE:
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600672 blk_queue_max_discard_sectors(q, 0);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500673 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
674 return;
675
676 case SD_LBP_UNMAP:
Martin K. Petersen5db44862012-09-18 12:19:32 -0400677 max_blocks = min_not_zero(sdkp->max_unmap_blocks,
678 (u32)SD_MAX_WS16_BLOCKS);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500679 break;
680
681 case SD_LBP_WS16:
Martin K. Petersen5db44862012-09-18 12:19:32 -0400682 max_blocks = min_not_zero(sdkp->max_ws_blocks,
683 (u32)SD_MAX_WS16_BLOCKS);
Martin K. Petersen79850902014-11-07 00:08:13 -0500684 q->limits.discard_zeroes_data = sdkp->lbprz;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500685 break;
686
687 case SD_LBP_WS10:
Martin K. Petersen5db44862012-09-18 12:19:32 -0400688 max_blocks = min_not_zero(sdkp->max_ws_blocks,
689 (u32)SD_MAX_WS10_BLOCKS);
Martin K. Petersen79850902014-11-07 00:08:13 -0500690 q->limits.discard_zeroes_data = sdkp->lbprz;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500691 break;
692
693 case SD_LBP_ZERO:
Martin K. Petersen5db44862012-09-18 12:19:32 -0400694 max_blocks = min_not_zero(sdkp->max_ws_blocks,
695 (u32)SD_MAX_WS10_BLOCKS);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500696 q->limits.discard_zeroes_data = 1;
697 break;
698 }
699
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600700 blk_queue_max_discard_sectors(q, max_blocks * (logical_block_size >> 9));
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500701 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/**
Martin K. Petersen26e85fc2012-09-18 12:19:31 -0400705 * sd_setup_discard_cmnd - unmap blocks on thinly provisioned device
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200706 * @sdp: scsi device to operate one
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500707 * @rq: Request to prepare
708 *
709 * Will issue either UNMAP or WRITE SAME(16) depending on preference
710 * indicated by target device.
711 **/
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200712static int sd_setup_discard_cmnd(struct scsi_cmnd *cmd)
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500713{
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200714 struct request *rq = cmd->request;
715 struct scsi_device *sdp = cmd->device;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500716 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
Martin K. Petersen26e85fc2012-09-18 12:19:31 -0400717 sector_t sector = blk_rq_pos(rq);
718 unsigned int nr_sectors = blk_rq_sectors(rq);
719 unsigned int nr_bytes = blk_rq_bytes(rq);
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200720 unsigned int len;
FUJITA Tomonorif1126e92010-07-01 19:49:18 +0900721 int ret;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500722 char *buf;
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200723 struct page *page;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500724
Martin K. Petersen26e85fc2012-09-18 12:19:31 -0400725 sector >>= ilog2(sdp->sector_size) - 9;
726 nr_sectors >>= ilog2(sdp->sector_size) - 9;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500727
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200728 page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
729 if (!page)
730 return BLKPREP_DEFER;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500731
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500732 switch (sdkp->provisioning_mode) {
733 case SD_LBP_UNMAP:
734 buf = page_address(page);
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200735
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200736 cmd->cmd_len = 10;
737 cmd->cmnd[0] = UNMAP;
738 cmd->cmnd[8] = 24;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500739
740 put_unaligned_be16(6 + 16, &buf[0]);
741 put_unaligned_be16(16, &buf[2]);
742 put_unaligned_be64(sector, &buf[8]);
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200743 put_unaligned_be32(nr_sectors, &buf[16]);
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500744
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200745 len = 24;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500746 break;
747
748 case SD_LBP_WS16:
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200749 cmd->cmd_len = 16;
750 cmd->cmnd[0] = WRITE_SAME_16;
751 cmd->cmnd[1] = 0x8; /* UNMAP */
752 put_unaligned_be64(sector, &cmd->cmnd[2]);
753 put_unaligned_be32(nr_sectors, &cmd->cmnd[10]);
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200754
755 len = sdkp->device->sector_size;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500756 break;
757
758 case SD_LBP_WS10:
759 case SD_LBP_ZERO:
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200760 cmd->cmd_len = 10;
761 cmd->cmnd[0] = WRITE_SAME;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500762 if (sdkp->provisioning_mode == SD_LBP_WS10)
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200763 cmd->cmnd[1] = 0x8; /* UNMAP */
764 put_unaligned_be32(sector, &cmd->cmnd[2]);
765 put_unaligned_be16(nr_sectors, &cmd->cmnd[7]);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500766
767 len = sdkp->device->sector_size;
768 break;
769
770 default:
Martin K. Petersen0fb5b1f2016-02-04 00:52:12 -0500771 ret = BLKPREP_INVALID;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500772 goto out;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500773 }
774
Jens Axboedc4a9302014-04-16 21:37:30 -0600775 rq->completion_data = page;
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200776 rq->timeout = SD_TIMEOUT;
777
778 cmd->transfersize = len;
Christoph Hellwige4200f82014-06-28 16:18:59 +0200779 cmd->allowed = SD_MAX_RETRIES;
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200780
781 /*
782 * Initially __data_len is set to the amount of data that needs to be
783 * transferred to the target. This amount depends on whether WRITE SAME
784 * or UNMAP is being used. After the scatterlist has been mapped by
785 * scsi_init_io() we set __data_len to the size of the area to be
786 * discarded on disk. This allows us to report completion on the full
787 * amount of blocks described by the request.
788 */
Ming Lin37e58232016-03-22 00:24:44 -0700789 blk_add_request_payload(rq, page, 0, len);
Christoph Hellwig3c356bd2014-09-05 18:20:23 -0700790 ret = scsi_init_io(cmd);
Martin K. Petersen26e85fc2012-09-18 12:19:31 -0400791 rq->__data_len = nr_bytes;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500792
793out:
Jens Axboeb4f42e22014-04-10 09:46:28 -0600794 if (ret != BLKPREP_OK)
FUJITA Tomonori610a6342010-07-08 10:16:17 +0200795 __free_page(page);
FUJITA Tomonorif1126e92010-07-01 19:49:18 +0900796 return ret;
797}
798
Martin K. Petersen5db44862012-09-18 12:19:32 -0400799static void sd_config_write_same(struct scsi_disk *sdkp)
800{
801 struct request_queue *q = sdkp->disk->queue;
802 unsigned int logical_block_size = sdkp->device->sector_size;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400803
804 if (sdkp->device->no_write_same) {
805 sdkp->max_ws_blocks = 0;
806 goto out;
807 }
808
809 /* Some devices can not handle block counts above 0xffff despite
810 * supporting WRITE SAME(16). Consequently we default to 64k
811 * blocks per I/O unless the device explicitly advertises a
812 * bigger limit.
813 */
Martin K. Petersen66c28f92013-06-06 22:15:55 -0400814 if (sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS)
815 sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
816 (u32)SD_MAX_WS16_BLOCKS);
817 else if (sdkp->ws16 || sdkp->ws10 || sdkp->device->no_report_opcodes)
818 sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
819 (u32)SD_MAX_WS10_BLOCKS);
820 else {
821 sdkp->device->no_write_same = 1;
822 sdkp->max_ws_blocks = 0;
823 }
Martin K. Petersen5db44862012-09-18 12:19:32 -0400824
825out:
Martin K. Petersen66c28f92013-06-06 22:15:55 -0400826 blk_queue_max_write_same_sectors(q, sdkp->max_ws_blocks *
827 (logical_block_size >> 9));
Martin K. Petersen5db44862012-09-18 12:19:32 -0400828}
829
830/**
831 * sd_setup_write_same_cmnd - write the same data to multiple blocks
Christoph Hellwig59b11342014-06-28 12:22:22 +0200832 * @cmd: command to prepare
Martin K. Petersen5db44862012-09-18 12:19:32 -0400833 *
834 * Will issue either WRITE SAME(10) or WRITE SAME(16) depending on
835 * preference indicated by target device.
836 **/
Christoph Hellwig59b11342014-06-28 12:22:22 +0200837static int sd_setup_write_same_cmnd(struct scsi_cmnd *cmd)
Martin K. Petersen5db44862012-09-18 12:19:32 -0400838{
Christoph Hellwig59b11342014-06-28 12:22:22 +0200839 struct request *rq = cmd->request;
840 struct scsi_device *sdp = cmd->device;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400841 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
842 struct bio *bio = rq->bio;
843 sector_t sector = blk_rq_pos(rq);
844 unsigned int nr_sectors = blk_rq_sectors(rq);
845 unsigned int nr_bytes = blk_rq_bytes(rq);
846 int ret;
847
848 if (sdkp->device->no_write_same)
Martin K. Petersen0fb5b1f2016-02-04 00:52:12 -0500849 return BLKPREP_INVALID;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400850
Kent Overstreeta4ad39b12013-08-07 14:24:32 -0700851 BUG_ON(bio_offset(bio) || bio_iovec(bio).bv_len != sdp->sector_size);
Martin K. Petersen5db44862012-09-18 12:19:32 -0400852
Hannes Reinecke89d94752016-10-18 15:40:34 +0900853 if (sd_is_zoned(sdkp)) {
854 ret = sd_zbc_setup_write_cmnd(cmd);
855 if (ret != BLKPREP_OK)
856 return ret;
857 }
858
Martin K. Petersen5db44862012-09-18 12:19:32 -0400859 sector >>= ilog2(sdp->sector_size) - 9;
860 nr_sectors >>= ilog2(sdp->sector_size) - 9;
861
Martin K. Petersen5db44862012-09-18 12:19:32 -0400862 rq->timeout = SD_WRITE_SAME_TIMEOUT;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400863
864 if (sdkp->ws16 || sector > 0xffffffff || nr_sectors > 0xffff) {
Christoph Hellwig59b11342014-06-28 12:22:22 +0200865 cmd->cmd_len = 16;
866 cmd->cmnd[0] = WRITE_SAME_16;
867 put_unaligned_be64(sector, &cmd->cmnd[2]);
868 put_unaligned_be32(nr_sectors, &cmd->cmnd[10]);
Martin K. Petersen5db44862012-09-18 12:19:32 -0400869 } else {
Christoph Hellwig59b11342014-06-28 12:22:22 +0200870 cmd->cmd_len = 10;
871 cmd->cmnd[0] = WRITE_SAME;
872 put_unaligned_be32(sector, &cmd->cmnd[2]);
873 put_unaligned_be16(nr_sectors, &cmd->cmnd[7]);
Martin K. Petersen5db44862012-09-18 12:19:32 -0400874 }
875
Christoph Hellwig59b11342014-06-28 12:22:22 +0200876 cmd->transfersize = sdp->sector_size;
Christoph Hellwiga25ee542014-06-28 12:29:19 +0200877 cmd->allowed = SD_MAX_RETRIES;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400878
Christoph Hellwig59b11342014-06-28 12:22:22 +0200879 /*
880 * For WRITE_SAME the data transferred in the DATA IN buffer is
881 * different from the amount of data actually written to the target.
882 *
883 * We set up __data_len to the amount of data transferred from the
884 * DATA IN buffer so that blk_rq_map_sg set up the proper S/G list
885 * to transfer a single sector of data first, but then reset it to
886 * the amount of data to be written right after so that the I/O path
887 * knows how much to actually write.
888 */
889 rq->__data_len = sdp->sector_size;
Christoph Hellwig3c356bd2014-09-05 18:20:23 -0700890 ret = scsi_init_io(cmd);
Christoph Hellwig59b11342014-06-28 12:22:22 +0200891 rq->__data_len = nr_bytes;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400892 return ret;
893}
894
Christoph Hellwiga118c6c2014-06-28 12:08:05 +0200895static int sd_setup_flush_cmnd(struct scsi_cmnd *cmd)
FUJITA Tomonori90467c22010-07-03 17:45:34 +0900896{
Christoph Hellwiga118c6c2014-06-28 12:08:05 +0200897 struct request *rq = cmd->request;
FUJITA Tomonori90467c22010-07-03 17:45:34 +0900898
Christoph Hellwiga118c6c2014-06-28 12:08:05 +0200899 /* flush requests don't perform I/O, zero the S/G table */
900 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
901
902 cmd->cmnd[0] = SYNCHRONIZE_CACHE;
903 cmd->cmd_len = 10;
904 cmd->transfersize = 0;
905 cmd->allowed = SD_MAX_RETRIES;
906
K. Y. Srinivasan26b9fd82014-07-18 17:11:27 +0200907 rq->timeout = rq->q->rq_timeout * SD_FLUSH_TIMEOUT_MULTIPLIER;
Christoph Hellwiga118c6c2014-06-28 12:08:05 +0200908 return BLKPREP_OK;
FUJITA Tomonori90467c22010-07-03 17:45:34 +0900909}
910
Christoph Hellwig87949ee2014-06-28 12:40:18 +0200911static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200913 struct request *rq = SCpnt->request;
914 struct scsi_device *sdp = SCpnt->device;
Christoph Hellwig776b23a2006-01-06 18:34:07 +0100915 struct gendisk *disk = rq->rq_disk;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900916 struct scsi_disk *sdkp = scsi_disk(disk);
Tejun Heo83096eb2009-05-07 22:24:39 +0900917 sector_t block = blk_rq_pos(rq);
Linus Torvalds18351072008-08-05 21:42:21 -0700918 sector_t threshold;
Tejun Heo83096eb2009-05-07 22:24:39 +0900919 unsigned int this_count = blk_rq_sectors(rq);
Martin K. Petersenc6115292014-09-26 19:20:08 -0400920 unsigned int dif, dix;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900921 bool zoned_write = sd_is_zoned(sdkp) && rq_data_dir(rq) == WRITE;
Martin K. Petersenc6115292014-09-26 19:20:08 -0400922 int ret;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400923 unsigned char protect;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500924
Hannes Reinecke89d94752016-10-18 15:40:34 +0900925 if (zoned_write) {
926 ret = sd_zbc_setup_write_cmnd(SCpnt);
927 if (ret != BLKPREP_OK)
928 return ret;
929 }
930
Christoph Hellwig3c356bd2014-09-05 18:20:23 -0700931 ret = scsi_init_io(SCpnt);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500932 if (ret != BLKPREP_OK)
933 goto out;
934 SCpnt = rq->special;
935
936 /* from here on until we're complete, any goto out
937 * is used for a killable error condition */
938 ret = BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200940 SCSI_LOG_HLQUEUE(1,
941 scmd_printk(KERN_INFO, SCpnt,
942 "%s: block=%llu, count=%d\n",
943 __func__, (unsigned long long)block, this_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (!sdp || !scsi_device_online(sdp) ||
Tejun Heo83096eb2009-05-07 22:24:39 +0900946 block + blk_rq_sectors(rq) > get_capacity(disk)) {
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500947 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
Tejun Heo83096eb2009-05-07 22:24:39 +0900948 "Finishing %u sectors\n",
949 blk_rq_sectors(rq)));
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500950 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
951 "Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500952 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954
955 if (sdp->changed) {
956 /*
957 * quietly refuse to do anything to a changed disc until
958 * the changed bit has been reset
959 */
Alan Stern3ff55882010-09-07 11:44:01 -0400960 /* printk("SCSI disk has been changed or is not present. Prohibiting further I/O.\n"); */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500961 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500963
Hans de Goedea0899d42008-01-20 11:12:26 +0100964 /*
Linus Torvalds18351072008-08-05 21:42:21 -0700965 * Some SD card readers can't handle multi-sector accesses which touch
966 * the last one or two hardware sectors. Split accesses as needed.
Hans de Goedea0899d42008-01-20 11:12:26 +0100967 */
Linus Torvalds18351072008-08-05 21:42:21 -0700968 threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
969 (sdp->sector_size / 512);
970
971 if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
972 if (block < threshold) {
973 /* Access up to the threshold but not beyond */
974 this_count = threshold - block;
975 } else {
976 /* Access only a single hardware sector */
977 this_count = sdp->sector_size / 512;
978 }
979 }
Hans de Goedea0899d42008-01-20 11:12:26 +0100980
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500981 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
982 (unsigned long long)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 /*
985 * If we have a 1K hardware sectorsize, prevent access to single
986 * 512 byte sectors. In theory we could handle this - in fact
987 * the scsi cdrom driver must be able to handle this because
988 * we typically use 1K blocksizes, and cdroms typically have
989 * 2K hardware sectorsizes. Of course, things are simpler
990 * with the cdrom, since it is read-only. For performance
991 * reasons, the filesystems should be able to handle this
992 * and not force the scsi disk driver to use bounce buffers
993 * for this.
994 */
995 if (sdp->sector_size == 1024) {
Tejun Heo83096eb2009-05-07 22:24:39 +0900996 if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500997 scmd_printk(KERN_ERR, SCpnt,
998 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500999 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 } else {
1001 block = block >> 1;
1002 this_count = this_count >> 1;
1003 }
1004 }
1005 if (sdp->sector_size == 2048) {
Tejun Heo83096eb2009-05-07 22:24:39 +09001006 if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001007 scmd_printk(KERN_ERR, SCpnt,
1008 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 } else {
1011 block = block >> 2;
1012 this_count = this_count >> 2;
1013 }
1014 }
1015 if (sdp->sector_size == 4096) {
Tejun Heo83096eb2009-05-07 22:24:39 +09001016 if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001017 scmd_printk(KERN_ERR, SCpnt,
1018 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001019 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 } else {
1021 block = block >> 3;
1022 this_count = this_count >> 3;
1023 }
1024 }
1025 if (rq_data_dir(rq) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 SCpnt->cmnd[0] = WRITE_6;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001027
Martin K. Petersen8c579ab2012-08-28 14:29:33 -04001028 if (blk_integrity_rq(rq))
Martin K. Petersenc6115292014-09-26 19:20:08 -04001029 sd_dif_prepare(SCpnt);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 } else if (rq_data_dir(rq) == READ) {
1032 SCpnt->cmnd[0] = READ_6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 } else {
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001034 scmd_printk(KERN_ERR, SCpnt, "Unknown command %d\n", req_op(rq));
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001035 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001038 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
Tejun Heo83096eb2009-05-07 22:24:39 +09001039 "%s %d/%u 512 byte blocks.\n",
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001040 (rq_data_dir(rq) == WRITE) ?
1041 "writing" : "reading", this_count,
Tejun Heo83096eb2009-05-07 22:24:39 +09001042 blk_rq_sectors(rq)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Martin K. Petersenc6115292014-09-26 19:20:08 -04001044 dix = scsi_prot_sg_count(SCpnt);
1045 dif = scsi_host_dif_capable(SCpnt->device->host, sdkp->protection_type);
1046
1047 if (dif || dix)
1048 protect = sd_setup_protect_cmnd(SCpnt, dix, dif);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001049 else
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001050 protect = 0;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001051
Christoph Hellwig8475c812016-09-11 19:35:41 +02001052 if (protect && sdkp->protection_type == T10_PI_TYPE2_PROTECTION) {
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001053 SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
1054
1055 if (unlikely(SCpnt->cmnd == NULL)) {
1056 ret = BLKPREP_DEFER;
1057 goto out;
1058 }
1059
1060 SCpnt->cmd_len = SD_EXT_CDB_SIZE;
1061 memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
1062 SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
1063 SCpnt->cmnd[7] = 0x18;
1064 SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001065 SCpnt->cmnd[10] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001066
1067 /* LBA */
1068 SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
1069 SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
1070 SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
1071 SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
1072 SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
1073 SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
1074 SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
1075 SCpnt->cmnd[19] = (unsigned char) block & 0xff;
1076
1077 /* Expected Indirect LBA */
1078 SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
1079 SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
1080 SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
1081 SCpnt->cmnd[23] = (unsigned char) block & 0xff;
1082
1083 /* Transfer length */
1084 SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
1085 SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
1086 SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
1087 SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
Akinobu Mitae430cbc2014-06-02 22:56:47 +09001088 } else if (sdp->use_16_for_rw || (this_count > 0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 SCpnt->cmnd[0] += READ_16 - READ_6;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001090 SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
1092 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
1093 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
1094 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
1095 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
1096 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
1097 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
1098 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
1099 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
1100 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
1101 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
1102 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
1103 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
1104 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001105 scsi_device_protection(SCpnt->device) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 SCpnt->device->use_10_for_rw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 SCpnt->cmnd[0] += READ_10 - READ_6;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001108 SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
1110 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
1111 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
1112 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
1113 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
1114 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
1115 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
1116 } else {
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001117 if (unlikely(rq->cmd_flags & REQ_FUA)) {
Tejun Heo007365a2006-01-06 09:53:52 +01001118 /*
1119 * This happens only if this drive failed
1120 * 10byte rw command with ILLEGAL_REQUEST
1121 * during operation and thus turned off
1122 * use_10_for_rw.
1123 */
Martin K. Petersene73aec82007-02-27 22:40:55 -05001124 scmd_printk(KERN_ERR, SCpnt,
1125 "FUA write on READ/WRITE(6) drive\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001126 goto out;
Tejun Heo007365a2006-01-06 09:53:52 +01001127 }
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
1130 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
1131 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
1132 SCpnt->cmnd[4] = (unsigned char) this_count;
1133 SCpnt->cmnd[5] = 0;
1134 }
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001135 SCpnt->sdb.length = this_count * sdp->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 /*
1138 * We shouldn't disconnect in the middle of a sector, so with a dumb
1139 * host adapter, it's safe to assume that we can at least transfer
1140 * this many bytes between each connect / disconnect.
1141 */
1142 SCpnt->transfersize = sdp->sector_size;
1143 SCpnt->underflow = this_count << 9;
1144 SCpnt->allowed = SD_MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 * This indicates that the command is ready from our end to be
1148 * queued.
1149 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001150 ret = BLKPREP_OK;
1151 out:
Hannes Reinecke89d94752016-10-18 15:40:34 +09001152 if (zoned_write && ret != BLKPREP_OK)
1153 sd_zbc_cancel_write_cmnd(SCpnt);
1154
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001155 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001158static int sd_init_command(struct scsi_cmnd *cmd)
1159{
1160 struct request *rq = cmd->request;
1161
Mike Christiec2df40d2016-06-05 14:32:17 -05001162 switch (req_op(rq)) {
1163 case REQ_OP_DISCARD:
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001164 return sd_setup_discard_cmnd(cmd);
Mike Christiec2df40d2016-06-05 14:32:17 -05001165 case REQ_OP_WRITE_SAME:
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001166 return sd_setup_write_same_cmnd(cmd);
Mike Christie3a5e02c2016-06-05 14:32:23 -05001167 case REQ_OP_FLUSH:
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001168 return sd_setup_flush_cmnd(cmd);
Mike Christiec2df40d2016-06-05 14:32:17 -05001169 case REQ_OP_READ:
1170 case REQ_OP_WRITE:
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001171 return sd_setup_read_write_cmnd(cmd);
Hannes Reinecke89d94752016-10-18 15:40:34 +09001172 case REQ_OP_ZONE_REPORT:
1173 return sd_zbc_setup_report_cmnd(cmd);
1174 case REQ_OP_ZONE_RESET:
1175 return sd_zbc_setup_reset_cmnd(cmd);
Mike Christiec2df40d2016-06-05 14:32:17 -05001176 default:
1177 BUG();
1178 }
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001179}
1180
1181static void sd_uninit_command(struct scsi_cmnd *SCpnt)
1182{
1183 struct request *rq = SCpnt->request;
1184
Mike Christiec2df40d2016-06-05 14:32:17 -05001185 if (req_op(rq) == REQ_OP_DISCARD)
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001186 __free_page(rq->completion_data);
1187
1188 if (SCpnt->cmnd != rq->cmd) {
1189 mempool_free(SCpnt->cmnd, sd_cdb_pool);
1190 SCpnt->cmnd = NULL;
1191 SCpnt->cmd_len = 0;
1192 }
1193}
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195/**
1196 * sd_open - open a scsi disk device
1197 * @inode: only i_rdev member may be used
1198 * @filp: only f_mode and f_flags may be used
1199 *
1200 * Returns 0 if successful. Returns a negated errno value in case
1201 * of error.
1202 *
1203 * Note: This can be called from a user context (e.g. fsck(1) )
1204 * or from within the kernel (e.g. as a result of a mount(1) ).
1205 * In the latter case @inode and @filp carry an abridged amount
1206 * of information as noted above.
Arnd Bergmann409f3492010-07-07 16:51:29 +02001207 *
1208 * Locking: called with bdev->bd_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 **/
Al Viro0338e292008-03-02 10:41:04 -05001210static int sd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Al Viro0338e292008-03-02 10:41:04 -05001212 struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 struct scsi_device *sdev;
1214 int retval;
1215
Al Viro0338e292008-03-02 10:41:04 -05001216 if (!sdkp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return -ENXIO;
1218
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001219 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 sdev = sdkp->device;
1222
1223 /*
1224 * If the device is in error recovery, wait until it is done.
1225 * If the device is offline, then disallow any access to it.
1226 */
1227 retval = -ENXIO;
1228 if (!scsi_block_when_processing_errors(sdev))
1229 goto error_out;
1230
1231 if (sdev->removable || sdkp->write_prot)
Al Viro0338e292008-03-02 10:41:04 -05001232 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /*
1235 * If the drive is empty, just let the open fail.
1236 */
1237 retval = -ENOMEDIUM;
Al Viro0338e292008-03-02 10:41:04 -05001238 if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 goto error_out;
1240
1241 /*
1242 * If the device has the write protect tab set, have the open fail
1243 * if the user expects to be able to write to the thing.
1244 */
1245 retval = -EROFS;
Al Viro0338e292008-03-02 10:41:04 -05001246 if (sdkp->write_prot && (mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 goto error_out;
1248
1249 /*
1250 * It is possible that the disk changing stuff resulted in
1251 * the device being taken offline. If this is the case,
1252 * report this to the user, and don't pretend that the
1253 * open actually succeeded.
1254 */
1255 retval = -ENXIO;
1256 if (!scsi_device_online(sdev))
1257 goto error_out;
1258
Arnd Bergmann409f3492010-07-07 16:51:29 +02001259 if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (scsi_block_when_processing_errors(sdev))
1261 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
1262 }
1263
1264 return 0;
1265
1266error_out:
1267 scsi_disk_put(sdkp);
1268 return retval;
1269}
1270
1271/**
1272 * sd_release - invoked when the (last) close(2) is called on this
1273 * scsi disk.
1274 * @inode: only i_rdev member may be used
1275 * @filp: only f_mode and f_flags may be used
1276 *
1277 * Returns 0.
1278 *
1279 * Note: may block (uninterruptible) if error recovery is underway
1280 * on this disk.
Arnd Bergmann409f3492010-07-07 16:51:29 +02001281 *
1282 * Locking: called with bdev->bd_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 **/
Al Virodb2a1442013-05-05 21:52:57 -04001284static void sd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 struct scsi_disk *sdkp = scsi_disk(disk);
1287 struct scsi_device *sdev = sdkp->device;
1288
James Bottomley56937f72007-03-11 12:25:33 -05001289 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Alan Stern7e443312010-09-07 11:27:52 -04001291 if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (scsi_block_when_processing_errors(sdev))
1293 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
1294 }
1295
1296 /*
1297 * XXX and what if there are packets in flight and this close()
1298 * XXX is followed by a "rmmod sd_mod"?
1299 */
Alan Stern478a8a02010-06-16 14:52:17 -04001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 scsi_disk_put(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001304static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
1307 struct scsi_device *sdp = sdkp->device;
1308 struct Scsi_Host *host = sdp->host;
Martin K. Petersenf08bb1e2016-03-28 21:18:56 -04001309 sector_t capacity = logical_to_sectors(sdp, sdkp->capacity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 int diskinfo[4];
1311
1312 /* default to most commonly used values */
Martin K. Petersenf08bb1e2016-03-28 21:18:56 -04001313 diskinfo[0] = 0x40; /* 1 << 6 */
1314 diskinfo[1] = 0x20; /* 1 << 5 */
1315 diskinfo[2] = capacity >> 11;
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /* override with calculated, extended default, or driver values */
1318 if (host->hostt->bios_param)
Martin K. Petersenf08bb1e2016-03-28 21:18:56 -04001319 host->hostt->bios_param(sdp, bdev, capacity, diskinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 else
Martin K. Petersenf08bb1e2016-03-28 21:18:56 -04001321 scsicam_bios_param(bdev, capacity, diskinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001323 geo->heads = diskinfo[0];
1324 geo->sectors = diskinfo[1];
1325 geo->cylinders = diskinfo[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return 0;
1327}
1328
1329/**
1330 * sd_ioctl - process an ioctl
1331 * @inode: only i_rdev/i_bdev members may be used
1332 * @filp: only f_mode and f_flags may be used
1333 * @cmd: ioctl command number
1334 * @arg: this is third argument given to ioctl(2) system call.
1335 * Often contains a pointer.
1336 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001337 * Returns 0 if successful (some ioctls return positive numbers on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 * success as well). Returns a negated errno value in case of error.
1339 *
1340 * Note: most ioctls are forward onto the block subsystem or further
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001341 * down in the scsi subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 **/
Al Viro0338e292008-03-02 10:41:04 -05001343static int sd_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 unsigned int cmd, unsigned long arg)
1345{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 struct gendisk *disk = bdev->bd_disk;
Nao Nishijimafe2d1852011-08-25 18:04:14 +09001347 struct scsi_disk *sdkp = scsi_disk(disk);
1348 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 void __user *p = (void __user *)arg;
1350 int error;
1351
Nao Nishijimafe2d1852011-08-25 18:04:14 +09001352 SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, "
1353 "cmd=0x%x\n", disk->disk_name, cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Paolo Bonzini0bfc96c2012-01-12 16:01:28 +01001355 error = scsi_verify_blk_ioctl(bdev, cmd);
1356 if (error < 0)
1357 return error;
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /*
1360 * If we are in the middle of error recovery, don't let anyone
1361 * else try and use this device. Also, if error recovery fails, it
1362 * may try and take the device offline, in which case all further
1363 * access to the device is prohibited.
1364 */
Christoph Hellwig906d15f2014-10-11 16:25:31 +02001365 error = scsi_ioctl_block_when_processing_errors(sdp, cmd,
1366 (mode & FMODE_NDELAY) != 0);
1367 if (error)
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /*
1371 * Send SCSI addressing ioctls directly to mid level, send other
1372 * ioctls to block level and then onto mid level if they can't be
1373 * resolved.
1374 */
1375 switch (cmd) {
1376 case SCSI_IOCTL_GET_IDLUN:
1377 case SCSI_IOCTL_GET_BUS_NUMBER:
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001378 error = scsi_ioctl(sdp, cmd, p);
1379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 default:
Paolo Bonzini577ebb32012-01-12 16:01:27 +01001381 error = scsi_cmd_blk_ioctl(bdev, mode, cmd, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (error != -ENOTTY)
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001383 break;
1384 error = scsi_ioctl(sdp, cmd, p);
1385 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001387out:
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001388 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391static void set_media_not_present(struct scsi_disk *sdkp)
1392{
Tejun Heo2bae0092010-12-18 18:42:23 +01001393 if (sdkp->media_present)
1394 sdkp->device->changed = 1;
1395
1396 if (sdkp->device->removable) {
1397 sdkp->media_present = 0;
1398 sdkp->capacity = 0;
1399 }
1400}
1401
1402static int media_not_present(struct scsi_disk *sdkp,
1403 struct scsi_sense_hdr *sshdr)
1404{
1405 if (!scsi_sense_valid(sshdr))
1406 return 0;
1407
1408 /* not invoked for commands that could return deferred errors */
1409 switch (sshdr->sense_key) {
1410 case UNIT_ATTENTION:
1411 case NOT_READY:
1412 /* medium not present */
1413 if (sshdr->asc == 0x3A) {
1414 set_media_not_present(sdkp);
1415 return 1;
1416 }
1417 }
1418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
1421/**
Tejun Heo2bae0092010-12-18 18:42:23 +01001422 * sd_check_events - check media events
1423 * @disk: kernel device descriptor
1424 * @clearing: disk events currently being cleared
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 *
Tejun Heo2bae0092010-12-18 18:42:23 +01001426 * Returns mask of DISK_EVENT_*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 *
1428 * Note: this function is invoked from the block subsystem.
1429 **/
Tejun Heo2bae0092010-12-18 18:42:23 +01001430static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Hannes Reineckeeb72d0b2016-04-26 08:06:58 +02001432 struct scsi_disk *sdkp = scsi_disk_get(disk);
1433 struct scsi_device *sdp;
James Bottomley001aac22007-12-02 19:10:40 +02001434 struct scsi_sense_hdr *sshdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int retval;
1436
Hannes Reineckeeb72d0b2016-04-26 08:06:58 +02001437 if (!sdkp)
1438 return 0;
1439
1440 sdp = sdkp->device;
Tejun Heo2bae0092010-12-18 18:42:23 +01001441 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_check_events\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /*
1444 * If the device is offline, don't send any commands - just pretend as
1445 * if the command failed. If the device ever comes back online, we
1446 * can deal with it then. It is only because of unrecoverable errors
1447 * that we would ever take a device offline in the first place.
1448 */
Kay Sievers285e9672007-08-14 14:10:39 +02001449 if (!scsi_device_online(sdp)) {
1450 set_media_not_present(sdkp);
Kay Sievers285e9672007-08-14 14:10:39 +02001451 goto out;
1452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 /*
1455 * Using TEST_UNIT_READY enables differentiation between drive with
1456 * no cartridge loaded - NOT READY, drive with changed cartridge -
1457 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
1458 *
1459 * Drives that auto spin down. eg iomega jaz 1G, will be started
1460 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
1461 * sd_revalidate() is called.
1462 */
1463 retval = -ENODEV;
Kay Sievers285e9672007-08-14 14:10:39 +02001464
James Bottomley001aac22007-12-02 19:10:40 +02001465 if (scsi_block_when_processing_errors(sdp)) {
1466 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
1467 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
1468 sshdr);
1469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Tejun Heo2bae0092010-12-18 18:42:23 +01001471 /* failed to execute TUR, assume media not present */
1472 if (host_byte(retval)) {
Kay Sievers285e9672007-08-14 14:10:39 +02001473 set_media_not_present(sdkp);
Kay Sievers285e9672007-08-14 14:10:39 +02001474 goto out;
1475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Tejun Heo2bae0092010-12-18 18:42:23 +01001477 if (media_not_present(sdkp, sshdr))
1478 goto out;
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /*
1481 * For removable scsi disk we have to recognise the presence
Tejun Heo2bae0092010-12-18 18:42:23 +01001482 * of a disk in the drive.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 */
Tejun Heo2bae0092010-12-18 18:42:23 +01001484 if (!sdkp->media_present)
1485 sdp->changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 sdkp->media_present = 1;
Kay Sievers285e9672007-08-14 14:10:39 +02001487out:
Alan Stern3ff55882010-09-07 11:44:01 -04001488 /*
Tejun Heo2bae0092010-12-18 18:42:23 +01001489 * sdp->changed is set under the following conditions:
Alan Stern3ff55882010-09-07 11:44:01 -04001490 *
Tejun Heo2bae0092010-12-18 18:42:23 +01001491 * Medium present state has changed in either direction.
1492 * Device has indicated UNIT_ATTENTION.
Alan Stern3ff55882010-09-07 11:44:01 -04001493 */
James Bottomley001aac22007-12-02 19:10:40 +02001494 kfree(sshdr);
Tejun Heo2bae0092010-12-18 18:42:23 +01001495 retval = sdp->changed ? DISK_EVENT_MEDIA_CHANGE : 0;
1496 sdp->changed = 0;
Hannes Reineckeeb72d0b2016-04-26 08:06:58 +02001497 scsi_disk_put(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
Martin K. Petersene73aec82007-02-27 22:40:55 -05001501static int sd_sync_cache(struct scsi_disk *sdkp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 int retries, res;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001504 struct scsi_device *sdp = sdkp->device;
James Bottomley7e660102013-10-04 21:42:24 +00001505 const int timeout = sdp->request_queue->rq_timeout
1506 * SD_FLUSH_TIMEOUT_MULTIPLIER;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001507 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 if (!scsi_device_online(sdp))
1510 return -ENODEV;
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 for (retries = 3; retries > 0; --retries) {
1513 unsigned char cmd[10] = { 0 };
1514
1515 cmd[0] = SYNCHRONIZE_CACHE;
1516 /*
1517 * Leave the rest of the command zero to indicate
1518 * flush everything.
1519 */
Lin Ming9b214932013-03-23 11:42:25 +08001520 res = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0,
James Bottomley7e660102013-10-04 21:42:24 +00001521 &sshdr, timeout, SD_MAX_RETRIES,
Christoph Hellwige8064022016-10-20 15:12:13 +02001522 NULL, 0, RQF_PM);
James Bottomleyea73a9f2005-08-28 11:33:52 -05001523 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 break;
1525 }
1526
Martin K. Petersene73aec82007-02-27 22:40:55 -05001527 if (res) {
Hannes Reineckeef613292014-10-24 14:27:00 +02001528 sd_print_result(sdkp, "Synchronize Cache(10) failed", res);
Oliver Neukum95897912013-09-16 13:28:15 +02001529
Martin K. Petersene73aec82007-02-27 22:40:55 -05001530 if (driver_byte(res) & DRIVER_SENSE)
1531 sd_print_sense_hdr(sdkp, &sshdr);
Oliver Neukum95897912013-09-16 13:28:15 +02001532 /* we need to evaluate the error return */
1533 if (scsi_sense_valid(&sshdr) &&
Alan Stern7aae5132014-01-15 15:37:04 -05001534 (sshdr.asc == 0x3a || /* medium not present */
1535 sshdr.asc == 0x20)) /* invalid command */
Oliver Neukum95897912013-09-16 13:28:15 +02001536 /* this is no error here */
1537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Oliver Neukum95897912013-09-16 13:28:15 +02001539 switch (host_byte(res)) {
1540 /* ignore errors due to racing a disconnection */
1541 case DID_BAD_TARGET:
1542 case DID_NO_CONNECT:
1543 return 0;
1544 /* signal the upper layer it might try again */
1545 case DID_BUS_BUSY:
1546 case DID_IMM_RETRY:
1547 case DID_REQUEUE:
1548 case DID_SOFT_ERROR:
1549 return -EBUSY;
1550 default:
1551 return -EIO;
1552 }
1553 }
Tejun Heo37210502007-03-21 00:07:18 +09001554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557static void sd_rescan(struct device *dev)
1558{
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01001559 struct scsi_disk *sdkp = dev_get_drvdata(dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001560
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01001561 revalidate_disk(sdkp->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
1564
1565#ifdef CONFIG_COMPAT
1566/*
1567 * This gets directly called from VFS. When the ioctl
1568 * is not recognized we go back to the other translation paths.
1569 */
Al Viro0338e292008-03-02 10:41:04 -05001570static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
1571 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Al Viro0338e292008-03-02 10:41:04 -05001573 struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
Christoph Hellwig21a9d4c2014-10-24 18:39:37 +02001574 int error;
Paolo Bonzini0bfc96c2012-01-12 16:01:28 +01001575
Christoph Hellwig21a9d4c2014-10-24 18:39:37 +02001576 error = scsi_ioctl_block_when_processing_errors(sdev, cmd,
1577 (mode & FMODE_NDELAY) != 0);
1578 if (error)
1579 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /*
1582 * Let the static ioctl translation table take care of it.
1583 */
Christoph Hellwig21a9d4c2014-10-24 18:39:37 +02001584 if (!sdev->host->hostt->compat_ioctl)
1585 return -ENOIOCTLCMD;
1586 return sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588#endif
1589
Christoph Hellwig924d55b2015-10-15 14:10:49 +02001590static char sd_pr_type(enum pr_type type)
1591{
1592 switch (type) {
1593 case PR_WRITE_EXCLUSIVE:
1594 return 0x01;
1595 case PR_EXCLUSIVE_ACCESS:
1596 return 0x03;
1597 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1598 return 0x05;
1599 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1600 return 0x06;
1601 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1602 return 0x07;
1603 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1604 return 0x08;
1605 default:
1606 return 0;
1607 }
1608};
1609
1610static int sd_pr_command(struct block_device *bdev, u8 sa,
1611 u64 key, u64 sa_key, u8 type, u8 flags)
1612{
1613 struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
1614 struct scsi_sense_hdr sshdr;
1615 int result;
1616 u8 cmd[16] = { 0, };
1617 u8 data[24] = { 0, };
1618
1619 cmd[0] = PERSISTENT_RESERVE_OUT;
1620 cmd[1] = sa;
1621 cmd[2] = type;
1622 put_unaligned_be32(sizeof(data), &cmd[5]);
1623
1624 put_unaligned_be64(key, &data[0]);
1625 put_unaligned_be64(sa_key, &data[8]);
1626 data[20] = flags;
1627
1628 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, &data, sizeof(data),
1629 &sshdr, SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1630
1631 if ((driver_byte(result) & DRIVER_SENSE) &&
1632 (scsi_sense_valid(&sshdr))) {
1633 sdev_printk(KERN_INFO, sdev, "PR command failed: %d\n", result);
1634 scsi_print_sense_hdr(sdev, NULL, &sshdr);
1635 }
1636
1637 return result;
1638}
1639
1640static int sd_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
1641 u32 flags)
1642{
1643 if (flags & ~PR_FL_IGNORE_KEY)
1644 return -EOPNOTSUPP;
1645 return sd_pr_command(bdev, (flags & PR_FL_IGNORE_KEY) ? 0x06 : 0x00,
1646 old_key, new_key, 0,
Christoph Hellwig01f90dd2016-07-08 21:23:50 +09001647 (1 << 0) /* APTPL */);
Christoph Hellwig924d55b2015-10-15 14:10:49 +02001648}
1649
1650static int sd_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
1651 u32 flags)
1652{
1653 if (flags)
1654 return -EOPNOTSUPP;
1655 return sd_pr_command(bdev, 0x01, key, 0, sd_pr_type(type), 0);
1656}
1657
1658static int sd_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1659{
1660 return sd_pr_command(bdev, 0x02, key, 0, sd_pr_type(type), 0);
1661}
1662
1663static int sd_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
1664 enum pr_type type, bool abort)
1665{
1666 return sd_pr_command(bdev, abort ? 0x05 : 0x04, old_key, new_key,
1667 sd_pr_type(type), 0);
1668}
1669
1670static int sd_pr_clear(struct block_device *bdev, u64 key)
1671{
1672 return sd_pr_command(bdev, 0x03, key, 0, 0, 0);
1673}
1674
1675static const struct pr_ops sd_pr_ops = {
1676 .pr_register = sd_pr_register,
1677 .pr_reserve = sd_pr_reserve,
1678 .pr_release = sd_pr_release,
1679 .pr_preempt = sd_pr_preempt,
1680 .pr_clear = sd_pr_clear,
1681};
1682
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001683static const struct block_device_operations sd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 .owner = THIS_MODULE,
Al Viro0338e292008-03-02 10:41:04 -05001685 .open = sd_open,
1686 .release = sd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001687 .ioctl = sd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001688 .getgeo = sd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689#ifdef CONFIG_COMPAT
Al Viro0338e292008-03-02 10:41:04 -05001690 .compat_ioctl = sd_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691#endif
Tejun Heo2bae0092010-12-18 18:42:23 +01001692 .check_events = sd_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 .revalidate_disk = sd_revalidate_disk,
Tejun Heo72ec24b2010-05-15 20:09:32 +02001694 .unlock_native_capacity = sd_unlock_native_capacity,
Christoph Hellwig924d55b2015-10-15 14:10:49 +02001695 .pr_ops = &sd_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696};
1697
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001698/**
1699 * sd_eh_action - error handling callback
1700 * @scmd: sd-issued command that has failed
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001701 * @eh_disp: The recovery disposition suggested by the midlayer
1702 *
James Bottomley24510792013-11-11 13:44:53 +01001703 * This function is called by the SCSI midlayer upon completion of an
1704 * error test command (currently TEST UNIT READY). The result of sending
1705 * the eh command is passed in eh_disp. We're looking for devices that
1706 * fail medium access commands but are OK with non access commands like
1707 * test unit ready (so wrongly see the device as having a successful
1708 * recovery)
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001709 **/
James Bottomley24510792013-11-11 13:44:53 +01001710static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp)
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001711{
1712 struct scsi_disk *sdkp = scsi_disk(scmd->request->rq_disk);
1713
1714 if (!scsi_device_online(scmd->device) ||
James Bottomley24510792013-11-11 13:44:53 +01001715 !scsi_medium_access_command(scmd) ||
1716 host_byte(scmd->result) != DID_TIME_OUT ||
1717 eh_disp != SUCCESS)
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001718 return eh_disp;
1719
1720 /*
1721 * The device has timed out executing a medium access command.
1722 * However, the TEST UNIT READY command sent during error
1723 * handling completed successfully. Either the device is in the
1724 * process of recovering or has it suffered an internal failure
1725 * that prevents access to the storage medium.
1726 */
James Bottomley24510792013-11-11 13:44:53 +01001727 sdkp->medium_access_timed_out++;
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001728
1729 /*
1730 * If the device keeps failing read/write commands but TEST UNIT
1731 * READY always completes successfully we assume that medium
1732 * access is no longer possible and take the device offline.
1733 */
1734 if (sdkp->medium_access_timed_out >= sdkp->max_medium_access_timeouts) {
1735 scmd_printk(KERN_ERR, scmd,
1736 "Medium access timeout failure. Offlining disk!\n");
1737 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
1738
1739 return FAILED;
1740 }
1741
1742 return eh_disp;
1743}
1744
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001745static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1746{
Tejun Heo83096eb2009-05-07 22:24:39 +09001747 u64 start_lba = blk_rq_pos(scmd->request);
1748 u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
Mark Hounschell74856fb2015-05-13 10:49:09 +02001749 u64 factor = scmd->device->sector_size / 512;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001750 u64 bad_lba;
1751 int info_valid;
James Bottomleya8733c72010-12-17 15:36:34 -05001752 /*
1753 * resid is optional but mostly filled in. When it's unused,
1754 * its value is zero, so we assume the whole buffer transferred
1755 */
1756 unsigned int transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd);
1757 unsigned int good_bytes;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001758
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001759 if (scmd->request->cmd_type != REQ_TYPE_FS)
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001760 return 0;
1761
1762 info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
1763 SCSI_SENSE_BUFFERSIZE,
1764 &bad_lba);
1765 if (!info_valid)
1766 return 0;
1767
1768 if (scsi_bufflen(scmd) <= scmd->device->sector_size)
1769 return 0;
1770
Mark Hounschell74856fb2015-05-13 10:49:09 +02001771 /* be careful ... don't want any overflows */
1772 do_div(start_lba, factor);
1773 do_div(end_lba, factor);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001774
1775 /* The bad lba was reported incorrectly, we have no idea where
1776 * the error is.
1777 */
1778 if (bad_lba < start_lba || bad_lba >= end_lba)
1779 return 0;
1780
1781 /* This computation should always be done in terms of
1782 * the resolution of the device's medium.
1783 */
James Bottomleya8733c72010-12-17 15:36:34 -05001784 good_bytes = (bad_lba - start_lba) * scmd->device->sector_size;
1785 return min(good_bytes, transferred);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001786}
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788/**
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001789 * sd_done - bottom half handler: called when the lower level
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 * driver has completed (successfully or otherwise) a scsi command.
1791 * @SCpnt: mid-level's per command structure.
1792 *
1793 * Note: potentially run from within an ISR. Must not block.
1794 **/
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001795static int sd_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 int result = SCpnt->result;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001798 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 struct scsi_sense_hdr sshdr;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001800 struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
Martin K. Petersen26e85fc2012-09-18 12:19:31 -04001801 struct request *req = SCpnt->request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 int sense_valid = 0;
1803 int sense_deferred = 0;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05001804 unsigned char op = SCpnt->cmnd[0];
Martin K. Petersen5db44862012-09-18 12:19:32 -04001805 unsigned char unmap = SCpnt->cmnd[1] & 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Hannes Reinecke89d94752016-10-18 15:40:34 +09001807 switch (req_op(req)) {
1808 case REQ_OP_DISCARD:
1809 case REQ_OP_WRITE_SAME:
1810 case REQ_OP_ZONE_RESET:
Martin K. Petersen26e85fc2012-09-18 12:19:31 -04001811 if (!result) {
1812 good_bytes = blk_rq_bytes(req);
1813 scsi_set_resid(SCpnt, 0);
1814 } else {
1815 good_bytes = 0;
1816 scsi_set_resid(SCpnt, blk_rq_bytes(req));
1817 }
Hannes Reinecke89d94752016-10-18 15:40:34 +09001818 break;
1819 case REQ_OP_ZONE_REPORT:
1820 if (!result) {
1821 good_bytes = scsi_bufflen(SCpnt)
1822 - scsi_get_resid(SCpnt);
1823 scsi_set_resid(SCpnt, 0);
1824 } else {
1825 good_bytes = 0;
1826 scsi_set_resid(SCpnt, blk_rq_bytes(req));
1827 }
1828 break;
Martin K. Petersen26e85fc2012-09-18 12:19:31 -04001829 }
FUJITA Tomonori6a32a8a2010-07-21 10:29:37 +09001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (result) {
1832 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
1833 if (sense_valid)
1834 sense_deferred = scsi_sense_is_deferred(&sshdr);
1835 }
David Jeffery2a863ba2014-04-10 11:08:30 -04001836 sdkp->medium_access_timed_out = 0;
1837
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001838 if (driver_byte(result) != DRIVER_SENSE &&
1839 (!sense_valid || sense_deferred))
1840 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001842 switch (sshdr.sense_key) {
1843 case HARDWARE_ERROR:
1844 case MEDIUM_ERROR:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001845 good_bytes = sd_completed_bytes(SCpnt);
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001846 break;
1847 case RECOVERED_ERROR:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001848 good_bytes = scsi_bufflen(SCpnt);
1849 break;
Jamie Wellnitz10dab222008-09-11 21:39:36 -04001850 case NO_SENSE:
1851 /* This indicates a false check condition, so ignore it. An
1852 * unknown amount of data was transferred so treat it as an
1853 * error.
1854 */
Jamie Wellnitz10dab222008-09-11 21:39:36 -04001855 SCpnt->result = 0;
1856 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1857 break;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05001858 case ABORTED_COMMAND:
1859 if (sshdr.asc == 0x10) /* DIF: Target detected corruption */
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001860 good_bytes = sd_completed_bytes(SCpnt);
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001861 break;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05001862 case ILLEGAL_REQUEST:
1863 if (sshdr.asc == 0x10) /* DIX: Host detected corruption */
1864 good_bytes = sd_completed_bytes(SCpnt);
1865 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
Martin K. Petersen5db44862012-09-18 12:19:32 -04001866 if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
1867 switch (op) {
1868 case UNMAP:
1869 sd_config_discard(sdkp, SD_LBP_DISABLE);
1870 break;
1871 case WRITE_SAME_16:
1872 case WRITE_SAME:
1873 if (unmap)
1874 sd_config_discard(sdkp, SD_LBP_DISABLE);
1875 else {
1876 sdkp->device->no_write_same = 1;
1877 sd_config_write_same(sdkp);
1878
1879 good_bytes = 0;
1880 req->__data_len = blk_rq_bytes(req);
Christoph Hellwige8064022016-10-20 15:12:13 +02001881 req->rq_flags |= RQF_QUIET;
Martin K. Petersen5db44862012-09-18 12:19:32 -04001882 }
1883 }
1884 }
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05001885 break;
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001886 default:
1887 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
Hannes Reinecke89d94752016-10-18 15:40:34 +09001889
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001890 out:
Hannes Reinecke89d94752016-10-18 15:40:34 +09001891 if (sd_is_zoned(sdkp))
1892 sd_zbc_complete(SCpnt, good_bytes, &sshdr);
1893
Hannes Reineckeef613292014-10-24 14:27:00 +02001894 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
1895 "sd_done: completed %d of %d bytes\n",
1896 good_bytes, scsi_bufflen(SCpnt)));
1897
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001898 if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
1899 sd_dif_complete(SCpnt, good_bytes);
1900
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001901 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904/*
1905 * spinup disk - called only in sd_revalidate_disk()
1906 */
1907static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001908sd_spinup_disk(struct scsi_disk *sdkp)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001909{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 unsigned char cmd[10];
Alan Stern4451e472005-07-12 10:45:17 -04001911 unsigned long spintime_expire = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 int retries, spintime;
1913 unsigned int the_result;
1914 struct scsi_sense_hdr sshdr;
1915 int sense_valid = 0;
1916
1917 spintime = 0;
1918
1919 /* Spin up drives, as required. Only do this at boot time */
1920 /* Spinup needs to be done for module loads too. */
1921 do {
1922 retries = 0;
1923
1924 do {
1925 cmd[0] = TEST_UNIT_READY;
1926 memset((void *) &cmd[1], 0, 9);
1927
James Bottomleyea73a9f2005-08-28 11:33:52 -05001928 the_result = scsi_execute_req(sdkp->device, cmd,
1929 DMA_NONE, NULL, 0,
1930 &sshdr, SD_TIMEOUT,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09001931 SD_MAX_RETRIES, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Alan Sternb4d38e32006-10-11 16:48:28 -04001933 /*
1934 * If the drive has indicated to us that it
1935 * doesn't have any media in it, don't bother
1936 * with any more polling.
1937 */
1938 if (media_not_present(sdkp, &sshdr))
1939 return;
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (the_result)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001942 sense_valid = scsi_sense_valid(&sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 retries++;
1944 } while (retries < 3 &&
1945 (!scsi_status_is_good(the_result) ||
1946 ((driver_byte(the_result) & DRIVER_SENSE) &&
1947 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1950 /* no sense, TUR either succeeded or failed
1951 * with a status error */
Martin K. Petersene73aec82007-02-27 22:40:55 -05001952 if(!spintime && !scsi_status_is_good(the_result)) {
Hannes Reineckeef613292014-10-24 14:27:00 +02001953 sd_print_result(sdkp, "Test Unit Ready failed",
1954 the_result);
Martin K. Petersene73aec82007-02-27 22:40:55 -05001955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 break;
1957 }
Hannes Reineckeef613292014-10-24 14:27:00 +02001958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 /*
1960 * The device does not want the automatic start to be issued.
1961 */
Matthew Wilcox33dd6f92009-02-20 06:53:48 -07001962 if (sdkp->device->no_start_on_add)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Matthew Wilcox33dd6f92009-02-20 06:53:48 -07001965 if (sense_valid && sshdr.sense_key == NOT_READY) {
1966 if (sshdr.asc == 4 && sshdr.ascq == 3)
1967 break; /* manual intervention required */
1968 if (sshdr.asc == 4 && sshdr.ascq == 0xb)
1969 break; /* standby */
1970 if (sshdr.asc == 4 && sshdr.ascq == 0xc)
1971 break; /* unavailable */
1972 /*
1973 * Issue command to spin up drive when not ready
1974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001976 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 cmd[0] = START_STOP;
1978 cmd[1] = 1; /* Return immediately */
1979 memset((void *) &cmd[2], 0, 8);
1980 cmd[4] = 1; /* Start spin cycle */
Stefan Richterd2886ea2008-05-11 00:34:07 +02001981 if (sdkp->device->start_stop_pwr_cond)
1982 cmd[4] |= 1 << 4;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001983 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1984 NULL, 0, &sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09001985 SD_TIMEOUT, SD_MAX_RETRIES,
1986 NULL);
Alan Stern4451e472005-07-12 10:45:17 -04001987 spintime_expire = jiffies + 100 * HZ;
1988 spintime = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 /* Wait 1 second for next try */
1991 msleep(1000);
1992 printk(".");
Alan Stern4451e472005-07-12 10:45:17 -04001993
1994 /*
1995 * Wait for USB flash devices with slow firmware.
1996 * Yes, this sense key/ASC combination shouldn't
1997 * occur here. It's characteristic of these devices.
1998 */
1999 } else if (sense_valid &&
2000 sshdr.sense_key == UNIT_ATTENTION &&
2001 sshdr.asc == 0x28) {
2002 if (!spintime) {
2003 spintime_expire = jiffies + 5 * HZ;
2004 spintime = 1;
2005 }
2006 /* Wait 1 second for next try */
2007 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 } else {
2009 /* we don't understand the sense code, so it's
2010 * probably pointless to loop */
2011 if(!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002012 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
2013 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015 break;
2016 }
2017
Alan Stern4451e472005-07-12 10:45:17 -04002018 } while (spintime && time_before_eq(jiffies, spintime_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 if (spintime) {
2021 if (scsi_status_is_good(the_result))
2022 printk("ready\n");
2023 else
2024 printk("not responding...\n");
2025 }
2026}
2027
Martin K. Petersene0597d72008-07-17 04:28:34 -04002028/*
2029 * Determine whether disk supports Data Integrity Field.
2030 */
Martin K. Petersenfe542392012-09-21 12:44:12 -04002031static int sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
Martin K. Petersene0597d72008-07-17 04:28:34 -04002032{
2033 struct scsi_device *sdp = sdkp->device;
2034 u8 type;
Martin K. Petersenfe542392012-09-21 12:44:12 -04002035 int ret = 0;
Martin K. Petersene0597d72008-07-17 04:28:34 -04002036
2037 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
Martin K. Petersenfe542392012-09-21 12:44:12 -04002038 return ret;
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -04002039
2040 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
2041
Christoph Hellwig8475c812016-09-11 19:35:41 +02002042 if (type > T10_PI_TYPE3_PROTECTION)
Martin K. Petersenfe542392012-09-21 12:44:12 -04002043 ret = -ENODEV;
2044 else if (scsi_host_dif_capable(sdp->host, type))
2045 ret = 1;
2046
2047 if (sdkp->first_scan || type != sdkp->protection_type)
2048 switch (ret) {
2049 case -ENODEV:
2050 sd_printk(KERN_ERR, sdkp, "formatted with unsupported" \
2051 " protection type %u. Disabling disk!\n",
2052 type);
2053 break;
2054 case 1:
2055 sd_printk(KERN_NOTICE, sdkp,
2056 "Enabling DIF Type %u protection\n", type);
2057 break;
2058 case 0:
2059 sd_printk(KERN_NOTICE, sdkp,
2060 "Disabling DIF Type %u protection\n", type);
2061 break;
2062 }
Martin K. Petersene0597d72008-07-17 04:28:34 -04002063
Martin K. Petersenbe922f42008-09-19 18:47:20 -04002064 sdkp->protection_type = type;
2065
Martin K. Petersenfe542392012-09-21 12:44:12 -04002066 return ret;
Martin K. Petersene0597d72008-07-17 04:28:34 -04002067}
2068
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002069static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
2070 struct scsi_sense_hdr *sshdr, int sense_valid,
2071 int the_result)
2072{
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002073 if (driver_byte(the_result) & DRIVER_SENSE)
2074 sd_print_sense_hdr(sdkp, sshdr);
2075 else
2076 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
2077
2078 /*
2079 * Set dirty bit for removable devices if not ready -
2080 * sometimes drives will not report this properly.
2081 */
2082 if (sdp->removable &&
2083 sense_valid && sshdr->sense_key == NOT_READY)
Tejun Heo2bae0092010-12-18 18:42:23 +01002084 set_media_not_present(sdkp);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002085
2086 /*
2087 * We used to set media_present to 0 here to indicate no media
2088 * in the drive, but some drives fail read capacity even with
2089 * media present, so we can't do that.
2090 */
2091 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
2092}
2093
2094#define RC16_LEN 32
2095#if RC16_LEN > SD_BUF_SIZE
2096#error RC16_LEN must not be more than SD_BUF_SIZE
2097#endif
2098
James Bottomley3233ac12010-04-01 10:30:01 -04002099#define READ_CAPACITY_RETRIES_ON_RESET 10
2100
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002101static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
2102 unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05002103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 unsigned char cmd[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 struct scsi_sense_hdr sshdr;
2106 int sense_valid = 0;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002107 int the_result;
James Bottomley3233ac12010-04-01 10:30:01 -04002108 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04002109 unsigned int alignment;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002110 unsigned long long lba;
2111 unsigned sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Hans de Goede5ce524b2010-10-01 14:20:10 -07002113 if (sdp->no_read_capacity_16)
2114 return -EINVAL;
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 do {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002117 memset(cmd, 0, 16);
Hannes Reineckeeb846d92014-11-17 14:25:19 +01002118 cmd[0] = SERVICE_ACTION_IN_16;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002119 cmd[1] = SAI_READ_CAPACITY_16;
2120 cmd[13] = RC16_LEN;
2121 memset(buffer, 0, RC16_LEN);
2122
James Bottomleyea73a9f2005-08-28 11:33:52 -05002123 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002124 buffer, RC16_LEN, &sshdr,
2125 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
James Bottomleyea73a9f2005-08-28 11:33:52 -05002127 if (media_not_present(sdkp, &sshdr))
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002128 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Matthew Wilcox2b301302009-03-12 14:20:30 -04002130 if (the_result) {
James Bottomleyea73a9f2005-08-28 11:33:52 -05002131 sense_valid = scsi_sense_valid(&sshdr);
Matthew Wilcox2b301302009-03-12 14:20:30 -04002132 if (sense_valid &&
2133 sshdr.sense_key == ILLEGAL_REQUEST &&
2134 (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
2135 sshdr.ascq == 0x00)
2136 /* Invalid Command Operation Code or
2137 * Invalid Field in CDB, just retry
2138 * silently with RC10 */
2139 return -EINVAL;
James Bottomley3233ac12010-04-01 10:30:01 -04002140 if (sense_valid &&
2141 sshdr.sense_key == UNIT_ATTENTION &&
2142 sshdr.asc == 0x29 && sshdr.ascq == 0x00)
2143 /* Device reset might occur several times,
2144 * give it one more chance */
2145 if (--reset_retries > 0)
2146 continue;
Matthew Wilcox2b301302009-03-12 14:20:30 -04002147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 retries--;
2149
2150 } while (the_result && retries);
2151
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002152 if (the_result) {
Hannes Reineckeef613292014-10-24 14:27:00 +02002153 sd_print_result(sdkp, "Read Capacity(16) failed", the_result);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002154 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
2155 return -EINVAL;
2156 }
Martin K. Petersene73aec82007-02-27 22:40:55 -05002157
Dave Hansen8f76d152009-04-21 16:43:27 -07002158 sector_size = get_unaligned_be32(&buffer[8]);
2159 lba = get_unaligned_be64(&buffer[0]);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002160
Martin K. Petersenfe542392012-09-21 12:44:12 -04002161 if (sd_read_protection_type(sdkp, buffer) < 0) {
2162 sdkp->capacity = 0;
2163 return -ENODEV;
2164 }
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002165
2166 if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
2167 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
2168 "kernel compiled with support for large block "
2169 "devices.\n");
2170 sdkp->capacity = 0;
2171 return -EOVERFLOW;
2172 }
2173
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04002174 /* Logical blocks per physical block exponent */
Martin K. Petersen526f7c72010-09-28 14:48:47 -04002175 sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size;
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04002176
Hannes Reinecke89d94752016-10-18 15:40:34 +09002177 /* RC basis */
2178 sdkp->rc_basis = (buffer[12] >> 4) & 0x3;
2179
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04002180 /* Lowest aligned logical block */
2181 alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
2182 blk_queue_alignment_offset(sdp->request_queue, alignment);
2183 if (alignment && sdkp->first_scan)
2184 sd_printk(KERN_NOTICE, sdkp,
2185 "physical block alignment offset: %u\n", alignment);
2186
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002187 if (buffer[14] & 0x80) { /* LBPME */
2188 sdkp->lbpme = 1;
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002189
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002190 if (buffer[14] & 0x40) /* LBPRZ */
2191 sdkp->lbprz = 1;
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002192
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002193 sd_config_discard(sdkp, SD_LBP_WS16);
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002194 }
2195
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002196 sdkp->capacity = lba + 1;
2197 return sector_size;
2198}
2199
2200static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
2201 unsigned char *buffer)
2202{
2203 unsigned char cmd[16];
2204 struct scsi_sense_hdr sshdr;
2205 int sense_valid = 0;
2206 int the_result;
James Bottomley3233ac12010-04-01 10:30:01 -04002207 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002208 sector_t lba;
2209 unsigned sector_size;
2210
2211 do {
2212 cmd[0] = READ_CAPACITY;
2213 memset(&cmd[1], 0, 9);
2214 memset(buffer, 0, 8);
2215
2216 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
2217 buffer, 8, &sshdr,
2218 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
2219
2220 if (media_not_present(sdkp, &sshdr))
2221 return -ENODEV;
2222
James Bottomley3233ac12010-04-01 10:30:01 -04002223 if (the_result) {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002224 sense_valid = scsi_sense_valid(&sshdr);
James Bottomley3233ac12010-04-01 10:30:01 -04002225 if (sense_valid &&
2226 sshdr.sense_key == UNIT_ATTENTION &&
2227 sshdr.asc == 0x29 && sshdr.ascq == 0x00)
2228 /* Device reset might occur several times,
2229 * give it one more chance */
2230 if (--reset_retries > 0)
2231 continue;
2232 }
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002233 retries--;
2234
2235 } while (the_result && retries);
2236
2237 if (the_result) {
Hannes Reineckeef613292014-10-24 14:27:00 +02002238 sd_print_result(sdkp, "Read Capacity(10) failed", the_result);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002239 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
2240 return -EINVAL;
2241 }
2242
Dave Hansen8f76d152009-04-21 16:43:27 -07002243 sector_size = get_unaligned_be32(&buffer[4]);
2244 lba = get_unaligned_be32(&buffer[0]);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002245
Hans de Goede5ce524b2010-10-01 14:20:10 -07002246 if (sdp->no_read_capacity_16 && (lba == 0xffffffff)) {
2247 /* Some buggy (usb cardreader) devices return an lba of
2248 0xffffffff when the want to report a size of 0 (with
2249 which they really mean no media is present) */
2250 sdkp->capacity = 0;
Linus Torvalds5cc10352010-10-22 20:30:48 -07002251 sdkp->physical_block_size = sector_size;
Hans de Goede5ce524b2010-10-01 14:20:10 -07002252 return sector_size;
2253 }
2254
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002255 if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
2256 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
2257 "kernel compiled with support for large block "
2258 "devices.\n");
2259 sdkp->capacity = 0;
2260 return -EOVERFLOW;
2261 }
2262
2263 sdkp->capacity = lba + 1;
Martin K. Petersen526f7c72010-09-28 14:48:47 -04002264 sdkp->physical_block_size = sector_size;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002265 return sector_size;
2266}
2267
Matthew Wilcox2b301302009-03-12 14:20:30 -04002268static int sd_try_rc16_first(struct scsi_device *sdp)
2269{
Hannes Reineckef87146b2010-03-29 09:29:24 +02002270 if (sdp->host->max_cmd_len < 16)
2271 return 0;
Alan Stern6a0bdff2012-06-20 16:04:19 -04002272 if (sdp->try_rc_10_first)
2273 return 0;
Matthew Wilcox2b301302009-03-12 14:20:30 -04002274 if (sdp->scsi_level > SCSI_SPC_2)
2275 return 1;
2276 if (scsi_device_protection(sdp))
2277 return 1;
2278 return 0;
2279}
2280
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002281/*
2282 * read disk capacity
2283 */
2284static void
2285sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
2286{
2287 int sector_size;
2288 struct scsi_device *sdp = sdkp->device;
2289
Matthew Wilcox2b301302009-03-12 14:20:30 -04002290 if (sd_try_rc16_first(sdp)) {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002291 sector_size = read_capacity_16(sdkp, sdp, buffer);
2292 if (sector_size == -EOVERFLOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 goto got_data;
Matthew Wilcox2b301302009-03-12 14:20:30 -04002294 if (sector_size == -ENODEV)
2295 return;
2296 if (sector_size < 0)
2297 sector_size = read_capacity_10(sdkp, sdp, buffer);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002298 if (sector_size < 0)
2299 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 } else {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002301 sector_size = read_capacity_10(sdkp, sdp, buffer);
2302 if (sector_size == -EOVERFLOW)
2303 goto got_data;
2304 if (sector_size < 0)
2305 return;
2306 if ((sizeof(sdkp->capacity) > 4) &&
2307 (sdkp->capacity > 0xffffffffULL)) {
2308 int old_sector_size = sector_size;
2309 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
2310 "Trying to use READ CAPACITY(16).\n");
2311 sector_size = read_capacity_16(sdkp, sdp, buffer);
2312 if (sector_size < 0) {
2313 sd_printk(KERN_NOTICE, sdkp,
2314 "Using 0xffffffff as device size\n");
2315 sdkp->capacity = 1 + (sector_t) 0xffffffff;
2316 sector_size = old_sector_size;
2317 goto got_data;
2318 }
2319 }
2320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Alan Stern5c211ca2009-02-18 10:54:44 -05002322 /* Some devices are known to return the total number of blocks,
2323 * not the highest block number. Some devices have versions
2324 * which do this and others which do not. Some devices we might
2325 * suspect of doing this but we don't know for certain.
2326 *
2327 * If we know the reported capacity is wrong, decrement it. If
2328 * we can only guess, then assume the number of blocks is even
2329 * (usually true but not always) and err on the side of lowering
2330 * the capacity.
2331 */
2332 if (sdp->fix_capacity ||
2333 (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
2334 sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
2335 "from its reported value: %llu\n",
2336 (unsigned long long) sdkp->capacity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 --sdkp->capacity;
Oliver Neukum61bf54b2007-02-08 09:04:48 +01002338 }
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340got_data:
2341 if (sector_size == 0) {
2342 sector_size = 512;
Martin K. Petersene73aec82007-02-27 22:40:55 -05002343 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
2344 "assuming 512.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346
2347 if (sector_size != 512 &&
2348 sector_size != 1024 &&
2349 sector_size != 2048 &&
Mark Hounschell74856fb2015-05-13 10:49:09 +02002350 sector_size != 4096) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002351 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
2352 sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /*
2354 * The user might want to re-format the drive with
2355 * a supported sectorsize. Once this happens, it
2356 * would be relatively trivial to set the thing up.
2357 * For this reason, we leave the thing in the table.
2358 */
2359 sdkp->capacity = 0;
2360 /*
2361 * set a bogus sector size so the normal read/write
2362 * logic in the block layer will eventually refuse any
2363 * request on this device without tripping over power
2364 * of two sector size assumptions
2365 */
2366 sector_size = 512;
2367 }
Martin K. Petersene1defc42009-05-22 17:17:49 -04002368 blk_queue_logical_block_size(sdp->request_queue, sector_size);
Hannes Reinecke89d94752016-10-18 15:40:34 +09002369 blk_queue_physical_block_size(sdp->request_queue,
2370 sdkp->physical_block_size);
2371 sdkp->device->sector_size = sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Martin K. Petersenca369d52015-11-13 16:46:48 -05002373 if (sdkp->capacity > 0xffffffff)
Martin K. Petersenbcdb2472014-06-03 18:45:51 -04002374 sdp->use_16_for_rw = 1;
Jason J. Herne53ad5702012-11-14 17:03:22 -05002375
Hannes Reinecke89d94752016-10-18 15:40:34 +09002376}
2377
2378/*
2379 * Print disk capacity
2380 */
2381static void
2382sd_print_capacity(struct scsi_disk *sdkp,
2383 sector_t old_capacity)
2384{
2385 int sector_size = sdkp->device->sector_size;
2386 char cap_str_2[10], cap_str_10[10];
2387
2388 string_get_size(sdkp->capacity, sector_size,
2389 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
2390 string_get_size(sdkp->capacity, sector_size,
2391 STRING_UNITS_10, cap_str_10,
2392 sizeof(cap_str_10));
2393
2394 if (sdkp->first_scan || old_capacity != sdkp->capacity) {
2395 sd_printk(KERN_NOTICE, sdkp,
2396 "%llu %d-byte logical blocks: (%s/%s)\n",
2397 (unsigned long long)sdkp->capacity,
2398 sector_size, cap_str_10, cap_str_2);
2399
2400 if (sdkp->physical_block_size != sector_size)
2401 sd_printk(KERN_NOTICE, sdkp,
2402 "%u-byte physical blocks\n",
2403 sdkp->physical_block_size);
2404
2405 sd_zbc_print_zones(sdkp);
2406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
2409/* called with buffer of length 512 */
2410static inline int
James Bottomleyea73a9f2005-08-28 11:33:52 -05002411sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
2412 unsigned char *buffer, int len, struct scsi_mode_data *data,
2413 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
James Bottomleyea73a9f2005-08-28 11:33:52 -05002415 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
James Bottomley1cf72692005-08-28 11:27:01 -05002416 SD_TIMEOUT, SD_MAX_RETRIES, data,
James Bottomleyea73a9f2005-08-28 11:33:52 -05002417 sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
2420/*
2421 * read write protect setting, if possible - called only in sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06002422 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 */
2424static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05002425sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05002426{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 int res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05002428 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 struct scsi_mode_data data;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002430 int old_wp = sdkp->write_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 set_disk_ro(sdkp->disk, 0);
James Bottomleyea73a9f2005-08-28 11:33:52 -05002433 if (sdp->skip_ms_page_3f) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002434 sd_first_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return;
2436 }
2437
James Bottomleyea73a9f2005-08-28 11:33:52 -05002438 if (sdp->use_192_bytes_for_3f) {
2439 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 } else {
2441 /*
2442 * First attempt: ask for all pages (0x3F), but only 4 bytes.
2443 * We have to start carefully: some devices hang if we ask
2444 * for more than is available.
2445 */
James Bottomleyea73a9f2005-08-28 11:33:52 -05002446 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 /*
2449 * Second attempt: ask for page 0 When only page 0 is
2450 * implemented, a request for page 3F may return Sense Key
2451 * 5: Illegal Request, Sense Code 24: Invalid field in
2452 * CDB.
2453 */
2454 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05002455 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 /*
2458 * Third attempt: ask 255 bytes, as we did earlier.
2459 */
2460 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05002461 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
2462 &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
2464
2465 if (!scsi_status_is_good(res)) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002466 sd_first_printk(KERN_WARNING, sdkp,
Martin K. Petersene73aec82007-02-27 22:40:55 -05002467 "Test WP failed, assume Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 } else {
2469 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
2470 set_disk_ro(sdkp->disk, sdkp->write_prot);
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002471 if (sdkp->first_scan || old_wp != sdkp->write_prot) {
2472 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
2473 sdkp->write_prot ? "on" : "off");
2474 sd_printk(KERN_DEBUG, sdkp,
2475 "Mode Sense: %02x %02x %02x %02x\n",
2476 buffer[0], buffer[1], buffer[2], buffer[3]);
2477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
2479}
2480
2481/*
2482 * sd_read_cache_type - called only from sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06002483 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 */
2485static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05002486sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
Al Viro 631e8a12005-05-16 01:59:55 +01002487{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 int len = 0, res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05002489 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Al Viro 631e8a12005-05-16 01:59:55 +01002491 int dbd;
2492 int modepage;
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002493 int first_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 struct scsi_mode_data data;
2495 struct scsi_sense_hdr sshdr;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002496 int old_wce = sdkp->WCE;
2497 int old_rcd = sdkp->RCD;
2498 int old_dpofua = sdkp->DPOFUA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
James Bottomley39c60a02013-04-24 14:02:53 -07002500
2501 if (sdkp->cache_override)
2502 return;
2503
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002504 first_len = 4;
2505 if (sdp->skip_ms_page_8) {
2506 if (sdp->type == TYPE_RBC)
2507 goto defaults;
2508 else {
2509 if (sdp->skip_ms_page_3f)
2510 goto defaults;
2511 modepage = 0x3F;
2512 if (sdp->use_192_bytes_for_3f)
2513 first_len = 192;
2514 dbd = 0;
2515 }
2516 } else if (sdp->type == TYPE_RBC) {
Al Viro 631e8a12005-05-16 01:59:55 +01002517 modepage = 6;
2518 dbd = 8;
2519 } else {
2520 modepage = 8;
2521 dbd = 0;
2522 }
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 /* cautiously ask */
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002525 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, first_len,
2526 &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 if (!scsi_status_is_good(res))
2529 goto bad_sense;
2530
Al Viro6d73c852006-02-23 02:03:16 +01002531 if (!data.header_length) {
2532 modepage = 6;
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002533 first_len = 0;
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002534 sd_first_printk(KERN_ERR, sdkp,
2535 "Missing header in MODE_SENSE response\n");
Al Viro6d73c852006-02-23 02:03:16 +01002536 }
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 /* that went OK, now ask for the proper length */
2539 len = data.length;
2540
2541 /*
2542 * We're only interested in the first three bytes, actually.
2543 * But the data cache page is defined for the first 20.
2544 */
2545 if (len < 3)
2546 goto bad_sense;
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002547 else if (len > SD_BUF_SIZE) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002548 sd_first_printk(KERN_NOTICE, sdkp, "Truncating mode parameter "
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002549 "data from %d to %d bytes\n", len, SD_BUF_SIZE);
2550 len = SD_BUF_SIZE;
2551 }
2552 if (modepage == 0x3F && sdp->use_192_bytes_for_3f)
2553 len = 192;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555 /* Get the data */
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002556 if (len > first_len)
2557 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len,
2558 &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 if (scsi_status_is_good(res)) {
Al Viro 631e8a12005-05-16 01:59:55 +01002561 int offset = data.header_length + data.block_descriptor_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002563 while (offset < len) {
2564 u8 page_code = buffer[offset] & 0x3F;
2565 u8 spf = buffer[offset] & 0x40;
2566
2567 if (page_code == 8 || page_code == 6) {
2568 /* We're interested only in the first 3 bytes.
2569 */
2570 if (len - offset <= 2) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002571 sd_first_printk(KERN_ERR, sdkp,
2572 "Incomplete mode parameter "
2573 "data\n");
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002574 goto defaults;
2575 } else {
2576 modepage = page_code;
2577 goto Page_found;
2578 }
2579 } else {
2580 /* Go to the next page */
2581 if (spf && len - offset > 3)
2582 offset += 4 + (buffer[offset+2] << 8) +
2583 buffer[offset+3];
2584 else if (!spf && len - offset > 1)
2585 offset += 2 + buffer[offset+1];
2586 else {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002587 sd_first_printk(KERN_ERR, sdkp,
2588 "Incomplete mode "
2589 "parameter data\n");
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002590 goto defaults;
2591 }
2592 }
Al Viro48970802006-02-26 08:34:10 -06002593 }
2594
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002595 sd_first_printk(KERN_ERR, sdkp, "No Caching mode page found\n");
Alan Stern984f1732013-09-06 11:49:51 -04002596 goto defaults;
2597
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002598 Page_found:
Al Viro 631e8a12005-05-16 01:59:55 +01002599 if (modepage == 8) {
2600 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
2601 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
2602 } else {
2603 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
2604 sdkp->RCD = 0;
2605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Tejun Heo007365a2006-01-06 09:53:52 +01002607 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
Alan Sternb14bf2d2014-06-30 11:04:21 -04002608 if (sdp->broken_fua) {
2609 sd_first_printk(KERN_NOTICE, sdkp, "Disabling FUA\n");
2610 sdkp->DPOFUA = 0;
2611 } else if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002612 sd_first_printk(KERN_NOTICE, sdkp,
Martin K. Petersene73aec82007-02-27 22:40:55 -05002613 "Uses READ/WRITE(6), disabling FUA\n");
Tejun Heo007365a2006-01-06 09:53:52 +01002614 sdkp->DPOFUA = 0;
2615 }
2616
Sujit Reddy Thumma2eefd572014-08-11 15:40:37 +03002617 /* No cache flush allowed for write protected devices */
2618 if (sdkp->WCE && sdkp->write_prot)
2619 sdkp->WCE = 0;
2620
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002621 if (sdkp->first_scan || old_wce != sdkp->WCE ||
2622 old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
2623 sd_printk(KERN_NOTICE, sdkp,
2624 "Write cache: %s, read cache: %s, %s\n",
2625 sdkp->WCE ? "enabled" : "disabled",
2626 sdkp->RCD ? "disabled" : "enabled",
2627 sdkp->DPOFUA ? "supports DPO and FUA"
2628 : "doesn't support DPO or FUA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 return;
2631 }
2632
2633bad_sense:
James Bottomleyea73a9f2005-08-28 11:33:52 -05002634 if (scsi_sense_valid(&sshdr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 sshdr.sense_key == ILLEGAL_REQUEST &&
2636 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
Martin K. Petersene73aec82007-02-27 22:40:55 -05002637 /* Invalid field in CDB */
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002638 sd_first_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 else
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002640 sd_first_printk(KERN_ERR, sdkp,
2641 "Asking for cache data failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643defaults:
Namjae Jeonb81478d2012-07-07 23:05:08 -04002644 if (sdp->wce_default_on) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002645 sd_first_printk(KERN_NOTICE, sdkp,
2646 "Assuming drive cache: write back\n");
Namjae Jeonb81478d2012-07-07 23:05:08 -04002647 sdkp->WCE = 1;
2648 } else {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002649 sd_first_printk(KERN_ERR, sdkp,
2650 "Assuming drive cache: write through\n");
Namjae Jeonb81478d2012-07-07 23:05:08 -04002651 sdkp->WCE = 0;
2652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 sdkp->RCD = 0;
Al Viro48970802006-02-26 08:34:10 -06002654 sdkp->DPOFUA = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655}
2656
Martin K. Petersene0597d72008-07-17 04:28:34 -04002657/*
2658 * The ATO bit indicates whether the DIF application tag is available
2659 * for use by the operating system.
2660 */
H Hartley Sweeten439d77f2010-08-10 18:01:20 -07002661static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
Martin K. Petersene0597d72008-07-17 04:28:34 -04002662{
2663 int res, offset;
2664 struct scsi_device *sdp = sdkp->device;
2665 struct scsi_mode_data data;
2666 struct scsi_sense_hdr sshdr;
2667
Hannes Reinecke89d94752016-10-18 15:40:34 +09002668 if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC)
Martin K. Petersene0597d72008-07-17 04:28:34 -04002669 return;
2670
2671 if (sdkp->protection_type == 0)
2672 return;
2673
2674 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
2675 SD_MAX_RETRIES, &data, &sshdr);
2676
2677 if (!scsi_status_is_good(res) || !data.header_length ||
2678 data.length < 6) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002679 sd_first_printk(KERN_WARNING, sdkp,
Martin K. Petersene0597d72008-07-17 04:28:34 -04002680 "getting Control mode page failed, assume no ATO\n");
2681
2682 if (scsi_sense_valid(&sshdr))
2683 sd_print_sense_hdr(sdkp, &sshdr);
2684
2685 return;
2686 }
2687
2688 offset = data.header_length + data.block_descriptor_length;
2689
2690 if ((buffer[offset] & 0x3f) != 0x0a) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002691 sd_first_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
Martin K. Petersene0597d72008-07-17 04:28:34 -04002692 return;
2693 }
2694
2695 if ((buffer[offset + 5] & 0x80) == 0)
2696 return;
2697
2698 sdkp->ATO = 1;
2699
2700 return;
2701}
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703/**
Martin K. Petersend11b6912009-05-23 11:43:39 -04002704 * sd_read_block_limits - Query disk device for preferred I/O sizes.
2705 * @disk: disk to query
2706 */
2707static void sd_read_block_limits(struct scsi_disk *sdkp)
2708{
2709 unsigned int sector_sz = sdkp->device->sector_size;
Martin K. Petersenbb2d3de2010-03-02 08:44:34 -05002710 const int vpd_len = 64;
James Bottomleye3deec02009-11-03 12:33:07 -06002711 unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
Martin K. Petersend11b6912009-05-23 11:43:39 -04002712
James Bottomleye3deec02009-11-03 12:33:07 -06002713 if (!buffer ||
2714 /* Block Limits VPD */
2715 scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
2716 goto out;
Martin K. Petersend11b6912009-05-23 11:43:39 -04002717
2718 blk_queue_io_min(sdkp->disk->queue,
2719 get_unaligned_be16(&buffer[6]) * sector_sz);
Martin K. Petersenca369d52015-11-13 16:46:48 -05002720
2721 sdkp->max_xfer_blocks = get_unaligned_be32(&buffer[8]);
2722 sdkp->opt_xfer_blocks = get_unaligned_be32(&buffer[12]);
Martin K. Petersend11b6912009-05-23 11:43:39 -04002723
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002724 if (buffer[3] == 0x3c) {
2725 unsigned int lba_count, desc_count;
2726
Martin K. Petersen5db44862012-09-18 12:19:32 -04002727 sdkp->max_ws_blocks = (u32)get_unaligned_be64(&buffer[36]);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002728
2729 if (!sdkp->lbpme)
2730 goto out;
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002731
2732 lba_count = get_unaligned_be32(&buffer[20]);
2733 desc_count = get_unaligned_be32(&buffer[24]);
2734
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002735 if (lba_count && desc_count)
2736 sdkp->max_unmap_blocks = lba_count;
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002737
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002738 sdkp->unmap_granularity = get_unaligned_be32(&buffer[28]);
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002739
2740 if (buffer[32] & 0x80)
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002741 sdkp->unmap_alignment =
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002742 get_unaligned_be32(&buffer[32]) & ~(1 << 31);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002743
2744 if (!sdkp->lbpvpd) { /* LBP VPD page not provided */
2745
2746 if (sdkp->max_unmap_blocks)
2747 sd_config_discard(sdkp, SD_LBP_UNMAP);
2748 else
2749 sd_config_discard(sdkp, SD_LBP_WS16);
2750
2751 } else { /* LBP VPD page tells us what to use */
Martin K. Petersene4613382014-12-05 08:58:03 -05002752 if (sdkp->lbpu && sdkp->max_unmap_blocks && !sdkp->lbprz)
2753 sd_config_discard(sdkp, SD_LBP_UNMAP);
2754 else if (sdkp->lbpws)
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002755 sd_config_discard(sdkp, SD_LBP_WS16);
2756 else if (sdkp->lbpws10)
2757 sd_config_discard(sdkp, SD_LBP_WS10);
Martin K. Petersen79850902014-11-07 00:08:13 -05002758 else if (sdkp->lbpu && sdkp->max_unmap_blocks)
2759 sd_config_discard(sdkp, SD_LBP_UNMAP);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002760 else
2761 sd_config_discard(sdkp, SD_LBP_DISABLE);
2762 }
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002763 }
2764
James Bottomleye3deec02009-11-03 12:33:07 -06002765 out:
Martin K. Petersend11b6912009-05-23 11:43:39 -04002766 kfree(buffer);
2767}
2768
2769/**
Martin K. Petersen3821d762009-05-23 11:43:38 -04002770 * sd_read_block_characteristics - Query block dev. characteristics
2771 * @disk: disk to query
2772 */
2773static void sd_read_block_characteristics(struct scsi_disk *sdkp)
2774{
Hannes Reinecke89d94752016-10-18 15:40:34 +09002775 struct request_queue *q = sdkp->disk->queue;
James Bottomleye3deec02009-11-03 12:33:07 -06002776 unsigned char *buffer;
Martin K. Petersen3821d762009-05-23 11:43:38 -04002777 u16 rot;
Martin K. Petersenbb2d3de2010-03-02 08:44:34 -05002778 const int vpd_len = 64;
Martin K. Petersen3821d762009-05-23 11:43:38 -04002779
James Bottomleye3deec02009-11-03 12:33:07 -06002780 buffer = kmalloc(vpd_len, GFP_KERNEL);
Martin K. Petersen3821d762009-05-23 11:43:38 -04002781
James Bottomleye3deec02009-11-03 12:33:07 -06002782 if (!buffer ||
2783 /* Block Device Characteristics VPD */
2784 scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len))
2785 goto out;
Martin K. Petersen3821d762009-05-23 11:43:38 -04002786
2787 rot = get_unaligned_be16(&buffer[4]);
2788
Mike Snitzerb277da02014-10-04 10:55:32 -06002789 if (rot == 1) {
Hannes Reinecke89d94752016-10-18 15:40:34 +09002790 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
2791 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
Mike Snitzerb277da02014-10-04 10:55:32 -06002792 }
Martin K. Petersen3821d762009-05-23 11:43:38 -04002793
Hannes Reinecke89d94752016-10-18 15:40:34 +09002794 sdkp->zoned = (buffer[8] >> 4) & 3;
2795 if (sdkp->zoned == 1)
2796 q->limits.zoned = BLK_ZONED_HA;
2797 else if (sdkp->device->type == TYPE_ZBC)
2798 q->limits.zoned = BLK_ZONED_HM;
2799 else
2800 q->limits.zoned = BLK_ZONED_NONE;
2801 if (blk_queue_is_zoned(q) && sdkp->first_scan)
2802 sd_printk(KERN_NOTICE, sdkp, "Host-%s zoned block device\n",
2803 q->limits.zoned == BLK_ZONED_HM ? "managed" : "aware");
2804
James Bottomleye3deec02009-11-03 12:33:07 -06002805 out:
Martin K. Petersen3821d762009-05-23 11:43:38 -04002806 kfree(buffer);
2807}
2808
Martin K. Petersen045d3fe2010-09-10 01:22:07 -04002809/**
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002810 * sd_read_block_provisioning - Query provisioning VPD page
Martin K. Petersen045d3fe2010-09-10 01:22:07 -04002811 * @disk: disk to query
2812 */
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002813static void sd_read_block_provisioning(struct scsi_disk *sdkp)
Martin K. Petersen045d3fe2010-09-10 01:22:07 -04002814{
2815 unsigned char *buffer;
2816 const int vpd_len = 8;
2817
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002818 if (sdkp->lbpme == 0)
Martin K. Petersen045d3fe2010-09-10 01:22:07 -04002819 return;
2820
2821 buffer = kmalloc(vpd_len, GFP_KERNEL);
2822
2823 if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb2, buffer, vpd_len))
2824 goto out;
2825
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002826 sdkp->lbpvpd = 1;
2827 sdkp->lbpu = (buffer[5] >> 7) & 1; /* UNMAP */
2828 sdkp->lbpws = (buffer[5] >> 6) & 1; /* WRITE SAME(16) with UNMAP */
2829 sdkp->lbpws10 = (buffer[5] >> 5) & 1; /* WRITE SAME(10) with UNMAP */
Martin K. Petersen045d3fe2010-09-10 01:22:07 -04002830
2831 out:
2832 kfree(buffer);
2833}
2834
Martin K. Petersen5db44862012-09-18 12:19:32 -04002835static void sd_read_write_same(struct scsi_disk *sdkp, unsigned char *buffer)
2836{
Martin K. Petersen66c28f92013-06-06 22:15:55 -04002837 struct scsi_device *sdev = sdkp->device;
2838
Martin K. Petersen54b2b502013-10-23 06:25:40 -04002839 if (sdev->host->no_write_same) {
2840 sdev->no_write_same = 1;
2841
2842 return;
2843 }
2844
Martin K. Petersen66c28f92013-06-06 22:15:55 -04002845 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, INQUIRY) < 0) {
Bernd Schubertaf736232013-09-23 14:47:32 +02002846 /* too large values might cause issues with arcmsr */
2847 int vpd_buf_len = 64;
2848
Martin K. Petersen66c28f92013-06-06 22:15:55 -04002849 sdev->no_report_opcodes = 1;
2850
2851 /* Disable WRITE SAME if REPORT SUPPORTED OPERATION
2852 * CODES is unsupported and the device has an ATA
2853 * Information VPD page (SAT).
2854 */
Bernd Schubertaf736232013-09-23 14:47:32 +02002855 if (!scsi_get_vpd_page(sdev, 0x89, buffer, vpd_buf_len))
Martin K. Petersen66c28f92013-06-06 22:15:55 -04002856 sdev->no_write_same = 1;
2857 }
2858
2859 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME_16) == 1)
Martin K. Petersen5db44862012-09-18 12:19:32 -04002860 sdkp->ws16 = 1;
Martin K. Petersen66c28f92013-06-06 22:15:55 -04002861
2862 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME) == 1)
2863 sdkp->ws10 = 1;
Martin K. Petersen5db44862012-09-18 12:19:32 -04002864}
2865
Martin K. Petersen3821d762009-05-23 11:43:38 -04002866/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 * sd_revalidate_disk - called the first time a new disk is seen,
2868 * performs disk spin up, read_capacity, etc.
2869 * @disk: struct gendisk we care about
2870 **/
2871static int sd_revalidate_disk(struct gendisk *disk)
2872{
2873 struct scsi_disk *sdkp = scsi_disk(disk);
2874 struct scsi_device *sdp = sdkp->device;
Martin K. Petersenca369d52015-11-13 16:46:48 -05002875 struct request_queue *q = sdkp->disk->queue;
Hannes Reinecke89d94752016-10-18 15:40:34 +09002876 sector_t old_capacity = sdkp->capacity;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 unsigned char *buffer;
Martin K. Petersenca369d52015-11-13 16:46:48 -05002878 unsigned int dev_max, rw_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05002880 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
2881 "sd_revalidate_disk\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 /*
2884 * If the device is offline, don't try and read capacity or any
2885 * of the other niceties.
2886 */
2887 if (!scsi_device_online(sdp))
2888 goto out;
2889
Bernhard Wallea6123f12007-05-21 17:15:26 +02002890 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 if (!buffer) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002892 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
2893 "allocation failure.\n");
James Bottomleyea73a9f2005-08-28 11:33:52 -05002894 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
2896
Martin K. Petersene73aec82007-02-27 22:40:55 -05002897 sd_spinup_disk(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
2899 /*
2900 * Without media there is no reason to ask; moreover, some devices
2901 * react badly if we do.
2902 */
2903 if (sdkp->media_present) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002904 sd_read_capacity(sdkp, buffer);
Martin K. Petersenffd4bc22009-07-29 14:06:53 -04002905
Hannes Reinecke5ddfe082016-04-01 08:57:36 +02002906 if (scsi_device_supports_vpd(sdp)) {
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002907 sd_read_block_provisioning(sdkp);
Martin K. Petersenffd4bc22009-07-29 14:06:53 -04002908 sd_read_block_limits(sdkp);
2909 sd_read_block_characteristics(sdkp);
Hannes Reinecke89d94752016-10-18 15:40:34 +09002910 sd_zbc_read_zones(sdkp, buffer);
Martin K. Petersenffd4bc22009-07-29 14:06:53 -04002911 }
2912
Hannes Reinecke89d94752016-10-18 15:40:34 +09002913 sd_print_capacity(sdkp, old_capacity);
2914
Martin K. Petersene73aec82007-02-27 22:40:55 -05002915 sd_read_write_protect_flag(sdkp, buffer);
2916 sd_read_cache_type(sdkp, buffer);
Martin K. Petersene0597d72008-07-17 04:28:34 -04002917 sd_read_app_tag_own(sdkp, buffer);
Martin K. Petersen5db44862012-09-18 12:19:32 -04002918 sd_read_write_same(sdkp, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 }
Tejun Heo461d4e92006-01-06 09:52:55 +01002920
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002921 sdkp->first_scan = 0;
2922
Tejun Heo461d4e92006-01-06 09:52:55 +01002923 /*
2924 * We now have all cache related info, determine how we deal
Tejun Heo4913efe2010-09-03 11:56:16 +02002925 * with flush requests.
Tejun Heo461d4e92006-01-06 09:52:55 +01002926 */
Vaughan Caocb2fb682014-06-03 17:37:30 +08002927 sd_set_flush_flag(sdkp);
Tejun Heo461d4e92006-01-06 09:52:55 +01002928
Martin K. Petersenca369d52015-11-13 16:46:48 -05002929 /* Initial block count limit based on CDB TRANSFER LENGTH field size. */
2930 dev_max = sdp->use_16_for_rw ? SD_MAX_XFER_BLOCKS : SD_DEF_XFER_BLOCKS;
Brian King3a9794d2015-01-29 15:54:40 -06002931
Martin K. Petersenca369d52015-11-13 16:46:48 -05002932 /* Some devices report a maximum block count for READ/WRITE requests. */
2933 dev_max = min_not_zero(dev_max, sdkp->max_xfer_blocks);
2934 q->limits.max_dev_sectors = logical_to_sectors(sdp, dev_max);
2935
2936 /*
2937 * Use the device's preferred I/O size for reads and writes
Martin K. Petersen9c1d9c22015-12-16 17:53:52 -05002938 * unless the reported value is unreasonably small, large, or
2939 * garbage.
Martin K. Petersenca369d52015-11-13 16:46:48 -05002940 */
Martin K. Petersen9c1d9c22015-12-16 17:53:52 -05002941 if (sdkp->opt_xfer_blocks &&
2942 sdkp->opt_xfer_blocks <= dev_max &&
2943 sdkp->opt_xfer_blocks <= SD_DEF_XFER_BLOCKS &&
Martin K. Petersen6b7e9cd2016-05-12 22:17:34 -04002944 logical_to_bytes(sdp, sdkp->opt_xfer_blocks) >= PAGE_SIZE) {
2945 q->limits.io_opt = logical_to_bytes(sdp, sdkp->opt_xfer_blocks);
2946 rw_max = logical_to_sectors(sdp, sdkp->opt_xfer_blocks);
2947 } else
Martin K. Petersenca369d52015-11-13 16:46:48 -05002948 rw_max = BLK_DEF_MAX_SECTORS;
2949
2950 /* Combine with controller limits */
2951 q->limits.max_sectors = min(rw_max, queue_max_hw_sectors(q));
Martin K. Petersen4f258a42015-06-23 12:13:59 -04002952
Martin K. Petersenf08bb1e2016-03-28 21:18:56 -04002953 set_capacity(disk, logical_to_sectors(sdp, sdkp->capacity));
Martin K. Petersen5db44862012-09-18 12:19:32 -04002954 sd_config_write_same(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 kfree(buffer);
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 out:
2958 return 0;
2959}
2960
2961/**
Tejun Heo72ec24b2010-05-15 20:09:32 +02002962 * sd_unlock_native_capacity - unlock native capacity
2963 * @disk: struct gendisk to set capacity for
2964 *
2965 * Block layer calls this function if it detects that partitions
2966 * on @disk reach beyond the end of the device. If the SCSI host
2967 * implements ->unlock_native_capacity() method, it's invoked to
2968 * give it a chance to adjust the device capacity.
2969 *
2970 * CONTEXT:
2971 * Defined by block layer. Might sleep.
2972 */
2973static void sd_unlock_native_capacity(struct gendisk *disk)
2974{
2975 struct scsi_device *sdev = scsi_disk(disk)->device;
2976
2977 if (sdev->host->hostt->unlock_native_capacity)
2978 sdev->host->hostt->unlock_native_capacity(sdev);
2979}
2980
2981/**
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002982 * sd_format_disk_name - format disk name
2983 * @prefix: name prefix - ie. "sd" for SCSI disks
2984 * @index: index of the disk to format name for
2985 * @buf: output buffer
2986 * @buflen: length of the output buffer
2987 *
2988 * SCSI disk names starts at sda. The 26th device is sdz and the
2989 * 27th is sdaa. The last one for two lettered suffix is sdzz
2990 * which is followed by sdaaa.
2991 *
2992 * This is basically 26 base counting with one extra 'nil' entry
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08002993 * at the beginning from the second digit on and can be
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002994 * determined using similar method as 26 base conversion with the
2995 * index shifted -1 after each digit is computed.
2996 *
2997 * CONTEXT:
2998 * Don't care.
2999 *
3000 * RETURNS:
3001 * 0 on success, -errno on failure.
3002 */
3003static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
3004{
3005 const int base = 'z' - 'a' + 1;
3006 char *begin = buf + strlen(prefix);
3007 char *end = buf + buflen;
3008 char *p;
3009 int unit;
3010
3011 p = end - 1;
3012 *p = '\0';
3013 unit = base;
3014 do {
3015 if (p == begin)
3016 return -EINVAL;
3017 *--p = 'a' + (index % unit);
3018 index = (index / unit) - 1;
3019 } while (index >= 0);
3020
3021 memmove(begin, p, end - p);
3022 memcpy(buf, prefix, strlen(prefix));
3023
3024 return 0;
3025}
3026
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08003027/*
3028 * The asynchronous part of sd_probe
3029 */
3030static void sd_probe_async(void *data, async_cookie_t cookie)
3031{
3032 struct scsi_disk *sdkp = data;
3033 struct scsi_device *sdp;
3034 struct gendisk *gd;
3035 u32 index;
3036 struct device *dev;
3037
3038 sdp = sdkp->device;
3039 gd = sdkp->disk;
3040 index = sdkp->index;
3041 dev = &sdp->sdev_gendev;
3042
Michael Reed1a03ae02010-09-20 11:20:22 -05003043 gd->major = sd_major((index & 0xf0) >> 4);
3044 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
3045 gd->minors = SD_MINORS;
3046
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08003047 gd->fops = &sd_fops;
3048 gd->private_data = &sdkp->driver;
3049 gd->queue = sdkp->device->request_queue;
3050
Martin K. Petersen70a9b872009-03-09 11:33:31 -04003051 /* defaults, until the device tells us otherwise */
3052 sdp->sector_size = 512;
3053 sdkp->capacity = 0;
3054 sdkp->media_present = 1;
3055 sdkp->write_prot = 0;
James Bottomley39c60a02013-04-24 14:02:53 -07003056 sdkp->cache_override = 0;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04003057 sdkp->WCE = 0;
3058 sdkp->RCD = 0;
3059 sdkp->ATO = 0;
3060 sdkp->first_scan = 1;
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05003061 sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04003062
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08003063 sd_revalidate_disk(gd);
3064
NeilBrown97fedbb2010-03-16 08:55:32 +01003065 gd->flags = GENHD_FL_EXT_DEVT;
Tejun Heo2bae0092010-12-18 18:42:23 +01003066 if (sdp->removable) {
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08003067 gd->flags |= GENHD_FL_REMOVABLE;
Tejun Heo2bae0092010-12-18 18:42:23 +01003068 gd->events |= DISK_EVENT_MEDIA_CHANGE;
3069 }
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08003070
Aaron Lu10c580e2013-10-10 13:22:36 +08003071 blk_pm_runtime_init(sdp->request_queue, dev);
Dan Williams0d52c7562016-06-15 19:44:20 -07003072 device_add_disk(dev, gd);
Martin K. Petersenfe542392012-09-21 12:44:12 -04003073 if (sdkp->capacity)
3074 sd_dif_config_host(sdkp);
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08003075
Martin K. Petersen3821d762009-05-23 11:43:38 -04003076 sd_revalidate_disk(gd);
3077
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08003078 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
3079 sdp->removable ? "removable " : "");
Alan Stern478a8a02010-06-16 14:52:17 -04003080 scsi_autopm_put_device(sdp);
James Bottomleyea038f62009-08-21 09:47:54 -06003081 put_device(&sdkp->dev);
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08003082}
3083
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09003084/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 * sd_probe - called during driver initialization and whenever a
3086 * new scsi device is attached to the system. It is called once
3087 * for each scsi device (not just disks) present.
3088 * @dev: pointer to device object
3089 *
3090 * Returns 0 if successful (or not interested in this scsi device
3091 * (e.g. scanner)); 1 when there is an error.
3092 *
3093 * Note: this function is invoked from the scsi mid-level.
3094 * This function sets up the mapping between a given
3095 * <host,channel,id,lun> (found in sdp) and new device name
3096 * (e.g. /dev/sda). More precisely it is the block device major
3097 * and minor number that is chosen here.
3098 *
Petr Uzel2db93ce2012-02-24 16:56:54 +01003099 * Assume sd_probe is not re-entrant (for time being)
3100 * Also think about sd_probe() and sd_remove() running coincidentally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 **/
3102static int sd_probe(struct device *dev)
3103{
3104 struct scsi_device *sdp = to_scsi_device(dev);
3105 struct scsi_disk *sdkp;
3106 struct gendisk *gd;
H Hartley Sweeten439d77f2010-08-10 18:01:20 -07003107 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 int error;
3109
Subhash Jadavani6fe8c1d2014-09-10 14:54:09 +03003110 scsi_autopm_get_device(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 error = -ENODEV;
Hannes Reinecke89d94752016-10-18 15:40:34 +09003112 if (sdp->type != TYPE_DISK &&
3113 sdp->type != TYPE_ZBC &&
3114 sdp->type != TYPE_MOD &&
3115 sdp->type != TYPE_RBC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 goto out;
3117
Hannes Reinecke89d94752016-10-18 15:40:34 +09003118#ifndef CONFIG_BLK_DEV_ZONED
3119 if (sdp->type == TYPE_ZBC)
3120 goto out;
3121#endif
James Bottomley9ccfc752005-10-02 11:45:08 -05003122 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
Petr Uzel2db93ce2012-02-24 16:56:54 +01003123 "sd_probe\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -05003126 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (!sdkp)
3128 goto out;
3129
Tejun Heo689d6fa2008-08-25 19:56:16 +09003130 gd = alloc_disk(SD_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 if (!gd)
3132 goto out_free;
3133
Tejun Heof27bac22008-07-14 14:59:30 +09003134 do {
3135 if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
3136 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Tejun Heo4034cc62009-02-21 11:04:45 +09003138 spin_lock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09003139 error = ida_get_new(&sd_index_ida, &index);
Tejun Heo4034cc62009-02-21 11:04:45 +09003140 spin_unlock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09003141 } while (error == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Dave Kleikamp21208ae2011-10-19 11:49:04 -05003143 if (error) {
3144 sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 goto out_put;
Michael Reed1a03ae02010-09-20 11:20:22 -05003146 }
3147
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09003148 error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
Dave Kleikamp21208ae2011-10-19 11:49:04 -05003149 if (error) {
3150 sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n");
Tejun Heof27bac22008-07-14 14:59:30 +09003151 goto out_free_index;
Dave Kleikamp21208ae2011-10-19 11:49:04 -05003152 }
Tejun Heof27bac22008-07-14 14:59:30 +09003153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 sdkp->device = sdp;
3155 sdkp->driver = &sd_template;
3156 sdkp->disk = gd;
3157 sdkp->index = index;
Arnd Bergmann409f3492010-07-07 16:51:29 +02003158 atomic_set(&sdkp->openers, 0);
Josh Hunt9e1a1532012-06-09 07:03:39 -07003159 atomic_set(&sdkp->device->ioerr_cnt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
James Bottomley601e7632009-05-26 20:35:48 +00003161 if (!sdp->request_queue->rq_timeout) {
3162 if (sdp->type != TYPE_MOD)
3163 blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
3164 else
3165 blk_queue_rq_timeout(sdp->request_queue,
3166 SD_MOD_TIMEOUT);
3167 }
3168
3169 device_initialize(&sdkp->dev);
Alan Stern478a8a02010-06-16 14:52:17 -04003170 sdkp->dev.parent = dev;
James Bottomley601e7632009-05-26 20:35:48 +00003171 sdkp->dev.class = &sd_disk_class;
Kees Cook02aa2a32013-07-03 15:04:56 -07003172 dev_set_name(&sdkp->dev, "%s", dev_name(dev));
James Bottomley601e7632009-05-26 20:35:48 +00003173
Dan Carpenterdee05862015-01-19 17:41:12 +03003174 error = device_add(&sdkp->dev);
3175 if (error)
James Bottomley601e7632009-05-26 20:35:48 +00003176 goto out_free_index;
3177
Alan Stern478a8a02010-06-16 14:52:17 -04003178 get_device(dev);
3179 dev_set_drvdata(dev, sdkp);
James Bottomley601e7632009-05-26 20:35:48 +00003180
James Bottomleyea038f62009-08-21 09:47:54 -06003181 get_device(&sdkp->dev); /* prevent release before async_schedule */
Dan Williamsa7a20d12012-03-22 17:05:11 -07003182 async_schedule_domain(sd_probe_async, sdkp, &scsi_sd_probe_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 return 0;
3185
Tejun Heof27bac22008-07-14 14:59:30 +09003186 out_free_index:
Tejun Heo4034cc62009-02-21 11:04:45 +09003187 spin_lock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09003188 ida_remove(&sd_index_ida, index);
Tejun Heo4034cc62009-02-21 11:04:45 +09003189 spin_unlock(&sd_index_lock);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06003190 out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 put_disk(gd);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06003192 out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 kfree(sdkp);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06003194 out:
Subhash Jadavani6fe8c1d2014-09-10 14:54:09 +03003195 scsi_autopm_put_device(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return error;
3197}
3198
3199/**
3200 * sd_remove - called whenever a scsi disk (previously recognized by
3201 * sd_probe) is detached from the system. It is called (potentially
3202 * multiple times) during sd module unload.
3203 * @sdp: pointer to mid level scsi device object
3204 *
3205 * Note: this function is invoked from the scsi mid-level.
3206 * This function potentially frees up a device name (e.g. /dev/sdc)
3207 * that could be re-used by a subsequent sd_probe().
3208 * This function is not called when the built-in sd driver is "exit-ed".
3209 **/
3210static int sd_remove(struct device *dev)
3211{
James Bottomley601e7632009-05-26 20:35:48 +00003212 struct scsi_disk *sdkp;
Hannes Reinecke0761df92013-05-10 11:06:16 +02003213 dev_t devt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
James Bottomley601e7632009-05-26 20:35:48 +00003215 sdkp = dev_get_drvdata(dev);
Hannes Reinecke0761df92013-05-10 11:06:16 +02003216 devt = disk_devt(sdkp->disk);
Alan Stern478a8a02010-06-16 14:52:17 -04003217 scsi_autopm_get_device(sdkp->device);
3218
Dan Williams3c31b522014-04-10 15:30:35 -07003219 async_synchronize_full_domain(&scsi_sd_pm_domain);
Dan Williamsa7a20d12012-03-22 17:05:11 -07003220 async_synchronize_full_domain(&scsi_sd_probe_domain);
Tony Jonesee959b02008-02-22 00:13:36 +01003221 device_del(&sdkp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 del_gendisk(sdkp->disk);
3223 sd_shutdown(dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -05003224
Hannes Reinecke89d94752016-10-18 15:40:34 +09003225 sd_zbc_remove(sdkp);
3226
Hannes Reinecke0761df92013-05-10 11:06:16 +02003227 blk_register_region(devt, SD_MINORS, NULL,
3228 sd_default_probe, NULL, NULL);
3229
Arjan van de Ven0b950672006-01-11 13:16:10 +01003230 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -05003231 dev_set_drvdata(dev, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01003232 put_device(&sdkp->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01003233 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
3235 return 0;
3236}
3237
3238/**
3239 * scsi_disk_release - Called to free the scsi_disk structure
Tony Jonesee959b02008-02-22 00:13:36 +01003240 * @dev: pointer to embedded class device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 *
Arjan van de Ven0b950672006-01-11 13:16:10 +01003242 * sd_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 * called on last put, you should always use the scsi_disk_get()
3244 * scsi_disk_put() helpers which manipulate the semaphore directly
Tony Jonesee959b02008-02-22 00:13:36 +01003245 * and never do a direct put_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 **/
Tony Jonesee959b02008-02-22 00:13:36 +01003247static void scsi_disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Tony Jonesee959b02008-02-22 00:13:36 +01003249 struct scsi_disk *sdkp = to_scsi_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 struct gendisk *disk = sdkp->disk;
3251
Tejun Heo4034cc62009-02-21 11:04:45 +09003252 spin_lock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09003253 ida_remove(&sd_index_ida, sdkp->index);
Tejun Heo4034cc62009-02-21 11:04:45 +09003254 spin_unlock(&sd_index_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
3256 disk->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 put_disk(disk);
Alan Stern39b7f1e2005-11-04 14:44:41 -05003258 put_device(&sdkp->device->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
3260 kfree(sdkp);
3261}
3262
James Bottomleycc5d2c82007-03-20 12:26:03 -05003263static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003264{
3265 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
3266 struct scsi_sense_hdr sshdr;
James Bottomleycc5d2c82007-03-20 12:26:03 -05003267 struct scsi_device *sdp = sdkp->device;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003268 int res;
3269
3270 if (start)
3271 cmd[4] |= 1; /* START */
3272
Stefan Richterd2886ea2008-05-11 00:34:07 +02003273 if (sdp->start_stop_pwr_cond)
3274 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
3275
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003276 if (!scsi_device_online(sdp))
3277 return -ENODEV;
3278
Lin Ming9b214932013-03-23 11:42:25 +08003279 res = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
Christoph Hellwige8064022016-10-20 15:12:13 +02003280 SD_TIMEOUT, SD_MAX_RETRIES, NULL, 0, RQF_PM);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003281 if (res) {
Hannes Reineckeef613292014-10-24 14:27:00 +02003282 sd_print_result(sdkp, "Start/Stop Unit failed", res);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003283 if (driver_byte(res) & DRIVER_SENSE)
James Bottomleycc5d2c82007-03-20 12:26:03 -05003284 sd_print_sense_hdr(sdkp, &sshdr);
Oliver Neukum95897912013-09-16 13:28:15 +02003285 if (scsi_sense_valid(&sshdr) &&
3286 /* 0x3a is medium not present */
3287 sshdr.asc == 0x3a)
3288 res = 0;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003289 }
3290
Oliver Neukum95897912013-09-16 13:28:15 +02003291 /* SCSI error codes must not go to the generic layer */
3292 if (res)
3293 return -EIO;
3294
3295 return 0;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003296}
3297
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298/*
3299 * Send a SYNCHRONIZE CACHE instruction down to the device through
3300 * the normal SCSI command structure. Wait for the command to
3301 * complete.
3302 */
3303static void sd_shutdown(struct device *dev)
3304{
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01003305 struct scsi_disk *sdkp = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
3307 if (!sdkp)
3308 return; /* this can happen */
3309
Lin Ming54f575882011-12-05 09:20:26 +08003310 if (pm_runtime_suspended(dev))
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01003311 return;
Lin Ming54f575882011-12-05 09:20:26 +08003312
Oliver Neukum95897912013-09-16 13:28:15 +02003313 if (sdkp->WCE && sdkp->media_present) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05003314 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
3315 sd_sync_cache(sdkp);
Alan Stern39b7f1e2005-11-04 14:44:41 -05003316 }
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003317
James Bottomleycc5d2c82007-03-20 12:26:03 -05003318 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
3319 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
3320 sd_start_stop_device(sdkp, 0);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003321 }
Alan Stern39b7f1e2005-11-04 14:44:41 -05003322}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
Oliver Neukum95897912013-09-16 13:28:15 +02003324static int sd_suspend_common(struct device *dev, bool ignore_stop_errors)
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003325{
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01003326 struct scsi_disk *sdkp = dev_get_drvdata(dev);
Alan Stern09ff92f2007-05-21 09:55:04 -04003327 int ret = 0;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003328
Alan Stern13b43892016-01-20 11:26:01 -05003329 if (!sdkp) /* E.g.: runtime suspend following sd_remove() */
3330 return 0;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003331
Oliver Neukum95897912013-09-16 13:28:15 +02003332 if (sdkp->WCE && sdkp->media_present) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05003333 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003334 ret = sd_sync_cache(sdkp);
Oliver Neukum95897912013-09-16 13:28:15 +02003335 if (ret) {
3336 /* ignore OFFLINE device */
3337 if (ret == -ENODEV)
3338 ret = 0;
Alan Stern09ff92f2007-05-21 09:55:04 -04003339 goto done;
Oliver Neukum95897912013-09-16 13:28:15 +02003340 }
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003341 }
3342
Aaron Lu691e3d32012-11-09 15:27:55 +08003343 if (sdkp->device->manage_start_stop) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05003344 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
Oliver Neukum95897912013-09-16 13:28:15 +02003345 /* an error is not worth aborting a system sleep */
James Bottomleycc5d2c82007-03-20 12:26:03 -05003346 ret = sd_start_stop_device(sdkp, 0);
Oliver Neukum95897912013-09-16 13:28:15 +02003347 if (ignore_stop_errors)
3348 ret = 0;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003349 }
3350
Alan Stern09ff92f2007-05-21 09:55:04 -04003351done:
Alan Stern09ff92f2007-05-21 09:55:04 -04003352 return ret;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003353}
3354
Oliver Neukum95897912013-09-16 13:28:15 +02003355static int sd_suspend_system(struct device *dev)
3356{
3357 return sd_suspend_common(dev, true);
3358}
3359
3360static int sd_suspend_runtime(struct device *dev)
3361{
3362 return sd_suspend_common(dev, false);
3363}
3364
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003365static int sd_resume(struct device *dev)
3366{
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01003367 struct scsi_disk *sdkp = dev_get_drvdata(dev);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003368
Alan Stern13b43892016-01-20 11:26:01 -05003369 if (!sdkp) /* E.g.: runtime resume at the start of sd_probe() */
3370 return 0;
3371
James Bottomleycc5d2c82007-03-20 12:26:03 -05003372 if (!sdkp->device->manage_start_stop)
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01003373 return 0;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003374
James Bottomleycc5d2c82007-03-20 12:26:03 -05003375 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01003376 return sd_start_stop_device(sdkp, 1);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09003377}
3378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379/**
3380 * init_sd - entry point for this driver (both when built in or when
3381 * a module).
3382 *
3383 * Note: this function registers this driver with the scsi mid-level.
3384 **/
3385static int __init init_sd(void)
3386{
Jeff Garzik5e4009b2006-10-04 05:32:54 -04003387 int majors = 0, i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
3389 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
3390
Hannes Reinecke0761df92013-05-10 11:06:16 +02003391 for (i = 0; i < SD_MAJORS; i++) {
3392 if (register_blkdev(sd_major(i), "sd") != 0)
3393 continue;
3394 majors++;
3395 blk_register_region(sd_major(i), SD_MINORS, NULL,
3396 sd_default_probe, NULL, NULL);
3397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
3399 if (!majors)
3400 return -ENODEV;
3401
Jeff Garzik5e4009b2006-10-04 05:32:54 -04003402 err = class_register(&sd_disk_class);
3403 if (err)
3404 goto err_out;
James Bottomley6bdaa1f2006-03-18 14:14:21 -06003405
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04003406 sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
3407 0, 0, NULL);
3408 if (!sd_cdb_cache) {
3409 printk(KERN_ERR "sd: can't init extended cdb cache\n");
Clément Calmels8d964472014-06-03 23:34:25 +02003410 err = -ENOMEM;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04003411 goto err_out_class;
3412 }
3413
3414 sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
3415 if (!sd_cdb_pool) {
3416 printk(KERN_ERR "sd: can't init extended cdb pool\n");
Clément Calmels8d964472014-06-03 23:34:25 +02003417 err = -ENOMEM;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04003418 goto err_out_cache;
3419 }
3420
Joel D. Diazafd5e342012-10-10 10:36:11 +02003421 err = scsi_register_driver(&sd_template.gendrv);
3422 if (err)
3423 goto err_out_driver;
3424
Jeff Garzik5e4009b2006-10-04 05:32:54 -04003425 return 0;
3426
Joel D. Diazafd5e342012-10-10 10:36:11 +02003427err_out_driver:
3428 mempool_destroy(sd_cdb_pool);
3429
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04003430err_out_cache:
3431 kmem_cache_destroy(sd_cdb_cache);
3432
Jeff Garzik5e4009b2006-10-04 05:32:54 -04003433err_out_class:
3434 class_unregister(&sd_disk_class);
3435err_out:
3436 for (i = 0; i < SD_MAJORS; i++)
3437 unregister_blkdev(sd_major(i), "sd");
3438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439}
3440
3441/**
3442 * exit_sd - exit point for this driver (when it is a module).
3443 *
3444 * Note: this function unregisters this driver from the scsi mid-level.
3445 **/
3446static void __exit exit_sd(void)
3447{
3448 int i;
3449
3450 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
3451
Joel D. Diazafd5e342012-10-10 10:36:11 +02003452 scsi_unregister_driver(&sd_template.gendrv);
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04003453 mempool_destroy(sd_cdb_pool);
3454 kmem_cache_destroy(sd_cdb_cache);
3455
Jeff Garzik5e4009b2006-10-04 05:32:54 -04003456 class_unregister(&sd_disk_class);
3457
Hannes Reinecke0761df92013-05-10 11:06:16 +02003458 for (i = 0; i < SD_MAJORS; i++) {
3459 blk_unregister_region(sd_major(i), SD_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 unregister_blkdev(sd_major(i), "sd");
Hannes Reinecke0761df92013-05-10 11:06:16 +02003461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464module_init(init_sd);
3465module_exit(exit_sd);
Martin K. Petersene73aec82007-02-27 22:40:55 -05003466
3467static void sd_print_sense_hdr(struct scsi_disk *sdkp,
3468 struct scsi_sense_hdr *sshdr)
3469{
Hannes Reinecke21045512015-01-08 07:43:46 +01003470 scsi_print_sense_hdr(sdkp->device,
3471 sdkp->disk ? sdkp->disk->disk_name : NULL, sshdr);
Martin K. Petersene73aec82007-02-27 22:40:55 -05003472}
3473
Hannes Reineckeef613292014-10-24 14:27:00 +02003474static void sd_print_result(const struct scsi_disk *sdkp, const char *msg,
3475 int result)
Martin K. Petersene73aec82007-02-27 22:40:55 -05003476{
Hannes Reineckeef613292014-10-24 14:27:00 +02003477 const char *hb_string = scsi_hostbyte_string(result);
3478 const char *db_string = scsi_driverbyte_string(result);
3479
3480 if (hb_string || db_string)
3481 sd_printk(KERN_INFO, sdkp,
3482 "%s: Result: hostbyte=%s driverbyte=%s\n", msg,
3483 hb_string ? hb_string : "invalid",
3484 db_string ? db_string : "invalid");
3485 else
3486 sd_printk(KERN_INFO, sdkp,
3487 "%s: Result: hostbyte=0x%02x driverbyte=0x%02x\n",
3488 msg, host_byte(result), driver_byte(result));
Martin K. Petersene73aec82007-02-27 22:40:55 -05003489}
3490