ide.c 40 KB

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