ide.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 _IDE_C /* Tell ide.h it's really us */
  46. #include <linux/module.h>
  47. #include <linux/types.h>
  48. #include <linux/string.h>
  49. #include <linux/kernel.h>
  50. #include <linux/timer.h>
  51. #include <linux/mm.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/major.h>
  54. #include <linux/errno.h>
  55. #include <linux/genhd.h>
  56. #include <linux/blkpg.h>
  57. #include <linux/slab.h>
  58. #include <linux/init.h>
  59. #include <linux/pci.h>
  60. #include <linux/delay.h>
  61. #include <linux/ide.h>
  62. #include <linux/completion.h>
  63. #include <linux/reboot.h>
  64. #include <linux/cdrom.h>
  65. #include <linux/seq_file.h>
  66. #include <linux/device.h>
  67. #include <linux/bitops.h>
  68. #include <asm/byteorder.h>
  69. #include <asm/irq.h>
  70. #include <asm/uaccess.h>
  71. #include <asm/io.h>
  72. /* default maximum number of failures */
  73. #define IDE_DEFAULT_MAX_FAILURES 1
  74. struct class *ide_port_class;
  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. DEFINE_MUTEX(ide_cfg_mtx);
  81. __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
  82. int noautodma = 0;
  83. ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
  84. static void ide_port_init_devices_data(ide_hwif_t *);
  85. /*
  86. * Do not even *think* about calling this!
  87. */
  88. void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
  89. {
  90. /* bulk initialize hwif & drive info with zeros */
  91. memset(hwif, 0, sizeof(ide_hwif_t));
  92. /* fill in any non-zero initial values */
  93. hwif->index = index;
  94. hwif->major = ide_hwif_to_major[index];
  95. hwif->name[0] = 'i';
  96. hwif->name[1] = 'd';
  97. hwif->name[2] = 'e';
  98. hwif->name[3] = '0' + index;
  99. hwif->bus_state = BUSSTATE_ON;
  100. init_completion(&hwif->gendev_rel_comp);
  101. default_hwif_iops(hwif);
  102. default_hwif_transport(hwif);
  103. ide_port_init_devices_data(hwif);
  104. }
  105. EXPORT_SYMBOL_GPL(ide_init_port_data);
  106. static void ide_port_init_devices_data(ide_hwif_t *hwif)
  107. {
  108. int unit;
  109. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  110. ide_drive_t *drive = &hwif->drives[unit];
  111. u8 j = (hwif->index * MAX_DRIVES) + unit;
  112. memset(drive, 0, sizeof(*drive));
  113. drive->media = ide_disk;
  114. drive->select.all = (unit<<4)|0xa0;
  115. drive->hwif = hwif;
  116. drive->ctl = 0x08;
  117. drive->ready_stat = READY_STAT;
  118. drive->bad_wstat = BAD_W_STAT;
  119. drive->special.b.recalibrate = 1;
  120. drive->special.b.set_geometry = 1;
  121. drive->name[0] = 'h';
  122. drive->name[1] = 'd';
  123. drive->name[2] = 'a' + j;
  124. drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
  125. INIT_LIST_HEAD(&drive->list);
  126. init_completion(&drive->gendev_rel_comp);
  127. }
  128. }
  129. /*
  130. * init_ide_data() sets reasonable default values into all fields
  131. * of all instances of the hwifs and drives, but only on the first call.
  132. * Subsequent calls have no effect (they don't wipe out anything).
  133. *
  134. * This routine is normally called at driver initialization time,
  135. * but may also be called MUCH earlier during kernel "command-line"
  136. * parameter processing. As such, we cannot depend on any other parts
  137. * of the kernel (such as memory allocation) to be functioning yet.
  138. *
  139. * This is too bad, as otherwise we could dynamically allocate the
  140. * ide_drive_t structs as needed, rather than always consuming memory
  141. * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
  142. *
  143. * FIXME: We should stuff the setup data into __init and copy the
  144. * relevant hwifs/allocate them properly during boot.
  145. */
  146. #define MAGIC_COOKIE 0x12345678
  147. static void __init init_ide_data (void)
  148. {
  149. unsigned int index;
  150. static unsigned long magic_cookie = MAGIC_COOKIE;
  151. if (magic_cookie != MAGIC_COOKIE)
  152. return; /* already initialized */
  153. magic_cookie = 0;
  154. /* Initialise all interface structures */
  155. for (index = 0; index < MAX_HWIFS; ++index) {
  156. ide_hwif_t *hwif = &ide_hwifs[index];
  157. ide_init_port_data(hwif, index);
  158. }
  159. }
  160. void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
  161. {
  162. ide_hwgroup_t *hwgroup = hwif->hwgroup;
  163. spin_lock_irq(&ide_lock);
  164. /*
  165. * Remove us from the hwgroup, and free
  166. * the hwgroup if we were the only member
  167. */
  168. if (hwif->next == hwif) {
  169. BUG_ON(hwgroup->hwif != hwif);
  170. kfree(hwgroup);
  171. } else {
  172. /* There is another interface in hwgroup.
  173. * Unlink us, and set hwgroup->drive and ->hwif to
  174. * something sane.
  175. */
  176. ide_hwif_t *g = hwgroup->hwif;
  177. while (g->next != hwif)
  178. g = g->next;
  179. g->next = hwif->next;
  180. if (hwgroup->hwif == hwif) {
  181. /* Chose a random hwif for hwgroup->hwif.
  182. * It's guaranteed that there are no drives
  183. * left in the hwgroup.
  184. */
  185. BUG_ON(hwgroup->drive != NULL);
  186. hwgroup->hwif = g;
  187. }
  188. BUG_ON(hwgroup->hwif == hwif);
  189. }
  190. spin_unlock_irq(&ide_lock);
  191. }
  192. /* Called with ide_lock held. */
  193. static void __ide_port_unregister_devices(ide_hwif_t *hwif)
  194. {
  195. int i;
  196. for (i = 0; i < MAX_DRIVES; i++) {
  197. ide_drive_t *drive = &hwif->drives[i];
  198. if (drive->present) {
  199. spin_unlock_irq(&ide_lock);
  200. device_unregister(&drive->gendev);
  201. wait_for_completion(&drive->gendev_rel_comp);
  202. spin_lock_irq(&ide_lock);
  203. }
  204. }
  205. }
  206. void ide_port_unregister_devices(ide_hwif_t *hwif)
  207. {
  208. mutex_lock(&ide_cfg_mtx);
  209. spin_lock_irq(&ide_lock);
  210. __ide_port_unregister_devices(hwif);
  211. hwif->present = 0;
  212. ide_port_init_devices_data(hwif);
  213. spin_unlock_irq(&ide_lock);
  214. mutex_unlock(&ide_cfg_mtx);
  215. }
  216. EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
  217. /**
  218. * ide_unregister - free an IDE interface
  219. * @hwif: IDE interface
  220. *
  221. * Perform the final unregister of an IDE interface. At the moment
  222. * we don't refcount interfaces so this will also get split up.
  223. *
  224. * Locking:
  225. * The caller must not hold the IDE locks
  226. * The drive present/vanishing is not yet properly locked
  227. * Take care with the callbacks. These have been split to avoid
  228. * deadlocking the IDE layer. The shutdown callback is called
  229. * before we take the lock and free resources. It is up to the
  230. * caller to be sure there is no pending I/O here, and that
  231. * the interface will not be reopened (present/vanishing locking
  232. * isn't yet done BTW). After we commit to the final kill we
  233. * call the cleanup callback with the ide locks held.
  234. *
  235. * Unregister restores the hwif structures to the default state.
  236. * This is raving bonkers.
  237. */
  238. void ide_unregister(ide_hwif_t *hwif)
  239. {
  240. ide_hwif_t *g;
  241. ide_hwgroup_t *hwgroup;
  242. int irq_count = 0;
  243. BUG_ON(in_interrupt());
  244. BUG_ON(irqs_disabled());
  245. mutex_lock(&ide_cfg_mtx);
  246. spin_lock_irq(&ide_lock);
  247. if (hwif->present) {
  248. __ide_port_unregister_devices(hwif);
  249. hwif->present = 0;
  250. }
  251. spin_unlock_irq(&ide_lock);
  252. ide_proc_unregister_port(hwif);
  253. hwgroup = hwif->hwgroup;
  254. /*
  255. * free the irq if we were the only hwif using it
  256. */
  257. g = hwgroup->hwif;
  258. do {
  259. if (g->irq == hwif->irq)
  260. ++irq_count;
  261. g = g->next;
  262. } while (g != hwgroup->hwif);
  263. if (irq_count == 1)
  264. free_irq(hwif->irq, hwgroup);
  265. ide_remove_port_from_hwgroup(hwif);
  266. device_unregister(hwif->portdev);
  267. device_unregister(&hwif->gendev);
  268. wait_for_completion(&hwif->gendev_rel_comp);
  269. /*
  270. * Remove us from the kernel's knowledge
  271. */
  272. blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
  273. kfree(hwif->sg_table);
  274. unregister_blkdev(hwif->major, hwif->name);
  275. if (hwif->dma_base)
  276. ide_release_dma_engine(hwif);
  277. spin_lock_irq(&ide_lock);
  278. /* restore hwif data to pristine status */
  279. ide_init_port_data(hwif, hwif->index);
  280. spin_unlock_irq(&ide_lock);
  281. mutex_unlock(&ide_cfg_mtx);
  282. }
  283. EXPORT_SYMBOL(ide_unregister);
  284. void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
  285. {
  286. memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
  287. hwif->irq = hw->irq;
  288. hwif->chipset = hw->chipset;
  289. hwif->gendev.parent = hw->dev;
  290. hwif->ack_intr = hw->ack_intr;
  291. }
  292. EXPORT_SYMBOL_GPL(ide_init_port_hw);
  293. /*
  294. * Locks for IDE setting functionality
  295. */
  296. DEFINE_MUTEX(ide_setting_mtx);
  297. EXPORT_SYMBOL_GPL(ide_setting_mtx);
  298. /**
  299. * ide_spin_wait_hwgroup - wait for group
  300. * @drive: drive in the group
  301. *
  302. * Wait for an IDE device group to go non busy and then return
  303. * holding the ide_lock which guards the hwgroup->busy status
  304. * and right to use it.
  305. */
  306. int ide_spin_wait_hwgroup (ide_drive_t *drive)
  307. {
  308. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  309. unsigned long timeout = jiffies + (3 * HZ);
  310. spin_lock_irq(&ide_lock);
  311. while (hwgroup->busy) {
  312. unsigned long lflags;
  313. spin_unlock_irq(&ide_lock);
  314. local_irq_set(lflags);
  315. if (time_after(jiffies, timeout)) {
  316. local_irq_restore(lflags);
  317. printk(KERN_ERR "%s: channel busy\n", drive->name);
  318. return -EBUSY;
  319. }
  320. local_irq_restore(lflags);
  321. spin_lock_irq(&ide_lock);
  322. }
  323. return 0;
  324. }
  325. EXPORT_SYMBOL(ide_spin_wait_hwgroup);
  326. int set_io_32bit(ide_drive_t *drive, int arg)
  327. {
  328. if (drive->no_io_32bit)
  329. return -EPERM;
  330. if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
  331. return -EINVAL;
  332. if (ide_spin_wait_hwgroup(drive))
  333. return -EBUSY;
  334. drive->io_32bit = arg;
  335. spin_unlock_irq(&ide_lock);
  336. return 0;
  337. }
  338. static int set_ksettings(ide_drive_t *drive, int arg)
  339. {
  340. if (arg < 0 || arg > 1)
  341. return -EINVAL;
  342. if (ide_spin_wait_hwgroup(drive))
  343. return -EBUSY;
  344. drive->keep_settings = arg;
  345. spin_unlock_irq(&ide_lock);
  346. return 0;
  347. }
  348. int set_using_dma(ide_drive_t *drive, int arg)
  349. {
  350. #ifdef CONFIG_BLK_DEV_IDEDMA
  351. ide_hwif_t *hwif = drive->hwif;
  352. int err = -EPERM;
  353. if (arg < 0 || arg > 1)
  354. return -EINVAL;
  355. if (!drive->id || !(drive->id->capability & 1))
  356. goto out;
  357. if (hwif->dma_ops == NULL)
  358. goto out;
  359. err = -EBUSY;
  360. if (ide_spin_wait_hwgroup(drive))
  361. goto out;
  362. /*
  363. * set ->busy flag, unlock and let it ride
  364. */
  365. hwif->hwgroup->busy = 1;
  366. spin_unlock_irq(&ide_lock);
  367. err = 0;
  368. if (arg) {
  369. if (ide_set_dma(drive))
  370. err = -EIO;
  371. } else
  372. ide_dma_off(drive);
  373. /*
  374. * lock, clear ->busy flag and unlock before leaving
  375. */
  376. spin_lock_irq(&ide_lock);
  377. hwif->hwgroup->busy = 0;
  378. spin_unlock_irq(&ide_lock);
  379. out:
  380. return err;
  381. #else
  382. if (arg < 0 || arg > 1)
  383. return -EINVAL;
  384. return -EPERM;
  385. #endif
  386. }
  387. int set_pio_mode(ide_drive_t *drive, int arg)
  388. {
  389. struct request *rq;
  390. ide_hwif_t *hwif = drive->hwif;
  391. const struct ide_port_ops *port_ops = hwif->port_ops;
  392. if (arg < 0 || arg > 255)
  393. return -EINVAL;
  394. if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
  395. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  396. return -ENOSYS;
  397. if (drive->special.b.set_tune)
  398. return -EBUSY;
  399. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  400. rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
  401. drive->tune_req = (u8) arg;
  402. drive->special.b.set_tune = 1;
  403. blk_execute_rq(drive->queue, NULL, rq, 0);
  404. blk_put_request(rq);
  405. return 0;
  406. }
  407. static int set_unmaskirq(ide_drive_t *drive, int arg)
  408. {
  409. if (drive->no_unmask)
  410. return -EPERM;
  411. if (arg < 0 || arg > 1)
  412. return -EINVAL;
  413. if (ide_spin_wait_hwgroup(drive))
  414. return -EBUSY;
  415. drive->unmask = arg;
  416. spin_unlock_irq(&ide_lock);
  417. return 0;
  418. }
  419. static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
  420. {
  421. ide_drive_t *drive = dev->driver_data;
  422. ide_hwif_t *hwif = HWIF(drive);
  423. struct request *rq;
  424. struct request_pm_state rqpm;
  425. ide_task_t args;
  426. int ret;
  427. /* Call ACPI _GTM only once */
  428. if (!(drive->dn % 2))
  429. ide_acpi_get_timing(hwif);
  430. memset(&rqpm, 0, sizeof(rqpm));
  431. memset(&args, 0, sizeof(args));
  432. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  433. rq->cmd_type = REQ_TYPE_PM_SUSPEND;
  434. rq->special = &args;
  435. rq->data = &rqpm;
  436. rqpm.pm_step = ide_pm_state_start_suspend;
  437. if (mesg.event == PM_EVENT_PRETHAW)
  438. mesg.event = PM_EVENT_FREEZE;
  439. rqpm.pm_state = mesg.event;
  440. ret = blk_execute_rq(drive->queue, NULL, rq, 0);
  441. blk_put_request(rq);
  442. /* only call ACPI _PS3 after both drivers are suspended */
  443. if (!ret && (((drive->dn % 2) && hwif->drives[0].present
  444. && hwif->drives[1].present)
  445. || !hwif->drives[0].present
  446. || !hwif->drives[1].present))
  447. ide_acpi_set_state(hwif, 0);
  448. return ret;
  449. }
  450. static int generic_ide_resume(struct device *dev)
  451. {
  452. ide_drive_t *drive = dev->driver_data;
  453. ide_hwif_t *hwif = HWIF(drive);
  454. struct request *rq;
  455. struct request_pm_state rqpm;
  456. ide_task_t args;
  457. int err;
  458. /* Call ACPI _STM only once */
  459. if (!(drive->dn % 2)) {
  460. ide_acpi_set_state(hwif, 1);
  461. ide_acpi_push_timing(hwif);
  462. }
  463. ide_acpi_exec_tfs(drive);
  464. memset(&rqpm, 0, sizeof(rqpm));
  465. memset(&args, 0, sizeof(args));
  466. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  467. rq->cmd_type = REQ_TYPE_PM_RESUME;
  468. rq->cmd_flags |= REQ_PREEMPT;
  469. rq->special = &args;
  470. rq->data = &rqpm;
  471. rqpm.pm_step = ide_pm_state_start_resume;
  472. rqpm.pm_state = PM_EVENT_ON;
  473. err = blk_execute_rq(drive->queue, NULL, rq, 1);
  474. blk_put_request(rq);
  475. if (err == 0 && dev->driver) {
  476. ide_driver_t *drv = to_ide_driver(dev->driver);
  477. if (drv->resume)
  478. drv->resume(drive);
  479. }
  480. return err;
  481. }
  482. int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
  483. unsigned int cmd, unsigned long arg)
  484. {
  485. unsigned long flags;
  486. ide_driver_t *drv;
  487. void __user *p = (void __user *)arg;
  488. int err = 0, (*setfunc)(ide_drive_t *, int);
  489. u8 *val;
  490. switch (cmd) {
  491. case HDIO_GET_32BIT: val = &drive->io_32bit; goto read_val;
  492. case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val;
  493. case HDIO_GET_UNMASKINTR: val = &drive->unmask; goto read_val;
  494. case HDIO_GET_DMA: val = &drive->using_dma; goto read_val;
  495. case HDIO_SET_32BIT: setfunc = set_io_32bit; goto set_val;
  496. case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings; goto set_val;
  497. case HDIO_SET_PIO_MODE: setfunc = set_pio_mode; goto set_val;
  498. case HDIO_SET_UNMASKINTR: setfunc = set_unmaskirq; goto set_val;
  499. case HDIO_SET_DMA: setfunc = set_using_dma; goto set_val;
  500. }
  501. switch (cmd) {
  502. case HDIO_OBSOLETE_IDENTITY:
  503. case HDIO_GET_IDENTITY:
  504. if (bdev != bdev->bd_contains)
  505. return -EINVAL;
  506. if (drive->id_read == 0)
  507. return -ENOMSG;
  508. if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
  509. return -EFAULT;
  510. return 0;
  511. case HDIO_GET_NICE:
  512. return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
  513. drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
  514. drive->nice1 << IDE_NICE_1,
  515. (long __user *) arg);
  516. #ifdef CONFIG_IDE_TASK_IOCTL
  517. case HDIO_DRIVE_TASKFILE:
  518. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  519. return -EACCES;
  520. switch(drive->media) {
  521. case ide_disk:
  522. return ide_taskfile_ioctl(drive, cmd, arg);
  523. default:
  524. return -ENOMSG;
  525. }
  526. #endif /* CONFIG_IDE_TASK_IOCTL */
  527. case HDIO_DRIVE_CMD:
  528. if (!capable(CAP_SYS_RAWIO))
  529. return -EACCES;
  530. return ide_cmd_ioctl(drive, cmd, arg);
  531. case HDIO_DRIVE_TASK:
  532. if (!capable(CAP_SYS_RAWIO))
  533. return -EACCES;
  534. return ide_task_ioctl(drive, cmd, arg);
  535. case HDIO_SET_NICE:
  536. if (!capable(CAP_SYS_ADMIN)) return -EACCES;
  537. if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
  538. return -EPERM;
  539. drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
  540. drv = *(ide_driver_t **)bdev->bd_disk->private_data;
  541. if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
  542. drive->dsc_overlap = 0;
  543. return -EPERM;
  544. }
  545. drive->nice1 = (arg >> IDE_NICE_1) & 1;
  546. return 0;
  547. case HDIO_DRIVE_RESET:
  548. if (!capable(CAP_SYS_ADMIN))
  549. return -EACCES;
  550. /*
  551. * Abort the current command on the
  552. * group if there is one, taking
  553. * care not to allow anything else
  554. * to be queued and to die on the
  555. * spot if we miss one somehow
  556. */
  557. spin_lock_irqsave(&ide_lock, flags);
  558. if (HWGROUP(drive)->resetting) {
  559. spin_unlock_irqrestore(&ide_lock, flags);
  560. return -EBUSY;
  561. }
  562. ide_abort(drive, "drive reset");
  563. BUG_ON(HWGROUP(drive)->handler);
  564. /* Ensure nothing gets queued after we
  565. drop the lock. Reset will clear the busy */
  566. HWGROUP(drive)->busy = 1;
  567. spin_unlock_irqrestore(&ide_lock, flags);
  568. (void) ide_do_reset(drive);
  569. return 0;
  570. case HDIO_GET_BUSSTATE:
  571. if (!capable(CAP_SYS_ADMIN))
  572. return -EACCES;
  573. if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
  574. return -EFAULT;
  575. return 0;
  576. case HDIO_SET_BUSSTATE:
  577. if (!capable(CAP_SYS_ADMIN))
  578. return -EACCES;
  579. return -EOPNOTSUPP;
  580. default:
  581. return -EINVAL;
  582. }
  583. read_val:
  584. mutex_lock(&ide_setting_mtx);
  585. spin_lock_irqsave(&ide_lock, flags);
  586. err = *val;
  587. spin_unlock_irqrestore(&ide_lock, flags);
  588. mutex_unlock(&ide_setting_mtx);
  589. return err >= 0 ? put_user(err, (long __user *)arg) : err;
  590. set_val:
  591. if (bdev != bdev->bd_contains)
  592. err = -EINVAL;
  593. else {
  594. if (!capable(CAP_SYS_ADMIN))
  595. err = -EACCES;
  596. else {
  597. mutex_lock(&ide_setting_mtx);
  598. err = setfunc(drive, arg);
  599. mutex_unlock(&ide_setting_mtx);
  600. }
  601. }
  602. return err;
  603. }
  604. EXPORT_SYMBOL(generic_ide_ioctl);
  605. /*
  606. * ide_setup() gets called VERY EARLY during initialization,
  607. * to handle kernel "command line" strings beginning with "ide".
  608. *
  609. * Remember to update Documentation/ide/ide.txt if you change something here.
  610. */
  611. static int __init ide_setup(char *s)
  612. {
  613. printk(KERN_INFO "ide_setup: %s", s);
  614. init_ide_data ();
  615. #ifdef CONFIG_BLK_DEV_IDEDOUBLER
  616. if (!strcmp(s, "ide=doubler")) {
  617. extern int ide_doubler;
  618. printk(" : Enabled support for IDE doublers\n");
  619. ide_doubler = 1;
  620. goto obsolete_option;
  621. }
  622. #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
  623. if (!strcmp(s, "ide=nodma")) {
  624. printk(" : Prevented DMA\n");
  625. noautodma = 1;
  626. goto obsolete_option;
  627. }
  628. #ifdef CONFIG_BLK_DEV_IDEACPI
  629. if (!strcmp(s, "ide=noacpi")) {
  630. //printk(" : Disable IDE ACPI support.\n");
  631. ide_noacpi = 1;
  632. goto obsolete_option;
  633. }
  634. if (!strcmp(s, "ide=acpigtf")) {
  635. //printk(" : Enable IDE ACPI _GTF support.\n");
  636. ide_acpigtf = 1;
  637. goto obsolete_option;
  638. }
  639. if (!strcmp(s, "ide=acpionboot")) {
  640. //printk(" : Call IDE ACPI methods on boot.\n");
  641. ide_acpionboot = 1;
  642. goto obsolete_option;
  643. }
  644. #endif /* CONFIG_BLK_DEV_IDEACPI */
  645. printk(" -- BAD OPTION\n");
  646. return 1;
  647. obsolete_option:
  648. printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
  649. return 1;
  650. }
  651. EXPORT_SYMBOL(ide_lock);
  652. static int ide_bus_match(struct device *dev, struct device_driver *drv)
  653. {
  654. return 1;
  655. }
  656. static char *media_string(ide_drive_t *drive)
  657. {
  658. switch (drive->media) {
  659. case ide_disk:
  660. return "disk";
  661. case ide_cdrom:
  662. return "cdrom";
  663. case ide_tape:
  664. return "tape";
  665. case ide_floppy:
  666. return "floppy";
  667. case ide_optical:
  668. return "optical";
  669. default:
  670. return "UNKNOWN";
  671. }
  672. }
  673. static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
  674. {
  675. ide_drive_t *drive = to_ide_device(dev);
  676. return sprintf(buf, "%s\n", media_string(drive));
  677. }
  678. static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
  679. {
  680. ide_drive_t *drive = to_ide_device(dev);
  681. return sprintf(buf, "%s\n", drive->name);
  682. }
  683. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  684. {
  685. ide_drive_t *drive = to_ide_device(dev);
  686. return sprintf(buf, "ide:m-%s\n", media_string(drive));
  687. }
  688. static ssize_t model_show(struct device *dev, struct device_attribute *attr,
  689. char *buf)
  690. {
  691. ide_drive_t *drive = to_ide_device(dev);
  692. return sprintf(buf, "%s\n", drive->id->model);
  693. }
  694. static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
  695. char *buf)
  696. {
  697. ide_drive_t *drive = to_ide_device(dev);
  698. return sprintf(buf, "%s\n", drive->id->fw_rev);
  699. }
  700. static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
  701. char *buf)
  702. {
  703. ide_drive_t *drive = to_ide_device(dev);
  704. return sprintf(buf, "%s\n", drive->id->serial_no);
  705. }
  706. static struct device_attribute ide_dev_attrs[] = {
  707. __ATTR_RO(media),
  708. __ATTR_RO(drivename),
  709. __ATTR_RO(modalias),
  710. __ATTR_RO(model),
  711. __ATTR_RO(firmware),
  712. __ATTR(serial, 0400, serial_show, NULL),
  713. __ATTR_NULL
  714. };
  715. static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
  716. {
  717. ide_drive_t *drive = to_ide_device(dev);
  718. add_uevent_var(env, "MEDIA=%s", media_string(drive));
  719. add_uevent_var(env, "DRIVENAME=%s", drive->name);
  720. add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
  721. return 0;
  722. }
  723. static int generic_ide_probe(struct device *dev)
  724. {
  725. ide_drive_t *drive = to_ide_device(dev);
  726. ide_driver_t *drv = to_ide_driver(dev->driver);
  727. return drv->probe ? drv->probe(drive) : -ENODEV;
  728. }
  729. static int generic_ide_remove(struct device *dev)
  730. {
  731. ide_drive_t *drive = to_ide_device(dev);
  732. ide_driver_t *drv = to_ide_driver(dev->driver);
  733. if (drv->remove)
  734. drv->remove(drive);
  735. return 0;
  736. }
  737. static void generic_ide_shutdown(struct device *dev)
  738. {
  739. ide_drive_t *drive = to_ide_device(dev);
  740. ide_driver_t *drv = to_ide_driver(dev->driver);
  741. if (dev->driver && drv->shutdown)
  742. drv->shutdown(drive);
  743. }
  744. struct bus_type ide_bus_type = {
  745. .name = "ide",
  746. .match = ide_bus_match,
  747. .uevent = ide_uevent,
  748. .probe = generic_ide_probe,
  749. .remove = generic_ide_remove,
  750. .shutdown = generic_ide_shutdown,
  751. .dev_attrs = ide_dev_attrs,
  752. .suspend = generic_ide_suspend,
  753. .resume = generic_ide_resume,
  754. };
  755. EXPORT_SYMBOL_GPL(ide_bus_type);
  756. int ide_vlb_clk;
  757. EXPORT_SYMBOL_GPL(ide_vlb_clk);
  758. module_param_named(vlb_clock, ide_vlb_clk, int, 0);
  759. MODULE_PARM_DESC(vlb_clock, "VLB clock frequency (in MHz)");
  760. int ide_pci_clk;
  761. EXPORT_SYMBOL_GPL(ide_pci_clk);
  762. module_param_named(pci_clock, ide_pci_clk, int, 0);
  763. MODULE_PARM_DESC(pci_clock, "PCI bus clock frequency (in MHz)");
  764. static int ide_set_dev_param_mask(const char *s, struct kernel_param *kp)
  765. {
  766. int a, b, i, j = 1;
  767. unsigned int *dev_param_mask = (unsigned int *)kp->arg;
  768. if (sscanf(s, "%d.%d:%d", &a, &b, &j) != 3 &&
  769. sscanf(s, "%d.%d", &a, &b) != 2)
  770. return -EINVAL;
  771. i = a * MAX_DRIVES + b;
  772. if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
  773. return -EINVAL;
  774. if (j)
  775. *dev_param_mask |= (1 << i);
  776. else
  777. *dev_param_mask &= (1 << i);
  778. return 0;
  779. }
  780. static unsigned int ide_nodma;
  781. module_param_call(nodma, ide_set_dev_param_mask, NULL, &ide_nodma, 0);
  782. MODULE_PARM_DESC(nodma, "disallow DMA for a device");
  783. static unsigned int ide_noflush;
  784. module_param_call(noflush, ide_set_dev_param_mask, NULL, &ide_noflush, 0);
  785. MODULE_PARM_DESC(noflush, "disable flush requests for a device");
  786. static unsigned int ide_noprobe;
  787. module_param_call(noprobe, ide_set_dev_param_mask, NULL, &ide_noprobe, 0);
  788. MODULE_PARM_DESC(noprobe, "skip probing for a device");
  789. static unsigned int ide_nowerr;
  790. module_param_call(nowerr, ide_set_dev_param_mask, NULL, &ide_nowerr, 0);
  791. MODULE_PARM_DESC(nowerr, "ignore the WRERR_STAT bit for a device");
  792. static unsigned int ide_cdroms;
  793. module_param_call(cdrom, ide_set_dev_param_mask, NULL, &ide_cdroms, 0);
  794. MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
  795. struct chs_geom {
  796. unsigned int cyl;
  797. u8 head;
  798. u8 sect;
  799. };
  800. static unsigned int ide_disks;
  801. static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
  802. static int ide_set_disk_chs(const char *str, struct kernel_param *kp)
  803. {
  804. int a, b, c = 0, h = 0, s = 0, i, j = 1;
  805. if (sscanf(str, "%d.%d:%d,%d,%d", &a, &b, &c, &h, &s) != 5 &&
  806. sscanf(str, "%d.%d:%d", &a, &b, &j) != 3)
  807. return -EINVAL;
  808. i = a * MAX_DRIVES + b;
  809. if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
  810. return -EINVAL;
  811. if (c > INT_MAX || h > 255 || s > 255)
  812. return -EINVAL;
  813. if (j)
  814. ide_disks |= (1 << i);
  815. else
  816. ide_disks &= (1 << i);
  817. ide_disks_chs[i].cyl = c;
  818. ide_disks_chs[i].head = h;
  819. ide_disks_chs[i].sect = s;
  820. return 0;
  821. }
  822. module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
  823. MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
  824. static void ide_dev_apply_params(ide_drive_t *drive)
  825. {
  826. int i = drive->hwif->index * MAX_DRIVES + drive->select.b.unit;
  827. if (ide_nodma & (1 << i)) {
  828. printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
  829. drive->nodma = 1;
  830. }
  831. if (ide_noflush & (1 << i)) {
  832. printk(KERN_INFO "ide: disabling flush requests for %s\n",
  833. drive->name);
  834. drive->noflush = 1;
  835. }
  836. if (ide_noprobe & (1 << i)) {
  837. printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
  838. drive->noprobe = 1;
  839. }
  840. if (ide_nowerr & (1 << i)) {
  841. printk(KERN_INFO "ide: ignoring the WRERR_STAT bit for %s\n",
  842. drive->name);
  843. drive->bad_wstat = BAD_R_STAT;
  844. }
  845. if (ide_cdroms & (1 << i)) {
  846. printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
  847. drive->present = 1;
  848. drive->media = ide_cdrom;
  849. /* an ATAPI device ignores DRDY */
  850. drive->ready_stat = 0;
  851. }
  852. if (ide_disks & (1 << i)) {
  853. drive->cyl = drive->bios_cyl = ide_disks_chs[i].cyl;
  854. drive->head = drive->bios_head = ide_disks_chs[i].head;
  855. drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
  856. drive->forced_geom = 1;
  857. printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
  858. drive->name,
  859. drive->cyl, drive->head, drive->sect);
  860. drive->present = 1;
  861. drive->media = ide_disk;
  862. drive->ready_stat = READY_STAT;
  863. }
  864. }
  865. static unsigned int ide_ignore_cable;
  866. static int ide_set_ignore_cable(const char *s, struct kernel_param *kp)
  867. {
  868. int i, j = 1;
  869. if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
  870. return -EINVAL;
  871. if (i >= MAX_HWIFS || j < 0 || j > 1)
  872. return -EINVAL;
  873. if (j)
  874. ide_ignore_cable |= (1 << i);
  875. else
  876. ide_ignore_cable &= (1 << i);
  877. return 0;
  878. }
  879. module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
  880. MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
  881. void ide_port_apply_params(ide_hwif_t *hwif)
  882. {
  883. int i;
  884. if (ide_ignore_cable & (1 << hwif->index)) {
  885. printk(KERN_INFO "ide: ignoring cable detection for %s\n",
  886. hwif->name);
  887. hwif->cbl = ATA_CBL_PATA40_SHORT;
  888. }
  889. for (i = 0; i < MAX_DRIVES; i++)
  890. ide_dev_apply_params(&hwif->drives[i]);
  891. }
  892. /*
  893. * This is gets invoked once during initialization, to set *everything* up
  894. */
  895. static int __init ide_init(void)
  896. {
  897. int ret;
  898. printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
  899. ret = bus_register(&ide_bus_type);
  900. if (ret < 0) {
  901. printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
  902. return ret;
  903. }
  904. ide_port_class = class_create(THIS_MODULE, "ide_port");
  905. if (IS_ERR(ide_port_class)) {
  906. ret = PTR_ERR(ide_port_class);
  907. goto out_port_class;
  908. }
  909. init_ide_data();
  910. proc_ide_create();
  911. return 0;
  912. out_port_class:
  913. bus_unregister(&ide_bus_type);
  914. return ret;
  915. }
  916. #ifdef MODULE
  917. static char *options = NULL;
  918. module_param(options, charp, 0);
  919. MODULE_LICENSE("GPL");
  920. static void __init parse_options (char *line)
  921. {
  922. char *next = line;
  923. if (line == NULL || !*line)
  924. return;
  925. while ((line = next) != NULL) {
  926. if ((next = strchr(line,' ')) != NULL)
  927. *next++ = 0;
  928. if (!ide_setup(line))
  929. printk (KERN_INFO "Unknown option '%s'\n", line);
  930. }
  931. }
  932. int __init init_module (void)
  933. {
  934. parse_options(options);
  935. return ide_init();
  936. }
  937. void __exit cleanup_module (void)
  938. {
  939. proc_ide_destroy();
  940. class_destroy(ide_port_class);
  941. bus_unregister(&ide_bus_type);
  942. }
  943. #else /* !MODULE */
  944. __setup("", ide_setup);
  945. module_init(ide_init);
  946. #endif /* MODULE */