Make all network interrupt service routines MPSAFE part 1/3.
[dragonfly.git] / sys / dev / netif / fpa / if_fpa.c
1 /*-
2  * Copyright (c) 1995, 1996 Matt Thomas <matt@3am-software.com>
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, this list of conditions and the following disclaimer.
10  * 2. The name of the author may not be used to endorse or promote products
11  *    derived from this software withough specific prior written permission
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * $FreeBSD: src/sys/dev/pdq/if_fpa.c,v 1.13 1999/08/28 00:50:50 peter Exp $
25  * $DragonFly: src/sys/dev/netif/fpa/Attic/if_fpa.c,v 1.12 2005/11/28 17:13:42 dillon Exp $
26  */
27
28 /*
29  * DEC PDQ FDDI Controller; code for BSD derived operating systems
30  *
31  *   This module supports the DEC DEFPA PCI FDDI Controller
32  */
33
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37 #if defined(__bsdi__) || defined(__NetBSD__)
38 #include <sys/device.h>
39 #endif
40 #include <sys/serialize.h>
41
42 #include <net/if.h>
43
44 #if defined(__DragonFly__) || defined(__FreeBSD__)
45 #include "use_fpa.h"
46 #include <sys/eventhandler.h>
47 #include <net/ethernet.h>
48 #include <net/if_arp.h>
49 #include <bus/pci/pcivar.h>
50 #include <dev/netif/pdq_layer/pdqvar.h>
51 #include <dev/netif/pdq_layer/pdqreg.h>
52 #elif defined(__bsdi__)
53 #include <i386/isa/isavar.h>
54 #include <i386/icu/icu.h>
55 #ifndef DRQNONE
56 #define DRQNONE         0
57 #endif
58 #if _BSDI_VERSION < 199401
59 #define IRQSHARE        0
60 #endif
61 #elif defined(__NetBSD__)
62 #include <dev/pci/pcivar.h>
63 #include <dev/ic/pdqvar.h>
64 #include <dev/ic/pdqreg.h>
65 #endif /* __NetBSD__ */
66
67
68 #define DEC_VENDORID            0x1011
69 #define DEFPA_CHIPID            0x000F
70 #define PCI_VENDORID(x)         ((x) & 0xFFFF)
71 #define PCI_CHIPID(x)           (((x) >> 16) & 0xFFFF)
72
73 #define DEFPA_LATENCY   0x88
74
75 #define PCI_CFLT        0x0C    /* Configuration Latency */
76 #define PCI_CBMA        0x10    /* Configuration Base Memory Address */
77 #define PCI_CBIO        0x14    /* Configuration Base I/O Address */
78
79 #if defined(__DragonFly__) || defined(__FreeBSD__)
80 #if NFPA < 4
81 #undef NFPA
82 #define NFPA    4
83 #endif
84 static pdq_softc_t *pdqs_pci[NFPA];
85 #define PDQ_PCI_UNIT_TO_SOFTC(unit)     (pdqs_pci[unit])
86 #if BSD >= 199506
87 #define pdq_pci_ifwatchdog              NULL
88 #endif
89
90 #elif defined(__bsdi__)
91 extern struct cfdriver fpacd;
92 #define PDQ_PCI_UNIT_TO_SOFTC(unit)     ((pdq_softc_t *)fpacd.cd_devs[unit])
93
94 #elif defined(__NetBSD__)
95 extern struct cfattach fpa_ca;
96 extern struct cfdriver fpa_cd;
97 #define PDQ_PCI_UNIT_TO_SOFTC(unit)     ((pdq_softc_t *)fpa_cd.cd_devs[unit])
98 #define pdq_pci_ifwatchdog              NULL
99 #endif
100
101 #ifndef pdq_pci_ifwatchdog
102 static ifnet_ret_t
103 pdq_pci_ifwatchdog(
104     int unit)
105 {
106     pdq_ifwatchdog(&PDQ_PCI_UNIT_TO_SOFTC(unit)->sc_if);
107 }
108 #endif
109
110 #if defined(__DragonFly__) || (defined(__FreeBSD__) && BSD >= 199506)
111 static void
112 pdq_pci_ifintr(
113     void *arg)
114 {
115     pdq_softc_t *sc = arg;
116
117     lwkt_serialize_enter(sc->sc_if.if_serializer);
118     pdq_interrupt(sc->sc_pdq);
119     lwkt_serialize_exit(sc->sc_if.if_serializer);
120 }
121 #else
122 static int
123 pdq_pci_ifintr(
124     void *arg)
125 {
126     pdq_softc_t * const sc = (pdq_softc_t *) arg;
127 #if defined(__DragonFly__) || defined(__FreeBSD__)
128     return pdq_interrupt(sc->sc_pdq);
129 #elif defined(__bsdi__) || defined(__NetBSD__)
130     (void) pdq_interrupt(sc->sc_pdq);
131     return 1;
132 #endif
133 }
134 #endif /* __FreeBSD && BSD */
135
136 #if defined(__DragonFly__) || defined(__FreeBSD__)
137 static void pdq_pci_shutdown(void *, int);
138
139 /*
140  * This is the PCI configuration support.  Since the PDQ is available
141  * on both EISA and PCI boards, one must be careful in how defines the
142  * PDQ in the config file.
143  */
144 static const char *
145 pdq_pci_probe(
146     pcici_t config_id,
147     pcidi_t device_id)
148 {
149     if (PCI_VENDORID(device_id) == DEC_VENDORID &&
150             PCI_CHIPID(device_id) == DEFPA_CHIPID)
151         return "Digital DEFPA PCI FDDI Controller";
152     return NULL;
153 }
154
155 static void
156 pdq_pci_attach(
157     pcici_t config_id,
158     int unit)
159 {
160     pdq_softc_t *sc;
161     vm_offset_t va_csrs, pa_csrs;
162     pdq_uint32_t data;
163
164     if (unit == NFPA) {
165         printf("fpa%d: not configured; kernel is built for only %d device%s.\n",
166                unit, NFPA, NFPA == 1 ? "" : "s");
167         return;
168     }
169
170     data = pci_conf_read(config_id, PCI_CFLT);
171     if ((data & 0xFF00) < (DEFPA_LATENCY << 8)) {
172         data &= ~0xFF00;
173         data |= DEFPA_LATENCY << 8;
174         pci_conf_write(config_id, PCI_CFLT, data);
175     }
176
177     sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
178     if (!pci_map_mem(config_id, PCI_CBMA, &va_csrs, &pa_csrs)) {
179         free((void *) sc, M_DEVBUF);
180         return;
181     }
182
183     if_initname(&(sc->sc_if), "fpa", unit);
184     sc->sc_membase = (pdq_bus_memaddr_t) va_csrs;
185     sc->sc_pdq = pdq_initialize(PDQ_BUS_PCI, sc->sc_membase,
186                                 sc->sc_if.if_dname, sc->sc_if.if_dunit,
187                                 (void *) sc, PDQ_DEFPA);
188     if (sc->sc_pdq == NULL) {
189         free((void *) sc, M_DEVBUF);
190         return;
191     }
192     bcopy((caddr_t) sc->sc_pdq->pdq_hwaddr.lanaddr_bytes, sc->sc_ac.ac_enaddr, 6);
193     pdqs_pci[unit] = sc;
194     pdq_ifattach(sc, pdq_pci_ifwatchdog);
195     pci_map_int(config_id, pdq_pci_ifintr, (void*) sc);
196     EVENTHANDLER_REGISTER(shutdown_post_sync, pdq_pci_shutdown, sc,
197                           SHUTDOWN_PRI_DEFAULT);
198
199 }
200
201 static void
202 pdq_pci_shutdown(
203     void *sc,
204     int howto)
205 {
206     lwkt_serialize_enter(sc->sc_if.if_serializer);
207     pdq_hwreset(((pdq_softc_t *)sc)->sc_pdq);
208     lwkt_serialize_exit(sc->sc_if.if_serializer);
209 }
210
211 static u_long pdq_pci_count;
212
213 static struct pci_device fpadevice = {
214     "fpa",
215     pdq_pci_probe,
216     pdq_pci_attach,
217     &pdq_pci_count,
218     NULL
219 };
220
221 COMPAT_PCI_DRIVER (if_fpa, fpadevice);
222
223 #elif defined(__bsdi__)
224
225 static int
226 pdq_pci_match(
227     pci_devaddr_t *pa)
228 {
229     int irq;
230     int id;
231
232     id = pci_inl(pa, PCI_VENDOR_ID);
233     if (PCI_VENDORID(id) != DEC_VENDORID || PCI_CHIPID(id) != DEFPA_CHIPID)
234         return 0;
235
236     irq = pci_inl(pa, PCI_I_LINE) & 0xFF;
237     if (irq == 0 || irq >= 16)
238         return 0;
239
240     return 1;
241 }
242
243 int
244 pdq_pci_probe(
245     struct device *parent,
246     struct cfdata *cf,
247     void *aux)
248 {
249     struct isa_attach_args *ia = (struct isa_attach_args *) aux;
250     pdq_uint32_t irq, data;
251     pci_devaddr_t *pa;
252
253     pa = pci_scan(pdq_pci_match);
254     if (pa == NULL)
255         return 0;
256
257     irq = (1 << (pci_inl(pa, PCI_I_LINE) & 0xFF));
258
259     if (ia->ia_irq != IRQUNK && irq != ia->ia_irq) {
260         printf("fpa%d: error: desired IRQ of %d does not match device's actual IRQ of %d\n",
261                cf->cf_unit,
262                ffs(ia->ia_irq) - 1, ffs(irq) - 1);
263         return 0;
264     }
265     if (ia->ia_irq == IRQUNK) {
266         (void) isa_irqalloc(irq);
267         ia->ia_irq = irq;
268     }
269
270     /* PCI bus masters don't use host DMA channels */
271     ia->ia_drq = DRQNONE;
272
273     /* Get the memory base address; assume the BIOS set it up correctly */
274     ia->ia_maddr = (caddr_t) (pci_inl(pa, PCI_CBMA) & ~7);
275     pci_outl(pa, PCI_CBMA, 0xFFFFFFFF);
276     ia->ia_msize = ((~pci_inl(pa, PCI_CBMA)) | 7) + 1;
277     pci_outl(pa, PCI_CBMA, (int) ia->ia_maddr);
278
279     /* Disable I/O space access */
280     pci_outl(pa, PCI_COMMAND, pci_inl(pa, PCI_COMMAND) & ~1);
281     ia->ia_iobase = 0;
282     ia->ia_iosize = 0;
283
284     /* Make sure the latency timer is what the DEFPA likes */
285     data = pci_inl(pa, PCI_CFLT);
286     if ((data & 0xFF00) < (DEFPA_LATENCY << 8)) {
287         data &= ~0xFF00;
288         data |= DEFPA_LATENCY << 8;
289         pci_outl(pa, PCI_CFLT, data);
290     }
291     ia->ia_irq |= IRQSHARE;
292
293     return 1;
294 }
295
296 void
297 pdq_pci_attach(
298     struct device *parent,
299     struct device *self,
300     void *aux)
301 {
302     pdq_softc_t *sc = (pdq_softc_t *) self;
303     struct isa_attach_args *ia = (struct isa_attach_args *) aux;
304     struct ifnet *ifp = &sc->sc_if;
305     int i;
306
307     if_initname(&(sc->sc_if), "fpa", sc->sc_dev.dv_unit);
308     sc->sc_if.if_flags = 0;
309     sc->sc_membase = (pdq_bus_memaddr_t) mapphys((vm_offset_t)ia->ia_maddr, ia->ia_msize);
310
311     sc->sc_pdq = pdq_initialize(PDQ_BUS_PCI, sc->sc_membase,
312                                 sc->sc_if.if_dname, sc->sc_if.if_dunit,
313                                 (void *) sc, PDQ_DEFPA);
314     if (sc->sc_pdq == NULL) {
315         printf("%s: initialization failed\n", sc->sc_if.if_xname);
316         return;
317     }
318
319     bcopy((caddr_t) sc->sc_pdq->pdq_hwaddr.lanaddr_bytes, sc->sc_ac.ac_enaddr, 6);
320
321     pdq_ifattach(sc, pdq_pci_ifwatchdog);
322
323     isa_establish(&sc->sc_id, &sc->sc_dev);
324
325     sc->sc_ih.ih_fun = pdq_pci_ifintr;
326     sc->sc_ih.ih_arg = (void *)sc;
327     intr_establish(ia->ia_irq, &sc->sc_ih, DV_NET);
328
329     sc->sc_ats.func = (void (*)(void *)) pdq_hwreset;
330     sc->sc_ats.arg = (void *) sc->sc_pdq;
331     atshutdown(&sc->sc_ats, ATSH_ADD);
332 }
333
334 struct cfdriver fpacd = {
335     0, "fpa", pdq_pci_probe, pdq_pci_attach,
336 #if _BSDI_VERSION >= 199401
337     DV_IFNET,
338 #endif
339     sizeof(pdq_softc_t)
340 };
341
342 #elif defined(__NetBSD__)
343
344 static int
345 pdq_pci_match(
346     struct device *parent,
347     void *match,
348     void *aux)
349 {
350     struct pci_attach_args *pa = (struct pci_attach_args *) aux;
351
352     if (PCI_VENDORID(pa->pa_id) != DEC_VENDORID)
353         return 0;
354     if (PCI_CHIPID(pa->pa_id) == DEFPA_CHIPID)
355         return 1;
356
357     return 0;
358 }
359
360 static void
361 pdq_pci_attach(
362     struct device * const parent,
363     struct device * const self,
364     void * const aux)
365 {
366     pdq_softc_t * const sc = (pdq_softc_t *) self;
367     struct pci_attach_args * const pa = (struct pci_attach_args *) aux;
368     pdq_uint32_t data;
369     pci_intr_handle_t intrhandle;
370     const char *intrstr;
371 #ifdef PDQ_IOMAPPED
372     bus_io_addr_t iobase;
373     bus_io_size_t iosize;
374 #else
375     bus_mem_addr_t membase;
376     bus_mem_size_t memsize;
377 #endif
378
379     data = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CFLT);
380     if ((data & 0xFF00) < (DEFPA_LATENCY << 8)) {
381         data &= ~0xFF00;
382         data |= DEFPA_LATENCY << 8;
383         pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_CFLT, data);
384     }
385
386     sc->sc_bc = pa->pa_bc;
387     bcopy(sc->sc_dev.dv_xname, sc->sc_if.if_xname, IFNAMSIZ);
388     sc->sc_if.if_flags = 0;
389     sc->sc_if.if_softc = sc;
390
391 #ifdef PDQ_IOMAPPED
392     if (pci_io_find(pa->pa_pc, pa->pa_tag, PCI_CBIO, &iobase, &iosize)
393             || bus_io_map(pa->pa_bc, iobase, iosize, &sc->sc_iobase))
394         return;
395 #else
396     if (pci_mem_find(pa->pa_pc, pa->pa_tag, PCI_CBMA, &membase, &memsize, NULL)
397             || bus_mem_map(pa->pa_bc, membase, memsize, 0, &sc->sc_membase))
398         return;
399 #endif
400
401     sc->sc_pdq = pdq_initialize(sc->sc_bc, sc->sc_membase,
402                                 sc->sc_if.if_xname, 0,
403                                 (void *) sc, PDQ_DEFPA);
404     if (sc->sc_pdq == NULL) {
405         printf("%s: initialization failed\n", sc->sc_dev.dv_xname);
406         return;
407     }
408
409     bcopy((caddr_t) sc->sc_pdq->pdq_hwaddr.lanaddr_bytes, sc->sc_ac.ac_enaddr, 6);
410     pdq_ifattach(sc, pdq_pci_ifwatchdog);
411
412     if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
413                      pa->pa_intrline, &intrhandle)) {
414         printf("%s: couldn't map interrupt\n", self->dv_xname);
415         return;
416     }
417     intrstr = pci_intr_string(pa->pa_pc, intrhandle);
418     sc->sc_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET, pdq_pci_ifintr, sc);
419     if (sc->sc_ih == NULL) {
420         printf("%s: couldn't establish interrupt", self->dv_xname);
421         if (intrstr != NULL)
422             printf(" at %s", intrstr);
423         printf("\n");
424         return;
425     }
426
427     sc->sc_ats = shutdownhook_establish((void (*)(void *)) pdq_hwreset, sc->sc_pdq);
428     if (sc->sc_ats == NULL)
429         printf("%s: warning: couldn't establish shutdown hook\n", self->dv_xname);
430     if (intrstr != NULL)
431         printf("%s: interrupting at %s\n", self->dv_xname, intrstr);
432 }
433
434 struct cfattach fpa_ca = {
435     sizeof(pdq_softc_t), pdq_pci_match, pdq_pci_attach
436 };
437
438 struct cfdriver fpa_cd = {
439     0, "fpa", DV_IFNET
440 };
441
442 #endif /* __NetBSD__ */