ide.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156
  1. /*
  2. * linux/drivers/ide/ide.c Version 7.00beta2 Mar 05 2003
  3. *
  4. * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
  5. */
  6. /*
  7. * Mostly written by Mark Lord <mlord@pobox.com>
  8. * and Gadi Oxman <gadio@netvision.net.il>
  9. * and Andre Hedrick <andre@linux-ide.org>
  10. *
  11. * See linux/MAINTAINERS for address of current maintainer.
  12. *
  13. * This is the multiple IDE interface driver, as evolved from hd.c.
  14. * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs
  15. * (usually 14 & 15).
  16. * There can be up to two drives per interface, as per the ATA-2 spec.
  17. *
  18. * Primary: ide0, port 0x1f0; major=3; hda is minor=0; hdb is minor=64
  19. * Secondary: ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
  20. * Tertiary: ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
  21. * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
  22. * ...
  23. *
  24. * From hd.c:
  25. * |
  26. * | It traverses the request-list, using interrupts to jump between functions.
  27. * | As nearly all functions can be called within interrupts, we may not sleep.
  28. * | Special care is recommended. Have Fun!
  29. * |
  30. * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
  31. * |
  32. * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
  33. * | in the early extended-partition checks and added DM partitions.
  34. * |
  35. * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
  36. * |
  37. * | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
  38. * | and general streamlining by Mark Lord (mlord@pobox.com).
  39. *
  40. * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
  41. *
  42. * Mark Lord (mlord@pobox.com) (IDE Perf.Pkg)
  43. * Delman Lee (delman@ieee.org) ("Mr. atdisk2")
  44. * Scott Snyder (snyder@fnald0.fnal.gov) (ATAPI IDE cd-rom)
  45. *
  46. * This was a rewrite of just about everything from hd.c, though some original
  47. * code is still sprinkled about. Think of it as a major evolution, with
  48. * inspiration from lots of linux users, esp. hamish@zot.apana.org.au
  49. *
  50. * Version 1.0 ALPHA initial code, primary i/f working okay
  51. * Version 1.3 BETA dual i/f on shared irq tested & working!
  52. * Version 1.4 BETA added auto probing for irq(s)
  53. * Version 1.5 BETA added ALPHA (untested) support for IDE cd-roms,
  54. * ...
  55. * Version 5.50 allow values as small as 20 for idebus=
  56. * Version 5.51 force non io_32bit in drive_cmd_intr()
  57. * change delay_10ms() to delay_50ms() to fix problems
  58. * Version 5.52 fix incorrect invalidation of removable devices
  59. * add "hdx=slow" command line option
  60. * Version 5.60 start to modularize the driver; the disk and ATAPI
  61. * drivers can be compiled as loadable modules.
  62. * move IDE probe code to ide-probe.c
  63. * move IDE disk code to ide-disk.c
  64. * add support for generic IDE device subdrivers
  65. * add m68k code from Geert Uytterhoeven
  66. * probe all interfaces by default
  67. * add ioctl to (re)probe an interface
  68. * Version 6.00 use per device request queues
  69. * attempt to optimize shared hwgroup performance
  70. * add ioctl to manually adjust bandwidth algorithms
  71. * add kerneld support for the probe module
  72. * fix bug in ide_error()
  73. * fix bug in the first ide_get_lock() call for Atari
  74. * don't flush leftover data for ATAPI devices
  75. * Version 6.01 clear hwgroup->active while the hwgroup sleeps
  76. * support HDIO_GETGEO for floppies
  77. * Version 6.02 fix ide_ack_intr() call
  78. * check partition table on floppies
  79. * Version 6.03 handle bad status bit sequencing in ide_wait_stat()
  80. * Version 6.10 deleted old entries from this list of updates
  81. * replaced triton.c with ide-dma.c generic PCI DMA
  82. * added support for BIOS-enabled UltraDMA
  83. * rename all "promise" things to "pdc4030"
  84. * fix EZ-DRIVE handling on small disks
  85. * Version 6.11 fix probe error in ide_scan_devices()
  86. * fix ancient "jiffies" polling bugs
  87. * mask all hwgroup interrupts on each irq entry
  88. * Version 6.12 integrate ioctl and proc interfaces
  89. * fix parsing of "idex=" command line parameter
  90. * Version 6.13 add support for ide4/ide5 courtesy rjones@orchestream.com
  91. * Version 6.14 fixed IRQ sharing among PCI devices
  92. * Version 6.15 added SMP awareness to IDE drivers
  93. * Version 6.16 fixed various bugs; even more SMP friendly
  94. * Version 6.17 fix for newest EZ-Drive problem
  95. * Version 6.18 default unpartitioned-disk translation now "BIOS LBA"
  96. * Version 6.19 Re-design for a UNIFORM driver for all platforms,
  97. * model based on suggestions from Russell King and
  98. * Geert Uytterhoeven
  99. * Promise DC4030VL now supported.
  100. * add support for ide6/ide7
  101. * delay_50ms() changed to ide_delay_50ms() and exported.
  102. * Version 6.20 Added/Fixed Generic ATA-66 support and hwif detection.
  103. * Added hdx=flash to allow for second flash disk
  104. * detection w/o the hang loop.
  105. * Added support for ide8/ide9
  106. * Added idex=ata66 for the quirky chipsets that are
  107. * ATA-66 compliant, but have yet to determine a method
  108. * of verification of the 80c cable presence.
  109. * Specifically Promise's PDC20262 chipset.
  110. * Version 6.21 Fixing/Fixed SMP spinlock issue with insight from an old
  111. * hat that clarified original low level driver design.
  112. * Version 6.30 Added SMP support; fixed multmode issues. -ml
  113. * Version 6.31 Debug Share INTR's and request queue streaming
  114. * Native ATA-100 support
  115. * Prep for Cascades Project
  116. * Version 7.00alpha First named revision of ide rearrange
  117. *
  118. * Some additional driver compile-time options are in ./include/linux/ide.h
  119. *
  120. * To do, in likely order of completion:
  121. * - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
  122. *
  123. */
  124. #define REVISION "Revision: 7.00alpha2"
  125. #define VERSION "Id: ide.c 7.00a2 20020906"
  126. #define _IDE_C /* Tell ide.h it's really us */
  127. #include <linux/module.h>
  128. #include <linux/types.h>
  129. #include <linux/string.h>
  130. #include <linux/kernel.h>
  131. #include <linux/timer.h>
  132. #include <linux/mm.h>
  133. #include <linux/interrupt.h>
  134. #include <linux/major.h>
  135. #include <linux/errno.h>
  136. #include <linux/genhd.h>
  137. #include <linux/blkpg.h>
  138. #include <linux/slab.h>
  139. #include <linux/init.h>
  140. #include <linux/pci.h>
  141. #include <linux/delay.h>
  142. #include <linux/ide.h>
  143. #include <linux/completion.h>
  144. #include <linux/reboot.h>
  145. #include <linux/cdrom.h>
  146. #include <linux/seq_file.h>
  147. #include <linux/device.h>
  148. #include <linux/bitops.h>
  149. #include <asm/byteorder.h>
  150. #include <asm/irq.h>
  151. #include <asm/uaccess.h>
  152. #include <asm/io.h>
  153. /* default maximum number of failures */
  154. #define IDE_DEFAULT_MAX_FAILURES 1
  155. static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
  156. IDE2_MAJOR, IDE3_MAJOR,
  157. IDE4_MAJOR, IDE5_MAJOR,
  158. IDE6_MAJOR, IDE7_MAJOR,
  159. IDE8_MAJOR, IDE9_MAJOR };
  160. static int idebus_parameter; /* holds the "idebus=" parameter */
  161. static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
  162. static int initializing; /* set while initializing built-in drivers */
  163. DECLARE_MUTEX(ide_cfg_sem);
  164. __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
  165. #ifdef CONFIG_BLK_DEV_IDEPCI
  166. static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
  167. #endif
  168. int noautodma = 0;
  169. EXPORT_SYMBOL(noautodma);
  170. #ifdef CONFIG_BLK_DEV_IDEACPI
  171. int ide_noacpi = 0;
  172. int ide_noacpitfs = 1;
  173. int ide_noacpionboot = 1;
  174. #endif
  175. /*
  176. * This is declared extern in ide.h, for access by other IDE modules:
  177. */
  178. ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
  179. EXPORT_SYMBOL(ide_hwifs);
  180. /*
  181. * Do not even *think* about calling this!
  182. */
  183. static void init_hwif_data(ide_hwif_t *hwif, unsigned int index)
  184. {
  185. unsigned int unit;
  186. /* bulk initialize hwif & drive info with zeros */
  187. memset(hwif, 0, sizeof(ide_hwif_t));
  188. /* fill in any non-zero initial values */
  189. hwif->index = index;
  190. hwif->major = ide_hwif_to_major[index];
  191. hwif->name[0] = 'i';
  192. hwif->name[1] = 'd';
  193. hwif->name[2] = 'e';
  194. hwif->name[3] = '0' + index;
  195. hwif->bus_state = BUSSTATE_ON;
  196. hwif->atapi_dma = 0; /* disable all atapi dma */
  197. init_completion(&hwif->gendev_rel_comp);
  198. default_hwif_iops(hwif);
  199. default_hwif_transport(hwif);
  200. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  201. ide_drive_t *drive = &hwif->drives[unit];
  202. drive->media = ide_disk;
  203. drive->select.all = (unit<<4)|0xa0;
  204. drive->hwif = hwif;
  205. drive->ctl = 0x08;
  206. drive->ready_stat = READY_STAT;
  207. drive->bad_wstat = BAD_W_STAT;
  208. drive->special.b.recalibrate = 1;
  209. drive->special.b.set_geometry = 1;
  210. drive->name[0] = 'h';
  211. drive->name[1] = 'd';
  212. drive->name[2] = 'a' + (index * MAX_DRIVES) + unit;
  213. drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
  214. drive->using_dma = 0;
  215. drive->vdma = 0;
  216. INIT_LIST_HEAD(&drive->list);
  217. init_completion(&drive->gendev_rel_comp);
  218. }
  219. }
  220. static void init_hwif_default(ide_hwif_t *hwif, unsigned int index)
  221. {
  222. hw_regs_t hw;
  223. memset(&hw, 0, sizeof(hw_regs_t));
  224. ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
  225. memcpy(&hwif->hw, &hw, sizeof(hw));
  226. memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
  227. hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
  228. #ifdef CONFIG_BLK_DEV_HD
  229. if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
  230. hwif->noprobe = 1; /* may be overridden by ide_setup() */
  231. #endif
  232. }
  233. extern void ide_arm_init(void);
  234. /*
  235. * init_ide_data() sets reasonable default values into all fields
  236. * of all instances of the hwifs and drives, but only on the first call.
  237. * Subsequent calls have no effect (they don't wipe out anything).
  238. *
  239. * This routine is normally called at driver initialization time,
  240. * but may also be called MUCH earlier during kernel "command-line"
  241. * parameter processing. As such, we cannot depend on any other parts
  242. * of the kernel (such as memory allocation) to be functioning yet.
  243. *
  244. * This is too bad, as otherwise we could dynamically allocate the
  245. * ide_drive_t structs as needed, rather than always consuming memory
  246. * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
  247. *
  248. * FIXME: We should stuff the setup data into __init and copy the
  249. * relevant hwifs/allocate them properly during boot.
  250. */
  251. #define MAGIC_COOKIE 0x12345678
  252. static void __init init_ide_data (void)
  253. {
  254. ide_hwif_t *hwif;
  255. unsigned int index;
  256. static unsigned long magic_cookie = MAGIC_COOKIE;
  257. if (magic_cookie != MAGIC_COOKIE)
  258. return; /* already initialized */
  259. magic_cookie = 0;
  260. /* Initialise all interface structures */
  261. for (index = 0; index < MAX_HWIFS; ++index) {
  262. hwif = &ide_hwifs[index];
  263. init_hwif_data(hwif, index);
  264. init_hwif_default(hwif, index);
  265. #if !defined(CONFIG_PPC32) || !defined(CONFIG_PCI)
  266. hwif->irq = hwif->hw.irq =
  267. ide_init_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
  268. #endif
  269. }
  270. #ifdef CONFIG_IDE_ARM
  271. initializing = 1;
  272. ide_arm_init();
  273. initializing = 0;
  274. #endif
  275. }
  276. /**
  277. * ide_system_bus_speed - guess bus speed
  278. *
  279. * ide_system_bus_speed() returns what we think is the system VESA/PCI
  280. * bus speed (in MHz). This is used for calculating interface PIO timings.
  281. * The default is 40 for known PCI systems, 50 otherwise.
  282. * The "idebus=xx" parameter can be used to override this value.
  283. * The actual value to be used is computed/displayed the first time
  284. * through. Drivers should only use this as a last resort.
  285. *
  286. * Returns a guessed speed in MHz.
  287. */
  288. static int ide_system_bus_speed(void)
  289. {
  290. #ifdef CONFIG_PCI
  291. static struct pci_device_id pci_default[] = {
  292. { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
  293. { }
  294. };
  295. #else
  296. #define pci_default 0
  297. #endif /* CONFIG_PCI */
  298. if (!system_bus_speed) {
  299. if (idebus_parameter) {
  300. /* user supplied value */
  301. system_bus_speed = idebus_parameter;
  302. } else if (pci_dev_present(pci_default)) {
  303. /* safe default value for PCI */
  304. system_bus_speed = 33;
  305. } else {
  306. /* safe default value for VESA and PCI */
  307. system_bus_speed = 50;
  308. }
  309. printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
  310. "for PIO modes%s\n", system_bus_speed,
  311. idebus_parameter ? "" : "; override with idebus=xx");
  312. }
  313. return system_bus_speed;
  314. }
  315. #ifdef CONFIG_PROC_FS
  316. struct proc_dir_entry *proc_ide_root;
  317. #endif
  318. static struct resource* hwif_request_region(ide_hwif_t *hwif,
  319. unsigned long addr, int num)
  320. {
  321. struct resource *res = request_region(addr, num, hwif->name);
  322. if (!res)
  323. printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
  324. hwif->name, addr, addr+num-1);
  325. return res;
  326. }
  327. /**
  328. * ide_hwif_request_regions - request resources for IDE
  329. * @hwif: interface to use
  330. *
  331. * Requests all the needed resources for an interface.
  332. * Right now core IDE code does this work which is deeply wrong.
  333. * MMIO leaves it to the controller driver,
  334. * PIO will migrate this way over time.
  335. */
  336. int ide_hwif_request_regions(ide_hwif_t *hwif)
  337. {
  338. unsigned long addr;
  339. unsigned int i;
  340. if (hwif->mmio)
  341. return 0;
  342. addr = hwif->io_ports[IDE_CONTROL_OFFSET];
  343. if (addr && !hwif_request_region(hwif, addr, 1))
  344. goto control_region_busy;
  345. hwif->straight8 = 0;
  346. addr = hwif->io_ports[IDE_DATA_OFFSET];
  347. if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
  348. if (!hwif_request_region(hwif, addr, 8))
  349. goto data_region_busy;
  350. hwif->straight8 = 1;
  351. return 0;
  352. }
  353. for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
  354. addr = hwif->io_ports[i];
  355. if (!hwif_request_region(hwif, addr, 1)) {
  356. while (--i)
  357. release_region(addr, 1);
  358. goto data_region_busy;
  359. }
  360. }
  361. return 0;
  362. data_region_busy:
  363. addr = hwif->io_ports[IDE_CONTROL_OFFSET];
  364. if (addr)
  365. release_region(addr, 1);
  366. control_region_busy:
  367. /* If any errors are return, we drop the hwif interface. */
  368. return -EBUSY;
  369. }
  370. /**
  371. * ide_hwif_release_regions - free IDE resources
  372. *
  373. * Note that we only release the standard ports,
  374. * and do not even try to handle any extra ports
  375. * allocated for weird IDE interface chipsets.
  376. *
  377. * Note also that we don't yet handle mmio resources here. More
  378. * importantly our caller should be doing this so we need to
  379. * restructure this as a helper function for drivers.
  380. */
  381. void ide_hwif_release_regions(ide_hwif_t *hwif)
  382. {
  383. u32 i = 0;
  384. if (hwif->mmio)
  385. return;
  386. if (hwif->io_ports[IDE_CONTROL_OFFSET])
  387. release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
  388. if (hwif->straight8) {
  389. release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
  390. return;
  391. }
  392. for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
  393. if (hwif->io_ports[i])
  394. release_region(hwif->io_ports[i], 1);
  395. }
  396. /**
  397. * ide_hwif_restore - restore hwif to template
  398. * @hwif: hwif to update
  399. * @tmp_hwif: template
  400. *
  401. * Restore hwif to a previous state by copying most settings
  402. * from the template.
  403. */
  404. static void ide_hwif_restore(ide_hwif_t *hwif, ide_hwif_t *tmp_hwif)
  405. {
  406. hwif->hwgroup = tmp_hwif->hwgroup;
  407. hwif->gendev.parent = tmp_hwif->gendev.parent;
  408. hwif->proc = tmp_hwif->proc;
  409. hwif->major = tmp_hwif->major;
  410. hwif->straight8 = tmp_hwif->straight8;
  411. hwif->bus_state = tmp_hwif->bus_state;
  412. hwif->atapi_dma = tmp_hwif->atapi_dma;
  413. hwif->ultra_mask = tmp_hwif->ultra_mask;
  414. hwif->mwdma_mask = tmp_hwif->mwdma_mask;
  415. hwif->swdma_mask = tmp_hwif->swdma_mask;
  416. hwif->chipset = tmp_hwif->chipset;
  417. hwif->hold = tmp_hwif->hold;
  418. #ifdef CONFIG_BLK_DEV_IDEPCI
  419. hwif->pci_dev = tmp_hwif->pci_dev;
  420. hwif->cds = tmp_hwif->cds;
  421. #endif
  422. hwif->tuneproc = tmp_hwif->tuneproc;
  423. hwif->speedproc = tmp_hwif->speedproc;
  424. hwif->selectproc = tmp_hwif->selectproc;
  425. hwif->reset_poll = tmp_hwif->reset_poll;
  426. hwif->pre_reset = tmp_hwif->pre_reset;
  427. hwif->resetproc = tmp_hwif->resetproc;
  428. hwif->intrproc = tmp_hwif->intrproc;
  429. hwif->maskproc = tmp_hwif->maskproc;
  430. hwif->quirkproc = tmp_hwif->quirkproc;
  431. hwif->busproc = tmp_hwif->busproc;
  432. hwif->ata_input_data = tmp_hwif->ata_input_data;
  433. hwif->ata_output_data = tmp_hwif->ata_output_data;
  434. hwif->atapi_input_bytes = tmp_hwif->atapi_input_bytes;
  435. hwif->atapi_output_bytes = tmp_hwif->atapi_output_bytes;
  436. hwif->dma_setup = tmp_hwif->dma_setup;
  437. hwif->dma_exec_cmd = tmp_hwif->dma_exec_cmd;
  438. hwif->dma_start = tmp_hwif->dma_start;
  439. hwif->ide_dma_end = tmp_hwif->ide_dma_end;
  440. hwif->ide_dma_check = tmp_hwif->ide_dma_check;
  441. hwif->ide_dma_on = tmp_hwif->ide_dma_on;
  442. hwif->dma_off_quietly = tmp_hwif->dma_off_quietly;
  443. hwif->ide_dma_test_irq = tmp_hwif->ide_dma_test_irq;
  444. hwif->ide_dma_clear_irq = tmp_hwif->ide_dma_clear_irq;
  445. hwif->dma_host_on = tmp_hwif->dma_host_on;
  446. hwif->dma_host_off = tmp_hwif->dma_host_off;
  447. hwif->ide_dma_lostirq = tmp_hwif->ide_dma_lostirq;
  448. hwif->ide_dma_timeout = tmp_hwif->ide_dma_timeout;
  449. hwif->OUTB = tmp_hwif->OUTB;
  450. hwif->OUTBSYNC = tmp_hwif->OUTBSYNC;
  451. hwif->OUTW = tmp_hwif->OUTW;
  452. hwif->OUTSW = tmp_hwif->OUTSW;
  453. hwif->OUTSL = tmp_hwif->OUTSL;
  454. hwif->INB = tmp_hwif->INB;
  455. hwif->INW = tmp_hwif->INW;
  456. hwif->INSW = tmp_hwif->INSW;
  457. hwif->INSL = tmp_hwif->INSL;
  458. hwif->sg_max_nents = tmp_hwif->sg_max_nents;
  459. hwif->mmio = tmp_hwif->mmio;
  460. hwif->rqsize = tmp_hwif->rqsize;
  461. hwif->no_lba48 = tmp_hwif->no_lba48;
  462. #ifndef CONFIG_BLK_DEV_IDECS
  463. hwif->irq = tmp_hwif->irq;
  464. #endif
  465. hwif->dma_base = tmp_hwif->dma_base;
  466. hwif->dma_master = tmp_hwif->dma_master;
  467. hwif->dma_command = tmp_hwif->dma_command;
  468. hwif->dma_vendor1 = tmp_hwif->dma_vendor1;
  469. hwif->dma_status = tmp_hwif->dma_status;
  470. hwif->dma_vendor3 = tmp_hwif->dma_vendor3;
  471. hwif->dma_prdtable = tmp_hwif->dma_prdtable;
  472. hwif->config_data = tmp_hwif->config_data;
  473. hwif->select_data = tmp_hwif->select_data;
  474. hwif->extra_base = tmp_hwif->extra_base;
  475. hwif->extra_ports = tmp_hwif->extra_ports;
  476. hwif->autodma = tmp_hwif->autodma;
  477. hwif->udma_four = tmp_hwif->udma_four;
  478. hwif->hwif_data = tmp_hwif->hwif_data;
  479. }
  480. /**
  481. * ide_unregister - free an IDE interface
  482. * @index: index of interface (will change soon to a pointer)
  483. *
  484. * Perform the final unregister of an IDE interface. At the moment
  485. * we don't refcount interfaces so this will also get split up.
  486. *
  487. * Locking:
  488. * The caller must not hold the IDE locks
  489. * The drive present/vanishing is not yet properly locked
  490. * Take care with the callbacks. These have been split to avoid
  491. * deadlocking the IDE layer. The shutdown callback is called
  492. * before we take the lock and free resources. It is up to the
  493. * caller to be sure there is no pending I/O here, and that
  494. * the interface will not be reopened (present/vanishing locking
  495. * isn't yet done BTW). After we commit to the final kill we
  496. * call the cleanup callback with the ide locks held.
  497. *
  498. * Unregister restores the hwif structures to the default state.
  499. * This is raving bonkers.
  500. */
  501. void ide_unregister(unsigned int index)
  502. {
  503. ide_drive_t *drive;
  504. ide_hwif_t *hwif, *g;
  505. static ide_hwif_t tmp_hwif; /* protected by ide_cfg_sem */
  506. ide_hwgroup_t *hwgroup;
  507. int irq_count = 0, unit;
  508. BUG_ON(index >= MAX_HWIFS);
  509. BUG_ON(in_interrupt());
  510. BUG_ON(irqs_disabled());
  511. down(&ide_cfg_sem);
  512. spin_lock_irq(&ide_lock);
  513. hwif = &ide_hwifs[index];
  514. if (!hwif->present)
  515. goto abort;
  516. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  517. drive = &hwif->drives[unit];
  518. if (!drive->present)
  519. continue;
  520. spin_unlock_irq(&ide_lock);
  521. device_unregister(&drive->gendev);
  522. wait_for_completion(&drive->gendev_rel_comp);
  523. spin_lock_irq(&ide_lock);
  524. }
  525. hwif->present = 0;
  526. spin_unlock_irq(&ide_lock);
  527. destroy_proc_ide_interface(hwif);
  528. hwgroup = hwif->hwgroup;
  529. /*
  530. * free the irq if we were the only hwif using it
  531. */
  532. g = hwgroup->hwif;
  533. do {
  534. if (g->irq == hwif->irq)
  535. ++irq_count;
  536. g = g->next;
  537. } while (g != hwgroup->hwif);
  538. if (irq_count == 1)
  539. free_irq(hwif->irq, hwgroup);
  540. spin_lock_irq(&ide_lock);
  541. /*
  542. * Note that we only release the standard ports,
  543. * and do not even try to handle any extra ports
  544. * allocated for weird IDE interface chipsets.
  545. */
  546. ide_hwif_release_regions(hwif);
  547. /*
  548. * Remove us from the hwgroup, and free
  549. * the hwgroup if we were the only member
  550. */
  551. if (hwif->next == hwif) {
  552. BUG_ON(hwgroup->hwif != hwif);
  553. kfree(hwgroup);
  554. } else {
  555. /* There is another interface in hwgroup.
  556. * Unlink us, and set hwgroup->drive and ->hwif to
  557. * something sane.
  558. */
  559. g = hwgroup->hwif;
  560. while (g->next != hwif)
  561. g = g->next;
  562. g->next = hwif->next;
  563. if (hwgroup->hwif == hwif) {
  564. /* Chose a random hwif for hwgroup->hwif.
  565. * It's guaranteed that there are no drives
  566. * left in the hwgroup.
  567. */
  568. BUG_ON(hwgroup->drive != NULL);
  569. hwgroup->hwif = g;
  570. }
  571. BUG_ON(hwgroup->hwif == hwif);
  572. }
  573. /* More messed up locking ... */
  574. spin_unlock_irq(&ide_lock);
  575. device_unregister(&hwif->gendev);
  576. wait_for_completion(&hwif->gendev_rel_comp);
  577. /*
  578. * Remove us from the kernel's knowledge
  579. */
  580. blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
  581. kfree(hwif->sg_table);
  582. unregister_blkdev(hwif->major, hwif->name);
  583. spin_lock_irq(&ide_lock);
  584. if (hwif->dma_base) {
  585. (void) ide_release_dma(hwif);
  586. hwif->dma_base = 0;
  587. hwif->dma_master = 0;
  588. hwif->dma_command = 0;
  589. hwif->dma_vendor1 = 0;
  590. hwif->dma_status = 0;
  591. hwif->dma_vendor3 = 0;
  592. hwif->dma_prdtable = 0;
  593. hwif->extra_base = 0;
  594. hwif->extra_ports = 0;
  595. }
  596. /* copy original settings */
  597. tmp_hwif = *hwif;
  598. /* restore hwif data to pristine status */
  599. init_hwif_data(hwif, index);
  600. init_hwif_default(hwif, index);
  601. ide_hwif_restore(hwif, &tmp_hwif);
  602. abort:
  603. spin_unlock_irq(&ide_lock);
  604. up(&ide_cfg_sem);
  605. }
  606. EXPORT_SYMBOL(ide_unregister);
  607. /**
  608. * ide_setup_ports - set up IDE interface ports
  609. * @hw: register descriptions
  610. * @base: base register
  611. * @offsets: table of register offsets
  612. * @ctrl: control register
  613. * @ack_irq: IRQ ack
  614. * @irq: interrupt lie
  615. *
  616. * Setup hw_regs_t structure described by parameters. You
  617. * may set up the hw structure yourself OR use this routine to
  618. * do it for you. This is basically a helper
  619. *
  620. */
  621. void ide_setup_ports ( hw_regs_t *hw,
  622. unsigned long base, int *offsets,
  623. unsigned long ctrl, unsigned long intr,
  624. ide_ack_intr_t *ack_intr,
  625. /*
  626. * ide_io_ops_t *iops,
  627. */
  628. int irq)
  629. {
  630. int i;
  631. memset(hw, 0, sizeof(hw_regs_t));
  632. for (i = 0; i < IDE_NR_PORTS; i++) {
  633. if (offsets[i] == -1) {
  634. switch(i) {
  635. case IDE_CONTROL_OFFSET:
  636. hw->io_ports[i] = ctrl;
  637. break;
  638. #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
  639. case IDE_IRQ_OFFSET:
  640. hw->io_ports[i] = intr;
  641. break;
  642. #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
  643. default:
  644. hw->io_ports[i] = 0;
  645. break;
  646. }
  647. } else {
  648. hw->io_ports[i] = base + offsets[i];
  649. }
  650. }
  651. hw->irq = irq;
  652. hw->dma = NO_DMA;
  653. hw->ack_intr = ack_intr;
  654. /*
  655. * hw->iops = iops;
  656. */
  657. }
  658. /**
  659. * ide_register_hw_with_fixup - register IDE interface
  660. * @hw: hardware registers
  661. * @hwifp: pointer to returned hwif
  662. * @fixup: fixup function
  663. *
  664. * Register an IDE interface, specifying exactly the registers etc.
  665. * Set init=1 iff calling before probes have taken place.
  666. *
  667. * Returns -1 on error.
  668. */
  669. int ide_register_hw_with_fixup(hw_regs_t *hw, ide_hwif_t **hwifp, void(*fixup)(ide_hwif_t *hwif))
  670. {
  671. int index, retry = 1;
  672. ide_hwif_t *hwif;
  673. do {
  674. for (index = 0; index < MAX_HWIFS; ++index) {
  675. hwif = &ide_hwifs[index];
  676. if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
  677. goto found;
  678. }
  679. for (index = 0; index < MAX_HWIFS; ++index) {
  680. hwif = &ide_hwifs[index];
  681. if (hwif->hold)
  682. continue;
  683. if ((!hwif->present && !hwif->mate && !initializing) ||
  684. (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
  685. goto found;
  686. }
  687. for (index = 0; index < MAX_HWIFS; index++)
  688. ide_unregister(index);
  689. } while (retry--);
  690. return -1;
  691. found:
  692. if (hwif->present)
  693. ide_unregister(index);
  694. else if (!hwif->hold) {
  695. init_hwif_data(hwif, index);
  696. init_hwif_default(hwif, index);
  697. }
  698. if (hwif->present)
  699. return -1;
  700. memcpy(&hwif->hw, hw, sizeof(*hw));
  701. memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
  702. hwif->irq = hw->irq;
  703. hwif->noprobe = 0;
  704. hwif->chipset = hw->chipset;
  705. hwif->gendev.parent = hw->dev;
  706. if (!initializing) {
  707. probe_hwif_init_with_fixup(hwif, fixup);
  708. create_proc_ide_interfaces();
  709. }
  710. if (hwifp)
  711. *hwifp = hwif;
  712. return (initializing || hwif->present) ? index : -1;
  713. }
  714. EXPORT_SYMBOL(ide_register_hw_with_fixup);
  715. int ide_register_hw(hw_regs_t *hw, ide_hwif_t **hwifp)
  716. {
  717. return ide_register_hw_with_fixup(hw, hwifp, NULL);
  718. }
  719. EXPORT_SYMBOL(ide_register_hw);
  720. /*
  721. * Locks for IDE setting functionality
  722. */
  723. DECLARE_MUTEX(ide_setting_sem);
  724. /**
  725. * __ide_add_setting - add an ide setting option
  726. * @drive: drive to use
  727. * @name: setting name
  728. * @rw: true if the function is read write
  729. * @read_ioctl: function to call on read
  730. * @write_ioctl: function to call on write
  731. * @data_type: type of data
  732. * @min: range minimum
  733. * @max: range maximum
  734. * @mul_factor: multiplication scale
  735. * @div_factor: divison scale
  736. * @data: private data field
  737. * @set: setting
  738. * @auto_remove: setting auto removal flag
  739. *
  740. * Removes the setting named from the device if it is present.
  741. * The function takes the settings_lock to protect against
  742. * parallel changes. This function must not be called from IRQ
  743. * context. Returns 0 on success or -1 on failure.
  744. *
  745. * BUGS: This code is seriously over-engineered. There is also
  746. * magic about how the driver specific features are setup. If
  747. * a driver is attached we assume the driver settings are auto
  748. * remove.
  749. */
  750. static int __ide_add_setting(ide_drive_t *drive, const char *name, int rw, int read_ioctl, int write_ioctl, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set, int auto_remove)
  751. {
  752. ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
  753. down(&ide_setting_sem);
  754. while ((*p) && strcmp((*p)->name, name) < 0)
  755. p = &((*p)->next);
  756. if ((setting = kzalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
  757. goto abort;
  758. if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
  759. goto abort;
  760. strcpy(setting->name, name);
  761. setting->rw = rw;
  762. setting->read_ioctl = read_ioctl;
  763. setting->write_ioctl = write_ioctl;
  764. setting->data_type = data_type;
  765. setting->min = min;
  766. setting->max = max;
  767. setting->mul_factor = mul_factor;
  768. setting->div_factor = div_factor;
  769. setting->data = data;
  770. setting->set = set;
  771. setting->next = *p;
  772. if (auto_remove)
  773. setting->auto_remove = 1;
  774. *p = setting;
  775. up(&ide_setting_sem);
  776. return 0;
  777. abort:
  778. up(&ide_setting_sem);
  779. kfree(setting);
  780. return -1;
  781. }
  782. int ide_add_setting(ide_drive_t *drive, const char *name, int rw, int read_ioctl, int write_ioctl, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set)
  783. {
  784. return __ide_add_setting(drive, name, rw, read_ioctl, write_ioctl, data_type, min, max, mul_factor, div_factor, data, set, 1);
  785. }
  786. EXPORT_SYMBOL(ide_add_setting);
  787. /**
  788. * __ide_remove_setting - remove an ide setting option
  789. * @drive: drive to use
  790. * @name: setting name
  791. *
  792. * Removes the setting named from the device if it is present.
  793. * The caller must hold the setting semaphore.
  794. */
  795. static void __ide_remove_setting (ide_drive_t *drive, char *name)
  796. {
  797. ide_settings_t **p, *setting;
  798. p = (ide_settings_t **) &drive->settings;
  799. while ((*p) && strcmp((*p)->name, name))
  800. p = &((*p)->next);
  801. if ((setting = (*p)) == NULL)
  802. return;
  803. (*p) = setting->next;
  804. kfree(setting->name);
  805. kfree(setting);
  806. }
  807. /**
  808. * ide_find_setting_by_ioctl - find a drive specific ioctl
  809. * @drive: drive to scan
  810. * @cmd: ioctl command to handle
  811. *
  812. * Scan's the device setting table for a matching entry and returns
  813. * this or NULL if no entry is found. The caller must hold the
  814. * setting semaphore
  815. */
  816. static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
  817. {
  818. ide_settings_t *setting = drive->settings;
  819. while (setting) {
  820. if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
  821. break;
  822. setting = setting->next;
  823. }
  824. return setting;
  825. }
  826. /**
  827. * ide_find_setting_by_name - find a drive specific setting
  828. * @drive: drive to scan
  829. * @name: setting name
  830. *
  831. * Scan's the device setting table for a matching entry and returns
  832. * this or NULL if no entry is found. The caller must hold the
  833. * setting semaphore
  834. */
  835. ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
  836. {
  837. ide_settings_t *setting = drive->settings;
  838. while (setting) {
  839. if (strcmp(setting->name, name) == 0)
  840. break;
  841. setting = setting->next;
  842. }
  843. return setting;
  844. }
  845. /**
  846. * auto_remove_settings - remove driver specific settings
  847. * @drive: drive
  848. *
  849. * Automatically remove all the driver specific settings for this
  850. * drive. This function may not be called from IRQ context. The
  851. * caller must hold ide_setting_sem.
  852. */
  853. static void auto_remove_settings (ide_drive_t *drive)
  854. {
  855. ide_settings_t *setting;
  856. repeat:
  857. setting = drive->settings;
  858. while (setting) {
  859. if (setting->auto_remove) {
  860. __ide_remove_setting(drive, setting->name);
  861. goto repeat;
  862. }
  863. setting = setting->next;
  864. }
  865. }
  866. /**
  867. * ide_read_setting - read an IDE setting
  868. * @drive: drive to read from
  869. * @setting: drive setting
  870. *
  871. * Read a drive setting and return the value. The caller
  872. * must hold the ide_setting_sem when making this call.
  873. *
  874. * BUGS: the data return and error are the same return value
  875. * so an error -EINVAL and true return of the same value cannot
  876. * be told apart
  877. */
  878. int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
  879. {
  880. int val = -EINVAL;
  881. unsigned long flags;
  882. if ((setting->rw & SETTING_READ)) {
  883. spin_lock_irqsave(&ide_lock, flags);
  884. switch(setting->data_type) {
  885. case TYPE_BYTE:
  886. val = *((u8 *) setting->data);
  887. break;
  888. case TYPE_SHORT:
  889. val = *((u16 *) setting->data);
  890. break;
  891. case TYPE_INT:
  892. case TYPE_INTA:
  893. val = *((u32 *) setting->data);
  894. break;
  895. }
  896. spin_unlock_irqrestore(&ide_lock, flags);
  897. }
  898. return val;
  899. }
  900. /**
  901. * ide_spin_wait_hwgroup - wait for group
  902. * @drive: drive in the group
  903. *
  904. * Wait for an IDE device group to go non busy and then return
  905. * holding the ide_lock which guards the hwgroup->busy status
  906. * and right to use it.
  907. */
  908. int ide_spin_wait_hwgroup (ide_drive_t *drive)
  909. {
  910. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  911. unsigned long timeout = jiffies + (3 * HZ);
  912. spin_lock_irq(&ide_lock);
  913. while (hwgroup->busy) {
  914. unsigned long lflags;
  915. spin_unlock_irq(&ide_lock);
  916. local_irq_set(lflags);
  917. if (time_after(jiffies, timeout)) {
  918. local_irq_restore(lflags);
  919. printk(KERN_ERR "%s: channel busy\n", drive->name);
  920. return -EBUSY;
  921. }
  922. local_irq_restore(lflags);
  923. spin_lock_irq(&ide_lock);
  924. }
  925. return 0;
  926. }
  927. EXPORT_SYMBOL(ide_spin_wait_hwgroup);
  928. /**
  929. * ide_write_setting - read an IDE setting
  930. * @drive: drive to read from
  931. * @setting: drive setting
  932. * @val: value
  933. *
  934. * Write a drive setting if it is possible. The caller
  935. * must hold the ide_setting_sem when making this call.
  936. *
  937. * BUGS: the data return and error are the same return value
  938. * so an error -EINVAL and true return of the same value cannot
  939. * be told apart
  940. *
  941. * FIXME: This should be changed to enqueue a special request
  942. * to the driver to change settings, and then wait on a sema for completion.
  943. * The current scheme of polling is kludgy, though safe enough.
  944. */
  945. int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
  946. {
  947. int i;
  948. u32 *p;
  949. if (!capable(CAP_SYS_ADMIN))
  950. return -EACCES;
  951. if (!(setting->rw & SETTING_WRITE))
  952. return -EPERM;
  953. if (val < setting->min || val > setting->max)
  954. return -EINVAL;
  955. if (setting->set)
  956. return setting->set(drive, val);
  957. if (ide_spin_wait_hwgroup(drive))
  958. return -EBUSY;
  959. switch (setting->data_type) {
  960. case TYPE_BYTE:
  961. *((u8 *) setting->data) = val;
  962. break;
  963. case TYPE_SHORT:
  964. *((u16 *) setting->data) = val;
  965. break;
  966. case TYPE_INT:
  967. *((u32 *) setting->data) = val;
  968. break;
  969. case TYPE_INTA:
  970. p = (u32 *) setting->data;
  971. for (i = 0; i < 1 << PARTN_BITS; i++, p++)
  972. *p = val;
  973. break;
  974. }
  975. spin_unlock_irq(&ide_lock);
  976. return 0;
  977. }
  978. static int set_io_32bit(ide_drive_t *drive, int arg)
  979. {
  980. drive->io_32bit = arg;
  981. #ifdef CONFIG_BLK_DEV_DTC2278
  982. if (HWIF(drive)->chipset == ide_dtc2278)
  983. HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
  984. #endif /* CONFIG_BLK_DEV_DTC2278 */
  985. return 0;
  986. }
  987. static int set_using_dma (ide_drive_t *drive, int arg)
  988. {
  989. #ifdef CONFIG_BLK_DEV_IDEDMA
  990. ide_hwif_t *hwif = drive->hwif;
  991. int err = -EPERM;
  992. if (!drive->id || !(drive->id->capability & 1))
  993. goto out;
  994. if (hwif->ide_dma_check == NULL)
  995. goto out;
  996. err = -EBUSY;
  997. if (ide_spin_wait_hwgroup(drive))
  998. goto out;
  999. /*
  1000. * set ->busy flag, unlock and let it ride
  1001. */
  1002. hwif->hwgroup->busy = 1;
  1003. spin_unlock_irq(&ide_lock);
  1004. err = 0;
  1005. if (arg) {
  1006. if (ide_set_dma(drive) || hwif->ide_dma_on(drive))
  1007. err = -EIO;
  1008. } else
  1009. ide_dma_off(drive);
  1010. /*
  1011. * lock, clear ->busy flag and unlock before leaving
  1012. */
  1013. spin_lock_irq(&ide_lock);
  1014. hwif->hwgroup->busy = 0;
  1015. spin_unlock_irq(&ide_lock);
  1016. out:
  1017. return err;
  1018. #else
  1019. return -EPERM;
  1020. #endif
  1021. }
  1022. static int set_pio_mode (ide_drive_t *drive, int arg)
  1023. {
  1024. struct request rq;
  1025. if (!HWIF(drive)->tuneproc)
  1026. return -ENOSYS;
  1027. if (drive->special.b.set_tune)
  1028. return -EBUSY;
  1029. ide_init_drive_cmd(&rq);
  1030. drive->tune_req = (u8) arg;
  1031. drive->special.b.set_tune = 1;
  1032. (void) ide_do_drive_cmd(drive, &rq, ide_wait);
  1033. return 0;
  1034. }
  1035. static int set_xfer_rate (ide_drive_t *drive, int arg)
  1036. {
  1037. int err = ide_wait_cmd(drive,
  1038. WIN_SETFEATURES, (u8) arg,
  1039. SETFEATURES_XFER, 0, NULL);
  1040. if (!err && arg) {
  1041. ide_set_xfer_rate(drive, (u8) arg);
  1042. ide_driveid_update(drive);
  1043. }
  1044. return err;
  1045. }
  1046. /**
  1047. * ide_add_generic_settings - generic ide settings
  1048. * @drive: drive being configured
  1049. *
  1050. * Add the generic parts of the system settings to the /proc files and
  1051. * ioctls for this IDE device. The caller must not be holding the
  1052. * ide_setting_sem.
  1053. */
  1054. void ide_add_generic_settings (ide_drive_t *drive)
  1055. {
  1056. /*
  1057. * drive setting name read/write access read ioctl write ioctl data type min max mul_factor div_factor data pointer set function
  1058. */
  1059. __ide_add_setting(drive, "io_32bit", drive->no_io_32bit ? SETTING_READ : SETTING_RW, HDIO_GET_32BIT, HDIO_SET_32BIT, TYPE_BYTE, 0, 1 + (SUPPORT_VLB_SYNC << 1), 1, 1, &drive->io_32bit, set_io_32bit, 0);
  1060. __ide_add_setting(drive, "keepsettings", SETTING_RW, HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, TYPE_BYTE, 0, 1, 1, 1, &drive->keep_settings, NULL, 0);
  1061. __ide_add_setting(drive, "nice1", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->nice1, NULL, 0);
  1062. __ide_add_setting(drive, "pio_mode", SETTING_WRITE, -1, HDIO_SET_PIO_MODE, TYPE_BYTE, 0, 255, 1, 1, NULL, set_pio_mode, 0);
  1063. __ide_add_setting(drive, "unmaskirq", drive->no_unmask ? SETTING_READ : SETTING_RW, HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, TYPE_BYTE, 0, 1, 1, 1, &drive->unmask, NULL, 0);
  1064. __ide_add_setting(drive, "using_dma", SETTING_RW, HDIO_GET_DMA, HDIO_SET_DMA, TYPE_BYTE, 0, 1, 1, 1, &drive->using_dma, set_using_dma, 0);
  1065. __ide_add_setting(drive, "init_speed", SETTING_RW, -1, -1, TYPE_BYTE, 0, 70, 1, 1, &drive->init_speed, NULL, 0);
  1066. __ide_add_setting(drive, "current_speed", SETTING_RW, -1, -1, TYPE_BYTE, 0, 70, 1, 1, &drive->current_speed, set_xfer_rate, 0);
  1067. __ide_add_setting(drive, "number", SETTING_RW, -1, -1, TYPE_BYTE, 0, 3, 1, 1, &drive->dn, NULL, 0);
  1068. }
  1069. /**
  1070. * system_bus_clock - clock guess
  1071. *
  1072. * External version of the bus clock guess used by very old IDE drivers
  1073. * for things like VLB timings. Should not be used.
  1074. */
  1075. int system_bus_clock (void)
  1076. {
  1077. return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
  1078. }
  1079. EXPORT_SYMBOL(system_bus_clock);
  1080. static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
  1081. {
  1082. ide_drive_t *drive = dev->driver_data;
  1083. ide_hwif_t *hwif = HWIF(drive);
  1084. struct request rq;
  1085. struct request_pm_state rqpm;
  1086. ide_task_t args;
  1087. /* Call ACPI _GTM only once */
  1088. if (!(drive->dn % 2))
  1089. ide_acpi_get_timing(hwif);
  1090. memset(&rq, 0, sizeof(rq));
  1091. memset(&rqpm, 0, sizeof(rqpm));
  1092. memset(&args, 0, sizeof(args));
  1093. rq.cmd_type = REQ_TYPE_PM_SUSPEND;
  1094. rq.special = &args;
  1095. rq.data = &rqpm;
  1096. rqpm.pm_step = ide_pm_state_start_suspend;
  1097. if (mesg.event == PM_EVENT_PRETHAW)
  1098. mesg.event = PM_EVENT_FREEZE;
  1099. rqpm.pm_state = mesg.event;
  1100. return ide_do_drive_cmd(drive, &rq, ide_wait);
  1101. }
  1102. static int generic_ide_resume(struct device *dev)
  1103. {
  1104. ide_drive_t *drive = dev->driver_data;
  1105. ide_hwif_t *hwif = HWIF(drive);
  1106. struct request rq;
  1107. struct request_pm_state rqpm;
  1108. ide_task_t args;
  1109. /* Call ACPI _STM only once */
  1110. if (!(drive->dn % 2))
  1111. ide_acpi_push_timing(hwif);
  1112. ide_acpi_exec_tfs(drive);
  1113. memset(&rq, 0, sizeof(rq));
  1114. memset(&rqpm, 0, sizeof(rqpm));
  1115. memset(&args, 0, sizeof(args));
  1116. rq.cmd_type = REQ_TYPE_PM_RESUME;
  1117. rq.special = &args;
  1118. rq.data = &rqpm;
  1119. rqpm.pm_step = ide_pm_state_start_resume;
  1120. rqpm.pm_state = PM_EVENT_ON;
  1121. return ide_do_drive_cmd(drive, &rq, ide_head_wait);
  1122. }
  1123. int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
  1124. unsigned int cmd, unsigned long arg)
  1125. {
  1126. ide_settings_t *setting;
  1127. ide_driver_t *drv;
  1128. int err = 0;
  1129. void __user *p = (void __user *)arg;
  1130. down(&ide_setting_sem);
  1131. if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
  1132. if (cmd == setting->read_ioctl) {
  1133. err = ide_read_setting(drive, setting);
  1134. up(&ide_setting_sem);
  1135. return err >= 0 ? put_user(err, (long __user *)arg) : err;
  1136. } else {
  1137. if (bdev != bdev->bd_contains)
  1138. err = -EINVAL;
  1139. else
  1140. err = ide_write_setting(drive, setting, arg);
  1141. up(&ide_setting_sem);
  1142. return err;
  1143. }
  1144. }
  1145. up(&ide_setting_sem);
  1146. switch (cmd) {
  1147. case HDIO_OBSOLETE_IDENTITY:
  1148. case HDIO_GET_IDENTITY:
  1149. if (bdev != bdev->bd_contains)
  1150. return -EINVAL;
  1151. if (drive->id_read == 0)
  1152. return -ENOMSG;
  1153. if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
  1154. return -EFAULT;
  1155. return 0;
  1156. case HDIO_GET_NICE:
  1157. return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
  1158. drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
  1159. drive->nice0 << IDE_NICE_0 |
  1160. drive->nice1 << IDE_NICE_1 |
  1161. drive->nice2 << IDE_NICE_2,
  1162. (long __user *) arg);
  1163. #ifdef CONFIG_IDE_TASK_IOCTL
  1164. case HDIO_DRIVE_TASKFILE:
  1165. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  1166. return -EACCES;
  1167. switch(drive->media) {
  1168. case ide_disk:
  1169. return ide_taskfile_ioctl(drive, cmd, arg);
  1170. default:
  1171. return -ENOMSG;
  1172. }
  1173. #endif /* CONFIG_IDE_TASK_IOCTL */
  1174. case HDIO_DRIVE_CMD:
  1175. if (!capable(CAP_SYS_RAWIO))
  1176. return -EACCES;
  1177. return ide_cmd_ioctl(drive, cmd, arg);
  1178. case HDIO_DRIVE_TASK:
  1179. if (!capable(CAP_SYS_RAWIO))
  1180. return -EACCES;
  1181. return ide_task_ioctl(drive, cmd, arg);
  1182. case HDIO_SCAN_HWIF:
  1183. {
  1184. hw_regs_t hw;
  1185. int args[3];
  1186. if (!capable(CAP_SYS_RAWIO)) return -EACCES;
  1187. if (copy_from_user(args, p, 3 * sizeof(int)))
  1188. return -EFAULT;
  1189. memset(&hw, 0, sizeof(hw));
  1190. ide_init_hwif_ports(&hw, (unsigned long) args[0],
  1191. (unsigned long) args[1], NULL);
  1192. hw.irq = args[2];
  1193. if (ide_register_hw(&hw, NULL) == -1)
  1194. return -EIO;
  1195. return 0;
  1196. }
  1197. case HDIO_UNREGISTER_HWIF:
  1198. if (!capable(CAP_SYS_RAWIO)) return -EACCES;
  1199. /* (arg > MAX_HWIFS) checked in function */
  1200. ide_unregister(arg);
  1201. return 0;
  1202. case HDIO_SET_NICE:
  1203. if (!capable(CAP_SYS_ADMIN)) return -EACCES;
  1204. if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
  1205. return -EPERM;
  1206. drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
  1207. drv = *(ide_driver_t **)bdev->bd_disk->private_data;
  1208. if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
  1209. drive->dsc_overlap = 0;
  1210. return -EPERM;
  1211. }
  1212. drive->nice1 = (arg >> IDE_NICE_1) & 1;
  1213. return 0;
  1214. case HDIO_DRIVE_RESET:
  1215. {
  1216. unsigned long flags;
  1217. if (!capable(CAP_SYS_ADMIN)) return -EACCES;
  1218. /*
  1219. * Abort the current command on the
  1220. * group if there is one, taking
  1221. * care not to allow anything else
  1222. * to be queued and to die on the
  1223. * spot if we miss one somehow
  1224. */
  1225. spin_lock_irqsave(&ide_lock, flags);
  1226. if (HWGROUP(drive)->resetting) {
  1227. spin_unlock_irqrestore(&ide_lock, flags);
  1228. return -EBUSY;
  1229. }
  1230. ide_abort(drive, "drive reset");
  1231. BUG_ON(HWGROUP(drive)->handler);
  1232. /* Ensure nothing gets queued after we
  1233. drop the lock. Reset will clear the busy */
  1234. HWGROUP(drive)->busy = 1;
  1235. spin_unlock_irqrestore(&ide_lock, flags);
  1236. (void) ide_do_reset(drive);
  1237. return 0;
  1238. }
  1239. case CDROMEJECT:
  1240. case CDROMCLOSETRAY:
  1241. return scsi_cmd_ioctl(file, bdev->bd_disk, cmd, p);
  1242. case HDIO_GET_BUSSTATE:
  1243. if (!capable(CAP_SYS_ADMIN))
  1244. return -EACCES;
  1245. if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
  1246. return -EFAULT;
  1247. return 0;
  1248. case HDIO_SET_BUSSTATE:
  1249. if (!capable(CAP_SYS_ADMIN))
  1250. return -EACCES;
  1251. if (HWIF(drive)->busproc)
  1252. return HWIF(drive)->busproc(drive, (int)arg);
  1253. return -EOPNOTSUPP;
  1254. default:
  1255. return -EINVAL;
  1256. }
  1257. }
  1258. EXPORT_SYMBOL(generic_ide_ioctl);
  1259. /*
  1260. * stridx() returns the offset of c within s,
  1261. * or -1 if c is '\0' or not found within s.
  1262. */
  1263. static int __init stridx (const char *s, char c)
  1264. {
  1265. char *i = strchr(s, c);
  1266. return (i && c) ? i - s : -1;
  1267. }
  1268. /*
  1269. * match_parm() does parsing for ide_setup():
  1270. *
  1271. * 1. the first char of s must be '='.
  1272. * 2. if the remainder matches one of the supplied keywords,
  1273. * the index (1 based) of the keyword is negated and returned.
  1274. * 3. if the remainder is a series of no more than max_vals numbers
  1275. * separated by commas, the numbers are saved in vals[] and a
  1276. * count of how many were saved is returned. Base10 is assumed,
  1277. * and base16 is allowed when prefixed with "0x".
  1278. * 4. otherwise, zero is returned.
  1279. */
  1280. static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
  1281. {
  1282. static const char *decimal = "0123456789";
  1283. static const char *hex = "0123456789abcdef";
  1284. int i, n;
  1285. if (*s++ == '=') {
  1286. /*
  1287. * Try matching against the supplied keywords,
  1288. * and return -(index+1) if we match one
  1289. */
  1290. if (keywords != NULL) {
  1291. for (i = 0; *keywords != NULL; ++i) {
  1292. if (!strcmp(s, *keywords++))
  1293. return -(i+1);
  1294. }
  1295. }
  1296. /*
  1297. * Look for a series of no more than "max_vals"
  1298. * numeric values separated by commas, in base10,
  1299. * or base16 when prefixed with "0x".
  1300. * Return a count of how many were found.
  1301. */
  1302. for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
  1303. vals[n] = i;
  1304. while ((i = stridx(decimal, *++s)) >= 0)
  1305. vals[n] = (vals[n] * 10) + i;
  1306. if (*s == 'x' && !vals[n]) {
  1307. while ((i = stridx(hex, *++s)) >= 0)
  1308. vals[n] = (vals[n] * 0x10) + i;
  1309. }
  1310. if (++n == max_vals)
  1311. break;
  1312. if (*s == ',' || *s == ';')
  1313. ++s;
  1314. }
  1315. if (!*s)
  1316. return n;
  1317. }
  1318. return 0; /* zero = nothing matched */
  1319. }
  1320. #ifdef CONFIG_BLK_DEV_ALI14XX
  1321. extern int probe_ali14xx;
  1322. extern int ali14xx_init(void);
  1323. #endif
  1324. #ifdef CONFIG_BLK_DEV_UMC8672
  1325. extern int probe_umc8672;
  1326. extern int umc8672_init(void);
  1327. #endif
  1328. #ifdef CONFIG_BLK_DEV_DTC2278
  1329. extern int probe_dtc2278;
  1330. extern int dtc2278_init(void);
  1331. #endif
  1332. #ifdef CONFIG_BLK_DEV_HT6560B
  1333. extern int probe_ht6560b;
  1334. extern int ht6560b_init(void);
  1335. #endif
  1336. #ifdef CONFIG_BLK_DEV_QD65XX
  1337. extern int probe_qd65xx;
  1338. extern int qd65xx_init(void);
  1339. #endif
  1340. static int __initdata is_chipset_set[MAX_HWIFS];
  1341. /*
  1342. * ide_setup() gets called VERY EARLY during initialization,
  1343. * to handle kernel "command line" strings beginning with "hdx=" or "ide".
  1344. *
  1345. * Remember to update Documentation/ide.txt if you change something here.
  1346. */
  1347. static int __init ide_setup(char *s)
  1348. {
  1349. int i, vals[3];
  1350. ide_hwif_t *hwif;
  1351. ide_drive_t *drive;
  1352. unsigned int hw, unit;
  1353. const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
  1354. const char max_hwif = '0' + (MAX_HWIFS - 1);
  1355. if (strncmp(s,"hd",2) == 0 && s[2] == '=') /* hd= is for hd.c */
  1356. return 0; /* driver and not us */
  1357. if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
  1358. return 0;
  1359. printk(KERN_INFO "ide_setup: %s", s);
  1360. init_ide_data ();
  1361. #ifdef CONFIG_BLK_DEV_IDEDOUBLER
  1362. if (!strcmp(s, "ide=doubler")) {
  1363. extern int ide_doubler;
  1364. printk(" : Enabled support for IDE doublers\n");
  1365. ide_doubler = 1;
  1366. return 1;
  1367. }
  1368. #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
  1369. if (!strcmp(s, "ide=nodma")) {
  1370. printk(" : Prevented DMA\n");
  1371. noautodma = 1;
  1372. return 1;
  1373. }
  1374. #ifdef CONFIG_BLK_DEV_IDEPCI
  1375. if (!strcmp(s, "ide=reverse")) {
  1376. ide_scan_direction = 1;
  1377. printk(" : Enabled support for IDE inverse scan order.\n");
  1378. return 1;
  1379. }
  1380. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1381. #ifdef CONFIG_BLK_DEV_IDEACPI
  1382. if (!strcmp(s, "ide=noacpi")) {
  1383. //printk(" : Disable IDE ACPI support.\n");
  1384. ide_noacpi = 1;
  1385. return 1;
  1386. }
  1387. if (!strcmp(s, "ide=acpigtf")) {
  1388. //printk(" : Enable IDE ACPI _GTF support.\n");
  1389. ide_noacpitfs = 0;
  1390. return 1;
  1391. }
  1392. if (!strcmp(s, "ide=acpionboot")) {
  1393. //printk(" : Call IDE ACPI methods on boot.\n");
  1394. ide_noacpionboot = 0;
  1395. return 1;
  1396. }
  1397. #endif /* CONFIG_BLK_DEV_IDEACPI */
  1398. /*
  1399. * Look for drive options: "hdx="
  1400. */
  1401. if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
  1402. const char *hd_words[] = {
  1403. "none", "noprobe", "nowerr", "cdrom", "minus5",
  1404. "autotune", "noautotune", "minus8", "swapdata", "bswap",
  1405. "noflush", "remap", "remap63", "scsi", NULL };
  1406. unit = s[2] - 'a';
  1407. hw = unit / MAX_DRIVES;
  1408. unit = unit % MAX_DRIVES;
  1409. hwif = &ide_hwifs[hw];
  1410. drive = &hwif->drives[unit];
  1411. if (strncmp(s + 4, "ide-", 4) == 0) {
  1412. strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
  1413. goto done;
  1414. }
  1415. switch (match_parm(&s[3], hd_words, vals, 3)) {
  1416. case -1: /* "none" */
  1417. case -2: /* "noprobe" */
  1418. drive->noprobe = 1;
  1419. goto done;
  1420. case -3: /* "nowerr" */
  1421. drive->bad_wstat = BAD_R_STAT;
  1422. hwif->noprobe = 0;
  1423. goto done;
  1424. case -4: /* "cdrom" */
  1425. drive->present = 1;
  1426. drive->media = ide_cdrom;
  1427. /* an ATAPI device ignores DRDY */
  1428. drive->ready_stat = 0;
  1429. hwif->noprobe = 0;
  1430. goto done;
  1431. case -6: /* "autotune" */
  1432. drive->autotune = IDE_TUNE_AUTO;
  1433. goto obsolete_option;
  1434. case -7: /* "noautotune" */
  1435. drive->autotune = IDE_TUNE_NOAUTO;
  1436. goto obsolete_option;
  1437. case -9: /* "swapdata" */
  1438. case -10: /* "bswap" */
  1439. drive->bswap = 1;
  1440. goto done;
  1441. case -11: /* noflush */
  1442. drive->noflush = 1;
  1443. goto done;
  1444. case -12: /* "remap" */
  1445. drive->remap_0_to_1 = 1;
  1446. goto done;
  1447. case -13: /* "remap63" */
  1448. drive->sect0 = 63;
  1449. goto done;
  1450. case -14: /* "scsi" */
  1451. drive->scsi = 1;
  1452. goto done;
  1453. case 3: /* cyl,head,sect */
  1454. drive->media = ide_disk;
  1455. drive->ready_stat = READY_STAT;
  1456. drive->cyl = drive->bios_cyl = vals[0];
  1457. drive->head = drive->bios_head = vals[1];
  1458. drive->sect = drive->bios_sect = vals[2];
  1459. drive->present = 1;
  1460. drive->forced_geom = 1;
  1461. hwif->noprobe = 0;
  1462. goto done;
  1463. default:
  1464. goto bad_option;
  1465. }
  1466. }
  1467. if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
  1468. goto bad_option;
  1469. /*
  1470. * Look for bus speed option: "idebus="
  1471. */
  1472. if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
  1473. if (match_parm(&s[6], NULL, vals, 1) != 1)
  1474. goto bad_option;
  1475. if (vals[0] >= 20 && vals[0] <= 66) {
  1476. idebus_parameter = vals[0];
  1477. } else
  1478. printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
  1479. goto done;
  1480. }
  1481. /*
  1482. * Look for interface options: "idex="
  1483. */
  1484. if (s[3] >= '0' && s[3] <= max_hwif) {
  1485. /*
  1486. * Be VERY CAREFUL changing this: note hardcoded indexes below
  1487. * (-8, -9, -10) are reserved to ease the hardcoding.
  1488. */
  1489. static const char *ide_words[] = {
  1490. "noprobe", "serialize", "minus3", "minus4",
  1491. "reset", "dma", "ata66", "minus8", "minus9",
  1492. "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
  1493. "dtc2278", "umc8672", "ali14xx", NULL };
  1494. hw = s[3] - '0';
  1495. hwif = &ide_hwifs[hw];
  1496. i = match_parm(&s[4], ide_words, vals, 3);
  1497. /*
  1498. * Cryptic check to ensure chipset not already set for hwif.
  1499. * Note: we can't depend on hwif->chipset here.
  1500. */
  1501. if ((i >= -18 && i <= -11) || (i > 0 && i <= 3)) {
  1502. /* chipset already specified */
  1503. if (is_chipset_set[hw])
  1504. goto bad_option;
  1505. if (i > -18 && i <= -11) {
  1506. /* these drivers are for "ide0=" only */
  1507. if (hw != 0)
  1508. goto bad_hwif;
  1509. /* chipset already specified for 2nd port */
  1510. if (is_chipset_set[hw+1])
  1511. goto bad_option;
  1512. }
  1513. is_chipset_set[hw] = 1;
  1514. printk("\n");
  1515. }
  1516. switch (i) {
  1517. #ifdef CONFIG_BLK_DEV_ALI14XX
  1518. case -17: /* "ali14xx" */
  1519. probe_ali14xx = 1;
  1520. goto done;
  1521. #endif
  1522. #ifdef CONFIG_BLK_DEV_UMC8672
  1523. case -16: /* "umc8672" */
  1524. probe_umc8672 = 1;
  1525. goto done;
  1526. #endif
  1527. #ifdef CONFIG_BLK_DEV_DTC2278
  1528. case -15: /* "dtc2278" */
  1529. probe_dtc2278 = 1;
  1530. goto done;
  1531. #endif
  1532. #ifdef CONFIG_BLK_DEV_CMD640
  1533. case -14: /* "cmd640_vlb" */
  1534. {
  1535. extern int cmd640_vlb; /* flag for cmd640.c */
  1536. cmd640_vlb = 1;
  1537. goto done;
  1538. }
  1539. #endif
  1540. #ifdef CONFIG_BLK_DEV_HT6560B
  1541. case -13: /* "ht6560b" */
  1542. probe_ht6560b = 1;
  1543. goto done;
  1544. #endif
  1545. #ifdef CONFIG_BLK_DEV_QD65XX
  1546. case -12: /* "qd65xx" */
  1547. probe_qd65xx = 1;
  1548. goto done;
  1549. #endif
  1550. #ifdef CONFIG_BLK_DEV_4DRIVES
  1551. case -11: /* "four" drives on one set of ports */
  1552. {
  1553. ide_hwif_t *mate = &ide_hwifs[hw^1];
  1554. mate->drives[0].select.all ^= 0x20;
  1555. mate->drives[1].select.all ^= 0x20;
  1556. hwif->chipset = mate->chipset = ide_4drives;
  1557. mate->irq = hwif->irq;
  1558. memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
  1559. hwif->mate = mate;
  1560. mate->mate = hwif;
  1561. hwif->serialized = mate->serialized = 1;
  1562. goto obsolete_option;
  1563. }
  1564. #endif /* CONFIG_BLK_DEV_4DRIVES */
  1565. case -10: /* minus10 */
  1566. case -9: /* minus9 */
  1567. case -8: /* minus8 */
  1568. case -4:
  1569. case -3:
  1570. goto bad_option;
  1571. case -7: /* ata66 */
  1572. #ifdef CONFIG_BLK_DEV_IDEPCI
  1573. hwif->udma_four = 1;
  1574. goto obsolete_option;
  1575. #else
  1576. goto bad_hwif;
  1577. #endif
  1578. case -6: /* dma */
  1579. hwif->autodma = 1;
  1580. goto obsolete_option;
  1581. case -5: /* "reset" */
  1582. hwif->reset = 1;
  1583. goto obsolete_option;
  1584. case -2: /* "serialize" */
  1585. hwif->mate = &ide_hwifs[hw^1];
  1586. hwif->mate->mate = hwif;
  1587. hwif->serialized = hwif->mate->serialized = 1;
  1588. goto obsolete_option;
  1589. case -1: /* "noprobe" */
  1590. hwif->noprobe = 1;
  1591. goto done;
  1592. case 1: /* base */
  1593. vals[1] = vals[0] + 0x206; /* default ctl */
  1594. case 2: /* base,ctl */
  1595. vals[2] = 0; /* default irq = probe for it */
  1596. case 3: /* base,ctl,irq */
  1597. hwif->hw.irq = vals[2];
  1598. ide_init_hwif_ports(&hwif->hw, (unsigned long) vals[0], (unsigned long) vals[1], &hwif->irq);
  1599. memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
  1600. hwif->irq = vals[2];
  1601. hwif->noprobe = 0;
  1602. hwif->chipset = ide_forced;
  1603. goto obsolete_option;
  1604. case 0: goto bad_option;
  1605. default:
  1606. printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
  1607. return 1;
  1608. }
  1609. }
  1610. bad_option:
  1611. printk(" -- BAD OPTION\n");
  1612. return 1;
  1613. obsolete_option:
  1614. printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
  1615. return 1;
  1616. bad_hwif:
  1617. printk("-- NOT SUPPORTED ON ide%d", hw);
  1618. done:
  1619. printk("\n");
  1620. return 1;
  1621. }
  1622. extern void __init pnpide_init(void);
  1623. extern void __exit pnpide_exit(void);
  1624. extern void __init h8300_ide_init(void);
  1625. /*
  1626. * probe_for_hwifs() finds/initializes "known" IDE interfaces
  1627. */
  1628. static void __init probe_for_hwifs (void)
  1629. {
  1630. #ifdef CONFIG_BLK_DEV_IDEPCI
  1631. ide_scan_pcibus(ide_scan_direction);
  1632. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1633. #ifdef CONFIG_ETRAX_IDE
  1634. {
  1635. extern void init_e100_ide(void);
  1636. init_e100_ide();
  1637. }
  1638. #endif /* CONFIG_ETRAX_IDE */
  1639. #ifdef CONFIG_BLK_DEV_CMD640
  1640. {
  1641. extern void ide_probe_for_cmd640x(void);
  1642. ide_probe_for_cmd640x();
  1643. }
  1644. #endif /* CONFIG_BLK_DEV_CMD640 */
  1645. #ifdef CONFIG_BLK_DEV_IDE_PMAC
  1646. {
  1647. extern int pmac_ide_probe(void);
  1648. (void)pmac_ide_probe();
  1649. }
  1650. #endif /* CONFIG_BLK_DEV_IDE_PMAC */
  1651. #ifdef CONFIG_BLK_DEV_GAYLE
  1652. {
  1653. extern void gayle_init(void);
  1654. gayle_init();
  1655. }
  1656. #endif /* CONFIG_BLK_DEV_GAYLE */
  1657. #ifdef CONFIG_BLK_DEV_FALCON_IDE
  1658. {
  1659. extern void falconide_init(void);
  1660. falconide_init();
  1661. }
  1662. #endif /* CONFIG_BLK_DEV_FALCON_IDE */
  1663. #ifdef CONFIG_BLK_DEV_MAC_IDE
  1664. {
  1665. extern void macide_init(void);
  1666. macide_init();
  1667. }
  1668. #endif /* CONFIG_BLK_DEV_MAC_IDE */
  1669. #ifdef CONFIG_BLK_DEV_Q40IDE
  1670. {
  1671. extern void q40ide_init(void);
  1672. q40ide_init();
  1673. }
  1674. #endif /* CONFIG_BLK_DEV_Q40IDE */
  1675. #ifdef CONFIG_BLK_DEV_BUDDHA
  1676. {
  1677. extern void buddha_init(void);
  1678. buddha_init();
  1679. }
  1680. #endif /* CONFIG_BLK_DEV_BUDDHA */
  1681. #ifdef CONFIG_BLK_DEV_IDEPNP
  1682. pnpide_init();
  1683. #endif
  1684. #ifdef CONFIG_H8300
  1685. h8300_ide_init();
  1686. #endif
  1687. }
  1688. void ide_register_subdriver(ide_drive_t *drive, ide_driver_t *driver)
  1689. {
  1690. #ifdef CONFIG_PROC_FS
  1691. ide_add_proc_entries(drive->proc, driver->proc, drive);
  1692. #endif
  1693. }
  1694. EXPORT_SYMBOL(ide_register_subdriver);
  1695. /**
  1696. * ide_unregister_subdriver - disconnect drive from driver
  1697. * @drive: drive to unplug
  1698. * @driver: driver
  1699. *
  1700. * Disconnect a drive from the driver it was attached to and then
  1701. * clean up the various proc files and other objects attached to it.
  1702. *
  1703. * Takes ide_setting_sem and ide_lock.
  1704. * Caller must hold none of the locks.
  1705. */
  1706. void ide_unregister_subdriver(ide_drive_t *drive, ide_driver_t *driver)
  1707. {
  1708. unsigned long flags;
  1709. #ifdef CONFIG_PROC_FS
  1710. ide_remove_proc_entries(drive->proc, driver->proc);
  1711. #endif
  1712. down(&ide_setting_sem);
  1713. spin_lock_irqsave(&ide_lock, flags);
  1714. /*
  1715. * ide_setting_sem protects the settings list
  1716. * ide_lock protects the use of settings
  1717. *
  1718. * so we need to hold both, ide_settings_sem because we want to
  1719. * modify the settings list, and ide_lock because we cannot take
  1720. * a setting out that is being used.
  1721. *
  1722. * OTOH both ide_{read,write}_setting are only ever used under
  1723. * ide_setting_sem.
  1724. */
  1725. auto_remove_settings(drive);
  1726. spin_unlock_irqrestore(&ide_lock, flags);
  1727. up(&ide_setting_sem);
  1728. }
  1729. EXPORT_SYMBOL(ide_unregister_subdriver);
  1730. /*
  1731. * Probe module
  1732. */
  1733. EXPORT_SYMBOL(ide_lock);
  1734. static int ide_bus_match(struct device *dev, struct device_driver *drv)
  1735. {
  1736. return 1;
  1737. }
  1738. static char *media_string(ide_drive_t *drive)
  1739. {
  1740. switch (drive->media) {
  1741. case ide_disk:
  1742. return "disk";
  1743. case ide_cdrom:
  1744. return "cdrom";
  1745. case ide_tape:
  1746. return "tape";
  1747. case ide_floppy:
  1748. return "floppy";
  1749. case ide_optical:
  1750. return "optical";
  1751. default:
  1752. return "UNKNOWN";
  1753. }
  1754. }
  1755. static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
  1756. {
  1757. ide_drive_t *drive = to_ide_device(dev);
  1758. return sprintf(buf, "%s\n", media_string(drive));
  1759. }
  1760. static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
  1761. {
  1762. ide_drive_t *drive = to_ide_device(dev);
  1763. return sprintf(buf, "%s\n", drive->name);
  1764. }
  1765. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  1766. {
  1767. ide_drive_t *drive = to_ide_device(dev);
  1768. return sprintf(buf, "ide:m-%s\n", media_string(drive));
  1769. }
  1770. static struct device_attribute ide_dev_attrs[] = {
  1771. __ATTR_RO(media),
  1772. __ATTR_RO(drivename),
  1773. __ATTR_RO(modalias),
  1774. __ATTR_NULL
  1775. };
  1776. static int ide_uevent(struct device *dev, char **envp, int num_envp,
  1777. char *buffer, int buffer_size)
  1778. {
  1779. ide_drive_t *drive = to_ide_device(dev);
  1780. int i = 0;
  1781. int length = 0;
  1782. add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
  1783. "MEDIA=%s", media_string(drive));
  1784. add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
  1785. "DRIVENAME=%s", drive->name);
  1786. add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
  1787. "MODALIAS=ide:m-%s", media_string(drive));
  1788. envp[i] = NULL;
  1789. return 0;
  1790. }
  1791. static int generic_ide_probe(struct device *dev)
  1792. {
  1793. ide_drive_t *drive = to_ide_device(dev);
  1794. ide_driver_t *drv = to_ide_driver(dev->driver);
  1795. return drv->probe ? drv->probe(drive) : -ENODEV;
  1796. }
  1797. static int generic_ide_remove(struct device *dev)
  1798. {
  1799. ide_drive_t *drive = to_ide_device(dev);
  1800. ide_driver_t *drv = to_ide_driver(dev->driver);
  1801. if (drv->remove)
  1802. drv->remove(drive);
  1803. return 0;
  1804. }
  1805. static void generic_ide_shutdown(struct device *dev)
  1806. {
  1807. ide_drive_t *drive = to_ide_device(dev);
  1808. ide_driver_t *drv = to_ide_driver(dev->driver);
  1809. if (dev->driver && drv->shutdown)
  1810. drv->shutdown(drive);
  1811. }
  1812. struct bus_type ide_bus_type = {
  1813. .name = "ide",
  1814. .match = ide_bus_match,
  1815. .uevent = ide_uevent,
  1816. .probe = generic_ide_probe,
  1817. .remove = generic_ide_remove,
  1818. .shutdown = generic_ide_shutdown,
  1819. .dev_attrs = ide_dev_attrs,
  1820. .suspend = generic_ide_suspend,
  1821. .resume = generic_ide_resume,
  1822. };
  1823. EXPORT_SYMBOL_GPL(ide_bus_type);
  1824. /*
  1825. * This is gets invoked once during initialization, to set *everything* up
  1826. */
  1827. static int __init ide_init(void)
  1828. {
  1829. int ret;
  1830. printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
  1831. system_bus_speed = ide_system_bus_speed();
  1832. ret = bus_register(&ide_bus_type);
  1833. if (ret < 0) {
  1834. printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
  1835. return ret;
  1836. }
  1837. init_ide_data();
  1838. #ifdef CONFIG_PROC_FS
  1839. proc_ide_root = proc_mkdir("ide", NULL);
  1840. #endif
  1841. #ifdef CONFIG_BLK_DEV_ALI14XX
  1842. if (probe_ali14xx)
  1843. (void)ali14xx_init();
  1844. #endif
  1845. #ifdef CONFIG_BLK_DEV_UMC8672
  1846. if (probe_umc8672)
  1847. (void)umc8672_init();
  1848. #endif
  1849. #ifdef CONFIG_BLK_DEV_DTC2278
  1850. if (probe_dtc2278)
  1851. (void)dtc2278_init();
  1852. #endif
  1853. #ifdef CONFIG_BLK_DEV_HT6560B
  1854. if (probe_ht6560b)
  1855. (void)ht6560b_init();
  1856. #endif
  1857. #ifdef CONFIG_BLK_DEV_QD65XX
  1858. if (probe_qd65xx)
  1859. (void)qd65xx_init();
  1860. #endif
  1861. initializing = 1;
  1862. /* Probe for special PCI and other "known" interface chipsets. */
  1863. probe_for_hwifs();
  1864. initializing = 0;
  1865. #ifdef CONFIG_PROC_FS
  1866. proc_ide_create();
  1867. #endif
  1868. return 0;
  1869. }
  1870. #ifdef MODULE
  1871. static char *options = NULL;
  1872. module_param(options, charp, 0);
  1873. MODULE_LICENSE("GPL");
  1874. static void __init parse_options (char *line)
  1875. {
  1876. char *next = line;
  1877. if (line == NULL || !*line)
  1878. return;
  1879. while ((line = next) != NULL) {
  1880. if ((next = strchr(line,' ')) != NULL)
  1881. *next++ = 0;
  1882. if (!ide_setup(line))
  1883. printk (KERN_INFO "Unknown option '%s'\n", line);
  1884. }
  1885. }
  1886. int __init init_module (void)
  1887. {
  1888. parse_options(options);
  1889. return ide_init();
  1890. }
  1891. void __exit cleanup_module (void)
  1892. {
  1893. int index;
  1894. for (index = 0; index < MAX_HWIFS; ++index)
  1895. ide_unregister(index);
  1896. #ifdef CONFIG_BLK_DEV_IDEPNP
  1897. pnpide_exit();
  1898. #endif
  1899. #ifdef CONFIG_PROC_FS
  1900. proc_ide_destroy();
  1901. #endif
  1902. bus_unregister(&ide_bus_type);
  1903. }
  1904. #else /* !MODULE */
  1905. __setup("", ide_setup);
  1906. module_init(ide_init);
  1907. #endif /* MODULE */