ide-io.c 51 KB

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