ide-io.c 48 KB

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