ide.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
  4. * Copyright (C) 2003-2005, 2007 Bartlomiej Zolnierkiewicz
  5. */
  6. /*
  7. * Mostly written by Mark Lord <mlord@pobox.com>
  8. * and Gadi Oxman <gadio@netvision.net.il>
  9. * and Andre Hedrick <andre@linux-ide.org>
  10. *
  11. * See linux/MAINTAINERS for address of current maintainer.
  12. *
  13. * This is the multiple IDE interface driver, as evolved from hd.c.
  14. * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs
  15. * (usually 14 & 15).
  16. * There can be up to two drives per interface, as per the ATA-2 spec.
  17. *
  18. * ...
  19. *
  20. * From hd.c:
  21. * |
  22. * | It traverses the request-list, using interrupts to jump between functions.
  23. * | As nearly all functions can be called within interrupts, we may not sleep.
  24. * | Special care is recommended. Have Fun!
  25. * |
  26. * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
  27. * |
  28. * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
  29. * | in the early extended-partition checks and added DM partitions.
  30. * |
  31. * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
  32. * |
  33. * | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
  34. * | and general streamlining by Mark Lord (mlord@pobox.com).
  35. *
  36. * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
  37. *
  38. * Mark Lord (mlord@pobox.com) (IDE Perf.Pkg)
  39. * Delman Lee (delman@ieee.org) ("Mr. atdisk2")
  40. * Scott Snyder (snyder@fnald0.fnal.gov) (ATAPI IDE cd-rom)
  41. *
  42. * This was a rewrite of just about everything from hd.c, though some original
  43. * code is still sprinkled about. Think of it as a major evolution, with
  44. * inspiration from lots of linux users, esp. hamish@zot.apana.org.au
  45. */
  46. #include <linux/module.h>
  47. #include <linux/types.h>
  48. #include <linux/string.h>
  49. #include <linux/kernel.h>
  50. #include <linux/interrupt.h>
  51. #include <linux/major.h>
  52. #include <linux/errno.h>
  53. #include <linux/genhd.h>
  54. #include <linux/init.h>
  55. #include <linux/pci.h>
  56. #include <linux/ide.h>
  57. #include <linux/hdreg.h>
  58. #include <linux/completion.h>
  59. #include <linux/device.h>
  60. struct class *ide_port_class;
  61. /**
  62. * ide_device_get - get an additional reference to a ide_drive_t
  63. * @drive: device to get a reference to
  64. *
  65. * Gets a reference to the ide_drive_t and increments the use count of the
  66. * underlying LLDD module.
  67. */
  68. int ide_device_get(ide_drive_t *drive)
  69. {
  70. struct device *host_dev;
  71. struct module *module;
  72. if (!get_device(&drive->gendev))
  73. return -ENXIO;
  74. host_dev = drive->hwif->host->dev[0];
  75. module = host_dev ? host_dev->driver->owner : NULL;
  76. if (module && !try_module_get(module)) {
  77. put_device(&drive->gendev);
  78. return -ENXIO;
  79. }
  80. return 0;
  81. }
  82. EXPORT_SYMBOL_GPL(ide_device_get);
  83. /**
  84. * ide_device_put - release a reference to a ide_drive_t
  85. * @drive: device to release a reference on
  86. *
  87. * Release a reference to the ide_drive_t and decrements the use count of
  88. * the underlying LLDD module.
  89. */
  90. void ide_device_put(ide_drive_t *drive)
  91. {
  92. #ifdef CONFIG_MODULE_UNLOAD
  93. struct device *host_dev = drive->hwif->host->dev[0];
  94. struct module *module = host_dev ? host_dev->driver->owner : NULL;
  95. module_put(module);
  96. #endif
  97. put_device(&drive->gendev);
  98. }
  99. EXPORT_SYMBOL_GPL(ide_device_put);
  100. static int ide_bus_match(struct device *dev, struct device_driver *drv)
  101. {
  102. return 1;
  103. }
  104. static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
  105. {
  106. ide_drive_t *drive = to_ide_device(dev);
  107. add_uevent_var(env, "MEDIA=%s", ide_media_string(drive));
  108. add_uevent_var(env, "DRIVENAME=%s", drive->name);
  109. add_uevent_var(env, "MODALIAS=ide:m-%s", ide_media_string(drive));
  110. return 0;
  111. }
  112. static int generic_ide_probe(struct device *dev)
  113. {
  114. ide_drive_t *drive = to_ide_device(dev);
  115. struct ide_driver *drv = to_ide_driver(dev->driver);
  116. return drv->probe ? drv->probe(drive) : -ENODEV;
  117. }
  118. static int generic_ide_remove(struct device *dev)
  119. {
  120. ide_drive_t *drive = to_ide_device(dev);
  121. struct ide_driver *drv = to_ide_driver(dev->driver);
  122. if (drv->remove)
  123. drv->remove(drive);
  124. return 0;
  125. }
  126. static void generic_ide_shutdown(struct device *dev)
  127. {
  128. ide_drive_t *drive = to_ide_device(dev);
  129. struct ide_driver *drv = to_ide_driver(dev->driver);
  130. if (dev->driver && drv->shutdown)
  131. drv->shutdown(drive);
  132. }
  133. struct bus_type ide_bus_type = {
  134. .name = "ide",
  135. .match = ide_bus_match,
  136. .uevent = ide_uevent,
  137. .probe = generic_ide_probe,
  138. .remove = generic_ide_remove,
  139. .shutdown = generic_ide_shutdown,
  140. .dev_groups = ide_dev_groups,
  141. .suspend = generic_ide_suspend,
  142. .resume = generic_ide_resume,
  143. };
  144. EXPORT_SYMBOL_GPL(ide_bus_type);
  145. int ide_vlb_clk;
  146. EXPORT_SYMBOL_GPL(ide_vlb_clk);
  147. module_param_named(vlb_clock, ide_vlb_clk, int, 0);
  148. MODULE_PARM_DESC(vlb_clock, "VLB clock frequency (in MHz)");
  149. int ide_pci_clk;
  150. EXPORT_SYMBOL_GPL(ide_pci_clk);
  151. module_param_named(pci_clock, ide_pci_clk, int, 0);
  152. MODULE_PARM_DESC(pci_clock, "PCI bus clock frequency (in MHz)");
  153. static int ide_set_dev_param_mask(const char *s, const struct kernel_param *kp)
  154. {
  155. unsigned int a, b, i, j = 1;
  156. unsigned int *dev_param_mask = (unsigned int *)kp->arg;
  157. /* controller . device (0 or 1) [ : 1 (set) | 0 (clear) ] */
  158. if (sscanf(s, "%u.%u:%u", &a, &b, &j) != 3 &&
  159. sscanf(s, "%u.%u", &a, &b) != 2)
  160. return -EINVAL;
  161. i = a * MAX_DRIVES + b;
  162. if (i >= MAX_HWIFS * MAX_DRIVES || j > 1)
  163. return -EINVAL;
  164. if (j)
  165. *dev_param_mask |= (1 << i);
  166. else
  167. *dev_param_mask &= ~(1 << i);
  168. return 0;
  169. }
  170. static const struct kernel_param_ops param_ops_ide_dev_mask = {
  171. .set = ide_set_dev_param_mask
  172. };
  173. #define param_check_ide_dev_mask(name, p) param_check_uint(name, p)
  174. static unsigned int ide_nodma;
  175. module_param_named(nodma, ide_nodma, ide_dev_mask, 0);
  176. MODULE_PARM_DESC(nodma, "disallow DMA for a device");
  177. static unsigned int ide_noflush;
  178. module_param_named(noflush, ide_noflush, ide_dev_mask, 0);
  179. MODULE_PARM_DESC(noflush, "disable flush requests for a device");
  180. static unsigned int ide_nohpa;
  181. module_param_named(nohpa, ide_nohpa, ide_dev_mask, 0);
  182. MODULE_PARM_DESC(nohpa, "disable Host Protected Area for a device");
  183. static unsigned int ide_noprobe;
  184. module_param_named(noprobe, ide_noprobe, ide_dev_mask, 0);
  185. MODULE_PARM_DESC(noprobe, "skip probing for a device");
  186. static unsigned int ide_nowerr;
  187. module_param_named(nowerr, ide_nowerr, ide_dev_mask, 0);
  188. MODULE_PARM_DESC(nowerr, "ignore the ATA_DF bit for a device");
  189. static unsigned int ide_cdroms;
  190. module_param_named(cdrom, ide_cdroms, ide_dev_mask, 0);
  191. MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
  192. struct chs_geom {
  193. unsigned int cyl;
  194. u8 head;
  195. u8 sect;
  196. };
  197. static unsigned int ide_disks;
  198. static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
  199. static int ide_set_disk_chs(const char *str, const struct kernel_param *kp)
  200. {
  201. unsigned int a, b, c = 0, h = 0, s = 0, i, j = 1;
  202. /* controller . device (0 or 1) : Cylinders , Heads , Sectors */
  203. /* controller . device (0 or 1) : 1 (use CHS) | 0 (ignore CHS) */
  204. if (sscanf(str, "%u.%u:%u,%u,%u", &a, &b, &c, &h, &s) != 5 &&
  205. sscanf(str, "%u.%u:%u", &a, &b, &j) != 3)
  206. return -EINVAL;
  207. i = a * MAX_DRIVES + b;
  208. if (i >= MAX_HWIFS * MAX_DRIVES || j > 1)
  209. return -EINVAL;
  210. if (c > INT_MAX || h > 255 || s > 255)
  211. return -EINVAL;
  212. if (j)
  213. ide_disks |= (1 << i);
  214. else
  215. ide_disks &= ~(1 << i);
  216. ide_disks_chs[i].cyl = c;
  217. ide_disks_chs[i].head = h;
  218. ide_disks_chs[i].sect = s;
  219. return 0;
  220. }
  221. module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
  222. MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
  223. static void ide_dev_apply_params(ide_drive_t *drive, u8 unit)
  224. {
  225. int i = drive->hwif->index * MAX_DRIVES + unit;
  226. if (ide_nodma & (1 << i)) {
  227. printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
  228. drive->dev_flags |= IDE_DFLAG_NODMA;
  229. }
  230. if (ide_noflush & (1 << i)) {
  231. printk(KERN_INFO "ide: disabling flush requests for %s\n",
  232. drive->name);
  233. drive->dev_flags |= IDE_DFLAG_NOFLUSH;
  234. }
  235. if (ide_nohpa & (1 << i)) {
  236. printk(KERN_INFO "ide: disabling Host Protected Area for %s\n",
  237. drive->name);
  238. drive->dev_flags |= IDE_DFLAG_NOHPA;
  239. }
  240. if (ide_noprobe & (1 << i)) {
  241. printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
  242. drive->dev_flags |= IDE_DFLAG_NOPROBE;
  243. }
  244. if (ide_nowerr & (1 << i)) {
  245. printk(KERN_INFO "ide: ignoring the ATA_DF bit for %s\n",
  246. drive->name);
  247. drive->bad_wstat = BAD_R_STAT;
  248. }
  249. if (ide_cdroms & (1 << i)) {
  250. printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
  251. drive->dev_flags |= IDE_DFLAG_PRESENT;
  252. drive->media = ide_cdrom;
  253. /* an ATAPI device ignores DRDY */
  254. drive->ready_stat = 0;
  255. }
  256. if (ide_disks & (1 << i)) {
  257. drive->cyl = drive->bios_cyl = ide_disks_chs[i].cyl;
  258. drive->head = drive->bios_head = ide_disks_chs[i].head;
  259. drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
  260. printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
  261. drive->name,
  262. drive->cyl, drive->head, drive->sect);
  263. drive->dev_flags |= IDE_DFLAG_FORCED_GEOM | IDE_DFLAG_PRESENT;
  264. drive->media = ide_disk;
  265. drive->ready_stat = ATA_DRDY;
  266. }
  267. }
  268. static unsigned int ide_ignore_cable;
  269. static int ide_set_ignore_cable(const char *s, const struct kernel_param *kp)
  270. {
  271. int i, j = 1;
  272. /* controller (ignore) */
  273. /* controller : 1 (ignore) | 0 (use) */
  274. if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
  275. return -EINVAL;
  276. if (i >= MAX_HWIFS || j < 0 || j > 1)
  277. return -EINVAL;
  278. if (j)
  279. ide_ignore_cable |= (1 << i);
  280. else
  281. ide_ignore_cable &= ~(1 << i);
  282. return 0;
  283. }
  284. module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
  285. MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
  286. void ide_port_apply_params(ide_hwif_t *hwif)
  287. {
  288. ide_drive_t *drive;
  289. int i;
  290. if (ide_ignore_cable & (1 << hwif->index)) {
  291. printk(KERN_INFO "ide: ignoring cable detection for %s\n",
  292. hwif->name);
  293. hwif->cbl = ATA_CBL_PATA40_SHORT;
  294. }
  295. ide_port_for_each_dev(i, drive, hwif)
  296. ide_dev_apply_params(drive, i);
  297. }
  298. /*
  299. * This is gets invoked once during initialization, to set *everything* up
  300. */
  301. static int __init ide_init(void)
  302. {
  303. int ret;
  304. printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
  305. ret = bus_register(&ide_bus_type);
  306. if (ret < 0) {
  307. printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
  308. return ret;
  309. }
  310. ide_port_class = class_create(THIS_MODULE, "ide_port");
  311. if (IS_ERR(ide_port_class)) {
  312. ret = PTR_ERR(ide_port_class);
  313. goto out_port_class;
  314. }
  315. ide_acpi_init();
  316. proc_ide_create();
  317. return 0;
  318. out_port_class:
  319. bus_unregister(&ide_bus_type);
  320. return ret;
  321. }
  322. static void __exit ide_exit(void)
  323. {
  324. proc_ide_destroy();
  325. class_destroy(ide_port_class);
  326. bus_unregister(&ide_bus_type);
  327. }
  328. module_init(ide_init);
  329. module_exit(ide_exit);
  330. MODULE_LICENSE("GPL");