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