Make all network interrupt service routines MPSAFE part 1/3.
[dragonfly.git] / sys / dev / netif / ex / if_ex_isa.c
1 /*-
2  * Copyright (c) 2000 Matthew N. Dodd
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. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/sys/dev/ex/if_ex_isa.c,v 1.3.2.1 2001/03/05 05:33:20 imp Exp $
27  *      $DragonFly: src/sys/dev/netif/ex/if_ex_isa.c,v 1.11 2005/11/28 17:13:42 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/serialize.h>
38
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <machine/clock.h>
42 #include <sys/rman.h>
43
44 #include <net/if.h>
45 #include <net/if_arp.h>
46 #include <net/if_media.h> 
47
48
49 #include <bus/isa/isavar.h>
50 #include <bus/isa/pnpvar.h>
51
52 #include "if_exreg.h"
53 #include "if_exvar.h"
54
55 /* Bus Front End Functions */
56 static int      ex_isa_identify (driver_t *, device_t);
57 static int      ex_isa_probe    (device_t);
58 static int      ex_isa_attach   (device_t);
59
60 #if 0
61 static  void    ex_pnp_wakeup   (void *);
62
63 SYSINIT(ex_pnpwakeup, SI_SUB_CPU, SI_ORDER_ANY, ex_pnp_wakeup, NULL);
64 #endif
65
66 /*
67  * We need an identify function to 'probe' the ISA bus.
68  */
69 static device_method_t ex_methods[] = {
70         /* Device interface */
71         DEVMETHOD(device_identify,      ex_isa_identify),
72         DEVMETHOD(device_probe,         ex_isa_probe),
73         DEVMETHOD(device_attach,        ex_isa_attach),
74
75         { 0, 0 }
76 };
77
78 static driver_t ex_driver = {
79         "ex",
80         ex_methods,
81         sizeof(struct ex_softc),
82 };
83
84 devclass_t ex_devclass;
85
86 DRIVER_MODULE(if_ex, isa, ex_driver, ex_devclass, 0, 0);
87
88 static struct isa_pnp_id ex_ids[] = {
89         { 0x3110d425,   NULL }, /* INT1031 */
90         { 0x3010d425,   NULL }, /* INT1030 */
91         { 0,            NULL },
92 };
93
94 #if 0
95 #define EX_PNP_WAKE             0x279
96
97 static u_int8_t ex_pnp_wake_seq[] =
98                         { 0x6A, 0xB5, 0xDA, 0xED, 0xF6, 0xFB, 0x7D, 0xBE,
99                           0xDF, 0x6F, 0x37, 0x1B, 0x0D, 0x86, 0xC3, 0x61,
100                           0xB0, 0x58, 0x2C, 0x16, 0x8B, 0x45, 0xA2, 0xD1,
101                           0xE8, 0x74, 0x3A, 0x9D, 0xCE, 0xE7, 0x73, 0x43 };
102
103 static void
104 ex_pnp_wakeup (void * dummy)
105 {
106         int     tmp;
107
108         if (bootverbose)
109                 printf("ex_pnp_wakeup()\n");
110
111         outb(EX_PNP_WAKE, 0);
112         outb(EX_PNP_WAKE, 0);
113         for (tmp = 0; tmp < 32; tmp++) {
114                 outb(EX_PNP_WAKE, ex_pnp_wake_seq[tmp]);
115         }
116 }
117 #endif
118
119 /*
120  * Non-destructive identify.
121  */
122 static int
123 ex_isa_identify (driver_t *driver, device_t parent)
124 {
125         device_t        child;
126         u_int32_t       ioport;
127         u_char          enaddr[6];
128         u_int           irq;
129         int             tmp;
130         int             count;
131         const char *    desc;
132
133         /*
134          * Rescanning ISA I/O ports is not supported.
135          */
136         if (device_get_state(parent) == DS_ATTACHED)
137                 return (0);
138
139         if (bootverbose)
140                 printf("ex_isa_identify()\n");
141
142         count = 0;
143         for (ioport = 0x200; ioport < 0x3a0; ioport += 0x10) {
144
145                 /* No board found at address */
146                 if (!look_for_card(ioport)) {
147                         continue;
148                 }
149
150                 if (bootverbose)
151                         printf("ex: Found card at 0x%03x!\n", ioport);
152
153                 /* Board in PnP mode */
154                 if (eeprom_read(ioport, EE_W0) & EE_W0_PNP) {
155                         /* Reset the card. */
156                         outb(ioport + CMD_REG, Reset_CMD);
157                         DELAY(500);
158                         if (bootverbose)
159                                 printf("ex: card at 0x%03x in PnP mode!\n", ioport);
160                         continue;
161                 }
162
163                 bzero(enaddr, sizeof(enaddr));
164
165                 /* Reset the card. */
166                 outb(ioport + CMD_REG, Reset_CMD);
167                 DELAY(400);
168
169                 ex_get_address(ioport, enaddr);
170                 tmp = eeprom_read(ioport, EE_W1) & EE_W1_INT_SEL;
171
172                 /* work out which set of irq <-> internal tables to use */
173                 if (ex_card_type(enaddr) == CARD_TYPE_EX_10_PLUS) {
174                         irq  = plus_ee2irqmap[tmp];
175                         desc = "Intel Pro/10+";
176                 } else {
177                         irq = ee2irqmap[tmp];
178                         desc = "Intel Pro/10";
179                 }
180
181                 child = BUS_ADD_CHILD(parent, parent,
182                                       ISA_ORDER_SPECULATIVE, "ex", -1);
183                 device_set_desc_copy(child, desc);
184                 device_set_driver(child, driver);
185                 bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
186                 bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EX_IOSIZE);
187                 ++count;
188
189                 if (bootverbose)
190                         printf("ex: Adding board at 0x%03x, irq %d\n", ioport, irq);
191         }
192         return (count ? 0 : ENXIO);
193 }
194
195 static int
196 ex_isa_probe(device_t dev)
197 {
198         u_int           iobase;
199         u_int           irq;
200         char *          irq2ee;
201         u_char *        ee2irq;
202         u_char          enaddr[6];
203         int             tmp;
204         int             error;
205
206         /* Check isapnp ids */
207         error = ISA_PNP_PROBE(device_get_parent(dev), dev, ex_ids);
208
209         /* If the card had a PnP ID that didn't match any we know about */
210         if (error == ENXIO) {
211                 return(error);
212         }
213
214         /* If we had some other problem. */
215         if (!(error == 0 || error == ENOENT)) {
216                 return(error);
217         }
218
219         iobase = bus_get_resource_start(dev, SYS_RES_IOPORT, 0);
220         if (!iobase) {
221                 printf("ex: no iobase?\n");
222                 return(ENXIO);
223         }
224
225         if (!look_for_card(iobase)) {
226                 printf("ex: no card found at 0x%03x\n", iobase);
227                 return(ENXIO);
228         }
229
230         if (bootverbose)
231                 printf("ex: ex_isa_probe() found card at 0x%03x\n", iobase);
232
233         /*
234          * Reset the card.
235          */
236         outb(iobase + CMD_REG, Reset_CMD);
237         DELAY(800);
238
239         ex_get_address(iobase, enaddr);
240
241         /* work out which set of irq <-> internal tables to use */
242         if (ex_card_type(enaddr) == CARD_TYPE_EX_10_PLUS) {
243                 irq2ee = plus_irq2eemap;
244                 ee2irq = plus_ee2irqmap;
245         } else {
246                 irq2ee = irq2eemap;
247                 ee2irq = ee2irqmap;
248         }
249
250         tmp = eeprom_read(iobase, EE_W1) & EE_W1_INT_SEL;
251         irq = bus_get_resource_start(dev, SYS_RES_IRQ, 0);
252
253         if (irq > 0) {
254                 /* This will happen if board is in PnP mode. */
255                 if (ee2irq[tmp] != irq) {
256                         printf("ex: WARNING: board's EEPROM is configured"
257                                 " for IRQ %d, using %d\n",
258                                 ee2irq[tmp], irq);
259                 }
260         } else {
261                 irq = ee2irq[tmp];
262                 bus_set_resource(dev, SYS_RES_IRQ, 0, irq, 1);
263         }
264
265         if (irq == 0) {
266                 printf("ex: invalid IRQ.\n");
267                 return(ENXIO);
268         }
269
270         return(0);
271 }
272
273 static int
274 ex_isa_attach(device_t dev)
275 {
276         struct ex_softc *       sc = device_get_softc(dev);
277         int                     error = 0;
278         u_int16_t               temp;
279
280         sc->dev = dev;
281         sc->ioport_rid = 0;
282         sc->irq_rid = 0;
283
284         if ((error = ex_alloc_resources(dev)) != 0) {
285                 device_printf(dev, "ex_alloc_resources() failed!\n");
286                 goto bad;
287         }
288
289         /*
290          * Fill in several fields of the softc structure:
291          *      - I/O base address.
292          *      - Hardware Ethernet address.
293          *      - IRQ number (if not supplied in config file, read it from EEPROM).
294          *      - Connector type.
295          */
296         sc->iobase = rman_get_start(sc->ioport);
297         sc->irq_no = rman_get_start(sc->irq);
298
299         ex_get_address(sc->iobase, sc->arpcom.ac_enaddr);
300
301         temp = eeprom_read(sc->iobase, EE_W0);
302         device_printf(sc->dev, "%s config, %s bus, ",
303                 (temp & EE_W0_PNP) ? "PnP" : "Manual",
304                 (temp & EE_W0_BUS16) ? "16-bit" : "8-bit");
305
306         temp = eeprom_read(sc->iobase, EE_W6);
307         printf("board id 0x%03x, stepping 0x%01x\n",
308                 (temp & EE_W6_BOARD_MASK) >> EE_W6_BOARD_SHIFT,
309                 temp & EE_W6_STEP_MASK);
310
311         if ((error = ex_attach(dev)) != 0) {
312                 device_printf(dev, "ex_attach() failed!\n");
313                 goto bad;
314         }
315
316         error = bus_setup_intr(dev, sc->irq, INTR_NETSAFE,
317                                 ex_intr, (void *)sc, &sc->ih, 
318                                 sc->arpcom.ac_if.if_serializer);
319         if (error) {
320                 device_printf(dev, "bus_setup_intr() failed!\n");
321                 goto bad;
322         }
323
324         return(0);
325 bad:
326         ex_release_resources(dev);
327         return (error);
328 }