ide-io.c 42 KB

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