ide.c 42 KB

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