ide.c 25 KB

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