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