ide-gd.c 11 KB

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