ide-io.c 50 KB

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