ide.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. /* controller . device (0 or 1) [ : 1 (set) | 0 (clear) ] */
  278. if (sscanf(s, "%d.%d:%d", &a, &b, &j) != 3 &&
  279. sscanf(s, "%d.%d", &a, &b) != 2)
  280. return -EINVAL;
  281. i = a * MAX_DRIVES + b;
  282. if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
  283. return -EINVAL;
  284. if (j)
  285. *dev_param_mask |= (1 << i);
  286. else
  287. *dev_param_mask &= ~(1 << i);
  288. return 0;
  289. }
  290. static unsigned int ide_nodma;
  291. module_param_call(nodma, ide_set_dev_param_mask, NULL, &ide_nodma, 0);
  292. MODULE_PARM_DESC(nodma, "disallow DMA for a device");
  293. static unsigned int ide_noflush;
  294. module_param_call(noflush, ide_set_dev_param_mask, NULL, &ide_noflush, 0);
  295. MODULE_PARM_DESC(noflush, "disable flush requests for a device");
  296. static unsigned int ide_noprobe;
  297. module_param_call(noprobe, ide_set_dev_param_mask, NULL, &ide_noprobe, 0);
  298. MODULE_PARM_DESC(noprobe, "skip probing for a device");
  299. static unsigned int ide_nowerr;
  300. module_param_call(nowerr, ide_set_dev_param_mask, NULL, &ide_nowerr, 0);
  301. MODULE_PARM_DESC(nowerr, "ignore the ATA_DF bit for a device");
  302. static unsigned int ide_cdroms;
  303. module_param_call(cdrom, ide_set_dev_param_mask, NULL, &ide_cdroms, 0);
  304. MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
  305. struct chs_geom {
  306. unsigned int cyl;
  307. u8 head;
  308. u8 sect;
  309. };
  310. static unsigned int ide_disks;
  311. static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
  312. static int ide_set_disk_chs(const char *str, struct kernel_param *kp)
  313. {
  314. int a, b, c = 0, h = 0, s = 0, i, j = 1;
  315. /* controller . device (0 or 1) : Cylinders , Heads , Sectors */
  316. /* controller . device (0 or 1) : 1 (use CHS) | 0 (ignore CHS) */
  317. if (sscanf(str, "%d.%d:%d,%d,%d", &a, &b, &c, &h, &s) != 5 &&
  318. sscanf(str, "%d.%d:%d", &a, &b, &j) != 3)
  319. return -EINVAL;
  320. i = a * MAX_DRIVES + b;
  321. if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
  322. return -EINVAL;
  323. if (c > INT_MAX || h > 255 || s > 255)
  324. return -EINVAL;
  325. if (j)
  326. ide_disks |= (1 << i);
  327. else
  328. ide_disks &= ~(1 << i);
  329. ide_disks_chs[i].cyl = c;
  330. ide_disks_chs[i].head = h;
  331. ide_disks_chs[i].sect = s;
  332. return 0;
  333. }
  334. module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
  335. MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
  336. static void ide_dev_apply_params(ide_drive_t *drive, u8 unit)
  337. {
  338. int i = drive->hwif->index * MAX_DRIVES + unit;
  339. if (ide_nodma & (1 << i)) {
  340. printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
  341. drive->dev_flags |= IDE_DFLAG_NODMA;
  342. }
  343. if (ide_noflush & (1 << i)) {
  344. printk(KERN_INFO "ide: disabling flush requests for %s\n",
  345. drive->name);
  346. drive->dev_flags |= IDE_DFLAG_NOFLUSH;
  347. }
  348. if (ide_noprobe & (1 << i)) {
  349. printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
  350. drive->dev_flags |= IDE_DFLAG_NOPROBE;
  351. }
  352. if (ide_nowerr & (1 << i)) {
  353. printk(KERN_INFO "ide: ignoring the ATA_DF bit for %s\n",
  354. drive->name);
  355. drive->bad_wstat = BAD_R_STAT;
  356. }
  357. if (ide_cdroms & (1 << i)) {
  358. printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
  359. drive->dev_flags |= IDE_DFLAG_PRESENT;
  360. drive->media = ide_cdrom;
  361. /* an ATAPI device ignores DRDY */
  362. drive->ready_stat = 0;
  363. }
  364. if (ide_disks & (1 << i)) {
  365. drive->cyl = drive->bios_cyl = ide_disks_chs[i].cyl;
  366. drive->head = drive->bios_head = ide_disks_chs[i].head;
  367. drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
  368. printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
  369. drive->name,
  370. drive->cyl, drive->head, drive->sect);
  371. drive->dev_flags |= IDE_DFLAG_FORCED_GEOM | IDE_DFLAG_PRESENT;
  372. drive->media = ide_disk;
  373. drive->ready_stat = ATA_DRDY;
  374. }
  375. }
  376. static unsigned int ide_ignore_cable;
  377. static int ide_set_ignore_cable(const char *s, struct kernel_param *kp)
  378. {
  379. int i, j = 1;
  380. /* controller (ignore) */
  381. /* controller : 1 (ignore) | 0 (use) */
  382. if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
  383. return -EINVAL;
  384. if (i >= MAX_HWIFS || j < 0 || j > 1)
  385. return -EINVAL;
  386. if (j)
  387. ide_ignore_cable |= (1 << i);
  388. else
  389. ide_ignore_cable &= ~(1 << i);
  390. return 0;
  391. }
  392. module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
  393. MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
  394. void ide_port_apply_params(ide_hwif_t *hwif)
  395. {
  396. ide_drive_t *drive;
  397. int i;
  398. if (ide_ignore_cable & (1 << hwif->index)) {
  399. printk(KERN_INFO "ide: ignoring cable detection for %s\n",
  400. hwif->name);
  401. hwif->cbl = ATA_CBL_PATA40_SHORT;
  402. }
  403. ide_port_for_each_dev(i, drive, hwif)
  404. ide_dev_apply_params(drive, i);
  405. }
  406. /*
  407. * This is gets invoked once during initialization, to set *everything* up
  408. */
  409. static int __init ide_init(void)
  410. {
  411. int ret;
  412. printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
  413. ret = bus_register(&ide_bus_type);
  414. if (ret < 0) {
  415. printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
  416. return ret;
  417. }
  418. ide_port_class = class_create(THIS_MODULE, "ide_port");
  419. if (IS_ERR(ide_port_class)) {
  420. ret = PTR_ERR(ide_port_class);
  421. goto out_port_class;
  422. }
  423. proc_ide_create();
  424. return 0;
  425. out_port_class:
  426. bus_unregister(&ide_bus_type);
  427. return ret;
  428. }
  429. static void __exit ide_exit(void)
  430. {
  431. proc_ide_destroy();
  432. class_destroy(ide_port_class);
  433. bus_unregister(&ide_bus_type);
  434. }
  435. module_init(ide_init);
  436. module_exit(ide_exit);
  437. MODULE_LICENSE("GPL");