blob: b9290e77bb19a8e9df617302f1cf0f8c6119026a [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 Tokarevd7b8bcb2006-10-27 16:02:37 +040093MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
94MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
95MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
Rene Hermanf018fa52006-03-08 00:14:20 -080096
Tejun Heo870d6652008-08-25 19:47:25 +090097#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
Tejun Heof615b482008-08-25 19:47:24 +090098#define SD_MINORS 16
Tejun Heo870d6652008-08-25 19:47:25 +090099#else
Tejun Heo3e1a7ff2008-08-25 19:56:17 +0900100#define SD_MINORS 0
Tejun Heo870d6652008-08-25 19:47:25 +0900101#endif
102
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500103static void sd_config_discard(struct scsi_disk *, unsigned int);
Martin K. Petersen5db44862012-09-18 12:19:32 -0400104static void sd_config_write_same(struct scsi_disk *);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800105static int sd_revalidate_disk(struct gendisk *);
Tejun Heo72ec24b2010-05-15 20:09:32 +0200106static void sd_unlock_native_capacity(struct gendisk *disk);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800107static int sd_probe(struct device *);
108static int sd_remove(struct device *);
109static void sd_shutdown(struct device *);
Oliver Neukum95897912013-09-16 13:28:15 +0200110static int sd_suspend_system(struct device *);
111static int sd_suspend_runtime(struct device *);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800112static int sd_resume(struct device *);
113static void sd_rescan(struct device *);
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200114static int sd_init_command(struct scsi_cmnd *SCpnt);
115static void sd_uninit_command(struct scsi_cmnd *SCpnt);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800116static int sd_done(struct scsi_cmnd *);
James Bottomley24510792013-11-11 13:44:53 +0100117static int sd_eh_action(struct scsi_cmnd *, int);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800118static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
Tony Jonesee959b02008-02-22 00:13:36 +0100119static void scsi_disk_release(struct device *cdev);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800120static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
Hannes Reineckeef613292014-10-24 14:27:00 +0200121static void sd_print_result(const struct scsi_disk *, const char *, int);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800122
Tejun Heo4034cc62009-02-21 11:04:45 +0900123static DEFINE_SPINLOCK(sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +0900124static DEFINE_IDA(sd_index_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* This semaphore is used to mediate the 0->1 reference get in the
127 * face of object destruction (i.e. we can't allow a get on an
128 * object after last put) */
Arjan van de Ven0b950672006-01-11 13:16:10 +0100129static DEFINE_MUTEX(sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
H Hartley Sweeten439d77f2010-08-10 18:01:20 -0700131static struct kmem_cache *sd_cdb_cache;
132static mempool_t *sd_cdb_pool;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400133
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600134static const char *sd_cache_types[] = {
135 "write through", "none", "write back",
136 "write back, no read (daft)"
137};
138
Vaughan Caocb2fb682014-06-03 17:37:30 +0800139static void sd_set_flush_flag(struct scsi_disk *sdkp)
140{
Jens Axboeeb310e22016-03-30 10:06:11 -0600141 bool wc = false, fua = false;
Vaughan Caocb2fb682014-06-03 17:37:30 +0800142
143 if (sdkp->WCE) {
Jens Axboeeb310e22016-03-30 10:06:11 -0600144 wc = true;
Vaughan Caocb2fb682014-06-03 17:37:30 +0800145 if (sdkp->DPOFUA)
Jens Axboeeb310e22016-03-30 10:06:11 -0600146 fua = true;
Vaughan Caocb2fb682014-06-03 17:37:30 +0800147 }
148
Jens Axboeeb310e22016-03-30 10:06:11 -0600149 blk_queue_write_cache(sdkp->disk->queue, wc, fua);
Vaughan Caocb2fb682014-06-03 17:37:30 +0800150}
151
Tony Jonesee959b02008-02-22 00:13:36 +0100152static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700153cache_type_store(struct device *dev, struct device_attribute *attr,
154 const char *buf, size_t count)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600155{
156 int i, ct = -1, rcd, wce, sp;
Tony Jonesee959b02008-02-22 00:13:36 +0100157 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600158 struct scsi_device *sdp = sdkp->device;
159 char buffer[64];
160 char *buffer_data;
161 struct scsi_mode_data data;
162 struct scsi_sense_hdr sshdr;
Ben Hutchings2ee3e262013-05-27 19:07:19 +0100163 static const char temp[] = "temporary ";
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600164 int len;
165
166 if (sdp->type != TYPE_DISK)
167 /* no cache control on RBC devices; theoretically they
168 * can do it, but there's probably so many exceptions
169 * it's not worth the risk */
170 return -EINVAL;
171
James Bottomley39c60a02013-04-24 14:02:53 -0700172 if (strncmp(buf, temp, sizeof(temp) - 1) == 0) {
173 buf += sizeof(temp) - 1;
174 sdkp->cache_override = 1;
175 } else {
176 sdkp->cache_override = 0;
177 }
178
Tobias Klauser6391a112006-06-08 22:23:48 -0700179 for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
H Hartley Sweeten439d77f2010-08-10 18:01:20 -0700180 len = strlen(sd_cache_types[i]);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600181 if (strncmp(sd_cache_types[i], buf, len) == 0 &&
182 buf[len] == '\n') {
183 ct = i;
184 break;
185 }
186 }
187 if (ct < 0)
188 return -EINVAL;
189 rcd = ct & 0x01 ? 1 : 0;
Sujit Reddy Thumma2eefd572014-08-11 15:40:37 +0300190 wce = (ct & 0x02) && !sdkp->write_prot ? 1 : 0;
James Bottomley39c60a02013-04-24 14:02:53 -0700191
192 if (sdkp->cache_override) {
193 sdkp->WCE = wce;
194 sdkp->RCD = rcd;
Vaughan Caocb2fb682014-06-03 17:37:30 +0800195 sd_set_flush_flag(sdkp);
James Bottomley39c60a02013-04-24 14:02:53 -0700196 return count;
197 }
198
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600199 if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
200 SD_MAX_RETRIES, &data, NULL))
201 return -EINVAL;
Andrew Mortona9312fb2006-03-25 03:08:30 -0800202 len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600203 data.block_descriptor_length);
204 buffer_data = buffer + data.header_length +
205 data.block_descriptor_length;
206 buffer_data[2] &= ~0x05;
207 buffer_data[2] |= wce << 2 | rcd;
208 sp = buffer_data[0] & 0x80 ? 1 : 0;
Gabriel Krisman Bertazi2c5d16d2015-10-30 16:04:43 -0200209 buffer_data[0] &= ~0x80;
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600210
211 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
212 SD_MAX_RETRIES, &data, &sshdr)) {
213 if (scsi_sense_valid(&sshdr))
Martin K. Petersene73aec82007-02-27 22:40:55 -0500214 sd_print_sense_hdr(sdkp, &sshdr);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600215 return -EINVAL;
216 }
Andrew Pattersonf98a8ca2008-09-04 14:27:35 -0600217 revalidate_disk(sdkp->disk);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600218 return count;
219}
220
Tony Jonesee959b02008-02-22 00:13:36 +0100221static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700222manage_start_stop_show(struct device *dev, struct device_attribute *attr,
223 char *buf)
224{
225 struct scsi_disk *sdkp = to_scsi_disk(dev);
226 struct scsi_device *sdp = sdkp->device;
227
228 return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
229}
230
231static ssize_t
232manage_start_stop_store(struct device *dev, struct device_attribute *attr,
233 const char *buf, size_t count)
Tejun Heoc3c94c52007-03-21 00:13:59 +0900234{
Tony Jonesee959b02008-02-22 00:13:36 +0100235 struct scsi_disk *sdkp = to_scsi_disk(dev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900236 struct scsi_device *sdp = sdkp->device;
237
238 if (!capable(CAP_SYS_ADMIN))
239 return -EACCES;
240
241 sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
242
243 return count;
244}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700245static DEVICE_ATTR_RW(manage_start_stop);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900246
Tony Jonesee959b02008-02-22 00:13:36 +0100247static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700248allow_restart_show(struct device *dev, struct device_attribute *attr, char *buf)
249{
250 struct scsi_disk *sdkp = to_scsi_disk(dev);
251
252 return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
253}
254
255static ssize_t
256allow_restart_store(struct device *dev, struct device_attribute *attr,
257 const char *buf, size_t count)
Brian Kinga144c5a2006-06-27 11:10:31 -0500258{
Tony Jonesee959b02008-02-22 00:13:36 +0100259 struct scsi_disk *sdkp = to_scsi_disk(dev);
Brian Kinga144c5a2006-06-27 11:10:31 -0500260 struct scsi_device *sdp = sdkp->device;
261
262 if (!capable(CAP_SYS_ADMIN))
263 return -EACCES;
264
265 if (sdp->type != TYPE_DISK)
266 return -EINVAL;
267
268 sdp->allow_restart = simple_strtoul(buf, NULL, 10);
269
270 return count;
271}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700272static DEVICE_ATTR_RW(allow_restart);
Brian Kinga144c5a2006-06-27 11:10:31 -0500273
Tony Jonesee959b02008-02-22 00:13:36 +0100274static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700275cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600276{
Tony Jonesee959b02008-02-22 00:13:36 +0100277 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600278 int ct = sdkp->RCD + 2*sdkp->WCE;
279
280 return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
281}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700282static DEVICE_ATTR_RW(cache_type);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600283
Tony Jonesee959b02008-02-22 00:13:36 +0100284static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700285FUA_show(struct device *dev, struct device_attribute *attr, char *buf)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600286{
Tony Jonesee959b02008-02-22 00:13:36 +0100287 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600288
289 return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
290}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700291static DEVICE_ATTR_RO(FUA);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600292
Tony Jonesee959b02008-02-22 00:13:36 +0100293static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700294protection_type_show(struct device *dev, struct device_attribute *attr,
295 char *buf)
Martin K. Petersene0597d72008-07-17 04:28:34 -0400296{
297 struct scsi_disk *sdkp = to_scsi_disk(dev);
298
299 return snprintf(buf, 20, "%u\n", sdkp->protection_type);
300}
301
302static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700303protection_type_store(struct device *dev, struct device_attribute *attr,
304 const char *buf, size_t count)
Martin K. Petersen81724992012-08-28 14:29:34 -0400305{
306 struct scsi_disk *sdkp = to_scsi_disk(dev);
307 unsigned int val;
308 int err;
309
310 if (!capable(CAP_SYS_ADMIN))
311 return -EACCES;
312
313 err = kstrtouint(buf, 10, &val);
314
315 if (err)
316 return err;
317
Christoph Hellwig8475c812016-09-11 19:35:41 +0200318 if (val >= 0 && val <= T10_PI_TYPE3_PROTECTION)
Martin K. Petersen81724992012-08-28 14:29:34 -0400319 sdkp->protection_type = val;
320
321 return count;
322}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700323static DEVICE_ATTR_RW(protection_type);
Martin K. Petersen81724992012-08-28 14:29:34 -0400324
325static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700326protection_mode_show(struct device *dev, struct device_attribute *attr,
327 char *buf)
Martin K. Petersen518fa8e2010-10-08 01:36:24 -0400328{
329 struct scsi_disk *sdkp = to_scsi_disk(dev);
330 struct scsi_device *sdp = sdkp->device;
331 unsigned int dif, dix;
332
333 dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
334 dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type);
335
Christoph Hellwig8475c812016-09-11 19:35:41 +0200336 if (!dix && scsi_host_dix_capable(sdp->host, T10_PI_TYPE0_PROTECTION)) {
Martin K. Petersen518fa8e2010-10-08 01:36:24 -0400337 dif = 0;
338 dix = 1;
339 }
340
341 if (!dif && !dix)
342 return snprintf(buf, 20, "none\n");
343
344 return snprintf(buf, 20, "%s%u\n", dix ? "dix" : "dif", dif);
345}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700346static DEVICE_ATTR_RO(protection_mode);
Martin K. Petersen518fa8e2010-10-08 01:36:24 -0400347
348static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700349app_tag_own_show(struct device *dev, struct device_attribute *attr, char *buf)
Martin K. Petersene0597d72008-07-17 04:28:34 -0400350{
351 struct scsi_disk *sdkp = to_scsi_disk(dev);
352
353 return snprintf(buf, 20, "%u\n", sdkp->ATO);
354}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700355static DEVICE_ATTR_RO(app_tag_own);
Martin K. Petersene0597d72008-07-17 04:28:34 -0400356
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500357static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700358thin_provisioning_show(struct device *dev, struct device_attribute *attr,
359 char *buf)
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500360{
361 struct scsi_disk *sdkp = to_scsi_disk(dev);
362
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500363 return snprintf(buf, 20, "%u\n", sdkp->lbpme);
364}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700365static DEVICE_ATTR_RO(thin_provisioning);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500366
367static const char *lbp_mode[] = {
368 [SD_LBP_FULL] = "full",
369 [SD_LBP_UNMAP] = "unmap",
370 [SD_LBP_WS16] = "writesame_16",
371 [SD_LBP_WS10] = "writesame_10",
372 [SD_LBP_ZERO] = "writesame_zero",
373 [SD_LBP_DISABLE] = "disabled",
374};
375
376static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700377provisioning_mode_show(struct device *dev, struct device_attribute *attr,
378 char *buf)
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500379{
380 struct scsi_disk *sdkp = to_scsi_disk(dev);
381
382 return snprintf(buf, 20, "%s\n", lbp_mode[sdkp->provisioning_mode]);
383}
384
385static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700386provisioning_mode_store(struct device *dev, struct device_attribute *attr,
387 const char *buf, size_t count)
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500388{
389 struct scsi_disk *sdkp = to_scsi_disk(dev);
390 struct scsi_device *sdp = sdkp->device;
391
392 if (!capable(CAP_SYS_ADMIN))
393 return -EACCES;
394
395 if (sdp->type != TYPE_DISK)
396 return -EINVAL;
397
398 if (!strncmp(buf, lbp_mode[SD_LBP_UNMAP], 20))
399 sd_config_discard(sdkp, SD_LBP_UNMAP);
400 else if (!strncmp(buf, lbp_mode[SD_LBP_WS16], 20))
401 sd_config_discard(sdkp, SD_LBP_WS16);
402 else if (!strncmp(buf, lbp_mode[SD_LBP_WS10], 20))
403 sd_config_discard(sdkp, SD_LBP_WS10);
404 else if (!strncmp(buf, lbp_mode[SD_LBP_ZERO], 20))
405 sd_config_discard(sdkp, SD_LBP_ZERO);
406 else if (!strncmp(buf, lbp_mode[SD_LBP_DISABLE], 20))
407 sd_config_discard(sdkp, SD_LBP_DISABLE);
408 else
409 return -EINVAL;
410
411 return count;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500412}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700413static DEVICE_ATTR_RW(provisioning_mode);
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500414
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -0500415static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700416max_medium_access_timeouts_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -0500418{
419 struct scsi_disk *sdkp = to_scsi_disk(dev);
420
421 return snprintf(buf, 20, "%u\n", sdkp->max_medium_access_timeouts);
422}
423
424static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700425max_medium_access_timeouts_store(struct device *dev,
426 struct device_attribute *attr, const char *buf,
427 size_t count)
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -0500428{
429 struct scsi_disk *sdkp = to_scsi_disk(dev);
430 int err;
431
432 if (!capable(CAP_SYS_ADMIN))
433 return -EACCES;
434
435 err = kstrtouint(buf, 10, &sdkp->max_medium_access_timeouts);
436
437 return err ? err : count;
438}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700439static DEVICE_ATTR_RW(max_medium_access_timeouts);
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -0500440
Martin K. Petersen5db44862012-09-18 12:19:32 -0400441static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700442max_write_same_blocks_show(struct device *dev, struct device_attribute *attr,
443 char *buf)
Martin K. Petersen5db44862012-09-18 12:19:32 -0400444{
445 struct scsi_disk *sdkp = to_scsi_disk(dev);
446
447 return snprintf(buf, 20, "%u\n", sdkp->max_ws_blocks);
448}
449
450static ssize_t
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700451max_write_same_blocks_store(struct device *dev, struct device_attribute *attr,
452 const char *buf, size_t count)
Martin K. Petersen5db44862012-09-18 12:19:32 -0400453{
454 struct scsi_disk *sdkp = to_scsi_disk(dev);
455 struct scsi_device *sdp = sdkp->device;
456 unsigned long max;
457 int err;
458
459 if (!capable(CAP_SYS_ADMIN))
460 return -EACCES;
461
462 if (sdp->type != TYPE_DISK)
463 return -EINVAL;
464
465 err = kstrtoul(buf, 10, &max);
466
467 if (err)
468 return err;
469
470 if (max == 0)
471 sdp->no_write_same = 1;
Martin K. Petersen66c28f92013-06-06 22:15:55 -0400472 else if (max <= SD_MAX_WS16_BLOCKS) {
473 sdp->no_write_same = 0;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400474 sdkp->max_ws_blocks = max;
Martin K. Petersen66c28f92013-06-06 22:15:55 -0400475 }
Martin K. Petersen5db44862012-09-18 12:19:32 -0400476
477 sd_config_write_same(sdkp);
478
479 return count;
480}
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700481static DEVICE_ATTR_RW(max_write_same_blocks);
Martin K. Petersen5db44862012-09-18 12:19:32 -0400482
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700483static struct attribute *sd_disk_attrs[] = {
484 &dev_attr_cache_type.attr,
485 &dev_attr_FUA.attr,
486 &dev_attr_allow_restart.attr,
487 &dev_attr_manage_start_stop.attr,
488 &dev_attr_protection_type.attr,
489 &dev_attr_protection_mode.attr,
490 &dev_attr_app_tag_own.attr,
491 &dev_attr_thin_provisioning.attr,
492 &dev_attr_provisioning_mode.attr,
493 &dev_attr_max_write_same_blocks.attr,
494 &dev_attr_max_medium_access_timeouts.attr,
495 NULL,
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600496};
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700497ATTRIBUTE_GROUPS(sd_disk);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600498
499static struct class sd_disk_class = {
500 .name = "scsi_disk",
501 .owner = THIS_MODULE,
Tony Jonesee959b02008-02-22 00:13:36 +0100502 .dev_release = scsi_disk_release,
Greg Kroah-Hartmane1ea2352013-07-24 15:05:28 -0700503 .dev_groups = sd_disk_groups,
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600504};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Aaron Lu691e3d32012-11-09 15:27:55 +0800506static const struct dev_pm_ops sd_pm_ops = {
Oliver Neukum95897912013-09-16 13:28:15 +0200507 .suspend = sd_suspend_system,
Aaron Lu691e3d32012-11-09 15:27:55 +0800508 .resume = sd_resume,
Oliver Neukum95897912013-09-16 13:28:15 +0200509 .poweroff = sd_suspend_system,
Aaron Lu691e3d32012-11-09 15:27:55 +0800510 .restore = sd_resume,
Oliver Neukum95897912013-09-16 13:28:15 +0200511 .runtime_suspend = sd_suspend_runtime,
Aaron Lu691e3d32012-11-09 15:27:55 +0800512 .runtime_resume = sd_resume,
513};
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static struct scsi_driver sd_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 .gendrv = {
517 .name = "sd",
Christoph Hellwig3af6b352014-11-12 18:34:51 +0100518 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 .probe = sd_probe,
520 .remove = sd_remove,
521 .shutdown = sd_shutdown,
Aaron Lu691e3d32012-11-09 15:27:55 +0800522 .pm = &sd_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 },
524 .rescan = sd_rescan,
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200525 .init_command = sd_init_command,
526 .uninit_command = sd_uninit_command,
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800527 .done = sd_done,
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -0500528 .eh_action = sd_eh_action,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529};
530
531/*
Hannes Reinecke0761df92013-05-10 11:06:16 +0200532 * Dummy kobj_map->probe function.
533 * The default ->probe function will call modprobe, which is
534 * pointless as this module is already loaded.
535 */
536static struct kobject *sd_default_probe(dev_t devt, int *partno, void *data)
537{
538 return NULL;
539}
540
541/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * Device no to disk mapping:
543 *
544 * major disc2 disc p1
545 * |............|.............|....|....| <- dev_t
546 * 31 20 19 8 7 4 3 0
547 *
548 * Inside a major, we have 16k disks, however mapped non-
549 * contiguously. The first 16 disks are for major0, the next
550 * ones with major1, ... Disk 256 is for major0 again, disk 272
551 * for major1, ...
552 * As we stay compatible with our numbering scheme, we can reuse
553 * the well-know SCSI majors 8, 65--71, 136--143.
554 */
555static int sd_major(int major_idx)
556{
557 switch (major_idx) {
558 case 0:
559 return SCSI_DISK0_MAJOR;
560 case 1 ... 7:
561 return SCSI_DISK1_MAJOR + major_idx - 1;
562 case 8 ... 15:
563 return SCSI_DISK8_MAJOR + major_idx - 8;
564 default:
565 BUG();
566 return 0; /* shut up gcc */
567 }
568}
569
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +0100570static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct scsi_disk *sdkp = NULL;
573
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +0100574 mutex_lock(&sd_ref_mutex);
575
Alan Stern39b7f1e2005-11-04 14:44:41 -0500576 if (disk->private_data) {
577 sdkp = scsi_disk(disk);
578 if (scsi_device_get(sdkp->device) == 0)
Tony Jonesee959b02008-02-22 00:13:36 +0100579 get_device(&sdkp->dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500580 else
581 sdkp = NULL;
582 }
Arjan van de Ven0b950672006-01-11 13:16:10 +0100583 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return sdkp;
585}
586
587static void scsi_disk_put(struct scsi_disk *sdkp)
588{
589 struct scsi_device *sdev = sdkp->device;
590
Arjan van de Ven0b950672006-01-11 13:16:10 +0100591 mutex_lock(&sd_ref_mutex);
Tony Jonesee959b02008-02-22 00:13:36 +0100592 put_device(&sdkp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 scsi_device_put(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100594 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Martin K. Petersenc6115292014-09-26 19:20:08 -0400597static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd,
598 unsigned int dix, unsigned int dif)
599{
600 struct bio *bio = scmd->request->bio;
601 unsigned int prot_op = sd_prot_op(rq_data_dir(scmd->request), dix, dif);
602 unsigned int protect = 0;
603
604 if (dix) { /* DIX Type 0, 1, 2, 3 */
605 if (bio_integrity_flagged(bio, BIP_IP_CHECKSUM))
606 scmd->prot_flags |= SCSI_PROT_IP_CHECKSUM;
607
608 if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
609 scmd->prot_flags |= SCSI_PROT_GUARD_CHECK;
610 }
611
Christoph Hellwig8475c812016-09-11 19:35:41 +0200612 if (dif != T10_PI_TYPE3_PROTECTION) { /* DIX/DIF Type 0, 1, 2 */
Martin K. Petersenc6115292014-09-26 19:20:08 -0400613 scmd->prot_flags |= SCSI_PROT_REF_INCREMENT;
614
615 if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
616 scmd->prot_flags |= SCSI_PROT_REF_CHECK;
617 }
618
619 if (dif) { /* DIX/DIF Type 1, 2, 3 */
620 scmd->prot_flags |= SCSI_PROT_TRANSFER_PI;
621
622 if (bio_integrity_flagged(bio, BIP_DISK_NOCHECK))
623 protect = 3 << 5; /* Disable target PI checking */
624 else
625 protect = 1 << 5; /* Enable target PI checking */
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -0400626 }
627
628 scsi_set_prot_op(scmd, prot_op);
629 scsi_set_prot_type(scmd, dif);
Martin K. Petersenc6115292014-09-26 19:20:08 -0400630 scmd->prot_flags &= sd_prot_flag_mask(prot_op);
631
632 return protect;
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -0400633}
634
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500635static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode)
636{
637 struct request_queue *q = sdkp->disk->queue;
638 unsigned int logical_block_size = sdkp->device->sector_size;
639 unsigned int max_blocks = 0;
640
Martin K. Petersen79850902014-11-07 00:08:13 -0500641 q->limits.discard_zeroes_data = 0;
Martin K. Petersen39773722015-11-13 16:46:47 -0500642
643 /*
644 * When LBPRZ is reported, discard alignment and granularity
645 * must be fixed to the logical block size. Otherwise the block
646 * layer will drop misaligned portions of the request which can
647 * lead to data corruption. If LBPRZ is not set, we honor the
648 * device preference.
649 */
650 if (sdkp->lbprz) {
651 q->limits.discard_alignment = 0;
Martin K. Petersen6540a652016-03-05 17:52:02 -0500652 q->limits.discard_granularity = logical_block_size;
Martin K. Petersen39773722015-11-13 16:46:47 -0500653 } else {
654 q->limits.discard_alignment = sdkp->unmap_alignment *
655 logical_block_size;
656 q->limits.discard_granularity =
657 max(sdkp->physical_block_size,
658 sdkp->unmap_granularity * logical_block_size);
659 }
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500660
Martin K. Petersen89730392012-02-13 15:39:00 -0500661 sdkp->provisioning_mode = mode;
662
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500663 switch (mode) {
664
665 case SD_LBP_DISABLE:
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600666 blk_queue_max_discard_sectors(q, 0);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500667 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
668 return;
669
670 case SD_LBP_UNMAP:
Martin K. Petersen5db44862012-09-18 12:19:32 -0400671 max_blocks = min_not_zero(sdkp->max_unmap_blocks,
672 (u32)SD_MAX_WS16_BLOCKS);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500673 break;
674
675 case SD_LBP_WS16:
Martin K. Petersen5db44862012-09-18 12:19:32 -0400676 max_blocks = min_not_zero(sdkp->max_ws_blocks,
677 (u32)SD_MAX_WS16_BLOCKS);
Martin K. Petersen79850902014-11-07 00:08:13 -0500678 q->limits.discard_zeroes_data = sdkp->lbprz;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500679 break;
680
681 case SD_LBP_WS10:
Martin K. Petersen5db44862012-09-18 12:19:32 -0400682 max_blocks = min_not_zero(sdkp->max_ws_blocks,
683 (u32)SD_MAX_WS10_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_ZERO:
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. Petersenc98a0eb2011-03-08 02:07:15 -0500690 q->limits.discard_zeroes_data = 1;
691 break;
692 }
693
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600694 blk_queue_max_discard_sectors(q, max_blocks * (logical_block_size >> 9));
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500695 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500696}
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698/**
Martin K. Petersen26e85fc2012-09-18 12:19:31 -0400699 * sd_setup_discard_cmnd - unmap blocks on thinly provisioned device
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200700 * @sdp: scsi device to operate one
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500701 * @rq: Request to prepare
702 *
703 * Will issue either UNMAP or WRITE SAME(16) depending on preference
704 * indicated by target device.
705 **/
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200706static int sd_setup_discard_cmnd(struct scsi_cmnd *cmd)
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500707{
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200708 struct request *rq = cmd->request;
709 struct scsi_device *sdp = cmd->device;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500710 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
Martin K. Petersen26e85fc2012-09-18 12:19:31 -0400711 sector_t sector = blk_rq_pos(rq);
712 unsigned int nr_sectors = blk_rq_sectors(rq);
713 unsigned int nr_bytes = blk_rq_bytes(rq);
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200714 unsigned int len;
FUJITA Tomonorif1126e92010-07-01 19:49:18 +0900715 int ret;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500716 char *buf;
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200717 struct page *page;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500718
Martin K. Petersen26e85fc2012-09-18 12:19:31 -0400719 sector >>= ilog2(sdp->sector_size) - 9;
720 nr_sectors >>= ilog2(sdp->sector_size) - 9;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500721
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200722 page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
723 if (!page)
724 return BLKPREP_DEFER;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500725
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500726 switch (sdkp->provisioning_mode) {
727 case SD_LBP_UNMAP:
728 buf = page_address(page);
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200729
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200730 cmd->cmd_len = 10;
731 cmd->cmnd[0] = UNMAP;
732 cmd->cmnd[8] = 24;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500733
734 put_unaligned_be16(6 + 16, &buf[0]);
735 put_unaligned_be16(16, &buf[2]);
736 put_unaligned_be64(sector, &buf[8]);
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200737 put_unaligned_be32(nr_sectors, &buf[16]);
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500738
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200739 len = 24;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500740 break;
741
742 case SD_LBP_WS16:
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200743 cmd->cmd_len = 16;
744 cmd->cmnd[0] = WRITE_SAME_16;
745 cmd->cmnd[1] = 0x8; /* UNMAP */
746 put_unaligned_be64(sector, &cmd->cmnd[2]);
747 put_unaligned_be32(nr_sectors, &cmd->cmnd[10]);
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200748
749 len = sdkp->device->sector_size;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500750 break;
751
752 case SD_LBP_WS10:
753 case SD_LBP_ZERO:
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200754 cmd->cmd_len = 10;
755 cmd->cmnd[0] = WRITE_SAME;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500756 if (sdkp->provisioning_mode == SD_LBP_WS10)
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200757 cmd->cmnd[1] = 0x8; /* UNMAP */
758 put_unaligned_be32(sector, &cmd->cmnd[2]);
759 put_unaligned_be16(nr_sectors, &cmd->cmnd[7]);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500760
761 len = sdkp->device->sector_size;
762 break;
763
764 default:
Martin K. Petersen0fb5b1f2016-02-04 00:52:12 -0500765 ret = BLKPREP_INVALID;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500766 goto out;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500767 }
768
Jens Axboedc4a9302014-04-16 21:37:30 -0600769 rq->completion_data = page;
Subhash Jadavani70e75882016-08-12 16:44:37 -0700770 if (likely(!sdp->timeout_override))
771 rq->timeout = SD_TIMEOUT;
772 else
773 rq->timeout = sdp->timeout_override;
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200774
775 cmd->transfersize = len;
Christoph Hellwige4200f82014-06-28 16:18:59 +0200776 cmd->allowed = SD_MAX_RETRIES;
Christoph Hellwig6a7b4392014-06-28 12:35:13 +0200777
778 /*
779 * Initially __data_len is set to the amount of data that needs to be
780 * transferred to the target. This amount depends on whether WRITE SAME
781 * or UNMAP is being used. After the scatterlist has been mapped by
782 * scsi_init_io() we set __data_len to the size of the area to be
783 * discarded on disk. This allows us to report completion on the full
784 * amount of blocks described by the request.
785 */
Ming Lin37e58232016-03-22 00:24:44 -0700786 blk_add_request_payload(rq, page, 0, len);
Christoph Hellwig3c356bd2014-09-05 18:20:23 -0700787 ret = scsi_init_io(cmd);
Martin K. Petersen26e85fc2012-09-18 12:19:31 -0400788 rq->__data_len = nr_bytes;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -0500789
790out:
Jens Axboeb4f42e22014-04-10 09:46:28 -0600791 if (ret != BLKPREP_OK)
FUJITA Tomonori610a6342010-07-08 10:16:17 +0200792 __free_page(page);
FUJITA Tomonorif1126e92010-07-01 19:49:18 +0900793 return ret;
794}
795
Martin K. Petersen5db44862012-09-18 12:19:32 -0400796static void sd_config_write_same(struct scsi_disk *sdkp)
797{
798 struct request_queue *q = sdkp->disk->queue;
799 unsigned int logical_block_size = sdkp->device->sector_size;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400800
801 if (sdkp->device->no_write_same) {
802 sdkp->max_ws_blocks = 0;
803 goto out;
804 }
805
806 /* Some devices can not handle block counts above 0xffff despite
807 * supporting WRITE SAME(16). Consequently we default to 64k
808 * blocks per I/O unless the device explicitly advertises a
809 * bigger limit.
810 */
Martin K. Petersen66c28f92013-06-06 22:15:55 -0400811 if (sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS)
812 sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
813 (u32)SD_MAX_WS16_BLOCKS);
814 else if (sdkp->ws16 || sdkp->ws10 || sdkp->device->no_report_opcodes)
815 sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
816 (u32)SD_MAX_WS10_BLOCKS);
817 else {
818 sdkp->device->no_write_same = 1;
819 sdkp->max_ws_blocks = 0;
820 }
Martin K. Petersen5db44862012-09-18 12:19:32 -0400821
822out:
Martin K. Petersen66c28f92013-06-06 22:15:55 -0400823 blk_queue_max_write_same_sectors(q, sdkp->max_ws_blocks *
824 (logical_block_size >> 9));
Martin K. Petersen5db44862012-09-18 12:19:32 -0400825}
826
827/**
828 * sd_setup_write_same_cmnd - write the same data to multiple blocks
Christoph Hellwig59b11342014-06-28 12:22:22 +0200829 * @cmd: command to prepare
Martin K. Petersen5db44862012-09-18 12:19:32 -0400830 *
831 * Will issue either WRITE SAME(10) or WRITE SAME(16) depending on
832 * preference indicated by target device.
833 **/
Christoph Hellwig59b11342014-06-28 12:22:22 +0200834static int sd_setup_write_same_cmnd(struct scsi_cmnd *cmd)
Martin K. Petersen5db44862012-09-18 12:19:32 -0400835{
Christoph Hellwig59b11342014-06-28 12:22:22 +0200836 struct request *rq = cmd->request;
837 struct scsi_device *sdp = cmd->device;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400838 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
839 struct bio *bio = rq->bio;
840 sector_t sector = blk_rq_pos(rq);
841 unsigned int nr_sectors = blk_rq_sectors(rq);
842 unsigned int nr_bytes = blk_rq_bytes(rq);
843 int ret;
844
845 if (sdkp->device->no_write_same)
Martin K. Petersen0fb5b1f2016-02-04 00:52:12 -0500846 return BLKPREP_INVALID;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400847
Kent Overstreeta4ad39b12013-08-07 14:24:32 -0700848 BUG_ON(bio_offset(bio) || bio_iovec(bio).bv_len != sdp->sector_size);
Martin K. Petersen5db44862012-09-18 12:19:32 -0400849
850 sector >>= ilog2(sdp->sector_size) - 9;
851 nr_sectors >>= ilog2(sdp->sector_size) - 9;
852
Subhash Jadavani70e75882016-08-12 16:44:37 -0700853 if (likely(!sdp->timeout_override))
854 rq->timeout = SD_WRITE_SAME_TIMEOUT;
855 else
856 rq->timeout = sdp->timeout_override;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400857
858 if (sdkp->ws16 || sector > 0xffffffff || nr_sectors > 0xffff) {
Christoph Hellwig59b11342014-06-28 12:22:22 +0200859 cmd->cmd_len = 16;
860 cmd->cmnd[0] = WRITE_SAME_16;
861 put_unaligned_be64(sector, &cmd->cmnd[2]);
862 put_unaligned_be32(nr_sectors, &cmd->cmnd[10]);
Martin K. Petersen5db44862012-09-18 12:19:32 -0400863 } else {
Christoph Hellwig59b11342014-06-28 12:22:22 +0200864 cmd->cmd_len = 10;
865 cmd->cmnd[0] = WRITE_SAME;
866 put_unaligned_be32(sector, &cmd->cmnd[2]);
867 put_unaligned_be16(nr_sectors, &cmd->cmnd[7]);
Martin K. Petersen5db44862012-09-18 12:19:32 -0400868 }
869
Christoph Hellwig59b11342014-06-28 12:22:22 +0200870 cmd->transfersize = sdp->sector_size;
Christoph Hellwiga25ee542014-06-28 12:29:19 +0200871 cmd->allowed = SD_MAX_RETRIES;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400872
Christoph Hellwig59b11342014-06-28 12:22:22 +0200873 /*
874 * For WRITE_SAME the data transferred in the DATA IN buffer is
875 * different from the amount of data actually written to the target.
876 *
877 * We set up __data_len to the amount of data transferred from the
878 * DATA IN buffer so that blk_rq_map_sg set up the proper S/G list
879 * to transfer a single sector of data first, but then reset it to
880 * the amount of data to be written right after so that the I/O path
881 * knows how much to actually write.
882 */
883 rq->__data_len = sdp->sector_size;
Christoph Hellwig3c356bd2014-09-05 18:20:23 -0700884 ret = scsi_init_io(cmd);
Christoph Hellwig59b11342014-06-28 12:22:22 +0200885 rq->__data_len = nr_bytes;
Martin K. Petersen5db44862012-09-18 12:19:32 -0400886 return ret;
887}
888
Christoph Hellwiga118c6c2014-06-28 12:08:05 +0200889static int sd_setup_flush_cmnd(struct scsi_cmnd *cmd)
FUJITA Tomonori90467c22010-07-03 17:45:34 +0900890{
Christoph Hellwiga118c6c2014-06-28 12:08:05 +0200891 struct request *rq = cmd->request;
FUJITA Tomonori90467c22010-07-03 17:45:34 +0900892
Christoph Hellwiga118c6c2014-06-28 12:08:05 +0200893 /* flush requests don't perform I/O, zero the S/G table */
894 memset(&cmd->sdb, 0, sizeof(cmd->sdb));
895
896 cmd->cmnd[0] = SYNCHRONIZE_CACHE;
897 cmd->cmd_len = 10;
898 cmd->transfersize = 0;
899 cmd->allowed = SD_MAX_RETRIES;
900
K. Y. Srinivasan26b9fd82014-07-18 17:11:27 +0200901 rq->timeout = rq->q->rq_timeout * SD_FLUSH_TIMEOUT_MULTIPLIER;
Christoph Hellwiga118c6c2014-06-28 12:08:05 +0200902 return BLKPREP_OK;
FUJITA Tomonori90467c22010-07-03 17:45:34 +0900903}
904
Christoph Hellwig87949ee2014-06-28 12:40:18 +0200905static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200907 struct request *rq = SCpnt->request;
908 struct scsi_device *sdp = SCpnt->device;
Christoph Hellwig776b23a2006-01-06 18:34:07 +0100909 struct gendisk *disk = rq->rq_disk;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400910 struct scsi_disk *sdkp;
Tejun Heo83096eb2009-05-07 22:24:39 +0900911 sector_t block = blk_rq_pos(rq);
Linus Torvalds18351072008-08-05 21:42:21 -0700912 sector_t threshold;
Tejun Heo83096eb2009-05-07 22:24:39 +0900913 unsigned int this_count = blk_rq_sectors(rq);
Martin K. Petersenc6115292014-09-26 19:20:08 -0400914 unsigned int dif, dix;
915 int ret;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400916 unsigned char protect;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500917
Christoph Hellwig3c356bd2014-09-05 18:20:23 -0700918 ret = scsi_init_io(SCpnt);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500919 if (ret != BLKPREP_OK)
920 goto out;
921 SCpnt = rq->special;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400922 sdkp = scsi_disk(disk);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500923
924 /* from here on until we're complete, any goto out
925 * is used for a killable error condition */
926 ret = BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200928 SCSI_LOG_HLQUEUE(1,
929 scmd_printk(KERN_INFO, SCpnt,
930 "%s: block=%llu, count=%d\n",
931 __func__, (unsigned long long)block, this_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (!sdp || !scsi_device_online(sdp) ||
Tejun Heo83096eb2009-05-07 22:24:39 +0900934 block + blk_rq_sectors(rq) > get_capacity(disk)) {
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500935 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
Tejun Heo83096eb2009-05-07 22:24:39 +0900936 "Finishing %u sectors\n",
937 blk_rq_sectors(rq)));
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500938 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
939 "Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500940 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
943 if (sdp->changed) {
944 /*
945 * quietly refuse to do anything to a changed disc until
946 * the changed bit has been reset
947 */
Alan Stern3ff55882010-09-07 11:44:01 -0400948 /* printk("SCSI disk has been changed or is not present. Prohibiting further I/O.\n"); */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500951
Hans de Goedea0899d42008-01-20 11:12:26 +0100952 /*
Linus Torvalds18351072008-08-05 21:42:21 -0700953 * Some SD card readers can't handle multi-sector accesses which touch
954 * the last one or two hardware sectors. Split accesses as needed.
Hans de Goedea0899d42008-01-20 11:12:26 +0100955 */
Linus Torvalds18351072008-08-05 21:42:21 -0700956 threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
957 (sdp->sector_size / 512);
958
959 if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
960 if (block < threshold) {
961 /* Access up to the threshold but not beyond */
962 this_count = threshold - block;
963 } else {
964 /* Access only a single hardware sector */
965 this_count = sdp->sector_size / 512;
966 }
967 }
Hans de Goedea0899d42008-01-20 11:12:26 +0100968
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500969 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
970 (unsigned long long)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 /*
973 * If we have a 1K hardware sectorsize, prevent access to single
974 * 512 byte sectors. In theory we could handle this - in fact
975 * the scsi cdrom driver must be able to handle this because
976 * we typically use 1K blocksizes, and cdroms typically have
977 * 2K hardware sectorsizes. Of course, things are simpler
978 * with the cdrom, since it is read-only. For performance
979 * reasons, the filesystems should be able to handle this
980 * and not force the scsi disk driver to use bounce buffers
981 * for this.
982 */
983 if (sdp->sector_size == 1024) {
Tejun Heo83096eb2009-05-07 22:24:39 +0900984 if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500985 scmd_printk(KERN_ERR, SCpnt,
986 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500987 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 } else {
989 block = block >> 1;
990 this_count = this_count >> 1;
991 }
992 }
993 if (sdp->sector_size == 2048) {
Tejun Heo83096eb2009-05-07 22:24:39 +0900994 if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500995 scmd_printk(KERN_ERR, SCpnt,
996 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500997 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 } else {
999 block = block >> 2;
1000 this_count = this_count >> 2;
1001 }
1002 }
1003 if (sdp->sector_size == 4096) {
Tejun Heo83096eb2009-05-07 22:24:39 +09001004 if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001005 scmd_printk(KERN_ERR, SCpnt,
1006 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 } else {
1009 block = block >> 3;
1010 this_count = this_count >> 3;
1011 }
1012 }
1013 if (rq_data_dir(rq) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 SCpnt->cmnd[0] = WRITE_6;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001015
Martin K. Petersen8c579ab2012-08-28 14:29:33 -04001016 if (blk_integrity_rq(rq))
Martin K. Petersenc6115292014-09-26 19:20:08 -04001017 sd_dif_prepare(SCpnt);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 } else if (rq_data_dir(rq) == READ) {
1020 SCpnt->cmnd[0] = READ_6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 } else {
Mike Christie4e1b2d522016-06-05 14:32:22 -05001022 scmd_printk(KERN_ERR, SCpnt, "Unknown command %llu,%llx\n",
Mike Christiec2df40d2016-06-05 14:32:17 -05001023 req_op(rq), (unsigned long long) rq->cmd_flags);
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001024 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001027 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
Tejun Heo83096eb2009-05-07 22:24:39 +09001028 "%s %d/%u 512 byte blocks.\n",
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001029 (rq_data_dir(rq) == WRITE) ?
1030 "writing" : "reading", this_count,
Tejun Heo83096eb2009-05-07 22:24:39 +09001031 blk_rq_sectors(rq)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Martin K. Petersenc6115292014-09-26 19:20:08 -04001033 dix = scsi_prot_sg_count(SCpnt);
1034 dif = scsi_host_dif_capable(SCpnt->device->host, sdkp->protection_type);
1035
1036 if (dif || dix)
1037 protect = sd_setup_protect_cmnd(SCpnt, dix, dif);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001038 else
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001039 protect = 0;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001040
Christoph Hellwig8475c812016-09-11 19:35:41 +02001041 if (protect && sdkp->protection_type == T10_PI_TYPE2_PROTECTION) {
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001042 SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
1043
1044 if (unlikely(SCpnt->cmnd == NULL)) {
1045 ret = BLKPREP_DEFER;
1046 goto out;
1047 }
1048
1049 SCpnt->cmd_len = SD_EXT_CDB_SIZE;
1050 memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
1051 SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
1052 SCpnt->cmnd[7] = 0x18;
1053 SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001054 SCpnt->cmnd[10] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001055
1056 /* LBA */
1057 SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
1058 SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
1059 SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
1060 SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
1061 SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
1062 SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
1063 SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
1064 SCpnt->cmnd[19] = (unsigned char) block & 0xff;
1065
1066 /* Expected Indirect LBA */
1067 SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
1068 SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
1069 SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
1070 SCpnt->cmnd[23] = (unsigned char) block & 0xff;
1071
1072 /* Transfer length */
1073 SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
1074 SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
1075 SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
1076 SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
Akinobu Mitae430cbc2014-06-02 22:56:47 +09001077 } else if (sdp->use_16_for_rw || (this_count > 0xffff)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 SCpnt->cmnd[0] += READ_16 - READ_6;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001079 SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
1081 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
1082 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
1083 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
1084 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
1085 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
1086 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
1087 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
1088 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
1089 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
1090 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
1091 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
1092 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
1093 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001094 scsi_device_protection(SCpnt->device) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 SCpnt->device->use_10_for_rw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 SCpnt->cmnd[0] += READ_10 - READ_6;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001097 SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
1099 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
1100 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
1101 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
1102 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
1103 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
1104 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
1105 } else {
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001106 if (unlikely(rq->cmd_flags & REQ_FUA)) {
Tejun Heo007365a2006-01-06 09:53:52 +01001107 /*
1108 * This happens only if this drive failed
1109 * 10byte rw command with ILLEGAL_REQUEST
1110 * during operation and thus turned off
1111 * use_10_for_rw.
1112 */
Martin K. Petersene73aec82007-02-27 22:40:55 -05001113 scmd_printk(KERN_ERR, SCpnt,
1114 "FUA write on READ/WRITE(6) drive\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001115 goto out;
Tejun Heo007365a2006-01-06 09:53:52 +01001116 }
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
1119 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
1120 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
1121 SCpnt->cmnd[4] = (unsigned char) this_count;
1122 SCpnt->cmnd[5] = 0;
1123 }
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001124 SCpnt->sdb.length = this_count * sdp->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /*
1127 * We shouldn't disconnect in the middle of a sector, so with a dumb
1128 * host adapter, it's safe to assume that we can at least transfer
1129 * this many bytes between each connect / disconnect.
1130 */
1131 SCpnt->transfersize = sdp->sector_size;
1132 SCpnt->underflow = this_count << 9;
1133 SCpnt->allowed = SD_MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 * This indicates that the command is ready from our end to be
1137 * queued.
1138 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001139 ret = BLKPREP_OK;
1140 out:
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +02001141 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001144static int sd_init_command(struct scsi_cmnd *cmd)
1145{
1146 struct request *rq = cmd->request;
1147
Mike Christiec2df40d2016-06-05 14:32:17 -05001148 switch (req_op(rq)) {
1149 case REQ_OP_DISCARD:
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001150 return sd_setup_discard_cmnd(cmd);
Mike Christiec2df40d2016-06-05 14:32:17 -05001151 case REQ_OP_WRITE_SAME:
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001152 return sd_setup_write_same_cmnd(cmd);
Mike Christie3a5e02c2016-06-05 14:32:23 -05001153 case REQ_OP_FLUSH:
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001154 return sd_setup_flush_cmnd(cmd);
Mike Christiec2df40d2016-06-05 14:32:17 -05001155 case REQ_OP_READ:
1156 case REQ_OP_WRITE:
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001157 return sd_setup_read_write_cmnd(cmd);
Mike Christiec2df40d2016-06-05 14:32:17 -05001158 default:
1159 BUG();
1160 }
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001161}
1162
1163static void sd_uninit_command(struct scsi_cmnd *SCpnt)
1164{
1165 struct request *rq = SCpnt->request;
1166
Mike Christiec2df40d2016-06-05 14:32:17 -05001167 if (req_op(rq) == REQ_OP_DISCARD)
Christoph Hellwig87949ee2014-06-28 12:40:18 +02001168 __free_page(rq->completion_data);
1169
1170 if (SCpnt->cmnd != rq->cmd) {
1171 mempool_free(SCpnt->cmnd, sd_cdb_pool);
1172 SCpnt->cmnd = NULL;
1173 SCpnt->cmd_len = 0;
1174 }
1175}
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177/**
1178 * sd_open - open a scsi disk device
1179 * @inode: only i_rdev member may be used
1180 * @filp: only f_mode and f_flags may be used
1181 *
1182 * Returns 0 if successful. Returns a negated errno value in case
1183 * of error.
1184 *
1185 * Note: This can be called from a user context (e.g. fsck(1) )
1186 * or from within the kernel (e.g. as a result of a mount(1) ).
1187 * In the latter case @inode and @filp carry an abridged amount
1188 * of information as noted above.
Arnd Bergmann409f3492010-07-07 16:51:29 +02001189 *
1190 * Locking: called with bdev->bd_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 **/
Al Viro0338e292008-03-02 10:41:04 -05001192static int sd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Al Viro0338e292008-03-02 10:41:04 -05001194 struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 struct scsi_device *sdev;
1196 int retval;
1197
Al Viro0338e292008-03-02 10:41:04 -05001198 if (!sdkp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return -ENXIO;
1200
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001201 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 sdev = sdkp->device;
1204
1205 /*
1206 * If the device is in error recovery, wait until it is done.
1207 * If the device is offline, then disallow any access to it.
1208 */
1209 retval = -ENXIO;
1210 if (!scsi_block_when_processing_errors(sdev))
1211 goto error_out;
1212
1213 if (sdev->removable || sdkp->write_prot)
Al Viro0338e292008-03-02 10:41:04 -05001214 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /*
1217 * If the drive is empty, just let the open fail.
1218 */
1219 retval = -ENOMEDIUM;
Al Viro0338e292008-03-02 10:41:04 -05001220 if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 goto error_out;
1222
1223 /*
1224 * If the device has the write protect tab set, have the open fail
1225 * if the user expects to be able to write to the thing.
1226 */
1227 retval = -EROFS;
Al Viro0338e292008-03-02 10:41:04 -05001228 if (sdkp->write_prot && (mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 goto error_out;
1230
1231 /*
1232 * It is possible that the disk changing stuff resulted in
1233 * the device being taken offline. If this is the case,
1234 * report this to the user, and don't pretend that the
1235 * open actually succeeded.
1236 */
1237 retval = -ENXIO;
1238 if (!scsi_device_online(sdev))
1239 goto error_out;
1240
Arnd Bergmann409f3492010-07-07 16:51:29 +02001241 if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (scsi_block_when_processing_errors(sdev))
1243 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
1244 }
1245
1246 return 0;
1247
1248error_out:
1249 scsi_disk_put(sdkp);
1250 return retval;
1251}
1252
1253/**
1254 * sd_release - invoked when the (last) close(2) is called on this
1255 * scsi disk.
1256 * @inode: only i_rdev member may be used
1257 * @filp: only f_mode and f_flags may be used
1258 *
1259 * Returns 0.
1260 *
1261 * Note: may block (uninterruptible) if error recovery is underway
1262 * on this disk.
Arnd Bergmann409f3492010-07-07 16:51:29 +02001263 *
1264 * Locking: called with bdev->bd_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 **/
Al Virodb2a1442013-05-05 21:52:57 -04001266static void sd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct scsi_disk *sdkp = scsi_disk(disk);
1269 struct scsi_device *sdev = sdkp->device;
1270
James Bottomley56937f72007-03-11 12:25:33 -05001271 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Alan Stern7e443312010-09-07 11:27:52 -04001273 if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (scsi_block_when_processing_errors(sdev))
1275 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
1276 }
1277
1278 /*
1279 * XXX and what if there are packets in flight and this close()
1280 * XXX is followed by a "rmmod sd_mod"?
1281 */
Alan Stern478a8a02010-06-16 14:52:17 -04001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 scsi_disk_put(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001286static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
1289 struct scsi_device *sdp = sdkp->device;
1290 struct Scsi_Host *host = sdp->host;
Martin K. Petersenf08bb1e2016-03-28 21:18:56 -04001291 sector_t capacity = logical_to_sectors(sdp, sdkp->capacity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int diskinfo[4];
1293
1294 /* default to most commonly used values */
Martin K. Petersenf08bb1e2016-03-28 21:18:56 -04001295 diskinfo[0] = 0x40; /* 1 << 6 */
1296 diskinfo[1] = 0x20; /* 1 << 5 */
1297 diskinfo[2] = capacity >> 11;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /* override with calculated, extended default, or driver values */
1300 if (host->hostt->bios_param)
Martin K. Petersenf08bb1e2016-03-28 21:18:56 -04001301 host->hostt->bios_param(sdp, bdev, capacity, diskinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 else
Martin K. Petersenf08bb1e2016-03-28 21:18:56 -04001303 scsicam_bios_param(bdev, capacity, diskinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001305 geo->heads = diskinfo[0];
1306 geo->sectors = diskinfo[1];
1307 geo->cylinders = diskinfo[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
1309}
1310
1311/**
1312 * sd_ioctl - process an ioctl
1313 * @inode: only i_rdev/i_bdev members may be used
1314 * @filp: only f_mode and f_flags may be used
1315 * @cmd: ioctl command number
1316 * @arg: this is third argument given to ioctl(2) system call.
1317 * Often contains a pointer.
1318 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001319 * Returns 0 if successful (some ioctls return positive numbers on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 * success as well). Returns a negated errno value in case of error.
1321 *
1322 * Note: most ioctls are forward onto the block subsystem or further
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001323 * down in the scsi subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 **/
Al Viro0338e292008-03-02 10:41:04 -05001325static int sd_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 unsigned int cmd, unsigned long arg)
1327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct gendisk *disk = bdev->bd_disk;
Nao Nishijimafe2d1852011-08-25 18:04:14 +09001329 struct scsi_disk *sdkp = scsi_disk(disk);
1330 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 void __user *p = (void __user *)arg;
1332 int error;
1333
Nao Nishijimafe2d1852011-08-25 18:04:14 +09001334 SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, "
1335 "cmd=0x%x\n", disk->disk_name, cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Paolo Bonzini0bfc96c2012-01-12 16:01:28 +01001337 error = scsi_verify_blk_ioctl(bdev, cmd);
1338 if (error < 0)
1339 return error;
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /*
1342 * If we are in the middle of error recovery, don't let anyone
1343 * else try and use this device. Also, if error recovery fails, it
1344 * may try and take the device offline, in which case all further
1345 * access to the device is prohibited.
1346 */
Christoph Hellwig906d15f2014-10-11 16:25:31 +02001347 error = scsi_ioctl_block_when_processing_errors(sdp, cmd,
1348 (mode & FMODE_NDELAY) != 0);
1349 if (error)
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001350 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /*
1353 * Send SCSI addressing ioctls directly to mid level, send other
1354 * ioctls to block level and then onto mid level if they can't be
1355 * resolved.
1356 */
1357 switch (cmd) {
1358 case SCSI_IOCTL_GET_IDLUN:
1359 case SCSI_IOCTL_GET_BUS_NUMBER:
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001360 error = scsi_ioctl(sdp, cmd, p);
1361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 default:
Paolo Bonzini577ebb32012-01-12 16:01:27 +01001363 error = scsi_cmd_blk_ioctl(bdev, mode, cmd, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (error != -ENOTTY)
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001365 break;
1366 error = scsi_ioctl(sdp, cmd, p);
1367 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001369out:
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001370 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
1373static void set_media_not_present(struct scsi_disk *sdkp)
1374{
Tejun Heo2bae0092010-12-18 18:42:23 +01001375 if (sdkp->media_present)
1376 sdkp->device->changed = 1;
1377
1378 if (sdkp->device->removable) {
1379 sdkp->media_present = 0;
1380 sdkp->capacity = 0;
1381 }
1382}
1383
1384static int media_not_present(struct scsi_disk *sdkp,
1385 struct scsi_sense_hdr *sshdr)
1386{
1387 if (!scsi_sense_valid(sshdr))
1388 return 0;
1389
1390 /* not invoked for commands that could return deferred errors */
1391 switch (sshdr->sense_key) {
1392 case UNIT_ATTENTION:
1393 case NOT_READY:
1394 /* medium not present */
1395 if (sshdr->asc == 0x3A) {
1396 set_media_not_present(sdkp);
1397 return 1;
1398 }
1399 }
1400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Martin K. Petersene73aec82007-02-27 22:40:55 -05001403static int sd_sync_cache(struct scsi_disk *sdkp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 int retries, res;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001406 struct scsi_device *sdp = sdkp->device;
James Bottomley7e660102013-10-04 21:42:24 +00001407 const int timeout = sdp->request_queue->rq_timeout
1408 * SD_FLUSH_TIMEOUT_MULTIPLIER;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001409 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 if (!scsi_device_online(sdp))
1412 return -ENODEV;
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 for (retries = 3; retries > 0; --retries) {
1415 unsigned char cmd[10] = { 0 };
1416
1417 cmd[0] = SYNCHRONIZE_CACHE;
1418 /*
1419 * Leave the rest of the command zero to indicate
1420 * flush everything.
1421 */
Lin Ming9b214932013-03-23 11:42:25 +08001422 res = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0,
James Bottomley7e660102013-10-04 21:42:24 +00001423 &sshdr, timeout, SD_MAX_RETRIES,
1424 NULL, REQ_PM);
James Bottomleyea73a9f2005-08-28 11:33:52 -05001425 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
1427 }
1428
Martin K. Petersene73aec82007-02-27 22:40:55 -05001429 if (res) {
Hannes Reineckeef613292014-10-24 14:27:00 +02001430 sd_print_result(sdkp, "Synchronize Cache(10) failed", res);
Oliver Neukum95897912013-09-16 13:28:15 +02001431
Martin K. Petersene73aec82007-02-27 22:40:55 -05001432 if (driver_byte(res) & DRIVER_SENSE)
1433 sd_print_sense_hdr(sdkp, &sshdr);
Oliver Neukum95897912013-09-16 13:28:15 +02001434 /* we need to evaluate the error return */
1435 if (scsi_sense_valid(&sshdr) &&
Alan Stern7aae5132014-01-15 15:37:04 -05001436 (sshdr.asc == 0x3a || /* medium not present */
1437 sshdr.asc == 0x20)) /* invalid command */
Oliver Neukum95897912013-09-16 13:28:15 +02001438 /* this is no error here */
1439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Oliver Neukum95897912013-09-16 13:28:15 +02001441 switch (host_byte(res)) {
1442 /* ignore errors due to racing a disconnection */
1443 case DID_BAD_TARGET:
1444 case DID_NO_CONNECT:
1445 return 0;
1446 /* signal the upper layer it might try again */
1447 case DID_BUS_BUSY:
1448 case DID_IMM_RETRY:
1449 case DID_REQUEUE:
1450 case DID_SOFT_ERROR:
1451 return -EBUSY;
1452 default:
1453 return -EIO;
1454 }
1455 }
Tejun Heo37210502007-03-21 00:07:18 +09001456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459static void sd_rescan(struct device *dev)
1460{
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01001461 struct scsi_disk *sdkp = dev_get_drvdata(dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001462
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01001463 revalidate_disk(sdkp->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466
1467#ifdef CONFIG_COMPAT
1468/*
1469 * This gets directly called from VFS. When the ioctl
1470 * is not recognized we go back to the other translation paths.
1471 */
Al Viro0338e292008-03-02 10:41:04 -05001472static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
1473 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Al Viro0338e292008-03-02 10:41:04 -05001475 struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
Christoph Hellwig21a9d4c2014-10-24 18:39:37 +02001476 int error;
Paolo Bonzini0bfc96c2012-01-12 16:01:28 +01001477
Christoph Hellwig21a9d4c2014-10-24 18:39:37 +02001478 error = scsi_ioctl_block_when_processing_errors(sdev, cmd,
1479 (mode & FMODE_NDELAY) != 0);
1480 if (error)
1481 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /*
1484 * Let the static ioctl translation table take care of it.
1485 */
Christoph Hellwig21a9d4c2014-10-24 18:39:37 +02001486 if (!sdev->host->hostt->compat_ioctl)
1487 return -ENOIOCTLCMD;
1488 return sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490#endif
1491
Christoph Hellwig924d55b2015-10-15 14:10:49 +02001492static char sd_pr_type(enum pr_type type)
1493{
1494 switch (type) {
1495 case PR_WRITE_EXCLUSIVE:
1496 return 0x01;
1497 case PR_EXCLUSIVE_ACCESS:
1498 return 0x03;
1499 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1500 return 0x05;
1501 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1502 return 0x06;
1503 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1504 return 0x07;
1505 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1506 return 0x08;
1507 default:
1508 return 0;
1509 }
1510};
1511
1512static int sd_pr_command(struct block_device *bdev, u8 sa,
1513 u64 key, u64 sa_key, u8 type, u8 flags)
1514{
1515 struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
1516 struct scsi_sense_hdr sshdr;
1517 int result;
1518 u8 cmd[16] = { 0, };
1519 u8 data[24] = { 0, };
1520
1521 cmd[0] = PERSISTENT_RESERVE_OUT;
1522 cmd[1] = sa;
1523 cmd[2] = type;
1524 put_unaligned_be32(sizeof(data), &cmd[5]);
1525
1526 put_unaligned_be64(key, &data[0]);
1527 put_unaligned_be64(sa_key, &data[8]);
1528 data[20] = flags;
1529
1530 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, &data, sizeof(data),
1531 &sshdr, SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1532
1533 if ((driver_byte(result) & DRIVER_SENSE) &&
1534 (scsi_sense_valid(&sshdr))) {
1535 sdev_printk(KERN_INFO, sdev, "PR command failed: %d\n", result);
1536 scsi_print_sense_hdr(sdev, NULL, &sshdr);
1537 }
1538
1539 return result;
1540}
1541
1542static int sd_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
1543 u32 flags)
1544{
1545 if (flags & ~PR_FL_IGNORE_KEY)
1546 return -EOPNOTSUPP;
1547 return sd_pr_command(bdev, (flags & PR_FL_IGNORE_KEY) ? 0x06 : 0x00,
1548 old_key, new_key, 0,
Christoph Hellwig01f90dd2016-07-08 21:23:50 +09001549 (1 << 0) /* APTPL */);
Christoph Hellwig924d55b2015-10-15 14:10:49 +02001550}
1551
1552static int sd_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
1553 u32 flags)
1554{
1555 if (flags)
1556 return -EOPNOTSUPP;
1557 return sd_pr_command(bdev, 0x01, key, 0, sd_pr_type(type), 0);
1558}
1559
1560static int sd_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1561{
1562 return sd_pr_command(bdev, 0x02, key, 0, sd_pr_type(type), 0);
1563}
1564
1565static int sd_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
1566 enum pr_type type, bool abort)
1567{
1568 return sd_pr_command(bdev, abort ? 0x05 : 0x04, old_key, new_key,
1569 sd_pr_type(type), 0);
1570}
1571
1572static int sd_pr_clear(struct block_device *bdev, u64 key)
1573{
1574 return sd_pr_command(bdev, 0x03, key, 0, 0, 0);
1575}
1576
1577static const struct pr_ops sd_pr_ops = {
1578 .pr_register = sd_pr_register,
1579 .pr_reserve = sd_pr_reserve,
1580 .pr_release = sd_pr_release,
1581 .pr_preempt = sd_pr_preempt,
1582 .pr_clear = sd_pr_clear,
1583};
1584
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001585static const struct block_device_operations sd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 .owner = THIS_MODULE,
Al Viro0338e292008-03-02 10:41:04 -05001587 .open = sd_open,
1588 .release = sd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001589 .ioctl = sd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001590 .getgeo = sd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591#ifdef CONFIG_COMPAT
Al Viro0338e292008-03-02 10:41:04 -05001592 .compat_ioctl = sd_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 .revalidate_disk = sd_revalidate_disk,
Tejun Heo72ec24b2010-05-15 20:09:32 +02001595 .unlock_native_capacity = sd_unlock_native_capacity,
Christoph Hellwig924d55b2015-10-15 14:10:49 +02001596 .pr_ops = &sd_pr_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597};
1598
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001599/**
1600 * sd_eh_action - error handling callback
1601 * @scmd: sd-issued command that has failed
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001602 * @eh_disp: The recovery disposition suggested by the midlayer
1603 *
James Bottomley24510792013-11-11 13:44:53 +01001604 * This function is called by the SCSI midlayer upon completion of an
1605 * error test command (currently TEST UNIT READY). The result of sending
1606 * the eh command is passed in eh_disp. We're looking for devices that
1607 * fail medium access commands but are OK with non access commands like
1608 * test unit ready (so wrongly see the device as having a successful
1609 * recovery)
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001610 **/
James Bottomley24510792013-11-11 13:44:53 +01001611static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp)
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001612{
1613 struct scsi_disk *sdkp = scsi_disk(scmd->request->rq_disk);
1614
1615 if (!scsi_device_online(scmd->device) ||
James Bottomley24510792013-11-11 13:44:53 +01001616 !scsi_medium_access_command(scmd) ||
1617 host_byte(scmd->result) != DID_TIME_OUT ||
1618 eh_disp != SUCCESS)
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001619 return eh_disp;
1620
1621 /*
1622 * The device has timed out executing a medium access command.
1623 * However, the TEST UNIT READY command sent during error
1624 * handling completed successfully. Either the device is in the
1625 * process of recovering or has it suffered an internal failure
1626 * that prevents access to the storage medium.
1627 */
James Bottomley24510792013-11-11 13:44:53 +01001628 sdkp->medium_access_timed_out++;
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001629
1630 /*
1631 * If the device keeps failing read/write commands but TEST UNIT
1632 * READY always completes successfully we assume that medium
1633 * access is no longer possible and take the device offline.
1634 */
1635 if (sdkp->medium_access_timed_out >= sdkp->max_medium_access_timeouts) {
1636 scmd_printk(KERN_ERR, scmd,
1637 "Medium access timeout failure. Offlining disk!\n");
1638 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
1639
1640 return FAILED;
1641 }
1642
1643 return eh_disp;
1644}
1645
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001646static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1647{
Tejun Heo83096eb2009-05-07 22:24:39 +09001648 u64 start_lba = blk_rq_pos(scmd->request);
1649 u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
Mark Hounschell74856fb2015-05-13 10:49:09 +02001650 u64 factor = scmd->device->sector_size / 512;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001651 u64 bad_lba;
1652 int info_valid;
James Bottomleya8733c72010-12-17 15:36:34 -05001653 /*
1654 * resid is optional but mostly filled in. When it's unused,
1655 * its value is zero, so we assume the whole buffer transferred
1656 */
1657 unsigned int transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd);
1658 unsigned int good_bytes;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001659
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001660 if (scmd->request->cmd_type != REQ_TYPE_FS)
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001661 return 0;
1662
1663 info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
1664 SCSI_SENSE_BUFFERSIZE,
1665 &bad_lba);
1666 if (!info_valid)
1667 return 0;
1668
1669 if (scsi_bufflen(scmd) <= scmd->device->sector_size)
1670 return 0;
1671
Mark Hounschell74856fb2015-05-13 10:49:09 +02001672 /* be careful ... don't want any overflows */
1673 do_div(start_lba, factor);
1674 do_div(end_lba, factor);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001675
1676 /* The bad lba was reported incorrectly, we have no idea where
1677 * the error is.
1678 */
1679 if (bad_lba < start_lba || bad_lba >= end_lba)
1680 return 0;
1681
1682 /* This computation should always be done in terms of
1683 * the resolution of the device's medium.
1684 */
James Bottomleya8733c72010-12-17 15:36:34 -05001685 good_bytes = (bad_lba - start_lba) * scmd->device->sector_size;
1686 return min(good_bytes, transferred);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001687}
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689/**
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001690 * sd_done - bottom half handler: called when the lower level
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 * driver has completed (successfully or otherwise) a scsi command.
1692 * @SCpnt: mid-level's per command structure.
1693 *
1694 * Note: potentially run from within an ISR. Must not block.
1695 **/
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001696static int sd_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 int result = SCpnt->result;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001699 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 struct scsi_sense_hdr sshdr;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001701 struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
Martin K. Petersen26e85fc2012-09-18 12:19:31 -04001702 struct request *req = SCpnt->request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 int sense_valid = 0;
1704 int sense_deferred = 0;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05001705 unsigned char op = SCpnt->cmnd[0];
Martin K. Petersen5db44862012-09-18 12:19:32 -04001706 unsigned char unmap = SCpnt->cmnd[1] & 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Mike Christiec2df40d2016-06-05 14:32:17 -05001708 if (req_op(req) == REQ_OP_DISCARD || req_op(req) == REQ_OP_WRITE_SAME) {
Martin K. Petersen26e85fc2012-09-18 12:19:31 -04001709 if (!result) {
1710 good_bytes = blk_rq_bytes(req);
1711 scsi_set_resid(SCpnt, 0);
1712 } else {
1713 good_bytes = 0;
1714 scsi_set_resid(SCpnt, blk_rq_bytes(req));
1715 }
1716 }
FUJITA Tomonori6a32a8a2010-07-21 10:29:37 +09001717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (result) {
1719 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
1720 if (sense_valid)
1721 sense_deferred = scsi_sense_is_deferred(&sshdr);
1722 }
David Jeffery2a863ba2014-04-10 11:08:30 -04001723 sdkp->medium_access_timed_out = 0;
1724
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001725 if (driver_byte(result) != DRIVER_SENSE &&
1726 (!sense_valid || sense_deferred))
1727 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001729 switch (sshdr.sense_key) {
1730 case HARDWARE_ERROR:
1731 case MEDIUM_ERROR:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001732 good_bytes = sd_completed_bytes(SCpnt);
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001733 break;
1734 case RECOVERED_ERROR:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001735 good_bytes = scsi_bufflen(SCpnt);
1736 break;
Jamie Wellnitz10dab222008-09-11 21:39:36 -04001737 case NO_SENSE:
1738 /* This indicates a false check condition, so ignore it. An
1739 * unknown amount of data was transferred so treat it as an
1740 * error.
1741 */
Jamie Wellnitz10dab222008-09-11 21:39:36 -04001742 SCpnt->result = 0;
1743 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1744 break;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05001745 case ABORTED_COMMAND:
1746 if (sshdr.asc == 0x10) /* DIF: Target detected corruption */
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001747 good_bytes = sd_completed_bytes(SCpnt);
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001748 break;
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05001749 case ILLEGAL_REQUEST:
1750 if (sshdr.asc == 0x10) /* DIX: Host detected corruption */
1751 good_bytes = sd_completed_bytes(SCpnt);
1752 /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
Martin K. Petersen5db44862012-09-18 12:19:32 -04001753 if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
1754 switch (op) {
1755 case UNMAP:
1756 sd_config_discard(sdkp, SD_LBP_DISABLE);
1757 break;
1758 case WRITE_SAME_16:
1759 case WRITE_SAME:
1760 if (unmap)
1761 sd_config_discard(sdkp, SD_LBP_DISABLE);
1762 else {
1763 sdkp->device->no_write_same = 1;
1764 sd_config_write_same(sdkp);
1765
1766 good_bytes = 0;
1767 req->__data_len = blk_rq_bytes(req);
1768 req->cmd_flags |= REQ_QUIET;
1769 }
1770 }
1771 }
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05001772 break;
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001773 default:
1774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001776 out:
Hannes Reineckeef613292014-10-24 14:27:00 +02001777 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
1778 "sd_done: completed %d of %d bytes\n",
1779 good_bytes, scsi_bufflen(SCpnt)));
1780
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001781 if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
1782 sd_dif_complete(SCpnt, good_bytes);
1783
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001784 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787/*
1788 * spinup disk - called only in sd_revalidate_disk()
1789 */
1790static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001791sd_spinup_disk(struct scsi_disk *sdkp)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001792{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 unsigned char cmd[10];
Alan Stern4451e472005-07-12 10:45:17 -04001794 unsigned long spintime_expire = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 int retries, spintime;
1796 unsigned int the_result;
1797 struct scsi_sense_hdr sshdr;
1798 int sense_valid = 0;
1799
1800 spintime = 0;
1801
1802 /* Spin up drives, as required. Only do this at boot time */
1803 /* Spinup needs to be done for module loads too. */
1804 do {
1805 retries = 0;
1806
1807 do {
1808 cmd[0] = TEST_UNIT_READY;
1809 memset((void *) &cmd[1], 0, 9);
1810
James Bottomleyea73a9f2005-08-28 11:33:52 -05001811 the_result = scsi_execute_req(sdkp->device, cmd,
1812 DMA_NONE, NULL, 0,
1813 &sshdr, SD_TIMEOUT,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09001814 SD_MAX_RETRIES, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Alan Sternb4d38e32006-10-11 16:48:28 -04001816 /*
1817 * If the drive has indicated to us that it
1818 * doesn't have any media in it, don't bother
1819 * with any more polling.
1820 */
1821 if (media_not_present(sdkp, &sshdr))
1822 return;
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (the_result)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001825 sense_valid = scsi_sense_valid(&sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 retries++;
1827 } while (retries < 3 &&
1828 (!scsi_status_is_good(the_result) ||
1829 ((driver_byte(the_result) & DRIVER_SENSE) &&
1830 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1833 /* no sense, TUR either succeeded or failed
1834 * with a status error */
Martin K. Petersene73aec82007-02-27 22:40:55 -05001835 if(!spintime && !scsi_status_is_good(the_result)) {
Hannes Reineckeef613292014-10-24 14:27:00 +02001836 sd_print_result(sdkp, "Test Unit Ready failed",
1837 the_result);
Martin K. Petersene73aec82007-02-27 22:40:55 -05001838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 break;
1840 }
Hannes Reineckeef613292014-10-24 14:27:00 +02001841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 /*
1843 * The device does not want the automatic start to be issued.
1844 */
Matthew Wilcox33dd6f92009-02-20 06:53:48 -07001845 if (sdkp->device->no_start_on_add)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Matthew Wilcox33dd6f92009-02-20 06:53:48 -07001848 if (sense_valid && sshdr.sense_key == NOT_READY) {
1849 if (sshdr.asc == 4 && sshdr.ascq == 3)
1850 break; /* manual intervention required */
1851 if (sshdr.asc == 4 && sshdr.ascq == 0xb)
1852 break; /* standby */
1853 if (sshdr.asc == 4 && sshdr.ascq == 0xc)
1854 break; /* unavailable */
1855 /*
1856 * Issue command to spin up drive when not ready
1857 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001859 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 cmd[0] = START_STOP;
1861 cmd[1] = 1; /* Return immediately */
1862 memset((void *) &cmd[2], 0, 8);
1863 cmd[4] = 1; /* Start spin cycle */
Stefan Richterd2886ea2008-05-11 00:34:07 +02001864 if (sdkp->device->start_stop_pwr_cond)
1865 cmd[4] |= 1 << 4;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001866 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1867 NULL, 0, &sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09001868 SD_TIMEOUT, SD_MAX_RETRIES,
1869 NULL);
Alan Stern4451e472005-07-12 10:45:17 -04001870 spintime_expire = jiffies + 100 * HZ;
1871 spintime = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /* Wait 1 second for next try */
1874 msleep(1000);
1875 printk(".");
Alan Stern4451e472005-07-12 10:45:17 -04001876
1877 /*
1878 * Wait for USB flash devices with slow firmware.
1879 * Yes, this sense key/ASC combination shouldn't
1880 * occur here. It's characteristic of these devices.
1881 */
1882 } else if (sense_valid &&
1883 sshdr.sense_key == UNIT_ATTENTION &&
1884 sshdr.asc == 0x28) {
1885 if (!spintime) {
1886 spintime_expire = jiffies + 5 * HZ;
1887 spintime = 1;
1888 }
1889 /* Wait 1 second for next try */
1890 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 } else {
1892 /* we don't understand the sense code, so it's
1893 * probably pointless to loop */
1894 if(!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001895 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1896 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898 break;
1899 }
1900
Alan Stern4451e472005-07-12 10:45:17 -04001901 } while (spintime && time_before_eq(jiffies, spintime_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 if (spintime) {
1904 if (scsi_status_is_good(the_result))
1905 printk("ready\n");
1906 else
1907 printk("not responding...\n");
1908 }
1909}
1910
Martin K. Petersene0597d72008-07-17 04:28:34 -04001911
1912/*
1913 * Determine whether disk supports Data Integrity Field.
1914 */
Martin K. Petersenfe542392012-09-21 12:44:12 -04001915static int sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
Martin K. Petersene0597d72008-07-17 04:28:34 -04001916{
1917 struct scsi_device *sdp = sdkp->device;
1918 u8 type;
Martin K. Petersenfe542392012-09-21 12:44:12 -04001919 int ret = 0;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001920
1921 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
Martin K. Petersenfe542392012-09-21 12:44:12 -04001922 return ret;
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -04001923
1924 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
1925
Christoph Hellwig8475c812016-09-11 19:35:41 +02001926 if (type > T10_PI_TYPE3_PROTECTION)
Martin K. Petersenfe542392012-09-21 12:44:12 -04001927 ret = -ENODEV;
1928 else if (scsi_host_dif_capable(sdp->host, type))
1929 ret = 1;
1930
1931 if (sdkp->first_scan || type != sdkp->protection_type)
1932 switch (ret) {
1933 case -ENODEV:
1934 sd_printk(KERN_ERR, sdkp, "formatted with unsupported" \
1935 " protection type %u. Disabling disk!\n",
1936 type);
1937 break;
1938 case 1:
1939 sd_printk(KERN_NOTICE, sdkp,
1940 "Enabling DIF Type %u protection\n", type);
1941 break;
1942 case 0:
1943 sd_printk(KERN_NOTICE, sdkp,
1944 "Disabling DIF Type %u protection\n", type);
1945 break;
1946 }
Martin K. Petersene0597d72008-07-17 04:28:34 -04001947
Martin K. Petersenbe922f42008-09-19 18:47:20 -04001948 sdkp->protection_type = type;
1949
Martin K. Petersenfe542392012-09-21 12:44:12 -04001950 return ret;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001951}
1952
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001953static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
1954 struct scsi_sense_hdr *sshdr, int sense_valid,
1955 int the_result)
1956{
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001957 if (driver_byte(the_result) & DRIVER_SENSE)
1958 sd_print_sense_hdr(sdkp, sshdr);
1959 else
1960 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
1961
1962 /*
1963 * Set dirty bit for removable devices if not ready -
1964 * sometimes drives will not report this properly.
1965 */
1966 if (sdp->removable &&
1967 sense_valid && sshdr->sense_key == NOT_READY)
Tejun Heo2bae0092010-12-18 18:42:23 +01001968 set_media_not_present(sdkp);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001969
1970 /*
1971 * We used to set media_present to 0 here to indicate no media
1972 * in the drive, but some drives fail read capacity even with
1973 * media present, so we can't do that.
1974 */
1975 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
1976}
1977
1978#define RC16_LEN 32
1979#if RC16_LEN > SD_BUF_SIZE
1980#error RC16_LEN must not be more than SD_BUF_SIZE
1981#endif
1982
James Bottomley3233ac12010-04-01 10:30:01 -04001983#define READ_CAPACITY_RETRIES_ON_RESET 10
1984
Martin K. Petersen08383b02017-04-04 10:42:30 -04001985/*
1986 * Ensure that we don't overflow sector_t when CONFIG_LBDAF is not set
1987 * and the reported logical block size is bigger than 512 bytes. Note
1988 * that last_sector is a u64 and therefore logical_to_sectors() is not
1989 * applicable.
1990 */
1991static bool sd_addressable_capacity(u64 lba, unsigned int sector_size)
1992{
1993 u64 last_sector = (lba + 1ULL) << (ilog2(sector_size) - 9);
1994
1995 if (sizeof(sector_t) == 4 && last_sector > U32_MAX)
1996 return false;
1997
1998 return true;
1999}
2000
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002001static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
2002 unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05002003{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 unsigned char cmd[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 struct scsi_sense_hdr sshdr;
2006 int sense_valid = 0;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002007 int the_result;
James Bottomley3233ac12010-04-01 10:30:01 -04002008 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04002009 unsigned int alignment;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002010 unsigned long long lba;
2011 unsigned sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Hans de Goede5ce524b2010-10-01 14:20:10 -07002013 if (sdp->no_read_capacity_16)
2014 return -EINVAL;
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 do {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002017 memset(cmd, 0, 16);
Hannes Reineckeeb846d92014-11-17 14:25:19 +01002018 cmd[0] = SERVICE_ACTION_IN_16;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002019 cmd[1] = SAI_READ_CAPACITY_16;
2020 cmd[13] = RC16_LEN;
2021 memset(buffer, 0, RC16_LEN);
2022
James Bottomleyea73a9f2005-08-28 11:33:52 -05002023 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002024 buffer, RC16_LEN, &sshdr,
2025 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
James Bottomleyea73a9f2005-08-28 11:33:52 -05002027 if (media_not_present(sdkp, &sshdr))
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002028 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Matthew Wilcox2b301302009-03-12 14:20:30 -04002030 if (the_result) {
James Bottomleyea73a9f2005-08-28 11:33:52 -05002031 sense_valid = scsi_sense_valid(&sshdr);
Matthew Wilcox2b301302009-03-12 14:20:30 -04002032 if (sense_valid &&
2033 sshdr.sense_key == ILLEGAL_REQUEST &&
2034 (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
2035 sshdr.ascq == 0x00)
2036 /* Invalid Command Operation Code or
2037 * Invalid Field in CDB, just retry
2038 * silently with RC10 */
2039 return -EINVAL;
James Bottomley3233ac12010-04-01 10:30:01 -04002040 if (sense_valid &&
2041 sshdr.sense_key == UNIT_ATTENTION &&
2042 sshdr.asc == 0x29 && sshdr.ascq == 0x00)
2043 /* Device reset might occur several times,
2044 * give it one more chance */
2045 if (--reset_retries > 0)
2046 continue;
Matthew Wilcox2b301302009-03-12 14:20:30 -04002047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 retries--;
2049
2050 } while (the_result && retries);
2051
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002052 if (the_result) {
Hannes Reineckeef613292014-10-24 14:27:00 +02002053 sd_print_result(sdkp, "Read Capacity(16) failed", the_result);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002054 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
2055 return -EINVAL;
2056 }
Martin K. Petersene73aec82007-02-27 22:40:55 -05002057
Dave Hansen8f76d152009-04-21 16:43:27 -07002058 sector_size = get_unaligned_be32(&buffer[8]);
2059 lba = get_unaligned_be64(&buffer[0]);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002060
Martin K. Petersenfe542392012-09-21 12:44:12 -04002061 if (sd_read_protection_type(sdkp, buffer) < 0) {
2062 sdkp->capacity = 0;
2063 return -ENODEV;
2064 }
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002065
Martin K. Petersen08383b02017-04-04 10:42:30 -04002066 if (!sd_addressable_capacity(lba, sector_size)) {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002067 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
2068 "kernel compiled with support for large block "
2069 "devices.\n");
2070 sdkp->capacity = 0;
2071 return -EOVERFLOW;
2072 }
2073
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04002074 /* Logical blocks per physical block exponent */
Martin K. Petersen526f7c72010-09-28 14:48:47 -04002075 sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size;
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04002076
2077 /* Lowest aligned logical block */
2078 alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
2079 blk_queue_alignment_offset(sdp->request_queue, alignment);
2080 if (alignment && sdkp->first_scan)
2081 sd_printk(KERN_NOTICE, sdkp,
2082 "physical block alignment offset: %u\n", alignment);
2083
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002084 if (buffer[14] & 0x80) { /* LBPME */
2085 sdkp->lbpme = 1;
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002086
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002087 if (buffer[14] & 0x40) /* LBPRZ */
2088 sdkp->lbprz = 1;
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002089
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002090 sd_config_discard(sdkp, SD_LBP_WS16);
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002091 }
2092
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002093 sdkp->capacity = lba + 1;
2094 return sector_size;
2095}
2096
2097static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
2098 unsigned char *buffer)
2099{
2100 unsigned char cmd[16];
2101 struct scsi_sense_hdr sshdr;
2102 int sense_valid = 0;
2103 int the_result;
James Bottomley3233ac12010-04-01 10:30:01 -04002104 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002105 sector_t lba;
2106 unsigned sector_size;
2107
2108 do {
2109 cmd[0] = READ_CAPACITY;
2110 memset(&cmd[1], 0, 9);
2111 memset(buffer, 0, 8);
2112
2113 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
2114 buffer, 8, &sshdr,
2115 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
2116
2117 if (media_not_present(sdkp, &sshdr))
2118 return -ENODEV;
2119
James Bottomley3233ac12010-04-01 10:30:01 -04002120 if (the_result) {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002121 sense_valid = scsi_sense_valid(&sshdr);
James Bottomley3233ac12010-04-01 10:30:01 -04002122 if (sense_valid &&
2123 sshdr.sense_key == UNIT_ATTENTION &&
2124 sshdr.asc == 0x29 && sshdr.ascq == 0x00)
2125 /* Device reset might occur several times,
2126 * give it one more chance */
2127 if (--reset_retries > 0)
2128 continue;
2129 }
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002130 retries--;
2131
2132 } while (the_result && retries);
2133
2134 if (the_result) {
Hannes Reineckeef613292014-10-24 14:27:00 +02002135 sd_print_result(sdkp, "Read Capacity(10) failed", the_result);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002136 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
2137 return -EINVAL;
2138 }
2139
Dave Hansen8f76d152009-04-21 16:43:27 -07002140 sector_size = get_unaligned_be32(&buffer[4]);
2141 lba = get_unaligned_be32(&buffer[0]);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002142
Hans de Goede5ce524b2010-10-01 14:20:10 -07002143 if (sdp->no_read_capacity_16 && (lba == 0xffffffff)) {
2144 /* Some buggy (usb cardreader) devices return an lba of
2145 0xffffffff when the want to report a size of 0 (with
2146 which they really mean no media is present) */
2147 sdkp->capacity = 0;
Linus Torvalds5cc10352010-10-22 20:30:48 -07002148 sdkp->physical_block_size = sector_size;
Hans de Goede5ce524b2010-10-01 14:20:10 -07002149 return sector_size;
2150 }
2151
Martin K. Petersen08383b02017-04-04 10:42:30 -04002152 if (!sd_addressable_capacity(lba, sector_size)) {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002153 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
2154 "kernel compiled with support for large block "
2155 "devices.\n");
2156 sdkp->capacity = 0;
2157 return -EOVERFLOW;
2158 }
2159
2160 sdkp->capacity = lba + 1;
Martin K. Petersen526f7c72010-09-28 14:48:47 -04002161 sdkp->physical_block_size = sector_size;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002162 return sector_size;
2163}
2164
Matthew Wilcox2b301302009-03-12 14:20:30 -04002165static int sd_try_rc16_first(struct scsi_device *sdp)
2166{
Hannes Reineckef87146b2010-03-29 09:29:24 +02002167 if (sdp->host->max_cmd_len < 16)
2168 return 0;
Alan Stern6a0bdff2012-06-20 16:04:19 -04002169 if (sdp->try_rc_10_first)
2170 return 0;
Matthew Wilcox2b301302009-03-12 14:20:30 -04002171 if (sdp->scsi_level > SCSI_SPC_2)
2172 return 1;
2173 if (scsi_device_protection(sdp))
2174 return 1;
2175 return 0;
2176}
2177
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002178/*
2179 * read disk capacity
2180 */
2181static void
2182sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
2183{
2184 int sector_size;
2185 struct scsi_device *sdp = sdkp->device;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002186 sector_t old_capacity = sdkp->capacity;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002187
Matthew Wilcox2b301302009-03-12 14:20:30 -04002188 if (sd_try_rc16_first(sdp)) {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002189 sector_size = read_capacity_16(sdkp, sdp, buffer);
2190 if (sector_size == -EOVERFLOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 goto got_data;
Matthew Wilcox2b301302009-03-12 14:20:30 -04002192 if (sector_size == -ENODEV)
2193 return;
2194 if (sector_size < 0)
2195 sector_size = read_capacity_10(sdkp, sdp, buffer);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002196 if (sector_size < 0)
2197 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 } else {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04002199 sector_size = read_capacity_10(sdkp, sdp, buffer);
2200 if (sector_size == -EOVERFLOW)
2201 goto got_data;
2202 if (sector_size < 0)
2203 return;
2204 if ((sizeof(sdkp->capacity) > 4) &&
2205 (sdkp->capacity > 0xffffffffULL)) {
2206 int old_sector_size = sector_size;
2207 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
2208 "Trying to use READ CAPACITY(16).\n");
2209 sector_size = read_capacity_16(sdkp, sdp, buffer);
2210 if (sector_size < 0) {
2211 sd_printk(KERN_NOTICE, sdkp,
2212 "Using 0xffffffff as device size\n");
2213 sdkp->capacity = 1 + (sector_t) 0xffffffff;
2214 sector_size = old_sector_size;
2215 goto got_data;
2216 }
2217 }
2218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Alan Stern5c211ca2009-02-18 10:54:44 -05002220 /* Some devices are known to return the total number of blocks,
2221 * not the highest block number. Some devices have versions
2222 * which do this and others which do not. Some devices we might
2223 * suspect of doing this but we don't know for certain.
2224 *
2225 * If we know the reported capacity is wrong, decrement it. If
2226 * we can only guess, then assume the number of blocks is even
2227 * (usually true but not always) and err on the side of lowering
2228 * the capacity.
2229 */
2230 if (sdp->fix_capacity ||
2231 (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
2232 sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
2233 "from its reported value: %llu\n",
2234 (unsigned long long) sdkp->capacity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 --sdkp->capacity;
Oliver Neukum61bf54b2007-02-08 09:04:48 +01002236 }
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238got_data:
2239 if (sector_size == 0) {
2240 sector_size = 512;
Martin K. Petersene73aec82007-02-27 22:40:55 -05002241 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
2242 "assuming 512.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
2244
2245 if (sector_size != 512 &&
2246 sector_size != 1024 &&
2247 sector_size != 2048 &&
Mark Hounschell74856fb2015-05-13 10:49:09 +02002248 sector_size != 4096) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002249 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
2250 sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 /*
2252 * The user might want to re-format the drive with
2253 * a supported sectorsize. Once this happens, it
2254 * would be relatively trivial to set the thing up.
2255 * For this reason, we leave the thing in the table.
2256 */
2257 sdkp->capacity = 0;
2258 /*
2259 * set a bogus sector size so the normal read/write
2260 * logic in the block layer will eventually refuse any
2261 * request on this device without tripping over power
2262 * of two sector size assumptions
2263 */
2264 sector_size = 512;
2265 }
Martin K. Petersene1defc42009-05-22 17:17:49 -04002266 blk_queue_logical_block_size(sdp->request_queue, sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
James Bottomley7404ad3b2008-08-31 10:41:52 -05002268 {
2269 char cap_str_2[10], cap_str_10[10];
James Bottomley7404ad3b2008-08-31 10:41:52 -05002270
James Bottomleyb9f28d82015-03-05 18:47:01 -08002271 string_get_size(sdkp->capacity, sector_size,
2272 STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
2273 string_get_size(sdkp->capacity, sector_size,
2274 STRING_UNITS_10, cap_str_10,
James Bottomley7404ad3b2008-08-31 10:41:52 -05002275 sizeof(cap_str_10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04002277 if (sdkp->first_scan || old_capacity != sdkp->capacity) {
Martin K. Petersen526f7c72010-09-28 14:48:47 -04002278 if (sdkp->physical_block_size != sector_size)
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04002279 sd_printk(KERN_NOTICE, sdkp,
2280 "%u-byte physical blocks\n",
Martin K. Petersen526f7c72010-09-28 14:48:47 -04002281 sdkp->physical_block_size);
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04002282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284
Martin K. Petersenca369d52015-11-13 16:46:48 -05002285 if (sdkp->capacity > 0xffffffff)
Martin K. Petersenbcdb2472014-06-03 18:45:51 -04002286 sdp->use_16_for_rw = 1;
Jason J. Herne53ad5702012-11-14 17:03:22 -05002287
Martin K. Petersen526f7c72010-09-28 14:48:47 -04002288 blk_queue_physical_block_size(sdp->request_queue,
2289 sdkp->physical_block_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 sdkp->device->sector_size = sector_size;
2291}
2292
2293/* called with buffer of length 512 */
2294static inline int
James Bottomleyea73a9f2005-08-28 11:33:52 -05002295sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
2296 unsigned char *buffer, int len, struct scsi_mode_data *data,
2297 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
James Bottomleyea73a9f2005-08-28 11:33:52 -05002299 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
James Bottomley1cf72692005-08-28 11:27:01 -05002300 SD_TIMEOUT, SD_MAX_RETRIES, data,
James Bottomleyea73a9f2005-08-28 11:33:52 -05002301 sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
2304/*
2305 * read write protect setting, if possible - called only in sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06002306 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 */
2308static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05002309sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05002310{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 int res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05002312 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 struct scsi_mode_data data;
2314
2315 set_disk_ro(sdkp->disk, 0);
James Bottomleyea73a9f2005-08-28 11:33:52 -05002316 if (sdp->skip_ms_page_3f) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002317 sd_first_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return;
2319 }
2320
James Bottomleyea73a9f2005-08-28 11:33:52 -05002321 if (sdp->use_192_bytes_for_3f) {
2322 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 } else {
2324 /*
2325 * First attempt: ask for all pages (0x3F), but only 4 bytes.
2326 * We have to start carefully: some devices hang if we ask
2327 * for more than is available.
2328 */
James Bottomleyea73a9f2005-08-28 11:33:52 -05002329 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 /*
2332 * Second attempt: ask for page 0 When only page 0 is
2333 * implemented, a request for page 3F may return Sense Key
2334 * 5: Illegal Request, Sense Code 24: Invalid field in
2335 * CDB.
2336 */
2337 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05002338 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 /*
2341 * Third attempt: ask 255 bytes, as we did earlier.
2342 */
2343 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05002344 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
2345 &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347
2348 if (!scsi_status_is_good(res)) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002349 sd_first_printk(KERN_WARNING, sdkp,
Martin K. Petersene73aec82007-02-27 22:40:55 -05002350 "Test WP failed, assume Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 } else {
2352 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
2353 set_disk_ro(sdkp->disk, sdkp->write_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355}
2356
2357/*
2358 * sd_read_cache_type - called only from sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06002359 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 */
2361static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05002362sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
Al Viro 631e8a12005-05-16 01:59:55 +01002363{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 int len = 0, res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05002365 struct scsi_device *sdp = sdkp->device;
Subhash Jadavani57d6b142016-07-29 16:50:07 -07002366 struct Scsi_Host *host = sdp->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Al Viro 631e8a12005-05-16 01:59:55 +01002368 int dbd;
2369 int modepage;
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002370 int first_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 struct scsi_mode_data data;
2372 struct scsi_sense_hdr sshdr;
James Bottomley39c60a02013-04-24 14:02:53 -07002373
2374 if (sdkp->cache_override)
2375 return;
2376
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002377 first_len = 4;
2378 if (sdp->skip_ms_page_8) {
2379 if (sdp->type == TYPE_RBC)
2380 goto defaults;
2381 else {
2382 if (sdp->skip_ms_page_3f)
2383 goto defaults;
2384 modepage = 0x3F;
2385 if (sdp->use_192_bytes_for_3f)
2386 first_len = 192;
2387 dbd = 0;
2388 }
2389 } else if (sdp->type == TYPE_RBC) {
Al Viro 631e8a12005-05-16 01:59:55 +01002390 modepage = 6;
2391 dbd = 8;
2392 } else {
2393 modepage = 8;
Subhash Jadavani57d6b142016-07-29 16:50:07 -07002394 if (host->set_dbd_for_caching)
2395 dbd = 8;
2396 else
2397 dbd = 0;
Al Viro 631e8a12005-05-16 01:59:55 +01002398 }
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 /* cautiously ask */
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002401 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, first_len,
2402 &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 if (!scsi_status_is_good(res))
2405 goto bad_sense;
2406
Al Viro6d73c852006-02-23 02:03:16 +01002407 if (!data.header_length) {
2408 modepage = 6;
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002409 first_len = 0;
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002410 sd_first_printk(KERN_ERR, sdkp,
2411 "Missing header in MODE_SENSE response\n");
Al Viro6d73c852006-02-23 02:03:16 +01002412 }
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 /* that went OK, now ask for the proper length */
2415 len = data.length;
2416
2417 /*
2418 * We're only interested in the first three bytes, actually.
2419 * But the data cache page is defined for the first 20.
2420 */
2421 if (len < 3)
2422 goto bad_sense;
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002423 else if (len > SD_BUF_SIZE) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002424 sd_first_printk(KERN_NOTICE, sdkp, "Truncating mode parameter "
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002425 "data from %d to %d bytes\n", len, SD_BUF_SIZE);
2426 len = SD_BUF_SIZE;
2427 }
2428 if (modepage == 0x3F && sdp->use_192_bytes_for_3f)
2429 len = 192;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431 /* Get the data */
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002432 if (len > first_len)
2433 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len,
2434 &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 if (scsi_status_is_good(res)) {
Al Viro 631e8a12005-05-16 01:59:55 +01002437 int offset = data.header_length + data.block_descriptor_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002439 while (offset < len) {
2440 u8 page_code = buffer[offset] & 0x3F;
2441 u8 spf = buffer[offset] & 0x40;
2442
2443 if (page_code == 8 || page_code == 6) {
2444 /* We're interested only in the first 3 bytes.
2445 */
2446 if (len - offset <= 2) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002447 sd_first_printk(KERN_ERR, sdkp,
2448 "Incomplete mode parameter "
2449 "data\n");
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002450 goto defaults;
2451 } else {
2452 modepage = page_code;
2453 goto Page_found;
2454 }
2455 } else {
2456 /* Go to the next page */
2457 if (spf && len - offset > 3)
2458 offset += 4 + (buffer[offset+2] << 8) +
2459 buffer[offset+3];
2460 else if (!spf && len - offset > 1)
2461 offset += 2 + buffer[offset+1];
2462 else {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002463 sd_first_printk(KERN_ERR, sdkp,
2464 "Incomplete mode "
2465 "parameter data\n");
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002466 goto defaults;
2467 }
2468 }
Al Viro48970802006-02-26 08:34:10 -06002469 }
2470
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002471 sd_first_printk(KERN_ERR, sdkp, "No Caching mode page found\n");
Alan Stern984f1732013-09-06 11:49:51 -04002472 goto defaults;
2473
Luben Tuikov0bcaa112011-05-19 00:00:58 -07002474 Page_found:
Al Viro 631e8a12005-05-16 01:59:55 +01002475 if (modepage == 8) {
2476 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
2477 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
2478 } else {
2479 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
2480 sdkp->RCD = 0;
2481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Tejun Heo007365a2006-01-06 09:53:52 +01002483 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
Alan Sternb14bf2d2014-06-30 11:04:21 -04002484 if (sdp->broken_fua) {
2485 sd_first_printk(KERN_NOTICE, sdkp, "Disabling FUA\n");
2486 sdkp->DPOFUA = 0;
Damien Le Moalee4494c2017-01-12 15:25:10 +09002487 } else if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw &&
2488 !sdkp->device->use_16_for_rw) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002489 sd_first_printk(KERN_NOTICE, sdkp,
Martin K. Petersene73aec82007-02-27 22:40:55 -05002490 "Uses READ/WRITE(6), disabling FUA\n");
Tejun Heo007365a2006-01-06 09:53:52 +01002491 sdkp->DPOFUA = 0;
2492 }
2493
Sujit Reddy Thumma2eefd572014-08-11 15:40:37 +03002494 /* No cache flush allowed for write protected devices */
2495 if (sdkp->WCE && sdkp->write_prot)
2496 sdkp->WCE = 0;
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return;
2499 }
2500
2501bad_sense:
James Bottomleyea73a9f2005-08-28 11:33:52 -05002502 if (scsi_sense_valid(&sshdr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 sshdr.sense_key == ILLEGAL_REQUEST &&
2504 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
Martin K. Petersene73aec82007-02-27 22:40:55 -05002505 /* Invalid field in CDB */
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002506 sd_first_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 else
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002508 sd_first_printk(KERN_ERR, sdkp,
2509 "Asking for cache data failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511defaults:
Namjae Jeonb81478d2012-07-07 23:05:08 -04002512 if (sdp->wce_default_on) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002513 sd_first_printk(KERN_NOTICE, sdkp,
2514 "Assuming drive cache: write back\n");
Namjae Jeonb81478d2012-07-07 23:05:08 -04002515 sdkp->WCE = 1;
2516 } else {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002517 sd_first_printk(KERN_ERR, sdkp,
2518 "Assuming drive cache: write through\n");
Namjae Jeonb81478d2012-07-07 23:05:08 -04002519 sdkp->WCE = 0;
2520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 sdkp->RCD = 0;
Al Viro48970802006-02-26 08:34:10 -06002522 sdkp->DPOFUA = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
Martin K. Petersene0597d72008-07-17 04:28:34 -04002525/*
2526 * The ATO bit indicates whether the DIF application tag is available
2527 * for use by the operating system.
2528 */
H Hartley Sweeten439d77f2010-08-10 18:01:20 -07002529static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
Martin K. Petersene0597d72008-07-17 04:28:34 -04002530{
2531 int res, offset;
2532 struct scsi_device *sdp = sdkp->device;
2533 struct scsi_mode_data data;
2534 struct scsi_sense_hdr sshdr;
2535
2536 if (sdp->type != TYPE_DISK)
2537 return;
2538
2539 if (sdkp->protection_type == 0)
2540 return;
2541
2542 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
2543 SD_MAX_RETRIES, &data, &sshdr);
2544
2545 if (!scsi_status_is_good(res) || !data.header_length ||
2546 data.length < 6) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002547 sd_first_printk(KERN_WARNING, sdkp,
Martin K. Petersene0597d72008-07-17 04:28:34 -04002548 "getting Control mode page failed, assume no ATO\n");
2549
2550 if (scsi_sense_valid(&sshdr))
2551 sd_print_sense_hdr(sdkp, &sshdr);
2552
2553 return;
2554 }
2555
2556 offset = data.header_length + data.block_descriptor_length;
2557
2558 if ((buffer[offset] & 0x3f) != 0x0a) {
Martin K. Petersenb2bff6c2014-01-03 18:19:26 -05002559 sd_first_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
Martin K. Petersene0597d72008-07-17 04:28:34 -04002560 return;
2561 }
2562
2563 if ((buffer[offset + 5] & 0x80) == 0)
2564 return;
2565
2566 sdkp->ATO = 1;
2567
2568 return;
2569}
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571/**
Martin K. Petersend11b6912009-05-23 11:43:39 -04002572 * sd_read_block_limits - Query disk device for preferred I/O sizes.
2573 * @disk: disk to query
2574 */
2575static void sd_read_block_limits(struct scsi_disk *sdkp)
2576{
2577 unsigned int sector_sz = sdkp->device->sector_size;
Martin K. Petersenbb2d3de2010-03-02 08:44:34 -05002578 const int vpd_len = 64;
James Bottomleye3deec02009-11-03 12:33:07 -06002579 unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
Martin K. Petersend11b6912009-05-23 11:43:39 -04002580
James Bottomleye3deec02009-11-03 12:33:07 -06002581 if (!buffer ||
2582 /* Block Limits VPD */
2583 scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
2584 goto out;
Martin K. Petersend11b6912009-05-23 11:43:39 -04002585
2586 blk_queue_io_min(sdkp->disk->queue,
2587 get_unaligned_be16(&buffer[6]) * sector_sz);
Martin K. Petersenca369d52015-11-13 16:46:48 -05002588
2589 sdkp->max_xfer_blocks = get_unaligned_be32(&buffer[8]);
2590 sdkp->opt_xfer_blocks = get_unaligned_be32(&buffer[12]);
Martin K. Petersend11b6912009-05-23 11:43:39 -04002591
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002592 if (buffer[3] == 0x3c) {
2593 unsigned int lba_count, desc_count;
2594
Martin K. Petersen5db44862012-09-18 12:19:32 -04002595 sdkp->max_ws_blocks = (u32)get_unaligned_be64(&buffer[36]);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002596
2597 if (!sdkp->lbpme)
2598 goto out;
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002599
2600 lba_count = get_unaligned_be32(&buffer[20]);
2601 desc_count = get_unaligned_be32(&buffer[24]);
2602
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002603 if (lba_count && desc_count)
2604 sdkp->max_unmap_blocks = lba_count;
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002605
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002606 sdkp->unmap_granularity = get_unaligned_be32(&buffer[28]);
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002607
2608 if (buffer[32] & 0x80)
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002609 sdkp->unmap_alignment =
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002610 get_unaligned_be32(&buffer[32]) & ~(1 << 31);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002611
2612 if (!sdkp->lbpvpd) { /* LBP VPD page not provided */
2613
2614 if (sdkp->max_unmap_blocks)
2615 sd_config_discard(sdkp, SD_LBP_UNMAP);
2616 else
2617 sd_config_discard(sdkp, SD_LBP_WS16);
2618
2619 } else { /* LBP VPD page tells us what to use */
Martin K. Petersene4613382014-12-05 08:58:03 -05002620 if (sdkp->lbpu && sdkp->max_unmap_blocks && !sdkp->lbprz)
2621 sd_config_discard(sdkp, SD_LBP_UNMAP);
2622 else if (sdkp->lbpws)
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002623 sd_config_discard(sdkp, SD_LBP_WS16);
2624 else if (sdkp->lbpws10)
2625 sd_config_discard(sdkp, SD_LBP_WS10);
Martin K. Petersen79850902014-11-07 00:08:13 -05002626 else if (sdkp->lbpu && sdkp->max_unmap_blocks)
2627 sd_config_discard(sdkp, SD_LBP_UNMAP);
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002628 else
2629 sd_config_discard(sdkp, SD_LBP_DISABLE);
2630 }
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002631 }
2632
James Bottomleye3deec02009-11-03 12:33:07 -06002633 out:
Martin K. Petersend11b6912009-05-23 11:43:39 -04002634 kfree(buffer);
2635}
2636
2637/**
Martin K. Petersen3821d762009-05-23 11:43:38 -04002638 * sd_read_block_characteristics - Query block dev. characteristics
2639 * @disk: disk to query
2640 */
2641static void sd_read_block_characteristics(struct scsi_disk *sdkp)
2642{
James Bottomleye3deec02009-11-03 12:33:07 -06002643 unsigned char *buffer;
Martin K. Petersen3821d762009-05-23 11:43:38 -04002644 u16 rot;
Martin K. Petersenbb2d3de2010-03-02 08:44:34 -05002645 const int vpd_len = 64;
Martin K. Petersen3821d762009-05-23 11:43:38 -04002646
James Bottomleye3deec02009-11-03 12:33:07 -06002647 buffer = kmalloc(vpd_len, GFP_KERNEL);
Martin K. Petersen3821d762009-05-23 11:43:38 -04002648
James Bottomleye3deec02009-11-03 12:33:07 -06002649 if (!buffer ||
2650 /* Block Device Characteristics VPD */
2651 scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len))
2652 goto out;
Martin K. Petersen3821d762009-05-23 11:43:38 -04002653
2654 rot = get_unaligned_be16(&buffer[4]);
2655
Mike Snitzerb277da02014-10-04 10:55:32 -06002656 if (rot == 1) {
Martin K. Petersen3821d762009-05-23 11:43:38 -04002657 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -06002658 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, sdkp->disk->queue);
2659 }
Martin K. Petersen3821d762009-05-23 11:43:38 -04002660
James Bottomleye3deec02009-11-03 12:33:07 -06002661 out:
Martin K. Petersen3821d762009-05-23 11:43:38 -04002662 kfree(buffer);
2663}
2664
Martin K. Petersen045d3fe2010-09-10 01:22:07 -04002665/**
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002666 * sd_read_block_provisioning - Query provisioning VPD page
Martin K. Petersen045d3fe2010-09-10 01:22:07 -04002667 * @disk: disk to query
2668 */
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002669static void sd_read_block_provisioning(struct scsi_disk *sdkp)
Martin K. Petersen045d3fe2010-09-10 01:22:07 -04002670{
2671 unsigned char *buffer;
2672 const int vpd_len = 8;
2673
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002674 if (sdkp->lbpme == 0)
Martin K. Petersen045d3fe2010-09-10 01:22:07 -04002675 return;
2676
2677 buffer = kmalloc(vpd_len, GFP_KERNEL);
2678
2679 if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb2, buffer, vpd_len))
2680 goto out;
2681
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002682 sdkp->lbpvpd = 1;
2683 sdkp->lbpu = (buffer[5] >> 7) & 1; /* UNMAP */
2684 sdkp->lbpws = (buffer[5] >> 6) & 1; /* WRITE SAME(16) with UNMAP */
2685 sdkp->lbpws10 = (buffer[5] >> 5) & 1; /* WRITE SAME(10) with UNMAP */
Martin K. Petersen045d3fe2010-09-10 01:22:07 -04002686
2687 out:
2688 kfree(buffer);
2689}
2690
Martin K. Petersen5db44862012-09-18 12:19:32 -04002691static void sd_read_write_same(struct scsi_disk *sdkp, unsigned char *buffer)
2692{
Martin K. Petersen66c28f92013-06-06 22:15:55 -04002693 struct scsi_device *sdev = sdkp->device;
2694
Martin K. Petersen54b2b502013-10-23 06:25:40 -04002695 if (sdev->host->no_write_same) {
2696 sdev->no_write_same = 1;
2697
2698 return;
2699 }
2700
Martin K. Petersen66c28f92013-06-06 22:15:55 -04002701 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, INQUIRY) < 0) {
Bernd Schubertaf736232013-09-23 14:47:32 +02002702 /* too large values might cause issues with arcmsr */
2703 int vpd_buf_len = 64;
2704
Martin K. Petersen66c28f92013-06-06 22:15:55 -04002705 sdev->no_report_opcodes = 1;
2706
2707 /* Disable WRITE SAME if REPORT SUPPORTED OPERATION
2708 * CODES is unsupported and the device has an ATA
2709 * Information VPD page (SAT).
2710 */
Bernd Schubertaf736232013-09-23 14:47:32 +02002711 if (!scsi_get_vpd_page(sdev, 0x89, buffer, vpd_buf_len))
Martin K. Petersen66c28f92013-06-06 22:15:55 -04002712 sdev->no_write_same = 1;
2713 }
2714
2715 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME_16) == 1)
Martin K. Petersen5db44862012-09-18 12:19:32 -04002716 sdkp->ws16 = 1;
Martin K. Petersen66c28f92013-06-06 22:15:55 -04002717
2718 if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME) == 1)
2719 sdkp->ws10 = 1;
Martin K. Petersen5db44862012-09-18 12:19:32 -04002720}
2721
Martin K. Petersen3821d762009-05-23 11:43:38 -04002722/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 * sd_revalidate_disk - called the first time a new disk is seen,
2724 * performs disk spin up, read_capacity, etc.
2725 * @disk: struct gendisk we care about
2726 **/
2727static int sd_revalidate_disk(struct gendisk *disk)
2728{
2729 struct scsi_disk *sdkp = scsi_disk(disk);
2730 struct scsi_device *sdp = sdkp->device;
Martin K. Petersenca369d52015-11-13 16:46:48 -05002731 struct request_queue *q = sdkp->disk->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 unsigned char *buffer;
Martin K. Petersenca369d52015-11-13 16:46:48 -05002733 unsigned int dev_max, rw_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05002735 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
2736 "sd_revalidate_disk\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 /*
2739 * If the device is offline, don't try and read capacity or any
2740 * of the other niceties.
2741 */
2742 if (!scsi_device_online(sdp))
2743 goto out;
2744
Bernhard Wallea6123f12007-05-21 17:15:26 +02002745 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 if (!buffer) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002747 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
2748 "allocation failure.\n");
James Bottomleyea73a9f2005-08-28 11:33:52 -05002749 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
2751
Martin K. Petersene73aec82007-02-27 22:40:55 -05002752 sd_spinup_disk(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 /*
2755 * Without media there is no reason to ask; moreover, some devices
2756 * react badly if we do.
2757 */
2758 if (sdkp->media_present) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002759 sd_read_capacity(sdkp, buffer);
Martin K. Petersenffd4bc22009-07-29 14:06:53 -04002760
Hannes Reinecke5ddfe082016-04-01 08:57:36 +02002761 if (scsi_device_supports_vpd(sdp)) {
Martin K. Petersenc98a0eb2011-03-08 02:07:15 -05002762 sd_read_block_provisioning(sdkp);
Martin K. Petersenffd4bc22009-07-29 14:06:53 -04002763 sd_read_block_limits(sdkp);
2764 sd_read_block_characteristics(sdkp);
2765 }
2766
Martin K. Petersene73aec82007-02-27 22:40:55 -05002767 sd_read_write_protect_flag(sdkp, buffer);
2768 sd_read_cache_type(sdkp, buffer);
Martin K. Petersene0597d72008-07-17 04:28:34 -04002769 sd_read_app_tag_own(sdkp, buffer);
Martin K. Petersen5db44862012-09-18 12:19:32 -04002770 sd_read_write_same(sdkp, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
Tejun Heo461d4e92006-01-06 09:52:55 +01002772
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002773 sdkp->first_scan = 0;
2774
Tejun Heo461d4e92006-01-06 09:52:55 +01002775 /*
2776 * We now have all cache related info, determine how we deal
Tejun Heo4913efe2010-09-03 11:56:16 +02002777 * with flush requests.
Tejun Heo461d4e92006-01-06 09:52:55 +01002778 */
Vaughan Caocb2fb682014-06-03 17:37:30 +08002779 sd_set_flush_flag(sdkp);
Tejun Heo461d4e92006-01-06 09:52:55 +01002780
Martin K. Petersenca369d52015-11-13 16:46:48 -05002781 /* Initial block count limit based on CDB TRANSFER LENGTH field size. */
2782 dev_max = sdp->use_16_for_rw ? SD_MAX_XFER_BLOCKS : SD_DEF_XFER_BLOCKS;
Brian King3a9794d2015-01-29 15:54:40 -06002783
Martin K. Petersenca369d52015-11-13 16:46:48 -05002784 /* Some devices report a maximum block count for READ/WRITE requests. */
2785 dev_max = min_not_zero(dev_max, sdkp->max_xfer_blocks);
2786 q->limits.max_dev_sectors = logical_to_sectors(sdp, dev_max);
2787
2788 /*
2789 * Use the device's preferred I/O size for reads and writes
Martin K. Petersen9c1d9c22015-12-16 17:53:52 -05002790 * unless the reported value is unreasonably small, large, or
2791 * garbage.
Martin K. Petersenca369d52015-11-13 16:46:48 -05002792 */
Martin K. Petersen9c1d9c22015-12-16 17:53:52 -05002793 if (sdkp->opt_xfer_blocks &&
2794 sdkp->opt_xfer_blocks <= dev_max &&
2795 sdkp->opt_xfer_blocks <= SD_DEF_XFER_BLOCKS &&
Sayali Lokhandec60b0502017-04-06 09:56:52 +05302796 sdkp->opt_xfer_blocks * sdp->sector_size >= PAGE_SIZE)
2797 rw_max = q->limits.io_opt =
2798 sdkp->opt_xfer_blocks * sdp->sector_size;
2799 else
Fam Zheng82d181d2017-03-28 12:41:26 +08002800 rw_max = min_not_zero(logical_to_sectors(sdp, dev_max),
2801 (sector_t)BLK_DEF_MAX_SECTORS);
Martin K. Petersenca369d52015-11-13 16:46:48 -05002802
2803 /* Combine with controller limits */
2804 q->limits.max_sectors = min(rw_max, queue_max_hw_sectors(q));
Martin K. Petersen4f258a42015-06-23 12:13:59 -04002805
Martin K. Petersenf08bb1e2016-03-28 21:18:56 -04002806 set_capacity(disk, logical_to_sectors(sdp, sdkp->capacity));
Martin K. Petersen5db44862012-09-18 12:19:32 -04002807 sd_config_write_same(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 kfree(buffer);
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 out:
2811 return 0;
2812}
2813
2814/**
Tejun Heo72ec24b2010-05-15 20:09:32 +02002815 * sd_unlock_native_capacity - unlock native capacity
2816 * @disk: struct gendisk to set capacity for
2817 *
2818 * Block layer calls this function if it detects that partitions
2819 * on @disk reach beyond the end of the device. If the SCSI host
2820 * implements ->unlock_native_capacity() method, it's invoked to
2821 * give it a chance to adjust the device capacity.
2822 *
2823 * CONTEXT:
2824 * Defined by block layer. Might sleep.
2825 */
2826static void sd_unlock_native_capacity(struct gendisk *disk)
2827{
2828 struct scsi_device *sdev = scsi_disk(disk)->device;
2829
2830 if (sdev->host->hostt->unlock_native_capacity)
2831 sdev->host->hostt->unlock_native_capacity(sdev);
2832}
2833
2834/**
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002835 * sd_format_disk_name - format disk name
2836 * @prefix: name prefix - ie. "sd" for SCSI disks
2837 * @index: index of the disk to format name for
2838 * @buf: output buffer
2839 * @buflen: length of the output buffer
2840 *
2841 * SCSI disk names starts at sda. The 26th device is sdz and the
2842 * 27th is sdaa. The last one for two lettered suffix is sdzz
2843 * which is followed by sdaaa.
2844 *
2845 * This is basically 26 base counting with one extra 'nil' entry
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08002846 * at the beginning from the second digit on and can be
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002847 * determined using similar method as 26 base conversion with the
2848 * index shifted -1 after each digit is computed.
2849 *
2850 * CONTEXT:
2851 * Don't care.
2852 *
2853 * RETURNS:
2854 * 0 on success, -errno on failure.
2855 */
2856static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
2857{
2858 const int base = 'z' - 'a' + 1;
2859 char *begin = buf + strlen(prefix);
2860 char *end = buf + buflen;
2861 char *p;
2862 int unit;
2863
2864 p = end - 1;
2865 *p = '\0';
2866 unit = base;
2867 do {
2868 if (p == begin)
2869 return -EINVAL;
2870 *--p = 'a' + (index % unit);
2871 index = (index / unit) - 1;
2872 } while (index >= 0);
2873
2874 memmove(begin, p, end - p);
2875 memcpy(buf, prefix, strlen(prefix));
2876
2877 return 0;
2878}
2879
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002880/*
2881 * The asynchronous part of sd_probe
2882 */
2883static void sd_probe_async(void *data, async_cookie_t cookie)
2884{
2885 struct scsi_disk *sdkp = data;
2886 struct scsi_device *sdp;
2887 struct gendisk *gd;
2888 u32 index;
2889 struct device *dev;
2890
2891 sdp = sdkp->device;
2892 gd = sdkp->disk;
2893 index = sdkp->index;
2894 dev = &sdp->sdev_gendev;
2895
Michael Reed1a03ae02010-09-20 11:20:22 -05002896 gd->major = sd_major((index & 0xf0) >> 4);
2897 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
2898 gd->minors = SD_MINORS;
2899
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002900 gd->fops = &sd_fops;
2901 gd->private_data = &sdkp->driver;
2902 gd->queue = sdkp->device->request_queue;
2903
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002904 /* defaults, until the device tells us otherwise */
2905 sdp->sector_size = 512;
2906 sdkp->capacity = 0;
2907 sdkp->media_present = 1;
2908 sdkp->write_prot = 0;
James Bottomley39c60a02013-04-24 14:02:53 -07002909 sdkp->cache_override = 0;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002910 sdkp->WCE = 0;
2911 sdkp->RCD = 0;
2912 sdkp->ATO = 0;
2913 sdkp->first_scan = 1;
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05002914 sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002915
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002916 sd_revalidate_disk(gd);
2917
NeilBrown97fedbb2010-03-16 08:55:32 +01002918 gd->flags = GENHD_FL_EXT_DEVT;
Tejun Heo2bae0092010-12-18 18:42:23 +01002919 if (sdp->removable) {
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002920 gd->flags |= GENHD_FL_REMOVABLE;
Tejun Heo2bae0092010-12-18 18:42:23 +01002921 gd->events |= DISK_EVENT_MEDIA_CHANGE;
2922 }
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002923
Aaron Lu10c580e2013-10-10 13:22:36 +08002924 blk_pm_runtime_init(sdp->request_queue, dev);
Subhash Jadavani57d6b142016-07-29 16:50:07 -07002925 if (sdp->autosuspend_delay >= 0)
2926 pm_runtime_set_autosuspend_delay(dev, sdp->autosuspend_delay);
2927
Dan Williams0d52c7562016-06-15 19:44:20 -07002928 device_add_disk(dev, gd);
Martin K. Petersenfe542392012-09-21 12:44:12 -04002929 if (sdkp->capacity)
2930 sd_dif_config_host(sdkp);
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002931
Martin K. Petersen3821d762009-05-23 11:43:38 -04002932 sd_revalidate_disk(gd);
2933
Alan Stern478a8a02010-06-16 14:52:17 -04002934 scsi_autopm_put_device(sdp);
James Bottomleyea038f62009-08-21 09:47:54 -06002935 put_device(&sdkp->dev);
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002936}
2937
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002938/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 * sd_probe - called during driver initialization and whenever a
2940 * new scsi device is attached to the system. It is called once
2941 * for each scsi device (not just disks) present.
2942 * @dev: pointer to device object
2943 *
2944 * Returns 0 if successful (or not interested in this scsi device
2945 * (e.g. scanner)); 1 when there is an error.
2946 *
2947 * Note: this function is invoked from the scsi mid-level.
2948 * This function sets up the mapping between a given
2949 * <host,channel,id,lun> (found in sdp) and new device name
2950 * (e.g. /dev/sda). More precisely it is the block device major
2951 * and minor number that is chosen here.
2952 *
Petr Uzel2db93ce2012-02-24 16:56:54 +01002953 * Assume sd_probe is not re-entrant (for time being)
2954 * Also think about sd_probe() and sd_remove() running coincidentally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 **/
2956static int sd_probe(struct device *dev)
2957{
2958 struct scsi_device *sdp = to_scsi_device(dev);
2959 struct scsi_disk *sdkp;
2960 struct gendisk *gd;
H Hartley Sweeten439d77f2010-08-10 18:01:20 -07002961 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 int error;
2963
Subhash Jadavani6fe8c1d2014-09-10 14:54:09 +03002964 scsi_autopm_get_device(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 error = -ENODEV;
Al Viro 631e8a12005-05-16 01:59:55 +01002966 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 goto out;
2968
James Bottomley9ccfc752005-10-02 11:45:08 -05002969 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
Petr Uzel2db93ce2012-02-24 16:56:54 +01002970 "sd_probe\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -05002973 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 if (!sdkp)
2975 goto out;
2976
Tejun Heo689d6fa2008-08-25 19:56:16 +09002977 gd = alloc_disk(SD_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (!gd)
2979 goto out_free;
2980
Tejun Heof27bac22008-07-14 14:59:30 +09002981 do {
2982 if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
2983 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Tejun Heo4034cc62009-02-21 11:04:45 +09002985 spin_lock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09002986 error = ida_get_new(&sd_index_ida, &index);
Tejun Heo4034cc62009-02-21 11:04:45 +09002987 spin_unlock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09002988 } while (error == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Dave Kleikamp21208ae2011-10-19 11:49:04 -05002990 if (error) {
2991 sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 goto out_put;
Michael Reed1a03ae02010-09-20 11:20:22 -05002993 }
2994
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002995 error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
Dave Kleikamp21208ae2011-10-19 11:49:04 -05002996 if (error) {
2997 sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n");
Tejun Heof27bac22008-07-14 14:59:30 +09002998 goto out_free_index;
Dave Kleikamp21208ae2011-10-19 11:49:04 -05002999 }
Tejun Heof27bac22008-07-14 14:59:30 +09003000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 sdkp->device = sdp;
3002 sdkp->driver = &sd_template;
3003 sdkp->disk = gd;
3004 sdkp->index = index;
Arnd Bergmann409f3492010-07-07 16:51:29 +02003005 atomic_set(&sdkp->openers, 0);
Josh Hunt9e1a1532012-06-09 07:03:39 -07003006 atomic_set(&sdkp->device->ioerr_cnt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
James Bottomley601e7632009-05-26 20:35:48 +00003008 if (!sdp->request_queue->rq_timeout) {
3009 if (sdp->type != TYPE_MOD)
3010 blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
3011 else
3012 blk_queue_rq_timeout(sdp->request_queue,
3013 SD_MOD_TIMEOUT);
3014 }
3015
3016 device_initialize(&sdkp->dev);
Alan Stern478a8a02010-06-16 14:52:17 -04003017 sdkp->dev.parent = dev;
James Bottomley601e7632009-05-26 20:35:48 +00003018 sdkp->dev.class = &sd_disk_class;
Kees Cook02aa2a32013-07-03 15:04:56 -07003019 dev_set_name(&sdkp->dev, "%s", dev_name(dev));
James Bottomley601e7632009-05-26 20:35:48 +00003020
Dan Carpenterdee05862015-01-19 17:41:12 +03003021 error = device_add(&sdkp->dev);
3022 if (error)
James Bottomley601e7632009-05-26 20:35:48 +00003023 goto out_free_index;
3024
Alan Stern478a8a02010-06-16 14:52:17 -04003025 get_device(dev);
3026 dev_set_drvdata(dev, sdkp);
James Bottomley601e7632009-05-26 20:35:48 +00003027
James Bottomleyea038f62009-08-21 09:47:54 -06003028 get_device(&sdkp->dev); /* prevent release before async_schedule */
Dan Williamsa7a20d12012-03-22 17:05:11 -07003029 async_schedule_domain(sd_probe_async, sdkp, &scsi_sd_probe_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 return 0;
3032
Tejun Heof27bac22008-07-14 14:59:30 +09003033 out_free_index:
Tejun Heo4034cc62009-02-21 11:04:45 +09003034 spin_lock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09003035 ida_remove(&sd_index_ida, index);
Tejun Heo4034cc62009-02-21 11:04:45 +09003036 spin_unlock(&sd_index_lock);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06003037 out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 put_disk(gd);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06003039 out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 kfree(sdkp);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06003041 out:
Subhash Jadavani6fe8c1d2014-09-10 14:54:09 +03003042 scsi_autopm_put_device(sdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return error;
3044}
3045
3046/**
3047 * sd_remove - called whenever a scsi disk (previously recognized by
3048 * sd_probe) is detached from the system. It is called (potentially
3049 * multiple times) during sd module unload.
3050 * @sdp: pointer to mid level scsi device object
3051 *
3052 * Note: this function is invoked from the scsi mid-level.
3053 * This function potentially frees up a device name (e.g. /dev/sdc)
3054 * that could be re-used by a subsequent sd_probe().
3055 * This function is not called when the built-in sd driver is "exit-ed".
3056 **/
3057static int sd_remove(struct device *dev)
3058{
James Bottomley601e7632009-05-26 20:35:48 +00003059 struct scsi_disk *sdkp;
Hannes Reinecke0761df92013-05-10 11:06:16 +02003060 dev_t devt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
James Bottomley601e7632009-05-26 20:35:48 +00003062 sdkp = dev_get_drvdata(dev);
Hannes Reinecke0761df92013-05-10 11:06:16 +02003063 devt = disk_devt(sdkp->disk);
Alan Stern478a8a02010-06-16 14:52:17 -04003064 scsi_autopm_get_device(sdkp->device);
3065
Dan Williams3c31b522014-04-10 15:30:35 -07003066 async_synchronize_full_domain(&scsi_sd_pm_domain);
Dan Williamsa7a20d12012-03-22 17:05:11 -07003067 async_synchronize_full_domain(&scsi_sd_probe_domain);
Tony Jonesee959b02008-02-22 00:13:36 +01003068 device_del(&sdkp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 del_gendisk(sdkp->disk);
3070 sd_shutdown(dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -05003071
Hannes Reinecke0761df92013-05-10 11:06:16 +02003072 blk_register_region(devt, SD_MINORS, NULL,
3073 sd_default_probe, NULL, NULL);
3074
Arjan van de Ven0b950672006-01-11 13:16:10 +01003075 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -05003076 dev_set_drvdata(dev, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01003077 put_device(&sdkp->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01003078 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
3080 return 0;
3081}
3082
3083/**
3084 * scsi_disk_release - Called to free the scsi_disk structure
Tony Jonesee959b02008-02-22 00:13:36 +01003085 * @dev: pointer to embedded class device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 *
Arjan van de Ven0b950672006-01-11 13:16:10 +01003087 * sd_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 * called on last put, you should always use the scsi_disk_get()
3089 * scsi_disk_put() helpers which manipulate the semaphore directly
Tony Jonesee959b02008-02-22 00:13:36 +01003090 * and never do a direct put_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 **/
Tony Jonesee959b02008-02-22 00:13:36 +01003092static void scsi_disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
Tony Jonesee959b02008-02-22 00:13:36 +01003094 struct scsi_disk *sdkp = to_scsi_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 struct gendisk *disk = sdkp->disk;
3096
Tejun Heo4034cc62009-02-21 11:04:45 +09003097 spin_lock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09003098 ida_remove(&sd_index_ida, sdkp->index);
Tejun Heo4034cc62009-02-21 11:04:45 +09003099 spin_unlock(&sd_index_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
3101 disk->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 put_disk(disk);
Alan Stern39b7f1e2005-11-04 14:44:41 -05003103 put_device(&sdkp->device->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 kfree(sdkp);
3106}
3107
James Bottomleycc5d2c82007-03-20 12:26:03 -05003108static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
Tejun Heoc3c94c52007-03-21 00:13:59 +09003109{
3110 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
3111 struct scsi_sense_hdr sshdr;
James Bottomleycc5d2c82007-03-20 12:26:03 -05003112 struct scsi_device *sdp = sdkp->device;
Tejun Heoc3c94c52007-03-21 00:13:59 +09003113 int res;
3114
3115 if (start)
3116 cmd[4] |= 1; /* START */
3117
Stefan Richterd2886ea2008-05-11 00:34:07 +02003118 if (sdp->start_stop_pwr_cond)
3119 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
3120
Tejun Heoc3c94c52007-03-21 00:13:59 +09003121 if (!scsi_device_online(sdp))
3122 return -ENODEV;
3123
Lin Ming9b214932013-03-23 11:42:25 +08003124 res = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
3125 SD_TIMEOUT, SD_MAX_RETRIES, NULL, REQ_PM);
Tejun Heoc3c94c52007-03-21 00:13:59 +09003126 if (res) {
Hannes Reineckeef613292014-10-24 14:27:00 +02003127 sd_print_result(sdkp, "Start/Stop Unit failed", res);
Tejun Heoc3c94c52007-03-21 00:13:59 +09003128 if (driver_byte(res) & DRIVER_SENSE)
James Bottomleycc5d2c82007-03-20 12:26:03 -05003129 sd_print_sense_hdr(sdkp, &sshdr);
Oliver Neukum95897912013-09-16 13:28:15 +02003130 if (scsi_sense_valid(&sshdr) &&
3131 /* 0x3a is medium not present */
3132 sshdr.asc == 0x3a)
3133 res = 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09003134 }
3135
Oliver Neukum95897912013-09-16 13:28:15 +02003136 /* SCSI error codes must not go to the generic layer */
3137 if (res)
3138 return -EIO;
3139
3140 return 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09003141}
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143/*
3144 * Send a SYNCHRONIZE CACHE instruction down to the device through
3145 * the normal SCSI command structure. Wait for the command to
3146 * complete.
3147 */
3148static void sd_shutdown(struct device *dev)
3149{
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01003150 struct scsi_disk *sdkp = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152 if (!sdkp)
3153 return; /* this can happen */
3154
Lin Ming54f575882011-12-05 09:20:26 +08003155 if (pm_runtime_suspended(dev))
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01003156 return;
Lin Ming54f575882011-12-05 09:20:26 +08003157
Oliver Neukum95897912013-09-16 13:28:15 +02003158 if (sdkp->WCE && sdkp->media_present) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05003159 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
3160 sd_sync_cache(sdkp);
Alan Stern39b7f1e2005-11-04 14:44:41 -05003161 }
Tejun Heoc3c94c52007-03-21 00:13:59 +09003162
James Bottomleycc5d2c82007-03-20 12:26:03 -05003163 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
3164 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
3165 sd_start_stop_device(sdkp, 0);
Tejun Heoc3c94c52007-03-21 00:13:59 +09003166 }
Alan Stern39b7f1e2005-11-04 14:44:41 -05003167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Oliver Neukum95897912013-09-16 13:28:15 +02003169static int sd_suspend_common(struct device *dev, bool ignore_stop_errors)
Tejun Heoc3c94c52007-03-21 00:13:59 +09003170{
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01003171 struct scsi_disk *sdkp = dev_get_drvdata(dev);
Alan Stern09ff92f2007-05-21 09:55:04 -04003172 int ret = 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09003173
Alan Stern13b43892016-01-20 11:26:01 -05003174 if (!sdkp) /* E.g.: runtime suspend following sd_remove() */
3175 return 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09003176
Oliver Neukum95897912013-09-16 13:28:15 +02003177 if (sdkp->WCE && sdkp->media_present) {
Tejun Heoc3c94c52007-03-21 00:13:59 +09003178 ret = sd_sync_cache(sdkp);
Oliver Neukum95897912013-09-16 13:28:15 +02003179 if (ret) {
3180 /* ignore OFFLINE device */
3181 if (ret == -ENODEV)
3182 ret = 0;
Alan Stern09ff92f2007-05-21 09:55:04 -04003183 goto done;
Oliver Neukum95897912013-09-16 13:28:15 +02003184 }
Tejun Heoc3c94c52007-03-21 00:13:59 +09003185 }
3186
Aaron Lu691e3d32012-11-09 15:27:55 +08003187 if (sdkp->device->manage_start_stop) {
Subhash Jadavani57d6b142016-07-29 16:50:07 -07003188 sd_printk(KERN_DEBUG, sdkp, "Stopping disk\n");
Oliver Neukum95897912013-09-16 13:28:15 +02003189 /* an error is not worth aborting a system sleep */
James Bottomleycc5d2c82007-03-20 12:26:03 -05003190 ret = sd_start_stop_device(sdkp, 0);
Oliver Neukum95897912013-09-16 13:28:15 +02003191 if (ignore_stop_errors)
3192 ret = 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09003193 }
3194
Alan Stern09ff92f2007-05-21 09:55:04 -04003195done:
Alan Stern09ff92f2007-05-21 09:55:04 -04003196 return ret;
Tejun Heoc3c94c52007-03-21 00:13:59 +09003197}
3198
Oliver Neukum95897912013-09-16 13:28:15 +02003199static int sd_suspend_system(struct device *dev)
3200{
3201 return sd_suspend_common(dev, true);
3202}
3203
3204static int sd_suspend_runtime(struct device *dev)
3205{
3206 return sd_suspend_common(dev, false);
3207}
3208
Tejun Heoc3c94c52007-03-21 00:13:59 +09003209static int sd_resume(struct device *dev)
3210{
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01003211 struct scsi_disk *sdkp = dev_get_drvdata(dev);
Tejun Heoc3c94c52007-03-21 00:13:59 +09003212
Alan Stern13b43892016-01-20 11:26:01 -05003213 if (!sdkp) /* E.g.: runtime resume at the start of sd_probe() */
3214 return 0;
3215
James Bottomleycc5d2c82007-03-20 12:26:03 -05003216 if (!sdkp->device->manage_start_stop)
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01003217 return 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09003218
Subhash Jadavani57d6b142016-07-29 16:50:07 -07003219 sd_printk(KERN_DEBUG, sdkp, "Starting disk\n");
Christoph Hellwig3d9a1f52015-02-02 14:01:25 +01003220 return sd_start_stop_device(sdkp, 1);
Tejun Heoc3c94c52007-03-21 00:13:59 +09003221}
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223/**
3224 * init_sd - entry point for this driver (both when built in or when
3225 * a module).
3226 *
3227 * Note: this function registers this driver with the scsi mid-level.
3228 **/
3229static int __init init_sd(void)
3230{
Jeff Garzik5e4009b2006-10-04 05:32:54 -04003231 int majors = 0, i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
3233 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
3234
Hannes Reinecke0761df92013-05-10 11:06:16 +02003235 for (i = 0; i < SD_MAJORS; i++) {
3236 if (register_blkdev(sd_major(i), "sd") != 0)
3237 continue;
3238 majors++;
3239 blk_register_region(sd_major(i), SD_MINORS, NULL,
3240 sd_default_probe, NULL, NULL);
3241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243 if (!majors)
3244 return -ENODEV;
3245
Jeff Garzik5e4009b2006-10-04 05:32:54 -04003246 err = class_register(&sd_disk_class);
3247 if (err)
3248 goto err_out;
James Bottomley6bdaa1f2006-03-18 14:14:21 -06003249
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04003250 sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
3251 0, 0, NULL);
3252 if (!sd_cdb_cache) {
3253 printk(KERN_ERR "sd: can't init extended cdb cache\n");
Clément Calmels8d964472014-06-03 23:34:25 +02003254 err = -ENOMEM;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04003255 goto err_out_class;
3256 }
3257
3258 sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
3259 if (!sd_cdb_pool) {
3260 printk(KERN_ERR "sd: can't init extended cdb pool\n");
Clément Calmels8d964472014-06-03 23:34:25 +02003261 err = -ENOMEM;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04003262 goto err_out_cache;
3263 }
3264
Joel D. Diazafd5e342012-10-10 10:36:11 +02003265 err = scsi_register_driver(&sd_template.gendrv);
3266 if (err)
3267 goto err_out_driver;
3268
Jeff Garzik5e4009b2006-10-04 05:32:54 -04003269 return 0;
3270
Joel D. Diazafd5e342012-10-10 10:36:11 +02003271err_out_driver:
3272 mempool_destroy(sd_cdb_pool);
3273
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04003274err_out_cache:
3275 kmem_cache_destroy(sd_cdb_cache);
3276
Jeff Garzik5e4009b2006-10-04 05:32:54 -04003277err_out_class:
3278 class_unregister(&sd_disk_class);
3279err_out:
3280 for (i = 0; i < SD_MAJORS; i++)
3281 unregister_blkdev(sd_major(i), "sd");
3282 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
3285/**
3286 * exit_sd - exit point for this driver (when it is a module).
3287 *
3288 * Note: this function unregisters this driver from the scsi mid-level.
3289 **/
3290static void __exit exit_sd(void)
3291{
3292 int i;
3293
3294 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
3295
Joel D. Diazafd5e342012-10-10 10:36:11 +02003296 scsi_unregister_driver(&sd_template.gendrv);
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04003297 mempool_destroy(sd_cdb_pool);
3298 kmem_cache_destroy(sd_cdb_cache);
3299
Jeff Garzik5e4009b2006-10-04 05:32:54 -04003300 class_unregister(&sd_disk_class);
3301
Hannes Reinecke0761df92013-05-10 11:06:16 +02003302 for (i = 0; i < SD_MAJORS; i++) {
3303 blk_unregister_region(sd_major(i), SD_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 unregister_blkdev(sd_major(i), "sd");
Hannes Reinecke0761df92013-05-10 11:06:16 +02003305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306}
3307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308module_init(init_sd);
3309module_exit(exit_sd);
Martin K. Petersene73aec82007-02-27 22:40:55 -05003310
3311static void sd_print_sense_hdr(struct scsi_disk *sdkp,
3312 struct scsi_sense_hdr *sshdr)
3313{
Hannes Reinecke21045512015-01-08 07:43:46 +01003314 scsi_print_sense_hdr(sdkp->device,
3315 sdkp->disk ? sdkp->disk->disk_name : NULL, sshdr);
Martin K. Petersene73aec82007-02-27 22:40:55 -05003316}
3317
Hannes Reineckeef613292014-10-24 14:27:00 +02003318static void sd_print_result(const struct scsi_disk *sdkp, const char *msg,
3319 int result)
Martin K. Petersene73aec82007-02-27 22:40:55 -05003320{
Hannes Reineckeef613292014-10-24 14:27:00 +02003321 const char *hb_string = scsi_hostbyte_string(result);
3322 const char *db_string = scsi_driverbyte_string(result);
3323
3324 if (hb_string || db_string)
3325 sd_printk(KERN_INFO, sdkp,
3326 "%s: Result: hostbyte=%s driverbyte=%s\n", msg,
3327 hb_string ? hb_string : "invalid",
3328 db_string ? db_string : "invalid");
3329 else
3330 sd_printk(KERN_INFO, sdkp,
3331 "%s: Result: hostbyte=0x%02x driverbyte=0x%02x\n",
3332 msg, host_byte(result), driver_byte(result));
Martin K. Petersene73aec82007-02-27 22:40:55 -05003333}
3334