ide-io.c 52 KB

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