ide.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. /**
  214. * ide_spin_wait_hwgroup - wait for group
  215. * @drive: drive in the group
  216. *
  217. * Wait for an IDE device group to go non busy and then return
  218. * holding the ide_lock which guards the hwgroup->busy status
  219. * and right to use it.
  220. */
  221. int ide_spin_wait_hwgroup (ide_drive_t *drive)
  222. {
  223. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  224. unsigned long timeout = jiffies + (3 * HZ);
  225. spin_lock_irq(&ide_lock);
  226. while (hwgroup->busy) {
  227. unsigned long lflags;
  228. spin_unlock_irq(&ide_lock);
  229. local_irq_set(lflags);
  230. if (time_after(jiffies, timeout)) {
  231. local_irq_restore(lflags);
  232. printk(KERN_ERR "%s: channel busy\n", drive->name);
  233. return -EBUSY;
  234. }
  235. local_irq_restore(lflags);
  236. spin_lock_irq(&ide_lock);
  237. }
  238. return 0;
  239. }
  240. EXPORT_SYMBOL(ide_spin_wait_hwgroup);
  241. ide_devset_get(io_32bit, io_32bit);
  242. int set_io_32bit(ide_drive_t *drive, int arg)
  243. {
  244. if (drive->no_io_32bit)
  245. return -EPERM;
  246. if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
  247. return -EINVAL;
  248. if (ide_spin_wait_hwgroup(drive))
  249. return -EBUSY;
  250. drive->io_32bit = arg;
  251. spin_unlock_irq(&ide_lock);
  252. return 0;
  253. }
  254. ide_devset_get(ksettings, keep_settings);
  255. 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. ide_devset_get(using_dma, using_dma);
  266. int set_using_dma(ide_drive_t *drive, int arg)
  267. {
  268. #ifdef CONFIG_BLK_DEV_IDEDMA
  269. ide_hwif_t *hwif = drive->hwif;
  270. int err = -EPERM;
  271. if (arg < 0 || arg > 1)
  272. return -EINVAL;
  273. if (ata_id_has_dma(drive->id) == 0)
  274. goto out;
  275. if (hwif->dma_ops == NULL)
  276. goto out;
  277. err = -EBUSY;
  278. if (ide_spin_wait_hwgroup(drive))
  279. goto out;
  280. /*
  281. * set ->busy flag, unlock and let it ride
  282. */
  283. hwif->hwgroup->busy = 1;
  284. spin_unlock_irq(&ide_lock);
  285. err = 0;
  286. if (arg) {
  287. if (ide_set_dma(drive))
  288. err = -EIO;
  289. } else
  290. ide_dma_off(drive);
  291. /*
  292. * lock, clear ->busy flag and unlock before leaving
  293. */
  294. spin_lock_irq(&ide_lock);
  295. hwif->hwgroup->busy = 0;
  296. spin_unlock_irq(&ide_lock);
  297. out:
  298. return err;
  299. #else
  300. if (arg < 0 || arg > 1)
  301. return -EINVAL;
  302. return -EPERM;
  303. #endif
  304. }
  305. int set_pio_mode(ide_drive_t *drive, int arg)
  306. {
  307. struct request *rq;
  308. ide_hwif_t *hwif = drive->hwif;
  309. const struct ide_port_ops *port_ops = hwif->port_ops;
  310. if (arg < 0 || arg > 255)
  311. return -EINVAL;
  312. if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
  313. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  314. return -ENOSYS;
  315. if (drive->special.b.set_tune)
  316. return -EBUSY;
  317. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  318. rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
  319. drive->tune_req = (u8) arg;
  320. drive->special.b.set_tune = 1;
  321. blk_execute_rq(drive->queue, NULL, rq, 0);
  322. blk_put_request(rq);
  323. return 0;
  324. }
  325. ide_devset_get(unmaskirq, unmask);
  326. int set_unmaskirq(ide_drive_t *drive, int arg)
  327. {
  328. if (drive->no_unmask)
  329. return -EPERM;
  330. if (arg < 0 || arg > 1)
  331. return -EINVAL;
  332. if (ide_spin_wait_hwgroup(drive))
  333. return -EBUSY;
  334. drive->unmask = arg;
  335. spin_unlock_irq(&ide_lock);
  336. return 0;
  337. }
  338. static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
  339. {
  340. ide_drive_t *drive = dev->driver_data;
  341. ide_hwif_t *hwif = HWIF(drive);
  342. struct request *rq;
  343. struct request_pm_state rqpm;
  344. ide_task_t args;
  345. int ret;
  346. /* Call ACPI _GTM only once */
  347. if (!(drive->dn % 2))
  348. ide_acpi_get_timing(hwif);
  349. memset(&rqpm, 0, sizeof(rqpm));
  350. memset(&args, 0, sizeof(args));
  351. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  352. rq->cmd_type = REQ_TYPE_PM_SUSPEND;
  353. rq->special = &args;
  354. rq->data = &rqpm;
  355. rqpm.pm_step = ide_pm_state_start_suspend;
  356. if (mesg.event == PM_EVENT_PRETHAW)
  357. mesg.event = PM_EVENT_FREEZE;
  358. rqpm.pm_state = mesg.event;
  359. ret = blk_execute_rq(drive->queue, NULL, rq, 0);
  360. blk_put_request(rq);
  361. /* only call ACPI _PS3 after both drivers are suspended */
  362. if (!ret && (((drive->dn % 2) && hwif->drives[0].present
  363. && hwif->drives[1].present)
  364. || !hwif->drives[0].present
  365. || !hwif->drives[1].present))
  366. ide_acpi_set_state(hwif, 0);
  367. return ret;
  368. }
  369. static int generic_ide_resume(struct device *dev)
  370. {
  371. ide_drive_t *drive = dev->driver_data;
  372. ide_hwif_t *hwif = HWIF(drive);
  373. struct request *rq;
  374. struct request_pm_state rqpm;
  375. ide_task_t args;
  376. int err;
  377. /* Call ACPI _STM only once */
  378. if (!(drive->dn % 2)) {
  379. ide_acpi_set_state(hwif, 1);
  380. ide_acpi_push_timing(hwif);
  381. }
  382. ide_acpi_exec_tfs(drive);
  383. memset(&rqpm, 0, sizeof(rqpm));
  384. memset(&args, 0, sizeof(args));
  385. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  386. rq->cmd_type = REQ_TYPE_PM_RESUME;
  387. rq->cmd_flags |= REQ_PREEMPT;
  388. rq->special = &args;
  389. rq->data = &rqpm;
  390. rqpm.pm_step = ide_pm_state_start_resume;
  391. rqpm.pm_state = PM_EVENT_ON;
  392. err = blk_execute_rq(drive->queue, NULL, rq, 1);
  393. blk_put_request(rq);
  394. if (err == 0 && dev->driver) {
  395. ide_driver_t *drv = to_ide_driver(dev->driver);
  396. if (drv->resume)
  397. drv->resume(drive);
  398. }
  399. return err;
  400. }
  401. static int generic_drive_reset(ide_drive_t *drive)
  402. {
  403. struct request *rq;
  404. int ret = 0;
  405. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  406. rq->cmd_type = REQ_TYPE_SPECIAL;
  407. rq->cmd_len = 1;
  408. rq->cmd[0] = REQ_DRIVE_RESET;
  409. rq->cmd_flags |= REQ_SOFTBARRIER;
  410. if (blk_execute_rq(drive->queue, NULL, rq, 1))
  411. ret = rq->errors;
  412. blk_put_request(rq);
  413. return ret;
  414. }
  415. static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
  416. unsigned long arg)
  417. {
  418. u16 *id = NULL;
  419. int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
  420. int rc = 0;
  421. if (drive->id_read == 0) {
  422. rc = -ENOMSG;
  423. goto out;
  424. }
  425. id = kmalloc(size, GFP_KERNEL);
  426. if (id == NULL) {
  427. rc = -ENOMEM;
  428. goto out;
  429. }
  430. memcpy(id, drive->id, size);
  431. ata_id_to_hd_driveid(id);
  432. if (copy_to_user((void __user *)arg, id, size))
  433. rc = -EFAULT;
  434. kfree(id);
  435. out:
  436. return rc;
  437. }
  438. static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
  439. {
  440. return put_user((drive->dsc_overlap << IDE_NICE_DSC_OVERLAP) |
  441. (drive->nice1 << IDE_NICE_1), (long __user *)arg);
  442. }
  443. static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
  444. {
  445. if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
  446. return -EPERM;
  447. if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
  448. (drive->media == ide_disk || drive->media == ide_floppy ||
  449. drive->scsi))
  450. return -EPERM;
  451. drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
  452. drive->nice1 = (arg >> IDE_NICE_1) & 1;
  453. return 0;
  454. }
  455. static const struct ide_ioctl_devset ide_ioctl_settings[] = {
  456. { HDIO_GET_32BIT, HDIO_SET_32BIT, get_io_32bit, set_io_32bit },
  457. { HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, get_ksettings, set_ksettings },
  458. { HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, get_unmaskirq, set_unmaskirq },
  459. { HDIO_GET_DMA, HDIO_SET_DMA, get_using_dma, set_using_dma },
  460. { -1, HDIO_SET_PIO_MODE, NULL, set_pio_mode },
  461. { 0 }
  462. };
  463. int generic_ide_ioctl(ide_drive_t *drive, struct file *file,
  464. struct block_device *bdev,
  465. unsigned int cmd, unsigned long arg)
  466. {
  467. int err;
  468. err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
  469. if (err != -EOPNOTSUPP)
  470. return err;
  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 ide_get_nice_ioctl(drive, arg);
  479. case HDIO_SET_NICE:
  480. if (!capable(CAP_SYS_ADMIN))
  481. return -EACCES;
  482. return ide_set_nice_ioctl(drive, arg);
  483. #ifdef CONFIG_IDE_TASK_IOCTL
  484. case HDIO_DRIVE_TASKFILE:
  485. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  486. return -EACCES;
  487. if (drive->media == ide_disk)
  488. return ide_taskfile_ioctl(drive, cmd, arg);
  489. return -ENOMSG;
  490. #endif
  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_DRIVE_RESET:
  500. if (!capable(CAP_SYS_ADMIN))
  501. return -EACCES;
  502. return generic_drive_reset(drive);
  503. case HDIO_GET_BUSSTATE:
  504. if (!capable(CAP_SYS_ADMIN))
  505. return -EACCES;
  506. if (put_user(BUSSTATE_ON, (long __user *)arg))
  507. return -EFAULT;
  508. return 0;
  509. case HDIO_SET_BUSSTATE:
  510. if (!capable(CAP_SYS_ADMIN))
  511. return -EACCES;
  512. return -EOPNOTSUPP;
  513. default:
  514. return -EINVAL;
  515. }
  516. }
  517. EXPORT_SYMBOL(generic_ide_ioctl);
  518. int ide_setting_ioctl(ide_drive_t *drive, struct block_device *bdev,
  519. unsigned int cmd, unsigned long arg,
  520. const struct ide_ioctl_devset *s)
  521. {
  522. unsigned long flags;
  523. int err = -EOPNOTSUPP;
  524. for (; s->get_ioctl; s++) {
  525. if (s->get && s->get_ioctl == cmd)
  526. goto read_val;
  527. else if (s->set && s->set_ioctl == cmd)
  528. goto set_val;
  529. }
  530. return err;
  531. read_val:
  532. mutex_lock(&ide_setting_mtx);
  533. spin_lock_irqsave(&ide_lock, flags);
  534. err = s->get(drive);
  535. spin_unlock_irqrestore(&ide_lock, flags);
  536. mutex_unlock(&ide_setting_mtx);
  537. return err >= 0 ? put_user(err, (long __user *)arg) : err;
  538. set_val:
  539. if (bdev != bdev->bd_contains)
  540. err = -EINVAL;
  541. else {
  542. if (!capable(CAP_SYS_ADMIN))
  543. err = -EACCES;
  544. else {
  545. mutex_lock(&ide_setting_mtx);
  546. err = s->set(drive, arg);
  547. mutex_unlock(&ide_setting_mtx);
  548. }
  549. }
  550. return err;
  551. }
  552. EXPORT_SYMBOL_GPL(ide_setting_ioctl);
  553. /**
  554. * ide_device_get - get an additional reference to a ide_drive_t
  555. * @drive: device to get a reference to
  556. *
  557. * Gets a reference to the ide_drive_t and increments the use count of the
  558. * underlying LLDD module.
  559. */
  560. int ide_device_get(ide_drive_t *drive)
  561. {
  562. struct device *host_dev;
  563. struct module *module;
  564. if (!get_device(&drive->gendev))
  565. return -ENXIO;
  566. host_dev = drive->hwif->host->dev[0];
  567. module = host_dev ? host_dev->driver->owner : NULL;
  568. if (module && !try_module_get(module)) {
  569. put_device(&drive->gendev);
  570. return -ENXIO;
  571. }
  572. return 0;
  573. }
  574. EXPORT_SYMBOL_GPL(ide_device_get);
  575. /**
  576. * ide_device_put - release a reference to a ide_drive_t
  577. * @drive: device to release a reference on
  578. *
  579. * Release a reference to the ide_drive_t and decrements the use count of
  580. * the underlying LLDD module.
  581. */
  582. void ide_device_put(ide_drive_t *drive)
  583. {
  584. #ifdef CONFIG_MODULE_UNLOAD
  585. struct device *host_dev = drive->hwif->host->dev[0];
  586. struct module *module = host_dev ? host_dev->driver->owner : NULL;
  587. if (module)
  588. module_put(module);
  589. #endif
  590. put_device(&drive->gendev);
  591. }
  592. EXPORT_SYMBOL_GPL(ide_device_put);
  593. static int ide_bus_match(struct device *dev, struct device_driver *drv)
  594. {
  595. return 1;
  596. }
  597. static char *media_string(ide_drive_t *drive)
  598. {
  599. switch (drive->media) {
  600. case ide_disk:
  601. return "disk";
  602. case ide_cdrom:
  603. return "cdrom";
  604. case ide_tape:
  605. return "tape";
  606. case ide_floppy:
  607. return "floppy";
  608. case ide_optical:
  609. return "optical";
  610. default:
  611. return "UNKNOWN";
  612. }
  613. }
  614. static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
  615. {
  616. ide_drive_t *drive = to_ide_device(dev);
  617. return sprintf(buf, "%s\n", media_string(drive));
  618. }
  619. static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
  620. {
  621. ide_drive_t *drive = to_ide_device(dev);
  622. return sprintf(buf, "%s\n", drive->name);
  623. }
  624. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  625. {
  626. ide_drive_t *drive = to_ide_device(dev);
  627. return sprintf(buf, "ide:m-%s\n", media_string(drive));
  628. }
  629. static ssize_t model_show(struct device *dev, struct device_attribute *attr,
  630. char *buf)
  631. {
  632. ide_drive_t *drive = to_ide_device(dev);
  633. return sprintf(buf, "%s\n", (char *)&drive->id[ATA_ID_PROD]);
  634. }
  635. static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
  636. char *buf)
  637. {
  638. ide_drive_t *drive = to_ide_device(dev);
  639. return sprintf(buf, "%s\n", (char *)&drive->id[ATA_ID_FW_REV]);
  640. }
  641. static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
  642. char *buf)
  643. {
  644. ide_drive_t *drive = to_ide_device(dev);
  645. return sprintf(buf, "%s\n", (char *)&drive->id[ATA_ID_SERNO]);
  646. }
  647. static struct device_attribute ide_dev_attrs[] = {
  648. __ATTR_RO(media),
  649. __ATTR_RO(drivename),
  650. __ATTR_RO(modalias),
  651. __ATTR_RO(model),
  652. __ATTR_RO(firmware),
  653. __ATTR(serial, 0400, serial_show, NULL),
  654. __ATTR_NULL
  655. };
  656. static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
  657. {
  658. ide_drive_t *drive = to_ide_device(dev);
  659. add_uevent_var(env, "MEDIA=%s", media_string(drive));
  660. add_uevent_var(env, "DRIVENAME=%s", drive->name);
  661. add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
  662. return 0;
  663. }
  664. static int generic_ide_probe(struct device *dev)
  665. {
  666. ide_drive_t *drive = to_ide_device(dev);
  667. ide_driver_t *drv = to_ide_driver(dev->driver);
  668. return drv->probe ? drv->probe(drive) : -ENODEV;
  669. }
  670. static int generic_ide_remove(struct device *dev)
  671. {
  672. ide_drive_t *drive = to_ide_device(dev);
  673. ide_driver_t *drv = to_ide_driver(dev->driver);
  674. if (drv->remove)
  675. drv->remove(drive);
  676. return 0;
  677. }
  678. static void generic_ide_shutdown(struct device *dev)
  679. {
  680. ide_drive_t *drive = to_ide_device(dev);
  681. ide_driver_t *drv = to_ide_driver(dev->driver);
  682. if (dev->driver && drv->shutdown)
  683. drv->shutdown(drive);
  684. }
  685. struct bus_type ide_bus_type = {
  686. .name = "ide",
  687. .match = ide_bus_match,
  688. .uevent = ide_uevent,
  689. .probe = generic_ide_probe,
  690. .remove = generic_ide_remove,
  691. .shutdown = generic_ide_shutdown,
  692. .dev_attrs = ide_dev_attrs,
  693. .suspend = generic_ide_suspend,
  694. .resume = generic_ide_resume,
  695. };
  696. EXPORT_SYMBOL_GPL(ide_bus_type);
  697. int ide_vlb_clk;
  698. EXPORT_SYMBOL_GPL(ide_vlb_clk);
  699. module_param_named(vlb_clock, ide_vlb_clk, int, 0);
  700. MODULE_PARM_DESC(vlb_clock, "VLB clock frequency (in MHz)");
  701. int ide_pci_clk;
  702. EXPORT_SYMBOL_GPL(ide_pci_clk);
  703. module_param_named(pci_clock, ide_pci_clk, int, 0);
  704. MODULE_PARM_DESC(pci_clock, "PCI bus clock frequency (in MHz)");
  705. static int ide_set_dev_param_mask(const char *s, struct kernel_param *kp)
  706. {
  707. int a, b, i, j = 1;
  708. unsigned int *dev_param_mask = (unsigned int *)kp->arg;
  709. if (sscanf(s, "%d.%d:%d", &a, &b, &j) != 3 &&
  710. sscanf(s, "%d.%d", &a, &b) != 2)
  711. return -EINVAL;
  712. i = a * MAX_DRIVES + b;
  713. if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
  714. return -EINVAL;
  715. if (j)
  716. *dev_param_mask |= (1 << i);
  717. else
  718. *dev_param_mask &= (1 << i);
  719. return 0;
  720. }
  721. static unsigned int ide_nodma;
  722. module_param_call(nodma, ide_set_dev_param_mask, NULL, &ide_nodma, 0);
  723. MODULE_PARM_DESC(nodma, "disallow DMA for a device");
  724. static unsigned int ide_noflush;
  725. module_param_call(noflush, ide_set_dev_param_mask, NULL, &ide_noflush, 0);
  726. MODULE_PARM_DESC(noflush, "disable flush requests for a device");
  727. static unsigned int ide_noprobe;
  728. module_param_call(noprobe, ide_set_dev_param_mask, NULL, &ide_noprobe, 0);
  729. MODULE_PARM_DESC(noprobe, "skip probing for a device");
  730. static unsigned int ide_nowerr;
  731. module_param_call(nowerr, ide_set_dev_param_mask, NULL, &ide_nowerr, 0);
  732. MODULE_PARM_DESC(nowerr, "ignore the ATA_DF bit for a device");
  733. static unsigned int ide_cdroms;
  734. module_param_call(cdrom, ide_set_dev_param_mask, NULL, &ide_cdroms, 0);
  735. MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
  736. struct chs_geom {
  737. unsigned int cyl;
  738. u8 head;
  739. u8 sect;
  740. };
  741. static unsigned int ide_disks;
  742. static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
  743. static int ide_set_disk_chs(const char *str, struct kernel_param *kp)
  744. {
  745. int a, b, c = 0, h = 0, s = 0, i, j = 1;
  746. if (sscanf(str, "%d.%d:%d,%d,%d", &a, &b, &c, &h, &s) != 5 &&
  747. sscanf(str, "%d.%d:%d", &a, &b, &j) != 3)
  748. return -EINVAL;
  749. i = a * MAX_DRIVES + b;
  750. if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
  751. return -EINVAL;
  752. if (c > INT_MAX || h > 255 || s > 255)
  753. return -EINVAL;
  754. if (j)
  755. ide_disks |= (1 << i);
  756. else
  757. ide_disks &= (1 << i);
  758. ide_disks_chs[i].cyl = c;
  759. ide_disks_chs[i].head = h;
  760. ide_disks_chs[i].sect = s;
  761. return 0;
  762. }
  763. module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
  764. MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
  765. static void ide_dev_apply_params(ide_drive_t *drive)
  766. {
  767. int i = drive->hwif->index * MAX_DRIVES + drive->select.b.unit;
  768. if (ide_nodma & (1 << i)) {
  769. printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
  770. drive->nodma = 1;
  771. }
  772. if (ide_noflush & (1 << i)) {
  773. printk(KERN_INFO "ide: disabling flush requests for %s\n",
  774. drive->name);
  775. drive->noflush = 1;
  776. }
  777. if (ide_noprobe & (1 << i)) {
  778. printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
  779. drive->noprobe = 1;
  780. }
  781. if (ide_nowerr & (1 << i)) {
  782. printk(KERN_INFO "ide: ignoring the ATA_DF bit for %s\n",
  783. drive->name);
  784. drive->bad_wstat = BAD_R_STAT;
  785. }
  786. if (ide_cdroms & (1 << i)) {
  787. printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
  788. drive->present = 1;
  789. drive->media = ide_cdrom;
  790. /* an ATAPI device ignores DRDY */
  791. drive->ready_stat = 0;
  792. }
  793. if (ide_disks & (1 << i)) {
  794. drive->cyl = drive->bios_cyl = ide_disks_chs[i].cyl;
  795. drive->head = drive->bios_head = ide_disks_chs[i].head;
  796. drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
  797. drive->forced_geom = 1;
  798. printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
  799. drive->name,
  800. drive->cyl, drive->head, drive->sect);
  801. drive->present = 1;
  802. drive->media = ide_disk;
  803. drive->ready_stat = ATA_DRDY;
  804. }
  805. }
  806. static unsigned int ide_ignore_cable;
  807. static int ide_set_ignore_cable(const char *s, struct kernel_param *kp)
  808. {
  809. int i, j = 1;
  810. if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
  811. return -EINVAL;
  812. if (i >= MAX_HWIFS || j < 0 || j > 1)
  813. return -EINVAL;
  814. if (j)
  815. ide_ignore_cable |= (1 << i);
  816. else
  817. ide_ignore_cable &= (1 << i);
  818. return 0;
  819. }
  820. module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
  821. MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
  822. void ide_port_apply_params(ide_hwif_t *hwif)
  823. {
  824. int i;
  825. if (ide_ignore_cable & (1 << hwif->index)) {
  826. printk(KERN_INFO "ide: ignoring cable detection for %s\n",
  827. hwif->name);
  828. hwif->cbl = ATA_CBL_PATA40_SHORT;
  829. }
  830. for (i = 0; i < MAX_DRIVES; i++)
  831. ide_dev_apply_params(&hwif->drives[i]);
  832. }
  833. /*
  834. * This is gets invoked once during initialization, to set *everything* up
  835. */
  836. static int __init ide_init(void)
  837. {
  838. int ret;
  839. printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
  840. ret = bus_register(&ide_bus_type);
  841. if (ret < 0) {
  842. printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
  843. return ret;
  844. }
  845. ide_port_class = class_create(THIS_MODULE, "ide_port");
  846. if (IS_ERR(ide_port_class)) {
  847. ret = PTR_ERR(ide_port_class);
  848. goto out_port_class;
  849. }
  850. proc_ide_create();
  851. return 0;
  852. out_port_class:
  853. bus_unregister(&ide_bus_type);
  854. return ret;
  855. }
  856. static void __exit ide_exit(void)
  857. {
  858. proc_ide_destroy();
  859. class_destroy(ide_port_class);
  860. bus_unregister(&ide_bus_type);
  861. }
  862. module_init(ide_init);
  863. module_exit(ide_exit);
  864. MODULE_LICENSE("GPL");