ide.c 42 KB

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