ide-io.c 43 KB

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