ide-io.c 49 KB

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