ide-io.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  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/config.h>
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/string.h>
  29. #include <linux/kernel.h>
  30. #include <linux/timer.h>
  31. #include <linux/mm.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/major.h>
  34. #include <linux/errno.h>
  35. #include <linux/genhd.h>
  36. #include <linux/blkpg.h>
  37. #include <linux/slab.h>
  38. #include <linux/init.h>
  39. #include <linux/pci.h>
  40. #include <linux/delay.h>
  41. #include <linux/ide.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 <asm/byteorder.h>
  50. #include <asm/irq.h>
  51. #include <asm/uaccess.h>
  52. #include <asm/io.h>
  53. #include <asm/bitops.h>
  54. static int __ide_end_request(ide_drive_t *drive, struct request *rq,
  55. int uptodate, int nr_sectors)
  56. {
  57. int ret = 1;
  58. BUG_ON(!(rq->flags & REQ_STARTED));
  59. /*
  60. * if failfast is set on a request, override number of sectors and
  61. * complete the whole request right now
  62. */
  63. if (blk_noretry_request(rq) && end_io_error(uptodate))
  64. nr_sectors = rq->hard_nr_sectors;
  65. if (!blk_fs_request(rq) && end_io_error(uptodate) && !rq->errors)
  66. rq->errors = -EIO;
  67. /*
  68. * decide whether to reenable DMA -- 3 is a random magic for now,
  69. * if we DMA timeout more than 3 times, just stay in PIO
  70. */
  71. if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
  72. drive->state = 0;
  73. HWGROUP(drive)->hwif->ide_dma_on(drive);
  74. }
  75. if (!end_that_request_first(rq, uptodate, nr_sectors)) {
  76. add_disk_randomness(rq->rq_disk);
  77. blkdev_dequeue_request(rq);
  78. HWGROUP(drive)->rq = NULL;
  79. end_that_request_last(rq, uptodate);
  80. ret = 0;
  81. }
  82. return ret;
  83. }
  84. /**
  85. * ide_end_request - complete an IDE I/O
  86. * @drive: IDE device for the I/O
  87. * @uptodate:
  88. * @nr_sectors: number of sectors completed
  89. *
  90. * This is our end_request wrapper function. We complete the I/O
  91. * update random number input and dequeue the request, which if
  92. * it was tagged may be out of order.
  93. */
  94. int ide_end_request (ide_drive_t *drive, int uptodate, int nr_sectors)
  95. {
  96. struct request *rq;
  97. unsigned long flags;
  98. int ret = 1;
  99. /*
  100. * room for locking improvements here, the calls below don't
  101. * need the queue lock held at all
  102. */
  103. spin_lock_irqsave(&ide_lock, flags);
  104. rq = HWGROUP(drive)->rq;
  105. if (!nr_sectors)
  106. nr_sectors = rq->hard_cur_sectors;
  107. ret = __ide_end_request(drive, rq, uptodate, nr_sectors);
  108. spin_unlock_irqrestore(&ide_lock, flags);
  109. return ret;
  110. }
  111. EXPORT_SYMBOL(ide_end_request);
  112. /*
  113. * Power Management state machine. This one is rather trivial for now,
  114. * we should probably add more, like switching back to PIO on suspend
  115. * to help some BIOSes, re-do the door locking on resume, etc...
  116. */
  117. enum {
  118. ide_pm_flush_cache = ide_pm_state_start_suspend,
  119. idedisk_pm_standby,
  120. idedisk_pm_idle = ide_pm_state_start_resume,
  121. ide_pm_restore_dma,
  122. };
  123. static void ide_complete_power_step(ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
  124. {
  125. if (drive->media != ide_disk)
  126. return;
  127. switch (rq->pm->pm_step) {
  128. case ide_pm_flush_cache: /* Suspend step 1 (flush cache) complete */
  129. if (rq->pm->pm_state == PM_EVENT_FREEZE)
  130. rq->pm->pm_step = ide_pm_state_completed;
  131. else
  132. rq->pm->pm_step = idedisk_pm_standby;
  133. break;
  134. case idedisk_pm_standby: /* Suspend step 2 (standby) complete */
  135. rq->pm->pm_step = ide_pm_state_completed;
  136. break;
  137. case idedisk_pm_idle: /* Resume step 1 (idle) complete */
  138. rq->pm->pm_step = ide_pm_restore_dma;
  139. break;
  140. }
  141. }
  142. static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
  143. {
  144. ide_task_t *args = rq->special;
  145. memset(args, 0, sizeof(*args));
  146. if (drive->media != ide_disk) {
  147. /* skip idedisk_pm_idle for ATAPI devices */
  148. if (rq->pm->pm_step == idedisk_pm_idle)
  149. rq->pm->pm_step = ide_pm_restore_dma;
  150. }
  151. switch (rq->pm->pm_step) {
  152. case ide_pm_flush_cache: /* Suspend step 1 (flush cache) */
  153. if (drive->media != ide_disk)
  154. break;
  155. /* Not supported? Switch to next step now. */
  156. if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
  157. ide_complete_power_step(drive, rq, 0, 0);
  158. return ide_stopped;
  159. }
  160. if (ide_id_has_flush_cache_ext(drive->id))
  161. args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
  162. else
  163. args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
  164. args->command_type = IDE_DRIVE_TASK_NO_DATA;
  165. args->handler = &task_no_data_intr;
  166. return do_rw_taskfile(drive, args);
  167. case idedisk_pm_standby: /* Suspend step 2 (standby) */
  168. args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
  169. args->command_type = IDE_DRIVE_TASK_NO_DATA;
  170. args->handler = &task_no_data_intr;
  171. return do_rw_taskfile(drive, args);
  172. case idedisk_pm_idle: /* Resume step 1 (idle) */
  173. args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE;
  174. args->command_type = IDE_DRIVE_TASK_NO_DATA;
  175. args->handler = task_no_data_intr;
  176. return do_rw_taskfile(drive, args);
  177. case ide_pm_restore_dma: /* Resume step 2 (restore DMA) */
  178. /*
  179. * Right now, all we do is call hwif->ide_dma_check(drive),
  180. * we could be smarter and check for current xfer_speed
  181. * in struct drive etc...
  182. */
  183. if ((drive->id->capability & 1) == 0)
  184. break;
  185. if (drive->hwif->ide_dma_check == NULL)
  186. break;
  187. drive->hwif->ide_dma_check(drive);
  188. break;
  189. }
  190. rq->pm->pm_step = ide_pm_state_completed;
  191. return ide_stopped;
  192. }
  193. /**
  194. * ide_complete_pm_request - end the current Power Management request
  195. * @drive: target drive
  196. * @rq: request
  197. *
  198. * This function cleans up the current PM request and stops the queue
  199. * if necessary.
  200. */
  201. static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
  202. {
  203. unsigned long flags;
  204. #ifdef DEBUG_PM
  205. printk("%s: completing PM request, %s\n", drive->name,
  206. blk_pm_suspend_request(rq) ? "suspend" : "resume");
  207. #endif
  208. spin_lock_irqsave(&ide_lock, flags);
  209. if (blk_pm_suspend_request(rq)) {
  210. blk_stop_queue(drive->queue);
  211. } else {
  212. drive->blocked = 0;
  213. blk_start_queue(drive->queue);
  214. }
  215. blkdev_dequeue_request(rq);
  216. HWGROUP(drive)->rq = NULL;
  217. end_that_request_last(rq, 1);
  218. spin_unlock_irqrestore(&ide_lock, flags);
  219. }
  220. /*
  221. * FIXME: probably move this somewhere else, name is bad too :)
  222. */
  223. u64 ide_get_error_location(ide_drive_t *drive, char *args)
  224. {
  225. u32 high, low;
  226. u8 hcyl, lcyl, sect;
  227. u64 sector;
  228. high = 0;
  229. hcyl = args[5];
  230. lcyl = args[4];
  231. sect = args[3];
  232. if (ide_id_has_flush_cache_ext(drive->id)) {
  233. low = (hcyl << 16) | (lcyl << 8) | sect;
  234. HWIF(drive)->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
  235. high = ide_read_24(drive);
  236. } else {
  237. u8 cur = HWIF(drive)->INB(IDE_SELECT_REG);
  238. if (cur & 0x40) {
  239. high = cur & 0xf;
  240. low = (hcyl << 16) | (lcyl << 8) | sect;
  241. } else {
  242. low = hcyl * drive->head * drive->sect;
  243. low += lcyl * drive->sect;
  244. low += sect - 1;
  245. }
  246. }
  247. sector = ((u64) high << 24) | low;
  248. return sector;
  249. }
  250. EXPORT_SYMBOL(ide_get_error_location);
  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. ide_hwif_t *hwif = HWIF(drive);
  267. unsigned long flags;
  268. struct request *rq;
  269. spin_lock_irqsave(&ide_lock, flags);
  270. rq = HWGROUP(drive)->rq;
  271. spin_unlock_irqrestore(&ide_lock, flags);
  272. if (rq->flags & REQ_DRIVE_CMD) {
  273. u8 *args = (u8 *) rq->buffer;
  274. if (rq->errors == 0)
  275. rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
  276. if (args) {
  277. args[0] = stat;
  278. args[1] = err;
  279. args[2] = hwif->INB(IDE_NSECTOR_REG);
  280. }
  281. } else if (rq->flags & REQ_DRIVE_TASK) {
  282. u8 *args = (u8 *) rq->buffer;
  283. if (rq->errors == 0)
  284. rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
  285. if (args) {
  286. args[0] = stat;
  287. args[1] = err;
  288. args[2] = hwif->INB(IDE_NSECTOR_REG);
  289. args[3] = hwif->INB(IDE_SECTOR_REG);
  290. args[4] = hwif->INB(IDE_LCYL_REG);
  291. args[5] = hwif->INB(IDE_HCYL_REG);
  292. args[6] = hwif->INB(IDE_SELECT_REG);
  293. }
  294. } else if (rq->flags & REQ_DRIVE_TASKFILE) {
  295. ide_task_t *args = (ide_task_t *) rq->special;
  296. if (rq->errors == 0)
  297. rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
  298. if (args) {
  299. if (args->tf_in_flags.b.data) {
  300. u16 data = hwif->INW(IDE_DATA_REG);
  301. args->tfRegister[IDE_DATA_OFFSET] = (data) & 0xFF;
  302. args->hobRegister[IDE_DATA_OFFSET] = (data >> 8) & 0xFF;
  303. }
  304. args->tfRegister[IDE_ERROR_OFFSET] = err;
  305. /* be sure we're looking at the low order bits */
  306. hwif->OUTB(drive->ctl & ~0x80, IDE_CONTROL_REG);
  307. args->tfRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
  308. args->tfRegister[IDE_SECTOR_OFFSET] = hwif->INB(IDE_SECTOR_REG);
  309. args->tfRegister[IDE_LCYL_OFFSET] = hwif->INB(IDE_LCYL_REG);
  310. args->tfRegister[IDE_HCYL_OFFSET] = hwif->INB(IDE_HCYL_REG);
  311. args->tfRegister[IDE_SELECT_OFFSET] = hwif->INB(IDE_SELECT_REG);
  312. args->tfRegister[IDE_STATUS_OFFSET] = stat;
  313. if (drive->addressing == 1) {
  314. hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
  315. args->hobRegister[IDE_FEATURE_OFFSET] = hwif->INB(IDE_FEATURE_REG);
  316. args->hobRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
  317. args->hobRegister[IDE_SECTOR_OFFSET] = hwif->INB(IDE_SECTOR_REG);
  318. args->hobRegister[IDE_LCYL_OFFSET] = hwif->INB(IDE_LCYL_REG);
  319. args->hobRegister[IDE_HCYL_OFFSET] = hwif->INB(IDE_HCYL_REG);
  320. }
  321. }
  322. } else if (blk_pm_request(rq)) {
  323. #ifdef DEBUG_PM
  324. printk("%s: complete_power_step(step: %d, stat: %x, err: %x)\n",
  325. drive->name, rq->pm->pm_step, stat, err);
  326. #endif
  327. ide_complete_power_step(drive, rq, stat, err);
  328. if (rq->pm->pm_step == ide_pm_state_completed)
  329. ide_complete_pm_request(drive, rq);
  330. return;
  331. }
  332. spin_lock_irqsave(&ide_lock, flags);
  333. blkdev_dequeue_request(rq);
  334. HWGROUP(drive)->rq = NULL;
  335. rq->errors = err;
  336. end_that_request_last(rq, !rq->errors);
  337. spin_unlock_irqrestore(&ide_lock, flags);
  338. }
  339. EXPORT_SYMBOL(ide_end_drive_cmd);
  340. /**
  341. * try_to_flush_leftover_data - flush junk
  342. * @drive: drive to flush
  343. *
  344. * try_to_flush_leftover_data() is invoked in response to a drive
  345. * unexpectedly having its DRQ_STAT bit set. As an alternative to
  346. * resetting the drive, this routine tries to clear the condition
  347. * by read a sector's worth of data from the drive. Of course,
  348. * this may not help if the drive is *waiting* for data from *us*.
  349. */
  350. static void try_to_flush_leftover_data (ide_drive_t *drive)
  351. {
  352. int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
  353. if (drive->media != ide_disk)
  354. return;
  355. while (i > 0) {
  356. u32 buffer[16];
  357. u32 wcount = (i > 16) ? 16 : i;
  358. i -= wcount;
  359. HWIF(drive)->ata_input_data(drive, buffer, wcount);
  360. }
  361. }
  362. static void ide_kill_rq(ide_drive_t *drive, struct request *rq)
  363. {
  364. if (rq->rq_disk) {
  365. ide_driver_t *drv;
  366. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  367. drv->end_request(drive, 0, 0);
  368. } else
  369. ide_end_request(drive, 0, 0);
  370. }
  371. static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  372. {
  373. ide_hwif_t *hwif = drive->hwif;
  374. if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
  375. /* other bits are useless when BUSY */
  376. rq->errors |= ERROR_RESET;
  377. } else if (stat & ERR_STAT) {
  378. /* err has different meaning on cdrom and tape */
  379. if (err == ABRT_ERR) {
  380. if (drive->select.b.lba &&
  381. /* some newer drives don't support WIN_SPECIFY */
  382. hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
  383. return ide_stopped;
  384. } else if ((err & BAD_CRC) == BAD_CRC) {
  385. /* UDMA crc error, just retry the operation */
  386. drive->crc_count++;
  387. } else if (err & (BBD_ERR | ECC_ERR)) {
  388. /* retries won't help these */
  389. rq->errors = ERROR_MAX;
  390. } else if (err & TRK0_ERR) {
  391. /* help it find track zero */
  392. rq->errors |= ERROR_RECAL;
  393. }
  394. }
  395. if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
  396. try_to_flush_leftover_data(drive);
  397. if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
  398. /* force an abort */
  399. hwif->OUTB(WIN_IDLEIMMEDIATE, IDE_COMMAND_REG);
  400. if (rq->errors >= ERROR_MAX || blk_noretry_request(rq))
  401. ide_kill_rq(drive, rq);
  402. else {
  403. if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
  404. ++rq->errors;
  405. return ide_do_reset(drive);
  406. }
  407. if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
  408. drive->special.b.recalibrate = 1;
  409. ++rq->errors;
  410. }
  411. return ide_stopped;
  412. }
  413. static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  414. {
  415. ide_hwif_t *hwif = drive->hwif;
  416. if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
  417. /* other bits are useless when BUSY */
  418. rq->errors |= ERROR_RESET;
  419. } else {
  420. /* add decoding error stuff */
  421. }
  422. if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
  423. /* force an abort */
  424. hwif->OUTB(WIN_IDLEIMMEDIATE, IDE_COMMAND_REG);
  425. if (rq->errors >= ERROR_MAX) {
  426. ide_kill_rq(drive, rq);
  427. } else {
  428. if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
  429. ++rq->errors;
  430. return ide_do_reset(drive);
  431. }
  432. ++rq->errors;
  433. }
  434. return ide_stopped;
  435. }
  436. ide_startstop_t
  437. __ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  438. {
  439. if (drive->media == ide_disk)
  440. return ide_ata_error(drive, rq, stat, err);
  441. return ide_atapi_error(drive, rq, stat, err);
  442. }
  443. EXPORT_SYMBOL_GPL(__ide_error);
  444. /**
  445. * ide_error - handle an error on the IDE
  446. * @drive: drive the error occurred on
  447. * @msg: message to report
  448. * @stat: status bits
  449. *
  450. * ide_error() takes action based on the error returned by the drive.
  451. * For normal I/O that may well include retries. We deal with
  452. * both new-style (taskfile) and old style command handling here.
  453. * In the case of taskfile command handling there is work left to
  454. * do
  455. */
  456. ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
  457. {
  458. struct request *rq;
  459. u8 err;
  460. err = ide_dump_status(drive, msg, stat);
  461. if ((rq = HWGROUP(drive)->rq) == NULL)
  462. return ide_stopped;
  463. /* retry only "normal" I/O: */
  464. if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
  465. rq->errors = 1;
  466. ide_end_drive_cmd(drive, stat, err);
  467. return ide_stopped;
  468. }
  469. if (rq->rq_disk) {
  470. ide_driver_t *drv;
  471. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  472. return drv->error(drive, rq, stat, err);
  473. } else
  474. return __ide_error(drive, rq, stat, err);
  475. }
  476. EXPORT_SYMBOL_GPL(ide_error);
  477. ide_startstop_t __ide_abort(ide_drive_t *drive, struct request *rq)
  478. {
  479. if (drive->media != ide_disk)
  480. rq->errors |= ERROR_RESET;
  481. ide_kill_rq(drive, rq);
  482. return ide_stopped;
  483. }
  484. EXPORT_SYMBOL_GPL(__ide_abort);
  485. /**
  486. * ide_abort - abort pending IDE operations
  487. * @drive: drive the error occurred on
  488. * @msg: message to report
  489. *
  490. * ide_abort kills and cleans up when we are about to do a
  491. * host initiated reset on active commands. Longer term we
  492. * want handlers to have sensible abort handling themselves
  493. *
  494. * This differs fundamentally from ide_error because in
  495. * this case the command is doing just fine when we
  496. * blow it away.
  497. */
  498. ide_startstop_t ide_abort(ide_drive_t *drive, const char *msg)
  499. {
  500. struct request *rq;
  501. if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
  502. return ide_stopped;
  503. /* retry only "normal" I/O: */
  504. if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
  505. rq->errors = 1;
  506. ide_end_drive_cmd(drive, BUSY_STAT, 0);
  507. return ide_stopped;
  508. }
  509. if (rq->rq_disk) {
  510. ide_driver_t *drv;
  511. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  512. return drv->abort(drive, rq);
  513. } else
  514. return __ide_abort(drive, rq);
  515. }
  516. /**
  517. * ide_cmd - issue a simple drive command
  518. * @drive: drive the command is for
  519. * @cmd: command byte
  520. * @nsect: sector byte
  521. * @handler: handler for the command completion
  522. *
  523. * Issue a simple drive command with interrupts.
  524. * The drive must be selected beforehand.
  525. */
  526. static void ide_cmd (ide_drive_t *drive, u8 cmd, u8 nsect,
  527. ide_handler_t *handler)
  528. {
  529. ide_hwif_t *hwif = HWIF(drive);
  530. if (IDE_CONTROL_REG)
  531. hwif->OUTB(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
  532. SELECT_MASK(drive,0);
  533. hwif->OUTB(nsect,IDE_NSECTOR_REG);
  534. ide_execute_command(drive, cmd, handler, WAIT_CMD, NULL);
  535. }
  536. /**
  537. * drive_cmd_intr - drive command completion interrupt
  538. * @drive: drive the completion interrupt occurred on
  539. *
  540. * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
  541. * We do any necessary data reading and then wait for the drive to
  542. * go non busy. At that point we may read the error data and complete
  543. * the request
  544. */
  545. static ide_startstop_t drive_cmd_intr (ide_drive_t *drive)
  546. {
  547. struct request *rq = HWGROUP(drive)->rq;
  548. ide_hwif_t *hwif = HWIF(drive);
  549. u8 *args = (u8 *) rq->buffer;
  550. u8 stat = hwif->INB(IDE_STATUS_REG);
  551. int retries = 10;
  552. local_irq_enable();
  553. if ((stat & DRQ_STAT) && args && args[3]) {
  554. u8 io_32bit = drive->io_32bit;
  555. drive->io_32bit = 0;
  556. hwif->ata_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
  557. drive->io_32bit = io_32bit;
  558. while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
  559. udelay(100);
  560. }
  561. if (!OK_STAT(stat, READY_STAT, BAD_STAT))
  562. return ide_error(drive, "drive_cmd", stat);
  563. /* calls ide_end_drive_cmd */
  564. ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
  565. return ide_stopped;
  566. }
  567. static void ide_init_specify_cmd(ide_drive_t *drive, ide_task_t *task)
  568. {
  569. task->tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
  570. task->tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
  571. task->tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
  572. task->tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
  573. task->tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
  574. task->tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
  575. task->handler = &set_geometry_intr;
  576. }
  577. static void ide_init_restore_cmd(ide_drive_t *drive, ide_task_t *task)
  578. {
  579. task->tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
  580. task->tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
  581. task->handler = &recal_intr;
  582. }
  583. static void ide_init_setmult_cmd(ide_drive_t *drive, ide_task_t *task)
  584. {
  585. task->tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
  586. task->tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
  587. task->handler = &set_multmode_intr;
  588. }
  589. static ide_startstop_t ide_disk_special(ide_drive_t *drive)
  590. {
  591. special_t *s = &drive->special;
  592. ide_task_t args;
  593. memset(&args, 0, sizeof(ide_task_t));
  594. args.command_type = IDE_DRIVE_TASK_NO_DATA;
  595. if (s->b.set_geometry) {
  596. s->b.set_geometry = 0;
  597. ide_init_specify_cmd(drive, &args);
  598. } else if (s->b.recalibrate) {
  599. s->b.recalibrate = 0;
  600. ide_init_restore_cmd(drive, &args);
  601. } else if (s->b.set_multmode) {
  602. s->b.set_multmode = 0;
  603. if (drive->mult_req > drive->id->max_multsect)
  604. drive->mult_req = drive->id->max_multsect;
  605. ide_init_setmult_cmd(drive, &args);
  606. } else if (s->all) {
  607. int special = s->all;
  608. s->all = 0;
  609. printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
  610. return ide_stopped;
  611. }
  612. do_rw_taskfile(drive, &args);
  613. return ide_started;
  614. }
  615. /**
  616. * do_special - issue some special commands
  617. * @drive: drive the command is for
  618. *
  619. * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
  620. * commands to a drive. It used to do much more, but has been scaled
  621. * back.
  622. */
  623. static ide_startstop_t do_special (ide_drive_t *drive)
  624. {
  625. special_t *s = &drive->special;
  626. #ifdef DEBUG
  627. printk("%s: do_special: 0x%02x\n", drive->name, s->all);
  628. #endif
  629. if (s->b.set_tune) {
  630. s->b.set_tune = 0;
  631. if (HWIF(drive)->tuneproc != NULL)
  632. HWIF(drive)->tuneproc(drive, drive->tune_req);
  633. return ide_stopped;
  634. } else {
  635. if (drive->media == ide_disk)
  636. return ide_disk_special(drive);
  637. s->all = 0;
  638. drive->mult_req = 0;
  639. return ide_stopped;
  640. }
  641. }
  642. void ide_map_sg(ide_drive_t *drive, struct request *rq)
  643. {
  644. ide_hwif_t *hwif = drive->hwif;
  645. struct scatterlist *sg = hwif->sg_table;
  646. if (hwif->sg_mapped) /* needed by ide-scsi */
  647. return;
  648. if ((rq->flags & REQ_DRIVE_TASKFILE) == 0) {
  649. hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
  650. } else {
  651. sg_init_one(sg, rq->buffer, rq->nr_sectors * SECTOR_SIZE);
  652. hwif->sg_nents = 1;
  653. }
  654. }
  655. EXPORT_SYMBOL_GPL(ide_map_sg);
  656. void ide_init_sg_cmd(ide_drive_t *drive, struct request *rq)
  657. {
  658. ide_hwif_t *hwif = drive->hwif;
  659. hwif->nsect = hwif->nleft = rq->nr_sectors;
  660. hwif->cursg = hwif->cursg_ofs = 0;
  661. }
  662. EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
  663. /**
  664. * execute_drive_command - issue special drive command
  665. * @drive: the drive to issue the command on
  666. * @rq: the request structure holding the command
  667. *
  668. * execute_drive_cmd() issues a special drive command, usually
  669. * initiated by ioctl() from the external hdparm program. The
  670. * command can be a drive command, drive task or taskfile
  671. * operation. Weirdly you can call it with NULL to wait for
  672. * all commands to finish. Don't do this as that is due to change
  673. */
  674. static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
  675. struct request *rq)
  676. {
  677. ide_hwif_t *hwif = HWIF(drive);
  678. if (rq->flags & REQ_DRIVE_TASKFILE) {
  679. ide_task_t *args = rq->special;
  680. if (!args)
  681. goto done;
  682. hwif->data_phase = args->data_phase;
  683. switch (hwif->data_phase) {
  684. case TASKFILE_MULTI_OUT:
  685. case TASKFILE_OUT:
  686. case TASKFILE_MULTI_IN:
  687. case TASKFILE_IN:
  688. ide_init_sg_cmd(drive, rq);
  689. ide_map_sg(drive, rq);
  690. default:
  691. break;
  692. }
  693. if (args->tf_out_flags.all != 0)
  694. return flagged_taskfile(drive, args);
  695. return do_rw_taskfile(drive, args);
  696. } else if (rq->flags & REQ_DRIVE_TASK) {
  697. u8 *args = rq->buffer;
  698. u8 sel;
  699. if (!args)
  700. goto done;
  701. #ifdef DEBUG
  702. printk("%s: DRIVE_TASK_CMD ", drive->name);
  703. printk("cmd=0x%02x ", args[0]);
  704. printk("fr=0x%02x ", args[1]);
  705. printk("ns=0x%02x ", args[2]);
  706. printk("sc=0x%02x ", args[3]);
  707. printk("lcyl=0x%02x ", args[4]);
  708. printk("hcyl=0x%02x ", args[5]);
  709. printk("sel=0x%02x\n", args[6]);
  710. #endif
  711. hwif->OUTB(args[1], IDE_FEATURE_REG);
  712. hwif->OUTB(args[3], IDE_SECTOR_REG);
  713. hwif->OUTB(args[4], IDE_LCYL_REG);
  714. hwif->OUTB(args[5], IDE_HCYL_REG);
  715. sel = (args[6] & ~0x10);
  716. if (drive->select.b.unit)
  717. sel |= 0x10;
  718. hwif->OUTB(sel, IDE_SELECT_REG);
  719. ide_cmd(drive, args[0], args[2], &drive_cmd_intr);
  720. return ide_started;
  721. } else if (rq->flags & REQ_DRIVE_CMD) {
  722. u8 *args = rq->buffer;
  723. if (!args)
  724. goto done;
  725. #ifdef DEBUG
  726. printk("%s: DRIVE_CMD ", drive->name);
  727. printk("cmd=0x%02x ", args[0]);
  728. printk("sc=0x%02x ", args[1]);
  729. printk("fr=0x%02x ", args[2]);
  730. printk("xx=0x%02x\n", args[3]);
  731. #endif
  732. if (args[0] == WIN_SMART) {
  733. hwif->OUTB(0x4f, IDE_LCYL_REG);
  734. hwif->OUTB(0xc2, IDE_HCYL_REG);
  735. hwif->OUTB(args[2],IDE_FEATURE_REG);
  736. hwif->OUTB(args[1],IDE_SECTOR_REG);
  737. ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
  738. return ide_started;
  739. }
  740. hwif->OUTB(args[2],IDE_FEATURE_REG);
  741. ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
  742. return ide_started;
  743. }
  744. done:
  745. /*
  746. * NULL is actually a valid way of waiting for
  747. * all current requests to be flushed from the queue.
  748. */
  749. #ifdef DEBUG
  750. printk("%s: DRIVE_CMD (null)\n", drive->name);
  751. #endif
  752. ide_end_drive_cmd(drive,
  753. hwif->INB(IDE_STATUS_REG),
  754. hwif->INB(IDE_ERROR_REG));
  755. return ide_stopped;
  756. }
  757. /**
  758. * start_request - start of I/O and command issuing for IDE
  759. *
  760. * start_request() initiates handling of a new I/O request. It
  761. * accepts commands and I/O (read/write) requests. It also does
  762. * the final remapping for weird stuff like EZDrive. Once
  763. * device mapper can work sector level the EZDrive stuff can go away
  764. *
  765. * FIXME: this function needs a rename
  766. */
  767. static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
  768. {
  769. ide_startstop_t startstop;
  770. sector_t block;
  771. BUG_ON(!(rq->flags & REQ_STARTED));
  772. #ifdef DEBUG
  773. printk("%s: start_request: current=0x%08lx\n",
  774. HWIF(drive)->name, (unsigned long) rq);
  775. #endif
  776. /* bail early if we've exceeded max_failures */
  777. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  778. goto kill_rq;
  779. }
  780. block = rq->sector;
  781. if (blk_fs_request(rq) &&
  782. (drive->media == ide_disk || drive->media == ide_floppy)) {
  783. block += drive->sect0;
  784. }
  785. /* Yecch - this will shift the entire interval,
  786. possibly killing some innocent following sector */
  787. if (block == 0 && drive->remap_0_to_1 == 1)
  788. block = 1; /* redirect MBR access to EZ-Drive partn table */
  789. if (blk_pm_suspend_request(rq) &&
  790. rq->pm->pm_step == ide_pm_state_start_suspend)
  791. /* Mark drive blocked when starting the suspend sequence. */
  792. drive->blocked = 1;
  793. else if (blk_pm_resume_request(rq) &&
  794. rq->pm->pm_step == ide_pm_state_start_resume) {
  795. /*
  796. * The first thing we do on wakeup is to wait for BSY bit to
  797. * go away (with a looong timeout) as a drive on this hwif may
  798. * just be POSTing itself.
  799. * We do that before even selecting as the "other" device on
  800. * the bus may be broken enough to walk on our toes at this
  801. * point.
  802. */
  803. int rc;
  804. #ifdef DEBUG_PM
  805. printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
  806. #endif
  807. rc = ide_wait_not_busy(HWIF(drive), 35000);
  808. if (rc)
  809. printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
  810. SELECT_DRIVE(drive);
  811. HWIF(drive)->OUTB(8, HWIF(drive)->io_ports[IDE_CONTROL_OFFSET]);
  812. rc = ide_wait_not_busy(HWIF(drive), 10000);
  813. if (rc)
  814. printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
  815. }
  816. SELECT_DRIVE(drive);
  817. if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
  818. printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
  819. return startstop;
  820. }
  821. if (!drive->special.all) {
  822. ide_driver_t *drv;
  823. if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK))
  824. return execute_drive_cmd(drive, rq);
  825. else if (rq->flags & REQ_DRIVE_TASKFILE)
  826. return execute_drive_cmd(drive, rq);
  827. else if (blk_pm_request(rq)) {
  828. #ifdef DEBUG_PM
  829. printk("%s: start_power_step(step: %d)\n",
  830. drive->name, rq->pm->pm_step);
  831. #endif
  832. startstop = ide_start_power_step(drive, rq);
  833. if (startstop == ide_stopped &&
  834. rq->pm->pm_step == ide_pm_state_completed)
  835. ide_complete_pm_request(drive, rq);
  836. return startstop;
  837. }
  838. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  839. return drv->do_request(drive, rq, block);
  840. }
  841. return do_special(drive);
  842. kill_rq:
  843. ide_kill_rq(drive, rq);
  844. return ide_stopped;
  845. }
  846. /**
  847. * ide_stall_queue - pause an IDE device
  848. * @drive: drive to stall
  849. * @timeout: time to stall for (jiffies)
  850. *
  851. * ide_stall_queue() can be used by a drive to give excess bandwidth back
  852. * to the hwgroup by sleeping for timeout jiffies.
  853. */
  854. void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
  855. {
  856. if (timeout > WAIT_WORSTCASE)
  857. timeout = WAIT_WORSTCASE;
  858. drive->sleep = timeout + jiffies;
  859. drive->sleeping = 1;
  860. }
  861. EXPORT_SYMBOL(ide_stall_queue);
  862. #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
  863. /**
  864. * choose_drive - select a drive to service
  865. * @hwgroup: hardware group to select on
  866. *
  867. * choose_drive() selects the next drive which will be serviced.
  868. * This is necessary because the IDE layer can't issue commands
  869. * to both drives on the same cable, unlike SCSI.
  870. */
  871. static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
  872. {
  873. ide_drive_t *drive, *best;
  874. repeat:
  875. best = NULL;
  876. drive = hwgroup->drive;
  877. /*
  878. * drive is doing pre-flush, ordered write, post-flush sequence. even
  879. * though that is 3 requests, it must be seen as a single transaction.
  880. * we must not preempt this drive until that is complete
  881. */
  882. if (blk_queue_flushing(drive->queue)) {
  883. /*
  884. * small race where queue could get replugged during
  885. * the 3-request flush cycle, just yank the plug since
  886. * we want it to finish asap
  887. */
  888. blk_remove_plug(drive->queue);
  889. return drive;
  890. }
  891. do {
  892. if ((!drive->sleeping || time_after_eq(jiffies, drive->sleep))
  893. && !elv_queue_empty(drive->queue)) {
  894. if (!best
  895. || (drive->sleeping && (!best->sleeping || time_before(drive->sleep, best->sleep)))
  896. || (!best->sleeping && time_before(WAKEUP(drive), WAKEUP(best))))
  897. {
  898. if (!blk_queue_plugged(drive->queue))
  899. best = drive;
  900. }
  901. }
  902. } while ((drive = drive->next) != hwgroup->drive);
  903. if (best && best->nice1 && !best->sleeping && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
  904. long t = (signed long)(WAKEUP(best) - jiffies);
  905. if (t >= WAIT_MIN_SLEEP) {
  906. /*
  907. * We *may* have some time to spare, but first let's see if
  908. * someone can potentially benefit from our nice mood today..
  909. */
  910. drive = best->next;
  911. do {
  912. if (!drive->sleeping
  913. && time_before(jiffies - best->service_time, WAKEUP(drive))
  914. && time_before(WAKEUP(drive), jiffies + t))
  915. {
  916. ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
  917. goto repeat;
  918. }
  919. } while ((drive = drive->next) != best);
  920. }
  921. }
  922. return best;
  923. }
  924. /*
  925. * Issue a new request to a drive from hwgroup
  926. * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
  927. *
  928. * A hwgroup is a serialized group of IDE interfaces. Usually there is
  929. * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
  930. * may have both interfaces in a single hwgroup to "serialize" access.
  931. * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
  932. * together into one hwgroup for serialized access.
  933. *
  934. * Note also that several hwgroups can end up sharing a single IRQ,
  935. * possibly along with many other devices. This is especially common in
  936. * PCI-based systems with off-board IDE controller cards.
  937. *
  938. * The IDE driver uses the single global ide_lock spinlock to protect
  939. * access to the request queues, and to protect the hwgroup->busy flag.
  940. *
  941. * The first thread into the driver for a particular hwgroup sets the
  942. * hwgroup->busy flag to indicate that this hwgroup is now active,
  943. * and then initiates processing of the top request from the request queue.
  944. *
  945. * Other threads attempting entry notice the busy setting, and will simply
  946. * queue their new requests and exit immediately. Note that hwgroup->busy
  947. * remains set even when the driver is merely awaiting the next interrupt.
  948. * Thus, the meaning is "this hwgroup is busy processing a request".
  949. *
  950. * When processing of a request completes, the completing thread or IRQ-handler
  951. * will start the next request from the queue. If no more work remains,
  952. * the driver will clear the hwgroup->busy flag and exit.
  953. *
  954. * The ide_lock (spinlock) is used to protect all access to the
  955. * hwgroup->busy flag, but is otherwise not needed for most processing in
  956. * the driver. This makes the driver much more friendlier to shared IRQs
  957. * than previous designs, while remaining 100% (?) SMP safe and capable.
  958. */
  959. static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
  960. {
  961. ide_drive_t *drive;
  962. ide_hwif_t *hwif;
  963. struct request *rq;
  964. ide_startstop_t startstop;
  965. int loops = 0;
  966. /* for atari only: POSSIBLY BROKEN HERE(?) */
  967. ide_get_lock(ide_intr, hwgroup);
  968. /* caller must own ide_lock */
  969. BUG_ON(!irqs_disabled());
  970. while (!hwgroup->busy) {
  971. hwgroup->busy = 1;
  972. drive = choose_drive(hwgroup);
  973. if (drive == NULL) {
  974. int sleeping = 0;
  975. unsigned long sleep = 0; /* shut up, gcc */
  976. hwgroup->rq = NULL;
  977. drive = hwgroup->drive;
  978. do {
  979. if (drive->sleeping && (!sleeping || time_before(drive->sleep, sleep))) {
  980. sleeping = 1;
  981. sleep = drive->sleep;
  982. }
  983. } while ((drive = drive->next) != hwgroup->drive);
  984. if (sleeping) {
  985. /*
  986. * Take a short snooze, and then wake up this hwgroup again.
  987. * This gives other hwgroups on the same a chance to
  988. * play fairly with us, just in case there are big differences
  989. * in relative throughputs.. don't want to hog the cpu too much.
  990. */
  991. if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
  992. sleep = jiffies + WAIT_MIN_SLEEP;
  993. #if 1
  994. if (timer_pending(&hwgroup->timer))
  995. printk(KERN_CRIT "ide_set_handler: timer already active\n");
  996. #endif
  997. /* so that ide_timer_expiry knows what to do */
  998. hwgroup->sleeping = 1;
  999. mod_timer(&hwgroup->timer, sleep);
  1000. /* we purposely leave hwgroup->busy==1
  1001. * while sleeping */
  1002. } else {
  1003. /* Ugly, but how can we sleep for the lock
  1004. * otherwise? perhaps from tq_disk?
  1005. */
  1006. /* for atari only */
  1007. ide_release_lock();
  1008. hwgroup->busy = 0;
  1009. }
  1010. /* no more work for this hwgroup (for now) */
  1011. return;
  1012. }
  1013. again:
  1014. hwif = HWIF(drive);
  1015. if (hwgroup->hwif->sharing_irq &&
  1016. hwif != hwgroup->hwif &&
  1017. hwif->io_ports[IDE_CONTROL_OFFSET]) {
  1018. /* set nIEN for previous hwif */
  1019. SELECT_INTERRUPT(drive);
  1020. }
  1021. hwgroup->hwif = hwif;
  1022. hwgroup->drive = drive;
  1023. drive->sleeping = 0;
  1024. drive->service_start = jiffies;
  1025. if (blk_queue_plugged(drive->queue)) {
  1026. printk(KERN_ERR "ide: huh? queue was plugged!\n");
  1027. break;
  1028. }
  1029. /*
  1030. * we know that the queue isn't empty, but this can happen
  1031. * if the q->prep_rq_fn() decides to kill a request
  1032. */
  1033. rq = elv_next_request(drive->queue);
  1034. if (!rq) {
  1035. hwgroup->busy = 0;
  1036. break;
  1037. }
  1038. /*
  1039. * Sanity: don't accept a request that isn't a PM request
  1040. * if we are currently power managed. This is very important as
  1041. * blk_stop_queue() doesn't prevent the elv_next_request()
  1042. * above to return us whatever is in the queue. Since we call
  1043. * ide_do_request() ourselves, we end up taking requests while
  1044. * the queue is blocked...
  1045. *
  1046. * We let requests forced at head of queue with ide-preempt
  1047. * though. I hope that doesn't happen too much, hopefully not
  1048. * unless the subdriver triggers such a thing in its own PM
  1049. * state machine.
  1050. *
  1051. * We count how many times we loop here to make sure we service
  1052. * all drives in the hwgroup without looping for ever
  1053. */
  1054. if (drive->blocked && !blk_pm_request(rq) && !(rq->flags & REQ_PREEMPT)) {
  1055. drive = drive->next ? drive->next : hwgroup->drive;
  1056. if (loops++ < 4 && !blk_queue_plugged(drive->queue))
  1057. goto again;
  1058. /* We clear busy, there should be no pending ATA command at this point. */
  1059. hwgroup->busy = 0;
  1060. break;
  1061. }
  1062. hwgroup->rq = rq;
  1063. /*
  1064. * Some systems have trouble with IDE IRQs arriving while
  1065. * the driver is still setting things up. So, here we disable
  1066. * the IRQ used by this interface while the request is being started.
  1067. * This may look bad at first, but pretty much the same thing
  1068. * happens anyway when any interrupt comes in, IDE or otherwise
  1069. * -- the kernel masks the IRQ while it is being handled.
  1070. */
  1071. if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
  1072. disable_irq_nosync(hwif->irq);
  1073. spin_unlock(&ide_lock);
  1074. local_irq_enable();
  1075. /* allow other IRQs while we start this request */
  1076. startstop = start_request(drive, rq);
  1077. spin_lock_irq(&ide_lock);
  1078. if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
  1079. enable_irq(hwif->irq);
  1080. if (startstop == ide_stopped)
  1081. hwgroup->busy = 0;
  1082. }
  1083. }
  1084. /*
  1085. * Passes the stuff to ide_do_request
  1086. */
  1087. void do_ide_request(request_queue_t *q)
  1088. {
  1089. ide_drive_t *drive = q->queuedata;
  1090. ide_do_request(HWGROUP(drive), IDE_NO_IRQ);
  1091. }
  1092. /*
  1093. * un-busy the hwgroup etc, and clear any pending DMA status. we want to
  1094. * retry the current request in pio mode instead of risking tossing it
  1095. * all away
  1096. */
  1097. static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
  1098. {
  1099. ide_hwif_t *hwif = HWIF(drive);
  1100. struct request *rq;
  1101. ide_startstop_t ret = ide_stopped;
  1102. /*
  1103. * end current dma transaction
  1104. */
  1105. if (error < 0) {
  1106. printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
  1107. (void)HWIF(drive)->ide_dma_end(drive);
  1108. ret = ide_error(drive, "dma timeout error",
  1109. hwif->INB(IDE_STATUS_REG));
  1110. } else {
  1111. printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
  1112. (void) hwif->ide_dma_timeout(drive);
  1113. }
  1114. /*
  1115. * disable dma for now, but remember that we did so because of
  1116. * a timeout -- we'll reenable after we finish this next request
  1117. * (or rather the first chunk of it) in pio.
  1118. */
  1119. drive->retry_pio++;
  1120. drive->state = DMA_PIO_RETRY;
  1121. (void) hwif->ide_dma_off_quietly(drive);
  1122. /*
  1123. * un-busy drive etc (hwgroup->busy is cleared on return) and
  1124. * make sure request is sane
  1125. */
  1126. rq = HWGROUP(drive)->rq;
  1127. HWGROUP(drive)->rq = NULL;
  1128. rq->errors = 0;
  1129. if (!rq->bio)
  1130. goto out;
  1131. rq->sector = rq->bio->bi_sector;
  1132. rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
  1133. rq->hard_cur_sectors = rq->current_nr_sectors;
  1134. rq->buffer = bio_data(rq->bio);
  1135. out:
  1136. return ret;
  1137. }
  1138. /**
  1139. * ide_timer_expiry - handle lack of an IDE interrupt
  1140. * @data: timer callback magic (hwgroup)
  1141. *
  1142. * An IDE command has timed out before the expected drive return
  1143. * occurred. At this point we attempt to clean up the current
  1144. * mess. If the current handler includes an expiry handler then
  1145. * we invoke the expiry handler, and providing it is happy the
  1146. * work is done. If that fails we apply generic recovery rules
  1147. * invoking the handler and checking the drive DMA status. We
  1148. * have an excessively incestuous relationship with the DMA
  1149. * logic that wants cleaning up.
  1150. */
  1151. void ide_timer_expiry (unsigned long data)
  1152. {
  1153. ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
  1154. ide_handler_t *handler;
  1155. ide_expiry_t *expiry;
  1156. unsigned long flags;
  1157. unsigned long wait = -1;
  1158. spin_lock_irqsave(&ide_lock, flags);
  1159. if ((handler = hwgroup->handler) == NULL) {
  1160. /*
  1161. * Either a marginal timeout occurred
  1162. * (got the interrupt just as timer expired),
  1163. * or we were "sleeping" to give other devices a chance.
  1164. * Either way, we don't really want to complain about anything.
  1165. */
  1166. if (hwgroup->sleeping) {
  1167. hwgroup->sleeping = 0;
  1168. hwgroup->busy = 0;
  1169. }
  1170. } else {
  1171. ide_drive_t *drive = hwgroup->drive;
  1172. if (!drive) {
  1173. printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
  1174. hwgroup->handler = NULL;
  1175. } else {
  1176. ide_hwif_t *hwif;
  1177. ide_startstop_t startstop = ide_stopped;
  1178. if (!hwgroup->busy) {
  1179. hwgroup->busy = 1; /* paranoia */
  1180. printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
  1181. }
  1182. if ((expiry = hwgroup->expiry) != NULL) {
  1183. /* continue */
  1184. if ((wait = expiry(drive)) > 0) {
  1185. /* reset timer */
  1186. hwgroup->timer.expires = jiffies + wait;
  1187. add_timer(&hwgroup->timer);
  1188. spin_unlock_irqrestore(&ide_lock, flags);
  1189. return;
  1190. }
  1191. }
  1192. hwgroup->handler = NULL;
  1193. /*
  1194. * We need to simulate a real interrupt when invoking
  1195. * the handler() function, which means we need to
  1196. * globally mask the specific IRQ:
  1197. */
  1198. spin_unlock(&ide_lock);
  1199. hwif = HWIF(drive);
  1200. #if DISABLE_IRQ_NOSYNC
  1201. disable_irq_nosync(hwif->irq);
  1202. #else
  1203. /* disable_irq_nosync ?? */
  1204. disable_irq(hwif->irq);
  1205. #endif /* DISABLE_IRQ_NOSYNC */
  1206. /* local CPU only,
  1207. * as if we were handling an interrupt */
  1208. local_irq_disable();
  1209. if (hwgroup->polling) {
  1210. startstop = handler(drive);
  1211. } else if (drive_is_ready(drive)) {
  1212. if (drive->waiting_for_dma)
  1213. (void) hwgroup->hwif->ide_dma_lostirq(drive);
  1214. (void)ide_ack_intr(hwif);
  1215. printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
  1216. startstop = handler(drive);
  1217. } else {
  1218. if (drive->waiting_for_dma) {
  1219. startstop = ide_dma_timeout_retry(drive, wait);
  1220. } else
  1221. startstop =
  1222. ide_error(drive, "irq timeout", hwif->INB(IDE_STATUS_REG));
  1223. }
  1224. drive->service_time = jiffies - drive->service_start;
  1225. spin_lock_irq(&ide_lock);
  1226. enable_irq(hwif->irq);
  1227. if (startstop == ide_stopped)
  1228. hwgroup->busy = 0;
  1229. }
  1230. }
  1231. ide_do_request(hwgroup, IDE_NO_IRQ);
  1232. spin_unlock_irqrestore(&ide_lock, flags);
  1233. }
  1234. /**
  1235. * unexpected_intr - handle an unexpected IDE interrupt
  1236. * @irq: interrupt line
  1237. * @hwgroup: hwgroup being processed
  1238. *
  1239. * There's nothing really useful we can do with an unexpected interrupt,
  1240. * other than reading the status register (to clear it), and logging it.
  1241. * There should be no way that an irq can happen before we're ready for it,
  1242. * so we needn't worry much about losing an "important" interrupt here.
  1243. *
  1244. * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
  1245. * the drive enters "idle", "standby", or "sleep" mode, so if the status
  1246. * looks "good", we just ignore the interrupt completely.
  1247. *
  1248. * This routine assumes __cli() is in effect when called.
  1249. *
  1250. * If an unexpected interrupt happens on irq15 while we are handling irq14
  1251. * and if the two interfaces are "serialized" (CMD640), then it looks like
  1252. * we could screw up by interfering with a new request being set up for
  1253. * irq15.
  1254. *
  1255. * In reality, this is a non-issue. The new command is not sent unless
  1256. * the drive is ready to accept one, in which case we know the drive is
  1257. * not trying to interrupt us. And ide_set_handler() is always invoked
  1258. * before completing the issuance of any new drive command, so we will not
  1259. * be accidentally invoked as a result of any valid command completion
  1260. * interrupt.
  1261. *
  1262. * Note that we must walk the entire hwgroup here. We know which hwif
  1263. * is doing the current command, but we don't know which hwif burped
  1264. * mysteriously.
  1265. */
  1266. static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
  1267. {
  1268. u8 stat;
  1269. ide_hwif_t *hwif = hwgroup->hwif;
  1270. /*
  1271. * handle the unexpected interrupt
  1272. */
  1273. do {
  1274. if (hwif->irq == irq) {
  1275. stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
  1276. if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
  1277. /* Try to not flood the console with msgs */
  1278. static unsigned long last_msgtime, count;
  1279. ++count;
  1280. if (time_after(jiffies, last_msgtime + HZ)) {
  1281. last_msgtime = jiffies;
  1282. printk(KERN_ERR "%s%s: unexpected interrupt, "
  1283. "status=0x%02x, count=%ld\n",
  1284. hwif->name,
  1285. (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
  1286. }
  1287. }
  1288. }
  1289. } while ((hwif = hwif->next) != hwgroup->hwif);
  1290. }
  1291. /**
  1292. * ide_intr - default IDE interrupt handler
  1293. * @irq: interrupt number
  1294. * @dev_id: hwif group
  1295. * @regs: unused weirdness from the kernel irq layer
  1296. *
  1297. * This is the default IRQ handler for the IDE layer. You should
  1298. * not need to override it. If you do be aware it is subtle in
  1299. * places
  1300. *
  1301. * hwgroup->hwif is the interface in the group currently performing
  1302. * a command. hwgroup->drive is the drive and hwgroup->handler is
  1303. * the IRQ handler to call. As we issue a command the handlers
  1304. * step through multiple states, reassigning the handler to the
  1305. * next step in the process. Unlike a smart SCSI controller IDE
  1306. * expects the main processor to sequence the various transfer
  1307. * stages. We also manage a poll timer to catch up with most
  1308. * timeout situations. There are still a few where the handlers
  1309. * don't ever decide to give up.
  1310. *
  1311. * The handler eventually returns ide_stopped to indicate the
  1312. * request completed. At this point we issue the next request
  1313. * on the hwgroup and the process begins again.
  1314. */
  1315. irqreturn_t ide_intr (int irq, void *dev_id, struct pt_regs *regs)
  1316. {
  1317. unsigned long flags;
  1318. ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
  1319. ide_hwif_t *hwif;
  1320. ide_drive_t *drive;
  1321. ide_handler_t *handler;
  1322. ide_startstop_t startstop;
  1323. spin_lock_irqsave(&ide_lock, flags);
  1324. hwif = hwgroup->hwif;
  1325. if (!ide_ack_intr(hwif)) {
  1326. spin_unlock_irqrestore(&ide_lock, flags);
  1327. return IRQ_NONE;
  1328. }
  1329. if ((handler = hwgroup->handler) == NULL || hwgroup->polling) {
  1330. /*
  1331. * Not expecting an interrupt from this drive.
  1332. * That means this could be:
  1333. * (1) an interrupt from another PCI device
  1334. * sharing the same PCI INT# as us.
  1335. * or (2) a drive just entered sleep or standby mode,
  1336. * and is interrupting to let us know.
  1337. * or (3) a spurious interrupt of unknown origin.
  1338. *
  1339. * For PCI, we cannot tell the difference,
  1340. * so in that case we just ignore it and hope it goes away.
  1341. *
  1342. * FIXME: unexpected_intr should be hwif-> then we can
  1343. * remove all the ifdef PCI crap
  1344. */
  1345. #ifdef CONFIG_BLK_DEV_IDEPCI
  1346. if (hwif->pci_dev && !hwif->pci_dev->vendor)
  1347. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1348. {
  1349. /*
  1350. * Probably not a shared PCI interrupt,
  1351. * so we can safely try to do something about it:
  1352. */
  1353. unexpected_intr(irq, hwgroup);
  1354. #ifdef CONFIG_BLK_DEV_IDEPCI
  1355. } else {
  1356. /*
  1357. * Whack the status register, just in case
  1358. * we have a leftover pending IRQ.
  1359. */
  1360. (void) hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
  1361. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1362. }
  1363. spin_unlock_irqrestore(&ide_lock, flags);
  1364. return IRQ_NONE;
  1365. }
  1366. drive = hwgroup->drive;
  1367. if (!drive) {
  1368. /*
  1369. * This should NEVER happen, and there isn't much
  1370. * we could do about it here.
  1371. *
  1372. * [Note - this can occur if the drive is hot unplugged]
  1373. */
  1374. spin_unlock_irqrestore(&ide_lock, flags);
  1375. return IRQ_HANDLED;
  1376. }
  1377. if (!drive_is_ready(drive)) {
  1378. /*
  1379. * This happens regularly when we share a PCI IRQ with
  1380. * another device. Unfortunately, it can also happen
  1381. * with some buggy drives that trigger the IRQ before
  1382. * their status register is up to date. Hopefully we have
  1383. * enough advance overhead that the latter isn't a problem.
  1384. */
  1385. spin_unlock_irqrestore(&ide_lock, flags);
  1386. return IRQ_NONE;
  1387. }
  1388. if (!hwgroup->busy) {
  1389. hwgroup->busy = 1; /* paranoia */
  1390. printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
  1391. }
  1392. hwgroup->handler = NULL;
  1393. del_timer(&hwgroup->timer);
  1394. spin_unlock(&ide_lock);
  1395. if (drive->unmask)
  1396. local_irq_enable();
  1397. /* service this interrupt, may set handler for next interrupt */
  1398. startstop = handler(drive);
  1399. spin_lock_irq(&ide_lock);
  1400. /*
  1401. * Note that handler() may have set things up for another
  1402. * interrupt to occur soon, but it cannot happen until
  1403. * we exit from this routine, because it will be the
  1404. * same irq as is currently being serviced here, and Linux
  1405. * won't allow another of the same (on any CPU) until we return.
  1406. */
  1407. drive->service_time = jiffies - drive->service_start;
  1408. if (startstop == ide_stopped) {
  1409. if (hwgroup->handler == NULL) { /* paranoia */
  1410. hwgroup->busy = 0;
  1411. ide_do_request(hwgroup, hwif->irq);
  1412. } else {
  1413. printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
  1414. "on exit\n", drive->name);
  1415. }
  1416. }
  1417. spin_unlock_irqrestore(&ide_lock, flags);
  1418. return IRQ_HANDLED;
  1419. }
  1420. /**
  1421. * ide_init_drive_cmd - initialize a drive command request
  1422. * @rq: request object
  1423. *
  1424. * Initialize a request before we fill it in and send it down to
  1425. * ide_do_drive_cmd. Commands must be set up by this function. Right
  1426. * now it doesn't do a lot, but if that changes abusers will have a
  1427. * nasty suprise.
  1428. */
  1429. void ide_init_drive_cmd (struct request *rq)
  1430. {
  1431. memset(rq, 0, sizeof(*rq));
  1432. rq->flags = REQ_DRIVE_CMD;
  1433. rq->ref_count = 1;
  1434. }
  1435. EXPORT_SYMBOL(ide_init_drive_cmd);
  1436. /**
  1437. * ide_do_drive_cmd - issue IDE special command
  1438. * @drive: device to issue command
  1439. * @rq: request to issue
  1440. * @action: action for processing
  1441. *
  1442. * This function issues a special IDE device request
  1443. * onto the request queue.
  1444. *
  1445. * If action is ide_wait, then the rq is queued at the end of the
  1446. * request queue, and the function sleeps until it has been processed.
  1447. * This is for use when invoked from an ioctl handler.
  1448. *
  1449. * If action is ide_preempt, then the rq is queued at the head of
  1450. * the request queue, displacing the currently-being-processed
  1451. * request and this function returns immediately without waiting
  1452. * for the new rq to be completed. This is VERY DANGEROUS, and is
  1453. * intended for careful use by the ATAPI tape/cdrom driver code.
  1454. *
  1455. * If action is ide_end, then the rq is queued at the end of the
  1456. * request queue, and the function returns immediately without waiting
  1457. * for the new rq to be completed. This is again intended for careful
  1458. * use by the ATAPI tape/cdrom driver code.
  1459. */
  1460. int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
  1461. {
  1462. unsigned long flags;
  1463. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  1464. DECLARE_COMPLETION(wait);
  1465. int where = ELEVATOR_INSERT_BACK, err;
  1466. int must_wait = (action == ide_wait || action == ide_head_wait);
  1467. rq->errors = 0;
  1468. rq->rq_status = RQ_ACTIVE;
  1469. /*
  1470. * we need to hold an extra reference to request for safe inspection
  1471. * after completion
  1472. */
  1473. if (must_wait) {
  1474. rq->ref_count++;
  1475. rq->waiting = &wait;
  1476. rq->end_io = blk_end_sync_rq;
  1477. }
  1478. spin_lock_irqsave(&ide_lock, flags);
  1479. if (action == ide_preempt)
  1480. hwgroup->rq = NULL;
  1481. if (action == ide_preempt || action == ide_head_wait) {
  1482. where = ELEVATOR_INSERT_FRONT;
  1483. rq->flags |= REQ_PREEMPT;
  1484. }
  1485. __elv_add_request(drive->queue, rq, where, 0);
  1486. ide_do_request(hwgroup, IDE_NO_IRQ);
  1487. spin_unlock_irqrestore(&ide_lock, flags);
  1488. err = 0;
  1489. if (must_wait) {
  1490. wait_for_completion(&wait);
  1491. rq->waiting = NULL;
  1492. if (rq->errors)
  1493. err = -EIO;
  1494. blk_put_request(rq);
  1495. }
  1496. return err;
  1497. }
  1498. EXPORT_SYMBOL(ide_do_drive_cmd);