ide.c 25 KB

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