ide.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
  114. {
  115. ide_hwgroup_t *hwgroup = hwif->hwgroup;
  116. spin_lock_irq(&ide_lock);
  117. /*
  118. * Remove us from the hwgroup, and free
  119. * the hwgroup if we were the only member
  120. */
  121. if (hwif->next == hwif) {
  122. BUG_ON(hwgroup->hwif != hwif);
  123. kfree(hwgroup);
  124. } else {
  125. /* There is another interface in hwgroup.
  126. * Unlink us, and set hwgroup->drive and ->hwif to
  127. * something sane.
  128. */
  129. ide_hwif_t *g = hwgroup->hwif;
  130. while (g->next != hwif)
  131. g = g->next;
  132. g->next = hwif->next;
  133. if (hwgroup->hwif == hwif) {
  134. /* Chose a random hwif for hwgroup->hwif.
  135. * It's guaranteed that there are no drives
  136. * left in the hwgroup.
  137. */
  138. BUG_ON(hwgroup->drive != NULL);
  139. hwgroup->hwif = g;
  140. }
  141. BUG_ON(hwgroup->hwif == hwif);
  142. }
  143. spin_unlock_irq(&ide_lock);
  144. }
  145. /* Called with ide_lock held. */
  146. static void __ide_port_unregister_devices(ide_hwif_t *hwif)
  147. {
  148. int i;
  149. for (i = 0; i < MAX_DRIVES; i++) {
  150. ide_drive_t *drive = &hwif->drives[i];
  151. if (drive->present) {
  152. spin_unlock_irq(&ide_lock);
  153. device_unregister(&drive->gendev);
  154. wait_for_completion(&drive->gendev_rel_comp);
  155. spin_lock_irq(&ide_lock);
  156. }
  157. }
  158. }
  159. void ide_port_unregister_devices(ide_hwif_t *hwif)
  160. {
  161. mutex_lock(&ide_cfg_mtx);
  162. spin_lock_irq(&ide_lock);
  163. __ide_port_unregister_devices(hwif);
  164. hwif->present = 0;
  165. ide_port_init_devices_data(hwif);
  166. spin_unlock_irq(&ide_lock);
  167. mutex_unlock(&ide_cfg_mtx);
  168. }
  169. EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
  170. /**
  171. * ide_unregister - free an IDE interface
  172. * @hwif: IDE interface
  173. *
  174. * Perform the final unregister of an IDE interface. At the moment
  175. * we don't refcount interfaces so this will also get split up.
  176. *
  177. * Locking:
  178. * The caller must not hold the IDE locks
  179. * The drive present/vanishing is not yet properly locked
  180. * Take care with the callbacks. These have been split to avoid
  181. * deadlocking the IDE layer. The shutdown callback is called
  182. * before we take the lock and free resources. It is up to the
  183. * caller to be sure there is no pending I/O here, and that
  184. * the interface will not be reopened (present/vanishing locking
  185. * isn't yet done BTW). After we commit to the final kill we
  186. * call the cleanup callback with the ide locks held.
  187. *
  188. * Unregister restores the hwif structures to the default state.
  189. * This is raving bonkers.
  190. */
  191. void ide_unregister(ide_hwif_t *hwif)
  192. {
  193. ide_hwif_t *g;
  194. ide_hwgroup_t *hwgroup;
  195. int irq_count = 0;
  196. BUG_ON(in_interrupt());
  197. BUG_ON(irqs_disabled());
  198. mutex_lock(&ide_cfg_mtx);
  199. spin_lock_irq(&ide_lock);
  200. if (hwif->present) {
  201. __ide_port_unregister_devices(hwif);
  202. hwif->present = 0;
  203. }
  204. spin_unlock_irq(&ide_lock);
  205. ide_proc_unregister_port(hwif);
  206. hwgroup = hwif->hwgroup;
  207. /*
  208. * free the irq if we were the only hwif using it
  209. */
  210. g = hwgroup->hwif;
  211. do {
  212. if (g->irq == hwif->irq)
  213. ++irq_count;
  214. g = g->next;
  215. } while (g != hwgroup->hwif);
  216. if (irq_count == 1)
  217. free_irq(hwif->irq, hwgroup);
  218. ide_remove_port_from_hwgroup(hwif);
  219. device_unregister(hwif->portdev);
  220. device_unregister(&hwif->gendev);
  221. wait_for_completion(&hwif->gendev_rel_comp);
  222. /*
  223. * Remove us from the kernel's knowledge
  224. */
  225. blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
  226. kfree(hwif->sg_table);
  227. unregister_blkdev(hwif->major, hwif->name);
  228. if (hwif->dma_base)
  229. ide_release_dma_engine(hwif);
  230. spin_lock_irq(&ide_lock);
  231. /* restore hwif data to pristine status */
  232. ide_init_port_data(hwif, hwif->index);
  233. spin_unlock_irq(&ide_lock);
  234. mutex_unlock(&ide_cfg_mtx);
  235. }
  236. void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
  237. {
  238. memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
  239. hwif->irq = hw->irq;
  240. hwif->chipset = hw->chipset;
  241. hwif->dev = hw->dev;
  242. hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
  243. hwif->ack_intr = hw->ack_intr;
  244. hwif->config_data = hw->config;
  245. }
  246. /*
  247. * Locks for IDE setting functionality
  248. */
  249. DEFINE_MUTEX(ide_setting_mtx);
  250. EXPORT_SYMBOL_GPL(ide_setting_mtx);
  251. /**
  252. * ide_spin_wait_hwgroup - wait for group
  253. * @drive: drive in the group
  254. *
  255. * Wait for an IDE device group to go non busy and then return
  256. * holding the ide_lock which guards the hwgroup->busy status
  257. * and right to use it.
  258. */
  259. int ide_spin_wait_hwgroup (ide_drive_t *drive)
  260. {
  261. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  262. unsigned long timeout = jiffies + (3 * HZ);
  263. spin_lock_irq(&ide_lock);
  264. while (hwgroup->busy) {
  265. unsigned long lflags;
  266. spin_unlock_irq(&ide_lock);
  267. local_irq_set(lflags);
  268. if (time_after(jiffies, timeout)) {
  269. local_irq_restore(lflags);
  270. printk(KERN_ERR "%s: channel busy\n", drive->name);
  271. return -EBUSY;
  272. }
  273. local_irq_restore(lflags);
  274. spin_lock_irq(&ide_lock);
  275. }
  276. return 0;
  277. }
  278. EXPORT_SYMBOL(ide_spin_wait_hwgroup);
  279. int set_io_32bit(ide_drive_t *drive, int arg)
  280. {
  281. if (drive->no_io_32bit)
  282. return -EPERM;
  283. if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
  284. return -EINVAL;
  285. if (ide_spin_wait_hwgroup(drive))
  286. return -EBUSY;
  287. drive->io_32bit = arg;
  288. spin_unlock_irq(&ide_lock);
  289. return 0;
  290. }
  291. static int set_ksettings(ide_drive_t *drive, int arg)
  292. {
  293. if (arg < 0 || arg > 1)
  294. return -EINVAL;
  295. if (ide_spin_wait_hwgroup(drive))
  296. return -EBUSY;
  297. drive->keep_settings = arg;
  298. spin_unlock_irq(&ide_lock);
  299. return 0;
  300. }
  301. int set_using_dma(ide_drive_t *drive, int arg)
  302. {
  303. #ifdef CONFIG_BLK_DEV_IDEDMA
  304. ide_hwif_t *hwif = drive->hwif;
  305. int err = -EPERM;
  306. if (arg < 0 || arg > 1)
  307. return -EINVAL;
  308. if (!drive->id || !(drive->id->capability & 1))
  309. goto out;
  310. if (hwif->dma_ops == NULL)
  311. goto out;
  312. err = -EBUSY;
  313. if (ide_spin_wait_hwgroup(drive))
  314. goto out;
  315. /*
  316. * set ->busy flag, unlock and let it ride
  317. */
  318. hwif->hwgroup->busy = 1;
  319. spin_unlock_irq(&ide_lock);
  320. err = 0;
  321. if (arg) {
  322. if (ide_set_dma(drive))
  323. err = -EIO;
  324. } else
  325. ide_dma_off(drive);
  326. /*
  327. * lock, clear ->busy flag and unlock before leaving
  328. */
  329. spin_lock_irq(&ide_lock);
  330. hwif->hwgroup->busy = 0;
  331. spin_unlock_irq(&ide_lock);
  332. out:
  333. return err;
  334. #else
  335. if (arg < 0 || arg > 1)
  336. return -EINVAL;
  337. return -EPERM;
  338. #endif
  339. }
  340. int set_pio_mode(ide_drive_t *drive, int arg)
  341. {
  342. struct request *rq;
  343. ide_hwif_t *hwif = drive->hwif;
  344. const struct ide_port_ops *port_ops = hwif->port_ops;
  345. if (arg < 0 || arg > 255)
  346. return -EINVAL;
  347. if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
  348. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  349. return -ENOSYS;
  350. if (drive->special.b.set_tune)
  351. return -EBUSY;
  352. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  353. rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
  354. drive->tune_req = (u8) arg;
  355. drive->special.b.set_tune = 1;
  356. blk_execute_rq(drive->queue, NULL, rq, 0);
  357. blk_put_request(rq);
  358. return 0;
  359. }
  360. static int set_unmaskirq(ide_drive_t *drive, int arg)
  361. {
  362. if (drive->no_unmask)
  363. return -EPERM;
  364. if (arg < 0 || arg > 1)
  365. return -EINVAL;
  366. if (ide_spin_wait_hwgroup(drive))
  367. return -EBUSY;
  368. drive->unmask = arg;
  369. spin_unlock_irq(&ide_lock);
  370. return 0;
  371. }
  372. static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
  373. {
  374. ide_drive_t *drive = dev->driver_data;
  375. ide_hwif_t *hwif = HWIF(drive);
  376. struct request *rq;
  377. struct request_pm_state rqpm;
  378. ide_task_t args;
  379. int ret;
  380. /* Call ACPI _GTM only once */
  381. if (!(drive->dn % 2))
  382. ide_acpi_get_timing(hwif);
  383. memset(&rqpm, 0, sizeof(rqpm));
  384. memset(&args, 0, sizeof(args));
  385. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  386. rq->cmd_type = REQ_TYPE_PM_SUSPEND;
  387. rq->special = &args;
  388. rq->data = &rqpm;
  389. rqpm.pm_step = ide_pm_state_start_suspend;
  390. if (mesg.event == PM_EVENT_PRETHAW)
  391. mesg.event = PM_EVENT_FREEZE;
  392. rqpm.pm_state = mesg.event;
  393. ret = blk_execute_rq(drive->queue, NULL, rq, 0);
  394. blk_put_request(rq);
  395. /* only call ACPI _PS3 after both drivers are suspended */
  396. if (!ret && (((drive->dn % 2) && hwif->drives[0].present
  397. && hwif->drives[1].present)
  398. || !hwif->drives[0].present
  399. || !hwif->drives[1].present))
  400. ide_acpi_set_state(hwif, 0);
  401. return ret;
  402. }
  403. static int generic_ide_resume(struct device *dev)
  404. {
  405. ide_drive_t *drive = dev->driver_data;
  406. ide_hwif_t *hwif = HWIF(drive);
  407. struct request *rq;
  408. struct request_pm_state rqpm;
  409. ide_task_t args;
  410. int err;
  411. /* Call ACPI _STM only once */
  412. if (!(drive->dn % 2)) {
  413. ide_acpi_set_state(hwif, 1);
  414. ide_acpi_push_timing(hwif);
  415. }
  416. ide_acpi_exec_tfs(drive);
  417. memset(&rqpm, 0, sizeof(rqpm));
  418. memset(&args, 0, sizeof(args));
  419. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  420. rq->cmd_type = REQ_TYPE_PM_RESUME;
  421. rq->cmd_flags |= REQ_PREEMPT;
  422. rq->special = &args;
  423. rq->data = &rqpm;
  424. rqpm.pm_step = ide_pm_state_start_resume;
  425. rqpm.pm_state = PM_EVENT_ON;
  426. err = blk_execute_rq(drive->queue, NULL, rq, 1);
  427. blk_put_request(rq);
  428. if (err == 0 && dev->driver) {
  429. ide_driver_t *drv = to_ide_driver(dev->driver);
  430. if (drv->resume)
  431. drv->resume(drive);
  432. }
  433. return err;
  434. }
  435. static int generic_drive_reset(ide_drive_t *drive)
  436. {
  437. struct request *rq;
  438. int ret = 0;
  439. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  440. rq->cmd_type = REQ_TYPE_SPECIAL;
  441. rq->cmd_len = 1;
  442. rq->cmd[0] = REQ_DRIVE_RESET;
  443. rq->cmd_flags |= REQ_SOFTBARRIER;
  444. if (blk_execute_rq(drive->queue, NULL, rq, 1))
  445. ret = rq->errors;
  446. blk_put_request(rq);
  447. return ret;
  448. }
  449. int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
  450. unsigned int cmd, unsigned long arg)
  451. {
  452. unsigned long flags;
  453. ide_driver_t *drv;
  454. void __user *p = (void __user *)arg;
  455. int err = 0, (*setfunc)(ide_drive_t *, int);
  456. u8 *val;
  457. switch (cmd) {
  458. case HDIO_GET_32BIT: val = &drive->io_32bit; goto read_val;
  459. case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val;
  460. case HDIO_GET_UNMASKINTR: val = &drive->unmask; goto read_val;
  461. case HDIO_GET_DMA: val = &drive->using_dma; goto read_val;
  462. case HDIO_SET_32BIT: setfunc = set_io_32bit; goto set_val;
  463. case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings; goto set_val;
  464. case HDIO_SET_PIO_MODE: setfunc = set_pio_mode; goto set_val;
  465. case HDIO_SET_UNMASKINTR: setfunc = set_unmaskirq; goto set_val;
  466. case HDIO_SET_DMA: setfunc = set_using_dma; goto set_val;
  467. }
  468. switch (cmd) {
  469. case HDIO_OBSOLETE_IDENTITY:
  470. case HDIO_GET_IDENTITY:
  471. if (bdev != bdev->bd_contains)
  472. return -EINVAL;
  473. if (drive->id_read == 0)
  474. return -ENOMSG;
  475. if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
  476. return -EFAULT;
  477. return 0;
  478. case HDIO_GET_NICE:
  479. return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
  480. drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
  481. drive->nice1 << IDE_NICE_1,
  482. (long __user *) arg);
  483. #ifdef CONFIG_IDE_TASK_IOCTL
  484. case HDIO_DRIVE_TASKFILE:
  485. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  486. return -EACCES;
  487. switch(drive->media) {
  488. case ide_disk:
  489. return ide_taskfile_ioctl(drive, cmd, arg);
  490. default:
  491. return -ENOMSG;
  492. }
  493. #endif /* CONFIG_IDE_TASK_IOCTL */
  494. case HDIO_DRIVE_CMD:
  495. if (!capable(CAP_SYS_RAWIO))
  496. return -EACCES;
  497. return ide_cmd_ioctl(drive, cmd, arg);
  498. case HDIO_DRIVE_TASK:
  499. if (!capable(CAP_SYS_RAWIO))
  500. return -EACCES;
  501. return ide_task_ioctl(drive, cmd, arg);
  502. case HDIO_SET_NICE:
  503. if (!capable(CAP_SYS_ADMIN)) return -EACCES;
  504. if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
  505. return -EPERM;
  506. drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
  507. drv = *(ide_driver_t **)bdev->bd_disk->private_data;
  508. if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
  509. drive->dsc_overlap = 0;
  510. return -EPERM;
  511. }
  512. drive->nice1 = (arg >> IDE_NICE_1) & 1;
  513. return 0;
  514. case HDIO_DRIVE_RESET:
  515. if (!capable(CAP_SYS_ADMIN))
  516. return -EACCES;
  517. return generic_drive_reset(drive);
  518. case HDIO_GET_BUSSTATE:
  519. if (!capable(CAP_SYS_ADMIN))
  520. return -EACCES;
  521. if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
  522. return -EFAULT;
  523. return 0;
  524. case HDIO_SET_BUSSTATE:
  525. if (!capable(CAP_SYS_ADMIN))
  526. return -EACCES;
  527. return -EOPNOTSUPP;
  528. default:
  529. return -EINVAL;
  530. }
  531. read_val:
  532. mutex_lock(&ide_setting_mtx);
  533. spin_lock_irqsave(&ide_lock, flags);
  534. err = *val;
  535. spin_unlock_irqrestore(&ide_lock, flags);
  536. mutex_unlock(&ide_setting_mtx);
  537. return err >= 0 ? put_user(err, (long __user *)arg) : err;
  538. set_val:
  539. if (bdev != bdev->bd_contains)
  540. err = -EINVAL;
  541. else {
  542. if (!capable(CAP_SYS_ADMIN))
  543. err = -EACCES;
  544. else {
  545. mutex_lock(&ide_setting_mtx);
  546. err = setfunc(drive, arg);
  547. mutex_unlock(&ide_setting_mtx);
  548. }
  549. }
  550. return err;
  551. }
  552. EXPORT_SYMBOL(generic_ide_ioctl);
  553. static int ide_bus_match(struct device *dev, struct device_driver *drv)
  554. {
  555. return 1;
  556. }
  557. static char *media_string(ide_drive_t *drive)
  558. {
  559. switch (drive->media) {
  560. case ide_disk:
  561. return "disk";
  562. case ide_cdrom:
  563. return "cdrom";
  564. case ide_tape:
  565. return "tape";
  566. case ide_floppy:
  567. return "floppy";
  568. case ide_optical:
  569. return "optical";
  570. default:
  571. return "UNKNOWN";
  572. }
  573. }
  574. static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
  575. {
  576. ide_drive_t *drive = to_ide_device(dev);
  577. return sprintf(buf, "%s\n", media_string(drive));
  578. }
  579. static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
  580. {
  581. ide_drive_t *drive = to_ide_device(dev);
  582. return sprintf(buf, "%s\n", drive->name);
  583. }
  584. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  585. {
  586. ide_drive_t *drive = to_ide_device(dev);
  587. return sprintf(buf, "ide:m-%s\n", media_string(drive));
  588. }
  589. static ssize_t model_show(struct device *dev, struct device_attribute *attr,
  590. char *buf)
  591. {
  592. ide_drive_t *drive = to_ide_device(dev);
  593. return sprintf(buf, "%s\n", drive->id->model);
  594. }
  595. static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
  596. char *buf)
  597. {
  598. ide_drive_t *drive = to_ide_device(dev);
  599. return sprintf(buf, "%s\n", drive->id->fw_rev);
  600. }
  601. static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
  602. char *buf)
  603. {
  604. ide_drive_t *drive = to_ide_device(dev);
  605. return sprintf(buf, "%s\n", drive->id->serial_no);
  606. }
  607. static struct device_attribute ide_dev_attrs[] = {
  608. __ATTR_RO(media),
  609. __ATTR_RO(drivename),
  610. __ATTR_RO(modalias),
  611. __ATTR_RO(model),
  612. __ATTR_RO(firmware),
  613. __ATTR(serial, 0400, serial_show, NULL),
  614. __ATTR_NULL
  615. };
  616. static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
  617. {
  618. ide_drive_t *drive = to_ide_device(dev);
  619. add_uevent_var(env, "MEDIA=%s", media_string(drive));
  620. add_uevent_var(env, "DRIVENAME=%s", drive->name);
  621. add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
  622. return 0;
  623. }
  624. static int generic_ide_probe(struct device *dev)
  625. {
  626. ide_drive_t *drive = to_ide_device(dev);
  627. ide_driver_t *drv = to_ide_driver(dev->driver);
  628. return drv->probe ? drv->probe(drive) : -ENODEV;
  629. }
  630. static int generic_ide_remove(struct device *dev)
  631. {
  632. ide_drive_t *drive = to_ide_device(dev);
  633. ide_driver_t *drv = to_ide_driver(dev->driver);
  634. if (drv->remove)
  635. drv->remove(drive);
  636. return 0;
  637. }
  638. static void generic_ide_shutdown(struct device *dev)
  639. {
  640. ide_drive_t *drive = to_ide_device(dev);
  641. ide_driver_t *drv = to_ide_driver(dev->driver);
  642. if (dev->driver && drv->shutdown)
  643. drv->shutdown(drive);
  644. }
  645. struct bus_type ide_bus_type = {
  646. .name = "ide",
  647. .match = ide_bus_match,
  648. .uevent = ide_uevent,
  649. .probe = generic_ide_probe,
  650. .remove = generic_ide_remove,
  651. .shutdown = generic_ide_shutdown,
  652. .dev_attrs = ide_dev_attrs,
  653. .suspend = generic_ide_suspend,
  654. .resume = generic_ide_resume,
  655. };
  656. EXPORT_SYMBOL_GPL(ide_bus_type);
  657. int ide_vlb_clk;
  658. EXPORT_SYMBOL_GPL(ide_vlb_clk);
  659. module_param_named(vlb_clock, ide_vlb_clk, int, 0);
  660. MODULE_PARM_DESC(vlb_clock, "VLB clock frequency (in MHz)");
  661. int ide_pci_clk;
  662. EXPORT_SYMBOL_GPL(ide_pci_clk);
  663. module_param_named(pci_clock, ide_pci_clk, int, 0);
  664. MODULE_PARM_DESC(pci_clock, "PCI bus clock frequency (in MHz)");
  665. static int ide_set_dev_param_mask(const char *s, struct kernel_param *kp)
  666. {
  667. int a, b, i, j = 1;
  668. unsigned int *dev_param_mask = (unsigned int *)kp->arg;
  669. if (sscanf(s, "%d.%d:%d", &a, &b, &j) != 3 &&
  670. sscanf(s, "%d.%d", &a, &b) != 2)
  671. return -EINVAL;
  672. i = a * MAX_DRIVES + b;
  673. if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
  674. return -EINVAL;
  675. if (j)
  676. *dev_param_mask |= (1 << i);
  677. else
  678. *dev_param_mask &= (1 << i);
  679. return 0;
  680. }
  681. static unsigned int ide_nodma;
  682. module_param_call(nodma, ide_set_dev_param_mask, NULL, &ide_nodma, 0);
  683. MODULE_PARM_DESC(nodma, "disallow DMA for a device");
  684. static unsigned int ide_noflush;
  685. module_param_call(noflush, ide_set_dev_param_mask, NULL, &ide_noflush, 0);
  686. MODULE_PARM_DESC(noflush, "disable flush requests for a device");
  687. static unsigned int ide_noprobe;
  688. module_param_call(noprobe, ide_set_dev_param_mask, NULL, &ide_noprobe, 0);
  689. MODULE_PARM_DESC(noprobe, "skip probing for a device");
  690. static unsigned int ide_nowerr;
  691. module_param_call(nowerr, ide_set_dev_param_mask, NULL, &ide_nowerr, 0);
  692. MODULE_PARM_DESC(nowerr, "ignore the WRERR_STAT bit for a device");
  693. static unsigned int ide_cdroms;
  694. module_param_call(cdrom, ide_set_dev_param_mask, NULL, &ide_cdroms, 0);
  695. MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
  696. struct chs_geom {
  697. unsigned int cyl;
  698. u8 head;
  699. u8 sect;
  700. };
  701. static unsigned int ide_disks;
  702. static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
  703. static int ide_set_disk_chs(const char *str, struct kernel_param *kp)
  704. {
  705. int a, b, c = 0, h = 0, s = 0, i, j = 1;
  706. if (sscanf(str, "%d.%d:%d,%d,%d", &a, &b, &c, &h, &s) != 5 &&
  707. sscanf(str, "%d.%d:%d", &a, &b, &j) != 3)
  708. return -EINVAL;
  709. i = a * MAX_DRIVES + b;
  710. if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
  711. return -EINVAL;
  712. if (c > INT_MAX || h > 255 || s > 255)
  713. return -EINVAL;
  714. if (j)
  715. ide_disks |= (1 << i);
  716. else
  717. ide_disks &= (1 << i);
  718. ide_disks_chs[i].cyl = c;
  719. ide_disks_chs[i].head = h;
  720. ide_disks_chs[i].sect = s;
  721. return 0;
  722. }
  723. module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
  724. MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
  725. static void ide_dev_apply_params(ide_drive_t *drive)
  726. {
  727. int i = drive->hwif->index * MAX_DRIVES + drive->select.b.unit;
  728. if (ide_nodma & (1 << i)) {
  729. printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
  730. drive->nodma = 1;
  731. }
  732. if (ide_noflush & (1 << i)) {
  733. printk(KERN_INFO "ide: disabling flush requests for %s\n",
  734. drive->name);
  735. drive->noflush = 1;
  736. }
  737. if (ide_noprobe & (1 << i)) {
  738. printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
  739. drive->noprobe = 1;
  740. }
  741. if (ide_nowerr & (1 << i)) {
  742. printk(KERN_INFO "ide: ignoring the WRERR_STAT bit for %s\n",
  743. drive->name);
  744. drive->bad_wstat = BAD_R_STAT;
  745. }
  746. if (ide_cdroms & (1 << i)) {
  747. printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
  748. drive->present = 1;
  749. drive->media = ide_cdrom;
  750. /* an ATAPI device ignores DRDY */
  751. drive->ready_stat = 0;
  752. }
  753. if (ide_disks & (1 << i)) {
  754. drive->cyl = drive->bios_cyl = ide_disks_chs[i].cyl;
  755. drive->head = drive->bios_head = ide_disks_chs[i].head;
  756. drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
  757. drive->forced_geom = 1;
  758. printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
  759. drive->name,
  760. drive->cyl, drive->head, drive->sect);
  761. drive->present = 1;
  762. drive->media = ide_disk;
  763. drive->ready_stat = READY_STAT;
  764. }
  765. }
  766. static unsigned int ide_ignore_cable;
  767. static int ide_set_ignore_cable(const char *s, struct kernel_param *kp)
  768. {
  769. int i, j = 1;
  770. if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
  771. return -EINVAL;
  772. if (i >= MAX_HWIFS || j < 0 || j > 1)
  773. return -EINVAL;
  774. if (j)
  775. ide_ignore_cable |= (1 << i);
  776. else
  777. ide_ignore_cable &= (1 << i);
  778. return 0;
  779. }
  780. module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
  781. MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
  782. void ide_port_apply_params(ide_hwif_t *hwif)
  783. {
  784. int i;
  785. if (ide_ignore_cable & (1 << hwif->index)) {
  786. printk(KERN_INFO "ide: ignoring cable detection for %s\n",
  787. hwif->name);
  788. hwif->cbl = ATA_CBL_PATA40_SHORT;
  789. }
  790. for (i = 0; i < MAX_DRIVES; i++)
  791. ide_dev_apply_params(&hwif->drives[i]);
  792. }
  793. /*
  794. * This is gets invoked once during initialization, to set *everything* up
  795. */
  796. static int __init ide_init(void)
  797. {
  798. int ret;
  799. printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
  800. ret = bus_register(&ide_bus_type);
  801. if (ret < 0) {
  802. printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
  803. return ret;
  804. }
  805. ide_port_class = class_create(THIS_MODULE, "ide_port");
  806. if (IS_ERR(ide_port_class)) {
  807. ret = PTR_ERR(ide_port_class);
  808. goto out_port_class;
  809. }
  810. proc_ide_create();
  811. return 0;
  812. out_port_class:
  813. bus_unregister(&ide_bus_type);
  814. return ret;
  815. }
  816. static void __exit ide_exit(void)
  817. {
  818. proc_ide_destroy();
  819. class_destroy(ide_port_class);
  820. bus_unregister(&ide_bus_type);
  821. }
  822. module_init(ide_init);
  823. module_exit(ide_exit);
  824. MODULE_LICENSE("GPL");