ide-io.c 47 KB

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