ide-io.c 52 KB

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