ide-io.c 25 KB

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