]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
35 | #include <linux/config.h> | |
36 | #include <linux/module.h> | |
37 | #include <linux/fs.h> | |
38 | #include <linux/kernel.h> | |
39 | #include <linux/sched.h> | |
40 | #include <linux/mm.h> | |
41 | #include <linux/bio.h> | |
42 | #include <linux/genhd.h> | |
43 | #include <linux/hdreg.h> | |
44 | #include <linux/errno.h> | |
45 | #include <linux/idr.h> | |
46 | #include <linux/interrupt.h> | |
47 | #include <linux/init.h> | |
48 | #include <linux/blkdev.h> | |
49 | #include <linux/blkpg.h> | |
50 | #include <linux/kref.h> | |
51 | #include <linux/delay.h> | |
52 | #include <asm/uaccess.h> | |
53 | ||
54 | #include <scsi/scsi.h> | |
55 | #include <scsi/scsi_cmnd.h> | |
56 | #include <scsi/scsi_dbg.h> | |
57 | #include <scsi/scsi_device.h> | |
58 | #include <scsi/scsi_driver.h> | |
59 | #include <scsi/scsi_eh.h> | |
60 | #include <scsi/scsi_host.h> | |
61 | #include <scsi/scsi_ioctl.h> | |
1da177e4 LT |
62 | #include <scsi/scsicam.h> |
63 | ||
64 | #include "scsi_logging.h" | |
65 | ||
66 | /* | |
67 | * More than enough for everybody ;) The huge number of majors | |
68 | * is a leftover from 16bit dev_t days, we don't really need that | |
69 | * much numberspace. | |
70 | */ | |
71 | #define SD_MAJORS 16 | |
72 | ||
73 | /* | |
74 | * This is limited by the naming scheme enforced in sd_probe, | |
75 | * add another character to it if you really need more disks. | |
76 | */ | |
77 | #define SD_MAX_DISKS (((26 * 26) + 26 + 1) * 26) | |
78 | ||
79 | /* | |
80 | * Time out in seconds for disks and Magneto-opticals (which are slower). | |
81 | */ | |
82 | #define SD_TIMEOUT (30 * HZ) | |
83 | #define SD_MOD_TIMEOUT (75 * HZ) | |
84 | ||
85 | /* | |
86 | * Number of allowed retries | |
87 | */ | |
88 | #define SD_MAX_RETRIES 5 | |
89 | #define SD_PASSTHROUGH_RETRIES 1 | |
90 | ||
91 | static void scsi_disk_release(struct kref *kref); | |
92 | ||
93 | struct scsi_disk { | |
94 | struct scsi_driver *driver; /* always &sd_template */ | |
95 | struct scsi_device *device; | |
96 | struct kref kref; | |
97 | struct gendisk *disk; | |
98 | unsigned int openers; /* protected by BKL for now, yuck */ | |
99 | sector_t capacity; /* size in 512-byte sectors */ | |
100 | u32 index; | |
101 | u8 media_present; | |
102 | u8 write_prot; | |
103 | unsigned WCE : 1; /* state of disk WCE bit */ | |
104 | unsigned RCD : 1; /* state of disk RCD bit, unused */ | |
105 | }; | |
106 | ||
107 | static DEFINE_IDR(sd_index_idr); | |
108 | static DEFINE_SPINLOCK(sd_index_lock); | |
109 | ||
110 | /* This semaphore is used to mediate the 0->1 reference get in the | |
111 | * face of object destruction (i.e. we can't allow a get on an | |
112 | * object after last put) */ | |
113 | static DECLARE_MUTEX(sd_ref_sem); | |
114 | ||
115 | static int sd_revalidate_disk(struct gendisk *disk); | |
116 | static void sd_rw_intr(struct scsi_cmnd * SCpnt); | |
117 | ||
118 | static int sd_probe(struct device *); | |
119 | static int sd_remove(struct device *); | |
120 | static void sd_shutdown(struct device *dev); | |
121 | static void sd_rescan(struct device *); | |
122 | static int sd_init_command(struct scsi_cmnd *); | |
123 | static int sd_issue_flush(struct device *, sector_t *); | |
124 | static void sd_end_flush(request_queue_t *, struct request *); | |
125 | static int sd_prepare_flush(request_queue_t *, struct request *); | |
126 | static void sd_read_capacity(struct scsi_disk *sdkp, char *diskname, | |
ea73a9f2 | 127 | unsigned char *buffer); |
1da177e4 LT |
128 | |
129 | static struct scsi_driver sd_template = { | |
130 | .owner = THIS_MODULE, | |
131 | .gendrv = { | |
132 | .name = "sd", | |
133 | .probe = sd_probe, | |
134 | .remove = sd_remove, | |
135 | .shutdown = sd_shutdown, | |
136 | }, | |
137 | .rescan = sd_rescan, | |
138 | .init_command = sd_init_command, | |
139 | .issue_flush = sd_issue_flush, | |
140 | .prepare_flush = sd_prepare_flush, | |
141 | .end_flush = sd_end_flush, | |
142 | }; | |
143 | ||
144 | /* | |
145 | * Device no to disk mapping: | |
146 | * | |
147 | * major disc2 disc p1 | |
148 | * |............|.............|....|....| <- dev_t | |
149 | * 31 20 19 8 7 4 3 0 | |
150 | * | |
151 | * Inside a major, we have 16k disks, however mapped non- | |
152 | * contiguously. The first 16 disks are for major0, the next | |
153 | * ones with major1, ... Disk 256 is for major0 again, disk 272 | |
154 | * for major1, ... | |
155 | * As we stay compatible with our numbering scheme, we can reuse | |
156 | * the well-know SCSI majors 8, 65--71, 136--143. | |
157 | */ | |
158 | static int sd_major(int major_idx) | |
159 | { | |
160 | switch (major_idx) { | |
161 | case 0: | |
162 | return SCSI_DISK0_MAJOR; | |
163 | case 1 ... 7: | |
164 | return SCSI_DISK1_MAJOR + major_idx - 1; | |
165 | case 8 ... 15: | |
166 | return SCSI_DISK8_MAJOR + major_idx - 8; | |
167 | default: | |
168 | BUG(); | |
169 | return 0; /* shut up gcc */ | |
170 | } | |
171 | } | |
172 | ||
173 | #define to_scsi_disk(obj) container_of(obj,struct scsi_disk,kref) | |
174 | ||
175 | static inline struct scsi_disk *scsi_disk(struct gendisk *disk) | |
176 | { | |
177 | return container_of(disk->private_data, struct scsi_disk, driver); | |
178 | } | |
179 | ||
39b7f1e2 | 180 | static struct scsi_disk *__scsi_disk_get(struct gendisk *disk) |
1da177e4 LT |
181 | { |
182 | struct scsi_disk *sdkp = NULL; | |
183 | ||
39b7f1e2 AS |
184 | if (disk->private_data) { |
185 | sdkp = scsi_disk(disk); | |
186 | if (scsi_device_get(sdkp->device) == 0) | |
187 | kref_get(&sdkp->kref); | |
188 | else | |
189 | sdkp = NULL; | |
190 | } | |
191 | return sdkp; | |
192 | } | |
193 | ||
194 | static struct scsi_disk *scsi_disk_get(struct gendisk *disk) | |
195 | { | |
196 | struct scsi_disk *sdkp; | |
197 | ||
1da177e4 | 198 | down(&sd_ref_sem); |
39b7f1e2 | 199 | sdkp = __scsi_disk_get(disk); |
1da177e4 LT |
200 | up(&sd_ref_sem); |
201 | return sdkp; | |
39b7f1e2 | 202 | } |
1da177e4 | 203 | |
39b7f1e2 AS |
204 | static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev) |
205 | { | |
206 | struct scsi_disk *sdkp; | |
207 | ||
208 | down(&sd_ref_sem); | |
209 | sdkp = dev_get_drvdata(dev); | |
210 | if (sdkp) | |
211 | sdkp = __scsi_disk_get(sdkp->disk); | |
1da177e4 LT |
212 | up(&sd_ref_sem); |
213 | return sdkp; | |
214 | } | |
215 | ||
216 | static void scsi_disk_put(struct scsi_disk *sdkp) | |
217 | { | |
218 | struct scsi_device *sdev = sdkp->device; | |
219 | ||
220 | down(&sd_ref_sem); | |
221 | kref_put(&sdkp->kref, scsi_disk_release); | |
222 | scsi_device_put(sdev); | |
223 | up(&sd_ref_sem); | |
224 | } | |
225 | ||
226 | /** | |
227 | * sd_init_command - build a scsi (read or write) command from | |
228 | * information in the request structure. | |
229 | * @SCpnt: pointer to mid-level's per scsi command structure that | |
230 | * contains request and into which the scsi command is written | |
231 | * | |
232 | * Returns 1 if successful and 0 if error (or cannot be done now). | |
233 | **/ | |
234 | static int sd_init_command(struct scsi_cmnd * SCpnt) | |
235 | { | |
236 | unsigned int this_count, timeout; | |
237 | struct gendisk *disk; | |
238 | sector_t block; | |
239 | struct scsi_device *sdp = SCpnt->device; | |
240 | struct request *rq = SCpnt->request; | |
241 | ||
242 | timeout = sdp->timeout; | |
243 | ||
244 | /* | |
245 | * SG_IO from block layer already setup, just copy cdb basically | |
246 | */ | |
247 | if (blk_pc_request(rq)) { | |
7b16318d | 248 | scsi_setup_blk_pc_cmnd(SCpnt); |
1da177e4 LT |
249 | if (rq->timeout) |
250 | timeout = rq->timeout; | |
251 | ||
1da177e4 LT |
252 | goto queue; |
253 | } | |
254 | ||
255 | /* | |
256 | * we only do REQ_CMD and REQ_BLOCK_PC | |
257 | */ | |
258 | if (!blk_fs_request(rq)) | |
259 | return 0; | |
260 | ||
261 | disk = rq->rq_disk; | |
262 | block = rq->sector; | |
263 | this_count = SCpnt->request_bufflen >> 9; | |
264 | ||
265 | SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, " | |
266 | "count=%d\n", disk->disk_name, | |
267 | (unsigned long long)block, this_count)); | |
268 | ||
269 | if (!sdp || !scsi_device_online(sdp) || | |
270 | block + rq->nr_sectors > get_capacity(disk)) { | |
271 | SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", | |
272 | rq->nr_sectors)); | |
273 | SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt)); | |
274 | return 0; | |
275 | } | |
276 | ||
277 | if (sdp->changed) { | |
278 | /* | |
279 | * quietly refuse to do anything to a changed disc until | |
280 | * the changed bit has been reset | |
281 | */ | |
282 | /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */ | |
283 | return 0; | |
284 | } | |
285 | SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n", | |
286 | disk->disk_name, (unsigned long long)block)); | |
287 | ||
288 | /* | |
289 | * If we have a 1K hardware sectorsize, prevent access to single | |
290 | * 512 byte sectors. In theory we could handle this - in fact | |
291 | * the scsi cdrom driver must be able to handle this because | |
292 | * we typically use 1K blocksizes, and cdroms typically have | |
293 | * 2K hardware sectorsizes. Of course, things are simpler | |
294 | * with the cdrom, since it is read-only. For performance | |
295 | * reasons, the filesystems should be able to handle this | |
296 | * and not force the scsi disk driver to use bounce buffers | |
297 | * for this. | |
298 | */ | |
299 | if (sdp->sector_size == 1024) { | |
300 | if ((block & 1) || (rq->nr_sectors & 1)) { | |
301 | printk(KERN_ERR "sd: Bad block number requested"); | |
302 | return 0; | |
303 | } else { | |
304 | block = block >> 1; | |
305 | this_count = this_count >> 1; | |
306 | } | |
307 | } | |
308 | if (sdp->sector_size == 2048) { | |
309 | if ((block & 3) || (rq->nr_sectors & 3)) { | |
310 | printk(KERN_ERR "sd: Bad block number requested"); | |
311 | return 0; | |
312 | } else { | |
313 | block = block >> 2; | |
314 | this_count = this_count >> 2; | |
315 | } | |
316 | } | |
317 | if (sdp->sector_size == 4096) { | |
318 | if ((block & 7) || (rq->nr_sectors & 7)) { | |
319 | printk(KERN_ERR "sd: Bad block number requested"); | |
320 | return 0; | |
321 | } else { | |
322 | block = block >> 3; | |
323 | this_count = this_count >> 3; | |
324 | } | |
325 | } | |
326 | if (rq_data_dir(rq) == WRITE) { | |
327 | if (!sdp->writeable) { | |
328 | return 0; | |
329 | } | |
330 | SCpnt->cmnd[0] = WRITE_6; | |
331 | SCpnt->sc_data_direction = DMA_TO_DEVICE; | |
332 | } else if (rq_data_dir(rq) == READ) { | |
333 | SCpnt->cmnd[0] = READ_6; | |
334 | SCpnt->sc_data_direction = DMA_FROM_DEVICE; | |
335 | } else { | |
336 | printk(KERN_ERR "sd: Unknown command %lx\n", rq->flags); | |
337 | /* overkill panic("Unknown sd command %lx\n", rq->flags); */ | |
338 | return 0; | |
339 | } | |
340 | ||
341 | SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n", | |
342 | disk->disk_name, (rq_data_dir(rq) == WRITE) ? | |
343 | "writing" : "reading", this_count, rq->nr_sectors)); | |
344 | ||
345 | SCpnt->cmnd[1] = 0; | |
346 | ||
347 | if (block > 0xffffffff) { | |
348 | SCpnt->cmnd[0] += READ_16 - READ_6; | |
349 | SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0; | |
350 | SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0; | |
351 | SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0; | |
352 | SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0; | |
353 | SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff; | |
354 | SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff; | |
355 | SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff; | |
356 | SCpnt->cmnd[9] = (unsigned char) block & 0xff; | |
357 | SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff; | |
358 | SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff; | |
359 | SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff; | |
360 | SCpnt->cmnd[13] = (unsigned char) this_count & 0xff; | |
361 | SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0; | |
362 | } else if ((this_count > 0xff) || (block > 0x1fffff) || | |
363 | SCpnt->device->use_10_for_rw) { | |
364 | if (this_count > 0xffff) | |
365 | this_count = 0xffff; | |
366 | ||
367 | SCpnt->cmnd[0] += READ_10 - READ_6; | |
368 | SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff; | |
369 | SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff; | |
370 | SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff; | |
371 | SCpnt->cmnd[5] = (unsigned char) block & 0xff; | |
372 | SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; | |
373 | SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff; | |
374 | SCpnt->cmnd[8] = (unsigned char) this_count & 0xff; | |
375 | } else { | |
1da177e4 LT |
376 | SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f); |
377 | SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff); | |
378 | SCpnt->cmnd[3] = (unsigned char) block & 0xff; | |
379 | SCpnt->cmnd[4] = (unsigned char) this_count; | |
380 | SCpnt->cmnd[5] = 0; | |
381 | } | |
382 | SCpnt->request_bufflen = SCpnt->bufflen = | |
383 | this_count * sdp->sector_size; | |
384 | ||
385 | /* | |
386 | * We shouldn't disconnect in the middle of a sector, so with a dumb | |
387 | * host adapter, it's safe to assume that we can at least transfer | |
388 | * this many bytes between each connect / disconnect. | |
389 | */ | |
390 | SCpnt->transfersize = sdp->sector_size; | |
391 | SCpnt->underflow = this_count << 9; | |
392 | SCpnt->allowed = SD_MAX_RETRIES; | |
393 | ||
394 | queue: | |
395 | SCpnt->timeout_per_command = timeout; | |
396 | ||
397 | /* | |
398 | * This is the completion routine we use. This is matched in terms | |
399 | * of capability to this function. | |
400 | */ | |
401 | SCpnt->done = sd_rw_intr; | |
402 | ||
403 | /* | |
404 | * This indicates that the command is ready from our end to be | |
405 | * queued. | |
406 | */ | |
407 | return 1; | |
408 | } | |
409 | ||
410 | /** | |
411 | * sd_open - open a scsi disk device | |
412 | * @inode: only i_rdev member may be used | |
413 | * @filp: only f_mode and f_flags may be used | |
414 | * | |
415 | * Returns 0 if successful. Returns a negated errno value in case | |
416 | * of error. | |
417 | * | |
418 | * Note: This can be called from a user context (e.g. fsck(1) ) | |
419 | * or from within the kernel (e.g. as a result of a mount(1) ). | |
420 | * In the latter case @inode and @filp carry an abridged amount | |
421 | * of information as noted above. | |
422 | **/ | |
423 | static int sd_open(struct inode *inode, struct file *filp) | |
424 | { | |
425 | struct gendisk *disk = inode->i_bdev->bd_disk; | |
426 | struct scsi_disk *sdkp; | |
427 | struct scsi_device *sdev; | |
428 | int retval; | |
429 | ||
430 | if (!(sdkp = scsi_disk_get(disk))) | |
431 | return -ENXIO; | |
432 | ||
433 | ||
434 | SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk->disk_name)); | |
435 | ||
436 | sdev = sdkp->device; | |
437 | ||
438 | /* | |
439 | * If the device is in error recovery, wait until it is done. | |
440 | * If the device is offline, then disallow any access to it. | |
441 | */ | |
442 | retval = -ENXIO; | |
443 | if (!scsi_block_when_processing_errors(sdev)) | |
444 | goto error_out; | |
445 | ||
446 | if (sdev->removable || sdkp->write_prot) | |
447 | check_disk_change(inode->i_bdev); | |
448 | ||
449 | /* | |
450 | * If the drive is empty, just let the open fail. | |
451 | */ | |
452 | retval = -ENOMEDIUM; | |
453 | if (sdev->removable && !sdkp->media_present && | |
454 | !(filp->f_flags & O_NDELAY)) | |
455 | goto error_out; | |
456 | ||
457 | /* | |
458 | * If the device has the write protect tab set, have the open fail | |
459 | * if the user expects to be able to write to the thing. | |
460 | */ | |
461 | retval = -EROFS; | |
462 | if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE)) | |
463 | goto error_out; | |
464 | ||
465 | /* | |
466 | * It is possible that the disk changing stuff resulted in | |
467 | * the device being taken offline. If this is the case, | |
468 | * report this to the user, and don't pretend that the | |
469 | * open actually succeeded. | |
470 | */ | |
471 | retval = -ENXIO; | |
472 | if (!scsi_device_online(sdev)) | |
473 | goto error_out; | |
474 | ||
475 | if (!sdkp->openers++ && sdev->removable) { | |
476 | if (scsi_block_when_processing_errors(sdev)) | |
477 | scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT); | |
478 | } | |
479 | ||
480 | return 0; | |
481 | ||
482 | error_out: | |
483 | scsi_disk_put(sdkp); | |
484 | return retval; | |
485 | } | |
486 | ||
487 | /** | |
488 | * sd_release - invoked when the (last) close(2) is called on this | |
489 | * scsi disk. | |
490 | * @inode: only i_rdev member may be used | |
491 | * @filp: only f_mode and f_flags may be used | |
492 | * | |
493 | * Returns 0. | |
494 | * | |
495 | * Note: may block (uninterruptible) if error recovery is underway | |
496 | * on this disk. | |
497 | **/ | |
498 | static int sd_release(struct inode *inode, struct file *filp) | |
499 | { | |
500 | struct gendisk *disk = inode->i_bdev->bd_disk; | |
501 | struct scsi_disk *sdkp = scsi_disk(disk); | |
502 | struct scsi_device *sdev = sdkp->device; | |
503 | ||
504 | SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk->disk_name)); | |
505 | ||
506 | if (!--sdkp->openers && sdev->removable) { | |
507 | if (scsi_block_when_processing_errors(sdev)) | |
508 | scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW); | |
509 | } | |
510 | ||
511 | /* | |
512 | * XXX and what if there are packets in flight and this close() | |
513 | * XXX is followed by a "rmmod sd_mod"? | |
514 | */ | |
515 | scsi_disk_put(sdkp); | |
516 | return 0; | |
517 | } | |
518 | ||
519 | static int sd_hdio_getgeo(struct block_device *bdev, struct hd_geometry __user *loc) | |
520 | { | |
521 | struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk); | |
522 | struct scsi_device *sdp = sdkp->device; | |
523 | struct Scsi_Host *host = sdp->host; | |
524 | int diskinfo[4]; | |
525 | ||
526 | /* default to most commonly used values */ | |
527 | diskinfo[0] = 0x40; /* 1 << 6 */ | |
528 | diskinfo[1] = 0x20; /* 1 << 5 */ | |
529 | diskinfo[2] = sdkp->capacity >> 11; | |
530 | ||
531 | /* override with calculated, extended default, or driver values */ | |
532 | if (host->hostt->bios_param) | |
533 | host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo); | |
534 | else | |
535 | scsicam_bios_param(bdev, sdkp->capacity, diskinfo); | |
536 | ||
537 | if (put_user(diskinfo[0], &loc->heads)) | |
538 | return -EFAULT; | |
539 | if (put_user(diskinfo[1], &loc->sectors)) | |
540 | return -EFAULT; | |
541 | if (put_user(diskinfo[2], &loc->cylinders)) | |
542 | return -EFAULT; | |
543 | if (put_user((unsigned)get_start_sect(bdev), | |
544 | (unsigned long __user *)&loc->start)) | |
545 | return -EFAULT; | |
546 | return 0; | |
547 | } | |
548 | ||
549 | /** | |
550 | * sd_ioctl - process an ioctl | |
551 | * @inode: only i_rdev/i_bdev members may be used | |
552 | * @filp: only f_mode and f_flags may be used | |
553 | * @cmd: ioctl command number | |
554 | * @arg: this is third argument given to ioctl(2) system call. | |
555 | * Often contains a pointer. | |
556 | * | |
557 | * Returns 0 if successful (some ioctls return postive numbers on | |
558 | * success as well). Returns a negated errno value in case of error. | |
559 | * | |
560 | * Note: most ioctls are forward onto the block subsystem or further | |
561 | * down in the scsi subsytem. | |
562 | **/ | |
563 | static int sd_ioctl(struct inode * inode, struct file * filp, | |
564 | unsigned int cmd, unsigned long arg) | |
565 | { | |
566 | struct block_device *bdev = inode->i_bdev; | |
567 | struct gendisk *disk = bdev->bd_disk; | |
568 | struct scsi_device *sdp = scsi_disk(disk)->device; | |
569 | void __user *p = (void __user *)arg; | |
570 | int error; | |
571 | ||
572 | SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n", | |
573 | disk->disk_name, cmd)); | |
574 | ||
575 | /* | |
576 | * If we are in the middle of error recovery, don't let anyone | |
577 | * else try and use this device. Also, if error recovery fails, it | |
578 | * may try and take the device offline, in which case all further | |
579 | * access to the device is prohibited. | |
580 | */ | |
581 | error = scsi_nonblockable_ioctl(sdp, cmd, p, filp); | |
582 | if (!scsi_block_when_processing_errors(sdp) || !error) | |
583 | return error; | |
584 | ||
585 | if (cmd == HDIO_GETGEO) { | |
586 | if (!arg) | |
587 | return -EINVAL; | |
588 | return sd_hdio_getgeo(bdev, p); | |
589 | } | |
590 | ||
591 | /* | |
592 | * Send SCSI addressing ioctls directly to mid level, send other | |
593 | * ioctls to block level and then onto mid level if they can't be | |
594 | * resolved. | |
595 | */ | |
596 | switch (cmd) { | |
597 | case SCSI_IOCTL_GET_IDLUN: | |
598 | case SCSI_IOCTL_GET_BUS_NUMBER: | |
599 | return scsi_ioctl(sdp, cmd, p); | |
600 | default: | |
601 | error = scsi_cmd_ioctl(filp, disk, cmd, p); | |
602 | if (error != -ENOTTY) | |
603 | return error; | |
604 | } | |
605 | return scsi_ioctl(sdp, cmd, p); | |
606 | } | |
607 | ||
608 | static void set_media_not_present(struct scsi_disk *sdkp) | |
609 | { | |
610 | sdkp->media_present = 0; | |
611 | sdkp->capacity = 0; | |
612 | sdkp->device->changed = 1; | |
613 | } | |
614 | ||
615 | /** | |
616 | * sd_media_changed - check if our medium changed | |
617 | * @disk: kernel device descriptor | |
618 | * | |
619 | * Returns 0 if not applicable or no change; 1 if change | |
620 | * | |
621 | * Note: this function is invoked from the block subsystem. | |
622 | **/ | |
623 | static int sd_media_changed(struct gendisk *disk) | |
624 | { | |
625 | struct scsi_disk *sdkp = scsi_disk(disk); | |
626 | struct scsi_device *sdp = sdkp->device; | |
627 | int retval; | |
628 | ||
629 | SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n", | |
630 | disk->disk_name)); | |
631 | ||
632 | if (!sdp->removable) | |
633 | return 0; | |
634 | ||
635 | /* | |
636 | * If the device is offline, don't send any commands - just pretend as | |
637 | * if the command failed. If the device ever comes back online, we | |
638 | * can deal with it then. It is only because of unrecoverable errors | |
639 | * that we would ever take a device offline in the first place. | |
640 | */ | |
641 | if (!scsi_device_online(sdp)) | |
642 | goto not_present; | |
643 | ||
644 | /* | |
645 | * Using TEST_UNIT_READY enables differentiation between drive with | |
646 | * no cartridge loaded - NOT READY, drive with changed cartridge - | |
647 | * UNIT ATTENTION, or with same cartridge - GOOD STATUS. | |
648 | * | |
649 | * Drives that auto spin down. eg iomega jaz 1G, will be started | |
650 | * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever | |
651 | * sd_revalidate() is called. | |
652 | */ | |
653 | retval = -ENODEV; | |
654 | if (scsi_block_when_processing_errors(sdp)) | |
655 | retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES); | |
656 | ||
657 | /* | |
658 | * Unable to test, unit probably not ready. This usually | |
659 | * means there is no disc in the drive. Mark as changed, | |
660 | * and we will figure it out later once the drive is | |
661 | * available again. | |
662 | */ | |
663 | if (retval) | |
664 | goto not_present; | |
665 | ||
666 | /* | |
667 | * For removable scsi disk we have to recognise the presence | |
668 | * of a disk in the drive. This is kept in the struct scsi_disk | |
669 | * struct and tested at open ! Daniel Roche (dan@lectra.fr) | |
670 | */ | |
671 | sdkp->media_present = 1; | |
672 | ||
673 | retval = sdp->changed; | |
674 | sdp->changed = 0; | |
675 | ||
676 | return retval; | |
677 | ||
678 | not_present: | |
679 | set_media_not_present(sdkp); | |
680 | return 1; | |
681 | } | |
682 | ||
683 | static int sd_sync_cache(struct scsi_device *sdp) | |
684 | { | |
1da177e4 | 685 | int retries, res; |
ea73a9f2 | 686 | struct scsi_sense_hdr sshdr; |
1da177e4 LT |
687 | |
688 | if (!scsi_device_online(sdp)) | |
689 | return -ENODEV; | |
690 | ||
1da177e4 | 691 | |
1da177e4 LT |
692 | for (retries = 3; retries > 0; --retries) { |
693 | unsigned char cmd[10] = { 0 }; | |
694 | ||
695 | cmd[0] = SYNCHRONIZE_CACHE; | |
696 | /* | |
697 | * Leave the rest of the command zero to indicate | |
698 | * flush everything. | |
699 | */ | |
ea73a9f2 JB |
700 | res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr, |
701 | SD_TIMEOUT, SD_MAX_RETRIES); | |
702 | if (res == 0) | |
1da177e4 LT |
703 | break; |
704 | } | |
705 | ||
ea73a9f2 | 706 | if (res) { printk(KERN_WARNING "FAILED\n status = %x, message = %02x, " |
1da177e4 LT |
707 | "host = %d, driver = %02x\n ", |
708 | status_byte(res), msg_byte(res), | |
709 | host_byte(res), driver_byte(res)); | |
710 | if (driver_byte(res) & DRIVER_SENSE) | |
ea73a9f2 | 711 | scsi_print_sense_hdr("sd", &sshdr); |
1da177e4 LT |
712 | } |
713 | ||
1da177e4 LT |
714 | return res; |
715 | } | |
716 | ||
717 | static int sd_issue_flush(struct device *dev, sector_t *error_sector) | |
718 | { | |
39b7f1e2 | 719 | int ret = 0; |
1da177e4 | 720 | struct scsi_device *sdp = to_scsi_device(dev); |
39b7f1e2 | 721 | struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); |
1da177e4 LT |
722 | |
723 | if (!sdkp) | |
724 | return -ENODEV; | |
725 | ||
39b7f1e2 AS |
726 | if (sdkp->WCE) |
727 | ret = sd_sync_cache(sdp); | |
728 | scsi_disk_put(sdkp); | |
729 | return ret; | |
1da177e4 LT |
730 | } |
731 | ||
732 | static void sd_end_flush(request_queue_t *q, struct request *flush_rq) | |
733 | { | |
734 | struct request *rq = flush_rq->end_io_data; | |
735 | struct scsi_cmnd *cmd = rq->special; | |
736 | unsigned int bytes = rq->hard_nr_sectors << 9; | |
737 | ||
738 | if (!flush_rq->errors) { | |
739 | spin_unlock(q->queue_lock); | |
740 | scsi_io_completion(cmd, bytes, 0); | |
741 | spin_lock(q->queue_lock); | |
742 | } else if (blk_barrier_postflush(rq)) { | |
743 | spin_unlock(q->queue_lock); | |
744 | scsi_io_completion(cmd, 0, bytes); | |
745 | spin_lock(q->queue_lock); | |
746 | } else { | |
747 | /* | |
748 | * force journal abort of barriers | |
749 | */ | |
750 | end_that_request_first(rq, -EOPNOTSUPP, rq->hard_nr_sectors); | |
8ffdc655 | 751 | end_that_request_last(rq, -EOPNOTSUPP); |
1da177e4 LT |
752 | } |
753 | } | |
754 | ||
755 | static int sd_prepare_flush(request_queue_t *q, struct request *rq) | |
756 | { | |
757 | struct scsi_device *sdev = q->queuedata; | |
c0ed79a3 | 758 | struct scsi_disk *sdkp = dev_get_drvdata(&sdev->sdev_gendev); |
39b7f1e2 | 759 | |
c0ed79a3 JB |
760 | if (!sdkp || !sdkp->WCE) |
761 | return 0; | |
762 | ||
763 | memset(rq->cmd, 0, sizeof(rq->cmd)); | |
764 | rq->flags |= REQ_BLOCK_PC | REQ_SOFTBARRIER; | |
765 | rq->timeout = SD_TIMEOUT; | |
766 | rq->cmd[0] = SYNCHRONIZE_CACHE; | |
767 | return 1; | |
1da177e4 LT |
768 | } |
769 | ||
770 | static void sd_rescan(struct device *dev) | |
771 | { | |
39b7f1e2 AS |
772 | struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); |
773 | ||
774 | if (sdkp) { | |
775 | sd_revalidate_disk(sdkp->disk); | |
776 | scsi_disk_put(sdkp); | |
777 | } | |
1da177e4 LT |
778 | } |
779 | ||
780 | ||
781 | #ifdef CONFIG_COMPAT | |
782 | /* | |
783 | * This gets directly called from VFS. When the ioctl | |
784 | * is not recognized we go back to the other translation paths. | |
785 | */ | |
786 | static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
787 | { | |
788 | struct block_device *bdev = file->f_dentry->d_inode->i_bdev; | |
789 | struct gendisk *disk = bdev->bd_disk; | |
790 | struct scsi_device *sdev = scsi_disk(disk)->device; | |
791 | ||
792 | /* | |
793 | * If we are in the middle of error recovery, don't let anyone | |
794 | * else try and use this device. Also, if error recovery fails, it | |
795 | * may try and take the device offline, in which case all further | |
796 | * access to the device is prohibited. | |
797 | */ | |
798 | if (!scsi_block_when_processing_errors(sdev)) | |
799 | return -ENODEV; | |
800 | ||
801 | if (sdev->host->hostt->compat_ioctl) { | |
802 | int ret; | |
803 | ||
804 | ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg); | |
805 | ||
806 | return ret; | |
807 | } | |
808 | ||
809 | /* | |
810 | * Let the static ioctl translation table take care of it. | |
811 | */ | |
812 | return -ENOIOCTLCMD; | |
813 | } | |
814 | #endif | |
815 | ||
816 | static struct block_device_operations sd_fops = { | |
817 | .owner = THIS_MODULE, | |
818 | .open = sd_open, | |
819 | .release = sd_release, | |
820 | .ioctl = sd_ioctl, | |
821 | #ifdef CONFIG_COMPAT | |
822 | .compat_ioctl = sd_compat_ioctl, | |
823 | #endif | |
824 | .media_changed = sd_media_changed, | |
825 | .revalidate_disk = sd_revalidate_disk, | |
826 | }; | |
827 | ||
828 | /** | |
829 | * sd_rw_intr - bottom half handler: called when the lower level | |
830 | * driver has completed (successfully or otherwise) a scsi command. | |
831 | * @SCpnt: mid-level's per command structure. | |
832 | * | |
833 | * Note: potentially run from within an ISR. Must not block. | |
834 | **/ | |
835 | static void sd_rw_intr(struct scsi_cmnd * SCpnt) | |
836 | { | |
837 | int result = SCpnt->result; | |
838 | int this_count = SCpnt->bufflen; | |
839 | int good_bytes = (result == 0 ? this_count : 0); | |
840 | sector_t block_sectors = 1; | |
841 | u64 first_err_block; | |
842 | sector_t error_sector; | |
843 | struct scsi_sense_hdr sshdr; | |
844 | int sense_valid = 0; | |
845 | int sense_deferred = 0; | |
846 | int info_valid; | |
847 | ||
848 | if (result) { | |
849 | sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr); | |
850 | if (sense_valid) | |
851 | sense_deferred = scsi_sense_is_deferred(&sshdr); | |
852 | } | |
853 | ||
854 | #ifdef CONFIG_SCSI_LOGGING | |
855 | SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n", | |
856 | SCpnt->request->rq_disk->disk_name, result)); | |
857 | if (sense_valid) { | |
858 | SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[respc,sk,asc," | |
859 | "ascq]=%x,%x,%x,%x\n", sshdr.response_code, | |
860 | sshdr.sense_key, sshdr.asc, sshdr.ascq)); | |
861 | } | |
862 | #endif | |
863 | /* | |
864 | Handle MEDIUM ERRORs that indicate partial success. Since this is a | |
865 | relatively rare error condition, no care is taken to avoid | |
866 | unnecessary additional work such as memcpy's that could be avoided. | |
867 | */ | |
868 | ||
869 | /* | |
870 | * If SG_IO from block layer then set good_bytes to stop retries; | |
871 | * else if errors, check them, and if necessary prepare for | |
872 | * (partial) retries. | |
873 | */ | |
874 | if (blk_pc_request(SCpnt->request)) | |
875 | good_bytes = this_count; | |
876 | else if (driver_byte(result) != 0 && | |
877 | sense_valid && !sense_deferred) { | |
878 | switch (sshdr.sense_key) { | |
879 | case MEDIUM_ERROR: | |
880 | if (!blk_fs_request(SCpnt->request)) | |
881 | break; | |
882 | info_valid = scsi_get_sense_info_fld( | |
883 | SCpnt->sense_buffer, SCSI_SENSE_BUFFERSIZE, | |
884 | &first_err_block); | |
885 | /* | |
886 | * May want to warn and skip if following cast results | |
887 | * in actual truncation (if sector_t < 64 bits) | |
888 | */ | |
889 | error_sector = (sector_t)first_err_block; | |
890 | if (SCpnt->request->bio != NULL) | |
891 | block_sectors = bio_sectors(SCpnt->request->bio); | |
892 | switch (SCpnt->device->sector_size) { | |
893 | case 1024: | |
894 | error_sector <<= 1; | |
895 | if (block_sectors < 2) | |
896 | block_sectors = 2; | |
897 | break; | |
898 | case 2048: | |
899 | error_sector <<= 2; | |
900 | if (block_sectors < 4) | |
901 | block_sectors = 4; | |
902 | break; | |
903 | case 4096: | |
904 | error_sector <<=3; | |
905 | if (block_sectors < 8) | |
906 | block_sectors = 8; | |
907 | break; | |
908 | case 256: | |
909 | error_sector >>= 1; | |
910 | break; | |
911 | default: | |
912 | break; | |
913 | } | |
914 | ||
915 | error_sector &= ~(block_sectors - 1); | |
916 | good_bytes = (error_sector - SCpnt->request->sector) << 9; | |
917 | if (good_bytes < 0 || good_bytes >= this_count) | |
918 | good_bytes = 0; | |
919 | break; | |
920 | ||
921 | case RECOVERED_ERROR: /* an error occurred, but it recovered */ | |
922 | case NO_SENSE: /* LLDD got sense data */ | |
923 | /* | |
924 | * Inform the user, but make sure that it's not treated | |
925 | * as a hard error. | |
926 | */ | |
927 | scsi_print_sense("sd", SCpnt); | |
928 | SCpnt->result = 0; | |
929 | memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); | |
930 | good_bytes = this_count; | |
931 | break; | |
932 | ||
933 | case ILLEGAL_REQUEST: | |
934 | if (SCpnt->device->use_10_for_rw && | |
935 | (SCpnt->cmnd[0] == READ_10 || | |
936 | SCpnt->cmnd[0] == WRITE_10)) | |
937 | SCpnt->device->use_10_for_rw = 0; | |
938 | if (SCpnt->device->use_10_for_ms && | |
939 | (SCpnt->cmnd[0] == MODE_SENSE_10 || | |
940 | SCpnt->cmnd[0] == MODE_SELECT_10)) | |
941 | SCpnt->device->use_10_for_ms = 0; | |
942 | break; | |
943 | ||
944 | default: | |
945 | break; | |
946 | } | |
947 | } | |
948 | /* | |
949 | * This calls the generic completion function, now that we know | |
950 | * how many actual sectors finished, and how many sectors we need | |
951 | * to say have failed. | |
952 | */ | |
953 | scsi_io_completion(SCpnt, good_bytes, block_sectors << 9); | |
954 | } | |
955 | ||
ea73a9f2 JB |
956 | static int media_not_present(struct scsi_disk *sdkp, |
957 | struct scsi_sense_hdr *sshdr) | |
1da177e4 | 958 | { |
1da177e4 | 959 | |
ea73a9f2 | 960 | if (!scsi_sense_valid(sshdr)) |
1da177e4 LT |
961 | return 0; |
962 | /* not invoked for commands that could return deferred errors */ | |
ea73a9f2 JB |
963 | if (sshdr->sense_key != NOT_READY && |
964 | sshdr->sense_key != UNIT_ATTENTION) | |
965 | return 0; | |
966 | if (sshdr->asc != 0x3A) /* medium not present */ | |
967 | return 0; | |
968 | ||
1da177e4 LT |
969 | set_media_not_present(sdkp); |
970 | return 1; | |
971 | } | |
972 | ||
973 | /* | |
974 | * spinup disk - called only in sd_revalidate_disk() | |
975 | */ | |
976 | static void | |
ea73a9f2 JB |
977 | sd_spinup_disk(struct scsi_disk *sdkp, char *diskname) |
978 | { | |
1da177e4 | 979 | unsigned char cmd[10]; |
4451e472 | 980 | unsigned long spintime_expire = 0; |
1da177e4 LT |
981 | int retries, spintime; |
982 | unsigned int the_result; | |
983 | struct scsi_sense_hdr sshdr; | |
984 | int sense_valid = 0; | |
985 | ||
986 | spintime = 0; | |
987 | ||
988 | /* Spin up drives, as required. Only do this at boot time */ | |
989 | /* Spinup needs to be done for module loads too. */ | |
990 | do { | |
991 | retries = 0; | |
992 | ||
993 | do { | |
994 | cmd[0] = TEST_UNIT_READY; | |
995 | memset((void *) &cmd[1], 0, 9); | |
996 | ||
ea73a9f2 JB |
997 | the_result = scsi_execute_req(sdkp->device, cmd, |
998 | DMA_NONE, NULL, 0, | |
999 | &sshdr, SD_TIMEOUT, | |
1000 | SD_MAX_RETRIES); | |
1da177e4 | 1001 | |
1da177e4 | 1002 | if (the_result) |
ea73a9f2 | 1003 | sense_valid = scsi_sense_valid(&sshdr); |
1da177e4 LT |
1004 | retries++; |
1005 | } while (retries < 3 && | |
1006 | (!scsi_status_is_good(the_result) || | |
1007 | ((driver_byte(the_result) & DRIVER_SENSE) && | |
1008 | sense_valid && sshdr.sense_key == UNIT_ATTENTION))); | |
1009 | ||
1010 | /* | |
1011 | * If the drive has indicated to us that it doesn't have | |
1012 | * any media in it, don't bother with any of the rest of | |
1013 | * this crap. | |
1014 | */ | |
ea73a9f2 | 1015 | if (media_not_present(sdkp, &sshdr)) |
1da177e4 LT |
1016 | return; |
1017 | ||
1018 | if ((driver_byte(the_result) & DRIVER_SENSE) == 0) { | |
1019 | /* no sense, TUR either succeeded or failed | |
1020 | * with a status error */ | |
1021 | if(!spintime && !scsi_status_is_good(the_result)) | |
1022 | printk(KERN_NOTICE "%s: Unit Not Ready, " | |
1023 | "error = 0x%x\n", diskname, the_result); | |
1024 | break; | |
1025 | } | |
1026 | ||
1027 | /* | |
1028 | * The device does not want the automatic start to be issued. | |
1029 | */ | |
1030 | if (sdkp->device->no_start_on_add) { | |
1031 | break; | |
1032 | } | |
1033 | ||
1034 | /* | |
1035 | * If manual intervention is required, or this is an | |
1036 | * absent USB storage device, a spinup is meaningless. | |
1037 | */ | |
1038 | if (sense_valid && | |
1039 | sshdr.sense_key == NOT_READY && | |
1040 | sshdr.asc == 4 && sshdr.ascq == 3) { | |
1041 | break; /* manual intervention required */ | |
1042 | ||
1043 | /* | |
1044 | * Issue command to spin up drive when not ready | |
1045 | */ | |
1046 | } else if (sense_valid && sshdr.sense_key == NOT_READY) { | |
1047 | if (!spintime) { | |
1048 | printk(KERN_NOTICE "%s: Spinning up disk...", | |
1049 | diskname); | |
1050 | cmd[0] = START_STOP; | |
1051 | cmd[1] = 1; /* Return immediately */ | |
1052 | memset((void *) &cmd[2], 0, 8); | |
1053 | cmd[4] = 1; /* Start spin cycle */ | |
ea73a9f2 JB |
1054 | scsi_execute_req(sdkp->device, cmd, DMA_NONE, |
1055 | NULL, 0, &sshdr, | |
1056 | SD_TIMEOUT, SD_MAX_RETRIES); | |
4451e472 AS |
1057 | spintime_expire = jiffies + 100 * HZ; |
1058 | spintime = 1; | |
1da177e4 | 1059 | } |
1da177e4 LT |
1060 | /* Wait 1 second for next try */ |
1061 | msleep(1000); | |
1062 | printk("."); | |
4451e472 AS |
1063 | |
1064 | /* | |
1065 | * Wait for USB flash devices with slow firmware. | |
1066 | * Yes, this sense key/ASC combination shouldn't | |
1067 | * occur here. It's characteristic of these devices. | |
1068 | */ | |
1069 | } else if (sense_valid && | |
1070 | sshdr.sense_key == UNIT_ATTENTION && | |
1071 | sshdr.asc == 0x28) { | |
1072 | if (!spintime) { | |
1073 | spintime_expire = jiffies + 5 * HZ; | |
1074 | spintime = 1; | |
1075 | } | |
1076 | /* Wait 1 second for next try */ | |
1077 | msleep(1000); | |
1da177e4 LT |
1078 | } else { |
1079 | /* we don't understand the sense code, so it's | |
1080 | * probably pointless to loop */ | |
1081 | if(!spintime) { | |
1082 | printk(KERN_NOTICE "%s: Unit Not Ready, " | |
1083 | "sense:\n", diskname); | |
ea73a9f2 | 1084 | scsi_print_sense_hdr("", &sshdr); |
1da177e4 LT |
1085 | } |
1086 | break; | |
1087 | } | |
1088 | ||
4451e472 | 1089 | } while (spintime && time_before_eq(jiffies, spintime_expire)); |
1da177e4 LT |
1090 | |
1091 | if (spintime) { | |
1092 | if (scsi_status_is_good(the_result)) | |
1093 | printk("ready\n"); | |
1094 | else | |
1095 | printk("not responding...\n"); | |
1096 | } | |
1097 | } | |
1098 | ||
1099 | /* | |
1100 | * read disk capacity | |
1101 | */ | |
1102 | static void | |
1103 | sd_read_capacity(struct scsi_disk *sdkp, char *diskname, | |
ea73a9f2 JB |
1104 | unsigned char *buffer) |
1105 | { | |
1da177e4 | 1106 | unsigned char cmd[16]; |
1da177e4 LT |
1107 | int the_result, retries; |
1108 | int sector_size = 0; | |
1109 | int longrc = 0; | |
1110 | struct scsi_sense_hdr sshdr; | |
1111 | int sense_valid = 0; | |
ea73a9f2 | 1112 | struct scsi_device *sdp = sdkp->device; |
1da177e4 LT |
1113 | |
1114 | repeat: | |
1115 | retries = 3; | |
1116 | do { | |
1117 | if (longrc) { | |
1118 | memset((void *) cmd, 0, 16); | |
1119 | cmd[0] = SERVICE_ACTION_IN; | |
1120 | cmd[1] = SAI_READ_CAPACITY_16; | |
1121 | cmd[13] = 12; | |
1122 | memset((void *) buffer, 0, 12); | |
1123 | } else { | |
1124 | cmd[0] = READ_CAPACITY; | |
1125 | memset((void *) &cmd[1], 0, 9); | |
1126 | memset((void *) buffer, 0, 8); | |
1127 | } | |
1128 | ||
ea73a9f2 JB |
1129 | the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, |
1130 | buffer, longrc ? 12 : 8, &sshdr, | |
1131 | SD_TIMEOUT, SD_MAX_RETRIES); | |
1da177e4 | 1132 | |
ea73a9f2 | 1133 | if (media_not_present(sdkp, &sshdr)) |
1da177e4 LT |
1134 | return; |
1135 | ||
1da177e4 | 1136 | if (the_result) |
ea73a9f2 | 1137 | sense_valid = scsi_sense_valid(&sshdr); |
1da177e4 LT |
1138 | retries--; |
1139 | ||
1140 | } while (the_result && retries); | |
1141 | ||
1142 | if (the_result && !longrc) { | |
1143 | printk(KERN_NOTICE "%s : READ CAPACITY failed.\n" | |
1144 | "%s : status=%x, message=%02x, host=%d, driver=%02x \n", | |
1145 | diskname, diskname, | |
1146 | status_byte(the_result), | |
1147 | msg_byte(the_result), | |
1148 | host_byte(the_result), | |
1149 | driver_byte(the_result)); | |
1150 | ||
1151 | if (driver_byte(the_result) & DRIVER_SENSE) | |
ea73a9f2 | 1152 | scsi_print_sense_hdr("sd", &sshdr); |
1da177e4 LT |
1153 | else |
1154 | printk("%s : sense not available. \n", diskname); | |
1155 | ||
1156 | /* Set dirty bit for removable devices if not ready - | |
1157 | * sometimes drives will not report this properly. */ | |
1158 | if (sdp->removable && | |
1159 | sense_valid && sshdr.sense_key == NOT_READY) | |
1160 | sdp->changed = 1; | |
1161 | ||
1162 | /* Either no media are present but the drive didn't tell us, | |
1163 | or they are present but the read capacity command fails */ | |
1164 | /* sdkp->media_present = 0; -- not always correct */ | |
1165 | sdkp->capacity = 0x200000; /* 1 GB - random */ | |
1166 | ||
1167 | return; | |
1168 | } else if (the_result && longrc) { | |
1169 | /* READ CAPACITY(16) has been failed */ | |
1170 | printk(KERN_NOTICE "%s : READ CAPACITY(16) failed.\n" | |
1171 | "%s : status=%x, message=%02x, host=%d, driver=%02x \n", | |
1172 | diskname, diskname, | |
1173 | status_byte(the_result), | |
1174 | msg_byte(the_result), | |
1175 | host_byte(the_result), | |
1176 | driver_byte(the_result)); | |
1177 | printk(KERN_NOTICE "%s : use 0xffffffff as device size\n", | |
1178 | diskname); | |
1179 | ||
1180 | sdkp->capacity = 1 + (sector_t) 0xffffffff; | |
1181 | goto got_data; | |
1182 | } | |
1183 | ||
1184 | if (!longrc) { | |
1185 | sector_size = (buffer[4] << 24) | | |
1186 | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7]; | |
1187 | if (buffer[0] == 0xff && buffer[1] == 0xff && | |
1188 | buffer[2] == 0xff && buffer[3] == 0xff) { | |
1189 | if(sizeof(sdkp->capacity) > 4) { | |
1190 | printk(KERN_NOTICE "%s : very big device. try to use" | |
1191 | " READ CAPACITY(16).\n", diskname); | |
1192 | longrc = 1; | |
1193 | goto repeat; | |
1194 | } | |
1195 | printk(KERN_ERR "%s: too big for this kernel. Use a " | |
1196 | "kernel compiled with support for large block " | |
1197 | "devices.\n", diskname); | |
1198 | sdkp->capacity = 0; | |
1199 | goto got_data; | |
1200 | } | |
1201 | sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) | | |
1202 | (buffer[1] << 16) | | |
1203 | (buffer[2] << 8) | | |
1204 | buffer[3]); | |
1205 | } else { | |
1206 | sdkp->capacity = 1 + (((u64)buffer[0] << 56) | | |
1207 | ((u64)buffer[1] << 48) | | |
1208 | ((u64)buffer[2] << 40) | | |
1209 | ((u64)buffer[3] << 32) | | |
1210 | ((sector_t)buffer[4] << 24) | | |
1211 | ((sector_t)buffer[5] << 16) | | |
1212 | ((sector_t)buffer[6] << 8) | | |
1213 | (sector_t)buffer[7]); | |
1214 | ||
1215 | sector_size = (buffer[8] << 24) | | |
1216 | (buffer[9] << 16) | (buffer[10] << 8) | buffer[11]; | |
1217 | } | |
1218 | ||
1219 | /* Some devices return the total number of sectors, not the | |
1220 | * highest sector number. Make the necessary adjustment. */ | |
1221 | if (sdp->fix_capacity) | |
1222 | --sdkp->capacity; | |
1223 | ||
1224 | got_data: | |
1225 | if (sector_size == 0) { | |
1226 | sector_size = 512; | |
1227 | printk(KERN_NOTICE "%s : sector size 0 reported, " | |
1228 | "assuming 512.\n", diskname); | |
1229 | } | |
1230 | ||
1231 | if (sector_size != 512 && | |
1232 | sector_size != 1024 && | |
1233 | sector_size != 2048 && | |
1234 | sector_size != 4096 && | |
1235 | sector_size != 256) { | |
1236 | printk(KERN_NOTICE "%s : unsupported sector size " | |
1237 | "%d.\n", diskname, sector_size); | |
1238 | /* | |
1239 | * The user might want to re-format the drive with | |
1240 | * a supported sectorsize. Once this happens, it | |
1241 | * would be relatively trivial to set the thing up. | |
1242 | * For this reason, we leave the thing in the table. | |
1243 | */ | |
1244 | sdkp->capacity = 0; | |
1245 | /* | |
1246 | * set a bogus sector size so the normal read/write | |
1247 | * logic in the block layer will eventually refuse any | |
1248 | * request on this device without tripping over power | |
1249 | * of two sector size assumptions | |
1250 | */ | |
1251 | sector_size = 512; | |
1252 | } | |
1253 | { | |
1254 | /* | |
1255 | * The msdos fs needs to know the hardware sector size | |
1256 | * So I have created this table. See ll_rw_blk.c | |
1257 | * Jacques Gelinas (Jacques@solucorp.qc.ca) | |
1258 | */ | |
1259 | int hard_sector = sector_size; | |
7a691bd3 | 1260 | sector_t sz = (sdkp->capacity/2) * (hard_sector/256); |
1da177e4 | 1261 | request_queue_t *queue = sdp->request_queue; |
7a691bd3 | 1262 | sector_t mb = sz; |
1da177e4 LT |
1263 | |
1264 | blk_queue_hardsect_size(queue, hard_sector); | |
1265 | /* avoid 64-bit division on 32-bit platforms */ | |
7a691bd3 | 1266 | sector_div(sz, 625); |
1da177e4 LT |
1267 | mb -= sz - 974; |
1268 | sector_div(mb, 1950); | |
1269 | ||
1270 | printk(KERN_NOTICE "SCSI device %s: " | |
1271 | "%llu %d-byte hdwr sectors (%llu MB)\n", | |
1272 | diskname, (unsigned long long)sdkp->capacity, | |
1273 | hard_sector, (unsigned long long)mb); | |
1274 | } | |
1275 | ||
1276 | /* Rescale capacity to 512-byte units */ | |
1277 | if (sector_size == 4096) | |
1278 | sdkp->capacity <<= 3; | |
1279 | else if (sector_size == 2048) | |
1280 | sdkp->capacity <<= 2; | |
1281 | else if (sector_size == 1024) | |
1282 | sdkp->capacity <<= 1; | |
1283 | else if (sector_size == 256) | |
1284 | sdkp->capacity >>= 1; | |
1285 | ||
1286 | sdkp->device->sector_size = sector_size; | |
1287 | } | |
1288 | ||
1289 | /* called with buffer of length 512 */ | |
1290 | static inline int | |
ea73a9f2 JB |
1291 | sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage, |
1292 | unsigned char *buffer, int len, struct scsi_mode_data *data, | |
1293 | struct scsi_sense_hdr *sshdr) | |
1da177e4 | 1294 | { |
ea73a9f2 | 1295 | return scsi_mode_sense(sdp, dbd, modepage, buffer, len, |
1cf72699 | 1296 | SD_TIMEOUT, SD_MAX_RETRIES, data, |
ea73a9f2 | 1297 | sshdr); |
1da177e4 LT |
1298 | } |
1299 | ||
1300 | /* | |
1301 | * read write protect setting, if possible - called only in sd_revalidate_disk() | |
1302 | * called with buffer of length 512 | |
1303 | */ | |
1304 | static void | |
1305 | sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname, | |
ea73a9f2 JB |
1306 | unsigned char *buffer) |
1307 | { | |
1da177e4 | 1308 | int res; |
ea73a9f2 | 1309 | struct scsi_device *sdp = sdkp->device; |
1da177e4 LT |
1310 | struct scsi_mode_data data; |
1311 | ||
1312 | set_disk_ro(sdkp->disk, 0); | |
ea73a9f2 | 1313 | if (sdp->skip_ms_page_3f) { |
1da177e4 LT |
1314 | printk(KERN_NOTICE "%s: assuming Write Enabled\n", diskname); |
1315 | return; | |
1316 | } | |
1317 | ||
ea73a9f2 JB |
1318 | if (sdp->use_192_bytes_for_3f) { |
1319 | res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL); | |
1da177e4 LT |
1320 | } else { |
1321 | /* | |
1322 | * First attempt: ask for all pages (0x3F), but only 4 bytes. | |
1323 | * We have to start carefully: some devices hang if we ask | |
1324 | * for more than is available. | |
1325 | */ | |
ea73a9f2 | 1326 | res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL); |
1da177e4 LT |
1327 | |
1328 | /* | |
1329 | * Second attempt: ask for page 0 When only page 0 is | |
1330 | * implemented, a request for page 3F may return Sense Key | |
1331 | * 5: Illegal Request, Sense Code 24: Invalid field in | |
1332 | * CDB. | |
1333 | */ | |
1334 | if (!scsi_status_is_good(res)) | |
ea73a9f2 | 1335 | res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL); |
1da177e4 LT |
1336 | |
1337 | /* | |
1338 | * Third attempt: ask 255 bytes, as we did earlier. | |
1339 | */ | |
1340 | if (!scsi_status_is_good(res)) | |
ea73a9f2 JB |
1341 | res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255, |
1342 | &data, NULL); | |
1da177e4 LT |
1343 | } |
1344 | ||
1345 | if (!scsi_status_is_good(res)) { | |
1346 | printk(KERN_WARNING | |
1347 | "%s: test WP failed, assume Write Enabled\n", diskname); | |
1348 | } else { | |
1349 | sdkp->write_prot = ((data.device_specific & 0x80) != 0); | |
1350 | set_disk_ro(sdkp->disk, sdkp->write_prot); | |
1351 | printk(KERN_NOTICE "%s: Write Protect is %s\n", diskname, | |
1352 | sdkp->write_prot ? "on" : "off"); | |
1353 | printk(KERN_DEBUG "%s: Mode Sense: %02x %02x %02x %02x\n", | |
1354 | diskname, buffer[0], buffer[1], buffer[2], buffer[3]); | |
1355 | } | |
1356 | } | |
1357 | ||
1358 | /* | |
1359 | * sd_read_cache_type - called only from sd_revalidate_disk() | |
1360 | * called with buffer of length 512 | |
1361 | */ | |
1362 | static void | |
1363 | sd_read_cache_type(struct scsi_disk *sdkp, char *diskname, | |
ea73a9f2 | 1364 | unsigned char *buffer) |
631e8a13 | 1365 | { |
1da177e4 | 1366 | int len = 0, res; |
ea73a9f2 | 1367 | struct scsi_device *sdp = sdkp->device; |
1da177e4 | 1368 | |
631e8a13 AV |
1369 | int dbd; |
1370 | int modepage; | |
1da177e4 LT |
1371 | struct scsi_mode_data data; |
1372 | struct scsi_sense_hdr sshdr; | |
1373 | ||
ea73a9f2 | 1374 | if (sdp->skip_ms_page_8) |
1da177e4 LT |
1375 | goto defaults; |
1376 | ||
ea73a9f2 | 1377 | if (sdp->type == TYPE_RBC) { |
631e8a13 AV |
1378 | modepage = 6; |
1379 | dbd = 8; | |
1380 | } else { | |
1381 | modepage = 8; | |
1382 | dbd = 0; | |
1383 | } | |
1384 | ||
1da177e4 | 1385 | /* cautiously ask */ |
ea73a9f2 | 1386 | res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr); |
1da177e4 LT |
1387 | |
1388 | if (!scsi_status_is_good(res)) | |
1389 | goto bad_sense; | |
1390 | ||
1391 | /* that went OK, now ask for the proper length */ | |
1392 | len = data.length; | |
1393 | ||
1394 | /* | |
1395 | * We're only interested in the first three bytes, actually. | |
1396 | * But the data cache page is defined for the first 20. | |
1397 | */ | |
1398 | if (len < 3) | |
1399 | goto bad_sense; | |
1400 | if (len > 20) | |
1401 | len = 20; | |
1402 | ||
1403 | /* Take headers and block descriptors into account */ | |
1404 | len += data.header_length + data.block_descriptor_length; | |
1405 | ||
1406 | /* Get the data */ | |
ea73a9f2 | 1407 | res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr); |
1da177e4 LT |
1408 | |
1409 | if (scsi_status_is_good(res)) { | |
1410 | const char *types[] = { | |
1411 | "write through", "none", "write back", | |
1412 | "write back, no read (daft)" | |
1413 | }; | |
1414 | int ct = 0; | |
631e8a13 | 1415 | int offset = data.header_length + data.block_descriptor_length; |
1da177e4 | 1416 | |
631e8a13 AV |
1417 | if ((buffer[offset] & 0x3f) != modepage) { |
1418 | printk(KERN_ERR "%s: got wrong page\n", diskname); | |
1419 | goto defaults; | |
1420 | } | |
1421 | ||
1422 | if (modepage == 8) { | |
1423 | sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0); | |
1424 | sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0); | |
1425 | } else { | |
1426 | sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0); | |
1427 | sdkp->RCD = 0; | |
1428 | } | |
1da177e4 LT |
1429 | |
1430 | ct = sdkp->RCD + 2*sdkp->WCE; | |
1431 | ||
1432 | printk(KERN_NOTICE "SCSI device %s: drive cache: %s\n", | |
1433 | diskname, types[ct]); | |
1434 | ||
1435 | return; | |
1436 | } | |
1437 | ||
1438 | bad_sense: | |
ea73a9f2 | 1439 | if (scsi_sense_valid(&sshdr) && |
1da177e4 LT |
1440 | sshdr.sense_key == ILLEGAL_REQUEST && |
1441 | sshdr.asc == 0x24 && sshdr.ascq == 0x0) | |
1442 | printk(KERN_NOTICE "%s: cache data unavailable\n", | |
1443 | diskname); /* Invalid field in CDB */ | |
1444 | else | |
1445 | printk(KERN_ERR "%s: asking for cache data failed\n", | |
1446 | diskname); | |
1447 | ||
1448 | defaults: | |
1449 | printk(KERN_ERR "%s: assuming drive cache: write through\n", | |
1450 | diskname); | |
1451 | sdkp->WCE = 0; | |
1452 | sdkp->RCD = 0; | |
1453 | } | |
1454 | ||
1455 | /** | |
1456 | * sd_revalidate_disk - called the first time a new disk is seen, | |
1457 | * performs disk spin up, read_capacity, etc. | |
1458 | * @disk: struct gendisk we care about | |
1459 | **/ | |
1460 | static int sd_revalidate_disk(struct gendisk *disk) | |
1461 | { | |
1462 | struct scsi_disk *sdkp = scsi_disk(disk); | |
1463 | struct scsi_device *sdp = sdkp->device; | |
1da177e4 LT |
1464 | unsigned char *buffer; |
1465 | ||
1466 | SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk->disk_name)); | |
1467 | ||
1468 | /* | |
1469 | * If the device is offline, don't try and read capacity or any | |
1470 | * of the other niceties. | |
1471 | */ | |
1472 | if (!scsi_device_online(sdp)) | |
1473 | goto out; | |
1474 | ||
1da177e4 LT |
1475 | buffer = kmalloc(512, GFP_KERNEL | __GFP_DMA); |
1476 | if (!buffer) { | |
1477 | printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation " | |
1478 | "failure.\n"); | |
ea73a9f2 | 1479 | goto out; |
1da177e4 LT |
1480 | } |
1481 | ||
1482 | /* defaults, until the device tells us otherwise */ | |
1483 | sdp->sector_size = 512; | |
1484 | sdkp->capacity = 0; | |
1485 | sdkp->media_present = 1; | |
1486 | sdkp->write_prot = 0; | |
1487 | sdkp->WCE = 0; | |
1488 | sdkp->RCD = 0; | |
1489 | ||
ea73a9f2 | 1490 | sd_spinup_disk(sdkp, disk->disk_name); |
1da177e4 LT |
1491 | |
1492 | /* | |
1493 | * Without media there is no reason to ask; moreover, some devices | |
1494 | * react badly if we do. | |
1495 | */ | |
1496 | if (sdkp->media_present) { | |
ea73a9f2 | 1497 | sd_read_capacity(sdkp, disk->disk_name, buffer); |
38d76df2 | 1498 | sd_read_write_protect_flag(sdkp, disk->disk_name, buffer); |
ea73a9f2 | 1499 | sd_read_cache_type(sdkp, disk->disk_name, buffer); |
1da177e4 LT |
1500 | } |
1501 | ||
1502 | set_capacity(disk, sdkp->capacity); | |
1503 | kfree(buffer); | |
1504 | ||
1da177e4 LT |
1505 | out: |
1506 | return 0; | |
1507 | } | |
1508 | ||
1509 | /** | |
1510 | * sd_probe - called during driver initialization and whenever a | |
1511 | * new scsi device is attached to the system. It is called once | |
1512 | * for each scsi device (not just disks) present. | |
1513 | * @dev: pointer to device object | |
1514 | * | |
1515 | * Returns 0 if successful (or not interested in this scsi device | |
1516 | * (e.g. scanner)); 1 when there is an error. | |
1517 | * | |
1518 | * Note: this function is invoked from the scsi mid-level. | |
1519 | * This function sets up the mapping between a given | |
1520 | * <host,channel,id,lun> (found in sdp) and new device name | |
1521 | * (e.g. /dev/sda). More precisely it is the block device major | |
1522 | * and minor number that is chosen here. | |
1523 | * | |
1524 | * Assume sd_attach is not re-entrant (for time being) | |
1525 | * Also think about sd_attach() and sd_remove() running coincidentally. | |
1526 | **/ | |
1527 | static int sd_probe(struct device *dev) | |
1528 | { | |
1529 | struct scsi_device *sdp = to_scsi_device(dev); | |
1530 | struct scsi_disk *sdkp; | |
1531 | struct gendisk *gd; | |
1532 | u32 index; | |
1533 | int error; | |
1534 | ||
1535 | error = -ENODEV; | |
631e8a13 | 1536 | if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC) |
1da177e4 LT |
1537 | goto out; |
1538 | ||
9ccfc756 JB |
1539 | SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp, |
1540 | "sd_attach\n")); | |
1da177e4 LT |
1541 | |
1542 | error = -ENOMEM; | |
1543 | sdkp = kmalloc(sizeof(*sdkp), GFP_KERNEL); | |
1544 | if (!sdkp) | |
1545 | goto out; | |
1546 | ||
1547 | memset (sdkp, 0, sizeof(*sdkp)); | |
1548 | kref_init(&sdkp->kref); | |
1549 | ||
1550 | gd = alloc_disk(16); | |
1551 | if (!gd) | |
1552 | goto out_free; | |
1553 | ||
1554 | if (!idr_pre_get(&sd_index_idr, GFP_KERNEL)) | |
1555 | goto out_put; | |
1556 | ||
1557 | spin_lock(&sd_index_lock); | |
1558 | error = idr_get_new(&sd_index_idr, NULL, &index); | |
1559 | spin_unlock(&sd_index_lock); | |
1560 | ||
1561 | if (index >= SD_MAX_DISKS) | |
1562 | error = -EBUSY; | |
1563 | if (error) | |
1564 | goto out_put; | |
1565 | ||
39b7f1e2 | 1566 | get_device(&sdp->sdev_gendev); |
1da177e4 LT |
1567 | sdkp->device = sdp; |
1568 | sdkp->driver = &sd_template; | |
1569 | sdkp->disk = gd; | |
1570 | sdkp->index = index; | |
1571 | sdkp->openers = 0; | |
1572 | ||
1573 | if (!sdp->timeout) { | |
631e8a13 | 1574 | if (sdp->type != TYPE_MOD) |
1da177e4 LT |
1575 | sdp->timeout = SD_TIMEOUT; |
1576 | else | |
1577 | sdp->timeout = SD_MOD_TIMEOUT; | |
1578 | } | |
1579 | ||
1580 | gd->major = sd_major((index & 0xf0) >> 4); | |
1581 | gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00); | |
1582 | gd->minors = 16; | |
1583 | gd->fops = &sd_fops; | |
1584 | ||
1585 | if (index < 26) { | |
1586 | sprintf(gd->disk_name, "sd%c", 'a' + index % 26); | |
1587 | } else if (index < (26 + 1) * 26) { | |
1588 | sprintf(gd->disk_name, "sd%c%c", | |
1589 | 'a' + index / 26 - 1,'a' + index % 26); | |
1590 | } else { | |
1591 | const unsigned int m1 = (index / 26 - 1) / 26 - 1; | |
1592 | const unsigned int m2 = (index / 26 - 1) % 26; | |
1593 | const unsigned int m3 = index % 26; | |
1594 | sprintf(gd->disk_name, "sd%c%c%c", | |
1595 | 'a' + m1, 'a' + m2, 'a' + m3); | |
1596 | } | |
1597 | ||
1598 | strcpy(gd->devfs_name, sdp->devfs_name); | |
1599 | ||
1600 | gd->private_data = &sdkp->driver; | |
1601 | ||
1602 | sd_revalidate_disk(gd); | |
1603 | ||
1604 | gd->driverfs_dev = &sdp->sdev_gendev; | |
1605 | gd->flags = GENHD_FL_DRIVERFS; | |
1606 | if (sdp->removable) | |
1607 | gd->flags |= GENHD_FL_REMOVABLE; | |
1608 | gd->queue = sdkp->device->request_queue; | |
1609 | ||
1610 | dev_set_drvdata(dev, sdkp); | |
1611 | add_disk(gd); | |
1612 | ||
9ccfc756 JB |
1613 | sdev_printk(KERN_NOTICE, sdp, "Attached scsi %sdisk %s\n", |
1614 | sdp->removable ? "removable " : "", gd->disk_name); | |
1da177e4 LT |
1615 | |
1616 | return 0; | |
1617 | ||
1618 | out_put: | |
1619 | put_disk(gd); | |
1620 | out_free: | |
1621 | kfree(sdkp); | |
1622 | out: | |
1623 | return error; | |
1624 | } | |
1625 | ||
1626 | /** | |
1627 | * sd_remove - called whenever a scsi disk (previously recognized by | |
1628 | * sd_probe) is detached from the system. It is called (potentially | |
1629 | * multiple times) during sd module unload. | |
1630 | * @sdp: pointer to mid level scsi device object | |
1631 | * | |
1632 | * Note: this function is invoked from the scsi mid-level. | |
1633 | * This function potentially frees up a device name (e.g. /dev/sdc) | |
1634 | * that could be re-used by a subsequent sd_probe(). | |
1635 | * This function is not called when the built-in sd driver is "exit-ed". | |
1636 | **/ | |
1637 | static int sd_remove(struct device *dev) | |
1638 | { | |
1639 | struct scsi_disk *sdkp = dev_get_drvdata(dev); | |
1640 | ||
1641 | del_gendisk(sdkp->disk); | |
1642 | sd_shutdown(dev); | |
39b7f1e2 | 1643 | |
1da177e4 | 1644 | down(&sd_ref_sem); |
39b7f1e2 | 1645 | dev_set_drvdata(dev, NULL); |
1da177e4 LT |
1646 | kref_put(&sdkp->kref, scsi_disk_release); |
1647 | up(&sd_ref_sem); | |
1648 | ||
1649 | return 0; | |
1650 | } | |
1651 | ||
1652 | /** | |
1653 | * scsi_disk_release - Called to free the scsi_disk structure | |
1654 | * @kref: pointer to embedded kref | |
1655 | * | |
1656 | * sd_ref_sem must be held entering this routine. Because it is | |
1657 | * called on last put, you should always use the scsi_disk_get() | |
1658 | * scsi_disk_put() helpers which manipulate the semaphore directly | |
1659 | * and never do a direct kref_put(). | |
1660 | **/ | |
1661 | static void scsi_disk_release(struct kref *kref) | |
1662 | { | |
1663 | struct scsi_disk *sdkp = to_scsi_disk(kref); | |
1664 | struct gendisk *disk = sdkp->disk; | |
1665 | ||
1666 | spin_lock(&sd_index_lock); | |
1667 | idr_remove(&sd_index_idr, sdkp->index); | |
1668 | spin_unlock(&sd_index_lock); | |
1669 | ||
1670 | disk->private_data = NULL; | |
1da177e4 | 1671 | put_disk(disk); |
39b7f1e2 | 1672 | put_device(&sdkp->device->sdev_gendev); |
1da177e4 LT |
1673 | |
1674 | kfree(sdkp); | |
1675 | } | |
1676 | ||
1677 | /* | |
1678 | * Send a SYNCHRONIZE CACHE instruction down to the device through | |
1679 | * the normal SCSI command structure. Wait for the command to | |
1680 | * complete. | |
1681 | */ | |
1682 | static void sd_shutdown(struct device *dev) | |
1683 | { | |
1684 | struct scsi_device *sdp = to_scsi_device(dev); | |
39b7f1e2 | 1685 | struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); |
1da177e4 LT |
1686 | |
1687 | if (!sdkp) | |
1688 | return; /* this can happen */ | |
1689 | ||
39b7f1e2 AS |
1690 | if (sdkp->WCE) { |
1691 | printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: \n", | |
1692 | sdkp->disk->disk_name); | |
1693 | sd_sync_cache(sdp); | |
1694 | } | |
1695 | scsi_disk_put(sdkp); | |
1696 | } | |
1da177e4 LT |
1697 | |
1698 | /** | |
1699 | * init_sd - entry point for this driver (both when built in or when | |
1700 | * a module). | |
1701 | * | |
1702 | * Note: this function registers this driver with the scsi mid-level. | |
1703 | **/ | |
1704 | static int __init init_sd(void) | |
1705 | { | |
1706 | int majors = 0, i; | |
1707 | ||
1708 | SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n")); | |
1709 | ||
1710 | for (i = 0; i < SD_MAJORS; i++) | |
1711 | if (register_blkdev(sd_major(i), "sd") == 0) | |
1712 | majors++; | |
1713 | ||
1714 | if (!majors) | |
1715 | return -ENODEV; | |
1716 | ||
1717 | return scsi_register_driver(&sd_template.gendrv); | |
1718 | } | |
1719 | ||
1720 | /** | |
1721 | * exit_sd - exit point for this driver (when it is a module). | |
1722 | * | |
1723 | * Note: this function unregisters this driver from the scsi mid-level. | |
1724 | **/ | |
1725 | static void __exit exit_sd(void) | |
1726 | { | |
1727 | int i; | |
1728 | ||
1729 | SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n")); | |
1730 | ||
1731 | scsi_unregister_driver(&sd_template.gendrv); | |
1732 | for (i = 0; i < SD_MAJORS; i++) | |
1733 | unregister_blkdev(sd_major(i), "sd"); | |
1734 | } | |
1735 | ||
1736 | MODULE_LICENSE("GPL"); | |
1737 | MODULE_AUTHOR("Eric Youngdale"); | |
1738 | MODULE_DESCRIPTION("SCSI disk (sd) driver"); | |
1739 | ||
1740 | module_init(init_sd); | |
1741 | module_exit(exit_sd); |