ide.c 25 KB

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