blob: fc5d69a84af5ff66ca39fbed2e1496b3b6453f28 [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>
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +020049#include <linux/smp_lock.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010050#include <linux/mutex.h>
James Bottomley7404ad3b2008-08-31 10:41:52 -050051#include <linux/string_helpers.h>
Arjan van de Ven4ace92f2009-01-04 05:32:28 -080052#include <linux/async.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/uaccess.h>
Dave Hansen8f76d152009-04-21 16:43:27 -070055#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#include <scsi/scsi.h>
58#include <scsi/scsi_cmnd.h>
59#include <scsi/scsi_dbg.h>
60#include <scsi/scsi_device.h>
61#include <scsi/scsi_driver.h>
62#include <scsi/scsi_eh.h>
63#include <scsi/scsi_host.h>
64#include <scsi/scsi_ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <scsi/scsicam.h>
66
Martin K. Petersenaa916962008-06-17 12:47:32 -040067#include "sd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include "scsi_logging.h"
69
Rene Hermanf018fa52006-03-08 00:14:20 -080070MODULE_AUTHOR("Eric Youngdale");
71MODULE_DESCRIPTION("SCSI disk (sd) driver");
72MODULE_LICENSE("GPL");
73
74MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
75MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
76MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
77MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
78MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
79MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
80MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
81MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
82MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
83MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
84MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
85MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
86MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
87MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
88MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
89MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +040090MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
91MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
92MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
Rene Hermanf018fa52006-03-08 00:14:20 -080093
Tejun Heo870d6652008-08-25 19:47:25 +090094#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
Tejun Heof615b482008-08-25 19:47:24 +090095#define SD_MINORS 16
Tejun Heo870d6652008-08-25 19:47:25 +090096#else
Tejun Heo3e1a7ff2008-08-25 19:56:17 +090097#define SD_MINORS 0
Tejun Heo870d6652008-08-25 19:47:25 +090098#endif
99
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800100static int sd_revalidate_disk(struct gendisk *);
Tejun Heo72ec24b2010-05-15 20:09:32 +0200101static void sd_unlock_native_capacity(struct gendisk *disk);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800102static int sd_probe(struct device *);
103static int sd_remove(struct device *);
104static void sd_shutdown(struct device *);
105static int sd_suspend(struct device *, pm_message_t state);
106static int sd_resume(struct device *);
107static void sd_rescan(struct device *);
108static int sd_done(struct scsi_cmnd *);
109static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
Tony Jonesee959b02008-02-22 00:13:36 +0100110static void scsi_disk_release(struct device *cdev);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800111static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
112static void sd_print_result(struct scsi_disk *, int);
113
Tejun Heo4034cc62009-02-21 11:04:45 +0900114static DEFINE_SPINLOCK(sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +0900115static DEFINE_IDA(sd_index_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/* This semaphore is used to mediate the 0->1 reference get in the
118 * face of object destruction (i.e. we can't allow a get on an
119 * object after last put) */
Arjan van de Ven0b950672006-01-11 13:16:10 +0100120static DEFINE_MUTEX(sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400122struct kmem_cache *sd_cdb_cache;
123mempool_t *sd_cdb_pool;
124
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600125static const char *sd_cache_types[] = {
126 "write through", "none", "write back",
127 "write back, no read (daft)"
128};
129
Tony Jonesee959b02008-02-22 00:13:36 +0100130static ssize_t
131sd_store_cache_type(struct device *dev, struct device_attribute *attr,
132 const char *buf, size_t count)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600133{
134 int i, ct = -1, rcd, wce, sp;
Tony Jonesee959b02008-02-22 00:13:36 +0100135 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600136 struct scsi_device *sdp = sdkp->device;
137 char buffer[64];
138 char *buffer_data;
139 struct scsi_mode_data data;
140 struct scsi_sense_hdr sshdr;
141 int len;
142
143 if (sdp->type != TYPE_DISK)
144 /* no cache control on RBC devices; theoretically they
145 * can do it, but there's probably so many exceptions
146 * it's not worth the risk */
147 return -EINVAL;
148
Tobias Klauser6391a112006-06-08 22:23:48 -0700149 for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600150 const int len = strlen(sd_cache_types[i]);
151 if (strncmp(sd_cache_types[i], buf, len) == 0 &&
152 buf[len] == '\n') {
153 ct = i;
154 break;
155 }
156 }
157 if (ct < 0)
158 return -EINVAL;
159 rcd = ct & 0x01 ? 1 : 0;
160 wce = ct & 0x02 ? 1 : 0;
161 if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
162 SD_MAX_RETRIES, &data, NULL))
163 return -EINVAL;
Andrew Mortona9312fb2006-03-25 03:08:30 -0800164 len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600165 data.block_descriptor_length);
166 buffer_data = buffer + data.header_length +
167 data.block_descriptor_length;
168 buffer_data[2] &= ~0x05;
169 buffer_data[2] |= wce << 2 | rcd;
170 sp = buffer_data[0] & 0x80 ? 1 : 0;
171
172 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
173 SD_MAX_RETRIES, &data, &sshdr)) {
174 if (scsi_sense_valid(&sshdr))
Martin K. Petersene73aec82007-02-27 22:40:55 -0500175 sd_print_sense_hdr(sdkp, &sshdr);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600176 return -EINVAL;
177 }
Andrew Pattersonf98a8ca2008-09-04 14:27:35 -0600178 revalidate_disk(sdkp->disk);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600179 return count;
180}
181
Tony Jonesee959b02008-02-22 00:13:36 +0100182static ssize_t
183sd_store_manage_start_stop(struct device *dev, struct device_attribute *attr,
184 const char *buf, size_t count)
Tejun Heoc3c94c52007-03-21 00:13:59 +0900185{
Tony Jonesee959b02008-02-22 00:13:36 +0100186 struct scsi_disk *sdkp = to_scsi_disk(dev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900187 struct scsi_device *sdp = sdkp->device;
188
189 if (!capable(CAP_SYS_ADMIN))
190 return -EACCES;
191
192 sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
193
194 return count;
195}
196
Tony Jonesee959b02008-02-22 00:13:36 +0100197static ssize_t
198sd_store_allow_restart(struct device *dev, struct device_attribute *attr,
199 const char *buf, size_t count)
Brian Kinga144c5a2006-06-27 11:10:31 -0500200{
Tony Jonesee959b02008-02-22 00:13:36 +0100201 struct scsi_disk *sdkp = to_scsi_disk(dev);
Brian Kinga144c5a2006-06-27 11:10:31 -0500202 struct scsi_device *sdp = sdkp->device;
203
204 if (!capable(CAP_SYS_ADMIN))
205 return -EACCES;
206
207 if (sdp->type != TYPE_DISK)
208 return -EINVAL;
209
210 sdp->allow_restart = simple_strtoul(buf, NULL, 10);
211
212 return count;
213}
214
Tony Jonesee959b02008-02-22 00:13:36 +0100215static ssize_t
216sd_show_cache_type(struct device *dev, struct device_attribute *attr,
217 char *buf)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600218{
Tony Jonesee959b02008-02-22 00:13:36 +0100219 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600220 int ct = sdkp->RCD + 2*sdkp->WCE;
221
222 return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
223}
224
Tony Jonesee959b02008-02-22 00:13:36 +0100225static ssize_t
226sd_show_fua(struct device *dev, struct device_attribute *attr, char *buf)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600227{
Tony Jonesee959b02008-02-22 00:13:36 +0100228 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600229
230 return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
231}
232
Tony Jonesee959b02008-02-22 00:13:36 +0100233static ssize_t
234sd_show_manage_start_stop(struct device *dev, struct device_attribute *attr,
235 char *buf)
Tejun Heoc3c94c52007-03-21 00:13:59 +0900236{
Tony Jonesee959b02008-02-22 00:13:36 +0100237 struct scsi_disk *sdkp = to_scsi_disk(dev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900238 struct scsi_device *sdp = sdkp->device;
239
240 return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
241}
242
Tony Jonesee959b02008-02-22 00:13:36 +0100243static ssize_t
244sd_show_allow_restart(struct device *dev, struct device_attribute *attr,
245 char *buf)
Brian Kinga144c5a2006-06-27 11:10:31 -0500246{
Tony Jonesee959b02008-02-22 00:13:36 +0100247 struct scsi_disk *sdkp = to_scsi_disk(dev);
Brian Kinga144c5a2006-06-27 11:10:31 -0500248
249 return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
250}
251
Martin K. Petersene0597d72008-07-17 04:28:34 -0400252static ssize_t
253sd_show_protection_type(struct device *dev, struct device_attribute *attr,
254 char *buf)
255{
256 struct scsi_disk *sdkp = to_scsi_disk(dev);
257
258 return snprintf(buf, 20, "%u\n", sdkp->protection_type);
259}
260
261static ssize_t
262sd_show_app_tag_own(struct device *dev, struct device_attribute *attr,
263 char *buf)
264{
265 struct scsi_disk *sdkp = to_scsi_disk(dev);
266
267 return snprintf(buf, 20, "%u\n", sdkp->ATO);
268}
269
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500270static ssize_t
271sd_show_thin_provisioning(struct device *dev, struct device_attribute *attr,
272 char *buf)
273{
274 struct scsi_disk *sdkp = to_scsi_disk(dev);
275
276 return snprintf(buf, 20, "%u\n", sdkp->thin_provisioning);
277}
278
Tony Jonesee959b02008-02-22 00:13:36 +0100279static struct device_attribute sd_disk_attrs[] = {
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600280 __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
281 sd_store_cache_type),
282 __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
Brian Kinga144c5a2006-06-27 11:10:31 -0500283 __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
284 sd_store_allow_restart),
Tejun Heoc3c94c52007-03-21 00:13:59 +0900285 __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
286 sd_store_manage_start_stop),
Martin K. Petersene0597d72008-07-17 04:28:34 -0400287 __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
288 __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500289 __ATTR(thin_provisioning, S_IRUGO, sd_show_thin_provisioning, NULL),
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600290 __ATTR_NULL,
291};
292
293static struct class sd_disk_class = {
294 .name = "scsi_disk",
295 .owner = THIS_MODULE,
Tony Jonesee959b02008-02-22 00:13:36 +0100296 .dev_release = scsi_disk_release,
297 .dev_attrs = sd_disk_attrs,
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600298};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300static struct scsi_driver sd_template = {
301 .owner = THIS_MODULE,
302 .gendrv = {
303 .name = "sd",
304 .probe = sd_probe,
305 .remove = sd_remove,
Tejun Heoc3c94c52007-03-21 00:13:59 +0900306 .suspend = sd_suspend,
307 .resume = sd_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 .shutdown = sd_shutdown,
309 },
310 .rescan = sd_rescan,
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800311 .done = sd_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312};
313
314/*
315 * Device no to disk mapping:
316 *
317 * major disc2 disc p1
318 * |............|.............|....|....| <- dev_t
319 * 31 20 19 8 7 4 3 0
320 *
321 * Inside a major, we have 16k disks, however mapped non-
322 * contiguously. The first 16 disks are for major0, the next
323 * ones with major1, ... Disk 256 is for major0 again, disk 272
324 * for major1, ...
325 * As we stay compatible with our numbering scheme, we can reuse
326 * the well-know SCSI majors 8, 65--71, 136--143.
327 */
328static int sd_major(int major_idx)
329{
330 switch (major_idx) {
331 case 0:
332 return SCSI_DISK0_MAJOR;
333 case 1 ... 7:
334 return SCSI_DISK1_MAJOR + major_idx - 1;
335 case 8 ... 15:
336 return SCSI_DISK8_MAJOR + major_idx - 8;
337 default:
338 BUG();
339 return 0; /* shut up gcc */
340 }
341}
342
Alan Stern39b7f1e2005-11-04 14:44:41 -0500343static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct scsi_disk *sdkp = NULL;
346
Alan Stern39b7f1e2005-11-04 14:44:41 -0500347 if (disk->private_data) {
348 sdkp = scsi_disk(disk);
349 if (scsi_device_get(sdkp->device) == 0)
Tony Jonesee959b02008-02-22 00:13:36 +0100350 get_device(&sdkp->dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500351 else
352 sdkp = NULL;
353 }
354 return sdkp;
355}
356
357static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
358{
359 struct scsi_disk *sdkp;
360
Arjan van de Ven0b950672006-01-11 13:16:10 +0100361 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500362 sdkp = __scsi_disk_get(disk);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100363 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return sdkp;
Alan Stern39b7f1e2005-11-04 14:44:41 -0500365}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Alan Stern39b7f1e2005-11-04 14:44:41 -0500367static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
368{
369 struct scsi_disk *sdkp;
370
Arjan van de Ven0b950672006-01-11 13:16:10 +0100371 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500372 sdkp = dev_get_drvdata(dev);
373 if (sdkp)
374 sdkp = __scsi_disk_get(sdkp->disk);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100375 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return sdkp;
377}
378
379static void scsi_disk_put(struct scsi_disk *sdkp)
380{
381 struct scsi_device *sdev = sdkp->device;
382
Arjan van de Ven0b950672006-01-11 13:16:10 +0100383 mutex_lock(&sd_ref_mutex);
Tony Jonesee959b02008-02-22 00:13:36 +0100384 put_device(&sdkp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 scsi_device_put(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100386 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -0400389static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
390{
391 unsigned int prot_op = SCSI_PROT_NORMAL;
392 unsigned int dix = scsi_prot_sg_count(scmd);
393
394 if (scmd->sc_data_direction == DMA_FROM_DEVICE) {
395 if (dif && dix)
396 prot_op = SCSI_PROT_READ_PASS;
397 else if (dif && !dix)
398 prot_op = SCSI_PROT_READ_STRIP;
399 else if (!dif && dix)
400 prot_op = SCSI_PROT_READ_INSERT;
401 } else {
402 if (dif && dix)
403 prot_op = SCSI_PROT_WRITE_PASS;
404 else if (dif && !dix)
405 prot_op = SCSI_PROT_WRITE_INSERT;
406 else if (!dif && dix)
407 prot_op = SCSI_PROT_WRITE_STRIP;
408 }
409
410 scsi_set_prot_op(scmd, prot_op);
411 scsi_set_prot_type(scmd, dif);
412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/**
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200415 * scsi_setup_discard_cmnd - unmap blocks on thinly provisioned device
416 * @sdp: scsi device to operate one
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500417 * @rq: Request to prepare
418 *
419 * Will issue either UNMAP or WRITE SAME(16) depending on preference
420 * indicated by target device.
421 **/
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200422static int scsi_setup_discard_cmnd(struct scsi_device *sdp, struct request *rq)
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500423{
424 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
425 struct bio *bio = rq->bio;
426 sector_t sector = bio->bi_sector;
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200427 unsigned int nr_sectors = bio_sectors(bio);
428 unsigned int len;
FUJITA Tomonorif1126e92010-07-01 19:49:18 +0900429 int ret;
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200430 struct page *page;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500431
432 if (sdkp->device->sector_size == 4096) {
433 sector >>= 3;
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200434 nr_sectors >>= 3;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500435 }
436
437 rq->cmd_type = REQ_TYPE_BLOCK_PC;
438 rq->timeout = SD_TIMEOUT;
439
440 memset(rq->cmd, 0, rq->cmd_len);
441
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200442 page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
443 if (!page)
444 return BLKPREP_DEFER;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500445
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200446 if (sdkp->unmap) {
447 char *buf = page_address(page);
448
449 rq->cmd_len = 10;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500450 rq->cmd[0] = UNMAP;
451 rq->cmd[8] = 24;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500452
453 put_unaligned_be16(6 + 16, &buf[0]);
454 put_unaligned_be16(16, &buf[2]);
455 put_unaligned_be64(sector, &buf[8]);
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200456 put_unaligned_be32(nr_sectors, &buf[16]);
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500457
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200458 len = 24;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500459 } else {
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200460 rq->cmd_len = 16;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500461 rq->cmd[0] = WRITE_SAME_16;
462 rq->cmd[1] = 0x8; /* UNMAP */
463 put_unaligned_be64(sector, &rq->cmd[2]);
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200464 put_unaligned_be32(nr_sectors, &rq->cmd[10]);
465
466 len = sdkp->device->sector_size;
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500467 }
468
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200469 blk_add_request_payload(rq, page, len);
FUJITA Tomonorif1126e92010-07-01 19:49:18 +0900470 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
471 rq->buffer = page_address(page);
FUJITA Tomonori610a6342010-07-08 10:16:17 +0200472 if (ret != BLKPREP_OK) {
473 __free_page(page);
474 rq->buffer = NULL;
475 }
FUJITA Tomonorif1126e92010-07-01 19:49:18 +0900476 return ret;
477}
478
FUJITA Tomonori90467c22010-07-03 17:45:34 +0900479static int scsi_setup_flush_cmnd(struct scsi_device *sdp, struct request *rq)
480{
481 /* for now, we use REQ_TYPE_BLOCK_PC. */
482 rq->cmd_type = REQ_TYPE_BLOCK_PC;
483 rq->timeout = SD_TIMEOUT;
484 rq->retries = SD_MAX_RETRIES;
485 rq->cmd[0] = SYNCHRONIZE_CACHE;
486 rq->cmd_len = 10;
487
488 return scsi_setup_blk_pc_cmnd(sdp, rq);
489}
490
FUJITA Tomonorif1126e92010-07-01 19:49:18 +0900491static void sd_unprep_fn(struct request_queue *q, struct request *rq)
492{
FUJITA Tomonori610a6342010-07-08 10:16:17 +0200493 if (rq->cmd_flags & REQ_DISCARD) {
494 free_page((unsigned long)rq->buffer);
495 rq->buffer = NULL;
496 }
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500497}
498
499/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * sd_init_command - build a scsi (read or write) command from
501 * information in the request structure.
502 * @SCpnt: pointer to mid-level's per scsi command structure that
503 * contains request and into which the scsi command is written
504 *
505 * Returns 1 if successful and 0 if error (or cannot be done now).
506 **/
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500507static int sd_prep_fn(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500509 struct scsi_cmnd *SCpnt;
510 struct scsi_device *sdp = q->queuedata;
Christoph Hellwig776b23a2006-01-06 18:34:07 +0100511 struct gendisk *disk = rq->rq_disk;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400512 struct scsi_disk *sdkp;
Tejun Heo83096eb2009-05-07 22:24:39 +0900513 sector_t block = blk_rq_pos(rq);
Linus Torvalds18351072008-08-05 21:42:21 -0700514 sector_t threshold;
Tejun Heo83096eb2009-05-07 22:24:39 +0900515 unsigned int this_count = blk_rq_sectors(rq);
Martin K. Petersenbd623e72008-09-19 18:47:19 -0400516 int ret, host_dif;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400517 unsigned char protect;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500518
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500519 /*
520 * Discard request come in as REQ_TYPE_FS but we turn them into
521 * block PC requests to make life easier.
522 */
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200523 if (rq->cmd_flags & REQ_DISCARD) {
524 ret = scsi_setup_discard_cmnd(sdp, rq);
525 goto out;
FUJITA Tomonori90467c22010-07-03 17:45:34 +0900526 } else if (rq->cmd_flags & REQ_FLUSH) {
527 ret = scsi_setup_flush_cmnd(sdp, rq);
528 goto out;
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200529 } else if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500530 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
531 goto out;
532 } else if (rq->cmd_type != REQ_TYPE_FS) {
533 ret = BLKPREP_KILL;
534 goto out;
535 }
536 ret = scsi_setup_fs_cmnd(sdp, rq);
537 if (ret != BLKPREP_OK)
538 goto out;
539 SCpnt = rq->special;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400540 sdkp = scsi_disk(disk);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500541
542 /* from here on until we're complete, any goto out
543 * is used for a killable error condition */
544 ret = BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500546 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
547 "sd_init_command: block=%llu, "
548 "count=%d\n",
549 (unsigned long long)block,
550 this_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (!sdp || !scsi_device_online(sdp) ||
Tejun Heo83096eb2009-05-07 22:24:39 +0900553 block + blk_rq_sectors(rq) > get_capacity(disk)) {
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500554 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
Tejun Heo83096eb2009-05-07 22:24:39 +0900555 "Finishing %u sectors\n",
556 blk_rq_sectors(rq)));
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500557 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
558 "Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
562 if (sdp->changed) {
563 /*
564 * quietly refuse to do anything to a changed disc until
565 * the changed bit has been reset
566 */
567 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500568 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500570
Hans de Goedea0899d42008-01-20 11:12:26 +0100571 /*
Linus Torvalds18351072008-08-05 21:42:21 -0700572 * Some SD card readers can't handle multi-sector accesses which touch
573 * the last one or two hardware sectors. Split accesses as needed.
Hans de Goedea0899d42008-01-20 11:12:26 +0100574 */
Linus Torvalds18351072008-08-05 21:42:21 -0700575 threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
576 (sdp->sector_size / 512);
577
578 if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
579 if (block < threshold) {
580 /* Access up to the threshold but not beyond */
581 this_count = threshold - block;
582 } else {
583 /* Access only a single hardware sector */
584 this_count = sdp->sector_size / 512;
585 }
586 }
Hans de Goedea0899d42008-01-20 11:12:26 +0100587
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500588 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
589 (unsigned long long)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 /*
592 * If we have a 1K hardware sectorsize, prevent access to single
593 * 512 byte sectors. In theory we could handle this - in fact
594 * the scsi cdrom driver must be able to handle this because
595 * we typically use 1K blocksizes, and cdroms typically have
596 * 2K hardware sectorsizes. Of course, things are simpler
597 * with the cdrom, since it is read-only. For performance
598 * reasons, the filesystems should be able to handle this
599 * and not force the scsi disk driver to use bounce buffers
600 * for this.
601 */
602 if (sdp->sector_size == 1024) {
Tejun Heo83096eb2009-05-07 22:24:39 +0900603 if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500604 scmd_printk(KERN_ERR, SCpnt,
605 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500606 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 } else {
608 block = block >> 1;
609 this_count = this_count >> 1;
610 }
611 }
612 if (sdp->sector_size == 2048) {
Tejun Heo83096eb2009-05-07 22:24:39 +0900613 if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500614 scmd_printk(KERN_ERR, SCpnt,
615 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500616 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 } else {
618 block = block >> 2;
619 this_count = this_count >> 2;
620 }
621 }
622 if (sdp->sector_size == 4096) {
Tejun Heo83096eb2009-05-07 22:24:39 +0900623 if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500624 scmd_printk(KERN_ERR, SCpnt,
625 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 } else {
628 block = block >> 3;
629 this_count = this_count >> 3;
630 }
631 }
632 if (rq_data_dir(rq) == WRITE) {
633 if (!sdp->writeable) {
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500634 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 SCpnt->cmnd[0] = WRITE_6;
637 SCpnt->sc_data_direction = DMA_TO_DEVICE;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400638
639 if (blk_integrity_rq(rq) &&
640 sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
641 goto out;
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 } else if (rq_data_dir(rq) == READ) {
644 SCpnt->cmnd[0] = READ_6;
645 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
646 } else {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500647 scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500651 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
Tejun Heo83096eb2009-05-07 22:24:39 +0900652 "%s %d/%u 512 byte blocks.\n",
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500653 (rq_data_dir(rq) == WRITE) ?
654 "writing" : "reading", this_count,
Tejun Heo83096eb2009-05-07 22:24:39 +0900655 blk_rq_sectors(rq)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400657 /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
Martin K. Petersenbd623e72008-09-19 18:47:19 -0400658 host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
659 if (host_dif)
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400660 protect = 1 << 5;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400661 else
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400662 protect = 0;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400663
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400664 if (host_dif == SD_DIF_TYPE2_PROTECTION) {
665 SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
666
667 if (unlikely(SCpnt->cmnd == NULL)) {
668 ret = BLKPREP_DEFER;
669 goto out;
670 }
671
672 SCpnt->cmd_len = SD_EXT_CDB_SIZE;
673 memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
674 SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
675 SCpnt->cmnd[7] = 0x18;
676 SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200677 SCpnt->cmnd[10] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400678
679 /* LBA */
680 SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
681 SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
682 SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
683 SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
684 SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
685 SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
686 SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
687 SCpnt->cmnd[19] = (unsigned char) block & 0xff;
688
689 /* Expected Indirect LBA */
690 SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
691 SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
692 SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
693 SCpnt->cmnd[23] = (unsigned char) block & 0xff;
694
695 /* Transfer length */
696 SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
697 SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
698 SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
699 SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
700 } else if (block > 0xffffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 SCpnt->cmnd[0] += READ_16 - READ_6;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200702 SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
704 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
705 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
706 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
707 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
708 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
709 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
710 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
711 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
712 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
713 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
714 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
715 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
716 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400717 scsi_device_protection(SCpnt->device) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 SCpnt->device->use_10_for_rw) {
719 if (this_count > 0xffff)
720 this_count = 0xffff;
721
722 SCpnt->cmnd[0] += READ_10 - READ_6;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200723 SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
725 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
726 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
727 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
728 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
729 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
730 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
731 } else {
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200732 if (unlikely(rq->cmd_flags & REQ_FUA)) {
Tejun Heo007365a2006-01-06 09:53:52 +0100733 /*
734 * This happens only if this drive failed
735 * 10byte rw command with ILLEGAL_REQUEST
736 * during operation and thus turned off
737 * use_10_for_rw.
738 */
Martin K. Petersene73aec82007-02-27 22:40:55 -0500739 scmd_printk(KERN_ERR, SCpnt,
740 "FUA write on READ/WRITE(6) drive\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500741 goto out;
Tejun Heo007365a2006-01-06 09:53:52 +0100742 }
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
745 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
746 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
747 SCpnt->cmnd[4] = (unsigned char) this_count;
748 SCpnt->cmnd[5] = 0;
749 }
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200750 SCpnt->sdb.length = this_count * sdp->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400752 /* If DIF or DIX is enabled, tell HBA how to handle request */
Martin K. Petersenbd623e72008-09-19 18:47:19 -0400753 if (host_dif || scsi_prot_sg_count(SCpnt))
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -0400754 sd_prot_op(SCpnt, host_dif);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /*
757 * We shouldn't disconnect in the middle of a sector, so with a dumb
758 * host adapter, it's safe to assume that we can at least transfer
759 * this many bytes between each connect / disconnect.
760 */
761 SCpnt->transfersize = sdp->sector_size;
762 SCpnt->underflow = this_count << 9;
763 SCpnt->allowed = SD_MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 * This indicates that the command is ready from our end to be
767 * queued.
768 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500769 ret = BLKPREP_OK;
770 out:
771 return scsi_prep_return(q, rq, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/**
775 * sd_open - open a scsi disk device
776 * @inode: only i_rdev member may be used
777 * @filp: only f_mode and f_flags may be used
778 *
779 * Returns 0 if successful. Returns a negated errno value in case
780 * of error.
781 *
782 * Note: This can be called from a user context (e.g. fsck(1) )
783 * or from within the kernel (e.g. as a result of a mount(1) ).
784 * In the latter case @inode and @filp carry an abridged amount
785 * of information as noted above.
Arnd Bergmann409f3492010-07-07 16:51:29 +0200786 *
787 * Locking: called with bdev->bd_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 **/
Al Viro0338e292008-03-02 10:41:04 -0500789static int sd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Al Viro0338e292008-03-02 10:41:04 -0500791 struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct scsi_device *sdev;
793 int retval;
794
Al Viro0338e292008-03-02 10:41:04 -0500795 if (!sdkp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -ENXIO;
797
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500798 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 sdev = sdkp->device;
801
802 /*
803 * If the device is in error recovery, wait until it is done.
804 * If the device is offline, then disallow any access to it.
805 */
806 retval = -ENXIO;
807 if (!scsi_block_when_processing_errors(sdev))
808 goto error_out;
809
810 if (sdev->removable || sdkp->write_prot)
Al Viro0338e292008-03-02 10:41:04 -0500811 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 /*
814 * If the drive is empty, just let the open fail.
815 */
816 retval = -ENOMEDIUM;
Al Viro0338e292008-03-02 10:41:04 -0500817 if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto error_out;
819
820 /*
821 * If the device has the write protect tab set, have the open fail
822 * if the user expects to be able to write to the thing.
823 */
824 retval = -EROFS;
Al Viro0338e292008-03-02 10:41:04 -0500825 if (sdkp->write_prot && (mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto error_out;
827
828 /*
829 * It is possible that the disk changing stuff resulted in
830 * the device being taken offline. If this is the case,
831 * report this to the user, and don't pretend that the
832 * open actually succeeded.
833 */
834 retval = -ENXIO;
835 if (!scsi_device_online(sdev))
836 goto error_out;
837
Arnd Bergmann409f3492010-07-07 16:51:29 +0200838 if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (scsi_block_when_processing_errors(sdev))
840 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
841 }
842
843 return 0;
844
845error_out:
846 scsi_disk_put(sdkp);
847 return retval;
848}
849
850/**
851 * sd_release - invoked when the (last) close(2) is called on this
852 * scsi disk.
853 * @inode: only i_rdev member may be used
854 * @filp: only f_mode and f_flags may be used
855 *
856 * Returns 0.
857 *
858 * Note: may block (uninterruptible) if error recovery is underway
859 * on this disk.
Arnd Bergmann409f3492010-07-07 16:51:29 +0200860 *
861 * Locking: called with bdev->bd_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 **/
Al Viro0338e292008-03-02 10:41:04 -0500863static int sd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 struct scsi_disk *sdkp = scsi_disk(disk);
866 struct scsi_device *sdev = sdkp->device;
867
James Bottomley56937f72007-03-11 12:25:33 -0500868 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Arnd Bergmann409f3492010-07-07 16:51:29 +0200870 if (atomic_dec_return(&sdkp->openers) && sdev->removable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (scsi_block_when_processing_errors(sdev))
872 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
873 }
874
875 /*
876 * XXX and what if there are packets in flight and this close()
877 * XXX is followed by a "rmmod sd_mod"?
878 */
879 scsi_disk_put(sdkp);
880 return 0;
881}
882
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800883static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
886 struct scsi_device *sdp = sdkp->device;
887 struct Scsi_Host *host = sdp->host;
888 int diskinfo[4];
889
890 /* default to most commonly used values */
891 diskinfo[0] = 0x40; /* 1 << 6 */
892 diskinfo[1] = 0x20; /* 1 << 5 */
893 diskinfo[2] = sdkp->capacity >> 11;
894
895 /* override with calculated, extended default, or driver values */
896 if (host->hostt->bios_param)
897 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
898 else
899 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
900
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800901 geo->heads = diskinfo[0];
902 geo->sectors = diskinfo[1];
903 geo->cylinders = diskinfo[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return 0;
905}
906
907/**
908 * sd_ioctl - process an ioctl
909 * @inode: only i_rdev/i_bdev members may be used
910 * @filp: only f_mode and f_flags may be used
911 * @cmd: ioctl command number
912 * @arg: this is third argument given to ioctl(2) system call.
913 * Often contains a pointer.
914 *
915 * Returns 0 if successful (some ioctls return postive numbers on
916 * success as well). Returns a negated errno value in case of error.
917 *
918 * Note: most ioctls are forward onto the block subsystem or further
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200919 * down in the scsi subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 **/
Al Viro0338e292008-03-02 10:41:04 -0500921static int sd_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 unsigned int cmd, unsigned long arg)
923{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct gendisk *disk = bdev->bd_disk;
925 struct scsi_device *sdp = scsi_disk(disk)->device;
926 void __user *p = (void __user *)arg;
927 int error;
928
929 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
930 disk->disk_name, cmd));
931
932 /*
933 * If we are in the middle of error recovery, don't let anyone
934 * else try and use this device. Also, if error recovery fails, it
935 * may try and take the device offline, in which case all further
936 * access to the device is prohibited.
937 */
Al Viro83ff6fe2008-03-02 08:15:49 -0500938 error = scsi_nonblockable_ioctl(sdp, cmd, p,
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +0100939 (mode & FMODE_NDELAY) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (!scsi_block_when_processing_errors(sdp) || !error)
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200941 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /*
944 * Send SCSI addressing ioctls directly to mid level, send other
945 * ioctls to block level and then onto mid level if they can't be
946 * resolved.
947 */
948 switch (cmd) {
949 case SCSI_IOCTL_GET_IDLUN:
950 case SCSI_IOCTL_GET_BUS_NUMBER:
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200951 error = scsi_ioctl(sdp, cmd, p);
952 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 default:
Al Viro0338e292008-03-02 10:41:04 -0500954 error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (error != -ENOTTY)
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200956 break;
957 error = scsi_ioctl(sdp, cmd, p);
958 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200960out:
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200961 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964static void set_media_not_present(struct scsi_disk *sdkp)
965{
966 sdkp->media_present = 0;
967 sdkp->capacity = 0;
968 sdkp->device->changed = 1;
969}
970
971/**
972 * sd_media_changed - check if our medium changed
973 * @disk: kernel device descriptor
974 *
975 * Returns 0 if not applicable or no change; 1 if change
976 *
977 * Note: this function is invoked from the block subsystem.
978 **/
979static int sd_media_changed(struct gendisk *disk)
980{
981 struct scsi_disk *sdkp = scsi_disk(disk);
982 struct scsi_device *sdp = sdkp->device;
James Bottomley001aac22007-12-02 19:10:40 +0200983 struct scsi_sense_hdr *sshdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 int retval;
985
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500986 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 if (!sdp->removable)
989 return 0;
990
991 /*
992 * If the device is offline, don't send any commands - just pretend as
993 * if the command failed. If the device ever comes back online, we
994 * can deal with it then. It is only because of unrecoverable errors
995 * that we would ever take a device offline in the first place.
996 */
Kay Sievers285e9672007-08-14 14:10:39 +0200997 if (!scsi_device_online(sdp)) {
998 set_media_not_present(sdkp);
999 retval = 1;
1000 goto out;
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /*
1004 * Using TEST_UNIT_READY enables differentiation between drive with
1005 * no cartridge loaded - NOT READY, drive with changed cartridge -
1006 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
1007 *
1008 * Drives that auto spin down. eg iomega jaz 1G, will be started
1009 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
1010 * sd_revalidate() is called.
1011 */
1012 retval = -ENODEV;
Kay Sievers285e9672007-08-14 14:10:39 +02001013
James Bottomley001aac22007-12-02 19:10:40 +02001014 if (scsi_block_when_processing_errors(sdp)) {
1015 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
1016 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
1017 sshdr);
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /*
1021 * Unable to test, unit probably not ready. This usually
1022 * means there is no disc in the drive. Mark as changed,
1023 * and we will figure it out later once the drive is
1024 * available again.
1025 */
James Bottomley001aac22007-12-02 19:10:40 +02001026 if (retval || (scsi_sense_valid(sshdr) &&
1027 /* 0x3a is medium not present */
1028 sshdr->asc == 0x3a)) {
Kay Sievers285e9672007-08-14 14:10:39 +02001029 set_media_not_present(sdkp);
1030 retval = 1;
1031 goto out;
1032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 /*
1035 * For removable scsi disk we have to recognise the presence
1036 * of a disk in the drive. This is kept in the struct scsi_disk
1037 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
1038 */
1039 sdkp->media_present = 1;
1040
1041 retval = sdp->changed;
1042 sdp->changed = 0;
Kay Sievers285e9672007-08-14 14:10:39 +02001043out:
1044 if (retval != sdkp->previous_state)
1045 sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
1046 sdkp->previous_state = retval;
James Bottomley001aac22007-12-02 19:10:40 +02001047 kfree(sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Martin K. Petersene73aec82007-02-27 22:40:55 -05001051static int sd_sync_cache(struct scsi_disk *sdkp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 int retries, res;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001054 struct scsi_device *sdp = sdkp->device;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001055 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (!scsi_device_online(sdp))
1058 return -ENODEV;
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 for (retries = 3; retries > 0; --retries) {
1062 unsigned char cmd[10] = { 0 };
1063
1064 cmd[0] = SYNCHRONIZE_CACHE;
1065 /*
1066 * Leave the rest of the command zero to indicate
1067 * flush everything.
1068 */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001069 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09001070 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
James Bottomleyea73a9f2005-08-28 11:33:52 -05001071 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 break;
1073 }
1074
Martin K. Petersene73aec82007-02-27 22:40:55 -05001075 if (res) {
1076 sd_print_result(sdkp, res);
1077 if (driver_byte(res) & DRIVER_SENSE)
1078 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
Tejun Heo37210502007-03-21 00:07:18 +09001081 if (res)
1082 return -EIO;
1083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086static void sd_rescan(struct device *dev)
1087{
Alan Stern39b7f1e2005-11-04 14:44:41 -05001088 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1089
1090 if (sdkp) {
Andrew Pattersonf98a8ca2008-09-04 14:27:35 -06001091 revalidate_disk(sdkp->disk);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001092 scsi_disk_put(sdkp);
1093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
1096
1097#ifdef CONFIG_COMPAT
1098/*
1099 * This gets directly called from VFS. When the ioctl
1100 * is not recognized we go back to the other translation paths.
1101 */
Al Viro0338e292008-03-02 10:41:04 -05001102static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
1103 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Al Viro0338e292008-03-02 10:41:04 -05001105 struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 /*
1108 * If we are in the middle of error recovery, don't let anyone
1109 * else try and use this device. Also, if error recovery fails, it
1110 * may try and take the device offline, in which case all further
1111 * access to the device is prohibited.
1112 */
1113 if (!scsi_block_when_processing_errors(sdev))
1114 return -ENODEV;
1115
1116 if (sdev->host->hostt->compat_ioctl) {
1117 int ret;
1118
1119 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
1120
1121 return ret;
1122 }
1123
1124 /*
1125 * Let the static ioctl translation table take care of it.
1126 */
1127 return -ENOIOCTLCMD;
1128}
1129#endif
1130
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001131static const struct block_device_operations sd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 .owner = THIS_MODULE,
Al Viro0338e292008-03-02 10:41:04 -05001133 .open = sd_open,
1134 .release = sd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001135 .ioctl = sd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001136 .getgeo = sd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137#ifdef CONFIG_COMPAT
Al Viro0338e292008-03-02 10:41:04 -05001138 .compat_ioctl = sd_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139#endif
1140 .media_changed = sd_media_changed,
1141 .revalidate_disk = sd_revalidate_disk,
Tejun Heo72ec24b2010-05-15 20:09:32 +02001142 .unlock_native_capacity = sd_unlock_native_capacity,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143};
1144
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001145static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1146{
Tejun Heo83096eb2009-05-07 22:24:39 +09001147 u64 start_lba = blk_rq_pos(scmd->request);
1148 u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001149 u64 bad_lba;
1150 int info_valid;
1151
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001152 if (scmd->request->cmd_type != REQ_TYPE_FS)
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001153 return 0;
1154
1155 info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
1156 SCSI_SENSE_BUFFERSIZE,
1157 &bad_lba);
1158 if (!info_valid)
1159 return 0;
1160
1161 if (scsi_bufflen(scmd) <= scmd->device->sector_size)
1162 return 0;
1163
1164 if (scmd->device->sector_size < 512) {
1165 /* only legitimate sector_size here is 256 */
1166 start_lba <<= 1;
1167 end_lba <<= 1;
1168 } else {
1169 /* be careful ... don't want any overflows */
1170 u64 factor = scmd->device->sector_size / 512;
1171 do_div(start_lba, factor);
1172 do_div(end_lba, factor);
1173 }
1174
1175 /* The bad lba was reported incorrectly, we have no idea where
1176 * the error is.
1177 */
1178 if (bad_lba < start_lba || bad_lba >= end_lba)
1179 return 0;
1180
1181 /* This computation should always be done in terms of
1182 * the resolution of the device's medium.
1183 */
1184 return (bad_lba - start_lba) * scmd->device->sector_size;
1185}
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187/**
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001188 * sd_done - bottom half handler: called when the lower level
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * driver has completed (successfully or otherwise) a scsi command.
1190 * @SCpnt: mid-level's per command structure.
1191 *
1192 * Note: potentially run from within an ISR. Must not block.
1193 **/
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001194static int sd_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 int result = SCpnt->result;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001197 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct scsi_sense_hdr sshdr;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001199 struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int sense_valid = 0;
1201 int sense_deferred = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 if (result) {
1204 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
1205 if (sense_valid)
1206 sense_deferred = scsi_sense_is_deferred(&sshdr);
1207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208#ifdef CONFIG_SCSI_LOGGING
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001209 SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (sense_valid) {
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001211 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001212 "sd_done: sb[respc,sk,asc,"
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001213 "ascq]=%x,%x,%x,%x\n",
1214 sshdr.response_code,
1215 sshdr.sense_key, sshdr.asc,
1216 sshdr.ascq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218#endif
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001219 if (driver_byte(result) != DRIVER_SENSE &&
1220 (!sense_valid || sense_deferred))
1221 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001223 switch (sshdr.sense_key) {
1224 case HARDWARE_ERROR:
1225 case MEDIUM_ERROR:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001226 good_bytes = sd_completed_bytes(SCpnt);
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001227 break;
1228 case RECOVERED_ERROR:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001229 good_bytes = scsi_bufflen(SCpnt);
1230 break;
Jamie Wellnitz10dab222008-09-11 21:39:36 -04001231 case NO_SENSE:
1232 /* This indicates a false check condition, so ignore it. An
1233 * unknown amount of data was transferred so treat it as an
1234 * error.
1235 */
1236 scsi_print_sense("sd", SCpnt);
1237 SCpnt->result = 0;
1238 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1239 break;
Martin K. Petersenfa4698f2010-01-18 18:43:18 -05001240 case ABORTED_COMMAND: /* DIF: Target detected corruption */
1241 case ILLEGAL_REQUEST: /* DIX: Host detected corruption */
1242 if (sshdr.asc == 0x10)
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001243 good_bytes = sd_completed_bytes(SCpnt);
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001244 break;
1245 default:
1246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001248 out:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001249 if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
1250 sd_dif_complete(SCpnt, good_bytes);
1251
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001252 if (scsi_host_dif_capable(sdkp->device->host, sdkp->protection_type)
Martin K. Petersen77c9cfc2010-01-20 02:20:43 -05001253 == SD_DIF_TYPE2_PROTECTION && SCpnt->cmnd != SCpnt->request->cmd) {
1254
1255 /* We have to print a failed command here as the
1256 * extended CDB gets freed before scsi_io_completion()
1257 * is called.
1258 */
1259 if (result)
1260 scsi_print_command(SCpnt);
1261
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001262 mempool_free(SCpnt->cmnd, sd_cdb_pool);
Martin K. Petersen77c9cfc2010-01-20 02:20:43 -05001263 SCpnt->cmnd = NULL;
1264 SCpnt->cmd_len = 0;
1265 }
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001266
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001267 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
James Bottomleyea73a9f2005-08-28 11:33:52 -05001270static int media_not_present(struct scsi_disk *sdkp,
1271 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
James Bottomleyea73a9f2005-08-28 11:33:52 -05001274 if (!scsi_sense_valid(sshdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return 0;
1276 /* not invoked for commands that could return deferred errors */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001277 if (sshdr->sense_key != NOT_READY &&
1278 sshdr->sense_key != UNIT_ATTENTION)
1279 return 0;
1280 if (sshdr->asc != 0x3A) /* medium not present */
1281 return 0;
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 set_media_not_present(sdkp);
1284 return 1;
1285}
1286
1287/*
1288 * spinup disk - called only in sd_revalidate_disk()
1289 */
1290static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001291sd_spinup_disk(struct scsi_disk *sdkp)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001292{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 unsigned char cmd[10];
Alan Stern4451e472005-07-12 10:45:17 -04001294 unsigned long spintime_expire = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 int retries, spintime;
1296 unsigned int the_result;
1297 struct scsi_sense_hdr sshdr;
1298 int sense_valid = 0;
1299
1300 spintime = 0;
1301
1302 /* Spin up drives, as required. Only do this at boot time */
1303 /* Spinup needs to be done for module loads too. */
1304 do {
1305 retries = 0;
1306
1307 do {
1308 cmd[0] = TEST_UNIT_READY;
1309 memset((void *) &cmd[1], 0, 9);
1310
James Bottomleyea73a9f2005-08-28 11:33:52 -05001311 the_result = scsi_execute_req(sdkp->device, cmd,
1312 DMA_NONE, NULL, 0,
1313 &sshdr, SD_TIMEOUT,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09001314 SD_MAX_RETRIES, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Alan Sternb4d38e32006-10-11 16:48:28 -04001316 /*
1317 * If the drive has indicated to us that it
1318 * doesn't have any media in it, don't bother
1319 * with any more polling.
1320 */
1321 if (media_not_present(sdkp, &sshdr))
1322 return;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (the_result)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001325 sense_valid = scsi_sense_valid(&sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 retries++;
1327 } while (retries < 3 &&
1328 (!scsi_status_is_good(the_result) ||
1329 ((driver_byte(the_result) & DRIVER_SENSE) &&
1330 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1333 /* no sense, TUR either succeeded or failed
1334 * with a status error */
Martin K. Petersene73aec82007-02-27 22:40:55 -05001335 if(!spintime && !scsi_status_is_good(the_result)) {
1336 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1337 sd_print_result(sdkp, the_result);
1338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 break;
1340 }
1341
1342 /*
1343 * The device does not want the automatic start to be issued.
1344 */
Matthew Wilcox33dd6f92009-02-20 06:53:48 -07001345 if (sdkp->device->no_start_on_add)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Matthew Wilcox33dd6f92009-02-20 06:53:48 -07001348 if (sense_valid && sshdr.sense_key == NOT_READY) {
1349 if (sshdr.asc == 4 && sshdr.ascq == 3)
1350 break; /* manual intervention required */
1351 if (sshdr.asc == 4 && sshdr.ascq == 0xb)
1352 break; /* standby */
1353 if (sshdr.asc == 4 && sshdr.ascq == 0xc)
1354 break; /* unavailable */
1355 /*
1356 * Issue command to spin up drive when not ready
1357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001359 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 cmd[0] = START_STOP;
1361 cmd[1] = 1; /* Return immediately */
1362 memset((void *) &cmd[2], 0, 8);
1363 cmd[4] = 1; /* Start spin cycle */
Stefan Richterd2886ea2008-05-11 00:34:07 +02001364 if (sdkp->device->start_stop_pwr_cond)
1365 cmd[4] |= 1 << 4;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001366 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1367 NULL, 0, &sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09001368 SD_TIMEOUT, SD_MAX_RETRIES,
1369 NULL);
Alan Stern4451e472005-07-12 10:45:17 -04001370 spintime_expire = jiffies + 100 * HZ;
1371 spintime = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 /* Wait 1 second for next try */
1374 msleep(1000);
1375 printk(".");
Alan Stern4451e472005-07-12 10:45:17 -04001376
1377 /*
1378 * Wait for USB flash devices with slow firmware.
1379 * Yes, this sense key/ASC combination shouldn't
1380 * occur here. It's characteristic of these devices.
1381 */
1382 } else if (sense_valid &&
1383 sshdr.sense_key == UNIT_ATTENTION &&
1384 sshdr.asc == 0x28) {
1385 if (!spintime) {
1386 spintime_expire = jiffies + 5 * HZ;
1387 spintime = 1;
1388 }
1389 /* Wait 1 second for next try */
1390 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 } else {
1392 /* we don't understand the sense code, so it's
1393 * probably pointless to loop */
1394 if(!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001395 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1396 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398 break;
1399 }
1400
Alan Stern4451e472005-07-12 10:45:17 -04001401 } while (spintime && time_before_eq(jiffies, spintime_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 if (spintime) {
1404 if (scsi_status_is_good(the_result))
1405 printk("ready\n");
1406 else
1407 printk("not responding...\n");
1408 }
1409}
1410
Martin K. Petersene0597d72008-07-17 04:28:34 -04001411
1412/*
1413 * Determine whether disk supports Data Integrity Field.
1414 */
1415void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
1416{
1417 struct scsi_device *sdp = sdkp->device;
1418 u8 type;
1419
1420 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -04001421 return;
1422
1423 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
1424
1425 if (type == sdkp->protection_type || !sdkp->first_scan)
1426 return;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001427
Martin K. Petersenbe922f42008-09-19 18:47:20 -04001428 sdkp->protection_type = type;
1429
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001430 if (type > SD_DIF_TYPE3_PROTECTION) {
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -04001431 sd_printk(KERN_ERR, sdkp, "formatted with unsupported " \
1432 "protection type %u. Disabling disk!\n", type);
1433 sdkp->capacity = 0;
1434 return;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001435 }
1436
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -04001437 if (scsi_host_dif_capable(sdp->host, type))
1438 sd_printk(KERN_NOTICE, sdkp,
1439 "Enabling DIF Type %u protection\n", type);
1440 else
1441 sd_printk(KERN_NOTICE, sdkp,
1442 "Disabling DIF Type %u protection\n", type);
Martin K. Petersene0597d72008-07-17 04:28:34 -04001443}
1444
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001445static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
1446 struct scsi_sense_hdr *sshdr, int sense_valid,
1447 int the_result)
1448{
1449 sd_print_result(sdkp, the_result);
1450 if (driver_byte(the_result) & DRIVER_SENSE)
1451 sd_print_sense_hdr(sdkp, sshdr);
1452 else
1453 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
1454
1455 /*
1456 * Set dirty bit for removable devices if not ready -
1457 * sometimes drives will not report this properly.
1458 */
1459 if (sdp->removable &&
1460 sense_valid && sshdr->sense_key == NOT_READY)
1461 sdp->changed = 1;
1462
1463 /*
1464 * We used to set media_present to 0 here to indicate no media
1465 * in the drive, but some drives fail read capacity even with
1466 * media present, so we can't do that.
1467 */
1468 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
1469}
1470
1471#define RC16_LEN 32
1472#if RC16_LEN > SD_BUF_SIZE
1473#error RC16_LEN must not be more than SD_BUF_SIZE
1474#endif
1475
James Bottomley3233ac12010-04-01 10:30:01 -04001476#define READ_CAPACITY_RETRIES_ON_RESET 10
1477
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001478static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
1479 unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 unsigned char cmd[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 struct scsi_sense_hdr sshdr;
1483 int sense_valid = 0;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001484 int the_result;
James Bottomley3233ac12010-04-01 10:30:01 -04001485 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001486 unsigned int alignment;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001487 unsigned long long lba;
1488 unsigned sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 do {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001491 memset(cmd, 0, 16);
1492 cmd[0] = SERVICE_ACTION_IN;
1493 cmd[1] = SAI_READ_CAPACITY_16;
1494 cmd[13] = RC16_LEN;
1495 memset(buffer, 0, RC16_LEN);
1496
James Bottomleyea73a9f2005-08-28 11:33:52 -05001497 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001498 buffer, RC16_LEN, &sshdr,
1499 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
James Bottomleyea73a9f2005-08-28 11:33:52 -05001501 if (media_not_present(sdkp, &sshdr))
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001502 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Matthew Wilcox2b301302009-03-12 14:20:30 -04001504 if (the_result) {
James Bottomleyea73a9f2005-08-28 11:33:52 -05001505 sense_valid = scsi_sense_valid(&sshdr);
Matthew Wilcox2b301302009-03-12 14:20:30 -04001506 if (sense_valid &&
1507 sshdr.sense_key == ILLEGAL_REQUEST &&
1508 (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
1509 sshdr.ascq == 0x00)
1510 /* Invalid Command Operation Code or
1511 * Invalid Field in CDB, just retry
1512 * silently with RC10 */
1513 return -EINVAL;
James Bottomley3233ac12010-04-01 10:30:01 -04001514 if (sense_valid &&
1515 sshdr.sense_key == UNIT_ATTENTION &&
1516 sshdr.asc == 0x29 && sshdr.ascq == 0x00)
1517 /* Device reset might occur several times,
1518 * give it one more chance */
1519 if (--reset_retries > 0)
1520 continue;
Matthew Wilcox2b301302009-03-12 14:20:30 -04001521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 retries--;
1523
1524 } while (the_result && retries);
1525
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001526 if (the_result) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001527 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001528 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1529 return -EINVAL;
1530 }
Martin K. Petersene73aec82007-02-27 22:40:55 -05001531
Dave Hansen8f76d152009-04-21 16:43:27 -07001532 sector_size = get_unaligned_be32(&buffer[8]);
1533 lba = get_unaligned_be64(&buffer[0]);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001534
1535 sd_read_protection_type(sdkp, buffer);
1536
1537 if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
1538 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1539 "kernel compiled with support for large block "
1540 "devices.\n");
1541 sdkp->capacity = 0;
1542 return -EOVERFLOW;
1543 }
1544
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001545 /* Logical blocks per physical block exponent */
1546 sdkp->hw_sector_size = (1 << (buffer[13] & 0xf)) * sector_size;
1547
1548 /* Lowest aligned logical block */
1549 alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
1550 blk_queue_alignment_offset(sdp->request_queue, alignment);
1551 if (alignment && sdkp->first_scan)
1552 sd_printk(KERN_NOTICE, sdkp,
1553 "physical block alignment offset: %u\n", alignment);
1554
Martin K. Petersene339c1a2009-11-26 12:00:40 -05001555 if (buffer[14] & 0x80) { /* TPE */
1556 struct request_queue *q = sdp->request_queue;
1557
1558 sdkp->thin_provisioning = 1;
1559 q->limits.discard_granularity = sdkp->hw_sector_size;
1560 q->limits.max_discard_sectors = 0xffffffff;
1561
1562 if (buffer[14] & 0x40) /* TPRZ */
1563 q->limits.discard_zeroes_data = 1;
1564
1565 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1566 }
1567
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001568 sdkp->capacity = lba + 1;
1569 return sector_size;
1570}
1571
1572static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
1573 unsigned char *buffer)
1574{
1575 unsigned char cmd[16];
1576 struct scsi_sense_hdr sshdr;
1577 int sense_valid = 0;
1578 int the_result;
James Bottomley3233ac12010-04-01 10:30:01 -04001579 int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001580 sector_t lba;
1581 unsigned sector_size;
1582
1583 do {
1584 cmd[0] = READ_CAPACITY;
1585 memset(&cmd[1], 0, 9);
1586 memset(buffer, 0, 8);
1587
1588 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1589 buffer, 8, &sshdr,
1590 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1591
1592 if (media_not_present(sdkp, &sshdr))
1593 return -ENODEV;
1594
James Bottomley3233ac12010-04-01 10:30:01 -04001595 if (the_result) {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001596 sense_valid = scsi_sense_valid(&sshdr);
James Bottomley3233ac12010-04-01 10:30:01 -04001597 if (sense_valid &&
1598 sshdr.sense_key == UNIT_ATTENTION &&
1599 sshdr.asc == 0x29 && sshdr.ascq == 0x00)
1600 /* Device reset might occur several times,
1601 * give it one more chance */
1602 if (--reset_retries > 0)
1603 continue;
1604 }
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001605 retries--;
1606
1607 } while (the_result && retries);
1608
1609 if (the_result) {
1610 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1611 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1612 return -EINVAL;
1613 }
1614
Dave Hansen8f76d152009-04-21 16:43:27 -07001615 sector_size = get_unaligned_be32(&buffer[4]);
1616 lba = get_unaligned_be32(&buffer[0]);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001617
1618 if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
1619 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1620 "kernel compiled with support for large block "
1621 "devices.\n");
1622 sdkp->capacity = 0;
1623 return -EOVERFLOW;
1624 }
1625
1626 sdkp->capacity = lba + 1;
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001627 sdkp->hw_sector_size = sector_size;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001628 return sector_size;
1629}
1630
Matthew Wilcox2b301302009-03-12 14:20:30 -04001631static int sd_try_rc16_first(struct scsi_device *sdp)
1632{
Hannes Reineckef87146b2010-03-29 09:29:24 +02001633 if (sdp->host->max_cmd_len < 16)
1634 return 0;
Matthew Wilcox2b301302009-03-12 14:20:30 -04001635 if (sdp->scsi_level > SCSI_SPC_2)
1636 return 1;
1637 if (scsi_device_protection(sdp))
1638 return 1;
1639 return 0;
1640}
1641
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001642/*
1643 * read disk capacity
1644 */
1645static void
1646sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
1647{
1648 int sector_size;
1649 struct scsi_device *sdp = sdkp->device;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001650 sector_t old_capacity = sdkp->capacity;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001651
Matthew Wilcox2b301302009-03-12 14:20:30 -04001652 if (sd_try_rc16_first(sdp)) {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001653 sector_size = read_capacity_16(sdkp, sdp, buffer);
1654 if (sector_size == -EOVERFLOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 goto got_data;
Matthew Wilcox2b301302009-03-12 14:20:30 -04001656 if (sector_size == -ENODEV)
1657 return;
1658 if (sector_size < 0)
1659 sector_size = read_capacity_10(sdkp, sdp, buffer);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001660 if (sector_size < 0)
1661 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 } else {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001663 sector_size = read_capacity_10(sdkp, sdp, buffer);
1664 if (sector_size == -EOVERFLOW)
1665 goto got_data;
1666 if (sector_size < 0)
1667 return;
1668 if ((sizeof(sdkp->capacity) > 4) &&
1669 (sdkp->capacity > 0xffffffffULL)) {
1670 int old_sector_size = sector_size;
1671 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1672 "Trying to use READ CAPACITY(16).\n");
1673 sector_size = read_capacity_16(sdkp, sdp, buffer);
1674 if (sector_size < 0) {
1675 sd_printk(KERN_NOTICE, sdkp,
1676 "Using 0xffffffff as device size\n");
1677 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1678 sector_size = old_sector_size;
1679 goto got_data;
1680 }
1681 }
1682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Alan Stern5c211ca2009-02-18 10:54:44 -05001684 /* Some devices are known to return the total number of blocks,
1685 * not the highest block number. Some devices have versions
1686 * which do this and others which do not. Some devices we might
1687 * suspect of doing this but we don't know for certain.
1688 *
1689 * If we know the reported capacity is wrong, decrement it. If
1690 * we can only guess, then assume the number of blocks is even
1691 * (usually true but not always) and err on the side of lowering
1692 * the capacity.
1693 */
1694 if (sdp->fix_capacity ||
1695 (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
1696 sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
1697 "from its reported value: %llu\n",
1698 (unsigned long long) sdkp->capacity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 --sdkp->capacity;
Oliver Neukum61bf54b2007-02-08 09:04:48 +01001700 }
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702got_data:
1703 if (sector_size == 0) {
1704 sector_size = 512;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001705 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
1706 "assuming 512.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
1708
1709 if (sector_size != 512 &&
1710 sector_size != 1024 &&
1711 sector_size != 2048 &&
1712 sector_size != 4096 &&
1713 sector_size != 256) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001714 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
1715 sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /*
1717 * The user might want to re-format the drive with
1718 * a supported sectorsize. Once this happens, it
1719 * would be relatively trivial to set the thing up.
1720 * For this reason, we leave the thing in the table.
1721 */
1722 sdkp->capacity = 0;
1723 /*
1724 * set a bogus sector size so the normal read/write
1725 * logic in the block layer will eventually refuse any
1726 * request on this device without tripping over power
1727 * of two sector size assumptions
1728 */
1729 sector_size = 512;
1730 }
Martin K. Petersene1defc42009-05-22 17:17:49 -04001731 blk_queue_logical_block_size(sdp->request_queue, sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
James Bottomley7404ad3b2008-08-31 10:41:52 -05001733 {
1734 char cap_str_2[10], cap_str_10[10];
H. Peter Anvin520a2c22008-10-14 11:34:20 -07001735 u64 sz = (u64)sdkp->capacity << ilog2(sector_size);
James Bottomley7404ad3b2008-08-31 10:41:52 -05001736
1737 string_get_size(sz, STRING_UNITS_2, cap_str_2,
1738 sizeof(cap_str_2));
1739 string_get_size(sz, STRING_UNITS_10, cap_str_10,
1740 sizeof(cap_str_10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001742 if (sdkp->first_scan || old_capacity != sdkp->capacity) {
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001743 sd_printk(KERN_NOTICE, sdkp,
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001744 "%llu %d-byte logical blocks: (%s/%s)\n",
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001745 (unsigned long long)sdkp->capacity,
1746 sector_size, cap_str_10, cap_str_2);
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001747
1748 if (sdkp->hw_sector_size != sector_size)
1749 sd_printk(KERN_NOTICE, sdkp,
1750 "%u-byte physical blocks\n",
1751 sdkp->hw_sector_size);
1752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
1755 /* Rescale capacity to 512-byte units */
1756 if (sector_size == 4096)
1757 sdkp->capacity <<= 3;
1758 else if (sector_size == 2048)
1759 sdkp->capacity <<= 2;
1760 else if (sector_size == 1024)
1761 sdkp->capacity <<= 1;
1762 else if (sector_size == 256)
1763 sdkp->capacity >>= 1;
1764
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001765 blk_queue_physical_block_size(sdp->request_queue, sdkp->hw_sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 sdkp->device->sector_size = sector_size;
1767}
1768
1769/* called with buffer of length 512 */
1770static inline int
James Bottomleyea73a9f2005-08-28 11:33:52 -05001771sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1772 unsigned char *buffer, int len, struct scsi_mode_data *data,
1773 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
James Bottomleyea73a9f2005-08-28 11:33:52 -05001775 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
James Bottomley1cf72692005-08-28 11:27:01 -05001776 SD_TIMEOUT, SD_MAX_RETRIES, data,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001777 sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
1780/*
1781 * read write protect setting, if possible - called only in sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06001782 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 */
1784static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001785sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001786{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 int res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001788 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct scsi_mode_data data;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001790 int old_wp = sdkp->write_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 set_disk_ro(sdkp->disk, 0);
James Bottomleyea73a9f2005-08-28 11:33:52 -05001793 if (sdp->skip_ms_page_3f) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001794 sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return;
1796 }
1797
James Bottomleyea73a9f2005-08-28 11:33:52 -05001798 if (sdp->use_192_bytes_for_3f) {
1799 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 } else {
1801 /*
1802 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1803 * We have to start carefully: some devices hang if we ask
1804 * for more than is available.
1805 */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001806 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 /*
1809 * Second attempt: ask for page 0 When only page 0 is
1810 * implemented, a request for page 3F may return Sense Key
1811 * 5: Illegal Request, Sense Code 24: Invalid field in
1812 * CDB.
1813 */
1814 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05001815 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 /*
1818 * Third attempt: ask 255 bytes, as we did earlier.
1819 */
1820 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05001821 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1822 &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824
1825 if (!scsi_status_is_good(res)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001826 sd_printk(KERN_WARNING, sdkp,
1827 "Test WP failed, assume Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 } else {
1829 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1830 set_disk_ro(sdkp->disk, sdkp->write_prot);
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001831 if (sdkp->first_scan || old_wp != sdkp->write_prot) {
1832 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
1833 sdkp->write_prot ? "on" : "off");
1834 sd_printk(KERN_DEBUG, sdkp,
1835 "Mode Sense: %02x %02x %02x %02x\n",
1836 buffer[0], buffer[1], buffer[2], buffer[3]);
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839}
1840
1841/*
1842 * sd_read_cache_type - called only from sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06001843 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 */
1845static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001846sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
Al Viro 631e8a12005-05-16 01:59:55 +01001847{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 int len = 0, res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001849 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Al Viro 631e8a12005-05-16 01:59:55 +01001851 int dbd;
1852 int modepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 struct scsi_mode_data data;
1854 struct scsi_sense_hdr sshdr;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001855 int old_wce = sdkp->WCE;
1856 int old_rcd = sdkp->RCD;
1857 int old_dpofua = sdkp->DPOFUA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
James Bottomleyea73a9f2005-08-28 11:33:52 -05001859 if (sdp->skip_ms_page_8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 goto defaults;
1861
James Bottomleyea73a9f2005-08-28 11:33:52 -05001862 if (sdp->type == TYPE_RBC) {
Al Viro 631e8a12005-05-16 01:59:55 +01001863 modepage = 6;
1864 dbd = 8;
1865 } else {
1866 modepage = 8;
1867 dbd = 0;
1868 }
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /* cautiously ask */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001871 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 if (!scsi_status_is_good(res))
1874 goto bad_sense;
1875
Al Viro6d73c852006-02-23 02:03:16 +01001876 if (!data.header_length) {
1877 modepage = 6;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001878 sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
Al Viro6d73c852006-02-23 02:03:16 +01001879 }
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /* that went OK, now ask for the proper length */
1882 len = data.length;
1883
1884 /*
1885 * We're only interested in the first three bytes, actually.
1886 * But the data cache page is defined for the first 20.
1887 */
1888 if (len < 3)
1889 goto bad_sense;
1890 if (len > 20)
1891 len = 20;
1892
1893 /* Take headers and block descriptors into account */
1894 len += data.header_length + data.block_descriptor_length;
Al Viro48970802006-02-26 08:34:10 -06001895 if (len > SD_BUF_SIZE)
1896 goto bad_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /* Get the data */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001899 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 if (scsi_status_is_good(res)) {
Al Viro 631e8a12005-05-16 01:59:55 +01001902 int offset = data.header_length + data.block_descriptor_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Al Viro48970802006-02-26 08:34:10 -06001904 if (offset >= SD_BUF_SIZE - 2) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001905 sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
Al Viro48970802006-02-26 08:34:10 -06001906 goto defaults;
1907 }
1908
Al Viro 631e8a12005-05-16 01:59:55 +01001909 if ((buffer[offset] & 0x3f) != modepage) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001910 sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
Al Viro 631e8a12005-05-16 01:59:55 +01001911 goto defaults;
1912 }
1913
1914 if (modepage == 8) {
1915 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1916 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1917 } else {
1918 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1919 sdkp->RCD = 0;
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Tejun Heo007365a2006-01-06 09:53:52 +01001922 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1923 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001924 sd_printk(KERN_NOTICE, sdkp,
1925 "Uses READ/WRITE(6), disabling FUA\n");
Tejun Heo007365a2006-01-06 09:53:52 +01001926 sdkp->DPOFUA = 0;
1927 }
1928
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001929 if (sdkp->first_scan || old_wce != sdkp->WCE ||
1930 old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
1931 sd_printk(KERN_NOTICE, sdkp,
1932 "Write cache: %s, read cache: %s, %s\n",
1933 sdkp->WCE ? "enabled" : "disabled",
1934 sdkp->RCD ? "disabled" : "enabled",
1935 sdkp->DPOFUA ? "supports DPO and FUA"
1936 : "doesn't support DPO or FUA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 return;
1939 }
1940
1941bad_sense:
James Bottomleyea73a9f2005-08-28 11:33:52 -05001942 if (scsi_sense_valid(&sshdr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 sshdr.sense_key == ILLEGAL_REQUEST &&
1944 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
Martin K. Petersene73aec82007-02-27 22:40:55 -05001945 /* Invalid field in CDB */
1946 sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 else
Martin K. Petersene73aec82007-02-27 22:40:55 -05001948 sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950defaults:
Martin K. Petersene73aec82007-02-27 22:40:55 -05001951 sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 sdkp->WCE = 0;
1953 sdkp->RCD = 0;
Al Viro48970802006-02-26 08:34:10 -06001954 sdkp->DPOFUA = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Martin K. Petersene0597d72008-07-17 04:28:34 -04001957/*
1958 * The ATO bit indicates whether the DIF application tag is available
1959 * for use by the operating system.
1960 */
1961void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
1962{
1963 int res, offset;
1964 struct scsi_device *sdp = sdkp->device;
1965 struct scsi_mode_data data;
1966 struct scsi_sense_hdr sshdr;
1967
1968 if (sdp->type != TYPE_DISK)
1969 return;
1970
1971 if (sdkp->protection_type == 0)
1972 return;
1973
1974 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
1975 SD_MAX_RETRIES, &data, &sshdr);
1976
1977 if (!scsi_status_is_good(res) || !data.header_length ||
1978 data.length < 6) {
1979 sd_printk(KERN_WARNING, sdkp,
1980 "getting Control mode page failed, assume no ATO\n");
1981
1982 if (scsi_sense_valid(&sshdr))
1983 sd_print_sense_hdr(sdkp, &sshdr);
1984
1985 return;
1986 }
1987
1988 offset = data.header_length + data.block_descriptor_length;
1989
1990 if ((buffer[offset] & 0x3f) != 0x0a) {
1991 sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
1992 return;
1993 }
1994
1995 if ((buffer[offset + 5] & 0x80) == 0)
1996 return;
1997
1998 sdkp->ATO = 1;
1999
2000 return;
2001}
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003/**
Martin K. Petersend11b6912009-05-23 11:43:39 -04002004 * sd_read_block_limits - Query disk device for preferred I/O sizes.
2005 * @disk: disk to query
2006 */
2007static void sd_read_block_limits(struct scsi_disk *sdkp)
2008{
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002009 struct request_queue *q = sdkp->disk->queue;
Martin K. Petersend11b6912009-05-23 11:43:39 -04002010 unsigned int sector_sz = sdkp->device->sector_size;
Martin K. Petersenbb2d3de2010-03-02 08:44:34 -05002011 const int vpd_len = 64;
James Bottomleye3deec02009-11-03 12:33:07 -06002012 unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
Martin K. Petersend11b6912009-05-23 11:43:39 -04002013
James Bottomleye3deec02009-11-03 12:33:07 -06002014 if (!buffer ||
2015 /* Block Limits VPD */
2016 scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
2017 goto out;
Martin K. Petersend11b6912009-05-23 11:43:39 -04002018
2019 blk_queue_io_min(sdkp->disk->queue,
2020 get_unaligned_be16(&buffer[6]) * sector_sz);
2021 blk_queue_io_opt(sdkp->disk->queue,
2022 get_unaligned_be32(&buffer[12]) * sector_sz);
2023
Martin K. Petersene339c1a2009-11-26 12:00:40 -05002024 /* Thin provisioning enabled and page length indicates TP support */
2025 if (sdkp->thin_provisioning && buffer[3] == 0x3c) {
2026 unsigned int lba_count, desc_count, granularity;
2027
2028 lba_count = get_unaligned_be32(&buffer[20]);
2029 desc_count = get_unaligned_be32(&buffer[24]);
2030
2031 if (lba_count) {
2032 q->limits.max_discard_sectors =
2033 lba_count * sector_sz >> 9;
2034
2035 if (desc_count)
2036 sdkp->unmap = 1;
2037 }
2038
2039 granularity = get_unaligned_be32(&buffer[28]);
2040
2041 if (granularity)
2042 q->limits.discard_granularity = granularity * sector_sz;
2043
2044 if (buffer[32] & 0x80)
2045 q->limits.discard_alignment =
2046 get_unaligned_be32(&buffer[32]) & ~(1 << 31);
2047 }
2048
James Bottomleye3deec02009-11-03 12:33:07 -06002049 out:
Martin K. Petersend11b6912009-05-23 11:43:39 -04002050 kfree(buffer);
2051}
2052
2053/**
Martin K. Petersen3821d762009-05-23 11:43:38 -04002054 * sd_read_block_characteristics - Query block dev. characteristics
2055 * @disk: disk to query
2056 */
2057static void sd_read_block_characteristics(struct scsi_disk *sdkp)
2058{
James Bottomleye3deec02009-11-03 12:33:07 -06002059 unsigned char *buffer;
Martin K. Petersen3821d762009-05-23 11:43:38 -04002060 u16 rot;
Martin K. Petersenbb2d3de2010-03-02 08:44:34 -05002061 const int vpd_len = 64;
Martin K. Petersen3821d762009-05-23 11:43:38 -04002062
James Bottomleye3deec02009-11-03 12:33:07 -06002063 buffer = kmalloc(vpd_len, GFP_KERNEL);
Martin K. Petersen3821d762009-05-23 11:43:38 -04002064
James Bottomleye3deec02009-11-03 12:33:07 -06002065 if (!buffer ||
2066 /* Block Device Characteristics VPD */
2067 scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len))
2068 goto out;
Martin K. Petersen3821d762009-05-23 11:43:38 -04002069
2070 rot = get_unaligned_be16(&buffer[4]);
2071
2072 if (rot == 1)
2073 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
2074
James Bottomleye3deec02009-11-03 12:33:07 -06002075 out:
Martin K. Petersen3821d762009-05-23 11:43:38 -04002076 kfree(buffer);
2077}
2078
Martin K. Petersenffd4bc22009-07-29 14:06:53 -04002079static int sd_try_extended_inquiry(struct scsi_device *sdp)
2080{
2081 /*
2082 * Although VPD inquiries can go to SCSI-2 type devices,
2083 * some USB ones crash on receiving them, and the pages
2084 * we currently ask for are for SPC-3 and beyond
2085 */
2086 if (sdp->scsi_level > SCSI_SPC_2)
2087 return 1;
2088 return 0;
2089}
2090
Martin K. Petersen3821d762009-05-23 11:43:38 -04002091/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 * sd_revalidate_disk - called the first time a new disk is seen,
2093 * performs disk spin up, read_capacity, etc.
2094 * @disk: struct gendisk we care about
2095 **/
2096static int sd_revalidate_disk(struct gendisk *disk)
2097{
2098 struct scsi_disk *sdkp = scsi_disk(disk);
2099 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 unsigned char *buffer;
Tejun Heo461d4e92006-01-06 09:52:55 +01002101 unsigned ordered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05002103 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
2104 "sd_revalidate_disk\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 /*
2107 * If the device is offline, don't try and read capacity or any
2108 * of the other niceties.
2109 */
2110 if (!scsi_device_online(sdp))
2111 goto out;
2112
Bernhard Wallea6123f12007-05-21 17:15:26 +02002113 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 if (!buffer) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002115 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
2116 "allocation failure.\n");
James Bottomleyea73a9f2005-08-28 11:33:52 -05002117 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119
Martin K. Petersene73aec82007-02-27 22:40:55 -05002120 sd_spinup_disk(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 /*
2123 * Without media there is no reason to ask; moreover, some devices
2124 * react badly if we do.
2125 */
2126 if (sdkp->media_present) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002127 sd_read_capacity(sdkp, buffer);
Martin K. Petersenffd4bc22009-07-29 14:06:53 -04002128
2129 if (sd_try_extended_inquiry(sdp)) {
2130 sd_read_block_limits(sdkp);
2131 sd_read_block_characteristics(sdkp);
2132 }
2133
Martin K. Petersene73aec82007-02-27 22:40:55 -05002134 sd_read_write_protect_flag(sdkp, buffer);
2135 sd_read_cache_type(sdkp, buffer);
Martin K. Petersene0597d72008-07-17 04:28:34 -04002136 sd_read_app_tag_own(sdkp, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
Tejun Heo461d4e92006-01-06 09:52:55 +01002138
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002139 sdkp->first_scan = 0;
2140
Tejun Heo461d4e92006-01-06 09:52:55 +01002141 /*
2142 * We now have all cache related info, determine how we deal
2143 * with ordered requests. Note that as the current SCSI
2144 * dispatch function can alter request order, we cannot use
2145 * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
2146 */
2147 if (sdkp->WCE)
Tejun Heo007365a2006-01-06 09:53:52 +01002148 ordered = sdkp->DPOFUA
2149 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
Tejun Heo461d4e92006-01-06 09:52:55 +01002150 else
2151 ordered = QUEUE_ORDERED_DRAIN;
2152
FUJITA Tomonori00fff262010-07-03 17:45:40 +09002153 blk_queue_ordered(sdkp->disk->queue, ordered);
Tejun Heo461d4e92006-01-06 09:52:55 +01002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 set_capacity(disk, sdkp->capacity);
2156 kfree(buffer);
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 out:
2159 return 0;
2160}
2161
2162/**
Tejun Heo72ec24b2010-05-15 20:09:32 +02002163 * sd_unlock_native_capacity - unlock native capacity
2164 * @disk: struct gendisk to set capacity for
2165 *
2166 * Block layer calls this function if it detects that partitions
2167 * on @disk reach beyond the end of the device. If the SCSI host
2168 * implements ->unlock_native_capacity() method, it's invoked to
2169 * give it a chance to adjust the device capacity.
2170 *
2171 * CONTEXT:
2172 * Defined by block layer. Might sleep.
2173 */
2174static void sd_unlock_native_capacity(struct gendisk *disk)
2175{
2176 struct scsi_device *sdev = scsi_disk(disk)->device;
2177
2178 if (sdev->host->hostt->unlock_native_capacity)
2179 sdev->host->hostt->unlock_native_capacity(sdev);
2180}
2181
2182/**
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002183 * sd_format_disk_name - format disk name
2184 * @prefix: name prefix - ie. "sd" for SCSI disks
2185 * @index: index of the disk to format name for
2186 * @buf: output buffer
2187 * @buflen: length of the output buffer
2188 *
2189 * SCSI disk names starts at sda. The 26th device is sdz and the
2190 * 27th is sdaa. The last one for two lettered suffix is sdzz
2191 * which is followed by sdaaa.
2192 *
2193 * This is basically 26 base counting with one extra 'nil' entry
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08002194 * at the beginning from the second digit on and can be
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002195 * determined using similar method as 26 base conversion with the
2196 * index shifted -1 after each digit is computed.
2197 *
2198 * CONTEXT:
2199 * Don't care.
2200 *
2201 * RETURNS:
2202 * 0 on success, -errno on failure.
2203 */
2204static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
2205{
2206 const int base = 'z' - 'a' + 1;
2207 char *begin = buf + strlen(prefix);
2208 char *end = buf + buflen;
2209 char *p;
2210 int unit;
2211
2212 p = end - 1;
2213 *p = '\0';
2214 unit = base;
2215 do {
2216 if (p == begin)
2217 return -EINVAL;
2218 *--p = 'a' + (index % unit);
2219 index = (index / unit) - 1;
2220 } while (index >= 0);
2221
2222 memmove(begin, p, end - p);
2223 memcpy(buf, prefix, strlen(prefix));
2224
2225 return 0;
2226}
2227
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002228/*
2229 * The asynchronous part of sd_probe
2230 */
2231static void sd_probe_async(void *data, async_cookie_t cookie)
2232{
2233 struct scsi_disk *sdkp = data;
2234 struct scsi_device *sdp;
2235 struct gendisk *gd;
2236 u32 index;
2237 struct device *dev;
2238
2239 sdp = sdkp->device;
2240 gd = sdkp->disk;
2241 index = sdkp->index;
2242 dev = &sdp->sdev_gendev;
2243
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002244 if (index < SD_MAX_DISKS) {
2245 gd->major = sd_major((index & 0xf0) >> 4);
2246 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
2247 gd->minors = SD_MINORS;
2248 }
2249 gd->fops = &sd_fops;
2250 gd->private_data = &sdkp->driver;
2251 gd->queue = sdkp->device->request_queue;
2252
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002253 /* defaults, until the device tells us otherwise */
2254 sdp->sector_size = 512;
2255 sdkp->capacity = 0;
2256 sdkp->media_present = 1;
2257 sdkp->write_prot = 0;
2258 sdkp->WCE = 0;
2259 sdkp->RCD = 0;
2260 sdkp->ATO = 0;
2261 sdkp->first_scan = 1;
2262
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002263 sd_revalidate_disk(gd);
2264
2265 blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
FUJITA Tomonorif1126e92010-07-01 19:49:18 +09002266 blk_queue_unprep_rq(sdp->request_queue, sd_unprep_fn);
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002267
2268 gd->driverfs_dev = &sdp->sdev_gendev;
NeilBrown97fedbb2010-03-16 08:55:32 +01002269 gd->flags = GENHD_FL_EXT_DEVT;
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002270 if (sdp->removable)
2271 gd->flags |= GENHD_FL_REMOVABLE;
2272
2273 dev_set_drvdata(dev, sdkp);
2274 add_disk(gd);
2275 sd_dif_config_host(sdkp);
2276
Martin K. Petersen3821d762009-05-23 11:43:38 -04002277 sd_revalidate_disk(gd);
2278
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002279 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
2280 sdp->removable ? "removable " : "");
James Bottomleyea038f62009-08-21 09:47:54 -06002281 put_device(&sdkp->dev);
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002282}
2283
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002284/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 * sd_probe - called during driver initialization and whenever a
2286 * new scsi device is attached to the system. It is called once
2287 * for each scsi device (not just disks) present.
2288 * @dev: pointer to device object
2289 *
2290 * Returns 0 if successful (or not interested in this scsi device
2291 * (e.g. scanner)); 1 when there is an error.
2292 *
2293 * Note: this function is invoked from the scsi mid-level.
2294 * This function sets up the mapping between a given
2295 * <host,channel,id,lun> (found in sdp) and new device name
2296 * (e.g. /dev/sda). More precisely it is the block device major
2297 * and minor number that is chosen here.
2298 *
2299 * Assume sd_attach is not re-entrant (for time being)
2300 * Also think about sd_attach() and sd_remove() running coincidentally.
2301 **/
2302static int sd_probe(struct device *dev)
2303{
2304 struct scsi_device *sdp = to_scsi_device(dev);
2305 struct scsi_disk *sdkp;
2306 struct gendisk *gd;
2307 u32 index;
2308 int error;
2309
2310 error = -ENODEV;
Al Viro 631e8a12005-05-16 01:59:55 +01002311 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 goto out;
2313
James Bottomley9ccfc752005-10-02 11:45:08 -05002314 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
2315 "sd_attach\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -05002318 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (!sdkp)
2320 goto out;
2321
Tejun Heo689d6fa2008-08-25 19:56:16 +09002322 gd = alloc_disk(SD_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (!gd)
2324 goto out_free;
2325
Tejun Heof27bac22008-07-14 14:59:30 +09002326 do {
2327 if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
2328 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Tejun Heo4034cc62009-02-21 11:04:45 +09002330 spin_lock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09002331 error = ida_get_new(&sd_index_ida, &index);
Tejun Heo4034cc62009-02-21 11:04:45 +09002332 spin_unlock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09002333 } while (error == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (error)
2336 goto out_put;
2337
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002338 error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
2339 if (error)
Tejun Heof27bac22008-07-14 14:59:30 +09002340 goto out_free_index;
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 sdkp->device = sdp;
2343 sdkp->driver = &sd_template;
2344 sdkp->disk = gd;
2345 sdkp->index = index;
Arnd Bergmann409f3492010-07-07 16:51:29 +02002346 atomic_set(&sdkp->openers, 0);
Kay Sieversc02e6002008-03-19 13:09:56 +01002347 sdkp->previous_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
James Bottomley601e7632009-05-26 20:35:48 +00002349 if (!sdp->request_queue->rq_timeout) {
2350 if (sdp->type != TYPE_MOD)
2351 blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
2352 else
2353 blk_queue_rq_timeout(sdp->request_queue,
2354 SD_MOD_TIMEOUT);
2355 }
2356
2357 device_initialize(&sdkp->dev);
2358 sdkp->dev.parent = &sdp->sdev_gendev;
2359 sdkp->dev.class = &sd_disk_class;
2360 dev_set_name(&sdkp->dev, dev_name(&sdp->sdev_gendev));
2361
2362 if (device_add(&sdkp->dev))
2363 goto out_free_index;
2364
2365 get_device(&sdp->sdev_gendev);
2366
James Bottomleyea038f62009-08-21 09:47:54 -06002367 get_device(&sdkp->dev); /* prevent release before async_schedule */
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002368 async_schedule(sd_probe_async, sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 return 0;
2371
Tejun Heof27bac22008-07-14 14:59:30 +09002372 out_free_index:
Tejun Heo4034cc62009-02-21 11:04:45 +09002373 spin_lock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09002374 ida_remove(&sd_index_ida, index);
Tejun Heo4034cc62009-02-21 11:04:45 +09002375 spin_unlock(&sd_index_lock);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06002376 out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 put_disk(gd);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06002378 out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 kfree(sdkp);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06002380 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return error;
2382}
2383
2384/**
2385 * sd_remove - called whenever a scsi disk (previously recognized by
2386 * sd_probe) is detached from the system. It is called (potentially
2387 * multiple times) during sd module unload.
2388 * @sdp: pointer to mid level scsi device object
2389 *
2390 * Note: this function is invoked from the scsi mid-level.
2391 * This function potentially frees up a device name (e.g. /dev/sdc)
2392 * that could be re-used by a subsequent sd_probe().
2393 * This function is not called when the built-in sd driver is "exit-ed".
2394 **/
2395static int sd_remove(struct device *dev)
2396{
James Bottomley601e7632009-05-26 20:35:48 +00002397 struct scsi_disk *sdkp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
James Bottomley601e7632009-05-26 20:35:48 +00002399 async_synchronize_full();
2400 sdkp = dev_get_drvdata(dev);
Hannes Reineckeb3912772009-06-18 09:57:18 +02002401 blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
FUJITA Tomonori82b6d572010-07-03 08:07:04 -06002402 blk_queue_unprep_rq(sdkp->device->request_queue, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002403 device_del(&sdkp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 del_gendisk(sdkp->disk);
2405 sd_shutdown(dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -05002406
Arjan van de Ven0b950672006-01-11 13:16:10 +01002407 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -05002408 dev_set_drvdata(dev, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002409 put_device(&sdkp->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002410 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412 return 0;
2413}
2414
2415/**
2416 * scsi_disk_release - Called to free the scsi_disk structure
Tony Jonesee959b02008-02-22 00:13:36 +01002417 * @dev: pointer to embedded class device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 *
Arjan van de Ven0b950672006-01-11 13:16:10 +01002419 * sd_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 * called on last put, you should always use the scsi_disk_get()
2421 * scsi_disk_put() helpers which manipulate the semaphore directly
Tony Jonesee959b02008-02-22 00:13:36 +01002422 * and never do a direct put_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002424static void scsi_disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Tony Jonesee959b02008-02-22 00:13:36 +01002426 struct scsi_disk *sdkp = to_scsi_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 struct gendisk *disk = sdkp->disk;
2428
Tejun Heo4034cc62009-02-21 11:04:45 +09002429 spin_lock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09002430 ida_remove(&sd_index_ida, sdkp->index);
Tejun Heo4034cc62009-02-21 11:04:45 +09002431 spin_unlock(&sd_index_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 disk->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 put_disk(disk);
Alan Stern39b7f1e2005-11-04 14:44:41 -05002435 put_device(&sdkp->device->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 kfree(sdkp);
2438}
2439
James Bottomleycc5d2c82007-03-20 12:26:03 -05002440static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
Tejun Heoc3c94c52007-03-21 00:13:59 +09002441{
2442 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
2443 struct scsi_sense_hdr sshdr;
James Bottomleycc5d2c82007-03-20 12:26:03 -05002444 struct scsi_device *sdp = sdkp->device;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002445 int res;
2446
2447 if (start)
2448 cmd[4] |= 1; /* START */
2449
Stefan Richterd2886ea2008-05-11 00:34:07 +02002450 if (sdp->start_stop_pwr_cond)
2451 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
2452
Tejun Heoc3c94c52007-03-21 00:13:59 +09002453 if (!scsi_device_online(sdp))
2454 return -ENODEV;
2455
2456 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002457 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
Tejun Heoc3c94c52007-03-21 00:13:59 +09002458 if (res) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05002459 sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
2460 sd_print_result(sdkp, res);
Tejun Heoc3c94c52007-03-21 00:13:59 +09002461 if (driver_byte(res) & DRIVER_SENSE)
James Bottomleycc5d2c82007-03-20 12:26:03 -05002462 sd_print_sense_hdr(sdkp, &sshdr);
Tejun Heoc3c94c52007-03-21 00:13:59 +09002463 }
2464
2465 return res;
2466}
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468/*
2469 * Send a SYNCHRONIZE CACHE instruction down to the device through
2470 * the normal SCSI command structure. Wait for the command to
2471 * complete.
2472 */
2473static void sd_shutdown(struct device *dev)
2474{
Alan Stern39b7f1e2005-11-04 14:44:41 -05002475 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 if (!sdkp)
2478 return; /* this can happen */
2479
Alan Stern39b7f1e2005-11-04 14:44:41 -05002480 if (sdkp->WCE) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002481 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
2482 sd_sync_cache(sdkp);
Alan Stern39b7f1e2005-11-04 14:44:41 -05002483 }
Tejun Heoc3c94c52007-03-21 00:13:59 +09002484
James Bottomleycc5d2c82007-03-20 12:26:03 -05002485 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
2486 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2487 sd_start_stop_device(sdkp, 0);
Tejun Heoc3c94c52007-03-21 00:13:59 +09002488 }
2489
Alan Stern39b7f1e2005-11-04 14:44:41 -05002490 scsi_disk_put(sdkp);
2491}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Tejun Heoc3c94c52007-03-21 00:13:59 +09002493static int sd_suspend(struct device *dev, pm_message_t mesg)
2494{
Tejun Heoc3c94c52007-03-21 00:13:59 +09002495 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Alan Stern09ff92f2007-05-21 09:55:04 -04002496 int ret = 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002497
2498 if (!sdkp)
2499 return 0; /* this can happen */
2500
2501 if (sdkp->WCE) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05002502 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
Tejun Heoc3c94c52007-03-21 00:13:59 +09002503 ret = sd_sync_cache(sdkp);
2504 if (ret)
Alan Stern09ff92f2007-05-21 09:55:04 -04002505 goto done;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002506 }
2507
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01002508 if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05002509 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2510 ret = sd_start_stop_device(sdkp, 0);
Tejun Heoc3c94c52007-03-21 00:13:59 +09002511 }
2512
Alan Stern09ff92f2007-05-21 09:55:04 -04002513done:
2514 scsi_disk_put(sdkp);
2515 return ret;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002516}
2517
2518static int sd_resume(struct device *dev)
2519{
Tejun Heoc3c94c52007-03-21 00:13:59 +09002520 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Alan Stern09ff92f2007-05-21 09:55:04 -04002521 int ret = 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002522
James Bottomleycc5d2c82007-03-20 12:26:03 -05002523 if (!sdkp->device->manage_start_stop)
Alan Stern09ff92f2007-05-21 09:55:04 -04002524 goto done;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002525
James Bottomleycc5d2c82007-03-20 12:26:03 -05002526 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
Alan Stern09ff92f2007-05-21 09:55:04 -04002527 ret = sd_start_stop_device(sdkp, 1);
Tejun Heoc3c94c52007-03-21 00:13:59 +09002528
Alan Stern09ff92f2007-05-21 09:55:04 -04002529done:
2530 scsi_disk_put(sdkp);
2531 return ret;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002532}
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534/**
2535 * init_sd - entry point for this driver (both when built in or when
2536 * a module).
2537 *
2538 * Note: this function registers this driver with the scsi mid-level.
2539 **/
2540static int __init init_sd(void)
2541{
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002542 int majors = 0, i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
2545
2546 for (i = 0; i < SD_MAJORS; i++)
2547 if (register_blkdev(sd_major(i), "sd") == 0)
2548 majors++;
2549
2550 if (!majors)
2551 return -ENODEV;
2552
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002553 err = class_register(&sd_disk_class);
2554 if (err)
2555 goto err_out;
James Bottomley6bdaa1f2006-03-18 14:14:21 -06002556
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002557 err = scsi_register_driver(&sd_template.gendrv);
2558 if (err)
2559 goto err_out_class;
2560
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04002561 sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
2562 0, 0, NULL);
2563 if (!sd_cdb_cache) {
2564 printk(KERN_ERR "sd: can't init extended cdb cache\n");
2565 goto err_out_class;
2566 }
2567
2568 sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
2569 if (!sd_cdb_pool) {
2570 printk(KERN_ERR "sd: can't init extended cdb pool\n");
2571 goto err_out_cache;
2572 }
2573
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002574 return 0;
2575
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04002576err_out_cache:
2577 kmem_cache_destroy(sd_cdb_cache);
2578
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002579err_out_class:
2580 class_unregister(&sd_disk_class);
2581err_out:
2582 for (i = 0; i < SD_MAJORS; i++)
2583 unregister_blkdev(sd_major(i), "sd");
2584 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586
2587/**
2588 * exit_sd - exit point for this driver (when it is a module).
2589 *
2590 * Note: this function unregisters this driver from the scsi mid-level.
2591 **/
2592static void __exit exit_sd(void)
2593{
2594 int i;
2595
2596 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
2597
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04002598 mempool_destroy(sd_cdb_pool);
2599 kmem_cache_destroy(sd_cdb_cache);
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 scsi_unregister_driver(&sd_template.gendrv);
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002602 class_unregister(&sd_disk_class);
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 for (i = 0; i < SD_MAJORS; i++)
2605 unregister_blkdev(sd_major(i), "sd");
2606}
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608module_init(init_sd);
2609module_exit(exit_sd);
Martin K. Petersene73aec82007-02-27 22:40:55 -05002610
2611static void sd_print_sense_hdr(struct scsi_disk *sdkp,
2612 struct scsi_sense_hdr *sshdr)
2613{
2614 sd_printk(KERN_INFO, sdkp, "");
2615 scsi_show_sense_hdr(sshdr);
2616 sd_printk(KERN_INFO, sdkp, "");
2617 scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
2618}
2619
2620static void sd_print_result(struct scsi_disk *sdkp, int result)
2621{
2622 sd_printk(KERN_INFO, sdkp, "");
2623 scsi_show_result(result);
2624}
2625