ide-io.c 42 KB

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