ide-io.c 44 KB

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