ide-io.c 50 KB

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