ide-io.c 24 KB

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