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