ide.c 57 KB

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