ide-io.c 43 KB

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