8bca822c8ffc1897e622825704ee1aaaad13701c
[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  */
29
30  
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/socket.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/interrupt.h>
37 #include <sys/module.h>
38 #include <sys/rman.h>
39 #include <sys/malloc.h>
40
41 #include <net/if.h>
42 #include <net/ethernet.h>
43 #include <net/if_arp.h>
44
45 #include <bus/pci/pcivar.h>
46 #include <bus/pci/pcireg.h>
47
48 #include "if_sbnireg.h"
49 #include "if_sbnivar.h"
50
51 static int      sbni_pci_probe(device_t);
52 static int      sbni_pci_attach(device_t);
53
54 static device_method_t sbni_pci_methods[] = {
55         /* Device interface */
56         DEVMETHOD(device_probe, sbni_pci_probe),
57         DEVMETHOD(device_attach, sbni_pci_attach),
58         { 0, 0 }
59 };
60
61 static driver_t sbni_pci_driver = {
62         "sbni",
63         sbni_pci_methods,
64         sizeof(struct sbni_softc)
65 };
66
67 static devclass_t sbni_pci_devclass;
68
69 DRIVER_MODULE(if_sbni, pci, sbni_pci_driver, sbni_pci_devclass, NULL, NULL);
70
71
72 static int
73 sbni_pci_probe(device_t dev)
74 {
75         struct sbni_softc  *sc;
76         u_int32_t  ports;
77    
78         ports = SBNI_PORTS;
79         if (pci_get_vendor(dev) != SBNI_PCI_VENDOR ||
80             pci_get_device(dev) != SBNI_PCI_DEVICE)
81                 return (ENXIO);
82
83         sc = device_get_softc(dev);
84
85         if (pci_get_subdevice(dev) == 2) {
86                 ports <<= 1;
87                 sc->slave_sc = kmalloc(sizeof(struct sbni_softc),
88                                       M_DEVBUF, M_INTWAIT | M_ZERO);
89                 device_set_desc(dev, "Granch SBNI12/PCI Dual adapter");
90         } else {
91                 device_set_desc(dev, "Granch SBNI12/PCI adapter");
92         }
93
94         sc->io_rid = PCIR_MAPS;
95         sc->io_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->io_rid,
96                                         0ul, ~0ul, ports, RF_ACTIVE);
97         if (!sc->io_res) {
98                 kprintf("sbni: cannot allocate io ports!\n");
99                 if (sc->slave_sc)
100                         kfree(sc->slave_sc, M_DEVBUF);
101                 return (ENOENT);
102         }
103
104         if (sc->slave_sc) {
105                 sc->slave_sc->io_res = sc->io_res;
106                 sc->slave_sc->io_off = 4;
107         }
108         if (sbni_probe(sc) != 0) {
109                 bus_release_resource(dev, SYS_RES_IOPORT,
110                                      sc->io_rid, sc->io_res);
111                 if (sc->slave_sc)
112                         kfree(sc->slave_sc, M_DEVBUF);
113                 return (ENXIO);
114         }
115
116         device_quiet(dev);
117         return (0);
118 }
119
120 static int
121 sbni_pci_attach(device_t dev)
122 {
123         struct sbni_softc *sc;
124         struct sbni_flags flags;
125         int error;
126
127         sc = device_get_softc(dev);
128
129         kprintf("sbni%d: <Granch SBNI12/PCI%sadapter> port 0x%lx",
130                next_sbni_unit, sc->slave_sc ? " Dual " : " ",
131                rman_get_start(sc->io_res));
132         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
133             RF_SHAREABLE);
134
135         *(u_int32_t*)&flags = 0;
136
137         sbni_attach(sc, next_sbni_unit++, flags);
138         if (sc->slave_sc)
139                 sbni_attach(sc->slave_sc, next_sbni_unit++, flags);
140
141         if (sc->irq_res) {
142                 struct ifnet *ifp = &sc->arpcom.ac_if;
143
144                 error = bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE,
145                                        sbni_intr, sc, &sc->irq_handle,
146                                        ifp->if_serializer);
147                 if (error) {
148                         kprintf("sbni%d: bus_setup_intr\n", next_sbni_unit);
149                 } else {
150                         ifp->if_cpuid = rman_get_cpuid(sc->irq_res);
151                         KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
152                 }
153         } else {
154                 kprintf("\nsbni%d: cannot claim irq!\n", next_sbni_unit);
155         }
156
157         return (0);
158
159 #if 0
160         bus_release_resource(dev, SYS_RES_IOPORT, sc->io_rid, sc->io_res);
161         if (sc->irq_res) {
162                 bus_release_resource(
163                     dev, SYS_RES_IRQ, sc->irq_rid, sc->irq_res);
164         }
165         if (sc->slave_sc)
166                 kfree(sc->slave_sc, M_DEVBUF);
167         return (error);
168 #endif
169 }