ide-io.c 49 KB

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