ide-io.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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_ops == 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. /**
  261. * ide_end_drive_cmd - end an explicit drive command
  262. * @drive: command
  263. * @stat: status bits
  264. * @err: error bits
  265. *
  266. * Clean up after success/failure of an explicit drive command.
  267. * These get thrown onto the queue so they are synchronized with
  268. * real I/O operations on the drive.
  269. *
  270. * In LBA48 mode we have to read the register set twice to get
  271. * all the extra information out.
  272. */
  273. void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
  274. {
  275. unsigned long flags;
  276. struct request *rq;
  277. spin_lock_irqsave(&ide_lock, flags);
  278. rq = HWGROUP(drive)->rq;
  279. spin_unlock_irqrestore(&ide_lock, flags);
  280. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  281. ide_task_t *task = (ide_task_t *)rq->special;
  282. if (rq->errors == 0)
  283. rq->errors = !OK_STAT(stat, READY_STAT, BAD_STAT);
  284. if (task) {
  285. struct ide_taskfile *tf = &task->tf;
  286. tf->error = err;
  287. tf->status = stat;
  288. drive->hwif->tf_read(drive, task);
  289. if (task->tf_flags & IDE_TFLAG_DYN)
  290. kfree(task);
  291. }
  292. } else if (blk_pm_request(rq)) {
  293. struct request_pm_state *pm = rq->data;
  294. #ifdef DEBUG_PM
  295. printk("%s: complete_power_step(step: %d, stat: %x, err: %x)\n",
  296. drive->name, rq->pm->pm_step, stat, err);
  297. #endif
  298. ide_complete_power_step(drive, rq, stat, err);
  299. if (pm->pm_step == ide_pm_state_completed)
  300. ide_complete_pm_request(drive, rq);
  301. return;
  302. }
  303. spin_lock_irqsave(&ide_lock, flags);
  304. HWGROUP(drive)->rq = NULL;
  305. rq->errors = err;
  306. if (unlikely(__blk_end_request(rq, (rq->errors ? -EIO : 0),
  307. blk_rq_bytes(rq))))
  308. BUG();
  309. spin_unlock_irqrestore(&ide_lock, flags);
  310. }
  311. EXPORT_SYMBOL(ide_end_drive_cmd);
  312. static void ide_kill_rq(ide_drive_t *drive, struct request *rq)
  313. {
  314. if (rq->rq_disk) {
  315. ide_driver_t *drv;
  316. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  317. drv->end_request(drive, 0, 0);
  318. } else
  319. ide_end_request(drive, 0, 0);
  320. }
  321. static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  322. {
  323. ide_hwif_t *hwif = drive->hwif;
  324. if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
  325. /* other bits are useless when BUSY */
  326. rq->errors |= ERROR_RESET;
  327. } else if (stat & ERR_STAT) {
  328. /* err has different meaning on cdrom and tape */
  329. if (err == ABRT_ERR) {
  330. if (drive->select.b.lba &&
  331. /* some newer drives don't support WIN_SPECIFY */
  332. hwif->INB(hwif->io_ports.command_addr) ==
  333. WIN_SPECIFY)
  334. return ide_stopped;
  335. } else if ((err & BAD_CRC) == BAD_CRC) {
  336. /* UDMA crc error, just retry the operation */
  337. drive->crc_count++;
  338. } else if (err & (BBD_ERR | ECC_ERR)) {
  339. /* retries won't help these */
  340. rq->errors = ERROR_MAX;
  341. } else if (err & TRK0_ERR) {
  342. /* help it find track zero */
  343. rq->errors |= ERROR_RECAL;
  344. }
  345. }
  346. if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ &&
  347. (hwif->host_flags & IDE_HFLAG_ERROR_STOPS_FIFO) == 0) {
  348. int nsect = drive->mult_count ? drive->mult_count : 1;
  349. ide_pad_transfer(drive, READ, nsect * SECTOR_SIZE);
  350. }
  351. if (rq->errors >= ERROR_MAX || blk_noretry_request(rq)) {
  352. ide_kill_rq(drive, rq);
  353. return ide_stopped;
  354. }
  355. if (ide_read_status(drive) & (BUSY_STAT | DRQ_STAT))
  356. rq->errors |= ERROR_RESET;
  357. if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
  358. ++rq->errors;
  359. return ide_do_reset(drive);
  360. }
  361. if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
  362. drive->special.b.recalibrate = 1;
  363. ++rq->errors;
  364. return ide_stopped;
  365. }
  366. static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  367. {
  368. ide_hwif_t *hwif = drive->hwif;
  369. if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
  370. /* other bits are useless when BUSY */
  371. rq->errors |= ERROR_RESET;
  372. } else {
  373. /* add decoding error stuff */
  374. }
  375. if (ide_read_status(drive) & (BUSY_STAT | DRQ_STAT))
  376. /* force an abort */
  377. hwif->OUTBSYNC(hwif, WIN_IDLEIMMEDIATE,
  378. hwif->io_ports.command_addr);
  379. if (rq->errors >= ERROR_MAX) {
  380. ide_kill_rq(drive, rq);
  381. } else {
  382. if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
  383. ++rq->errors;
  384. return ide_do_reset(drive);
  385. }
  386. ++rq->errors;
  387. }
  388. return ide_stopped;
  389. }
  390. ide_startstop_t
  391. __ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  392. {
  393. if (drive->media == ide_disk)
  394. return ide_ata_error(drive, rq, stat, err);
  395. return ide_atapi_error(drive, rq, stat, err);
  396. }
  397. EXPORT_SYMBOL_GPL(__ide_error);
  398. /**
  399. * ide_error - handle an error on the IDE
  400. * @drive: drive the error occurred on
  401. * @msg: message to report
  402. * @stat: status bits
  403. *
  404. * ide_error() takes action based on the error returned by the drive.
  405. * For normal I/O that may well include retries. We deal with
  406. * both new-style (taskfile) and old style command handling here.
  407. * In the case of taskfile command handling there is work left to
  408. * do
  409. */
  410. ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
  411. {
  412. struct request *rq;
  413. u8 err;
  414. err = ide_dump_status(drive, msg, stat);
  415. if ((rq = HWGROUP(drive)->rq) == NULL)
  416. return ide_stopped;
  417. /* retry only "normal" I/O: */
  418. if (!blk_fs_request(rq)) {
  419. rq->errors = 1;
  420. ide_end_drive_cmd(drive, stat, err);
  421. return ide_stopped;
  422. }
  423. if (rq->rq_disk) {
  424. ide_driver_t *drv;
  425. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  426. return drv->error(drive, rq, stat, err);
  427. } else
  428. return __ide_error(drive, rq, stat, err);
  429. }
  430. EXPORT_SYMBOL_GPL(ide_error);
  431. static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  432. {
  433. tf->nsect = drive->sect;
  434. tf->lbal = drive->sect;
  435. tf->lbam = drive->cyl;
  436. tf->lbah = drive->cyl >> 8;
  437. tf->device = ((drive->head - 1) | drive->select.all) & ~ATA_LBA;
  438. tf->command = WIN_SPECIFY;
  439. }
  440. static void ide_tf_set_restore_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  441. {
  442. tf->nsect = drive->sect;
  443. tf->command = WIN_RESTORE;
  444. }
  445. static void ide_tf_set_setmult_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  446. {
  447. tf->nsect = drive->mult_req;
  448. tf->command = WIN_SETMULT;
  449. }
  450. static ide_startstop_t ide_disk_special(ide_drive_t *drive)
  451. {
  452. special_t *s = &drive->special;
  453. ide_task_t args;
  454. memset(&args, 0, sizeof(ide_task_t));
  455. args.data_phase = TASKFILE_NO_DATA;
  456. if (s->b.set_geometry) {
  457. s->b.set_geometry = 0;
  458. ide_tf_set_specify_cmd(drive, &args.tf);
  459. } else if (s->b.recalibrate) {
  460. s->b.recalibrate = 0;
  461. ide_tf_set_restore_cmd(drive, &args.tf);
  462. } else if (s->b.set_multmode) {
  463. s->b.set_multmode = 0;
  464. if (drive->mult_req > drive->id->max_multsect)
  465. drive->mult_req = drive->id->max_multsect;
  466. ide_tf_set_setmult_cmd(drive, &args.tf);
  467. } else if (s->all) {
  468. int special = s->all;
  469. s->all = 0;
  470. printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
  471. return ide_stopped;
  472. }
  473. args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE |
  474. IDE_TFLAG_CUSTOM_HANDLER;
  475. do_rw_taskfile(drive, &args);
  476. return ide_started;
  477. }
  478. /*
  479. * handle HDIO_SET_PIO_MODE ioctl abusers here, eventually it will go away
  480. */
  481. static int set_pio_mode_abuse(ide_hwif_t *hwif, u8 req_pio)
  482. {
  483. switch (req_pio) {
  484. case 202:
  485. case 201:
  486. case 200:
  487. case 102:
  488. case 101:
  489. case 100:
  490. return (hwif->host_flags & IDE_HFLAG_ABUSE_DMA_MODES) ? 1 : 0;
  491. case 9:
  492. case 8:
  493. return (hwif->host_flags & IDE_HFLAG_ABUSE_PREFETCH) ? 1 : 0;
  494. case 7:
  495. case 6:
  496. return (hwif->host_flags & IDE_HFLAG_ABUSE_FAST_DEVSEL) ? 1 : 0;
  497. default:
  498. return 0;
  499. }
  500. }
  501. /**
  502. * do_special - issue some special commands
  503. * @drive: drive the command is for
  504. *
  505. * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
  506. * commands to a drive. It used to do much more, but has been scaled
  507. * back.
  508. */
  509. static ide_startstop_t do_special (ide_drive_t *drive)
  510. {
  511. special_t *s = &drive->special;
  512. #ifdef DEBUG
  513. printk("%s: do_special: 0x%02x\n", drive->name, s->all);
  514. #endif
  515. if (s->b.set_tune) {
  516. ide_hwif_t *hwif = drive->hwif;
  517. const struct ide_port_ops *port_ops = hwif->port_ops;
  518. u8 req_pio = drive->tune_req;
  519. s->b.set_tune = 0;
  520. if (set_pio_mode_abuse(drive->hwif, req_pio)) {
  521. /*
  522. * take ide_lock for drive->[no_]unmask/[no_]io_32bit
  523. */
  524. if (req_pio == 8 || req_pio == 9) {
  525. unsigned long flags;
  526. spin_lock_irqsave(&ide_lock, flags);
  527. port_ops->set_pio_mode(drive, req_pio);
  528. spin_unlock_irqrestore(&ide_lock, flags);
  529. } else
  530. port_ops->set_pio_mode(drive, req_pio);
  531. } else {
  532. int keep_dma = drive->using_dma;
  533. ide_set_pio(drive, req_pio);
  534. if (hwif->host_flags & IDE_HFLAG_SET_PIO_MODE_KEEP_DMA) {
  535. if (keep_dma)
  536. ide_dma_on(drive);
  537. }
  538. }
  539. return ide_stopped;
  540. } else {
  541. if (drive->media == ide_disk)
  542. return ide_disk_special(drive);
  543. s->all = 0;
  544. drive->mult_req = 0;
  545. return ide_stopped;
  546. }
  547. }
  548. void ide_map_sg(ide_drive_t *drive, struct request *rq)
  549. {
  550. ide_hwif_t *hwif = drive->hwif;
  551. struct scatterlist *sg = hwif->sg_table;
  552. if (hwif->sg_mapped) /* needed by ide-scsi */
  553. return;
  554. if (rq->cmd_type != REQ_TYPE_ATA_TASKFILE) {
  555. hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
  556. } else {
  557. sg_init_one(sg, rq->buffer, rq->nr_sectors * SECTOR_SIZE);
  558. hwif->sg_nents = 1;
  559. }
  560. }
  561. EXPORT_SYMBOL_GPL(ide_map_sg);
  562. void ide_init_sg_cmd(ide_drive_t *drive, struct request *rq)
  563. {
  564. ide_hwif_t *hwif = drive->hwif;
  565. hwif->nsect = hwif->nleft = rq->nr_sectors;
  566. hwif->cursg_ofs = 0;
  567. hwif->cursg = NULL;
  568. }
  569. EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
  570. /**
  571. * execute_drive_command - issue special drive command
  572. * @drive: the drive to issue the command on
  573. * @rq: the request structure holding the command
  574. *
  575. * execute_drive_cmd() issues a special drive command, usually
  576. * initiated by ioctl() from the external hdparm program. The
  577. * command can be a drive command, drive task or taskfile
  578. * operation. Weirdly you can call it with NULL to wait for
  579. * all commands to finish. Don't do this as that is due to change
  580. */
  581. static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
  582. struct request *rq)
  583. {
  584. ide_hwif_t *hwif = HWIF(drive);
  585. ide_task_t *task = rq->special;
  586. if (task) {
  587. hwif->data_phase = task->data_phase;
  588. switch (hwif->data_phase) {
  589. case TASKFILE_MULTI_OUT:
  590. case TASKFILE_OUT:
  591. case TASKFILE_MULTI_IN:
  592. case TASKFILE_IN:
  593. ide_init_sg_cmd(drive, rq);
  594. ide_map_sg(drive, rq);
  595. default:
  596. break;
  597. }
  598. return do_rw_taskfile(drive, task);
  599. }
  600. /*
  601. * NULL is actually a valid way of waiting for
  602. * all current requests to be flushed from the queue.
  603. */
  604. #ifdef DEBUG
  605. printk("%s: DRIVE_CMD (null)\n", drive->name);
  606. #endif
  607. ide_end_drive_cmd(drive, ide_read_status(drive), ide_read_error(drive));
  608. return ide_stopped;
  609. }
  610. static ide_startstop_t ide_special_rq(ide_drive_t *drive, struct request *rq)
  611. {
  612. switch (rq->cmd[0]) {
  613. case REQ_DRIVE_RESET:
  614. return ide_do_reset(drive);
  615. default:
  616. blk_dump_rq_flags(rq, "ide_special_rq - bad request");
  617. ide_end_request(drive, 0, 0);
  618. return ide_stopped;
  619. }
  620. }
  621. static void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
  622. {
  623. struct request_pm_state *pm = rq->data;
  624. if (blk_pm_suspend_request(rq) &&
  625. pm->pm_step == ide_pm_state_start_suspend)
  626. /* Mark drive blocked when starting the suspend sequence. */
  627. drive->blocked = 1;
  628. else if (blk_pm_resume_request(rq) &&
  629. pm->pm_step == ide_pm_state_start_resume) {
  630. /*
  631. * The first thing we do on wakeup is to wait for BSY bit to
  632. * go away (with a looong timeout) as a drive on this hwif may
  633. * just be POSTing itself.
  634. * We do that before even selecting as the "other" device on
  635. * the bus may be broken enough to walk on our toes at this
  636. * point.
  637. */
  638. int rc;
  639. #ifdef DEBUG_PM
  640. printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
  641. #endif
  642. rc = ide_wait_not_busy(HWIF(drive), 35000);
  643. if (rc)
  644. printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
  645. SELECT_DRIVE(drive);
  646. ide_set_irq(drive, 1);
  647. rc = ide_wait_not_busy(HWIF(drive), 100000);
  648. if (rc)
  649. printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
  650. }
  651. }
  652. /**
  653. * start_request - start of I/O and command issuing for IDE
  654. *
  655. * start_request() initiates handling of a new I/O request. It
  656. * accepts commands and I/O (read/write) requests. It also does
  657. * the final remapping for weird stuff like EZDrive. Once
  658. * device mapper can work sector level the EZDrive stuff can go away
  659. *
  660. * FIXME: this function needs a rename
  661. */
  662. static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
  663. {
  664. ide_startstop_t startstop;
  665. sector_t block;
  666. BUG_ON(!blk_rq_started(rq));
  667. #ifdef DEBUG
  668. printk("%s: start_request: current=0x%08lx\n",
  669. HWIF(drive)->name, (unsigned long) rq);
  670. #endif
  671. /* bail early if we've exceeded max_failures */
  672. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  673. rq->cmd_flags |= REQ_FAILED;
  674. goto kill_rq;
  675. }
  676. block = rq->sector;
  677. if (blk_fs_request(rq) &&
  678. (drive->media == ide_disk || drive->media == ide_floppy)) {
  679. block += drive->sect0;
  680. }
  681. /* Yecch - this will shift the entire interval,
  682. possibly killing some innocent following sector */
  683. if (block == 0 && drive->remap_0_to_1 == 1)
  684. block = 1; /* redirect MBR access to EZ-Drive partn table */
  685. if (blk_pm_request(rq))
  686. ide_check_pm_state(drive, rq);
  687. SELECT_DRIVE(drive);
  688. if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
  689. printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
  690. return startstop;
  691. }
  692. if (!drive->special.all) {
  693. ide_driver_t *drv;
  694. /*
  695. * We reset the drive so we need to issue a SETFEATURES.
  696. * Do it _after_ do_special() restored device parameters.
  697. */
  698. if (drive->current_speed == 0xff)
  699. ide_config_drive_speed(drive, drive->desired_speed);
  700. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  701. return execute_drive_cmd(drive, rq);
  702. else if (blk_pm_request(rq)) {
  703. struct request_pm_state *pm = rq->data;
  704. #ifdef DEBUG_PM
  705. printk("%s: start_power_step(step: %d)\n",
  706. drive->name, rq->pm->pm_step);
  707. #endif
  708. startstop = ide_start_power_step(drive, rq);
  709. if (startstop == ide_stopped &&
  710. pm->pm_step == ide_pm_state_completed)
  711. ide_complete_pm_request(drive, rq);
  712. return startstop;
  713. } else if (!rq->rq_disk && blk_special_request(rq))
  714. /*
  715. * TODO: Once all ULDs have been modified to
  716. * check for specific op codes rather than
  717. * blindly accepting any special request, the
  718. * check for ->rq_disk above may be replaced
  719. * by a more suitable mechanism or even
  720. * dropped entirely.
  721. */
  722. return ide_special_rq(drive, rq);
  723. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  724. return drv->do_request(drive, rq, block);
  725. }
  726. return do_special(drive);
  727. kill_rq:
  728. ide_kill_rq(drive, rq);
  729. return ide_stopped;
  730. }
  731. /**
  732. * ide_stall_queue - pause an IDE device
  733. * @drive: drive to stall
  734. * @timeout: time to stall for (jiffies)
  735. *
  736. * ide_stall_queue() can be used by a drive to give excess bandwidth back
  737. * to the hwgroup by sleeping for timeout jiffies.
  738. */
  739. void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
  740. {
  741. if (timeout > WAIT_WORSTCASE)
  742. timeout = WAIT_WORSTCASE;
  743. drive->sleep = timeout + jiffies;
  744. drive->sleeping = 1;
  745. }
  746. EXPORT_SYMBOL(ide_stall_queue);
  747. #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
  748. /**
  749. * choose_drive - select a drive to service
  750. * @hwgroup: hardware group to select on
  751. *
  752. * choose_drive() selects the next drive which will be serviced.
  753. * This is necessary because the IDE layer can't issue commands
  754. * to both drives on the same cable, unlike SCSI.
  755. */
  756. static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
  757. {
  758. ide_drive_t *drive, *best;
  759. repeat:
  760. best = NULL;
  761. drive = hwgroup->drive;
  762. /*
  763. * drive is doing pre-flush, ordered write, post-flush sequence. even
  764. * though that is 3 requests, it must be seen as a single transaction.
  765. * we must not preempt this drive until that is complete
  766. */
  767. if (blk_queue_flushing(drive->queue)) {
  768. /*
  769. * small race where queue could get replugged during
  770. * the 3-request flush cycle, just yank the plug since
  771. * we want it to finish asap
  772. */
  773. blk_remove_plug(drive->queue);
  774. return drive;
  775. }
  776. do {
  777. if ((!drive->sleeping || time_after_eq(jiffies, drive->sleep))
  778. && !elv_queue_empty(drive->queue)) {
  779. if (!best
  780. || (drive->sleeping && (!best->sleeping || time_before(drive->sleep, best->sleep)))
  781. || (!best->sleeping && time_before(WAKEUP(drive), WAKEUP(best))))
  782. {
  783. if (!blk_queue_plugged(drive->queue))
  784. best = drive;
  785. }
  786. }
  787. } while ((drive = drive->next) != hwgroup->drive);
  788. if (best && best->nice1 && !best->sleeping && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
  789. long t = (signed long)(WAKEUP(best) - jiffies);
  790. if (t >= WAIT_MIN_SLEEP) {
  791. /*
  792. * We *may* have some time to spare, but first let's see if
  793. * someone can potentially benefit from our nice mood today..
  794. */
  795. drive = best->next;
  796. do {
  797. if (!drive->sleeping
  798. && time_before(jiffies - best->service_time, WAKEUP(drive))
  799. && time_before(WAKEUP(drive), jiffies + t))
  800. {
  801. ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
  802. goto repeat;
  803. }
  804. } while ((drive = drive->next) != best);
  805. }
  806. }
  807. return best;
  808. }
  809. /*
  810. * Issue a new request to a drive from hwgroup
  811. * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
  812. *
  813. * A hwgroup is a serialized group of IDE interfaces. Usually there is
  814. * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
  815. * may have both interfaces in a single hwgroup to "serialize" access.
  816. * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
  817. * together into one hwgroup for serialized access.
  818. *
  819. * Note also that several hwgroups can end up sharing a single IRQ,
  820. * possibly along with many other devices. This is especially common in
  821. * PCI-based systems with off-board IDE controller cards.
  822. *
  823. * The IDE driver uses the single global ide_lock spinlock to protect
  824. * access to the request queues, and to protect the hwgroup->busy flag.
  825. *
  826. * The first thread into the driver for a particular hwgroup sets the
  827. * hwgroup->busy flag to indicate that this hwgroup is now active,
  828. * and then initiates processing of the top request from the request queue.
  829. *
  830. * Other threads attempting entry notice the busy setting, and will simply
  831. * queue their new requests and exit immediately. Note that hwgroup->busy
  832. * remains set even when the driver is merely awaiting the next interrupt.
  833. * Thus, the meaning is "this hwgroup is busy processing a request".
  834. *
  835. * When processing of a request completes, the completing thread or IRQ-handler
  836. * will start the next request from the queue. If no more work remains,
  837. * the driver will clear the hwgroup->busy flag and exit.
  838. *
  839. * The ide_lock (spinlock) is used to protect all access to the
  840. * hwgroup->busy flag, but is otherwise not needed for most processing in
  841. * the driver. This makes the driver much more friendlier to shared IRQs
  842. * than previous designs, while remaining 100% (?) SMP safe and capable.
  843. */
  844. static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
  845. {
  846. ide_drive_t *drive;
  847. ide_hwif_t *hwif;
  848. struct request *rq;
  849. ide_startstop_t startstop;
  850. int loops = 0;
  851. /* for atari only: POSSIBLY BROKEN HERE(?) */
  852. ide_get_lock(ide_intr, hwgroup);
  853. /* caller must own ide_lock */
  854. BUG_ON(!irqs_disabled());
  855. while (!hwgroup->busy) {
  856. hwgroup->busy = 1;
  857. drive = choose_drive(hwgroup);
  858. if (drive == NULL) {
  859. int sleeping = 0;
  860. unsigned long sleep = 0; /* shut up, gcc */
  861. hwgroup->rq = NULL;
  862. drive = hwgroup->drive;
  863. do {
  864. if (drive->sleeping && (!sleeping || time_before(drive->sleep, sleep))) {
  865. sleeping = 1;
  866. sleep = drive->sleep;
  867. }
  868. } while ((drive = drive->next) != hwgroup->drive);
  869. if (sleeping) {
  870. /*
  871. * Take a short snooze, and then wake up this hwgroup again.
  872. * This gives other hwgroups on the same a chance to
  873. * play fairly with us, just in case there are big differences
  874. * in relative throughputs.. don't want to hog the cpu too much.
  875. */
  876. if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
  877. sleep = jiffies + WAIT_MIN_SLEEP;
  878. #if 1
  879. if (timer_pending(&hwgroup->timer))
  880. printk(KERN_CRIT "ide_set_handler: timer already active\n");
  881. #endif
  882. /* so that ide_timer_expiry knows what to do */
  883. hwgroup->sleeping = 1;
  884. hwgroup->req_gen_timer = hwgroup->req_gen;
  885. mod_timer(&hwgroup->timer, sleep);
  886. /* we purposely leave hwgroup->busy==1
  887. * while sleeping */
  888. } else {
  889. /* Ugly, but how can we sleep for the lock
  890. * otherwise? perhaps from tq_disk?
  891. */
  892. /* for atari only */
  893. ide_release_lock();
  894. hwgroup->busy = 0;
  895. }
  896. /* no more work for this hwgroup (for now) */
  897. return;
  898. }
  899. again:
  900. hwif = HWIF(drive);
  901. if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif) {
  902. /*
  903. * set nIEN for previous hwif, drives in the
  904. * quirk_list may not like intr setups/cleanups
  905. */
  906. if (drive->quirk_list != 1)
  907. ide_set_irq(drive, 0);
  908. }
  909. hwgroup->hwif = hwif;
  910. hwgroup->drive = drive;
  911. drive->sleeping = 0;
  912. drive->service_start = jiffies;
  913. if (blk_queue_plugged(drive->queue)) {
  914. printk(KERN_ERR "ide: huh? queue was plugged!\n");
  915. break;
  916. }
  917. /*
  918. * we know that the queue isn't empty, but this can happen
  919. * if the q->prep_rq_fn() decides to kill a request
  920. */
  921. rq = elv_next_request(drive->queue);
  922. if (!rq) {
  923. hwgroup->busy = 0;
  924. break;
  925. }
  926. /*
  927. * Sanity: don't accept a request that isn't a PM request
  928. * if we are currently power managed. This is very important as
  929. * blk_stop_queue() doesn't prevent the elv_next_request()
  930. * above to return us whatever is in the queue. Since we call
  931. * ide_do_request() ourselves, we end up taking requests while
  932. * the queue is blocked...
  933. *
  934. * We let requests forced at head of queue with ide-preempt
  935. * though. I hope that doesn't happen too much, hopefully not
  936. * unless the subdriver triggers such a thing in its own PM
  937. * state machine.
  938. *
  939. * We count how many times we loop here to make sure we service
  940. * all drives in the hwgroup without looping for ever
  941. */
  942. if (drive->blocked && !blk_pm_request(rq) && !(rq->cmd_flags & REQ_PREEMPT)) {
  943. drive = drive->next ? drive->next : hwgroup->drive;
  944. if (loops++ < 4 && !blk_queue_plugged(drive->queue))
  945. goto again;
  946. /* We clear busy, there should be no pending ATA command at this point. */
  947. hwgroup->busy = 0;
  948. break;
  949. }
  950. hwgroup->rq = rq;
  951. /*
  952. * Some systems have trouble with IDE IRQs arriving while
  953. * the driver is still setting things up. So, here we disable
  954. * the IRQ used by this interface while the request is being started.
  955. * This may look bad at first, but pretty much the same thing
  956. * happens anyway when any interrupt comes in, IDE or otherwise
  957. * -- the kernel masks the IRQ while it is being handled.
  958. */
  959. if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
  960. disable_irq_nosync(hwif->irq);
  961. spin_unlock(&ide_lock);
  962. local_irq_enable_in_hardirq();
  963. /* allow other IRQs while we start this request */
  964. startstop = start_request(drive, rq);
  965. spin_lock_irq(&ide_lock);
  966. if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
  967. enable_irq(hwif->irq);
  968. if (startstop == ide_stopped)
  969. hwgroup->busy = 0;
  970. }
  971. }
  972. /*
  973. * Passes the stuff to ide_do_request
  974. */
  975. void do_ide_request(struct request_queue *q)
  976. {
  977. ide_drive_t *drive = q->queuedata;
  978. ide_do_request(HWGROUP(drive), IDE_NO_IRQ);
  979. }
  980. /*
  981. * un-busy the hwgroup etc, and clear any pending DMA status. we want to
  982. * retry the current request in pio mode instead of risking tossing it
  983. * all away
  984. */
  985. static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
  986. {
  987. ide_hwif_t *hwif = HWIF(drive);
  988. struct request *rq;
  989. ide_startstop_t ret = ide_stopped;
  990. /*
  991. * end current dma transaction
  992. */
  993. if (error < 0) {
  994. printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
  995. (void)hwif->dma_ops->dma_end(drive);
  996. ret = ide_error(drive, "dma timeout error",
  997. ide_read_status(drive));
  998. } else {
  999. printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
  1000. hwif->dma_ops->dma_timeout(drive);
  1001. }
  1002. /*
  1003. * disable dma for now, but remember that we did so because of
  1004. * a timeout -- we'll reenable after we finish this next request
  1005. * (or rather the first chunk of it) in pio.
  1006. */
  1007. drive->retry_pio++;
  1008. drive->state = DMA_PIO_RETRY;
  1009. ide_dma_off_quietly(drive);
  1010. /*
  1011. * un-busy drive etc (hwgroup->busy is cleared on return) and
  1012. * make sure request is sane
  1013. */
  1014. rq = HWGROUP(drive)->rq;
  1015. if (!rq)
  1016. goto out;
  1017. HWGROUP(drive)->rq = NULL;
  1018. rq->errors = 0;
  1019. if (!rq->bio)
  1020. goto out;
  1021. rq->sector = rq->bio->bi_sector;
  1022. rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
  1023. rq->hard_cur_sectors = rq->current_nr_sectors;
  1024. rq->buffer = bio_data(rq->bio);
  1025. out:
  1026. return ret;
  1027. }
  1028. /**
  1029. * ide_timer_expiry - handle lack of an IDE interrupt
  1030. * @data: timer callback magic (hwgroup)
  1031. *
  1032. * An IDE command has timed out before the expected drive return
  1033. * occurred. At this point we attempt to clean up the current
  1034. * mess. If the current handler includes an expiry handler then
  1035. * we invoke the expiry handler, and providing it is happy the
  1036. * work is done. If that fails we apply generic recovery rules
  1037. * invoking the handler and checking the drive DMA status. We
  1038. * have an excessively incestuous relationship with the DMA
  1039. * logic that wants cleaning up.
  1040. */
  1041. void ide_timer_expiry (unsigned long data)
  1042. {
  1043. ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
  1044. ide_handler_t *handler;
  1045. ide_expiry_t *expiry;
  1046. unsigned long flags;
  1047. unsigned long wait = -1;
  1048. spin_lock_irqsave(&ide_lock, flags);
  1049. if (((handler = hwgroup->handler) == NULL) ||
  1050. (hwgroup->req_gen != hwgroup->req_gen_timer)) {
  1051. /*
  1052. * Either a marginal timeout occurred
  1053. * (got the interrupt just as timer expired),
  1054. * or we were "sleeping" to give other devices a chance.
  1055. * Either way, we don't really want to complain about anything.
  1056. */
  1057. if (hwgroup->sleeping) {
  1058. hwgroup->sleeping = 0;
  1059. hwgroup->busy = 0;
  1060. }
  1061. } else {
  1062. ide_drive_t *drive = hwgroup->drive;
  1063. if (!drive) {
  1064. printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
  1065. hwgroup->handler = NULL;
  1066. } else {
  1067. ide_hwif_t *hwif;
  1068. ide_startstop_t startstop = ide_stopped;
  1069. if (!hwgroup->busy) {
  1070. hwgroup->busy = 1; /* paranoia */
  1071. printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
  1072. }
  1073. if ((expiry = hwgroup->expiry) != NULL) {
  1074. /* continue */
  1075. if ((wait = expiry(drive)) > 0) {
  1076. /* reset timer */
  1077. hwgroup->timer.expires = jiffies + wait;
  1078. hwgroup->req_gen_timer = hwgroup->req_gen;
  1079. add_timer(&hwgroup->timer);
  1080. spin_unlock_irqrestore(&ide_lock, flags);
  1081. return;
  1082. }
  1083. }
  1084. hwgroup->handler = NULL;
  1085. /*
  1086. * We need to simulate a real interrupt when invoking
  1087. * the handler() function, which means we need to
  1088. * globally mask the specific IRQ:
  1089. */
  1090. spin_unlock(&ide_lock);
  1091. hwif = HWIF(drive);
  1092. /* disable_irq_nosync ?? */
  1093. disable_irq(hwif->irq);
  1094. /* local CPU only,
  1095. * as if we were handling an interrupt */
  1096. local_irq_disable();
  1097. if (hwgroup->polling) {
  1098. startstop = handler(drive);
  1099. } else if (drive_is_ready(drive)) {
  1100. if (drive->waiting_for_dma)
  1101. hwif->dma_ops->dma_lost_irq(drive);
  1102. (void)ide_ack_intr(hwif);
  1103. printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
  1104. startstop = handler(drive);
  1105. } else {
  1106. if (drive->waiting_for_dma) {
  1107. startstop = ide_dma_timeout_retry(drive, wait);
  1108. } else
  1109. startstop =
  1110. ide_error(drive, "irq timeout",
  1111. ide_read_status(drive));
  1112. }
  1113. drive->service_time = jiffies - drive->service_start;
  1114. spin_lock_irq(&ide_lock);
  1115. enable_irq(hwif->irq);
  1116. if (startstop == ide_stopped)
  1117. hwgroup->busy = 0;
  1118. }
  1119. }
  1120. ide_do_request(hwgroup, IDE_NO_IRQ);
  1121. spin_unlock_irqrestore(&ide_lock, flags);
  1122. }
  1123. /**
  1124. * unexpected_intr - handle an unexpected IDE interrupt
  1125. * @irq: interrupt line
  1126. * @hwgroup: hwgroup being processed
  1127. *
  1128. * There's nothing really useful we can do with an unexpected interrupt,
  1129. * other than reading the status register (to clear it), and logging it.
  1130. * There should be no way that an irq can happen before we're ready for it,
  1131. * so we needn't worry much about losing an "important" interrupt here.
  1132. *
  1133. * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
  1134. * the drive enters "idle", "standby", or "sleep" mode, so if the status
  1135. * looks "good", we just ignore the interrupt completely.
  1136. *
  1137. * This routine assumes __cli() is in effect when called.
  1138. *
  1139. * If an unexpected interrupt happens on irq15 while we are handling irq14
  1140. * and if the two interfaces are "serialized" (CMD640), then it looks like
  1141. * we could screw up by interfering with a new request being set up for
  1142. * irq15.
  1143. *
  1144. * In reality, this is a non-issue. The new command is not sent unless
  1145. * the drive is ready to accept one, in which case we know the drive is
  1146. * not trying to interrupt us. And ide_set_handler() is always invoked
  1147. * before completing the issuance of any new drive command, so we will not
  1148. * be accidentally invoked as a result of any valid command completion
  1149. * interrupt.
  1150. *
  1151. * Note that we must walk the entire hwgroup here. We know which hwif
  1152. * is doing the current command, but we don't know which hwif burped
  1153. * mysteriously.
  1154. */
  1155. static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
  1156. {
  1157. u8 stat;
  1158. ide_hwif_t *hwif = hwgroup->hwif;
  1159. /*
  1160. * handle the unexpected interrupt
  1161. */
  1162. do {
  1163. if (hwif->irq == irq) {
  1164. stat = hwif->INB(hwif->io_ports.status_addr);
  1165. if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
  1166. /* Try to not flood the console with msgs */
  1167. static unsigned long last_msgtime, count;
  1168. ++count;
  1169. if (time_after(jiffies, last_msgtime + HZ)) {
  1170. last_msgtime = jiffies;
  1171. printk(KERN_ERR "%s%s: unexpected interrupt, "
  1172. "status=0x%02x, count=%ld\n",
  1173. hwif->name,
  1174. (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
  1175. }
  1176. }
  1177. }
  1178. } while ((hwif = hwif->next) != hwgroup->hwif);
  1179. }
  1180. /**
  1181. * ide_intr - default IDE interrupt handler
  1182. * @irq: interrupt number
  1183. * @dev_id: hwif group
  1184. * @regs: unused weirdness from the kernel irq layer
  1185. *
  1186. * This is the default IRQ handler for the IDE layer. You should
  1187. * not need to override it. If you do be aware it is subtle in
  1188. * places
  1189. *
  1190. * hwgroup->hwif is the interface in the group currently performing
  1191. * a command. hwgroup->drive is the drive and hwgroup->handler is
  1192. * the IRQ handler to call. As we issue a command the handlers
  1193. * step through multiple states, reassigning the handler to the
  1194. * next step in the process. Unlike a smart SCSI controller IDE
  1195. * expects the main processor to sequence the various transfer
  1196. * stages. We also manage a poll timer to catch up with most
  1197. * timeout situations. There are still a few where the handlers
  1198. * don't ever decide to give up.
  1199. *
  1200. * The handler eventually returns ide_stopped to indicate the
  1201. * request completed. At this point we issue the next request
  1202. * on the hwgroup and the process begins again.
  1203. */
  1204. irqreturn_t ide_intr (int irq, void *dev_id)
  1205. {
  1206. unsigned long flags;
  1207. ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
  1208. ide_hwif_t *hwif;
  1209. ide_drive_t *drive;
  1210. ide_handler_t *handler;
  1211. ide_startstop_t startstop;
  1212. spin_lock_irqsave(&ide_lock, flags);
  1213. hwif = hwgroup->hwif;
  1214. if (!ide_ack_intr(hwif)) {
  1215. spin_unlock_irqrestore(&ide_lock, flags);
  1216. return IRQ_NONE;
  1217. }
  1218. if ((handler = hwgroup->handler) == NULL || hwgroup->polling) {
  1219. /*
  1220. * Not expecting an interrupt from this drive.
  1221. * That means this could be:
  1222. * (1) an interrupt from another PCI device
  1223. * sharing the same PCI INT# as us.
  1224. * or (2) a drive just entered sleep or standby mode,
  1225. * and is interrupting to let us know.
  1226. * or (3) a spurious interrupt of unknown origin.
  1227. *
  1228. * For PCI, we cannot tell the difference,
  1229. * so in that case we just ignore it and hope it goes away.
  1230. *
  1231. * FIXME: unexpected_intr should be hwif-> then we can
  1232. * remove all the ifdef PCI crap
  1233. */
  1234. #ifdef CONFIG_BLK_DEV_IDEPCI
  1235. if (hwif->chipset != ide_pci)
  1236. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1237. {
  1238. /*
  1239. * Probably not a shared PCI interrupt,
  1240. * so we can safely try to do something about it:
  1241. */
  1242. unexpected_intr(irq, hwgroup);
  1243. #ifdef CONFIG_BLK_DEV_IDEPCI
  1244. } else {
  1245. /*
  1246. * Whack the status register, just in case
  1247. * we have a leftover pending IRQ.
  1248. */
  1249. (void) hwif->INB(hwif->io_ports.status_addr);
  1250. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1251. }
  1252. spin_unlock_irqrestore(&ide_lock, flags);
  1253. return IRQ_NONE;
  1254. }
  1255. drive = hwgroup->drive;
  1256. if (!drive) {
  1257. /*
  1258. * This should NEVER happen, and there isn't much
  1259. * we could do about it here.
  1260. *
  1261. * [Note - this can occur if the drive is hot unplugged]
  1262. */
  1263. spin_unlock_irqrestore(&ide_lock, flags);
  1264. return IRQ_HANDLED;
  1265. }
  1266. if (!drive_is_ready(drive)) {
  1267. /*
  1268. * This happens regularly when we share a PCI IRQ with
  1269. * another device. Unfortunately, it can also happen
  1270. * with some buggy drives that trigger the IRQ before
  1271. * their status register is up to date. Hopefully we have
  1272. * enough advance overhead that the latter isn't a problem.
  1273. */
  1274. spin_unlock_irqrestore(&ide_lock, flags);
  1275. return IRQ_NONE;
  1276. }
  1277. if (!hwgroup->busy) {
  1278. hwgroup->busy = 1; /* paranoia */
  1279. printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
  1280. }
  1281. hwgroup->handler = NULL;
  1282. hwgroup->req_gen++;
  1283. del_timer(&hwgroup->timer);
  1284. spin_unlock(&ide_lock);
  1285. /* Some controllers might set DMA INTR no matter DMA or PIO;
  1286. * bmdma status might need to be cleared even for
  1287. * PIO interrupts to prevent spurious/lost irq.
  1288. */
  1289. if (hwif->ide_dma_clear_irq && !(drive->waiting_for_dma))
  1290. /* ide_dma_end() needs bmdma status for error checking.
  1291. * So, skip clearing bmdma status here and leave it
  1292. * to ide_dma_end() if this is dma interrupt.
  1293. */
  1294. hwif->ide_dma_clear_irq(drive);
  1295. if (drive->unmask)
  1296. local_irq_enable_in_hardirq();
  1297. /* service this interrupt, may set handler for next interrupt */
  1298. startstop = handler(drive);
  1299. spin_lock_irq(&ide_lock);
  1300. /*
  1301. * Note that handler() may have set things up for another
  1302. * interrupt to occur soon, but it cannot happen until
  1303. * we exit from this routine, because it will be the
  1304. * same irq as is currently being serviced here, and Linux
  1305. * won't allow another of the same (on any CPU) until we return.
  1306. */
  1307. drive->service_time = jiffies - drive->service_start;
  1308. if (startstop == ide_stopped) {
  1309. if (hwgroup->handler == NULL) { /* paranoia */
  1310. hwgroup->busy = 0;
  1311. ide_do_request(hwgroup, hwif->irq);
  1312. } else {
  1313. printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
  1314. "on exit\n", drive->name);
  1315. }
  1316. }
  1317. spin_unlock_irqrestore(&ide_lock, flags);
  1318. return IRQ_HANDLED;
  1319. }
  1320. /**
  1321. * ide_do_drive_cmd - issue IDE special command
  1322. * @drive: device to issue command
  1323. * @rq: request to issue
  1324. *
  1325. * This function issues a special IDE device request
  1326. * onto the request queue.
  1327. *
  1328. * the rq is queued at the head of the request queue, displacing
  1329. * the currently-being-processed request and this function
  1330. * returns immediately without waiting for the new rq to be
  1331. * completed. This is VERY DANGEROUS, and is intended for
  1332. * careful use by the ATAPI tape/cdrom driver code.
  1333. */
  1334. void ide_do_drive_cmd(ide_drive_t *drive, struct request *rq)
  1335. {
  1336. unsigned long flags;
  1337. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  1338. spin_lock_irqsave(&ide_lock, flags);
  1339. hwgroup->rq = NULL;
  1340. __elv_add_request(drive->queue, rq, ELEVATOR_INSERT_FRONT, 1);
  1341. __generic_unplug_device(drive->queue);
  1342. spin_unlock_irqrestore(&ide_lock, flags);
  1343. }
  1344. EXPORT_SYMBOL(ide_do_drive_cmd);
  1345. void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount, u8 dma)
  1346. {
  1347. ide_task_t task;
  1348. memset(&task, 0, sizeof(task));
  1349. task.tf_flags = IDE_TFLAG_OUT_LBAH | IDE_TFLAG_OUT_LBAM |
  1350. IDE_TFLAG_OUT_FEATURE | tf_flags;
  1351. task.tf.feature = dma; /* Use PIO/DMA */
  1352. task.tf.lbam = bcount & 0xff;
  1353. task.tf.lbah = (bcount >> 8) & 0xff;
  1354. ide_tf_dump(drive->name, &task.tf);
  1355. ide_set_irq(drive, 1);
  1356. SELECT_MASK(drive, 0);
  1357. drive->hwif->tf_load(drive, &task);
  1358. }
  1359. EXPORT_SYMBOL_GPL(ide_pktcmd_tf_load);
  1360. void ide_pad_transfer(ide_drive_t *drive, int write, int len)
  1361. {
  1362. ide_hwif_t *hwif = drive->hwif;
  1363. u8 buf[4] = { 0 };
  1364. while (len > 0) {
  1365. if (write)
  1366. hwif->output_data(drive, NULL, buf, min(4, len));
  1367. else
  1368. hwif->input_data(drive, NULL, buf, min(4, len));
  1369. len -= 4;
  1370. }
  1371. }
  1372. EXPORT_SYMBOL_GPL(ide_pad_transfer);