ide-io.c 50 KB

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