ide-io.c 46 KB

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