ide-gd.c 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
#include <linux/module.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/genhd.h>
#include <linux/mutex.h>
#include <linux/ide.h>
#include <linux/hdreg.h>
10
#include <linux/dmi.h>
11
#include <linux/slab.h>
12 13 14 15 16 17 18 19

#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
#define IDE_DISK_MINORS		(1 << PARTN_BITS)
#else
#define IDE_DISK_MINORS		0
#endif

#include "ide-disk.h"
20
#include "ide-floppy.h"
21 22 23

#define IDE_GD_VERSION	"1.18"

24
/* module parameters */
25
static DEFINE_MUTEX(ide_gd_mutex);
26 27 28
static unsigned long debug_mask;
module_param(debug_mask, ulong, 0644);

29 30
static DEFINE_MUTEX(ide_disk_ref_mutex);

31
static void ide_disk_release(struct device *);
32 33 34 35 36 37 38 39 40 41 42

static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
{
	struct ide_disk_obj *idkp = NULL;

	mutex_lock(&ide_disk_ref_mutex);
	idkp = ide_drv_g(disk, ide_disk_obj);
	if (idkp) {
		if (ide_device_get(idkp->drive))
			idkp = NULL;
		else
43
			get_device(&idkp->dev);
44 45 46 47 48 49 50 51 52 53
	}
	mutex_unlock(&ide_disk_ref_mutex);
	return idkp;
}

static void ide_disk_put(struct ide_disk_obj *idkp)
{
	ide_drive_t *drive = idkp->drive;

	mutex_lock(&ide_disk_ref_mutex);
54
	put_device(&idkp->dev);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	ide_device_put(drive);
	mutex_unlock(&ide_disk_ref_mutex);
}

sector_t ide_gd_capacity(ide_drive_t *drive)
{
	return drive->capacity64;
}

static int ide_gd_probe(ide_drive_t *);

static void ide_gd_remove(ide_drive_t *drive)
{
	struct ide_disk_obj *idkp = drive->driver_data;
	struct gendisk *g = idkp->disk;

	ide_proc_unregister_driver(drive, idkp->driver);
72
	device_del(&idkp->dev);
73
	del_gendisk(g);
74
	drive->disk_ops->flush(drive);
75

76 77 78
	mutex_lock(&ide_disk_ref_mutex);
	put_device(&idkp->dev);
	mutex_unlock(&ide_disk_ref_mutex);
79 80
}

81
static void ide_disk_release(struct device *dev)
82
{
83
	struct ide_disk_obj *idkp = to_ide_drv(dev, ide_disk_obj);
84 85 86
	ide_drive_t *drive = idkp->drive;
	struct gendisk *g = idkp->disk;

87
	drive->disk_ops = NULL;
88 89 90 91 92 93 94 95
	drive->driver_data = NULL;
	g->private_data = NULL;
	put_disk(g);
	kfree(idkp);
}

/*
 * On HPA drives the capacity needs to be
96
 * reinitialized on resume otherwise the disk
97 98 99 100 101
 * can not be used and a hard reset is required
 */
static void ide_gd_resume(ide_drive_t *drive)
{
	if (ata_id_hpa_enabled(drive->id))
102
		(void)drive->disk_ops->get_capacity(drive);
103 104
}

105 106 107 108 109 110 111 112 113 114 115 116 117
static const struct dmi_system_id ide_coldreboot_table[] = {
	{
		/* Acer TravelMate 66x cuts power during reboot */
		.ident   = "Acer TravelMate 660",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 660"),
		},
	},

	{ }	/* terminate list */
};

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
static void ide_gd_shutdown(ide_drive_t *drive)
{
#ifdef	CONFIG_ALPHA
	/* On Alpha, halt(8) doesn't actually turn the machine off,
	   it puts you into the sort of firmware monitor. Typically,
	   it's used to boot another kernel image, so it's not much
	   different from reboot(8). Therefore, we don't need to
	   spin down the disk in this case, especially since Alpha
	   firmware doesn't handle disks in standby mode properly.
	   On the other hand, it's reasonably safe to turn the power
	   off when the shutdown process reaches the firmware prompt,
	   as the firmware initialization takes rather long time -
	   at least 10 seconds, which should be sufficient for
	   the disk to expire its write cache. */
	if (system_state != SYSTEM_POWER_OFF) {
#else
134 135
	if (system_state == SYSTEM_RESTART &&
		!dmi_check_system(ide_coldreboot_table)) {
136
#endif
137
		drive->disk_ops->flush(drive);
138 139 140 141 142 143 144 145
		return;
	}

	printk(KERN_INFO "Shutdown: %s\n", drive->name);

	drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
}

146 147 148
#ifdef CONFIG_IDE_PROC_FS
static ide_proc_entry_t *ide_disk_proc_entries(ide_drive_t *drive)
{
149
	return (drive->media == ide_disk) ? ide_disk_proc : ide_floppy_proc;
150 151 152 153
}

static const struct ide_proc_devset *ide_disk_proc_devsets(ide_drive_t *drive)
{
154 155
	return (drive->media == ide_disk) ? ide_disk_settings
					  : ide_floppy_settings;
156 157 158
}
#endif

159 160 161 162 163 164
static ide_startstop_t ide_gd_do_request(ide_drive_t *drive,
					 struct request *rq, sector_t sector)
{
	return drive->disk_ops->do_request(drive, rq, sector);
}

165
static struct ide_driver ide_gd_driver = {
166 167
	.gen_driver = {
		.owner		= THIS_MODULE,
168
		.name		= "ide-gd",
169 170 171 172 173 174 175
		.bus		= &ide_bus_type,
	},
	.probe			= ide_gd_probe,
	.remove			= ide_gd_remove,
	.resume			= ide_gd_resume,
	.shutdown		= ide_gd_shutdown,
	.version		= IDE_GD_VERSION,
176
	.do_request		= ide_gd_do_request,
177
#ifdef CONFIG_IDE_PROC_FS
178 179
	.proc_entries		= ide_disk_proc_entries,
	.proc_devsets		= ide_disk_proc_devsets,
180 181 182
#endif
};

Al Viro's avatar
Al Viro committed
183
static int ide_gd_open(struct block_device *bdev, fmode_t mode)
184
{
Al Viro's avatar
Al Viro committed
185
	struct gendisk *disk = bdev->bd_disk;
186 187
	struct ide_disk_obj *idkp;
	ide_drive_t *drive;
188
	int ret = 0;
189 190 191 192 193 194 195

	idkp = ide_disk_get(disk);
	if (idkp == NULL)
		return -ENXIO;

	drive = idkp->drive;

196
	ide_debug_log(IDE_DBG_FUNC, "enter");
197

198 199 200
	idkp->openers++;

	if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
201 202 203 204 205 206 207 208 209 210
		drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
		/* Just in case */

		ret = drive->disk_ops->init_media(drive, disk);

		/*
		 * Allow O_NDELAY to open a drive without a disk, or with an
		 * unreadable disk, so that we can get the format capacity
		 * of the drive or begin the format - Sam
		 */
Al Viro's avatar
Al Viro committed
211
		if (ret && (mode & FMODE_NDELAY) == 0) {
212 213 214 215
			ret = -EIO;
			goto out_put_idkp;
		}

Al Viro's avatar
Al Viro committed
216
		if ((drive->dev_flags & IDE_DFLAG_WP) && (mode & FMODE_WRITE)) {
217 218 219 220
			ret = -EROFS;
			goto out_put_idkp;
		}

221 222 223 224 225
		/*
		 * Ignore the return code from door_lock,
		 * since the open() has already succeeded,
		 * and the door_lock is irrelevant at this point.
		 */
226
		drive->disk_ops->set_doorlock(drive, disk, 1);
227
		drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
Al Viro's avatar
Al Viro committed
228
		check_disk_change(bdev);
229 230 231
	} else if (drive->dev_flags & IDE_DFLAG_FORMAT_IN_PROGRESS) {
		ret = -EBUSY;
		goto out_put_idkp;
232 233
	}
	return 0;
234 235 236 237 238

out_put_idkp:
	idkp->openers--;
	ide_disk_put(idkp);
	return ret;
239 240
}

241 242 243 244
static int ide_gd_unlocked_open(struct block_device *bdev, fmode_t mode)
{
	int ret;

245
	mutex_lock(&ide_gd_mutex);
246
	ret = ide_gd_open(bdev, mode);
247
	mutex_unlock(&ide_gd_mutex);
248 249 250 251 252

	return ret;
}


253
static void ide_gd_release(struct gendisk *disk, fmode_t mode)
254 255 256 257
{
	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
	ide_drive_t *drive = idkp->drive;

258
	ide_debug_log(IDE_DBG_FUNC, "enter");
259

260
	mutex_lock(&ide_gd_mutex);
261
	if (idkp->openers == 1)
262
		drive->disk_ops->flush(drive);
263

264 265 266 267
	if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
		drive->disk_ops->set_doorlock(drive, disk, 0);
		drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
	}
268 269 270 271

	idkp->openers--;

	ide_disk_put(idkp);
272
	mutex_unlock(&ide_gd_mutex);
273 274 275 276 277 278 279 280 281 282 283 284 285
}

static int ide_gd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
{
	struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
	ide_drive_t *drive = idkp->drive;

	geo->heads = drive->bios_head;
	geo->sectors = drive->bios_sect;
	geo->cylinders = (u16)drive->bios_cyl; /* truncate */
	return 0;
}

286 287
static unsigned int ide_gd_check_events(struct gendisk *disk,
					unsigned int clearing)
288 289 290
{
	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
	ide_drive_t *drive = idkp->drive;
291
	bool ret;
292 293 294 295 296 297 298

	/* do not scan partitions twice if this is a removable device */
	if (drive->dev_flags & IDE_DFLAG_ATTACH) {
		drive->dev_flags &= ~IDE_DFLAG_ATTACH;
		return 0;
	}

299 300 301 302 303 304
	/*
	 * The following is used to force revalidation on the first open on
	 * removeable devices, and never gets reported to userland as
	 * genhd->events is 0.  This is intended as removeable ide disk
	 * can't really detect MEDIA_CHANGE events.
	 */
305
	ret = drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED;
306 307
	drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;

308
	return ret ? DISK_EVENT_MEDIA_CHANGE : 0;
309 310
}

311
static void ide_gd_unlock_native_capacity(struct gendisk *disk)
312 313 314 315 316
{
	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
	ide_drive_t *drive = idkp->drive;
	const struct ide_disk_ops *disk_ops = drive->disk_ops;

317 318
	if (disk_ops->unlock_native_capacity)
		disk_ops->unlock_native_capacity(drive);
319 320
}

321 322 323
static int ide_gd_revalidate_disk(struct gendisk *disk)
{
	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
324 325
	ide_drive_t *drive = idkp->drive;

326
	if (ide_gd_check_events(disk, 0))
327 328 329
		drive->disk_ops->get_capacity(drive);

	set_capacity(disk, ide_gd_capacity(drive));
330 331 332
	return 0;
}

Al Viro's avatar
Al Viro committed
333
static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode,
334 335 336 337 338
			     unsigned int cmd, unsigned long arg)
{
	struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
	ide_drive_t *drive = idkp->drive;

Al Viro's avatar
Al Viro committed
339
	return drive->disk_ops->ioctl(drive, bdev, mode, cmd, arg);
340 341
}

342
static const struct block_device_operations ide_gd_ops = {
343
	.owner			= THIS_MODULE,
344
	.open			= ide_gd_unlocked_open,
Al Viro's avatar
Al Viro committed
345
	.release		= ide_gd_release,
346
	.ioctl			= ide_gd_ioctl,
347
	.getgeo			= ide_gd_getgeo,
348
	.check_events		= ide_gd_check_events,
349
	.unlock_native_capacity	= ide_gd_unlock_native_capacity,
350 351 352 353 354
	.revalidate_disk	= ide_gd_revalidate_disk
};

static int ide_gd_probe(ide_drive_t *drive)
{
355
	const struct ide_disk_ops *disk_ops = NULL;
356 357 358 359
	struct ide_disk_obj *idkp;
	struct gendisk *g;

	/* strstr("foo", "") is non-NULL */
360 361 362 363 364 365 366 367 368 369 370 371
	if (!strstr("ide-gd", drive->driver_req))
		goto failed;

#ifdef CONFIG_IDE_GD_ATA
	if (drive->media == ide_disk)
		disk_ops = &ide_ata_disk_ops;
#endif
#ifdef CONFIG_IDE_GD_ATAPI
	if (drive->media == ide_floppy)
		disk_ops = &ide_atapi_disk_ops;
#endif
	if (disk_ops == NULL)
372 373
		goto failed;

374 375 376
	if (disk_ops->check(drive, DRV_NAME) == 0) {
		printk(KERN_ERR PFX "%s: not supported by this driver\n",
			drive->name);
377
		goto failed;
378
	}
379 380

	idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
381 382 383
	if (!idkp) {
		printk(KERN_ERR PFX "%s: can't allocate a disk structure\n",
			drive->name);
384
		goto failed;
385
	}
386 387 388 389 390 391 392

	g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
	if (!g)
		goto out_free_idkp;

	ide_init_disk(g, drive);

393 394
	idkp->dev.parent = &drive->gendev;
	idkp->dev.release = ide_disk_release;
395
	dev_set_name(&idkp->dev, "%s", dev_name(&drive->gendev));
396 397 398

	if (device_register(&idkp->dev))
		goto out_free_disk;
399 400 401 402 403 404 405 406

	idkp->drive = drive;
	idkp->driver = &ide_gd_driver;
	idkp->disk = g;

	g->private_data = &idkp->driver;

	drive->driver_data = idkp;
407 408
	drive->debug_mask = debug_mask;
	drive->disk_ops = disk_ops;
409

410
	disk_ops->setup(drive);
411 412 413 414 415 416 417 418

	set_capacity(g, ide_gd_capacity(drive));

	g->minors = IDE_DISK_MINORS;
	g->flags |= GENHD_FL_EXT_DEVT;
	if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
		g->flags = GENHD_FL_REMOVABLE;
	g->fops = &ide_gd_ops;
419
	device_add_disk(&drive->gendev, g, NULL);
420 421
	return 0;

422 423
out_free_disk:
	put_disk(g);
424 425 426 427 428 429 430 431
out_free_idkp:
	kfree(idkp);
failed:
	return -ENODEV;
}

static int __init ide_gd_init(void)
{
432
	printk(KERN_INFO DRV_NAME " driver " IDE_GD_VERSION "\n");
433 434 435 436 437 438 439 440 441 442
	return driver_register(&ide_gd_driver.gen_driver);
}

static void __exit ide_gd_exit(void)
{
	driver_unregister(&ide_gd_driver.gen_driver);
}

MODULE_ALIAS("ide:*m-disk*");
MODULE_ALIAS("ide-disk");
443 444
MODULE_ALIAS("ide:*m-floppy*");
MODULE_ALIAS("ide-floppy");
445 446 447
module_init(ide_gd_init);
module_exit(ide_gd_exit);
MODULE_LICENSE("GPL");
448
MODULE_DESCRIPTION("generic ATA/ATAPI disk driver");