ide.c 44 KB

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