blob: 908d400b601a2e773d3b16db3e4386dc5273975f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sd.c Copyright (C) 1992 Drew Eckhardt
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4 *
5 * Linux scsi disk driver
6 * Initial versions: Drew Eckhardt
7 * Subsequent revisions: Eric Youngdale
8 * Modification history:
9 * - Drew Eckhardt <drew@colorado.edu> original
10 * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple
11 * outstanding request, and other enhancements.
12 * Support loadable low-level scsi drivers.
13 * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using
14 * eight major numbers.
15 * - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
16 * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in
17 * sd_init and cleanups.
18 * - Alex Davis <letmein@erols.com> Fix problem where partition info
19 * not being read in sd_open. Fix problem where removable media
20 * could be ejected after sd_open.
21 * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
22 * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox
23 * <willy@debian.org>, Kurt Garloff <garloff@suse.de>:
24 * Support 32k/1M disks.
25 *
26 * Logging policy (needs CONFIG_SCSI_LOGGING defined):
27 * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
28 * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
29 * - entering sd_ioctl: SCSI_LOG_IOCTL level 1
30 * - entering other commands: SCSI_LOG_HLQUEUE level 3
31 * Note: when the logging level is set by the user, it must be greater
32 * than the level indicated above to trigger output.
33 */
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/fs.h>
37#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/mm.h>
39#include <linux/bio.h>
40#include <linux/genhd.h>
41#include <linux/hdreg.h>
42#include <linux/errno.h>
43#include <linux/idr.h>
44#include <linux/interrupt.h>
45#include <linux/init.h>
46#include <linux/blkdev.h>
47#include <linux/blkpg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/delay.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010049#include <linux/mutex.h>
James Bottomley7404ad3b2008-08-31 10:41:52 -050050#include <linux/string_helpers.h>
Arjan van de Ven4ace92f2009-01-04 05:32:28 -080051#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/uaccess.h>
Dave Hansen8f76d152009-04-21 16:43:27 -070053#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include <scsi/scsi.h>
56#include <scsi/scsi_cmnd.h>
57#include <scsi/scsi_dbg.h>
58#include <scsi/scsi_device.h>
59#include <scsi/scsi_driver.h>
60#include <scsi/scsi_eh.h>
61#include <scsi/scsi_host.h>
62#include <scsi/scsi_ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <scsi/scsicam.h>
64
Martin K. Petersenaa916962008-06-17 12:47:32 -040065#include "sd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include "scsi_logging.h"
67
Rene Hermanf018fa52006-03-08 00:14:20 -080068MODULE_AUTHOR("Eric Youngdale");
69MODULE_DESCRIPTION("SCSI disk (sd) driver");
70MODULE_LICENSE("GPL");
71
72MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
73MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
74MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
75MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
76MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
77MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
78MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
79MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
80MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
81MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
82MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
83MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
84MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
85MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
86MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
87MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
Michael Tokarevd7b8bcb02006-10-27 16:02:37 +040088MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
89MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
90MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
Rene Hermanf018fa52006-03-08 00:14:20 -080091
Tejun Heo870d6652008-08-25 19:47:25 +090092#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
Tejun Heof615b482008-08-25 19:47:24 +090093#define SD_MINORS 16
Tejun Heo870d6652008-08-25 19:47:25 +090094#else
Tejun Heo3e1a7ff2008-08-25 19:56:17 +090095#define SD_MINORS 0
Tejun Heo870d6652008-08-25 19:47:25 +090096#endif
97
Linus Torvalds7b3d9542008-01-06 10:17:12 -080098static int sd_revalidate_disk(struct gendisk *);
99static int sd_probe(struct device *);
100static int sd_remove(struct device *);
101static void sd_shutdown(struct device *);
102static int sd_suspend(struct device *, pm_message_t state);
103static int sd_resume(struct device *);
104static void sd_rescan(struct device *);
105static int sd_done(struct scsi_cmnd *);
106static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
Tony Jonesee959b02008-02-22 00:13:36 +0100107static void scsi_disk_release(struct device *cdev);
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800108static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
109static void sd_print_result(struct scsi_disk *, int);
110
Tejun Heo4034cc62009-02-21 11:04:45 +0900111static DEFINE_SPINLOCK(sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +0900112static DEFINE_IDA(sd_index_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/* This semaphore is used to mediate the 0->1 reference get in the
115 * face of object destruction (i.e. we can't allow a get on an
116 * object after last put) */
Arjan van de Ven0b950672006-01-11 13:16:10 +0100117static DEFINE_MUTEX(sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400119struct kmem_cache *sd_cdb_cache;
120mempool_t *sd_cdb_pool;
121
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600122static const char *sd_cache_types[] = {
123 "write through", "none", "write back",
124 "write back, no read (daft)"
125};
126
Tony Jonesee959b02008-02-22 00:13:36 +0100127static ssize_t
128sd_store_cache_type(struct device *dev, struct device_attribute *attr,
129 const char *buf, size_t count)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600130{
131 int i, ct = -1, rcd, wce, sp;
Tony Jonesee959b02008-02-22 00:13:36 +0100132 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600133 struct scsi_device *sdp = sdkp->device;
134 char buffer[64];
135 char *buffer_data;
136 struct scsi_mode_data data;
137 struct scsi_sense_hdr sshdr;
138 int len;
139
140 if (sdp->type != TYPE_DISK)
141 /* no cache control on RBC devices; theoretically they
142 * can do it, but there's probably so many exceptions
143 * it's not worth the risk */
144 return -EINVAL;
145
Tobias Klauser6391a112006-06-08 22:23:48 -0700146 for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600147 const int len = strlen(sd_cache_types[i]);
148 if (strncmp(sd_cache_types[i], buf, len) == 0 &&
149 buf[len] == '\n') {
150 ct = i;
151 break;
152 }
153 }
154 if (ct < 0)
155 return -EINVAL;
156 rcd = ct & 0x01 ? 1 : 0;
157 wce = ct & 0x02 ? 1 : 0;
158 if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
159 SD_MAX_RETRIES, &data, NULL))
160 return -EINVAL;
Andrew Mortona9312fb2006-03-25 03:08:30 -0800161 len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600162 data.block_descriptor_length);
163 buffer_data = buffer + data.header_length +
164 data.block_descriptor_length;
165 buffer_data[2] &= ~0x05;
166 buffer_data[2] |= wce << 2 | rcd;
167 sp = buffer_data[0] & 0x80 ? 1 : 0;
168
169 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
170 SD_MAX_RETRIES, &data, &sshdr)) {
171 if (scsi_sense_valid(&sshdr))
Martin K. Petersene73aec82007-02-27 22:40:55 -0500172 sd_print_sense_hdr(sdkp, &sshdr);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600173 return -EINVAL;
174 }
Andrew Pattersonf98a8ca2008-09-04 14:27:35 -0600175 revalidate_disk(sdkp->disk);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600176 return count;
177}
178
Tony Jonesee959b02008-02-22 00:13:36 +0100179static ssize_t
180sd_store_manage_start_stop(struct device *dev, struct device_attribute *attr,
181 const char *buf, size_t count)
Tejun Heoc3c94c5a2007-03-21 00:13:59 +0900182{
Tony Jonesee959b02008-02-22 00:13:36 +0100183 struct scsi_disk *sdkp = to_scsi_disk(dev);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +0900184 struct scsi_device *sdp = sdkp->device;
185
186 if (!capable(CAP_SYS_ADMIN))
187 return -EACCES;
188
189 sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
190
191 return count;
192}
193
Tony Jonesee959b02008-02-22 00:13:36 +0100194static ssize_t
195sd_store_allow_restart(struct device *dev, struct device_attribute *attr,
196 const char *buf, size_t count)
Brian Kinga144c5a2006-06-27 11:10:31 -0500197{
Tony Jonesee959b02008-02-22 00:13:36 +0100198 struct scsi_disk *sdkp = to_scsi_disk(dev);
Brian Kinga144c5a2006-06-27 11:10:31 -0500199 struct scsi_device *sdp = sdkp->device;
200
201 if (!capable(CAP_SYS_ADMIN))
202 return -EACCES;
203
204 if (sdp->type != TYPE_DISK)
205 return -EINVAL;
206
207 sdp->allow_restart = simple_strtoul(buf, NULL, 10);
208
209 return count;
210}
211
Tony Jonesee959b02008-02-22 00:13:36 +0100212static ssize_t
213sd_show_cache_type(struct device *dev, struct device_attribute *attr,
214 char *buf)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600215{
Tony Jonesee959b02008-02-22 00:13:36 +0100216 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600217 int ct = sdkp->RCD + 2*sdkp->WCE;
218
219 return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
220}
221
Tony Jonesee959b02008-02-22 00:13:36 +0100222static ssize_t
223sd_show_fua(struct device *dev, struct device_attribute *attr, char *buf)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600224{
Tony Jonesee959b02008-02-22 00:13:36 +0100225 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600226
227 return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
228}
229
Tony Jonesee959b02008-02-22 00:13:36 +0100230static ssize_t
231sd_show_manage_start_stop(struct device *dev, struct device_attribute *attr,
232 char *buf)
Tejun Heoc3c94c5a2007-03-21 00:13:59 +0900233{
Tony Jonesee959b02008-02-22 00:13:36 +0100234 struct scsi_disk *sdkp = to_scsi_disk(dev);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +0900235 struct scsi_device *sdp = sdkp->device;
236
237 return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
238}
239
Tony Jonesee959b02008-02-22 00:13:36 +0100240static ssize_t
241sd_show_allow_restart(struct device *dev, struct device_attribute *attr,
242 char *buf)
Brian Kinga144c5a2006-06-27 11:10:31 -0500243{
Tony Jonesee959b02008-02-22 00:13:36 +0100244 struct scsi_disk *sdkp = to_scsi_disk(dev);
Brian Kinga144c5a2006-06-27 11:10:31 -0500245
246 return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
247}
248
Martin K. Petersene0597d72008-07-17 04:28:34 -0400249static ssize_t
250sd_show_protection_type(struct device *dev, struct device_attribute *attr,
251 char *buf)
252{
253 struct scsi_disk *sdkp = to_scsi_disk(dev);
254
255 return snprintf(buf, 20, "%u\n", sdkp->protection_type);
256}
257
258static ssize_t
259sd_show_app_tag_own(struct device *dev, struct device_attribute *attr,
260 char *buf)
261{
262 struct scsi_disk *sdkp = to_scsi_disk(dev);
263
264 return snprintf(buf, 20, "%u\n", sdkp->ATO);
265}
266
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500267static ssize_t
268sd_show_thin_provisioning(struct device *dev, struct device_attribute *attr,
269 char *buf)
270{
271 struct scsi_disk *sdkp = to_scsi_disk(dev);
272
273 return snprintf(buf, 20, "%u\n", sdkp->thin_provisioning);
274}
275
Tony Jonesee959b02008-02-22 00:13:36 +0100276static struct device_attribute sd_disk_attrs[] = {
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600277 __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
278 sd_store_cache_type),
279 __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
Brian Kinga144c5a2006-06-27 11:10:31 -0500280 __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
281 sd_store_allow_restart),
Tejun Heoc3c94c5a2007-03-21 00:13:59 +0900282 __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
283 sd_store_manage_start_stop),
Martin K. Petersene0597d72008-07-17 04:28:34 -0400284 __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
285 __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500286 __ATTR(thin_provisioning, S_IRUGO, sd_show_thin_provisioning, NULL),
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600287 __ATTR_NULL,
288};
289
290static struct class sd_disk_class = {
291 .name = "scsi_disk",
292 .owner = THIS_MODULE,
Tony Jonesee959b02008-02-22 00:13:36 +0100293 .dev_release = scsi_disk_release,
294 .dev_attrs = sd_disk_attrs,
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600295};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297static struct scsi_driver sd_template = {
298 .owner = THIS_MODULE,
299 .gendrv = {
300 .name = "sd",
301 .probe = sd_probe,
302 .remove = sd_remove,
Tejun Heoc3c94c5a2007-03-21 00:13:59 +0900303 .suspend = sd_suspend,
304 .resume = sd_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 .shutdown = sd_shutdown,
306 },
307 .rescan = sd_rescan,
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800308 .done = sd_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309};
310
311/*
312 * Device no to disk mapping:
313 *
314 * major disc2 disc p1
315 * |............|.............|....|....| <- dev_t
316 * 31 20 19 8 7 4 3 0
317 *
318 * Inside a major, we have 16k disks, however mapped non-
319 * contiguously. The first 16 disks are for major0, the next
320 * ones with major1, ... Disk 256 is for major0 again, disk 272
321 * for major1, ...
322 * As we stay compatible with our numbering scheme, we can reuse
323 * the well-know SCSI majors 8, 65--71, 136--143.
324 */
325static int sd_major(int major_idx)
326{
327 switch (major_idx) {
328 case 0:
329 return SCSI_DISK0_MAJOR;
330 case 1 ... 7:
331 return SCSI_DISK1_MAJOR + major_idx - 1;
332 case 8 ... 15:
333 return SCSI_DISK8_MAJOR + major_idx - 8;
334 default:
335 BUG();
336 return 0; /* shut up gcc */
337 }
338}
339
Alan Stern39b7f1e2005-11-04 14:44:41 -0500340static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 struct scsi_disk *sdkp = NULL;
343
Alan Stern39b7f1e2005-11-04 14:44:41 -0500344 if (disk->private_data) {
345 sdkp = scsi_disk(disk);
346 if (scsi_device_get(sdkp->device) == 0)
Tony Jonesee959b02008-02-22 00:13:36 +0100347 get_device(&sdkp->dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500348 else
349 sdkp = NULL;
350 }
351 return sdkp;
352}
353
354static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
355{
356 struct scsi_disk *sdkp;
357
Arjan van de Ven0b950672006-01-11 13:16:10 +0100358 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500359 sdkp = __scsi_disk_get(disk);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100360 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return sdkp;
Alan Stern39b7f1e2005-11-04 14:44:41 -0500362}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Alan Stern39b7f1e2005-11-04 14:44:41 -0500364static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
365{
366 struct scsi_disk *sdkp;
367
Arjan van de Ven0b950672006-01-11 13:16:10 +0100368 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500369 sdkp = dev_get_drvdata(dev);
370 if (sdkp)
371 sdkp = __scsi_disk_get(sdkp->disk);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100372 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return sdkp;
374}
375
376static void scsi_disk_put(struct scsi_disk *sdkp)
377{
378 struct scsi_device *sdev = sdkp->device;
379
Arjan van de Ven0b950672006-01-11 13:16:10 +0100380 mutex_lock(&sd_ref_mutex);
Tony Jonesee959b02008-02-22 00:13:36 +0100381 put_device(&sdkp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 scsi_device_put(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100383 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -0400386static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
387{
388 unsigned int prot_op = SCSI_PROT_NORMAL;
389 unsigned int dix = scsi_prot_sg_count(scmd);
390
391 if (scmd->sc_data_direction == DMA_FROM_DEVICE) {
392 if (dif && dix)
393 prot_op = SCSI_PROT_READ_PASS;
394 else if (dif && !dix)
395 prot_op = SCSI_PROT_READ_STRIP;
396 else if (!dif && dix)
397 prot_op = SCSI_PROT_READ_INSERT;
398 } else {
399 if (dif && dix)
400 prot_op = SCSI_PROT_WRITE_PASS;
401 else if (dif && !dix)
402 prot_op = SCSI_PROT_WRITE_INSERT;
403 else if (!dif && dix)
404 prot_op = SCSI_PROT_WRITE_STRIP;
405 }
406
407 scsi_set_prot_op(scmd, prot_op);
408 scsi_set_prot_type(scmd, dif);
409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/**
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500412 * sd_prepare_discard - unmap blocks on thinly provisioned device
413 * @rq: Request to prepare
414 *
415 * Will issue either UNMAP or WRITE SAME(16) depending on preference
416 * indicated by target device.
417 **/
418static int sd_prepare_discard(struct request *rq)
419{
420 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
421 struct bio *bio = rq->bio;
422 sector_t sector = bio->bi_sector;
423 unsigned int num = bio_sectors(bio);
424
425 if (sdkp->device->sector_size == 4096) {
426 sector >>= 3;
427 num >>= 3;
428 }
429
430 rq->cmd_type = REQ_TYPE_BLOCK_PC;
431 rq->timeout = SD_TIMEOUT;
432
433 memset(rq->cmd, 0, rq->cmd_len);
434
435 if (sdkp->unmap) {
436 char *buf = kmap_atomic(bio_page(bio), KM_USER0);
437
438 rq->cmd[0] = UNMAP;
439 rq->cmd[8] = 24;
440 rq->cmd_len = 10;
441
442 /* Ensure that data length matches payload */
443 rq->__data_len = bio->bi_size = bio->bi_io_vec->bv_len = 24;
444
445 put_unaligned_be16(6 + 16, &buf[0]);
446 put_unaligned_be16(16, &buf[2]);
447 put_unaligned_be64(sector, &buf[8]);
448 put_unaligned_be32(num, &buf[16]);
449
450 kunmap_atomic(buf, KM_USER0);
451 } else {
452 rq->cmd[0] = WRITE_SAME_16;
453 rq->cmd[1] = 0x8; /* UNMAP */
454 put_unaligned_be64(sector, &rq->cmd[2]);
455 put_unaligned_be32(num, &rq->cmd[10]);
456 rq->cmd_len = 16;
457 }
458
459 return BLKPREP_OK;
460}
461
462/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * sd_init_command - build a scsi (read or write) command from
464 * information in the request structure.
465 * @SCpnt: pointer to mid-level's per scsi command structure that
466 * contains request and into which the scsi command is written
467 *
468 * Returns 1 if successful and 0 if error (or cannot be done now).
469 **/
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500470static int sd_prep_fn(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500472 struct scsi_cmnd *SCpnt;
473 struct scsi_device *sdp = q->queuedata;
Christoph Hellwig776b23a2006-01-06 18:34:07 +0100474 struct gendisk *disk = rq->rq_disk;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400475 struct scsi_disk *sdkp;
Tejun Heo83096eb2009-05-07 22:24:39 +0900476 sector_t block = blk_rq_pos(rq);
Linus Torvalds18351072008-08-05 21:42:21 -0700477 sector_t threshold;
Tejun Heo83096eb2009-05-07 22:24:39 +0900478 unsigned int this_count = blk_rq_sectors(rq);
Martin K. Petersenbd623e72008-09-19 18:47:19 -0400479 int ret, host_dif;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400480 unsigned char protect;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500481
Martin K. Petersene339c1a2009-11-26 12:00:40 -0500482 /*
483 * Discard request come in as REQ_TYPE_FS but we turn them into
484 * block PC requests to make life easier.
485 */
486 if (blk_discard_rq(rq))
487 ret = sd_prepare_discard(rq);
488
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500489 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
490 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
491 goto out;
492 } else if (rq->cmd_type != REQ_TYPE_FS) {
493 ret = BLKPREP_KILL;
494 goto out;
495 }
496 ret = scsi_setup_fs_cmnd(sdp, rq);
497 if (ret != BLKPREP_OK)
498 goto out;
499 SCpnt = rq->special;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400500 sdkp = scsi_disk(disk);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500501
502 /* from here on until we're complete, any goto out
503 * is used for a killable error condition */
504 ret = BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500506 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
507 "sd_init_command: block=%llu, "
508 "count=%d\n",
509 (unsigned long long)block,
510 this_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 if (!sdp || !scsi_device_online(sdp) ||
Tejun Heo83096eb2009-05-07 22:24:39 +0900513 block + blk_rq_sectors(rq) > get_capacity(disk)) {
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500514 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
Tejun Heo83096eb2009-05-07 22:24:39 +0900515 "Finishing %u sectors\n",
516 blk_rq_sectors(rq)));
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500517 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
518 "Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500519 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 if (sdp->changed) {
523 /*
524 * quietly refuse to do anything to a changed disc until
525 * the changed bit has been reset
526 */
527 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500528 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500530
Hans de Goedea0899d42008-01-20 11:12:26 +0100531 /*
Linus Torvalds18351072008-08-05 21:42:21 -0700532 * Some SD card readers can't handle multi-sector accesses which touch
533 * the last one or two hardware sectors. Split accesses as needed.
Hans de Goedea0899d42008-01-20 11:12:26 +0100534 */
Linus Torvalds18351072008-08-05 21:42:21 -0700535 threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
536 (sdp->sector_size / 512);
537
538 if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
539 if (block < threshold) {
540 /* Access up to the threshold but not beyond */
541 this_count = threshold - block;
542 } else {
543 /* Access only a single hardware sector */
544 this_count = sdp->sector_size / 512;
545 }
546 }
Hans de Goedea0899d42008-01-20 11:12:26 +0100547
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500548 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
549 (unsigned long long)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
552 * If we have a 1K hardware sectorsize, prevent access to single
553 * 512 byte sectors. In theory we could handle this - in fact
554 * the scsi cdrom driver must be able to handle this because
555 * we typically use 1K blocksizes, and cdroms typically have
556 * 2K hardware sectorsizes. Of course, things are simpler
557 * with the cdrom, since it is read-only. For performance
558 * reasons, the filesystems should be able to handle this
559 * and not force the scsi disk driver to use bounce buffers
560 * for this.
561 */
562 if (sdp->sector_size == 1024) {
Tejun Heo83096eb2009-05-07 22:24:39 +0900563 if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500564 scmd_printk(KERN_ERR, SCpnt,
565 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500566 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 } else {
568 block = block >> 1;
569 this_count = this_count >> 1;
570 }
571 }
572 if (sdp->sector_size == 2048) {
Tejun Heo83096eb2009-05-07 22:24:39 +0900573 if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500574 scmd_printk(KERN_ERR, SCpnt,
575 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500576 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 } else {
578 block = block >> 2;
579 this_count = this_count >> 2;
580 }
581 }
582 if (sdp->sector_size == 4096) {
Tejun Heo83096eb2009-05-07 22:24:39 +0900583 if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500584 scmd_printk(KERN_ERR, SCpnt,
585 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500586 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 } else {
588 block = block >> 3;
589 this_count = this_count >> 3;
590 }
591 }
592 if (rq_data_dir(rq) == WRITE) {
593 if (!sdp->writeable) {
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500594 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596 SCpnt->cmnd[0] = WRITE_6;
597 SCpnt->sc_data_direction = DMA_TO_DEVICE;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400598
599 if (blk_integrity_rq(rq) &&
600 sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
601 goto out;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 } else if (rq_data_dir(rq) == READ) {
604 SCpnt->cmnd[0] = READ_6;
605 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
606 } else {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500607 scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500608 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500611 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
Tejun Heo83096eb2009-05-07 22:24:39 +0900612 "%s %d/%u 512 byte blocks.\n",
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500613 (rq_data_dir(rq) == WRITE) ?
614 "writing" : "reading", this_count,
Tejun Heo83096eb2009-05-07 22:24:39 +0900615 blk_rq_sectors(rq)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400617 /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
Martin K. Petersenbd623e72008-09-19 18:47:19 -0400618 host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
619 if (host_dif)
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400620 protect = 1 << 5;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400621 else
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400622 protect = 0;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400623
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400624 if (host_dif == SD_DIF_TYPE2_PROTECTION) {
625 SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
626
627 if (unlikely(SCpnt->cmnd == NULL)) {
628 ret = BLKPREP_DEFER;
629 goto out;
630 }
631
632 SCpnt->cmd_len = SD_EXT_CDB_SIZE;
633 memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
634 SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
635 SCpnt->cmnd[7] = 0x18;
636 SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
637 SCpnt->cmnd[10] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
638
639 /* LBA */
640 SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
641 SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
642 SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
643 SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
644 SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
645 SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
646 SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
647 SCpnt->cmnd[19] = (unsigned char) block & 0xff;
648
649 /* Expected Indirect LBA */
650 SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
651 SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
652 SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
653 SCpnt->cmnd[23] = (unsigned char) block & 0xff;
654
655 /* Transfer length */
656 SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
657 SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
658 SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
659 SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
660 } else if (block > 0xffffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 SCpnt->cmnd[0] += READ_16 - READ_6;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400662 SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
664 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
665 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
666 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
667 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
668 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
669 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
670 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
671 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
672 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
673 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
674 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
675 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
676 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400677 scsi_device_protection(SCpnt->device) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 SCpnt->device->use_10_for_rw) {
679 if (this_count > 0xffff)
680 this_count = 0xffff;
681
682 SCpnt->cmnd[0] += READ_10 - READ_6;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -0400683 SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
685 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
686 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
687 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
688 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
689 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
690 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
691 } else {
Tejun Heo007365a2006-01-06 09:53:52 +0100692 if (unlikely(blk_fua_rq(rq))) {
693 /*
694 * This happens only if this drive failed
695 * 10byte rw command with ILLEGAL_REQUEST
696 * during operation and thus turned off
697 * use_10_for_rw.
698 */
Martin K. Petersene73aec82007-02-27 22:40:55 -0500699 scmd_printk(KERN_ERR, SCpnt,
700 "FUA write on READ/WRITE(6) drive\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500701 goto out;
Tejun Heo007365a2006-01-06 09:53:52 +0100702 }
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
705 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
706 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
707 SCpnt->cmnd[4] = (unsigned char) this_count;
708 SCpnt->cmnd[5] = 0;
709 }
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200710 SCpnt->sdb.length = this_count * sdp->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400712 /* If DIF or DIX is enabled, tell HBA how to handle request */
Martin K. Petersenbd623e72008-09-19 18:47:19 -0400713 if (host_dif || scsi_prot_sg_count(SCpnt))
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -0400714 sd_prot_op(SCpnt, host_dif);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /*
717 * We shouldn't disconnect in the middle of a sector, so with a dumb
718 * host adapter, it's safe to assume that we can at least transfer
719 * this many bytes between each connect / disconnect.
720 */
721 SCpnt->transfersize = sdp->sector_size;
722 SCpnt->underflow = this_count << 9;
723 SCpnt->allowed = SD_MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * This indicates that the command is ready from our end to be
727 * queued.
728 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500729 ret = BLKPREP_OK;
730 out:
731 return scsi_prep_return(q, rq, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/**
735 * sd_open - open a scsi disk device
736 * @inode: only i_rdev member may be used
737 * @filp: only f_mode and f_flags may be used
738 *
739 * Returns 0 if successful. Returns a negated errno value in case
740 * of error.
741 *
742 * Note: This can be called from a user context (e.g. fsck(1) )
743 * or from within the kernel (e.g. as a result of a mount(1) ).
744 * In the latter case @inode and @filp carry an abridged amount
745 * of information as noted above.
746 **/
Al Viro0338e292008-03-02 10:41:04 -0500747static int sd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Al Viro0338e292008-03-02 10:41:04 -0500749 struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 struct scsi_device *sdev;
751 int retval;
752
Al Viro0338e292008-03-02 10:41:04 -0500753 if (!sdkp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return -ENXIO;
755
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500756 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 sdev = sdkp->device;
759
760 /*
761 * If the device is in error recovery, wait until it is done.
762 * If the device is offline, then disallow any access to it.
763 */
764 retval = -ENXIO;
765 if (!scsi_block_when_processing_errors(sdev))
766 goto error_out;
767
768 if (sdev->removable || sdkp->write_prot)
Al Viro0338e292008-03-02 10:41:04 -0500769 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /*
772 * If the drive is empty, just let the open fail.
773 */
774 retval = -ENOMEDIUM;
Al Viro0338e292008-03-02 10:41:04 -0500775 if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 goto error_out;
777
778 /*
779 * If the device has the write protect tab set, have the open fail
780 * if the user expects to be able to write to the thing.
781 */
782 retval = -EROFS;
Al Viro0338e292008-03-02 10:41:04 -0500783 if (sdkp->write_prot && (mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto error_out;
785
786 /*
787 * It is possible that the disk changing stuff resulted in
788 * the device being taken offline. If this is the case,
789 * report this to the user, and don't pretend that the
790 * open actually succeeded.
791 */
792 retval = -ENXIO;
793 if (!scsi_device_online(sdev))
794 goto error_out;
795
796 if (!sdkp->openers++ && sdev->removable) {
797 if (scsi_block_when_processing_errors(sdev))
798 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
799 }
800
801 return 0;
802
803error_out:
804 scsi_disk_put(sdkp);
805 return retval;
806}
807
808/**
809 * sd_release - invoked when the (last) close(2) is called on this
810 * scsi disk.
811 * @inode: only i_rdev member may be used
812 * @filp: only f_mode and f_flags may be used
813 *
814 * Returns 0.
815 *
816 * Note: may block (uninterruptible) if error recovery is underway
817 * on this disk.
818 **/
Al Viro0338e292008-03-02 10:41:04 -0500819static int sd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 struct scsi_disk *sdkp = scsi_disk(disk);
822 struct scsi_device *sdev = sdkp->device;
823
James Bottomley56937f72007-03-11 12:25:33 -0500824 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 if (!--sdkp->openers && sdev->removable) {
827 if (scsi_block_when_processing_errors(sdev))
828 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
829 }
830
831 /*
832 * XXX and what if there are packets in flight and this close()
833 * XXX is followed by a "rmmod sd_mod"?
834 */
835 scsi_disk_put(sdkp);
836 return 0;
837}
838
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800839static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
842 struct scsi_device *sdp = sdkp->device;
843 struct Scsi_Host *host = sdp->host;
844 int diskinfo[4];
845
846 /* default to most commonly used values */
847 diskinfo[0] = 0x40; /* 1 << 6 */
848 diskinfo[1] = 0x20; /* 1 << 5 */
849 diskinfo[2] = sdkp->capacity >> 11;
850
851 /* override with calculated, extended default, or driver values */
852 if (host->hostt->bios_param)
853 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
854 else
855 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
856
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800857 geo->heads = diskinfo[0];
858 geo->sectors = diskinfo[1];
859 geo->cylinders = diskinfo[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 0;
861}
862
863/**
864 * sd_ioctl - process an ioctl
865 * @inode: only i_rdev/i_bdev members may be used
866 * @filp: only f_mode and f_flags may be used
867 * @cmd: ioctl command number
868 * @arg: this is third argument given to ioctl(2) system call.
869 * Often contains a pointer.
870 *
871 * Returns 0 if successful (some ioctls return postive numbers on
872 * success as well). Returns a negated errno value in case of error.
873 *
874 * Note: most ioctls are forward onto the block subsystem or further
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200875 * down in the scsi subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 **/
Al Viro0338e292008-03-02 10:41:04 -0500877static int sd_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 unsigned int cmd, unsigned long arg)
879{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct gendisk *disk = bdev->bd_disk;
881 struct scsi_device *sdp = scsi_disk(disk)->device;
882 void __user *p = (void __user *)arg;
883 int error;
884
885 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
886 disk->disk_name, cmd));
887
888 /*
889 * If we are in the middle of error recovery, don't let anyone
890 * else try and use this device. Also, if error recovery fails, it
891 * may try and take the device offline, in which case all further
892 * access to the device is prohibited.
893 */
Al Viro83ff6fe2008-03-02 08:15:49 -0500894 error = scsi_nonblockable_ioctl(sdp, cmd, p,
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +0100895 (mode & FMODE_NDELAY) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (!scsi_block_when_processing_errors(sdp) || !error)
897 return error;
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /*
900 * Send SCSI addressing ioctls directly to mid level, send other
901 * ioctls to block level and then onto mid level if they can't be
902 * resolved.
903 */
904 switch (cmd) {
905 case SCSI_IOCTL_GET_IDLUN:
906 case SCSI_IOCTL_GET_BUS_NUMBER:
907 return scsi_ioctl(sdp, cmd, p);
908 default:
Al Viro0338e292008-03-02 10:41:04 -0500909 error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (error != -ENOTTY)
911 return error;
912 }
913 return scsi_ioctl(sdp, cmd, p);
914}
915
916static void set_media_not_present(struct scsi_disk *sdkp)
917{
918 sdkp->media_present = 0;
919 sdkp->capacity = 0;
920 sdkp->device->changed = 1;
921}
922
923/**
924 * sd_media_changed - check if our medium changed
925 * @disk: kernel device descriptor
926 *
927 * Returns 0 if not applicable or no change; 1 if change
928 *
929 * Note: this function is invoked from the block subsystem.
930 **/
931static int sd_media_changed(struct gendisk *disk)
932{
933 struct scsi_disk *sdkp = scsi_disk(disk);
934 struct scsi_device *sdp = sdkp->device;
James Bottomley001aac22007-12-02 19:10:40 +0200935 struct scsi_sense_hdr *sshdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int retval;
937
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500938 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (!sdp->removable)
941 return 0;
942
943 /*
944 * If the device is offline, don't send any commands - just pretend as
945 * if the command failed. If the device ever comes back online, we
946 * can deal with it then. It is only because of unrecoverable errors
947 * that we would ever take a device offline in the first place.
948 */
Kay Sievers285e9672007-08-14 14:10:39 +0200949 if (!scsi_device_online(sdp)) {
950 set_media_not_present(sdkp);
951 retval = 1;
952 goto out;
953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 /*
956 * Using TEST_UNIT_READY enables differentiation between drive with
957 * no cartridge loaded - NOT READY, drive with changed cartridge -
958 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
959 *
960 * Drives that auto spin down. eg iomega jaz 1G, will be started
961 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
962 * sd_revalidate() is called.
963 */
964 retval = -ENODEV;
Kay Sievers285e9672007-08-14 14:10:39 +0200965
James Bottomley001aac22007-12-02 19:10:40 +0200966 if (scsi_block_when_processing_errors(sdp)) {
967 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
968 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
969 sshdr);
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 /*
973 * Unable to test, unit probably not ready. This usually
974 * means there is no disc in the drive. Mark as changed,
975 * and we will figure it out later once the drive is
976 * available again.
977 */
James Bottomley001aac22007-12-02 19:10:40 +0200978 if (retval || (scsi_sense_valid(sshdr) &&
979 /* 0x3a is medium not present */
980 sshdr->asc == 0x3a)) {
Kay Sievers285e9672007-08-14 14:10:39 +0200981 set_media_not_present(sdkp);
982 retval = 1;
983 goto out;
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 /*
987 * For removable scsi disk we have to recognise the presence
988 * of a disk in the drive. This is kept in the struct scsi_disk
989 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
990 */
991 sdkp->media_present = 1;
992
993 retval = sdp->changed;
994 sdp->changed = 0;
Kay Sievers285e9672007-08-14 14:10:39 +0200995out:
996 if (retval != sdkp->previous_state)
997 sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
998 sdkp->previous_state = retval;
James Bottomley001aac22007-12-02 19:10:40 +0200999 kfree(sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Martin K. Petersene73aec82007-02-27 22:40:55 -05001003static int sd_sync_cache(struct scsi_disk *sdkp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int retries, res;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001006 struct scsi_device *sdp = sdkp->device;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001007 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if (!scsi_device_online(sdp))
1010 return -ENODEV;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 for (retries = 3; retries > 0; --retries) {
1014 unsigned char cmd[10] = { 0 };
1015
1016 cmd[0] = SYNCHRONIZE_CACHE;
1017 /*
1018 * Leave the rest of the command zero to indicate
1019 * flush everything.
1020 */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001021 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09001022 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
James Bottomleyea73a9f2005-08-28 11:33:52 -05001023 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 break;
1025 }
1026
Martin K. Petersene73aec82007-02-27 22:40:55 -05001027 if (res) {
1028 sd_print_result(sdkp, res);
1029 if (driver_byte(res) & DRIVER_SENSE)
1030 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032
Tejun Heo37210502007-03-21 00:07:18 +09001033 if (res)
1034 return -EIO;
1035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Jens Axboe165125e2007-07-24 09:28:11 +02001038static void sd_prepare_flush(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001040 rq->cmd_type = REQ_TYPE_BLOCK_PC;
James Bottomleyc0ed79a2005-11-08 09:21:07 -05001041 rq->timeout = SD_TIMEOUT;
1042 rq->cmd[0] = SYNCHRONIZE_CACHE;
Tejun Heo461d4e92006-01-06 09:52:55 +01001043 rq->cmd_len = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046static void sd_rescan(struct device *dev)
1047{
Alan Stern39b7f1e2005-11-04 14:44:41 -05001048 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1049
1050 if (sdkp) {
Andrew Pattersonf98a8ca2008-09-04 14:27:35 -06001051 revalidate_disk(sdkp->disk);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001052 scsi_disk_put(sdkp);
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056
1057#ifdef CONFIG_COMPAT
1058/*
1059 * This gets directly called from VFS. When the ioctl
1060 * is not recognized we go back to the other translation paths.
1061 */
Al Viro0338e292008-03-02 10:41:04 -05001062static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
1063 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Al Viro0338e292008-03-02 10:41:04 -05001065 struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 /*
1068 * If we are in the middle of error recovery, don't let anyone
1069 * else try and use this device. Also, if error recovery fails, it
1070 * may try and take the device offline, in which case all further
1071 * access to the device is prohibited.
1072 */
1073 if (!scsi_block_when_processing_errors(sdev))
1074 return -ENODEV;
1075
1076 if (sdev->host->hostt->compat_ioctl) {
1077 int ret;
1078
1079 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
1080
1081 return ret;
1082 }
1083
1084 /*
1085 * Let the static ioctl translation table take care of it.
1086 */
1087 return -ENOIOCTLCMD;
1088}
1089#endif
1090
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001091static const struct block_device_operations sd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 .owner = THIS_MODULE,
Al Viro0338e292008-03-02 10:41:04 -05001093 .open = sd_open,
1094 .release = sd_release,
1095 .locked_ioctl = sd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001096 .getgeo = sd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097#ifdef CONFIG_COMPAT
Al Viro0338e292008-03-02 10:41:04 -05001098 .compat_ioctl = sd_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099#endif
1100 .media_changed = sd_media_changed,
1101 .revalidate_disk = sd_revalidate_disk,
1102};
1103
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001104static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1105{
Tejun Heo83096eb2009-05-07 22:24:39 +09001106 u64 start_lba = blk_rq_pos(scmd->request);
1107 u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001108 u64 bad_lba;
1109 int info_valid;
1110
1111 if (!blk_fs_request(scmd->request))
1112 return 0;
1113
1114 info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
1115 SCSI_SENSE_BUFFERSIZE,
1116 &bad_lba);
1117 if (!info_valid)
1118 return 0;
1119
1120 if (scsi_bufflen(scmd) <= scmd->device->sector_size)
1121 return 0;
1122
1123 if (scmd->device->sector_size < 512) {
1124 /* only legitimate sector_size here is 256 */
1125 start_lba <<= 1;
1126 end_lba <<= 1;
1127 } else {
1128 /* be careful ... don't want any overflows */
1129 u64 factor = scmd->device->sector_size / 512;
1130 do_div(start_lba, factor);
1131 do_div(end_lba, factor);
1132 }
1133
1134 /* The bad lba was reported incorrectly, we have no idea where
1135 * the error is.
1136 */
1137 if (bad_lba < start_lba || bad_lba >= end_lba)
1138 return 0;
1139
1140 /* This computation should always be done in terms of
1141 * the resolution of the device's medium.
1142 */
1143 return (bad_lba - start_lba) * scmd->device->sector_size;
1144}
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146/**
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001147 * sd_done - bottom half handler: called when the lower level
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 * driver has completed (successfully or otherwise) a scsi command.
1149 * @SCpnt: mid-level's per command structure.
1150 *
1151 * Note: potentially run from within an ISR. Must not block.
1152 **/
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001153static int sd_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 int result = SCpnt->result;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001156 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 struct scsi_sense_hdr sshdr;
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001158 struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int sense_valid = 0;
1160 int sense_deferred = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 if (result) {
1163 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
1164 if (sense_valid)
1165 sense_deferred = scsi_sense_is_deferred(&sshdr);
1166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167#ifdef CONFIG_SCSI_LOGGING
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001168 SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (sense_valid) {
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001170 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001171 "sd_done: sb[respc,sk,asc,"
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001172 "ascq]=%x,%x,%x,%x\n",
1173 sshdr.response_code,
1174 sshdr.sense_key, sshdr.asc,
1175 sshdr.ascq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177#endif
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001178 if (driver_byte(result) != DRIVER_SENSE &&
1179 (!sense_valid || sense_deferred))
1180 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001182 switch (sshdr.sense_key) {
1183 case HARDWARE_ERROR:
1184 case MEDIUM_ERROR:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001185 good_bytes = sd_completed_bytes(SCpnt);
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001186 break;
1187 case RECOVERED_ERROR:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001188 good_bytes = scsi_bufflen(SCpnt);
1189 break;
Jamie Wellnitz10dab222008-09-11 21:39:36 -04001190 case NO_SENSE:
1191 /* This indicates a false check condition, so ignore it. An
1192 * unknown amount of data was transferred so treat it as an
1193 * error.
1194 */
1195 scsi_print_sense("sd", SCpnt);
1196 SCpnt->result = 0;
1197 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1198 break;
Martin K. Petersenfa4698f2010-01-18 18:43:18 -05001199 case ABORTED_COMMAND: /* DIF: Target detected corruption */
1200 case ILLEGAL_REQUEST: /* DIX: Host detected corruption */
1201 if (sshdr.asc == 0x10)
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001202 good_bytes = sd_completed_bytes(SCpnt);
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001203 break;
1204 default:
1205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001207 out:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001208 if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
1209 sd_dif_complete(SCpnt, good_bytes);
1210
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001211 if (scsi_host_dif_capable(sdkp->device->host, sdkp->protection_type)
1212 == SD_DIF_TYPE2_PROTECTION && SCpnt->cmnd != SCpnt->request->cmd)
1213 mempool_free(SCpnt->cmnd, sd_cdb_pool);
1214
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001215 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
James Bottomleyea73a9f2005-08-28 11:33:52 -05001218static int media_not_present(struct scsi_disk *sdkp,
1219 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
James Bottomleyea73a9f2005-08-28 11:33:52 -05001222 if (!scsi_sense_valid(sshdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return 0;
1224 /* not invoked for commands that could return deferred errors */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001225 if (sshdr->sense_key != NOT_READY &&
1226 sshdr->sense_key != UNIT_ATTENTION)
1227 return 0;
1228 if (sshdr->asc != 0x3A) /* medium not present */
1229 return 0;
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 set_media_not_present(sdkp);
1232 return 1;
1233}
1234
1235/*
1236 * spinup disk - called only in sd_revalidate_disk()
1237 */
1238static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001239sd_spinup_disk(struct scsi_disk *sdkp)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001240{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 unsigned char cmd[10];
Alan Stern4451e472005-07-12 10:45:17 -04001242 unsigned long spintime_expire = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int retries, spintime;
1244 unsigned int the_result;
1245 struct scsi_sense_hdr sshdr;
1246 int sense_valid = 0;
1247
1248 spintime = 0;
1249
1250 /* Spin up drives, as required. Only do this at boot time */
1251 /* Spinup needs to be done for module loads too. */
1252 do {
1253 retries = 0;
1254
1255 do {
1256 cmd[0] = TEST_UNIT_READY;
1257 memset((void *) &cmd[1], 0, 9);
1258
James Bottomleyea73a9f2005-08-28 11:33:52 -05001259 the_result = scsi_execute_req(sdkp->device, cmd,
1260 DMA_NONE, NULL, 0,
1261 &sshdr, SD_TIMEOUT,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09001262 SD_MAX_RETRIES, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Alan Sternb4d38e32006-10-11 16:48:28 -04001264 /*
1265 * If the drive has indicated to us that it
1266 * doesn't have any media in it, don't bother
1267 * with any more polling.
1268 */
1269 if (media_not_present(sdkp, &sshdr))
1270 return;
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (the_result)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001273 sense_valid = scsi_sense_valid(&sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 retries++;
1275 } while (retries < 3 &&
1276 (!scsi_status_is_good(the_result) ||
1277 ((driver_byte(the_result) & DRIVER_SENSE) &&
1278 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1281 /* no sense, TUR either succeeded or failed
1282 * with a status error */
Martin K. Petersene73aec82007-02-27 22:40:55 -05001283 if(!spintime && !scsi_status_is_good(the_result)) {
1284 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1285 sd_print_result(sdkp, the_result);
1286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 break;
1288 }
1289
1290 /*
1291 * The device does not want the automatic start to be issued.
1292 */
Matthew Wilcox33dd6f92009-02-20 06:53:48 -07001293 if (sdkp->device->no_start_on_add)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Matthew Wilcox33dd6f92009-02-20 06:53:48 -07001296 if (sense_valid && sshdr.sense_key == NOT_READY) {
1297 if (sshdr.asc == 4 && sshdr.ascq == 3)
1298 break; /* manual intervention required */
1299 if (sshdr.asc == 4 && sshdr.ascq == 0xb)
1300 break; /* standby */
1301 if (sshdr.asc == 4 && sshdr.ascq == 0xc)
1302 break; /* unavailable */
1303 /*
1304 * Issue command to spin up drive when not ready
1305 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001307 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 cmd[0] = START_STOP;
1309 cmd[1] = 1; /* Return immediately */
1310 memset((void *) &cmd[2], 0, 8);
1311 cmd[4] = 1; /* Start spin cycle */
Stefan Richterd2886ea2008-05-11 00:34:07 +02001312 if (sdkp->device->start_stop_pwr_cond)
1313 cmd[4] |= 1 << 4;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001314 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1315 NULL, 0, &sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09001316 SD_TIMEOUT, SD_MAX_RETRIES,
1317 NULL);
Alan Stern4451e472005-07-12 10:45:17 -04001318 spintime_expire = jiffies + 100 * HZ;
1319 spintime = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* Wait 1 second for next try */
1322 msleep(1000);
1323 printk(".");
Alan Stern4451e472005-07-12 10:45:17 -04001324
1325 /*
1326 * Wait for USB flash devices with slow firmware.
1327 * Yes, this sense key/ASC combination shouldn't
1328 * occur here. It's characteristic of these devices.
1329 */
1330 } else if (sense_valid &&
1331 sshdr.sense_key == UNIT_ATTENTION &&
1332 sshdr.asc == 0x28) {
1333 if (!spintime) {
1334 spintime_expire = jiffies + 5 * HZ;
1335 spintime = 1;
1336 }
1337 /* Wait 1 second for next try */
1338 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 } else {
1340 /* we don't understand the sense code, so it's
1341 * probably pointless to loop */
1342 if(!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001343 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1344 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 break;
1347 }
1348
Alan Stern4451e472005-07-12 10:45:17 -04001349 } while (spintime && time_before_eq(jiffies, spintime_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 if (spintime) {
1352 if (scsi_status_is_good(the_result))
1353 printk("ready\n");
1354 else
1355 printk("not responding...\n");
1356 }
1357}
1358
Martin K. Petersene0597d72008-07-17 04:28:34 -04001359
1360/*
1361 * Determine whether disk supports Data Integrity Field.
1362 */
1363void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
1364{
1365 struct scsi_device *sdp = sdkp->device;
1366 u8 type;
1367
1368 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -04001369 return;
1370
1371 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
1372
1373 if (type == sdkp->protection_type || !sdkp->first_scan)
1374 return;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001375
Martin K. Petersenbe922f42008-09-19 18:47:20 -04001376 sdkp->protection_type = type;
1377
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04001378 if (type > SD_DIF_TYPE3_PROTECTION) {
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -04001379 sd_printk(KERN_ERR, sdkp, "formatted with unsupported " \
1380 "protection type %u. Disabling disk!\n", type);
1381 sdkp->capacity = 0;
1382 return;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001383 }
1384
Martin K. Petersen35e1a5d2009-09-18 17:33:00 -04001385 if (scsi_host_dif_capable(sdp->host, type))
1386 sd_printk(KERN_NOTICE, sdkp,
1387 "Enabling DIF Type %u protection\n", type);
1388 else
1389 sd_printk(KERN_NOTICE, sdkp,
1390 "Disabling DIF Type %u protection\n", type);
Martin K. Petersene0597d72008-07-17 04:28:34 -04001391}
1392
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001393static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
1394 struct scsi_sense_hdr *sshdr, int sense_valid,
1395 int the_result)
1396{
1397 sd_print_result(sdkp, the_result);
1398 if (driver_byte(the_result) & DRIVER_SENSE)
1399 sd_print_sense_hdr(sdkp, sshdr);
1400 else
1401 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
1402
1403 /*
1404 * Set dirty bit for removable devices if not ready -
1405 * sometimes drives will not report this properly.
1406 */
1407 if (sdp->removable &&
1408 sense_valid && sshdr->sense_key == NOT_READY)
1409 sdp->changed = 1;
1410
1411 /*
1412 * We used to set media_present to 0 here to indicate no media
1413 * in the drive, but some drives fail read capacity even with
1414 * media present, so we can't do that.
1415 */
1416 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
1417}
1418
1419#define RC16_LEN 32
1420#if RC16_LEN > SD_BUF_SIZE
1421#error RC16_LEN must not be more than SD_BUF_SIZE
1422#endif
1423
1424static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
1425 unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001426{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 unsigned char cmd[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 struct scsi_sense_hdr sshdr;
1429 int sense_valid = 0;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001430 int the_result;
1431 int retries = 3;
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001432 unsigned int alignment;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001433 unsigned long long lba;
1434 unsigned sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 do {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001437 memset(cmd, 0, 16);
1438 cmd[0] = SERVICE_ACTION_IN;
1439 cmd[1] = SAI_READ_CAPACITY_16;
1440 cmd[13] = RC16_LEN;
1441 memset(buffer, 0, RC16_LEN);
1442
James Bottomleyea73a9f2005-08-28 11:33:52 -05001443 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001444 buffer, RC16_LEN, &sshdr,
1445 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
James Bottomleyea73a9f2005-08-28 11:33:52 -05001447 if (media_not_present(sdkp, &sshdr))
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001448 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Matthew Wilcox2b301302009-03-12 14:20:30 -04001450 if (the_result) {
James Bottomleyea73a9f2005-08-28 11:33:52 -05001451 sense_valid = scsi_sense_valid(&sshdr);
Matthew Wilcox2b301302009-03-12 14:20:30 -04001452 if (sense_valid &&
1453 sshdr.sense_key == ILLEGAL_REQUEST &&
1454 (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
1455 sshdr.ascq == 0x00)
1456 /* Invalid Command Operation Code or
1457 * Invalid Field in CDB, just retry
1458 * silently with RC10 */
1459 return -EINVAL;
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 retries--;
1462
1463 } while (the_result && retries);
1464
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001465 if (the_result) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001466 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001467 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1468 return -EINVAL;
1469 }
Martin K. Petersene73aec82007-02-27 22:40:55 -05001470
Dave Hansen8f76d152009-04-21 16:43:27 -07001471 sector_size = get_unaligned_be32(&buffer[8]);
1472 lba = get_unaligned_be64(&buffer[0]);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001473
1474 sd_read_protection_type(sdkp, buffer);
1475
1476 if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
1477 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1478 "kernel compiled with support for large block "
1479 "devices.\n");
1480 sdkp->capacity = 0;
1481 return -EOVERFLOW;
1482 }
1483
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001484 /* Logical blocks per physical block exponent */
1485 sdkp->hw_sector_size = (1 << (buffer[13] & 0xf)) * sector_size;
1486
1487 /* Lowest aligned logical block */
1488 alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
1489 blk_queue_alignment_offset(sdp->request_queue, alignment);
1490 if (alignment && sdkp->first_scan)
1491 sd_printk(KERN_NOTICE, sdkp,
1492 "physical block alignment offset: %u\n", alignment);
1493
Martin K. Petersene339c1a2009-11-26 12:00:40 -05001494 if (buffer[14] & 0x80) { /* TPE */
1495 struct request_queue *q = sdp->request_queue;
1496
1497 sdkp->thin_provisioning = 1;
1498 q->limits.discard_granularity = sdkp->hw_sector_size;
1499 q->limits.max_discard_sectors = 0xffffffff;
1500
1501 if (buffer[14] & 0x40) /* TPRZ */
1502 q->limits.discard_zeroes_data = 1;
1503
1504 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1505 }
1506
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001507 sdkp->capacity = lba + 1;
1508 return sector_size;
1509}
1510
1511static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
1512 unsigned char *buffer)
1513{
1514 unsigned char cmd[16];
1515 struct scsi_sense_hdr sshdr;
1516 int sense_valid = 0;
1517 int the_result;
1518 int retries = 3;
1519 sector_t lba;
1520 unsigned sector_size;
1521
1522 do {
1523 cmd[0] = READ_CAPACITY;
1524 memset(&cmd[1], 0, 9);
1525 memset(buffer, 0, 8);
1526
1527 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1528 buffer, 8, &sshdr,
1529 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1530
1531 if (media_not_present(sdkp, &sshdr))
1532 return -ENODEV;
1533
1534 if (the_result)
1535 sense_valid = scsi_sense_valid(&sshdr);
1536 retries--;
1537
1538 } while (the_result && retries);
1539
1540 if (the_result) {
1541 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1542 read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1543 return -EINVAL;
1544 }
1545
Dave Hansen8f76d152009-04-21 16:43:27 -07001546 sector_size = get_unaligned_be32(&buffer[4]);
1547 lba = get_unaligned_be32(&buffer[0]);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001548
1549 if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
1550 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1551 "kernel compiled with support for large block "
1552 "devices.\n");
1553 sdkp->capacity = 0;
1554 return -EOVERFLOW;
1555 }
1556
1557 sdkp->capacity = lba + 1;
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001558 sdkp->hw_sector_size = sector_size;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001559 return sector_size;
1560}
1561
Matthew Wilcox2b301302009-03-12 14:20:30 -04001562static int sd_try_rc16_first(struct scsi_device *sdp)
1563{
1564 if (sdp->scsi_level > SCSI_SPC_2)
1565 return 1;
1566 if (scsi_device_protection(sdp))
1567 return 1;
1568 return 0;
1569}
1570
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001571/*
1572 * read disk capacity
1573 */
1574static void
1575sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
1576{
1577 int sector_size;
1578 struct scsi_device *sdp = sdkp->device;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001579 sector_t old_capacity = sdkp->capacity;
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001580
Matthew Wilcox2b301302009-03-12 14:20:30 -04001581 if (sd_try_rc16_first(sdp)) {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001582 sector_size = read_capacity_16(sdkp, sdp, buffer);
1583 if (sector_size == -EOVERFLOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 goto got_data;
Matthew Wilcox2b301302009-03-12 14:20:30 -04001585 if (sector_size == -ENODEV)
1586 return;
1587 if (sector_size < 0)
1588 sector_size = read_capacity_10(sdkp, sdp, buffer);
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001589 if (sector_size < 0)
1590 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 } else {
Matthew Wilcox0da205e2009-03-12 14:20:29 -04001592 sector_size = read_capacity_10(sdkp, sdp, buffer);
1593 if (sector_size == -EOVERFLOW)
1594 goto got_data;
1595 if (sector_size < 0)
1596 return;
1597 if ((sizeof(sdkp->capacity) > 4) &&
1598 (sdkp->capacity > 0xffffffffULL)) {
1599 int old_sector_size = sector_size;
1600 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1601 "Trying to use READ CAPACITY(16).\n");
1602 sector_size = read_capacity_16(sdkp, sdp, buffer);
1603 if (sector_size < 0) {
1604 sd_printk(KERN_NOTICE, sdkp,
1605 "Using 0xffffffff as device size\n");
1606 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1607 sector_size = old_sector_size;
1608 goto got_data;
1609 }
1610 }
1611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Alan Stern5c211ca2009-02-18 10:54:44 -05001613 /* Some devices are known to return the total number of blocks,
1614 * not the highest block number. Some devices have versions
1615 * which do this and others which do not. Some devices we might
1616 * suspect of doing this but we don't know for certain.
1617 *
1618 * If we know the reported capacity is wrong, decrement it. If
1619 * we can only guess, then assume the number of blocks is even
1620 * (usually true but not always) and err on the side of lowering
1621 * the capacity.
1622 */
1623 if (sdp->fix_capacity ||
1624 (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
1625 sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
1626 "from its reported value: %llu\n",
1627 (unsigned long long) sdkp->capacity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 --sdkp->capacity;
Oliver Neukum61bf54b2007-02-08 09:04:48 +01001629 }
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631got_data:
1632 if (sector_size == 0) {
1633 sector_size = 512;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001634 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
1635 "assuming 512.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637
1638 if (sector_size != 512 &&
1639 sector_size != 1024 &&
1640 sector_size != 2048 &&
1641 sector_size != 4096 &&
1642 sector_size != 256) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001643 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
1644 sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 /*
1646 * The user might want to re-format the drive with
1647 * a supported sectorsize. Once this happens, it
1648 * would be relatively trivial to set the thing up.
1649 * For this reason, we leave the thing in the table.
1650 */
1651 sdkp->capacity = 0;
1652 /*
1653 * set a bogus sector size so the normal read/write
1654 * logic in the block layer will eventually refuse any
1655 * request on this device without tripping over power
1656 * of two sector size assumptions
1657 */
1658 sector_size = 512;
1659 }
Martin K. Petersene1defc42009-05-22 17:17:49 -04001660 blk_queue_logical_block_size(sdp->request_queue, sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
James Bottomley7404ad3b2008-08-31 10:41:52 -05001662 {
1663 char cap_str_2[10], cap_str_10[10];
H. Peter Anvin520a2c22008-10-14 11:34:20 -07001664 u64 sz = (u64)sdkp->capacity << ilog2(sector_size);
James Bottomley7404ad3b2008-08-31 10:41:52 -05001665
1666 string_get_size(sz, STRING_UNITS_2, cap_str_2,
1667 sizeof(cap_str_2));
1668 string_get_size(sz, STRING_UNITS_10, cap_str_10,
1669 sizeof(cap_str_10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001671 if (sdkp->first_scan || old_capacity != sdkp->capacity) {
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001672 sd_printk(KERN_NOTICE, sdkp,
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001673 "%llu %d-byte logical blocks: (%s/%s)\n",
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001674 (unsigned long long)sdkp->capacity,
1675 sector_size, cap_str_10, cap_str_2);
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001676
1677 if (sdkp->hw_sector_size != sector_size)
1678 sd_printk(KERN_NOTICE, sdkp,
1679 "%u-byte physical blocks\n",
1680 sdkp->hw_sector_size);
1681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683
1684 /* Rescale capacity to 512-byte units */
1685 if (sector_size == 4096)
1686 sdkp->capacity <<= 3;
1687 else if (sector_size == 2048)
1688 sdkp->capacity <<= 2;
1689 else if (sector_size == 1024)
1690 sdkp->capacity <<= 1;
1691 else if (sector_size == 256)
1692 sdkp->capacity >>= 1;
1693
Martin K. Petersenea09bcc2009-05-23 11:43:37 -04001694 blk_queue_physical_block_size(sdp->request_queue, sdkp->hw_sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 sdkp->device->sector_size = sector_size;
1696}
1697
1698/* called with buffer of length 512 */
1699static inline int
James Bottomleyea73a9f2005-08-28 11:33:52 -05001700sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1701 unsigned char *buffer, int len, struct scsi_mode_data *data,
1702 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
James Bottomleyea73a9f2005-08-28 11:33:52 -05001704 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
James Bottomley1cf72692005-08-28 11:27:01 -05001705 SD_TIMEOUT, SD_MAX_RETRIES, data,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001706 sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
1709/*
1710 * read write protect setting, if possible - called only in sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06001711 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 */
1713static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001714sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001717 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 struct scsi_mode_data data;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001719 int old_wp = sdkp->write_prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 set_disk_ro(sdkp->disk, 0);
James Bottomleyea73a9f2005-08-28 11:33:52 -05001722 if (sdp->skip_ms_page_3f) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001723 sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return;
1725 }
1726
James Bottomleyea73a9f2005-08-28 11:33:52 -05001727 if (sdp->use_192_bytes_for_3f) {
1728 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 } else {
1730 /*
1731 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1732 * We have to start carefully: some devices hang if we ask
1733 * for more than is available.
1734 */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001735 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /*
1738 * Second attempt: ask for page 0 When only page 0 is
1739 * implemented, a request for page 3F may return Sense Key
1740 * 5: Illegal Request, Sense Code 24: Invalid field in
1741 * CDB.
1742 */
1743 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05001744 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 /*
1747 * Third attempt: ask 255 bytes, as we did earlier.
1748 */
1749 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05001750 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1751 &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753
1754 if (!scsi_status_is_good(res)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001755 sd_printk(KERN_WARNING, sdkp,
1756 "Test WP failed, assume Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 } else {
1758 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1759 set_disk_ro(sdkp->disk, sdkp->write_prot);
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001760 if (sdkp->first_scan || old_wp != sdkp->write_prot) {
1761 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
1762 sdkp->write_prot ? "on" : "off");
1763 sd_printk(KERN_DEBUG, sdkp,
1764 "Mode Sense: %02x %02x %02x %02x\n",
1765 buffer[0], buffer[1], buffer[2], buffer[3]);
1766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768}
1769
1770/*
1771 * sd_read_cache_type - called only from sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06001772 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
1774static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001775sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
Al Viro 631e8a12005-05-16 01:59:55 +01001776{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 int len = 0, res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001778 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Al Viro 631e8a12005-05-16 01:59:55 +01001780 int dbd;
1781 int modepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 struct scsi_mode_data data;
1783 struct scsi_sense_hdr sshdr;
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001784 int old_wce = sdkp->WCE;
1785 int old_rcd = sdkp->RCD;
1786 int old_dpofua = sdkp->DPOFUA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
James Bottomleyea73a9f2005-08-28 11:33:52 -05001788 if (sdp->skip_ms_page_8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 goto defaults;
1790
James Bottomleyea73a9f2005-08-28 11:33:52 -05001791 if (sdp->type == TYPE_RBC) {
Al Viro 631e8a12005-05-16 01:59:55 +01001792 modepage = 6;
1793 dbd = 8;
1794 } else {
1795 modepage = 8;
1796 dbd = 0;
1797 }
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 /* cautiously ask */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001800 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 if (!scsi_status_is_good(res))
1803 goto bad_sense;
1804
Al Viro6d73c852006-02-23 02:03:16 +01001805 if (!data.header_length) {
1806 modepage = 6;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001807 sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
Al Viro6d73c852006-02-23 02:03:16 +01001808 }
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 /* that went OK, now ask for the proper length */
1811 len = data.length;
1812
1813 /*
1814 * We're only interested in the first three bytes, actually.
1815 * But the data cache page is defined for the first 20.
1816 */
1817 if (len < 3)
1818 goto bad_sense;
1819 if (len > 20)
1820 len = 20;
1821
1822 /* Take headers and block descriptors into account */
1823 len += data.header_length + data.block_descriptor_length;
Al Viro48970802006-02-26 08:34:10 -06001824 if (len > SD_BUF_SIZE)
1825 goto bad_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 /* Get the data */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001828 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 if (scsi_status_is_good(res)) {
Al Viro 631e8a12005-05-16 01:59:55 +01001831 int offset = data.header_length + data.block_descriptor_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Al Viro48970802006-02-26 08:34:10 -06001833 if (offset >= SD_BUF_SIZE - 2) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001834 sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
Al Viro48970802006-02-26 08:34:10 -06001835 goto defaults;
1836 }
1837
Al Viro 631e8a12005-05-16 01:59:55 +01001838 if ((buffer[offset] & 0x3f) != modepage) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001839 sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
Al Viro 631e8a12005-05-16 01:59:55 +01001840 goto defaults;
1841 }
1842
1843 if (modepage == 8) {
1844 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1845 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1846 } else {
1847 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1848 sdkp->RCD = 0;
1849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Tejun Heo007365a2006-01-06 09:53:52 +01001851 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1852 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001853 sd_printk(KERN_NOTICE, sdkp,
1854 "Uses READ/WRITE(6), disabling FUA\n");
Tejun Heo007365a2006-01-06 09:53:52 +01001855 sdkp->DPOFUA = 0;
1856 }
1857
Martin K. Petersen70a9b872009-03-09 11:33:31 -04001858 if (sdkp->first_scan || old_wce != sdkp->WCE ||
1859 old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
1860 sd_printk(KERN_NOTICE, sdkp,
1861 "Write cache: %s, read cache: %s, %s\n",
1862 sdkp->WCE ? "enabled" : "disabled",
1863 sdkp->RCD ? "disabled" : "enabled",
1864 sdkp->DPOFUA ? "supports DPO and FUA"
1865 : "doesn't support DPO or FUA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 return;
1868 }
1869
1870bad_sense:
James Bottomleyea73a9f2005-08-28 11:33:52 -05001871 if (scsi_sense_valid(&sshdr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 sshdr.sense_key == ILLEGAL_REQUEST &&
1873 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
Martin K. Petersene73aec82007-02-27 22:40:55 -05001874 /* Invalid field in CDB */
1875 sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 else
Martin K. Petersene73aec82007-02-27 22:40:55 -05001877 sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879defaults:
Martin K. Petersene73aec82007-02-27 22:40:55 -05001880 sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 sdkp->WCE = 0;
1882 sdkp->RCD = 0;
Al Viro48970802006-02-26 08:34:10 -06001883 sdkp->DPOFUA = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Martin K. Petersene0597d72008-07-17 04:28:34 -04001886/*
1887 * The ATO bit indicates whether the DIF application tag is available
1888 * for use by the operating system.
1889 */
1890void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
1891{
1892 int res, offset;
1893 struct scsi_device *sdp = sdkp->device;
1894 struct scsi_mode_data data;
1895 struct scsi_sense_hdr sshdr;
1896
1897 if (sdp->type != TYPE_DISK)
1898 return;
1899
1900 if (sdkp->protection_type == 0)
1901 return;
1902
1903 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
1904 SD_MAX_RETRIES, &data, &sshdr);
1905
1906 if (!scsi_status_is_good(res) || !data.header_length ||
1907 data.length < 6) {
1908 sd_printk(KERN_WARNING, sdkp,
1909 "getting Control mode page failed, assume no ATO\n");
1910
1911 if (scsi_sense_valid(&sshdr))
1912 sd_print_sense_hdr(sdkp, &sshdr);
1913
1914 return;
1915 }
1916
1917 offset = data.header_length + data.block_descriptor_length;
1918
1919 if ((buffer[offset] & 0x3f) != 0x0a) {
1920 sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
1921 return;
1922 }
1923
1924 if ((buffer[offset + 5] & 0x80) == 0)
1925 return;
1926
1927 sdkp->ATO = 1;
1928
1929 return;
1930}
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932/**
Martin K. Petersend11b6912009-05-23 11:43:39 -04001933 * sd_read_block_limits - Query disk device for preferred I/O sizes.
1934 * @disk: disk to query
1935 */
1936static void sd_read_block_limits(struct scsi_disk *sdkp)
1937{
Martin K. Petersene339c1a2009-11-26 12:00:40 -05001938 struct request_queue *q = sdkp->disk->queue;
Martin K. Petersend11b6912009-05-23 11:43:39 -04001939 unsigned int sector_sz = sdkp->device->sector_size;
James Bottomleye3deec02009-11-03 12:33:07 -06001940 const int vpd_len = 32;
1941 unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
Martin K. Petersend11b6912009-05-23 11:43:39 -04001942
James Bottomleye3deec02009-11-03 12:33:07 -06001943 if (!buffer ||
1944 /* Block Limits VPD */
1945 scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
1946 goto out;
Martin K. Petersend11b6912009-05-23 11:43:39 -04001947
1948 blk_queue_io_min(sdkp->disk->queue,
1949 get_unaligned_be16(&buffer[6]) * sector_sz);
1950 blk_queue_io_opt(sdkp->disk->queue,
1951 get_unaligned_be32(&buffer[12]) * sector_sz);
1952
Martin K. Petersene339c1a2009-11-26 12:00:40 -05001953 /* Thin provisioning enabled and page length indicates TP support */
1954 if (sdkp->thin_provisioning && buffer[3] == 0x3c) {
1955 unsigned int lba_count, desc_count, granularity;
1956
1957 lba_count = get_unaligned_be32(&buffer[20]);
1958 desc_count = get_unaligned_be32(&buffer[24]);
1959
1960 if (lba_count) {
1961 q->limits.max_discard_sectors =
1962 lba_count * sector_sz >> 9;
1963
1964 if (desc_count)
1965 sdkp->unmap = 1;
1966 }
1967
1968 granularity = get_unaligned_be32(&buffer[28]);
1969
1970 if (granularity)
1971 q->limits.discard_granularity = granularity * sector_sz;
1972
1973 if (buffer[32] & 0x80)
1974 q->limits.discard_alignment =
1975 get_unaligned_be32(&buffer[32]) & ~(1 << 31);
1976 }
1977
James Bottomleye3deec02009-11-03 12:33:07 -06001978 out:
Martin K. Petersend11b6912009-05-23 11:43:39 -04001979 kfree(buffer);
1980}
1981
1982/**
Martin K. Petersen3821d762009-05-23 11:43:38 -04001983 * sd_read_block_characteristics - Query block dev. characteristics
1984 * @disk: disk to query
1985 */
1986static void sd_read_block_characteristics(struct scsi_disk *sdkp)
1987{
James Bottomleye3deec02009-11-03 12:33:07 -06001988 unsigned char *buffer;
Martin K. Petersen3821d762009-05-23 11:43:38 -04001989 u16 rot;
James Bottomleye3deec02009-11-03 12:33:07 -06001990 const int vpd_len = 32;
Martin K. Petersen3821d762009-05-23 11:43:38 -04001991
James Bottomleye3deec02009-11-03 12:33:07 -06001992 buffer = kmalloc(vpd_len, GFP_KERNEL);
Martin K. Petersen3821d762009-05-23 11:43:38 -04001993
James Bottomleye3deec02009-11-03 12:33:07 -06001994 if (!buffer ||
1995 /* Block Device Characteristics VPD */
1996 scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len))
1997 goto out;
Martin K. Petersen3821d762009-05-23 11:43:38 -04001998
1999 rot = get_unaligned_be16(&buffer[4]);
2000
2001 if (rot == 1)
2002 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
2003
James Bottomleye3deec02009-11-03 12:33:07 -06002004 out:
Martin K. Petersen3821d762009-05-23 11:43:38 -04002005 kfree(buffer);
2006}
2007
Martin K. Petersenffd4bc22009-07-29 14:06:53 -04002008static int sd_try_extended_inquiry(struct scsi_device *sdp)
2009{
2010 /*
2011 * Although VPD inquiries can go to SCSI-2 type devices,
2012 * some USB ones crash on receiving them, and the pages
2013 * we currently ask for are for SPC-3 and beyond
2014 */
2015 if (sdp->scsi_level > SCSI_SPC_2)
2016 return 1;
2017 return 0;
2018}
2019
Martin K. Petersen3821d762009-05-23 11:43:38 -04002020/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 * sd_revalidate_disk - called the first time a new disk is seen,
2022 * performs disk spin up, read_capacity, etc.
2023 * @disk: struct gendisk we care about
2024 **/
2025static int sd_revalidate_disk(struct gendisk *disk)
2026{
2027 struct scsi_disk *sdkp = scsi_disk(disk);
2028 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 unsigned char *buffer;
Tejun Heo461d4e92006-01-06 09:52:55 +01002030 unsigned ordered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05002032 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
2033 "sd_revalidate_disk\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 /*
2036 * If the device is offline, don't try and read capacity or any
2037 * of the other niceties.
2038 */
2039 if (!scsi_device_online(sdp))
2040 goto out;
2041
Bernhard Wallea6123f12007-05-21 17:15:26 +02002042 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (!buffer) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002044 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
2045 "allocation failure.\n");
James Bottomleyea73a9f2005-08-28 11:33:52 -05002046 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048
Martin K. Petersene73aec82007-02-27 22:40:55 -05002049 sd_spinup_disk(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 /*
2052 * Without media there is no reason to ask; moreover, some devices
2053 * react badly if we do.
2054 */
2055 if (sdkp->media_present) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002056 sd_read_capacity(sdkp, buffer);
Martin K. Petersenffd4bc22009-07-29 14:06:53 -04002057
2058 if (sd_try_extended_inquiry(sdp)) {
2059 sd_read_block_limits(sdkp);
2060 sd_read_block_characteristics(sdkp);
2061 }
2062
Martin K. Petersene73aec82007-02-27 22:40:55 -05002063 sd_read_write_protect_flag(sdkp, buffer);
2064 sd_read_cache_type(sdkp, buffer);
Martin K. Petersene0597d72008-07-17 04:28:34 -04002065 sd_read_app_tag_own(sdkp, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
Tejun Heo461d4e92006-01-06 09:52:55 +01002067
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002068 sdkp->first_scan = 0;
2069
Tejun Heo461d4e92006-01-06 09:52:55 +01002070 /*
2071 * We now have all cache related info, determine how we deal
2072 * with ordered requests. Note that as the current SCSI
2073 * dispatch function can alter request order, we cannot use
2074 * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
2075 */
2076 if (sdkp->WCE)
Tejun Heo007365a2006-01-06 09:53:52 +01002077 ordered = sdkp->DPOFUA
2078 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
Tejun Heo461d4e92006-01-06 09:52:55 +01002079 else
2080 ordered = QUEUE_ORDERED_DRAIN;
2081
2082 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 set_capacity(disk, sdkp->capacity);
2085 kfree(buffer);
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 out:
2088 return 0;
2089}
2090
2091/**
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002092 * sd_format_disk_name - format disk name
2093 * @prefix: name prefix - ie. "sd" for SCSI disks
2094 * @index: index of the disk to format name for
2095 * @buf: output buffer
2096 * @buflen: length of the output buffer
2097 *
2098 * SCSI disk names starts at sda. The 26th device is sdz and the
2099 * 27th is sdaa. The last one for two lettered suffix is sdzz
2100 * which is followed by sdaaa.
2101 *
2102 * This is basically 26 base counting with one extra 'nil' entry
2103 * at the beggining from the second digit on and can be
2104 * determined using similar method as 26 base conversion with the
2105 * index shifted -1 after each digit is computed.
2106 *
2107 * CONTEXT:
2108 * Don't care.
2109 *
2110 * RETURNS:
2111 * 0 on success, -errno on failure.
2112 */
2113static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
2114{
2115 const int base = 'z' - 'a' + 1;
2116 char *begin = buf + strlen(prefix);
2117 char *end = buf + buflen;
2118 char *p;
2119 int unit;
2120
2121 p = end - 1;
2122 *p = '\0';
2123 unit = base;
2124 do {
2125 if (p == begin)
2126 return -EINVAL;
2127 *--p = 'a' + (index % unit);
2128 index = (index / unit) - 1;
2129 } while (index >= 0);
2130
2131 memmove(begin, p, end - p);
2132 memcpy(buf, prefix, strlen(prefix));
2133
2134 return 0;
2135}
2136
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002137/*
2138 * The asynchronous part of sd_probe
2139 */
2140static void sd_probe_async(void *data, async_cookie_t cookie)
2141{
2142 struct scsi_disk *sdkp = data;
2143 struct scsi_device *sdp;
2144 struct gendisk *gd;
2145 u32 index;
2146 struct device *dev;
2147
2148 sdp = sdkp->device;
2149 gd = sdkp->disk;
2150 index = sdkp->index;
2151 dev = &sdp->sdev_gendev;
2152
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002153 if (index < SD_MAX_DISKS) {
2154 gd->major = sd_major((index & 0xf0) >> 4);
2155 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
2156 gd->minors = SD_MINORS;
2157 }
2158 gd->fops = &sd_fops;
2159 gd->private_data = &sdkp->driver;
2160 gd->queue = sdkp->device->request_queue;
2161
Martin K. Petersen70a9b872009-03-09 11:33:31 -04002162 /* defaults, until the device tells us otherwise */
2163 sdp->sector_size = 512;
2164 sdkp->capacity = 0;
2165 sdkp->media_present = 1;
2166 sdkp->write_prot = 0;
2167 sdkp->WCE = 0;
2168 sdkp->RCD = 0;
2169 sdkp->ATO = 0;
2170 sdkp->first_scan = 1;
2171
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002172 sd_revalidate_disk(gd);
2173
2174 blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
2175
2176 gd->driverfs_dev = &sdp->sdev_gendev;
2177 gd->flags = GENHD_FL_EXT_DEVT | GENHD_FL_DRIVERFS;
2178 if (sdp->removable)
2179 gd->flags |= GENHD_FL_REMOVABLE;
2180
2181 dev_set_drvdata(dev, sdkp);
2182 add_disk(gd);
2183 sd_dif_config_host(sdkp);
2184
Martin K. Petersen3821d762009-05-23 11:43:38 -04002185 sd_revalidate_disk(gd);
2186
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002187 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
2188 sdp->removable ? "removable " : "");
James Bottomleyea038f62009-08-21 09:47:54 -06002189 put_device(&sdkp->dev);
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002190}
2191
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002192/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 * sd_probe - called during driver initialization and whenever a
2194 * new scsi device is attached to the system. It is called once
2195 * for each scsi device (not just disks) present.
2196 * @dev: pointer to device object
2197 *
2198 * Returns 0 if successful (or not interested in this scsi device
2199 * (e.g. scanner)); 1 when there is an error.
2200 *
2201 * Note: this function is invoked from the scsi mid-level.
2202 * This function sets up the mapping between a given
2203 * <host,channel,id,lun> (found in sdp) and new device name
2204 * (e.g. /dev/sda). More precisely it is the block device major
2205 * and minor number that is chosen here.
2206 *
2207 * Assume sd_attach is not re-entrant (for time being)
2208 * Also think about sd_attach() and sd_remove() running coincidentally.
2209 **/
2210static int sd_probe(struct device *dev)
2211{
2212 struct scsi_device *sdp = to_scsi_device(dev);
2213 struct scsi_disk *sdkp;
2214 struct gendisk *gd;
2215 u32 index;
2216 int error;
2217
2218 error = -ENODEV;
Al Viro 631e8a12005-05-16 01:59:55 +01002219 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 goto out;
2221
James Bottomley9ccfc752005-10-02 11:45:08 -05002222 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
2223 "sd_attach\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -05002226 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (!sdkp)
2228 goto out;
2229
Tejun Heo689d6fa2008-08-25 19:56:16 +09002230 gd = alloc_disk(SD_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (!gd)
2232 goto out_free;
2233
Tejun Heof27bac22008-07-14 14:59:30 +09002234 do {
2235 if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
2236 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Tejun Heo4034cc62009-02-21 11:04:45 +09002238 spin_lock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09002239 error = ida_get_new(&sd_index_ida, &index);
Tejun Heo4034cc62009-02-21 11:04:45 +09002240 spin_unlock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09002241 } while (error == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 if (error)
2244 goto out_put;
2245
Tejun Heo3e1a7ff2008-08-25 19:56:17 +09002246 error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
2247 if (error)
Tejun Heof27bac22008-07-14 14:59:30 +09002248 goto out_free_index;
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 sdkp->device = sdp;
2251 sdkp->driver = &sd_template;
2252 sdkp->disk = gd;
2253 sdkp->index = index;
2254 sdkp->openers = 0;
Kay Sieversc02e6002008-03-19 13:09:56 +01002255 sdkp->previous_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
James Bottomley601e7632009-05-26 20:35:48 +00002257 if (!sdp->request_queue->rq_timeout) {
2258 if (sdp->type != TYPE_MOD)
2259 blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
2260 else
2261 blk_queue_rq_timeout(sdp->request_queue,
2262 SD_MOD_TIMEOUT);
2263 }
2264
2265 device_initialize(&sdkp->dev);
2266 sdkp->dev.parent = &sdp->sdev_gendev;
2267 sdkp->dev.class = &sd_disk_class;
2268 dev_set_name(&sdkp->dev, dev_name(&sdp->sdev_gendev));
2269
2270 if (device_add(&sdkp->dev))
2271 goto out_free_index;
2272
2273 get_device(&sdp->sdev_gendev);
2274
James Bottomleyea038f62009-08-21 09:47:54 -06002275 get_device(&sdkp->dev); /* prevent release before async_schedule */
Arjan van de Ven4ace92f2009-01-04 05:32:28 -08002276 async_schedule(sd_probe_async, sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 return 0;
2279
Tejun Heof27bac22008-07-14 14:59:30 +09002280 out_free_index:
Tejun Heo4034cc62009-02-21 11:04:45 +09002281 spin_lock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09002282 ida_remove(&sd_index_ida, index);
Tejun Heo4034cc62009-02-21 11:04:45 +09002283 spin_unlock(&sd_index_lock);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06002284 out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 put_disk(gd);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06002286 out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 kfree(sdkp);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06002288 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return error;
2290}
2291
2292/**
2293 * sd_remove - called whenever a scsi disk (previously recognized by
2294 * sd_probe) is detached from the system. It is called (potentially
2295 * multiple times) during sd module unload.
2296 * @sdp: pointer to mid level scsi device object
2297 *
2298 * Note: this function is invoked from the scsi mid-level.
2299 * This function potentially frees up a device name (e.g. /dev/sdc)
2300 * that could be re-used by a subsequent sd_probe().
2301 * This function is not called when the built-in sd driver is "exit-ed".
2302 **/
2303static int sd_remove(struct device *dev)
2304{
James Bottomley601e7632009-05-26 20:35:48 +00002305 struct scsi_disk *sdkp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
James Bottomley601e7632009-05-26 20:35:48 +00002307 async_synchronize_full();
2308 sdkp = dev_get_drvdata(dev);
Hannes Reineckeb3912772009-06-18 09:57:18 +02002309 blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
Tony Jonesee959b02008-02-22 00:13:36 +01002310 device_del(&sdkp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 del_gendisk(sdkp->disk);
2312 sd_shutdown(dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -05002313
Arjan van de Ven0b950672006-01-11 13:16:10 +01002314 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -05002315 dev_set_drvdata(dev, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01002316 put_device(&sdkp->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01002317 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 return 0;
2320}
2321
2322/**
2323 * scsi_disk_release - Called to free the scsi_disk structure
Tony Jonesee959b02008-02-22 00:13:36 +01002324 * @dev: pointer to embedded class device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 *
Arjan van de Ven0b950672006-01-11 13:16:10 +01002326 * sd_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 * called on last put, you should always use the scsi_disk_get()
2328 * scsi_disk_put() helpers which manipulate the semaphore directly
Tony Jonesee959b02008-02-22 00:13:36 +01002329 * and never do a direct put_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 **/
Tony Jonesee959b02008-02-22 00:13:36 +01002331static void scsi_disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
Tony Jonesee959b02008-02-22 00:13:36 +01002333 struct scsi_disk *sdkp = to_scsi_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 struct gendisk *disk = sdkp->disk;
2335
Tejun Heo4034cc62009-02-21 11:04:45 +09002336 spin_lock(&sd_index_lock);
Tejun Heof27bac22008-07-14 14:59:30 +09002337 ida_remove(&sd_index_ida, sdkp->index);
Tejun Heo4034cc62009-02-21 11:04:45 +09002338 spin_unlock(&sd_index_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 disk->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 put_disk(disk);
Alan Stern39b7f1e2005-11-04 14:44:41 -05002342 put_device(&sdkp->device->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 kfree(sdkp);
2345}
2346
James Bottomleycc5d2c82007-03-20 12:26:03 -05002347static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002348{
2349 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
2350 struct scsi_sense_hdr sshdr;
James Bottomleycc5d2c82007-03-20 12:26:03 -05002351 struct scsi_device *sdp = sdkp->device;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002352 int res;
2353
2354 if (start)
2355 cmd[4] |= 1; /* START */
2356
Stefan Richterd2886ea2008-05-11 00:34:07 +02002357 if (sdp->start_stop_pwr_cond)
2358 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
2359
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002360 if (!scsi_device_online(sdp))
2361 return -ENODEV;
2362
2363 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +09002364 SD_TIMEOUT, SD_MAX_RETRIES, NULL);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002365 if (res) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05002366 sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
2367 sd_print_result(sdkp, res);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002368 if (driver_byte(res) & DRIVER_SENSE)
James Bottomleycc5d2c82007-03-20 12:26:03 -05002369 sd_print_sense_hdr(sdkp, &sshdr);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002370 }
2371
2372 return res;
2373}
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375/*
2376 * Send a SYNCHRONIZE CACHE instruction down to the device through
2377 * the normal SCSI command structure. Wait for the command to
2378 * complete.
2379 */
2380static void sd_shutdown(struct device *dev)
2381{
Alan Stern39b7f1e2005-11-04 14:44:41 -05002382 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 if (!sdkp)
2385 return; /* this can happen */
2386
Alan Stern39b7f1e2005-11-04 14:44:41 -05002387 if (sdkp->WCE) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05002388 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
2389 sd_sync_cache(sdkp);
Alan Stern39b7f1e2005-11-04 14:44:41 -05002390 }
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002391
James Bottomleycc5d2c82007-03-20 12:26:03 -05002392 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
2393 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2394 sd_start_stop_device(sdkp, 0);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002395 }
2396
Alan Stern39b7f1e2005-11-04 14:44:41 -05002397 scsi_disk_put(sdkp);
2398}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002400static int sd_suspend(struct device *dev, pm_message_t mesg)
2401{
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002402 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Alan Stern09ff92f2007-05-21 09:55:04 -04002403 int ret = 0;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002404
2405 if (!sdkp)
2406 return 0; /* this can happen */
2407
2408 if (sdkp->WCE) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05002409 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002410 ret = sd_sync_cache(sdkp);
2411 if (ret)
Alan Stern09ff92f2007-05-21 09:55:04 -04002412 goto done;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002413 }
2414
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01002415 if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05002416 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2417 ret = sd_start_stop_device(sdkp, 0);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002418 }
2419
Alan Stern09ff92f2007-05-21 09:55:04 -04002420done:
2421 scsi_disk_put(sdkp);
2422 return ret;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002423}
2424
2425static int sd_resume(struct device *dev)
2426{
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002427 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Alan Stern09ff92f2007-05-21 09:55:04 -04002428 int ret = 0;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002429
James Bottomleycc5d2c82007-03-20 12:26:03 -05002430 if (!sdkp->device->manage_start_stop)
Alan Stern09ff92f2007-05-21 09:55:04 -04002431 goto done;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002432
James Bottomleycc5d2c82007-03-20 12:26:03 -05002433 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
Alan Stern09ff92f2007-05-21 09:55:04 -04002434 ret = sd_start_stop_device(sdkp, 1);
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002435
Alan Stern09ff92f2007-05-21 09:55:04 -04002436done:
2437 scsi_disk_put(sdkp);
2438 return ret;
Tejun Heoc3c94c5a2007-03-21 00:13:59 +09002439}
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441/**
2442 * init_sd - entry point for this driver (both when built in or when
2443 * a module).
2444 *
2445 * Note: this function registers this driver with the scsi mid-level.
2446 **/
2447static int __init init_sd(void)
2448{
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002449 int majors = 0, i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
2452
2453 for (i = 0; i < SD_MAJORS; i++)
2454 if (register_blkdev(sd_major(i), "sd") == 0)
2455 majors++;
2456
2457 if (!majors)
2458 return -ENODEV;
2459
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002460 err = class_register(&sd_disk_class);
2461 if (err)
2462 goto err_out;
James Bottomley6bdaa1f2006-03-18 14:14:21 -06002463
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002464 err = scsi_register_driver(&sd_template.gendrv);
2465 if (err)
2466 goto err_out_class;
2467
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04002468 sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
2469 0, 0, NULL);
2470 if (!sd_cdb_cache) {
2471 printk(KERN_ERR "sd: can't init extended cdb cache\n");
2472 goto err_out_class;
2473 }
2474
2475 sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
2476 if (!sd_cdb_pool) {
2477 printk(KERN_ERR "sd: can't init extended cdb pool\n");
2478 goto err_out_cache;
2479 }
2480
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002481 return 0;
2482
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04002483err_out_cache:
2484 kmem_cache_destroy(sd_cdb_cache);
2485
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002486err_out_class:
2487 class_unregister(&sd_disk_class);
2488err_out:
2489 for (i = 0; i < SD_MAJORS; i++)
2490 unregister_blkdev(sd_major(i), "sd");
2491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
2494/**
2495 * exit_sd - exit point for this driver (when it is a module).
2496 *
2497 * Note: this function unregisters this driver from the scsi mid-level.
2498 **/
2499static void __exit exit_sd(void)
2500{
2501 int i;
2502
2503 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
2504
Martin K. Petersen4e7392e2009-09-20 16:49:38 -04002505 mempool_destroy(sd_cdb_pool);
2506 kmem_cache_destroy(sd_cdb_cache);
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 scsi_unregister_driver(&sd_template.gendrv);
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002509 class_unregister(&sd_disk_class);
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 for (i = 0; i < SD_MAJORS; i++)
2512 unregister_blkdev(sd_major(i), "sd");
2513}
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515module_init(init_sd);
2516module_exit(exit_sd);
Martin K. Petersene73aec82007-02-27 22:40:55 -05002517
2518static void sd_print_sense_hdr(struct scsi_disk *sdkp,
2519 struct scsi_sense_hdr *sshdr)
2520{
2521 sd_printk(KERN_INFO, sdkp, "");
2522 scsi_show_sense_hdr(sshdr);
2523 sd_printk(KERN_INFO, sdkp, "");
2524 scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
2525}
2526
2527static void sd_print_result(struct scsi_disk *sdkp, int result)
2528{
2529 sd_printk(KERN_INFO, sdkp, "");
2530 scsi_show_result(result);
2531}
2532