ide-io.c 23 KB

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