Merge git://git.kernel.org/pub/scm/linux/kernel/git/brodo/pcmcia-fixes-2.6
[deliverable/linux.git] / drivers / ide / ide-io.c
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
26
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/string.h>
31 #include <linux/kernel.h>
32 #include <linux/timer.h>
33 #include <linux/mm.h>
34 #include <linux/interrupt.h>
35 #include <linux/major.h>
36 #include <linux/errno.h>
37 #include <linux/genhd.h>
38 #include <linux/blkpg.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/pci.h>
42 #include <linux/delay.h>
43 #include <linux/ide.h>
44 #include <linux/completion.h>
45 #include <linux/reboot.h>
46 #include <linux/cdrom.h>
47 #include <linux/seq_file.h>
48 #include <linux/device.h>
49 #include <linux/kmod.h>
50 #include <linux/scatterlist.h>
51
52 #include <asm/byteorder.h>
53 #include <asm/irq.h>
54 #include <asm/uaccess.h>
55 #include <asm/io.h>
56 #include <asm/bitops.h>
57
58 int __ide_end_request(ide_drive_t *drive, struct request *rq, int uptodate,
59 int nr_sectors)
60 {
61 int ret = 1;
62
63 BUG_ON(!(rq->flags & REQ_STARTED));
64
65 /*
66 * if failfast is set on a request, override number of sectors and
67 * complete the whole request right now
68 */
69 if (blk_noretry_request(rq) && end_io_error(uptodate))
70 nr_sectors = rq->hard_nr_sectors;
71
72 if (!blk_fs_request(rq) && end_io_error(uptodate) && !rq->errors)
73 rq->errors = -EIO;
74
75 /*
76 * decide whether to reenable DMA -- 3 is a random magic for now,
77 * if we DMA timeout more than 3 times, just stay in PIO
78 */
79 if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
80 drive->state = 0;
81 HWGROUP(drive)->hwif->ide_dma_on(drive);
82 }
83
84 if (!end_that_request_first(rq, uptodate, nr_sectors)) {
85 add_disk_randomness(rq->rq_disk);
86 blkdev_dequeue_request(rq);
87 HWGROUP(drive)->rq = NULL;
88 end_that_request_last(rq, uptodate);
89 ret = 0;
90 }
91
92 return ret;
93 }
94 EXPORT_SYMBOL(__ide_end_request);
95
96 /**
97 * ide_end_request - complete an IDE I/O
98 * @drive: IDE device for the I/O
99 * @uptodate:
100 * @nr_sectors: number of sectors completed
101 *
102 * This is our end_request wrapper function. We complete the I/O
103 * update random number input and dequeue the request, which if
104 * it was tagged may be out of order.
105 */
106
107 int ide_end_request (ide_drive_t *drive, int uptodate, int nr_sectors)
108 {
109 struct request *rq;
110 unsigned long flags;
111 int ret = 1;
112
113 /*
114 * room for locking improvements here, the calls below don't
115 * need the queue lock held at all
116 */
117 spin_lock_irqsave(&ide_lock, flags);
118 rq = HWGROUP(drive)->rq;
119
120 if (!nr_sectors)
121 nr_sectors = rq->hard_cur_sectors;
122
123 ret = __ide_end_request(drive, rq, uptodate, nr_sectors);
124
125 spin_unlock_irqrestore(&ide_lock, flags);
126 return ret;
127 }
128 EXPORT_SYMBOL(ide_end_request);
129
130 /*
131 * Power Management state machine. This one is rather trivial for now,
132 * we should probably add more, like switching back to PIO on suspend
133 * to help some BIOSes, re-do the door locking on resume, etc...
134 */
135
136 enum {
137 ide_pm_flush_cache = ide_pm_state_start_suspend,
138 idedisk_pm_standby,
139
140 idedisk_pm_idle = ide_pm_state_start_resume,
141 ide_pm_restore_dma,
142 };
143
144 static void ide_complete_power_step(ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
145 {
146 if (drive->media != ide_disk)
147 return;
148
149 switch (rq->pm->pm_step) {
150 case ide_pm_flush_cache: /* Suspend step 1 (flush cache) complete */
151 if (rq->pm->pm_state == PM_EVENT_FREEZE)
152 rq->pm->pm_step = ide_pm_state_completed;
153 else
154 rq->pm->pm_step = idedisk_pm_standby;
155 break;
156 case idedisk_pm_standby: /* Suspend step 2 (standby) complete */
157 rq->pm->pm_step = ide_pm_state_completed;
158 break;
159 case idedisk_pm_idle: /* Resume step 1 (idle) complete */
160 rq->pm->pm_step = ide_pm_restore_dma;
161 break;
162 }
163 }
164
165 static ide_startstop_t ide_start_power_step(ide_drive_t *drive, struct request *rq)
166 {
167 ide_task_t *args = rq->special;
168
169 memset(args, 0, sizeof(*args));
170
171 if (drive->media != ide_disk) {
172 /* skip idedisk_pm_idle for ATAPI devices */
173 if (rq->pm->pm_step == idedisk_pm_idle)
174 rq->pm->pm_step = ide_pm_restore_dma;
175 }
176
177 switch (rq->pm->pm_step) {
178 case ide_pm_flush_cache: /* Suspend step 1 (flush cache) */
179 if (drive->media != ide_disk)
180 break;
181 /* Not supported? Switch to next step now. */
182 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
183 ide_complete_power_step(drive, rq, 0, 0);
184 return ide_stopped;
185 }
186 if (ide_id_has_flush_cache_ext(drive->id))
187 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
188 else
189 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
190 args->command_type = IDE_DRIVE_TASK_NO_DATA;
191 args->handler = &task_no_data_intr;
192 return do_rw_taskfile(drive, args);
193
194 case idedisk_pm_standby: /* Suspend step 2 (standby) */
195 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
196 args->command_type = IDE_DRIVE_TASK_NO_DATA;
197 args->handler = &task_no_data_intr;
198 return do_rw_taskfile(drive, args);
199
200 case idedisk_pm_idle: /* Resume step 1 (idle) */
201 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE;
202 args->command_type = IDE_DRIVE_TASK_NO_DATA;
203 args->handler = task_no_data_intr;
204 return do_rw_taskfile(drive, args);
205
206 case ide_pm_restore_dma: /* Resume step 2 (restore DMA) */
207 /*
208 * Right now, all we do is call hwif->ide_dma_check(drive),
209 * we could be smarter and check for current xfer_speed
210 * in struct drive etc...
211 */
212 if ((drive->id->capability & 1) == 0)
213 break;
214 if (drive->hwif->ide_dma_check == NULL)
215 break;
216 drive->hwif->ide_dma_check(drive);
217 break;
218 }
219 rq->pm->pm_step = ide_pm_state_completed;
220 return ide_stopped;
221 }
222
223 /**
224 * ide_complete_pm_request - end the current Power Management request
225 * @drive: target drive
226 * @rq: request
227 *
228 * This function cleans up the current PM request and stops the queue
229 * if necessary.
230 */
231 static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
232 {
233 unsigned long flags;
234
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->blocked = 0;
244 blk_start_queue(drive->queue);
245 }
246 blkdev_dequeue_request(rq);
247 HWGROUP(drive)->rq = NULL;
248 end_that_request_last(rq, 1);
249 spin_unlock_irqrestore(&ide_lock, flags);
250 }
251
252 /*
253 * FIXME: probably move this somewhere else, name is bad too :)
254 */
255 u64 ide_get_error_location(ide_drive_t *drive, char *args)
256 {
257 u32 high, low;
258 u8 hcyl, lcyl, sect;
259 u64 sector;
260
261 high = 0;
262 hcyl = args[5];
263 lcyl = args[4];
264 sect = args[3];
265
266 if (ide_id_has_flush_cache_ext(drive->id)) {
267 low = (hcyl << 16) | (lcyl << 8) | sect;
268 HWIF(drive)->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
269 high = ide_read_24(drive);
270 } else {
271 u8 cur = HWIF(drive)->INB(IDE_SELECT_REG);
272 if (cur & 0x40) {
273 high = cur & 0xf;
274 low = (hcyl << 16) | (lcyl << 8) | sect;
275 } else {
276 low = hcyl * drive->head * drive->sect;
277 low += lcyl * drive->sect;
278 low += sect - 1;
279 }
280 }
281
282 sector = ((u64) high << 24) | low;
283 return sector;
284 }
285 EXPORT_SYMBOL(ide_get_error_location);
286
287 /**
288 * ide_end_drive_cmd - end an explicit drive command
289 * @drive: command
290 * @stat: status bits
291 * @err: error bits
292 *
293 * Clean up after success/failure of an explicit drive command.
294 * These get thrown onto the queue so they are synchronized with
295 * real I/O operations on the drive.
296 *
297 * In LBA48 mode we have to read the register set twice to get
298 * all the extra information out.
299 */
300
301 void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
302 {
303 ide_hwif_t *hwif = HWIF(drive);
304 unsigned long flags;
305 struct request *rq;
306
307 spin_lock_irqsave(&ide_lock, flags);
308 rq = HWGROUP(drive)->rq;
309 spin_unlock_irqrestore(&ide_lock, flags);
310
311 if (rq->flags & REQ_DRIVE_CMD) {
312 u8 *args = (u8 *) rq->buffer;
313 if (rq->errors == 0)
314 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
315
316 if (args) {
317 args[0] = stat;
318 args[1] = err;
319 args[2] = hwif->INB(IDE_NSECTOR_REG);
320 }
321 } else if (rq->flags & REQ_DRIVE_TASK) {
322 u8 *args = (u8 *) rq->buffer;
323 if (rq->errors == 0)
324 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
325
326 if (args) {
327 args[0] = stat;
328 args[1] = err;
329 args[2] = hwif->INB(IDE_NSECTOR_REG);
330 args[3] = hwif->INB(IDE_SECTOR_REG);
331 args[4] = hwif->INB(IDE_LCYL_REG);
332 args[5] = hwif->INB(IDE_HCYL_REG);
333 args[6] = hwif->INB(IDE_SELECT_REG);
334 }
335 } else if (rq->flags & REQ_DRIVE_TASKFILE) {
336 ide_task_t *args = (ide_task_t *) rq->special;
337 if (rq->errors == 0)
338 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
339
340 if (args) {
341 if (args->tf_in_flags.b.data) {
342 u16 data = hwif->INW(IDE_DATA_REG);
343 args->tfRegister[IDE_DATA_OFFSET] = (data) & 0xFF;
344 args->hobRegister[IDE_DATA_OFFSET] = (data >> 8) & 0xFF;
345 }
346 args->tfRegister[IDE_ERROR_OFFSET] = err;
347 /* be sure we're looking at the low order bits */
348 hwif->OUTB(drive->ctl & ~0x80, IDE_CONTROL_REG);
349 args->tfRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
350 args->tfRegister[IDE_SECTOR_OFFSET] = hwif->INB(IDE_SECTOR_REG);
351 args->tfRegister[IDE_LCYL_OFFSET] = hwif->INB(IDE_LCYL_REG);
352 args->tfRegister[IDE_HCYL_OFFSET] = hwif->INB(IDE_HCYL_REG);
353 args->tfRegister[IDE_SELECT_OFFSET] = hwif->INB(IDE_SELECT_REG);
354 args->tfRegister[IDE_STATUS_OFFSET] = stat;
355
356 if (drive->addressing == 1) {
357 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
358 args->hobRegister[IDE_FEATURE_OFFSET] = hwif->INB(IDE_FEATURE_REG);
359 args->hobRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
360 args->hobRegister[IDE_SECTOR_OFFSET] = hwif->INB(IDE_SECTOR_REG);
361 args->hobRegister[IDE_LCYL_OFFSET] = hwif->INB(IDE_LCYL_REG);
362 args->hobRegister[IDE_HCYL_OFFSET] = hwif->INB(IDE_HCYL_REG);
363 }
364 }
365 } else if (blk_pm_request(rq)) {
366 #ifdef DEBUG_PM
367 printk("%s: complete_power_step(step: %d, stat: %x, err: %x)\n",
368 drive->name, rq->pm->pm_step, stat, err);
369 #endif
370 ide_complete_power_step(drive, rq, stat, err);
371 if (rq->pm->pm_step == ide_pm_state_completed)
372 ide_complete_pm_request(drive, rq);
373 return;
374 }
375
376 spin_lock_irqsave(&ide_lock, flags);
377 blkdev_dequeue_request(rq);
378 HWGROUP(drive)->rq = NULL;
379 rq->errors = err;
380 end_that_request_last(rq, !rq->errors);
381 spin_unlock_irqrestore(&ide_lock, flags);
382 }
383
384 EXPORT_SYMBOL(ide_end_drive_cmd);
385
386 /**
387 * try_to_flush_leftover_data - flush junk
388 * @drive: drive to flush
389 *
390 * try_to_flush_leftover_data() is invoked in response to a drive
391 * unexpectedly having its DRQ_STAT bit set. As an alternative to
392 * resetting the drive, this routine tries to clear the condition
393 * by read a sector's worth of data from the drive. Of course,
394 * this may not help if the drive is *waiting* for data from *us*.
395 */
396 static void try_to_flush_leftover_data (ide_drive_t *drive)
397 {
398 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
399
400 if (drive->media != ide_disk)
401 return;
402 while (i > 0) {
403 u32 buffer[16];
404 u32 wcount = (i > 16) ? 16 : i;
405
406 i -= wcount;
407 HWIF(drive)->ata_input_data(drive, buffer, wcount);
408 }
409 }
410
411 static void ide_kill_rq(ide_drive_t *drive, struct request *rq)
412 {
413 if (rq->rq_disk) {
414 ide_driver_t *drv;
415
416 drv = *(ide_driver_t **)rq->rq_disk->private_data;
417 drv->end_request(drive, 0, 0);
418 } else
419 ide_end_request(drive, 0, 0);
420 }
421
422 static ide_startstop_t ide_ata_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
423 {
424 ide_hwif_t *hwif = drive->hwif;
425
426 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
427 /* other bits are useless when BUSY */
428 rq->errors |= ERROR_RESET;
429 } else if (stat & ERR_STAT) {
430 /* err has different meaning on cdrom and tape */
431 if (err == ABRT_ERR) {
432 if (drive->select.b.lba &&
433 /* some newer drives don't support WIN_SPECIFY */
434 hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
435 return ide_stopped;
436 } else if ((err & BAD_CRC) == BAD_CRC) {
437 /* UDMA crc error, just retry the operation */
438 drive->crc_count++;
439 } else if (err & (BBD_ERR | ECC_ERR)) {
440 /* retries won't help these */
441 rq->errors = ERROR_MAX;
442 } else if (err & TRK0_ERR) {
443 /* help it find track zero */
444 rq->errors |= ERROR_RECAL;
445 }
446 }
447
448 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
449 try_to_flush_leftover_data(drive);
450
451 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
452 /* force an abort */
453 hwif->OUTB(WIN_IDLEIMMEDIATE, IDE_COMMAND_REG);
454
455 if (rq->errors >= ERROR_MAX || blk_noretry_request(rq))
456 ide_kill_rq(drive, rq);
457 else {
458 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
459 ++rq->errors;
460 return ide_do_reset(drive);
461 }
462 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
463 drive->special.b.recalibrate = 1;
464 ++rq->errors;
465 }
466 return ide_stopped;
467 }
468
469 static ide_startstop_t ide_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
470 {
471 ide_hwif_t *hwif = drive->hwif;
472
473 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
474 /* other bits are useless when BUSY */
475 rq->errors |= ERROR_RESET;
476 } else {
477 /* add decoding error stuff */
478 }
479
480 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
481 /* force an abort */
482 hwif->OUTB(WIN_IDLEIMMEDIATE, IDE_COMMAND_REG);
483
484 if (rq->errors >= ERROR_MAX) {
485 ide_kill_rq(drive, rq);
486 } else {
487 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
488 ++rq->errors;
489 return ide_do_reset(drive);
490 }
491 ++rq->errors;
492 }
493
494 return ide_stopped;
495 }
496
497 ide_startstop_t
498 __ide_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
499 {
500 if (drive->media == ide_disk)
501 return ide_ata_error(drive, rq, stat, err);
502 return ide_atapi_error(drive, rq, stat, err);
503 }
504
505 EXPORT_SYMBOL_GPL(__ide_error);
506
507 /**
508 * ide_error - handle an error on the IDE
509 * @drive: drive the error occurred on
510 * @msg: message to report
511 * @stat: status bits
512 *
513 * ide_error() takes action based on the error returned by the drive.
514 * For normal I/O that may well include retries. We deal with
515 * both new-style (taskfile) and old style command handling here.
516 * In the case of taskfile command handling there is work left to
517 * do
518 */
519
520 ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
521 {
522 struct request *rq;
523 u8 err;
524
525 err = ide_dump_status(drive, msg, stat);
526
527 if ((rq = HWGROUP(drive)->rq) == NULL)
528 return ide_stopped;
529
530 /* retry only "normal" I/O: */
531 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
532 rq->errors = 1;
533 ide_end_drive_cmd(drive, stat, err);
534 return ide_stopped;
535 }
536
537 if (rq->rq_disk) {
538 ide_driver_t *drv;
539
540 drv = *(ide_driver_t **)rq->rq_disk->private_data;
541 return drv->error(drive, rq, stat, err);
542 } else
543 return __ide_error(drive, rq, stat, err);
544 }
545
546 EXPORT_SYMBOL_GPL(ide_error);
547
548 ide_startstop_t __ide_abort(ide_drive_t *drive, struct request *rq)
549 {
550 if (drive->media != ide_disk)
551 rq->errors |= ERROR_RESET;
552
553 ide_kill_rq(drive, rq);
554
555 return ide_stopped;
556 }
557
558 EXPORT_SYMBOL_GPL(__ide_abort);
559
560 /**
561 * ide_abort - abort pending IDE operations
562 * @drive: drive the error occurred on
563 * @msg: message to report
564 *
565 * ide_abort kills and cleans up when we are about to do a
566 * host initiated reset on active commands. Longer term we
567 * want handlers to have sensible abort handling themselves
568 *
569 * This differs fundamentally from ide_error because in
570 * this case the command is doing just fine when we
571 * blow it away.
572 */
573
574 ide_startstop_t ide_abort(ide_drive_t *drive, const char *msg)
575 {
576 struct request *rq;
577
578 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
579 return ide_stopped;
580
581 /* retry only "normal" I/O: */
582 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
583 rq->errors = 1;
584 ide_end_drive_cmd(drive, BUSY_STAT, 0);
585 return ide_stopped;
586 }
587
588 if (rq->rq_disk) {
589 ide_driver_t *drv;
590
591 drv = *(ide_driver_t **)rq->rq_disk->private_data;
592 return drv->abort(drive, rq);
593 } else
594 return __ide_abort(drive, rq);
595 }
596
597 /**
598 * ide_cmd - issue a simple drive command
599 * @drive: drive the command is for
600 * @cmd: command byte
601 * @nsect: sector byte
602 * @handler: handler for the command completion
603 *
604 * Issue a simple drive command with interrupts.
605 * The drive must be selected beforehand.
606 */
607
608 static void ide_cmd (ide_drive_t *drive, u8 cmd, u8 nsect,
609 ide_handler_t *handler)
610 {
611 ide_hwif_t *hwif = HWIF(drive);
612 if (IDE_CONTROL_REG)
613 hwif->OUTB(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
614 SELECT_MASK(drive,0);
615 hwif->OUTB(nsect,IDE_NSECTOR_REG);
616 ide_execute_command(drive, cmd, handler, WAIT_CMD, NULL);
617 }
618
619 /**
620 * drive_cmd_intr - drive command completion interrupt
621 * @drive: drive the completion interrupt occurred on
622 *
623 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
624 * We do any necessary data reading and then wait for the drive to
625 * go non busy. At that point we may read the error data and complete
626 * the request
627 */
628
629 static ide_startstop_t drive_cmd_intr (ide_drive_t *drive)
630 {
631 struct request *rq = HWGROUP(drive)->rq;
632 ide_hwif_t *hwif = HWIF(drive);
633 u8 *args = (u8 *) rq->buffer;
634 u8 stat = hwif->INB(IDE_STATUS_REG);
635 int retries = 10;
636
637 local_irq_enable();
638 if ((stat & DRQ_STAT) && args && args[3]) {
639 u8 io_32bit = drive->io_32bit;
640 drive->io_32bit = 0;
641 hwif->ata_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
642 drive->io_32bit = io_32bit;
643 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
644 udelay(100);
645 }
646
647 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
648 return ide_error(drive, "drive_cmd", stat);
649 /* calls ide_end_drive_cmd */
650 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
651 return ide_stopped;
652 }
653
654 static void ide_init_specify_cmd(ide_drive_t *drive, ide_task_t *task)
655 {
656 task->tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
657 task->tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
658 task->tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
659 task->tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
660 task->tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
661 task->tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
662
663 task->handler = &set_geometry_intr;
664 }
665
666 static void ide_init_restore_cmd(ide_drive_t *drive, ide_task_t *task)
667 {
668 task->tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
669 task->tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
670
671 task->handler = &recal_intr;
672 }
673
674 static void ide_init_setmult_cmd(ide_drive_t *drive, ide_task_t *task)
675 {
676 task->tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
677 task->tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
678
679 task->handler = &set_multmode_intr;
680 }
681
682 static ide_startstop_t ide_disk_special(ide_drive_t *drive)
683 {
684 special_t *s = &drive->special;
685 ide_task_t args;
686
687 memset(&args, 0, sizeof(ide_task_t));
688 args.command_type = IDE_DRIVE_TASK_NO_DATA;
689
690 if (s->b.set_geometry) {
691 s->b.set_geometry = 0;
692 ide_init_specify_cmd(drive, &args);
693 } else if (s->b.recalibrate) {
694 s->b.recalibrate = 0;
695 ide_init_restore_cmd(drive, &args);
696 } else if (s->b.set_multmode) {
697 s->b.set_multmode = 0;
698 if (drive->mult_req > drive->id->max_multsect)
699 drive->mult_req = drive->id->max_multsect;
700 ide_init_setmult_cmd(drive, &args);
701 } else if (s->all) {
702 int special = s->all;
703 s->all = 0;
704 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
705 return ide_stopped;
706 }
707
708 do_rw_taskfile(drive, &args);
709
710 return ide_started;
711 }
712
713 /**
714 * do_special - issue some special commands
715 * @drive: drive the command is for
716 *
717 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
718 * commands to a drive. It used to do much more, but has been scaled
719 * back.
720 */
721
722 static ide_startstop_t do_special (ide_drive_t *drive)
723 {
724 special_t *s = &drive->special;
725
726 #ifdef DEBUG
727 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
728 #endif
729 if (s->b.set_tune) {
730 s->b.set_tune = 0;
731 if (HWIF(drive)->tuneproc != NULL)
732 HWIF(drive)->tuneproc(drive, drive->tune_req);
733 return ide_stopped;
734 } else {
735 if (drive->media == ide_disk)
736 return ide_disk_special(drive);
737
738 s->all = 0;
739 drive->mult_req = 0;
740 return ide_stopped;
741 }
742 }
743
744 void ide_map_sg(ide_drive_t *drive, struct request *rq)
745 {
746 ide_hwif_t *hwif = drive->hwif;
747 struct scatterlist *sg = hwif->sg_table;
748
749 if (hwif->sg_mapped) /* needed by ide-scsi */
750 return;
751
752 if ((rq->flags & REQ_DRIVE_TASKFILE) == 0) {
753 hwif->sg_nents = blk_rq_map_sg(drive->queue, rq, sg);
754 } else {
755 sg_init_one(sg, rq->buffer, rq->nr_sectors * SECTOR_SIZE);
756 hwif->sg_nents = 1;
757 }
758 }
759
760 EXPORT_SYMBOL_GPL(ide_map_sg);
761
762 void ide_init_sg_cmd(ide_drive_t *drive, struct request *rq)
763 {
764 ide_hwif_t *hwif = drive->hwif;
765
766 hwif->nsect = hwif->nleft = rq->nr_sectors;
767 hwif->cursg = hwif->cursg_ofs = 0;
768 }
769
770 EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
771
772 /**
773 * execute_drive_command - issue special drive command
774 * @drive: the drive to issue the command on
775 * @rq: the request structure holding the command
776 *
777 * execute_drive_cmd() issues a special drive command, usually
778 * initiated by ioctl() from the external hdparm program. The
779 * command can be a drive command, drive task or taskfile
780 * operation. Weirdly you can call it with NULL to wait for
781 * all commands to finish. Don't do this as that is due to change
782 */
783
784 static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
785 struct request *rq)
786 {
787 ide_hwif_t *hwif = HWIF(drive);
788 if (rq->flags & REQ_DRIVE_TASKFILE) {
789 ide_task_t *args = rq->special;
790
791 if (!args)
792 goto done;
793
794 hwif->data_phase = args->data_phase;
795
796 switch (hwif->data_phase) {
797 case TASKFILE_MULTI_OUT:
798 case TASKFILE_OUT:
799 case TASKFILE_MULTI_IN:
800 case TASKFILE_IN:
801 ide_init_sg_cmd(drive, rq);
802 ide_map_sg(drive, rq);
803 default:
804 break;
805 }
806
807 if (args->tf_out_flags.all != 0)
808 return flagged_taskfile(drive, args);
809 return do_rw_taskfile(drive, args);
810 } else if (rq->flags & REQ_DRIVE_TASK) {
811 u8 *args = rq->buffer;
812 u8 sel;
813
814 if (!args)
815 goto done;
816 #ifdef DEBUG
817 printk("%s: DRIVE_TASK_CMD ", drive->name);
818 printk("cmd=0x%02x ", args[0]);
819 printk("fr=0x%02x ", args[1]);
820 printk("ns=0x%02x ", args[2]);
821 printk("sc=0x%02x ", args[3]);
822 printk("lcyl=0x%02x ", args[4]);
823 printk("hcyl=0x%02x ", args[5]);
824 printk("sel=0x%02x\n", args[6]);
825 #endif
826 hwif->OUTB(args[1], IDE_FEATURE_REG);
827 hwif->OUTB(args[3], IDE_SECTOR_REG);
828 hwif->OUTB(args[4], IDE_LCYL_REG);
829 hwif->OUTB(args[5], IDE_HCYL_REG);
830 sel = (args[6] & ~0x10);
831 if (drive->select.b.unit)
832 sel |= 0x10;
833 hwif->OUTB(sel, IDE_SELECT_REG);
834 ide_cmd(drive, args[0], args[2], &drive_cmd_intr);
835 return ide_started;
836 } else if (rq->flags & REQ_DRIVE_CMD) {
837 u8 *args = rq->buffer;
838
839 if (!args)
840 goto done;
841 #ifdef DEBUG
842 printk("%s: DRIVE_CMD ", drive->name);
843 printk("cmd=0x%02x ", args[0]);
844 printk("sc=0x%02x ", args[1]);
845 printk("fr=0x%02x ", args[2]);
846 printk("xx=0x%02x\n", args[3]);
847 #endif
848 if (args[0] == WIN_SMART) {
849 hwif->OUTB(0x4f, IDE_LCYL_REG);
850 hwif->OUTB(0xc2, IDE_HCYL_REG);
851 hwif->OUTB(args[2],IDE_FEATURE_REG);
852 hwif->OUTB(args[1],IDE_SECTOR_REG);
853 ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
854 return ide_started;
855 }
856 hwif->OUTB(args[2],IDE_FEATURE_REG);
857 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
858 return ide_started;
859 }
860
861 done:
862 /*
863 * NULL is actually a valid way of waiting for
864 * all current requests to be flushed from the queue.
865 */
866 #ifdef DEBUG
867 printk("%s: DRIVE_CMD (null)\n", drive->name);
868 #endif
869 ide_end_drive_cmd(drive,
870 hwif->INB(IDE_STATUS_REG),
871 hwif->INB(IDE_ERROR_REG));
872 return ide_stopped;
873 }
874
875 /**
876 * start_request - start of I/O and command issuing for IDE
877 *
878 * start_request() initiates handling of a new I/O request. It
879 * accepts commands and I/O (read/write) requests. It also does
880 * the final remapping for weird stuff like EZDrive. Once
881 * device mapper can work sector level the EZDrive stuff can go away
882 *
883 * FIXME: this function needs a rename
884 */
885
886 static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
887 {
888 ide_startstop_t startstop;
889 sector_t block;
890
891 BUG_ON(!(rq->flags & REQ_STARTED));
892
893 #ifdef DEBUG
894 printk("%s: start_request: current=0x%08lx\n",
895 HWIF(drive)->name, (unsigned long) rq);
896 #endif
897
898 /* bail early if we've exceeded max_failures */
899 if (drive->max_failures && (drive->failures > drive->max_failures)) {
900 goto kill_rq;
901 }
902
903 block = rq->sector;
904 if (blk_fs_request(rq) &&
905 (drive->media == ide_disk || drive->media == ide_floppy)) {
906 block += drive->sect0;
907 }
908 /* Yecch - this will shift the entire interval,
909 possibly killing some innocent following sector */
910 if (block == 0 && drive->remap_0_to_1 == 1)
911 block = 1; /* redirect MBR access to EZ-Drive partn table */
912
913 if (blk_pm_suspend_request(rq) &&
914 rq->pm->pm_step == ide_pm_state_start_suspend)
915 /* Mark drive blocked when starting the suspend sequence. */
916 drive->blocked = 1;
917 else if (blk_pm_resume_request(rq) &&
918 rq->pm->pm_step == ide_pm_state_start_resume) {
919 /*
920 * The first thing we do on wakeup is to wait for BSY bit to
921 * go away (with a looong timeout) as a drive on this hwif may
922 * just be POSTing itself.
923 * We do that before even selecting as the "other" device on
924 * the bus may be broken enough to walk on our toes at this
925 * point.
926 */
927 int rc;
928 #ifdef DEBUG_PM
929 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
930 #endif
931 rc = ide_wait_not_busy(HWIF(drive), 35000);
932 if (rc)
933 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
934 SELECT_DRIVE(drive);
935 HWIF(drive)->OUTB(8, HWIF(drive)->io_ports[IDE_CONTROL_OFFSET]);
936 rc = ide_wait_not_busy(HWIF(drive), 10000);
937 if (rc)
938 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
939 }
940
941 SELECT_DRIVE(drive);
942 if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
943 printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
944 return startstop;
945 }
946 if (!drive->special.all) {
947 ide_driver_t *drv;
948
949 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK))
950 return execute_drive_cmd(drive, rq);
951 else if (rq->flags & REQ_DRIVE_TASKFILE)
952 return execute_drive_cmd(drive, rq);
953 else if (blk_pm_request(rq)) {
954 #ifdef DEBUG_PM
955 printk("%s: start_power_step(step: %d)\n",
956 drive->name, rq->pm->pm_step);
957 #endif
958 startstop = ide_start_power_step(drive, rq);
959 if (startstop == ide_stopped &&
960 rq->pm->pm_step == ide_pm_state_completed)
961 ide_complete_pm_request(drive, rq);
962 return startstop;
963 }
964
965 drv = *(ide_driver_t **)rq->rq_disk->private_data;
966 return drv->do_request(drive, rq, block);
967 }
968 return do_special(drive);
969 kill_rq:
970 ide_kill_rq(drive, rq);
971 return ide_stopped;
972 }
973
974 /**
975 * ide_stall_queue - pause an IDE device
976 * @drive: drive to stall
977 * @timeout: time to stall for (jiffies)
978 *
979 * ide_stall_queue() can be used by a drive to give excess bandwidth back
980 * to the hwgroup by sleeping for timeout jiffies.
981 */
982
983 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
984 {
985 if (timeout > WAIT_WORSTCASE)
986 timeout = WAIT_WORSTCASE;
987 drive->sleep = timeout + jiffies;
988 drive->sleeping = 1;
989 }
990
991 EXPORT_SYMBOL(ide_stall_queue);
992
993 #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
994
995 /**
996 * choose_drive - select a drive to service
997 * @hwgroup: hardware group to select on
998 *
999 * choose_drive() selects the next drive which will be serviced.
1000 * This is necessary because the IDE layer can't issue commands
1001 * to both drives on the same cable, unlike SCSI.
1002 */
1003
1004 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
1005 {
1006 ide_drive_t *drive, *best;
1007
1008 repeat:
1009 best = NULL;
1010 drive = hwgroup->drive;
1011
1012 /*
1013 * drive is doing pre-flush, ordered write, post-flush sequence. even
1014 * though that is 3 requests, it must be seen as a single transaction.
1015 * we must not preempt this drive until that is complete
1016 */
1017 if (blk_queue_flushing(drive->queue)) {
1018 /*
1019 * small race where queue could get replugged during
1020 * the 3-request flush cycle, just yank the plug since
1021 * we want it to finish asap
1022 */
1023 blk_remove_plug(drive->queue);
1024 return drive;
1025 }
1026
1027 do {
1028 if ((!drive->sleeping || time_after_eq(jiffies, drive->sleep))
1029 && !elv_queue_empty(drive->queue)) {
1030 if (!best
1031 || (drive->sleeping && (!best->sleeping || time_before(drive->sleep, best->sleep)))
1032 || (!best->sleeping && time_before(WAKEUP(drive), WAKEUP(best))))
1033 {
1034 if (!blk_queue_plugged(drive->queue))
1035 best = drive;
1036 }
1037 }
1038 } while ((drive = drive->next) != hwgroup->drive);
1039 if (best && best->nice1 && !best->sleeping && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
1040 long t = (signed long)(WAKEUP(best) - jiffies);
1041 if (t >= WAIT_MIN_SLEEP) {
1042 /*
1043 * We *may* have some time to spare, but first let's see if
1044 * someone can potentially benefit from our nice mood today..
1045 */
1046 drive = best->next;
1047 do {
1048 if (!drive->sleeping
1049 && time_before(jiffies - best->service_time, WAKEUP(drive))
1050 && time_before(WAKEUP(drive), jiffies + t))
1051 {
1052 ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
1053 goto repeat;
1054 }
1055 } while ((drive = drive->next) != best);
1056 }
1057 }
1058 return best;
1059 }
1060
1061 /*
1062 * Issue a new request to a drive from hwgroup
1063 * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
1064 *
1065 * A hwgroup is a serialized group of IDE interfaces. Usually there is
1066 * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
1067 * may have both interfaces in a single hwgroup to "serialize" access.
1068 * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
1069 * together into one hwgroup for serialized access.
1070 *
1071 * Note also that several hwgroups can end up sharing a single IRQ,
1072 * possibly along with many other devices. This is especially common in
1073 * PCI-based systems with off-board IDE controller cards.
1074 *
1075 * The IDE driver uses the single global ide_lock spinlock to protect
1076 * access to the request queues, and to protect the hwgroup->busy flag.
1077 *
1078 * The first thread into the driver for a particular hwgroup sets the
1079 * hwgroup->busy flag to indicate that this hwgroup is now active,
1080 * and then initiates processing of the top request from the request queue.
1081 *
1082 * Other threads attempting entry notice the busy setting, and will simply
1083 * queue their new requests and exit immediately. Note that hwgroup->busy
1084 * remains set even when the driver is merely awaiting the next interrupt.
1085 * Thus, the meaning is "this hwgroup is busy processing a request".
1086 *
1087 * When processing of a request completes, the completing thread or IRQ-handler
1088 * will start the next request from the queue. If no more work remains,
1089 * the driver will clear the hwgroup->busy flag and exit.
1090 *
1091 * The ide_lock (spinlock) is used to protect all access to the
1092 * hwgroup->busy flag, but is otherwise not needed for most processing in
1093 * the driver. This makes the driver much more friendlier to shared IRQs
1094 * than previous designs, while remaining 100% (?) SMP safe and capable.
1095 */
1096 static void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
1097 {
1098 ide_drive_t *drive;
1099 ide_hwif_t *hwif;
1100 struct request *rq;
1101 ide_startstop_t startstop;
1102 int loops = 0;
1103
1104 /* for atari only: POSSIBLY BROKEN HERE(?) */
1105 ide_get_lock(ide_intr, hwgroup);
1106
1107 /* caller must own ide_lock */
1108 BUG_ON(!irqs_disabled());
1109
1110 while (!hwgroup->busy) {
1111 hwgroup->busy = 1;
1112 drive = choose_drive(hwgroup);
1113 if (drive == NULL) {
1114 int sleeping = 0;
1115 unsigned long sleep = 0; /* shut up, gcc */
1116 hwgroup->rq = NULL;
1117 drive = hwgroup->drive;
1118 do {
1119 if (drive->sleeping && (!sleeping || time_before(drive->sleep, sleep))) {
1120 sleeping = 1;
1121 sleep = drive->sleep;
1122 }
1123 } while ((drive = drive->next) != hwgroup->drive);
1124 if (sleeping) {
1125 /*
1126 * Take a short snooze, and then wake up this hwgroup again.
1127 * This gives other hwgroups on the same a chance to
1128 * play fairly with us, just in case there are big differences
1129 * in relative throughputs.. don't want to hog the cpu too much.
1130 */
1131 if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
1132 sleep = jiffies + WAIT_MIN_SLEEP;
1133 #if 1
1134 if (timer_pending(&hwgroup->timer))
1135 printk(KERN_CRIT "ide_set_handler: timer already active\n");
1136 #endif
1137 /* so that ide_timer_expiry knows what to do */
1138 hwgroup->sleeping = 1;
1139 mod_timer(&hwgroup->timer, sleep);
1140 /* we purposely leave hwgroup->busy==1
1141 * while sleeping */
1142 } else {
1143 /* Ugly, but how can we sleep for the lock
1144 * otherwise? perhaps from tq_disk?
1145 */
1146
1147 /* for atari only */
1148 ide_release_lock();
1149 hwgroup->busy = 0;
1150 }
1151
1152 /* no more work for this hwgroup (for now) */
1153 return;
1154 }
1155 again:
1156 hwif = HWIF(drive);
1157 if (hwgroup->hwif->sharing_irq &&
1158 hwif != hwgroup->hwif &&
1159 hwif->io_ports[IDE_CONTROL_OFFSET]) {
1160 /* set nIEN for previous hwif */
1161 SELECT_INTERRUPT(drive);
1162 }
1163 hwgroup->hwif = hwif;
1164 hwgroup->drive = drive;
1165 drive->sleeping = 0;
1166 drive->service_start = jiffies;
1167
1168 if (blk_queue_plugged(drive->queue)) {
1169 printk(KERN_ERR "ide: huh? queue was plugged!\n");
1170 break;
1171 }
1172
1173 /*
1174 * we know that the queue isn't empty, but this can happen
1175 * if the q->prep_rq_fn() decides to kill a request
1176 */
1177 rq = elv_next_request(drive->queue);
1178 if (!rq) {
1179 hwgroup->busy = 0;
1180 break;
1181 }
1182
1183 /*
1184 * Sanity: don't accept a request that isn't a PM request
1185 * if we are currently power managed. This is very important as
1186 * blk_stop_queue() doesn't prevent the elv_next_request()
1187 * above to return us whatever is in the queue. Since we call
1188 * ide_do_request() ourselves, we end up taking requests while
1189 * the queue is blocked...
1190 *
1191 * We let requests forced at head of queue with ide-preempt
1192 * though. I hope that doesn't happen too much, hopefully not
1193 * unless the subdriver triggers such a thing in its own PM
1194 * state machine.
1195 *
1196 * We count how many times we loop here to make sure we service
1197 * all drives in the hwgroup without looping for ever
1198 */
1199 if (drive->blocked && !blk_pm_request(rq) && !(rq->flags & REQ_PREEMPT)) {
1200 drive = drive->next ? drive->next : hwgroup->drive;
1201 if (loops++ < 4 && !blk_queue_plugged(drive->queue))
1202 goto again;
1203 /* We clear busy, there should be no pending ATA command at this point. */
1204 hwgroup->busy = 0;
1205 break;
1206 }
1207
1208 hwgroup->rq = rq;
1209
1210 /*
1211 * Some systems have trouble with IDE IRQs arriving while
1212 * the driver is still setting things up. So, here we disable
1213 * the IRQ used by this interface while the request is being started.
1214 * This may look bad at first, but pretty much the same thing
1215 * happens anyway when any interrupt comes in, IDE or otherwise
1216 * -- the kernel masks the IRQ while it is being handled.
1217 */
1218 if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
1219 disable_irq_nosync(hwif->irq);
1220 spin_unlock(&ide_lock);
1221 local_irq_enable();
1222 /* allow other IRQs while we start this request */
1223 startstop = start_request(drive, rq);
1224 spin_lock_irq(&ide_lock);
1225 if (masked_irq != IDE_NO_IRQ && hwif->irq != masked_irq)
1226 enable_irq(hwif->irq);
1227 if (startstop == ide_stopped)
1228 hwgroup->busy = 0;
1229 }
1230 }
1231
1232 /*
1233 * Passes the stuff to ide_do_request
1234 */
1235 void do_ide_request(request_queue_t *q)
1236 {
1237 ide_drive_t *drive = q->queuedata;
1238
1239 ide_do_request(HWGROUP(drive), IDE_NO_IRQ);
1240 }
1241
1242 /*
1243 * un-busy the hwgroup etc, and clear any pending DMA status. we want to
1244 * retry the current request in pio mode instead of risking tossing it
1245 * all away
1246 */
1247 static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
1248 {
1249 ide_hwif_t *hwif = HWIF(drive);
1250 struct request *rq;
1251 ide_startstop_t ret = ide_stopped;
1252
1253 /*
1254 * end current dma transaction
1255 */
1256
1257 if (error < 0) {
1258 printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
1259 (void)HWIF(drive)->ide_dma_end(drive);
1260 ret = ide_error(drive, "dma timeout error",
1261 hwif->INB(IDE_STATUS_REG));
1262 } else {
1263 printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
1264 (void) hwif->ide_dma_timeout(drive);
1265 }
1266
1267 /*
1268 * disable dma for now, but remember that we did so because of
1269 * a timeout -- we'll reenable after we finish this next request
1270 * (or rather the first chunk of it) in pio.
1271 */
1272 drive->retry_pio++;
1273 drive->state = DMA_PIO_RETRY;
1274 (void) hwif->ide_dma_off_quietly(drive);
1275
1276 /*
1277 * un-busy drive etc (hwgroup->busy is cleared on return) and
1278 * make sure request is sane
1279 */
1280 rq = HWGROUP(drive)->rq;
1281 HWGROUP(drive)->rq = NULL;
1282
1283 rq->errors = 0;
1284
1285 if (!rq->bio)
1286 goto out;
1287
1288 rq->sector = rq->bio->bi_sector;
1289 rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
1290 rq->hard_cur_sectors = rq->current_nr_sectors;
1291 rq->buffer = bio_data(rq->bio);
1292 out:
1293 return ret;
1294 }
1295
1296 /**
1297 * ide_timer_expiry - handle lack of an IDE interrupt
1298 * @data: timer callback magic (hwgroup)
1299 *
1300 * An IDE command has timed out before the expected drive return
1301 * occurred. At this point we attempt to clean up the current
1302 * mess. If the current handler includes an expiry handler then
1303 * we invoke the expiry handler, and providing it is happy the
1304 * work is done. If that fails we apply generic recovery rules
1305 * invoking the handler and checking the drive DMA status. We
1306 * have an excessively incestuous relationship with the DMA
1307 * logic that wants cleaning up.
1308 */
1309
1310 void ide_timer_expiry (unsigned long data)
1311 {
1312 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1313 ide_handler_t *handler;
1314 ide_expiry_t *expiry;
1315 unsigned long flags;
1316 unsigned long wait = -1;
1317
1318 spin_lock_irqsave(&ide_lock, flags);
1319
1320 if ((handler = hwgroup->handler) == NULL) {
1321 /*
1322 * Either a marginal timeout occurred
1323 * (got the interrupt just as timer expired),
1324 * or we were "sleeping" to give other devices a chance.
1325 * Either way, we don't really want to complain about anything.
1326 */
1327 if (hwgroup->sleeping) {
1328 hwgroup->sleeping = 0;
1329 hwgroup->busy = 0;
1330 }
1331 } else {
1332 ide_drive_t *drive = hwgroup->drive;
1333 if (!drive) {
1334 printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
1335 hwgroup->handler = NULL;
1336 } else {
1337 ide_hwif_t *hwif;
1338 ide_startstop_t startstop = ide_stopped;
1339 if (!hwgroup->busy) {
1340 hwgroup->busy = 1; /* paranoia */
1341 printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
1342 }
1343 if ((expiry = hwgroup->expiry) != NULL) {
1344 /* continue */
1345 if ((wait = expiry(drive)) > 0) {
1346 /* reset timer */
1347 hwgroup->timer.expires = jiffies + wait;
1348 add_timer(&hwgroup->timer);
1349 spin_unlock_irqrestore(&ide_lock, flags);
1350 return;
1351 }
1352 }
1353 hwgroup->handler = NULL;
1354 /*
1355 * We need to simulate a real interrupt when invoking
1356 * the handler() function, which means we need to
1357 * globally mask the specific IRQ:
1358 */
1359 spin_unlock(&ide_lock);
1360 hwif = HWIF(drive);
1361 #if DISABLE_IRQ_NOSYNC
1362 disable_irq_nosync(hwif->irq);
1363 #else
1364 /* disable_irq_nosync ?? */
1365 disable_irq(hwif->irq);
1366 #endif /* DISABLE_IRQ_NOSYNC */
1367 /* local CPU only,
1368 * as if we were handling an interrupt */
1369 local_irq_disable();
1370 if (hwgroup->polling) {
1371 startstop = handler(drive);
1372 } else if (drive_is_ready(drive)) {
1373 if (drive->waiting_for_dma)
1374 (void) hwgroup->hwif->ide_dma_lostirq(drive);
1375 (void)ide_ack_intr(hwif);
1376 printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
1377 startstop = handler(drive);
1378 } else {
1379 if (drive->waiting_for_dma) {
1380 startstop = ide_dma_timeout_retry(drive, wait);
1381 } else
1382 startstop =
1383 ide_error(drive, "irq timeout", hwif->INB(IDE_STATUS_REG));
1384 }
1385 drive->service_time = jiffies - drive->service_start;
1386 spin_lock_irq(&ide_lock);
1387 enable_irq(hwif->irq);
1388 if (startstop == ide_stopped)
1389 hwgroup->busy = 0;
1390 }
1391 }
1392 ide_do_request(hwgroup, IDE_NO_IRQ);
1393 spin_unlock_irqrestore(&ide_lock, flags);
1394 }
1395
1396 /**
1397 * unexpected_intr - handle an unexpected IDE interrupt
1398 * @irq: interrupt line
1399 * @hwgroup: hwgroup being processed
1400 *
1401 * There's nothing really useful we can do with an unexpected interrupt,
1402 * other than reading the status register (to clear it), and logging it.
1403 * There should be no way that an irq can happen before we're ready for it,
1404 * so we needn't worry much about losing an "important" interrupt here.
1405 *
1406 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
1407 * the drive enters "idle", "standby", or "sleep" mode, so if the status
1408 * looks "good", we just ignore the interrupt completely.
1409 *
1410 * This routine assumes __cli() is in effect when called.
1411 *
1412 * If an unexpected interrupt happens on irq15 while we are handling irq14
1413 * and if the two interfaces are "serialized" (CMD640), then it looks like
1414 * we could screw up by interfering with a new request being set up for
1415 * irq15.
1416 *
1417 * In reality, this is a non-issue. The new command is not sent unless
1418 * the drive is ready to accept one, in which case we know the drive is
1419 * not trying to interrupt us. And ide_set_handler() is always invoked
1420 * before completing the issuance of any new drive command, so we will not
1421 * be accidentally invoked as a result of any valid command completion
1422 * interrupt.
1423 *
1424 * Note that we must walk the entire hwgroup here. We know which hwif
1425 * is doing the current command, but we don't know which hwif burped
1426 * mysteriously.
1427 */
1428
1429 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1430 {
1431 u8 stat;
1432 ide_hwif_t *hwif = hwgroup->hwif;
1433
1434 /*
1435 * handle the unexpected interrupt
1436 */
1437 do {
1438 if (hwif->irq == irq) {
1439 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1440 if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1441 /* Try to not flood the console with msgs */
1442 static unsigned long last_msgtime, count;
1443 ++count;
1444 if (time_after(jiffies, last_msgtime + HZ)) {
1445 last_msgtime = jiffies;
1446 printk(KERN_ERR "%s%s: unexpected interrupt, "
1447 "status=0x%02x, count=%ld\n",
1448 hwif->name,
1449 (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
1450 }
1451 }
1452 }
1453 } while ((hwif = hwif->next) != hwgroup->hwif);
1454 }
1455
1456 /**
1457 * ide_intr - default IDE interrupt handler
1458 * @irq: interrupt number
1459 * @dev_id: hwif group
1460 * @regs: unused weirdness from the kernel irq layer
1461 *
1462 * This is the default IRQ handler for the IDE layer. You should
1463 * not need to override it. If you do be aware it is subtle in
1464 * places
1465 *
1466 * hwgroup->hwif is the interface in the group currently performing
1467 * a command. hwgroup->drive is the drive and hwgroup->handler is
1468 * the IRQ handler to call. As we issue a command the handlers
1469 * step through multiple states, reassigning the handler to the
1470 * next step in the process. Unlike a smart SCSI controller IDE
1471 * expects the main processor to sequence the various transfer
1472 * stages. We also manage a poll timer to catch up with most
1473 * timeout situations. There are still a few where the handlers
1474 * don't ever decide to give up.
1475 *
1476 * The handler eventually returns ide_stopped to indicate the
1477 * request completed. At this point we issue the next request
1478 * on the hwgroup and the process begins again.
1479 */
1480
1481 irqreturn_t ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1482 {
1483 unsigned long flags;
1484 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1485 ide_hwif_t *hwif;
1486 ide_drive_t *drive;
1487 ide_handler_t *handler;
1488 ide_startstop_t startstop;
1489
1490 spin_lock_irqsave(&ide_lock, flags);
1491 hwif = hwgroup->hwif;
1492
1493 if (!ide_ack_intr(hwif)) {
1494 spin_unlock_irqrestore(&ide_lock, flags);
1495 return IRQ_NONE;
1496 }
1497
1498 if ((handler = hwgroup->handler) == NULL || hwgroup->polling) {
1499 /*
1500 * Not expecting an interrupt from this drive.
1501 * That means this could be:
1502 * (1) an interrupt from another PCI device
1503 * sharing the same PCI INT# as us.
1504 * or (2) a drive just entered sleep or standby mode,
1505 * and is interrupting to let us know.
1506 * or (3) a spurious interrupt of unknown origin.
1507 *
1508 * For PCI, we cannot tell the difference,
1509 * so in that case we just ignore it and hope it goes away.
1510 *
1511 * FIXME: unexpected_intr should be hwif-> then we can
1512 * remove all the ifdef PCI crap
1513 */
1514 #ifdef CONFIG_BLK_DEV_IDEPCI
1515 if (hwif->pci_dev && !hwif->pci_dev->vendor)
1516 #endif /* CONFIG_BLK_DEV_IDEPCI */
1517 {
1518 /*
1519 * Probably not a shared PCI interrupt,
1520 * so we can safely try to do something about it:
1521 */
1522 unexpected_intr(irq, hwgroup);
1523 #ifdef CONFIG_BLK_DEV_IDEPCI
1524 } else {
1525 /*
1526 * Whack the status register, just in case
1527 * we have a leftover pending IRQ.
1528 */
1529 (void) hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1530 #endif /* CONFIG_BLK_DEV_IDEPCI */
1531 }
1532 spin_unlock_irqrestore(&ide_lock, flags);
1533 return IRQ_NONE;
1534 }
1535 drive = hwgroup->drive;
1536 if (!drive) {
1537 /*
1538 * This should NEVER happen, and there isn't much
1539 * we could do about it here.
1540 *
1541 * [Note - this can occur if the drive is hot unplugged]
1542 */
1543 spin_unlock_irqrestore(&ide_lock, flags);
1544 return IRQ_HANDLED;
1545 }
1546 if (!drive_is_ready(drive)) {
1547 /*
1548 * This happens regularly when we share a PCI IRQ with
1549 * another device. Unfortunately, it can also happen
1550 * with some buggy drives that trigger the IRQ before
1551 * their status register is up to date. Hopefully we have
1552 * enough advance overhead that the latter isn't a problem.
1553 */
1554 spin_unlock_irqrestore(&ide_lock, flags);
1555 return IRQ_NONE;
1556 }
1557 if (!hwgroup->busy) {
1558 hwgroup->busy = 1; /* paranoia */
1559 printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
1560 }
1561 hwgroup->handler = NULL;
1562 del_timer(&hwgroup->timer);
1563 spin_unlock(&ide_lock);
1564
1565 if (drive->unmask)
1566 local_irq_enable();
1567 /* service this interrupt, may set handler for next interrupt */
1568 startstop = handler(drive);
1569 spin_lock_irq(&ide_lock);
1570
1571 /*
1572 * Note that handler() may have set things up for another
1573 * interrupt to occur soon, but it cannot happen until
1574 * we exit from this routine, because it will be the
1575 * same irq as is currently being serviced here, and Linux
1576 * won't allow another of the same (on any CPU) until we return.
1577 */
1578 drive->service_time = jiffies - drive->service_start;
1579 if (startstop == ide_stopped) {
1580 if (hwgroup->handler == NULL) { /* paranoia */
1581 hwgroup->busy = 0;
1582 ide_do_request(hwgroup, hwif->irq);
1583 } else {
1584 printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
1585 "on exit\n", drive->name);
1586 }
1587 }
1588 spin_unlock_irqrestore(&ide_lock, flags);
1589 return IRQ_HANDLED;
1590 }
1591
1592 /**
1593 * ide_init_drive_cmd - initialize a drive command request
1594 * @rq: request object
1595 *
1596 * Initialize a request before we fill it in and send it down to
1597 * ide_do_drive_cmd. Commands must be set up by this function. Right
1598 * now it doesn't do a lot, but if that changes abusers will have a
1599 * nasty suprise.
1600 */
1601
1602 void ide_init_drive_cmd (struct request *rq)
1603 {
1604 memset(rq, 0, sizeof(*rq));
1605 rq->flags = REQ_DRIVE_CMD;
1606 rq->ref_count = 1;
1607 }
1608
1609 EXPORT_SYMBOL(ide_init_drive_cmd);
1610
1611 /**
1612 * ide_do_drive_cmd - issue IDE special command
1613 * @drive: device to issue command
1614 * @rq: request to issue
1615 * @action: action for processing
1616 *
1617 * This function issues a special IDE device request
1618 * onto the request queue.
1619 *
1620 * If action is ide_wait, then the rq is queued at the end of the
1621 * request queue, and the function sleeps until it has been processed.
1622 * This is for use when invoked from an ioctl handler.
1623 *
1624 * If action is ide_preempt, then the rq is queued at the head of
1625 * the request queue, displacing the currently-being-processed
1626 * request and this function returns immediately without waiting
1627 * for the new rq to be completed. This is VERY DANGEROUS, and is
1628 * intended for careful use by the ATAPI tape/cdrom driver code.
1629 *
1630 * If action is ide_end, then the rq is queued at the end of the
1631 * request queue, and the function returns immediately without waiting
1632 * for the new rq to be completed. This is again intended for careful
1633 * use by the ATAPI tape/cdrom driver code.
1634 */
1635
1636 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1637 {
1638 unsigned long flags;
1639 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1640 DECLARE_COMPLETION(wait);
1641 int where = ELEVATOR_INSERT_BACK, err;
1642 int must_wait = (action == ide_wait || action == ide_head_wait);
1643
1644 rq->errors = 0;
1645 rq->rq_status = RQ_ACTIVE;
1646
1647 /*
1648 * we need to hold an extra reference to request for safe inspection
1649 * after completion
1650 */
1651 if (must_wait) {
1652 rq->ref_count++;
1653 rq->waiting = &wait;
1654 rq->end_io = blk_end_sync_rq;
1655 }
1656
1657 spin_lock_irqsave(&ide_lock, flags);
1658 if (action == ide_preempt)
1659 hwgroup->rq = NULL;
1660 if (action == ide_preempt || action == ide_head_wait) {
1661 where = ELEVATOR_INSERT_FRONT;
1662 rq->flags |= REQ_PREEMPT;
1663 }
1664 __elv_add_request(drive->queue, rq, where, 0);
1665 ide_do_request(hwgroup, IDE_NO_IRQ);
1666 spin_unlock_irqrestore(&ide_lock, flags);
1667
1668 err = 0;
1669 if (must_wait) {
1670 wait_for_completion(&wait);
1671 rq->waiting = NULL;
1672 if (rq->errors)
1673 err = -EIO;
1674
1675 blk_put_request(rq);
1676 }
1677
1678 return err;
1679 }
1680
1681 EXPORT_SYMBOL(ide_do_drive_cmd);
This page took 0.105993 seconds and 6 git commands to generate.