ide.c 23 KB

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