ide.c 44 KB

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