Reduce ifnet.if_serializer contention on output path:
[dragonfly.git] / sys / dev / netif / ep / if_ep_isa.c
1 /*
2  * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Herb Peyerl.
16  * 4. The name of Herb Peyerl may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD: src/sys/dev/ep/if_ep_isa.c,v 1.8.2.1 2000/12/16 03:47:57 nyan Exp $
31  * $DragonFly: src/sys/dev/netif/ep/if_ep_isa.c,v 1.14 2008/05/14 11:59:19 sephe Exp $
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/socket.h>
38 #include <sys/module.h>
39 #include <sys/interrupt.h>
40 #include <sys/bus.h>
41 #include <sys/rman.h> 
42
43 #include <net/if.h>
44 #include <net/if_arp.h>
45 #include <net/if_media.h> 
46
47 #include <machine/clock.h>
48
49 #include <bus/isa/isavar.h>
50 #include <bus/isa/pnpvar.h>
51
52 #include "if_epreg.h"
53 #include "if_epvar.h"
54 #include "../elink_layer/elink.h"
55
56 static u_int16_t        get_eeprom_data (int, int);
57
58 static int              ep_isa_identify (driver_t *, device_t);
59 static int              ep_isa_probe    (device_t);
60 static int              ep_isa_attach   (device_t);
61
62 struct isa_ident {
63         u_int32_t       id;
64         char *          name;
65 };
66 const char * ep_isa_match_id (u_int32_t, struct isa_ident *);
67
68 #define ISA_ID_3C509_XXX   0x0506d509
69 #define ISA_ID_3C509_TP    0x506d5090
70 #define ISA_ID_3C509_BNC   0x506d5091
71 #define ISA_ID_3C509_COMBO 0x506d5094
72 #define ISA_ID_3C509_TPO   0x506d5095
73 #define ISA_ID_3C509_TPC   0x506d5098
74
75 static struct isa_ident ep_isa_devs[] = {
76         { ISA_ID_3C509_TP,      "3Com 3C509-TP EtherLink III" },
77         { ISA_ID_3C509_BNC,     "3Com 3C509-BNC EtherLink III" },
78         { ISA_ID_3C509_COMBO,   "3Com 3C509-Combo EtherLink III" },
79         { ISA_ID_3C509_TPO,     "3Com 3C509-TPO EtherLink III" },
80         { ISA_ID_3C509_TPC,     "3Com 3C509-TPC EtherLink III" },
81         { 0,                    NULL },
82 };
83
84 static struct isa_pnp_id ep_ids[] = {
85         { 0x90506d50,   "3Com 3C509B-TP EtherLink III (PnP)" }, /* TCM5090 */
86         { 0x91506d50,   "3Com 3C509B-BNC EtherLink III (PnP)" },/* TCM5091 */
87         { 0x94506d50,   "3Com 3C509B-Combo EtherLink III (PnP)" },/* TCM5094 */
88         { 0x95506d50,   "3Com 3C509B-TPO EtherLink III (PnP)" },/* TCM5095 */
89         { 0x98506d50,   "3Com 3C509B-TPC EtherLink III (PnP)" },/* TCM5098 */
90         { 0xf780d041,   NULL }, /* PNP80f7 */
91         { 0,            NULL },
92 };
93
94 /*
95  * We get eeprom data from the id_port given an offset into the eeprom.
96  * Basically; after the ID_sequence is sent to all of the cards; they enter
97  * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
98  * the eeprom data.  We then read the port 16 times and with every read; the
99  * cards check for contention (ie: if one card writes a 0 bit and another
100  * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
101  * compares the data on the bus; if there is a difference then that card goes
102  * into ID_WAIT state again). In the meantime; one bit of data is returned in
103  * the AX register which is conveniently returned to us by inb().  Hence; we
104  * read 16 times getting one bit of data with each read.
105  */
106
107 static u_int16_t
108 get_eeprom_data(int id_port, int offset)
109 {
110         int             i;
111         u_int16_t       data = 0;
112
113         outb(id_port, EEPROM_CMD_RD|offset);
114         DELAY(BIT_DELAY_MULTIPLE * 1000);
115         for (i = 0; i < 16; i++) {
116                 DELAY(50);
117                 data = (data << 1) | (inw(id_port) & 1);
118         }
119         return (data);
120 }
121
122 const char *
123 ep_isa_match_id(u_int32_t id, struct isa_ident *isa_devs)
124 {
125         struct isa_ident *      i = isa_devs;
126         while(i->name != NULL) {
127                if (id == i->id)
128                       return (i->name);
129                i++;
130         }
131         /*
132          * If we see a card that is likely to be a 3c509
133          * return something so that it will work; be annoying
134          * so that the user will tell us about it though.
135          */
136         if ((id >> 4) == ISA_ID_3C509_XXX) {
137                 return ("Unknown 3c509; notify maintainer!");
138         }
139         return (NULL);
140 }
141
142 static int
143 ep_isa_identify(driver_t *driver, device_t parent)
144 {
145         int             tag = EP_LAST_TAG;
146         int             found = 0;
147         int             i;
148         int             j;
149         const char *    desc;
150         u_int16_t       data;
151         u_int32_t       irq;
152         u_int32_t       ioport;
153         u_int32_t       isa_id;
154         device_t        child;
155
156         /*
157          * Rescans not currently supported.
158          */
159         if (device_get_state(parent) == DS_ATTACHED)
160                 return (0);
161
162         /*
163          * Check for the existance of the EISA bus.
164          */
165         outb(ELINK_ID_PORT, 0);
166         outb(ELINK_ID_PORT, 0);
167         elink_idseq(ELINK_509_POLY);
168         elink_reset();
169
170         DELAY(DELAY_MULTIPLE * 10000);
171
172         for (i = 0; i < EP_MAX_BOARDS; i++) {
173
174                 outb(ELINK_ID_PORT, 0);
175                 outb(ELINK_ID_PORT, 0);
176                 elink_idseq(ELINK_509_POLY);
177                 DELAY(400);
178
179                 /* For the first probe, clear all
180                  * board's tag registers.
181                  * Otherwise kill off already-found
182                  * boards. -- linux 3c509.c
183                  */
184                 if (i == 0) {
185                         outb(ELINK_ID_PORT, 0xd0);
186                 } else {
187                         outb(ELINK_ID_PORT, 0xd8);
188                 }
189
190                 /* Get out of loop if we're out of cards. */
191                 data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
192                 if (data != MFG_ID) {
193                         break;
194                 }
195
196                 /* resolve contention using the Ethernet address */
197                 for (j = 0; j < 3; j++) {
198                         get_eeprom_data(ELINK_ID_PORT, j);
199                 }
200
201                 /*
202                  * Construct an 'isa_id' in 'EISA'
203                  * format.
204                  */
205                 data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
206                 isa_id = (htons(data) << 16);
207                 data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
208                 isa_id |= htons(data);
209
210                 /* Find known ISA boards */
211                 desc = ep_isa_match_id(isa_id, ep_isa_devs);
212                 if (!desc) {
213                         if (bootverbose) {
214                                 device_printf(parent, "if_ep: unknown ID 0x%08x\n",
215                                                 isa_id);
216                         }
217                         continue;
218                 }
219
220                 /* Retreive IRQ */
221                 data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
222                 irq = (data >> 12);
223
224                 /* Retreive IOPORT */
225                 data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
226                 ioport = (((data & 0x1f) << 4) + 0x200);
227
228                 /* Test for an adapter with PnP support. */
229                 data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
230                 if (data == CAP_ISA) {
231                         data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
232                         if (data & ICW1_IAS_PNP) {
233                                 if (bootverbose) {
234                                         device_printf(parent, "if_ep: <%s> at 0x%03x in PnP mode!\n",
235                                                       desc, ioport);
236                                 }
237                                 /* Set the adaptor tag so that the next card can be found. */
238                                 outb(ELINK_ID_PORT, tag--);
239                                 continue;
240                         }
241                 }
242
243                 /* Set the adaptor tag so that the next card can be found. */
244                 outb(ELINK_ID_PORT, tag--);
245
246                 /* Activate the adaptor at the EEPROM location. */
247                 outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
248
249                 /* Test for an adapter in TEST mode. */
250                 outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
251                 data = inw(ioport + EP_W0_EEPROM_COMMAND);
252                 if (data & EEPROM_TST_MODE) {
253                         device_printf(parent, "if_ep: <%s> at port 0x%03x in TEST mode!  Erase pencil mark.\n",
254                                         desc, ioport);
255                         continue;
256                 }
257
258                 child = BUS_ADD_CHILD(parent, parent,
259                                       ISA_ORDER_SPECULATIVE, "ep", -1);
260                 device_set_desc_copy(child, desc);
261                 device_set_driver(child, driver);
262                 bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
263                 bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
264
265                 if (bootverbose) {
266                         device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
267                                         desc, ioport, ioport + EP_IOSIZE, irq);
268                 }
269
270                 found++;
271         }
272         return (found ? 0 : ENXIO);
273 }
274
275 static int
276 ep_isa_probe(device_t dev)
277 {
278         int     error = 0;
279
280         /* Check isapnp ids */
281         error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
282
283         /* If the card had a PnP ID that didn't match any we know about */
284         if (error == ENXIO) {
285                return (error);
286         }
287
288         /* If we had some other problem. */
289         if (!(error == 0 || error == ENOENT)) {
290                 return (error);
291         }
292
293         /* If we have the resources we need then we're good to go. */
294         if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
295             (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
296                 return (0);
297         }
298
299         return (ENXIO);
300 }
301
302 static int
303 ep_isa_attach(device_t dev)
304 {
305         struct ep_softc *       sc = device_get_softc(dev);
306         struct ifnet *          ifp = &sc->arpcom.ac_if;
307         int                     error = 0;
308
309         if ((error = ep_alloc(dev))) {
310                 device_printf(dev, "ep_alloc() failed! (%d)\n", error);
311                 goto bad;
312         }
313
314         ep_get_media(sc);
315
316         GO_WINDOW(0);
317         SET_IRQ(BASE, rman_get_start(sc->irq));
318
319         if ((error = ep_attach(sc))) {
320                 device_printf(dev, "ep_attach() failed! (%d)\n", error);
321                 goto bad;
322         }
323
324         error = bus_setup_intr(dev, sc->irq, INTR_NETSAFE, ep_intr,
325                                sc, &sc->ep_intrhand, ifp->if_serializer);
326         if (error) {
327                 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
328                 goto bad;
329         }
330
331         ifp->if_cpuid = ithread_cpuid(rman_get_start(sc->irq));
332         KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
333
334         return (0);
335 bad:
336         ep_free(dev);
337         return (error);
338 }
339
340 static device_method_t ep_isa_methods[] = {
341         /* Device interface */
342         DEVMETHOD(device_identify,      ep_isa_identify),
343         DEVMETHOD(device_probe,         ep_isa_probe),
344         DEVMETHOD(device_attach,        ep_isa_attach),
345
346         { 0, 0 }
347 };
348
349 static driver_t ep_isa_driver = {
350         "ep",
351         ep_isa_methods,
352         sizeof(struct ep_softc),
353 };
354
355 extern devclass_t ep_devclass;
356
357 DRIVER_MODULE(if_ep, isa, ep_isa_driver, ep_devclass, 0, 0);