ide-io.c 51 KB

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