ide-io.c 51 KB

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