ide.c 25 KB

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