ide.c 57 KB

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