^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/genhd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/ide.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/hdreg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/dmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define IDE_DISK_MINORS (1 << PARTN_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define IDE_DISK_MINORS 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "ide-disk.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "ide-floppy.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define IDE_GD_VERSION "1.18"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* module parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static DEFINE_MUTEX(ide_gd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static unsigned long debug_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) module_param(debug_mask, ulong, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static DEFINE_MUTEX(ide_disk_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static void ide_disk_release(struct device *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct ide_disk_obj *idkp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) mutex_lock(&ide_disk_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) idkp = ide_drv_g(disk, ide_disk_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (idkp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (ide_device_get(idkp->drive))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) idkp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) get_device(&idkp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) mutex_unlock(&ide_disk_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return idkp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static void ide_disk_put(struct ide_disk_obj *idkp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ide_drive_t *drive = idkp->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) mutex_lock(&ide_disk_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) put_device(&idkp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ide_device_put(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) mutex_unlock(&ide_disk_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) sector_t ide_gd_capacity(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return drive->capacity64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static int ide_gd_probe(ide_drive_t *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static void ide_gd_remove(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct ide_disk_obj *idkp = drive->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct gendisk *g = idkp->disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ide_proc_unregister_driver(drive, idkp->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) device_del(&idkp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) del_gendisk(g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) drive->disk_ops->flush(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) mutex_lock(&ide_disk_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) put_device(&idkp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) mutex_unlock(&ide_disk_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static void ide_disk_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct ide_disk_obj *idkp = to_ide_drv(dev, ide_disk_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ide_drive_t *drive = idkp->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct gendisk *g = idkp->disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) drive->disk_ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) drive->driver_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) g->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) put_disk(g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) kfree(idkp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * On HPA drives the capacity needs to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * reinitialized on resume otherwise the disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * can not be used and a hard reset is required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static void ide_gd_resume(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (ata_id_hpa_enabled(drive->id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) (void)drive->disk_ops->get_capacity(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static const struct dmi_system_id ide_coldreboot_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /* Acer TravelMate 66x cuts power during reboot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .ident = "Acer TravelMate 660",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .matches = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 660"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) { } /* terminate list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static void ide_gd_shutdown(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #ifdef CONFIG_ALPHA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* On Alpha, halt(8) doesn't actually turn the machine off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) it puts you into the sort of firmware monitor. Typically,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) it's used to boot another kernel image, so it's not much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) different from reboot(8). Therefore, we don't need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) spin down the disk in this case, especially since Alpha
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) firmware doesn't handle disks in standby mode properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) On the other hand, it's reasonably safe to turn the power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) off when the shutdown process reaches the firmware prompt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) as the firmware initialization takes rather long time -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) at least 10 seconds, which should be sufficient for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) the disk to expire its write cache. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (system_state != SYSTEM_POWER_OFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (system_state == SYSTEM_RESTART &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) !dmi_check_system(ide_coldreboot_table)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) drive->disk_ops->flush(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) printk(KERN_INFO "Shutdown: %s\n", drive->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #ifdef CONFIG_IDE_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static ide_proc_entry_t *ide_disk_proc_entries(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return (drive->media == ide_disk) ? ide_disk_proc : ide_floppy_proc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static const struct ide_proc_devset *ide_disk_proc_devsets(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return (drive->media == ide_disk) ? ide_disk_settings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) : ide_floppy_settings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static ide_startstop_t ide_gd_do_request(ide_drive_t *drive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct request *rq, sector_t sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return drive->disk_ops->do_request(drive, rq, sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static struct ide_driver ide_gd_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .gen_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .name = "ide-gd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .bus = &ide_bus_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) .probe = ide_gd_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .remove = ide_gd_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .resume = ide_gd_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .shutdown = ide_gd_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .version = IDE_GD_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .do_request = ide_gd_do_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) #ifdef CONFIG_IDE_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .proc_entries = ide_disk_proc_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .proc_devsets = ide_disk_proc_devsets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int ide_gd_open(struct block_device *bdev, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct gendisk *disk = bdev->bd_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct ide_disk_obj *idkp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ide_drive_t *drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) idkp = ide_disk_get(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (idkp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) drive = idkp->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ide_debug_log(IDE_DBG_FUNC, "enter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) idkp->openers++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /* Just in case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) ret = drive->disk_ops->init_media(drive, disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * Allow O_NDELAY to open a drive without a disk, or with an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * unreadable disk, so that we can get the format capacity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * of the drive or begin the format - Sam
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (ret && (mode & FMODE_NDELAY) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) goto out_put_idkp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if ((drive->dev_flags & IDE_DFLAG_WP) && (mode & FMODE_WRITE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ret = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) goto out_put_idkp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * Ignore the return code from door_lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * since the open() has already succeeded,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * and the door_lock is irrelevant at this point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) drive->disk_ops->set_doorlock(drive, disk, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (__invalidate_device(bdev, true))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) pr_warn("VFS: busy inodes on changed media %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) bdev->bd_disk->disk_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) drive->disk_ops->get_capacity(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) set_capacity(disk, ide_gd_capacity(drive));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) set_bit(GD_NEED_PART_SCAN, &disk->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) } else if (drive->dev_flags & IDE_DFLAG_FORMAT_IN_PROGRESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) goto out_put_idkp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) out_put_idkp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) idkp->openers--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ide_disk_put(idkp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static int ide_gd_unlocked_open(struct block_device *bdev, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) mutex_lock(&ide_gd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) ret = ide_gd_open(bdev, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) mutex_unlock(&ide_gd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static void ide_gd_release(struct gendisk *disk, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) ide_drive_t *drive = idkp->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) ide_debug_log(IDE_DBG_FUNC, "enter");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) mutex_lock(&ide_gd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (idkp->openers == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) drive->disk_ops->flush(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) drive->disk_ops->set_doorlock(drive, disk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) idkp->openers--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ide_disk_put(idkp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) mutex_unlock(&ide_gd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static int ide_gd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ide_drive_t *drive = idkp->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) geo->heads = drive->bios_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) geo->sectors = drive->bios_sect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) geo->cylinders = (u16)drive->bios_cyl; /* truncate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static void ide_gd_unlock_native_capacity(struct gendisk *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) ide_drive_t *drive = idkp->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) const struct ide_disk_ops *disk_ops = drive->disk_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (disk_ops->unlock_native_capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) disk_ops->unlock_native_capacity(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) ide_drive_t *drive = idkp->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return drive->disk_ops->ioctl(drive, bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static int ide_gd_compat_ioctl(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) ide_drive_t *drive = idkp->drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (!drive->disk_ops->compat_ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return drive->disk_ops->compat_ioctl(drive, bdev, mode, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static const struct block_device_operations ide_gd_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) .open = ide_gd_unlocked_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) .release = ide_gd_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) .ioctl = ide_gd_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) .compat_ioctl = ide_gd_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) .getgeo = ide_gd_getgeo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) .unlock_native_capacity = ide_gd_unlock_native_capacity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static int ide_gd_probe(ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) const struct ide_disk_ops *disk_ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct ide_disk_obj *idkp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct gendisk *g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /* strstr("foo", "") is non-NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (!strstr("ide-gd", drive->driver_req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) #ifdef CONFIG_IDE_GD_ATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (drive->media == ide_disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) disk_ops = &ide_ata_disk_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) #ifdef CONFIG_IDE_GD_ATAPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (drive->media == ide_floppy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) disk_ops = &ide_atapi_disk_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (disk_ops == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (disk_ops->check(drive, DRV_NAME) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) printk(KERN_ERR PFX "%s: not supported by this driver\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) drive->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (!idkp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) printk(KERN_ERR PFX "%s: can't allocate a disk structure\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) drive->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (!g)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) goto out_free_idkp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ide_init_disk(g, drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) idkp->dev.parent = &drive->gendev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) idkp->dev.release = ide_disk_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) dev_set_name(&idkp->dev, "%s", dev_name(&drive->gendev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (device_register(&idkp->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto out_free_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) idkp->drive = drive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) idkp->driver = &ide_gd_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) idkp->disk = g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) g->private_data = &idkp->driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) drive->driver_data = idkp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) drive->debug_mask = debug_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) drive->disk_ops = disk_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) disk_ops->setup(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) set_capacity(g, ide_gd_capacity(drive));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) g->minors = IDE_DISK_MINORS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) g->flags |= GENHD_FL_EXT_DEVT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) g->flags = GENHD_FL_REMOVABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) g->fops = &ide_gd_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) g->events = DISK_EVENT_MEDIA_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) device_add_disk(&drive->gendev, g, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) out_free_disk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) put_disk(g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) out_free_idkp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) kfree(idkp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) static int __init ide_gd_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) printk(KERN_INFO DRV_NAME " driver " IDE_GD_VERSION "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return driver_register(&ide_gd_driver.gen_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static void __exit ide_gd_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) driver_unregister(&ide_gd_driver.gen_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) MODULE_ALIAS("ide:*m-disk*");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) MODULE_ALIAS("ide-disk");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) MODULE_ALIAS("ide:*m-floppy*");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) MODULE_ALIAS("ide-floppy");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) module_init(ide_gd_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) module_exit(ide_gd_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) MODULE_DESCRIPTION("generic ATA/ATAPI disk driver");