ide.c 25 KB

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