ide-io.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. sg_init_one(sg, rq->buffer, rq->nr_sectors * SECTOR_SIZE);
  355. hwif->sg_nents = 1;
  356. } else if (!rq->bio) {
  357. sg_init_one(sg, rq->data, rq->data_len);
  358. hwif->sg_nents = 1;
  359. } else {
  360. hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
  361. }
  362. }
  363. EXPORT_SYMBOL_GPL(ide_map_sg);
  364. void ide_init_sg_cmd(ide_drive_t *drive, struct request *rq)
  365. {
  366. ide_hwif_t *hwif = drive->hwif;
  367. hwif->nsect = hwif->nleft = rq->nr_sectors;
  368. hwif->cursg_ofs = 0;
  369. hwif->cursg = NULL;
  370. }
  371. EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
  372. /**
  373. * execute_drive_command - issue special drive command
  374. * @drive: the drive to issue the command on
  375. * @rq: the request structure holding the command
  376. *
  377. * execute_drive_cmd() issues a special drive command, usually
  378. * initiated by ioctl() from the external hdparm program. The
  379. * command can be a drive command, drive task or taskfile
  380. * operation. Weirdly you can call it with NULL to wait for
  381. * all commands to finish. Don't do this as that is due to change
  382. */
  383. static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
  384. struct request *rq)
  385. {
  386. ide_hwif_t *hwif = drive->hwif;
  387. ide_task_t *task = rq->special;
  388. if (task) {
  389. hwif->data_phase = task->data_phase;
  390. switch (hwif->data_phase) {
  391. case TASKFILE_MULTI_OUT:
  392. case TASKFILE_OUT:
  393. case TASKFILE_MULTI_IN:
  394. case TASKFILE_IN:
  395. ide_init_sg_cmd(drive, rq);
  396. ide_map_sg(drive, rq);
  397. default:
  398. break;
  399. }
  400. return do_rw_taskfile(drive, task);
  401. }
  402. /*
  403. * NULL is actually a valid way of waiting for
  404. * all current requests to be flushed from the queue.
  405. */
  406. #ifdef DEBUG
  407. printk("%s: DRIVE_CMD (null)\n", drive->name);
  408. #endif
  409. ide_end_drive_cmd(drive, hwif->tp_ops->read_status(hwif),
  410. ide_read_error(drive));
  411. return ide_stopped;
  412. }
  413. int ide_devset_execute(ide_drive_t *drive, const struct ide_devset *setting,
  414. int arg)
  415. {
  416. struct request_queue *q = drive->queue;
  417. struct request *rq;
  418. int ret = 0;
  419. if (!(setting->flags & DS_SYNC))
  420. return setting->set(drive, arg);
  421. rq = blk_get_request(q, READ, __GFP_WAIT);
  422. rq->cmd_type = REQ_TYPE_SPECIAL;
  423. rq->cmd_len = 5;
  424. rq->cmd[0] = REQ_DEVSET_EXEC;
  425. *(int *)&rq->cmd[1] = arg;
  426. rq->special = setting->set;
  427. if (blk_execute_rq(q, NULL, rq, 0))
  428. ret = rq->errors;
  429. blk_put_request(rq);
  430. return ret;
  431. }
  432. static ide_startstop_t ide_special_rq(ide_drive_t *drive, struct request *rq)
  433. {
  434. u8 cmd = rq->cmd[0];
  435. if (cmd == REQ_PARK_HEADS || cmd == REQ_UNPARK_HEADS) {
  436. ide_task_t task;
  437. struct ide_taskfile *tf = &task.tf;
  438. memset(&task, 0, sizeof(task));
  439. if (cmd == REQ_PARK_HEADS) {
  440. drive->sleep = *(unsigned long *)rq->special;
  441. drive->dev_flags |= IDE_DFLAG_SLEEPING;
  442. tf->command = ATA_CMD_IDLEIMMEDIATE;
  443. tf->feature = 0x44;
  444. tf->lbal = 0x4c;
  445. tf->lbam = 0x4e;
  446. tf->lbah = 0x55;
  447. task.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER;
  448. } else /* cmd == REQ_UNPARK_HEADS */
  449. tf->command = ATA_CMD_CHK_POWER;
  450. task.tf_flags |= IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  451. task.rq = rq;
  452. drive->hwif->data_phase = task.data_phase = TASKFILE_NO_DATA;
  453. return do_rw_taskfile(drive, &task);
  454. }
  455. switch (cmd) {
  456. case REQ_DEVSET_EXEC:
  457. {
  458. int err, (*setfunc)(ide_drive_t *, int) = rq->special;
  459. err = setfunc(drive, *(int *)&rq->cmd[1]);
  460. if (err)
  461. rq->errors = err;
  462. else
  463. err = 1;
  464. ide_end_request(drive, err, 0);
  465. return ide_stopped;
  466. }
  467. case REQ_DRIVE_RESET:
  468. return ide_do_reset(drive);
  469. default:
  470. blk_dump_rq_flags(rq, "ide_special_rq - bad request");
  471. ide_end_request(drive, 0, 0);
  472. return ide_stopped;
  473. }
  474. }
  475. /**
  476. * start_request - start of I/O and command issuing for IDE
  477. *
  478. * start_request() initiates handling of a new I/O request. It
  479. * accepts commands and I/O (read/write) requests.
  480. *
  481. * FIXME: this function needs a rename
  482. */
  483. static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
  484. {
  485. ide_startstop_t startstop;
  486. BUG_ON(!blk_rq_started(rq));
  487. #ifdef DEBUG
  488. printk("%s: start_request: current=0x%08lx\n",
  489. drive->hwif->name, (unsigned long) rq);
  490. #endif
  491. /* bail early if we've exceeded max_failures */
  492. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  493. rq->cmd_flags |= REQ_FAILED;
  494. goto kill_rq;
  495. }
  496. if (blk_pm_request(rq))
  497. ide_check_pm_state(drive, rq);
  498. SELECT_DRIVE(drive);
  499. if (ide_wait_stat(&startstop, drive, drive->ready_stat,
  500. ATA_BUSY | ATA_DRQ, WAIT_READY)) {
  501. printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
  502. return startstop;
  503. }
  504. if (!drive->special.all) {
  505. struct ide_driver *drv;
  506. /*
  507. * We reset the drive so we need to issue a SETFEATURES.
  508. * Do it _after_ do_special() restored device parameters.
  509. */
  510. if (drive->current_speed == 0xff)
  511. ide_config_drive_speed(drive, drive->desired_speed);
  512. if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE)
  513. return execute_drive_cmd(drive, rq);
  514. else if (blk_pm_request(rq)) {
  515. struct request_pm_state *pm = rq->data;
  516. #ifdef DEBUG_PM
  517. printk("%s: start_power_step(step: %d)\n",
  518. drive->name, pm->pm_step);
  519. #endif
  520. startstop = ide_start_power_step(drive, rq);
  521. if (startstop == ide_stopped &&
  522. pm->pm_step == IDE_PM_COMPLETED)
  523. ide_complete_pm_request(drive, rq);
  524. return startstop;
  525. } else if (!rq->rq_disk && blk_special_request(rq))
  526. /*
  527. * TODO: Once all ULDs have been modified to
  528. * check for specific op codes rather than
  529. * blindly accepting any special request, the
  530. * check for ->rq_disk above may be replaced
  531. * by a more suitable mechanism or even
  532. * dropped entirely.
  533. */
  534. return ide_special_rq(drive, rq);
  535. drv = *(struct ide_driver **)rq->rq_disk->private_data;
  536. return drv->do_request(drive, rq, rq->sector);
  537. }
  538. return do_special(drive);
  539. kill_rq:
  540. ide_kill_rq(drive, rq);
  541. return ide_stopped;
  542. }
  543. /**
  544. * ide_stall_queue - pause an IDE device
  545. * @drive: drive to stall
  546. * @timeout: time to stall for (jiffies)
  547. *
  548. * ide_stall_queue() can be used by a drive to give excess bandwidth back
  549. * to the port by sleeping for timeout jiffies.
  550. */
  551. void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
  552. {
  553. if (timeout > WAIT_WORSTCASE)
  554. timeout = WAIT_WORSTCASE;
  555. drive->sleep = timeout + jiffies;
  556. drive->dev_flags |= IDE_DFLAG_SLEEPING;
  557. }
  558. EXPORT_SYMBOL(ide_stall_queue);
  559. static inline int ide_lock_port(ide_hwif_t *hwif)
  560. {
  561. if (hwif->busy)
  562. return 1;
  563. hwif->busy = 1;
  564. return 0;
  565. }
  566. static inline void ide_unlock_port(ide_hwif_t *hwif)
  567. {
  568. hwif->busy = 0;
  569. }
  570. static inline int ide_lock_host(struct ide_host *host, ide_hwif_t *hwif)
  571. {
  572. int rc = 0;
  573. if (host->host_flags & IDE_HFLAG_SERIALIZE) {
  574. rc = test_and_set_bit_lock(IDE_HOST_BUSY, &host->host_busy);
  575. if (rc == 0) {
  576. /* for atari only */
  577. ide_get_lock(ide_intr, hwif);
  578. }
  579. }
  580. return rc;
  581. }
  582. static inline void ide_unlock_host(struct ide_host *host)
  583. {
  584. if (host->host_flags & IDE_HFLAG_SERIALIZE) {
  585. /* for atari only */
  586. ide_release_lock();
  587. clear_bit_unlock(IDE_HOST_BUSY, &host->host_busy);
  588. }
  589. }
  590. /*
  591. * Issue a new request to a device.
  592. */
  593. void do_ide_request(struct request_queue *q)
  594. {
  595. ide_drive_t *drive = q->queuedata;
  596. ide_hwif_t *hwif = drive->hwif;
  597. struct ide_host *host = hwif->host;
  598. struct request *rq = NULL;
  599. ide_startstop_t startstop;
  600. /*
  601. * drive is doing pre-flush, ordered write, post-flush sequence. even
  602. * though that is 3 requests, it must be seen as a single transaction.
  603. * we must not preempt this drive until that is complete
  604. */
  605. if (blk_queue_flushing(q))
  606. /*
  607. * small race where queue could get replugged during
  608. * the 3-request flush cycle, just yank the plug since
  609. * we want it to finish asap
  610. */
  611. blk_remove_plug(q);
  612. spin_unlock_irq(q->queue_lock);
  613. if (ide_lock_host(host, hwif))
  614. goto plug_device_2;
  615. spin_lock_irq(&hwif->lock);
  616. if (!ide_lock_port(hwif)) {
  617. ide_hwif_t *prev_port;
  618. repeat:
  619. prev_port = hwif->host->cur_port;
  620. hwif->rq = NULL;
  621. if (drive->dev_flags & IDE_DFLAG_SLEEPING) {
  622. if (time_before(drive->sleep, jiffies)) {
  623. ide_unlock_port(hwif);
  624. goto plug_device;
  625. }
  626. }
  627. if ((hwif->host->host_flags & IDE_HFLAG_SERIALIZE) &&
  628. hwif != prev_port) {
  629. /*
  630. * set nIEN for previous port, drives in the
  631. * quirk_list may not like intr setups/cleanups
  632. */
  633. if (prev_port && prev_port->cur_dev->quirk_list == 0)
  634. prev_port->tp_ops->set_irq(prev_port, 0);
  635. hwif->host->cur_port = hwif;
  636. }
  637. hwif->cur_dev = drive;
  638. drive->dev_flags &= ~(IDE_DFLAG_SLEEPING | IDE_DFLAG_PARKED);
  639. spin_unlock_irq(&hwif->lock);
  640. spin_lock_irq(q->queue_lock);
  641. /*
  642. * we know that the queue isn't empty, but this can happen
  643. * if the q->prep_rq_fn() decides to kill a request
  644. */
  645. rq = elv_next_request(drive->queue);
  646. spin_unlock_irq(q->queue_lock);
  647. spin_lock_irq(&hwif->lock);
  648. if (!rq) {
  649. ide_unlock_port(hwif);
  650. goto out;
  651. }
  652. /*
  653. * Sanity: don't accept a request that isn't a PM request
  654. * if we are currently power managed. This is very important as
  655. * blk_stop_queue() doesn't prevent the elv_next_request()
  656. * above to return us whatever is in the queue. Since we call
  657. * ide_do_request() ourselves, we end up taking requests while
  658. * the queue is blocked...
  659. *
  660. * We let requests forced at head of queue with ide-preempt
  661. * though. I hope that doesn't happen too much, hopefully not
  662. * unless the subdriver triggers such a thing in its own PM
  663. * state machine.
  664. */
  665. if ((drive->dev_flags & IDE_DFLAG_BLOCKED) &&
  666. blk_pm_request(rq) == 0 &&
  667. (rq->cmd_flags & REQ_PREEMPT) == 0) {
  668. /* there should be no pending command at this point */
  669. ide_unlock_port(hwif);
  670. goto plug_device;
  671. }
  672. hwif->rq = rq;
  673. spin_unlock_irq(&hwif->lock);
  674. startstop = start_request(drive, rq);
  675. spin_lock_irq(&hwif->lock);
  676. if (startstop == ide_stopped)
  677. goto repeat;
  678. } else
  679. goto plug_device;
  680. out:
  681. spin_unlock_irq(&hwif->lock);
  682. if (rq == NULL)
  683. ide_unlock_host(host);
  684. spin_lock_irq(q->queue_lock);
  685. return;
  686. plug_device:
  687. spin_unlock_irq(&hwif->lock);
  688. ide_unlock_host(host);
  689. plug_device_2:
  690. spin_lock_irq(q->queue_lock);
  691. if (!elv_queue_empty(q))
  692. blk_plug_device(q);
  693. }
  694. static void ide_plug_device(ide_drive_t *drive)
  695. {
  696. struct request_queue *q = drive->queue;
  697. unsigned long flags;
  698. spin_lock_irqsave(q->queue_lock, flags);
  699. if (!elv_queue_empty(q))
  700. blk_plug_device(q);
  701. spin_unlock_irqrestore(q->queue_lock, flags);
  702. }
  703. static int drive_is_ready(ide_drive_t *drive)
  704. {
  705. ide_hwif_t *hwif = drive->hwif;
  706. u8 stat = 0;
  707. if (drive->waiting_for_dma)
  708. return hwif->dma_ops->dma_test_irq(drive);
  709. if (hwif->io_ports.ctl_addr &&
  710. (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0)
  711. stat = hwif->tp_ops->read_altstatus(hwif);
  712. else
  713. /* Note: this may clear a pending IRQ!! */
  714. stat = hwif->tp_ops->read_status(hwif);
  715. if (stat & ATA_BUSY)
  716. /* drive busy: definitely not interrupting */
  717. return 0;
  718. /* drive ready: *might* be interrupting */
  719. return 1;
  720. }
  721. /**
  722. * ide_timer_expiry - handle lack of an IDE interrupt
  723. * @data: timer callback magic (hwif)
  724. *
  725. * An IDE command has timed out before the expected drive return
  726. * occurred. At this point we attempt to clean up the current
  727. * mess. If the current handler includes an expiry handler then
  728. * we invoke the expiry handler, and providing it is happy the
  729. * work is done. If that fails we apply generic recovery rules
  730. * invoking the handler and checking the drive DMA status. We
  731. * have an excessively incestuous relationship with the DMA
  732. * logic that wants cleaning up.
  733. */
  734. void ide_timer_expiry (unsigned long data)
  735. {
  736. ide_hwif_t *hwif = (ide_hwif_t *)data;
  737. ide_drive_t *uninitialized_var(drive);
  738. ide_handler_t *handler;
  739. unsigned long flags;
  740. int wait = -1;
  741. int plug_device = 0;
  742. spin_lock_irqsave(&hwif->lock, flags);
  743. handler = hwif->handler;
  744. if (handler == NULL || hwif->req_gen != hwif->req_gen_timer) {
  745. /*
  746. * Either a marginal timeout occurred
  747. * (got the interrupt just as timer expired),
  748. * or we were "sleeping" to give other devices a chance.
  749. * Either way, we don't really want to complain about anything.
  750. */
  751. } else {
  752. ide_expiry_t *expiry = hwif->expiry;
  753. ide_startstop_t startstop = ide_stopped;
  754. drive = hwif->cur_dev;
  755. if (expiry) {
  756. wait = expiry(drive);
  757. if (wait > 0) { /* continue */
  758. /* reset timer */
  759. hwif->timer.expires = jiffies + wait;
  760. hwif->req_gen_timer = hwif->req_gen;
  761. add_timer(&hwif->timer);
  762. spin_unlock_irqrestore(&hwif->lock, flags);
  763. return;
  764. }
  765. }
  766. hwif->handler = NULL;
  767. /*
  768. * We need to simulate a real interrupt when invoking
  769. * the handler() function, which means we need to
  770. * globally mask the specific IRQ:
  771. */
  772. spin_unlock(&hwif->lock);
  773. /* disable_irq_nosync ?? */
  774. disable_irq(hwif->irq);
  775. /* local CPU only, as if we were handling an interrupt */
  776. local_irq_disable();
  777. if (hwif->polling) {
  778. startstop = handler(drive);
  779. } else if (drive_is_ready(drive)) {
  780. if (drive->waiting_for_dma)
  781. hwif->dma_ops->dma_lost_irq(drive);
  782. (void)ide_ack_intr(hwif);
  783. printk(KERN_WARNING "%s: lost interrupt\n",
  784. drive->name);
  785. startstop = handler(drive);
  786. } else {
  787. if (drive->waiting_for_dma)
  788. startstop = ide_dma_timeout_retry(drive, wait);
  789. else
  790. startstop = ide_error(drive, "irq timeout",
  791. hwif->tp_ops->read_status(hwif));
  792. }
  793. spin_lock_irq(&hwif->lock);
  794. enable_irq(hwif->irq);
  795. if (startstop == ide_stopped) {
  796. ide_unlock_port(hwif);
  797. plug_device = 1;
  798. }
  799. }
  800. spin_unlock_irqrestore(&hwif->lock, flags);
  801. if (plug_device) {
  802. ide_unlock_host(hwif->host);
  803. ide_plug_device(drive);
  804. }
  805. }
  806. /**
  807. * unexpected_intr - handle an unexpected IDE interrupt
  808. * @irq: interrupt line
  809. * @hwif: port being processed
  810. *
  811. * There's nothing really useful we can do with an unexpected interrupt,
  812. * other than reading the status register (to clear it), and logging it.
  813. * There should be no way that an irq can happen before we're ready for it,
  814. * so we needn't worry much about losing an "important" interrupt here.
  815. *
  816. * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
  817. * the drive enters "idle", "standby", or "sleep" mode, so if the status
  818. * looks "good", we just ignore the interrupt completely.
  819. *
  820. * This routine assumes __cli() is in effect when called.
  821. *
  822. * If an unexpected interrupt happens on irq15 while we are handling irq14
  823. * and if the two interfaces are "serialized" (CMD640), then it looks like
  824. * we could screw up by interfering with a new request being set up for
  825. * irq15.
  826. *
  827. * In reality, this is a non-issue. The new command is not sent unless
  828. * the drive is ready to accept one, in which case we know the drive is
  829. * not trying to interrupt us. And ide_set_handler() is always invoked
  830. * before completing the issuance of any new drive command, so we will not
  831. * be accidentally invoked as a result of any valid command completion
  832. * interrupt.
  833. */
  834. static void unexpected_intr(int irq, ide_hwif_t *hwif)
  835. {
  836. u8 stat = hwif->tp_ops->read_status(hwif);
  837. if (!OK_STAT(stat, ATA_DRDY, BAD_STAT)) {
  838. /* Try to not flood the console with msgs */
  839. static unsigned long last_msgtime, count;
  840. ++count;
  841. if (time_after(jiffies, last_msgtime + HZ)) {
  842. last_msgtime = jiffies;
  843. printk(KERN_ERR "%s: unexpected interrupt, "
  844. "status=0x%02x, count=%ld\n",
  845. hwif->name, stat, count);
  846. }
  847. }
  848. }
  849. /**
  850. * ide_intr - default IDE interrupt handler
  851. * @irq: interrupt number
  852. * @dev_id: hwif
  853. * @regs: unused weirdness from the kernel irq layer
  854. *
  855. * This is the default IRQ handler for the IDE layer. You should
  856. * not need to override it. If you do be aware it is subtle in
  857. * places
  858. *
  859. * hwif is the interface in the group currently performing
  860. * a command. hwif->cur_dev is the drive and hwif->handler is
  861. * the IRQ handler to call. As we issue a command the handlers
  862. * step through multiple states, reassigning the handler to the
  863. * next step in the process. Unlike a smart SCSI controller IDE
  864. * expects the main processor to sequence the various transfer
  865. * stages. We also manage a poll timer to catch up with most
  866. * timeout situations. There are still a few where the handlers
  867. * don't ever decide to give up.
  868. *
  869. * The handler eventually returns ide_stopped to indicate the
  870. * request completed. At this point we issue the next request
  871. * on the port and the process begins again.
  872. */
  873. irqreturn_t ide_intr (int irq, void *dev_id)
  874. {
  875. ide_hwif_t *hwif = (ide_hwif_t *)dev_id;
  876. ide_drive_t *uninitialized_var(drive);
  877. ide_handler_t *handler;
  878. unsigned long flags;
  879. ide_startstop_t startstop;
  880. irqreturn_t irq_ret = IRQ_NONE;
  881. int plug_device = 0;
  882. if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE) {
  883. if (hwif != hwif->host->cur_port)
  884. goto out_early;
  885. }
  886. spin_lock_irqsave(&hwif->lock, flags);
  887. if (!ide_ack_intr(hwif))
  888. goto out;
  889. handler = hwif->handler;
  890. if (handler == NULL || hwif->polling) {
  891. /*
  892. * Not expecting an interrupt from this drive.
  893. * That means this could be:
  894. * (1) an interrupt from another PCI device
  895. * sharing the same PCI INT# as us.
  896. * or (2) a drive just entered sleep or standby mode,
  897. * and is interrupting to let us know.
  898. * or (3) a spurious interrupt of unknown origin.
  899. *
  900. * For PCI, we cannot tell the difference,
  901. * so in that case we just ignore it and hope it goes away.
  902. *
  903. * FIXME: unexpected_intr should be hwif-> then we can
  904. * remove all the ifdef PCI crap
  905. */
  906. #ifdef CONFIG_BLK_DEV_IDEPCI
  907. if (hwif->chipset != ide_pci)
  908. #endif /* CONFIG_BLK_DEV_IDEPCI */
  909. {
  910. /*
  911. * Probably not a shared PCI interrupt,
  912. * so we can safely try to do something about it:
  913. */
  914. unexpected_intr(irq, hwif);
  915. #ifdef CONFIG_BLK_DEV_IDEPCI
  916. } else {
  917. /*
  918. * Whack the status register, just in case
  919. * we have a leftover pending IRQ.
  920. */
  921. (void)hwif->tp_ops->read_status(hwif);
  922. #endif /* CONFIG_BLK_DEV_IDEPCI */
  923. }
  924. goto out;
  925. }
  926. drive = hwif->cur_dev;
  927. if (!drive_is_ready(drive))
  928. /*
  929. * This happens regularly when we share a PCI IRQ with
  930. * another device. Unfortunately, it can also happen
  931. * with some buggy drives that trigger the IRQ before
  932. * their status register is up to date. Hopefully we have
  933. * enough advance overhead that the latter isn't a problem.
  934. */
  935. goto out;
  936. hwif->handler = NULL;
  937. hwif->req_gen++;
  938. del_timer(&hwif->timer);
  939. spin_unlock(&hwif->lock);
  940. if (hwif->port_ops && hwif->port_ops->clear_irq)
  941. hwif->port_ops->clear_irq(drive);
  942. if (drive->dev_flags & IDE_DFLAG_UNMASK)
  943. local_irq_enable_in_hardirq();
  944. /* service this interrupt, may set handler for next interrupt */
  945. startstop = handler(drive);
  946. spin_lock_irq(&hwif->lock);
  947. /*
  948. * Note that handler() may have set things up for another
  949. * interrupt to occur soon, but it cannot happen until
  950. * we exit from this routine, because it will be the
  951. * same irq as is currently being serviced here, and Linux
  952. * won't allow another of the same (on any CPU) until we return.
  953. */
  954. if (startstop == ide_stopped) {
  955. BUG_ON(hwif->handler);
  956. ide_unlock_port(hwif);
  957. plug_device = 1;
  958. }
  959. irq_ret = IRQ_HANDLED;
  960. out:
  961. spin_unlock_irqrestore(&hwif->lock, flags);
  962. out_early:
  963. if (plug_device) {
  964. ide_unlock_host(hwif->host);
  965. ide_plug_device(drive);
  966. }
  967. return irq_ret;
  968. }
  969. EXPORT_SYMBOL_GPL(ide_intr);
  970. void ide_pad_transfer(ide_drive_t *drive, int write, int len)
  971. {
  972. ide_hwif_t *hwif = drive->hwif;
  973. u8 buf[4] = { 0 };
  974. while (len > 0) {
  975. if (write)
  976. hwif->tp_ops->output_data(drive, NULL, buf, min(4, len));
  977. else
  978. hwif->tp_ops->input_data(drive, NULL, buf, min(4, len));
  979. len -= 4;
  980. }
  981. }
  982. EXPORT_SYMBOL_GPL(ide_pad_transfer);