ide.c 42 KB

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