ide-io.c 51 KB

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