ide-io.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /*
  2. * IDE I/O functions
  3. *
  4. * Basic PIO and command management functionality.
  5. *
  6. * This code was split off from ide.c. See ide.c for history and original
  7. * copyrights.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2, or (at your option) any
  12. * later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * For the avoidance of doubt the "preferred form" of this code is one which
  20. * is in an open non patent encumbered format. Where cryptographic key signing
  21. * forms part of the process of creating an executable the information
  22. * including keys needed to generate an equivalently functional executable
  23. * are deemed to be part of the source code.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <linux/string.h>
  28. #include <linux/kernel.h>
  29. #include <linux/timer.h>
  30. #include <linux/mm.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/major.h>
  33. #include <linux/errno.h>
  34. #include <linux/genhd.h>
  35. #include <linux/blkpg.h>
  36. #include <linux/slab.h>
  37. #include <linux/init.h>
  38. #include <linux/pci.h>
  39. #include <linux/delay.h>
  40. #include <linux/ide.h>
  41. #include <linux/completion.h>
  42. #include <linux/reboot.h>
  43. #include <linux/cdrom.h>
  44. #include <linux/seq_file.h>
  45. #include <linux/device.h>
  46. #include <linux/kmod.h>
  47. #include <linux/scatterlist.h>
  48. #include <linux/bitops.h>
  49. #include <asm/byteorder.h>
  50. #include <asm/irq.h>
  51. #include <asm/uaccess.h>
  52. #include <asm/io.h>
  53. int ide_end_rq(ide_drive_t *drive, struct request *rq, int error,
  54. unsigned int nr_bytes)
  55. {
  56. /*
  57. * decide whether to reenable DMA -- 3 is a random magic for now,
  58. * if we DMA timeout more than 3 times, just stay in PIO
  59. */
  60. if ((drive->dev_flags & IDE_DFLAG_DMA_PIO_RETRY) &&
  61. drive->retry_pio <= 3) {
  62. drive->dev_flags &= ~IDE_DFLAG_DMA_PIO_RETRY;
  63. ide_dma_on(drive);
  64. }
  65. return blk_end_request(rq, error, nr_bytes);
  66. }
  67. EXPORT_SYMBOL_GPL(ide_end_rq);
  68. void ide_complete_cmd(ide_drive_t *drive, struct ide_cmd *cmd, u8 stat, u8 err)
  69. {
  70. const struct ide_tp_ops *tp_ops = drive->hwif->tp_ops;
  71. struct ide_taskfile *tf = &cmd->tf;
  72. struct request *rq = cmd->rq;
  73. u8 tf_cmd = tf->command;
  74. tf->error = err;
  75. tf->status = stat;
  76. if (cmd->ftf_flags & IDE_FTFLAG_IN_DATA) {
  77. u8 data[2];
  78. tp_ops->input_data(drive, cmd, data, 2);
  79. cmd->tf.data = data[0];
  80. cmd->hob.data = data[1];
  81. }
  82. ide_tf_readback(drive, cmd);
  83. if ((cmd->tf_flags & IDE_TFLAG_CUSTOM_HANDLER) &&
  84. tf_cmd == ATA_CMD_IDLEIMMEDIATE) {
  85. if (tf->lbal != 0xc4) {
  86. printk(KERN_ERR "%s: head unload failed!\n",
  87. drive->name);
  88. ide_tf_dump(drive->name, cmd);
  89. } else
  90. drive->dev_flags |= IDE_DFLAG_PARKED;
  91. }
  92. if (rq && rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  93. struct ide_cmd *orig_cmd = rq->special;
  94. if (cmd->tf_flags & IDE_TFLAG_DYN)
  95. kfree(orig_cmd);
  96. else
  97. memcpy(orig_cmd, cmd, sizeof(*cmd));
  98. }
  99. }
  100. int ide_complete_rq(ide_drive_t *drive, int error, unsigned int nr_bytes)
  101. {
  102. ide_hwif_t *hwif = drive->hwif;
  103. struct request *rq = hwif->rq;
  104. int rc;
  105. /*
  106. * if failfast is set on a request, override number of sectors
  107. * and complete the whole request right now
  108. */
  109. if (blk_noretry_request(rq) && error <= 0)
  110. nr_bytes = blk_rq_sectors(rq) << 9;
  111. rc = ide_end_rq(drive, rq, error, nr_bytes);
  112. if (rc == 0)
  113. hwif->rq = NULL;
  114. return rc;
  115. }
  116. EXPORT_SYMBOL(ide_complete_rq);
  117. void ide_kill_rq(ide_drive_t *drive, struct request *rq)
  118. {
  119. u8 drv_req = (rq->cmd_type == REQ_TYPE_DRV_PRIV) && rq->rq_disk;
  120. u8 media = drive->media;
  121. drive->failed_pc = NULL;
  122. if ((media == ide_floppy || media == ide_tape) && drv_req) {
  123. rq->errors = 0;
  124. } else {
  125. if (media == ide_tape)
  126. rq->errors = IDE_DRV_ERROR_GENERAL;
  127. else if (rq->cmd_type != REQ_TYPE_FS && rq->errors == 0)
  128. rq->errors = -EIO;
  129. }
  130. ide_complete_rq(drive, -EIO, blk_rq_bytes(rq));
  131. }
  132. static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  133. {
  134. tf->nsect = drive->sect;
  135. tf->lbal = drive->sect;
  136. tf->lbam = drive->cyl;
  137. tf->lbah = drive->cyl >> 8;
  138. tf->device = (drive->head - 1) | drive->select;
  139. tf->command = ATA_CMD_INIT_DEV_PARAMS;
  140. }
  141. static void ide_tf_set_restore_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  142. {
  143. tf->nsect = drive->sect;
  144. tf->command = ATA_CMD_RESTORE;
  145. }
  146. static void ide_tf_set_setmult_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  147. {
  148. tf->nsect = drive->mult_req;
  149. tf->command = ATA_CMD_SET_MULTI;
  150. }
  151. /**
  152. * do_special - issue some special commands
  153. * @drive: drive the command is for
  154. *
  155. * do_special() is used to issue ATA_CMD_INIT_DEV_PARAMS,
  156. * ATA_CMD_RESTORE and ATA_CMD_SET_MULTI commands to a drive.
  157. */
  158. static ide_startstop_t do_special(ide_drive_t *drive)
  159. {
  160. struct ide_cmd cmd;
  161. #ifdef DEBUG
  162. printk(KERN_DEBUG "%s: %s: 0x%02x\n", drive->name, __func__,
  163. drive->special_flags);
  164. #endif
  165. if (drive->media != ide_disk) {
  166. drive->special_flags = 0;
  167. drive->mult_req = 0;
  168. return ide_stopped;
  169. }
  170. memset(&cmd, 0, sizeof(cmd));
  171. cmd.protocol = ATA_PROT_NODATA;
  172. if (drive->special_flags & IDE_SFLAG_SET_GEOMETRY) {
  173. drive->special_flags &= ~IDE_SFLAG_SET_GEOMETRY;
  174. ide_tf_set_specify_cmd(drive, &cmd.tf);
  175. } else if (drive->special_flags & IDE_SFLAG_RECALIBRATE) {
  176. drive->special_flags &= ~IDE_SFLAG_RECALIBRATE;
  177. ide_tf_set_restore_cmd(drive, &cmd.tf);
  178. } else if (drive->special_flags & IDE_SFLAG_SET_MULTMODE) {
  179. drive->special_flags &= ~IDE_SFLAG_SET_MULTMODE;
  180. ide_tf_set_setmult_cmd(drive, &cmd.tf);
  181. } else
  182. BUG();
  183. cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
  184. cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
  185. cmd.tf_flags = IDE_TFLAG_CUSTOM_HANDLER;
  186. do_rw_taskfile(drive, &cmd);
  187. return ide_started;
  188. }
  189. void ide_map_sg(ide_drive_t *drive, struct ide_cmd *cmd)
  190. {
  191. ide_hwif_t *hwif = drive->hwif;
  192. struct scatterlist *sg = hwif->sg_table;
  193. struct request *rq = cmd->rq;
  194. cmd->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
  195. }
  196. EXPORT_SYMBOL_GPL(ide_map_sg);
  197. void ide_init_sg_cmd(struct ide_cmd *cmd, unsigned int nr_bytes)
  198. {
  199. cmd->nbytes = cmd->nleft = nr_bytes;
  200. cmd->cursg_ofs = 0;
  201. cmd->cursg = NULL;
  202. }
  203. EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
  204. /**
  205. * execute_drive_command - issue special drive command
  206. * @drive: the drive to issue the command on
  207. * @rq: the request structure holding the command
  208. *
  209. * execute_drive_cmd() issues a special drive command, usually
  210. * initiated by ioctl() from the external hdparm program. The
  211. * command can be a drive command, drive task or taskfile
  212. * operation. Weirdly you can call it with NULL to wait for
  213. * all commands to finish. Don't do this as that is due to change
  214. */
  215. static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
  216. struct request *rq)
  217. {
  218. struct ide_cmd *cmd = rq->special;
  219. if (cmd) {
  220. if (cmd->protocol == ATA_PROT_PIO) {
  221. ide_init_sg_cmd(cmd, blk_rq_sectors(rq) << 9);
  222. ide_map_sg(drive, cmd);
  223. }
  224. return do_rw_taskfile(drive, cmd);
  225. }
  226. /*
  227. * NULL is actually a valid way of waiting for
  228. * all current requests to be flushed from the queue.
  229. */
  230. #ifdef DEBUG
  231. printk("%s: DRIVE_CMD (null)\n", drive->name);
  232. #endif
  233. rq->errors = 0;
  234. ide_complete_rq(drive, 0, blk_rq_bytes(rq));
  235. return ide_stopped;
  236. }
  237. static ide_startstop_t ide_special_rq(ide_drive_t *drive, struct request *rq)
  238. {
  239. u8 cmd = rq->cmd[0];
  240. switch (cmd) {
  241. case REQ_PARK_HEADS:
  242. case REQ_UNPARK_HEADS:
  243. return ide_do_park_unpark(drive, rq);
  244. case REQ_DEVSET_EXEC:
  245. return ide_do_devset(drive, rq);
  246. case REQ_DRIVE_RESET:
  247. return ide_do_reset(drive);
  248. default:
  249. BUG();
  250. }
  251. }
  252. /**
  253. * start_request - start of I/O and command issuing for IDE
  254. *
  255. * start_request() initiates handling of a new I/O request. It
  256. * accepts commands and I/O (read/write) requests.
  257. *
  258. * FIXME: this function needs a rename
  259. */
  260. static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
  261. {
  262. ide_startstop_t startstop;
  263. BUG_ON(!(rq->cmd_flags & REQ_STARTED));
  264. #ifdef DEBUG
  265. printk("%s: start_request: current=0x%08lx\n",
  266. drive->hwif->name, (unsigned long) rq);
  267. #endif
  268. /* bail early if we've exceeded max_failures */
  269. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  270. rq->cmd_flags |= REQ_FAILED;
  271. goto kill_rq;
  272. }
  273. if (ata_pm_request(rq))
  274. ide_check_pm_state(drive, rq);
  275. drive->hwif->tp_ops->dev_select(drive);
  276. if (ide_wait_stat(&startstop, drive, drive->ready_stat,
  277. ATA_BUSY | ATA_DRQ, WAIT_READY)) {
  278. printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
  279. return startstop;
  280. }
  281. if (drive->special_flags == 0) {
  282. struct ide_driver *drv;
  283. /*
  284. * We reset the drive so we need to issue a SETFEATURES.
  285. * Do it _after_ do_special() restored device parameters.
  286. */
  287. if (drive->current_speed == 0xff)
  288. ide_config_drive_speed(drive, drive->desired_speed);
  289. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  290. return execute_drive_cmd(drive, rq);
  291. else if (ata_pm_request(rq)) {
  292. struct ide_pm_state *pm = rq->special;
  293. #ifdef DEBUG_PM
  294. printk("%s: start_power_step(step: %d)\n",
  295. drive->name, pm->pm_step);
  296. #endif
  297. startstop = ide_start_power_step(drive, rq);
  298. if (startstop == ide_stopped &&
  299. pm->pm_step == IDE_PM_COMPLETED)
  300. ide_complete_pm_rq(drive, rq);
  301. return startstop;
  302. } else if (!rq->rq_disk && rq->cmd_type == REQ_TYPE_DRV_PRIV)
  303. /*
  304. * TODO: Once all ULDs have been modified to
  305. * check for specific op codes rather than
  306. * blindly accepting any special request, the
  307. * check for ->rq_disk above may be replaced
  308. * by a more suitable mechanism or even
  309. * dropped entirely.
  310. */
  311. return ide_special_rq(drive, rq);
  312. drv = *(struct ide_driver **)rq->rq_disk->private_data;
  313. return drv->do_request(drive, rq, blk_rq_pos(rq));
  314. }
  315. return do_special(drive);
  316. kill_rq:
  317. ide_kill_rq(drive, rq);
  318. return ide_stopped;
  319. }
  320. /**
  321. * ide_stall_queue - pause an IDE device
  322. * @drive: drive to stall
  323. * @timeout: time to stall for (jiffies)
  324. *
  325. * ide_stall_queue() can be used by a drive to give excess bandwidth back
  326. * to the port by sleeping for timeout jiffies.
  327. */
  328. void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
  329. {
  330. if (timeout > WAIT_WORSTCASE)
  331. timeout = WAIT_WORSTCASE;
  332. drive->sleep = timeout + jiffies;
  333. drive->dev_flags |= IDE_DFLAG_SLEEPING;
  334. }
  335. EXPORT_SYMBOL(ide_stall_queue);
  336. static inline int ide_lock_port(ide_hwif_t *hwif)
  337. {
  338. if (hwif->busy)
  339. return 1;
  340. hwif->busy = 1;
  341. return 0;
  342. }
  343. static inline void ide_unlock_port(ide_hwif_t *hwif)
  344. {
  345. hwif->busy = 0;
  346. }
  347. static inline int ide_lock_host(struct ide_host *host, ide_hwif_t *hwif)
  348. {
  349. int rc = 0;
  350. if (host->host_flags & IDE_HFLAG_SERIALIZE) {
  351. rc = test_and_set_bit_lock(IDE_HOST_BUSY, &host->host_busy);
  352. if (rc == 0) {
  353. if (host->get_lock)
  354. host->get_lock(ide_intr, hwif);
  355. }
  356. }
  357. return rc;
  358. }
  359. static inline void ide_unlock_host(struct ide_host *host)
  360. {
  361. if (host->host_flags & IDE_HFLAG_SERIALIZE) {
  362. if (host->release_lock)
  363. host->release_lock();
  364. clear_bit_unlock(IDE_HOST_BUSY, &host->host_busy);
  365. }
  366. }
  367. static void __ide_requeue_and_plug(struct request_queue *q, struct request *rq)
  368. {
  369. if (rq)
  370. blk_requeue_request(q, rq);
  371. if (rq || blk_peek_request(q)) {
  372. /* Use 3ms as that was the old plug delay */
  373. blk_delay_queue(q, 3);
  374. }
  375. }
  376. void ide_requeue_and_plug(ide_drive_t *drive, struct request *rq)
  377. {
  378. struct request_queue *q = drive->queue;
  379. unsigned long flags;
  380. spin_lock_irqsave(q->queue_lock, flags);
  381. __ide_requeue_and_plug(q, rq);
  382. spin_unlock_irqrestore(q->queue_lock, flags);
  383. }
  384. /*
  385. * Issue a new request to a device.
  386. */
  387. void do_ide_request(struct request_queue *q)
  388. {
  389. ide_drive_t *drive = q->queuedata;
  390. ide_hwif_t *hwif = drive->hwif;
  391. struct ide_host *host = hwif->host;
  392. struct request *rq = NULL;
  393. ide_startstop_t startstop;
  394. unsigned long queue_run_ms = 3; /* old plug delay */
  395. spin_unlock_irq(q->queue_lock);
  396. /* HLD do_request() callback might sleep, make sure it's okay */
  397. might_sleep();
  398. if (ide_lock_host(host, hwif))
  399. goto plug_device_2;
  400. spin_lock_irq(&hwif->lock);
  401. if (!ide_lock_port(hwif)) {
  402. ide_hwif_t *prev_port;
  403. WARN_ON_ONCE(hwif->rq);
  404. repeat:
  405. prev_port = hwif->host->cur_port;
  406. if (drive->dev_flags & IDE_DFLAG_SLEEPING &&
  407. time_after(drive->sleep, jiffies)) {
  408. unsigned long left = jiffies - drive->sleep;
  409. queue_run_ms = jiffies_to_msecs(left + 1);
  410. ide_unlock_port(hwif);
  411. goto plug_device;
  412. }
  413. if ((hwif->host->host_flags & IDE_HFLAG_SERIALIZE) &&
  414. hwif != prev_port) {
  415. ide_drive_t *cur_dev =
  416. prev_port ? prev_port->cur_dev : NULL;
  417. /*
  418. * set nIEN for previous port, drives in the
  419. * quirk list may not like intr setups/cleanups
  420. */
  421. if (cur_dev &&
  422. (cur_dev->dev_flags & IDE_DFLAG_NIEN_QUIRK) == 0)
  423. prev_port->tp_ops->write_devctl(prev_port,
  424. ATA_NIEN |
  425. ATA_DEVCTL_OBS);
  426. hwif->host->cur_port = hwif;
  427. }
  428. hwif->cur_dev = drive;
  429. drive->dev_flags &= ~(IDE_DFLAG_SLEEPING | IDE_DFLAG_PARKED);
  430. spin_unlock_irq(&hwif->lock);
  431. spin_lock_irq(q->queue_lock);
  432. /*
  433. * we know that the queue isn't empty, but this can happen
  434. * if the q->prep_rq_fn() decides to kill a request
  435. */
  436. if (!rq)
  437. rq = blk_fetch_request(drive->queue);
  438. spin_unlock_irq(q->queue_lock);
  439. spin_lock_irq(&hwif->lock);
  440. if (!rq) {
  441. ide_unlock_port(hwif);
  442. goto out;
  443. }
  444. /*
  445. * Sanity: don't accept a request that isn't a PM request
  446. * if we are currently power managed. This is very important as
  447. * blk_stop_queue() doesn't prevent the blk_fetch_request()
  448. * above to return us whatever is in the queue. Since we call
  449. * ide_do_request() ourselves, we end up taking requests while
  450. * the queue is blocked...
  451. *
  452. * We let requests forced at head of queue with ide-preempt
  453. * though. I hope that doesn't happen too much, hopefully not
  454. * unless the subdriver triggers such a thing in its own PM
  455. * state machine.
  456. */
  457. if ((drive->dev_flags & IDE_DFLAG_BLOCKED) &&
  458. ata_pm_request(rq) == 0 &&
  459. (rq->cmd_flags & REQ_PREEMPT) == 0) {
  460. /* there should be no pending command at this point */
  461. ide_unlock_port(hwif);
  462. goto plug_device;
  463. }
  464. hwif->rq = rq;
  465. spin_unlock_irq(&hwif->lock);
  466. startstop = start_request(drive, rq);
  467. spin_lock_irq(&hwif->lock);
  468. if (startstop == ide_stopped) {
  469. rq = hwif->rq;
  470. hwif->rq = NULL;
  471. goto repeat;
  472. }
  473. } else
  474. goto plug_device;
  475. out:
  476. spin_unlock_irq(&hwif->lock);
  477. if (rq == NULL)
  478. ide_unlock_host(host);
  479. spin_lock_irq(q->queue_lock);
  480. return;
  481. plug_device:
  482. spin_unlock_irq(&hwif->lock);
  483. ide_unlock_host(host);
  484. plug_device_2:
  485. spin_lock_irq(q->queue_lock);
  486. __ide_requeue_and_plug(q, rq);
  487. }
  488. static int drive_is_ready(ide_drive_t *drive)
  489. {
  490. ide_hwif_t *hwif = drive->hwif;
  491. u8 stat = 0;
  492. if (drive->waiting_for_dma)
  493. return hwif->dma_ops->dma_test_irq(drive);
  494. if (hwif->io_ports.ctl_addr &&
  495. (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0)
  496. stat = hwif->tp_ops->read_altstatus(hwif);
  497. else
  498. /* Note: this may clear a pending IRQ!! */
  499. stat = hwif->tp_ops->read_status(hwif);
  500. if (stat & ATA_BUSY)
  501. /* drive busy: definitely not interrupting */
  502. return 0;
  503. /* drive ready: *might* be interrupting */
  504. return 1;
  505. }
  506. /**
  507. * ide_timer_expiry - handle lack of an IDE interrupt
  508. * @data: timer callback magic (hwif)
  509. *
  510. * An IDE command has timed out before the expected drive return
  511. * occurred. At this point we attempt to clean up the current
  512. * mess. If the current handler includes an expiry handler then
  513. * we invoke the expiry handler, and providing it is happy the
  514. * work is done. If that fails we apply generic recovery rules
  515. * invoking the handler and checking the drive DMA status. We
  516. * have an excessively incestuous relationship with the DMA
  517. * logic that wants cleaning up.
  518. */
  519. void ide_timer_expiry (unsigned long data)
  520. {
  521. ide_hwif_t *hwif = (ide_hwif_t *)data;
  522. ide_drive_t *uninitialized_var(drive);
  523. ide_handler_t *handler;
  524. unsigned long flags;
  525. int wait = -1;
  526. int plug_device = 0;
  527. struct request *uninitialized_var(rq_in_flight);
  528. spin_lock_irqsave(&hwif->lock, flags);
  529. handler = hwif->handler;
  530. if (handler == NULL || hwif->req_gen != hwif->req_gen_timer) {
  531. /*
  532. * Either a marginal timeout occurred
  533. * (got the interrupt just as timer expired),
  534. * or we were "sleeping" to give other devices a chance.
  535. * Either way, we don't really want to complain about anything.
  536. */
  537. } else {
  538. ide_expiry_t *expiry = hwif->expiry;
  539. ide_startstop_t startstop = ide_stopped;
  540. drive = hwif->cur_dev;
  541. if (expiry) {
  542. wait = expiry(drive);
  543. if (wait > 0) { /* continue */
  544. /* reset timer */
  545. hwif->timer.expires = jiffies + wait;
  546. hwif->req_gen_timer = hwif->req_gen;
  547. add_timer(&hwif->timer);
  548. spin_unlock_irqrestore(&hwif->lock, flags);
  549. return;
  550. }
  551. }
  552. hwif->handler = NULL;
  553. hwif->expiry = NULL;
  554. /*
  555. * We need to simulate a real interrupt when invoking
  556. * the handler() function, which means we need to
  557. * globally mask the specific IRQ:
  558. */
  559. spin_unlock(&hwif->lock);
  560. /* disable_irq_nosync ?? */
  561. disable_irq(hwif->irq);
  562. /* local CPU only, as if we were handling an interrupt */
  563. local_irq_disable();
  564. if (hwif->polling) {
  565. startstop = handler(drive);
  566. } else if (drive_is_ready(drive)) {
  567. if (drive->waiting_for_dma)
  568. hwif->dma_ops->dma_lost_irq(drive);
  569. if (hwif->port_ops && hwif->port_ops->clear_irq)
  570. hwif->port_ops->clear_irq(drive);
  571. printk(KERN_WARNING "%s: lost interrupt\n",
  572. drive->name);
  573. startstop = handler(drive);
  574. } else {
  575. if (drive->waiting_for_dma)
  576. startstop = ide_dma_timeout_retry(drive, wait);
  577. else
  578. startstop = ide_error(drive, "irq timeout",
  579. hwif->tp_ops->read_status(hwif));
  580. }
  581. spin_lock_irq(&hwif->lock);
  582. enable_irq(hwif->irq);
  583. if (startstop == ide_stopped && hwif->polling == 0) {
  584. rq_in_flight = hwif->rq;
  585. hwif->rq = NULL;
  586. ide_unlock_port(hwif);
  587. plug_device = 1;
  588. }
  589. }
  590. spin_unlock_irqrestore(&hwif->lock, flags);
  591. if (plug_device) {
  592. ide_unlock_host(hwif->host);
  593. ide_requeue_and_plug(drive, rq_in_flight);
  594. }
  595. }
  596. /**
  597. * unexpected_intr - handle an unexpected IDE interrupt
  598. * @irq: interrupt line
  599. * @hwif: port being processed
  600. *
  601. * There's nothing really useful we can do with an unexpected interrupt,
  602. * other than reading the status register (to clear it), and logging it.
  603. * There should be no way that an irq can happen before we're ready for it,
  604. * so we needn't worry much about losing an "important" interrupt here.
  605. *
  606. * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
  607. * the drive enters "idle", "standby", or "sleep" mode, so if the status
  608. * looks "good", we just ignore the interrupt completely.
  609. *
  610. * This routine assumes __cli() is in effect when called.
  611. *
  612. * If an unexpected interrupt happens on irq15 while we are handling irq14
  613. * and if the two interfaces are "serialized" (CMD640), then it looks like
  614. * we could screw up by interfering with a new request being set up for
  615. * irq15.
  616. *
  617. * In reality, this is a non-issue. The new command is not sent unless
  618. * the drive is ready to accept one, in which case we know the drive is
  619. * not trying to interrupt us. And ide_set_handler() is always invoked
  620. * before completing the issuance of any new drive command, so we will not
  621. * be accidentally invoked as a result of any valid command completion
  622. * interrupt.
  623. */
  624. static void unexpected_intr(int irq, ide_hwif_t *hwif)
  625. {
  626. u8 stat = hwif->tp_ops->read_status(hwif);
  627. if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
  628. /* Try to not flood the console with msgs */
  629. static unsigned long last_msgtime, count;
  630. ++count;
  631. if (time_after(jiffies, last_msgtime + HZ)) {
  632. last_msgtime = jiffies;
  633. printk(KERN_ERR "%s: unexpected interrupt, "
  634. "status=0x%02x, count=%ld\n",
  635. hwif->name, stat, count);
  636. }
  637. }
  638. }
  639. /**
  640. * ide_intr - default IDE interrupt handler
  641. * @irq: interrupt number
  642. * @dev_id: hwif
  643. * @regs: unused weirdness from the kernel irq layer
  644. *
  645. * This is the default IRQ handler for the IDE layer. You should
  646. * not need to override it. If you do be aware it is subtle in
  647. * places
  648. *
  649. * hwif is the interface in the group currently performing
  650. * a command. hwif->cur_dev is the drive and hwif->handler is
  651. * the IRQ handler to call. As we issue a command the handlers
  652. * step through multiple states, reassigning the handler to the
  653. * next step in the process. Unlike a smart SCSI controller IDE
  654. * expects the main processor to sequence the various transfer
  655. * stages. We also manage a poll timer to catch up with most
  656. * timeout situations. There are still a few where the handlers
  657. * don't ever decide to give up.
  658. *
  659. * The handler eventually returns ide_stopped to indicate the
  660. * request completed. At this point we issue the next request
  661. * on the port and the process begins again.
  662. */
  663. irqreturn_t ide_intr (int irq, void *dev_id)
  664. {
  665. ide_hwif_t *hwif = (ide_hwif_t *)dev_id;
  666. struct ide_host *host = hwif->host;
  667. ide_drive_t *uninitialized_var(drive);
  668. ide_handler_t *handler;
  669. unsigned long flags;
  670. ide_startstop_t startstop;
  671. irqreturn_t irq_ret = IRQ_NONE;
  672. int plug_device = 0;
  673. struct request *uninitialized_var(rq_in_flight);
  674. if (host->host_flags & IDE_HFLAG_SERIALIZE) {
  675. if (hwif != host->cur_port)
  676. goto out_early;
  677. }
  678. spin_lock_irqsave(&hwif->lock, flags);
  679. if (hwif->port_ops && hwif->port_ops->test_irq &&
  680. hwif->port_ops->test_irq(hwif) == 0)
  681. goto out;
  682. handler = hwif->handler;
  683. if (handler == NULL || hwif->polling) {
  684. /*
  685. * Not expecting an interrupt from this drive.
  686. * That means this could be:
  687. * (1) an interrupt from another PCI device
  688. * sharing the same PCI INT# as us.
  689. * or (2) a drive just entered sleep or standby mode,
  690. * and is interrupting to let us know.
  691. * or (3) a spurious interrupt of unknown origin.
  692. *
  693. * For PCI, we cannot tell the difference,
  694. * so in that case we just ignore it and hope it goes away.
  695. */
  696. if ((host->irq_flags & IRQF_SHARED) == 0) {
  697. /*
  698. * Probably not a shared PCI interrupt,
  699. * so we can safely try to do something about it:
  700. */
  701. unexpected_intr(irq, hwif);
  702. } else {
  703. /*
  704. * Whack the status register, just in case
  705. * we have a leftover pending IRQ.
  706. */
  707. (void)hwif->tp_ops->read_status(hwif);
  708. }
  709. goto out;
  710. }
  711. drive = hwif->cur_dev;
  712. if (!drive_is_ready(drive))
  713. /*
  714. * This happens regularly when we share a PCI IRQ with
  715. * another device. Unfortunately, it can also happen
  716. * with some buggy drives that trigger the IRQ before
  717. * their status register is up to date. Hopefully we have
  718. * enough advance overhead that the latter isn't a problem.
  719. */
  720. goto out;
  721. hwif->handler = NULL;
  722. hwif->expiry = NULL;
  723. hwif->req_gen++;
  724. del_timer(&hwif->timer);
  725. spin_unlock(&hwif->lock);
  726. if (hwif->port_ops && hwif->port_ops->clear_irq)
  727. hwif->port_ops->clear_irq(drive);
  728. if (drive->dev_flags & IDE_DFLAG_UNMASK)
  729. local_irq_enable_in_hardirq();
  730. /* service this interrupt, may set handler for next interrupt */
  731. startstop = handler(drive);
  732. spin_lock_irq(&hwif->lock);
  733. /*
  734. * Note that handler() may have set things up for another
  735. * interrupt to occur soon, but it cannot happen until
  736. * we exit from this routine, because it will be the
  737. * same irq as is currently being serviced here, and Linux
  738. * won't allow another of the same (on any CPU) until we return.
  739. */
  740. if (startstop == ide_stopped && hwif->polling == 0) {
  741. BUG_ON(hwif->handler);
  742. rq_in_flight = hwif->rq;
  743. hwif->rq = NULL;
  744. ide_unlock_port(hwif);
  745. plug_device = 1;
  746. }
  747. irq_ret = IRQ_HANDLED;
  748. out:
  749. spin_unlock_irqrestore(&hwif->lock, flags);
  750. out_early:
  751. if (plug_device) {
  752. ide_unlock_host(hwif->host);
  753. ide_requeue_and_plug(drive, rq_in_flight);
  754. }
  755. return irq_ret;
  756. }
  757. EXPORT_SYMBOL_GPL(ide_intr);
  758. void ide_pad_transfer(ide_drive_t *drive, int write, int len)
  759. {
  760. ide_hwif_t *hwif = drive->hwif;
  761. u8 buf[4] = { 0 };
  762. while (len > 0) {
  763. if (write)
  764. hwif->tp_ops->output_data(drive, NULL, buf, min(4, len));
  765. else
  766. hwif->tp_ops->input_data(drive, NULL, buf, min(4, len));
  767. len -= 4;
  768. }
  769. }
  770. EXPORT_SYMBOL_GPL(ide_pad_transfer);