blob: 8e08d51a0f0531d8bf661dc0d3bf8f822d35c357 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/uaccess.h>
51
52#include <scsi/scsi.h>
53#include <scsi/scsi_cmnd.h>
54#include <scsi/scsi_dbg.h>
55#include <scsi/scsi_device.h>
56#include <scsi/scsi_driver.h>
57#include <scsi/scsi_eh.h>
58#include <scsi/scsi_host.h>
59#include <scsi/scsi_ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <scsi/scsicam.h>
61
Martin K. Petersenaa916962008-06-17 12:47:32 -040062#include "sd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "scsi_logging.h"
64
Rene Hermanf018fa52006-03-08 00:14:20 -080065MODULE_AUTHOR("Eric Youngdale");
66MODULE_DESCRIPTION("SCSI disk (sd) driver");
67MODULE_LICENSE("GPL");
68
69MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
70MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
71MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
72MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
73MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
74MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
75MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
76MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
77MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
78MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
79MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
80MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
81MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
82MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
83MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
84MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
Michael Tokarevd7b8bcb2006-10-27 16:02:37 +040085MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
86MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
87MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
Rene Hermanf018fa52006-03-08 00:14:20 -080088
Linus Torvalds7b3d9542008-01-06 10:17:12 -080089static int sd_revalidate_disk(struct gendisk *);
90static int sd_probe(struct device *);
91static int sd_remove(struct device *);
92static void sd_shutdown(struct device *);
93static int sd_suspend(struct device *, pm_message_t state);
94static int sd_resume(struct device *);
95static void sd_rescan(struct device *);
96static int sd_done(struct scsi_cmnd *);
97static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
Tony Jonesee959b02008-02-22 00:13:36 +010098static void scsi_disk_release(struct device *cdev);
Linus Torvalds7b3d9542008-01-06 10:17:12 -080099static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
100static void sd_print_result(struct scsi_disk *, int);
101
Tejun Heof27bac22008-07-14 14:59:30 +0900102static DEFINE_IDA(sd_index_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/* This semaphore is used to mediate the 0->1 reference get in the
105 * face of object destruction (i.e. we can't allow a get on an
106 * object after last put) */
Arjan van de Ven0b950672006-01-11 13:16:10 +0100107static DEFINE_MUTEX(sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600109static const char *sd_cache_types[] = {
110 "write through", "none", "write back",
111 "write back, no read (daft)"
112};
113
Tony Jonesee959b02008-02-22 00:13:36 +0100114static ssize_t
115sd_store_cache_type(struct device *dev, struct device_attribute *attr,
116 const char *buf, size_t count)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600117{
118 int i, ct = -1, rcd, wce, sp;
Tony Jonesee959b02008-02-22 00:13:36 +0100119 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600120 struct scsi_device *sdp = sdkp->device;
121 char buffer[64];
122 char *buffer_data;
123 struct scsi_mode_data data;
124 struct scsi_sense_hdr sshdr;
125 int len;
126
127 if (sdp->type != TYPE_DISK)
128 /* no cache control on RBC devices; theoretically they
129 * can do it, but there's probably so many exceptions
130 * it's not worth the risk */
131 return -EINVAL;
132
Tobias Klauser6391a112006-06-08 22:23:48 -0700133 for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600134 const int len = strlen(sd_cache_types[i]);
135 if (strncmp(sd_cache_types[i], buf, len) == 0 &&
136 buf[len] == '\n') {
137 ct = i;
138 break;
139 }
140 }
141 if (ct < 0)
142 return -EINVAL;
143 rcd = ct & 0x01 ? 1 : 0;
144 wce = ct & 0x02 ? 1 : 0;
145 if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
146 SD_MAX_RETRIES, &data, NULL))
147 return -EINVAL;
Andrew Mortona9312fb2006-03-25 03:08:30 -0800148 len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600149 data.block_descriptor_length);
150 buffer_data = buffer + data.header_length +
151 data.block_descriptor_length;
152 buffer_data[2] &= ~0x05;
153 buffer_data[2] |= wce << 2 | rcd;
154 sp = buffer_data[0] & 0x80 ? 1 : 0;
155
156 if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
157 SD_MAX_RETRIES, &data, &sshdr)) {
158 if (scsi_sense_valid(&sshdr))
Martin K. Petersene73aec82007-02-27 22:40:55 -0500159 sd_print_sense_hdr(sdkp, &sshdr);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600160 return -EINVAL;
161 }
162 sd_revalidate_disk(sdkp->disk);
163 return count;
164}
165
Tony Jonesee959b02008-02-22 00:13:36 +0100166static ssize_t
167sd_store_manage_start_stop(struct device *dev, struct device_attribute *attr,
168 const char *buf, size_t count)
Tejun Heoc3c94c52007-03-21 00:13:59 +0900169{
Tony Jonesee959b02008-02-22 00:13:36 +0100170 struct scsi_disk *sdkp = to_scsi_disk(dev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900171 struct scsi_device *sdp = sdkp->device;
172
173 if (!capable(CAP_SYS_ADMIN))
174 return -EACCES;
175
176 sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
177
178 return count;
179}
180
Tony Jonesee959b02008-02-22 00:13:36 +0100181static ssize_t
182sd_store_allow_restart(struct device *dev, struct device_attribute *attr,
183 const char *buf, size_t count)
Brian Kinga144c5a2006-06-27 11:10:31 -0500184{
Tony Jonesee959b02008-02-22 00:13:36 +0100185 struct scsi_disk *sdkp = to_scsi_disk(dev);
Brian Kinga144c5a2006-06-27 11:10:31 -0500186 struct scsi_device *sdp = sdkp->device;
187
188 if (!capable(CAP_SYS_ADMIN))
189 return -EACCES;
190
191 if (sdp->type != TYPE_DISK)
192 return -EINVAL;
193
194 sdp->allow_restart = simple_strtoul(buf, NULL, 10);
195
196 return count;
197}
198
Tony Jonesee959b02008-02-22 00:13:36 +0100199static ssize_t
200sd_show_cache_type(struct device *dev, struct device_attribute *attr,
201 char *buf)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600202{
Tony Jonesee959b02008-02-22 00:13:36 +0100203 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600204 int ct = sdkp->RCD + 2*sdkp->WCE;
205
206 return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
207}
208
Tony Jonesee959b02008-02-22 00:13:36 +0100209static ssize_t
210sd_show_fua(struct device *dev, struct device_attribute *attr, char *buf)
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600211{
Tony Jonesee959b02008-02-22 00:13:36 +0100212 struct scsi_disk *sdkp = to_scsi_disk(dev);
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600213
214 return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
215}
216
Tony Jonesee959b02008-02-22 00:13:36 +0100217static ssize_t
218sd_show_manage_start_stop(struct device *dev, struct device_attribute *attr,
219 char *buf)
Tejun Heoc3c94c52007-03-21 00:13:59 +0900220{
Tony Jonesee959b02008-02-22 00:13:36 +0100221 struct scsi_disk *sdkp = to_scsi_disk(dev);
Tejun Heoc3c94c52007-03-21 00:13:59 +0900222 struct scsi_device *sdp = sdkp->device;
223
224 return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
225}
226
Tony Jonesee959b02008-02-22 00:13:36 +0100227static ssize_t
228sd_show_allow_restart(struct device *dev, struct device_attribute *attr,
229 char *buf)
Brian Kinga144c5a2006-06-27 11:10:31 -0500230{
Tony Jonesee959b02008-02-22 00:13:36 +0100231 struct scsi_disk *sdkp = to_scsi_disk(dev);
Brian Kinga144c5a2006-06-27 11:10:31 -0500232
233 return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
234}
235
Martin K. Petersene0597d72008-07-17 04:28:34 -0400236static ssize_t
237sd_show_protection_type(struct device *dev, struct device_attribute *attr,
238 char *buf)
239{
240 struct scsi_disk *sdkp = to_scsi_disk(dev);
241
242 return snprintf(buf, 20, "%u\n", sdkp->protection_type);
243}
244
245static ssize_t
246sd_show_app_tag_own(struct device *dev, struct device_attribute *attr,
247 char *buf)
248{
249 struct scsi_disk *sdkp = to_scsi_disk(dev);
250
251 return snprintf(buf, 20, "%u\n", sdkp->ATO);
252}
253
Tony Jonesee959b02008-02-22 00:13:36 +0100254static struct device_attribute sd_disk_attrs[] = {
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600255 __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
256 sd_store_cache_type),
257 __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
Brian Kinga144c5a2006-06-27 11:10:31 -0500258 __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
259 sd_store_allow_restart),
Tejun Heoc3c94c52007-03-21 00:13:59 +0900260 __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
261 sd_store_manage_start_stop),
Martin K. Petersene0597d72008-07-17 04:28:34 -0400262 __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
263 __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600264 __ATTR_NULL,
265};
266
267static struct class sd_disk_class = {
268 .name = "scsi_disk",
269 .owner = THIS_MODULE,
Tony Jonesee959b02008-02-22 00:13:36 +0100270 .dev_release = scsi_disk_release,
271 .dev_attrs = sd_disk_attrs,
James Bottomley6bdaa1f2006-03-18 14:14:21 -0600272};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274static struct scsi_driver sd_template = {
275 .owner = THIS_MODULE,
276 .gendrv = {
277 .name = "sd",
278 .probe = sd_probe,
279 .remove = sd_remove,
Tejun Heoc3c94c52007-03-21 00:13:59 +0900280 .suspend = sd_suspend,
281 .resume = sd_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 .shutdown = sd_shutdown,
283 },
284 .rescan = sd_rescan,
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800285 .done = sd_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286};
287
288/*
289 * Device no to disk mapping:
290 *
291 * major disc2 disc p1
292 * |............|.............|....|....| <- dev_t
293 * 31 20 19 8 7 4 3 0
294 *
295 * Inside a major, we have 16k disks, however mapped non-
296 * contiguously. The first 16 disks are for major0, the next
297 * ones with major1, ... Disk 256 is for major0 again, disk 272
298 * for major1, ...
299 * As we stay compatible with our numbering scheme, we can reuse
300 * the well-know SCSI majors 8, 65--71, 136--143.
301 */
302static int sd_major(int major_idx)
303{
304 switch (major_idx) {
305 case 0:
306 return SCSI_DISK0_MAJOR;
307 case 1 ... 7:
308 return SCSI_DISK1_MAJOR + major_idx - 1;
309 case 8 ... 15:
310 return SCSI_DISK8_MAJOR + major_idx - 8;
311 default:
312 BUG();
313 return 0; /* shut up gcc */
314 }
315}
316
Alan Stern39b7f1e2005-11-04 14:44:41 -0500317static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct scsi_disk *sdkp = NULL;
320
Alan Stern39b7f1e2005-11-04 14:44:41 -0500321 if (disk->private_data) {
322 sdkp = scsi_disk(disk);
323 if (scsi_device_get(sdkp->device) == 0)
Tony Jonesee959b02008-02-22 00:13:36 +0100324 get_device(&sdkp->dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500325 else
326 sdkp = NULL;
327 }
328 return sdkp;
329}
330
331static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
332{
333 struct scsi_disk *sdkp;
334
Arjan van de Ven0b950672006-01-11 13:16:10 +0100335 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500336 sdkp = __scsi_disk_get(disk);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100337 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return sdkp;
Alan Stern39b7f1e2005-11-04 14:44:41 -0500339}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Alan Stern39b7f1e2005-11-04 14:44:41 -0500341static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
342{
343 struct scsi_disk *sdkp;
344
Arjan van de Ven0b950672006-01-11 13:16:10 +0100345 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -0500346 sdkp = dev_get_drvdata(dev);
347 if (sdkp)
348 sdkp = __scsi_disk_get(sdkp->disk);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100349 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return sdkp;
351}
352
353static void scsi_disk_put(struct scsi_disk *sdkp)
354{
355 struct scsi_device *sdev = sdkp->device;
356
Arjan van de Ven0b950672006-01-11 13:16:10 +0100357 mutex_lock(&sd_ref_mutex);
Tony Jonesee959b02008-02-22 00:13:36 +0100358 put_device(&sdkp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 scsi_device_put(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100360 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/**
364 * sd_init_command - build a scsi (read or write) command from
365 * information in the request structure.
366 * @SCpnt: pointer to mid-level's per scsi command structure that
367 * contains request and into which the scsi command is written
368 *
369 * Returns 1 if successful and 0 if error (or cannot be done now).
370 **/
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500371static int sd_prep_fn(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500373 struct scsi_cmnd *SCpnt;
374 struct scsi_device *sdp = q->queuedata;
Christoph Hellwig776b23a2006-01-06 18:34:07 +0100375 struct gendisk *disk = rq->rq_disk;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400376 struct scsi_disk *sdkp;
Christoph Hellwig776b23a2006-01-06 18:34:07 +0100377 sector_t block = rq->sector;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500378 unsigned int this_count = rq->nr_sectors;
Christoph Hellwig776b23a2006-01-06 18:34:07 +0100379 unsigned int timeout = sdp->timeout;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500380 int ret;
381
382 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
383 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
384 goto out;
385 } else if (rq->cmd_type != REQ_TYPE_FS) {
386 ret = BLKPREP_KILL;
387 goto out;
388 }
389 ret = scsi_setup_fs_cmnd(sdp, rq);
390 if (ret != BLKPREP_OK)
391 goto out;
392 SCpnt = rq->special;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400393 sdkp = scsi_disk(disk);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500394
395 /* from here on until we're complete, any goto out
396 * is used for a killable error condition */
397 ret = BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500399 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
400 "sd_init_command: block=%llu, "
401 "count=%d\n",
402 (unsigned long long)block,
403 this_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!sdp || !scsi_device_online(sdp) ||
406 block + rq->nr_sectors > get_capacity(disk)) {
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500407 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
408 "Finishing %ld sectors\n",
409 rq->nr_sectors));
410 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
411 "Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 if (sdp->changed) {
416 /*
417 * quietly refuse to do anything to a changed disc until
418 * the changed bit has been reset
419 */
420 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500421 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500423
Hans de Goedea0899d42008-01-20 11:12:26 +0100424 /*
425 * Some devices (some sdcards for one) don't like it if the
426 * last sector gets read in a larger then 1 sector read.
427 */
428 if (unlikely(sdp->last_sector_bug &&
429 rq->nr_sectors > sdp->sector_size / 512 &&
430 block + this_count == get_capacity(disk)))
431 this_count -= sdp->sector_size / 512;
432
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500433 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
434 (unsigned long long)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /*
437 * If we have a 1K hardware sectorsize, prevent access to single
438 * 512 byte sectors. In theory we could handle this - in fact
439 * the scsi cdrom driver must be able to handle this because
440 * we typically use 1K blocksizes, and cdroms typically have
441 * 2K hardware sectorsizes. Of course, things are simpler
442 * with the cdrom, since it is read-only. For performance
443 * reasons, the filesystems should be able to handle this
444 * and not force the scsi disk driver to use bounce buffers
445 * for this.
446 */
447 if (sdp->sector_size == 1024) {
448 if ((block & 1) || (rq->nr_sectors & 1)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500449 scmd_printk(KERN_ERR, SCpnt,
450 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500451 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 } else {
453 block = block >> 1;
454 this_count = this_count >> 1;
455 }
456 }
457 if (sdp->sector_size == 2048) {
458 if ((block & 3) || (rq->nr_sectors & 3)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500459 scmd_printk(KERN_ERR, SCpnt,
460 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500461 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 } else {
463 block = block >> 2;
464 this_count = this_count >> 2;
465 }
466 }
467 if (sdp->sector_size == 4096) {
468 if ((block & 7) || (rq->nr_sectors & 7)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500469 scmd_printk(KERN_ERR, SCpnt,
470 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500471 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 } else {
473 block = block >> 3;
474 this_count = this_count >> 3;
475 }
476 }
477 if (rq_data_dir(rq) == WRITE) {
478 if (!sdp->writeable) {
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500479 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 SCpnt->cmnd[0] = WRITE_6;
482 SCpnt->sc_data_direction = DMA_TO_DEVICE;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400483
484 if (blk_integrity_rq(rq) &&
485 sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
486 goto out;
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 } else if (rq_data_dir(rq) == READ) {
489 SCpnt->cmnd[0] = READ_6;
490 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
491 } else {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500492 scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500493 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500496 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
497 "%s %d/%ld 512 byte blocks.\n",
498 (rq_data_dir(rq) == WRITE) ?
499 "writing" : "reading", this_count,
500 rq->nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400502 /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
503 if (scsi_host_dif_capable(sdp->host, sdkp->protection_type))
504 SCpnt->cmnd[1] = 1 << 5;
505 else
506 SCpnt->cmnd[1] = 0;
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (block > 0xffffffff) {
509 SCpnt->cmnd[0] += READ_16 - READ_6;
Tejun Heo007365a2006-01-06 09:53:52 +0100510 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
512 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
513 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
514 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
515 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
516 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
517 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
518 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
519 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
520 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
521 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
522 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
523 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
524 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400525 scsi_device_protection(SCpnt->device) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 SCpnt->device->use_10_for_rw) {
527 if (this_count > 0xffff)
528 this_count = 0xffff;
529
530 SCpnt->cmnd[0] += READ_10 - READ_6;
Tejun Heo007365a2006-01-06 09:53:52 +0100531 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
533 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
534 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
535 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
536 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
537 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
538 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
539 } else {
Tejun Heo007365a2006-01-06 09:53:52 +0100540 if (unlikely(blk_fua_rq(rq))) {
541 /*
542 * This happens only if this drive failed
543 * 10byte rw command with ILLEGAL_REQUEST
544 * during operation and thus turned off
545 * use_10_for_rw.
546 */
Martin K. Petersene73aec82007-02-27 22:40:55 -0500547 scmd_printk(KERN_ERR, SCpnt,
548 "FUA write on READ/WRITE(6) drive\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500549 goto out;
Tejun Heo007365a2006-01-06 09:53:52 +0100550 }
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
553 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
554 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
555 SCpnt->cmnd[4] = (unsigned char) this_count;
556 SCpnt->cmnd[5] = 0;
557 }
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200558 SCpnt->sdb.length = this_count * sdp->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400560 /* If DIF or DIX is enabled, tell HBA how to handle request */
561 if (sdkp->protection_type || scsi_prot_sg_count(SCpnt))
562 sd_dif_op(SCpnt, sdkp->protection_type, scsi_prot_sg_count(SCpnt));
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * We shouldn't disconnect in the middle of a sector, so with a dumb
566 * host adapter, it's safe to assume that we can at least transfer
567 * this many bytes between each connect / disconnect.
568 */
569 SCpnt->transfersize = sdp->sector_size;
570 SCpnt->underflow = this_count << 9;
571 SCpnt->allowed = SD_MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 SCpnt->timeout_per_command = timeout;
573
574 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * This indicates that the command is ready from our end to be
576 * queued.
577 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500578 ret = BLKPREP_OK;
579 out:
580 return scsi_prep_return(q, rq, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583/**
584 * sd_open - open a scsi disk device
585 * @inode: only i_rdev member may be used
586 * @filp: only f_mode and f_flags may be used
587 *
588 * Returns 0 if successful. Returns a negated errno value in case
589 * of error.
590 *
591 * Note: This can be called from a user context (e.g. fsck(1) )
592 * or from within the kernel (e.g. as a result of a mount(1) ).
593 * In the latter case @inode and @filp carry an abridged amount
594 * of information as noted above.
595 **/
596static int sd_open(struct inode *inode, struct file *filp)
597{
598 struct gendisk *disk = inode->i_bdev->bd_disk;
599 struct scsi_disk *sdkp;
600 struct scsi_device *sdev;
601 int retval;
602
603 if (!(sdkp = scsi_disk_get(disk)))
604 return -ENXIO;
605
606
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500607 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 sdev = sdkp->device;
610
611 /*
612 * If the device is in error recovery, wait until it is done.
613 * If the device is offline, then disallow any access to it.
614 */
615 retval = -ENXIO;
616 if (!scsi_block_when_processing_errors(sdev))
617 goto error_out;
618
619 if (sdev->removable || sdkp->write_prot)
620 check_disk_change(inode->i_bdev);
621
622 /*
623 * If the drive is empty, just let the open fail.
624 */
625 retval = -ENOMEDIUM;
626 if (sdev->removable && !sdkp->media_present &&
627 !(filp->f_flags & O_NDELAY))
628 goto error_out;
629
630 /*
631 * If the device has the write protect tab set, have the open fail
632 * if the user expects to be able to write to the thing.
633 */
634 retval = -EROFS;
635 if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
636 goto error_out;
637
638 /*
639 * It is possible that the disk changing stuff resulted in
640 * the device being taken offline. If this is the case,
641 * report this to the user, and don't pretend that the
642 * open actually succeeded.
643 */
644 retval = -ENXIO;
645 if (!scsi_device_online(sdev))
646 goto error_out;
647
648 if (!sdkp->openers++ && sdev->removable) {
649 if (scsi_block_when_processing_errors(sdev))
650 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
651 }
652
653 return 0;
654
655error_out:
656 scsi_disk_put(sdkp);
657 return retval;
658}
659
660/**
661 * sd_release - invoked when the (last) close(2) is called on this
662 * scsi disk.
663 * @inode: only i_rdev member may be used
664 * @filp: only f_mode and f_flags may be used
665 *
666 * Returns 0.
667 *
668 * Note: may block (uninterruptible) if error recovery is underway
669 * on this disk.
670 **/
671static int sd_release(struct inode *inode, struct file *filp)
672{
673 struct gendisk *disk = inode->i_bdev->bd_disk;
674 struct scsi_disk *sdkp = scsi_disk(disk);
675 struct scsi_device *sdev = sdkp->device;
676
James Bottomley56937f72007-03-11 12:25:33 -0500677 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (!--sdkp->openers && sdev->removable) {
680 if (scsi_block_when_processing_errors(sdev))
681 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
682 }
683
684 /*
685 * XXX and what if there are packets in flight and this close()
686 * XXX is followed by a "rmmod sd_mod"?
687 */
688 scsi_disk_put(sdkp);
689 return 0;
690}
691
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800692static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
695 struct scsi_device *sdp = sdkp->device;
696 struct Scsi_Host *host = sdp->host;
697 int diskinfo[4];
698
699 /* default to most commonly used values */
700 diskinfo[0] = 0x40; /* 1 << 6 */
701 diskinfo[1] = 0x20; /* 1 << 5 */
702 diskinfo[2] = sdkp->capacity >> 11;
703
704 /* override with calculated, extended default, or driver values */
705 if (host->hostt->bios_param)
706 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
707 else
708 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
709
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800710 geo->heads = diskinfo[0];
711 geo->sectors = diskinfo[1];
712 geo->cylinders = diskinfo[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 0;
714}
715
716/**
717 * sd_ioctl - process an ioctl
718 * @inode: only i_rdev/i_bdev members may be used
719 * @filp: only f_mode and f_flags may be used
720 * @cmd: ioctl command number
721 * @arg: this is third argument given to ioctl(2) system call.
722 * Often contains a pointer.
723 *
724 * Returns 0 if successful (some ioctls return postive numbers on
725 * success as well). Returns a negated errno value in case of error.
726 *
727 * Note: most ioctls are forward onto the block subsystem or further
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200728 * down in the scsi subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 **/
730static int sd_ioctl(struct inode * inode, struct file * filp,
731 unsigned int cmd, unsigned long arg)
732{
733 struct block_device *bdev = inode->i_bdev;
734 struct gendisk *disk = bdev->bd_disk;
735 struct scsi_device *sdp = scsi_disk(disk)->device;
736 void __user *p = (void __user *)arg;
737 int error;
738
739 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
740 disk->disk_name, cmd));
741
742 /*
743 * If we are in the middle of error recovery, don't let anyone
744 * else try and use this device. Also, if error recovery fails, it
745 * may try and take the device offline, in which case all further
746 * access to the device is prohibited.
747 */
748 error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
749 if (!scsi_block_when_processing_errors(sdp) || !error)
750 return error;
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /*
753 * Send SCSI addressing ioctls directly to mid level, send other
754 * ioctls to block level and then onto mid level if they can't be
755 * resolved.
756 */
757 switch (cmd) {
758 case SCSI_IOCTL_GET_IDLUN:
759 case SCSI_IOCTL_GET_BUS_NUMBER:
760 return scsi_ioctl(sdp, cmd, p);
761 default:
FUJITA Tomonori45e79a32007-07-09 12:39:20 +0200762 error = scsi_cmd_ioctl(filp, disk->queue, disk, cmd, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (error != -ENOTTY)
764 return error;
765 }
766 return scsi_ioctl(sdp, cmd, p);
767}
768
769static void set_media_not_present(struct scsi_disk *sdkp)
770{
771 sdkp->media_present = 0;
772 sdkp->capacity = 0;
773 sdkp->device->changed = 1;
774}
775
776/**
777 * sd_media_changed - check if our medium changed
778 * @disk: kernel device descriptor
779 *
780 * Returns 0 if not applicable or no change; 1 if change
781 *
782 * Note: this function is invoked from the block subsystem.
783 **/
784static int sd_media_changed(struct gendisk *disk)
785{
786 struct scsi_disk *sdkp = scsi_disk(disk);
787 struct scsi_device *sdp = sdkp->device;
James Bottomley001aac22007-12-02 19:10:40 +0200788 struct scsi_sense_hdr *sshdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 int retval;
790
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500791 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (!sdp->removable)
794 return 0;
795
796 /*
797 * If the device is offline, don't send any commands - just pretend as
798 * if the command failed. If the device ever comes back online, we
799 * can deal with it then. It is only because of unrecoverable errors
800 * that we would ever take a device offline in the first place.
801 */
Kay Sievers285e9672007-08-14 14:10:39 +0200802 if (!scsi_device_online(sdp)) {
803 set_media_not_present(sdkp);
804 retval = 1;
805 goto out;
806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /*
809 * Using TEST_UNIT_READY enables differentiation between drive with
810 * no cartridge loaded - NOT READY, drive with changed cartridge -
811 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
812 *
813 * Drives that auto spin down. eg iomega jaz 1G, will be started
814 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
815 * sd_revalidate() is called.
816 */
817 retval = -ENODEV;
Kay Sievers285e9672007-08-14 14:10:39 +0200818
James Bottomley001aac22007-12-02 19:10:40 +0200819 if (scsi_block_when_processing_errors(sdp)) {
820 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
821 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
822 sshdr);
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /*
826 * Unable to test, unit probably not ready. This usually
827 * means there is no disc in the drive. Mark as changed,
828 * and we will figure it out later once the drive is
829 * available again.
830 */
James Bottomley001aac22007-12-02 19:10:40 +0200831 if (retval || (scsi_sense_valid(sshdr) &&
832 /* 0x3a is medium not present */
833 sshdr->asc == 0x3a)) {
Kay Sievers285e9672007-08-14 14:10:39 +0200834 set_media_not_present(sdkp);
835 retval = 1;
836 goto out;
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /*
840 * For removable scsi disk we have to recognise the presence
841 * of a disk in the drive. This is kept in the struct scsi_disk
842 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
843 */
844 sdkp->media_present = 1;
845
846 retval = sdp->changed;
847 sdp->changed = 0;
Kay Sievers285e9672007-08-14 14:10:39 +0200848out:
849 if (retval != sdkp->previous_state)
850 sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
851 sdkp->previous_state = retval;
James Bottomley001aac22007-12-02 19:10:40 +0200852 kfree(sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
Martin K. Petersene73aec82007-02-27 22:40:55 -0500856static int sd_sync_cache(struct scsi_disk *sdkp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 int retries, res;
Martin K. Petersene73aec82007-02-27 22:40:55 -0500859 struct scsi_device *sdp = sdkp->device;
James Bottomleyea73a9f2005-08-28 11:33:52 -0500860 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 if (!scsi_device_online(sdp))
863 return -ENODEV;
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 for (retries = 3; retries > 0; --retries) {
867 unsigned char cmd[10] = { 0 };
868
869 cmd[0] = SYNCHRONIZE_CACHE;
870 /*
871 * Leave the rest of the command zero to indicate
872 * flush everything.
873 */
James Bottomleyea73a9f2005-08-28 11:33:52 -0500874 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
875 SD_TIMEOUT, SD_MAX_RETRIES);
876 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 break;
878 }
879
Martin K. Petersene73aec82007-02-27 22:40:55 -0500880 if (res) {
881 sd_print_result(sdkp, res);
882 if (driver_byte(res) & DRIVER_SENSE)
883 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
Tejun Heo37210502007-03-21 00:07:18 +0900886 if (res)
887 return -EIO;
888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Jens Axboe165125e2007-07-24 09:28:11 +0200891static void sd_prepare_flush(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200893 rq->cmd_type = REQ_TYPE_BLOCK_PC;
James Bottomleyc0ed79a2005-11-08 09:21:07 -0500894 rq->timeout = SD_TIMEOUT;
895 rq->cmd[0] = SYNCHRONIZE_CACHE;
Tejun Heo461d4e92006-01-06 09:52:55 +0100896 rq->cmd_len = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899static void sd_rescan(struct device *dev)
900{
Alan Stern39b7f1e2005-11-04 14:44:41 -0500901 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
902
903 if (sdkp) {
904 sd_revalidate_disk(sdkp->disk);
905 scsi_disk_put(sdkp);
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909
910#ifdef CONFIG_COMPAT
911/*
912 * This gets directly called from VFS. When the ioctl
913 * is not recognized we go back to the other translation paths.
914 */
915static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
916{
Josef Sipek7ac62072006-12-08 02:37:37 -0800917 struct block_device *bdev = file->f_path.dentry->d_inode->i_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 struct gendisk *disk = bdev->bd_disk;
919 struct scsi_device *sdev = scsi_disk(disk)->device;
920
921 /*
922 * If we are in the middle of error recovery, don't let anyone
923 * else try and use this device. Also, if error recovery fails, it
924 * may try and take the device offline, in which case all further
925 * access to the device is prohibited.
926 */
927 if (!scsi_block_when_processing_errors(sdev))
928 return -ENODEV;
929
930 if (sdev->host->hostt->compat_ioctl) {
931 int ret;
932
933 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
934
935 return ret;
936 }
937
938 /*
939 * Let the static ioctl translation table take care of it.
940 */
941 return -ENOIOCTLCMD;
942}
943#endif
944
945static struct block_device_operations sd_fops = {
946 .owner = THIS_MODULE,
947 .open = sd_open,
948 .release = sd_release,
949 .ioctl = sd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800950 .getgeo = sd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951#ifdef CONFIG_COMPAT
952 .compat_ioctl = sd_compat_ioctl,
953#endif
954 .media_changed = sd_media_changed,
955 .revalidate_disk = sd_revalidate_disk,
956};
957
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400958static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
959{
960 u64 start_lba = scmd->request->sector;
961 u64 end_lba = scmd->request->sector + (scsi_bufflen(scmd) / 512);
962 u64 bad_lba;
963 int info_valid;
964
965 if (!blk_fs_request(scmd->request))
966 return 0;
967
968 info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
969 SCSI_SENSE_BUFFERSIZE,
970 &bad_lba);
971 if (!info_valid)
972 return 0;
973
974 if (scsi_bufflen(scmd) <= scmd->device->sector_size)
975 return 0;
976
977 if (scmd->device->sector_size < 512) {
978 /* only legitimate sector_size here is 256 */
979 start_lba <<= 1;
980 end_lba <<= 1;
981 } else {
982 /* be careful ... don't want any overflows */
983 u64 factor = scmd->device->sector_size / 512;
984 do_div(start_lba, factor);
985 do_div(end_lba, factor);
986 }
987
988 /* The bad lba was reported incorrectly, we have no idea where
989 * the error is.
990 */
991 if (bad_lba < start_lba || bad_lba >= end_lba)
992 return 0;
993
994 /* This computation should always be done in terms of
995 * the resolution of the device's medium.
996 */
997 return (bad_lba - start_lba) * scmd->device->sector_size;
998}
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000/**
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001001 * sd_done - bottom half handler: called when the lower level
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * driver has completed (successfully or otherwise) a scsi command.
1003 * @SCpnt: mid-level's per command structure.
1004 *
1005 * Note: potentially run from within an ISR. Must not block.
1006 **/
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001007static int sd_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 int result = SCpnt->result;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001010 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 struct scsi_sense_hdr sshdr;
1012 int sense_valid = 0;
1013 int sense_deferred = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (result) {
1016 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
1017 if (sense_valid)
1018 sense_deferred = scsi_sense_is_deferred(&sshdr);
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020#ifdef CONFIG_SCSI_LOGGING
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001021 SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (sense_valid) {
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001023 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001024 "sd_done: sb[respc,sk,asc,"
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001025 "ascq]=%x,%x,%x,%x\n",
1026 sshdr.response_code,
1027 sshdr.sense_key, sshdr.asc,
1028 sshdr.ascq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030#endif
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001031 if (driver_byte(result) != DRIVER_SENSE &&
1032 (!sense_valid || sense_deferred))
1033 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001035 switch (sshdr.sense_key) {
1036 case HARDWARE_ERROR:
1037 case MEDIUM_ERROR:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001038 good_bytes = sd_completed_bytes(SCpnt);
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001039 break;
1040 case RECOVERED_ERROR:
1041 case NO_SENSE:
1042 /* Inform the user, but make sure that it's not treated
1043 * as a hard error.
1044 */
1045 scsi_print_sense("sd", SCpnt);
1046 SCpnt->result = 0;
1047 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001048 good_bytes = scsi_bufflen(SCpnt);
1049 break;
1050 case ABORTED_COMMAND:
1051 if (sshdr.asc == 0x10) { /* DIF: Disk detected corruption */
1052 scsi_print_result(SCpnt);
1053 scsi_print_sense("sd", SCpnt);
1054 good_bytes = sd_completed_bytes(SCpnt);
1055 }
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001056 break;
1057 case ILLEGAL_REQUEST:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001058 if (sshdr.asc == 0x10) { /* DIX: HBA detected corruption */
1059 scsi_print_result(SCpnt);
1060 scsi_print_sense("sd", SCpnt);
1061 good_bytes = sd_completed_bytes(SCpnt);
1062 }
1063 if (!scsi_device_protection(SCpnt->device) &&
1064 SCpnt->device->use_10_for_rw &&
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001065 (SCpnt->cmnd[0] == READ_10 ||
1066 SCpnt->cmnd[0] == WRITE_10))
1067 SCpnt->device->use_10_for_rw = 0;
1068 if (SCpnt->device->use_10_for_ms &&
1069 (SCpnt->cmnd[0] == MODE_SENSE_10 ||
1070 SCpnt->cmnd[0] == MODE_SELECT_10))
1071 SCpnt->device->use_10_for_ms = 0;
1072 break;
1073 default:
1074 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001076 out:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001077 if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
1078 sd_dif_complete(SCpnt, good_bytes);
1079
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001080 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
James Bottomleyea73a9f2005-08-28 11:33:52 -05001083static int media_not_present(struct scsi_disk *sdkp,
1084 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
James Bottomleyea73a9f2005-08-28 11:33:52 -05001087 if (!scsi_sense_valid(sshdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return 0;
1089 /* not invoked for commands that could return deferred errors */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001090 if (sshdr->sense_key != NOT_READY &&
1091 sshdr->sense_key != UNIT_ATTENTION)
1092 return 0;
1093 if (sshdr->asc != 0x3A) /* medium not present */
1094 return 0;
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 set_media_not_present(sdkp);
1097 return 1;
1098}
1099
1100/*
1101 * spinup disk - called only in sd_revalidate_disk()
1102 */
1103static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001104sd_spinup_disk(struct scsi_disk *sdkp)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001105{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 unsigned char cmd[10];
Alan Stern4451e472005-07-12 10:45:17 -04001107 unsigned long spintime_expire = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 int retries, spintime;
1109 unsigned int the_result;
1110 struct scsi_sense_hdr sshdr;
1111 int sense_valid = 0;
1112
1113 spintime = 0;
1114
1115 /* Spin up drives, as required. Only do this at boot time */
1116 /* Spinup needs to be done for module loads too. */
1117 do {
1118 retries = 0;
1119
1120 do {
1121 cmd[0] = TEST_UNIT_READY;
1122 memset((void *) &cmd[1], 0, 9);
1123
James Bottomleyea73a9f2005-08-28 11:33:52 -05001124 the_result = scsi_execute_req(sdkp->device, cmd,
1125 DMA_NONE, NULL, 0,
1126 &sshdr, SD_TIMEOUT,
1127 SD_MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Alan Sternb4d38e32006-10-11 16:48:28 -04001129 /*
1130 * If the drive has indicated to us that it
1131 * doesn't have any media in it, don't bother
1132 * with any more polling.
1133 */
1134 if (media_not_present(sdkp, &sshdr))
1135 return;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (the_result)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001138 sense_valid = scsi_sense_valid(&sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 retries++;
1140 } while (retries < 3 &&
1141 (!scsi_status_is_good(the_result) ||
1142 ((driver_byte(the_result) & DRIVER_SENSE) &&
1143 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1146 /* no sense, TUR either succeeded or failed
1147 * with a status error */
Martin K. Petersene73aec82007-02-27 22:40:55 -05001148 if(!spintime && !scsi_status_is_good(the_result)) {
1149 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1150 sd_print_result(sdkp, the_result);
1151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 break;
1153 }
1154
1155 /*
1156 * The device does not want the automatic start to be issued.
1157 */
1158 if (sdkp->device->no_start_on_add) {
1159 break;
1160 }
1161
1162 /*
1163 * If manual intervention is required, or this is an
1164 * absent USB storage device, a spinup is meaningless.
1165 */
1166 if (sense_valid &&
1167 sshdr.sense_key == NOT_READY &&
1168 sshdr.asc == 4 && sshdr.ascq == 3) {
1169 break; /* manual intervention required */
1170
1171 /*
1172 * Issue command to spin up drive when not ready
1173 */
1174 } else if (sense_valid && sshdr.sense_key == NOT_READY) {
1175 if (!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001176 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 cmd[0] = START_STOP;
1178 cmd[1] = 1; /* Return immediately */
1179 memset((void *) &cmd[2], 0, 8);
1180 cmd[4] = 1; /* Start spin cycle */
Stefan Richterd2886ea2008-05-11 00:34:07 +02001181 if (sdkp->device->start_stop_pwr_cond)
1182 cmd[4] |= 1 << 4;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001183 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1184 NULL, 0, &sshdr,
1185 SD_TIMEOUT, SD_MAX_RETRIES);
Alan Stern4451e472005-07-12 10:45:17 -04001186 spintime_expire = jiffies + 100 * HZ;
1187 spintime = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 /* Wait 1 second for next try */
1190 msleep(1000);
1191 printk(".");
Alan Stern4451e472005-07-12 10:45:17 -04001192
1193 /*
1194 * Wait for USB flash devices with slow firmware.
1195 * Yes, this sense key/ASC combination shouldn't
1196 * occur here. It's characteristic of these devices.
1197 */
1198 } else if (sense_valid &&
1199 sshdr.sense_key == UNIT_ATTENTION &&
1200 sshdr.asc == 0x28) {
1201 if (!spintime) {
1202 spintime_expire = jiffies + 5 * HZ;
1203 spintime = 1;
1204 }
1205 /* Wait 1 second for next try */
1206 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 } else {
1208 /* we don't understand the sense code, so it's
1209 * probably pointless to loop */
1210 if(!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001211 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1212 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214 break;
1215 }
1216
Alan Stern4451e472005-07-12 10:45:17 -04001217 } while (spintime && time_before_eq(jiffies, spintime_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 if (spintime) {
1220 if (scsi_status_is_good(the_result))
1221 printk("ready\n");
1222 else
1223 printk("not responding...\n");
1224 }
1225}
1226
Martin K. Petersene0597d72008-07-17 04:28:34 -04001227
1228/*
1229 * Determine whether disk supports Data Integrity Field.
1230 */
1231void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
1232{
1233 struct scsi_device *sdp = sdkp->device;
1234 u8 type;
1235
1236 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
1237 type = 0;
1238 else
1239 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
1240
1241 switch (type) {
1242 case SD_DIF_TYPE0_PROTECTION:
1243 sdkp->protection_type = 0;
1244 break;
1245
1246 case SD_DIF_TYPE1_PROTECTION:
1247 case SD_DIF_TYPE3_PROTECTION:
1248 sdkp->protection_type = type;
1249 break;
1250
1251 case SD_DIF_TYPE2_PROTECTION:
1252 sd_printk(KERN_ERR, sdkp, "formatted with DIF Type 2 " \
1253 "protection which is currently unsupported. " \
1254 "Disabling disk!\n");
1255 goto disable;
1256
1257 default:
1258 sd_printk(KERN_ERR, sdkp, "formatted with unknown " \
1259 "protection type %d. Disabling disk!\n", type);
1260 goto disable;
1261 }
1262
1263 return;
1264
1265disable:
1266 sdkp->protection_type = 0;
1267 sdkp->capacity = 0;
1268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/*
1271 * read disk capacity
1272 */
1273static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001274sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001275{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 unsigned char cmd[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int the_result, retries;
1278 int sector_size = 0;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001279 /* Force READ CAPACITY(16) when PROTECT=1 */
1280 int longrc = scsi_device_protection(sdkp->device) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 struct scsi_sense_hdr sshdr;
1282 int sense_valid = 0;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001283 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285repeat:
1286 retries = 3;
1287 do {
1288 if (longrc) {
1289 memset((void *) cmd, 0, 16);
1290 cmd[0] = SERVICE_ACTION_IN;
1291 cmd[1] = SAI_READ_CAPACITY_16;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001292 cmd[13] = 13;
1293 memset((void *) buffer, 0, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 } else {
1295 cmd[0] = READ_CAPACITY;
1296 memset((void *) &cmd[1], 0, 9);
1297 memset((void *) buffer, 0, 8);
1298 }
1299
James Bottomleyea73a9f2005-08-28 11:33:52 -05001300 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
Martin K. Petersene0597d72008-07-17 04:28:34 -04001301 buffer, longrc ? 13 : 8, &sshdr,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001302 SD_TIMEOUT, SD_MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
James Bottomleyea73a9f2005-08-28 11:33:52 -05001304 if (media_not_present(sdkp, &sshdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return;
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (the_result)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001308 sense_valid = scsi_sense_valid(&sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 retries--;
1310
1311 } while (the_result && retries);
1312
1313 if (the_result && !longrc) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001314 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1315 sd_print_result(sdkp, the_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (driver_byte(the_result) & DRIVER_SENSE)
Martin K. Petersene73aec82007-02-27 22:40:55 -05001317 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 else
Martin K. Petersene73aec82007-02-27 22:40:55 -05001319 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 /* Set dirty bit for removable devices if not ready -
1322 * sometimes drives will not report this properly. */
1323 if (sdp->removable &&
1324 sense_valid && sshdr.sense_key == NOT_READY)
1325 sdp->changed = 1;
1326
1327 /* Either no media are present but the drive didn't tell us,
1328 or they are present but the read capacity command fails */
1329 /* sdkp->media_present = 0; -- not always correct */
Hannes Reinecke69bdd882006-09-01 15:50:23 +02001330 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 return;
1333 } else if (the_result && longrc) {
1334 /* READ CAPACITY(16) has been failed */
Martin K. Petersene73aec82007-02-27 22:40:55 -05001335 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
1336 sd_print_result(sdkp, the_result);
1337 sd_printk(KERN_NOTICE, sdkp, "Use 0xffffffff as device size\n");
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1340 goto got_data;
1341 }
1342
1343 if (!longrc) {
1344 sector_size = (buffer[4] << 24) |
1345 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1346 if (buffer[0] == 0xff && buffer[1] == 0xff &&
1347 buffer[2] == 0xff && buffer[3] == 0xff) {
1348 if(sizeof(sdkp->capacity) > 4) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001349 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1350 "Trying to use READ CAPACITY(16).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 longrc = 1;
1352 goto repeat;
1353 }
Martin K. Petersene73aec82007-02-27 22:40:55 -05001354 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use "
1355 "a kernel compiled with support for large "
1356 "block devices.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 sdkp->capacity = 0;
1358 goto got_data;
1359 }
1360 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
1361 (buffer[1] << 16) |
1362 (buffer[2] << 8) |
1363 buffer[3]);
1364 } else {
1365 sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
1366 ((u64)buffer[1] << 48) |
1367 ((u64)buffer[2] << 40) |
1368 ((u64)buffer[3] << 32) |
1369 ((sector_t)buffer[4] << 24) |
1370 ((sector_t)buffer[5] << 16) |
1371 ((sector_t)buffer[6] << 8) |
1372 (sector_t)buffer[7]);
1373
1374 sector_size = (buffer[8] << 24) |
1375 (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
Martin K. Petersene0597d72008-07-17 04:28:34 -04001376
1377 sd_read_protection_type(sdkp, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379
1380 /* Some devices return the total number of sectors, not the
1381 * highest sector number. Make the necessary adjustment. */
Oliver Neukum61bf54b2007-02-08 09:04:48 +01001382 if (sdp->fix_capacity) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 --sdkp->capacity;
1384
Oliver Neukum61bf54b2007-02-08 09:04:48 +01001385 /* Some devices have version which report the correct sizes
1386 * and others which do not. We guess size according to a heuristic
1387 * and err on the side of lowering the capacity. */
1388 } else {
1389 if (sdp->guess_capacity)
1390 if (sdkp->capacity & 0x01) /* odd sizes are odd */
1391 --sdkp->capacity;
1392 }
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394got_data:
1395 if (sector_size == 0) {
1396 sector_size = 512;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001397 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
1398 "assuming 512.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
1401 if (sector_size != 512 &&
1402 sector_size != 1024 &&
1403 sector_size != 2048 &&
1404 sector_size != 4096 &&
1405 sector_size != 256) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001406 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
1407 sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 /*
1409 * The user might want to re-format the drive with
1410 * a supported sectorsize. Once this happens, it
1411 * would be relatively trivial to set the thing up.
1412 * For this reason, we leave the thing in the table.
1413 */
1414 sdkp->capacity = 0;
1415 /*
1416 * set a bogus sector size so the normal read/write
1417 * logic in the block layer will eventually refuse any
1418 * request on this device without tripping over power
1419 * of two sector size assumptions
1420 */
1421 sector_size = 512;
1422 }
1423 {
1424 /*
1425 * The msdos fs needs to know the hardware sector size
1426 * So I have created this table. See ll_rw_blk.c
1427 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1428 */
1429 int hard_sector = sector_size;
James Bottomley7a691bd2005-10-28 13:17:30 -05001430 sector_t sz = (sdkp->capacity/2) * (hard_sector/256);
Jens Axboe165125e2007-07-24 09:28:11 +02001431 struct request_queue *queue = sdp->request_queue;
James Bottomley7a691bd2005-10-28 13:17:30 -05001432 sector_t mb = sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 blk_queue_hardsect_size(queue, hard_sector);
1435 /* avoid 64-bit division on 32-bit platforms */
James Bottomley7a691bd2005-10-28 13:17:30 -05001436 sector_div(sz, 625);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 mb -= sz - 974;
1438 sector_div(mb, 1950);
1439
Martin K. Petersene73aec82007-02-27 22:40:55 -05001440 sd_printk(KERN_NOTICE, sdkp,
1441 "%llu %d-byte hardware sectors (%llu MB)\n",
1442 (unsigned long long)sdkp->capacity,
1443 hard_sector, (unsigned long long)mb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445
1446 /* Rescale capacity to 512-byte units */
1447 if (sector_size == 4096)
1448 sdkp->capacity <<= 3;
1449 else if (sector_size == 2048)
1450 sdkp->capacity <<= 2;
1451 else if (sector_size == 1024)
1452 sdkp->capacity <<= 1;
1453 else if (sector_size == 256)
1454 sdkp->capacity >>= 1;
1455
1456 sdkp->device->sector_size = sector_size;
1457}
1458
1459/* called with buffer of length 512 */
1460static inline int
James Bottomleyea73a9f2005-08-28 11:33:52 -05001461sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1462 unsigned char *buffer, int len, struct scsi_mode_data *data,
1463 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
James Bottomleyea73a9f2005-08-28 11:33:52 -05001465 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
James Bottomley1cf72692005-08-28 11:27:01 -05001466 SD_TIMEOUT, SD_MAX_RETRIES, data,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001467 sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470/*
1471 * read write protect setting, if possible - called only in sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06001472 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
1474static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001475sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001476{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 int res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001478 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 struct scsi_mode_data data;
1480
1481 set_disk_ro(sdkp->disk, 0);
James Bottomleyea73a9f2005-08-28 11:33:52 -05001482 if (sdp->skip_ms_page_3f) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001483 sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return;
1485 }
1486
James Bottomleyea73a9f2005-08-28 11:33:52 -05001487 if (sdp->use_192_bytes_for_3f) {
1488 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 } else {
1490 /*
1491 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1492 * We have to start carefully: some devices hang if we ask
1493 * for more than is available.
1494 */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001495 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /*
1498 * Second attempt: ask for page 0 When only page 0 is
1499 * implemented, a request for page 3F may return Sense Key
1500 * 5: Illegal Request, Sense Code 24: Invalid field in
1501 * CDB.
1502 */
1503 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05001504 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 /*
1507 * Third attempt: ask 255 bytes, as we did earlier.
1508 */
1509 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05001510 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1511 &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513
1514 if (!scsi_status_is_good(res)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001515 sd_printk(KERN_WARNING, sdkp,
1516 "Test WP failed, assume Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 } else {
1518 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1519 set_disk_ro(sdkp->disk, sdkp->write_prot);
Martin K. Petersene73aec82007-02-27 22:40:55 -05001520 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
1521 sdkp->write_prot ? "on" : "off");
1522 sd_printk(KERN_DEBUG, sdkp,
1523 "Mode Sense: %02x %02x %02x %02x\n",
1524 buffer[0], buffer[1], buffer[2], buffer[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526}
1527
1528/*
1529 * sd_read_cache_type - called only from sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06001530 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 */
1532static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001533sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
Al Viro 631e8a12005-05-16 01:59:55 +01001534{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 int len = 0, res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001536 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Al Viro 631e8a12005-05-16 01:59:55 +01001538 int dbd;
1539 int modepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 struct scsi_mode_data data;
1541 struct scsi_sense_hdr sshdr;
1542
James Bottomleyea73a9f2005-08-28 11:33:52 -05001543 if (sdp->skip_ms_page_8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 goto defaults;
1545
James Bottomleyea73a9f2005-08-28 11:33:52 -05001546 if (sdp->type == TYPE_RBC) {
Al Viro 631e8a12005-05-16 01:59:55 +01001547 modepage = 6;
1548 dbd = 8;
1549 } else {
1550 modepage = 8;
1551 dbd = 0;
1552 }
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /* cautiously ask */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001555 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 if (!scsi_status_is_good(res))
1558 goto bad_sense;
1559
Al Viro6d73c852006-02-23 02:03:16 +01001560 if (!data.header_length) {
1561 modepage = 6;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001562 sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
Al Viro6d73c852006-02-23 02:03:16 +01001563 }
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /* that went OK, now ask for the proper length */
1566 len = data.length;
1567
1568 /*
1569 * We're only interested in the first three bytes, actually.
1570 * But the data cache page is defined for the first 20.
1571 */
1572 if (len < 3)
1573 goto bad_sense;
1574 if (len > 20)
1575 len = 20;
1576
1577 /* Take headers and block descriptors into account */
1578 len += data.header_length + data.block_descriptor_length;
Al Viro48970802006-02-26 08:34:10 -06001579 if (len > SD_BUF_SIZE)
1580 goto bad_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 /* Get the data */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001583 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 if (scsi_status_is_good(res)) {
Al Viro 631e8a12005-05-16 01:59:55 +01001586 int offset = data.header_length + data.block_descriptor_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Al Viro48970802006-02-26 08:34:10 -06001588 if (offset >= SD_BUF_SIZE - 2) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001589 sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
Al Viro48970802006-02-26 08:34:10 -06001590 goto defaults;
1591 }
1592
Al Viro 631e8a12005-05-16 01:59:55 +01001593 if ((buffer[offset] & 0x3f) != modepage) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001594 sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
Al Viro 631e8a12005-05-16 01:59:55 +01001595 goto defaults;
1596 }
1597
1598 if (modepage == 8) {
1599 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1600 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1601 } else {
1602 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1603 sdkp->RCD = 0;
1604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Tejun Heo007365a2006-01-06 09:53:52 +01001606 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1607 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001608 sd_printk(KERN_NOTICE, sdkp,
1609 "Uses READ/WRITE(6), disabling FUA\n");
Tejun Heo007365a2006-01-06 09:53:52 +01001610 sdkp->DPOFUA = 0;
1611 }
1612
Martin K. Petersene73aec82007-02-27 22:40:55 -05001613 sd_printk(KERN_NOTICE, sdkp,
1614 "Write cache: %s, read cache: %s, %s\n",
Luben Tuikovfd44bab2006-11-15 12:47:08 -08001615 sdkp->WCE ? "enabled" : "disabled",
1616 sdkp->RCD ? "disabled" : "enabled",
1617 sdkp->DPOFUA ? "supports DPO and FUA"
1618 : "doesn't support DPO or FUA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 return;
1621 }
1622
1623bad_sense:
James Bottomleyea73a9f2005-08-28 11:33:52 -05001624 if (scsi_sense_valid(&sshdr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 sshdr.sense_key == ILLEGAL_REQUEST &&
1626 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
Martin K. Petersene73aec82007-02-27 22:40:55 -05001627 /* Invalid field in CDB */
1628 sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 else
Martin K. Petersene73aec82007-02-27 22:40:55 -05001630 sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632defaults:
Martin K. Petersene73aec82007-02-27 22:40:55 -05001633 sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 sdkp->WCE = 0;
1635 sdkp->RCD = 0;
Al Viro48970802006-02-26 08:34:10 -06001636 sdkp->DPOFUA = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
Martin K. Petersene0597d72008-07-17 04:28:34 -04001639/*
1640 * The ATO bit indicates whether the DIF application tag is available
1641 * for use by the operating system.
1642 */
1643void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
1644{
1645 int res, offset;
1646 struct scsi_device *sdp = sdkp->device;
1647 struct scsi_mode_data data;
1648 struct scsi_sense_hdr sshdr;
1649
1650 if (sdp->type != TYPE_DISK)
1651 return;
1652
1653 if (sdkp->protection_type == 0)
1654 return;
1655
1656 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
1657 SD_MAX_RETRIES, &data, &sshdr);
1658
1659 if (!scsi_status_is_good(res) || !data.header_length ||
1660 data.length < 6) {
1661 sd_printk(KERN_WARNING, sdkp,
1662 "getting Control mode page failed, assume no ATO\n");
1663
1664 if (scsi_sense_valid(&sshdr))
1665 sd_print_sense_hdr(sdkp, &sshdr);
1666
1667 return;
1668 }
1669
1670 offset = data.header_length + data.block_descriptor_length;
1671
1672 if ((buffer[offset] & 0x3f) != 0x0a) {
1673 sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
1674 return;
1675 }
1676
1677 if ((buffer[offset + 5] & 0x80) == 0)
1678 return;
1679
1680 sdkp->ATO = 1;
1681
1682 return;
1683}
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685/**
1686 * sd_revalidate_disk - called the first time a new disk is seen,
1687 * performs disk spin up, read_capacity, etc.
1688 * @disk: struct gendisk we care about
1689 **/
1690static int sd_revalidate_disk(struct gendisk *disk)
1691{
1692 struct scsi_disk *sdkp = scsi_disk(disk);
1693 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 unsigned char *buffer;
Tejun Heo461d4e92006-01-06 09:52:55 +01001695 unsigned ordered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001697 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
1698 "sd_revalidate_disk\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 /*
1701 * If the device is offline, don't try and read capacity or any
1702 * of the other niceties.
1703 */
1704 if (!scsi_device_online(sdp))
1705 goto out;
1706
Bernhard Wallea6123f12007-05-21 17:15:26 +02001707 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (!buffer) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001709 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
1710 "allocation failure.\n");
James Bottomleyea73a9f2005-08-28 11:33:52 -05001711 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713
1714 /* defaults, until the device tells us otherwise */
1715 sdp->sector_size = 512;
1716 sdkp->capacity = 0;
1717 sdkp->media_present = 1;
1718 sdkp->write_prot = 0;
1719 sdkp->WCE = 0;
1720 sdkp->RCD = 0;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001721 sdkp->ATO = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Martin K. Petersene73aec82007-02-27 22:40:55 -05001723 sd_spinup_disk(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 /*
1726 * Without media there is no reason to ask; moreover, some devices
1727 * react badly if we do.
1728 */
1729 if (sdkp->media_present) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001730 sd_read_capacity(sdkp, buffer);
1731 sd_read_write_protect_flag(sdkp, buffer);
1732 sd_read_cache_type(sdkp, buffer);
Martin K. Petersene0597d72008-07-17 04:28:34 -04001733 sd_read_app_tag_own(sdkp, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
Tejun Heo461d4e92006-01-06 09:52:55 +01001735
1736 /*
1737 * We now have all cache related info, determine how we deal
1738 * with ordered requests. Note that as the current SCSI
1739 * dispatch function can alter request order, we cannot use
1740 * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
1741 */
1742 if (sdkp->WCE)
Tejun Heo007365a2006-01-06 09:53:52 +01001743 ordered = sdkp->DPOFUA
1744 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
Tejun Heo461d4e92006-01-06 09:52:55 +01001745 else
1746 ordered = QUEUE_ORDERED_DRAIN;
1747
1748 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 set_capacity(disk, sdkp->capacity);
1751 kfree(buffer);
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 out:
1754 return 0;
1755}
1756
1757/**
1758 * sd_probe - called during driver initialization and whenever a
1759 * new scsi device is attached to the system. It is called once
1760 * for each scsi device (not just disks) present.
1761 * @dev: pointer to device object
1762 *
1763 * Returns 0 if successful (or not interested in this scsi device
1764 * (e.g. scanner)); 1 when there is an error.
1765 *
1766 * Note: this function is invoked from the scsi mid-level.
1767 * This function sets up the mapping between a given
1768 * <host,channel,id,lun> (found in sdp) and new device name
1769 * (e.g. /dev/sda). More precisely it is the block device major
1770 * and minor number that is chosen here.
1771 *
1772 * Assume sd_attach is not re-entrant (for time being)
1773 * Also think about sd_attach() and sd_remove() running coincidentally.
1774 **/
1775static int sd_probe(struct device *dev)
1776{
1777 struct scsi_device *sdp = to_scsi_device(dev);
1778 struct scsi_disk *sdkp;
1779 struct gendisk *gd;
1780 u32 index;
1781 int error;
1782
1783 error = -ENODEV;
Al Viro 631e8a12005-05-16 01:59:55 +01001784 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto out;
1786
James Bottomley9ccfc752005-10-02 11:45:08 -05001787 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
1788 "sd_attach\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -05001791 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (!sdkp)
1793 goto out;
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 gd = alloc_disk(16);
1796 if (!gd)
1797 goto out_free;
1798
Tejun Heof27bac22008-07-14 14:59:30 +09001799 do {
1800 if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
1801 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Tejun Heof27bac22008-07-14 14:59:30 +09001803 error = ida_get_new(&sd_index_ida, &index);
1804 } while (error == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (error)
1807 goto out_put;
1808
Tejun Heof27bac22008-07-14 14:59:30 +09001809 error = -EBUSY;
1810 if (index >= SD_MAX_DISKS)
1811 goto out_free_index;
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 sdkp->device = sdp;
1814 sdkp->driver = &sd_template;
1815 sdkp->disk = gd;
1816 sdkp->index = index;
1817 sdkp->openers = 0;
Kay Sieversc02e6002008-03-19 13:09:56 +01001818 sdkp->previous_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 if (!sdp->timeout) {
Al Viro 631e8a12005-05-16 01:59:55 +01001821 if (sdp->type != TYPE_MOD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 sdp->timeout = SD_TIMEOUT;
1823 else
1824 sdp->timeout = SD_MOD_TIMEOUT;
1825 }
1826
Tony Jonesee959b02008-02-22 00:13:36 +01001827 device_initialize(&sdkp->dev);
1828 sdkp->dev.parent = &sdp->sdev_gendev;
1829 sdkp->dev.class = &sd_disk_class;
1830 strncpy(sdkp->dev.bus_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE);
Nagendra Singh Tomar017f2e32007-02-02 17:34:56 +05301831
Tony Jonesee959b02008-02-22 00:13:36 +01001832 if (device_add(&sdkp->dev))
Tejun Heof27bac22008-07-14 14:59:30 +09001833 goto out_free_index;
Nagendra Singh Tomar017f2e32007-02-02 17:34:56 +05301834
1835 get_device(&sdp->sdev_gendev);
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 gd->major = sd_major((index & 0xf0) >> 4);
1838 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
1839 gd->minors = 16;
1840 gd->fops = &sd_fops;
1841
1842 if (index < 26) {
1843 sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
1844 } else if (index < (26 + 1) * 26) {
1845 sprintf(gd->disk_name, "sd%c%c",
1846 'a' + index / 26 - 1,'a' + index % 26);
1847 } else {
1848 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
1849 const unsigned int m2 = (index / 26 - 1) % 26;
1850 const unsigned int m3 = index % 26;
1851 sprintf(gd->disk_name, "sd%c%c%c",
1852 'a' + m1, 'a' + m2, 'a' + m3);
1853 }
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 gd->private_data = &sdkp->driver;
Tejun Heo461d4e92006-01-06 09:52:55 +01001856 gd->queue = sdkp->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 sd_revalidate_disk(gd);
1859
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001860 blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
James Bottomley03a57432007-08-03 16:41:11 -05001861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 gd->driverfs_dev = &sdp->sdev_gendev;
1863 gd->flags = GENHD_FL_DRIVERFS;
1864 if (sdp->removable)
1865 gd->flags |= GENHD_FL_REMOVABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 dev_set_drvdata(dev, sdkp);
1868 add_disk(gd);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001869 sd_dif_config_host(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Martin K. Petersene73aec82007-02-27 22:40:55 -05001871 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
1872 sdp->removable ? "removable " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 return 0;
1875
Tejun Heof27bac22008-07-14 14:59:30 +09001876 out_free_index:
1877 ida_remove(&sd_index_ida, index);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06001878 out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 put_disk(gd);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06001880 out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 kfree(sdkp);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06001882 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return error;
1884}
1885
1886/**
1887 * sd_remove - called whenever a scsi disk (previously recognized by
1888 * sd_probe) is detached from the system. It is called (potentially
1889 * multiple times) during sd module unload.
1890 * @sdp: pointer to mid level scsi device object
1891 *
1892 * Note: this function is invoked from the scsi mid-level.
1893 * This function potentially frees up a device name (e.g. /dev/sdc)
1894 * that could be re-used by a subsequent sd_probe().
1895 * This function is not called when the built-in sd driver is "exit-ed".
1896 **/
1897static int sd_remove(struct device *dev)
1898{
1899 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1900
Tony Jonesee959b02008-02-22 00:13:36 +01001901 device_del(&sdkp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 del_gendisk(sdkp->disk);
1903 sd_shutdown(dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001904
Arjan van de Ven0b950672006-01-11 13:16:10 +01001905 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001906 dev_set_drvdata(dev, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01001907 put_device(&sdkp->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001908 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 return 0;
1911}
1912
1913/**
1914 * scsi_disk_release - Called to free the scsi_disk structure
Tony Jonesee959b02008-02-22 00:13:36 +01001915 * @dev: pointer to embedded class device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 *
Arjan van de Ven0b950672006-01-11 13:16:10 +01001917 * sd_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 * called on last put, you should always use the scsi_disk_get()
1919 * scsi_disk_put() helpers which manipulate the semaphore directly
Tony Jonesee959b02008-02-22 00:13:36 +01001920 * and never do a direct put_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 **/
Tony Jonesee959b02008-02-22 00:13:36 +01001922static void scsi_disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Tony Jonesee959b02008-02-22 00:13:36 +01001924 struct scsi_disk *sdkp = to_scsi_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 struct gendisk *disk = sdkp->disk;
1926
Tejun Heof27bac22008-07-14 14:59:30 +09001927 ida_remove(&sd_index_ida, sdkp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 disk->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 put_disk(disk);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001931 put_device(&sdkp->device->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 kfree(sdkp);
1934}
1935
James Bottomleycc5d2c82007-03-20 12:26:03 -05001936static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
Tejun Heoc3c94c52007-03-21 00:13:59 +09001937{
1938 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
1939 struct scsi_sense_hdr sshdr;
James Bottomleycc5d2c82007-03-20 12:26:03 -05001940 struct scsi_device *sdp = sdkp->device;
Tejun Heoc3c94c52007-03-21 00:13:59 +09001941 int res;
1942
1943 if (start)
1944 cmd[4] |= 1; /* START */
1945
Stefan Richterd2886ea2008-05-11 00:34:07 +02001946 if (sdp->start_stop_pwr_cond)
1947 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
1948
Tejun Heoc3c94c52007-03-21 00:13:59 +09001949 if (!scsi_device_online(sdp))
1950 return -ENODEV;
1951
1952 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
1953 SD_TIMEOUT, SD_MAX_RETRIES);
1954 if (res) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05001955 sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
1956 sd_print_result(sdkp, res);
Tejun Heoc3c94c52007-03-21 00:13:59 +09001957 if (driver_byte(res) & DRIVER_SENSE)
James Bottomleycc5d2c82007-03-20 12:26:03 -05001958 sd_print_sense_hdr(sdkp, &sshdr);
Tejun Heoc3c94c52007-03-21 00:13:59 +09001959 }
1960
1961 return res;
1962}
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964/*
1965 * Send a SYNCHRONIZE CACHE instruction down to the device through
1966 * the normal SCSI command structure. Wait for the command to
1967 * complete.
1968 */
1969static void sd_shutdown(struct device *dev)
1970{
Alan Stern39b7f1e2005-11-04 14:44:41 -05001971 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 if (!sdkp)
1974 return; /* this can happen */
1975
Alan Stern39b7f1e2005-11-04 14:44:41 -05001976 if (sdkp->WCE) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001977 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
1978 sd_sync_cache(sdkp);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001979 }
Tejun Heoc3c94c52007-03-21 00:13:59 +09001980
James Bottomleycc5d2c82007-03-20 12:26:03 -05001981 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
1982 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
1983 sd_start_stop_device(sdkp, 0);
Tejun Heoc3c94c52007-03-21 00:13:59 +09001984 }
1985
Alan Stern39b7f1e2005-11-04 14:44:41 -05001986 scsi_disk_put(sdkp);
1987}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Tejun Heoc3c94c52007-03-21 00:13:59 +09001989static int sd_suspend(struct device *dev, pm_message_t mesg)
1990{
Tejun Heoc3c94c52007-03-21 00:13:59 +09001991 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Alan Stern09ff92f2007-05-21 09:55:04 -04001992 int ret = 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09001993
1994 if (!sdkp)
1995 return 0; /* this can happen */
1996
1997 if (sdkp->WCE) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05001998 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
Tejun Heoc3c94c52007-03-21 00:13:59 +09001999 ret = sd_sync_cache(sdkp);
2000 if (ret)
Alan Stern09ff92f2007-05-21 09:55:04 -04002001 goto done;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002002 }
2003
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01002004 if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05002005 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2006 ret = sd_start_stop_device(sdkp, 0);
Tejun Heoc3c94c52007-03-21 00:13:59 +09002007 }
2008
Alan Stern09ff92f2007-05-21 09:55:04 -04002009done:
2010 scsi_disk_put(sdkp);
2011 return ret;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002012}
2013
2014static int sd_resume(struct device *dev)
2015{
Tejun Heoc3c94c52007-03-21 00:13:59 +09002016 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Alan Stern09ff92f2007-05-21 09:55:04 -04002017 int ret = 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002018
James Bottomleycc5d2c82007-03-20 12:26:03 -05002019 if (!sdkp->device->manage_start_stop)
Alan Stern09ff92f2007-05-21 09:55:04 -04002020 goto done;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002021
James Bottomleycc5d2c82007-03-20 12:26:03 -05002022 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
Alan Stern09ff92f2007-05-21 09:55:04 -04002023 ret = sd_start_stop_device(sdkp, 1);
Tejun Heoc3c94c52007-03-21 00:13:59 +09002024
Alan Stern09ff92f2007-05-21 09:55:04 -04002025done:
2026 scsi_disk_put(sdkp);
2027 return ret;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002028}
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030/**
2031 * init_sd - entry point for this driver (both when built in or when
2032 * a module).
2033 *
2034 * Note: this function registers this driver with the scsi mid-level.
2035 **/
2036static int __init init_sd(void)
2037{
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002038 int majors = 0, i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
2041
2042 for (i = 0; i < SD_MAJORS; i++)
2043 if (register_blkdev(sd_major(i), "sd") == 0)
2044 majors++;
2045
2046 if (!majors)
2047 return -ENODEV;
2048
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002049 err = class_register(&sd_disk_class);
2050 if (err)
2051 goto err_out;
James Bottomley6bdaa1f2006-03-18 14:14:21 -06002052
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002053 err = scsi_register_driver(&sd_template.gendrv);
2054 if (err)
2055 goto err_out_class;
2056
2057 return 0;
2058
2059err_out_class:
2060 class_unregister(&sd_disk_class);
2061err_out:
2062 for (i = 0; i < SD_MAJORS; i++)
2063 unregister_blkdev(sd_major(i), "sd");
2064 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
2067/**
2068 * exit_sd - exit point for this driver (when it is a module).
2069 *
2070 * Note: this function unregisters this driver from the scsi mid-level.
2071 **/
2072static void __exit exit_sd(void)
2073{
2074 int i;
2075
2076 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
2077
2078 scsi_unregister_driver(&sd_template.gendrv);
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002079 class_unregister(&sd_disk_class);
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 for (i = 0; i < SD_MAJORS; i++)
2082 unregister_blkdev(sd_major(i), "sd");
2083}
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085module_init(init_sd);
2086module_exit(exit_sd);
Martin K. Petersene73aec82007-02-27 22:40:55 -05002087
2088static void sd_print_sense_hdr(struct scsi_disk *sdkp,
2089 struct scsi_sense_hdr *sshdr)
2090{
2091 sd_printk(KERN_INFO, sdkp, "");
2092 scsi_show_sense_hdr(sshdr);
2093 sd_printk(KERN_INFO, sdkp, "");
2094 scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
2095}
2096
2097static void sd_print_result(struct scsi_disk *sdkp, int result)
2098{
2099 sd_printk(KERN_INFO, sdkp, "");
2100 scsi_show_result(result);
2101}
2102