Merge nommu tree
[powerpc.git] / drivers / scsi / libata-bmdma.c
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
53 static 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
111 static 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  */
180 void 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
200 static 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  *
217  *      FIXME: missing write posting for 400nS delay enforcement
218  *
219  *      LOCKING:
220  *      spin_lock_irqsave(host_set lock)
221  */
222
223 static 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  */
243 void 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
263 static 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
297 static 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  */
336 void 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  */
355 static 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  */
371 static 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  */
390 u8 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  */
411 u8 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
421 #ifdef CONFIG_PCI
422 static struct ata_probe_ent *
423 ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
424 {
425         struct ata_probe_ent *probe_ent;
426
427         probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
428         if (!probe_ent) {
429                 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
430                        kobject_name(&(dev->kobj)));
431                 return NULL;
432         }
433
434         INIT_LIST_HEAD(&probe_ent->node);
435         probe_ent->dev = dev;
436
437         probe_ent->sht = port->sht;
438         probe_ent->host_flags = port->host_flags;
439         probe_ent->pio_mask = port->pio_mask;
440         probe_ent->mwdma_mask = port->mwdma_mask;
441         probe_ent->udma_mask = port->udma_mask;
442         probe_ent->port_ops = port->port_ops;
443
444         return probe_ent;
445 }
446
447
448 /**
449  *      ata_pci_init_native_mode - Initialize native-mode driver
450  *      @pdev:  pci device to be initialized
451  *      @port:  array[2] of pointers to port info structures.
452  *      @ports: bitmap of ports present
453  *
454  *      Utility function which allocates and initializes an
455  *      ata_probe_ent structure for a standard dual-port
456  *      PIO-based IDE controller.  The returned ata_probe_ent
457  *      structure can be passed to ata_device_add().  The returned
458  *      ata_probe_ent structure should then be freed with kfree().
459  *
460  *      The caller need only pass the address of the primary port, the
461  *      secondary will be deduced automatically. If the device has non
462  *      standard secondary port mappings this function can be called twice,
463  *      once for each interface.
464  */
465
466 struct ata_probe_ent *
467 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
468 {
469         struct ata_probe_ent *probe_ent =
470                 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
471         int p = 0;
472
473         if (!probe_ent)
474                 return NULL;
475
476         probe_ent->irq = pdev->irq;
477         probe_ent->irq_flags = SA_SHIRQ;
478         probe_ent->private_data = port[0]->private_data;
479
480         if (ports & ATA_PORT_PRIMARY) {
481                 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
482                 probe_ent->port[p].altstatus_addr =
483                 probe_ent->port[p].ctl_addr =
484                         pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
485                 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
486                 ata_std_ports(&probe_ent->port[p]);
487                 p++;
488         }
489
490         if (ports & ATA_PORT_SECONDARY) {
491                 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
492                 probe_ent->port[p].altstatus_addr =
493                 probe_ent->port[p].ctl_addr =
494                         pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
495                 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
496                 ata_std_ports(&probe_ent->port[p]);
497                 p++;
498         }
499
500         probe_ent->n_ports = p;
501         return probe_ent;
502 }
503
504
505 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev,
506                                 struct ata_port_info *port, int port_num)
507 {
508         struct ata_probe_ent *probe_ent;
509
510         probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
511         if (!probe_ent)
512                 return NULL;
513
514         probe_ent->legacy_mode = 1;
515         probe_ent->n_ports = 1;
516         probe_ent->hard_port_no = port_num;
517         probe_ent->private_data = port->private_data;
518
519         switch(port_num)
520         {
521                 case 0:
522                         probe_ent->irq = 14;
523                         probe_ent->port[0].cmd_addr = 0x1f0;
524                         probe_ent->port[0].altstatus_addr =
525                         probe_ent->port[0].ctl_addr = 0x3f6;
526                         break;
527                 case 1:
528                         probe_ent->irq = 15;
529                         probe_ent->port[0].cmd_addr = 0x170;
530                         probe_ent->port[0].altstatus_addr =
531                         probe_ent->port[0].ctl_addr = 0x376;
532                         break;
533         }
534
535         probe_ent->port[0].bmdma_addr =
536                 pci_resource_start(pdev, 4) + 8 * port_num;
537         ata_std_ports(&probe_ent->port[0]);
538
539         return probe_ent;
540 }
541
542
543 /**
544  *      ata_pci_init_one - Initialize/register PCI IDE host controller
545  *      @pdev: Controller to be initialized
546  *      @port_info: Information from low-level host driver
547  *      @n_ports: Number of ports attached to host controller
548  *
549  *      This is a helper function which can be called from a driver's
550  *      xxx_init_one() probe function if the hardware uses traditional
551  *      IDE taskfile registers.
552  *
553  *      This function calls pci_enable_device(), reserves its register
554  *      regions, sets the dma mask, enables bus master mode, and calls
555  *      ata_device_add()
556  *
557  *      LOCKING:
558  *      Inherited from PCI layer (may sleep).
559  *
560  *      RETURNS:
561  *      Zero on success, negative on errno-based value on error.
562  */
563
564 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
565                       unsigned int n_ports)
566 {
567         struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
568         struct ata_port_info *port[2];
569         u8 tmp8, mask;
570         unsigned int legacy_mode = 0;
571         int disable_dev_on_err = 1;
572         int rc;
573
574         DPRINTK("ENTER\n");
575
576         port[0] = port_info[0];
577         if (n_ports > 1)
578                 port[1] = port_info[1];
579         else
580                 port[1] = port[0];
581
582         if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
583             && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
584                 /* TODO: What if one channel is in native mode ... */
585                 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
586                 mask = (1 << 2) | (1 << 0);
587                 if ((tmp8 & mask) != mask)
588                         legacy_mode = (1 << 3);
589         }
590
591         /* FIXME... */
592         if ((!legacy_mode) && (n_ports > 2)) {
593                 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
594                 n_ports = 2;
595                 /* For now */
596         }
597
598         /* FIXME: Really for ATA it isn't safe because the device may be
599            multi-purpose and we want to leave it alone if it was already
600            enabled. Secondly for shared use as Arjan says we want refcounting
601
602            Checking dev->is_enabled is insufficient as this is not set at
603            boot for the primary video which is BIOS enabled
604          */
605
606         rc = pci_enable_device(pdev);
607         if (rc)
608                 return rc;
609
610         rc = pci_request_regions(pdev, DRV_NAME);
611         if (rc) {
612                 disable_dev_on_err = 0;
613                 goto err_out;
614         }
615
616         /* FIXME: Should use platform specific mappers for legacy port ranges */
617         if (legacy_mode) {
618                 if (!request_region(0x1f0, 8, "libata")) {
619                         struct resource *conflict, res;
620                         res.start = 0x1f0;
621                         res.end = 0x1f0 + 8 - 1;
622                         conflict = ____request_resource(&ioport_resource, &res);
623                         if (!strcmp(conflict->name, "libata"))
624                                 legacy_mode |= (1 << 0);
625                         else {
626                                 disable_dev_on_err = 0;
627                                 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
628                         }
629                 } else
630                         legacy_mode |= (1 << 0);
631
632                 if (!request_region(0x170, 8, "libata")) {
633                         struct resource *conflict, res;
634                         res.start = 0x170;
635                         res.end = 0x170 + 8 - 1;
636                         conflict = ____request_resource(&ioport_resource, &res);
637                         if (!strcmp(conflict->name, "libata"))
638                                 legacy_mode |= (1 << 1);
639                         else {
640                                 disable_dev_on_err = 0;
641                                 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
642                         }
643                 } else
644                         legacy_mode |= (1 << 1);
645         }
646
647         /* we have legacy mode, but all ports are unavailable */
648         if (legacy_mode == (1 << 3)) {
649                 rc = -EBUSY;
650                 goto err_out_regions;
651         }
652
653         /* FIXME: If we get no DMA mask we should fall back to PIO */
654         rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
655         if (rc)
656                 goto err_out_regions;
657         rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
658         if (rc)
659                 goto err_out_regions;
660
661         if (legacy_mode) {
662                 if (legacy_mode & (1 << 0))
663                         probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
664                 if (legacy_mode & (1 << 1))
665                         probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
666         } else {
667                 if (n_ports == 2)
668                         probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
669                 else
670                         probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
671         }
672         if (!probe_ent && !probe_ent2) {
673                 rc = -ENOMEM;
674                 goto err_out_regions;
675         }
676
677         pci_set_master(pdev);
678
679         /* FIXME: check ata_device_add return */
680         if (legacy_mode) {
681                 if (legacy_mode & (1 << 0))
682                         ata_device_add(probe_ent);
683                 if (legacy_mode & (1 << 1))
684                         ata_device_add(probe_ent2);
685         } else
686                 ata_device_add(probe_ent);
687
688         kfree(probe_ent);
689         kfree(probe_ent2);
690
691         return 0;
692
693 err_out_regions:
694         if (legacy_mode & (1 << 0))
695                 release_region(0x1f0, 8);
696         if (legacy_mode & (1 << 1))
697                 release_region(0x170, 8);
698         pci_release_regions(pdev);
699 err_out:
700         if (disable_dev_on_err)
701                 pci_disable_device(pdev);
702         return rc;
703 }
704
705 /**
706  *      ata_pci_clear_simplex   -       attempt to kick device out of simplex
707  *      @pdev: PCI device
708  *
709  *      Some PCI ATA devices report simplex mode but in fact can be told to
710  *      enter non simplex mode. This implements the neccessary logic to 
711  *      perform the task on such devices. Calling it on other devices will
712  *      have -undefined- behaviour.
713  */
714
715 int ata_pci_clear_simplex(struct pci_dev *pdev)
716 {
717         unsigned long bmdma = pci_resource_start(pdev, 4);
718         u8 simplex;
719
720         if (bmdma == 0)
721                 return -ENOENT;
722
723         simplex = inb(bmdma + 0x02);
724         outb(simplex & 0x60, bmdma + 0x02);
725         simplex = inb(bmdma + 0x02);
726         if (simplex & 0x80)
727                 return -EOPNOTSUPP;
728         return 0;
729 }
730
731 unsigned long ata_pci_default_filter(const struct ata_port *ap, struct ata_device *adev, unsigned long xfer_mask)
732 {
733         /* Filter out DMA modes if the device has been configured by
734            the BIOS as PIO only */
735            
736         if (ap->ioaddr.bmdma_addr == 0)
737                 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
738         return xfer_mask;
739 }
740
741 #endif /* CONFIG_PCI */
742