blob: b8078b3231f7f9de114affcd3b4c5ced5e436728 [file] [log] [blame]
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +02001#include <linux/module.h>
2#include <linux/types.h>
3#include <linux/string.h>
4#include <linux/kernel.h>
5#include <linux/errno.h>
6#include <linux/genhd.h>
7#include <linux/mutex.h>
8#include <linux/ide.h>
9#include <linux/hdreg.h>
10
11#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
12#define IDE_DISK_MINORS (1 << PARTN_BITS)
13#else
14#define IDE_DISK_MINORS 0
15#endif
16
17#include "ide-disk.h"
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +020018#include "ide-floppy.h"
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020019
20#define IDE_GD_VERSION "1.18"
21
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +020022/* module parameters */
23static unsigned long debug_mask;
24module_param(debug_mask, ulong, 0644);
25
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020026static DEFINE_MUTEX(ide_disk_ref_mutex);
27
28static void ide_disk_release(struct kref *);
29
30static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
31{
32 struct ide_disk_obj *idkp = NULL;
33
34 mutex_lock(&ide_disk_ref_mutex);
35 idkp = ide_drv_g(disk, ide_disk_obj);
36 if (idkp) {
37 if (ide_device_get(idkp->drive))
38 idkp = NULL;
39 else
40 kref_get(&idkp->kref);
41 }
42 mutex_unlock(&ide_disk_ref_mutex);
43 return idkp;
44}
45
46static void ide_disk_put(struct ide_disk_obj *idkp)
47{
48 ide_drive_t *drive = idkp->drive;
49
50 mutex_lock(&ide_disk_ref_mutex);
51 kref_put(&idkp->kref, ide_disk_release);
52 ide_device_put(drive);
53 mutex_unlock(&ide_disk_ref_mutex);
54}
55
56sector_t ide_gd_capacity(ide_drive_t *drive)
57{
58 return drive->capacity64;
59}
60
61static int ide_gd_probe(ide_drive_t *);
62
63static void ide_gd_remove(ide_drive_t *drive)
64{
65 struct ide_disk_obj *idkp = drive->driver_data;
66 struct gendisk *g = idkp->disk;
67
68 ide_proc_unregister_driver(drive, idkp->driver);
69
70 del_gendisk(g);
71
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +020072 drive->disk_ops->flush(drive);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020073
74 ide_disk_put(idkp);
75}
76
77static void ide_disk_release(struct kref *kref)
78{
79 struct ide_disk_obj *idkp = to_ide_drv(kref, ide_disk_obj);
80 ide_drive_t *drive = idkp->drive;
81 struct gendisk *g = idkp->disk;
82
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +020083 drive->disk_ops = NULL;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020084 drive->driver_data = NULL;
85 g->private_data = NULL;
86 put_disk(g);
87 kfree(idkp);
88}
89
90/*
91 * On HPA drives the capacity needs to be
92 * reinitilized on resume otherwise the disk
93 * can not be used and a hard reset is required
94 */
95static void ide_gd_resume(ide_drive_t *drive)
96{
97 if (ata_id_hpa_enabled(drive->id))
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +020098 (void)drive->disk_ops->get_capacity(drive);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020099}
100
101static void ide_gd_shutdown(ide_drive_t *drive)
102{
103#ifdef CONFIG_ALPHA
104 /* On Alpha, halt(8) doesn't actually turn the machine off,
105 it puts you into the sort of firmware monitor. Typically,
106 it's used to boot another kernel image, so it's not much
107 different from reboot(8). Therefore, we don't need to
108 spin down the disk in this case, especially since Alpha
109 firmware doesn't handle disks in standby mode properly.
110 On the other hand, it's reasonably safe to turn the power
111 off when the shutdown process reaches the firmware prompt,
112 as the firmware initialization takes rather long time -
113 at least 10 seconds, which should be sufficient for
114 the disk to expire its write cache. */
115 if (system_state != SYSTEM_POWER_OFF) {
116#else
117 if (system_state == SYSTEM_RESTART) {
118#endif
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200119 drive->disk_ops->flush(drive);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200120 return;
121 }
122
123 printk(KERN_INFO "Shutdown: %s\n", drive->name);
124
125 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
126}
127
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200128#ifdef CONFIG_IDE_PROC_FS
129static ide_proc_entry_t *ide_disk_proc_entries(ide_drive_t *drive)
130{
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200131 return (drive->media == ide_disk) ? ide_disk_proc : ide_floppy_proc;
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200132}
133
134static const struct ide_proc_devset *ide_disk_proc_devsets(ide_drive_t *drive)
135{
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200136 return (drive->media == ide_disk) ? ide_disk_settings
137 : ide_floppy_settings;
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200138}
139#endif
140
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200141static ide_startstop_t ide_gd_do_request(ide_drive_t *drive,
142 struct request *rq, sector_t sector)
143{
144 return drive->disk_ops->do_request(drive, rq, sector);
145}
146
147static int ide_gd_end_request(ide_drive_t *drive, int uptodate, int nrsecs)
148{
149 return drive->disk_ops->end_request(drive, uptodate, nrsecs);
150}
151
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200152static ide_driver_t ide_gd_driver = {
153 .gen_driver = {
154 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200155 .name = "ide-gd",
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200156 .bus = &ide_bus_type,
157 },
158 .probe = ide_gd_probe,
159 .remove = ide_gd_remove,
160 .resume = ide_gd_resume,
161 .shutdown = ide_gd_shutdown,
162 .version = IDE_GD_VERSION,
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200163 .do_request = ide_gd_do_request,
164 .end_request = ide_gd_end_request,
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200165 .error = __ide_error,
166#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200167 .proc_entries = ide_disk_proc_entries,
168 .proc_devsets = ide_disk_proc_devsets,
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200169#endif
170};
171
Al Virob2f21e02008-10-16 10:34:00 -0400172static int ide_gd_open(struct block_device *bdev, fmode_t mode)
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200173{
Al Virob2f21e02008-10-16 10:34:00 -0400174 struct gendisk *disk = bdev->bd_disk;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200175 struct ide_disk_obj *idkp;
176 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200177 int ret = 0;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200178
179 idkp = ide_disk_get(disk);
180 if (idkp == NULL)
181 return -ENXIO;
182
183 drive = idkp->drive;
184
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200185 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
186
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200187 idkp->openers++;
188
189 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200190 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
191 /* Just in case */
192
193 ret = drive->disk_ops->init_media(drive, disk);
194
195 /*
196 * Allow O_NDELAY to open a drive without a disk, or with an
197 * unreadable disk, so that we can get the format capacity
198 * of the drive or begin the format - Sam
199 */
Al Virob2f21e02008-10-16 10:34:00 -0400200 if (ret && (mode & FMODE_NDELAY) == 0) {
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200201 ret = -EIO;
202 goto out_put_idkp;
203 }
204
Al Virob2f21e02008-10-16 10:34:00 -0400205 if ((drive->dev_flags & IDE_DFLAG_WP) && (mode & FMODE_WRITE)) {
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200206 ret = -EROFS;
207 goto out_put_idkp;
208 }
209
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200210 /*
211 * Ignore the return code from door_lock,
212 * since the open() has already succeeded,
213 * and the door_lock is irrelevant at this point.
214 */
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200215 drive->disk_ops->set_doorlock(drive, disk, 1);
Bartlomiej Zolnierkiewiczcedd1202008-10-17 18:09:12 +0200216 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
Al Virob2f21e02008-10-16 10:34:00 -0400217 check_disk_change(bdev);
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200218 } else if (drive->dev_flags & IDE_DFLAG_FORMAT_IN_PROGRESS) {
219 ret = -EBUSY;
220 goto out_put_idkp;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200221 }
222 return 0;
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200223
224out_put_idkp:
225 idkp->openers--;
226 ide_disk_put(idkp);
227 return ret;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200228}
229
Al Virob2f21e02008-10-16 10:34:00 -0400230static int ide_gd_release(struct gendisk *disk, fmode_t mode)
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200231{
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200232 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
233 ide_drive_t *drive = idkp->drive;
234
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200235 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200236
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200237 if (idkp->openers == 1)
238 drive->disk_ops->flush(drive);
239
240 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
241 drive->disk_ops->set_doorlock(drive, disk, 0);
242 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
243 }
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200244
245 idkp->openers--;
246
247 ide_disk_put(idkp);
248
249 return 0;
250}
251
252static int ide_gd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
253{
254 struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
255 ide_drive_t *drive = idkp->drive;
256
257 geo->heads = drive->bios_head;
258 geo->sectors = drive->bios_sect;
259 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
260 return 0;
261}
262
263static int ide_gd_media_changed(struct gendisk *disk)
264{
265 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
266 ide_drive_t *drive = idkp->drive;
Bartlomiej Zolnierkiewiczcedd1202008-10-17 18:09:12 +0200267 int ret;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200268
269 /* do not scan partitions twice if this is a removable device */
270 if (drive->dev_flags & IDE_DFLAG_ATTACH) {
271 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
272 return 0;
273 }
274
Bartlomiej Zolnierkiewiczcedd1202008-10-17 18:09:12 +0200275 ret = !!(drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED);
276 drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;
277
278 return ret;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200279}
280
281static int ide_gd_revalidate_disk(struct gendisk *disk)
282{
283 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
Borislav Petkov52ebb432008-11-02 21:40:10 +0100284 ide_drive_t *drive = idkp->drive;
285
286 if (ide_gd_media_changed(disk))
287 drive->disk_ops->get_capacity(drive);
288
289 set_capacity(disk, ide_gd_capacity(drive));
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200290 return 0;
291}
292
Al Virob2f21e02008-10-16 10:34:00 -0400293static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode,
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200294 unsigned int cmd, unsigned long arg)
295{
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200296 struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
297 ide_drive_t *drive = idkp->drive;
298
Al Virob2f21e02008-10-16 10:34:00 -0400299 return drive->disk_ops->ioctl(drive, bdev, mode, cmd, arg);
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200300}
301
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200302static struct block_device_operations ide_gd_ops = {
303 .owner = THIS_MODULE,
Al Virob2f21e02008-10-16 10:34:00 -0400304 .open = ide_gd_open,
305 .release = ide_gd_release,
306 .locked_ioctl = ide_gd_ioctl,
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200307 .getgeo = ide_gd_getgeo,
308 .media_changed = ide_gd_media_changed,
309 .revalidate_disk = ide_gd_revalidate_disk
310};
311
312static int ide_gd_probe(ide_drive_t *drive)
313{
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200314 const struct ide_disk_ops *disk_ops = NULL;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200315 struct ide_disk_obj *idkp;
316 struct gendisk *g;
317
318 /* strstr("foo", "") is non-NULL */
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200319 if (!strstr("ide-gd", drive->driver_req))
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200320 goto failed;
321
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200322#ifdef CONFIG_IDE_GD_ATA
323 if (drive->media == ide_disk)
324 disk_ops = &ide_ata_disk_ops;
325#endif
326#ifdef CONFIG_IDE_GD_ATAPI
327 if (drive->media == ide_floppy)
328 disk_ops = &ide_atapi_disk_ops;
329#endif
330 if (disk_ops == NULL)
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200331 goto failed;
332
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200333 if (disk_ops->check(drive, DRV_NAME) == 0) {
334 printk(KERN_ERR PFX "%s: not supported by this driver\n",
335 drive->name);
336 goto failed;
337 }
338
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200339 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200340 if (!idkp) {
341 printk(KERN_ERR PFX "%s: can't allocate a disk structure\n",
342 drive->name);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200343 goto failed;
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200344 }
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200345
346 g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
347 if (!g)
348 goto out_free_idkp;
349
350 ide_init_disk(g, drive);
351
352 kref_init(&idkp->kref);
353
354 idkp->drive = drive;
355 idkp->driver = &ide_gd_driver;
356 idkp->disk = g;
357
358 g->private_data = &idkp->driver;
359
360 drive->driver_data = idkp;
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200361 drive->debug_mask = debug_mask;
362 drive->disk_ops = disk_ops;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200363
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200364 disk_ops->setup(drive);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200365
366 set_capacity(g, ide_gd_capacity(drive));
367
368 g->minors = IDE_DISK_MINORS;
369 g->driverfs_dev = &drive->gendev;
370 g->flags |= GENHD_FL_EXT_DEVT;
371 if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
372 g->flags = GENHD_FL_REMOVABLE;
373 g->fops = &ide_gd_ops;
374 add_disk(g);
375 return 0;
376
377out_free_idkp:
378 kfree(idkp);
379failed:
380 return -ENODEV;
381}
382
383static int __init ide_gd_init(void)
384{
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200385 printk(KERN_INFO DRV_NAME " driver " IDE_GD_VERSION "\n");
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200386 return driver_register(&ide_gd_driver.gen_driver);
387}
388
389static void __exit ide_gd_exit(void)
390{
391 driver_unregister(&ide_gd_driver.gen_driver);
392}
393
394MODULE_ALIAS("ide:*m-disk*");
395MODULE_ALIAS("ide-disk");
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200396MODULE_ALIAS("ide:*m-floppy*");
397MODULE_ALIAS("ide-floppy");
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200398module_init(ide_gd_init);
399module_exit(ide_gd_exit);
400MODULE_LICENSE("GPL");
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200401MODULE_DESCRIPTION("generic ATA/ATAPI disk driver");