]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/libata-bmdma.c
[PATCH] libata: implement per-dev EH action mask eh_info->dev_action[]
[net-next-2.6.git] / drivers / scsi / libata-bmdma.c
CommitLineData
1fdffbce
JG
1/*
2 * libata-bmdma.c - helper library for PCI IDE BMDMA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2006 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
33 */
34
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <linux/pci.h>
38#include <linux/libata.h>
39
40#include "libata.h"
41
42/**
43 * ata_tf_load_pio - send taskfile registers to host controller
44 * @ap: Port to which output is sent
45 * @tf: ATA taskfile register set
46 *
47 * Outputs ATA taskfile to standard ATA host controller.
48 *
49 * LOCKING:
50 * Inherited from caller.
51 */
52
53static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
54{
55 struct ata_ioports *ioaddr = &ap->ioaddr;
56 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
57
58 if (tf->ctl != ap->last_ctl) {
59 outb(tf->ctl, ioaddr->ctl_addr);
60 ap->last_ctl = tf->ctl;
61 ata_wait_idle(ap);
62 }
63
64 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
65 outb(tf->hob_feature, ioaddr->feature_addr);
66 outb(tf->hob_nsect, ioaddr->nsect_addr);
67 outb(tf->hob_lbal, ioaddr->lbal_addr);
68 outb(tf->hob_lbam, ioaddr->lbam_addr);
69 outb(tf->hob_lbah, ioaddr->lbah_addr);
70 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
71 tf->hob_feature,
72 tf->hob_nsect,
73 tf->hob_lbal,
74 tf->hob_lbam,
75 tf->hob_lbah);
76 }
77
78 if (is_addr) {
79 outb(tf->feature, ioaddr->feature_addr);
80 outb(tf->nsect, ioaddr->nsect_addr);
81 outb(tf->lbal, ioaddr->lbal_addr);
82 outb(tf->lbam, ioaddr->lbam_addr);
83 outb(tf->lbah, ioaddr->lbah_addr);
84 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
85 tf->feature,
86 tf->nsect,
87 tf->lbal,
88 tf->lbam,
89 tf->lbah);
90 }
91
92 if (tf->flags & ATA_TFLAG_DEVICE) {
93 outb(tf->device, ioaddr->device_addr);
94 VPRINTK("device 0x%X\n", tf->device);
95 }
96
97 ata_wait_idle(ap);
98}
99
100/**
101 * ata_tf_load_mmio - send taskfile registers to host controller
102 * @ap: Port to which output is sent
103 * @tf: ATA taskfile register set
104 *
105 * Outputs ATA taskfile to standard ATA host controller using MMIO.
106 *
107 * LOCKING:
108 * Inherited from caller.
109 */
110
111static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
112{
113 struct ata_ioports *ioaddr = &ap->ioaddr;
114 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
115
116 if (tf->ctl != ap->last_ctl) {
117 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
118 ap->last_ctl = tf->ctl;
119 ata_wait_idle(ap);
120 }
121
122 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
123 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
124 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
125 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
126 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
127 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
128 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
129 tf->hob_feature,
130 tf->hob_nsect,
131 tf->hob_lbal,
132 tf->hob_lbam,
133 tf->hob_lbah);
134 }
135
136 if (is_addr) {
137 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
138 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
139 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
140 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
141 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
142 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
143 tf->feature,
144 tf->nsect,
145 tf->lbal,
146 tf->lbam,
147 tf->lbah);
148 }
149
150 if (tf->flags & ATA_TFLAG_DEVICE) {
151 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
152 VPRINTK("device 0x%X\n", tf->device);
153 }
154
155 ata_wait_idle(ap);
156}
157
158
159/**
160 * ata_tf_load - send taskfile registers to host controller
161 * @ap: Port to which output is sent
162 * @tf: ATA taskfile register set
163 *
164 * Outputs ATA taskfile to standard ATA host controller using MMIO
165 * or PIO as indicated by the ATA_FLAG_MMIO flag.
166 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
167 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
168 * hob_lbal, hob_lbam, and hob_lbah.
169 *
170 * This function waits for idle (!BUSY and !DRQ) after writing
171 * registers. If the control register has a new value, this
172 * function also waits for idle after writing control and before
173 * writing the remaining registers.
174 *
175 * May be used as the tf_load() entry in ata_port_operations.
176 *
177 * LOCKING:
178 * Inherited from caller.
179 */
180void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
181{
182 if (ap->flags & ATA_FLAG_MMIO)
183 ata_tf_load_mmio(ap, tf);
184 else
185 ata_tf_load_pio(ap, tf);
186}
187
188/**
189 * ata_exec_command_pio - issue ATA command to host controller
190 * @ap: port to which command is being issued
191 * @tf: ATA taskfile register set
192 *
193 * Issues PIO write to ATA command register, with proper
194 * synchronization with interrupt handler / other threads.
195 *
196 * LOCKING:
197 * spin_lock_irqsave(host_set lock)
198 */
199
200static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
201{
202 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
203
204 outb(tf->command, ap->ioaddr.command_addr);
205 ata_pause(ap);
206}
207
208
209/**
210 * ata_exec_command_mmio - issue ATA command to host controller
211 * @ap: port to which command is being issued
212 * @tf: ATA taskfile register set
213 *
214 * Issues MMIO write to ATA command register, with proper
215 * synchronization with interrupt handler / other threads.
216 *
7c74ffd0
AC
217 * FIXME: missing write posting for 400nS delay enforcement
218 *
1fdffbce
JG
219 * LOCKING:
220 * spin_lock_irqsave(host_set lock)
221 */
222
223static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
224{
225 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
226
227 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
228 ata_pause(ap);
229}
230
231
232/**
233 * ata_exec_command - issue ATA command to host controller
234 * @ap: port to which command is being issued
235 * @tf: ATA taskfile register set
236 *
237 * Issues PIO/MMIO write to ATA command register, with proper
238 * synchronization with interrupt handler / other threads.
239 *
240 * LOCKING:
241 * spin_lock_irqsave(host_set lock)
242 */
243void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
244{
245 if (ap->flags & ATA_FLAG_MMIO)
246 ata_exec_command_mmio(ap, tf);
247 else
248 ata_exec_command_pio(ap, tf);
249}
250
251/**
252 * ata_tf_read_pio - input device's ATA taskfile shadow registers
253 * @ap: Port from which input is read
254 * @tf: ATA taskfile register set for storing input
255 *
256 * Reads ATA taskfile registers for currently-selected device
257 * into @tf.
258 *
259 * LOCKING:
260 * Inherited from caller.
261 */
262
263static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
264{
265 struct ata_ioports *ioaddr = &ap->ioaddr;
266
267 tf->command = ata_check_status(ap);
268 tf->feature = inb(ioaddr->error_addr);
269 tf->nsect = inb(ioaddr->nsect_addr);
270 tf->lbal = inb(ioaddr->lbal_addr);
271 tf->lbam = inb(ioaddr->lbam_addr);
272 tf->lbah = inb(ioaddr->lbah_addr);
273 tf->device = inb(ioaddr->device_addr);
274
275 if (tf->flags & ATA_TFLAG_LBA48) {
276 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
277 tf->hob_feature = inb(ioaddr->error_addr);
278 tf->hob_nsect = inb(ioaddr->nsect_addr);
279 tf->hob_lbal = inb(ioaddr->lbal_addr);
280 tf->hob_lbam = inb(ioaddr->lbam_addr);
281 tf->hob_lbah = inb(ioaddr->lbah_addr);
282 }
283}
284
285/**
286 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
287 * @ap: Port from which input is read
288 * @tf: ATA taskfile register set for storing input
289 *
290 * Reads ATA taskfile registers for currently-selected device
291 * into @tf via MMIO.
292 *
293 * LOCKING:
294 * Inherited from caller.
295 */
296
297static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
298{
299 struct ata_ioports *ioaddr = &ap->ioaddr;
300
301 tf->command = ata_check_status(ap);
302 tf->feature = readb((void __iomem *)ioaddr->error_addr);
303 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
304 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
305 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
306 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
307 tf->device = readb((void __iomem *)ioaddr->device_addr);
308
309 if (tf->flags & ATA_TFLAG_LBA48) {
310 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
311 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
312 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
313 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
314 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
315 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
316 }
317}
318
319
320/**
321 * ata_tf_read - input device's ATA taskfile shadow registers
322 * @ap: Port from which input is read
323 * @tf: ATA taskfile register set for storing input
324 *
325 * Reads ATA taskfile registers for currently-selected device
326 * into @tf.
327 *
328 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
329 * is set, also reads the hob registers.
330 *
331 * May be used as the tf_read() entry in ata_port_operations.
332 *
333 * LOCKING:
334 * Inherited from caller.
335 */
336void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
337{
338 if (ap->flags & ATA_FLAG_MMIO)
339 ata_tf_read_mmio(ap, tf);
340 else
341 ata_tf_read_pio(ap, tf);
342}
343
344/**
345 * ata_check_status_pio - Read device status reg & clear interrupt
346 * @ap: port where the device is
347 *
348 * Reads ATA taskfile status register for currently-selected device
349 * and return its value. This also clears pending interrupts
350 * from this device
351 *
352 * LOCKING:
353 * Inherited from caller.
354 */
355static u8 ata_check_status_pio(struct ata_port *ap)
356{
357 return inb(ap->ioaddr.status_addr);
358}
359
360/**
361 * ata_check_status_mmio - Read device status reg & clear interrupt
362 * @ap: port where the device is
363 *
364 * Reads ATA taskfile status register for currently-selected device
365 * via MMIO and return its value. This also clears pending interrupts
366 * from this device
367 *
368 * LOCKING:
369 * Inherited from caller.
370 */
371static u8 ata_check_status_mmio(struct ata_port *ap)
372{
373 return readb((void __iomem *) ap->ioaddr.status_addr);
374}
375
376
377/**
378 * ata_check_status - Read device status reg & clear interrupt
379 * @ap: port where the device is
380 *
381 * Reads ATA taskfile status register for currently-selected device
382 * and return its value. This also clears pending interrupts
383 * from this device
384 *
385 * May be used as the check_status() entry in ata_port_operations.
386 *
387 * LOCKING:
388 * Inherited from caller.
389 */
390u8 ata_check_status(struct ata_port *ap)
391{
392 if (ap->flags & ATA_FLAG_MMIO)
393 return ata_check_status_mmio(ap);
394 return ata_check_status_pio(ap);
395}
396
397
398/**
399 * ata_altstatus - Read device alternate status reg
400 * @ap: port where the device is
401 *
402 * Reads ATA taskfile alternate status register for
403 * currently-selected device and return its value.
404 *
405 * Note: may NOT be used as the check_altstatus() entry in
406 * ata_port_operations.
407 *
408 * LOCKING:
409 * Inherited from caller.
410 */
411u8 ata_altstatus(struct ata_port *ap)
412{
413 if (ap->ops->check_altstatus)
414 return ap->ops->check_altstatus(ap);
415
416 if (ap->flags & ATA_FLAG_MMIO)
417 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
418 return inb(ap->ioaddr.altstatus_addr);
419}
420
2cc432ee
JG
421/**
422 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
423 * @qc: Info associated with this ATA transaction.
424 *
425 * LOCKING:
426 * spin_lock_irqsave(host_set lock)
427 */
428
429static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
430{
431 struct ata_port *ap = qc->ap;
432 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
433 u8 dmactl;
434 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
435
436 /* load PRD table addr. */
437 mb(); /* make sure PRD table writes are visible to controller */
438 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
439
440 /* specify data direction, triple-check start bit is clear */
441 dmactl = readb(mmio + ATA_DMA_CMD);
442 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
443 if (!rw)
444 dmactl |= ATA_DMA_WR;
445 writeb(dmactl, mmio + ATA_DMA_CMD);
446
447 /* issue r/w command */
448 ap->ops->exec_command(ap, &qc->tf);
449}
450
451/**
452 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
453 * @qc: Info associated with this ATA transaction.
454 *
455 * LOCKING:
456 * spin_lock_irqsave(host_set lock)
457 */
458
459static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
460{
461 struct ata_port *ap = qc->ap;
462 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
463 u8 dmactl;
464
465 /* start host DMA transaction */
466 dmactl = readb(mmio + ATA_DMA_CMD);
467 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
468
469 /* Strictly, one may wish to issue a readb() here, to
470 * flush the mmio write. However, control also passes
471 * to the hardware at this point, and it will interrupt
472 * us when we are to resume control. So, in effect,
473 * we don't care when the mmio write flushes.
474 * Further, a read of the DMA status register _immediately_
475 * following the write may not be what certain flaky hardware
476 * is expected, so I think it is best to not add a readb()
477 * without first all the MMIO ATA cards/mobos.
478 * Or maybe I'm just being paranoid.
479 */
480}
481
482/**
483 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
484 * @qc: Info associated with this ATA transaction.
485 *
486 * LOCKING:
487 * spin_lock_irqsave(host_set lock)
488 */
489
490static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
491{
492 struct ata_port *ap = qc->ap;
493 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
494 u8 dmactl;
495
496 /* load PRD table addr. */
497 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
498
499 /* specify data direction, triple-check start bit is clear */
500 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
501 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
502 if (!rw)
503 dmactl |= ATA_DMA_WR;
504 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
505
506 /* issue r/w command */
507 ap->ops->exec_command(ap, &qc->tf);
508}
509
510/**
511 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
512 * @qc: Info associated with this ATA transaction.
513 *
514 * LOCKING:
515 * spin_lock_irqsave(host_set lock)
516 */
517
518static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
519{
520 struct ata_port *ap = qc->ap;
521 u8 dmactl;
522
523 /* start host DMA transaction */
524 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
525 outb(dmactl | ATA_DMA_START,
526 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
527}
528
529
530/**
531 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
532 * @qc: Info associated with this ATA transaction.
533 *
534 * Writes the ATA_DMA_START flag to the DMA command register.
535 *
536 * May be used as the bmdma_start() entry in ata_port_operations.
537 *
538 * LOCKING:
539 * spin_lock_irqsave(host_set lock)
540 */
541void ata_bmdma_start(struct ata_queued_cmd *qc)
542{
543 if (qc->ap->flags & ATA_FLAG_MMIO)
544 ata_bmdma_start_mmio(qc);
545 else
546 ata_bmdma_start_pio(qc);
547}
548
549
550/**
551 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
552 * @qc: Info associated with this ATA transaction.
553 *
554 * Writes address of PRD table to device's PRD Table Address
555 * register, sets the DMA control register, and calls
556 * ops->exec_command() to start the transfer.
557 *
558 * May be used as the bmdma_setup() entry in ata_port_operations.
559 *
560 * LOCKING:
561 * spin_lock_irqsave(host_set lock)
562 */
563void ata_bmdma_setup(struct ata_queued_cmd *qc)
564{
565 if (qc->ap->flags & ATA_FLAG_MMIO)
566 ata_bmdma_setup_mmio(qc);
567 else
568 ata_bmdma_setup_pio(qc);
569}
570
571
572/**
573 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
574 * @ap: Port associated with this ATA transaction.
575 *
576 * Clear interrupt and error flags in DMA status register.
577 *
578 * May be used as the irq_clear() entry in ata_port_operations.
579 *
580 * LOCKING:
581 * spin_lock_irqsave(host_set lock)
582 */
583
584void ata_bmdma_irq_clear(struct ata_port *ap)
585{
586 if (!ap->ioaddr.bmdma_addr)
587 return;
588
589 if (ap->flags & ATA_FLAG_MMIO) {
590 void __iomem *mmio =
591 ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
592 writeb(readb(mmio), mmio);
593 } else {
594 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
595 outb(inb(addr), addr);
596 }
597}
598
599
600/**
601 * ata_bmdma_status - Read PCI IDE BMDMA status
602 * @ap: Port associated with this ATA transaction.
603 *
604 * Read and return BMDMA status register.
605 *
606 * May be used as the bmdma_status() entry in ata_port_operations.
607 *
608 * LOCKING:
609 * spin_lock_irqsave(host_set lock)
610 */
611
612u8 ata_bmdma_status(struct ata_port *ap)
613{
614 u8 host_stat;
615 if (ap->flags & ATA_FLAG_MMIO) {
616 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
617 host_stat = readb(mmio + ATA_DMA_STATUS);
618 } else
619 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
620 return host_stat;
621}
622
623
624/**
625 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
626 * @qc: Command we are ending DMA for
627 *
628 * Clears the ATA_DMA_START flag in the dma control register
629 *
630 * May be used as the bmdma_stop() entry in ata_port_operations.
631 *
632 * LOCKING:
633 * spin_lock_irqsave(host_set lock)
634 */
635
636void ata_bmdma_stop(struct ata_queued_cmd *qc)
637{
638 struct ata_port *ap = qc->ap;
639 if (ap->flags & ATA_FLAG_MMIO) {
640 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
641
642 /* clear start/stop bit */
643 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
644 mmio + ATA_DMA_CMD);
645 } else {
646 /* clear start/stop bit */
647 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
648 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
649 }
650
651 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
652 ata_altstatus(ap); /* dummy read */
653}
654
6d97dbd7
TH
655/**
656 * ata_bmdma_freeze - Freeze BMDMA controller port
657 * @ap: port to freeze
658 *
659 * Freeze BMDMA controller port.
660 *
661 * LOCKING:
662 * Inherited from caller.
663 */
664void ata_bmdma_freeze(struct ata_port *ap)
665{
666 struct ata_ioports *ioaddr = &ap->ioaddr;
667
668 ap->ctl |= ATA_NIEN;
669 ap->last_ctl = ap->ctl;
670
671 if (ap->flags & ATA_FLAG_MMIO)
672 writeb(ap->ctl, (void __iomem *)ioaddr->ctl_addr);
673 else
674 outb(ap->ctl, ioaddr->ctl_addr);
675}
676
677/**
678 * ata_bmdma_thaw - Thaw BMDMA controller port
679 * @ap: port to thaw
680 *
681 * Thaw BMDMA controller port.
682 *
683 * LOCKING:
684 * Inherited from caller.
685 */
686void ata_bmdma_thaw(struct ata_port *ap)
687{
688 /* clear & re-enable interrupts */
689 ata_chk_status(ap);
690 ap->ops->irq_clear(ap);
691 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
692 ata_irq_on(ap);
693}
694
695/**
696 * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
697 * @ap: port to handle error for
f5914a46 698 * @prereset: prereset method (can be NULL)
6d97dbd7
TH
699 * @softreset: softreset method (can be NULL)
700 * @hardreset: hardreset method (can be NULL)
701 * @postreset: postreset method (can be NULL)
702 *
703 * Handle error for ATA BMDMA controller. It can handle both
704 * PATA and SATA controllers. Many controllers should be able to
705 * use this EH as-is or with some added handling before and
706 * after.
707 *
708 * This function is intended to be used for constructing
709 * ->error_handler callback by low level drivers.
710 *
711 * LOCKING:
712 * Kernel thread context (may sleep)
713 */
f5914a46
TH
714void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
715 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
716 ata_postreset_fn_t postreset)
6d97dbd7
TH
717{
718 struct ata_host_set *host_set = ap->host_set;
719 struct ata_eh_context *ehc = &ap->eh_context;
720 struct ata_queued_cmd *qc;
721 unsigned long flags;
722 int thaw = 0;
723
724 qc = __ata_qc_from_tag(ap, ap->active_tag);
725 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
726 qc = NULL;
727
728 /* reset PIO HSM and stop DMA engine */
729 spin_lock_irqsave(&host_set->lock, flags);
730
6d97dbd7
TH
731 ap->hsm_task_state = HSM_ST_IDLE;
732
733 if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
734 qc->tf.protocol == ATA_PROT_ATAPI_DMA)) {
735 u8 host_stat;
736
737 host_stat = ata_bmdma_status(ap);
738
739 ata_ehi_push_desc(&ehc->i, "BMDMA stat 0x%x", host_stat);
740
741 /* BMDMA controllers indicate host bus error by
742 * setting DMA_ERR bit and timing out. As it wasn't
743 * really a timeout event, adjust error mask and
744 * cancel frozen state.
745 */
746 if (qc->err_mask == AC_ERR_TIMEOUT && host_stat & ATA_DMA_ERR) {
747 qc->err_mask = AC_ERR_HOST_BUS;
748 thaw = 1;
749 }
750
751 ap->ops->bmdma_stop(qc);
752 }
753
754 ata_altstatus(ap);
755 ata_chk_status(ap);
756 ap->ops->irq_clear(ap);
757
758 spin_unlock_irqrestore(&host_set->lock, flags);
759
760 if (thaw)
761 ata_eh_thaw_port(ap);
762
763 /* PIO and DMA engines have been stopped, perform recovery */
f5914a46 764 ata_do_eh(ap, prereset, softreset, hardreset, postreset);
6d97dbd7
TH
765}
766
767/**
768 * ata_bmdma_error_handler - Stock error handler for BMDMA controller
769 * @ap: port to handle error for
770 *
771 * Stock error handler for BMDMA controller.
772 *
773 * LOCKING:
774 * Kernel thread context (may sleep)
775 */
776void ata_bmdma_error_handler(struct ata_port *ap)
777{
778 ata_reset_fn_t hardreset;
779
780 hardreset = NULL;
781 if (sata_scr_valid(ap))
782 hardreset = sata_std_hardreset;
783
f5914a46
TH
784 ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
785 ata_std_postreset);
6d97dbd7
TH
786}
787
788/**
789 * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
790 * BMDMA controller
791 * @qc: internal command to clean up
792 *
793 * LOCKING:
794 * Kernel thread context (may sleep)
795 */
796void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
797{
798 ata_bmdma_stop(qc);
799}
800
1fdffbce
JG
801#ifdef CONFIG_PCI
802static struct ata_probe_ent *
803ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
804{
805 struct ata_probe_ent *probe_ent;
806
807 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
808 if (!probe_ent) {
809 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
810 kobject_name(&(dev->kobj)));
811 return NULL;
812 }
813
814 INIT_LIST_HEAD(&probe_ent->node);
815 probe_ent->dev = dev;
816
817 probe_ent->sht = port->sht;
818 probe_ent->host_flags = port->host_flags;
819 probe_ent->pio_mask = port->pio_mask;
820 probe_ent->mwdma_mask = port->mwdma_mask;
821 probe_ent->udma_mask = port->udma_mask;
822 probe_ent->port_ops = port->port_ops;
823
824 return probe_ent;
825}
826
827
828/**
829 * ata_pci_init_native_mode - Initialize native-mode driver
830 * @pdev: pci device to be initialized
831 * @port: array[2] of pointers to port info structures.
832 * @ports: bitmap of ports present
833 *
834 * Utility function which allocates and initializes an
835 * ata_probe_ent structure for a standard dual-port
836 * PIO-based IDE controller. The returned ata_probe_ent
837 * structure can be passed to ata_device_add(). The returned
838 * ata_probe_ent structure should then be freed with kfree().
839 *
840 * The caller need only pass the address of the primary port, the
841 * secondary will be deduced automatically. If the device has non
842 * standard secondary port mappings this function can be called twice,
843 * once for each interface.
844 */
845
846struct ata_probe_ent *
847ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
848{
849 struct ata_probe_ent *probe_ent =
850 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
851 int p = 0;
4e5ec5db 852 unsigned long bmdma;
1fdffbce
JG
853
854 if (!probe_ent)
855 return NULL;
856
857 probe_ent->irq = pdev->irq;
858 probe_ent->irq_flags = SA_SHIRQ;
859 probe_ent->private_data = port[0]->private_data;
860
861 if (ports & ATA_PORT_PRIMARY) {
862 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
863 probe_ent->port[p].altstatus_addr =
864 probe_ent->port[p].ctl_addr =
865 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4e5ec5db
AC
866 bmdma = pci_resource_start(pdev, 4);
867 if (bmdma) {
868 if (inb(bmdma + 2) & 0x80)
869 probe_ent->host_set_flags |= ATA_HOST_SIMPLEX;
870 probe_ent->port[p].bmdma_addr = bmdma;
871 }
1fdffbce
JG
872 ata_std_ports(&probe_ent->port[p]);
873 p++;
874 }
875
876 if (ports & ATA_PORT_SECONDARY) {
877 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
878 probe_ent->port[p].altstatus_addr =
879 probe_ent->port[p].ctl_addr =
880 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4e5ec5db
AC
881 bmdma = pci_resource_start(pdev, 4);
882 if (bmdma) {
883 bmdma += 8;
884 if(inb(bmdma + 2) & 0x80)
885 probe_ent->host_set_flags |= ATA_HOST_SIMPLEX;
886 probe_ent->port[p].bmdma_addr = bmdma;
887 }
1fdffbce
JG
888 ata_std_ports(&probe_ent->port[p]);
889 p++;
890 }
891
892 probe_ent->n_ports = p;
893 return probe_ent;
894}
895
896
897static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev,
898 struct ata_port_info *port, int port_num)
899{
900 struct ata_probe_ent *probe_ent;
4e5ec5db 901 unsigned long bmdma;
1fdffbce
JG
902
903 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
904 if (!probe_ent)
905 return NULL;
906
907 probe_ent->legacy_mode = 1;
908 probe_ent->n_ports = 1;
909 probe_ent->hard_port_no = port_num;
910 probe_ent->private_data = port->private_data;
911
912 switch(port_num)
913 {
914 case 0:
915 probe_ent->irq = 14;
916 probe_ent->port[0].cmd_addr = 0x1f0;
917 probe_ent->port[0].altstatus_addr =
918 probe_ent->port[0].ctl_addr = 0x3f6;
919 break;
920 case 1:
921 probe_ent->irq = 15;
922 probe_ent->port[0].cmd_addr = 0x170;
923 probe_ent->port[0].altstatus_addr =
924 probe_ent->port[0].ctl_addr = 0x376;
925 break;
926 }
927
4e5ec5db
AC
928 bmdma = pci_resource_start(pdev, 4);
929 if (bmdma != 0) {
930 bmdma += 8 * port_num;
931 probe_ent->port[0].bmdma_addr = bmdma;
932 if (inb(bmdma + 2) & 0x80)
933 probe_ent->host_set_flags |= ATA_HOST_SIMPLEX;
934 }
1fdffbce
JG
935 ata_std_ports(&probe_ent->port[0]);
936
937 return probe_ent;
938}
939
940
941/**
942 * ata_pci_init_one - Initialize/register PCI IDE host controller
943 * @pdev: Controller to be initialized
944 * @port_info: Information from low-level host driver
945 * @n_ports: Number of ports attached to host controller
946 *
947 * This is a helper function which can be called from a driver's
948 * xxx_init_one() probe function if the hardware uses traditional
949 * IDE taskfile registers.
950 *
951 * This function calls pci_enable_device(), reserves its register
952 * regions, sets the dma mask, enables bus master mode, and calls
953 * ata_device_add()
954 *
955 * LOCKING:
956 * Inherited from PCI layer (may sleep).
957 *
958 * RETURNS:
959 * Zero on success, negative on errno-based value on error.
960 */
961
962int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
963 unsigned int n_ports)
964{
965 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
966 struct ata_port_info *port[2];
967 u8 tmp8, mask;
968 unsigned int legacy_mode = 0;
969 int disable_dev_on_err = 1;
970 int rc;
971
972 DPRINTK("ENTER\n");
973
974 port[0] = port_info[0];
975 if (n_ports > 1)
976 port[1] = port_info[1];
977 else
978 port[1] = port[0];
979
980 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
981 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
982 /* TODO: What if one channel is in native mode ... */
983 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
984 mask = (1 << 2) | (1 << 0);
985 if ((tmp8 & mask) != mask)
986 legacy_mode = (1 << 3);
987 }
988
989 /* FIXME... */
990 if ((!legacy_mode) && (n_ports > 2)) {
991 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
992 n_ports = 2;
993 /* For now */
994 }
995
996 /* FIXME: Really for ATA it isn't safe because the device may be
997 multi-purpose and we want to leave it alone if it was already
998 enabled. Secondly for shared use as Arjan says we want refcounting
999
1000 Checking dev->is_enabled is insufficient as this is not set at
1001 boot for the primary video which is BIOS enabled
1002 */
1003
1004 rc = pci_enable_device(pdev);
1005 if (rc)
1006 return rc;
1007
1008 rc = pci_request_regions(pdev, DRV_NAME);
1009 if (rc) {
1010 disable_dev_on_err = 0;
1011 goto err_out;
1012 }
1013
1014 /* FIXME: Should use platform specific mappers for legacy port ranges */
1015 if (legacy_mode) {
1016 if (!request_region(0x1f0, 8, "libata")) {
1017 struct resource *conflict, res;
1018 res.start = 0x1f0;
1019 res.end = 0x1f0 + 8 - 1;
1020 conflict = ____request_resource(&ioport_resource, &res);
1021 if (!strcmp(conflict->name, "libata"))
1022 legacy_mode |= (1 << 0);
1023 else {
1024 disable_dev_on_err = 0;
1025 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
1026 }
1027 } else
1028 legacy_mode |= (1 << 0);
1029
1030 if (!request_region(0x170, 8, "libata")) {
1031 struct resource *conflict, res;
1032 res.start = 0x170;
1033 res.end = 0x170 + 8 - 1;
1034 conflict = ____request_resource(&ioport_resource, &res);
1035 if (!strcmp(conflict->name, "libata"))
1036 legacy_mode |= (1 << 1);
1037 else {
1038 disable_dev_on_err = 0;
1039 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
1040 }
1041 } else
1042 legacy_mode |= (1 << 1);
1043 }
1044
1045 /* we have legacy mode, but all ports are unavailable */
1046 if (legacy_mode == (1 << 3)) {
1047 rc = -EBUSY;
1048 goto err_out_regions;
1049 }
1050
41bbc8bf 1051 /* FIXME: If we get no DMA mask we should fall back to PIO */
1fdffbce
JG
1052 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
1053 if (rc)
1054 goto err_out_regions;
1055 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
1056 if (rc)
1057 goto err_out_regions;
1058
1059 if (legacy_mode) {
1060 if (legacy_mode & (1 << 0))
1061 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
1062 if (legacy_mode & (1 << 1))
1063 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
1064 } else {
1065 if (n_ports == 2)
1066 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
1067 else
1068 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
1069 }
1070 if (!probe_ent && !probe_ent2) {
1071 rc = -ENOMEM;
1072 goto err_out_regions;
1073 }
1074
1075 pci_set_master(pdev);
1076
1077 /* FIXME: check ata_device_add return */
1078 if (legacy_mode) {
f0eb62b8
TH
1079 struct device *dev = &pdev->dev;
1080 struct ata_host_set *host_set = NULL;
1081
1082 if (legacy_mode & (1 << 0)) {
1fdffbce 1083 ata_device_add(probe_ent);
f0eb62b8
TH
1084 host_set = dev_get_drvdata(dev);
1085 }
1086
1087 if (legacy_mode & (1 << 1)) {
1fdffbce 1088 ata_device_add(probe_ent2);
f0eb62b8
TH
1089 if (host_set) {
1090 host_set->next = dev_get_drvdata(dev);
1091 dev_set_drvdata(dev, host_set);
1092 }
1093 }
1fdffbce
JG
1094 } else
1095 ata_device_add(probe_ent);
1096
1097 kfree(probe_ent);
1098 kfree(probe_ent2);
1099
1100 return 0;
1101
1102err_out_regions:
1103 if (legacy_mode & (1 << 0))
1104 release_region(0x1f0, 8);
1105 if (legacy_mode & (1 << 1))
1106 release_region(0x170, 8);
1107 pci_release_regions(pdev);
1108err_out:
1109 if (disable_dev_on_err)
1110 pci_disable_device(pdev);
1111 return rc;
1112}
1113
d33d44fa
AC
1114/**
1115 * ata_pci_clear_simplex - attempt to kick device out of simplex
1116 * @pdev: PCI device
1117 *
1118 * Some PCI ATA devices report simplex mode but in fact can be told to
2e9edbf8 1119 * enter non simplex mode. This implements the neccessary logic to
d33d44fa
AC
1120 * perform the task on such devices. Calling it on other devices will
1121 * have -undefined- behaviour.
1122 */
1123
1124int ata_pci_clear_simplex(struct pci_dev *pdev)
1125{
1126 unsigned long bmdma = pci_resource_start(pdev, 4);
1127 u8 simplex;
1128
1129 if (bmdma == 0)
1130 return -ENOENT;
1131
1132 simplex = inb(bmdma + 0x02);
1133 outb(simplex & 0x60, bmdma + 0x02);
1134 simplex = inb(bmdma + 0x02);
1135 if (simplex & 0x80)
1136 return -EOPNOTSUPP;
1137 return 0;
1138}
1139
1140unsigned long ata_pci_default_filter(const struct ata_port *ap, struct ata_device *adev, unsigned long xfer_mask)
1141{
1142 /* Filter out DMA modes if the device has been configured by
1143 the BIOS as PIO only */
2e9edbf8 1144
d33d44fa
AC
1145 if (ap->ioaddr.bmdma_addr == 0)
1146 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
1147 return xfer_mask;
1148}
1149
1fdffbce
JG
1150#endif /* CONFIG_PCI */
1151