Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.1.2.4 2002/08/11 09:32:00 fjoe Exp $
28  * $DragonFly: src/sys/dev/netif/sbni/if_sbni_pci.c,v 1.2 2003/06/17 04:28:29 dillon 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/bus_private.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42 #include <sys/malloc.h>
43
44 #include <net/if.h>
45 #include <net/ethernet.h>
46 #include <net/if_arp.h>
47
48 #include <pci/pcivar.h>
49 #include <pci/pcireg.h>
50
51 #include <dev/sbni/if_sbnireg.h>
52 #include <dev/sbni/if_sbnivar.h>
53
54 static int      sbni_pci_probe(device_t);
55 static int      sbni_pci_attach(device_t);
56
57 static device_method_t sbni_pci_methods[] = {
58         /* Device interface */
59         DEVMETHOD(device_probe, sbni_pci_probe),
60         DEVMETHOD(device_attach, sbni_pci_attach),
61         { 0, 0 }
62 };
63
64 static driver_t sbni_pci_driver = {
65         "sbni",
66         sbni_pci_methods,
67         sizeof(struct sbni_softc)
68 };
69
70 static devclass_t sbni_pci_devclass;
71
72 DRIVER_MODULE(if_sbni, pci, sbni_pci_driver, sbni_pci_devclass, 0, 0);
73
74
75 static int
76 sbni_pci_probe(device_t dev)
77 {
78         struct sbni_softc  *sc;
79         u_int32_t  ports;
80    
81         ports = SBNI_PORTS;
82         if (pci_get_vendor(dev) != SBNI_PCI_VENDOR ||
83             pci_get_device(dev) != SBNI_PCI_DEVICE)
84                 return (ENXIO);
85
86         sc = device_get_softc(dev);
87         bzero(sc, sizeof(struct sbni_softc));
88         if (pci_get_subdevice(dev) == 2) {
89                 ports <<= 1;
90                 sc->slave_sc = malloc(sizeof(struct sbni_softc),
91                                       M_DEVBUF, M_NOWAIT | M_ZERO);
92                 if (!sc->slave_sc)
93                         return (ENOMEM);
94                 device_set_desc(dev, "Granch SBNI12/PCI Dual adapter");
95         } else
96                 device_set_desc(dev, "Granch SBNI12/PCI adapter");
97
98         sc->io_rid = PCIR_MAPS;
99         sc->io_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->io_rid,
100                                         0ul, ~0ul, ports, RF_ACTIVE);
101         if (!sc->io_res) {
102                 printf("sbni: cannot allocate io ports!\n");
103                 if (sc->slave_sc)
104                         free(sc->slave_sc, M_DEVBUF);
105                 return (ENOENT);
106         }
107
108         if (sc->slave_sc) {
109                 sc->slave_sc->io_res = sc->io_res;
110                 sc->slave_sc->io_off = 4;
111         }
112         if (sbni_probe(sc) != 0) {
113                 bus_release_resource(dev, SYS_RES_IOPORT,
114                                      sc->io_rid, sc->io_res);
115                 if (sc->slave_sc)
116                         free(sc->slave_sc, M_DEVBUF);
117                 return (ENXIO);
118         }
119
120         device_quiet(dev);
121         return (0);
122 }
123
124 static int
125 sbni_pci_attach(device_t dev)
126 {
127         struct sbni_softc *sc;
128         struct sbni_flags flags;
129         int error;
130
131         sc = device_get_softc(dev);
132
133         printf("sbni%d: <Granch SBNI12/PCI%sadapter> port 0x%lx",
134                next_sbni_unit, sc->slave_sc ? " Dual " : " ",
135                rman_get_start(sc->io_res));
136         sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
137                                          0ul, ~0ul, 1, RF_SHAREABLE);
138
139         if (sc->irq_res) {
140                 printf(" irq %ld\n", rman_get_start(sc->irq_res));
141                 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
142                                        sbni_intr, sc, &sc->irq_handle);
143                 if (error) {
144                         printf("sbni%d: bus_setup_intr\n", next_sbni_unit);
145                         goto attach_failed;
146                 }
147         } else {
148                 printf("\nsbni%d: cannot claim irq!\n", next_sbni_unit);
149                 error = ENOENT;
150                 goto attach_failed;
151         }
152
153         *(u_int32_t*)&flags = 0;
154
155         sbni_attach(sc, next_sbni_unit++, flags);
156         if (sc->slave_sc)
157                 sbni_attach(sc->slave_sc, next_sbni_unit++, flags);
158         return (0);
159
160 attach_failed:
161         bus_release_resource(dev, SYS_RES_IOPORT, sc->io_rid, sc->io_res);
162         if (sc->irq_res) {
163                 bus_release_resource(
164                     dev, SYS_RES_IRQ, sc->irq_rid, sc->irq_res);
165         }
166         if (sc->slave_sc)
167                 free(sc->slave_sc, M_DEVBUF);
168         return (error);
169 }