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