ide.c 24 KB

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