sync with FreeBSD rev 1.6:
[dragonfly.git] / sys / dev / netif / sbni / if_sbni_pci.c
1 /*
2  * Copyright (c) 1997-2001 Granch, Ltd. All rights reserved.
3  * Author: Denis I.Timofeev <timofeev@granch.ru>
4  *
5  * Redistributon and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/sbni/if_sbni_pci.c,v 1.6 2002/09/28 20:59:59 phk Exp $
28  * $DragonFly: src/sys/dev/netif/sbni/if_sbni_pci.c,v 1.10 2006/06/11 08:43:34 y0netan1 Exp $
29  */
30
31  
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/socket.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <machine/bus.h>
39 #include <machine/resource.h>
40 #include <sys/rman.h>
41 #include <sys/malloc.h>
42
43 #include <net/if.h>
44 #include <net/ethernet.h>
45 #include <net/if_arp.h>
46
47 #include <bus/pci/pcivar.h>
48 #include <bus/pci/pcireg.h>
49
50 #include "if_sbnireg.h"
51 #include "if_sbnivar.h"
52
53 static int      sbni_pci_probe(device_t);
54 static int      sbni_pci_attach(device_t);
55
56 static device_method_t sbni_pci_methods[] = {
57         /* Device interface */
58         DEVMETHOD(device_probe, sbni_pci_probe),
59         DEVMETHOD(device_attach, sbni_pci_attach),
60         { 0, 0 }
61 };
62
63 static driver_t sbni_pci_driver = {
64         "sbni",
65         sbni_pci_methods,
66         sizeof(struct sbni_softc)
67 };
68
69 static devclass_t sbni_pci_devclass;
70
71 DRIVER_MODULE(if_sbni, pci, sbni_pci_driver, sbni_pci_devclass, 0, 0);
72
73
74 static int
75 sbni_pci_probe(device_t dev)
76 {
77         struct sbni_softc  *sc;
78         u_int32_t  ports;
79    
80         ports = SBNI_PORTS;
81         if (pci_get_vendor(dev) != SBNI_PCI_VENDOR ||
82             pci_get_device(dev) != SBNI_PCI_DEVICE)
83                 return (ENXIO);
84
85         sc = device_get_softc(dev);
86
87         if (pci_get_subdevice(dev) == 2) {
88                 ports <<= 1;
89                 sc->slave_sc = malloc(sizeof(struct sbni_softc),
90                                       M_DEVBUF, M_INTWAIT | M_ZERO);
91                 device_set_desc(dev, "Granch SBNI12/PCI Dual adapter");
92         } else {
93                 device_set_desc(dev, "Granch SBNI12/PCI adapter");
94         }
95
96         sc->io_rid = PCIR_MAPS;
97         sc->io_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->io_rid,
98                                         0ul, ~0ul, ports, RF_ACTIVE);
99         if (!sc->io_res) {
100                 printf("sbni: cannot allocate io ports!\n");
101                 if (sc->slave_sc)
102                         free(sc->slave_sc, M_DEVBUF);
103                 return (ENOENT);
104         }
105
106         if (sc->slave_sc) {
107                 sc->slave_sc->io_res = sc->io_res;
108                 sc->slave_sc->io_off = 4;
109         }
110         if (sbni_probe(sc) != 0) {
111                 bus_release_resource(dev, SYS_RES_IOPORT,
112                                      sc->io_rid, sc->io_res);
113                 if (sc->slave_sc)
114                         free(sc->slave_sc, M_DEVBUF);
115                 return (ENXIO);
116         }
117
118         device_quiet(dev);
119         return (0);
120 }
121
122 static int
123 sbni_pci_attach(device_t dev)
124 {
125         struct sbni_softc *sc;
126         struct sbni_flags flags;
127         int error;
128
129         sc = device_get_softc(dev);
130
131         printf("sbni%d: <Granch SBNI12/PCI%sadapter> port 0x%lx",
132                next_sbni_unit, sc->slave_sc ? " Dual " : " ",
133                rman_get_start(sc->io_res));
134         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
135             RF_SHAREABLE);
136
137         *(u_int32_t*)&flags = 0;
138
139         sbni_attach(sc, next_sbni_unit++, flags);
140         if (sc->slave_sc)
141                 sbni_attach(sc->slave_sc, next_sbni_unit++, flags);
142
143         if (sc->irq_res) {
144                 error = bus_setup_intr(dev, sc->irq_res, INTR_NETSAFE,
145                                        sbni_intr, sc, 
146                                        &sc->irq_handle, 
147                                        sc->arpcom.ac_if.if_serializer);
148                 if (error) {
149                         printf("sbni%d: bus_setup_intr\n", next_sbni_unit);
150                 }
151         } else {
152                 printf("\nsbni%d: cannot claim irq!\n", next_sbni_unit);
153         }
154
155         return (0);
156
157 #if 0
158         bus_release_resource(dev, SYS_RES_IOPORT, sc->io_rid, sc->io_res);
159         if (sc->irq_res) {
160                 bus_release_resource(
161                     dev, SYS_RES_IRQ, sc->irq_rid, sc->irq_res);
162         }
163         if (sc->slave_sc)
164                 free(sc->slave_sc, M_DEVBUF);
165         return (error);
166 #endif
167 }