blob: 2a2bc89aba83204f331f3023424150cc1a050f99 [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;
Linus Torvalds18351072008-08-05 21:42:21 -0700378 sector_t threshold;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500379 unsigned int this_count = rq->nr_sectors;
Christoph Hellwig776b23a2006-01-06 18:34:07 +0100380 unsigned int timeout = sdp->timeout;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500381 int ret;
382
383 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
384 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
385 goto out;
386 } else if (rq->cmd_type != REQ_TYPE_FS) {
387 ret = BLKPREP_KILL;
388 goto out;
389 }
390 ret = scsi_setup_fs_cmnd(sdp, rq);
391 if (ret != BLKPREP_OK)
392 goto out;
393 SCpnt = rq->special;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400394 sdkp = scsi_disk(disk);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500395
396 /* from here on until we're complete, any goto out
397 * is used for a killable error condition */
398 ret = BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500400 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
401 "sd_init_command: block=%llu, "
402 "count=%d\n",
403 (unsigned long long)block,
404 this_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 if (!sdp || !scsi_device_online(sdp) ||
407 block + rq->nr_sectors > get_capacity(disk)) {
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500408 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
409 "Finishing %ld sectors\n",
410 rq->nr_sectors));
411 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
412 "Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500413 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
416 if (sdp->changed) {
417 /*
418 * quietly refuse to do anything to a changed disc until
419 * the changed bit has been reset
420 */
421 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500422 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500424
Hans de Goedea0899d42008-01-20 11:12:26 +0100425 /*
Linus Torvalds18351072008-08-05 21:42:21 -0700426 * Some SD card readers can't handle multi-sector accesses which touch
427 * the last one or two hardware sectors. Split accesses as needed.
Hans de Goedea0899d42008-01-20 11:12:26 +0100428 */
Linus Torvalds18351072008-08-05 21:42:21 -0700429 threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
430 (sdp->sector_size / 512);
431
432 if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
433 if (block < threshold) {
434 /* Access up to the threshold but not beyond */
435 this_count = threshold - block;
436 } else {
437 /* Access only a single hardware sector */
438 this_count = sdp->sector_size / 512;
439 }
440 }
Hans de Goedea0899d42008-01-20 11:12:26 +0100441
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500442 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
443 (unsigned long long)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /*
446 * If we have a 1K hardware sectorsize, prevent access to single
447 * 512 byte sectors. In theory we could handle this - in fact
448 * the scsi cdrom driver must be able to handle this because
449 * we typically use 1K blocksizes, and cdroms typically have
450 * 2K hardware sectorsizes. Of course, things are simpler
451 * with the cdrom, since it is read-only. For performance
452 * reasons, the filesystems should be able to handle this
453 * and not force the scsi disk driver to use bounce buffers
454 * for this.
455 */
456 if (sdp->sector_size == 1024) {
457 if ((block & 1) || (rq->nr_sectors & 1)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500458 scmd_printk(KERN_ERR, SCpnt,
459 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500460 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 } else {
462 block = block >> 1;
463 this_count = this_count >> 1;
464 }
465 }
466 if (sdp->sector_size == 2048) {
467 if ((block & 3) || (rq->nr_sectors & 3)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500468 scmd_printk(KERN_ERR, SCpnt,
469 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500470 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 } else {
472 block = block >> 2;
473 this_count = this_count >> 2;
474 }
475 }
476 if (sdp->sector_size == 4096) {
477 if ((block & 7) || (rq->nr_sectors & 7)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500478 scmd_printk(KERN_ERR, SCpnt,
479 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500480 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 } else {
482 block = block >> 3;
483 this_count = this_count >> 3;
484 }
485 }
486 if (rq_data_dir(rq) == WRITE) {
487 if (!sdp->writeable) {
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500488 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 SCpnt->cmnd[0] = WRITE_6;
491 SCpnt->sc_data_direction = DMA_TO_DEVICE;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400492
493 if (blk_integrity_rq(rq) &&
494 sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
495 goto out;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 } else if (rq_data_dir(rq) == READ) {
498 SCpnt->cmnd[0] = READ_6;
499 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
500 } else {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500501 scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500505 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
506 "%s %d/%ld 512 byte blocks.\n",
507 (rq_data_dir(rq) == WRITE) ?
508 "writing" : "reading", this_count,
509 rq->nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400511 /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
512 if (scsi_host_dif_capable(sdp->host, sdkp->protection_type))
513 SCpnt->cmnd[1] = 1 << 5;
514 else
515 SCpnt->cmnd[1] = 0;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (block > 0xffffffff) {
518 SCpnt->cmnd[0] += READ_16 - READ_6;
Tejun Heo007365a2006-01-06 09:53:52 +0100519 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
521 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
522 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
523 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
524 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
525 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
526 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
527 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
528 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
529 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
530 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
531 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
532 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
533 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400534 scsi_device_protection(SCpnt->device) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 SCpnt->device->use_10_for_rw) {
536 if (this_count > 0xffff)
537 this_count = 0xffff;
538
539 SCpnt->cmnd[0] += READ_10 - READ_6;
Tejun Heo007365a2006-01-06 09:53:52 +0100540 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
542 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
543 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
544 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
545 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
546 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
547 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
548 } else {
Tejun Heo007365a2006-01-06 09:53:52 +0100549 if (unlikely(blk_fua_rq(rq))) {
550 /*
551 * This happens only if this drive failed
552 * 10byte rw command with ILLEGAL_REQUEST
553 * during operation and thus turned off
554 * use_10_for_rw.
555 */
Martin K. Petersene73aec82007-02-27 22:40:55 -0500556 scmd_printk(KERN_ERR, SCpnt,
557 "FUA write on READ/WRITE(6) drive\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500558 goto out;
Tejun Heo007365a2006-01-06 09:53:52 +0100559 }
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
562 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
563 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
564 SCpnt->cmnd[4] = (unsigned char) this_count;
565 SCpnt->cmnd[5] = 0;
566 }
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200567 SCpnt->sdb.length = this_count * sdp->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400569 /* If DIF or DIX is enabled, tell HBA how to handle request */
570 if (sdkp->protection_type || scsi_prot_sg_count(SCpnt))
571 sd_dif_op(SCpnt, sdkp->protection_type, scsi_prot_sg_count(SCpnt));
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /*
574 * We shouldn't disconnect in the middle of a sector, so with a dumb
575 * host adapter, it's safe to assume that we can at least transfer
576 * this many bytes between each connect / disconnect.
577 */
578 SCpnt->transfersize = sdp->sector_size;
579 SCpnt->underflow = this_count << 9;
580 SCpnt->allowed = SD_MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 SCpnt->timeout_per_command = timeout;
582
583 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * This indicates that the command is ready from our end to be
585 * queued.
586 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500587 ret = BLKPREP_OK;
588 out:
589 return scsi_prep_return(q, rq, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592/**
593 * sd_open - open a scsi disk device
594 * @inode: only i_rdev member may be used
595 * @filp: only f_mode and f_flags may be used
596 *
597 * Returns 0 if successful. Returns a negated errno value in case
598 * of error.
599 *
600 * Note: This can be called from a user context (e.g. fsck(1) )
601 * or from within the kernel (e.g. as a result of a mount(1) ).
602 * In the latter case @inode and @filp carry an abridged amount
603 * of information as noted above.
604 **/
605static int sd_open(struct inode *inode, struct file *filp)
606{
607 struct gendisk *disk = inode->i_bdev->bd_disk;
608 struct scsi_disk *sdkp;
609 struct scsi_device *sdev;
610 int retval;
611
612 if (!(sdkp = scsi_disk_get(disk)))
613 return -ENXIO;
614
615
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500616 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 sdev = sdkp->device;
619
620 /*
621 * If the device is in error recovery, wait until it is done.
622 * If the device is offline, then disallow any access to it.
623 */
624 retval = -ENXIO;
625 if (!scsi_block_when_processing_errors(sdev))
626 goto error_out;
627
628 if (sdev->removable || sdkp->write_prot)
629 check_disk_change(inode->i_bdev);
630
631 /*
632 * If the drive is empty, just let the open fail.
633 */
634 retval = -ENOMEDIUM;
635 if (sdev->removable && !sdkp->media_present &&
636 !(filp->f_flags & O_NDELAY))
637 goto error_out;
638
639 /*
640 * If the device has the write protect tab set, have the open fail
641 * if the user expects to be able to write to the thing.
642 */
643 retval = -EROFS;
644 if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
645 goto error_out;
646
647 /*
648 * It is possible that the disk changing stuff resulted in
649 * the device being taken offline. If this is the case,
650 * report this to the user, and don't pretend that the
651 * open actually succeeded.
652 */
653 retval = -ENXIO;
654 if (!scsi_device_online(sdev))
655 goto error_out;
656
657 if (!sdkp->openers++ && sdev->removable) {
658 if (scsi_block_when_processing_errors(sdev))
659 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
660 }
661
662 return 0;
663
664error_out:
665 scsi_disk_put(sdkp);
666 return retval;
667}
668
669/**
670 * sd_release - invoked when the (last) close(2) is called on this
671 * scsi disk.
672 * @inode: only i_rdev member may be used
673 * @filp: only f_mode and f_flags may be used
674 *
675 * Returns 0.
676 *
677 * Note: may block (uninterruptible) if error recovery is underway
678 * on this disk.
679 **/
680static int sd_release(struct inode *inode, struct file *filp)
681{
682 struct gendisk *disk = inode->i_bdev->bd_disk;
683 struct scsi_disk *sdkp = scsi_disk(disk);
684 struct scsi_device *sdev = sdkp->device;
685
James Bottomley56937f72007-03-11 12:25:33 -0500686 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (!--sdkp->openers && sdev->removable) {
689 if (scsi_block_when_processing_errors(sdev))
690 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
691 }
692
693 /*
694 * XXX and what if there are packets in flight and this close()
695 * XXX is followed by a "rmmod sd_mod"?
696 */
697 scsi_disk_put(sdkp);
698 return 0;
699}
700
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800701static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
704 struct scsi_device *sdp = sdkp->device;
705 struct Scsi_Host *host = sdp->host;
706 int diskinfo[4];
707
708 /* default to most commonly used values */
709 diskinfo[0] = 0x40; /* 1 << 6 */
710 diskinfo[1] = 0x20; /* 1 << 5 */
711 diskinfo[2] = sdkp->capacity >> 11;
712
713 /* override with calculated, extended default, or driver values */
714 if (host->hostt->bios_param)
715 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
716 else
717 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
718
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800719 geo->heads = diskinfo[0];
720 geo->sectors = diskinfo[1];
721 geo->cylinders = diskinfo[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return 0;
723}
724
725/**
726 * sd_ioctl - process an ioctl
727 * @inode: only i_rdev/i_bdev members may be used
728 * @filp: only f_mode and f_flags may be used
729 * @cmd: ioctl command number
730 * @arg: this is third argument given to ioctl(2) system call.
731 * Often contains a pointer.
732 *
733 * Returns 0 if successful (some ioctls return postive numbers on
734 * success as well). Returns a negated errno value in case of error.
735 *
736 * Note: most ioctls are forward onto the block subsystem or further
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200737 * down in the scsi subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 **/
739static int sd_ioctl(struct inode * inode, struct file * filp,
740 unsigned int cmd, unsigned long arg)
741{
742 struct block_device *bdev = inode->i_bdev;
743 struct gendisk *disk = bdev->bd_disk;
744 struct scsi_device *sdp = scsi_disk(disk)->device;
745 void __user *p = (void __user *)arg;
746 int error;
747
748 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
749 disk->disk_name, cmd));
750
751 /*
752 * If we are in the middle of error recovery, don't let anyone
753 * else try and use this device. Also, if error recovery fails, it
754 * may try and take the device offline, in which case all further
755 * access to the device is prohibited.
756 */
757 error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
758 if (!scsi_block_when_processing_errors(sdp) || !error)
759 return error;
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /*
762 * Send SCSI addressing ioctls directly to mid level, send other
763 * ioctls to block level and then onto mid level if they can't be
764 * resolved.
765 */
766 switch (cmd) {
767 case SCSI_IOCTL_GET_IDLUN:
768 case SCSI_IOCTL_GET_BUS_NUMBER:
769 return scsi_ioctl(sdp, cmd, p);
770 default:
FUJITA Tomonori45e79a32007-07-09 12:39:20 +0200771 error = scsi_cmd_ioctl(filp, disk->queue, disk, cmd, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (error != -ENOTTY)
773 return error;
774 }
775 return scsi_ioctl(sdp, cmd, p);
776}
777
778static void set_media_not_present(struct scsi_disk *sdkp)
779{
780 sdkp->media_present = 0;
781 sdkp->capacity = 0;
782 sdkp->device->changed = 1;
783}
784
785/**
786 * sd_media_changed - check if our medium changed
787 * @disk: kernel device descriptor
788 *
789 * Returns 0 if not applicable or no change; 1 if change
790 *
791 * Note: this function is invoked from the block subsystem.
792 **/
793static int sd_media_changed(struct gendisk *disk)
794{
795 struct scsi_disk *sdkp = scsi_disk(disk);
796 struct scsi_device *sdp = sdkp->device;
James Bottomley001aac22007-12-02 19:10:40 +0200797 struct scsi_sense_hdr *sshdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 int retval;
799
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500800 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 if (!sdp->removable)
803 return 0;
804
805 /*
806 * If the device is offline, don't send any commands - just pretend as
807 * if the command failed. If the device ever comes back online, we
808 * can deal with it then. It is only because of unrecoverable errors
809 * that we would ever take a device offline in the first place.
810 */
Kay Sievers285e9672007-08-14 14:10:39 +0200811 if (!scsi_device_online(sdp)) {
812 set_media_not_present(sdkp);
813 retval = 1;
814 goto out;
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /*
818 * Using TEST_UNIT_READY enables differentiation between drive with
819 * no cartridge loaded - NOT READY, drive with changed cartridge -
820 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
821 *
822 * Drives that auto spin down. eg iomega jaz 1G, will be started
823 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
824 * sd_revalidate() is called.
825 */
826 retval = -ENODEV;
Kay Sievers285e9672007-08-14 14:10:39 +0200827
James Bottomley001aac22007-12-02 19:10:40 +0200828 if (scsi_block_when_processing_errors(sdp)) {
829 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
830 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
831 sshdr);
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /*
835 * Unable to test, unit probably not ready. This usually
836 * means there is no disc in the drive. Mark as changed,
837 * and we will figure it out later once the drive is
838 * available again.
839 */
James Bottomley001aac22007-12-02 19:10:40 +0200840 if (retval || (scsi_sense_valid(sshdr) &&
841 /* 0x3a is medium not present */
842 sshdr->asc == 0x3a)) {
Kay Sievers285e9672007-08-14 14:10:39 +0200843 set_media_not_present(sdkp);
844 retval = 1;
845 goto out;
846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 /*
849 * For removable scsi disk we have to recognise the presence
850 * of a disk in the drive. This is kept in the struct scsi_disk
851 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
852 */
853 sdkp->media_present = 1;
854
855 retval = sdp->changed;
856 sdp->changed = 0;
Kay Sievers285e9672007-08-14 14:10:39 +0200857out:
858 if (retval != sdkp->previous_state)
859 sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
860 sdkp->previous_state = retval;
James Bottomley001aac22007-12-02 19:10:40 +0200861 kfree(sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Martin K. Petersene73aec82007-02-27 22:40:55 -0500865static int sd_sync_cache(struct scsi_disk *sdkp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 int retries, res;
Martin K. Petersene73aec82007-02-27 22:40:55 -0500868 struct scsi_device *sdp = sdkp->device;
James Bottomleyea73a9f2005-08-28 11:33:52 -0500869 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 if (!scsi_device_online(sdp))
872 return -ENODEV;
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 for (retries = 3; retries > 0; --retries) {
876 unsigned char cmd[10] = { 0 };
877
878 cmd[0] = SYNCHRONIZE_CACHE;
879 /*
880 * Leave the rest of the command zero to indicate
881 * flush everything.
882 */
James Bottomleyea73a9f2005-08-28 11:33:52 -0500883 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
884 SD_TIMEOUT, SD_MAX_RETRIES);
885 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 break;
887 }
888
Martin K. Petersene73aec82007-02-27 22:40:55 -0500889 if (res) {
890 sd_print_result(sdkp, res);
891 if (driver_byte(res) & DRIVER_SENSE)
892 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894
Tejun Heo37210502007-03-21 00:07:18 +0900895 if (res)
896 return -EIO;
897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Jens Axboe165125e2007-07-24 09:28:11 +0200900static void sd_prepare_flush(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200902 rq->cmd_type = REQ_TYPE_BLOCK_PC;
James Bottomleyc0ed79a2005-11-08 09:21:07 -0500903 rq->timeout = SD_TIMEOUT;
904 rq->cmd[0] = SYNCHRONIZE_CACHE;
Tejun Heo461d4e92006-01-06 09:52:55 +0100905 rq->cmd_len = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
908static void sd_rescan(struct device *dev)
909{
Alan Stern39b7f1e2005-11-04 14:44:41 -0500910 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
911
912 if (sdkp) {
913 sd_revalidate_disk(sdkp->disk);
914 scsi_disk_put(sdkp);
915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918
919#ifdef CONFIG_COMPAT
920/*
921 * This gets directly called from VFS. When the ioctl
922 * is not recognized we go back to the other translation paths.
923 */
924static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
925{
Josef Sipek7ac62072006-12-08 02:37:37 -0800926 struct block_device *bdev = file->f_path.dentry->d_inode->i_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 struct gendisk *disk = bdev->bd_disk;
928 struct scsi_device *sdev = scsi_disk(disk)->device;
929
930 /*
931 * If we are in the middle of error recovery, don't let anyone
932 * else try and use this device. Also, if error recovery fails, it
933 * may try and take the device offline, in which case all further
934 * access to the device is prohibited.
935 */
936 if (!scsi_block_when_processing_errors(sdev))
937 return -ENODEV;
938
939 if (sdev->host->hostt->compat_ioctl) {
940 int ret;
941
942 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
943
944 return ret;
945 }
946
947 /*
948 * Let the static ioctl translation table take care of it.
949 */
950 return -ENOIOCTLCMD;
951}
952#endif
953
954static struct block_device_operations sd_fops = {
955 .owner = THIS_MODULE,
956 .open = sd_open,
957 .release = sd_release,
958 .ioctl = sd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800959 .getgeo = sd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960#ifdef CONFIG_COMPAT
961 .compat_ioctl = sd_compat_ioctl,
962#endif
963 .media_changed = sd_media_changed,
964 .revalidate_disk = sd_revalidate_disk,
965};
966
Martin K. Petersenaf55ff62008-07-17 04:28:35 -0400967static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
968{
969 u64 start_lba = scmd->request->sector;
970 u64 end_lba = scmd->request->sector + (scsi_bufflen(scmd) / 512);
971 u64 bad_lba;
972 int info_valid;
973
974 if (!blk_fs_request(scmd->request))
975 return 0;
976
977 info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
978 SCSI_SENSE_BUFFERSIZE,
979 &bad_lba);
980 if (!info_valid)
981 return 0;
982
983 if (scsi_bufflen(scmd) <= scmd->device->sector_size)
984 return 0;
985
986 if (scmd->device->sector_size < 512) {
987 /* only legitimate sector_size here is 256 */
988 start_lba <<= 1;
989 end_lba <<= 1;
990 } else {
991 /* be careful ... don't want any overflows */
992 u64 factor = scmd->device->sector_size / 512;
993 do_div(start_lba, factor);
994 do_div(end_lba, factor);
995 }
996
997 /* The bad lba was reported incorrectly, we have no idea where
998 * the error is.
999 */
1000 if (bad_lba < start_lba || bad_lba >= end_lba)
1001 return 0;
1002
1003 /* This computation should always be done in terms of
1004 * the resolution of the device's medium.
1005 */
1006 return (bad_lba - start_lba) * scmd->device->sector_size;
1007}
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009/**
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001010 * sd_done - bottom half handler: called when the lower level
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 * driver has completed (successfully or otherwise) a scsi command.
1012 * @SCpnt: mid-level's per command structure.
1013 *
1014 * Note: potentially run from within an ISR. Must not block.
1015 **/
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001016static int sd_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 int result = SCpnt->result;
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001019 unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct scsi_sense_hdr sshdr;
1021 int sense_valid = 0;
1022 int sense_deferred = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if (result) {
1025 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
1026 if (sense_valid)
1027 sense_deferred = scsi_sense_is_deferred(&sshdr);
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029#ifdef CONFIG_SCSI_LOGGING
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001030 SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (sense_valid) {
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001032 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001033 "sd_done: sb[respc,sk,asc,"
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001034 "ascq]=%x,%x,%x,%x\n",
1035 sshdr.response_code,
1036 sshdr.sense_key, sshdr.asc,
1037 sshdr.ascq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039#endif
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001040 if (driver_byte(result) != DRIVER_SENSE &&
1041 (!sense_valid || sense_deferred))
1042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001044 switch (sshdr.sense_key) {
1045 case HARDWARE_ERROR:
1046 case MEDIUM_ERROR:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001047 good_bytes = sd_completed_bytes(SCpnt);
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001048 break;
1049 case RECOVERED_ERROR:
1050 case NO_SENSE:
1051 /* Inform the user, but make sure that it's not treated
1052 * as a hard error.
1053 */
1054 scsi_print_sense("sd", SCpnt);
1055 SCpnt->result = 0;
1056 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001057 good_bytes = scsi_bufflen(SCpnt);
1058 break;
1059 case ABORTED_COMMAND:
1060 if (sshdr.asc == 0x10) { /* DIF: Disk detected corruption */
1061 scsi_print_result(SCpnt);
1062 scsi_print_sense("sd", SCpnt);
1063 good_bytes = sd_completed_bytes(SCpnt);
1064 }
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001065 break;
1066 case ILLEGAL_REQUEST:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001067 if (sshdr.asc == 0x10) { /* DIX: HBA detected corruption */
1068 scsi_print_result(SCpnt);
1069 scsi_print_sense("sd", SCpnt);
1070 good_bytes = sd_completed_bytes(SCpnt);
1071 }
1072 if (!scsi_device_protection(SCpnt->device) &&
1073 SCpnt->device->use_10_for_rw &&
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001074 (SCpnt->cmnd[0] == READ_10 ||
1075 SCpnt->cmnd[0] == WRITE_10))
1076 SCpnt->device->use_10_for_rw = 0;
1077 if (SCpnt->device->use_10_for_ms &&
1078 (SCpnt->cmnd[0] == MODE_SENSE_10 ||
1079 SCpnt->cmnd[0] == MODE_SELECT_10))
1080 SCpnt->device->use_10_for_ms = 0;
1081 break;
1082 default:
1083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001085 out:
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001086 if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
1087 sd_dif_complete(SCpnt, good_bytes);
1088
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001089 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
James Bottomleyea73a9f2005-08-28 11:33:52 -05001092static int media_not_present(struct scsi_disk *sdkp,
1093 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
James Bottomleyea73a9f2005-08-28 11:33:52 -05001096 if (!scsi_sense_valid(sshdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return 0;
1098 /* not invoked for commands that could return deferred errors */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001099 if (sshdr->sense_key != NOT_READY &&
1100 sshdr->sense_key != UNIT_ATTENTION)
1101 return 0;
1102 if (sshdr->asc != 0x3A) /* medium not present */
1103 return 0;
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 set_media_not_present(sdkp);
1106 return 1;
1107}
1108
1109/*
1110 * spinup disk - called only in sd_revalidate_disk()
1111 */
1112static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001113sd_spinup_disk(struct scsi_disk *sdkp)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001114{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 unsigned char cmd[10];
Alan Stern4451e472005-07-12 10:45:17 -04001116 unsigned long spintime_expire = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int retries, spintime;
1118 unsigned int the_result;
1119 struct scsi_sense_hdr sshdr;
1120 int sense_valid = 0;
1121
1122 spintime = 0;
1123
1124 /* Spin up drives, as required. Only do this at boot time */
1125 /* Spinup needs to be done for module loads too. */
1126 do {
1127 retries = 0;
1128
1129 do {
1130 cmd[0] = TEST_UNIT_READY;
1131 memset((void *) &cmd[1], 0, 9);
1132
James Bottomleyea73a9f2005-08-28 11:33:52 -05001133 the_result = scsi_execute_req(sdkp->device, cmd,
1134 DMA_NONE, NULL, 0,
1135 &sshdr, SD_TIMEOUT,
1136 SD_MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Alan Sternb4d38e32006-10-11 16:48:28 -04001138 /*
1139 * If the drive has indicated to us that it
1140 * doesn't have any media in it, don't bother
1141 * with any more polling.
1142 */
1143 if (media_not_present(sdkp, &sshdr))
1144 return;
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (the_result)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001147 sense_valid = scsi_sense_valid(&sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 retries++;
1149 } while (retries < 3 &&
1150 (!scsi_status_is_good(the_result) ||
1151 ((driver_byte(the_result) & DRIVER_SENSE) &&
1152 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1155 /* no sense, TUR either succeeded or failed
1156 * with a status error */
Martin K. Petersene73aec82007-02-27 22:40:55 -05001157 if(!spintime && !scsi_status_is_good(the_result)) {
1158 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1159 sd_print_result(sdkp, the_result);
1160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 break;
1162 }
1163
1164 /*
1165 * The device does not want the automatic start to be issued.
1166 */
1167 if (sdkp->device->no_start_on_add) {
1168 break;
1169 }
1170
1171 /*
1172 * If manual intervention is required, or this is an
1173 * absent USB storage device, a spinup is meaningless.
1174 */
1175 if (sense_valid &&
1176 sshdr.sense_key == NOT_READY &&
1177 sshdr.asc == 4 && sshdr.ascq == 3) {
1178 break; /* manual intervention required */
1179
1180 /*
1181 * Issue command to spin up drive when not ready
1182 */
1183 } else if (sense_valid && sshdr.sense_key == NOT_READY) {
1184 if (!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001185 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 cmd[0] = START_STOP;
1187 cmd[1] = 1; /* Return immediately */
1188 memset((void *) &cmd[2], 0, 8);
1189 cmd[4] = 1; /* Start spin cycle */
Stefan Richterd2886ea2008-05-11 00:34:07 +02001190 if (sdkp->device->start_stop_pwr_cond)
1191 cmd[4] |= 1 << 4;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001192 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1193 NULL, 0, &sshdr,
1194 SD_TIMEOUT, SD_MAX_RETRIES);
Alan Stern4451e472005-07-12 10:45:17 -04001195 spintime_expire = jiffies + 100 * HZ;
1196 spintime = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* Wait 1 second for next try */
1199 msleep(1000);
1200 printk(".");
Alan Stern4451e472005-07-12 10:45:17 -04001201
1202 /*
1203 * Wait for USB flash devices with slow firmware.
1204 * Yes, this sense key/ASC combination shouldn't
1205 * occur here. It's characteristic of these devices.
1206 */
1207 } else if (sense_valid &&
1208 sshdr.sense_key == UNIT_ATTENTION &&
1209 sshdr.asc == 0x28) {
1210 if (!spintime) {
1211 spintime_expire = jiffies + 5 * HZ;
1212 spintime = 1;
1213 }
1214 /* Wait 1 second for next try */
1215 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 } else {
1217 /* we don't understand the sense code, so it's
1218 * probably pointless to loop */
1219 if(!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001220 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1221 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223 break;
1224 }
1225
Alan Stern4451e472005-07-12 10:45:17 -04001226 } while (spintime && time_before_eq(jiffies, spintime_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (spintime) {
1229 if (scsi_status_is_good(the_result))
1230 printk("ready\n");
1231 else
1232 printk("not responding...\n");
1233 }
1234}
1235
Martin K. Petersene0597d72008-07-17 04:28:34 -04001236
1237/*
1238 * Determine whether disk supports Data Integrity Field.
1239 */
1240void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
1241{
1242 struct scsi_device *sdp = sdkp->device;
1243 u8 type;
1244
1245 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
1246 type = 0;
1247 else
1248 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
1249
1250 switch (type) {
1251 case SD_DIF_TYPE0_PROTECTION:
1252 sdkp->protection_type = 0;
1253 break;
1254
1255 case SD_DIF_TYPE1_PROTECTION:
1256 case SD_DIF_TYPE3_PROTECTION:
1257 sdkp->protection_type = type;
1258 break;
1259
1260 case SD_DIF_TYPE2_PROTECTION:
1261 sd_printk(KERN_ERR, sdkp, "formatted with DIF Type 2 " \
1262 "protection which is currently unsupported. " \
1263 "Disabling disk!\n");
1264 goto disable;
1265
1266 default:
1267 sd_printk(KERN_ERR, sdkp, "formatted with unknown " \
1268 "protection type %d. Disabling disk!\n", type);
1269 goto disable;
1270 }
1271
1272 return;
1273
1274disable:
1275 sdkp->protection_type = 0;
1276 sdkp->capacity = 0;
1277}
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279/*
1280 * read disk capacity
1281 */
1282static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001283sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001284{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 unsigned char cmd[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int the_result, retries;
1287 int sector_size = 0;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001288 /* Force READ CAPACITY(16) when PROTECT=1 */
1289 int longrc = scsi_device_protection(sdkp->device) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 struct scsi_sense_hdr sshdr;
1291 int sense_valid = 0;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001292 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294repeat:
1295 retries = 3;
1296 do {
1297 if (longrc) {
1298 memset((void *) cmd, 0, 16);
1299 cmd[0] = SERVICE_ACTION_IN;
1300 cmd[1] = SAI_READ_CAPACITY_16;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001301 cmd[13] = 13;
1302 memset((void *) buffer, 0, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 } else {
1304 cmd[0] = READ_CAPACITY;
1305 memset((void *) &cmd[1], 0, 9);
1306 memset((void *) buffer, 0, 8);
1307 }
1308
James Bottomleyea73a9f2005-08-28 11:33:52 -05001309 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
Martin K. Petersene0597d72008-07-17 04:28:34 -04001310 buffer, longrc ? 13 : 8, &sshdr,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001311 SD_TIMEOUT, SD_MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
James Bottomleyea73a9f2005-08-28 11:33:52 -05001313 if (media_not_present(sdkp, &sshdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return;
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (the_result)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001317 sense_valid = scsi_sense_valid(&sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 retries--;
1319
1320 } while (the_result && retries);
1321
1322 if (the_result && !longrc) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001323 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1324 sd_print_result(sdkp, the_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (driver_byte(the_result) & DRIVER_SENSE)
Martin K. Petersene73aec82007-02-27 22:40:55 -05001326 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 else
Martin K. Petersene73aec82007-02-27 22:40:55 -05001328 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* Set dirty bit for removable devices if not ready -
1331 * sometimes drives will not report this properly. */
1332 if (sdp->removable &&
1333 sense_valid && sshdr.sense_key == NOT_READY)
1334 sdp->changed = 1;
1335
1336 /* Either no media are present but the drive didn't tell us,
1337 or they are present but the read capacity command fails */
1338 /* sdkp->media_present = 0; -- not always correct */
Hannes Reinecke69bdd882006-09-01 15:50:23 +02001339 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 return;
1342 } else if (the_result && longrc) {
1343 /* READ CAPACITY(16) has been failed */
Martin K. Petersene73aec82007-02-27 22:40:55 -05001344 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
1345 sd_print_result(sdkp, the_result);
1346 sd_printk(KERN_NOTICE, sdkp, "Use 0xffffffff as device size\n");
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1349 goto got_data;
1350 }
1351
1352 if (!longrc) {
1353 sector_size = (buffer[4] << 24) |
1354 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1355 if (buffer[0] == 0xff && buffer[1] == 0xff &&
1356 buffer[2] == 0xff && buffer[3] == 0xff) {
1357 if(sizeof(sdkp->capacity) > 4) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001358 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1359 "Trying to use READ CAPACITY(16).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 longrc = 1;
1361 goto repeat;
1362 }
Martin K. Petersene73aec82007-02-27 22:40:55 -05001363 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use "
1364 "a kernel compiled with support for large "
1365 "block devices.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 sdkp->capacity = 0;
1367 goto got_data;
1368 }
1369 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
1370 (buffer[1] << 16) |
1371 (buffer[2] << 8) |
1372 buffer[3]);
1373 } else {
1374 sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
1375 ((u64)buffer[1] << 48) |
1376 ((u64)buffer[2] << 40) |
1377 ((u64)buffer[3] << 32) |
1378 ((sector_t)buffer[4] << 24) |
1379 ((sector_t)buffer[5] << 16) |
1380 ((sector_t)buffer[6] << 8) |
1381 (sector_t)buffer[7]);
1382
1383 sector_size = (buffer[8] << 24) |
1384 (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
Martin K. Petersene0597d72008-07-17 04:28:34 -04001385
1386 sd_read_protection_type(sdkp, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388
1389 /* Some devices return the total number of sectors, not the
1390 * highest sector number. Make the necessary adjustment. */
Oliver Neukum61bf54b2007-02-08 09:04:48 +01001391 if (sdp->fix_capacity) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 --sdkp->capacity;
1393
Oliver Neukum61bf54b2007-02-08 09:04:48 +01001394 /* Some devices have version which report the correct sizes
1395 * and others which do not. We guess size according to a heuristic
1396 * and err on the side of lowering the capacity. */
1397 } else {
1398 if (sdp->guess_capacity)
1399 if (sdkp->capacity & 0x01) /* odd sizes are odd */
1400 --sdkp->capacity;
1401 }
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403got_data:
1404 if (sector_size == 0) {
1405 sector_size = 512;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001406 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
1407 "assuming 512.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
1410 if (sector_size != 512 &&
1411 sector_size != 1024 &&
1412 sector_size != 2048 &&
1413 sector_size != 4096 &&
1414 sector_size != 256) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001415 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
1416 sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /*
1418 * The user might want to re-format the drive with
1419 * a supported sectorsize. Once this happens, it
1420 * would be relatively trivial to set the thing up.
1421 * For this reason, we leave the thing in the table.
1422 */
1423 sdkp->capacity = 0;
1424 /*
1425 * set a bogus sector size so the normal read/write
1426 * logic in the block layer will eventually refuse any
1427 * request on this device without tripping over power
1428 * of two sector size assumptions
1429 */
1430 sector_size = 512;
1431 }
1432 {
1433 /*
1434 * The msdos fs needs to know the hardware sector size
1435 * So I have created this table. See ll_rw_blk.c
1436 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1437 */
1438 int hard_sector = sector_size;
James Bottomley7a691bd2005-10-28 13:17:30 -05001439 sector_t sz = (sdkp->capacity/2) * (hard_sector/256);
Jens Axboe165125e2007-07-24 09:28:11 +02001440 struct request_queue *queue = sdp->request_queue;
James Bottomley7a691bd2005-10-28 13:17:30 -05001441 sector_t mb = sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 blk_queue_hardsect_size(queue, hard_sector);
1444 /* avoid 64-bit division on 32-bit platforms */
James Bottomley7a691bd2005-10-28 13:17:30 -05001445 sector_div(sz, 625);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 mb -= sz - 974;
1447 sector_div(mb, 1950);
1448
Martin K. Petersene73aec82007-02-27 22:40:55 -05001449 sd_printk(KERN_NOTICE, sdkp,
1450 "%llu %d-byte hardware sectors (%llu MB)\n",
1451 (unsigned long long)sdkp->capacity,
1452 hard_sector, (unsigned long long)mb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454
1455 /* Rescale capacity to 512-byte units */
1456 if (sector_size == 4096)
1457 sdkp->capacity <<= 3;
1458 else if (sector_size == 2048)
1459 sdkp->capacity <<= 2;
1460 else if (sector_size == 1024)
1461 sdkp->capacity <<= 1;
1462 else if (sector_size == 256)
1463 sdkp->capacity >>= 1;
1464
1465 sdkp->device->sector_size = sector_size;
1466}
1467
1468/* called with buffer of length 512 */
1469static inline int
James Bottomleyea73a9f2005-08-28 11:33:52 -05001470sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1471 unsigned char *buffer, int len, struct scsi_mode_data *data,
1472 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
James Bottomleyea73a9f2005-08-28 11:33:52 -05001474 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
James Bottomley1cf72692005-08-28 11:27:01 -05001475 SD_TIMEOUT, SD_MAX_RETRIES, data,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001476 sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479/*
1480 * read write protect setting, if possible - called only in sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06001481 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 */
1483static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001484sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001485{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 int res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001487 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 struct scsi_mode_data data;
1489
1490 set_disk_ro(sdkp->disk, 0);
James Bottomleyea73a9f2005-08-28 11:33:52 -05001491 if (sdp->skip_ms_page_3f) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001492 sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return;
1494 }
1495
James Bottomleyea73a9f2005-08-28 11:33:52 -05001496 if (sdp->use_192_bytes_for_3f) {
1497 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 } else {
1499 /*
1500 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1501 * We have to start carefully: some devices hang if we ask
1502 * for more than is available.
1503 */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001504 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 /*
1507 * Second attempt: ask for page 0 When only page 0 is
1508 * implemented, a request for page 3F may return Sense Key
1509 * 5: Illegal Request, Sense Code 24: Invalid field in
1510 * CDB.
1511 */
1512 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05001513 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 /*
1516 * Third attempt: ask 255 bytes, as we did earlier.
1517 */
1518 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05001519 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1520 &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
1523 if (!scsi_status_is_good(res)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001524 sd_printk(KERN_WARNING, sdkp,
1525 "Test WP failed, assume Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 } else {
1527 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1528 set_disk_ro(sdkp->disk, sdkp->write_prot);
Martin K. Petersene73aec82007-02-27 22:40:55 -05001529 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
1530 sdkp->write_prot ? "on" : "off");
1531 sd_printk(KERN_DEBUG, sdkp,
1532 "Mode Sense: %02x %02x %02x %02x\n",
1533 buffer[0], buffer[1], buffer[2], buffer[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535}
1536
1537/*
1538 * sd_read_cache_type - called only from sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06001539 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 */
1541static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001542sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
Al Viro 631e8a12005-05-16 01:59:55 +01001543{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 int len = 0, res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001545 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Al Viro 631e8a12005-05-16 01:59:55 +01001547 int dbd;
1548 int modepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 struct scsi_mode_data data;
1550 struct scsi_sense_hdr sshdr;
1551
James Bottomleyea73a9f2005-08-28 11:33:52 -05001552 if (sdp->skip_ms_page_8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 goto defaults;
1554
James Bottomleyea73a9f2005-08-28 11:33:52 -05001555 if (sdp->type == TYPE_RBC) {
Al Viro 631e8a12005-05-16 01:59:55 +01001556 modepage = 6;
1557 dbd = 8;
1558 } else {
1559 modepage = 8;
1560 dbd = 0;
1561 }
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /* cautiously ask */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001564 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 if (!scsi_status_is_good(res))
1567 goto bad_sense;
1568
Al Viro6d73c852006-02-23 02:03:16 +01001569 if (!data.header_length) {
1570 modepage = 6;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001571 sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
Al Viro6d73c852006-02-23 02:03:16 +01001572 }
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 /* that went OK, now ask for the proper length */
1575 len = data.length;
1576
1577 /*
1578 * We're only interested in the first three bytes, actually.
1579 * But the data cache page is defined for the first 20.
1580 */
1581 if (len < 3)
1582 goto bad_sense;
1583 if (len > 20)
1584 len = 20;
1585
1586 /* Take headers and block descriptors into account */
1587 len += data.header_length + data.block_descriptor_length;
Al Viro48970802006-02-26 08:34:10 -06001588 if (len > SD_BUF_SIZE)
1589 goto bad_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 /* Get the data */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001592 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 if (scsi_status_is_good(res)) {
Al Viro 631e8a12005-05-16 01:59:55 +01001595 int offset = data.header_length + data.block_descriptor_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Al Viro48970802006-02-26 08:34:10 -06001597 if (offset >= SD_BUF_SIZE - 2) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001598 sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
Al Viro48970802006-02-26 08:34:10 -06001599 goto defaults;
1600 }
1601
Al Viro 631e8a12005-05-16 01:59:55 +01001602 if ((buffer[offset] & 0x3f) != modepage) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001603 sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
Al Viro 631e8a12005-05-16 01:59:55 +01001604 goto defaults;
1605 }
1606
1607 if (modepage == 8) {
1608 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1609 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1610 } else {
1611 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1612 sdkp->RCD = 0;
1613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Tejun Heo007365a2006-01-06 09:53:52 +01001615 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1616 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001617 sd_printk(KERN_NOTICE, sdkp,
1618 "Uses READ/WRITE(6), disabling FUA\n");
Tejun Heo007365a2006-01-06 09:53:52 +01001619 sdkp->DPOFUA = 0;
1620 }
1621
Martin K. Petersene73aec82007-02-27 22:40:55 -05001622 sd_printk(KERN_NOTICE, sdkp,
1623 "Write cache: %s, read cache: %s, %s\n",
Luben Tuikovfd44bab2006-11-15 12:47:08 -08001624 sdkp->WCE ? "enabled" : "disabled",
1625 sdkp->RCD ? "disabled" : "enabled",
1626 sdkp->DPOFUA ? "supports DPO and FUA"
1627 : "doesn't support DPO or FUA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 return;
1630 }
1631
1632bad_sense:
James Bottomleyea73a9f2005-08-28 11:33:52 -05001633 if (scsi_sense_valid(&sshdr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 sshdr.sense_key == ILLEGAL_REQUEST &&
1635 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
Martin K. Petersene73aec82007-02-27 22:40:55 -05001636 /* Invalid field in CDB */
1637 sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 else
Martin K. Petersene73aec82007-02-27 22:40:55 -05001639 sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641defaults:
Martin K. Petersene73aec82007-02-27 22:40:55 -05001642 sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 sdkp->WCE = 0;
1644 sdkp->RCD = 0;
Al Viro48970802006-02-26 08:34:10 -06001645 sdkp->DPOFUA = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
Martin K. Petersene0597d72008-07-17 04:28:34 -04001648/*
1649 * The ATO bit indicates whether the DIF application tag is available
1650 * for use by the operating system.
1651 */
1652void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
1653{
1654 int res, offset;
1655 struct scsi_device *sdp = sdkp->device;
1656 struct scsi_mode_data data;
1657 struct scsi_sense_hdr sshdr;
1658
1659 if (sdp->type != TYPE_DISK)
1660 return;
1661
1662 if (sdkp->protection_type == 0)
1663 return;
1664
1665 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
1666 SD_MAX_RETRIES, &data, &sshdr);
1667
1668 if (!scsi_status_is_good(res) || !data.header_length ||
1669 data.length < 6) {
1670 sd_printk(KERN_WARNING, sdkp,
1671 "getting Control mode page failed, assume no ATO\n");
1672
1673 if (scsi_sense_valid(&sshdr))
1674 sd_print_sense_hdr(sdkp, &sshdr);
1675
1676 return;
1677 }
1678
1679 offset = data.header_length + data.block_descriptor_length;
1680
1681 if ((buffer[offset] & 0x3f) != 0x0a) {
1682 sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
1683 return;
1684 }
1685
1686 if ((buffer[offset + 5] & 0x80) == 0)
1687 return;
1688
1689 sdkp->ATO = 1;
1690
1691 return;
1692}
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694/**
1695 * sd_revalidate_disk - called the first time a new disk is seen,
1696 * performs disk spin up, read_capacity, etc.
1697 * @disk: struct gendisk we care about
1698 **/
1699static int sd_revalidate_disk(struct gendisk *disk)
1700{
1701 struct scsi_disk *sdkp = scsi_disk(disk);
1702 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 unsigned char *buffer;
Tejun Heo461d4e92006-01-06 09:52:55 +01001704 unsigned ordered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001706 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
1707 "sd_revalidate_disk\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 /*
1710 * If the device is offline, don't try and read capacity or any
1711 * of the other niceties.
1712 */
1713 if (!scsi_device_online(sdp))
1714 goto out;
1715
Bernhard Wallea6123f12007-05-21 17:15:26 +02001716 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if (!buffer) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001718 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
1719 "allocation failure.\n");
James Bottomleyea73a9f2005-08-28 11:33:52 -05001720 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722
1723 /* defaults, until the device tells us otherwise */
1724 sdp->sector_size = 512;
1725 sdkp->capacity = 0;
1726 sdkp->media_present = 1;
1727 sdkp->write_prot = 0;
1728 sdkp->WCE = 0;
1729 sdkp->RCD = 0;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001730 sdkp->ATO = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Martin K. Petersene73aec82007-02-27 22:40:55 -05001732 sd_spinup_disk(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 /*
1735 * Without media there is no reason to ask; moreover, some devices
1736 * react badly if we do.
1737 */
1738 if (sdkp->media_present) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001739 sd_read_capacity(sdkp, buffer);
1740 sd_read_write_protect_flag(sdkp, buffer);
1741 sd_read_cache_type(sdkp, buffer);
Martin K. Petersene0597d72008-07-17 04:28:34 -04001742 sd_read_app_tag_own(sdkp, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
Tejun Heo461d4e92006-01-06 09:52:55 +01001744
1745 /*
1746 * We now have all cache related info, determine how we deal
1747 * with ordered requests. Note that as the current SCSI
1748 * dispatch function can alter request order, we cannot use
1749 * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
1750 */
1751 if (sdkp->WCE)
Tejun Heo007365a2006-01-06 09:53:52 +01001752 ordered = sdkp->DPOFUA
1753 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
Tejun Heo461d4e92006-01-06 09:52:55 +01001754 else
1755 ordered = QUEUE_ORDERED_DRAIN;
1756
1757 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 set_capacity(disk, sdkp->capacity);
1760 kfree(buffer);
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 out:
1763 return 0;
1764}
1765
1766/**
1767 * sd_probe - called during driver initialization and whenever a
1768 * new scsi device is attached to the system. It is called once
1769 * for each scsi device (not just disks) present.
1770 * @dev: pointer to device object
1771 *
1772 * Returns 0 if successful (or not interested in this scsi device
1773 * (e.g. scanner)); 1 when there is an error.
1774 *
1775 * Note: this function is invoked from the scsi mid-level.
1776 * This function sets up the mapping between a given
1777 * <host,channel,id,lun> (found in sdp) and new device name
1778 * (e.g. /dev/sda). More precisely it is the block device major
1779 * and minor number that is chosen here.
1780 *
1781 * Assume sd_attach is not re-entrant (for time being)
1782 * Also think about sd_attach() and sd_remove() running coincidentally.
1783 **/
1784static int sd_probe(struct device *dev)
1785{
1786 struct scsi_device *sdp = to_scsi_device(dev);
1787 struct scsi_disk *sdkp;
1788 struct gendisk *gd;
1789 u32 index;
1790 int error;
1791
1792 error = -ENODEV;
Al Viro 631e8a12005-05-16 01:59:55 +01001793 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 goto out;
1795
James Bottomley9ccfc752005-10-02 11:45:08 -05001796 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
1797 "sd_attach\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -05001800 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (!sdkp)
1802 goto out;
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 gd = alloc_disk(16);
1805 if (!gd)
1806 goto out_free;
1807
Tejun Heof27bac22008-07-14 14:59:30 +09001808 do {
1809 if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
1810 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Tejun Heof27bac22008-07-14 14:59:30 +09001812 error = ida_get_new(&sd_index_ida, &index);
1813 } while (error == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (error)
1816 goto out_put;
1817
Tejun Heof27bac22008-07-14 14:59:30 +09001818 error = -EBUSY;
1819 if (index >= SD_MAX_DISKS)
1820 goto out_free_index;
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 sdkp->device = sdp;
1823 sdkp->driver = &sd_template;
1824 sdkp->disk = gd;
1825 sdkp->index = index;
1826 sdkp->openers = 0;
Kay Sieversc02e6002008-03-19 13:09:56 +01001827 sdkp->previous_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 if (!sdp->timeout) {
Al Viro 631e8a12005-05-16 01:59:55 +01001830 if (sdp->type != TYPE_MOD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 sdp->timeout = SD_TIMEOUT;
1832 else
1833 sdp->timeout = SD_MOD_TIMEOUT;
1834 }
1835
Tony Jonesee959b02008-02-22 00:13:36 +01001836 device_initialize(&sdkp->dev);
1837 sdkp->dev.parent = &sdp->sdev_gendev;
1838 sdkp->dev.class = &sd_disk_class;
1839 strncpy(sdkp->dev.bus_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE);
Nagendra Singh Tomar017f2e32007-02-02 17:34:56 +05301840
Tony Jonesee959b02008-02-22 00:13:36 +01001841 if (device_add(&sdkp->dev))
Tejun Heof27bac22008-07-14 14:59:30 +09001842 goto out_free_index;
Nagendra Singh Tomar017f2e32007-02-02 17:34:56 +05301843
1844 get_device(&sdp->sdev_gendev);
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 gd->major = sd_major((index & 0xf0) >> 4);
1847 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
1848 gd->minors = 16;
1849 gd->fops = &sd_fops;
1850
1851 if (index < 26) {
1852 sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
1853 } else if (index < (26 + 1) * 26) {
1854 sprintf(gd->disk_name, "sd%c%c",
1855 'a' + index / 26 - 1,'a' + index % 26);
1856 } else {
1857 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
1858 const unsigned int m2 = (index / 26 - 1) % 26;
1859 const unsigned int m3 = index % 26;
1860 sprintf(gd->disk_name, "sd%c%c%c",
1861 'a' + m1, 'a' + m2, 'a' + m3);
1862 }
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 gd->private_data = &sdkp->driver;
Tejun Heo461d4e92006-01-06 09:52:55 +01001865 gd->queue = sdkp->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 sd_revalidate_disk(gd);
1868
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001869 blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
James Bottomley03a57432007-08-03 16:41:11 -05001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 gd->driverfs_dev = &sdp->sdev_gendev;
1872 gd->flags = GENHD_FL_DRIVERFS;
1873 if (sdp->removable)
1874 gd->flags |= GENHD_FL_REMOVABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 dev_set_drvdata(dev, sdkp);
1877 add_disk(gd);
FUJITA Tomonoribb23b432008-08-29 11:47:07 +02001878 blk_register_filter(gd);
Martin K. Petersenaf55ff62008-07-17 04:28:35 -04001879 sd_dif_config_host(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Martin K. Petersene73aec82007-02-27 22:40:55 -05001881 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
1882 sdp->removable ? "removable " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 return 0;
1885
Tejun Heof27bac22008-07-14 14:59:30 +09001886 out_free_index:
1887 ida_remove(&sd_index_ida, index);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06001888 out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 put_disk(gd);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06001890 out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 kfree(sdkp);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06001892 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return error;
1894}
1895
1896/**
1897 * sd_remove - called whenever a scsi disk (previously recognized by
1898 * sd_probe) is detached from the system. It is called (potentially
1899 * multiple times) during sd module unload.
1900 * @sdp: pointer to mid level scsi device object
1901 *
1902 * Note: this function is invoked from the scsi mid-level.
1903 * This function potentially frees up a device name (e.g. /dev/sdc)
1904 * that could be re-used by a subsequent sd_probe().
1905 * This function is not called when the built-in sd driver is "exit-ed".
1906 **/
1907static int sd_remove(struct device *dev)
1908{
1909 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1910
Tony Jonesee959b02008-02-22 00:13:36 +01001911 device_del(&sdkp->dev);
FUJITA Tomonoribb23b432008-08-29 11:47:07 +02001912 blk_unregister_filter(sdkp->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 del_gendisk(sdkp->disk);
1914 sd_shutdown(dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001915
Arjan van de Ven0b950672006-01-11 13:16:10 +01001916 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001917 dev_set_drvdata(dev, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01001918 put_device(&sdkp->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001919 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 return 0;
1922}
1923
1924/**
1925 * scsi_disk_release - Called to free the scsi_disk structure
Tony Jonesee959b02008-02-22 00:13:36 +01001926 * @dev: pointer to embedded class device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 *
Arjan van de Ven0b950672006-01-11 13:16:10 +01001928 * sd_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * called on last put, you should always use the scsi_disk_get()
1930 * scsi_disk_put() helpers which manipulate the semaphore directly
Tony Jonesee959b02008-02-22 00:13:36 +01001931 * and never do a direct put_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 **/
Tony Jonesee959b02008-02-22 00:13:36 +01001933static void scsi_disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Tony Jonesee959b02008-02-22 00:13:36 +01001935 struct scsi_disk *sdkp = to_scsi_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 struct gendisk *disk = sdkp->disk;
1937
Tejun Heof27bac22008-07-14 14:59:30 +09001938 ida_remove(&sd_index_ida, sdkp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 disk->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 put_disk(disk);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001942 put_device(&sdkp->device->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 kfree(sdkp);
1945}
1946
James Bottomleycc5d2c82007-03-20 12:26:03 -05001947static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
Tejun Heoc3c94c52007-03-21 00:13:59 +09001948{
1949 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
1950 struct scsi_sense_hdr sshdr;
James Bottomleycc5d2c82007-03-20 12:26:03 -05001951 struct scsi_device *sdp = sdkp->device;
Tejun Heoc3c94c52007-03-21 00:13:59 +09001952 int res;
1953
1954 if (start)
1955 cmd[4] |= 1; /* START */
1956
Stefan Richterd2886ea2008-05-11 00:34:07 +02001957 if (sdp->start_stop_pwr_cond)
1958 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
1959
Tejun Heoc3c94c52007-03-21 00:13:59 +09001960 if (!scsi_device_online(sdp))
1961 return -ENODEV;
1962
1963 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
1964 SD_TIMEOUT, SD_MAX_RETRIES);
1965 if (res) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05001966 sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
1967 sd_print_result(sdkp, res);
Tejun Heoc3c94c52007-03-21 00:13:59 +09001968 if (driver_byte(res) & DRIVER_SENSE)
James Bottomleycc5d2c82007-03-20 12:26:03 -05001969 sd_print_sense_hdr(sdkp, &sshdr);
Tejun Heoc3c94c52007-03-21 00:13:59 +09001970 }
1971
1972 return res;
1973}
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975/*
1976 * Send a SYNCHRONIZE CACHE instruction down to the device through
1977 * the normal SCSI command structure. Wait for the command to
1978 * complete.
1979 */
1980static void sd_shutdown(struct device *dev)
1981{
Alan Stern39b7f1e2005-11-04 14:44:41 -05001982 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 if (!sdkp)
1985 return; /* this can happen */
1986
Alan Stern39b7f1e2005-11-04 14:44:41 -05001987 if (sdkp->WCE) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001988 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
1989 sd_sync_cache(sdkp);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001990 }
Tejun Heoc3c94c52007-03-21 00:13:59 +09001991
James Bottomleycc5d2c82007-03-20 12:26:03 -05001992 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
1993 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
1994 sd_start_stop_device(sdkp, 0);
Tejun Heoc3c94c52007-03-21 00:13:59 +09001995 }
1996
Alan Stern39b7f1e2005-11-04 14:44:41 -05001997 scsi_disk_put(sdkp);
1998}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Tejun Heoc3c94c52007-03-21 00:13:59 +09002000static int sd_suspend(struct device *dev, pm_message_t mesg)
2001{
Tejun Heoc3c94c52007-03-21 00:13:59 +09002002 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Alan Stern09ff92f2007-05-21 09:55:04 -04002003 int ret = 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002004
2005 if (!sdkp)
2006 return 0; /* this can happen */
2007
2008 if (sdkp->WCE) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05002009 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
Tejun Heoc3c94c52007-03-21 00:13:59 +09002010 ret = sd_sync_cache(sdkp);
2011 if (ret)
Alan Stern09ff92f2007-05-21 09:55:04 -04002012 goto done;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002013 }
2014
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01002015 if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05002016 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2017 ret = sd_start_stop_device(sdkp, 0);
Tejun Heoc3c94c52007-03-21 00:13:59 +09002018 }
2019
Alan Stern09ff92f2007-05-21 09:55:04 -04002020done:
2021 scsi_disk_put(sdkp);
2022 return ret;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002023}
2024
2025static int sd_resume(struct device *dev)
2026{
Tejun Heoc3c94c52007-03-21 00:13:59 +09002027 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Alan Stern09ff92f2007-05-21 09:55:04 -04002028 int ret = 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002029
James Bottomleycc5d2c82007-03-20 12:26:03 -05002030 if (!sdkp->device->manage_start_stop)
Alan Stern09ff92f2007-05-21 09:55:04 -04002031 goto done;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002032
James Bottomleycc5d2c82007-03-20 12:26:03 -05002033 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
Alan Stern09ff92f2007-05-21 09:55:04 -04002034 ret = sd_start_stop_device(sdkp, 1);
Tejun Heoc3c94c52007-03-21 00:13:59 +09002035
Alan Stern09ff92f2007-05-21 09:55:04 -04002036done:
2037 scsi_disk_put(sdkp);
2038 return ret;
Tejun Heoc3c94c52007-03-21 00:13:59 +09002039}
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041/**
2042 * init_sd - entry point for this driver (both when built in or when
2043 * a module).
2044 *
2045 * Note: this function registers this driver with the scsi mid-level.
2046 **/
2047static int __init init_sd(void)
2048{
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002049 int majors = 0, i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
2052
2053 for (i = 0; i < SD_MAJORS; i++)
2054 if (register_blkdev(sd_major(i), "sd") == 0)
2055 majors++;
2056
2057 if (!majors)
2058 return -ENODEV;
2059
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002060 err = class_register(&sd_disk_class);
2061 if (err)
2062 goto err_out;
James Bottomley6bdaa1f2006-03-18 14:14:21 -06002063
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002064 err = scsi_register_driver(&sd_template.gendrv);
2065 if (err)
2066 goto err_out_class;
2067
2068 return 0;
2069
2070err_out_class:
2071 class_unregister(&sd_disk_class);
2072err_out:
2073 for (i = 0; i < SD_MAJORS; i++)
2074 unregister_blkdev(sd_major(i), "sd");
2075 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
2078/**
2079 * exit_sd - exit point for this driver (when it is a module).
2080 *
2081 * Note: this function unregisters this driver from the scsi mid-level.
2082 **/
2083static void __exit exit_sd(void)
2084{
2085 int i;
2086
2087 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
2088
2089 scsi_unregister_driver(&sd_template.gendrv);
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002090 class_unregister(&sd_disk_class);
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 for (i = 0; i < SD_MAJORS; i++)
2093 unregister_blkdev(sd_major(i), "sd");
2094}
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096module_init(init_sd);
2097module_exit(exit_sd);
Martin K. Petersene73aec82007-02-27 22:40:55 -05002098
2099static void sd_print_sense_hdr(struct scsi_disk *sdkp,
2100 struct scsi_sense_hdr *sshdr)
2101{
2102 sd_printk(KERN_INFO, sdkp, "");
2103 scsi_show_sense_hdr(sshdr);
2104 sd_printk(KERN_INFO, sdkp, "");
2105 scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
2106}
2107
2108static void sd_print_result(struct scsi_disk *sdkp, int result)
2109{
2110 sd_printk(KERN_INFO, sdkp, "");
2111 scsi_show_result(result);
2112}
2113