ide-io.c 49 KB

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