ide-io.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  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. int error = 0;
  58. if (uptodate <= 0)
  59. error = uptodate ? uptodate : -EIO;
  60. /*
  61. * if failfast is set on a request, override number of sectors and
  62. * complete the whole request right now
  63. */
  64. if (blk_noretry_request(rq) && error)
  65. nr_bytes = rq->hard_nr_sectors << 9;
  66. if (!blk_fs_request(rq) && error && !rq->errors)
  67. rq->errors = -EIO;
  68. /*
  69. * decide whether to reenable DMA -- 3 is a random magic for now,
  70. * if we DMA timeout more than 3 times, just stay in PIO
  71. */
  72. if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
  73. drive->state = 0;
  74. ide_dma_on(drive);
  75. }
  76. if (!__blk_end_request(rq, error, nr_bytes)) {
  77. if (dequeue)
  78. HWGROUP(drive)->rq = NULL;
  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, 1);
  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->tf.command = WIN_FLUSH_CACHE_EXT;
  167. else
  168. args->tf.command = WIN_FLUSH_CACHE;
  169. goto out_do_tf;
  170. case idedisk_pm_standby: /* Suspend step 2 (standby) */
  171. args->tf.command = WIN_STANDBYNOW1;
  172. goto out_do_tf;
  173. case idedisk_pm_restore_pio: /* Resume step 1 (restore PIO) */
  174. ide_set_max_pio(drive);
  175. /*
  176. * skip idedisk_pm_idle for ATAPI devices
  177. */
  178. if (drive->media != ide_disk)
  179. pm->pm_step = ide_pm_restore_dma;
  180. else
  181. ide_complete_power_step(drive, rq, 0, 0);
  182. return ide_stopped;
  183. case idedisk_pm_idle: /* Resume step 2 (idle) */
  184. args->tf.command = WIN_IDLEIMMEDIATE;
  185. goto out_do_tf;
  186. case ide_pm_restore_dma: /* Resume step 3 (restore DMA) */
  187. /*
  188. * Right now, all we do is call ide_set_dma(drive),
  189. * we could be smarter and check for current xfer_speed
  190. * in struct drive etc...
  191. */
  192. if (drive->hwif->dma_ops == NULL)
  193. break;
  194. /*
  195. * TODO: respect ->using_dma setting
  196. */
  197. ide_set_dma(drive);
  198. break;
  199. }
  200. pm->pm_step = ide_pm_state_completed;
  201. return ide_stopped;
  202. out_do_tf:
  203. args->tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  204. args->data_phase = TASKFILE_NO_DATA;
  205. return do_rw_taskfile(drive, args);
  206. }
  207. /**
  208. * ide_end_dequeued_request - complete an IDE I/O
  209. * @drive: IDE device for the I/O
  210. * @uptodate:
  211. * @nr_sectors: number of sectors completed
  212. *
  213. * Complete an I/O that is no longer on the request queue. This
  214. * typically occurs when we pull the request and issue a REQUEST_SENSE.
  215. * We must still finish the old request but we must not tamper with the
  216. * queue in the meantime.
  217. *
  218. * NOTE: This path does not handle barrier, but barrier is not supported
  219. * on ide-cd anyway.
  220. */
  221. int ide_end_dequeued_request(ide_drive_t *drive, struct request *rq,
  222. int uptodate, int nr_sectors)
  223. {
  224. unsigned long flags;
  225. int ret;
  226. spin_lock_irqsave(&ide_lock, flags);
  227. BUG_ON(!blk_rq_started(rq));
  228. ret = __ide_end_request(drive, rq, uptodate, nr_sectors << 9, 0);
  229. spin_unlock_irqrestore(&ide_lock, flags);
  230. return ret;
  231. }
  232. EXPORT_SYMBOL_GPL(ide_end_dequeued_request);
  233. /**
  234. * ide_complete_pm_request - end the current Power Management request
  235. * @drive: target drive
  236. * @rq: request
  237. *
  238. * This function cleans up the current PM request and stops the queue
  239. * if necessary.
  240. */
  241. static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
  242. {
  243. unsigned long flags;
  244. #ifdef DEBUG_PM
  245. printk("%s: completing PM request, %s\n", drive->name,
  246. blk_pm_suspend_request(rq) ? "suspend" : "resume");
  247. #endif
  248. spin_lock_irqsave(&ide_lock, flags);
  249. if (blk_pm_suspend_request(rq)) {
  250. blk_stop_queue(drive->queue);
  251. } else {
  252. drive->blocked = 0;
  253. blk_start_queue(drive->queue);
  254. }
  255. HWGROUP(drive)->rq = NULL;
  256. if (__blk_end_request(rq, 0, 0))
  257. BUG();
  258. spin_unlock_irqrestore(&ide_lock, flags);
  259. }
  260. void ide_tf_read(ide_drive_t *drive, ide_task_t *task)
  261. {
  262. ide_hwif_t *hwif = drive->hwif;
  263. struct ide_io_ports *io_ports = &hwif->io_ports;
  264. struct ide_taskfile *tf = &task->tf;
  265. if (task->tf_flags & IDE_TFLAG_IN_DATA) {
  266. u16 data = hwif->INW(io_ports->data_addr);
  267. tf->data = data & 0xff;
  268. tf->hob_data = (data >> 8) & 0xff;
  269. }
  270. /* be sure we're looking at the low order bits */
  271. hwif->OUTB(drive->ctl & ~0x80, io_ports->ctl_addr);
  272. if (task->tf_flags & IDE_TFLAG_IN_NSECT)
  273. tf->nsect = hwif->INB(io_ports->nsect_addr);
  274. if (task->tf_flags & IDE_TFLAG_IN_LBAL)
  275. tf->lbal = hwif->INB(io_ports->lbal_addr);
  276. if (task->tf_flags & IDE_TFLAG_IN_LBAM)
  277. tf->lbam = hwif->INB(io_ports->lbam_addr);
  278. if (task->tf_flags & IDE_TFLAG_IN_LBAH)
  279. tf->lbah = hwif->INB(io_ports->lbah_addr);
  280. if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
  281. tf->device = hwif->INB(io_ports->device_addr);
  282. if (task->tf_flags & IDE_TFLAG_LBA48) {
  283. hwif->OUTB(drive->ctl | 0x80, io_ports->ctl_addr);
  284. if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
  285. tf->hob_feature = hwif->INB(io_ports->feature_addr);
  286. if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
  287. tf->hob_nsect = hwif->INB(io_ports->nsect_addr);
  288. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
  289. tf->hob_lbal = hwif->INB(io_ports->lbal_addr);
  290. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
  291. tf->hob_lbam = hwif->INB(io_ports->lbam_addr);
  292. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
  293. tf->hob_lbah = hwif->INB(io_ports->lbah_addr);
  294. }
  295. }
  296. /**
  297. * ide_end_drive_cmd - end an explicit drive command
  298. * @drive: command
  299. * @stat: status bits
  300. * @err: error bits
  301. *
  302. * Clean up after success/failure of an explicit drive command.
  303. * These get thrown onto the queue so they are synchronized with
  304. * real I/O operations on the drive.
  305. *
  306. * In LBA48 mode we have to read the register set twice to get
  307. * all the extra information out.
  308. */
  309. void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
  310. {
  311. unsigned long flags;
  312. struct request *rq;
  313. spin_lock_irqsave(&ide_lock, flags);
  314. rq = HWGROUP(drive)->rq;
  315. spin_unlock_irqrestore(&ide_lock, flags);
  316. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  317. ide_task_t *task = (ide_task_t *)rq->special;
  318. if (rq->errors == 0)
  319. rq->errors = !OK_STAT(stat, READY_STAT, BAD_STAT);
  320. if (task) {
  321. struct ide_taskfile *tf = &task->tf;
  322. tf->error = err;
  323. tf->status = stat;
  324. ide_tf_read(drive, task);
  325. if (task->tf_flags & IDE_TFLAG_DYN)
  326. kfree(task);
  327. }
  328. } else if (blk_pm_request(rq)) {
  329. struct request_pm_state *pm = rq->data;
  330. #ifdef DEBUG_PM
  331. printk("%s: complete_power_step(step: %d, stat: %x, err: %x)\n",
  332. drive->name, rq->pm->pm_step, stat, err);
  333. #endif
  334. ide_complete_power_step(drive, rq, stat, err);
  335. if (pm->pm_step == ide_pm_state_completed)
  336. ide_complete_pm_request(drive, rq);
  337. return;
  338. }
  339. spin_lock_irqsave(&ide_lock, flags);
  340. HWGROUP(drive)->rq = NULL;
  341. rq->errors = err;
  342. if (unlikely(__blk_end_request(rq, (rq->errors ? -EIO : 0),
  343. blk_rq_bytes(rq))))
  344. BUG();
  345. spin_unlock_irqrestore(&ide_lock, flags);
  346. }
  347. EXPORT_SYMBOL(ide_end_drive_cmd);
  348. /**
  349. * try_to_flush_leftover_data - flush junk
  350. * @drive: drive to flush
  351. *
  352. * try_to_flush_leftover_data() is invoked in response to a drive
  353. * unexpectedly having its DRQ_STAT bit set. As an alternative to
  354. * resetting the drive, this routine tries to clear the condition
  355. * by read a sector's worth of data from the drive. Of course,
  356. * this may not help if the drive is *waiting* for data from *us*.
  357. */
  358. static void try_to_flush_leftover_data (ide_drive_t *drive)
  359. {
  360. int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
  361. if (drive->media != ide_disk)
  362. return;
  363. while (i > 0) {
  364. u32 buffer[16];
  365. u32 wcount = (i > 16) ? 16 : i;
  366. i -= wcount;
  367. drive->hwif->ata_input_data(drive, NULL, buffer, wcount);
  368. }
  369. }
  370. static void ide_kill_rq(ide_drive_t *drive, struct request *rq)
  371. {
  372. if (rq->rq_disk) {
  373. ide_driver_t *drv;
  374. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  375. drv->end_request(drive, 0, 0);
  376. } else
  377. ide_end_request(drive, 0, 0);
  378. }
  379. static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  380. {
  381. ide_hwif_t *hwif = drive->hwif;
  382. if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
  383. /* other bits are useless when BUSY */
  384. rq->errors |= ERROR_RESET;
  385. } else if (stat & ERR_STAT) {
  386. /* err has different meaning on cdrom and tape */
  387. if (err == ABRT_ERR) {
  388. if (drive->select.b.lba &&
  389. /* some newer drives don't support WIN_SPECIFY */
  390. hwif->INB(hwif->io_ports.command_addr) ==
  391. WIN_SPECIFY)
  392. return ide_stopped;
  393. } else if ((err & BAD_CRC) == BAD_CRC) {
  394. /* UDMA crc error, just retry the operation */
  395. drive->crc_count++;
  396. } else if (err & (BBD_ERR | ECC_ERR)) {
  397. /* retries won't help these */
  398. rq->errors = ERROR_MAX;
  399. } else if (err & TRK0_ERR) {
  400. /* help it find track zero */
  401. rq->errors |= ERROR_RECAL;
  402. }
  403. }
  404. if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ &&
  405. (hwif->host_flags & IDE_HFLAG_ERROR_STOPS_FIFO) == 0)
  406. try_to_flush_leftover_data(drive);
  407. if (rq->errors >= ERROR_MAX || blk_noretry_request(rq)) {
  408. ide_kill_rq(drive, rq);
  409. return ide_stopped;
  410. }
  411. if (ide_read_status(drive) & (BUSY_STAT | DRQ_STAT))
  412. rq->errors |= ERROR_RESET;
  413. if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
  414. ++rq->errors;
  415. return ide_do_reset(drive);
  416. }
  417. if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
  418. drive->special.b.recalibrate = 1;
  419. ++rq->errors;
  420. return ide_stopped;
  421. }
  422. static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  423. {
  424. ide_hwif_t *hwif = drive->hwif;
  425. if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
  426. /* other bits are useless when BUSY */
  427. rq->errors |= ERROR_RESET;
  428. } else {
  429. /* add decoding error stuff */
  430. }
  431. if (ide_read_status(drive) & (BUSY_STAT | DRQ_STAT))
  432. /* force an abort */
  433. hwif->OUTB(WIN_IDLEIMMEDIATE, hwif->io_ports.command_addr);
  434. if (rq->errors >= ERROR_MAX) {
  435. ide_kill_rq(drive, rq);
  436. } else {
  437. if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
  438. ++rq->errors;
  439. return ide_do_reset(drive);
  440. }
  441. ++rq->errors;
  442. }
  443. return ide_stopped;
  444. }
  445. ide_startstop_t
  446. __ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  447. {
  448. if (drive->media == ide_disk)
  449. return ide_ata_error(drive, rq, stat, err);
  450. return ide_atapi_error(drive, rq, stat, err);
  451. }
  452. EXPORT_SYMBOL_GPL(__ide_error);
  453. /**
  454. * ide_error - handle an error on the IDE
  455. * @drive: drive the error occurred on
  456. * @msg: message to report
  457. * @stat: status bits
  458. *
  459. * ide_error() takes action based on the error returned by the drive.
  460. * For normal I/O that may well include retries. We deal with
  461. * both new-style (taskfile) and old style command handling here.
  462. * In the case of taskfile command handling there is work left to
  463. * do
  464. */
  465. ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
  466. {
  467. struct request *rq;
  468. u8 err;
  469. err = ide_dump_status(drive, msg, stat);
  470. if ((rq = HWGROUP(drive)->rq) == NULL)
  471. return ide_stopped;
  472. /* retry only "normal" I/O: */
  473. if (!blk_fs_request(rq)) {
  474. rq->errors = 1;
  475. ide_end_drive_cmd(drive, stat, err);
  476. return ide_stopped;
  477. }
  478. if (rq->rq_disk) {
  479. ide_driver_t *drv;
  480. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  481. return drv->error(drive, rq, stat, err);
  482. } else
  483. return __ide_error(drive, rq, stat, err);
  484. }
  485. EXPORT_SYMBOL_GPL(ide_error);
  486. ide_startstop_t __ide_abort(ide_drive_t *drive, struct request *rq)
  487. {
  488. if (drive->media != ide_disk)
  489. rq->errors |= ERROR_RESET;
  490. ide_kill_rq(drive, rq);
  491. return ide_stopped;
  492. }
  493. EXPORT_SYMBOL_GPL(__ide_abort);
  494. /**
  495. * ide_abort - abort pending IDE operations
  496. * @drive: drive the error occurred on
  497. * @msg: message to report
  498. *
  499. * ide_abort kills and cleans up when we are about to do a
  500. * host initiated reset on active commands. Longer term we
  501. * want handlers to have sensible abort handling themselves
  502. *
  503. * This differs fundamentally from ide_error because in
  504. * this case the command is doing just fine when we
  505. * blow it away.
  506. */
  507. ide_startstop_t ide_abort(ide_drive_t *drive, const char *msg)
  508. {
  509. struct request *rq;
  510. if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
  511. return ide_stopped;
  512. /* retry only "normal" I/O: */
  513. if (!blk_fs_request(rq)) {
  514. rq->errors = 1;
  515. ide_end_drive_cmd(drive, BUSY_STAT, 0);
  516. return ide_stopped;
  517. }
  518. if (rq->rq_disk) {
  519. ide_driver_t *drv;
  520. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  521. return drv->abort(drive, rq);
  522. } else
  523. return __ide_abort(drive, rq);
  524. }
  525. static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  526. {
  527. tf->nsect = drive->sect;
  528. tf->lbal = drive->sect;
  529. tf->lbam = drive->cyl;
  530. tf->lbah = drive->cyl >> 8;
  531. tf->device = ((drive->head - 1) | drive->select.all) & ~ATA_LBA;
  532. tf->command = WIN_SPECIFY;
  533. }
  534. static void ide_tf_set_restore_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  535. {
  536. tf->nsect = drive->sect;
  537. tf->command = WIN_RESTORE;
  538. }
  539. static void ide_tf_set_setmult_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  540. {
  541. tf->nsect = drive->mult_req;
  542. tf->command = WIN_SETMULT;
  543. }
  544. static ide_startstop_t ide_disk_special(ide_drive_t *drive)
  545. {
  546. special_t *s = &drive->special;
  547. ide_task_t args;
  548. memset(&args, 0, sizeof(ide_task_t));
  549. args.data_phase = TASKFILE_NO_DATA;
  550. if (s->b.set_geometry) {
  551. s->b.set_geometry = 0;
  552. ide_tf_set_specify_cmd(drive, &args.tf);
  553. } else if (s->b.recalibrate) {
  554. s->b.recalibrate = 0;
  555. ide_tf_set_restore_cmd(drive, &args.tf);
  556. } else if (s->b.set_multmode) {
  557. s->b.set_multmode = 0;
  558. if (drive->mult_req > drive->id->max_multsect)
  559. drive->mult_req = drive->id->max_multsect;
  560. ide_tf_set_setmult_cmd(drive, &args.tf);
  561. } else if (s->all) {
  562. int special = s->all;
  563. s->all = 0;
  564. printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
  565. return ide_stopped;
  566. }
  567. args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE |
  568. IDE_TFLAG_CUSTOM_HANDLER;
  569. do_rw_taskfile(drive, &args);
  570. return ide_started;
  571. }
  572. /*
  573. * handle HDIO_SET_PIO_MODE ioctl abusers here, eventually it will go away
  574. */
  575. static int set_pio_mode_abuse(ide_hwif_t *hwif, u8 req_pio)
  576. {
  577. switch (req_pio) {
  578. case 202:
  579. case 201:
  580. case 200:
  581. case 102:
  582. case 101:
  583. case 100:
  584. return (hwif->host_flags & IDE_HFLAG_ABUSE_DMA_MODES) ? 1 : 0;
  585. case 9:
  586. case 8:
  587. return (hwif->host_flags & IDE_HFLAG_ABUSE_PREFETCH) ? 1 : 0;
  588. case 7:
  589. case 6:
  590. return (hwif->host_flags & IDE_HFLAG_ABUSE_FAST_DEVSEL) ? 1 : 0;
  591. default:
  592. return 0;
  593. }
  594. }
  595. /**
  596. * do_special - issue some special commands
  597. * @drive: drive the command is for
  598. *
  599. * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
  600. * commands to a drive. It used to do much more, but has been scaled
  601. * back.
  602. */
  603. static ide_startstop_t do_special (ide_drive_t *drive)
  604. {
  605. special_t *s = &drive->special;
  606. #ifdef DEBUG
  607. printk("%s: do_special: 0x%02x\n", drive->name, s->all);
  608. #endif
  609. if (s->b.set_tune) {
  610. ide_hwif_t *hwif = drive->hwif;
  611. const struct ide_port_ops *port_ops = hwif->port_ops;
  612. u8 req_pio = drive->tune_req;
  613. s->b.set_tune = 0;
  614. if (set_pio_mode_abuse(drive->hwif, req_pio)) {
  615. /*
  616. * take ide_lock for drive->[no_]unmask/[no_]io_32bit
  617. */
  618. if (req_pio == 8 || req_pio == 9) {
  619. unsigned long flags;
  620. spin_lock_irqsave(&ide_lock, flags);
  621. port_ops->set_pio_mode(drive, req_pio);
  622. spin_unlock_irqrestore(&ide_lock, flags);
  623. } else
  624. port_ops->set_pio_mode(drive, req_pio);
  625. } else {
  626. int keep_dma = drive->using_dma;
  627. ide_set_pio(drive, req_pio);
  628. if (hwif->host_flags & IDE_HFLAG_SET_PIO_MODE_KEEP_DMA) {
  629. if (keep_dma)
  630. ide_dma_on(drive);
  631. }
  632. }
  633. return ide_stopped;
  634. } else {
  635. if (drive->media == ide_disk)
  636. return ide_disk_special(drive);
  637. s->all = 0;
  638. drive->mult_req = 0;
  639. return ide_stopped;
  640. }
  641. }
  642. void ide_map_sg(ide_drive_t *drive, struct request *rq)
  643. {
  644. ide_hwif_t *hwif = drive->hwif;
  645. struct scatterlist *sg = hwif->sg_table;
  646. if (hwif->sg_mapped) /* needed by ide-scsi */
  647. return;
  648. if (rq->cmd_type != REQ_TYPE_ATA_TASKFILE) {
  649. hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
  650. } else {
  651. sg_init_one(sg, rq->buffer, rq->nr_sectors * SECTOR_SIZE);
  652. hwif->sg_nents = 1;
  653. }
  654. }
  655. EXPORT_SYMBOL_GPL(ide_map_sg);
  656. void ide_init_sg_cmd(ide_drive_t *drive, struct request *rq)
  657. {
  658. ide_hwif_t *hwif = drive->hwif;
  659. hwif->nsect = hwif->nleft = rq->nr_sectors;
  660. hwif->cursg_ofs = 0;
  661. hwif->cursg = NULL;
  662. }
  663. EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
  664. /**
  665. * execute_drive_command - issue special drive command
  666. * @drive: the drive to issue the command on
  667. * @rq: the request structure holding the command
  668. *
  669. * execute_drive_cmd() issues a special drive command, usually
  670. * initiated by ioctl() from the external hdparm program. The
  671. * command can be a drive command, drive task or taskfile
  672. * operation. Weirdly you can call it with NULL to wait for
  673. * all commands to finish. Don't do this as that is due to change
  674. */
  675. static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
  676. struct request *rq)
  677. {
  678. ide_hwif_t *hwif = HWIF(drive);
  679. ide_task_t *task = rq->special;
  680. if (task) {
  681. hwif->data_phase = task->data_phase;
  682. switch (hwif->data_phase) {
  683. case TASKFILE_MULTI_OUT:
  684. case TASKFILE_OUT:
  685. case TASKFILE_MULTI_IN:
  686. case TASKFILE_IN:
  687. ide_init_sg_cmd(drive, rq);
  688. ide_map_sg(drive, rq);
  689. default:
  690. break;
  691. }
  692. return do_rw_taskfile(drive, task);
  693. }
  694. /*
  695. * NULL is actually a valid way of waiting for
  696. * all current requests to be flushed from the queue.
  697. */
  698. #ifdef DEBUG
  699. printk("%s: DRIVE_CMD (null)\n", drive->name);
  700. #endif
  701. ide_end_drive_cmd(drive, ide_read_status(drive), ide_read_error(drive));
  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->dma_ops->dma_end(drive);
  1070. ret = ide_error(drive, "dma timeout error",
  1071. ide_read_status(drive));
  1072. } else {
  1073. printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
  1074. hwif->dma_ops->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. hwif->dma_ops->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",
  1185. ide_read_status(drive));
  1186. }
  1187. drive->service_time = jiffies - drive->service_start;
  1188. spin_lock_irq(&ide_lock);
  1189. enable_irq(hwif->irq);
  1190. if (startstop == ide_stopped)
  1191. hwgroup->busy = 0;
  1192. }
  1193. }
  1194. ide_do_request(hwgroup, IDE_NO_IRQ);
  1195. spin_unlock_irqrestore(&ide_lock, flags);
  1196. }
  1197. /**
  1198. * unexpected_intr - handle an unexpected IDE interrupt
  1199. * @irq: interrupt line
  1200. * @hwgroup: hwgroup being processed
  1201. *
  1202. * There's nothing really useful we can do with an unexpected interrupt,
  1203. * other than reading the status register (to clear it), and logging it.
  1204. * There should be no way that an irq can happen before we're ready for it,
  1205. * so we needn't worry much about losing an "important" interrupt here.
  1206. *
  1207. * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
  1208. * the drive enters "idle", "standby", or "sleep" mode, so if the status
  1209. * looks "good", we just ignore the interrupt completely.
  1210. *
  1211. * This routine assumes __cli() is in effect when called.
  1212. *
  1213. * If an unexpected interrupt happens on irq15 while we are handling irq14
  1214. * and if the two interfaces are "serialized" (CMD640), then it looks like
  1215. * we could screw up by interfering with a new request being set up for
  1216. * irq15.
  1217. *
  1218. * In reality, this is a non-issue. The new command is not sent unless
  1219. * the drive is ready to accept one, in which case we know the drive is
  1220. * not trying to interrupt us. And ide_set_handler() is always invoked
  1221. * before completing the issuance of any new drive command, so we will not
  1222. * be accidentally invoked as a result of any valid command completion
  1223. * interrupt.
  1224. *
  1225. * Note that we must walk the entire hwgroup here. We know which hwif
  1226. * is doing the current command, but we don't know which hwif burped
  1227. * mysteriously.
  1228. */
  1229. static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
  1230. {
  1231. u8 stat;
  1232. ide_hwif_t *hwif = hwgroup->hwif;
  1233. /*
  1234. * handle the unexpected interrupt
  1235. */
  1236. do {
  1237. if (hwif->irq == irq) {
  1238. stat = hwif->INB(hwif->io_ports.status_addr);
  1239. if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
  1240. /* Try to not flood the console with msgs */
  1241. static unsigned long last_msgtime, count;
  1242. ++count;
  1243. if (time_after(jiffies, last_msgtime + HZ)) {
  1244. last_msgtime = jiffies;
  1245. printk(KERN_ERR "%s%s: unexpected interrupt, "
  1246. "status=0x%02x, count=%ld\n",
  1247. hwif->name,
  1248. (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
  1249. }
  1250. }
  1251. }
  1252. } while ((hwif = hwif->next) != hwgroup->hwif);
  1253. }
  1254. /**
  1255. * ide_intr - default IDE interrupt handler
  1256. * @irq: interrupt number
  1257. * @dev_id: hwif group
  1258. * @regs: unused weirdness from the kernel irq layer
  1259. *
  1260. * This is the default IRQ handler for the IDE layer. You should
  1261. * not need to override it. If you do be aware it is subtle in
  1262. * places
  1263. *
  1264. * hwgroup->hwif is the interface in the group currently performing
  1265. * a command. hwgroup->drive is the drive and hwgroup->handler is
  1266. * the IRQ handler to call. As we issue a command the handlers
  1267. * step through multiple states, reassigning the handler to the
  1268. * next step in the process. Unlike a smart SCSI controller IDE
  1269. * expects the main processor to sequence the various transfer
  1270. * stages. We also manage a poll timer to catch up with most
  1271. * timeout situations. There are still a few where the handlers
  1272. * don't ever decide to give up.
  1273. *
  1274. * The handler eventually returns ide_stopped to indicate the
  1275. * request completed. At this point we issue the next request
  1276. * on the hwgroup and the process begins again.
  1277. */
  1278. irqreturn_t ide_intr (int irq, void *dev_id)
  1279. {
  1280. unsigned long flags;
  1281. ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
  1282. ide_hwif_t *hwif;
  1283. ide_drive_t *drive;
  1284. ide_handler_t *handler;
  1285. ide_startstop_t startstop;
  1286. spin_lock_irqsave(&ide_lock, flags);
  1287. hwif = hwgroup->hwif;
  1288. if (!ide_ack_intr(hwif)) {
  1289. spin_unlock_irqrestore(&ide_lock, flags);
  1290. return IRQ_NONE;
  1291. }
  1292. if ((handler = hwgroup->handler) == NULL || hwgroup->polling) {
  1293. /*
  1294. * Not expecting an interrupt from this drive.
  1295. * That means this could be:
  1296. * (1) an interrupt from another PCI device
  1297. * sharing the same PCI INT# as us.
  1298. * or (2) a drive just entered sleep or standby mode,
  1299. * and is interrupting to let us know.
  1300. * or (3) a spurious interrupt of unknown origin.
  1301. *
  1302. * For PCI, we cannot tell the difference,
  1303. * so in that case we just ignore it and hope it goes away.
  1304. *
  1305. * FIXME: unexpected_intr should be hwif-> then we can
  1306. * remove all the ifdef PCI crap
  1307. */
  1308. #ifdef CONFIG_BLK_DEV_IDEPCI
  1309. if (hwif->chipset != ide_pci)
  1310. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1311. {
  1312. /*
  1313. * Probably not a shared PCI interrupt,
  1314. * so we can safely try to do something about it:
  1315. */
  1316. unexpected_intr(irq, hwgroup);
  1317. #ifdef CONFIG_BLK_DEV_IDEPCI
  1318. } else {
  1319. /*
  1320. * Whack the status register, just in case
  1321. * we have a leftover pending IRQ.
  1322. */
  1323. (void) hwif->INB(hwif->io_ports.status_addr);
  1324. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1325. }
  1326. spin_unlock_irqrestore(&ide_lock, flags);
  1327. return IRQ_NONE;
  1328. }
  1329. drive = hwgroup->drive;
  1330. if (!drive) {
  1331. /*
  1332. * This should NEVER happen, and there isn't much
  1333. * we could do about it here.
  1334. *
  1335. * [Note - this can occur if the drive is hot unplugged]
  1336. */
  1337. spin_unlock_irqrestore(&ide_lock, flags);
  1338. return IRQ_HANDLED;
  1339. }
  1340. if (!drive_is_ready(drive)) {
  1341. /*
  1342. * This happens regularly when we share a PCI IRQ with
  1343. * another device. Unfortunately, it can also happen
  1344. * with some buggy drives that trigger the IRQ before
  1345. * their status register is up to date. Hopefully we have
  1346. * enough advance overhead that the latter isn't a problem.
  1347. */
  1348. spin_unlock_irqrestore(&ide_lock, flags);
  1349. return IRQ_NONE;
  1350. }
  1351. if (!hwgroup->busy) {
  1352. hwgroup->busy = 1; /* paranoia */
  1353. printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
  1354. }
  1355. hwgroup->handler = NULL;
  1356. hwgroup->req_gen++;
  1357. del_timer(&hwgroup->timer);
  1358. spin_unlock(&ide_lock);
  1359. /* Some controllers might set DMA INTR no matter DMA or PIO;
  1360. * bmdma status might need to be cleared even for
  1361. * PIO interrupts to prevent spurious/lost irq.
  1362. */
  1363. if (hwif->ide_dma_clear_irq && !(drive->waiting_for_dma))
  1364. /* ide_dma_end() needs bmdma status for error checking.
  1365. * So, skip clearing bmdma status here and leave it
  1366. * to ide_dma_end() if this is dma interrupt.
  1367. */
  1368. hwif->ide_dma_clear_irq(drive);
  1369. if (drive->unmask)
  1370. local_irq_enable_in_hardirq();
  1371. /* service this interrupt, may set handler for next interrupt */
  1372. startstop = handler(drive);
  1373. spin_lock_irq(&ide_lock);
  1374. /*
  1375. * Note that handler() may have set things up for another
  1376. * interrupt to occur soon, but it cannot happen until
  1377. * we exit from this routine, because it will be the
  1378. * same irq as is currently being serviced here, and Linux
  1379. * won't allow another of the same (on any CPU) until we return.
  1380. */
  1381. drive->service_time = jiffies - drive->service_start;
  1382. if (startstop == ide_stopped) {
  1383. if (hwgroup->handler == NULL) { /* paranoia */
  1384. hwgroup->busy = 0;
  1385. ide_do_request(hwgroup, hwif->irq);
  1386. } else {
  1387. printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
  1388. "on exit\n", drive->name);
  1389. }
  1390. }
  1391. spin_unlock_irqrestore(&ide_lock, flags);
  1392. return IRQ_HANDLED;
  1393. }
  1394. /**
  1395. * ide_init_drive_cmd - initialize a drive command request
  1396. * @rq: request object
  1397. *
  1398. * Initialize a request before we fill it in and send it down to
  1399. * ide_do_drive_cmd. Commands must be set up by this function. Right
  1400. * now it doesn't do a lot, but if that changes abusers will have a
  1401. * nasty surprise.
  1402. */
  1403. void ide_init_drive_cmd (struct request *rq)
  1404. {
  1405. memset(rq, 0, sizeof(*rq));
  1406. rq->ref_count = 1;
  1407. }
  1408. EXPORT_SYMBOL(ide_init_drive_cmd);
  1409. /**
  1410. * ide_do_drive_cmd - issue IDE special command
  1411. * @drive: device to issue command
  1412. * @rq: request to issue
  1413. * @action: action for processing
  1414. *
  1415. * This function issues a special IDE device request
  1416. * onto the request queue.
  1417. *
  1418. * If action is ide_wait, then the rq is queued at the end of the
  1419. * request queue, and the function sleeps until it has been processed.
  1420. * This is for use when invoked from an ioctl handler.
  1421. *
  1422. * If action is ide_preempt, then the rq is queued at the head of
  1423. * the request queue, displacing the currently-being-processed
  1424. * request and this function returns immediately without waiting
  1425. * for the new rq to be completed. This is VERY DANGEROUS, and is
  1426. * intended for careful use by the ATAPI tape/cdrom driver code.
  1427. *
  1428. * If action is ide_end, then the rq is queued at the end of the
  1429. * request queue, and the function returns immediately without waiting
  1430. * for the new rq to be completed. This is again intended for careful
  1431. * use by the ATAPI tape/cdrom driver code.
  1432. */
  1433. int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
  1434. {
  1435. unsigned long flags;
  1436. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  1437. DECLARE_COMPLETION_ONSTACK(wait);
  1438. int where = ELEVATOR_INSERT_BACK, err;
  1439. int must_wait = (action == ide_wait || action == ide_head_wait);
  1440. rq->errors = 0;
  1441. /*
  1442. * we need to hold an extra reference to request for safe inspection
  1443. * after completion
  1444. */
  1445. if (must_wait) {
  1446. rq->ref_count++;
  1447. rq->end_io_data = &wait;
  1448. rq->end_io = blk_end_sync_rq;
  1449. }
  1450. spin_lock_irqsave(&ide_lock, flags);
  1451. if (action == ide_preempt)
  1452. hwgroup->rq = NULL;
  1453. if (action == ide_preempt || action == ide_head_wait) {
  1454. where = ELEVATOR_INSERT_FRONT;
  1455. rq->cmd_flags |= REQ_PREEMPT;
  1456. }
  1457. __elv_add_request(drive->queue, rq, where, 0);
  1458. ide_do_request(hwgroup, IDE_NO_IRQ);
  1459. spin_unlock_irqrestore(&ide_lock, flags);
  1460. err = 0;
  1461. if (must_wait) {
  1462. wait_for_completion(&wait);
  1463. if (rq->errors)
  1464. err = -EIO;
  1465. blk_put_request(rq);
  1466. }
  1467. return err;
  1468. }
  1469. EXPORT_SYMBOL(ide_do_drive_cmd);
  1470. void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount, u8 dma)
  1471. {
  1472. ide_task_t task;
  1473. memset(&task, 0, sizeof(task));
  1474. task.tf_flags = IDE_TFLAG_OUT_LBAH | IDE_TFLAG_OUT_LBAM |
  1475. IDE_TFLAG_OUT_FEATURE | tf_flags;
  1476. task.tf.feature = dma; /* Use PIO/DMA */
  1477. task.tf.lbam = bcount & 0xff;
  1478. task.tf.lbah = (bcount >> 8) & 0xff;
  1479. ide_tf_load(drive, &task);
  1480. }
  1481. EXPORT_SYMBOL_GPL(ide_pktcmd_tf_load);