]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/snc/if_snc.c
Fix reference count overflow in mqueuefs.
[FreeBSD/FreeBSD.git] / sys / dev / snc / if_snc.c
1 /*-
2  * Copyright (c) 1995, David Greenman
3  * All rights reserved.
4  *
5  * Redistribution 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 NEGLIGENCE 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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 /*
33  *      National Semiconductor  DP8393X SONIC Driver
34  *
35  *      This is the bus independent attachment on FreeBSD 4.x
36  *              written by Motomichi Matsuzaki <mzaki@e-mail.ne.jp>
37  */
38
39 #include <sys/param.h>
40 #include <sys/socket.h>
41
42 #include <sys/bus.h>
43 #include <machine/bus.h>
44 #include <sys/rman.h>
45 #include <machine/resource.h>
46
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_arp.h>
50 #include <net/if_media.h>
51 #include <net/if_var.h>
52
53 #include <dev/snc/dp83932reg.h>
54 #include <dev/snc/dp83932var.h>
55 #include <dev/snc/dp83932subr.h>
56 #include <dev/snc/if_sncreg.h>
57 #include <dev/snc/if_sncvar.h>
58
59 /* devclass for "snc" */
60 devclass_t snc_devclass;
61
62 /****************************************************************
63   Resource management functions
64  ****************************************************************/
65
66 /*
67  * Allocate a port resource with the given resource id.
68  */
69 int
70 snc_alloc_port(device_t dev, int rid)
71 {
72         struct snc_softc *sc = device_get_softc(dev);
73         struct resource *res;
74
75         res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
76                                           SNEC_NREGS, RF_ACTIVE);
77         if (res) {
78                 sc->ioport = res;
79                 sc->ioport_rid = rid;
80                 sc->sc_iot = rman_get_bustag(res);
81                 sc->sc_ioh = rman_get_bushandle(res);
82                 return (0);
83         } else {
84                 device_printf(dev, "can't assign port\n");
85                 return (ENOENT);
86         }
87 }
88
89 /*
90  * Allocate a memory resource with the given resource id.
91  */
92 int
93 snc_alloc_memory(device_t dev, int rid)
94 {
95         struct snc_softc *sc = device_get_softc(dev);
96         struct resource *res;
97
98         res = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY, &rid,
99                                           SNEC_NMEMS, RF_ACTIVE);
100         if (res) {
101                 sc->iomem = res;
102                 sc->iomem_rid = rid;
103                 sc->sc_memt = rman_get_bustag(res);
104                 sc->sc_memh = rman_get_bushandle(res);
105                 return (0);
106         } else {
107                 device_printf(dev, "can't assign memory\n");
108                 return (ENOENT);
109         }
110 }
111
112 /*
113  * Allocate an irq resource with the given resource id.
114  */
115 int
116 snc_alloc_irq(device_t dev, int rid, int flags)
117 {
118         struct snc_softc *sc = device_get_softc(dev);
119         struct resource *res;
120
121         res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE | flags);
122         if (res) {
123                 sc->irq = res;
124                 sc->irq_rid = rid;
125                 return (0);
126         } else {
127                 device_printf(dev, "can't assign irq\n");
128                 return (ENOENT);
129         }
130 }
131
132 /*
133  * Release all resources
134  */
135 void
136 snc_release_resources(device_t dev)
137 {
138         struct snc_softc *sc = device_get_softc(dev);
139
140         if (sc->ioport) {
141                 bus_release_resource(dev, SYS_RES_IOPORT,
142                                      sc->ioport_rid, sc->ioport);
143                 sc->ioport = 0;
144         }
145         if (sc->iomem) {
146                 bus_release_resource(dev, SYS_RES_MEMORY,
147                                      sc->iomem_rid, sc->iomem);
148                 sc->iomem = 0;
149         }
150         if (sc->irq) {
151                 bus_release_resource(dev, SYS_RES_IRQ,
152                                      sc->irq_rid, sc->irq);
153                 sc->irq = 0;
154         }
155         if (sc->sc_ifp) {
156                 if_free(sc->sc_ifp);
157                 sc->sc_ifp = 0;
158         }
159 }
160
161 /****************************************************************
162   Probe routine
163  ****************************************************************/
164
165 int
166 snc_probe(device_t dev, int type)
167 {
168         struct snc_softc *sc = device_get_softc(dev);
169
170         return snc_nec16_detectsubr(sc->sc_iot, sc->sc_ioh,
171                                     sc->sc_memt, sc->sc_memh,
172                                     rman_get_start(sc->irq),
173                                     rman_get_start(sc->iomem),
174                                     type);
175 }
176
177 /****************************************************************
178   Attach routine
179  ****************************************************************/
180
181 int
182 snc_attach(device_t dev)
183 {
184         struct snc_softc *sc = device_get_softc(dev);
185         u_int8_t myea[ETHER_ADDR_LEN];
186         int error;
187
188         if (snc_nec16_register_irq(sc, rman_get_start(sc->irq)) == 0 || 
189             snc_nec16_register_mem(sc, rman_get_start(sc->iomem)) == 0) {
190                 snc_release_resources(dev);
191                 return(ENOENT);
192         }
193
194         snc_nec16_get_enaddr(sc->sc_iot, sc->sc_ioh, myea);
195         device_printf(dev, "%s Ethernet\n", snc_nec16_detect_type(myea));
196
197         sc->sc_dev = dev;
198
199         sc->sncr_dcr = DCR_SYNC | DCR_WAIT0 |
200             DCR_DMABLOCK | DCR_RFT16 | DCR_TFT28;
201         sc->sncr_dcr2 = 0;      /* XXX */
202         sc->bitmode = 0;        /* 16 bit card */
203
204         sc->sc_nic_put = snc_nec16_nic_put;
205         sc->sc_nic_get = snc_nec16_nic_get;
206         sc->sc_writetodesc = snc_nec16_writetodesc;
207         sc->sc_readfromdesc = snc_nec16_readfromdesc;
208         sc->sc_copytobuf = snc_nec16_copytobuf;
209         sc->sc_copyfrombuf = snc_nec16_copyfrombuf;
210         sc->sc_zerobuf = snc_nec16_zerobuf;
211
212         /* sncsetup returns 1 if something fails */
213         if (sncsetup(sc, myea)) {
214                 snc_release_resources(dev);
215                 return(ENOENT);
216         }
217
218         mtx_init(&sc->sc_lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
219             MTX_DEF);
220         callout_init_mtx(&sc->sc_timer, &sc->sc_lock, 0);
221         error = sncconfig(sc, NULL, 0, 0, myea);
222         if (error) {
223                 snc_release_resources(dev);
224                 mtx_destroy(&sc->sc_lock);
225                 return (error);
226         }
227
228         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
229                                NULL, sncintr, sc, &sc->irq_handle);
230         if (error) {
231                 printf("snc_isa_attach: bus_setup_intr() failed\n");
232                 ether_ifdetach(sc->sc_ifp);             
233                 snc_release_resources(dev);
234                 mtx_destroy(&sc->sc_lock);
235                 return (error);
236         }
237
238         return 0;
239 }
240
241 /****************************************************************
242   Shutdown routine
243  ****************************************************************/
244
245 int
246 snc_shutdown(device_t dev)
247 {
248         struct snc_softc *sc = device_get_softc(dev);
249
250         SNC_LOCK(sc);
251         sncshutdown(sc);
252         SNC_UNLOCK(sc);
253
254         return (0);
255 }