ide-io.c 50 KB

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