blob: 56b9501d12f3043c2af7c24b090fa2ea4a9d75bc [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;
376 sector_t block = rq->sector;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500377 unsigned int this_count = rq->nr_sectors;
Christoph Hellwig776b23a2006-01-06 18:34:07 +0100378 unsigned int timeout = sdp->timeout;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500379 int ret;
380
381 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
382 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
383 goto out;
384 } else if (rq->cmd_type != REQ_TYPE_FS) {
385 ret = BLKPREP_KILL;
386 goto out;
387 }
388 ret = scsi_setup_fs_cmnd(sdp, rq);
389 if (ret != BLKPREP_OK)
390 goto out;
391 SCpnt = rq->special;
392
393 /* from here on until we're complete, any goto out
394 * is used for a killable error condition */
395 ret = BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500397 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
398 "sd_init_command: block=%llu, "
399 "count=%d\n",
400 (unsigned long long)block,
401 this_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (!sdp || !scsi_device_online(sdp) ||
404 block + rq->nr_sectors > get_capacity(disk)) {
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500405 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
406 "Finishing %ld sectors\n",
407 rq->nr_sectors));
408 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
409 "Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500410 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
413 if (sdp->changed) {
414 /*
415 * quietly refuse to do anything to a changed disc until
416 * the changed bit has been reset
417 */
418 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500419 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500421
Hans de Goedea0899d42008-01-20 11:12:26 +0100422 /*
423 * Some devices (some sdcards for one) don't like it if the
424 * last sector gets read in a larger then 1 sector read.
425 */
426 if (unlikely(sdp->last_sector_bug &&
427 rq->nr_sectors > sdp->sector_size / 512 &&
428 block + this_count == get_capacity(disk)))
429 this_count -= sdp->sector_size / 512;
430
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500431 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
432 (unsigned long long)block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /*
435 * If we have a 1K hardware sectorsize, prevent access to single
436 * 512 byte sectors. In theory we could handle this - in fact
437 * the scsi cdrom driver must be able to handle this because
438 * we typically use 1K blocksizes, and cdroms typically have
439 * 2K hardware sectorsizes. Of course, things are simpler
440 * with the cdrom, since it is read-only. For performance
441 * reasons, the filesystems should be able to handle this
442 * and not force the scsi disk driver to use bounce buffers
443 * for this.
444 */
445 if (sdp->sector_size == 1024) {
446 if ((block & 1) || (rq->nr_sectors & 1)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500447 scmd_printk(KERN_ERR, SCpnt,
448 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 } else {
451 block = block >> 1;
452 this_count = this_count >> 1;
453 }
454 }
455 if (sdp->sector_size == 2048) {
456 if ((block & 3) || (rq->nr_sectors & 3)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500457 scmd_printk(KERN_ERR, SCpnt,
458 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500459 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 } else {
461 block = block >> 2;
462 this_count = this_count >> 2;
463 }
464 }
465 if (sdp->sector_size == 4096) {
466 if ((block & 7) || (rq->nr_sectors & 7)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500467 scmd_printk(KERN_ERR, SCpnt,
468 "Bad block number requested\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500469 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 } else {
471 block = block >> 3;
472 this_count = this_count >> 3;
473 }
474 }
475 if (rq_data_dir(rq) == WRITE) {
476 if (!sdp->writeable) {
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500477 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 SCpnt->cmnd[0] = WRITE_6;
480 SCpnt->sc_data_direction = DMA_TO_DEVICE;
481 } else if (rq_data_dir(rq) == READ) {
482 SCpnt->cmnd[0] = READ_6;
483 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
484 } else {
Martin K. Petersene73aec82007-02-27 22:40:55 -0500485 scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500486 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500489 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
490 "%s %d/%ld 512 byte blocks.\n",
491 (rq_data_dir(rq) == WRITE) ?
492 "writing" : "reading", this_count,
493 rq->nr_sectors));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 SCpnt->cmnd[1] = 0;
496
497 if (block > 0xffffffff) {
498 SCpnt->cmnd[0] += READ_16 - READ_6;
Tejun Heo007365a2006-01-06 09:53:52 +0100499 SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
501 SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
502 SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
503 SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
504 SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
505 SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
506 SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
507 SCpnt->cmnd[9] = (unsigned char) block & 0xff;
508 SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
509 SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
510 SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
511 SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
512 SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
513 } else if ((this_count > 0xff) || (block > 0x1fffff) ||
514 SCpnt->device->use_10_for_rw) {
515 if (this_count > 0xffff)
516 this_count = 0xffff;
517
518 SCpnt->cmnd[0] += READ_10 - 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] = (unsigned char) (block >> 24) & 0xff;
521 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
522 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
523 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
524 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
525 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
526 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
527 } else {
Tejun Heo007365a2006-01-06 09:53:52 +0100528 if (unlikely(blk_fua_rq(rq))) {
529 /*
530 * This happens only if this drive failed
531 * 10byte rw command with ILLEGAL_REQUEST
532 * during operation and thus turned off
533 * use_10_for_rw.
534 */
Martin K. Petersene73aec82007-02-27 22:40:55 -0500535 scmd_printk(KERN_ERR, SCpnt,
536 "FUA write on READ/WRITE(6) drive\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500537 goto out;
Tejun Heo007365a2006-01-06 09:53:52 +0100538 }
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
541 SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
542 SCpnt->cmnd[3] = (unsigned char) block & 0xff;
543 SCpnt->cmnd[4] = (unsigned char) this_count;
544 SCpnt->cmnd[5] = 0;
545 }
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200546 SCpnt->sdb.length = this_count * sdp->sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /*
549 * We shouldn't disconnect in the middle of a sector, so with a dumb
550 * host adapter, it's safe to assume that we can at least transfer
551 * this many bytes between each connect / disconnect.
552 */
553 SCpnt->transfersize = sdp->sector_size;
554 SCpnt->underflow = this_count << 9;
555 SCpnt->allowed = SD_MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 SCpnt->timeout_per_command = timeout;
557
558 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 * This indicates that the command is ready from our end to be
560 * queued.
561 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500562 ret = BLKPREP_OK;
563 out:
564 return scsi_prep_return(q, rq, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567/**
568 * sd_open - open a scsi disk device
569 * @inode: only i_rdev member may be used
570 * @filp: only f_mode and f_flags may be used
571 *
572 * Returns 0 if successful. Returns a negated errno value in case
573 * of error.
574 *
575 * Note: This can be called from a user context (e.g. fsck(1) )
576 * or from within the kernel (e.g. as a result of a mount(1) ).
577 * In the latter case @inode and @filp carry an abridged amount
578 * of information as noted above.
579 **/
580static int sd_open(struct inode *inode, struct file *filp)
581{
582 struct gendisk *disk = inode->i_bdev->bd_disk;
583 struct scsi_disk *sdkp;
584 struct scsi_device *sdev;
585 int retval;
586
587 if (!(sdkp = scsi_disk_get(disk)))
588 return -ENXIO;
589
590
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500591 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 sdev = sdkp->device;
594
595 /*
596 * If the device is in error recovery, wait until it is done.
597 * If the device is offline, then disallow any access to it.
598 */
599 retval = -ENXIO;
600 if (!scsi_block_when_processing_errors(sdev))
601 goto error_out;
602
603 if (sdev->removable || sdkp->write_prot)
604 check_disk_change(inode->i_bdev);
605
606 /*
607 * If the drive is empty, just let the open fail.
608 */
609 retval = -ENOMEDIUM;
610 if (sdev->removable && !sdkp->media_present &&
611 !(filp->f_flags & O_NDELAY))
612 goto error_out;
613
614 /*
615 * If the device has the write protect tab set, have the open fail
616 * if the user expects to be able to write to the thing.
617 */
618 retval = -EROFS;
619 if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
620 goto error_out;
621
622 /*
623 * It is possible that the disk changing stuff resulted in
624 * the device being taken offline. If this is the case,
625 * report this to the user, and don't pretend that the
626 * open actually succeeded.
627 */
628 retval = -ENXIO;
629 if (!scsi_device_online(sdev))
630 goto error_out;
631
632 if (!sdkp->openers++ && sdev->removable) {
633 if (scsi_block_when_processing_errors(sdev))
634 scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
635 }
636
637 return 0;
638
639error_out:
640 scsi_disk_put(sdkp);
641 return retval;
642}
643
644/**
645 * sd_release - invoked when the (last) close(2) is called on this
646 * scsi disk.
647 * @inode: only i_rdev member may be used
648 * @filp: only f_mode and f_flags may be used
649 *
650 * Returns 0.
651 *
652 * Note: may block (uninterruptible) if error recovery is underway
653 * on this disk.
654 **/
655static int sd_release(struct inode *inode, struct file *filp)
656{
657 struct gendisk *disk = inode->i_bdev->bd_disk;
658 struct scsi_disk *sdkp = scsi_disk(disk);
659 struct scsi_device *sdev = sdkp->device;
660
James Bottomley56937f72007-03-11 12:25:33 -0500661 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (!--sdkp->openers && sdev->removable) {
664 if (scsi_block_when_processing_errors(sdev))
665 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
666 }
667
668 /*
669 * XXX and what if there are packets in flight and this close()
670 * XXX is followed by a "rmmod sd_mod"?
671 */
672 scsi_disk_put(sdkp);
673 return 0;
674}
675
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800676static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
679 struct scsi_device *sdp = sdkp->device;
680 struct Scsi_Host *host = sdp->host;
681 int diskinfo[4];
682
683 /* default to most commonly used values */
684 diskinfo[0] = 0x40; /* 1 << 6 */
685 diskinfo[1] = 0x20; /* 1 << 5 */
686 diskinfo[2] = sdkp->capacity >> 11;
687
688 /* override with calculated, extended default, or driver values */
689 if (host->hostt->bios_param)
690 host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
691 else
692 scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
693
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800694 geo->heads = diskinfo[0];
695 geo->sectors = diskinfo[1];
696 geo->cylinders = diskinfo[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698}
699
700/**
701 * sd_ioctl - process an ioctl
702 * @inode: only i_rdev/i_bdev members may be used
703 * @filp: only f_mode and f_flags may be used
704 * @cmd: ioctl command number
705 * @arg: this is third argument given to ioctl(2) system call.
706 * Often contains a pointer.
707 *
708 * Returns 0 if successful (some ioctls return postive numbers on
709 * success as well). Returns a negated errno value in case of error.
710 *
711 * Note: most ioctls are forward onto the block subsystem or further
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200712 * down in the scsi subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 **/
714static int sd_ioctl(struct inode * inode, struct file * filp,
715 unsigned int cmd, unsigned long arg)
716{
717 struct block_device *bdev = inode->i_bdev;
718 struct gendisk *disk = bdev->bd_disk;
719 struct scsi_device *sdp = scsi_disk(disk)->device;
720 void __user *p = (void __user *)arg;
721 int error;
722
723 SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
724 disk->disk_name, cmd));
725
726 /*
727 * If we are in the middle of error recovery, don't let anyone
728 * else try and use this device. Also, if error recovery fails, it
729 * may try and take the device offline, in which case all further
730 * access to the device is prohibited.
731 */
732 error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
733 if (!scsi_block_when_processing_errors(sdp) || !error)
734 return error;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /*
737 * Send SCSI addressing ioctls directly to mid level, send other
738 * ioctls to block level and then onto mid level if they can't be
739 * resolved.
740 */
741 switch (cmd) {
742 case SCSI_IOCTL_GET_IDLUN:
743 case SCSI_IOCTL_GET_BUS_NUMBER:
744 return scsi_ioctl(sdp, cmd, p);
745 default:
FUJITA Tomonori45e79a32007-07-09 12:39:20 +0200746 error = scsi_cmd_ioctl(filp, disk->queue, disk, cmd, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (error != -ENOTTY)
748 return error;
749 }
750 return scsi_ioctl(sdp, cmd, p);
751}
752
753static void set_media_not_present(struct scsi_disk *sdkp)
754{
755 sdkp->media_present = 0;
756 sdkp->capacity = 0;
757 sdkp->device->changed = 1;
758}
759
760/**
761 * sd_media_changed - check if our medium changed
762 * @disk: kernel device descriptor
763 *
764 * Returns 0 if not applicable or no change; 1 if change
765 *
766 * Note: this function is invoked from the block subsystem.
767 **/
768static int sd_media_changed(struct gendisk *disk)
769{
770 struct scsi_disk *sdkp = scsi_disk(disk);
771 struct scsi_device *sdp = sdkp->device;
James Bottomley001aac22007-12-02 19:10:40 +0200772 struct scsi_sense_hdr *sshdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int retval;
774
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500775 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 if (!sdp->removable)
778 return 0;
779
780 /*
781 * If the device is offline, don't send any commands - just pretend as
782 * if the command failed. If the device ever comes back online, we
783 * can deal with it then. It is only because of unrecoverable errors
784 * that we would ever take a device offline in the first place.
785 */
Kay Sievers285e9672007-08-14 14:10:39 +0200786 if (!scsi_device_online(sdp)) {
787 set_media_not_present(sdkp);
788 retval = 1;
789 goto out;
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /*
793 * Using TEST_UNIT_READY enables differentiation between drive with
794 * no cartridge loaded - NOT READY, drive with changed cartridge -
795 * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
796 *
797 * Drives that auto spin down. eg iomega jaz 1G, will be started
798 * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
799 * sd_revalidate() is called.
800 */
801 retval = -ENODEV;
Kay Sievers285e9672007-08-14 14:10:39 +0200802
James Bottomley001aac22007-12-02 19:10:40 +0200803 if (scsi_block_when_processing_errors(sdp)) {
804 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
805 retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
806 sshdr);
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /*
810 * Unable to test, unit probably not ready. This usually
811 * means there is no disc in the drive. Mark as changed,
812 * and we will figure it out later once the drive is
813 * available again.
814 */
James Bottomley001aac22007-12-02 19:10:40 +0200815 if (retval || (scsi_sense_valid(sshdr) &&
816 /* 0x3a is medium not present */
817 sshdr->asc == 0x3a)) {
Kay Sievers285e9672007-08-14 14:10:39 +0200818 set_media_not_present(sdkp);
819 retval = 1;
820 goto out;
821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /*
824 * For removable scsi disk we have to recognise the presence
825 * of a disk in the drive. This is kept in the struct scsi_disk
826 * struct and tested at open ! Daniel Roche (dan@lectra.fr)
827 */
828 sdkp->media_present = 1;
829
830 retval = sdp->changed;
831 sdp->changed = 0;
Kay Sievers285e9672007-08-14 14:10:39 +0200832out:
833 if (retval != sdkp->previous_state)
834 sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
835 sdkp->previous_state = retval;
James Bottomley001aac22007-12-02 19:10:40 +0200836 kfree(sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Martin K. Petersene73aec82007-02-27 22:40:55 -0500840static int sd_sync_cache(struct scsi_disk *sdkp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 int retries, res;
Martin K. Petersene73aec82007-02-27 22:40:55 -0500843 struct scsi_device *sdp = sdkp->device;
James Bottomleyea73a9f2005-08-28 11:33:52 -0500844 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 if (!scsi_device_online(sdp))
847 return -ENODEV;
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 for (retries = 3; retries > 0; --retries) {
851 unsigned char cmd[10] = { 0 };
852
853 cmd[0] = SYNCHRONIZE_CACHE;
854 /*
855 * Leave the rest of the command zero to indicate
856 * flush everything.
857 */
James Bottomleyea73a9f2005-08-28 11:33:52 -0500858 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
859 SD_TIMEOUT, SD_MAX_RETRIES);
860 if (res == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862 }
863
Martin K. Petersene73aec82007-02-27 22:40:55 -0500864 if (res) {
865 sd_print_result(sdkp, res);
866 if (driver_byte(res) & DRIVER_SENSE)
867 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
Tejun Heo37210502007-03-21 00:07:18 +0900870 if (res)
871 return -EIO;
872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Jens Axboe165125e2007-07-24 09:28:11 +0200875static void sd_prepare_flush(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200877 rq->cmd_type = REQ_TYPE_BLOCK_PC;
James Bottomleyc0ed79a2005-11-08 09:21:07 -0500878 rq->timeout = SD_TIMEOUT;
879 rq->cmd[0] = SYNCHRONIZE_CACHE;
Tejun Heo461d4e92006-01-06 09:52:55 +0100880 rq->cmd_len = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883static void sd_rescan(struct device *dev)
884{
Alan Stern39b7f1e2005-11-04 14:44:41 -0500885 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
886
887 if (sdkp) {
888 sd_revalidate_disk(sdkp->disk);
889 scsi_disk_put(sdkp);
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893
894#ifdef CONFIG_COMPAT
895/*
896 * This gets directly called from VFS. When the ioctl
897 * is not recognized we go back to the other translation paths.
898 */
899static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
900{
Josef Sipek7ac62072006-12-08 02:37:37 -0800901 struct block_device *bdev = file->f_path.dentry->d_inode->i_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 struct gendisk *disk = bdev->bd_disk;
903 struct scsi_device *sdev = scsi_disk(disk)->device;
904
905 /*
906 * If we are in the middle of error recovery, don't let anyone
907 * else try and use this device. Also, if error recovery fails, it
908 * may try and take the device offline, in which case all further
909 * access to the device is prohibited.
910 */
911 if (!scsi_block_when_processing_errors(sdev))
912 return -ENODEV;
913
914 if (sdev->host->hostt->compat_ioctl) {
915 int ret;
916
917 ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
918
919 return ret;
920 }
921
922 /*
923 * Let the static ioctl translation table take care of it.
924 */
925 return -ENOIOCTLCMD;
926}
927#endif
928
929static struct block_device_operations sd_fops = {
930 .owner = THIS_MODULE,
931 .open = sd_open,
932 .release = sd_release,
933 .ioctl = sd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800934 .getgeo = sd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935#ifdef CONFIG_COMPAT
936 .compat_ioctl = sd_compat_ioctl,
937#endif
938 .media_changed = sd_media_changed,
939 .revalidate_disk = sd_revalidate_disk,
940};
941
942/**
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800943 * sd_done - bottom half handler: called when the lower level
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * driver has completed (successfully or otherwise) a scsi command.
945 * @SCpnt: mid-level's per command structure.
946 *
947 * Note: potentially run from within an ISR. Must not block.
948 **/
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800949static int sd_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 int result = SCpnt->result;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200952 unsigned int xfer_size = scsi_bufflen(SCpnt);
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700953 unsigned int good_bytes = result ? 0 : xfer_size;
954 u64 start_lba = SCpnt->request->sector;
James Bottomley366c2462008-02-02 16:06:23 -0600955 u64 end_lba = SCpnt->request->sector + (xfer_size / 512);
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700956 u64 bad_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 struct scsi_sense_hdr sshdr;
958 int sense_valid = 0;
959 int sense_deferred = 0;
960 int info_valid;
961
962 if (result) {
963 sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
964 if (sense_valid)
965 sense_deferred = scsi_sense_is_deferred(&sshdr);
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967#ifdef CONFIG_SCSI_LOGGING
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500968 SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (sense_valid) {
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500970 SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800971 "sd_done: sb[respc,sk,asc,"
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -0500972 "ascq]=%x,%x,%x,%x\n",
973 sshdr.response_code,
974 sshdr.sense_key, sshdr.asc,
975 sshdr.ascq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977#endif
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700978 if (driver_byte(result) != DRIVER_SENSE &&
979 (!sense_valid || sense_deferred))
980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700982 switch (sshdr.sense_key) {
983 case HARDWARE_ERROR:
984 case MEDIUM_ERROR:
985 if (!blk_fs_request(SCpnt->request))
986 goto out;
987 info_valid = scsi_get_sense_info_fld(SCpnt->sense_buffer,
988 SCSI_SENSE_BUFFERSIZE,
989 &bad_lba);
990 if (!info_valid)
991 goto out;
992 if (xfer_size <= SCpnt->device->sector_size)
993 goto out;
James Bottomley366c2462008-02-02 16:06:23 -0600994 if (SCpnt->device->sector_size < 512) {
995 /* only legitimate sector_size here is 256 */
Luben Tuikov03aba2f2006-06-23 09:39:09 -0700996 start_lba <<= 1;
James Bottomley366c2462008-02-02 16:06:23 -0600997 end_lba <<= 1;
998 } else {
999 /* be careful ... don't want any overflows */
1000 u64 factor = SCpnt->device->sector_size / 512;
1001 do_div(start_lba, factor);
1002 do_div(end_lba, factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
James Bottomley366c2462008-02-02 16:06:23 -06001004
1005 if (bad_lba < start_lba || bad_lba >= end_lba)
1006 /* the bad lba was reported incorrectly, we have
1007 * no idea where the error is
1008 */
1009 goto out;
1010
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001011 /* This computation should always be done in terms of
1012 * the resolution of the device's medium.
1013 */
1014 good_bytes = (bad_lba - start_lba)*SCpnt->device->sector_size;
1015 break;
1016 case RECOVERED_ERROR:
1017 case NO_SENSE:
1018 /* Inform the user, but make sure that it's not treated
1019 * as a hard error.
1020 */
1021 scsi_print_sense("sd", SCpnt);
1022 SCpnt->result = 0;
1023 memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1024 good_bytes = xfer_size;
1025 break;
1026 case ILLEGAL_REQUEST:
1027 if (SCpnt->device->use_10_for_rw &&
1028 (SCpnt->cmnd[0] == READ_10 ||
1029 SCpnt->cmnd[0] == WRITE_10))
1030 SCpnt->device->use_10_for_rw = 0;
1031 if (SCpnt->device->use_10_for_ms &&
1032 (SCpnt->cmnd[0] == MODE_SENSE_10 ||
1033 SCpnt->cmnd[0] == MODE_SELECT_10))
1034 SCpnt->device->use_10_for_ms = 0;
1035 break;
1036 default:
1037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
Luben Tuikov03aba2f2006-06-23 09:39:09 -07001039 out:
Linus Torvalds7b3d9542008-01-06 10:17:12 -08001040 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
James Bottomleyea73a9f2005-08-28 11:33:52 -05001043static int media_not_present(struct scsi_disk *sdkp,
1044 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
James Bottomleyea73a9f2005-08-28 11:33:52 -05001047 if (!scsi_sense_valid(sshdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return 0;
1049 /* not invoked for commands that could return deferred errors */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001050 if (sshdr->sense_key != NOT_READY &&
1051 sshdr->sense_key != UNIT_ATTENTION)
1052 return 0;
1053 if (sshdr->asc != 0x3A) /* medium not present */
1054 return 0;
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 set_media_not_present(sdkp);
1057 return 1;
1058}
1059
1060/*
1061 * spinup disk - called only in sd_revalidate_disk()
1062 */
1063static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001064sd_spinup_disk(struct scsi_disk *sdkp)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001065{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 unsigned char cmd[10];
Alan Stern4451e472005-07-12 10:45:17 -04001067 unsigned long spintime_expire = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 int retries, spintime;
1069 unsigned int the_result;
1070 struct scsi_sense_hdr sshdr;
1071 int sense_valid = 0;
1072
1073 spintime = 0;
1074
1075 /* Spin up drives, as required. Only do this at boot time */
1076 /* Spinup needs to be done for module loads too. */
1077 do {
1078 retries = 0;
1079
1080 do {
1081 cmd[0] = TEST_UNIT_READY;
1082 memset((void *) &cmd[1], 0, 9);
1083
James Bottomleyea73a9f2005-08-28 11:33:52 -05001084 the_result = scsi_execute_req(sdkp->device, cmd,
1085 DMA_NONE, NULL, 0,
1086 &sshdr, SD_TIMEOUT,
1087 SD_MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Alan Sternb4d38e32006-10-11 16:48:28 -04001089 /*
1090 * If the drive has indicated to us that it
1091 * doesn't have any media in it, don't bother
1092 * with any more polling.
1093 */
1094 if (media_not_present(sdkp, &sshdr))
1095 return;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (the_result)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001098 sense_valid = scsi_sense_valid(&sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 retries++;
1100 } while (retries < 3 &&
1101 (!scsi_status_is_good(the_result) ||
1102 ((driver_byte(the_result) & DRIVER_SENSE) &&
1103 sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1106 /* no sense, TUR either succeeded or failed
1107 * with a status error */
Martin K. Petersene73aec82007-02-27 22:40:55 -05001108 if(!spintime && !scsi_status_is_good(the_result)) {
1109 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1110 sd_print_result(sdkp, the_result);
1111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 break;
1113 }
1114
1115 /*
1116 * The device does not want the automatic start to be issued.
1117 */
1118 if (sdkp->device->no_start_on_add) {
1119 break;
1120 }
1121
1122 /*
1123 * If manual intervention is required, or this is an
1124 * absent USB storage device, a spinup is meaningless.
1125 */
1126 if (sense_valid &&
1127 sshdr.sense_key == NOT_READY &&
1128 sshdr.asc == 4 && sshdr.ascq == 3) {
1129 break; /* manual intervention required */
1130
1131 /*
1132 * Issue command to spin up drive when not ready
1133 */
1134 } else if (sense_valid && sshdr.sense_key == NOT_READY) {
1135 if (!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001136 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 cmd[0] = START_STOP;
1138 cmd[1] = 1; /* Return immediately */
1139 memset((void *) &cmd[2], 0, 8);
1140 cmd[4] = 1; /* Start spin cycle */
Stefan Richterd2886ea2008-05-11 00:34:07 +02001141 if (sdkp->device->start_stop_pwr_cond)
1142 cmd[4] |= 1 << 4;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001143 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1144 NULL, 0, &sshdr,
1145 SD_TIMEOUT, SD_MAX_RETRIES);
Alan Stern4451e472005-07-12 10:45:17 -04001146 spintime_expire = jiffies + 100 * HZ;
1147 spintime = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /* Wait 1 second for next try */
1150 msleep(1000);
1151 printk(".");
Alan Stern4451e472005-07-12 10:45:17 -04001152
1153 /*
1154 * Wait for USB flash devices with slow firmware.
1155 * Yes, this sense key/ASC combination shouldn't
1156 * occur here. It's characteristic of these devices.
1157 */
1158 } else if (sense_valid &&
1159 sshdr.sense_key == UNIT_ATTENTION &&
1160 sshdr.asc == 0x28) {
1161 if (!spintime) {
1162 spintime_expire = jiffies + 5 * HZ;
1163 spintime = 1;
1164 }
1165 /* Wait 1 second for next try */
1166 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 } else {
1168 /* we don't understand the sense code, so it's
1169 * probably pointless to loop */
1170 if(!spintime) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001171 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1172 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174 break;
1175 }
1176
Alan Stern4451e472005-07-12 10:45:17 -04001177 } while (spintime && time_before_eq(jiffies, spintime_expire));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (spintime) {
1180 if (scsi_status_is_good(the_result))
1181 printk("ready\n");
1182 else
1183 printk("not responding...\n");
1184 }
1185}
1186
Martin K. Petersene0597d72008-07-17 04:28:34 -04001187
1188/*
1189 * Determine whether disk supports Data Integrity Field.
1190 */
1191void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
1192{
1193 struct scsi_device *sdp = sdkp->device;
1194 u8 type;
1195
1196 if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
1197 type = 0;
1198 else
1199 type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
1200
1201 switch (type) {
1202 case SD_DIF_TYPE0_PROTECTION:
1203 sdkp->protection_type = 0;
1204 break;
1205
1206 case SD_DIF_TYPE1_PROTECTION:
1207 case SD_DIF_TYPE3_PROTECTION:
1208 sdkp->protection_type = type;
1209 break;
1210
1211 case SD_DIF_TYPE2_PROTECTION:
1212 sd_printk(KERN_ERR, sdkp, "formatted with DIF Type 2 " \
1213 "protection which is currently unsupported. " \
1214 "Disabling disk!\n");
1215 goto disable;
1216
1217 default:
1218 sd_printk(KERN_ERR, sdkp, "formatted with unknown " \
1219 "protection type %d. Disabling disk!\n", type);
1220 goto disable;
1221 }
1222
1223 return;
1224
1225disable:
1226 sdkp->protection_type = 0;
1227 sdkp->capacity = 0;
1228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230/*
1231 * read disk capacity
1232 */
1233static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001234sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001235{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 unsigned char cmd[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int the_result, retries;
1238 int sector_size = 0;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001239 /* Force READ CAPACITY(16) when PROTECT=1 */
1240 int longrc = scsi_device_protection(sdkp->device) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 struct scsi_sense_hdr sshdr;
1242 int sense_valid = 0;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001243 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245repeat:
1246 retries = 3;
1247 do {
1248 if (longrc) {
1249 memset((void *) cmd, 0, 16);
1250 cmd[0] = SERVICE_ACTION_IN;
1251 cmd[1] = SAI_READ_CAPACITY_16;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001252 cmd[13] = 13;
1253 memset((void *) buffer, 0, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 } else {
1255 cmd[0] = READ_CAPACITY;
1256 memset((void *) &cmd[1], 0, 9);
1257 memset((void *) buffer, 0, 8);
1258 }
1259
James Bottomleyea73a9f2005-08-28 11:33:52 -05001260 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
Martin K. Petersene0597d72008-07-17 04:28:34 -04001261 buffer, longrc ? 13 : 8, &sshdr,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001262 SD_TIMEOUT, SD_MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
James Bottomleyea73a9f2005-08-28 11:33:52 -05001264 if (media_not_present(sdkp, &sshdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (the_result)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001268 sense_valid = scsi_sense_valid(&sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 retries--;
1270
1271 } while (the_result && retries);
1272
1273 if (the_result && !longrc) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001274 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1275 sd_print_result(sdkp, the_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (driver_byte(the_result) & DRIVER_SENSE)
Martin K. Petersene73aec82007-02-27 22:40:55 -05001277 sd_print_sense_hdr(sdkp, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 else
Martin K. Petersene73aec82007-02-27 22:40:55 -05001279 sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 /* Set dirty bit for removable devices if not ready -
1282 * sometimes drives will not report this properly. */
1283 if (sdp->removable &&
1284 sense_valid && sshdr.sense_key == NOT_READY)
1285 sdp->changed = 1;
1286
1287 /* Either no media are present but the drive didn't tell us,
1288 or they are present but the read capacity command fails */
1289 /* sdkp->media_present = 0; -- not always correct */
Hannes Reinecke69bdd882006-09-01 15:50:23 +02001290 sdkp->capacity = 0; /* unknown mapped to zero - as usual */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 return;
1293 } else if (the_result && longrc) {
1294 /* READ CAPACITY(16) has been failed */
Martin K. Petersene73aec82007-02-27 22:40:55 -05001295 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
1296 sd_print_result(sdkp, the_result);
1297 sd_printk(KERN_NOTICE, sdkp, "Use 0xffffffff as device size\n");
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 sdkp->capacity = 1 + (sector_t) 0xffffffff;
1300 goto got_data;
1301 }
1302
1303 if (!longrc) {
1304 sector_size = (buffer[4] << 24) |
1305 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1306 if (buffer[0] == 0xff && buffer[1] == 0xff &&
1307 buffer[2] == 0xff && buffer[3] == 0xff) {
1308 if(sizeof(sdkp->capacity) > 4) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001309 sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1310 "Trying to use READ CAPACITY(16).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 longrc = 1;
1312 goto repeat;
1313 }
Martin K. Petersene73aec82007-02-27 22:40:55 -05001314 sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use "
1315 "a kernel compiled with support for large "
1316 "block devices.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 sdkp->capacity = 0;
1318 goto got_data;
1319 }
1320 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
1321 (buffer[1] << 16) |
1322 (buffer[2] << 8) |
1323 buffer[3]);
1324 } else {
1325 sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
1326 ((u64)buffer[1] << 48) |
1327 ((u64)buffer[2] << 40) |
1328 ((u64)buffer[3] << 32) |
1329 ((sector_t)buffer[4] << 24) |
1330 ((sector_t)buffer[5] << 16) |
1331 ((sector_t)buffer[6] << 8) |
1332 (sector_t)buffer[7]);
1333
1334 sector_size = (buffer[8] << 24) |
1335 (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
Martin K. Petersene0597d72008-07-17 04:28:34 -04001336
1337 sd_read_protection_type(sdkp, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339
1340 /* Some devices return the total number of sectors, not the
1341 * highest sector number. Make the necessary adjustment. */
Oliver Neukum61bf54b2007-02-08 09:04:48 +01001342 if (sdp->fix_capacity) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 --sdkp->capacity;
1344
Oliver Neukum61bf54b2007-02-08 09:04:48 +01001345 /* Some devices have version which report the correct sizes
1346 * and others which do not. We guess size according to a heuristic
1347 * and err on the side of lowering the capacity. */
1348 } else {
1349 if (sdp->guess_capacity)
1350 if (sdkp->capacity & 0x01) /* odd sizes are odd */
1351 --sdkp->capacity;
1352 }
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354got_data:
1355 if (sector_size == 0) {
1356 sector_size = 512;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001357 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
1358 "assuming 512.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360
1361 if (sector_size != 512 &&
1362 sector_size != 1024 &&
1363 sector_size != 2048 &&
1364 sector_size != 4096 &&
1365 sector_size != 256) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001366 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
1367 sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 /*
1369 * The user might want to re-format the drive with
1370 * a supported sectorsize. Once this happens, it
1371 * would be relatively trivial to set the thing up.
1372 * For this reason, we leave the thing in the table.
1373 */
1374 sdkp->capacity = 0;
1375 /*
1376 * set a bogus sector size so the normal read/write
1377 * logic in the block layer will eventually refuse any
1378 * request on this device without tripping over power
1379 * of two sector size assumptions
1380 */
1381 sector_size = 512;
1382 }
1383 {
1384 /*
1385 * The msdos fs needs to know the hardware sector size
1386 * So I have created this table. See ll_rw_blk.c
1387 * Jacques Gelinas (Jacques@solucorp.qc.ca)
1388 */
1389 int hard_sector = sector_size;
James Bottomley7a691bd2005-10-28 13:17:30 -05001390 sector_t sz = (sdkp->capacity/2) * (hard_sector/256);
Jens Axboe165125e2007-07-24 09:28:11 +02001391 struct request_queue *queue = sdp->request_queue;
James Bottomley7a691bd2005-10-28 13:17:30 -05001392 sector_t mb = sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 blk_queue_hardsect_size(queue, hard_sector);
1395 /* avoid 64-bit division on 32-bit platforms */
James Bottomley7a691bd2005-10-28 13:17:30 -05001396 sector_div(sz, 625);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 mb -= sz - 974;
1398 sector_div(mb, 1950);
1399
Martin K. Petersene73aec82007-02-27 22:40:55 -05001400 sd_printk(KERN_NOTICE, sdkp,
1401 "%llu %d-byte hardware sectors (%llu MB)\n",
1402 (unsigned long long)sdkp->capacity,
1403 hard_sector, (unsigned long long)mb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405
1406 /* Rescale capacity to 512-byte units */
1407 if (sector_size == 4096)
1408 sdkp->capacity <<= 3;
1409 else if (sector_size == 2048)
1410 sdkp->capacity <<= 2;
1411 else if (sector_size == 1024)
1412 sdkp->capacity <<= 1;
1413 else if (sector_size == 256)
1414 sdkp->capacity >>= 1;
1415
1416 sdkp->device->sector_size = sector_size;
1417}
1418
1419/* called with buffer of length 512 */
1420static inline int
James Bottomleyea73a9f2005-08-28 11:33:52 -05001421sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1422 unsigned char *buffer, int len, struct scsi_mode_data *data,
1423 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
James Bottomleyea73a9f2005-08-28 11:33:52 -05001425 return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
James Bottomley1cf72692005-08-28 11:27:01 -05001426 SD_TIMEOUT, SD_MAX_RETRIES, data,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001427 sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
1430/*
1431 * read write protect setting, if possible - called only in sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06001432 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 */
1434static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001435sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
James Bottomleyea73a9f2005-08-28 11:33:52 -05001436{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001438 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 struct scsi_mode_data data;
1440
1441 set_disk_ro(sdkp->disk, 0);
James Bottomleyea73a9f2005-08-28 11:33:52 -05001442 if (sdp->skip_ms_page_3f) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001443 sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return;
1445 }
1446
James Bottomleyea73a9f2005-08-28 11:33:52 -05001447 if (sdp->use_192_bytes_for_3f) {
1448 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 } else {
1450 /*
1451 * First attempt: ask for all pages (0x3F), but only 4 bytes.
1452 * We have to start carefully: some devices hang if we ask
1453 * for more than is available.
1454 */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001455 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 /*
1458 * Second attempt: ask for page 0 When only page 0 is
1459 * implemented, a request for page 3F may return Sense Key
1460 * 5: Illegal Request, Sense Code 24: Invalid field in
1461 * CDB.
1462 */
1463 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05001464 res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 /*
1467 * Third attempt: ask 255 bytes, as we did earlier.
1468 */
1469 if (!scsi_status_is_good(res))
James Bottomleyea73a9f2005-08-28 11:33:52 -05001470 res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1471 &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473
1474 if (!scsi_status_is_good(res)) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001475 sd_printk(KERN_WARNING, sdkp,
1476 "Test WP failed, assume Write Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 } else {
1478 sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1479 set_disk_ro(sdkp->disk, sdkp->write_prot);
Martin K. Petersene73aec82007-02-27 22:40:55 -05001480 sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
1481 sdkp->write_prot ? "on" : "off");
1482 sd_printk(KERN_DEBUG, sdkp,
1483 "Mode Sense: %02x %02x %02x %02x\n",
1484 buffer[0], buffer[1], buffer[2], buffer[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486}
1487
1488/*
1489 * sd_read_cache_type - called only from sd_revalidate_disk()
Al Viro48970802006-02-26 08:34:10 -06001490 * called with buffer of length SD_BUF_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 */
1492static void
Martin K. Petersene73aec82007-02-27 22:40:55 -05001493sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
Al Viro 631e8a12005-05-16 01:59:55 +01001494{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 int len = 0, res;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001496 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Al Viro 631e8a12005-05-16 01:59:55 +01001498 int dbd;
1499 int modepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 struct scsi_mode_data data;
1501 struct scsi_sense_hdr sshdr;
1502
James Bottomleyea73a9f2005-08-28 11:33:52 -05001503 if (sdp->skip_ms_page_8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 goto defaults;
1505
James Bottomleyea73a9f2005-08-28 11:33:52 -05001506 if (sdp->type == TYPE_RBC) {
Al Viro 631e8a12005-05-16 01:59:55 +01001507 modepage = 6;
1508 dbd = 8;
1509 } else {
1510 modepage = 8;
1511 dbd = 0;
1512 }
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 /* cautiously ask */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001515 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 if (!scsi_status_is_good(res))
1518 goto bad_sense;
1519
Al Viro6d73c852006-02-23 02:03:16 +01001520 if (!data.header_length) {
1521 modepage = 6;
Martin K. Petersene73aec82007-02-27 22:40:55 -05001522 sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
Al Viro6d73c852006-02-23 02:03:16 +01001523 }
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 /* that went OK, now ask for the proper length */
1526 len = data.length;
1527
1528 /*
1529 * We're only interested in the first three bytes, actually.
1530 * But the data cache page is defined for the first 20.
1531 */
1532 if (len < 3)
1533 goto bad_sense;
1534 if (len > 20)
1535 len = 20;
1536
1537 /* Take headers and block descriptors into account */
1538 len += data.header_length + data.block_descriptor_length;
Al Viro48970802006-02-26 08:34:10 -06001539 if (len > SD_BUF_SIZE)
1540 goto bad_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 /* Get the data */
James Bottomleyea73a9f2005-08-28 11:33:52 -05001543 res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 if (scsi_status_is_good(res)) {
Al Viro 631e8a12005-05-16 01:59:55 +01001546 int offset = data.header_length + data.block_descriptor_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Al Viro48970802006-02-26 08:34:10 -06001548 if (offset >= SD_BUF_SIZE - 2) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001549 sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
Al Viro48970802006-02-26 08:34:10 -06001550 goto defaults;
1551 }
1552
Al Viro 631e8a12005-05-16 01:59:55 +01001553 if ((buffer[offset] & 0x3f) != modepage) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001554 sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
Al Viro 631e8a12005-05-16 01:59:55 +01001555 goto defaults;
1556 }
1557
1558 if (modepage == 8) {
1559 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1560 sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1561 } else {
1562 sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1563 sdkp->RCD = 0;
1564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Tejun Heo007365a2006-01-06 09:53:52 +01001566 sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1567 if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001568 sd_printk(KERN_NOTICE, sdkp,
1569 "Uses READ/WRITE(6), disabling FUA\n");
Tejun Heo007365a2006-01-06 09:53:52 +01001570 sdkp->DPOFUA = 0;
1571 }
1572
Martin K. Petersene73aec82007-02-27 22:40:55 -05001573 sd_printk(KERN_NOTICE, sdkp,
1574 "Write cache: %s, read cache: %s, %s\n",
Luben Tuikovfd44bab2006-11-15 12:47:08 -08001575 sdkp->WCE ? "enabled" : "disabled",
1576 sdkp->RCD ? "disabled" : "enabled",
1577 sdkp->DPOFUA ? "supports DPO and FUA"
1578 : "doesn't support DPO or FUA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 return;
1581 }
1582
1583bad_sense:
James Bottomleyea73a9f2005-08-28 11:33:52 -05001584 if (scsi_sense_valid(&sshdr) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 sshdr.sense_key == ILLEGAL_REQUEST &&
1586 sshdr.asc == 0x24 && sshdr.ascq == 0x0)
Martin K. Petersene73aec82007-02-27 22:40:55 -05001587 /* Invalid field in CDB */
1588 sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 else
Martin K. Petersene73aec82007-02-27 22:40:55 -05001590 sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592defaults:
Martin K. Petersene73aec82007-02-27 22:40:55 -05001593 sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 sdkp->WCE = 0;
1595 sdkp->RCD = 0;
Al Viro48970802006-02-26 08:34:10 -06001596 sdkp->DPOFUA = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
Martin K. Petersene0597d72008-07-17 04:28:34 -04001599/*
1600 * The ATO bit indicates whether the DIF application tag is available
1601 * for use by the operating system.
1602 */
1603void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
1604{
1605 int res, offset;
1606 struct scsi_device *sdp = sdkp->device;
1607 struct scsi_mode_data data;
1608 struct scsi_sense_hdr sshdr;
1609
1610 if (sdp->type != TYPE_DISK)
1611 return;
1612
1613 if (sdkp->protection_type == 0)
1614 return;
1615
1616 res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
1617 SD_MAX_RETRIES, &data, &sshdr);
1618
1619 if (!scsi_status_is_good(res) || !data.header_length ||
1620 data.length < 6) {
1621 sd_printk(KERN_WARNING, sdkp,
1622 "getting Control mode page failed, assume no ATO\n");
1623
1624 if (scsi_sense_valid(&sshdr))
1625 sd_print_sense_hdr(sdkp, &sshdr);
1626
1627 return;
1628 }
1629
1630 offset = data.header_length + data.block_descriptor_length;
1631
1632 if ((buffer[offset] & 0x3f) != 0x0a) {
1633 sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
1634 return;
1635 }
1636
1637 if ((buffer[offset + 5] & 0x80) == 0)
1638 return;
1639
1640 sdkp->ATO = 1;
1641
1642 return;
1643}
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645/**
1646 * sd_revalidate_disk - called the first time a new disk is seen,
1647 * performs disk spin up, read_capacity, etc.
1648 * @disk: struct gendisk we care about
1649 **/
1650static int sd_revalidate_disk(struct gendisk *disk)
1651{
1652 struct scsi_disk *sdkp = scsi_disk(disk);
1653 struct scsi_device *sdp = sdkp->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 unsigned char *buffer;
Tejun Heo461d4e92006-01-06 09:52:55 +01001655 unsigned ordered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Martin K. Petersenfa0d34b2007-02-27 22:41:19 -05001657 SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
1658 "sd_revalidate_disk\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 /*
1661 * If the device is offline, don't try and read capacity or any
1662 * of the other niceties.
1663 */
1664 if (!scsi_device_online(sdp))
1665 goto out;
1666
Bernhard Wallea6123f12007-05-21 17:15:26 +02001667 buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (!buffer) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001669 sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
1670 "allocation failure.\n");
James Bottomleyea73a9f2005-08-28 11:33:52 -05001671 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
1674 /* defaults, until the device tells us otherwise */
1675 sdp->sector_size = 512;
1676 sdkp->capacity = 0;
1677 sdkp->media_present = 1;
1678 sdkp->write_prot = 0;
1679 sdkp->WCE = 0;
1680 sdkp->RCD = 0;
Martin K. Petersene0597d72008-07-17 04:28:34 -04001681 sdkp->ATO = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Martin K. Petersene73aec82007-02-27 22:40:55 -05001683 sd_spinup_disk(sdkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 /*
1686 * Without media there is no reason to ask; moreover, some devices
1687 * react badly if we do.
1688 */
1689 if (sdkp->media_present) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001690 sd_read_capacity(sdkp, buffer);
1691 sd_read_write_protect_flag(sdkp, buffer);
1692 sd_read_cache_type(sdkp, buffer);
Martin K. Petersene0597d72008-07-17 04:28:34 -04001693 sd_read_app_tag_own(sdkp, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Tejun Heo461d4e92006-01-06 09:52:55 +01001695
1696 /*
1697 * We now have all cache related info, determine how we deal
1698 * with ordered requests. Note that as the current SCSI
1699 * dispatch function can alter request order, we cannot use
1700 * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
1701 */
1702 if (sdkp->WCE)
Tejun Heo007365a2006-01-06 09:53:52 +01001703 ordered = sdkp->DPOFUA
1704 ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
Tejun Heo461d4e92006-01-06 09:52:55 +01001705 else
1706 ordered = QUEUE_ORDERED_DRAIN;
1707
1708 blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 set_capacity(disk, sdkp->capacity);
1711 kfree(buffer);
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 out:
1714 return 0;
1715}
1716
1717/**
1718 * sd_probe - called during driver initialization and whenever a
1719 * new scsi device is attached to the system. It is called once
1720 * for each scsi device (not just disks) present.
1721 * @dev: pointer to device object
1722 *
1723 * Returns 0 if successful (or not interested in this scsi device
1724 * (e.g. scanner)); 1 when there is an error.
1725 *
1726 * Note: this function is invoked from the scsi mid-level.
1727 * This function sets up the mapping between a given
1728 * <host,channel,id,lun> (found in sdp) and new device name
1729 * (e.g. /dev/sda). More precisely it is the block device major
1730 * and minor number that is chosen here.
1731 *
1732 * Assume sd_attach is not re-entrant (for time being)
1733 * Also think about sd_attach() and sd_remove() running coincidentally.
1734 **/
1735static int sd_probe(struct device *dev)
1736{
1737 struct scsi_device *sdp = to_scsi_device(dev);
1738 struct scsi_disk *sdkp;
1739 struct gendisk *gd;
1740 u32 index;
1741 int error;
1742
1743 error = -ENODEV;
Al Viro 631e8a12005-05-16 01:59:55 +01001744 if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 goto out;
1746
James Bottomley9ccfc752005-10-02 11:45:08 -05001747 SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
1748 "sd_attach\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -05001751 sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (!sdkp)
1753 goto out;
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 gd = alloc_disk(16);
1756 if (!gd)
1757 goto out_free;
1758
Tejun Heof27bac22008-07-14 14:59:30 +09001759 do {
1760 if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
1761 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Tejun Heof27bac22008-07-14 14:59:30 +09001763 error = ida_get_new(&sd_index_ida, &index);
1764 } while (error == -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (error)
1767 goto out_put;
1768
Tejun Heof27bac22008-07-14 14:59:30 +09001769 error = -EBUSY;
1770 if (index >= SD_MAX_DISKS)
1771 goto out_free_index;
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 sdkp->device = sdp;
1774 sdkp->driver = &sd_template;
1775 sdkp->disk = gd;
1776 sdkp->index = index;
1777 sdkp->openers = 0;
Kay Sieversc02e6002008-03-19 13:09:56 +01001778 sdkp->previous_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 if (!sdp->timeout) {
Al Viro 631e8a12005-05-16 01:59:55 +01001781 if (sdp->type != TYPE_MOD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 sdp->timeout = SD_TIMEOUT;
1783 else
1784 sdp->timeout = SD_MOD_TIMEOUT;
1785 }
1786
Tony Jonesee959b02008-02-22 00:13:36 +01001787 device_initialize(&sdkp->dev);
1788 sdkp->dev.parent = &sdp->sdev_gendev;
1789 sdkp->dev.class = &sd_disk_class;
1790 strncpy(sdkp->dev.bus_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE);
Nagendra Singh Tomar017f2e32007-02-02 17:34:56 +05301791
Tony Jonesee959b02008-02-22 00:13:36 +01001792 if (device_add(&sdkp->dev))
Tejun Heof27bac22008-07-14 14:59:30 +09001793 goto out_free_index;
Nagendra Singh Tomar017f2e32007-02-02 17:34:56 +05301794
1795 get_device(&sdp->sdev_gendev);
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 gd->major = sd_major((index & 0xf0) >> 4);
1798 gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
1799 gd->minors = 16;
1800 gd->fops = &sd_fops;
1801
1802 if (index < 26) {
1803 sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
1804 } else if (index < (26 + 1) * 26) {
1805 sprintf(gd->disk_name, "sd%c%c",
1806 'a' + index / 26 - 1,'a' + index % 26);
1807 } else {
1808 const unsigned int m1 = (index / 26 - 1) / 26 - 1;
1809 const unsigned int m2 = (index / 26 - 1) % 26;
1810 const unsigned int m3 = index % 26;
1811 sprintf(gd->disk_name, "sd%c%c%c",
1812 'a' + m1, 'a' + m2, 'a' + m3);
1813 }
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 gd->private_data = &sdkp->driver;
Tejun Heo461d4e92006-01-06 09:52:55 +01001816 gd->queue = sdkp->device->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 sd_revalidate_disk(gd);
1819
James Bottomley7f9a6bc2007-08-04 10:06:25 -05001820 blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
James Bottomley03a57432007-08-03 16:41:11 -05001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 gd->driverfs_dev = &sdp->sdev_gendev;
1823 gd->flags = GENHD_FL_DRIVERFS;
1824 if (sdp->removable)
1825 gd->flags |= GENHD_FL_REMOVABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 dev_set_drvdata(dev, sdkp);
1828 add_disk(gd);
1829
Martin K. Petersene73aec82007-02-27 22:40:55 -05001830 sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
1831 sdp->removable ? "removable " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 return 0;
1834
Tejun Heof27bac22008-07-14 14:59:30 +09001835 out_free_index:
1836 ida_remove(&sd_index_ida, index);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06001837 out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 put_disk(gd);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06001839 out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 kfree(sdkp);
James Bottomley6bdaa1f2006-03-18 14:14:21 -06001841 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return error;
1843}
1844
1845/**
1846 * sd_remove - called whenever a scsi disk (previously recognized by
1847 * sd_probe) is detached from the system. It is called (potentially
1848 * multiple times) during sd module unload.
1849 * @sdp: pointer to mid level scsi device object
1850 *
1851 * Note: this function is invoked from the scsi mid-level.
1852 * This function potentially frees up a device name (e.g. /dev/sdc)
1853 * that could be re-used by a subsequent sd_probe().
1854 * This function is not called when the built-in sd driver is "exit-ed".
1855 **/
1856static int sd_remove(struct device *dev)
1857{
1858 struct scsi_disk *sdkp = dev_get_drvdata(dev);
1859
Tony Jonesee959b02008-02-22 00:13:36 +01001860 device_del(&sdkp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 del_gendisk(sdkp->disk);
1862 sd_shutdown(dev);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001863
Arjan van de Ven0b950672006-01-11 13:16:10 +01001864 mutex_lock(&sd_ref_mutex);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001865 dev_set_drvdata(dev, NULL);
Tony Jonesee959b02008-02-22 00:13:36 +01001866 put_device(&sdkp->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001867 mutex_unlock(&sd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 return 0;
1870}
1871
1872/**
1873 * scsi_disk_release - Called to free the scsi_disk structure
Tony Jonesee959b02008-02-22 00:13:36 +01001874 * @dev: pointer to embedded class device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 *
Arjan van de Ven0b950672006-01-11 13:16:10 +01001876 * sd_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 * called on last put, you should always use the scsi_disk_get()
1878 * scsi_disk_put() helpers which manipulate the semaphore directly
Tony Jonesee959b02008-02-22 00:13:36 +01001879 * and never do a direct put_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 **/
Tony Jonesee959b02008-02-22 00:13:36 +01001881static void scsi_disk_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Tony Jonesee959b02008-02-22 00:13:36 +01001883 struct scsi_disk *sdkp = to_scsi_disk(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 struct gendisk *disk = sdkp->disk;
1885
Tejun Heof27bac22008-07-14 14:59:30 +09001886 ida_remove(&sd_index_ida, sdkp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 disk->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 put_disk(disk);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001890 put_device(&sdkp->device->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 kfree(sdkp);
1893}
1894
James Bottomleycc5d2c82007-03-20 12:26:03 -05001895static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
Tejun Heoc3c94c52007-03-21 00:13:59 +09001896{
1897 unsigned char cmd[6] = { START_STOP }; /* START_VALID */
1898 struct scsi_sense_hdr sshdr;
James Bottomleycc5d2c82007-03-20 12:26:03 -05001899 struct scsi_device *sdp = sdkp->device;
Tejun Heoc3c94c52007-03-21 00:13:59 +09001900 int res;
1901
1902 if (start)
1903 cmd[4] |= 1; /* START */
1904
Stefan Richterd2886ea2008-05-11 00:34:07 +02001905 if (sdp->start_stop_pwr_cond)
1906 cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
1907
Tejun Heoc3c94c52007-03-21 00:13:59 +09001908 if (!scsi_device_online(sdp))
1909 return -ENODEV;
1910
1911 res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
1912 SD_TIMEOUT, SD_MAX_RETRIES);
1913 if (res) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05001914 sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
1915 sd_print_result(sdkp, res);
Tejun Heoc3c94c52007-03-21 00:13:59 +09001916 if (driver_byte(res) & DRIVER_SENSE)
James Bottomleycc5d2c82007-03-20 12:26:03 -05001917 sd_print_sense_hdr(sdkp, &sshdr);
Tejun Heoc3c94c52007-03-21 00:13:59 +09001918 }
1919
1920 return res;
1921}
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923/*
1924 * Send a SYNCHRONIZE CACHE instruction down to the device through
1925 * the normal SCSI command structure. Wait for the command to
1926 * complete.
1927 */
1928static void sd_shutdown(struct device *dev)
1929{
Alan Stern39b7f1e2005-11-04 14:44:41 -05001930 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 if (!sdkp)
1933 return; /* this can happen */
1934
Alan Stern39b7f1e2005-11-04 14:44:41 -05001935 if (sdkp->WCE) {
Martin K. Petersene73aec82007-02-27 22:40:55 -05001936 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
1937 sd_sync_cache(sdkp);
Alan Stern39b7f1e2005-11-04 14:44:41 -05001938 }
Tejun Heoc3c94c52007-03-21 00:13:59 +09001939
James Bottomleycc5d2c82007-03-20 12:26:03 -05001940 if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
1941 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
1942 sd_start_stop_device(sdkp, 0);
Tejun Heoc3c94c52007-03-21 00:13:59 +09001943 }
1944
Alan Stern39b7f1e2005-11-04 14:44:41 -05001945 scsi_disk_put(sdkp);
1946}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Tejun Heoc3c94c52007-03-21 00:13:59 +09001948static int sd_suspend(struct device *dev, pm_message_t mesg)
1949{
Tejun Heoc3c94c52007-03-21 00:13:59 +09001950 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Alan Stern09ff92f2007-05-21 09:55:04 -04001951 int ret = 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09001952
1953 if (!sdkp)
1954 return 0; /* this can happen */
1955
1956 if (sdkp->WCE) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05001957 sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
Tejun Heoc3c94c52007-03-21 00:13:59 +09001958 ret = sd_sync_cache(sdkp);
1959 if (ret)
Alan Stern09ff92f2007-05-21 09:55:04 -04001960 goto done;
Tejun Heoc3c94c52007-03-21 00:13:59 +09001961 }
1962
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01001963 if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
James Bottomleycc5d2c82007-03-20 12:26:03 -05001964 sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
1965 ret = sd_start_stop_device(sdkp, 0);
Tejun Heoc3c94c52007-03-21 00:13:59 +09001966 }
1967
Alan Stern09ff92f2007-05-21 09:55:04 -04001968done:
1969 scsi_disk_put(sdkp);
1970 return ret;
Tejun Heoc3c94c52007-03-21 00:13:59 +09001971}
1972
1973static int sd_resume(struct device *dev)
1974{
Tejun Heoc3c94c52007-03-21 00:13:59 +09001975 struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
Alan Stern09ff92f2007-05-21 09:55:04 -04001976 int ret = 0;
Tejun Heoc3c94c52007-03-21 00:13:59 +09001977
James Bottomleycc5d2c82007-03-20 12:26:03 -05001978 if (!sdkp->device->manage_start_stop)
Alan Stern09ff92f2007-05-21 09:55:04 -04001979 goto done;
Tejun Heoc3c94c52007-03-21 00:13:59 +09001980
James Bottomleycc5d2c82007-03-20 12:26:03 -05001981 sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
Alan Stern09ff92f2007-05-21 09:55:04 -04001982 ret = sd_start_stop_device(sdkp, 1);
Tejun Heoc3c94c52007-03-21 00:13:59 +09001983
Alan Stern09ff92f2007-05-21 09:55:04 -04001984done:
1985 scsi_disk_put(sdkp);
1986 return ret;
Tejun Heoc3c94c52007-03-21 00:13:59 +09001987}
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989/**
1990 * init_sd - entry point for this driver (both when built in or when
1991 * a module).
1992 *
1993 * Note: this function registers this driver with the scsi mid-level.
1994 **/
1995static int __init init_sd(void)
1996{
Jeff Garzik5e4009b2006-10-04 05:32:54 -04001997 int majors = 0, i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
2000
2001 for (i = 0; i < SD_MAJORS; i++)
2002 if (register_blkdev(sd_major(i), "sd") == 0)
2003 majors++;
2004
2005 if (!majors)
2006 return -ENODEV;
2007
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002008 err = class_register(&sd_disk_class);
2009 if (err)
2010 goto err_out;
James Bottomley6bdaa1f2006-03-18 14:14:21 -06002011
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002012 err = scsi_register_driver(&sd_template.gendrv);
2013 if (err)
2014 goto err_out_class;
2015
2016 return 0;
2017
2018err_out_class:
2019 class_unregister(&sd_disk_class);
2020err_out:
2021 for (i = 0; i < SD_MAJORS; i++)
2022 unregister_blkdev(sd_major(i), "sd");
2023 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
2026/**
2027 * exit_sd - exit point for this driver (when it is a module).
2028 *
2029 * Note: this function unregisters this driver from the scsi mid-level.
2030 **/
2031static void __exit exit_sd(void)
2032{
2033 int i;
2034
2035 SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
2036
2037 scsi_unregister_driver(&sd_template.gendrv);
Jeff Garzik5e4009b2006-10-04 05:32:54 -04002038 class_unregister(&sd_disk_class);
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 for (i = 0; i < SD_MAJORS; i++)
2041 unregister_blkdev(sd_major(i), "sd");
2042}
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044module_init(init_sd);
2045module_exit(exit_sd);
Martin K. Petersene73aec82007-02-27 22:40:55 -05002046
2047static void sd_print_sense_hdr(struct scsi_disk *sdkp,
2048 struct scsi_sense_hdr *sshdr)
2049{
2050 sd_printk(KERN_INFO, sdkp, "");
2051 scsi_show_sense_hdr(sshdr);
2052 sd_printk(KERN_INFO, sdkp, "");
2053 scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
2054}
2055
2056static void sd_print_result(struct scsi_disk *sdkp, int result)
2057{
2058 sd_printk(KERN_INFO, sdkp, "");
2059 scsi_show_result(result);
2060}
2061