ide-io.c 48 KB

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