Get rid of bus_{disable,enable}_intr(), it wasn't generic enough for
[dragonfly.git] / sys / contrib / dev / oltr / if_oltr.c
1 /*
2  * Copyright (c) 1998, Larry Lile
3  * All rights reserved.
4  *
5  * For latest sources and information on this driver, please
6  * go to http://anarchy.stdio.com.
7  *
8  * Questions, comments or suggestions should be directed to
9  * Larry Lile <lile@stdio.com>.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice unmodified, this list of conditions, and the following
16  *    disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/contrib/dev/oltr/if_oltr.c,v 1.11.2.5 2001/10/20 04:15:21 mdodd Exp $
34  * $DragonFly: src/sys/contrib/dev/oltr/Attic/if_oltr.c,v 1.17 2005/05/24 20:58:56 dillon Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sockio.h>
40 #include <sys/mbuf.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/param.h>
45
46 #include <net/if.h>
47 #include <net/if_arp.h>
48 #include <net/iso88025.h>
49 #include <net/ethernet.h>
50 #include <net/if_dl.h>
51 #include <net/if_media.h>
52 #include <net/iso88025.h>
53
54 #if defined(__DragonFly__) || (NBPFILTER > 0)
55 #include <net/bpf.h>
56 #endif
57
58 #include <vm/vm.h>              /* for vtophys */
59 #include <vm/pmap.h>            /* for vtophys */
60 #include <machine/bus_memio.h>
61 #include <machine/bus_pio.h>
62 #include <machine/bus.h>
63 #include <machine/resource.h>
64 #include <machine/clock.h>
65 #include <sys/bus.h>
66 #include <sys/rman.h>
67
68 #include <bus/pci/pcireg.h>
69 #include <bus/pci/pcivar.h>
70
71 #include "contrib/dev/oltr/trlld.h"
72
73 /*#define DEBUG_MASK DEBUG_POLL*/
74
75 #ifndef DEBUG_MASK
76 #define DEBUG_MASK 0x0000
77 #endif
78
79 #define DEBUG_POLL      0x0001
80 #define DEBUG_INT       0x0002
81 #define DEBUG_INIT      0x0004
82 #define DEBUG_FN_ENT    0x8000
83
84 #define PCI_VENDOR_OLICOM 0x108D
85
86 #define MIN3(A,B,C) (MIN(A, (MIN(B, C))))
87
88 char *AdapterName[] = {
89         /*  0 */ "Olicom XT Adapter [unsupported]",
90         /*  1 */ "Olicom OC-3115",
91         /*  2 */ "Olicom ISA 16/4 Adapter (OC-3117)",
92         /*  3 */ "Olicom ISA 16/4 Adapter (OC-3118)",
93         /*  4 */ "Olicom MCA 16/4 Adapter (OC-3129) [unsupported]",
94         /*  5 */ "Olicom MCA 16/4 Adapter (OC-3129) [unsupported]",
95         /*  6 */ "Olicom MCA 16/4 Adapter (OC-3129) [unsupported]",
96         /*  7 */ "Olicom EISA 16/4 Adapter (OC-3133)",
97         /*  8 */ "Olicom EISA 16/4 Adapter (OC-3133)",
98         /*  9 */ "Olicom EISA 16/4 Server Adapter (OC-3135)",
99         /* 10 */ "Olicom PCI 16/4 Adapter (OC-3136)",
100         /* 11 */ "Olicom PCI 16/4 Adapter (OC-3136)",
101         /* 12 */ "Olicom PCI/II 16/4 Adapter (OC-3137)",
102         /* 13 */ "Olicom PCI 16/4 Adapter (OC-3139)",
103         /* 14 */ "Olicom RapidFire 3140 16/4 PCI Adapter (OC-3140)",
104         /* 15 */ "Olicom RapidFire 3141 Fiber Adapter (OC-3141)",
105         /* 16 */ "Olicom PCMCIA 16/4 Adapter (OC-3220) [unsupported]",
106         /* 17 */ "Olicom PCMCIA 16/4 Adapter (OC-3121, OC-3230, OC-3232) [unsupported]",
107         /* 18 */ "Olicom PCMCIA 16/4 Adapter (OC-3250)",
108         /* 19 */ "Olicom RapidFire 3540 100/16/4 Adapter (OC-3540)"
109 };
110
111 /*
112  * Glue function prototypes for PMW kit IO
113  */
114
115 #ifndef TRlldInlineIO
116 static void DriverOutByte       (unsigned short, unsigned char);
117 static void DriverOutWord       (unsigned short, unsigned short);
118 static void DriverOutDword      (unsigned short, unsigned long);
119 static void DriverRepOutByte    (unsigned short, unsigned char  *, int);
120 static void DriverRepOutWord    (unsigned short, unsigned short *, int);
121 static void DriverRepOutDword   (unsigned short, unsigned long  *, int);
122 static unsigned char  DriverInByte (unsigned short);
123 static unsigned short DriverInWord (unsigned short);
124 static unsigned long  DriverInDword (unsigned short);
125 static void DriverRepInByte     (unsigned short, unsigned char  *, int);
126 static void DriverRepInWord     (unsigned short, unsigned short *, int);
127 static void DriverRepInDword    (unsigned short, unsigned long  *, int);
128 #endif /*TRlldInlineIO*/
129 static void DriverSuspend       (unsigned short);
130 static void DriverStatus        (void *, TRlldStatus_t *);
131 static void DriverCloseCompleted (void *);
132 static void DriverStatistics    (void *, TRlldStatistics_t *);
133 static void DriverTransmitFrameCompleted (void *, void *, int);
134 static void DriverReceiveFrameCompleted (void *, int, int, void *, int);
135
136 static TRlldDriver_t LldDriver = {
137         TRLLD_VERSION,
138 #ifndef TRlldInlineIO
139         DriverOutByte,
140         DriverOutWord,
141         DriverOutDword,
142         DriverRepOutByte,
143         DriverRepOutWord,
144         DriverRepOutDword,
145         DriverInByte,
146         DriverInWord,
147         DriverInDword,
148         DriverRepInByte,
149         DriverRepInWord,
150         DriverRepInDword,
151 #endif /*TRlldInlineIO*/
152         DriverSuspend,
153         DriverStatus,
154         DriverCloseCompleted,
155         DriverStatistics,
156         DriverTransmitFrameCompleted,
157         DriverReceiveFrameCompleted,
158 };
159
160 struct oltr_rx_buf {
161         int                     index;
162         char                    *data;
163         u_long                  address;
164 };
165
166 struct oltr_tx_buf {
167         int                     index;
168         char                    *data;
169         u_long                  address;
170 };
171
172 #define RING_BUFFER_LEN         16
173 #define RING_BUFFER(x)          ((RING_BUFFER_LEN - 1) & x)
174 #define RX_BUFFER_LEN           2048
175 #define TX_BUFFER_LEN           2048
176
177 struct oltr_softc {
178         struct arpcom           arpcom;
179         struct ifmedia          ifmedia;
180         bus_space_handle_t      oltr_bhandle;
181         bus_space_tag_t         oltr_btag;
182         void                    *oltr_intrhand;
183         struct resource         *oltr_irq;
184         struct resource         *oltr_res;
185         int                     unit;
186         int                     state;
187 #define OL_UNKNOWN      0
188 #define OL_INIT         1
189 #define OL_READY        2
190 #define OL_CLOSING      3
191 #define OL_CLOSED       4
192 #define OL_OPENING      5
193 #define OL_OPEN         6
194 #define OL_PROMISC      7
195 #define OL_DEAD         8
196         struct oltr_rx_buf      rx_ring[RING_BUFFER_LEN];
197         int                     tx_head, tx_avail, tx_frame;
198         struct oltr_tx_buf      tx_ring[RING_BUFFER_LEN];
199         TRlldTransmit_t         frame_ring[RING_BUFFER_LEN];
200         struct mbuf             *restart;
201         TRlldAdapter_t          TRlldAdapter;
202         TRlldStatistics_t       statistics;
203         TRlldStatistics_t       current;
204         TRlldAdapterConfig_t    config;
205         u_short                 AdapterMode;
206         u_long                  GroupAddress;
207         u_long                  FunctionalAddress;
208         struct callout          oltr_poll_ch;
209         /*struct callout_handle oltr_stat_ch;*/
210         void                    *work_memory;
211 };
212
213 #define SELF_TEST_POLLS 32
214
215 void oltr_poll                  (void *);
216 /*void oltr_stat                        (void *);*/
217
218 static void oltr_start          (struct ifnet *);
219 static void oltr_stop           (struct oltr_softc *);
220 static void oltr_close          (struct oltr_softc *);
221 static void oltr_init           (void *);
222 static int oltr_ioctl           (struct ifnet *, u_long, caddr_t,
223                                  struct ucred *);
224 static void oltr_intr           (void *);
225 static int oltr_ifmedia_upd     (struct ifnet *);
226 static void oltr_ifmedia_sts    (struct ifnet *, struct ifmediareq *);
227
228 #if defined(__DragonFly__) || __FreeBSD_version > 400000
229
230 static int oltr_pci_probe               (device_t);
231 static int oltr_pci_attach      (device_t);
232 static int oltr_pci_detach      (device_t);
233 static void oltr_pci_shutdown   (device_t);
234
235 static device_method_t oltr_methods[] = {
236         DEVMETHOD(device_probe,         oltr_pci_probe),
237         DEVMETHOD(device_attach,        oltr_pci_attach),
238         DEVMETHOD(device_detach,        oltr_pci_detach),
239         DEVMETHOD(device_shutdown,      oltr_pci_shutdown),
240         { 0, 0 }
241 };
242
243 static driver_t oltr_driver = {
244         "oltr",
245         oltr_methods,
246         sizeof(struct oltr_softc)
247 };
248
249 static devclass_t oltr_devclass;
250
251 DRIVER_MODULE(oltr, pci, oltr_driver, oltr_devclass, 0, 0);
252
253 static int
254 oltr_pci_probe(device_t dev)
255 {
256         int                     i, rc;
257         char                    PCIConfigHeader[64];
258         TRlldAdapterConfig_t    config;
259
260         if ((pci_get_vendor(dev) == PCI_VENDOR_OLICOM) &&
261            ((pci_get_device(dev) == 0x0001) ||
262             (pci_get_device(dev) == 0x0004) ||
263             (pci_get_device(dev) == 0x0005) ||
264             (pci_get_device(dev) == 0x0007) ||
265             (pci_get_device(dev) == 0x0008))) {
266
267                 for (i = 0; i < sizeof(PCIConfigHeader); i++)
268                         PCIConfigHeader[i] = pci_read_config(dev, i, 1);
269
270                 rc = TRlldPCIConfig(&LldDriver, &config, PCIConfigHeader);
271                 if (rc == TRLLD_PCICONFIG_FAIL) {
272                         device_printf(dev, "TRlldPciConfig failed!\n");
273                         return(ENXIO);
274                 }
275                 if (rc == TRLLD_PCICONFIG_VERSION) {
276                         device_printf(dev, "wrong LLD version\n");
277                         return(ENXIO);
278                 }
279                 device_set_desc(dev, AdapterName[config.type]);
280                 return(0);
281         }
282         return(ENXIO);
283 }
284
285 static int
286 oltr_pci_attach(device_t dev)
287 {
288         int                     i, s, rc = 0, rid,
289                                 scratch_size;
290         int                     media = IFM_TOKEN|IFM_TOK_UTP16;
291         u_long                  command;
292         char                    PCIConfigHeader[64];
293         struct oltr_softc               *sc = device_get_softc(dev);
294         struct ifnet            *ifp = &sc->arpcom.ac_if;
295
296         s = splimp();
297
298         bzero(sc, sizeof(struct oltr_softc));
299         sc->unit = device_get_unit(dev);
300         sc->state = OL_UNKNOWN;
301
302         for (i = 0; i < sizeof(PCIConfigHeader); i++)
303                 PCIConfigHeader[i] = pci_read_config(dev, i, 1);
304
305         switch(TRlldPCIConfig(&LldDriver, &sc->config, PCIConfigHeader)) {
306         case TRLLD_PCICONFIG_OK:
307                 break;
308         case TRLLD_PCICONFIG_SET_COMMAND:
309                 device_printf(dev, "enabling bus master mode\n");
310                 command = pci_read_config(dev, PCIR_COMMAND, 4);
311                 pci_write_config(dev, PCIR_COMMAND,
312                         (command | PCIM_CMD_BUSMASTEREN), 4);
313                 command = pci_read_config(dev, PCIR_COMMAND, 4);
314                 if (!(command & PCIM_CMD_BUSMASTEREN)) {
315                         device_printf(dev, "failed to enable bus master mode\n");
316                         goto config_failed;
317                 }
318                 break;
319         case TRLLD_PCICONFIG_FAIL:
320                 device_printf(dev, "TRlldPciConfig failed!\n");
321                 goto config_failed;
322                 break;
323         case TRLLD_PCICONFIG_VERSION:
324                 device_printf(dev, "wrong LLD version\n");
325                 goto config_failed;
326                 break;
327         }
328         device_printf(dev, "MAC address %6D\n", sc->config.macaddress, ":");
329
330         scratch_size = TRlldAdapterSize();
331         if (bootverbose)
332                 device_printf(dev, "adapter memory block size %d bytes\n", scratch_size);
333         sc->TRlldAdapter = (TRlldAdapter_t)malloc(scratch_size, M_DEVBUF, M_NOWAIT);
334         if (sc->TRlldAdapter == NULL) {
335                 device_printf(dev, "couldn't allocate scratch buffer (%d bytes)\n", scratch_size);
336                 goto config_failed;
337         }
338
339         /*
340          * Allocate RX/TX Pools
341          */
342         for (i = 0; i < RING_BUFFER_LEN; i++) {
343                 sc->rx_ring[i].index = i;
344                 sc->rx_ring[i].data = (char *)malloc(RX_BUFFER_LEN, M_DEVBUF, M_NOWAIT);
345                 sc->rx_ring[i].address = vtophys(sc->rx_ring[i].data);
346                 sc->tx_ring[i].index = i;
347                 sc->tx_ring[i].data = (char *)malloc(TX_BUFFER_LEN, M_DEVBUF, M_NOWAIT);
348                 sc->tx_ring[i].address = vtophys(sc->tx_ring[i].data);
349                 if ((!sc->rx_ring[i].data) || (!sc->tx_ring[i].data)) {
350                         device_printf(dev, "unable to allocate ring buffers\n");
351                         while (i > 0) {
352                                 if (sc->rx_ring[i].data)
353                                         free(sc->rx_ring[i].data, M_DEVBUF);
354                                 if (sc->tx_ring[i].data)
355                                         free(sc->tx_ring[i].data, M_DEVBUF);
356                                 i--;
357                         }
358                         goto config_failed;
359                 }
360         }
361         
362         /*
363          * Allocate interrupt and DMA channel
364          */
365         rid = 0;
366         sc->oltr_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
367                 (sc->config.mode & TRLLD_MODE_SHARE_INTERRUPT ? RF_ACTIVE | RF_SHAREABLE : RF_ACTIVE));
368         if (sc->oltr_irq == NULL) {
369                 device_printf(dev, "couldn't map interrupt\n");
370                 goto config_failed;
371         }
372         if (bus_setup_intr(dev, sc->oltr_irq, INTR_TYPE_NET, oltr_intr, sc,
373             &sc->oltr_intrhand, NULL)) {
374                 device_printf(dev, "couldn't setup interrupt\n");
375                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->oltr_irq);
376                 goto config_failed;
377         }
378
379         /*
380          * Do the ifnet initialization
381          */
382         ifp->if_softc   = sc;
383         if_initname(ifp, "oltr", device_get_unit(dev));
384         ifp->if_init    = oltr_init;
385         ifp->if_start   = oltr_start;
386         ifp->if_ioctl   = oltr_ioctl;
387         ifp->if_flags   = IFF_BROADCAST;
388         bcopy(sc->config.macaddress, sc->arpcom.ac_enaddr, sizeof(sc->config.macaddress));
389
390         /*
391          * Do ifmedia setup.
392          */
393         ifmedia_init(&sc->ifmedia, 0, oltr_ifmedia_upd, oltr_ifmedia_sts);
394         rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_16MBPS);
395         switch(sc->config.type) {
396         case TRLLD_ADAPTER_PCI7:        /* OC-3540 */
397                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP100, 0, NULL);
398                 /* FALL THROUGH */
399         case TRLLD_ADAPTER_PCI4:        /* OC-3139 */
400         case TRLLD_ADAPTER_PCI5:        /* OC-3140 */
401         case TRLLD_ADAPTER_PCI6:        /* OC-3141 */
402                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_AUTO, 0, NULL);
403                 media = IFM_TOKEN|IFM_AUTO;
404                 rc = TRlldSetSpeed(sc->TRlldAdapter, 0);
405                 /* FALL THROUGH */
406         default:
407                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP4, 0, NULL);
408                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP16, 0, NULL);
409                 break;
410         }
411         sc->ifmedia.ifm_media = media;
412         ifmedia_set(&sc->ifmedia, media);
413
414         /*
415          * Attach the interface
416          */
417         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
418         iso88025_ifattach(ifp);
419
420         splx(s);
421         return(0);
422
423 config_failed:
424
425         splx(s);
426         return(ENXIO);
427 }
428
429 static int
430 oltr_pci_detach(device_t dev)
431 {
432         struct oltr_softc       *sc = device_get_softc(dev);
433         struct ifnet            *ifp = &sc->arpcom.ac_if;
434         int s, i;
435
436         device_printf(dev, "driver unloading\n");
437
438         s = splimp();
439
440         if_detach(ifp);
441         if (sc->state > OL_CLOSED)
442                 oltr_stop(sc);
443
444         callout_stop(&sc->oltr_poll_ch);
445         /*untimeout(oltr_stat, (void *)sc, sc->oltr_stat_ch);*/
446
447         bus_teardown_intr(dev, sc->oltr_irq, sc->oltr_intrhand);
448         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->oltr_irq);
449
450         /* Deallocate all dynamic memory regions */
451         for (i = 0; i < RING_BUFFER_LEN; i++) {
452                 free(sc->rx_ring[i].data, M_DEVBUF);
453                 free(sc->tx_ring[i].data, M_DEVBUF);
454         }
455         if (sc->work_memory)
456                 free(sc->work_memory, M_DEVBUF);
457         free(sc->TRlldAdapter, M_DEVBUF);
458
459         (void)splx(s);
460
461         return(0);
462 }
463
464 static void
465 oltr_pci_shutdown(device_t dev)
466 {
467         struct oltr_softc               *sc = device_get_softc(dev);
468
469         device_printf(dev, "oltr_pci_shutdown called\n");
470
471         if (sc->state > OL_CLOSED)
472                 oltr_stop(sc);
473
474         return;
475 }
476
477 #else
478
479 static const char *oltr_pci_probe       (pcici_t, pcidi_t);
480 static void oltr_pci_attach             (pcici_t, int);
481
482 static unsigned long oltr_count = 0;
483
484 static struct pci_device oltr_device = {
485         "oltr",
486         oltr_pci_probe,
487         oltr_pci_attach,
488         &oltr_count,
489         NULL
490 };
491
492 DATA_SET(pcidevice_set, oltr_device);
493
494 static const char *
495 oltr_pci_probe(pcici_t config_id, pcidi_t device_id)
496 {
497         int                     i, rc;
498         char                    PCIConfigHeader[64];
499         TRlldAdapterConfig_t    config;
500         
501         if (((device_id & 0xffff) == PCI_VENDOR_OLICOM) && (
502             (((device_id >> 16) & 0xffff) == 0x0001) ||
503             (((device_id >> 16) & 0xffff) == 0x0004) ||
504             (((device_id >> 16) & 0xffff) == 0x0005) ||
505             (((device_id >> 16) & 0xffff) == 0x0007) ||
506             (((device_id >> 16) & 0xffff) == 0x0008))) {
507         
508                 for (i = 0; i < 64; i++)
509                         PCIConfigHeader[i] = pci_cfgread(config_id, i, /* bytes */ 1);
510
511                 rc = TRlldPCIConfig(&LldDriver, &config, PCIConfigHeader);
512
513                 if (rc == TRLLD_PCICONFIG_FAIL) {
514                         printf("oltr: TRlldPciConfig failed!\n");
515                         return(NULL);
516                 }
517                 if (rc == TRLLD_PCICONFIG_VERSION) {
518                         printf("oltr: wrong LLD version.\n");
519                         return(NULL);
520                 }
521                 return(AdapterName[config.type]);
522         }
523
524         return(NULL);
525 }
526
527 static void
528 oltr_pci_attach(pcici_t config_id, int unit)
529 {
530         int                     i, s, rc = 0, scratch_size;
531         int                     media = IFM_TOKEN|IFM_TOK_UTP16;
532         u_long                  command;
533         char                    PCIConfigHeader[64];
534         struct oltr_softc               *sc;
535         struct ifnet            *ifp; /* = &sc->arpcom.ac_if; */
536
537         s = splimp();
538
539         sc = malloc(sizeof(struct oltr_softc), M_DEVBUF, M_NOWAIT | M_ZERO);
540         if (sc == NULL) {
541                 printf("oltr%d: no memory for softc struct!\n", unit);
542                 goto config_failed;
543         }
544         sc->unit = unit;
545         sc->state = OL_UNKNOWN;
546         ifp = &sc->arpcom.ac_if;
547
548         for (i = 0; i < sizeof(PCIConfigHeader); i++)
549                 PCIConfigHeader[i] = pci_cfgread(config_id, i, 1);
550
551         switch(TRlldPCIConfig(&LldDriver, &sc->config, PCIConfigHeader)) {
552         case TRLLD_PCICONFIG_OK:
553                 break;
554         case TRLLD_PCICONFIG_SET_COMMAND:
555                 printf("oltr%d: enabling bus master mode\n", unit);
556                 command = pci_conf_read(config_id, PCIR_COMMAND);
557                 pci_conf_write(config_id, PCIR_COMMAND, (command | PCIM_CMD_BUSMASTEREN));
558                 command = pci_conf_read(config_id, PCIR_COMMAND);
559                 if (!(command & PCIM_CMD_BUSMASTEREN)) {
560                         printf("oltr%d: failed to enable bus master mode\n", unit);
561                         goto config_failed;
562                 }
563                 break;
564         case TRLLD_PCICONFIG_FAIL:
565                 printf("oltr%d: TRlldPciConfig failed!\n", unit);
566                 goto config_failed;
567                 break;
568         case TRLLD_PCICONFIG_VERSION:
569                 printf("oltr%d: wrong LLD version\n", unit);
570                 goto config_failed;
571                 break;
572         }
573         printf("oltr%d: MAC address %6D\n", unit, sc->config.macaddress, ":");
574
575         scratch_size = TRlldAdapterSize();
576         if (bootverbose)
577                 printf("oltr%d: adapter memory block size %d bytes\n", unit, scratch_size);
578         sc->TRlldAdapter = (TRlldAdapter_t)malloc(scratch_size, M_DEVBUF, M_NOWAIT);
579         if (sc->TRlldAdapter == NULL) {
580                 printf("oltr%d: couldn't allocate scratch buffer (%d bytes)\n",unit, scratch_size);
581                 goto config_failed;
582         }
583
584         /*
585          * Allocate RX/TX Pools
586          */
587         for (i = 0; i < RING_BUFFER_LEN; i++) {
588                 sc->rx_ring[i].index = i;
589                 sc->rx_ring[i].data = (char *)malloc(RX_BUFFER_LEN, M_DEVBUF, M_NOWAIT);
590                 sc->rx_ring[i].address = vtophys(sc->rx_ring[i].data);
591                 sc->tx_ring[i].index = i;
592                 sc->tx_ring[i].data = (char *)malloc(TX_BUFFER_LEN, M_DEVBUF, M_NOWAIT);
593                 sc->tx_ring[i].address = vtophys(sc->tx_ring[i].data);
594                 if ((!sc->rx_ring[i].data) || (!sc->tx_ring[i].data)) {
595                         printf("oltr%d: unable to allocate ring buffers\n", unit);
596                         while (i > 0) {
597                                 if (sc->rx_ring[i].data)
598                                         free(sc->rx_ring[i].data, M_DEVBUF);
599                                 if (sc->tx_ring[i].data)
600                                         free(sc->tx_ring[i].data, M_DEVBUF);
601                                 i--;
602                         }
603                         goto config_failed;
604                 }
605         }
606         
607         /*
608          * Allocate interrupt and DMA channel
609          */
610         if (!pci_map_int(config_id, oltr_intr, sc, &net_imask)) {
611                 printf("oltr%d: couldn't setup interrupt\n", unit);
612                 goto config_failed;
613         }
614
615         /*
616          * Do the ifnet initialization
617          */
618         ifp->if_softc   = sc;
619         if_initname(ifp, "oltr", unit);
620         ifp->if_output  = iso88025_output;
621         ifp->if_init    = oltr_init;
622         ifp->if_start   = oltr_start;
623         ifp->if_ioctl   = oltr_ioctl;
624         ifp->if_flags   = IFF_BROADCAST;
625         bcopy(sc->config.macaddress, sc->arpcom.ac_enaddr, sizeof(sc->config.macaddress));
626
627         /*
628          * Do ifmedia setup.
629          */
630         ifmedia_init(&sc->ifmedia, 0, oltr_ifmedia_upd, oltr_ifmedia_sts);
631         rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_16MBPS);
632         switch(sc->config.type) {
633         case TRLLD_ADAPTER_PCI7:        /* OC-3540 */
634                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP100, 0, NULL);
635                 /* FALL THROUGH */
636         case TRLLD_ADAPTER_PCI4:        /* OC-3139 */
637         case TRLLD_ADAPTER_PCI5:        /* OC-3140 */
638         case TRLLD_ADAPTER_PCI6:        /* OC-3141 */
639                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_AUTO, 0, NULL);
640                 media = IFM_TOKEN|IFM_AUTO;
641                 rc = TRlldSetSpeed(sc->TRlldAdapter, 0);
642                 /* FALL THROUGH */
643         default:
644                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP4, 0, NULL);
645                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP16, 0, NULL);
646                 break;
647         }
648         sc->ifmedia.ifm_media = media;
649         ifmedia_set(&sc->ifmedia, media);
650
651         /*
652          * Attach the interface
653          */
654         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
655         iso88025_ifattach(ifp);
656
657         splx(s);
658         return;
659
660 config_failed:
661         (void)splx(s);
662
663         return;
664 }
665
666 #endif
667
668 static void
669 oltr_intr(void *xsc)
670 {
671         struct oltr_softc               *sc = (struct oltr_softc *)xsc;
672
673         if (DEBUG_MASK & DEBUG_INT)
674                 printf("I");
675
676         TRlldInterruptService(sc->TRlldAdapter);
677
678         return;
679 }
680
681 static void
682 oltr_start(struct ifnet *ifp)
683 {
684         struct oltr_softc       *sc = ifp->if_softc;
685         struct mbuf             *m0, *m;
686         int                     copy_len, buffer, frame, fragment, rc, s;
687         
688         /*
689          * Check to see if output is already active
690          */
691         if (ifp->if_flags & IFF_OACTIVE)
692                 return;
693
694 outloop:
695
696         /*
697          * Make sure we have buffers to transmit with
698          */
699         if (sc->tx_avail <= 0) {
700                 printf("oltr%d: tx queue full\n", sc->unit);
701                 ifp->if_flags |= IFF_OACTIVE;
702                 return;
703         }
704
705         if (sc->restart == NULL) {
706                 IF_DEQUEUE(&ifp->if_snd, m);
707                 if (m == NULL)
708                         return;
709         } else {
710                 m = sc->restart;
711                 sc->restart = NULL;
712         }
713
714         m0 = m;
715         frame = RING_BUFFER(sc->tx_frame);
716         buffer = RING_BUFFER(sc->tx_head);
717         fragment = 0;
718         copy_len = 0;
719         sc->frame_ring[frame].FragmentCount = 0;
720         
721         while (copy_len < m0->m_pkthdr.len) {
722                 sc->frame_ring[frame].FragmentCount++;
723                 if (sc->frame_ring[frame].FragmentCount > sc->tx_avail)
724                         goto nobuffers;
725                 sc->frame_ring[frame].TransmitFragment[fragment].VirtualAddress = sc->tx_ring[buffer].data;
726                 sc->frame_ring[frame].TransmitFragment[fragment].PhysicalAddress = sc->tx_ring[buffer].address;
727                 sc->frame_ring[frame].TransmitFragment[fragment].count = MIN(m0->m_pkthdr.len - copy_len, TX_BUFFER_LEN);
728                 m_copydata(m0, copy_len, MIN(m0->m_pkthdr.len - copy_len, TX_BUFFER_LEN), sc->tx_ring[buffer].data);
729                 copy_len += MIN(m0->m_pkthdr.len - copy_len, TX_BUFFER_LEN);
730                 fragment++;
731                 buffer = RING_BUFFER((buffer + 1));
732         }
733
734         s = splimp();
735         rc = TRlldTransmitFrame(sc->TRlldAdapter, &sc->frame_ring[frame], (void *)&sc->frame_ring[frame]);
736         (void)splx(s);
737
738         if (rc != TRLLD_TRANSMIT_OK) {
739                 printf("oltr%d: TRlldTransmitFrame returned %d\n", sc->unit, rc);
740                 ifp->if_oerrors++;
741                 goto bad;
742         }
743
744         sc->tx_avail -= sc->frame_ring[frame].FragmentCount;
745         sc->tx_head = RING_BUFFER((sc->tx_head + sc->frame_ring[frame].FragmentCount));
746         sc->tx_frame++;
747
748         BPF_MTAP(ifp, m0);
749         /*ifp->if_opackets++;*/
750
751 bad:
752         m_freem(m0);
753
754         goto outloop;
755
756 nobuffers:
757
758         printf("oltr%d: queue full\n", sc->unit);
759         ifp->if_flags |= IFF_OACTIVE;
760         ifp->if_oerrors++;
761         /*m_freem(m0);*/
762         sc->restart = m0;
763
764         return;
765 }
766
767 static void
768 oltr_close(struct oltr_softc *sc)
769 {
770         /*printf("oltr%d: oltr_close\n", sc->unit);*/
771
772         oltr_stop(sc);
773
774         tsleep(sc, 0, "oltrclose", 30*hz);
775 }
776
777 static void
778 oltr_stop(struct oltr_softc *sc)
779 {
780         struct ifnet            *ifp = &sc->arpcom.ac_if;
781
782         /*printf("oltr%d: oltr_stop\n", sc->unit);*/
783
784         ifp->if_flags &= ~(IFF_UP | IFF_RUNNING | IFF_OACTIVE);
785         TRlldClose(sc->TRlldAdapter, 0);
786         sc->state = OL_CLOSING;
787 }
788
789 static void
790 oltr_init(void * xsc)
791 {
792         struct oltr_softc       *sc = (struct oltr_softc *)xsc;
793         struct ifnet            *ifp = &sc->arpcom.ac_if;
794         struct ifmedia          *ifm = &sc->ifmedia;
795         int                     poll = 0, i, rc = 0, s;
796         int                     work_size;
797
798         /*
799          * Check adapter state, don't allow multiple inits
800          */
801         if (sc->state > OL_CLOSED) {
802                 printf("oltr%d: adapter not ready\n", sc->unit);
803                 return;
804         }
805
806         s = splimp();
807
808         /*
809          * Initialize Adapter
810          */
811         if ((rc = TRlldAdapterInit(&LldDriver, sc->TRlldAdapter, vtophys(sc->TRlldAdapter),
812             (void *)sc, &sc->config)) != TRLLD_INIT_OK) {
813                 switch(rc) {
814                 case TRLLD_INIT_NOT_FOUND:
815                         printf("oltr%d: adapter not found\n", sc->unit);
816                         break;
817                 case TRLLD_INIT_UNSUPPORTED:
818                         printf("oltr%d: adapter not supported by low level driver\n", sc->unit);
819                         break;
820                 case TRLLD_INIT_PHYS16:
821                         printf("oltr%d: adapter memory block above 16M cannot DMA\n", sc->unit);
822                         break;
823                 case TRLLD_INIT_VERSION:
824                         printf("oltr%d: low level driver version mismatch\n", sc->unit);
825                         break;
826                 default:
827                         printf("oltr%d: unknown init error %d\n", sc->unit, rc);
828                         break;
829                 }
830                 goto init_failed;
831         }
832         sc->state = OL_INIT;
833
834         switch(sc->config.type) {
835         case TRLLD_ADAPTER_PCI4:        /* OC-3139 */
836                 work_size = 32 * 1024;
837                 break;
838         case TRLLD_ADAPTER_PCI7:        /* OC-3540 */
839                 work_size = 256;
840                 break;
841         default:
842                 work_size = 0;
843         }
844
845         if (work_size) {
846                 if ((sc->work_memory = malloc(work_size, M_DEVBUF, M_NOWAIT)) == NULL) {
847                         printf("oltr%d: failed to allocate work memory (%d octets).\n", sc->unit, work_size);
848                 } else {
849                 TRlldAddMemory(sc->TRlldAdapter, sc->work_memory,
850                     vtophys(sc->work_memory), work_size);
851                 }
852         }
853
854         switch(IFM_SUBTYPE(ifm->ifm_media)) {
855         case IFM_AUTO:
856                 rc = TRlldSetSpeed(sc->TRlldAdapter, 0); /* TRLLD_SPEED_AUTO */
857                 break;
858         case IFM_TOK_UTP4:
859                 rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_4MBPS);
860                 break;
861         case IFM_TOK_UTP16:
862                 rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_16MBPS);
863                 break;
864         case IFM_TOK_UTP100:
865                 rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_100MBPS);
866                 break;
867         }
868
869         /*
870          * Download adapter micro-code
871          */
872         if (bootverbose)
873                 printf("oltr%d: Downloading adapter microcode: ", sc->unit);
874
875         switch(sc->config.mactype) {
876         case TRLLD_MAC_TMS:
877                 rc = TRlldDownload(sc->TRlldAdapter, TRlldMacCode);
878                 if (bootverbose)
879                         printf("TMS-380");
880                 break;
881         case TRLLD_MAC_HAWKEYE:
882                 rc = TRlldDownload(sc->TRlldAdapter, TRlldHawkeyeMac);
883                 if (bootverbose)
884                         printf("Hawkeye");
885                 break;
886         case TRLLD_MAC_BULLSEYE:
887                 rc = TRlldDownload(sc->TRlldAdapter, TRlldBullseyeMac);
888                 if (bootverbose)
889                         printf("Bullseye");
890                 break;
891         default:
892                 if (bootverbose)
893                         printf("unknown - failed!\n");
894                 goto init_failed;
895                 break;
896         }
897
898         /*
899          * Check download status
900          */
901         switch(rc) {
902         case TRLLD_DOWNLOAD_OK:
903                 if (bootverbose)
904                         printf(" - ok\n");
905                 break;
906         case TRLLD_DOWNLOAD_ERROR:
907                 if (bootverbose)
908                         printf(" - failed\n");
909                 else
910                         printf("oltr%d: adapter microcode download failed\n", sc->unit);
911                 goto init_failed;
912                 break;
913         case TRLLD_STATE:
914                 if (bootverbose)
915                         printf(" - not ready\n");
916                 goto init_failed;
917                 break;
918         }
919
920         /*
921          * Wait for self-test to complete
922          */
923         i = 0;
924         while ((poll++ < SELF_TEST_POLLS) && (sc->state < OL_READY)) {
925                 if (DEBUG_MASK & DEBUG_INIT)
926                         printf("p");
927                 DELAY(TRlldPoll(sc->TRlldAdapter) * 1000);
928                 if (TRlldInterruptService(sc->TRlldAdapter) != 0)
929                         if (DEBUG_MASK & DEBUG_INIT) printf("i");
930         }
931
932         if (sc->state != OL_CLOSED) {
933                 printf("oltr%d: self-test failed\n", sc->unit);
934                 goto init_failed;
935         }
936
937         /*
938          * Set up adapter poll
939          */
940         callout_init(&sc->oltr_poll_ch);
941         callout_reset(&sc->oltr_poll_ch, 1, oltr_poll, sc);
942
943         sc->state = OL_OPENING;
944
945         /*
946          * Open the adapter
947          */
948         rc = TRlldOpen(sc->TRlldAdapter, sc->arpcom.ac_enaddr, sc->GroupAddress,
949                 sc->FunctionalAddress, 1552, sc->AdapterMode);
950         switch(rc) {
951                 case TRLLD_OPEN_OK:
952                         break;
953                 case TRLLD_OPEN_STATE:
954                         printf("oltr%d: adapter not ready for open\n", sc->unit);
955                         (void)splx(s);
956                         return;
957                 case TRLLD_OPEN_ADDRESS_ERROR:
958                         printf("oltr%d: illegal MAC address\n", sc->unit);
959                         (void)splx(s);
960                         return;
961                 case TRLLD_OPEN_MODE_ERROR:
962                         printf("oltr%d: illegal open mode\n", sc->unit);
963                         (void)splx(s);
964                         return;
965                 default:
966                         printf("oltr%d: unknown open error (%d)\n", sc->unit, rc);
967                         (void)splx(s);
968                         return;
969         }
970
971         /*
972          * Set promiscious mode for now...
973          */
974         TRlldSetPromiscuousMode(sc->TRlldAdapter, TRLLD_PROM_LLC);
975         ifp->if_flags |= IFF_PROMISC;
976
977         /*
978          * Block on the ring insert and set a timeout
979          */
980         tsleep(sc, 0, "oltropen", 30*hz);
981
982         /*
983          * Set up receive buffer ring
984          */
985         for (i = 0; i < RING_BUFFER_LEN; i++) {
986                 rc = TRlldReceiveFragment(sc->TRlldAdapter, (void *)sc->rx_ring[i].data,
987                         sc->rx_ring[i].address, RX_BUFFER_LEN, (void *)sc->rx_ring[i].index);
988                 if (rc != TRLLD_RECEIVE_OK) {
989                         printf("oltr%d: adapter refused receive fragment %d (rc = %d)\n", sc->unit, i, rc);
990                         break;
991                 }       
992         }
993
994         sc->tx_avail = RING_BUFFER_LEN;
995         sc->tx_head = 0;
996         sc->tx_frame = 0;
997
998         sc->restart = NULL;
999
1000         ifp->if_flags |= IFF_RUNNING;
1001         ifp->if_flags &= ~IFF_OACTIVE;
1002
1003         /*
1004          * Set up adapter statistics poll
1005          */
1006         /*callout_handle_init(&sc->oltr_stat_ch);*/
1007         /*sc->oltr_stat_ch = timeout(oltr_stat, (void *)sc, 1*hz);*/
1008
1009         (void)splx(s);
1010         return;
1011
1012 init_failed:
1013         sc->state = OL_DEAD;
1014         (void)splx(s);
1015         return;
1016 }
1017
1018 static int
1019 oltr_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1020 {
1021         struct oltr_softc       *sc = ifp->if_softc;
1022         struct ifreq            *ifr = (struct ifreq *)data;
1023         int                     error = 0, s;
1024
1025         s = splimp();
1026
1027         switch(command) {
1028         case SIOCSIFADDR:
1029         case SIOCGIFADDR:
1030         case SIOCSIFMTU:
1031                 error = iso88025_ioctl(ifp, command, data);
1032                 break;
1033
1034         case SIOCSIFFLAGS:
1035                 if (ifp->if_flags & IFF_UP) {
1036                         oltr_init(sc);
1037                 } else {
1038                         if (ifp->if_flags & IFF_RUNNING) {
1039                                 oltr_close(sc);
1040                         }
1041                 }
1042                 break;
1043         case SIOCGIFMEDIA:
1044         case SIOCSIFMEDIA:
1045                 error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
1046                 break;
1047         default:
1048                 error = EINVAL;
1049                 break;
1050         }
1051
1052         (void)splx(s);  
1053
1054         return(error);
1055 }
1056
1057
1058 void
1059 oltr_poll(void *arg)
1060 {
1061         struct oltr_softc *sc = (struct oltr_softc *)arg;
1062         int s;
1063
1064         s = splimp();
1065
1066         if (DEBUG_MASK & DEBUG_POLL) printf("P");
1067
1068         /* Set up next adapter poll */
1069         callout_reset(&sc->oltr_poll_ch,
1070                       (TRlldPoll(sc->TRlldAdapter) * hz / 1000), oltr_poll, sc);
1071
1072         (void)splx(s);
1073 }
1074
1075 #ifdef NOTYET
1076 void
1077 oltr_stat(void *arg)
1078 {
1079         struct oltr_softc       *sc = (struct oltr_softc *)arg;
1080         int                     s;
1081
1082         s = splimp();
1083
1084         /* Set up next adapter poll */
1085         sc->oltr_stat_ch = timeout(oltr_stat, (void *)sc, 1*hz);
1086         if (TRlldGetStatistics(sc->TRlldAdapter, &sc->current, 0) != 0) {
1087                 /*printf("oltr%d: statistics available immediately...\n", sc->unit);*/
1088                 DriverStatistics((void *)sc, &sc->current);
1089         }
1090
1091         (void)splx(s);
1092 }
1093 #endif
1094 static int
1095 oltr_ifmedia_upd(struct ifnet *ifp)
1096 {
1097         struct oltr_softc       *sc = ifp->if_softc;
1098         struct ifmedia          *ifm = &sc->ifmedia;
1099         int                     rc;
1100
1101         if (IFM_TYPE(ifm->ifm_media) != IFM_TOKEN)
1102                 return(EINVAL);
1103
1104         switch(IFM_SUBTYPE(ifm->ifm_media)) {
1105         case IFM_AUTO:
1106                 rc = TRlldSetSpeed(sc->TRlldAdapter, 0); /* TRLLD_SPEED_AUTO */
1107                 break;
1108         case IFM_TOK_UTP4:
1109                 rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_4MBPS);
1110                 break;
1111         case IFM_TOK_UTP16:
1112                 rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_16MBPS);
1113                 break;
1114         case IFM_TOK_UTP100:
1115                 rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_100MBPS);
1116                 break;
1117         default:
1118                 return(EINVAL);
1119                 break;
1120         }
1121
1122         return(0);
1123
1124 }
1125
1126 static void
1127 oltr_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1128 {
1129         struct oltr_softc       *sc = ifp->if_softc;
1130         struct ifmedia          *ifm = &sc->ifmedia;
1131
1132         /*printf("oltr%d: oltr_ifmedia_sts\n", sc->unit);*/
1133
1134         ifmr->ifm_active = IFM_TYPE(ifm->ifm_media)|IFM_SUBTYPE(ifm->ifm_media);
1135
1136 }
1137
1138 /*
1139  * ---------------------- PMW Callback Functions -----------------------
1140  */
1141
1142 void
1143 DriverStatistics(void *DriverHandle, TRlldStatistics_t *statistics)
1144 {
1145 #ifdef NOTYET
1146         struct oltr_softc               *sc = (struct oltr_softc *)DriverHandle;
1147
1148         if (sc->statistics.LineErrors != statistics->LineErrors)
1149                 printf("oltr%d: Line Errors %lu\n", sc->unit,
1150                     statistics->LineErrors);
1151         if (sc->statistics.InternalErrors != statistics->InternalErrors)
1152                 printf("oltr%d: Internal Errors %lu\n", sc->unit,
1153                     statistics->InternalErrors);
1154         if (sc->statistics.BurstErrors != statistics->BurstErrors)
1155                 printf("oltr%d: Burst Errors %lu\n", sc->unit,
1156                     statistics->BurstErrors);
1157         if (sc->statistics.AbortDelimiters != statistics->AbortDelimiters)
1158                 printf("oltr%d: Abort Delimiters %lu\n", sc->unit,
1159                     statistics->AbortDelimiters);
1160         if (sc->statistics.ARIFCIErrors != statistics->ARIFCIErrors)
1161                 printf("oltr%d: ARIFCI Errors %lu\n", sc->unit,
1162                     statistics->ARIFCIErrors);
1163         if (sc->statistics.LostFrames != statistics->LostFrames)
1164                 printf("oltr%d: Lost Frames %lu\n", sc->unit,
1165                     statistics->LostFrames);
1166         if (sc->statistics.CongestionErrors != statistics->CongestionErrors)
1167                 printf("oltr%d: Congestion Errors %lu\n", sc->unit,
1168                     statistics->CongestionErrors);
1169         if (sc->statistics.FrequencyErrors != statistics->FrequencyErrors)
1170                 printf("oltr%d: Frequency Errors %lu\n", sc->unit,
1171                     statistics->FrequencyErrors);
1172         if (sc->statistics.TokenErrors != statistics->TokenErrors)
1173                 printf("oltr%d: Token Errors %lu\n", sc->unit,
1174                     statistics->TokenErrors);
1175         if (sc->statistics.DMABusErrors != statistics->DMABusErrors)
1176                 printf("oltr%d: DMA Bus Errors %lu\n", sc->unit,
1177                     statistics->DMABusErrors);
1178         if (sc->statistics.DMAParityErrors != statistics->DMAParityErrors)
1179                 printf("oltr%d: DMA Parity Errors %lu\n", sc->unit,
1180                     statistics->DMAParityErrors);
1181         if (sc->statistics.ReceiveLongFrame != statistics->ReceiveLongFrame)
1182                 printf("oltr%d: Long frames received %lu\n", sc->unit,
1183                     statistics->ReceiveLongFrame);
1184         if (sc->statistics.ReceiveCRCErrors != statistics->ReceiveCRCErrors)
1185                 printf("oltr%d: Receive CRC Errors %lu\n", sc->unit,
1186                     statistics->ReceiveCRCErrors);
1187         if (sc->statistics.ReceiveOverflow != statistics->ReceiveOverflow)
1188                 printf("oltr%d: Recieve overflows %lu\n", sc->unit,
1189                     statistics->ReceiveOverflow);
1190         if (sc->statistics.TransmitUnderrun != statistics->TransmitUnderrun)
1191                 printf("oltr%d: Frequency Errors %lu\n", sc->unit,
1192                     statistics->TransmitUnderrun);
1193         bcopy(statistics, &sc->statistics, sizeof(TRlldStatistics_t));
1194 #endif
1195 }
1196
1197 static void
1198 DriverSuspend(unsigned short MicroSeconds)
1199 {
1200     DELAY(MicroSeconds);
1201 }
1202
1203
1204 static void
1205 DriverStatus(void *DriverHandle, TRlldStatus_t *Status)
1206 {
1207         struct oltr_softc       *sc = (struct oltr_softc *)DriverHandle;
1208         struct ifnet            *ifp = &sc->arpcom.ac_if;
1209
1210         char *Protocol[] = { /* 0 */ "Unknown",
1211                              /* 1 */ "TKP",
1212                              /* 2 */ "TXI" };
1213         char *Timeout[]  = { /* 0 */ "command",
1214                              /* 1 */ "transmit",
1215                              /* 2 */ "interrupt" };
1216         
1217         switch (Status->Type) {
1218
1219         case TRLLD_STS_ON_WIRE:
1220                 printf("oltr%d: ring insert (%d Mbps - %s)\n", sc->unit,
1221                     Status->Specification.OnWireInformation.Speed,
1222                     Protocol[Status->Specification.OnWireInformation.AccessProtocol]);
1223                 sc->state = OL_OPEN;
1224                 wakeup(sc);
1225                 break;
1226         case TRLLD_STS_SELFTEST_STATUS:
1227                 if (Status->Specification.SelftestStatus == TRLLD_ST_OK) {
1228                         sc->state = OL_CLOSED;
1229                         if (bootverbose)
1230                                 printf("oltr%d: self test complete\n", sc->unit);
1231                 }
1232                 if (Status->Specification.SelftestStatus & TRLLD_ST_ERROR) {
1233                         printf("oltr%d: Adapter self test error %d", sc->unit,
1234                         Status->Specification.SelftestStatus & ~TRLLD_ST_ERROR);
1235                         sc->state = OL_DEAD;
1236                 }
1237                 if (Status->Specification.SelftestStatus & TRLLD_ST_TIMEOUT) {
1238                         printf("oltr%d: Adapter self test timed out.\n", sc->unit);
1239                         sc->state = OL_DEAD;
1240                 }
1241                 break;
1242         case TRLLD_STS_INIT_STATUS:
1243                 if (Status->Specification.InitStatus == 0x800) {
1244                         oltr_stop(sc);
1245                         ifmedia_set(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP16);
1246                         TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_16MBPS);
1247                         oltr_init(sc);
1248                         break;
1249                 }
1250                 printf("oltr%d: adapter init failure 0x%03x\n", sc->unit,
1251                     Status->Specification.InitStatus);
1252                 oltr_stop(sc);
1253                 break;
1254         case TRLLD_STS_RING_STATUS:
1255                 if (Status->Specification.RingStatus) {
1256                         printf("oltr%d: Ring status change: ", sc->unit);
1257                         if (Status->Specification.RingStatus &
1258                             TRLLD_RS_SIGNAL_LOSS)
1259                                 printf(" [Signal Loss]");
1260                         if (Status->Specification.RingStatus &
1261                             TRLLD_RS_HARD_ERROR)
1262                                 printf(" [Hard Error]");
1263                         if (Status->Specification.RingStatus &
1264                             TRLLD_RS_SOFT_ERROR)
1265                                 printf(" [Soft Error]");
1266                         if (Status->Specification.RingStatus &
1267                             TRLLD_RS_TRANSMIT_BEACON)
1268                                 printf(" [Beacon]");
1269                         if (Status->Specification.RingStatus &
1270                             TRLLD_RS_LOBE_WIRE_FAULT)
1271                                 printf(" [Wire Fault]");
1272                         if (Status->Specification.RingStatus &
1273                             TRLLD_RS_AUTO_REMOVAL_ERROR)
1274                                 printf(" [Auto Removal]");
1275                         if (Status->Specification.RingStatus &
1276                             TRLLD_RS_REMOVE_RECEIVED)
1277                                 printf(" [Remove Received]");
1278                         if (Status->Specification.RingStatus &
1279                             TRLLD_RS_COUNTER_OVERFLOW)
1280                                 printf(" [Counter Overflow]");
1281                         if (Status->Specification.RingStatus &
1282                             TRLLD_RS_SINGLE_STATION)
1283                                 printf(" [Single Station]");
1284                         if (Status->Specification.RingStatus &
1285                                 TRLLD_RS_RING_RECOVERY)
1286                                 printf(" [Ring Recovery]");
1287                         printf("\n");   
1288                 }
1289                 break;
1290         case TRLLD_STS_ADAPTER_CHECK:
1291                 printf("oltr%d: adapter check (%04x %04x %04x %04x)\n", sc->unit,
1292                     Status->Specification.AdapterCheck[0],
1293                     Status->Specification.AdapterCheck[1],
1294                     Status->Specification.AdapterCheck[2],
1295                     Status->Specification.AdapterCheck[3]);
1296                 sc->state = OL_DEAD;
1297                 oltr_stop(sc);
1298                 break;
1299         case TRLLD_STS_PROMISCUOUS_STOPPED:
1300                 printf("oltr%d: promiscuous mode ", sc->unit);
1301                 if (Status->Specification.PromRemovedCause == 1)
1302                         printf("remove received.");
1303                 if (Status->Specification.PromRemovedCause == 2)
1304                         printf("poll failure.");
1305                 if (Status->Specification.PromRemovedCause == 2)
1306                         printf("buffer size failure.");
1307                 printf("\n");
1308                 ifp->if_flags &= ~IFF_PROMISC;
1309                 break;
1310         case TRLLD_STS_LLD_ERROR:
1311                 printf("oltr%d: low level driver internal error ", sc->unit);
1312                 printf("(%04x %04x %04x %04x).\n",
1313                     Status->Specification.InternalError[0],
1314                     Status->Specification.InternalError[1],
1315                     Status->Specification.InternalError[2],
1316                     Status->Specification.InternalError[3]);
1317                 sc->state = OL_DEAD;
1318                 oltr_stop(sc);
1319                 break;
1320         case TRLLD_STS_ADAPTER_TIMEOUT:
1321                 printf("oltr%d: adapter %s timeout.\n", sc->unit,
1322                     Timeout[Status->Specification.AdapterTimeout]);
1323                 break;
1324         default:
1325                 printf("oltr%d: driver status Type = %d\n", sc->unit, Status->Type);
1326                 break;
1327
1328         }
1329         if (Status->Closed) {
1330                 sc->state = OL_CLOSING;
1331                 oltr_stop(sc);
1332         }
1333
1334 }
1335
1336 static void
1337 DriverCloseCompleted(void *DriverHandle)
1338 {
1339         struct oltr_softc               *sc = (struct oltr_softc *)DriverHandle;
1340         
1341         printf("oltr%d: adapter closed\n", sc->unit);
1342         wakeup(sc);
1343         sc->state = OL_CLOSED;
1344 }
1345
1346 static void
1347 DriverTransmitFrameCompleted(void *DriverHandle, void *FrameHandle, int TransmitStatus)
1348 {
1349         struct oltr_softc       *sc = (struct oltr_softc *)DriverHandle;
1350         struct ifnet            *ifp = &sc->arpcom.ac_if;
1351         TRlldTransmit_t         *frame = (TRlldTransmit_t *)FrameHandle;
1352         
1353         /*printf("oltr%d: DriverTransmitFrameCompleted\n", sc->unit);*/
1354
1355         if (TransmitStatus != TRLLD_TRANSMIT_OK) {
1356                 ifp->if_oerrors++;
1357                 printf("oltr%d: transmit error %d\n", sc->unit, TransmitStatus);
1358         } else {
1359                 ifp->if_opackets++;
1360         }
1361         
1362         sc->tx_avail += frame->FragmentCount;
1363
1364         if (ifp->if_flags & IFF_OACTIVE) {
1365                 printf("oltr%d: queue restart\n", sc->unit);
1366                 ifp->if_flags &= ~IFF_OACTIVE;
1367                 oltr_start(ifp);
1368         }
1369
1370
1371 }
1372
1373 static void
1374 DriverReceiveFrameCompleted(void *DriverHandle, int ByteCount, int FragmentCount, void *FragmentHandle, int ReceiveStatus)
1375 {
1376         struct oltr_softc       *sc = (struct oltr_softc *)DriverHandle;
1377         struct ifnet            *ifp = (struct ifnet *)&sc->arpcom.ac_if;
1378         struct mbuf             *m0, *m1, *m;
1379         struct iso88025_header  *th;
1380         int                     frame_len = ByteCount, i = (int)FragmentHandle, rc, s;
1381         int                     mbuf_offset, mbuf_size, frag_offset, copy_length;
1382         char                    *fragment = sc->rx_ring[RING_BUFFER(i)].data;
1383         
1384         if (sc->state > OL_CLOSED) {
1385                 if (ReceiveStatus == TRLLD_RCV_OK) {
1386                         MGETHDR(m0, MB_DONTWAIT, MT_DATA);
1387                         mbuf_size = MHLEN - 2;
1388                         if (!m0) {
1389                                 ifp->if_ierrors++;
1390                                 goto dropped;
1391                         }
1392                         if (ByteCount + 2 > MHLEN) {
1393                                 MCLGET(m0, MB_DONTWAIT);
1394                                 mbuf_size = MCLBYTES - 2;
1395                                 if (!(m0->m_flags & M_EXT)) {
1396                                         m_freem(m0);
1397                                         ifp->if_ierrors++;
1398                                         goto dropped;
1399                                 }
1400                         }
1401                         m0->m_pkthdr.rcvif = ifp;
1402                         m0->m_pkthdr.len = ByteCount;
1403                         m0->m_len = 0;
1404                         m0->m_data += 2;
1405                         th = mtod(m0, struct iso88025_header *);
1406                         m0->m_pkthdr.header = (void *)th;
1407
1408                         m = m0;
1409                         mbuf_offset = 0;
1410                         frag_offset = 0;
1411                         while (frame_len) {
1412                                 copy_length = MIN3(frame_len,
1413                                     (RX_BUFFER_LEN - frag_offset),
1414                                     (mbuf_size - mbuf_offset));
1415                                 bcopy(fragment + frag_offset, mtod(m, char *) +
1416                                     mbuf_offset, copy_length);
1417                                 m->m_len += copy_length;
1418                                 mbuf_offset += copy_length;
1419                                 frag_offset += copy_length;
1420                                 frame_len -= copy_length;
1421                         
1422                                 if (frag_offset == RX_BUFFER_LEN) {
1423                                         fragment =
1424                                             sc->rx_ring[RING_BUFFER(++i)].data;
1425                                         frag_offset = 0;
1426                                 }
1427                                 if ((mbuf_offset == mbuf_size) && (frame_len > 0)) {
1428                                         MGET(m1, MB_DONTWAIT, MT_DATA);
1429                                         mbuf_size = MHLEN;
1430                                         if (!m1) {
1431                                                 ifp->if_ierrors++;
1432                                                 m_freem(m0);
1433                                                 goto dropped;
1434                                         }
1435                                         if (frame_len > MHLEN) {
1436                                                 MCLGET(m1, MB_DONTWAIT);
1437                                                 mbuf_size = MCLBYTES;
1438                                                 if (!(m1->m_flags & M_EXT)) {
1439                                                         m_freem(m0);
1440                                                         m_freem(m1);
1441                                                         ifp->if_ierrors++;
1442                                                         goto dropped;
1443                                                 }
1444                                         }
1445                                         m->m_next = m1;
1446                                         m = m1;
1447                                         mbuf_offset = 0;
1448                                         m->m_len = 0;
1449                                 }
1450                         }
1451                         BPF_MTAP(ifp, m0);
1452
1453                         /*if (ifp->if_flags & IFF_PROMISC) {*/
1454                                 if (bcmp(th->iso88025_dhost, ifp->if_broadcastaddr,
1455                                          ifp->if_addrlen)) {
1456                                         if ((bcmp(th->iso88025_dhost + 1, sc->arpcom.ac_enaddr + 1, ISO88025_ADDR_LEN - 1)) ||
1457                                             ((th->iso88025_dhost[0] & 0x7f) != sc->arpcom.ac_enaddr[0])) {
1458                                                 m_freem(m0);
1459                                                 goto dropped;
1460                                         }
1461                                 }
1462                         /*}*/
1463                         ifp->if_ipackets++;
1464
1465                         (*ifp->if_input)(ifp, m0);
1466
1467                 } else {        /* Receiver error */
1468                         if (ReceiveStatus != TRLLD_RCV_NO_DATA) {
1469                                 printf("oltr%d: receive error %d\n", sc->unit,
1470                                     ReceiveStatus);
1471                                 ifp->if_ierrors++;
1472                         }
1473                 }
1474
1475 dropped:
1476                 s = splimp();
1477                 i = (int)FragmentHandle;
1478                 while (FragmentCount--) {
1479                         rc = TRlldReceiveFragment(sc->TRlldAdapter,
1480                             (void *)sc->rx_ring[RING_BUFFER(i)].data,
1481                             sc->rx_ring[RING_BUFFER(i)].address,
1482                             RX_BUFFER_LEN, (void *)sc->rx_ring[RING_BUFFER(i)].index);
1483                         if (rc != TRLLD_RECEIVE_OK) {
1484                                 printf("oltr%d: adapter refused receive fragment %d (rc = %d)\n", sc->unit, i, rc);
1485                                 break;
1486                         }
1487                         i++;
1488                 }
1489                 (void)splx(s);
1490         }
1491 }
1492
1493
1494 /*
1495  * ---------------------------- PMW Glue -------------------------------
1496  */
1497
1498 #ifndef TRlldInlineIO
1499
1500 static void
1501 DriverOutByte(unsigned short IOAddress, unsigned char value)
1502 {
1503         outb(IOAddress, value);
1504 }
1505
1506 static void
1507 DriverOutWord(unsigned short IOAddress, unsigned short value)
1508 {
1509         outw(IOAddress, value);
1510 }
1511
1512 static void
1513 DriverOutDword(unsigned short IOAddress, unsigned long value)
1514 {
1515         outl(IOAddress, value);
1516 }
1517
1518 static void
1519 DriverRepOutByte(unsigned short IOAddress, unsigned char *DataPointer, int ByteCount)
1520 {
1521         outsb(IOAddress, (void *)DataPointer, ByteCount);
1522 }
1523
1524 static void
1525 DriverRepOutWord(unsigned short IOAddress, unsigned short *DataPointer, int WordCount)
1526 {
1527         outsw(IOAddress, (void *)DataPointer, WordCount);
1528 }
1529
1530 static void
1531 DriverRepOutDword(unsigned short IOAddress, unsigned long *DataPointer, int DWordCount)
1532 {
1533         outsl(IOAddress, (void *)DataPointer, DWordCount);
1534 }
1535
1536 static unsigned char
1537 DriverInByte(unsigned short IOAddress)
1538 {
1539         return(inb(IOAddress));
1540 }
1541
1542 static unsigned short
1543 DriverInWord(unsigned short IOAddress)
1544 {
1545         return(inw(IOAddress));
1546 }
1547
1548 static unsigned long
1549 DriverInDword(unsigned short IOAddress)
1550 {
1551         return(inl(IOAddress));
1552 }
1553
1554 static void
1555 DriverRepInByte(unsigned short IOAddress, unsigned char *DataPointer, int ByteCount)
1556 {
1557         insb(IOAddress, (void *)DataPointer, ByteCount);
1558 }
1559
1560 static void
1561 DriverRepInWord(unsigned short IOAddress, unsigned short *DataPointer, int WordCount)
1562 {
1563         insw(IOAddress, (void *)DataPointer, WordCount);
1564 }
1565 static void
1566 DriverRepInDword( unsigned short IOAddress, unsigned long *DataPointer, int DWordCount)
1567 {
1568         insl(IOAddress, (void *)DataPointer, DWordCount);
1569 }
1570 #endif /* TRlldInlineIO */