blob: 1aebdf1a4f5866a4c6d0f430274e66d1ce3e6022 [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
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +010028static void ide_disk_release(struct device *);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020029
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
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +010040 get_device(&idkp->dev);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020041 }
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);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +010051 put_device(&idkp->dev);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020052 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);
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +010069 device_del(&idkp->dev);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020070 del_gendisk(g);
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +020071 drive->disk_ops->flush(drive);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020072
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +010073 mutex_lock(&ide_disk_ref_mutex);
74 put_device(&idkp->dev);
75 mutex_unlock(&ide_disk_ref_mutex);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020076}
77
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +010078static void ide_disk_release(struct device *dev)
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020079{
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +010080 struct ide_disk_obj *idkp = to_ide_drv(dev, ide_disk_obj);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020081 ide_drive_t *drive = idkp->drive;
82 struct gendisk *g = idkp->disk;
83
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +020084 drive->disk_ops = NULL;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +020085 drive->driver_data = NULL;
86 g->private_data = NULL;
87 put_disk(g);
88 kfree(idkp);
89}
90
91/*
92 * On HPA drives the capacity needs to be
93 * reinitilized on resume otherwise the disk
94 * can not be used and a hard reset is required
95 */
96static void ide_gd_resume(ide_drive_t *drive)
97{
98 if (ata_id_hpa_enabled(drive->id))
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +020099 (void)drive->disk_ops->get_capacity(drive);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200100}
101
102static void ide_gd_shutdown(ide_drive_t *drive)
103{
104#ifdef CONFIG_ALPHA
105 /* On Alpha, halt(8) doesn't actually turn the machine off,
106 it puts you into the sort of firmware monitor. Typically,
107 it's used to boot another kernel image, so it's not much
108 different from reboot(8). Therefore, we don't need to
109 spin down the disk in this case, especially since Alpha
110 firmware doesn't handle disks in standby mode properly.
111 On the other hand, it's reasonably safe to turn the power
112 off when the shutdown process reaches the firmware prompt,
113 as the firmware initialization takes rather long time -
114 at least 10 seconds, which should be sufficient for
115 the disk to expire its write cache. */
116 if (system_state != SYSTEM_POWER_OFF) {
117#else
118 if (system_state == SYSTEM_RESTART) {
119#endif
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200120 drive->disk_ops->flush(drive);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200121 return;
122 }
123
124 printk(KERN_INFO "Shutdown: %s\n", drive->name);
125
126 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
127}
128
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200129#ifdef CONFIG_IDE_PROC_FS
130static ide_proc_entry_t *ide_disk_proc_entries(ide_drive_t *drive)
131{
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200132 return (drive->media == ide_disk) ? ide_disk_proc : ide_floppy_proc;
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200133}
134
135static const struct ide_proc_devset *ide_disk_proc_devsets(ide_drive_t *drive)
136{
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200137 return (drive->media == ide_disk) ? ide_disk_settings
138 : ide_floppy_settings;
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200139}
140#endif
141
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200142static ide_startstop_t ide_gd_do_request(ide_drive_t *drive,
143 struct request *rq, sector_t sector)
144{
145 return drive->disk_ops->do_request(drive, rq, sector);
146}
147
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100148static struct ide_driver ide_gd_driver = {
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200149 .gen_driver = {
150 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200151 .name = "ide-gd",
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200152 .bus = &ide_bus_type,
153 },
154 .probe = ide_gd_probe,
155 .remove = ide_gd_remove,
156 .resume = ide_gd_resume,
157 .shutdown = ide_gd_shutdown,
158 .version = IDE_GD_VERSION,
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200159 .do_request = ide_gd_do_request,
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200160#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +0200161 .proc_entries = ide_disk_proc_entries,
162 .proc_devsets = ide_disk_proc_devsets,
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200163#endif
164};
165
Al Virob2f21e02008-10-16 10:34:00 -0400166static int ide_gd_open(struct block_device *bdev, fmode_t mode)
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200167{
Al Virob2f21e02008-10-16 10:34:00 -0400168 struct gendisk *disk = bdev->bd_disk;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200169 struct ide_disk_obj *idkp;
170 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200171 int ret = 0;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200172
173 idkp = ide_disk_get(disk);
174 if (idkp == NULL)
175 return -ENXIO;
176
177 drive = idkp->drive;
178
Borislav Petkov088b1b82009-01-02 13:34:47 +0100179 ide_debug_log(IDE_DBG_FUNC, "enter");
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200180
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200181 idkp->openers++;
182
183 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200184 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
185 /* Just in case */
186
187 ret = drive->disk_ops->init_media(drive, disk);
188
189 /*
190 * Allow O_NDELAY to open a drive without a disk, or with an
191 * unreadable disk, so that we can get the format capacity
192 * of the drive or begin the format - Sam
193 */
Al Virob2f21e02008-10-16 10:34:00 -0400194 if (ret && (mode & FMODE_NDELAY) == 0) {
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200195 ret = -EIO;
196 goto out_put_idkp;
197 }
198
Al Virob2f21e02008-10-16 10:34:00 -0400199 if ((drive->dev_flags & IDE_DFLAG_WP) && (mode & FMODE_WRITE)) {
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200200 ret = -EROFS;
201 goto out_put_idkp;
202 }
203
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200204 /*
205 * Ignore the return code from door_lock,
206 * since the open() has already succeeded,
207 * and the door_lock is irrelevant at this point.
208 */
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200209 drive->disk_ops->set_doorlock(drive, disk, 1);
Bartlomiej Zolnierkiewiczcedd1202008-10-17 18:09:12 +0200210 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
Al Virob2f21e02008-10-16 10:34:00 -0400211 check_disk_change(bdev);
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200212 } else if (drive->dev_flags & IDE_DFLAG_FORMAT_IN_PROGRESS) {
213 ret = -EBUSY;
214 goto out_put_idkp;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200215 }
216 return 0;
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200217
218out_put_idkp:
219 idkp->openers--;
220 ide_disk_put(idkp);
221 return ret;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200222}
223
Al Virob2f21e02008-10-16 10:34:00 -0400224static int ide_gd_release(struct gendisk *disk, fmode_t mode)
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200225{
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200226 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
227 ide_drive_t *drive = idkp->drive;
228
Borislav Petkov088b1b82009-01-02 13:34:47 +0100229 ide_debug_log(IDE_DBG_FUNC, "enter");
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200230
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200231 if (idkp->openers == 1)
232 drive->disk_ops->flush(drive);
233
234 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
235 drive->disk_ops->set_doorlock(drive, disk, 0);
236 drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
237 }
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200238
239 idkp->openers--;
240
241 ide_disk_put(idkp);
242
243 return 0;
244}
245
246static int ide_gd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
247{
248 struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
249 ide_drive_t *drive = idkp->drive;
250
251 geo->heads = drive->bios_head;
252 geo->sectors = drive->bios_sect;
253 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
254 return 0;
255}
256
257static int ide_gd_media_changed(struct gendisk *disk)
258{
259 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
260 ide_drive_t *drive = idkp->drive;
Bartlomiej Zolnierkiewiczcedd1202008-10-17 18:09:12 +0200261 int ret;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200262
263 /* do not scan partitions twice if this is a removable device */
264 if (drive->dev_flags & IDE_DFLAG_ATTACH) {
265 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
266 return 0;
267 }
268
Bartlomiej Zolnierkiewiczcedd1202008-10-17 18:09:12 +0200269 ret = !!(drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED);
270 drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;
271
272 return ret;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200273}
274
275static int ide_gd_revalidate_disk(struct gendisk *disk)
276{
277 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
Borislav Petkov52ebb432008-11-02 21:40:10 +0100278 ide_drive_t *drive = idkp->drive;
279
280 if (ide_gd_media_changed(disk))
281 drive->disk_ops->get_capacity(drive);
282
283 set_capacity(disk, ide_gd_capacity(drive));
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200284 return 0;
285}
286
Al Virob2f21e02008-10-16 10:34:00 -0400287static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode,
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200288 unsigned int cmd, unsigned long arg)
289{
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200290 struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
291 ide_drive_t *drive = idkp->drive;
292
Al Virob2f21e02008-10-16 10:34:00 -0400293 return drive->disk_ops->ioctl(drive, bdev, mode, cmd, arg);
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200294}
295
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200296static struct block_device_operations ide_gd_ops = {
297 .owner = THIS_MODULE,
Al Virob2f21e02008-10-16 10:34:00 -0400298 .open = ide_gd_open,
299 .release = ide_gd_release,
300 .locked_ioctl = ide_gd_ioctl,
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200301 .getgeo = ide_gd_getgeo,
302 .media_changed = ide_gd_media_changed,
303 .revalidate_disk = ide_gd_revalidate_disk
304};
305
306static int ide_gd_probe(ide_drive_t *drive)
307{
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200308 const struct ide_disk_ops *disk_ops = NULL;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200309 struct ide_disk_obj *idkp;
310 struct gendisk *g;
311
312 /* strstr("foo", "") is non-NULL */
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200313 if (!strstr("ide-gd", drive->driver_req))
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200314 goto failed;
315
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200316#ifdef CONFIG_IDE_GD_ATA
317 if (drive->media == ide_disk)
318 disk_ops = &ide_ata_disk_ops;
319#endif
320#ifdef CONFIG_IDE_GD_ATAPI
321 if (drive->media == ide_floppy)
322 disk_ops = &ide_atapi_disk_ops;
323#endif
324 if (disk_ops == NULL)
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200325 goto failed;
326
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200327 if (disk_ops->check(drive, DRV_NAME) == 0) {
328 printk(KERN_ERR PFX "%s: not supported by this driver\n",
329 drive->name);
330 goto failed;
331 }
332
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200333 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200334 if (!idkp) {
335 printk(KERN_ERR PFX "%s: can't allocate a disk structure\n",
336 drive->name);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200337 goto failed;
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200338 }
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200339
340 g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
341 if (!g)
342 goto out_free_idkp;
343
344 ide_init_disk(g, drive);
345
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100346 idkp->dev.parent = &drive->gendev;
347 idkp->dev.release = ide_disk_release;
348 dev_set_name(&idkp->dev, dev_name(&drive->gendev));
349
350 if (device_register(&idkp->dev))
351 goto out_free_disk;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200352
353 idkp->drive = drive;
354 idkp->driver = &ide_gd_driver;
355 idkp->disk = g;
356
357 g->private_data = &idkp->driver;
358
359 drive->driver_data = idkp;
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200360 drive->debug_mask = debug_mask;
361 drive->disk_ops = disk_ops;
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200362
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200363 disk_ops->setup(drive);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200364
365 set_capacity(g, ide_gd_capacity(drive));
366
367 g->minors = IDE_DISK_MINORS;
368 g->driverfs_dev = &drive->gendev;
369 g->flags |= GENHD_FL_EXT_DEVT;
370 if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
371 g->flags = GENHD_FL_REMOVABLE;
372 g->fops = &ide_gd_ops;
373 add_disk(g);
374 return 0;
375
Bartlomiej Zolnierkiewicz8fed4362009-02-25 20:28:24 +0100376out_free_disk:
377 put_disk(g);
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200378out_free_idkp:
379 kfree(idkp);
380failed:
381 return -ENODEV;
382}
383
384static int __init ide_gd_init(void)
385{
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200386 printk(KERN_INFO DRV_NAME " driver " IDE_GD_VERSION "\n");
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200387 return driver_register(&ide_gd_driver.gen_driver);
388}
389
390static void __exit ide_gd_exit(void)
391{
392 driver_unregister(&ide_gd_driver.gen_driver);
393}
394
395MODULE_ALIAS("ide:*m-disk*");
396MODULE_ALIAS("ide-disk");
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200397MODULE_ALIAS("ide:*m-floppy*");
398MODULE_ALIAS("ide-floppy");
Bartlomiej Zolnierkiewicz5fef0e52008-10-17 18:09:12 +0200399module_init(ide_gd_init);
400module_exit(ide_gd_exit);
401MODULE_LICENSE("GPL");
Bartlomiej Zolnierkiewicz806f80a2008-10-17 18:09:14 +0200402MODULE_DESCRIPTION("generic ATA/ATAPI disk driver");