ide-io.c 49 KB

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