ide.c 24 KB

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