ide-io.c 42 KB

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