ide-io.c 44 KB

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