ide-io.c 47 KB

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