ide-io.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  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_KERNEL);
  560. if (!rq)
  561. return -ENOMEM;
  562. rq->cmd_type = REQ_TYPE_SPECIAL;
  563. rq->cmd_len = 5;
  564. rq->cmd[0] = REQ_DEVSET_EXEC;
  565. *(int *)&rq->cmd[1] = arg;
  566. rq->special = setting->set;
  567. if (blk_execute_rq(q, NULL, rq, 0))
  568. ret = rq->errors;
  569. blk_put_request(rq);
  570. return ret;
  571. }
  572. EXPORT_SYMBOL_GPL(ide_devset_execute);
  573. static ide_startstop_t ide_special_rq(ide_drive_t *drive, struct request *rq)
  574. {
  575. switch (rq->cmd[0]) {
  576. case REQ_DEVSET_EXEC:
  577. {
  578. int err, (*setfunc)(ide_drive_t *, int) = rq->special;
  579. err = setfunc(drive, *(int *)&rq->cmd[1]);
  580. if (err)
  581. rq->errors = err;
  582. else
  583. err = 1;
  584. ide_end_request(drive, err, 0);
  585. return ide_stopped;
  586. }
  587. case REQ_DRIVE_RESET:
  588. return ide_do_reset(drive);
  589. default:
  590. blk_dump_rq_flags(rq, "ide_special_rq - bad request");
  591. ide_end_request(drive, 0, 0);
  592. return ide_stopped;
  593. }
  594. }
  595. static void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
  596. {
  597. struct request_pm_state *pm = rq->data;
  598. if (blk_pm_suspend_request(rq) &&
  599. pm->pm_step == IDE_PM_START_SUSPEND)
  600. /* Mark drive blocked when starting the suspend sequence. */
  601. drive->dev_flags |= IDE_DFLAG_BLOCKED;
  602. else if (blk_pm_resume_request(rq) &&
  603. pm->pm_step == IDE_PM_START_RESUME) {
  604. /*
  605. * The first thing we do on wakeup is to wait for BSY bit to
  606. * go away (with a looong timeout) as a drive on this hwif may
  607. * just be POSTing itself.
  608. * We do that before even selecting as the "other" device on
  609. * the bus may be broken enough to walk on our toes at this
  610. * point.
  611. */
  612. ide_hwif_t *hwif = drive->hwif;
  613. int rc;
  614. #ifdef DEBUG_PM
  615. printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
  616. #endif
  617. rc = ide_wait_not_busy(hwif, 35000);
  618. if (rc)
  619. printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
  620. SELECT_DRIVE(drive);
  621. hwif->tp_ops->set_irq(hwif, 1);
  622. rc = ide_wait_not_busy(hwif, 100000);
  623. if (rc)
  624. printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
  625. }
  626. }
  627. /**
  628. * start_request - start of I/O and command issuing for IDE
  629. *
  630. * start_request() initiates handling of a new I/O request. It
  631. * accepts commands and I/O (read/write) requests.
  632. *
  633. * FIXME: this function needs a rename
  634. */
  635. static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
  636. {
  637. ide_startstop_t startstop;
  638. BUG_ON(!blk_rq_started(rq));
  639. #ifdef DEBUG
  640. printk("%s: start_request: current=0x%08lx\n",
  641. HWIF(drive)->name, (unsigned long) rq);
  642. #endif
  643. /* bail early if we've exceeded max_failures */
  644. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  645. rq->cmd_flags |= REQ_FAILED;
  646. goto kill_rq;
  647. }
  648. if (blk_pm_request(rq))
  649. ide_check_pm_state(drive, rq);
  650. SELECT_DRIVE(drive);
  651. if (ide_wait_stat(&startstop, drive, drive->ready_stat,
  652. ATA_BUSY | ATA_DRQ, WAIT_READY)) {
  653. printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
  654. return startstop;
  655. }
  656. if (!drive->special.all) {
  657. ide_driver_t *drv;
  658. /*
  659. * We reset the drive so we need to issue a SETFEATURES.
  660. * Do it _after_ do_special() restored device parameters.
  661. */
  662. if (drive->current_speed == 0xff)
  663. ide_config_drive_speed(drive, drive->desired_speed);
  664. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  665. return execute_drive_cmd(drive, rq);
  666. else if (blk_pm_request(rq)) {
  667. struct request_pm_state *pm = rq->data;
  668. #ifdef DEBUG_PM
  669. printk("%s: start_power_step(step: %d)\n",
  670. drive->name, rq->pm->pm_step);
  671. #endif
  672. startstop = ide_start_power_step(drive, rq);
  673. if (startstop == ide_stopped &&
  674. pm->pm_step == IDE_PM_COMPLETED)
  675. ide_complete_pm_request(drive, rq);
  676. return startstop;
  677. } else if (!rq->rq_disk && blk_special_request(rq))
  678. /*
  679. * TODO: Once all ULDs have been modified to
  680. * check for specific op codes rather than
  681. * blindly accepting any special request, the
  682. * check for ->rq_disk above may be replaced
  683. * by a more suitable mechanism or even
  684. * dropped entirely.
  685. */
  686. return ide_special_rq(drive, rq);
  687. drv = *(ide_driver_t **)rq->rq_disk->private_data;
  688. return drv->do_request(drive, rq, rq->sector);
  689. }
  690. return do_special(drive);
  691. kill_rq:
  692. ide_kill_rq(drive, rq);
  693. return ide_stopped;
  694. }
  695. /**
  696. * ide_stall_queue - pause an IDE device
  697. * @drive: drive to stall
  698. * @timeout: time to stall for (jiffies)
  699. *
  700. * ide_stall_queue() can be used by a drive to give excess bandwidth back
  701. * to the hwgroup by sleeping for timeout jiffies.
  702. */
  703. void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
  704. {
  705. if (timeout > WAIT_WORSTCASE)
  706. timeout = WAIT_WORSTCASE;
  707. drive->sleep = timeout + jiffies;
  708. drive->dev_flags |= IDE_DFLAG_SLEEPING;
  709. }
  710. EXPORT_SYMBOL(ide_stall_queue);
  711. #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
  712. /**
  713. * choose_drive - select a drive to service
  714. * @hwgroup: hardware group to select on
  715. *
  716. * choose_drive() selects the next drive which will be serviced.
  717. * This is necessary because the IDE layer can't issue commands
  718. * to both drives on the same cable, unlike SCSI.
  719. */
  720. static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
  721. {
  722. ide_drive_t *drive, *best;
  723. repeat:
  724. best = NULL;
  725. drive = hwgroup->drive;
  726. /*
  727. * drive is doing pre-flush, ordered write, post-flush sequence. even
  728. * though that is 3 requests, it must be seen as a single transaction.
  729. * we must not preempt this drive until that is complete
  730. */
  731. if (blk_queue_flushing(drive->queue)) {
  732. /*
  733. * small race where queue could get replugged during
  734. * the 3-request flush cycle, just yank the plug since
  735. * we want it to finish asap
  736. */
  737. blk_remove_plug(drive->queue);
  738. return drive;
  739. }
  740. do {
  741. u8 dev_s = !!(drive->dev_flags & IDE_DFLAG_SLEEPING);
  742. u8 best_s = (best && !!(best->dev_flags & IDE_DFLAG_SLEEPING));
  743. if ((dev_s == 0 || time_after_eq(jiffies, drive->sleep)) &&
  744. !elv_queue_empty(drive->queue)) {
  745. if (best == NULL ||
  746. (dev_s && (best_s == 0 || time_before(drive->sleep, best->sleep))) ||
  747. (best_s == 0 && time_before(WAKEUP(drive), WAKEUP(best)))) {
  748. if (!blk_queue_plugged(drive->queue))
  749. best = drive;
  750. }
  751. }
  752. } while ((drive = drive->next) != hwgroup->drive);
  753. if (best && (best->dev_flags & IDE_DFLAG_NICE1) &&
  754. (best->dev_flags & IDE_DFLAG_SLEEPING) == 0 &&
  755. best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
  756. long t = (signed long)(WAKEUP(best) - jiffies);
  757. if (t >= WAIT_MIN_SLEEP) {
  758. /*
  759. * We *may* have some time to spare, but first let's see if
  760. * someone can potentially benefit from our nice mood today..
  761. */
  762. drive = best->next;
  763. do {
  764. if ((drive->dev_flags & IDE_DFLAG_SLEEPING) == 0
  765. && time_before(jiffies - best->service_time, WAKEUP(drive))
  766. && time_before(WAKEUP(drive), jiffies + t))
  767. {
  768. ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
  769. goto repeat;
  770. }
  771. } while ((drive = drive->next) != best);
  772. }
  773. }
  774. return best;
  775. }
  776. /*
  777. * Issue a new request to a drive from hwgroup
  778. * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
  779. *
  780. * A hwgroup is a serialized group of IDE interfaces. Usually there is
  781. * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
  782. * may have both interfaces in a single hwgroup to "serialize" access.
  783. * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
  784. * together into one hwgroup for serialized access.
  785. *
  786. * Note also that several hwgroups can end up sharing a single IRQ,
  787. * possibly along with many other devices. This is especially common in
  788. * PCI-based systems with off-board IDE controller cards.
  789. *
  790. * The IDE driver uses the single global ide_lock spinlock to protect
  791. * access to the request queues, and to protect the hwgroup->busy flag.
  792. *
  793. * The first thread into the driver for a particular hwgroup sets the
  794. * hwgroup->busy flag to indicate that this hwgroup is now active,
  795. * and then initiates processing of the top request from the request queue.
  796. *
  797. * Other threads attempting entry notice the busy setting, and will simply
  798. * queue their new requests and exit immediately. Note that hwgroup->busy
  799. * remains set even when the driver is merely awaiting the next interrupt.
  800. * Thus, the meaning is "this hwgroup is busy processing a request".
  801. *
  802. * When processing of a request completes, the completing thread or IRQ-handler
  803. * will start the next request from the queue. If no more work remains,
  804. * the driver will clear the hwgroup->busy flag and exit.
  805. *
  806. * The ide_lock (spinlock) is used to protect all access to the
  807. * hwgroup->busy flag, but is otherwise not needed for most processing in
  808. * the driver. This makes the driver much more friendlier to shared IRQs
  809. * than previous designs, while remaining 100% (?) SMP safe and capable.
  810. */
  811. static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
  812. {
  813. ide_drive_t *drive;
  814. ide_hwif_t *hwif;
  815. struct request *rq;
  816. ide_startstop_t startstop;
  817. int loops = 0;
  818. /* for atari only: POSSIBLY BROKEN HERE(?) */
  819. ide_get_lock(ide_intr, hwgroup);
  820. /* caller must own ide_lock */
  821. BUG_ON(!irqs_disabled());
  822. while (!hwgroup->busy) {
  823. hwgroup->busy = 1;
  824. drive = choose_drive(hwgroup);
  825. if (drive == NULL) {
  826. int sleeping = 0;
  827. unsigned long sleep = 0; /* shut up, gcc */
  828. hwgroup->rq = NULL;
  829. drive = hwgroup->drive;
  830. do {
  831. if ((drive->dev_flags & IDE_DFLAG_SLEEPING) &&
  832. (sleeping == 0 ||
  833. time_before(drive->sleep, sleep))) {
  834. sleeping = 1;
  835. sleep = drive->sleep;
  836. }
  837. } while ((drive = drive->next) != hwgroup->drive);
  838. if (sleeping) {
  839. /*
  840. * Take a short snooze, and then wake up this hwgroup again.
  841. * This gives other hwgroups on the same a chance to
  842. * play fairly with us, just in case there are big differences
  843. * in relative throughputs.. don't want to hog the cpu too much.
  844. */
  845. if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
  846. sleep = jiffies + WAIT_MIN_SLEEP;
  847. #if 1
  848. if (timer_pending(&hwgroup->timer))
  849. printk(KERN_CRIT "ide_set_handler: timer already active\n");
  850. #endif
  851. /* so that ide_timer_expiry knows what to do */
  852. hwgroup->sleeping = 1;
  853. hwgroup->req_gen_timer = hwgroup->req_gen;
  854. mod_timer(&hwgroup->timer, sleep);
  855. /* we purposely leave hwgroup->busy==1
  856. * while sleeping */
  857. } else {
  858. /* Ugly, but how can we sleep for the lock
  859. * otherwise? perhaps from tq_disk?
  860. */
  861. /* for atari only */
  862. ide_release_lock();
  863. hwgroup->busy = 0;
  864. }
  865. /* no more work for this hwgroup (for now) */
  866. return;
  867. }
  868. again:
  869. hwif = HWIF(drive);
  870. if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif) {
  871. /*
  872. * set nIEN for previous hwif, drives in the
  873. * quirk_list may not like intr setups/cleanups
  874. */
  875. if (drive->quirk_list != 1)
  876. hwif->tp_ops->set_irq(hwif, 0);
  877. }
  878. hwgroup->hwif = hwif;
  879. hwgroup->drive = drive;
  880. drive->dev_flags &= ~IDE_DFLAG_SLEEPING;
  881. drive->service_start = jiffies;
  882. if (blk_queue_plugged(drive->queue)) {
  883. printk(KERN_ERR "ide: huh? queue was plugged!\n");
  884. break;
  885. }
  886. /*
  887. * we know that the queue isn't empty, but this can happen
  888. * if the q->prep_rq_fn() decides to kill a request
  889. */
  890. rq = elv_next_request(drive->queue);
  891. if (!rq) {
  892. hwgroup->busy = 0;
  893. break;
  894. }
  895. /*
  896. * Sanity: don't accept a request that isn't a PM request
  897. * if we are currently power managed. This is very important as
  898. * blk_stop_queue() doesn't prevent the elv_next_request()
  899. * above to return us whatever is in the queue. Since we call
  900. * ide_do_request() ourselves, we end up taking requests while
  901. * the queue is blocked...
  902. *
  903. * We let requests forced at head of queue with ide-preempt
  904. * though. I hope that doesn't happen too much, hopefully not
  905. * unless the subdriver triggers such a thing in its own PM
  906. * state machine.
  907. *
  908. * We count how many times we loop here to make sure we service
  909. * all drives in the hwgroup without looping for ever
  910. */
  911. if ((drive->dev_flags & IDE_DFLAG_BLOCKED) &&
  912. blk_pm_request(rq) == 0 &&
  913. (rq->cmd_flags & REQ_PREEMPT) == 0) {
  914. drive = drive->next ? drive->next : hwgroup->drive;
  915. if (loops++ < 4 && !blk_queue_plugged(drive->queue))
  916. goto again;
  917. /* We clear busy, there should be no pending ATA command at this point. */
  918. hwgroup->busy = 0;
  919. break;
  920. }
  921. hwgroup->rq = rq;
  922. /*
  923. * Some systems have trouble with IDE IRQs arriving while
  924. * the driver is still setting things up. So, here we disable
  925. * the IRQ used by this interface while the request is being started.
  926. * This may look bad at first, but pretty much the same thing
  927. * happens anyway when any interrupt comes in, IDE or otherwise
  928. * -- the kernel masks the IRQ while it is being handled.
  929. */
  930. if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
  931. disable_irq_nosync(hwif->irq);
  932. spin_unlock(&ide_lock);
  933. local_irq_enable_in_hardirq();
  934. /* allow other IRQs while we start this request */
  935. startstop = start_request(drive, rq);
  936. spin_lock_irq(&ide_lock);
  937. if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
  938. enable_irq(hwif->irq);
  939. if (startstop == ide_stopped)
  940. hwgroup->busy = 0;
  941. }
  942. }
  943. /*
  944. * Passes the stuff to ide_do_request
  945. */
  946. void do_ide_request(struct request_queue *q)
  947. {
  948. ide_drive_t *drive = q->queuedata;
  949. ide_do_request(HWGROUP(drive), IDE_NO_IRQ);
  950. }
  951. /*
  952. * un-busy the hwgroup etc, and clear any pending DMA status. we want to
  953. * retry the current request in pio mode instead of risking tossing it
  954. * all away
  955. */
  956. static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
  957. {
  958. ide_hwif_t *hwif = HWIF(drive);
  959. struct request *rq;
  960. ide_startstop_t ret = ide_stopped;
  961. /*
  962. * end current dma transaction
  963. */
  964. if (error < 0) {
  965. printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
  966. (void)hwif->dma_ops->dma_end(drive);
  967. ret = ide_error(drive, "dma timeout error",
  968. hwif->tp_ops->read_status(hwif));
  969. } else {
  970. printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
  971. hwif->dma_ops->dma_timeout(drive);
  972. }
  973. /*
  974. * disable dma for now, but remember that we did so because of
  975. * a timeout -- we'll reenable after we finish this next request
  976. * (or rather the first chunk of it) in pio.
  977. */
  978. drive->dev_flags |= IDE_DFLAG_DMA_PIO_RETRY;
  979. drive->retry_pio++;
  980. ide_dma_off_quietly(drive);
  981. /*
  982. * un-busy drive etc (hwgroup->busy is cleared on return) and
  983. * make sure request is sane
  984. */
  985. rq = HWGROUP(drive)->rq;
  986. if (!rq)
  987. goto out;
  988. HWGROUP(drive)->rq = NULL;
  989. rq->errors = 0;
  990. if (!rq->bio)
  991. goto out;
  992. rq->sector = rq->bio->bi_sector;
  993. rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
  994. rq->hard_cur_sectors = rq->current_nr_sectors;
  995. rq->buffer = bio_data(rq->bio);
  996. out:
  997. return ret;
  998. }
  999. /**
  1000. * ide_timer_expiry - handle lack of an IDE interrupt
  1001. * @data: timer callback magic (hwgroup)
  1002. *
  1003. * An IDE command has timed out before the expected drive return
  1004. * occurred. At this point we attempt to clean up the current
  1005. * mess. If the current handler includes an expiry handler then
  1006. * we invoke the expiry handler, and providing it is happy the
  1007. * work is done. If that fails we apply generic recovery rules
  1008. * invoking the handler and checking the drive DMA status. We
  1009. * have an excessively incestuous relationship with the DMA
  1010. * logic that wants cleaning up.
  1011. */
  1012. void ide_timer_expiry (unsigned long data)
  1013. {
  1014. ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
  1015. ide_handler_t *handler;
  1016. ide_expiry_t *expiry;
  1017. unsigned long flags;
  1018. unsigned long wait = -1;
  1019. spin_lock_irqsave(&ide_lock, flags);
  1020. if (((handler = hwgroup->handler) == NULL) ||
  1021. (hwgroup->req_gen != hwgroup->req_gen_timer)) {
  1022. /*
  1023. * Either a marginal timeout occurred
  1024. * (got the interrupt just as timer expired),
  1025. * or we were "sleeping" to give other devices a chance.
  1026. * Either way, we don't really want to complain about anything.
  1027. */
  1028. if (hwgroup->sleeping) {
  1029. hwgroup->sleeping = 0;
  1030. hwgroup->busy = 0;
  1031. }
  1032. } else {
  1033. ide_drive_t *drive = hwgroup->drive;
  1034. if (!drive) {
  1035. printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
  1036. hwgroup->handler = NULL;
  1037. } else {
  1038. ide_hwif_t *hwif;
  1039. ide_startstop_t startstop = ide_stopped;
  1040. if (!hwgroup->busy) {
  1041. hwgroup->busy = 1; /* paranoia */
  1042. printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
  1043. }
  1044. if ((expiry = hwgroup->expiry) != NULL) {
  1045. /* continue */
  1046. if ((wait = expiry(drive)) > 0) {
  1047. /* reset timer */
  1048. hwgroup->timer.expires = jiffies + wait;
  1049. hwgroup->req_gen_timer = hwgroup->req_gen;
  1050. add_timer(&hwgroup->timer);
  1051. spin_unlock_irqrestore(&ide_lock, flags);
  1052. return;
  1053. }
  1054. }
  1055. hwgroup->handler = NULL;
  1056. /*
  1057. * We need to simulate a real interrupt when invoking
  1058. * the handler() function, which means we need to
  1059. * globally mask the specific IRQ:
  1060. */
  1061. spin_unlock(&ide_lock);
  1062. hwif = HWIF(drive);
  1063. /* disable_irq_nosync ?? */
  1064. disable_irq(hwif->irq);
  1065. /* local CPU only,
  1066. * as if we were handling an interrupt */
  1067. local_irq_disable();
  1068. if (hwgroup->polling) {
  1069. startstop = handler(drive);
  1070. } else if (drive_is_ready(drive)) {
  1071. if (drive->waiting_for_dma)
  1072. hwif->dma_ops->dma_lost_irq(drive);
  1073. (void)ide_ack_intr(hwif);
  1074. printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
  1075. startstop = handler(drive);
  1076. } else {
  1077. if (drive->waiting_for_dma) {
  1078. startstop = ide_dma_timeout_retry(drive, wait);
  1079. } else
  1080. startstop =
  1081. ide_error(drive, "irq timeout",
  1082. hwif->tp_ops->read_status(hwif));
  1083. }
  1084. drive->service_time = jiffies - drive->service_start;
  1085. spin_lock_irq(&ide_lock);
  1086. enable_irq(hwif->irq);
  1087. if (startstop == ide_stopped)
  1088. hwgroup->busy = 0;
  1089. }
  1090. }
  1091. ide_do_request(hwgroup, IDE_NO_IRQ);
  1092. spin_unlock_irqrestore(&ide_lock, flags);
  1093. }
  1094. /**
  1095. * unexpected_intr - handle an unexpected IDE interrupt
  1096. * @irq: interrupt line
  1097. * @hwgroup: hwgroup being processed
  1098. *
  1099. * There's nothing really useful we can do with an unexpected interrupt,
  1100. * other than reading the status register (to clear it), and logging it.
  1101. * There should be no way that an irq can happen before we're ready for it,
  1102. * so we needn't worry much about losing an "important" interrupt here.
  1103. *
  1104. * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
  1105. * the drive enters "idle", "standby", or "sleep" mode, so if the status
  1106. * looks "good", we just ignore the interrupt completely.
  1107. *
  1108. * This routine assumes __cli() is in effect when called.
  1109. *
  1110. * If an unexpected interrupt happens on irq15 while we are handling irq14
  1111. * and if the two interfaces are "serialized" (CMD640), then it looks like
  1112. * we could screw up by interfering with a new request being set up for
  1113. * irq15.
  1114. *
  1115. * In reality, this is a non-issue. The new command is not sent unless
  1116. * the drive is ready to accept one, in which case we know the drive is
  1117. * not trying to interrupt us. And ide_set_handler() is always invoked
  1118. * before completing the issuance of any new drive command, so we will not
  1119. * be accidentally invoked as a result of any valid command completion
  1120. * interrupt.
  1121. *
  1122. * Note that we must walk the entire hwgroup here. We know which hwif
  1123. * is doing the current command, but we don't know which hwif burped
  1124. * mysteriously.
  1125. */
  1126. static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
  1127. {
  1128. u8 stat;
  1129. ide_hwif_t *hwif = hwgroup->hwif;
  1130. /*
  1131. * handle the unexpected interrupt
  1132. */
  1133. do {
  1134. if (hwif->irq == irq) {
  1135. stat = hwif->tp_ops->read_status(hwif);
  1136. if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
  1137. /* Try to not flood the console with msgs */
  1138. static unsigned long last_msgtime, count;
  1139. ++count;
  1140. if (time_after(jiffies, last_msgtime + HZ)) {
  1141. last_msgtime = jiffies;
  1142. printk(KERN_ERR "%s%s: unexpected interrupt, "
  1143. "status=0x%02x, count=%ld\n",
  1144. hwif->name,
  1145. (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
  1146. }
  1147. }
  1148. }
  1149. } while ((hwif = hwif->next) != hwgroup->hwif);
  1150. }
  1151. /**
  1152. * ide_intr - default IDE interrupt handler
  1153. * @irq: interrupt number
  1154. * @dev_id: hwif group
  1155. * @regs: unused weirdness from the kernel irq layer
  1156. *
  1157. * This is the default IRQ handler for the IDE layer. You should
  1158. * not need to override it. If you do be aware it is subtle in
  1159. * places
  1160. *
  1161. * hwgroup->hwif is the interface in the group currently performing
  1162. * a command. hwgroup->drive is the drive and hwgroup->handler is
  1163. * the IRQ handler to call. As we issue a command the handlers
  1164. * step through multiple states, reassigning the handler to the
  1165. * next step in the process. Unlike a smart SCSI controller IDE
  1166. * expects the main processor to sequence the various transfer
  1167. * stages. We also manage a poll timer to catch up with most
  1168. * timeout situations. There are still a few where the handlers
  1169. * don't ever decide to give up.
  1170. *
  1171. * The handler eventually returns ide_stopped to indicate the
  1172. * request completed. At this point we issue the next request
  1173. * on the hwgroup and the process begins again.
  1174. */
  1175. irqreturn_t ide_intr (int irq, void *dev_id)
  1176. {
  1177. unsigned long flags;
  1178. ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
  1179. ide_hwif_t *hwif;
  1180. ide_drive_t *drive;
  1181. ide_handler_t *handler;
  1182. ide_startstop_t startstop;
  1183. spin_lock_irqsave(&ide_lock, flags);
  1184. hwif = hwgroup->hwif;
  1185. if (!ide_ack_intr(hwif)) {
  1186. spin_unlock_irqrestore(&ide_lock, flags);
  1187. return IRQ_NONE;
  1188. }
  1189. if ((handler = hwgroup->handler) == NULL || hwgroup->polling) {
  1190. /*
  1191. * Not expecting an interrupt from this drive.
  1192. * That means this could be:
  1193. * (1) an interrupt from another PCI device
  1194. * sharing the same PCI INT# as us.
  1195. * or (2) a drive just entered sleep or standby mode,
  1196. * and is interrupting to let us know.
  1197. * or (3) a spurious interrupt of unknown origin.
  1198. *
  1199. * For PCI, we cannot tell the difference,
  1200. * so in that case we just ignore it and hope it goes away.
  1201. *
  1202. * FIXME: unexpected_intr should be hwif-> then we can
  1203. * remove all the ifdef PCI crap
  1204. */
  1205. #ifdef CONFIG_BLK_DEV_IDEPCI
  1206. if (hwif->chipset != ide_pci)
  1207. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1208. {
  1209. /*
  1210. * Probably not a shared PCI interrupt,
  1211. * so we can safely try to do something about it:
  1212. */
  1213. unexpected_intr(irq, hwgroup);
  1214. #ifdef CONFIG_BLK_DEV_IDEPCI
  1215. } else {
  1216. /*
  1217. * Whack the status register, just in case
  1218. * we have a leftover pending IRQ.
  1219. */
  1220. (void)hwif->tp_ops->read_status(hwif);
  1221. #endif /* CONFIG_BLK_DEV_IDEPCI */
  1222. }
  1223. spin_unlock_irqrestore(&ide_lock, flags);
  1224. return IRQ_NONE;
  1225. }
  1226. drive = hwgroup->drive;
  1227. if (!drive) {
  1228. /*
  1229. * This should NEVER happen, and there isn't much
  1230. * we could do about it here.
  1231. *
  1232. * [Note - this can occur if the drive is hot unplugged]
  1233. */
  1234. spin_unlock_irqrestore(&ide_lock, flags);
  1235. return IRQ_HANDLED;
  1236. }
  1237. if (!drive_is_ready(drive)) {
  1238. /*
  1239. * This happens regularly when we share a PCI IRQ with
  1240. * another device. Unfortunately, it can also happen
  1241. * with some buggy drives that trigger the IRQ before
  1242. * their status register is up to date. Hopefully we have
  1243. * enough advance overhead that the latter isn't a problem.
  1244. */
  1245. spin_unlock_irqrestore(&ide_lock, flags);
  1246. return IRQ_NONE;
  1247. }
  1248. if (!hwgroup->busy) {
  1249. hwgroup->busy = 1; /* paranoia */
  1250. printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
  1251. }
  1252. hwgroup->handler = NULL;
  1253. hwgroup->req_gen++;
  1254. del_timer(&hwgroup->timer);
  1255. spin_unlock(&ide_lock);
  1256. if (hwif->port_ops && hwif->port_ops->clear_irq)
  1257. hwif->port_ops->clear_irq(drive);
  1258. if (drive->dev_flags & IDE_DFLAG_UNMASK)
  1259. local_irq_enable_in_hardirq();
  1260. /* service this interrupt, may set handler for next interrupt */
  1261. startstop = handler(drive);
  1262. spin_lock_irq(&ide_lock);
  1263. /*
  1264. * Note that handler() may have set things up for another
  1265. * interrupt to occur soon, but it cannot happen until
  1266. * we exit from this routine, because it will be the
  1267. * same irq as is currently being serviced here, and Linux
  1268. * won't allow another of the same (on any CPU) until we return.
  1269. */
  1270. drive->service_time = jiffies - drive->service_start;
  1271. if (startstop == ide_stopped) {
  1272. if (hwgroup->handler == NULL) { /* paranoia */
  1273. hwgroup->busy = 0;
  1274. ide_do_request(hwgroup, hwif->irq);
  1275. } else {
  1276. printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
  1277. "on exit\n", drive->name);
  1278. }
  1279. }
  1280. spin_unlock_irqrestore(&ide_lock, flags);
  1281. return IRQ_HANDLED;
  1282. }
  1283. /**
  1284. * ide_do_drive_cmd - issue IDE special command
  1285. * @drive: device to issue command
  1286. * @rq: request to issue
  1287. *
  1288. * This function issues a special IDE device request
  1289. * onto the request queue.
  1290. *
  1291. * the rq is queued at the head of the request queue, displacing
  1292. * the currently-being-processed request and this function
  1293. * returns immediately without waiting for the new rq to be
  1294. * completed. This is VERY DANGEROUS, and is intended for
  1295. * careful use by the ATAPI tape/cdrom driver code.
  1296. */
  1297. void ide_do_drive_cmd(ide_drive_t *drive, struct request *rq)
  1298. {
  1299. unsigned long flags;
  1300. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  1301. spin_lock_irqsave(&ide_lock, flags);
  1302. hwgroup->rq = NULL;
  1303. __elv_add_request(drive->queue, rq, ELEVATOR_INSERT_FRONT, 1);
  1304. __generic_unplug_device(drive->queue);
  1305. spin_unlock_irqrestore(&ide_lock, flags);
  1306. }
  1307. EXPORT_SYMBOL(ide_do_drive_cmd);
  1308. void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount, u8 dma)
  1309. {
  1310. ide_hwif_t *hwif = drive->hwif;
  1311. ide_task_t task;
  1312. memset(&task, 0, sizeof(task));
  1313. task.tf_flags = IDE_TFLAG_OUT_LBAH | IDE_TFLAG_OUT_LBAM |
  1314. IDE_TFLAG_OUT_FEATURE | tf_flags;
  1315. task.tf.feature = dma; /* Use PIO/DMA */
  1316. task.tf.lbam = bcount & 0xff;
  1317. task.tf.lbah = (bcount >> 8) & 0xff;
  1318. ide_tf_dump(drive->name, &task.tf);
  1319. hwif->tp_ops->set_irq(hwif, 1);
  1320. SELECT_MASK(drive, 0);
  1321. hwif->tp_ops->tf_load(drive, &task);
  1322. }
  1323. EXPORT_SYMBOL_GPL(ide_pktcmd_tf_load);
  1324. void ide_pad_transfer(ide_drive_t *drive, int write, int len)
  1325. {
  1326. ide_hwif_t *hwif = drive->hwif;
  1327. u8 buf[4] = { 0 };
  1328. while (len > 0) {
  1329. if (write)
  1330. hwif->tp_ops->output_data(drive, NULL, buf, min(4, len));
  1331. else
  1332. hwif->tp_ops->input_data(drive, NULL, buf, min(4, len));
  1333. len -= 4;
  1334. }
  1335. }
  1336. EXPORT_SYMBOL_GPL(ide_pad_transfer);