ide-io.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
  74. drive->state = 0;
  75. ide_dma_on(drive);
  76. }
  77. if (!__blk_end_request(rq, error, nr_bytes)) {
  78. if (dequeue)
  79. HWGROUP(drive)->rq = NULL;
  80. ret = 0;
  81. }
  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;
  98. unsigned long flags;
  99. int ret = 1;
  100. /*
  101. * room for locking improvements here, the calls below don't
  102. * need the queue lock held at all
  103. */
  104. spin_lock_irqsave(&ide_lock, flags);
  105. rq = HWGROUP(drive)->rq;
  106. if (!nr_bytes) {
  107. if (blk_pc_request(rq))
  108. nr_bytes = rq->data_len;
  109. else
  110. nr_bytes = rq->hard_cur_sectors << 9;
  111. }
  112. ret = __ide_end_request(drive, rq, uptodate, nr_bytes, 1);
  113. spin_unlock_irqrestore(&ide_lock, flags);
  114. return ret;
  115. }
  116. EXPORT_SYMBOL(ide_end_request);
  117. /*
  118. * Power Management state machine. This one is rather trivial for now,
  119. * we should probably add more, like switching back to PIO on suspend
  120. * to help some BIOSes, re-do the door locking on resume, etc...
  121. */
  122. enum {
  123. ide_pm_flush_cache = ide_pm_state_start_suspend,
  124. idedisk_pm_standby,
  125. idedisk_pm_restore_pio = ide_pm_state_start_resume,
  126. idedisk_pm_idle,
  127. ide_pm_restore_dma,
  128. };
  129. static void ide_complete_power_step(ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
  130. {
  131. struct request_pm_state *pm = rq->data;
  132. if (drive->media != ide_disk)
  133. return;
  134. switch (pm->pm_step) {
  135. case ide_pm_flush_cache: /* Suspend step 1 (flush cache) complete */
  136. if (pm->pm_state == PM_EVENT_FREEZE)
  137. pm->pm_step = ide_pm_state_completed;
  138. else
  139. pm->pm_step = idedisk_pm_standby;
  140. break;
  141. case idedisk_pm_standby: /* Suspend step 2 (standby) complete */
  142. pm->pm_step = ide_pm_state_completed;
  143. break;
  144. case idedisk_pm_restore_pio: /* Resume step 1 complete */
  145. pm->pm_step = idedisk_pm_idle;
  146. break;
  147. case idedisk_pm_idle: /* Resume step 2 (idle) complete */
  148. pm->pm_step = ide_pm_restore_dma;
  149. break;
  150. }
  151. }
  152. static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
  153. {
  154. struct request_pm_state *pm = rq->data;
  155. ide_task_t *args = rq->special;
  156. memset(args, 0, sizeof(*args));
  157. switch (pm->pm_step) {
  158. case ide_pm_flush_cache: /* Suspend step 1 (flush cache) */
  159. if (drive->media != ide_disk)
  160. break;
  161. /* Not supported? Switch to next step now. */
  162. if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
  163. ide_complete_power_step(drive, rq, 0, 0);
  164. return ide_stopped;
  165. }
  166. if (ide_id_has_flush_cache_ext(drive->id))
  167. args->tf.command = ATA_CMD_FLUSH_EXT;
  168. else
  169. args->tf.command = ATA_CMD_FLUSH;
  170. goto out_do_tf;
  171. case idedisk_pm_standby: /* Suspend step 2 (standby) */
  172. args->tf.command = ATA_CMD_STANDBYNOW1;
  173. goto out_do_tf;
  174. case idedisk_pm_restore_pio: /* Resume step 1 (restore PIO) */
  175. ide_set_max_pio(drive);
  176. /*
  177. * skip idedisk_pm_idle for ATAPI devices
  178. */
  179. if (drive->media != ide_disk)
  180. pm->pm_step = ide_pm_restore_dma;
  181. else
  182. ide_complete_power_step(drive, rq, 0, 0);
  183. return ide_stopped;
  184. case idedisk_pm_idle: /* Resume step 2 (idle) */
  185. args->tf.command = ATA_CMD_IDLEIMMEDIATE;
  186. goto out_do_tf;
  187. case ide_pm_restore_dma: /* Resume step 3 (restore DMA) */
  188. /*
  189. * Right now, all we do is call ide_set_dma(drive),
  190. * we could be smarter and check for current xfer_speed
  191. * in struct drive etc...
  192. */
  193. if (drive->hwif->dma_ops == NULL)
  194. break;
  195. /*
  196. * TODO: respect ->using_dma setting
  197. */
  198. ide_set_dma(drive);
  199. break;
  200. }
  201. pm->pm_step = ide_pm_state_completed;
  202. return ide_stopped;
  203. out_do_tf:
  204. args->tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  205. args->data_phase = TASKFILE_NO_DATA;
  206. return do_rw_taskfile(drive, args);
  207. }
  208. /**
  209. * ide_end_dequeued_request - complete an IDE I/O
  210. * @drive: IDE device for the I/O
  211. * @uptodate:
  212. * @nr_sectors: number of sectors completed
  213. *
  214. * Complete an I/O that is no longer on the request queue. This
  215. * typically occurs when we pull the request and issue a REQUEST_SENSE.
  216. * We must still finish the old request but we must not tamper with the
  217. * queue in the meantime.
  218. *
  219. * NOTE: This path does not handle barrier, but barrier is not supported
  220. * on ide-cd anyway.
  221. */
  222. int ide_end_dequeued_request(ide_drive_t *drive, struct request *rq,
  223. int uptodate, int nr_sectors)
  224. {
  225. unsigned long flags;
  226. int ret;
  227. spin_lock_irqsave(&ide_lock, flags);
  228. BUG_ON(!blk_rq_started(rq));
  229. ret = __ide_end_request(drive, rq, uptodate, nr_sectors << 9, 0);
  230. spin_unlock_irqrestore(&ide_lock, flags);
  231. return ret;
  232. }
  233. EXPORT_SYMBOL_GPL(ide_end_dequeued_request);
  234. /**
  235. * ide_complete_pm_request - end the current Power Management request
  236. * @drive: target drive
  237. * @rq: request
  238. *
  239. * This function cleans up the current PM request and stops the queue
  240. * if necessary.
  241. */
  242. static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
  243. {
  244. unsigned long flags;
  245. #ifdef DEBUG_PM
  246. printk("%s: completing PM request, %s\n", drive->name,
  247. blk_pm_suspend_request(rq) ? "suspend" : "resume");
  248. #endif
  249. spin_lock_irqsave(&ide_lock, flags);
  250. if (blk_pm_suspend_request(rq)) {
  251. blk_stop_queue(drive->queue);
  252. } else {
  253. drive->blocked = 0;
  254. blk_start_queue(drive->queue);
  255. }
  256. HWGROUP(drive)->rq = NULL;
  257. if (__blk_end_request(rq, 0, 0))
  258. BUG();
  259. spin_unlock_irqrestore(&ide_lock, flags);
  260. }
  261. /**
  262. * ide_end_drive_cmd - end an explicit drive command
  263. * @drive: command
  264. * @stat: status bits
  265. * @err: error bits
  266. *
  267. * Clean up after success/failure of an explicit drive command.
  268. * These get thrown onto the queue so they are synchronized with
  269. * real I/O operations on the drive.
  270. *
  271. * In LBA48 mode we have to read the register set twice to get
  272. * all the extra information out.
  273. */
  274. void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
  275. {
  276. unsigned long flags;
  277. struct request *rq;
  278. spin_lock_irqsave(&ide_lock, flags);
  279. rq = HWGROUP(drive)->rq;
  280. spin_unlock_irqrestore(&ide_lock, flags);
  281. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  282. ide_task_t *task = (ide_task_t *)rq->special;
  283. if (rq->errors == 0)
  284. rq->errors = !OK_STAT(stat, ATA_DRDY, BAD_STAT);
  285. if (task) {
  286. struct ide_taskfile *tf = &task->tf;
  287. tf->error = err;
  288. tf->status = stat;
  289. drive->hwif->tp_ops->tf_read(drive, task);
  290. if (task->tf_flags & IDE_TFLAG_DYN)
  291. kfree(task);
  292. }
  293. } else if (blk_pm_request(rq)) {
  294. struct request_pm_state *pm = rq->data;
  295. #ifdef DEBUG_PM
  296. printk("%s: complete_power_step(step: %d, stat: %x, err: %x)\n",
  297. drive->name, rq->pm->pm_step, stat, err);
  298. #endif
  299. ide_complete_power_step(drive, rq, stat, err);
  300. if (pm->pm_step == ide_pm_state_completed)
  301. ide_complete_pm_request(drive, rq);
  302. return;
  303. }
  304. spin_lock_irqsave(&ide_lock, flags);
  305. HWGROUP(drive)->rq = NULL;
  306. rq->errors = err;
  307. if (unlikely(__blk_end_request(rq, (rq->errors ? -EIO : 0),
  308. blk_rq_bytes(rq))))
  309. BUG();
  310. spin_unlock_irqrestore(&ide_lock, flags);
  311. }
  312. EXPORT_SYMBOL(ide_end_drive_cmd);
  313. static void ide_kill_rq(ide_drive_t *drive, struct request *rq)
  314. {
  315. if (rq->rq_disk) {
  316. ide_driver_t *drv;
  317. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  318. drv->end_request(drive, 0, 0);
  319. } else
  320. ide_end_request(drive, 0, 0);
  321. }
  322. static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  323. {
  324. ide_hwif_t *hwif = drive->hwif;
  325. if ((stat & ATA_BUSY) || ((stat & ATA_DF) && !drive->nowerr)) {
  326. /* other bits are useless when BUSY */
  327. rq->errors |= ERROR_RESET;
  328. } else if (stat & ATA_ERR) {
  329. /* err has different meaning on cdrom and tape */
  330. if (err == ATA_ABORTED) {
  331. if (drive->select.b.lba &&
  332. /* some newer drives don't support ATA_CMD_INIT_DEV_PARAMS */
  333. hwif->tp_ops->read_status(hwif) == ATA_CMD_INIT_DEV_PARAMS)
  334. return ide_stopped;
  335. } else if ((err & BAD_CRC) == BAD_CRC) {
  336. /* UDMA crc error, just retry the operation */
  337. drive->crc_count++;
  338. } else if (err & (ATA_BBK | ATA_UNC)) {
  339. /* retries won't help these */
  340. rq->errors = ERROR_MAX;
  341. } else if (err & ATA_TRK0NF) {
  342. /* help it find track zero */
  343. rq->errors |= ERROR_RECAL;
  344. }
  345. }
  346. if ((stat & ATA_DRQ) && rq_data_dir(rq) == READ &&
  347. (hwif->host_flags & IDE_HFLAG_ERROR_STOPS_FIFO) == 0) {
  348. int nsect = drive->mult_count ? drive->mult_count : 1;
  349. ide_pad_transfer(drive, READ, nsect * SECTOR_SIZE);
  350. }
  351. if (rq->errors >= ERROR_MAX || blk_noretry_request(rq)) {
  352. ide_kill_rq(drive, rq);
  353. return ide_stopped;
  354. }
  355. if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
  356. rq->errors |= ERROR_RESET;
  357. if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
  358. ++rq->errors;
  359. return ide_do_reset(drive);
  360. }
  361. if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
  362. drive->special.b.recalibrate = 1;
  363. ++rq->errors;
  364. return ide_stopped;
  365. }
  366. static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  367. {
  368. ide_hwif_t *hwif = drive->hwif;
  369. if ((stat & ATA_BUSY) || ((stat & ATA_DF) && !drive->nowerr)) {
  370. /* other bits are useless when BUSY */
  371. rq->errors |= ERROR_RESET;
  372. } else {
  373. /* add decoding error stuff */
  374. }
  375. if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
  376. /* force an abort */
  377. hwif->tp_ops->exec_command(hwif, ATA_CMD_IDLEIMMEDIATE);
  378. if (rq->errors >= ERROR_MAX) {
  379. ide_kill_rq(drive, rq);
  380. } else {
  381. if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
  382. ++rq->errors;
  383. return ide_do_reset(drive);
  384. }
  385. ++rq->errors;
  386. }
  387. return ide_stopped;
  388. }
  389. ide_startstop_t
  390. __ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  391. {
  392. if (drive->media == ide_disk)
  393. return ide_ata_error(drive, rq, stat, err);
  394. return ide_atapi_error(drive, rq, stat, err);
  395. }
  396. EXPORT_SYMBOL_GPL(__ide_error);
  397. /**
  398. * ide_error - handle an error on the IDE
  399. * @drive: drive the error occurred on
  400. * @msg: message to report
  401. * @stat: status bits
  402. *
  403. * ide_error() takes action based on the error returned by the drive.
  404. * For normal I/O that may well include retries. We deal with
  405. * both new-style (taskfile) and old style command handling here.
  406. * In the case of taskfile command handling there is work left to
  407. * do
  408. */
  409. ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
  410. {
  411. struct request *rq;
  412. u8 err;
  413. err = ide_dump_status(drive, msg, stat);
  414. if ((rq = HWGROUP(drive)->rq) == NULL)
  415. return ide_stopped;
  416. /* retry only "normal" I/O: */
  417. if (!blk_fs_request(rq)) {
  418. rq->errors = 1;
  419. ide_end_drive_cmd(drive, stat, err);
  420. return ide_stopped;
  421. }
  422. if (rq->rq_disk) {
  423. ide_driver_t *drv;
  424. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  425. return drv->error(drive, rq, stat, err);
  426. } else
  427. return __ide_error(drive, rq, stat, err);
  428. }
  429. EXPORT_SYMBOL_GPL(ide_error);
  430. static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  431. {
  432. tf->nsect = drive->sect;
  433. tf->lbal = drive->sect;
  434. tf->lbam = drive->cyl;
  435. tf->lbah = drive->cyl >> 8;
  436. tf->device = ((drive->head - 1) | drive->select.all) & ~ATA_LBA;
  437. tf->command = ATA_CMD_INIT_DEV_PARAMS;
  438. }
  439. static void ide_tf_set_restore_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  440. {
  441. tf->nsect = drive->sect;
  442. tf->command = ATA_CMD_RESTORE;
  443. }
  444. static void ide_tf_set_setmult_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  445. {
  446. tf->nsect = drive->mult_req;
  447. tf->command = ATA_CMD_SET_MULTI;
  448. }
  449. static ide_startstop_t ide_disk_special(ide_drive_t *drive)
  450. {
  451. special_t *s = &drive->special;
  452. ide_task_t args;
  453. memset(&args, 0, sizeof(ide_task_t));
  454. args.data_phase = TASKFILE_NO_DATA;
  455. if (s->b.set_geometry) {
  456. s->b.set_geometry = 0;
  457. ide_tf_set_specify_cmd(drive, &args.tf);
  458. } else if (s->b.recalibrate) {
  459. s->b.recalibrate = 0;
  460. ide_tf_set_restore_cmd(drive, &args.tf);
  461. } else if (s->b.set_multmode) {
  462. s->b.set_multmode = 0;
  463. ide_tf_set_setmult_cmd(drive, &args.tf);
  464. } else if (s->all) {
  465. int special = s->all;
  466. s->all = 0;
  467. printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
  468. return ide_stopped;
  469. }
  470. args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE |
  471. IDE_TFLAG_CUSTOM_HANDLER;
  472. do_rw_taskfile(drive, &args);
  473. return ide_started;
  474. }
  475. /*
  476. * handle HDIO_SET_PIO_MODE ioctl abusers here, eventually it will go away
  477. */
  478. static int set_pio_mode_abuse(ide_hwif_t *hwif, u8 req_pio)
  479. {
  480. switch (req_pio) {
  481. case 202:
  482. case 201:
  483. case 200:
  484. case 102:
  485. case 101:
  486. case 100:
  487. return (hwif->host_flags & IDE_HFLAG_ABUSE_DMA_MODES) ? 1 : 0;
  488. case 9:
  489. case 8:
  490. return (hwif->host_flags & IDE_HFLAG_ABUSE_PREFETCH) ? 1 : 0;
  491. case 7:
  492. case 6:
  493. return (hwif->host_flags & IDE_HFLAG_ABUSE_FAST_DEVSEL) ? 1 : 0;
  494. default:
  495. return 0;
  496. }
  497. }
  498. /**
  499. * do_special - issue some special commands
  500. * @drive: drive the command is for
  501. *
  502. * do_special() is used to issue ATA_CMD_INIT_DEV_PARAMS,
  503. * ATA_CMD_RESTORE and ATA_CMD_SET_MULTI commands to a drive.
  504. *
  505. * It used to do much more, but has been scaled back.
  506. */
  507. static ide_startstop_t do_special (ide_drive_t *drive)
  508. {
  509. special_t *s = &drive->special;
  510. #ifdef DEBUG
  511. printk("%s: do_special: 0x%02x\n", drive->name, s->all);
  512. #endif
  513. if (s->b.set_tune) {
  514. ide_hwif_t *hwif = drive->hwif;
  515. const struct ide_port_ops *port_ops = hwif->port_ops;
  516. u8 req_pio = drive->tune_req;
  517. s->b.set_tune = 0;
  518. if (set_pio_mode_abuse(drive->hwif, req_pio)) {
  519. /*
  520. * take ide_lock for drive->[no_]unmask/[no_]io_32bit
  521. */
  522. if (req_pio == 8 || req_pio == 9) {
  523. unsigned long flags;
  524. spin_lock_irqsave(&ide_lock, flags);
  525. port_ops->set_pio_mode(drive, req_pio);
  526. spin_unlock_irqrestore(&ide_lock, flags);
  527. } else
  528. port_ops->set_pio_mode(drive, req_pio);
  529. } else {
  530. int keep_dma = drive->using_dma;
  531. ide_set_pio(drive, req_pio);
  532. if (hwif->host_flags & IDE_HFLAG_SET_PIO_MODE_KEEP_DMA) {
  533. if (keep_dma)
  534. ide_dma_on(drive);
  535. }
  536. }
  537. return ide_stopped;
  538. } else {
  539. if (drive->media == ide_disk)
  540. return ide_disk_special(drive);
  541. s->all = 0;
  542. drive->mult_req = 0;
  543. return ide_stopped;
  544. }
  545. }
  546. void ide_map_sg(ide_drive_t *drive, struct request *rq)
  547. {
  548. ide_hwif_t *hwif = drive->hwif;
  549. struct scatterlist *sg = hwif->sg_table;
  550. if (hwif->sg_mapped) /* needed by ide-scsi */
  551. return;
  552. if (rq->cmd_type != REQ_TYPE_ATA_TASKFILE) {
  553. hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
  554. } else {
  555. sg_init_one(sg, rq->buffer, rq->nr_sectors * SECTOR_SIZE);
  556. hwif->sg_nents = 1;
  557. }
  558. }
  559. EXPORT_SYMBOL_GPL(ide_map_sg);
  560. void ide_init_sg_cmd(ide_drive_t *drive, struct request *rq)
  561. {
  562. ide_hwif_t *hwif = drive->hwif;
  563. hwif->nsect = hwif->nleft = rq->nr_sectors;
  564. hwif->cursg_ofs = 0;
  565. hwif->cursg = NULL;
  566. }
  567. EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
  568. /**
  569. * execute_drive_command - issue special drive command
  570. * @drive: the drive to issue the command on
  571. * @rq: the request structure holding the command
  572. *
  573. * execute_drive_cmd() issues a special drive command, usually
  574. * initiated by ioctl() from the external hdparm program. The
  575. * command can be a drive command, drive task or taskfile
  576. * operation. Weirdly you can call it with NULL to wait for
  577. * all commands to finish. Don't do this as that is due to change
  578. */
  579. static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
  580. struct request *rq)
  581. {
  582. ide_hwif_t *hwif = HWIF(drive);
  583. ide_task_t *task = rq->special;
  584. if (task) {
  585. hwif->data_phase = task->data_phase;
  586. switch (hwif->data_phase) {
  587. case TASKFILE_MULTI_OUT:
  588. case TASKFILE_OUT:
  589. case TASKFILE_MULTI_IN:
  590. case TASKFILE_IN:
  591. ide_init_sg_cmd(drive, rq);
  592. ide_map_sg(drive, rq);
  593. default:
  594. break;
  595. }
  596. return do_rw_taskfile(drive, task);
  597. }
  598. /*
  599. * NULL is actually a valid way of waiting for
  600. * all current requests to be flushed from the queue.
  601. */
  602. #ifdef DEBUG
  603. printk("%s: DRIVE_CMD (null)\n", drive->name);
  604. #endif
  605. ide_end_drive_cmd(drive, hwif->tp_ops->read_status(hwif),
  606. ide_read_error(drive));
  607. return ide_stopped;
  608. }
  609. static ide_startstop_t ide_special_rq(ide_drive_t *drive, struct request *rq)
  610. {
  611. switch (rq->cmd[0]) {
  612. case REQ_DRIVE_RESET:
  613. return ide_do_reset(drive);
  614. default:
  615. blk_dump_rq_flags(rq, "ide_special_rq - bad request");
  616. ide_end_request(drive, 0, 0);
  617. return ide_stopped;
  618. }
  619. }
  620. static void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
  621. {
  622. struct request_pm_state *pm = rq->data;
  623. if (blk_pm_suspend_request(rq) &&
  624. pm->pm_step == ide_pm_state_start_suspend)
  625. /* Mark drive blocked when starting the suspend sequence. */
  626. drive->blocked = 1;
  627. else if (blk_pm_resume_request(rq) &&
  628. pm->pm_step == ide_pm_state_start_resume) {
  629. /*
  630. * The first thing we do on wakeup is to wait for BSY bit to
  631. * go away (with a looong timeout) as a drive on this hwif may
  632. * just be POSTing itself.
  633. * We do that before even selecting as the "other" device on
  634. * the bus may be broken enough to walk on our toes at this
  635. * point.
  636. */
  637. ide_hwif_t *hwif = drive->hwif;
  638. int rc;
  639. #ifdef DEBUG_PM
  640. printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
  641. #endif
  642. rc = ide_wait_not_busy(hwif, 35000);
  643. if (rc)
  644. printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
  645. SELECT_DRIVE(drive);
  646. hwif->tp_ops->set_irq(hwif, 1);
  647. rc = ide_wait_not_busy(hwif, 100000);
  648. if (rc)
  649. printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
  650. }
  651. }
  652. /**
  653. * start_request - start of I/O and command issuing for IDE
  654. *
  655. * start_request() initiates handling of a new I/O request. It
  656. * accepts commands and I/O (read/write) requests.
  657. *
  658. * FIXME: this function needs a rename
  659. */
  660. static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
  661. {
  662. ide_startstop_t startstop;
  663. BUG_ON(!blk_rq_started(rq));
  664. #ifdef DEBUG
  665. printk("%s: start_request: current=0x%08lx\n",
  666. HWIF(drive)->name, (unsigned long) rq);
  667. #endif
  668. /* bail early if we've exceeded max_failures */
  669. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  670. rq->cmd_flags |= REQ_FAILED;
  671. goto kill_rq;
  672. }
  673. if (blk_pm_request(rq))
  674. ide_check_pm_state(drive, rq);
  675. SELECT_DRIVE(drive);
  676. if (ide_wait_stat(&startstop, drive, drive->ready_stat,
  677. ATA_BUSY | ATA_DRQ, WAIT_READY)) {
  678. printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
  679. return startstop;
  680. }
  681. if (!drive->special.all) {
  682. ide_driver_t *drv;
  683. /*
  684. * We reset the drive so we need to issue a SETFEATURES.
  685. * Do it _after_ do_special() restored device parameters.
  686. */
  687. if (drive->current_speed == 0xff)
  688. ide_config_drive_speed(drive, drive->desired_speed);
  689. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  690. return execute_drive_cmd(drive, rq);
  691. else if (blk_pm_request(rq)) {
  692. struct request_pm_state *pm = rq->data;
  693. #ifdef DEBUG_PM
  694. printk("%s: start_power_step(step: %d)\n",
  695. drive->name, rq->pm->pm_step);
  696. #endif
  697. startstop = ide_start_power_step(drive, rq);
  698. if (startstop == ide_stopped &&
  699. pm->pm_step == ide_pm_state_completed)
  700. ide_complete_pm_request(drive, rq);
  701. return startstop;
  702. } else if (!rq->rq_disk && blk_special_request(rq))
  703. /*
  704. * TODO: Once all ULDs have been modified to
  705. * check for specific op codes rather than
  706. * blindly accepting any special request, the
  707. * check for ->rq_disk above may be replaced
  708. * by a more suitable mechanism or even
  709. * dropped entirely.
  710. */
  711. return ide_special_rq(drive, rq);
  712. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  713. return drv->do_request(drive, rq, rq->sector);
  714. }
  715. return do_special(drive);
  716. kill_rq:
  717. ide_kill_rq(drive, rq);
  718. return ide_stopped;
  719. }
  720. /**
  721. * ide_stall_queue - pause an IDE device
  722. * @drive: drive to stall
  723. * @timeout: time to stall for (jiffies)
  724. *
  725. * ide_stall_queue() can be used by a drive to give excess bandwidth back
  726. * to the hwgroup by sleeping for timeout jiffies.
  727. */
  728. void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
  729. {
  730. if (timeout > WAIT_WORSTCASE)
  731. timeout = WAIT_WORSTCASE;
  732. drive->sleep = timeout + jiffies;
  733. drive->sleeping = 1;
  734. }
  735. EXPORT_SYMBOL(ide_stall_queue);
  736. #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
  737. /**
  738. * choose_drive - select a drive to service
  739. * @hwgroup: hardware group to select on
  740. *
  741. * choose_drive() selects the next drive which will be serviced.
  742. * This is necessary because the IDE layer can't issue commands
  743. * to both drives on the same cable, unlike SCSI.
  744. */
  745. static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
  746. {
  747. ide_drive_t *drive, *best;
  748. repeat:
  749. best = NULL;
  750. drive = hwgroup->drive;
  751. /*
  752. * drive is doing pre-flush, ordered write, post-flush sequence. even
  753. * though that is 3 requests, it must be seen as a single transaction.
  754. * we must not preempt this drive until that is complete
  755. */
  756. if (blk_queue_flushing(drive->queue)) {
  757. /*
  758. * small race where queue could get replugged during
  759. * the 3-request flush cycle, just yank the plug since
  760. * we want it to finish asap
  761. */
  762. blk_remove_plug(drive->queue);
  763. return drive;
  764. }
  765. do {
  766. if ((!drive->sleeping || time_after_eq(jiffies, drive->sleep))
  767. && !elv_queue_empty(drive->queue)) {
  768. if (!best
  769. || (drive->sleeping && (!best->sleeping || time_before(drive->sleep, best->sleep)))
  770. || (!best->sleeping && time_before(WAKEUP(drive), WAKEUP(best))))
  771. {
  772. if (!blk_queue_plugged(drive->queue))
  773. best = drive;
  774. }
  775. }
  776. } while ((drive = drive->next) != hwgroup->drive);
  777. if (best && best->nice1 && !best->sleeping && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
  778. long t = (signed long)(WAKEUP(best) - jiffies);
  779. if (t >= WAIT_MIN_SLEEP) {
  780. /*
  781. * We *may* have some time to spare, but first let's see if
  782. * someone can potentially benefit from our nice mood today..
  783. */
  784. drive = best->next;
  785. do {
  786. if (!drive->sleeping
  787. && time_before(jiffies - best->service_time, WAKEUP(drive))
  788. && time_before(WAKEUP(drive), jiffies + t))
  789. {
  790. ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
  791. goto repeat;
  792. }
  793. } while ((drive = drive->next) != best);
  794. }
  795. }
  796. return best;
  797. }
  798. /*
  799. * Issue a new request to a drive from hwgroup
  800. * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
  801. *
  802. * A hwgroup is a serialized group of IDE interfaces. Usually there is
  803. * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
  804. * may have both interfaces in a single hwgroup to "serialize" access.
  805. * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
  806. * together into one hwgroup for serialized access.
  807. *
  808. * Note also that several hwgroups can end up sharing a single IRQ,
  809. * possibly along with many other devices. This is especially common in
  810. * PCI-based systems with off-board IDE controller cards.
  811. *
  812. * The IDE driver uses the single global ide_lock spinlock to protect
  813. * access to the request queues, and to protect the hwgroup->busy flag.
  814. *
  815. * The first thread into the driver for a particular hwgroup sets the
  816. * hwgroup->busy flag to indicate that this hwgroup is now active,
  817. * and then initiates processing of the top request from the request queue.
  818. *
  819. * Other threads attempting entry notice the busy setting, and will simply
  820. * queue their new requests and exit immediately. Note that hwgroup->busy
  821. * remains set even when the driver is merely awaiting the next interrupt.
  822. * Thus, the meaning is "this hwgroup is busy processing a request".
  823. *
  824. * When processing of a request completes, the completing thread or IRQ-handler
  825. * will start the next request from the queue. If no more work remains,
  826. * the driver will clear the hwgroup->busy flag and exit.
  827. *
  828. * The ide_lock (spinlock) is used to protect all access to the
  829. * hwgroup->busy flag, but is otherwise not needed for most processing in
  830. * the driver. This makes the driver much more friendlier to shared IRQs
  831. * than previous designs, while remaining 100% (?) SMP safe and capable.
  832. */
  833. static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
  834. {
  835. ide_drive_t *drive;
  836. ide_hwif_t *hwif;
  837. struct request *rq;
  838. ide_startstop_t startstop;
  839. int loops = 0;
  840. /* for atari only: POSSIBLY BROKEN HERE(?) */
  841. ide_get_lock(ide_intr, hwgroup);
  842. /* caller must own ide_lock */
  843. BUG_ON(!irqs_disabled());
  844. while (!hwgroup->busy) {
  845. hwgroup->busy = 1;
  846. drive = choose_drive(hwgroup);
  847. if (drive == NULL) {
  848. int sleeping = 0;
  849. unsigned long sleep = 0; /* shut up, gcc */
  850. hwgroup->rq = NULL;
  851. drive = hwgroup->drive;
  852. do {
  853. if (drive->sleeping && (!sleeping || time_before(drive->sleep, sleep))) {
  854. sleeping = 1;
  855. sleep = drive->sleep;
  856. }
  857. } while ((drive = drive->next) != hwgroup->drive);
  858. if (sleeping) {
  859. /*
  860. * Take a short snooze, and then wake up this hwgroup again.
  861. * This gives other hwgroups on the same a chance to
  862. * play fairly with us, just in case there are big differences
  863. * in relative throughputs.. don't want to hog the cpu too much.
  864. */
  865. if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
  866. sleep = jiffies + WAIT_MIN_SLEEP;
  867. #if 1
  868. if (timer_pending(&hwgroup->timer))
  869. printk(KERN_CRIT "ide_set_handler: timer already active\n");
  870. #endif
  871. /* so that ide_timer_expiry knows what to do */
  872. hwgroup->sleeping = 1;
  873. hwgroup->req_gen_timer = hwgroup->req_gen;
  874. mod_timer(&hwgroup->timer, sleep);
  875. /* we purposely leave hwgroup->busy==1
  876. * while sleeping */
  877. } else {
  878. /* Ugly, but how can we sleep for the lock
  879. * otherwise? perhaps from tq_disk?
  880. */
  881. /* for atari only */
  882. ide_release_lock();
  883. hwgroup->busy = 0;
  884. }
  885. /* no more work for this hwgroup (for now) */
  886. return;
  887. }
  888. again:
  889. hwif = HWIF(drive);
  890. if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif) {
  891. /*
  892. * set nIEN for previous hwif, drives in the
  893. * quirk_list may not like intr setups/cleanups
  894. */
  895. if (drive->quirk_list != 1)
  896. hwif->tp_ops->set_irq(hwif, 0);
  897. }
  898. hwgroup->hwif = hwif;
  899. hwgroup->drive = drive;
  900. drive->sleeping = 0;
  901. drive->service_start = jiffies;
  902. if (blk_queue_plugged(drive->queue)) {
  903. printk(KERN_ERR "ide: huh? queue was plugged!\n");
  904. break;
  905. }
  906. /*
  907. * we know that the queue isn't empty, but this can happen
  908. * if the q->prep_rq_fn() decides to kill a request
  909. */
  910. rq = elv_next_request(drive->queue);
  911. if (!rq) {
  912. hwgroup->busy = 0;
  913. break;
  914. }
  915. /*
  916. * Sanity: don't accept a request that isn't a PM request
  917. * if we are currently power managed. This is very important as
  918. * blk_stop_queue() doesn't prevent the elv_next_request()
  919. * above to return us whatever is in the queue. Since we call
  920. * ide_do_request() ourselves, we end up taking requests while
  921. * the queue is blocked...
  922. *
  923. * We let requests forced at head of queue with ide-preempt
  924. * though. I hope that doesn't happen too much, hopefully not
  925. * unless the subdriver triggers such a thing in its own PM
  926. * state machine.
  927. *
  928. * We count how many times we loop here to make sure we service
  929. * all drives in the hwgroup without looping for ever
  930. */
  931. if (drive->blocked && !blk_pm_request(rq) && !(rq->cmd_flags & REQ_PREEMPT)) {
  932. drive = drive->next ? drive->next : hwgroup->drive;
  933. if (loops++ < 4 && !blk_queue_plugged(drive->queue))
  934. goto again;
  935. /* We clear busy, there should be no pending ATA command at this point. */
  936. hwgroup->busy = 0;
  937. break;
  938. }
  939. hwgroup->rq = rq;
  940. /*
  941. * Some systems have trouble with IDE IRQs arriving while
  942. * the driver is still setting things up. So, here we disable
  943. * the IRQ used by this interface while the request is being started.
  944. * This may look bad at first, but pretty much the same thing
  945. * happens anyway when any interrupt comes in, IDE or otherwise
  946. * -- the kernel masks the IRQ while it is being handled.
  947. */
  948. if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
  949. disable_irq_nosync(hwif->irq);
  950. spin_unlock(&ide_lock);
  951. local_irq_enable_in_hardirq();
  952. /* allow other IRQs while we start this request */
  953. startstop = start_request(drive, rq);
  954. spin_lock_irq(&ide_lock);
  955. if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
  956. enable_irq(hwif->irq);
  957. if (startstop == ide_stopped)
  958. hwgroup->busy = 0;
  959. }
  960. }
  961. /*
  962. * Passes the stuff to ide_do_request
  963. */
  964. void do_ide_request(struct request_queue *q)
  965. {
  966. ide_drive_t *drive = q->queuedata;
  967. ide_do_request(HWGROUP(drive), IDE_NO_IRQ);
  968. }
  969. /*
  970. * un-busy the hwgroup etc, and clear any pending DMA status. we want to
  971. * retry the current request in pio mode instead of risking tossing it
  972. * all away
  973. */
  974. static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
  975. {
  976. ide_hwif_t *hwif = HWIF(drive);
  977. struct request *rq;
  978. ide_startstop_t ret = ide_stopped;
  979. /*
  980. * end current dma transaction
  981. */
  982. if (error < 0) {
  983. printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
  984. (void)hwif->dma_ops->dma_end(drive);
  985. ret = ide_error(drive, "dma timeout error",
  986. hwif->tp_ops->read_status(hwif));
  987. } else {
  988. printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
  989. hwif->dma_ops->dma_timeout(drive);
  990. }
  991. /*
  992. * disable dma for now, but remember that we did so because of
  993. * a timeout -- we'll reenable after we finish this next request
  994. * (or rather the first chunk of it) in pio.
  995. */
  996. drive->retry_pio++;
  997. drive->state = DMA_PIO_RETRY;
  998. ide_dma_off_quietly(drive);
  999. /*
  1000. * un-busy drive etc (hwgroup->busy is cleared on return) and
  1001. * make sure request is sane
  1002. */
  1003. rq = HWGROUP(drive)->rq;
  1004. if (!rq)
  1005. goto out;
  1006. HWGROUP(drive)->rq = NULL;
  1007. rq->errors = 0;
  1008. if (!rq->bio)
  1009. goto out;
  1010. rq->sector = rq->bio->bi_sector;
  1011. rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
  1012. rq->hard_cur_sectors = rq->current_nr_sectors;
  1013. rq->buffer = bio_data(rq->bio);
  1014. out:
  1015. return ret;
  1016. }
  1017. /**
  1018. * ide_timer_expiry - handle lack of an IDE interrupt
  1019. * @data: timer callback magic (hwgroup)
  1020. *
  1021. * An IDE command has timed out before the expected drive return
  1022. * occurred. At this point we attempt to clean up the current
  1023. * mess. If the current handler includes an expiry handler then
  1024. * we invoke the expiry handler, and providing it is happy the
  1025. * work is done. If that fails we apply generic recovery rules
  1026. * invoking the handler and checking the drive DMA status. We
  1027. * have an excessively incestuous relationship with the DMA
  1028. * logic that wants cleaning up.
  1029. */
  1030. void ide_timer_expiry (unsigned long data)
  1031. {
  1032. ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
  1033. ide_handler_t *handler;
  1034. ide_expiry_t *expiry;
  1035. unsigned long flags;
  1036. unsigned long wait = -1;
  1037. spin_lock_irqsave(&ide_lock, flags);
  1038. if (((handler = hwgroup->handler) == NULL) ||
  1039. (hwgroup->req_gen != hwgroup->req_gen_timer)) {
  1040. /*
  1041. * Either a marginal timeout occurred
  1042. * (got the interrupt just as timer expired),
  1043. * or we were "sleeping" to give other devices a chance.
  1044. * Either way, we don't really want to complain about anything.
  1045. */
  1046. if (hwgroup->sleeping) {
  1047. hwgroup->sleeping = 0;
  1048. hwgroup->busy = 0;
  1049. }
  1050. } else {
  1051. ide_drive_t *drive = hwgroup->drive;
  1052. if (!drive) {
  1053. printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
  1054. hwgroup->handler = NULL;
  1055. } else {
  1056. ide_hwif_t *hwif;
  1057. ide_startstop_t startstop = ide_stopped;
  1058. if (!hwgroup->busy) {
  1059. hwgroup->busy = 1; /* paranoia */
  1060. printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
  1061. }
  1062. if ((expiry = hwgroup->expiry) != NULL) {
  1063. /* continue */
  1064. if ((wait = expiry(drive)) > 0) {
  1065. /* reset timer */
  1066. hwgroup->timer.expires = jiffies + wait;
  1067. hwgroup->req_gen_timer = hwgroup->req_gen;
  1068. add_timer(&hwgroup->timer);
  1069. spin_unlock_irqrestore(&ide_lock, flags);
  1070. return;
  1071. }
  1072. }
  1073. hwgroup->handler = NULL;
  1074. /*
  1075. * We need to simulate a real interrupt when invoking
  1076. * the handler() function, which means we need to
  1077. * globally mask the specific IRQ:
  1078. */
  1079. spin_unlock(&ide_lock);
  1080. hwif = HWIF(drive);
  1081. /* disable_irq_nosync ?? */
  1082. disable_irq(hwif->irq);
  1083. /* local CPU only,
  1084. * as if we were handling an interrupt */
  1085. local_irq_disable();
  1086. if (hwgroup->polling) {
  1087. startstop = handler(drive);
  1088. } else if (drive_is_ready(drive)) {
  1089. if (drive->waiting_for_dma)
  1090. hwif->dma_ops->dma_lost_irq(drive);
  1091. (void)ide_ack_intr(hwif);
  1092. printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
  1093. startstop = handler(drive);
  1094. } else {
  1095. if (drive->waiting_for_dma) {
  1096. startstop = ide_dma_timeout_retry(drive, wait);
  1097. } else
  1098. startstop =
  1099. ide_error(drive, "irq timeout",
  1100. hwif->tp_ops->read_status(hwif));
  1101. }
  1102. drive->service_time = jiffies - drive->service_start;
  1103. spin_lock_irq(&ide_lock);
  1104. enable_irq(hwif->irq);
  1105. if (startstop == ide_stopped)
  1106. hwgroup->busy = 0;
  1107. }
  1108. }
  1109. ide_do_request(hwgroup, IDE_NO_IRQ);
  1110. spin_unlock_irqrestore(&ide_lock, flags);
  1111. }
  1112. /**
  1113. * unexpected_intr - handle an unexpected IDE interrupt
  1114. * @irq: interrupt line
  1115. * @hwgroup: hwgroup being processed
  1116. *
  1117. * There's nothing really useful we can do with an unexpected interrupt,
  1118. * other than reading the status register (to clear it), and logging it.
  1119. * There should be no way that an irq can happen before we're ready for it,
  1120. * so we needn't worry much about losing an "important" interrupt here.
  1121. *
  1122. * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
  1123. * the drive enters "idle", "standby", or "sleep" mode, so if the status
  1124. * looks "good", we just ignore the interrupt completely.
  1125. *
  1126. * This routine assumes __cli() is in effect when called.
  1127. *
  1128. * If an unexpected interrupt happens on irq15 while we are handling irq14
  1129. * and if the two interfaces are "serialized" (CMD640), then it looks like
  1130. * we could screw up by interfering with a new request being set up for
  1131. * irq15.
  1132. *
  1133. * In reality, this is a non-issue. The new command is not sent unless
  1134. * the drive is ready to accept one, in which case we know the drive is
  1135. * not trying to interrupt us. And ide_set_handler() is always invoked
  1136. * before completing the issuance of any new drive command, so we will not
  1137. * be accidentally invoked as a result of any valid command completion
  1138. * interrupt.
  1139. *
  1140. * Note that we must walk the entire hwgroup here. We know which hwif
  1141. * is doing the current command, but we don't know which hwif burped
  1142. * mysteriously.
  1143. */
  1144. static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
  1145. {
  1146. u8 stat;
  1147. ide_hwif_t *hwif = hwgroup->hwif;
  1148. /*
  1149. * handle the unexpected interrupt
  1150. */
  1151. do {
  1152. if (hwif->irq == irq) {
  1153. stat = hwif->tp_ops->read_status(hwif);
  1154. if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
  1155. /* Try to not flood the console with msgs */
  1156. static unsigned long last_msgtime, count;
  1157. ++count;
  1158. if (time_after(jiffies, last_msgtime + HZ)) {
  1159. last_msgtime = jiffies;
  1160. printk(KERN_ERR "%s%s: unexpected interrupt, "
  1161. "status=0x%02x, count=%ld\n",
  1162. hwif->name,
  1163. (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
  1164. }
  1165. }
  1166. }
  1167. } while ((hwif = hwif->next) != hwgroup->hwif);
  1168. }
  1169. /**
  1170. * ide_intr - default IDE interrupt handler
  1171. * @irq: interrupt number
  1172. * @dev_id: hwif group
  1173. * @regs: unused weirdness from the kernel irq layer
  1174. *
  1175. * This is the default IRQ handler for the IDE layer. You should
  1176. * not need to override it. If you do be aware it is subtle in
  1177. * places
  1178. *
  1179. * hwgroup->hwif is the interface in the group currently performing
  1180. * a command. hwgroup->drive is the drive and hwgroup->handler is
  1181. * the IRQ handler to call. As we issue a command the handlers
  1182. * step through multiple states, reassigning the handler to the
  1183. * next step in the process. Unlike a smart SCSI controller IDE
  1184. * expects the main processor to sequence the various transfer
  1185. * stages. We also manage a poll timer to catch up with most
  1186. * timeout situations. There are still a few where the handlers
  1187. * don't ever decide to give up.
  1188. *
  1189. * The handler eventually returns ide_stopped to indicate the
  1190. * request completed. At this point we issue the next request
  1191. * on the hwgroup and the process begins again.
  1192. */
  1193. irqreturn_t ide_intr (int irq, void *dev_id)
  1194. {
  1195. unsigned long flags;
  1196. ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
  1197. ide_hwif_t *hwif;
  1198. ide_drive_t *drive;
  1199. ide_handler_t *handler;
  1200. ide_startstop_t startstop;
  1201. spin_lock_irqsave(&ide_lock, flags);
  1202. hwif = hwgroup->hwif;
  1203. if (!ide_ack_intr(hwif)) {
  1204. spin_unlock_irqrestore(&ide_lock, flags);
  1205. return IRQ_NONE;
  1206. }
  1207. if ((handler = hwgroup->handler) == NULL || hwgroup->polling) {
  1208. /*
  1209. * Not expecting an interrupt from this drive.
  1210. * That means this could be:
  1211. * (1) an interrupt from another PCI device
  1212. * sharing the same PCI INT# as us.
  1213. * or (2) a drive just entered sleep or standby mode,
  1214. * and is interrupting to let us know.
  1215. * or (3) a spurious interrupt of unknown origin.
  1216. *
  1217. * For PCI, we cannot tell the difference,
  1218. * so in that case we just ignore it and hope it goes away.
  1219. *
  1220. * FIXME: unexpected_intr should be hwif-> then we can
  1221. * remove all the ifdef PCI crap
  1222. */
  1223. #ifdef CONFIG_BLK_DEV_IDEPCI
  1224. if (hwif->chipset != ide_pci)
  1225. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1226. {
  1227. /*
  1228. * Probably not a shared PCI interrupt,
  1229. * so we can safely try to do something about it:
  1230. */
  1231. unexpected_intr(irq, hwgroup);
  1232. #ifdef CONFIG_BLK_DEV_IDEPCI
  1233. } else {
  1234. /*
  1235. * Whack the status register, just in case
  1236. * we have a leftover pending IRQ.
  1237. */
  1238. (void)hwif->tp_ops->read_status(hwif);
  1239. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1240. }
  1241. spin_unlock_irqrestore(&ide_lock, flags);
  1242. return IRQ_NONE;
  1243. }
  1244. drive = hwgroup->drive;
  1245. if (!drive) {
  1246. /*
  1247. * This should NEVER happen, and there isn't much
  1248. * we could do about it here.
  1249. *
  1250. * [Note - this can occur if the drive is hot unplugged]
  1251. */
  1252. spin_unlock_irqrestore(&ide_lock, flags);
  1253. return IRQ_HANDLED;
  1254. }
  1255. if (!drive_is_ready(drive)) {
  1256. /*
  1257. * This happens regularly when we share a PCI IRQ with
  1258. * another device. Unfortunately, it can also happen
  1259. * with some buggy drives that trigger the IRQ before
  1260. * their status register is up to date. Hopefully we have
  1261. * enough advance overhead that the latter isn't a problem.
  1262. */
  1263. spin_unlock_irqrestore(&ide_lock, flags);
  1264. return IRQ_NONE;
  1265. }
  1266. if (!hwgroup->busy) {
  1267. hwgroup->busy = 1; /* paranoia */
  1268. printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
  1269. }
  1270. hwgroup->handler = NULL;
  1271. hwgroup->req_gen++;
  1272. del_timer(&hwgroup->timer);
  1273. spin_unlock(&ide_lock);
  1274. /* Some controllers might set DMA INTR no matter DMA or PIO;
  1275. * bmdma status might need to be cleared even for
  1276. * PIO interrupts to prevent spurious/lost irq.
  1277. */
  1278. if (hwif->ide_dma_clear_irq && !(drive->waiting_for_dma))
  1279. /* ide_dma_end() needs bmdma status for error checking.
  1280. * So, skip clearing bmdma status here and leave it
  1281. * to ide_dma_end() if this is dma interrupt.
  1282. */
  1283. hwif->ide_dma_clear_irq(drive);
  1284. if (drive->unmask)
  1285. local_irq_enable_in_hardirq();
  1286. /* service this interrupt, may set handler for next interrupt */
  1287. startstop = handler(drive);
  1288. spin_lock_irq(&ide_lock);
  1289. /*
  1290. * Note that handler() may have set things up for another
  1291. * interrupt to occur soon, but it cannot happen until
  1292. * we exit from this routine, because it will be the
  1293. * same irq as is currently being serviced here, and Linux
  1294. * won't allow another of the same (on any CPU) until we return.
  1295. */
  1296. drive->service_time = jiffies - drive->service_start;
  1297. if (startstop == ide_stopped) {
  1298. if (hwgroup->handler == NULL) { /* paranoia */
  1299. hwgroup->busy = 0;
  1300. ide_do_request(hwgroup, hwif->irq);
  1301. } else {
  1302. printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
  1303. "on exit\n", drive->name);
  1304. }
  1305. }
  1306. spin_unlock_irqrestore(&ide_lock, flags);
  1307. return IRQ_HANDLED;
  1308. }
  1309. /**
  1310. * ide_do_drive_cmd - issue IDE special command
  1311. * @drive: device to issue command
  1312. * @rq: request to issue
  1313. *
  1314. * This function issues a special IDE device request
  1315. * onto the request queue.
  1316. *
  1317. * the rq is queued at the head of the request queue, displacing
  1318. * the currently-being-processed request and this function
  1319. * returns immediately without waiting for the new rq to be
  1320. * completed. This is VERY DANGEROUS, and is intended for
  1321. * careful use by the ATAPI tape/cdrom driver code.
  1322. */
  1323. void ide_do_drive_cmd(ide_drive_t *drive, struct request *rq)
  1324. {
  1325. unsigned long flags;
  1326. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  1327. spin_lock_irqsave(&ide_lock, flags);
  1328. hwgroup->rq = NULL;
  1329. __elv_add_request(drive->queue, rq, ELEVATOR_INSERT_FRONT, 1);
  1330. __generic_unplug_device(drive->queue);
  1331. spin_unlock_irqrestore(&ide_lock, flags);
  1332. }
  1333. EXPORT_SYMBOL(ide_do_drive_cmd);
  1334. void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount, u8 dma)
  1335. {
  1336. ide_hwif_t *hwif = drive->hwif;
  1337. ide_task_t task;
  1338. memset(&task, 0, sizeof(task));
  1339. task.tf_flags = IDE_TFLAG_OUT_LBAH | IDE_TFLAG_OUT_LBAM |
  1340. IDE_TFLAG_OUT_FEATURE | tf_flags;
  1341. task.tf.feature = dma; /* Use PIO/DMA */
  1342. task.tf.lbam = bcount & 0xff;
  1343. task.tf.lbah = (bcount >> 8) & 0xff;
  1344. ide_tf_dump(drive->name, &task.tf);
  1345. hwif->tp_ops->set_irq(hwif, 1);
  1346. SELECT_MASK(drive, 0);
  1347. hwif->tp_ops->tf_load(drive, &task);
  1348. }
  1349. EXPORT_SYMBOL_GPL(ide_pktcmd_tf_load);
  1350. void ide_pad_transfer(ide_drive_t *drive, int write, int len)
  1351. {
  1352. ide_hwif_t *hwif = drive->hwif;
  1353. u8 buf[4] = { 0 };
  1354. while (len > 0) {
  1355. if (write)
  1356. hwif->tp_ops->output_data(drive, NULL, buf, min(4, len));
  1357. else
  1358. hwif->tp_ops->input_data(drive, NULL, buf, min(4, len));
  1359. len -= 4;
  1360. }
  1361. }
  1362. EXPORT_SYMBOL_GPL(ide_pad_transfer);