ide.c 27 KB

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