ide-io.c 47 KB

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