ide.c 56 KB

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