ide.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
  3. * Copyright (C) 2003-2005, 2007 Bartlomiej Zolnierkiewicz
  4. */
  5. /*
  6. * Mostly written by Mark Lord <mlord@pobox.com>
  7. * and Gadi Oxman <gadio@netvision.net.il>
  8. * and Andre Hedrick <andre@linux-ide.org>
  9. *
  10. * See linux/MAINTAINERS for address of current maintainer.
  11. *
  12. * This is the multiple IDE interface driver, as evolved from hd.c.
  13. * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs
  14. * (usually 14 & 15).
  15. * There can be up to two drives per interface, as per the ATA-2 spec.
  16. *
  17. * ...
  18. *
  19. * From hd.c:
  20. * |
  21. * | It traverses the request-list, using interrupts to jump between functions.
  22. * | As nearly all functions can be called within interrupts, we may not sleep.
  23. * | Special care is recommended. Have Fun!
  24. * |
  25. * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
  26. * |
  27. * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
  28. * | in the early extended-partition checks and added DM partitions.
  29. * |
  30. * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
  31. * |
  32. * | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
  33. * | and general streamlining by Mark Lord (mlord@pobox.com).
  34. *
  35. * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
  36. *
  37. * Mark Lord (mlord@pobox.com) (IDE Perf.Pkg)
  38. * Delman Lee (delman@ieee.org) ("Mr. atdisk2")
  39. * Scott Snyder (snyder@fnald0.fnal.gov) (ATAPI IDE cd-rom)
  40. *
  41. * This was a rewrite of just about everything from hd.c, though some original
  42. * code is still sprinkled about. Think of it as a major evolution, with
  43. * inspiration from lots of linux users, esp. hamish@zot.apana.org.au
  44. */
  45. #include <linux/module.h>
  46. #include <linux/types.h>
  47. #include <linux/string.h>
  48. #include <linux/kernel.h>
  49. #include <linux/interrupt.h>
  50. #include <linux/major.h>
  51. #include <linux/errno.h>
  52. #include <linux/genhd.h>
  53. #include <linux/slab.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. * Locks for IDE setting functionality
  63. */
  64. DEFINE_MUTEX(ide_setting_mtx);
  65. ide_devset_get(io_32bit, io_32bit);
  66. static int set_io_32bit(ide_drive_t *drive, int arg)
  67. {
  68. if (drive->dev_flags & IDE_DFLAG_NO_IO_32BIT)
  69. return -EPERM;
  70. if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
  71. return -EINVAL;
  72. drive->io_32bit = arg;
  73. return 0;
  74. }
  75. ide_devset_get_flag(ksettings, IDE_DFLAG_KEEP_SETTINGS);
  76. static int set_ksettings(ide_drive_t *drive, int arg)
  77. {
  78. if (arg < 0 || arg > 1)
  79. return -EINVAL;
  80. if (arg)
  81. drive->dev_flags |= IDE_DFLAG_KEEP_SETTINGS;
  82. else
  83. drive->dev_flags &= ~IDE_DFLAG_KEEP_SETTINGS;
  84. return 0;
  85. }
  86. ide_devset_get_flag(using_dma, IDE_DFLAG_USING_DMA);
  87. static int set_using_dma(ide_drive_t *drive, int arg)
  88. {
  89. #ifdef CONFIG_BLK_DEV_IDEDMA
  90. int err = -EPERM;
  91. if (arg < 0 || arg > 1)
  92. return -EINVAL;
  93. if (ata_id_has_dma(drive->id) == 0)
  94. goto out;
  95. if (drive->hwif->dma_ops == NULL)
  96. goto out;
  97. err = 0;
  98. if (arg) {
  99. if (ide_set_dma(drive))
  100. err = -EIO;
  101. } else
  102. ide_dma_off(drive);
  103. out:
  104. return err;
  105. #else
  106. if (arg < 0 || arg > 1)
  107. return -EINVAL;
  108. return -EPERM;
  109. #endif
  110. }
  111. /*
  112. * handle HDIO_SET_PIO_MODE ioctl abusers here, eventually it will go away
  113. */
  114. static int set_pio_mode_abuse(ide_hwif_t *hwif, u8 req_pio)
  115. {
  116. switch (req_pio) {
  117. case 202:
  118. case 201:
  119. case 200:
  120. case 102:
  121. case 101:
  122. case 100:
  123. return (hwif->host_flags & IDE_HFLAG_ABUSE_DMA_MODES) ? 1 : 0;
  124. case 9:
  125. case 8:
  126. return (hwif->host_flags & IDE_HFLAG_ABUSE_PREFETCH) ? 1 : 0;
  127. case 7:
  128. case 6:
  129. return (hwif->host_flags & IDE_HFLAG_ABUSE_FAST_DEVSEL) ? 1 : 0;
  130. default:
  131. return 0;
  132. }
  133. }
  134. static int set_pio_mode(ide_drive_t *drive, int arg)
  135. {
  136. ide_hwif_t *hwif = drive->hwif;
  137. const struct ide_port_ops *port_ops = hwif->port_ops;
  138. if (arg < 0 || arg > 255)
  139. return -EINVAL;
  140. if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
  141. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  142. return -ENOSYS;
  143. if (set_pio_mode_abuse(drive->hwif, arg)) {
  144. if (arg == 8 || arg == 9) {
  145. unsigned long flags;
  146. /* take lock for IDE_DFLAG_[NO_]UNMASK/[NO_]IO_32BIT */
  147. spin_lock_irqsave(&hwif->lock, flags);
  148. port_ops->set_pio_mode(drive, arg);
  149. spin_unlock_irqrestore(&hwif->lock, flags);
  150. } else
  151. port_ops->set_pio_mode(drive, arg);
  152. } else {
  153. int keep_dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
  154. ide_set_pio(drive, arg);
  155. if (hwif->host_flags & IDE_HFLAG_SET_PIO_MODE_KEEP_DMA) {
  156. if (keep_dma)
  157. ide_dma_on(drive);
  158. }
  159. }
  160. return 0;
  161. }
  162. ide_devset_get_flag(unmaskirq, IDE_DFLAG_UNMASK);
  163. static int set_unmaskirq(ide_drive_t *drive, int arg)
  164. {
  165. if (drive->dev_flags & IDE_DFLAG_NO_UNMASK)
  166. return -EPERM;
  167. if (arg < 0 || arg > 1)
  168. return -EINVAL;
  169. if (arg)
  170. drive->dev_flags |= IDE_DFLAG_UNMASK;
  171. else
  172. drive->dev_flags &= ~IDE_DFLAG_UNMASK;
  173. return 0;
  174. }
  175. ide_ext_devset_rw_sync(io_32bit, io_32bit);
  176. ide_ext_devset_rw_sync(keepsettings, ksettings);
  177. ide_ext_devset_rw_sync(unmaskirq, unmaskirq);
  178. ide_ext_devset_rw_sync(using_dma, using_dma);
  179. __IDE_DEVSET(pio_mode, DS_SYNC, NULL, set_pio_mode);
  180. /**
  181. * ide_device_get - get an additional reference to a ide_drive_t
  182. * @drive: device to get a reference to
  183. *
  184. * Gets a reference to the ide_drive_t and increments the use count of the
  185. * underlying LLDD module.
  186. */
  187. int ide_device_get(ide_drive_t *drive)
  188. {
  189. struct device *host_dev;
  190. struct module *module;
  191. if (!get_device(&drive->gendev))
  192. return -ENXIO;
  193. host_dev = drive->hwif->host->dev[0];
  194. module = host_dev ? host_dev->driver->owner : NULL;
  195. if (module && !try_module_get(module)) {
  196. put_device(&drive->gendev);
  197. return -ENXIO;
  198. }
  199. return 0;
  200. }
  201. EXPORT_SYMBOL_GPL(ide_device_get);
  202. /**
  203. * ide_device_put - release a reference to a ide_drive_t
  204. * @drive: device to release a reference on
  205. *
  206. * Release a reference to the ide_drive_t and decrements the use count of
  207. * the underlying LLDD module.
  208. */
  209. void ide_device_put(ide_drive_t *drive)
  210. {
  211. #ifdef CONFIG_MODULE_UNLOAD
  212. struct device *host_dev = drive->hwif->host->dev[0];
  213. struct module *module = host_dev ? host_dev->driver->owner : NULL;
  214. if (module)
  215. module_put(module);
  216. #endif
  217. put_device(&drive->gendev);
  218. }
  219. EXPORT_SYMBOL_GPL(ide_device_put);
  220. static int ide_bus_match(struct device *dev, struct device_driver *drv)
  221. {
  222. return 1;
  223. }
  224. static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
  225. {
  226. ide_drive_t *drive = to_ide_device(dev);
  227. add_uevent_var(env, "MEDIA=%s", ide_media_string(drive));
  228. add_uevent_var(env, "DRIVENAME=%s", drive->name);
  229. add_uevent_var(env, "MODALIAS=ide:m-%s", ide_media_string(drive));
  230. return 0;
  231. }
  232. static int generic_ide_probe(struct device *dev)
  233. {
  234. ide_drive_t *drive = to_ide_device(dev);
  235. struct ide_driver *drv = to_ide_driver(dev->driver);
  236. return drv->probe ? drv->probe(drive) : -ENODEV;
  237. }
  238. static int generic_ide_remove(struct device *dev)
  239. {
  240. ide_drive_t *drive = to_ide_device(dev);
  241. struct ide_driver *drv = to_ide_driver(dev->driver);
  242. if (drv->remove)
  243. drv->remove(drive);
  244. return 0;
  245. }
  246. static void generic_ide_shutdown(struct device *dev)
  247. {
  248. ide_drive_t *drive = to_ide_device(dev);
  249. struct ide_driver *drv = to_ide_driver(dev->driver);
  250. if (dev->driver && drv->shutdown)
  251. drv->shutdown(drive);
  252. }
  253. struct bus_type ide_bus_type = {
  254. .name = "ide",
  255. .match = ide_bus_match,
  256. .uevent = ide_uevent,
  257. .probe = generic_ide_probe,
  258. .remove = generic_ide_remove,
  259. .shutdown = generic_ide_shutdown,
  260. .dev_attrs = ide_dev_attrs,
  261. .suspend = generic_ide_suspend,
  262. .resume = generic_ide_resume,
  263. };
  264. EXPORT_SYMBOL_GPL(ide_bus_type);
  265. int ide_vlb_clk;
  266. EXPORT_SYMBOL_GPL(ide_vlb_clk);
  267. module_param_named(vlb_clock, ide_vlb_clk, int, 0);
  268. MODULE_PARM_DESC(vlb_clock, "VLB clock frequency (in MHz)");
  269. int ide_pci_clk;
  270. EXPORT_SYMBOL_GPL(ide_pci_clk);
  271. module_param_named(pci_clock, ide_pci_clk, int, 0);
  272. MODULE_PARM_DESC(pci_clock, "PCI bus clock frequency (in MHz)");
  273. static int ide_set_dev_param_mask(const char *s, struct kernel_param *kp)
  274. {
  275. int a, b, i, j = 1;
  276. unsigned int *dev_param_mask = (unsigned int *)kp->arg;
  277. if (sscanf(s, "%d.%d:%d", &a, &b, &j) != 3 &&
  278. sscanf(s, "%d.%d", &a, &b) != 2)
  279. return -EINVAL;
  280. i = a * MAX_DRIVES + b;
  281. if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
  282. return -EINVAL;
  283. if (j)
  284. *dev_param_mask |= (1 << i);
  285. else
  286. *dev_param_mask &= (1 << i);
  287. return 0;
  288. }
  289. static unsigned int ide_nodma;
  290. module_param_call(nodma, ide_set_dev_param_mask, NULL, &ide_nodma, 0);
  291. MODULE_PARM_DESC(nodma, "disallow DMA for a device");
  292. static unsigned int ide_noflush;
  293. module_param_call(noflush, ide_set_dev_param_mask, NULL, &ide_noflush, 0);
  294. MODULE_PARM_DESC(noflush, "disable flush requests for a device");
  295. static unsigned int ide_noprobe;
  296. module_param_call(noprobe, ide_set_dev_param_mask, NULL, &ide_noprobe, 0);
  297. MODULE_PARM_DESC(noprobe, "skip probing for a device");
  298. static unsigned int ide_nowerr;
  299. module_param_call(nowerr, ide_set_dev_param_mask, NULL, &ide_nowerr, 0);
  300. MODULE_PARM_DESC(nowerr, "ignore the ATA_DF bit for a device");
  301. static unsigned int ide_cdroms;
  302. module_param_call(cdrom, ide_set_dev_param_mask, NULL, &ide_cdroms, 0);
  303. MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
  304. struct chs_geom {
  305. unsigned int cyl;
  306. u8 head;
  307. u8 sect;
  308. };
  309. static unsigned int ide_disks;
  310. static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
  311. static int ide_set_disk_chs(const char *str, struct kernel_param *kp)
  312. {
  313. int a, b, c = 0, h = 0, s = 0, i, j = 1;
  314. if (sscanf(str, "%d.%d:%d,%d,%d", &a, &b, &c, &h, &s) != 5 &&
  315. sscanf(str, "%d.%d:%d", &a, &b, &j) != 3)
  316. return -EINVAL;
  317. i = a * MAX_DRIVES + b;
  318. if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
  319. return -EINVAL;
  320. if (c > INT_MAX || h > 255 || s > 255)
  321. return -EINVAL;
  322. if (j)
  323. ide_disks |= (1 << i);
  324. else
  325. ide_disks &= (1 << i);
  326. ide_disks_chs[i].cyl = c;
  327. ide_disks_chs[i].head = h;
  328. ide_disks_chs[i].sect = s;
  329. return 0;
  330. }
  331. module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
  332. MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
  333. static void ide_dev_apply_params(ide_drive_t *drive, u8 unit)
  334. {
  335. int i = drive->hwif->index * MAX_DRIVES + unit;
  336. if (ide_nodma & (1 << i)) {
  337. printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
  338. drive->dev_flags |= IDE_DFLAG_NODMA;
  339. }
  340. if (ide_noflush & (1 << i)) {
  341. printk(KERN_INFO "ide: disabling flush requests for %s\n",
  342. drive->name);
  343. drive->dev_flags |= IDE_DFLAG_NOFLUSH;
  344. }
  345. if (ide_noprobe & (1 << i)) {
  346. printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
  347. drive->dev_flags |= IDE_DFLAG_NOPROBE;
  348. }
  349. if (ide_nowerr & (1 << i)) {
  350. printk(KERN_INFO "ide: ignoring the ATA_DF bit for %s\n",
  351. drive->name);
  352. drive->bad_wstat = BAD_R_STAT;
  353. }
  354. if (ide_cdroms & (1 << i)) {
  355. printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
  356. drive->dev_flags |= IDE_DFLAG_PRESENT;
  357. drive->media = ide_cdrom;
  358. /* an ATAPI device ignores DRDY */
  359. drive->ready_stat = 0;
  360. }
  361. if (ide_disks & (1 << i)) {
  362. drive->cyl = drive->bios_cyl = ide_disks_chs[i].cyl;
  363. drive->head = drive->bios_head = ide_disks_chs[i].head;
  364. drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
  365. printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
  366. drive->name,
  367. drive->cyl, drive->head, drive->sect);
  368. drive->dev_flags |= IDE_DFLAG_FORCED_GEOM | IDE_DFLAG_PRESENT;
  369. drive->media = ide_disk;
  370. drive->ready_stat = ATA_DRDY;
  371. }
  372. }
  373. static unsigned int ide_ignore_cable;
  374. static int ide_set_ignore_cable(const char *s, struct kernel_param *kp)
  375. {
  376. int i, j = 1;
  377. if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
  378. return -EINVAL;
  379. if (i >= MAX_HWIFS || j < 0 || j > 1)
  380. return -EINVAL;
  381. if (j)
  382. ide_ignore_cable |= (1 << i);
  383. else
  384. ide_ignore_cable &= (1 << i);
  385. return 0;
  386. }
  387. module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
  388. MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
  389. void ide_port_apply_params(ide_hwif_t *hwif)
  390. {
  391. ide_drive_t *drive;
  392. int i;
  393. if (ide_ignore_cable & (1 << hwif->index)) {
  394. printk(KERN_INFO "ide: ignoring cable detection for %s\n",
  395. hwif->name);
  396. hwif->cbl = ATA_CBL_PATA40_SHORT;
  397. }
  398. ide_port_for_each_dev(i, drive, hwif)
  399. ide_dev_apply_params(drive, i);
  400. }
  401. /*
  402. * This is gets invoked once during initialization, to set *everything* up
  403. */
  404. static int __init ide_init(void)
  405. {
  406. int ret;
  407. printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
  408. ret = bus_register(&ide_bus_type);
  409. if (ret < 0) {
  410. printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
  411. return ret;
  412. }
  413. ide_port_class = class_create(THIS_MODULE, "ide_port");
  414. if (IS_ERR(ide_port_class)) {
  415. ret = PTR_ERR(ide_port_class);
  416. goto out_port_class;
  417. }
  418. proc_ide_create();
  419. return 0;
  420. out_port_class:
  421. bus_unregister(&ide_bus_type);
  422. return ret;
  423. }
  424. static void __exit ide_exit(void)
  425. {
  426. proc_ide_destroy();
  427. class_destroy(ide_port_class);
  428. bus_unregister(&ide_bus_type);
  429. }
  430. module_init(ide_init);
  431. module_exit(ide_exit);
  432. MODULE_LICENSE("GPL");