xref: /freebsd/sys/dev/sbni/if_sbni_pci.c (revision c8e7f78a3d28ff6e6223ed136ada8e1e2f34965e)
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 
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/socket.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <machine/bus.h>
35 #include <machine/resource.h>
36 #include <sys/rman.h>
37 #include <sys/malloc.h>
38 
39 #include <net/if.h>
40 #include <net/ethernet.h>
41 #include <net/if_arp.h>
42 
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcireg.h>
45 
46 #include <dev/sbni/if_sbnireg.h>
47 #include <dev/sbni/if_sbnivar.h>
48 
49 static int	sbni_pci_probe(device_t);
50 static int	sbni_pci_attach(device_t);
51 static int	sbni_pci_detach(device_t);
52 
53 static device_method_t sbni_pci_methods[] = {
54 	/* Device interface */
55 	DEVMETHOD(device_probe,	sbni_pci_probe),
56 	DEVMETHOD(device_attach, sbni_pci_attach),
57 	DEVMETHOD(device_detach, sbni_pci_detach),
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 DRIVER_MODULE(sbni, pci, sbni_pci_driver, 0, 0);
68 MODULE_DEPEND(sbni, pci, 1, 1, 1);
69 
70 static int
71 sbni_pci_probe(device_t dev)
72 {
73 	struct sbni_softc  *sc;
74 
75 	if (pci_get_vendor(dev) != SBNI_PCI_VENDOR ||
76 	    pci_get_device(dev) != SBNI_PCI_DEVICE)
77 		return (ENXIO);
78 
79 	sc = device_get_softc(dev);
80 	if (pci_get_subdevice(dev) == 2) {
81 		sc->slave_sc = malloc(sizeof(struct sbni_softc),
82 				      M_DEVBUF, M_NOWAIT | M_ZERO);
83 		if (!sc->slave_sc)
84 			return (ENOMEM);
85 		device_set_desc(dev, "Granch SBNI12/PCI Dual adapter");
86 	} else
87 		device_set_desc(dev, "Granch SBNI12/PCI adapter");
88 
89 	sc->io_rid = PCIR_BAR(0);
90  	sc->io_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
91 					    &sc->io_rid, RF_ACTIVE);
92 	if (!sc->io_res) {
93 		device_printf(dev, "cannot allocate io ports!\n");
94 		if (sc->slave_sc)
95 			free(sc->slave_sc, M_DEVBUF);
96 		return (ENOENT);
97 	}
98 
99 	if (sc->slave_sc) {
100 		sc->slave_sc->io_res = sc->io_res;
101 		sc->slave_sc->io_off = 4;
102 	}
103 	if (sbni_probe(sc) != 0) {
104 		sbni_release_resources(sc);
105 		if (sc->slave_sc)
106 			free(sc->slave_sc, M_DEVBUF);
107 		return (ENXIO);
108 	}
109 
110 	return (0);
111 }
112 
113 static int
114 sbni_pci_attach(device_t dev)
115 {
116 	struct sbni_softc *sc;
117 	struct sbni_flags flags;
118 	int error;
119 
120 	sc = device_get_softc(dev);
121 	sc->dev = dev;
122 
123 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
124 					     RF_SHAREABLE);
125 
126 	if (sc->irq_res == NULL) {
127 		device_printf(dev, "cannot claim irq!\n");
128 		error = ENOENT;
129 		goto attach_failed;
130 	}
131 
132 	memset(&flags, 0, sizeof(flags));
133 
134 	error = sbni_attach(sc, device_get_unit(dev) * 2, flags);
135 	if (error) {
136 		device_printf(dev, "cannot initialize driver\n");
137 		goto attach_failed;
138 	}
139 	if (sc->slave_sc) {
140 		error = sbni_attach(sc->slave_sc, device_get_unit(dev) * 2 + 1,
141 		    flags);
142 		if (error) {
143 			device_printf(dev, "cannot initialize slave\n");
144 			sbni_detach(sc);
145 			goto attach_failed;
146 		}
147 	}
148 
149 	if (sc->irq_res) {
150 		error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET |
151 		    INTR_MPSAFE, NULL, sbni_intr, sc, &sc->irq_handle);
152 		if (error) {
153 			device_printf(dev, "bus_setup_intr\n");
154 			sbni_detach(sc);
155 			if (sc->slave_sc)
156 				sbni_detach(sc);
157 			goto attach_failed;
158 		}
159 	}
160 	return (0);
161 
162 attach_failed:
163 	sbni_release_resources(sc);
164 	if (sc->slave_sc)
165 		free(sc->slave_sc, M_DEVBUF);
166 	return (error);
167 }
168 
169 static int
170 sbni_pci_detach(device_t dev)
171 {
172 	struct sbni_softc *sc;
173 
174 	sc = device_get_softc(dev);
175 	sbni_detach(sc);
176 	if (sc->slave_sc)
177 		sbni_detach(sc);
178 
179 	sbni_release_resources(sc);
180 	if (sc->slave_sc)
181 		free(sc->slave_sc, M_DEVBUF);
182 	return (0);
183 }
184