ide-io.c 24 KB

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