ide-io.c 44 KB

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