ide-io.c 51 KB

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