ide-io.c 25 KB

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