ide-io.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  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_host_set == 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. u8 req_pio = drive->tune_req;
  618. s->b.set_tune = 0;
  619. if (set_pio_mode_abuse(drive->hwif, req_pio)) {
  620. if (hwif->set_pio_mode == NULL)
  621. return ide_stopped;
  622. /*
  623. * take ide_lock for drive->[no_]unmask/[no_]io_32bit
  624. */
  625. if (req_pio == 8 || req_pio == 9) {
  626. unsigned long flags;
  627. spin_lock_irqsave(&ide_lock, flags);
  628. hwif->set_pio_mode(drive, req_pio);
  629. spin_unlock_irqrestore(&ide_lock, flags);
  630. } else
  631. hwif->set_pio_mode(drive, req_pio);
  632. } else {
  633. int keep_dma = drive->using_dma;
  634. ide_set_pio(drive, req_pio);
  635. if (hwif->host_flags & IDE_HFLAG_SET_PIO_MODE_KEEP_DMA) {
  636. if (keep_dma)
  637. ide_dma_on(drive);
  638. }
  639. }
  640. return ide_stopped;
  641. } else {
  642. if (drive->media == ide_disk)
  643. return ide_disk_special(drive);
  644. s->all = 0;
  645. drive->mult_req = 0;
  646. return ide_stopped;
  647. }
  648. }
  649. void ide_map_sg(ide_drive_t *drive, struct request *rq)
  650. {
  651. ide_hwif_t *hwif = drive->hwif;
  652. struct scatterlist *sg = hwif->sg_table;
  653. if (hwif->sg_mapped) /* needed by ide-scsi */
  654. return;
  655. if (rq->cmd_type != REQ_TYPE_ATA_TASKFILE) {
  656. hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
  657. } else {
  658. sg_init_one(sg, rq->buffer, rq->nr_sectors * SECTOR_SIZE);
  659. hwif->sg_nents = 1;
  660. }
  661. }
  662. EXPORT_SYMBOL_GPL(ide_map_sg);
  663. void ide_init_sg_cmd(ide_drive_t *drive, struct request *rq)
  664. {
  665. ide_hwif_t *hwif = drive->hwif;
  666. hwif->nsect = hwif->nleft = rq->nr_sectors;
  667. hwif->cursg_ofs = 0;
  668. hwif->cursg = NULL;
  669. }
  670. EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
  671. /**
  672. * execute_drive_command - issue special drive command
  673. * @drive: the drive to issue the command on
  674. * @rq: the request structure holding the command
  675. *
  676. * execute_drive_cmd() issues a special drive command, usually
  677. * initiated by ioctl() from the external hdparm program. The
  678. * command can be a drive command, drive task or taskfile
  679. * operation. Weirdly you can call it with NULL to wait for
  680. * all commands to finish. Don't do this as that is due to change
  681. */
  682. static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
  683. struct request *rq)
  684. {
  685. ide_hwif_t *hwif = HWIF(drive);
  686. ide_task_t *task = rq->special;
  687. if (task) {
  688. hwif->data_phase = task->data_phase;
  689. switch (hwif->data_phase) {
  690. case TASKFILE_MULTI_OUT:
  691. case TASKFILE_OUT:
  692. case TASKFILE_MULTI_IN:
  693. case TASKFILE_IN:
  694. ide_init_sg_cmd(drive, rq);
  695. ide_map_sg(drive, rq);
  696. default:
  697. break;
  698. }
  699. return do_rw_taskfile(drive, task);
  700. }
  701. /*
  702. * NULL is actually a valid way of waiting for
  703. * all current requests to be flushed from the queue.
  704. */
  705. #ifdef DEBUG
  706. printk("%s: DRIVE_CMD (null)\n", drive->name);
  707. #endif
  708. ide_end_drive_cmd(drive, ide_read_status(drive), ide_read_error(drive));
  709. return ide_stopped;
  710. }
  711. static void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
  712. {
  713. struct request_pm_state *pm = rq->data;
  714. if (blk_pm_suspend_request(rq) &&
  715. pm->pm_step == ide_pm_state_start_suspend)
  716. /* Mark drive blocked when starting the suspend sequence. */
  717. drive->blocked = 1;
  718. else if (blk_pm_resume_request(rq) &&
  719. pm->pm_step == ide_pm_state_start_resume) {
  720. /*
  721. * The first thing we do on wakeup is to wait for BSY bit to
  722. * go away (with a looong timeout) as a drive on this hwif may
  723. * just be POSTing itself.
  724. * We do that before even selecting as the "other" device on
  725. * the bus may be broken enough to walk on our toes at this
  726. * point.
  727. */
  728. int rc;
  729. #ifdef DEBUG_PM
  730. printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
  731. #endif
  732. rc = ide_wait_not_busy(HWIF(drive), 35000);
  733. if (rc)
  734. printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
  735. SELECT_DRIVE(drive);
  736. ide_set_irq(drive, 1);
  737. rc = ide_wait_not_busy(HWIF(drive), 100000);
  738. if (rc)
  739. printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
  740. }
  741. }
  742. /**
  743. * start_request - start of I/O and command issuing for IDE
  744. *
  745. * start_request() initiates handling of a new I/O request. It
  746. * accepts commands and I/O (read/write) requests. It also does
  747. * the final remapping for weird stuff like EZDrive. Once
  748. * device mapper can work sector level the EZDrive stuff can go away
  749. *
  750. * FIXME: this function needs a rename
  751. */
  752. static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
  753. {
  754. ide_startstop_t startstop;
  755. sector_t block;
  756. BUG_ON(!blk_rq_started(rq));
  757. #ifdef DEBUG
  758. printk("%s: start_request: current=0x%08lx\n",
  759. HWIF(drive)->name, (unsigned long) rq);
  760. #endif
  761. /* bail early if we've exceeded max_failures */
  762. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  763. rq->cmd_flags |= REQ_FAILED;
  764. goto kill_rq;
  765. }
  766. block = rq->sector;
  767. if (blk_fs_request(rq) &&
  768. (drive->media == ide_disk || drive->media == ide_floppy)) {
  769. block += drive->sect0;
  770. }
  771. /* Yecch - this will shift the entire interval,
  772. possibly killing some innocent following sector */
  773. if (block == 0 && drive->remap_0_to_1 == 1)
  774. block = 1; /* redirect MBR access to EZ-Drive partn table */
  775. if (blk_pm_request(rq))
  776. ide_check_pm_state(drive, rq);
  777. SELECT_DRIVE(drive);
  778. if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
  779. printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
  780. return startstop;
  781. }
  782. if (!drive->special.all) {
  783. ide_driver_t *drv;
  784. /*
  785. * We reset the drive so we need to issue a SETFEATURES.
  786. * Do it _after_ do_special() restored device parameters.
  787. */
  788. if (drive->current_speed == 0xff)
  789. ide_config_drive_speed(drive, drive->desired_speed);
  790. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  791. return execute_drive_cmd(drive, rq);
  792. else if (blk_pm_request(rq)) {
  793. struct request_pm_state *pm = rq->data;
  794. #ifdef DEBUG_PM
  795. printk("%s: start_power_step(step: %d)\n",
  796. drive->name, rq->pm->pm_step);
  797. #endif
  798. startstop = ide_start_power_step(drive, rq);
  799. if (startstop == ide_stopped &&
  800. pm->pm_step == ide_pm_state_completed)
  801. ide_complete_pm_request(drive, rq);
  802. return startstop;
  803. }
  804. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  805. return drv->do_request(drive, rq, block);
  806. }
  807. return do_special(drive);
  808. kill_rq:
  809. ide_kill_rq(drive, rq);
  810. return ide_stopped;
  811. }
  812. /**
  813. * ide_stall_queue - pause an IDE device
  814. * @drive: drive to stall
  815. * @timeout: time to stall for (jiffies)
  816. *
  817. * ide_stall_queue() can be used by a drive to give excess bandwidth back
  818. * to the hwgroup by sleeping for timeout jiffies.
  819. */
  820. void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
  821. {
  822. if (timeout > WAIT_WORSTCASE)
  823. timeout = WAIT_WORSTCASE;
  824. drive->sleep = timeout + jiffies;
  825. drive->sleeping = 1;
  826. }
  827. EXPORT_SYMBOL(ide_stall_queue);
  828. #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
  829. /**
  830. * choose_drive - select a drive to service
  831. * @hwgroup: hardware group to select on
  832. *
  833. * choose_drive() selects the next drive which will be serviced.
  834. * This is necessary because the IDE layer can't issue commands
  835. * to both drives on the same cable, unlike SCSI.
  836. */
  837. static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
  838. {
  839. ide_drive_t *drive, *best;
  840. repeat:
  841. best = NULL;
  842. drive = hwgroup->drive;
  843. /*
  844. * drive is doing pre-flush, ordered write, post-flush sequence. even
  845. * though that is 3 requests, it must be seen as a single transaction.
  846. * we must not preempt this drive until that is complete
  847. */
  848. if (blk_queue_flushing(drive->queue)) {
  849. /*
  850. * small race where queue could get replugged during
  851. * the 3-request flush cycle, just yank the plug since
  852. * we want it to finish asap
  853. */
  854. blk_remove_plug(drive->queue);
  855. return drive;
  856. }
  857. do {
  858. if ((!drive->sleeping || time_after_eq(jiffies, drive->sleep))
  859. && !elv_queue_empty(drive->queue)) {
  860. if (!best
  861. || (drive->sleeping && (!best->sleeping || time_before(drive->sleep, best->sleep)))
  862. || (!best->sleeping && time_before(WAKEUP(drive), WAKEUP(best))))
  863. {
  864. if (!blk_queue_plugged(drive->queue))
  865. best = drive;
  866. }
  867. }
  868. } while ((drive = drive->next) != hwgroup->drive);
  869. if (best && best->nice1 && !best->sleeping && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
  870. long t = (signed long)(WAKEUP(best) - jiffies);
  871. if (t >= WAIT_MIN_SLEEP) {
  872. /*
  873. * We *may* have some time to spare, but first let's see if
  874. * someone can potentially benefit from our nice mood today..
  875. */
  876. drive = best->next;
  877. do {
  878. if (!drive->sleeping
  879. && time_before(jiffies - best->service_time, WAKEUP(drive))
  880. && time_before(WAKEUP(drive), jiffies + t))
  881. {
  882. ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
  883. goto repeat;
  884. }
  885. } while ((drive = drive->next) != best);
  886. }
  887. }
  888. return best;
  889. }
  890. /*
  891. * Issue a new request to a drive from hwgroup
  892. * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
  893. *
  894. * A hwgroup is a serialized group of IDE interfaces. Usually there is
  895. * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
  896. * may have both interfaces in a single hwgroup to "serialize" access.
  897. * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
  898. * together into one hwgroup for serialized access.
  899. *
  900. * Note also that several hwgroups can end up sharing a single IRQ,
  901. * possibly along with many other devices. This is especially common in
  902. * PCI-based systems with off-board IDE controller cards.
  903. *
  904. * The IDE driver uses the single global ide_lock spinlock to protect
  905. * access to the request queues, and to protect the hwgroup->busy flag.
  906. *
  907. * The first thread into the driver for a particular hwgroup sets the
  908. * hwgroup->busy flag to indicate that this hwgroup is now active,
  909. * and then initiates processing of the top request from the request queue.
  910. *
  911. * Other threads attempting entry notice the busy setting, and will simply
  912. * queue their new requests and exit immediately. Note that hwgroup->busy
  913. * remains set even when the driver is merely awaiting the next interrupt.
  914. * Thus, the meaning is "this hwgroup is busy processing a request".
  915. *
  916. * When processing of a request completes, the completing thread or IRQ-handler
  917. * will start the next request from the queue. If no more work remains,
  918. * the driver will clear the hwgroup->busy flag and exit.
  919. *
  920. * The ide_lock (spinlock) is used to protect all access to the
  921. * hwgroup->busy flag, but is otherwise not needed for most processing in
  922. * the driver. This makes the driver much more friendlier to shared IRQs
  923. * than previous designs, while remaining 100% (?) SMP safe and capable.
  924. */
  925. static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
  926. {
  927. ide_drive_t *drive;
  928. ide_hwif_t *hwif;
  929. struct request *rq;
  930. ide_startstop_t startstop;
  931. int loops = 0;
  932. /* for atari only: POSSIBLY BROKEN HERE(?) */
  933. ide_get_lock(ide_intr, hwgroup);
  934. /* caller must own ide_lock */
  935. BUG_ON(!irqs_disabled());
  936. while (!hwgroup->busy) {
  937. hwgroup->busy = 1;
  938. drive = choose_drive(hwgroup);
  939. if (drive == NULL) {
  940. int sleeping = 0;
  941. unsigned long sleep = 0; /* shut up, gcc */
  942. hwgroup->rq = NULL;
  943. drive = hwgroup->drive;
  944. do {
  945. if (drive->sleeping && (!sleeping || time_before(drive->sleep, sleep))) {
  946. sleeping = 1;
  947. sleep = drive->sleep;
  948. }
  949. } while ((drive = drive->next) != hwgroup->drive);
  950. if (sleeping) {
  951. /*
  952. * Take a short snooze, and then wake up this hwgroup again.
  953. * This gives other hwgroups on the same a chance to
  954. * play fairly with us, just in case there are big differences
  955. * in relative throughputs.. don't want to hog the cpu too much.
  956. */
  957. if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
  958. sleep = jiffies + WAIT_MIN_SLEEP;
  959. #if 1
  960. if (timer_pending(&hwgroup->timer))
  961. printk(KERN_CRIT "ide_set_handler: timer already active\n");
  962. #endif
  963. /* so that ide_timer_expiry knows what to do */
  964. hwgroup->sleeping = 1;
  965. hwgroup->req_gen_timer = hwgroup->req_gen;
  966. mod_timer(&hwgroup->timer, sleep);
  967. /* we purposely leave hwgroup->busy==1
  968. * while sleeping */
  969. } else {
  970. /* Ugly, but how can we sleep for the lock
  971. * otherwise? perhaps from tq_disk?
  972. */
  973. /* for atari only */
  974. ide_release_lock();
  975. hwgroup->busy = 0;
  976. }
  977. /* no more work for this hwgroup (for now) */
  978. return;
  979. }
  980. again:
  981. hwif = HWIF(drive);
  982. if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif) {
  983. /*
  984. * set nIEN for previous hwif, drives in the
  985. * quirk_list may not like intr setups/cleanups
  986. */
  987. if (drive->quirk_list != 1)
  988. ide_set_irq(drive, 0);
  989. }
  990. hwgroup->hwif = hwif;
  991. hwgroup->drive = drive;
  992. drive->sleeping = 0;
  993. drive->service_start = jiffies;
  994. if (blk_queue_plugged(drive->queue)) {
  995. printk(KERN_ERR "ide: huh? queue was plugged!\n");
  996. break;
  997. }
  998. /*
  999. * we know that the queue isn't empty, but this can happen
  1000. * if the q->prep_rq_fn() decides to kill a request
  1001. */
  1002. rq = elv_next_request(drive->queue);
  1003. if (!rq) {
  1004. hwgroup->busy = 0;
  1005. break;
  1006. }
  1007. /*
  1008. * Sanity: don't accept a request that isn't a PM request
  1009. * if we are currently power managed. This is very important as
  1010. * blk_stop_queue() doesn't prevent the elv_next_request()
  1011. * above to return us whatever is in the queue. Since we call
  1012. * ide_do_request() ourselves, we end up taking requests while
  1013. * the queue is blocked...
  1014. *
  1015. * We let requests forced at head of queue with ide-preempt
  1016. * though. I hope that doesn't happen too much, hopefully not
  1017. * unless the subdriver triggers such a thing in its own PM
  1018. * state machine.
  1019. *
  1020. * We count how many times we loop here to make sure we service
  1021. * all drives in the hwgroup without looping for ever
  1022. */
  1023. if (drive->blocked && !blk_pm_request(rq) && !(rq->cmd_flags & REQ_PREEMPT)) {
  1024. drive = drive->next ? drive->next : hwgroup->drive;
  1025. if (loops++ < 4 && !blk_queue_plugged(drive->queue))
  1026. goto again;
  1027. /* We clear busy, there should be no pending ATA command at this point. */
  1028. hwgroup->busy = 0;
  1029. break;
  1030. }
  1031. hwgroup->rq = rq;
  1032. /*
  1033. * Some systems have trouble with IDE IRQs arriving while
  1034. * the driver is still setting things up. So, here we disable
  1035. * the IRQ used by this interface while the request is being started.
  1036. * This may look bad at first, but pretty much the same thing
  1037. * happens anyway when any interrupt comes in, IDE or otherwise
  1038. * -- the kernel masks the IRQ while it is being handled.
  1039. */
  1040. if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
  1041. disable_irq_nosync(hwif->irq);
  1042. spin_unlock(&ide_lock);
  1043. local_irq_enable_in_hardirq();
  1044. /* allow other IRQs while we start this request */
  1045. startstop = start_request(drive, rq);
  1046. spin_lock_irq(&ide_lock);
  1047. if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
  1048. enable_irq(hwif->irq);
  1049. if (startstop == ide_stopped)
  1050. hwgroup->busy = 0;
  1051. }
  1052. }
  1053. /*
  1054. * Passes the stuff to ide_do_request
  1055. */
  1056. void do_ide_request(struct request_queue *q)
  1057. {
  1058. ide_drive_t *drive = q->queuedata;
  1059. ide_do_request(HWGROUP(drive), IDE_NO_IRQ);
  1060. }
  1061. /*
  1062. * un-busy the hwgroup etc, and clear any pending DMA status. we want to
  1063. * retry the current request in pio mode instead of risking tossing it
  1064. * all away
  1065. */
  1066. static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
  1067. {
  1068. ide_hwif_t *hwif = HWIF(drive);
  1069. struct request *rq;
  1070. ide_startstop_t ret = ide_stopped;
  1071. /*
  1072. * end current dma transaction
  1073. */
  1074. if (error < 0) {
  1075. printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
  1076. (void)HWIF(drive)->ide_dma_end(drive);
  1077. ret = ide_error(drive, "dma timeout error",
  1078. ide_read_status(drive));
  1079. } else {
  1080. printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
  1081. hwif->dma_timeout(drive);
  1082. }
  1083. /*
  1084. * disable dma for now, but remember that we did so because of
  1085. * a timeout -- we'll reenable after we finish this next request
  1086. * (or rather the first chunk of it) in pio.
  1087. */
  1088. drive->retry_pio++;
  1089. drive->state = DMA_PIO_RETRY;
  1090. ide_dma_off_quietly(drive);
  1091. /*
  1092. * un-busy drive etc (hwgroup->busy is cleared on return) and
  1093. * make sure request is sane
  1094. */
  1095. rq = HWGROUP(drive)->rq;
  1096. if (!rq)
  1097. goto out;
  1098. HWGROUP(drive)->rq = NULL;
  1099. rq->errors = 0;
  1100. if (!rq->bio)
  1101. goto out;
  1102. rq->sector = rq->bio->bi_sector;
  1103. rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
  1104. rq->hard_cur_sectors = rq->current_nr_sectors;
  1105. rq->buffer = bio_data(rq->bio);
  1106. out:
  1107. return ret;
  1108. }
  1109. /**
  1110. * ide_timer_expiry - handle lack of an IDE interrupt
  1111. * @data: timer callback magic (hwgroup)
  1112. *
  1113. * An IDE command has timed out before the expected drive return
  1114. * occurred. At this point we attempt to clean up the current
  1115. * mess. If the current handler includes an expiry handler then
  1116. * we invoke the expiry handler, and providing it is happy the
  1117. * work is done. If that fails we apply generic recovery rules
  1118. * invoking the handler and checking the drive DMA status. We
  1119. * have an excessively incestuous relationship with the DMA
  1120. * logic that wants cleaning up.
  1121. */
  1122. void ide_timer_expiry (unsigned long data)
  1123. {
  1124. ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
  1125. ide_handler_t *handler;
  1126. ide_expiry_t *expiry;
  1127. unsigned long flags;
  1128. unsigned long wait = -1;
  1129. spin_lock_irqsave(&ide_lock, flags);
  1130. if (((handler = hwgroup->handler) == NULL) ||
  1131. (hwgroup->req_gen != hwgroup->req_gen_timer)) {
  1132. /*
  1133. * Either a marginal timeout occurred
  1134. * (got the interrupt just as timer expired),
  1135. * or we were "sleeping" to give other devices a chance.
  1136. * Either way, we don't really want to complain about anything.
  1137. */
  1138. if (hwgroup->sleeping) {
  1139. hwgroup->sleeping = 0;
  1140. hwgroup->busy = 0;
  1141. }
  1142. } else {
  1143. ide_drive_t *drive = hwgroup->drive;
  1144. if (!drive) {
  1145. printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
  1146. hwgroup->handler = NULL;
  1147. } else {
  1148. ide_hwif_t *hwif;
  1149. ide_startstop_t startstop = ide_stopped;
  1150. if (!hwgroup->busy) {
  1151. hwgroup->busy = 1; /* paranoia */
  1152. printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
  1153. }
  1154. if ((expiry = hwgroup->expiry) != NULL) {
  1155. /* continue */
  1156. if ((wait = expiry(drive)) > 0) {
  1157. /* reset timer */
  1158. hwgroup->timer.expires = jiffies + wait;
  1159. hwgroup->req_gen_timer = hwgroup->req_gen;
  1160. add_timer(&hwgroup->timer);
  1161. spin_unlock_irqrestore(&ide_lock, flags);
  1162. return;
  1163. }
  1164. }
  1165. hwgroup->handler = NULL;
  1166. /*
  1167. * We need to simulate a real interrupt when invoking
  1168. * the handler() function, which means we need to
  1169. * globally mask the specific IRQ:
  1170. */
  1171. spin_unlock(&ide_lock);
  1172. hwif = HWIF(drive);
  1173. /* disable_irq_nosync ?? */
  1174. disable_irq(hwif->irq);
  1175. /* local CPU only,
  1176. * as if we were handling an interrupt */
  1177. local_irq_disable();
  1178. if (hwgroup->polling) {
  1179. startstop = handler(drive);
  1180. } else if (drive_is_ready(drive)) {
  1181. if (drive->waiting_for_dma)
  1182. hwgroup->hwif->dma_lost_irq(drive);
  1183. (void)ide_ack_intr(hwif);
  1184. printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
  1185. startstop = handler(drive);
  1186. } else {
  1187. if (drive->waiting_for_dma) {
  1188. startstop = ide_dma_timeout_retry(drive, wait);
  1189. } else
  1190. startstop =
  1191. ide_error(drive, "irq timeout",
  1192. ide_read_status(drive));
  1193. }
  1194. drive->service_time = jiffies - drive->service_start;
  1195. spin_lock_irq(&ide_lock);
  1196. enable_irq(hwif->irq);
  1197. if (startstop == ide_stopped)
  1198. hwgroup->busy = 0;
  1199. }
  1200. }
  1201. ide_do_request(hwgroup, IDE_NO_IRQ);
  1202. spin_unlock_irqrestore(&ide_lock, flags);
  1203. }
  1204. /**
  1205. * unexpected_intr - handle an unexpected IDE interrupt
  1206. * @irq: interrupt line
  1207. * @hwgroup: hwgroup being processed
  1208. *
  1209. * There's nothing really useful we can do with an unexpected interrupt,
  1210. * other than reading the status register (to clear it), and logging it.
  1211. * There should be no way that an irq can happen before we're ready for it,
  1212. * so we needn't worry much about losing an "important" interrupt here.
  1213. *
  1214. * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
  1215. * the drive enters "idle", "standby", or "sleep" mode, so if the status
  1216. * looks "good", we just ignore the interrupt completely.
  1217. *
  1218. * This routine assumes __cli() is in effect when called.
  1219. *
  1220. * If an unexpected interrupt happens on irq15 while we are handling irq14
  1221. * and if the two interfaces are "serialized" (CMD640), then it looks like
  1222. * we could screw up by interfering with a new request being set up for
  1223. * irq15.
  1224. *
  1225. * In reality, this is a non-issue. The new command is not sent unless
  1226. * the drive is ready to accept one, in which case we know the drive is
  1227. * not trying to interrupt us. And ide_set_handler() is always invoked
  1228. * before completing the issuance of any new drive command, so we will not
  1229. * be accidentally invoked as a result of any valid command completion
  1230. * interrupt.
  1231. *
  1232. * Note that we must walk the entire hwgroup here. We know which hwif
  1233. * is doing the current command, but we don't know which hwif burped
  1234. * mysteriously.
  1235. */
  1236. static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
  1237. {
  1238. u8 stat;
  1239. ide_hwif_t *hwif = hwgroup->hwif;
  1240. /*
  1241. * handle the unexpected interrupt
  1242. */
  1243. do {
  1244. if (hwif->irq == irq) {
  1245. stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
  1246. if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
  1247. /* Try to not flood the console with msgs */
  1248. static unsigned long last_msgtime, count;
  1249. ++count;
  1250. if (time_after(jiffies, last_msgtime + HZ)) {
  1251. last_msgtime = jiffies;
  1252. printk(KERN_ERR "%s%s: unexpected interrupt, "
  1253. "status=0x%02x, count=%ld\n",
  1254. hwif->name,
  1255. (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
  1256. }
  1257. }
  1258. }
  1259. } while ((hwif = hwif->next) != hwgroup->hwif);
  1260. }
  1261. /**
  1262. * ide_intr - default IDE interrupt handler
  1263. * @irq: interrupt number
  1264. * @dev_id: hwif group
  1265. * @regs: unused weirdness from the kernel irq layer
  1266. *
  1267. * This is the default IRQ handler for the IDE layer. You should
  1268. * not need to override it. If you do be aware it is subtle in
  1269. * places
  1270. *
  1271. * hwgroup->hwif is the interface in the group currently performing
  1272. * a command. hwgroup->drive is the drive and hwgroup->handler is
  1273. * the IRQ handler to call. As we issue a command the handlers
  1274. * step through multiple states, reassigning the handler to the
  1275. * next step in the process. Unlike a smart SCSI controller IDE
  1276. * expects the main processor to sequence the various transfer
  1277. * stages. We also manage a poll timer to catch up with most
  1278. * timeout situations. There are still a few where the handlers
  1279. * don't ever decide to give up.
  1280. *
  1281. * The handler eventually returns ide_stopped to indicate the
  1282. * request completed. At this point we issue the next request
  1283. * on the hwgroup and the process begins again.
  1284. */
  1285. irqreturn_t ide_intr (int irq, void *dev_id)
  1286. {
  1287. unsigned long flags;
  1288. ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
  1289. ide_hwif_t *hwif;
  1290. ide_drive_t *drive;
  1291. ide_handler_t *handler;
  1292. ide_startstop_t startstop;
  1293. spin_lock_irqsave(&ide_lock, flags);
  1294. hwif = hwgroup->hwif;
  1295. if (!ide_ack_intr(hwif)) {
  1296. spin_unlock_irqrestore(&ide_lock, flags);
  1297. return IRQ_NONE;
  1298. }
  1299. if ((handler = hwgroup->handler) == NULL || hwgroup->polling) {
  1300. /*
  1301. * Not expecting an interrupt from this drive.
  1302. * That means this could be:
  1303. * (1) an interrupt from another PCI device
  1304. * sharing the same PCI INT# as us.
  1305. * or (2) a drive just entered sleep or standby mode,
  1306. * and is interrupting to let us know.
  1307. * or (3) a spurious interrupt of unknown origin.
  1308. *
  1309. * For PCI, we cannot tell the difference,
  1310. * so in that case we just ignore it and hope it goes away.
  1311. *
  1312. * FIXME: unexpected_intr should be hwif-> then we can
  1313. * remove all the ifdef PCI crap
  1314. */
  1315. #ifdef CONFIG_BLK_DEV_IDEPCI
  1316. if (hwif->chipset != ide_pci)
  1317. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1318. {
  1319. /*
  1320. * Probably not a shared PCI interrupt,
  1321. * so we can safely try to do something about it:
  1322. */
  1323. unexpected_intr(irq, hwgroup);
  1324. #ifdef CONFIG_BLK_DEV_IDEPCI
  1325. } else {
  1326. /*
  1327. * Whack the status register, just in case
  1328. * we have a leftover pending IRQ.
  1329. */
  1330. (void) hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
  1331. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1332. }
  1333. spin_unlock_irqrestore(&ide_lock, flags);
  1334. return IRQ_NONE;
  1335. }
  1336. drive = hwgroup->drive;
  1337. if (!drive) {
  1338. /*
  1339. * This should NEVER happen, and there isn't much
  1340. * we could do about it here.
  1341. *
  1342. * [Note - this can occur if the drive is hot unplugged]
  1343. */
  1344. spin_unlock_irqrestore(&ide_lock, flags);
  1345. return IRQ_HANDLED;
  1346. }
  1347. if (!drive_is_ready(drive)) {
  1348. /*
  1349. * This happens regularly when we share a PCI IRQ with
  1350. * another device. Unfortunately, it can also happen
  1351. * with some buggy drives that trigger the IRQ before
  1352. * their status register is up to date. Hopefully we have
  1353. * enough advance overhead that the latter isn't a problem.
  1354. */
  1355. spin_unlock_irqrestore(&ide_lock, flags);
  1356. return IRQ_NONE;
  1357. }
  1358. if (!hwgroup->busy) {
  1359. hwgroup->busy = 1; /* paranoia */
  1360. printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
  1361. }
  1362. hwgroup->handler = NULL;
  1363. hwgroup->req_gen++;
  1364. del_timer(&hwgroup->timer);
  1365. spin_unlock(&ide_lock);
  1366. /* Some controllers might set DMA INTR no matter DMA or PIO;
  1367. * bmdma status might need to be cleared even for
  1368. * PIO interrupts to prevent spurious/lost irq.
  1369. */
  1370. if (hwif->ide_dma_clear_irq && !(drive->waiting_for_dma))
  1371. /* ide_dma_end() needs bmdma status for error checking.
  1372. * So, skip clearing bmdma status here and leave it
  1373. * to ide_dma_end() if this is dma interrupt.
  1374. */
  1375. hwif->ide_dma_clear_irq(drive);
  1376. if (drive->unmask)
  1377. local_irq_enable_in_hardirq();
  1378. /* service this interrupt, may set handler for next interrupt */
  1379. startstop = handler(drive);
  1380. spin_lock_irq(&ide_lock);
  1381. /*
  1382. * Note that handler() may have set things up for another
  1383. * interrupt to occur soon, but it cannot happen until
  1384. * we exit from this routine, because it will be the
  1385. * same irq as is currently being serviced here, and Linux
  1386. * won't allow another of the same (on any CPU) until we return.
  1387. */
  1388. drive->service_time = jiffies - drive->service_start;
  1389. if (startstop == ide_stopped) {
  1390. if (hwgroup->handler == NULL) { /* paranoia */
  1391. hwgroup->busy = 0;
  1392. ide_do_request(hwgroup, hwif->irq);
  1393. } else {
  1394. printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
  1395. "on exit\n", drive->name);
  1396. }
  1397. }
  1398. spin_unlock_irqrestore(&ide_lock, flags);
  1399. return IRQ_HANDLED;
  1400. }
  1401. /**
  1402. * ide_init_drive_cmd - initialize a drive command request
  1403. * @rq: request object
  1404. *
  1405. * Initialize a request before we fill it in and send it down to
  1406. * ide_do_drive_cmd. Commands must be set up by this function. Right
  1407. * now it doesn't do a lot, but if that changes abusers will have a
  1408. * nasty surprise.
  1409. */
  1410. void ide_init_drive_cmd (struct request *rq)
  1411. {
  1412. memset(rq, 0, sizeof(*rq));
  1413. rq->ref_count = 1;
  1414. }
  1415. EXPORT_SYMBOL(ide_init_drive_cmd);
  1416. /**
  1417. * ide_do_drive_cmd - issue IDE special command
  1418. * @drive: device to issue command
  1419. * @rq: request to issue
  1420. * @action: action for processing
  1421. *
  1422. * This function issues a special IDE device request
  1423. * onto the request queue.
  1424. *
  1425. * If action is ide_wait, then the rq is queued at the end of the
  1426. * request queue, and the function sleeps until it has been processed.
  1427. * This is for use when invoked from an ioctl handler.
  1428. *
  1429. * If action is ide_preempt, then the rq is queued at the head of
  1430. * the request queue, displacing the currently-being-processed
  1431. * request and this function returns immediately without waiting
  1432. * for the new rq to be completed. This is VERY DANGEROUS, and is
  1433. * intended for careful use by the ATAPI tape/cdrom driver code.
  1434. *
  1435. * If action is ide_end, then the rq is queued at the end of the
  1436. * request queue, and the function returns immediately without waiting
  1437. * for the new rq to be completed. This is again intended for careful
  1438. * use by the ATAPI tape/cdrom driver code.
  1439. */
  1440. int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
  1441. {
  1442. unsigned long flags;
  1443. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  1444. DECLARE_COMPLETION_ONSTACK(wait);
  1445. int where = ELEVATOR_INSERT_BACK, err;
  1446. int must_wait = (action == ide_wait || action == ide_head_wait);
  1447. rq->errors = 0;
  1448. /*
  1449. * we need to hold an extra reference to request for safe inspection
  1450. * after completion
  1451. */
  1452. if (must_wait) {
  1453. rq->ref_count++;
  1454. rq->end_io_data = &wait;
  1455. rq->end_io = blk_end_sync_rq;
  1456. }
  1457. spin_lock_irqsave(&ide_lock, flags);
  1458. if (action == ide_preempt)
  1459. hwgroup->rq = NULL;
  1460. if (action == ide_preempt || action == ide_head_wait) {
  1461. where = ELEVATOR_INSERT_FRONT;
  1462. rq->cmd_flags |= REQ_PREEMPT;
  1463. }
  1464. __elv_add_request(drive->queue, rq, where, 0);
  1465. ide_do_request(hwgroup, IDE_NO_IRQ);
  1466. spin_unlock_irqrestore(&ide_lock, flags);
  1467. err = 0;
  1468. if (must_wait) {
  1469. wait_for_completion(&wait);
  1470. if (rq->errors)
  1471. err = -EIO;
  1472. blk_put_request(rq);
  1473. }
  1474. return err;
  1475. }
  1476. EXPORT_SYMBOL(ide_do_drive_cmd);
  1477. void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount, u8 dma)
  1478. {
  1479. ide_task_t task;
  1480. memset(&task, 0, sizeof(task));
  1481. task.tf_flags = IDE_TFLAG_OUT_LBAH | IDE_TFLAG_OUT_LBAM |
  1482. IDE_TFLAG_OUT_FEATURE | tf_flags;
  1483. task.tf.feature = dma; /* Use PIO/DMA */
  1484. task.tf.lbam = bcount & 0xff;
  1485. task.tf.lbah = (bcount >> 8) & 0xff;
  1486. ide_tf_load(drive, &task);
  1487. }
  1488. EXPORT_SYMBOL_GPL(ide_pktcmd_tf_load);