ide-io.c 49 KB

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