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