ide-io.c 43 KB

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