ide-io.c 51 KB

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