ide-io.c 24 KB

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