ide.c 42 KB

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