ide.c 44 KB

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