ide-io.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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->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->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. /**
  108. * ide_end_dequeued_request - complete an IDE I/O
  109. * @drive: IDE device for the I/O
  110. * @uptodate:
  111. * @nr_sectors: number of sectors completed
  112. *
  113. * Complete an I/O that is no longer on the request queue. This
  114. * typically occurs when we pull the request and issue a REQUEST_SENSE.
  115. * We must still finish the old request but we must not tamper with the
  116. * queue in the meantime.
  117. *
  118. * NOTE: This path does not handle barrier, but barrier is not supported
  119. * on ide-cd anyway.
  120. */
  121. int ide_end_dequeued_request(ide_drive_t *drive, struct request *rq,
  122. int uptodate, int nr_sectors)
  123. {
  124. BUG_ON(!blk_rq_started(rq));
  125. return __ide_end_request(drive, rq, uptodate, nr_sectors << 9, 0);
  126. }
  127. EXPORT_SYMBOL_GPL(ide_end_dequeued_request);
  128. /**
  129. * ide_end_drive_cmd - end an explicit drive command
  130. * @drive: command
  131. * @stat: status bits
  132. * @err: error bits
  133. *
  134. * Clean up after success/failure of an explicit drive command.
  135. * These get thrown onto the queue so they are synchronized with
  136. * real I/O operations on the drive.
  137. *
  138. * In LBA48 mode we have to read the register set twice to get
  139. * all the extra information out.
  140. */
  141. void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
  142. {
  143. ide_hwif_t *hwif = drive->hwif;
  144. struct request *rq = hwif->rq;
  145. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
  146. ide_task_t *task = (ide_task_t *)rq->special;
  147. if (task) {
  148. struct ide_taskfile *tf = &task->tf;
  149. tf->error = err;
  150. tf->status = stat;
  151. drive->hwif->tp_ops->tf_read(drive, task);
  152. if (task->tf_flags & IDE_TFLAG_DYN)
  153. kfree(task);
  154. }
  155. } else if (blk_pm_request(rq)) {
  156. struct request_pm_state *pm = rq->data;
  157. ide_complete_power_step(drive, rq);
  158. if (pm->pm_step == IDE_PM_COMPLETED)
  159. ide_complete_pm_request(drive, rq);
  160. return;
  161. }
  162. hwif->rq = NULL;
  163. rq->errors = err;
  164. if (unlikely(blk_end_request(rq, (rq->errors ? -EIO : 0),
  165. blk_rq_bytes(rq))))
  166. BUG();
  167. }
  168. EXPORT_SYMBOL(ide_end_drive_cmd);
  169. static void ide_kill_rq(ide_drive_t *drive, struct request *rq)
  170. {
  171. if (rq->rq_disk) {
  172. struct ide_driver *drv;
  173. drv = *(struct ide_driver **)rq->rq_disk->private_data;
  174. drv->end_request(drive, 0, 0);
  175. } else
  176. ide_end_request(drive, 0, 0);
  177. }
  178. static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  179. {
  180. ide_hwif_t *hwif = drive->hwif;
  181. if ((stat & ATA_BUSY) ||
  182. ((stat & ATA_DF) && (drive->dev_flags & IDE_DFLAG_NOWERR) == 0)) {
  183. /* other bits are useless when BUSY */
  184. rq->errors |= ERROR_RESET;
  185. } else if (stat & ATA_ERR) {
  186. /* err has different meaning on cdrom and tape */
  187. if (err == ATA_ABORTED) {
  188. if ((drive->dev_flags & IDE_DFLAG_LBA) &&
  189. /* some newer drives don't support ATA_CMD_INIT_DEV_PARAMS */
  190. hwif->tp_ops->read_status(hwif) == ATA_CMD_INIT_DEV_PARAMS)
  191. return ide_stopped;
  192. } else if ((err & BAD_CRC) == BAD_CRC) {
  193. /* UDMA crc error, just retry the operation */
  194. drive->crc_count++;
  195. } else if (err & (ATA_BBK | ATA_UNC)) {
  196. /* retries won't help these */
  197. rq->errors = ERROR_MAX;
  198. } else if (err & ATA_TRK0NF) {
  199. /* help it find track zero */
  200. rq->errors |= ERROR_RECAL;
  201. }
  202. }
  203. if ((stat & ATA_DRQ) && rq_data_dir(rq) == READ &&
  204. (hwif->host_flags & IDE_HFLAG_ERROR_STOPS_FIFO) == 0) {
  205. int nsect = drive->mult_count ? drive->mult_count : 1;
  206. ide_pad_transfer(drive, READ, nsect * SECTOR_SIZE);
  207. }
  208. if (rq->errors >= ERROR_MAX || blk_noretry_request(rq)) {
  209. ide_kill_rq(drive, rq);
  210. return ide_stopped;
  211. }
  212. if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
  213. rq->errors |= ERROR_RESET;
  214. if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
  215. ++rq->errors;
  216. return ide_do_reset(drive);
  217. }
  218. if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
  219. drive->special.b.recalibrate = 1;
  220. ++rq->errors;
  221. return ide_stopped;
  222. }
  223. static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  224. {
  225. ide_hwif_t *hwif = drive->hwif;
  226. if ((stat & ATA_BUSY) ||
  227. ((stat & ATA_DF) && (drive->dev_flags & IDE_DFLAG_NOWERR) == 0)) {
  228. /* other bits are useless when BUSY */
  229. rq->errors |= ERROR_RESET;
  230. } else {
  231. /* add decoding error stuff */
  232. }
  233. if (hwif->tp_ops->read_status(hwif) & (ATA_BUSY | ATA_DRQ))
  234. /* force an abort */
  235. hwif->tp_ops->exec_command(hwif, ATA_CMD_IDLEIMMEDIATE);
  236. if (rq->errors >= ERROR_MAX) {
  237. ide_kill_rq(drive, rq);
  238. } else {
  239. if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
  240. ++rq->errors;
  241. return ide_do_reset(drive);
  242. }
  243. ++rq->errors;
  244. }
  245. return ide_stopped;
  246. }
  247. static ide_startstop_t
  248. __ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
  249. {
  250. if (drive->media == ide_disk)
  251. return ide_ata_error(drive, rq, stat, err);
  252. return ide_atapi_error(drive, rq, stat, err);
  253. }
  254. /**
  255. * ide_error - handle an error on the IDE
  256. * @drive: drive the error occurred on
  257. * @msg: message to report
  258. * @stat: status bits
  259. *
  260. * ide_error() takes action based on the error returned by the drive.
  261. * For normal I/O that may well include retries. We deal with
  262. * both new-style (taskfile) and old style command handling here.
  263. * In the case of taskfile command handling there is work left to
  264. * do
  265. */
  266. ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
  267. {
  268. struct request *rq;
  269. u8 err;
  270. err = ide_dump_status(drive, msg, stat);
  271. rq = drive->hwif->rq;
  272. if (rq == NULL)
  273. return ide_stopped;
  274. /* retry only "normal" I/O: */
  275. if (!blk_fs_request(rq)) {
  276. rq->errors = 1;
  277. ide_end_drive_cmd(drive, stat, err);
  278. return ide_stopped;
  279. }
  280. return __ide_error(drive, rq, stat, err);
  281. }
  282. EXPORT_SYMBOL_GPL(ide_error);
  283. static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  284. {
  285. tf->nsect = drive->sect;
  286. tf->lbal = drive->sect;
  287. tf->lbam = drive->cyl;
  288. tf->lbah = drive->cyl >> 8;
  289. tf->device = (drive->head - 1) | drive->select;
  290. tf->command = ATA_CMD_INIT_DEV_PARAMS;
  291. }
  292. static void ide_tf_set_restore_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  293. {
  294. tf->nsect = drive->sect;
  295. tf->command = ATA_CMD_RESTORE;
  296. }
  297. static void ide_tf_set_setmult_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
  298. {
  299. tf->nsect = drive->mult_req;
  300. tf->command = ATA_CMD_SET_MULTI;
  301. }
  302. static ide_startstop_t ide_disk_special(ide_drive_t *drive)
  303. {
  304. special_t *s = &drive->special;
  305. ide_task_t args;
  306. memset(&args, 0, sizeof(ide_task_t));
  307. args.data_phase = TASKFILE_NO_DATA;
  308. if (s->b.set_geometry) {
  309. s->b.set_geometry = 0;
  310. ide_tf_set_specify_cmd(drive, &args.tf);
  311. } else if (s->b.recalibrate) {
  312. s->b.recalibrate = 0;
  313. ide_tf_set_restore_cmd(drive, &args.tf);
  314. } else if (s->b.set_multmode) {
  315. s->b.set_multmode = 0;
  316. ide_tf_set_setmult_cmd(drive, &args.tf);
  317. } else if (s->all) {
  318. int special = s->all;
  319. s->all = 0;
  320. printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
  321. return ide_stopped;
  322. }
  323. args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE |
  324. IDE_TFLAG_CUSTOM_HANDLER;
  325. do_rw_taskfile(drive, &args);
  326. return ide_started;
  327. }
  328. /**
  329. * do_special - issue some special commands
  330. * @drive: drive the command is for
  331. *
  332. * do_special() is used to issue ATA_CMD_INIT_DEV_PARAMS,
  333. * ATA_CMD_RESTORE and ATA_CMD_SET_MULTI commands to a drive.
  334. *
  335. * It used to do much more, but has been scaled back.
  336. */
  337. static ide_startstop_t do_special (ide_drive_t *drive)
  338. {
  339. special_t *s = &drive->special;
  340. #ifdef DEBUG
  341. printk("%s: do_special: 0x%02x\n", drive->name, s->all);
  342. #endif
  343. if (drive->media == ide_disk)
  344. return ide_disk_special(drive);
  345. s->all = 0;
  346. drive->mult_req = 0;
  347. return ide_stopped;
  348. }
  349. void ide_map_sg(ide_drive_t *drive, struct request *rq)
  350. {
  351. ide_hwif_t *hwif = drive->hwif;
  352. struct scatterlist *sg = hwif->sg_table;
  353. if (rq->cmd_type != REQ_TYPE_ATA_TASKFILE) {
  354. hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
  355. } else {
  356. sg_init_one(sg, rq->buffer, rq->nr_sectors * SECTOR_SIZE);
  357. hwif->sg_nents = 1;
  358. }
  359. }
  360. EXPORT_SYMBOL_GPL(ide_map_sg);
  361. void ide_init_sg_cmd(ide_drive_t *drive, struct request *rq)
  362. {
  363. ide_hwif_t *hwif = drive->hwif;
  364. hwif->nsect = hwif->nleft = rq->nr_sectors;
  365. hwif->cursg_ofs = 0;
  366. hwif->cursg = NULL;
  367. }
  368. EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
  369. /**
  370. * execute_drive_command - issue special drive command
  371. * @drive: the drive to issue the command on
  372. * @rq: the request structure holding the command
  373. *
  374. * execute_drive_cmd() issues a special drive command, usually
  375. * initiated by ioctl() from the external hdparm program. The
  376. * command can be a drive command, drive task or taskfile
  377. * operation. Weirdly you can call it with NULL to wait for
  378. * all commands to finish. Don't do this as that is due to change
  379. */
  380. static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
  381. struct request *rq)
  382. {
  383. ide_hwif_t *hwif = drive->hwif;
  384. ide_task_t *task = rq->special;
  385. if (task) {
  386. hwif->data_phase = task->data_phase;
  387. switch (hwif->data_phase) {
  388. case TASKFILE_MULTI_OUT:
  389. case TASKFILE_OUT:
  390. case TASKFILE_MULTI_IN:
  391. case TASKFILE_IN:
  392. ide_init_sg_cmd(drive, rq);
  393. ide_map_sg(drive, rq);
  394. default:
  395. break;
  396. }
  397. return do_rw_taskfile(drive, task);
  398. }
  399. /*
  400. * NULL is actually a valid way of waiting for
  401. * all current requests to be flushed from the queue.
  402. */
  403. #ifdef DEBUG
  404. printk("%s: DRIVE_CMD (null)\n", drive->name);
  405. #endif
  406. ide_end_drive_cmd(drive, hwif->tp_ops->read_status(hwif),
  407. ide_read_error(drive));
  408. return ide_stopped;
  409. }
  410. int ide_devset_execute(ide_drive_t *drive, const struct ide_devset *setting,
  411. int arg)
  412. {
  413. struct request_queue *q = drive->queue;
  414. struct request *rq;
  415. int ret = 0;
  416. if (!(setting->flags & DS_SYNC))
  417. return setting->set(drive, arg);
  418. rq = blk_get_request(q, READ, __GFP_WAIT);
  419. rq->cmd_type = REQ_TYPE_SPECIAL;
  420. rq->cmd_len = 5;
  421. rq->cmd[0] = REQ_DEVSET_EXEC;
  422. *(int *)&rq->cmd[1] = arg;
  423. rq->special = setting->set;
  424. if (blk_execute_rq(q, NULL, rq, 0))
  425. ret = rq->errors;
  426. blk_put_request(rq);
  427. return ret;
  428. }
  429. EXPORT_SYMBOL_GPL(ide_devset_execute);
  430. static ide_startstop_t ide_special_rq(ide_drive_t *drive, struct request *rq)
  431. {
  432. u8 cmd = rq->cmd[0];
  433. if (cmd == REQ_PARK_HEADS || cmd == REQ_UNPARK_HEADS) {
  434. ide_task_t task;
  435. struct ide_taskfile *tf = &task.tf;
  436. memset(&task, 0, sizeof(task));
  437. if (cmd == REQ_PARK_HEADS) {
  438. drive->sleep = *(unsigned long *)rq->special;
  439. drive->dev_flags |= IDE_DFLAG_SLEEPING;
  440. tf->command = ATA_CMD_IDLEIMMEDIATE;
  441. tf->feature = 0x44;
  442. tf->lbal = 0x4c;
  443. tf->lbam = 0x4e;
  444. tf->lbah = 0x55;
  445. task.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER;
  446. } else /* cmd == REQ_UNPARK_HEADS */
  447. tf->command = ATA_CMD_CHK_POWER;
  448. task.tf_flags |= IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  449. task.rq = rq;
  450. drive->hwif->data_phase = task.data_phase = TASKFILE_NO_DATA;
  451. return do_rw_taskfile(drive, &task);
  452. }
  453. switch (cmd) {
  454. case REQ_DEVSET_EXEC:
  455. {
  456. int err, (*setfunc)(ide_drive_t *, int) = rq->special;
  457. err = setfunc(drive, *(int *)&rq->cmd[1]);
  458. if (err)
  459. rq->errors = err;
  460. else
  461. err = 1;
  462. ide_end_request(drive, err, 0);
  463. return ide_stopped;
  464. }
  465. case REQ_DRIVE_RESET:
  466. return ide_do_reset(drive);
  467. default:
  468. blk_dump_rq_flags(rq, "ide_special_rq - bad request");
  469. ide_end_request(drive, 0, 0);
  470. return ide_stopped;
  471. }
  472. }
  473. /**
  474. * start_request - start of I/O and command issuing for IDE
  475. *
  476. * start_request() initiates handling of a new I/O request. It
  477. * accepts commands and I/O (read/write) requests.
  478. *
  479. * FIXME: this function needs a rename
  480. */
  481. static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
  482. {
  483. ide_startstop_t startstop;
  484. BUG_ON(!blk_rq_started(rq));
  485. #ifdef DEBUG
  486. printk("%s: start_request: current=0x%08lx\n",
  487. drive->hwif->name, (unsigned long) rq);
  488. #endif
  489. /* bail early if we've exceeded max_failures */
  490. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  491. rq->cmd_flags |= REQ_FAILED;
  492. goto kill_rq;
  493. }
  494. if (blk_pm_request(rq))
  495. ide_check_pm_state(drive, rq);
  496. SELECT_DRIVE(drive);
  497. if (ide_wait_stat(&startstop, drive, drive->ready_stat,
  498. ATA_BUSY | ATA_DRQ, WAIT_READY)) {
  499. printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
  500. return startstop;
  501. }
  502. if (!drive->special.all) {
  503. struct ide_driver *drv;
  504. /*
  505. * We reset the drive so we need to issue a SETFEATURES.
  506. * Do it _after_ do_special() restored device parameters.
  507. */
  508. if (drive->current_speed == 0xff)
  509. ide_config_drive_speed(drive, drive->desired_speed);
  510. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  511. return execute_drive_cmd(drive, rq);
  512. else if (blk_pm_request(rq)) {
  513. struct request_pm_state *pm = rq->data;
  514. #ifdef DEBUG_PM
  515. printk("%s: start_power_step(step: %d)\n",
  516. drive->name, pm->pm_step);
  517. #endif
  518. startstop = ide_start_power_step(drive, rq);
  519. if (startstop == ide_stopped &&
  520. pm->pm_step == IDE_PM_COMPLETED)
  521. ide_complete_pm_request(drive, rq);
  522. return startstop;
  523. } else if (!rq->rq_disk && blk_special_request(rq))
  524. /*
  525. * TODO: Once all ULDs have been modified to
  526. * check for specific op codes rather than
  527. * blindly accepting any special request, the
  528. * check for ->rq_disk above may be replaced
  529. * by a more suitable mechanism or even
  530. * dropped entirely.
  531. */
  532. return ide_special_rq(drive, rq);
  533. drv = *(struct ide_driver **)rq->rq_disk->private_data;
  534. return drv->do_request(drive, rq, rq->sector);
  535. }
  536. return do_special(drive);
  537. kill_rq:
  538. ide_kill_rq(drive, rq);
  539. return ide_stopped;
  540. }
  541. /**
  542. * ide_stall_queue - pause an IDE device
  543. * @drive: drive to stall
  544. * @timeout: time to stall for (jiffies)
  545. *
  546. * ide_stall_queue() can be used by a drive to give excess bandwidth back
  547. * to the port by sleeping for timeout jiffies.
  548. */
  549. void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
  550. {
  551. if (timeout > WAIT_WORSTCASE)
  552. timeout = WAIT_WORSTCASE;
  553. drive->sleep = timeout + jiffies;
  554. drive->dev_flags |= IDE_DFLAG_SLEEPING;
  555. }
  556. EXPORT_SYMBOL(ide_stall_queue);
  557. static inline int ide_lock_port(ide_hwif_t *hwif)
  558. {
  559. if (hwif->busy)
  560. return 1;
  561. hwif->busy = 1;
  562. return 0;
  563. }
  564. static inline void ide_unlock_port(ide_hwif_t *hwif)
  565. {
  566. hwif->busy = 0;
  567. }
  568. static inline int ide_lock_host(struct ide_host *host, ide_hwif_t *hwif)
  569. {
  570. int rc = 0;
  571. if (host->host_flags & IDE_HFLAG_SERIALIZE) {
  572. rc = test_and_set_bit_lock(IDE_HOST_BUSY, &host->host_busy);
  573. if (rc == 0) {
  574. /* for atari only */
  575. ide_get_lock(ide_intr, hwif);
  576. }
  577. }
  578. return rc;
  579. }
  580. static inline void ide_unlock_host(struct ide_host *host)
  581. {
  582. if (host->host_flags & IDE_HFLAG_SERIALIZE) {
  583. /* for atari only */
  584. ide_release_lock();
  585. clear_bit_unlock(IDE_HOST_BUSY, &host->host_busy);
  586. }
  587. }
  588. /*
  589. * Issue a new request to a device.
  590. */
  591. void do_ide_request(struct request_queue *q)
  592. {
  593. ide_drive_t *drive = q->queuedata;
  594. ide_hwif_t *hwif = drive->hwif;
  595. struct ide_host *host = hwif->host;
  596. struct request *rq = NULL;
  597. ide_startstop_t startstop;
  598. /*
  599. * drive is doing pre-flush, ordered write, post-flush sequence. even
  600. * though that is 3 requests, it must be seen as a single transaction.
  601. * we must not preempt this drive until that is complete
  602. */
  603. if (blk_queue_flushing(q))
  604. /*
  605. * small race where queue could get replugged during
  606. * the 3-request flush cycle, just yank the plug since
  607. * we want it to finish asap
  608. */
  609. blk_remove_plug(q);
  610. spin_unlock_irq(q->queue_lock);
  611. if (ide_lock_host(host, hwif))
  612. goto plug_device_2;
  613. spin_lock_irq(&hwif->lock);
  614. if (!ide_lock_port(hwif)) {
  615. ide_hwif_t *prev_port;
  616. repeat:
  617. prev_port = hwif->host->cur_port;
  618. hwif->rq = NULL;
  619. if (drive->dev_flags & IDE_DFLAG_SLEEPING) {
  620. if (time_before(drive->sleep, jiffies)) {
  621. ide_unlock_port(hwif);
  622. goto plug_device;
  623. }
  624. }
  625. if ((hwif->host->host_flags & IDE_HFLAG_SERIALIZE) &&
  626. hwif != prev_port) {
  627. /*
  628. * set nIEN for previous port, drives in the
  629. * quirk_list may not like intr setups/cleanups
  630. */
  631. if (prev_port && prev_port->cur_dev->quirk_list == 0)
  632. prev_port->tp_ops->set_irq(prev_port, 0);
  633. hwif->host->cur_port = hwif;
  634. }
  635. hwif->cur_dev = drive;
  636. drive->dev_flags &= ~(IDE_DFLAG_SLEEPING | IDE_DFLAG_PARKED);
  637. spin_unlock_irq(&hwif->lock);
  638. spin_lock_irq(q->queue_lock);
  639. /*
  640. * we know that the queue isn't empty, but this can happen
  641. * if the q->prep_rq_fn() decides to kill a request
  642. */
  643. rq = elv_next_request(drive->queue);
  644. spin_unlock_irq(q->queue_lock);
  645. spin_lock_irq(&hwif->lock);
  646. if (!rq) {
  647. ide_unlock_port(hwif);
  648. goto out;
  649. }
  650. /*
  651. * Sanity: don't accept a request that isn't a PM request
  652. * if we are currently power managed. This is very important as
  653. * blk_stop_queue() doesn't prevent the elv_next_request()
  654. * above to return us whatever is in the queue. Since we call
  655. * ide_do_request() ourselves, we end up taking requests while
  656. * the queue is blocked...
  657. *
  658. * We let requests forced at head of queue with ide-preempt
  659. * though. I hope that doesn't happen too much, hopefully not
  660. * unless the subdriver triggers such a thing in its own PM
  661. * state machine.
  662. */
  663. if ((drive->dev_flags & IDE_DFLAG_BLOCKED) &&
  664. blk_pm_request(rq) == 0 &&
  665. (rq->cmd_flags & REQ_PREEMPT) == 0) {
  666. /* there should be no pending command at this point */
  667. ide_unlock_port(hwif);
  668. goto plug_device;
  669. }
  670. hwif->rq = rq;
  671. spin_unlock_irq(&hwif->lock);
  672. startstop = start_request(drive, rq);
  673. spin_lock_irq(&hwif->lock);
  674. if (startstop == ide_stopped)
  675. goto repeat;
  676. } else
  677. goto plug_device;
  678. out:
  679. spin_unlock_irq(&hwif->lock);
  680. if (rq == NULL)
  681. ide_unlock_host(host);
  682. spin_lock_irq(q->queue_lock);
  683. return;
  684. plug_device:
  685. spin_unlock_irq(&hwif->lock);
  686. ide_unlock_host(host);
  687. plug_device_2:
  688. spin_lock_irq(q->queue_lock);
  689. if (!elv_queue_empty(q))
  690. blk_plug_device(q);
  691. }
  692. /*
  693. * un-busy the port etc, and clear any pending DMA status. we want to
  694. * retry the current request in pio mode instead of risking tossing it
  695. * all away
  696. */
  697. static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
  698. {
  699. ide_hwif_t *hwif = drive->hwif;
  700. struct request *rq;
  701. ide_startstop_t ret = ide_stopped;
  702. /*
  703. * end current dma transaction
  704. */
  705. if (error < 0) {
  706. printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
  707. (void)hwif->dma_ops->dma_end(drive);
  708. ret = ide_error(drive, "dma timeout error",
  709. hwif->tp_ops->read_status(hwif));
  710. } else {
  711. printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
  712. hwif->dma_ops->dma_timeout(drive);
  713. }
  714. /*
  715. * disable dma for now, but remember that we did so because of
  716. * a timeout -- we'll reenable after we finish this next request
  717. * (or rather the first chunk of it) in pio.
  718. */
  719. drive->dev_flags |= IDE_DFLAG_DMA_PIO_RETRY;
  720. drive->retry_pio++;
  721. ide_dma_off_quietly(drive);
  722. /*
  723. * un-busy drive etc and make sure request is sane
  724. */
  725. rq = hwif->rq;
  726. if (!rq)
  727. goto out;
  728. hwif->rq = NULL;
  729. rq->errors = 0;
  730. if (!rq->bio)
  731. goto out;
  732. rq->sector = rq->bio->bi_sector;
  733. rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
  734. rq->hard_cur_sectors = rq->current_nr_sectors;
  735. rq->buffer = bio_data(rq->bio);
  736. out:
  737. return ret;
  738. }
  739. static void ide_plug_device(ide_drive_t *drive)
  740. {
  741. struct request_queue *q = drive->queue;
  742. unsigned long flags;
  743. spin_lock_irqsave(q->queue_lock, flags);
  744. if (!elv_queue_empty(q))
  745. blk_plug_device(q);
  746. spin_unlock_irqrestore(q->queue_lock, flags);
  747. }
  748. /**
  749. * ide_timer_expiry - handle lack of an IDE interrupt
  750. * @data: timer callback magic (hwif)
  751. *
  752. * An IDE command has timed out before the expected drive return
  753. * occurred. At this point we attempt to clean up the current
  754. * mess. If the current handler includes an expiry handler then
  755. * we invoke the expiry handler, and providing it is happy the
  756. * work is done. If that fails we apply generic recovery rules
  757. * invoking the handler and checking the drive DMA status. We
  758. * have an excessively incestuous relationship with the DMA
  759. * logic that wants cleaning up.
  760. */
  761. void ide_timer_expiry (unsigned long data)
  762. {
  763. ide_hwif_t *hwif = (ide_hwif_t *)data;
  764. ide_drive_t *uninitialized_var(drive);
  765. ide_handler_t *handler;
  766. unsigned long flags;
  767. unsigned long wait = -1;
  768. int plug_device = 0;
  769. spin_lock_irqsave(&hwif->lock, flags);
  770. handler = hwif->handler;
  771. if (handler == NULL || hwif->req_gen != hwif->req_gen_timer) {
  772. /*
  773. * Either a marginal timeout occurred
  774. * (got the interrupt just as timer expired),
  775. * or we were "sleeping" to give other devices a chance.
  776. * Either way, we don't really want to complain about anything.
  777. */
  778. } else {
  779. drive = hwif->cur_dev;
  780. if (!drive) {
  781. printk(KERN_ERR "%s: ->cur_dev was NULL\n", __func__);
  782. hwif->handler = NULL;
  783. } else {
  784. ide_expiry_t *expiry = hwif->expiry;
  785. ide_startstop_t startstop = ide_stopped;
  786. if (expiry) {
  787. /* continue */
  788. if ((wait = expiry(drive)) > 0) {
  789. /* reset timer */
  790. hwif->timer.expires = jiffies + wait;
  791. hwif->req_gen_timer = hwif->req_gen;
  792. add_timer(&hwif->timer);
  793. spin_unlock_irqrestore(&hwif->lock, flags);
  794. return;
  795. }
  796. }
  797. hwif->handler = NULL;
  798. /*
  799. * We need to simulate a real interrupt when invoking
  800. * the handler() function, which means we need to
  801. * globally mask the specific IRQ:
  802. */
  803. spin_unlock(&hwif->lock);
  804. /* disable_irq_nosync ?? */
  805. disable_irq(hwif->irq);
  806. /* local CPU only,
  807. * as if we were handling an interrupt */
  808. local_irq_disable();
  809. if (hwif->polling) {
  810. startstop = handler(drive);
  811. } else if (drive_is_ready(drive)) {
  812. if (drive->waiting_for_dma)
  813. hwif->dma_ops->dma_lost_irq(drive);
  814. (void)ide_ack_intr(hwif);
  815. printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
  816. startstop = handler(drive);
  817. } else {
  818. if (drive->waiting_for_dma) {
  819. startstop = ide_dma_timeout_retry(drive, wait);
  820. } else
  821. startstop =
  822. ide_error(drive, "irq timeout",
  823. hwif->tp_ops->read_status(hwif));
  824. }
  825. spin_lock_irq(&hwif->lock);
  826. enable_irq(hwif->irq);
  827. if (startstop == ide_stopped) {
  828. ide_unlock_port(hwif);
  829. plug_device = 1;
  830. }
  831. }
  832. }
  833. spin_unlock_irqrestore(&hwif->lock, flags);
  834. if (plug_device) {
  835. ide_unlock_host(hwif->host);
  836. ide_plug_device(drive);
  837. }
  838. }
  839. /**
  840. * unexpected_intr - handle an unexpected IDE interrupt
  841. * @irq: interrupt line
  842. * @hwif: port being processed
  843. *
  844. * There's nothing really useful we can do with an unexpected interrupt,
  845. * other than reading the status register (to clear it), and logging it.
  846. * There should be no way that an irq can happen before we're ready for it,
  847. * so we needn't worry much about losing an "important" interrupt here.
  848. *
  849. * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
  850. * the drive enters "idle", "standby", or "sleep" mode, so if the status
  851. * looks "good", we just ignore the interrupt completely.
  852. *
  853. * This routine assumes __cli() is in effect when called.
  854. *
  855. * If an unexpected interrupt happens on irq15 while we are handling irq14
  856. * and if the two interfaces are "serialized" (CMD640), then it looks like
  857. * we could screw up by interfering with a new request being set up for
  858. * irq15.
  859. *
  860. * In reality, this is a non-issue. The new command is not sent unless
  861. * the drive is ready to accept one, in which case we know the drive is
  862. * not trying to interrupt us. And ide_set_handler() is always invoked
  863. * before completing the issuance of any new drive command, so we will not
  864. * be accidentally invoked as a result of any valid command completion
  865. * interrupt.
  866. */
  867. static void unexpected_intr(int irq, ide_hwif_t *hwif)
  868. {
  869. u8 stat = hwif->tp_ops->read_status(hwif);
  870. if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
  871. /* Try to not flood the console with msgs */
  872. static unsigned long last_msgtime, count;
  873. ++count;
  874. if (time_after(jiffies, last_msgtime + HZ)) {
  875. last_msgtime = jiffies;
  876. printk(KERN_ERR "%s: unexpected interrupt, "
  877. "status=0x%02x, count=%ld\n",
  878. hwif->name, stat, count);
  879. }
  880. }
  881. }
  882. /**
  883. * ide_intr - default IDE interrupt handler
  884. * @irq: interrupt number
  885. * @dev_id: hwif
  886. * @regs: unused weirdness from the kernel irq layer
  887. *
  888. * This is the default IRQ handler for the IDE layer. You should
  889. * not need to override it. If you do be aware it is subtle in
  890. * places
  891. *
  892. * hwif is the interface in the group currently performing
  893. * a command. hwif->cur_dev is the drive and hwif->handler is
  894. * the IRQ handler to call. As we issue a command the handlers
  895. * step through multiple states, reassigning the handler to the
  896. * next step in the process. Unlike a smart SCSI controller IDE
  897. * expects the main processor to sequence the various transfer
  898. * stages. We also manage a poll timer to catch up with most
  899. * timeout situations. There are still a few where the handlers
  900. * don't ever decide to give up.
  901. *
  902. * The handler eventually returns ide_stopped to indicate the
  903. * request completed. At this point we issue the next request
  904. * on the port and the process begins again.
  905. */
  906. irqreturn_t ide_intr (int irq, void *dev_id)
  907. {
  908. ide_hwif_t *hwif = (ide_hwif_t *)dev_id;
  909. ide_drive_t *uninitialized_var(drive);
  910. ide_handler_t *handler;
  911. unsigned long flags;
  912. ide_startstop_t startstop;
  913. irqreturn_t irq_ret = IRQ_NONE;
  914. int plug_device = 0;
  915. if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE) {
  916. if (hwif != hwif->host->cur_port)
  917. goto out_early;
  918. }
  919. spin_lock_irqsave(&hwif->lock, flags);
  920. if (!ide_ack_intr(hwif))
  921. goto out;
  922. handler = hwif->handler;
  923. if (handler == NULL || hwif->polling) {
  924. /*
  925. * Not expecting an interrupt from this drive.
  926. * That means this could be:
  927. * (1) an interrupt from another PCI device
  928. * sharing the same PCI INT# as us.
  929. * or (2) a drive just entered sleep or standby mode,
  930. * and is interrupting to let us know.
  931. * or (3) a spurious interrupt of unknown origin.
  932. *
  933. * For PCI, we cannot tell the difference,
  934. * so in that case we just ignore it and hope it goes away.
  935. *
  936. * FIXME: unexpected_intr should be hwif-> then we can
  937. * remove all the ifdef PCI crap
  938. */
  939. #ifdef CONFIG_BLK_DEV_IDEPCI
  940. if (hwif->chipset != ide_pci)
  941. #endif /* CONFIG_BLK_DEV_IDEPCI */
  942. {
  943. /*
  944. * Probably not a shared PCI interrupt,
  945. * so we can safely try to do something about it:
  946. */
  947. unexpected_intr(irq, hwif);
  948. #ifdef CONFIG_BLK_DEV_IDEPCI
  949. } else {
  950. /*
  951. * Whack the status register, just in case
  952. * we have a leftover pending IRQ.
  953. */
  954. (void)hwif->tp_ops->read_status(hwif);
  955. #endif /* CONFIG_BLK_DEV_IDEPCI */
  956. }
  957. goto out;
  958. }
  959. drive = hwif->cur_dev;
  960. if (!drive) {
  961. /*
  962. * This should NEVER happen, and there isn't much
  963. * we could do about it here.
  964. *
  965. * [Note - this can occur if the drive is hot unplugged]
  966. */
  967. goto out_handled;
  968. }
  969. if (!drive_is_ready(drive))
  970. /*
  971. * This happens regularly when we share a PCI IRQ with
  972. * another device. Unfortunately, it can also happen
  973. * with some buggy drives that trigger the IRQ before
  974. * their status register is up to date. Hopefully we have
  975. * enough advance overhead that the latter isn't a problem.
  976. */
  977. goto out;
  978. hwif->handler = NULL;
  979. hwif->req_gen++;
  980. del_timer(&hwif->timer);
  981. spin_unlock(&hwif->lock);
  982. if (hwif->port_ops && hwif->port_ops->clear_irq)
  983. hwif->port_ops->clear_irq(drive);
  984. if (drive->dev_flags & IDE_DFLAG_UNMASK)
  985. local_irq_enable_in_hardirq();
  986. /* service this interrupt, may set handler for next interrupt */
  987. startstop = handler(drive);
  988. spin_lock_irq(&hwif->lock);
  989. /*
  990. * Note that handler() may have set things up for another
  991. * interrupt to occur soon, but it cannot happen until
  992. * we exit from this routine, because it will be the
  993. * same irq as is currently being serviced here, and Linux
  994. * won't allow another of the same (on any CPU) until we return.
  995. */
  996. if (startstop == ide_stopped) {
  997. BUG_ON(hwif->handler);
  998. ide_unlock_port(hwif);
  999. plug_device = 1;
  1000. }
  1001. out_handled:
  1002. irq_ret = IRQ_HANDLED;
  1003. out:
  1004. spin_unlock_irqrestore(&hwif->lock, flags);
  1005. out_early:
  1006. if (plug_device) {
  1007. ide_unlock_host(hwif->host);
  1008. ide_plug_device(drive);
  1009. }
  1010. return irq_ret;
  1011. }
  1012. /**
  1013. * ide_do_drive_cmd - issue IDE special command
  1014. * @drive: device to issue command
  1015. * @rq: request to issue
  1016. *
  1017. * This function issues a special IDE device request
  1018. * onto the request queue.
  1019. *
  1020. * the rq is queued at the head of the request queue, displacing
  1021. * the currently-being-processed request and this function
  1022. * returns immediately without waiting for the new rq to be
  1023. * completed. This is VERY DANGEROUS, and is intended for
  1024. * careful use by the ATAPI tape/cdrom driver code.
  1025. */
  1026. void ide_do_drive_cmd(ide_drive_t *drive, struct request *rq)
  1027. {
  1028. struct request_queue *q = drive->queue;
  1029. unsigned long flags;
  1030. drive->hwif->rq = NULL;
  1031. spin_lock_irqsave(q->queue_lock, flags);
  1032. __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0);
  1033. spin_unlock_irqrestore(q->queue_lock, flags);
  1034. }
  1035. EXPORT_SYMBOL(ide_do_drive_cmd);
  1036. void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount, u8 dma)
  1037. {
  1038. ide_hwif_t *hwif = drive->hwif;
  1039. ide_task_t task;
  1040. memset(&task, 0, sizeof(task));
  1041. task.tf_flags = IDE_TFLAG_OUT_LBAH | IDE_TFLAG_OUT_LBAM |
  1042. IDE_TFLAG_OUT_FEATURE | tf_flags;
  1043. task.tf.feature = dma; /* Use PIO/DMA */
  1044. task.tf.lbam = bcount & 0xff;
  1045. task.tf.lbah = (bcount >> 8) & 0xff;
  1046. ide_tf_dump(drive->name, &task.tf);
  1047. hwif->tp_ops->set_irq(hwif, 1);
  1048. SELECT_MASK(drive, 0);
  1049. hwif->tp_ops->tf_load(drive, &task);
  1050. }
  1051. EXPORT_SYMBOL_GPL(ide_pktcmd_tf_load);
  1052. void ide_pad_transfer(ide_drive_t *drive, int write, int len)
  1053. {
  1054. ide_hwif_t *hwif = drive->hwif;
  1055. u8 buf[4] = { 0 };
  1056. while (len > 0) {
  1057. if (write)
  1058. hwif->tp_ops->output_data(drive, NULL, buf, min(4, len));
  1059. else
  1060. hwif->tp_ops->input_data(drive, NULL, buf, min(4, len));
  1061. len -= 4;
  1062. }
  1063. }
  1064. EXPORT_SYMBOL_GPL(ide_pad_transfer);