ide.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. /*
  2. * linux/drivers/ide/ide.c Version 7.00beta2 Mar 05 2003
  3. *
  4. * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
  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. #define REVISION "Revision: 7.00alpha2"
  47. #define VERSION "Id: ide.c 7.00a2 20020906"
  48. #define _IDE_C /* Tell ide.h it's really us */
  49. #include <linux/module.h>
  50. #include <linux/types.h>
  51. #include <linux/string.h>
  52. #include <linux/kernel.h>
  53. #include <linux/timer.h>
  54. #include <linux/mm.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/major.h>
  57. #include <linux/errno.h>
  58. #include <linux/genhd.h>
  59. #include <linux/blkpg.h>
  60. #include <linux/slab.h>
  61. #include <linux/init.h>
  62. #include <linux/pci.h>
  63. #include <linux/delay.h>
  64. #include <linux/ide.h>
  65. #include <linux/completion.h>
  66. #include <linux/reboot.h>
  67. #include <linux/cdrom.h>
  68. #include <linux/seq_file.h>
  69. #include <linux/device.h>
  70. #include <linux/bitops.h>
  71. #include <asm/byteorder.h>
  72. #include <asm/irq.h>
  73. #include <asm/uaccess.h>
  74. #include <asm/io.h>
  75. /* default maximum number of failures */
  76. #define IDE_DEFAULT_MAX_FAILURES 1
  77. static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
  78. IDE2_MAJOR, IDE3_MAJOR,
  79. IDE4_MAJOR, IDE5_MAJOR,
  80. IDE6_MAJOR, IDE7_MAJOR,
  81. IDE8_MAJOR, IDE9_MAJOR };
  82. static int idebus_parameter; /* holds the "idebus=" parameter */
  83. static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
  84. DEFINE_MUTEX(ide_cfg_mtx);
  85. __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
  86. #ifdef CONFIG_IDEPCI_PCIBUS_ORDER
  87. static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
  88. #endif
  89. int noautodma = 0;
  90. #ifdef CONFIG_BLK_DEV_IDEACPI
  91. int ide_noacpi = 0;
  92. int ide_noacpitfs = 1;
  93. int ide_noacpionboot = 1;
  94. #endif
  95. /*
  96. * This is declared extern in ide.h, for access by other IDE modules:
  97. */
  98. ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
  99. EXPORT_SYMBOL(ide_hwifs);
  100. /*
  101. * Do not even *think* about calling this!
  102. */
  103. static void init_hwif_data(ide_hwif_t *hwif, unsigned int index)
  104. {
  105. unsigned int unit;
  106. /* bulk initialize hwif & drive info with zeros */
  107. memset(hwif, 0, sizeof(ide_hwif_t));
  108. /* fill in any non-zero initial values */
  109. hwif->index = index;
  110. hwif->major = ide_hwif_to_major[index];
  111. hwif->name[0] = 'i';
  112. hwif->name[1] = 'd';
  113. hwif->name[2] = 'e';
  114. hwif->name[3] = '0' + index;
  115. hwif->bus_state = BUSSTATE_ON;
  116. init_completion(&hwif->gendev_rel_comp);
  117. default_hwif_iops(hwif);
  118. default_hwif_transport(hwif);
  119. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  120. ide_drive_t *drive = &hwif->drives[unit];
  121. drive->media = ide_disk;
  122. drive->select.all = (unit<<4)|0xa0;
  123. drive->hwif = hwif;
  124. drive->ctl = 0x08;
  125. drive->ready_stat = READY_STAT;
  126. drive->bad_wstat = BAD_W_STAT;
  127. drive->special.b.recalibrate = 1;
  128. drive->special.b.set_geometry = 1;
  129. drive->name[0] = 'h';
  130. drive->name[1] = 'd';
  131. drive->name[2] = 'a' + (index * MAX_DRIVES) + unit;
  132. drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
  133. drive->using_dma = 0;
  134. drive->vdma = 0;
  135. INIT_LIST_HEAD(&drive->list);
  136. init_completion(&drive->gendev_rel_comp);
  137. }
  138. }
  139. static void init_hwif_default(ide_hwif_t *hwif, unsigned int index)
  140. {
  141. hw_regs_t hw;
  142. memset(&hw, 0, sizeof(hw_regs_t));
  143. ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
  144. memcpy(&hwif->hw, &hw, sizeof(hw));
  145. memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
  146. hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
  147. #ifdef CONFIG_BLK_DEV_HD
  148. if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
  149. hwif->noprobe = 1; /* may be overridden by ide_setup() */
  150. #endif
  151. }
  152. extern void ide_arm_init(void);
  153. /*
  154. * init_ide_data() sets reasonable default values into all fields
  155. * of all instances of the hwifs and drives, but only on the first call.
  156. * Subsequent calls have no effect (they don't wipe out anything).
  157. *
  158. * This routine is normally called at driver initialization time,
  159. * but may also be called MUCH earlier during kernel "command-line"
  160. * parameter processing. As such, we cannot depend on any other parts
  161. * of the kernel (such as memory allocation) to be functioning yet.
  162. *
  163. * This is too bad, as otherwise we could dynamically allocate the
  164. * ide_drive_t structs as needed, rather than always consuming memory
  165. * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
  166. *
  167. * FIXME: We should stuff the setup data into __init and copy the
  168. * relevant hwifs/allocate them properly during boot.
  169. */
  170. #define MAGIC_COOKIE 0x12345678
  171. static void __init init_ide_data (void)
  172. {
  173. ide_hwif_t *hwif;
  174. unsigned int index;
  175. static unsigned long magic_cookie = MAGIC_COOKIE;
  176. if (magic_cookie != MAGIC_COOKIE)
  177. return; /* already initialized */
  178. magic_cookie = 0;
  179. /* Initialise all interface structures */
  180. for (index = 0; index < MAX_HWIFS; ++index) {
  181. hwif = &ide_hwifs[index];
  182. init_hwif_data(hwif, index);
  183. init_hwif_default(hwif, index);
  184. #if !defined(CONFIG_PPC32) || !defined(CONFIG_PCI)
  185. hwif->irq = hwif->hw.irq =
  186. ide_init_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
  187. #endif
  188. }
  189. #ifdef CONFIG_IDE_ARM
  190. ide_arm_init();
  191. #endif
  192. }
  193. /**
  194. * ide_system_bus_speed - guess bus speed
  195. *
  196. * ide_system_bus_speed() returns what we think is the system VESA/PCI
  197. * bus speed (in MHz). This is used for calculating interface PIO timings.
  198. * The default is 40 for known PCI systems, 50 otherwise.
  199. * The "idebus=xx" parameter can be used to override this value.
  200. * The actual value to be used is computed/displayed the first time
  201. * through. Drivers should only use this as a last resort.
  202. *
  203. * Returns a guessed speed in MHz.
  204. */
  205. static int ide_system_bus_speed(void)
  206. {
  207. #ifdef CONFIG_PCI
  208. static struct pci_device_id pci_default[] = {
  209. { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
  210. { }
  211. };
  212. #else
  213. #define pci_default 0
  214. #endif /* CONFIG_PCI */
  215. if (!system_bus_speed) {
  216. if (idebus_parameter) {
  217. /* user supplied value */
  218. system_bus_speed = idebus_parameter;
  219. } else if (pci_dev_present(pci_default)) {
  220. /* safe default value for PCI */
  221. system_bus_speed = 33;
  222. } else {
  223. /* safe default value for VESA and PCI */
  224. system_bus_speed = 50;
  225. }
  226. printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
  227. "for PIO modes%s\n", system_bus_speed,
  228. idebus_parameter ? "" : "; override with idebus=xx");
  229. }
  230. return system_bus_speed;
  231. }
  232. ide_hwif_t * ide_find_port(unsigned long base)
  233. {
  234. ide_hwif_t *hwif;
  235. int i;
  236. for (i = 0; i < MAX_HWIFS; i++) {
  237. hwif = &ide_hwifs[i];
  238. if (hwif->io_ports[IDE_DATA_OFFSET] == base)
  239. goto found;
  240. }
  241. for (i = 0; i < MAX_HWIFS; i++) {
  242. hwif = &ide_hwifs[i];
  243. if (hwif->io_ports[IDE_DATA_OFFSET] == 0)
  244. goto found;
  245. }
  246. hwif = NULL;
  247. found:
  248. return hwif;
  249. }
  250. EXPORT_SYMBOL_GPL(ide_find_port);
  251. static struct resource* hwif_request_region(ide_hwif_t *hwif,
  252. unsigned long addr, int num)
  253. {
  254. struct resource *res = request_region(addr, num, hwif->name);
  255. if (!res)
  256. printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
  257. hwif->name, addr, addr+num-1);
  258. return res;
  259. }
  260. /**
  261. * ide_hwif_request_regions - request resources for IDE
  262. * @hwif: interface to use
  263. *
  264. * Requests all the needed resources for an interface.
  265. * Right now core IDE code does this work which is deeply wrong.
  266. * MMIO leaves it to the controller driver,
  267. * PIO will migrate this way over time.
  268. */
  269. int ide_hwif_request_regions(ide_hwif_t *hwif)
  270. {
  271. unsigned long addr;
  272. unsigned int i;
  273. if (hwif->mmio)
  274. return 0;
  275. addr = hwif->io_ports[IDE_CONTROL_OFFSET];
  276. if (addr && !hwif_request_region(hwif, addr, 1))
  277. goto control_region_busy;
  278. hwif->straight8 = 0;
  279. addr = hwif->io_ports[IDE_DATA_OFFSET];
  280. if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
  281. if (!hwif_request_region(hwif, addr, 8))
  282. goto data_region_busy;
  283. hwif->straight8 = 1;
  284. return 0;
  285. }
  286. for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
  287. addr = hwif->io_ports[i];
  288. if (!hwif_request_region(hwif, addr, 1)) {
  289. while (--i)
  290. release_region(addr, 1);
  291. goto data_region_busy;
  292. }
  293. }
  294. return 0;
  295. data_region_busy:
  296. addr = hwif->io_ports[IDE_CONTROL_OFFSET];
  297. if (addr)
  298. release_region(addr, 1);
  299. control_region_busy:
  300. /* If any errors are return, we drop the hwif interface. */
  301. return -EBUSY;
  302. }
  303. /**
  304. * ide_hwif_release_regions - free IDE resources
  305. *
  306. * Note that we only release the standard ports,
  307. * and do not even try to handle any extra ports
  308. * allocated for weird IDE interface chipsets.
  309. *
  310. * Note also that we don't yet handle mmio resources here. More
  311. * importantly our caller should be doing this so we need to
  312. * restructure this as a helper function for drivers.
  313. */
  314. void ide_hwif_release_regions(ide_hwif_t *hwif)
  315. {
  316. u32 i = 0;
  317. if (hwif->mmio)
  318. return;
  319. if (hwif->io_ports[IDE_CONTROL_OFFSET])
  320. release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
  321. if (hwif->straight8) {
  322. release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
  323. return;
  324. }
  325. for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
  326. if (hwif->io_ports[i])
  327. release_region(hwif->io_ports[i], 1);
  328. }
  329. /**
  330. * ide_hwif_restore - restore hwif to template
  331. * @hwif: hwif to update
  332. * @tmp_hwif: template
  333. *
  334. * Restore hwif to a previous state by copying most settings
  335. * from the template.
  336. */
  337. static void ide_hwif_restore(ide_hwif_t *hwif, ide_hwif_t *tmp_hwif)
  338. {
  339. hwif->hwgroup = tmp_hwif->hwgroup;
  340. hwif->gendev.parent = tmp_hwif->gendev.parent;
  341. hwif->proc = tmp_hwif->proc;
  342. hwif->major = tmp_hwif->major;
  343. hwif->straight8 = tmp_hwif->straight8;
  344. hwif->bus_state = tmp_hwif->bus_state;
  345. hwif->host_flags = tmp_hwif->host_flags;
  346. hwif->pio_mask = tmp_hwif->pio_mask;
  347. hwif->ultra_mask = tmp_hwif->ultra_mask;
  348. hwif->mwdma_mask = tmp_hwif->mwdma_mask;
  349. hwif->swdma_mask = tmp_hwif->swdma_mask;
  350. hwif->cbl = tmp_hwif->cbl;
  351. hwif->chipset = tmp_hwif->chipset;
  352. hwif->hold = tmp_hwif->hold;
  353. #ifdef CONFIG_BLK_DEV_IDEPCI
  354. hwif->pci_dev = tmp_hwif->pci_dev;
  355. hwif->cds = tmp_hwif->cds;
  356. #endif
  357. hwif->fixup = tmp_hwif->fixup;
  358. hwif->set_pio_mode = tmp_hwif->set_pio_mode;
  359. hwif->set_dma_mode = tmp_hwif->set_dma_mode;
  360. hwif->mdma_filter = tmp_hwif->mdma_filter;
  361. hwif->udma_filter = tmp_hwif->udma_filter;
  362. hwif->selectproc = tmp_hwif->selectproc;
  363. hwif->reset_poll = tmp_hwif->reset_poll;
  364. hwif->pre_reset = tmp_hwif->pre_reset;
  365. hwif->resetproc = tmp_hwif->resetproc;
  366. hwif->intrproc = tmp_hwif->intrproc;
  367. hwif->maskproc = tmp_hwif->maskproc;
  368. hwif->quirkproc = tmp_hwif->quirkproc;
  369. hwif->busproc = tmp_hwif->busproc;
  370. hwif->ata_input_data = tmp_hwif->ata_input_data;
  371. hwif->ata_output_data = tmp_hwif->ata_output_data;
  372. hwif->atapi_input_bytes = tmp_hwif->atapi_input_bytes;
  373. hwif->atapi_output_bytes = tmp_hwif->atapi_output_bytes;
  374. hwif->dma_setup = tmp_hwif->dma_setup;
  375. hwif->dma_exec_cmd = tmp_hwif->dma_exec_cmd;
  376. hwif->dma_start = tmp_hwif->dma_start;
  377. hwif->ide_dma_end = tmp_hwif->ide_dma_end;
  378. hwif->ide_dma_on = tmp_hwif->ide_dma_on;
  379. hwif->dma_off_quietly = tmp_hwif->dma_off_quietly;
  380. hwif->ide_dma_test_irq = tmp_hwif->ide_dma_test_irq;
  381. hwif->ide_dma_clear_irq = tmp_hwif->ide_dma_clear_irq;
  382. hwif->dma_host_on = tmp_hwif->dma_host_on;
  383. hwif->dma_host_off = tmp_hwif->dma_host_off;
  384. hwif->dma_lost_irq = tmp_hwif->dma_lost_irq;
  385. hwif->dma_timeout = tmp_hwif->dma_timeout;
  386. hwif->OUTB = tmp_hwif->OUTB;
  387. hwif->OUTBSYNC = tmp_hwif->OUTBSYNC;
  388. hwif->OUTW = tmp_hwif->OUTW;
  389. hwif->OUTSW = tmp_hwif->OUTSW;
  390. hwif->OUTSL = tmp_hwif->OUTSL;
  391. hwif->INB = tmp_hwif->INB;
  392. hwif->INW = tmp_hwif->INW;
  393. hwif->INSW = tmp_hwif->INSW;
  394. hwif->INSL = tmp_hwif->INSL;
  395. hwif->sg_max_nents = tmp_hwif->sg_max_nents;
  396. hwif->mmio = tmp_hwif->mmio;
  397. hwif->rqsize = tmp_hwif->rqsize;
  398. #ifndef CONFIG_BLK_DEV_IDECS
  399. hwif->irq = tmp_hwif->irq;
  400. #endif
  401. hwif->dma_base = tmp_hwif->dma_base;
  402. hwif->dma_master = tmp_hwif->dma_master;
  403. hwif->dma_command = tmp_hwif->dma_command;
  404. hwif->dma_vendor1 = tmp_hwif->dma_vendor1;
  405. hwif->dma_status = tmp_hwif->dma_status;
  406. hwif->dma_vendor3 = tmp_hwif->dma_vendor3;
  407. hwif->dma_prdtable = tmp_hwif->dma_prdtable;
  408. hwif->config_data = tmp_hwif->config_data;
  409. hwif->select_data = tmp_hwif->select_data;
  410. hwif->extra_base = tmp_hwif->extra_base;
  411. hwif->extra_ports = tmp_hwif->extra_ports;
  412. hwif->hwif_data = tmp_hwif->hwif_data;
  413. }
  414. /**
  415. * ide_unregister - free an IDE interface
  416. * @index: index of interface (will change soon to a pointer)
  417. *
  418. * Perform the final unregister of an IDE interface. At the moment
  419. * we don't refcount interfaces so this will also get split up.
  420. *
  421. * Locking:
  422. * The caller must not hold the IDE locks
  423. * The drive present/vanishing is not yet properly locked
  424. * Take care with the callbacks. These have been split to avoid
  425. * deadlocking the IDE layer. The shutdown callback is called
  426. * before we take the lock and free resources. It is up to the
  427. * caller to be sure there is no pending I/O here, and that
  428. * the interface will not be reopened (present/vanishing locking
  429. * isn't yet done BTW). After we commit to the final kill we
  430. * call the cleanup callback with the ide locks held.
  431. *
  432. * Unregister restores the hwif structures to the default state.
  433. * This is raving bonkers.
  434. */
  435. void ide_unregister(unsigned int index)
  436. {
  437. ide_drive_t *drive;
  438. ide_hwif_t *hwif, *g;
  439. static ide_hwif_t tmp_hwif; /* protected by ide_cfg_mtx */
  440. ide_hwgroup_t *hwgroup;
  441. int irq_count = 0, unit;
  442. BUG_ON(index >= MAX_HWIFS);
  443. BUG_ON(in_interrupt());
  444. BUG_ON(irqs_disabled());
  445. mutex_lock(&ide_cfg_mtx);
  446. spin_lock_irq(&ide_lock);
  447. hwif = &ide_hwifs[index];
  448. if (!hwif->present)
  449. goto abort;
  450. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  451. drive = &hwif->drives[unit];
  452. if (!drive->present)
  453. continue;
  454. spin_unlock_irq(&ide_lock);
  455. device_unregister(&drive->gendev);
  456. wait_for_completion(&drive->gendev_rel_comp);
  457. spin_lock_irq(&ide_lock);
  458. }
  459. hwif->present = 0;
  460. spin_unlock_irq(&ide_lock);
  461. ide_proc_unregister_port(hwif);
  462. hwgroup = hwif->hwgroup;
  463. /*
  464. * free the irq if we were the only hwif using it
  465. */
  466. g = hwgroup->hwif;
  467. do {
  468. if (g->irq == hwif->irq)
  469. ++irq_count;
  470. g = g->next;
  471. } while (g != hwgroup->hwif);
  472. if (irq_count == 1)
  473. free_irq(hwif->irq, hwgroup);
  474. spin_lock_irq(&ide_lock);
  475. /*
  476. * Note that we only release the standard ports,
  477. * and do not even try to handle any extra ports
  478. * allocated for weird IDE interface chipsets.
  479. */
  480. ide_hwif_release_regions(hwif);
  481. /*
  482. * Remove us from the hwgroup, and free
  483. * the hwgroup if we were the only member
  484. */
  485. if (hwif->next == hwif) {
  486. BUG_ON(hwgroup->hwif != hwif);
  487. kfree(hwgroup);
  488. } else {
  489. /* There is another interface in hwgroup.
  490. * Unlink us, and set hwgroup->drive and ->hwif to
  491. * something sane.
  492. */
  493. g = hwgroup->hwif;
  494. while (g->next != hwif)
  495. g = g->next;
  496. g->next = hwif->next;
  497. if (hwgroup->hwif == hwif) {
  498. /* Chose a random hwif for hwgroup->hwif.
  499. * It's guaranteed that there are no drives
  500. * left in the hwgroup.
  501. */
  502. BUG_ON(hwgroup->drive != NULL);
  503. hwgroup->hwif = g;
  504. }
  505. BUG_ON(hwgroup->hwif == hwif);
  506. }
  507. /* More messed up locking ... */
  508. spin_unlock_irq(&ide_lock);
  509. device_unregister(&hwif->gendev);
  510. wait_for_completion(&hwif->gendev_rel_comp);
  511. /*
  512. * Remove us from the kernel's knowledge
  513. */
  514. blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
  515. kfree(hwif->sg_table);
  516. unregister_blkdev(hwif->major, hwif->name);
  517. spin_lock_irq(&ide_lock);
  518. if (hwif->dma_base) {
  519. (void) ide_release_dma(hwif);
  520. hwif->dma_base = 0;
  521. hwif->dma_master = 0;
  522. hwif->dma_command = 0;
  523. hwif->dma_vendor1 = 0;
  524. hwif->dma_status = 0;
  525. hwif->dma_vendor3 = 0;
  526. hwif->dma_prdtable = 0;
  527. hwif->extra_base = 0;
  528. hwif->extra_ports = 0;
  529. }
  530. /* copy original settings */
  531. tmp_hwif = *hwif;
  532. /* restore hwif data to pristine status */
  533. init_hwif_data(hwif, index);
  534. init_hwif_default(hwif, index);
  535. ide_hwif_restore(hwif, &tmp_hwif);
  536. abort:
  537. spin_unlock_irq(&ide_lock);
  538. mutex_unlock(&ide_cfg_mtx);
  539. }
  540. EXPORT_SYMBOL(ide_unregister);
  541. /**
  542. * ide_setup_ports - set up IDE interface ports
  543. * @hw: register descriptions
  544. * @base: base register
  545. * @offsets: table of register offsets
  546. * @ctrl: control register
  547. * @ack_irq: IRQ ack
  548. * @irq: interrupt lie
  549. *
  550. * Setup hw_regs_t structure described by parameters. You
  551. * may set up the hw structure yourself OR use this routine to
  552. * do it for you. This is basically a helper
  553. *
  554. */
  555. void ide_setup_ports ( hw_regs_t *hw,
  556. unsigned long base, int *offsets,
  557. unsigned long ctrl, unsigned long intr,
  558. ide_ack_intr_t *ack_intr,
  559. /*
  560. * ide_io_ops_t *iops,
  561. */
  562. int irq)
  563. {
  564. int i;
  565. memset(hw, 0, sizeof(hw_regs_t));
  566. for (i = 0; i < IDE_NR_PORTS; i++) {
  567. if (offsets[i] == -1) {
  568. switch(i) {
  569. case IDE_CONTROL_OFFSET:
  570. hw->io_ports[i] = ctrl;
  571. break;
  572. #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
  573. case IDE_IRQ_OFFSET:
  574. hw->io_ports[i] = intr;
  575. break;
  576. #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
  577. default:
  578. hw->io_ports[i] = 0;
  579. break;
  580. }
  581. } else {
  582. hw->io_ports[i] = base + offsets[i];
  583. }
  584. }
  585. hw->irq = irq;
  586. hw->ack_intr = ack_intr;
  587. /*
  588. * hw->iops = iops;
  589. */
  590. }
  591. /**
  592. * ide_register_hw - register IDE interface
  593. * @hw: hardware registers
  594. * @fixup: fixup function
  595. * @initializing: set while initializing built-in drivers
  596. * @hwifp: pointer to returned hwif
  597. *
  598. * Register an IDE interface, specifying exactly the registers etc.
  599. * Set init=1 iff calling before probes have taken place.
  600. *
  601. * Returns -1 on error.
  602. */
  603. int ide_register_hw(hw_regs_t *hw, void (*fixup)(ide_hwif_t *),
  604. int initializing, ide_hwif_t **hwifp)
  605. {
  606. int index, retry = 1;
  607. ide_hwif_t *hwif;
  608. do {
  609. for (index = 0; index < MAX_HWIFS; ++index) {
  610. hwif = &ide_hwifs[index];
  611. if (hwif->io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
  612. goto found;
  613. }
  614. for (index = 0; index < MAX_HWIFS; ++index) {
  615. hwif = &ide_hwifs[index];
  616. if (hwif->hold)
  617. continue;
  618. if ((!hwif->present && !hwif->mate && !initializing) ||
  619. (!hwif->io_ports[IDE_DATA_OFFSET] && initializing))
  620. goto found;
  621. }
  622. for (index = 0; index < MAX_HWIFS; index++)
  623. ide_unregister(index);
  624. } while (retry--);
  625. return -1;
  626. found:
  627. if (hwif->present)
  628. ide_unregister(index);
  629. else if (!hwif->hold) {
  630. init_hwif_data(hwif, index);
  631. init_hwif_default(hwif, index);
  632. }
  633. if (hwif->present)
  634. return -1;
  635. memcpy(&hwif->hw, hw, sizeof(*hw));
  636. memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
  637. hwif->irq = hw->irq;
  638. hwif->noprobe = 0;
  639. hwif->fixup = fixup;
  640. hwif->chipset = hw->chipset;
  641. hwif->gendev.parent = hw->dev;
  642. hwif->ack_intr = hw->ack_intr;
  643. if (initializing == 0) {
  644. u8 idx[4] = { index, 0xff, 0xff, 0xff };
  645. ide_device_add(idx);
  646. }
  647. if (hwifp)
  648. *hwifp = hwif;
  649. return (initializing || hwif->present) ? index : -1;
  650. }
  651. EXPORT_SYMBOL(ide_register_hw);
  652. /*
  653. * Locks for IDE setting functionality
  654. */
  655. DEFINE_MUTEX(ide_setting_mtx);
  656. EXPORT_SYMBOL_GPL(ide_setting_mtx);
  657. /**
  658. * ide_spin_wait_hwgroup - wait for group
  659. * @drive: drive in the group
  660. *
  661. * Wait for an IDE device group to go non busy and then return
  662. * holding the ide_lock which guards the hwgroup->busy status
  663. * and right to use it.
  664. */
  665. int ide_spin_wait_hwgroup (ide_drive_t *drive)
  666. {
  667. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  668. unsigned long timeout = jiffies + (3 * HZ);
  669. spin_lock_irq(&ide_lock);
  670. while (hwgroup->busy) {
  671. unsigned long lflags;
  672. spin_unlock_irq(&ide_lock);
  673. local_irq_set(lflags);
  674. if (time_after(jiffies, timeout)) {
  675. local_irq_restore(lflags);
  676. printk(KERN_ERR "%s: channel busy\n", drive->name);
  677. return -EBUSY;
  678. }
  679. local_irq_restore(lflags);
  680. spin_lock_irq(&ide_lock);
  681. }
  682. return 0;
  683. }
  684. EXPORT_SYMBOL(ide_spin_wait_hwgroup);
  685. int set_io_32bit(ide_drive_t *drive, int arg)
  686. {
  687. if (drive->no_io_32bit)
  688. return -EPERM;
  689. if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
  690. return -EINVAL;
  691. drive->io_32bit = arg;
  692. #ifdef CONFIG_BLK_DEV_DTC2278
  693. if (HWIF(drive)->chipset == ide_dtc2278)
  694. HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
  695. #endif /* CONFIG_BLK_DEV_DTC2278 */
  696. return 0;
  697. }
  698. static int set_ksettings(ide_drive_t *drive, int arg)
  699. {
  700. if (arg < 0 || arg > 1)
  701. return -EINVAL;
  702. if (ide_spin_wait_hwgroup(drive))
  703. return -EBUSY;
  704. drive->keep_settings = arg;
  705. spin_unlock_irq(&ide_lock);
  706. return 0;
  707. }
  708. int set_using_dma(ide_drive_t *drive, int arg)
  709. {
  710. #ifdef CONFIG_BLK_DEV_IDEDMA
  711. ide_hwif_t *hwif = drive->hwif;
  712. int err = -EPERM;
  713. if (arg < 0 || arg > 1)
  714. return -EINVAL;
  715. if (!drive->id || !(drive->id->capability & 1))
  716. goto out;
  717. if (hwif->ide_dma_on == NULL)
  718. goto out;
  719. err = -EBUSY;
  720. if (ide_spin_wait_hwgroup(drive))
  721. goto out;
  722. /*
  723. * set ->busy flag, unlock and let it ride
  724. */
  725. hwif->hwgroup->busy = 1;
  726. spin_unlock_irq(&ide_lock);
  727. err = 0;
  728. if (arg) {
  729. hwif->dma_off_quietly(drive);
  730. if (ide_set_dma(drive) || hwif->ide_dma_on(drive))
  731. err = -EIO;
  732. } else
  733. ide_dma_off(drive);
  734. /*
  735. * lock, clear ->busy flag and unlock before leaving
  736. */
  737. spin_lock_irq(&ide_lock);
  738. hwif->hwgroup->busy = 0;
  739. spin_unlock_irq(&ide_lock);
  740. out:
  741. return err;
  742. #else
  743. if (arg < 0 || arg > 1)
  744. return -EINVAL;
  745. return -EPERM;
  746. #endif
  747. }
  748. int set_pio_mode(ide_drive_t *drive, int arg)
  749. {
  750. struct request rq;
  751. if (arg < 0 || arg > 255)
  752. return -EINVAL;
  753. if (drive->hwif->set_pio_mode == NULL)
  754. return -ENOSYS;
  755. if (drive->special.b.set_tune)
  756. return -EBUSY;
  757. ide_init_drive_cmd(&rq);
  758. drive->tune_req = (u8) arg;
  759. drive->special.b.set_tune = 1;
  760. (void) ide_do_drive_cmd(drive, &rq, ide_wait);
  761. return 0;
  762. }
  763. static int set_unmaskirq(ide_drive_t *drive, int arg)
  764. {
  765. if (drive->no_unmask)
  766. return -EPERM;
  767. if (arg < 0 || arg > 1)
  768. return -EINVAL;
  769. if (ide_spin_wait_hwgroup(drive))
  770. return -EBUSY;
  771. drive->unmask = arg;
  772. spin_unlock_irq(&ide_lock);
  773. return 0;
  774. }
  775. /**
  776. * system_bus_clock - clock guess
  777. *
  778. * External version of the bus clock guess used by very old IDE drivers
  779. * for things like VLB timings. Should not be used.
  780. */
  781. int system_bus_clock (void)
  782. {
  783. return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
  784. }
  785. EXPORT_SYMBOL(system_bus_clock);
  786. static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
  787. {
  788. ide_drive_t *drive = dev->driver_data;
  789. ide_hwif_t *hwif = HWIF(drive);
  790. struct request rq;
  791. struct request_pm_state rqpm;
  792. ide_task_t args;
  793. int ret;
  794. /* Call ACPI _GTM only once */
  795. if (!(drive->dn % 2))
  796. ide_acpi_get_timing(hwif);
  797. memset(&rq, 0, sizeof(rq));
  798. memset(&rqpm, 0, sizeof(rqpm));
  799. memset(&args, 0, sizeof(args));
  800. rq.cmd_type = REQ_TYPE_PM_SUSPEND;
  801. rq.special = &args;
  802. rq.data = &rqpm;
  803. rqpm.pm_step = ide_pm_state_start_suspend;
  804. if (mesg.event == PM_EVENT_PRETHAW)
  805. mesg.event = PM_EVENT_FREEZE;
  806. rqpm.pm_state = mesg.event;
  807. ret = ide_do_drive_cmd(drive, &rq, ide_wait);
  808. /* only call ACPI _PS3 after both drivers are suspended */
  809. if (!ret && (((drive->dn % 2) && hwif->drives[0].present
  810. && hwif->drives[1].present)
  811. || !hwif->drives[0].present
  812. || !hwif->drives[1].present))
  813. ide_acpi_set_state(hwif, 0);
  814. return ret;
  815. }
  816. static int generic_ide_resume(struct device *dev)
  817. {
  818. ide_drive_t *drive = dev->driver_data;
  819. ide_hwif_t *hwif = HWIF(drive);
  820. struct request rq;
  821. struct request_pm_state rqpm;
  822. ide_task_t args;
  823. int err;
  824. /* Call ACPI _STM only once */
  825. if (!(drive->dn % 2)) {
  826. ide_acpi_set_state(hwif, 1);
  827. ide_acpi_push_timing(hwif);
  828. }
  829. ide_acpi_exec_tfs(drive);
  830. memset(&rq, 0, sizeof(rq));
  831. memset(&rqpm, 0, sizeof(rqpm));
  832. memset(&args, 0, sizeof(args));
  833. rq.cmd_type = REQ_TYPE_PM_RESUME;
  834. rq.special = &args;
  835. rq.data = &rqpm;
  836. rqpm.pm_step = ide_pm_state_start_resume;
  837. rqpm.pm_state = PM_EVENT_ON;
  838. err = ide_do_drive_cmd(drive, &rq, ide_head_wait);
  839. if (err == 0 && dev->driver) {
  840. ide_driver_t *drv = to_ide_driver(dev->driver);
  841. if (drv->resume)
  842. drv->resume(drive);
  843. }
  844. return err;
  845. }
  846. int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
  847. unsigned int cmd, unsigned long arg)
  848. {
  849. unsigned long flags;
  850. ide_driver_t *drv;
  851. void __user *p = (void __user *)arg;
  852. int err = 0, (*setfunc)(ide_drive_t *, int);
  853. u8 *val;
  854. switch (cmd) {
  855. case HDIO_GET_32BIT: val = &drive->io_32bit; goto read_val;
  856. case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val;
  857. case HDIO_GET_UNMASKINTR: val = &drive->unmask; goto read_val;
  858. case HDIO_GET_DMA: val = &drive->using_dma; goto read_val;
  859. case HDIO_SET_32BIT: setfunc = set_io_32bit; goto set_val;
  860. case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings; goto set_val;
  861. case HDIO_SET_PIO_MODE: setfunc = set_pio_mode; goto set_val;
  862. case HDIO_SET_UNMASKINTR: setfunc = set_unmaskirq; goto set_val;
  863. case HDIO_SET_DMA: setfunc = set_using_dma; goto set_val;
  864. }
  865. switch (cmd) {
  866. case HDIO_OBSOLETE_IDENTITY:
  867. case HDIO_GET_IDENTITY:
  868. if (bdev != bdev->bd_contains)
  869. return -EINVAL;
  870. if (drive->id_read == 0)
  871. return -ENOMSG;
  872. if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
  873. return -EFAULT;
  874. return 0;
  875. case HDIO_GET_NICE:
  876. return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
  877. drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
  878. drive->nice0 << IDE_NICE_0 |
  879. drive->nice1 << IDE_NICE_1 |
  880. drive->nice2 << IDE_NICE_2,
  881. (long __user *) arg);
  882. #ifdef CONFIG_IDE_TASK_IOCTL
  883. case HDIO_DRIVE_TASKFILE:
  884. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  885. return -EACCES;
  886. switch(drive->media) {
  887. case ide_disk:
  888. return ide_taskfile_ioctl(drive, cmd, arg);
  889. default:
  890. return -ENOMSG;
  891. }
  892. #endif /* CONFIG_IDE_TASK_IOCTL */
  893. case HDIO_DRIVE_CMD:
  894. if (!capable(CAP_SYS_RAWIO))
  895. return -EACCES;
  896. return ide_cmd_ioctl(drive, cmd, arg);
  897. case HDIO_DRIVE_TASK:
  898. if (!capable(CAP_SYS_RAWIO))
  899. return -EACCES;
  900. return ide_task_ioctl(drive, cmd, arg);
  901. case HDIO_SCAN_HWIF:
  902. {
  903. hw_regs_t hw;
  904. int args[3];
  905. if (!capable(CAP_SYS_RAWIO)) return -EACCES;
  906. if (copy_from_user(args, p, 3 * sizeof(int)))
  907. return -EFAULT;
  908. memset(&hw, 0, sizeof(hw));
  909. ide_init_hwif_ports(&hw, (unsigned long) args[0],
  910. (unsigned long) args[1], NULL);
  911. hw.irq = args[2];
  912. if (ide_register_hw(&hw, NULL, 0, NULL) == -1)
  913. return -EIO;
  914. return 0;
  915. }
  916. case HDIO_UNREGISTER_HWIF:
  917. if (!capable(CAP_SYS_RAWIO)) return -EACCES;
  918. /* (arg > MAX_HWIFS) checked in function */
  919. ide_unregister(arg);
  920. return 0;
  921. case HDIO_SET_NICE:
  922. if (!capable(CAP_SYS_ADMIN)) return -EACCES;
  923. if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
  924. return -EPERM;
  925. drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
  926. drv = *(ide_driver_t **)bdev->bd_disk->private_data;
  927. if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
  928. drive->dsc_overlap = 0;
  929. return -EPERM;
  930. }
  931. drive->nice1 = (arg >> IDE_NICE_1) & 1;
  932. return 0;
  933. case HDIO_DRIVE_RESET:
  934. {
  935. unsigned long flags;
  936. if (!capable(CAP_SYS_ADMIN)) return -EACCES;
  937. /*
  938. * Abort the current command on the
  939. * group if there is one, taking
  940. * care not to allow anything else
  941. * to be queued and to die on the
  942. * spot if we miss one somehow
  943. */
  944. spin_lock_irqsave(&ide_lock, flags);
  945. if (HWGROUP(drive)->resetting) {
  946. spin_unlock_irqrestore(&ide_lock, flags);
  947. return -EBUSY;
  948. }
  949. ide_abort(drive, "drive reset");
  950. BUG_ON(HWGROUP(drive)->handler);
  951. /* Ensure nothing gets queued after we
  952. drop the lock. Reset will clear the busy */
  953. HWGROUP(drive)->busy = 1;
  954. spin_unlock_irqrestore(&ide_lock, flags);
  955. (void) ide_do_reset(drive);
  956. return 0;
  957. }
  958. case HDIO_GET_BUSSTATE:
  959. if (!capable(CAP_SYS_ADMIN))
  960. return -EACCES;
  961. if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
  962. return -EFAULT;
  963. return 0;
  964. case HDIO_SET_BUSSTATE:
  965. if (!capable(CAP_SYS_ADMIN))
  966. return -EACCES;
  967. if (HWIF(drive)->busproc)
  968. return HWIF(drive)->busproc(drive, (int)arg);
  969. return -EOPNOTSUPP;
  970. default:
  971. return -EINVAL;
  972. }
  973. read_val:
  974. mutex_lock(&ide_setting_mtx);
  975. spin_lock_irqsave(&ide_lock, flags);
  976. err = *val;
  977. spin_unlock_irqrestore(&ide_lock, flags);
  978. mutex_unlock(&ide_setting_mtx);
  979. return err >= 0 ? put_user(err, (long __user *)arg) : err;
  980. set_val:
  981. if (bdev != bdev->bd_contains)
  982. err = -EINVAL;
  983. else {
  984. if (!capable(CAP_SYS_ADMIN))
  985. err = -EACCES;
  986. else {
  987. mutex_lock(&ide_setting_mtx);
  988. err = setfunc(drive, arg);
  989. mutex_unlock(&ide_setting_mtx);
  990. }
  991. }
  992. return err;
  993. }
  994. EXPORT_SYMBOL(generic_ide_ioctl);
  995. /*
  996. * stridx() returns the offset of c within s,
  997. * or -1 if c is '\0' or not found within s.
  998. */
  999. static int __init stridx (const char *s, char c)
  1000. {
  1001. char *i = strchr(s, c);
  1002. return (i && c) ? i - s : -1;
  1003. }
  1004. /*
  1005. * match_parm() does parsing for ide_setup():
  1006. *
  1007. * 1. the first char of s must be '='.
  1008. * 2. if the remainder matches one of the supplied keywords,
  1009. * the index (1 based) of the keyword is negated and returned.
  1010. * 3. if the remainder is a series of no more than max_vals numbers
  1011. * separated by commas, the numbers are saved in vals[] and a
  1012. * count of how many were saved is returned. Base10 is assumed,
  1013. * and base16 is allowed when prefixed with "0x".
  1014. * 4. otherwise, zero is returned.
  1015. */
  1016. static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
  1017. {
  1018. static const char *decimal = "0123456789";
  1019. static const char *hex = "0123456789abcdef";
  1020. int i, n;
  1021. if (*s++ == '=') {
  1022. /*
  1023. * Try matching against the supplied keywords,
  1024. * and return -(index+1) if we match one
  1025. */
  1026. if (keywords != NULL) {
  1027. for (i = 0; *keywords != NULL; ++i) {
  1028. if (!strcmp(s, *keywords++))
  1029. return -(i+1);
  1030. }
  1031. }
  1032. /*
  1033. * Look for a series of no more than "max_vals"
  1034. * numeric values separated by commas, in base10,
  1035. * or base16 when prefixed with "0x".
  1036. * Return a count of how many were found.
  1037. */
  1038. for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
  1039. vals[n] = i;
  1040. while ((i = stridx(decimal, *++s)) >= 0)
  1041. vals[n] = (vals[n] * 10) + i;
  1042. if (*s == 'x' && !vals[n]) {
  1043. while ((i = stridx(hex, *++s)) >= 0)
  1044. vals[n] = (vals[n] * 0x10) + i;
  1045. }
  1046. if (++n == max_vals)
  1047. break;
  1048. if (*s == ',' || *s == ';')
  1049. ++s;
  1050. }
  1051. if (!*s)
  1052. return n;
  1053. }
  1054. return 0; /* zero = nothing matched */
  1055. }
  1056. #ifdef CONFIG_BLK_DEV_ALI14XX
  1057. extern int probe_ali14xx;
  1058. extern int ali14xx_init(void);
  1059. #endif
  1060. #ifdef CONFIG_BLK_DEV_UMC8672
  1061. extern int probe_umc8672;
  1062. extern int umc8672_init(void);
  1063. #endif
  1064. #ifdef CONFIG_BLK_DEV_DTC2278
  1065. extern int probe_dtc2278;
  1066. extern int dtc2278_init(void);
  1067. #endif
  1068. #ifdef CONFIG_BLK_DEV_HT6560B
  1069. extern int probe_ht6560b;
  1070. extern int ht6560b_init(void);
  1071. #endif
  1072. #ifdef CONFIG_BLK_DEV_QD65XX
  1073. extern int probe_qd65xx;
  1074. extern int qd65xx_init(void);
  1075. #endif
  1076. static int __initdata is_chipset_set[MAX_HWIFS];
  1077. /*
  1078. * ide_setup() gets called VERY EARLY during initialization,
  1079. * to handle kernel "command line" strings beginning with "hdx=" or "ide".
  1080. *
  1081. * Remember to update Documentation/ide.txt if you change something here.
  1082. */
  1083. static int __init ide_setup(char *s)
  1084. {
  1085. int i, vals[3];
  1086. ide_hwif_t *hwif;
  1087. ide_drive_t *drive;
  1088. unsigned int hw, unit;
  1089. const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
  1090. const char max_hwif = '0' + (MAX_HWIFS - 1);
  1091. if (strncmp(s,"hd",2) == 0 && s[2] == '=') /* hd= is for hd.c */
  1092. return 0; /* driver and not us */
  1093. if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
  1094. return 0;
  1095. printk(KERN_INFO "ide_setup: %s", s);
  1096. init_ide_data ();
  1097. #ifdef CONFIG_BLK_DEV_IDEDOUBLER
  1098. if (!strcmp(s, "ide=doubler")) {
  1099. extern int ide_doubler;
  1100. printk(" : Enabled support for IDE doublers\n");
  1101. ide_doubler = 1;
  1102. return 1;
  1103. }
  1104. #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
  1105. if (!strcmp(s, "ide=nodma")) {
  1106. printk(" : Prevented DMA\n");
  1107. noautodma = 1;
  1108. goto obsolete_option;
  1109. }
  1110. #ifdef CONFIG_IDEPCI_PCIBUS_ORDER
  1111. if (!strcmp(s, "ide=reverse")) {
  1112. ide_scan_direction = 1;
  1113. printk(" : Enabled support for IDE inverse scan order.\n");
  1114. return 1;
  1115. }
  1116. #endif
  1117. #ifdef CONFIG_BLK_DEV_IDEACPI
  1118. if (!strcmp(s, "ide=noacpi")) {
  1119. //printk(" : Disable IDE ACPI support.\n");
  1120. ide_noacpi = 1;
  1121. return 1;
  1122. }
  1123. if (!strcmp(s, "ide=acpigtf")) {
  1124. //printk(" : Enable IDE ACPI _GTF support.\n");
  1125. ide_noacpitfs = 0;
  1126. return 1;
  1127. }
  1128. if (!strcmp(s, "ide=acpionboot")) {
  1129. //printk(" : Call IDE ACPI methods on boot.\n");
  1130. ide_noacpionboot = 0;
  1131. return 1;
  1132. }
  1133. #endif /* CONFIG_BLK_DEV_IDEACPI */
  1134. /*
  1135. * Look for drive options: "hdx="
  1136. */
  1137. if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
  1138. const char *hd_words[] = {
  1139. "none", "noprobe", "nowerr", "cdrom", "nodma",
  1140. "autotune", "noautotune", "minus8", "swapdata", "bswap",
  1141. "noflush", "remap", "remap63", "scsi", NULL };
  1142. unit = s[2] - 'a';
  1143. hw = unit / MAX_DRIVES;
  1144. unit = unit % MAX_DRIVES;
  1145. hwif = &ide_hwifs[hw];
  1146. drive = &hwif->drives[unit];
  1147. if (strncmp(s + 4, "ide-", 4) == 0) {
  1148. strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
  1149. goto done;
  1150. }
  1151. switch (match_parm(&s[3], hd_words, vals, 3)) {
  1152. case -1: /* "none" */
  1153. case -2: /* "noprobe" */
  1154. drive->noprobe = 1;
  1155. goto done;
  1156. case -3: /* "nowerr" */
  1157. drive->bad_wstat = BAD_R_STAT;
  1158. hwif->noprobe = 0;
  1159. goto done;
  1160. case -4: /* "cdrom" */
  1161. drive->present = 1;
  1162. drive->media = ide_cdrom;
  1163. /* an ATAPI device ignores DRDY */
  1164. drive->ready_stat = 0;
  1165. hwif->noprobe = 0;
  1166. goto done;
  1167. case -5: /* nodma */
  1168. drive->nodma = 1;
  1169. goto done;
  1170. case -6: /* "autotune" */
  1171. drive->autotune = IDE_TUNE_AUTO;
  1172. goto obsolete_option;
  1173. case -7: /* "noautotune" */
  1174. drive->autotune = IDE_TUNE_NOAUTO;
  1175. goto obsolete_option;
  1176. case -9: /* "swapdata" */
  1177. case -10: /* "bswap" */
  1178. drive->bswap = 1;
  1179. goto done;
  1180. case -11: /* noflush */
  1181. drive->noflush = 1;
  1182. goto done;
  1183. case -12: /* "remap" */
  1184. drive->remap_0_to_1 = 1;
  1185. goto done;
  1186. case -13: /* "remap63" */
  1187. drive->sect0 = 63;
  1188. goto done;
  1189. case -14: /* "scsi" */
  1190. drive->scsi = 1;
  1191. goto done;
  1192. case 3: /* cyl,head,sect */
  1193. drive->media = ide_disk;
  1194. drive->ready_stat = READY_STAT;
  1195. drive->cyl = drive->bios_cyl = vals[0];
  1196. drive->head = drive->bios_head = vals[1];
  1197. drive->sect = drive->bios_sect = vals[2];
  1198. drive->present = 1;
  1199. drive->forced_geom = 1;
  1200. hwif->noprobe = 0;
  1201. goto done;
  1202. default:
  1203. goto bad_option;
  1204. }
  1205. }
  1206. if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
  1207. goto bad_option;
  1208. /*
  1209. * Look for bus speed option: "idebus="
  1210. */
  1211. if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
  1212. if (match_parm(&s[6], NULL, vals, 1) != 1)
  1213. goto bad_option;
  1214. if (vals[0] >= 20 && vals[0] <= 66) {
  1215. idebus_parameter = vals[0];
  1216. } else
  1217. printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
  1218. goto done;
  1219. }
  1220. /*
  1221. * Look for interface options: "idex="
  1222. */
  1223. if (s[3] >= '0' && s[3] <= max_hwif) {
  1224. /*
  1225. * Be VERY CAREFUL changing this: note hardcoded indexes below
  1226. * (-8, -9, -10) are reserved to ease the hardcoding.
  1227. */
  1228. static const char *ide_words[] = {
  1229. "noprobe", "serialize", "minus3", "minus4",
  1230. "reset", "minus6", "ata66", "minus8", "minus9",
  1231. "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
  1232. "dtc2278", "umc8672", "ali14xx", NULL };
  1233. hw = s[3] - '0';
  1234. hwif = &ide_hwifs[hw];
  1235. i = match_parm(&s[4], ide_words, vals, 3);
  1236. /*
  1237. * Cryptic check to ensure chipset not already set for hwif.
  1238. * Note: we can't depend on hwif->chipset here.
  1239. */
  1240. if ((i >= -18 && i <= -11) || (i > 0 && i <= 3)) {
  1241. /* chipset already specified */
  1242. if (is_chipset_set[hw])
  1243. goto bad_option;
  1244. if (i > -18 && i <= -11) {
  1245. /* these drivers are for "ide0=" only */
  1246. if (hw != 0)
  1247. goto bad_hwif;
  1248. /* chipset already specified for 2nd port */
  1249. if (is_chipset_set[hw+1])
  1250. goto bad_option;
  1251. }
  1252. is_chipset_set[hw] = 1;
  1253. printk("\n");
  1254. }
  1255. switch (i) {
  1256. #ifdef CONFIG_BLK_DEV_ALI14XX
  1257. case -17: /* "ali14xx" */
  1258. probe_ali14xx = 1;
  1259. goto done;
  1260. #endif
  1261. #ifdef CONFIG_BLK_DEV_UMC8672
  1262. case -16: /* "umc8672" */
  1263. probe_umc8672 = 1;
  1264. goto done;
  1265. #endif
  1266. #ifdef CONFIG_BLK_DEV_DTC2278
  1267. case -15: /* "dtc2278" */
  1268. probe_dtc2278 = 1;
  1269. goto done;
  1270. #endif
  1271. #ifdef CONFIG_BLK_DEV_CMD640
  1272. case -14: /* "cmd640_vlb" */
  1273. {
  1274. extern int cmd640_vlb; /* flag for cmd640.c */
  1275. cmd640_vlb = 1;
  1276. goto done;
  1277. }
  1278. #endif
  1279. #ifdef CONFIG_BLK_DEV_HT6560B
  1280. case -13: /* "ht6560b" */
  1281. probe_ht6560b = 1;
  1282. goto done;
  1283. #endif
  1284. #ifdef CONFIG_BLK_DEV_QD65XX
  1285. case -12: /* "qd65xx" */
  1286. probe_qd65xx = 1;
  1287. goto done;
  1288. #endif
  1289. #ifdef CONFIG_BLK_DEV_4DRIVES
  1290. case -11: /* "four" drives on one set of ports */
  1291. {
  1292. ide_hwif_t *mate = &ide_hwifs[hw^1];
  1293. mate->drives[0].select.all ^= 0x20;
  1294. mate->drives[1].select.all ^= 0x20;
  1295. hwif->chipset = mate->chipset = ide_4drives;
  1296. mate->irq = hwif->irq;
  1297. memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
  1298. hwif->mate = mate;
  1299. mate->mate = hwif;
  1300. hwif->serialized = mate->serialized = 1;
  1301. goto obsolete_option;
  1302. }
  1303. #endif /* CONFIG_BLK_DEV_4DRIVES */
  1304. case -10: /* minus10 */
  1305. case -9: /* minus9 */
  1306. case -8: /* minus8 */
  1307. case -6:
  1308. case -4:
  1309. case -3:
  1310. goto bad_option;
  1311. case -7: /* ata66 */
  1312. #ifdef CONFIG_BLK_DEV_IDEPCI
  1313. /*
  1314. * Use ATA_CBL_PATA40_SHORT so drive side
  1315. * cable detection is also overriden.
  1316. */
  1317. hwif->cbl = ATA_CBL_PATA40_SHORT;
  1318. goto obsolete_option;
  1319. #else
  1320. goto bad_hwif;
  1321. #endif
  1322. case -5: /* "reset" */
  1323. hwif->reset = 1;
  1324. goto obsolete_option;
  1325. case -2: /* "serialize" */
  1326. hwif->mate = &ide_hwifs[hw^1];
  1327. hwif->mate->mate = hwif;
  1328. hwif->serialized = hwif->mate->serialized = 1;
  1329. goto obsolete_option;
  1330. case -1: /* "noprobe" */
  1331. hwif->noprobe = 1;
  1332. goto done;
  1333. case 1: /* base */
  1334. vals[1] = vals[0] + 0x206; /* default ctl */
  1335. case 2: /* base,ctl */
  1336. vals[2] = 0; /* default irq = probe for it */
  1337. case 3: /* base,ctl,irq */
  1338. hwif->hw.irq = vals[2];
  1339. ide_init_hwif_ports(&hwif->hw, (unsigned long) vals[0], (unsigned long) vals[1], &hwif->irq);
  1340. memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
  1341. hwif->irq = vals[2];
  1342. hwif->noprobe = 0;
  1343. hwif->chipset = ide_forced;
  1344. goto obsolete_option;
  1345. case 0: goto bad_option;
  1346. default:
  1347. printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
  1348. return 1;
  1349. }
  1350. }
  1351. bad_option:
  1352. printk(" -- BAD OPTION\n");
  1353. return 1;
  1354. obsolete_option:
  1355. printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
  1356. return 1;
  1357. bad_hwif:
  1358. printk("-- NOT SUPPORTED ON ide%d", hw);
  1359. done:
  1360. printk("\n");
  1361. return 1;
  1362. }
  1363. extern void __init pnpide_init(void);
  1364. extern void __exit pnpide_exit(void);
  1365. extern void __init h8300_ide_init(void);
  1366. /*
  1367. * probe_for_hwifs() finds/initializes "known" IDE interfaces
  1368. */
  1369. static void __init probe_for_hwifs (void)
  1370. {
  1371. #ifdef CONFIG_IDEPCI_PCIBUS_ORDER
  1372. ide_scan_pcibus(ide_scan_direction);
  1373. #endif
  1374. #ifdef CONFIG_ETRAX_IDE
  1375. {
  1376. extern void init_e100_ide(void);
  1377. init_e100_ide();
  1378. }
  1379. #endif /* CONFIG_ETRAX_IDE */
  1380. #ifdef CONFIG_BLK_DEV_CMD640
  1381. {
  1382. extern void ide_probe_for_cmd640x(void);
  1383. ide_probe_for_cmd640x();
  1384. }
  1385. #endif /* CONFIG_BLK_DEV_CMD640 */
  1386. #ifdef CONFIG_BLK_DEV_IDE_PMAC
  1387. {
  1388. extern int pmac_ide_probe(void);
  1389. (void)pmac_ide_probe();
  1390. }
  1391. #endif /* CONFIG_BLK_DEV_IDE_PMAC */
  1392. #ifdef CONFIG_BLK_DEV_GAYLE
  1393. {
  1394. extern void gayle_init(void);
  1395. gayle_init();
  1396. }
  1397. #endif /* CONFIG_BLK_DEV_GAYLE */
  1398. #ifdef CONFIG_BLK_DEV_FALCON_IDE
  1399. {
  1400. extern void falconide_init(void);
  1401. falconide_init();
  1402. }
  1403. #endif /* CONFIG_BLK_DEV_FALCON_IDE */
  1404. #ifdef CONFIG_BLK_DEV_MAC_IDE
  1405. {
  1406. extern void macide_init(void);
  1407. macide_init();
  1408. }
  1409. #endif /* CONFIG_BLK_DEV_MAC_IDE */
  1410. #ifdef CONFIG_BLK_DEV_Q40IDE
  1411. {
  1412. extern void q40ide_init(void);
  1413. q40ide_init();
  1414. }
  1415. #endif /* CONFIG_BLK_DEV_Q40IDE */
  1416. #ifdef CONFIG_BLK_DEV_BUDDHA
  1417. {
  1418. extern void buddha_init(void);
  1419. buddha_init();
  1420. }
  1421. #endif /* CONFIG_BLK_DEV_BUDDHA */
  1422. #ifdef CONFIG_BLK_DEV_IDEPNP
  1423. pnpide_init();
  1424. #endif
  1425. #ifdef CONFIG_H8300
  1426. h8300_ide_init();
  1427. #endif
  1428. }
  1429. /*
  1430. * Probe module
  1431. */
  1432. EXPORT_SYMBOL(ide_lock);
  1433. static int ide_bus_match(struct device *dev, struct device_driver *drv)
  1434. {
  1435. return 1;
  1436. }
  1437. static char *media_string(ide_drive_t *drive)
  1438. {
  1439. switch (drive->media) {
  1440. case ide_disk:
  1441. return "disk";
  1442. case ide_cdrom:
  1443. return "cdrom";
  1444. case ide_tape:
  1445. return "tape";
  1446. case ide_floppy:
  1447. return "floppy";
  1448. case ide_optical:
  1449. return "optical";
  1450. default:
  1451. return "UNKNOWN";
  1452. }
  1453. }
  1454. static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
  1455. {
  1456. ide_drive_t *drive = to_ide_device(dev);
  1457. return sprintf(buf, "%s\n", media_string(drive));
  1458. }
  1459. static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
  1460. {
  1461. ide_drive_t *drive = to_ide_device(dev);
  1462. return sprintf(buf, "%s\n", drive->name);
  1463. }
  1464. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  1465. {
  1466. ide_drive_t *drive = to_ide_device(dev);
  1467. return sprintf(buf, "ide:m-%s\n", media_string(drive));
  1468. }
  1469. static struct device_attribute ide_dev_attrs[] = {
  1470. __ATTR_RO(media),
  1471. __ATTR_RO(drivename),
  1472. __ATTR_RO(modalias),
  1473. __ATTR_NULL
  1474. };
  1475. static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
  1476. {
  1477. ide_drive_t *drive = to_ide_device(dev);
  1478. add_uevent_var(env, "MEDIA=%s", media_string(drive));
  1479. add_uevent_var(env, "DRIVENAME=%s", drive->name);
  1480. add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
  1481. return 0;
  1482. }
  1483. static int generic_ide_probe(struct device *dev)
  1484. {
  1485. ide_drive_t *drive = to_ide_device(dev);
  1486. ide_driver_t *drv = to_ide_driver(dev->driver);
  1487. return drv->probe ? drv->probe(drive) : -ENODEV;
  1488. }
  1489. static int generic_ide_remove(struct device *dev)
  1490. {
  1491. ide_drive_t *drive = to_ide_device(dev);
  1492. ide_driver_t *drv = to_ide_driver(dev->driver);
  1493. if (drv->remove)
  1494. drv->remove(drive);
  1495. return 0;
  1496. }
  1497. static void generic_ide_shutdown(struct device *dev)
  1498. {
  1499. ide_drive_t *drive = to_ide_device(dev);
  1500. ide_driver_t *drv = to_ide_driver(dev->driver);
  1501. if (dev->driver && drv->shutdown)
  1502. drv->shutdown(drive);
  1503. }
  1504. struct bus_type ide_bus_type = {
  1505. .name = "ide",
  1506. .match = ide_bus_match,
  1507. .uevent = ide_uevent,
  1508. .probe = generic_ide_probe,
  1509. .remove = generic_ide_remove,
  1510. .shutdown = generic_ide_shutdown,
  1511. .dev_attrs = ide_dev_attrs,
  1512. .suspend = generic_ide_suspend,
  1513. .resume = generic_ide_resume,
  1514. };
  1515. EXPORT_SYMBOL_GPL(ide_bus_type);
  1516. /*
  1517. * This is gets invoked once during initialization, to set *everything* up
  1518. */
  1519. static int __init ide_init(void)
  1520. {
  1521. int ret;
  1522. printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
  1523. system_bus_speed = ide_system_bus_speed();
  1524. ret = bus_register(&ide_bus_type);
  1525. if (ret < 0) {
  1526. printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
  1527. return ret;
  1528. }
  1529. init_ide_data();
  1530. proc_ide_create();
  1531. #ifdef CONFIG_BLK_DEV_ALI14XX
  1532. if (probe_ali14xx)
  1533. (void)ali14xx_init();
  1534. #endif
  1535. #ifdef CONFIG_BLK_DEV_UMC8672
  1536. if (probe_umc8672)
  1537. (void)umc8672_init();
  1538. #endif
  1539. #ifdef CONFIG_BLK_DEV_DTC2278
  1540. if (probe_dtc2278)
  1541. (void)dtc2278_init();
  1542. #endif
  1543. #ifdef CONFIG_BLK_DEV_HT6560B
  1544. if (probe_ht6560b)
  1545. (void)ht6560b_init();
  1546. #endif
  1547. #ifdef CONFIG_BLK_DEV_QD65XX
  1548. if (probe_qd65xx)
  1549. (void)qd65xx_init();
  1550. #endif
  1551. /* Probe for special PCI and other "known" interface chipsets. */
  1552. probe_for_hwifs();
  1553. return 0;
  1554. }
  1555. #ifdef MODULE
  1556. static char *options = NULL;
  1557. module_param(options, charp, 0);
  1558. MODULE_LICENSE("GPL");
  1559. static void __init parse_options (char *line)
  1560. {
  1561. char *next = line;
  1562. if (line == NULL || !*line)
  1563. return;
  1564. while ((line = next) != NULL) {
  1565. if ((next = strchr(line,' ')) != NULL)
  1566. *next++ = 0;
  1567. if (!ide_setup(line))
  1568. printk (KERN_INFO "Unknown option '%s'\n", line);
  1569. }
  1570. }
  1571. int __init init_module (void)
  1572. {
  1573. parse_options(options);
  1574. return ide_init();
  1575. }
  1576. void __exit cleanup_module (void)
  1577. {
  1578. int index;
  1579. for (index = 0; index < MAX_HWIFS; ++index)
  1580. ide_unregister(index);
  1581. #ifdef CONFIG_BLK_DEV_IDEPNP
  1582. pnpide_exit();
  1583. #endif
  1584. proc_ide_destroy();
  1585. bus_unregister(&ide_bus_type);
  1586. }
  1587. #else /* !MODULE */
  1588. __setup("", ide_setup);
  1589. module_init(ide_init);
  1590. #endif /* MODULE */