Merge from vendor branch GDB:
[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.15 2004/09/16 23:34:19 joerg 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, &sc->oltr_intrhand)) {
373                 device_printf(dev, "couldn't setup interrupt\n");
374                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->oltr_irq);
375                 goto config_failed;
376         }
377
378         /*
379          * Do the ifnet initialization
380          */
381         ifp->if_softc   = sc;
382         if_initname(ifp, "oltr", device_get_unit(dev));
383         ifp->if_init    = oltr_init;
384         ifp->if_start   = oltr_start;
385         ifp->if_ioctl   = oltr_ioctl;
386         ifp->if_flags   = IFF_BROADCAST;
387         bcopy(sc->config.macaddress, sc->arpcom.ac_enaddr, sizeof(sc->config.macaddress));
388
389         /*
390          * Do ifmedia setup.
391          */
392         ifmedia_init(&sc->ifmedia, 0, oltr_ifmedia_upd, oltr_ifmedia_sts);
393         rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_16MBPS);
394         switch(sc->config.type) {
395         case TRLLD_ADAPTER_PCI7:        /* OC-3540 */
396                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP100, 0, NULL);
397                 /* FALL THROUGH */
398         case TRLLD_ADAPTER_PCI4:        /* OC-3139 */
399         case TRLLD_ADAPTER_PCI5:        /* OC-3140 */
400         case TRLLD_ADAPTER_PCI6:        /* OC-3141 */
401                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_AUTO, 0, NULL);
402                 media = IFM_TOKEN|IFM_AUTO;
403                 rc = TRlldSetSpeed(sc->TRlldAdapter, 0);
404                 /* FALL THROUGH */
405         default:
406                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP4, 0, NULL);
407                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP16, 0, NULL);
408                 break;
409         }
410         sc->ifmedia.ifm_media = media;
411         ifmedia_set(&sc->ifmedia, media);
412
413         /*
414          * Attach the interface
415          */
416         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
417         iso88025_ifattach(ifp);
418
419         splx(s);
420         return(0);
421
422 config_failed:
423
424         splx(s);
425         return(ENXIO);
426 }
427
428 static int
429 oltr_pci_detach(device_t dev)
430 {
431         struct oltr_softc       *sc = device_get_softc(dev);
432         struct ifnet            *ifp = &sc->arpcom.ac_if;
433         int s, i;
434
435         device_printf(dev, "driver unloading\n");
436
437         s = splimp();
438
439         if_detach(ifp);
440         if (sc->state > OL_CLOSED)
441                 oltr_stop(sc);
442
443         callout_stop(&sc->oltr_poll_ch);
444         /*untimeout(oltr_stat, (void *)sc, sc->oltr_stat_ch);*/
445
446         bus_teardown_intr(dev, sc->oltr_irq, sc->oltr_intrhand);
447         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->oltr_irq);
448
449         /* Deallocate all dynamic memory regions */
450         for (i = 0; i < RING_BUFFER_LEN; i++) {
451                 free(sc->rx_ring[i].data, M_DEVBUF);
452                 free(sc->tx_ring[i].data, M_DEVBUF);
453         }
454         if (sc->work_memory)
455                 free(sc->work_memory, M_DEVBUF);
456         free(sc->TRlldAdapter, M_DEVBUF);
457
458         (void)splx(s);
459
460         return(0);
461 }
462
463 static void
464 oltr_pci_shutdown(device_t dev)
465 {
466         struct oltr_softc               *sc = device_get_softc(dev);
467
468         device_printf(dev, "oltr_pci_shutdown called\n");
469
470         if (sc->state > OL_CLOSED)
471                 oltr_stop(sc);
472
473         return;
474 }
475
476 #else
477
478 static const char *oltr_pci_probe       (pcici_t, pcidi_t);
479 static void oltr_pci_attach             (pcici_t, int);
480
481 static unsigned long oltr_count = 0;
482
483 static struct pci_device oltr_device = {
484         "oltr",
485         oltr_pci_probe,
486         oltr_pci_attach,
487         &oltr_count,
488         NULL
489 };
490
491 DATA_SET(pcidevice_set, oltr_device);
492
493 static const char *
494 oltr_pci_probe(pcici_t config_id, pcidi_t device_id)
495 {
496         int                     i, rc;
497         char                    PCIConfigHeader[64];
498         TRlldAdapterConfig_t    config;
499         
500         if (((device_id & 0xffff) == PCI_VENDOR_OLICOM) && (
501             (((device_id >> 16) & 0xffff) == 0x0001) ||
502             (((device_id >> 16) & 0xffff) == 0x0004) ||
503             (((device_id >> 16) & 0xffff) == 0x0005) ||
504             (((device_id >> 16) & 0xffff) == 0x0007) ||
505             (((device_id >> 16) & 0xffff) == 0x0008))) {
506         
507                 for (i = 0; i < 64; i++)
508                         PCIConfigHeader[i] = pci_cfgread(config_id, i, /* bytes */ 1);
509
510                 rc = TRlldPCIConfig(&LldDriver, &config, PCIConfigHeader);
511
512                 if (rc == TRLLD_PCICONFIG_FAIL) {
513                         printf("oltr: TRlldPciConfig failed!\n");
514                         return(NULL);
515                 }
516                 if (rc == TRLLD_PCICONFIG_VERSION) {
517                         printf("oltr: wrong LLD version.\n");
518                         return(NULL);
519                 }
520                 return(AdapterName[config.type]);
521         }
522
523         return(NULL);
524 }
525
526 static void
527 oltr_pci_attach(pcici_t config_id, int unit)
528 {
529         int                     i, s, rc = 0, scratch_size;
530         int                     media = IFM_TOKEN|IFM_TOK_UTP16;
531         u_long                  command;
532         char                    PCIConfigHeader[64];
533         struct oltr_softc               *sc;
534         struct ifnet            *ifp; /* = &sc->arpcom.ac_if; */
535
536         s = splimp();
537
538         sc = malloc(sizeof(struct oltr_softc), M_DEVBUF, M_NOWAIT | M_ZERO);
539         if (sc == NULL) {
540                 printf("oltr%d: no memory for softc struct!\n", unit);
541                 goto config_failed;
542         }
543         sc->unit = unit;
544         sc->state = OL_UNKNOWN;
545         ifp = &sc->arpcom.ac_if;
546
547         for (i = 0; i < sizeof(PCIConfigHeader); i++)
548                 PCIConfigHeader[i] = pci_cfgread(config_id, i, 1);
549
550         switch(TRlldPCIConfig(&LldDriver, &sc->config, PCIConfigHeader)) {
551         case TRLLD_PCICONFIG_OK:
552                 break;
553         case TRLLD_PCICONFIG_SET_COMMAND:
554                 printf("oltr%d: enabling bus master mode\n", unit);
555                 command = pci_conf_read(config_id, PCIR_COMMAND);
556                 pci_conf_write(config_id, PCIR_COMMAND, (command | PCIM_CMD_BUSMASTEREN));
557                 command = pci_conf_read(config_id, PCIR_COMMAND);
558                 if (!(command & PCIM_CMD_BUSMASTEREN)) {
559                         printf("oltr%d: failed to enable bus master mode\n", unit);
560                         goto config_failed;
561                 }
562                 break;
563         case TRLLD_PCICONFIG_FAIL:
564                 printf("oltr%d: TRlldPciConfig failed!\n", unit);
565                 goto config_failed;
566                 break;
567         case TRLLD_PCICONFIG_VERSION:
568                 printf("oltr%d: wrong LLD version\n", unit);
569                 goto config_failed;
570                 break;
571         }
572         printf("oltr%d: MAC address %6D\n", unit, sc->config.macaddress, ":");
573
574         scratch_size = TRlldAdapterSize();
575         if (bootverbose)
576                 printf("oltr%d: adapter memory block size %d bytes\n", unit, scratch_size);
577         sc->TRlldAdapter = (TRlldAdapter_t)malloc(scratch_size, M_DEVBUF, M_NOWAIT);
578         if (sc->TRlldAdapter == NULL) {
579                 printf("oltr%d: couldn't allocate scratch buffer (%d bytes)\n",unit, scratch_size);
580                 goto config_failed;
581         }
582
583         /*
584          * Allocate RX/TX Pools
585          */
586         for (i = 0; i < RING_BUFFER_LEN; i++) {
587                 sc->rx_ring[i].index = i;
588                 sc->rx_ring[i].data = (char *)malloc(RX_BUFFER_LEN, M_DEVBUF, M_NOWAIT);
589                 sc->rx_ring[i].address = vtophys(sc->rx_ring[i].data);
590                 sc->tx_ring[i].index = i;
591                 sc->tx_ring[i].data = (char *)malloc(TX_BUFFER_LEN, M_DEVBUF, M_NOWAIT);
592                 sc->tx_ring[i].address = vtophys(sc->tx_ring[i].data);
593                 if ((!sc->rx_ring[i].data) || (!sc->tx_ring[i].data)) {
594                         printf("oltr%d: unable to allocate ring buffers\n", unit);
595                         while (i > 0) {
596                                 if (sc->rx_ring[i].data)
597                                         free(sc->rx_ring[i].data, M_DEVBUF);
598                                 if (sc->tx_ring[i].data)
599                                         free(sc->tx_ring[i].data, M_DEVBUF);
600                                 i--;
601                         }
602                         goto config_failed;
603                 }
604         }
605         
606         /*
607          * Allocate interrupt and DMA channel
608          */
609         if (!pci_map_int(config_id, oltr_intr, sc, &net_imask)) {
610                 printf("oltr%d: couldn't setup interrupt\n", unit);
611                 goto config_failed;
612         }
613
614         /*
615          * Do the ifnet initialization
616          */
617         ifp->if_softc   = sc;
618         if_initname(ifp, "oltr", unit);
619         ifp->if_output  = iso88025_output;
620         ifp->if_init    = oltr_init;
621         ifp->if_start   = oltr_start;
622         ifp->if_ioctl   = oltr_ioctl;
623         ifp->if_flags   = IFF_BROADCAST;
624         bcopy(sc->config.macaddress, sc->arpcom.ac_enaddr, sizeof(sc->config.macaddress));
625
626         /*
627          * Do ifmedia setup.
628          */
629         ifmedia_init(&sc->ifmedia, 0, oltr_ifmedia_upd, oltr_ifmedia_sts);
630         rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_16MBPS);
631         switch(sc->config.type) {
632         case TRLLD_ADAPTER_PCI7:        /* OC-3540 */
633                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP100, 0, NULL);
634                 /* FALL THROUGH */
635         case TRLLD_ADAPTER_PCI4:        /* OC-3139 */
636         case TRLLD_ADAPTER_PCI5:        /* OC-3140 */
637         case TRLLD_ADAPTER_PCI6:        /* OC-3141 */
638                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_AUTO, 0, NULL);
639                 media = IFM_TOKEN|IFM_AUTO;
640                 rc = TRlldSetSpeed(sc->TRlldAdapter, 0);
641                 /* FALL THROUGH */
642         default:
643                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP4, 0, NULL);
644                 ifmedia_add(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP16, 0, NULL);
645                 break;
646         }
647         sc->ifmedia.ifm_media = media;
648         ifmedia_set(&sc->ifmedia, media);
649
650         /*
651          * Attach the interface
652          */
653         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
654         iso88025_ifattach(ifp);
655
656         splx(s);
657         return;
658
659 config_failed:
660         (void)splx(s);
661
662         return;
663 }
664
665 #endif
666
667 static void
668 oltr_intr(void *xsc)
669 {
670         struct oltr_softc               *sc = (struct oltr_softc *)xsc;
671
672         if (DEBUG_MASK & DEBUG_INT)
673                 printf("I");
674
675         TRlldInterruptService(sc->TRlldAdapter);
676
677         return;
678 }
679
680 static void
681 oltr_start(struct ifnet *ifp)
682 {
683         struct oltr_softc       *sc = ifp->if_softc;
684         struct mbuf             *m0, *m;
685         int                     copy_len, buffer, frame, fragment, rc, s;
686         
687         /*
688          * Check to see if output is already active
689          */
690         if (ifp->if_flags & IFF_OACTIVE)
691                 return;
692
693 outloop:
694
695         /*
696          * Make sure we have buffers to transmit with
697          */
698         if (sc->tx_avail <= 0) {
699                 printf("oltr%d: tx queue full\n", sc->unit);
700                 ifp->if_flags |= IFF_OACTIVE;
701                 return;
702         }
703
704         if (sc->restart == NULL) {
705                 IF_DEQUEUE(&ifp->if_snd, m);
706                 if (m == NULL)
707                         return;
708         } else {
709                 m = sc->restart;
710                 sc->restart = NULL;
711         }
712
713         m0 = m;
714         frame = RING_BUFFER(sc->tx_frame);
715         buffer = RING_BUFFER(sc->tx_head);
716         fragment = 0;
717         copy_len = 0;
718         sc->frame_ring[frame].FragmentCount = 0;
719         
720         while (copy_len < m0->m_pkthdr.len) {
721                 sc->frame_ring[frame].FragmentCount++;
722                 if (sc->frame_ring[frame].FragmentCount > sc->tx_avail)
723                         goto nobuffers;
724                 sc->frame_ring[frame].TransmitFragment[fragment].VirtualAddress = sc->tx_ring[buffer].data;
725                 sc->frame_ring[frame].TransmitFragment[fragment].PhysicalAddress = sc->tx_ring[buffer].address;
726                 sc->frame_ring[frame].TransmitFragment[fragment].count = MIN(m0->m_pkthdr.len - copy_len, TX_BUFFER_LEN);
727                 m_copydata(m0, copy_len, MIN(m0->m_pkthdr.len - copy_len, TX_BUFFER_LEN), sc->tx_ring[buffer].data);
728                 copy_len += MIN(m0->m_pkthdr.len - copy_len, TX_BUFFER_LEN);
729                 fragment++;
730                 buffer = RING_BUFFER((buffer + 1));
731         }
732
733         s = splimp();
734         rc = TRlldTransmitFrame(sc->TRlldAdapter, &sc->frame_ring[frame], (void *)&sc->frame_ring[frame]);
735         (void)splx(s);
736
737         if (rc != TRLLD_TRANSMIT_OK) {
738                 printf("oltr%d: TRlldTransmitFrame returned %d\n", sc->unit, rc);
739                 ifp->if_oerrors++;
740                 goto bad;
741         }
742
743         sc->tx_avail -= sc->frame_ring[frame].FragmentCount;
744         sc->tx_head = RING_BUFFER((sc->tx_head + sc->frame_ring[frame].FragmentCount));
745         sc->tx_frame++;
746
747 #if (NBPFILTER > 0) || defined(__DragonFly__) || (__FreeBSD_version > 400000)
748         if (ifp->if_bpf)
749                 bpf_mtap(ifp, m0);
750 #endif
751         /*ifp->if_opackets++;*/
752
753 bad:
754         m_freem(m0);
755
756         goto outloop;
757
758 nobuffers:
759
760         printf("oltr%d: queue full\n", sc->unit);
761         ifp->if_flags |= IFF_OACTIVE;
762         ifp->if_oerrors++;
763         /*m_freem(m0);*/
764         sc->restart = m0;
765
766         return;
767 }
768
769 static void
770 oltr_close(struct oltr_softc *sc)
771 {
772         /*printf("oltr%d: oltr_close\n", sc->unit);*/
773
774         oltr_stop(sc);
775
776         tsleep(sc, 0, "oltrclose", 30*hz);
777 }
778
779 static void
780 oltr_stop(struct oltr_softc *sc)
781 {
782         struct ifnet            *ifp = &sc->arpcom.ac_if;
783
784         /*printf("oltr%d: oltr_stop\n", sc->unit);*/
785
786         ifp->if_flags &= ~(IFF_UP | IFF_RUNNING | IFF_OACTIVE);
787         TRlldClose(sc->TRlldAdapter, 0);
788         sc->state = OL_CLOSING;
789 }
790
791 static void
792 oltr_init(void * xsc)
793 {
794         struct oltr_softc       *sc = (struct oltr_softc *)xsc;
795         struct ifnet            *ifp = &sc->arpcom.ac_if;
796         struct ifmedia          *ifm = &sc->ifmedia;
797         int                     poll = 0, i, rc = 0, s;
798         int                     work_size;
799
800         /*
801          * Check adapter state, don't allow multiple inits
802          */
803         if (sc->state > OL_CLOSED) {
804                 printf("oltr%d: adapter not ready\n", sc->unit);
805                 return;
806         }
807
808         s = splimp();
809
810         /*
811          * Initialize Adapter
812          */
813         if ((rc = TRlldAdapterInit(&LldDriver, sc->TRlldAdapter, vtophys(sc->TRlldAdapter),
814             (void *)sc, &sc->config)) != TRLLD_INIT_OK) {
815                 switch(rc) {
816                 case TRLLD_INIT_NOT_FOUND:
817                         printf("oltr%d: adapter not found\n", sc->unit);
818                         break;
819                 case TRLLD_INIT_UNSUPPORTED:
820                         printf("oltr%d: adapter not supported by low level driver\n", sc->unit);
821                         break;
822                 case TRLLD_INIT_PHYS16:
823                         printf("oltr%d: adapter memory block above 16M cannot DMA\n", sc->unit);
824                         break;
825                 case TRLLD_INIT_VERSION:
826                         printf("oltr%d: low level driver version mismatch\n", sc->unit);
827                         break;
828                 default:
829                         printf("oltr%d: unknown init error %d\n", sc->unit, rc);
830                         break;
831                 }
832                 goto init_failed;
833         }
834         sc->state = OL_INIT;
835
836         switch(sc->config.type) {
837         case TRLLD_ADAPTER_PCI4:        /* OC-3139 */
838                 work_size = 32 * 1024;
839                 break;
840         case TRLLD_ADAPTER_PCI7:        /* OC-3540 */
841                 work_size = 256;
842                 break;
843         default:
844                 work_size = 0;
845         }
846
847         if (work_size) {
848                 if ((sc->work_memory = malloc(work_size, M_DEVBUF, M_NOWAIT)) == NULL) {
849                         printf("oltr%d: failed to allocate work memory (%d octets).\n", sc->unit, work_size);
850                 } else {
851                 TRlldAddMemory(sc->TRlldAdapter, sc->work_memory,
852                     vtophys(sc->work_memory), work_size);
853                 }
854         }
855
856         switch(IFM_SUBTYPE(ifm->ifm_media)) {
857         case IFM_AUTO:
858                 rc = TRlldSetSpeed(sc->TRlldAdapter, 0); /* TRLLD_SPEED_AUTO */
859                 break;
860         case IFM_TOK_UTP4:
861                 rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_4MBPS);
862                 break;
863         case IFM_TOK_UTP16:
864                 rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_16MBPS);
865                 break;
866         case IFM_TOK_UTP100:
867                 rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_100MBPS);
868                 break;
869         }
870
871         /*
872          * Download adapter micro-code
873          */
874         if (bootverbose)
875                 printf("oltr%d: Downloading adapter microcode: ", sc->unit);
876
877         switch(sc->config.mactype) {
878         case TRLLD_MAC_TMS:
879                 rc = TRlldDownload(sc->TRlldAdapter, TRlldMacCode);
880                 if (bootverbose)
881                         printf("TMS-380");
882                 break;
883         case TRLLD_MAC_HAWKEYE:
884                 rc = TRlldDownload(sc->TRlldAdapter, TRlldHawkeyeMac);
885                 if (bootverbose)
886                         printf("Hawkeye");
887                 break;
888         case TRLLD_MAC_BULLSEYE:
889                 rc = TRlldDownload(sc->TRlldAdapter, TRlldBullseyeMac);
890                 if (bootverbose)
891                         printf("Bullseye");
892                 break;
893         default:
894                 if (bootverbose)
895                         printf("unknown - failed!\n");
896                 goto init_failed;
897                 break;
898         }
899
900         /*
901          * Check download status
902          */
903         switch(rc) {
904         case TRLLD_DOWNLOAD_OK:
905                 if (bootverbose)
906                         printf(" - ok\n");
907                 break;
908         case TRLLD_DOWNLOAD_ERROR:
909                 if (bootverbose)
910                         printf(" - failed\n");
911                 else
912                         printf("oltr%d: adapter microcode download failed\n", sc->unit);
913                 goto init_failed;
914                 break;
915         case TRLLD_STATE:
916                 if (bootverbose)
917                         printf(" - not ready\n");
918                 goto init_failed;
919                 break;
920         }
921
922         /*
923          * Wait for self-test to complete
924          */
925         i = 0;
926         while ((poll++ < SELF_TEST_POLLS) && (sc->state < OL_READY)) {
927                 if (DEBUG_MASK & DEBUG_INIT)
928                         printf("p");
929                 DELAY(TRlldPoll(sc->TRlldAdapter) * 1000);
930                 if (TRlldInterruptService(sc->TRlldAdapter) != 0)
931                         if (DEBUG_MASK & DEBUG_INIT) printf("i");
932         }
933
934         if (sc->state != OL_CLOSED) {
935                 printf("oltr%d: self-test failed\n", sc->unit);
936                 goto init_failed;
937         }
938
939         /*
940          * Set up adapter poll
941          */
942         callout_init(&sc->oltr_poll_ch);
943         callout_reset(&sc->oltr_poll_ch, 1, oltr_poll, sc);
944
945         sc->state = OL_OPENING;
946
947         /*
948          * Open the adapter
949          */
950         rc = TRlldOpen(sc->TRlldAdapter, sc->arpcom.ac_enaddr, sc->GroupAddress,
951                 sc->FunctionalAddress, 1552, sc->AdapterMode);
952         switch(rc) {
953                 case TRLLD_OPEN_OK:
954                         break;
955                 case TRLLD_OPEN_STATE:
956                         printf("oltr%d: adapter not ready for open\n", sc->unit);
957                         (void)splx(s);
958                         return;
959                 case TRLLD_OPEN_ADDRESS_ERROR:
960                         printf("oltr%d: illegal MAC address\n", sc->unit);
961                         (void)splx(s);
962                         return;
963                 case TRLLD_OPEN_MODE_ERROR:
964                         printf("oltr%d: illegal open mode\n", sc->unit);
965                         (void)splx(s);
966                         return;
967                 default:
968                         printf("oltr%d: unknown open error (%d)\n", sc->unit, rc);
969                         (void)splx(s);
970                         return;
971         }
972
973         /*
974          * Set promiscious mode for now...
975          */
976         TRlldSetPromiscuousMode(sc->TRlldAdapter, TRLLD_PROM_LLC);
977         ifp->if_flags |= IFF_PROMISC;
978
979         /*
980          * Block on the ring insert and set a timeout
981          */
982         tsleep(sc, 0, "oltropen", 30*hz);
983
984         /*
985          * Set up receive buffer ring
986          */
987         for (i = 0; i < RING_BUFFER_LEN; i++) {
988                 rc = TRlldReceiveFragment(sc->TRlldAdapter, (void *)sc->rx_ring[i].data,
989                         sc->rx_ring[i].address, RX_BUFFER_LEN, (void *)sc->rx_ring[i].index);
990                 if (rc != TRLLD_RECEIVE_OK) {
991                         printf("oltr%d: adapter refused receive fragment %d (rc = %d)\n", sc->unit, i, rc);
992                         break;
993                 }       
994         }
995
996         sc->tx_avail = RING_BUFFER_LEN;
997         sc->tx_head = 0;
998         sc->tx_frame = 0;
999
1000         sc->restart = NULL;
1001
1002         ifp->if_flags |= IFF_RUNNING;
1003         ifp->if_flags &= ~IFF_OACTIVE;
1004
1005         /*
1006          * Set up adapter statistics poll
1007          */
1008         /*callout_handle_init(&sc->oltr_stat_ch);*/
1009         /*sc->oltr_stat_ch = timeout(oltr_stat, (void *)sc, 1*hz);*/
1010
1011         (void)splx(s);
1012         return;
1013
1014 init_failed:
1015         sc->state = OL_DEAD;
1016         (void)splx(s);
1017         return;
1018 }
1019
1020 static int
1021 oltr_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1022 {
1023         struct oltr_softc       *sc = ifp->if_softc;
1024         struct ifreq            *ifr = (struct ifreq *)data;
1025         int                     error = 0, s;
1026
1027         s = splimp();
1028
1029         switch(command) {
1030         case SIOCSIFADDR:
1031         case SIOCGIFADDR:
1032         case SIOCSIFMTU:
1033                 error = iso88025_ioctl(ifp, command, data);
1034                 break;
1035
1036         case SIOCSIFFLAGS:
1037                 if (ifp->if_flags & IFF_UP) {
1038                         oltr_init(sc);
1039                 } else {
1040                         if (ifp->if_flags & IFF_RUNNING) {
1041                                 oltr_close(sc);
1042                         }
1043                 }
1044                 break;
1045         case SIOCGIFMEDIA:
1046         case SIOCSIFMEDIA:
1047                 error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
1048                 break;
1049         default:
1050                 error = EINVAL;
1051                 break;
1052         }
1053
1054         (void)splx(s);  
1055
1056         return(error);
1057 }
1058
1059
1060 void
1061 oltr_poll(void *arg)
1062 {
1063         struct oltr_softc *sc = (struct oltr_softc *)arg;
1064         int s;
1065
1066         s = splimp();
1067
1068         if (DEBUG_MASK & DEBUG_POLL) printf("P");
1069
1070         /* Set up next adapter poll */
1071         callout_reset(&sc->oltr_poll_ch,
1072                       (TRlldPoll(sc->TRlldAdapter) * hz / 1000), oltr_poll, sc);
1073
1074         (void)splx(s);
1075 }
1076
1077 #ifdef NOTYET
1078 void
1079 oltr_stat(void *arg)
1080 {
1081         struct oltr_softc       *sc = (struct oltr_softc *)arg;
1082         int                     s;
1083
1084         s = splimp();
1085
1086         /* Set up next adapter poll */
1087         sc->oltr_stat_ch = timeout(oltr_stat, (void *)sc, 1*hz);
1088         if (TRlldGetStatistics(sc->TRlldAdapter, &sc->current, 0) != 0) {
1089                 /*printf("oltr%d: statistics available immediately...\n", sc->unit);*/
1090                 DriverStatistics((void *)sc, &sc->current);
1091         }
1092
1093         (void)splx(s);
1094 }
1095 #endif
1096 static int
1097 oltr_ifmedia_upd(struct ifnet *ifp)
1098 {
1099         struct oltr_softc       *sc = ifp->if_softc;
1100         struct ifmedia          *ifm = &sc->ifmedia;
1101         int                     rc;
1102
1103         if (IFM_TYPE(ifm->ifm_media) != IFM_TOKEN)
1104                 return(EINVAL);
1105
1106         switch(IFM_SUBTYPE(ifm->ifm_media)) {
1107         case IFM_AUTO:
1108                 rc = TRlldSetSpeed(sc->TRlldAdapter, 0); /* TRLLD_SPEED_AUTO */
1109                 break;
1110         case IFM_TOK_UTP4:
1111                 rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_4MBPS);
1112                 break;
1113         case IFM_TOK_UTP16:
1114                 rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_16MBPS);
1115                 break;
1116         case IFM_TOK_UTP100:
1117                 rc = TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_100MBPS);
1118                 break;
1119         default:
1120                 return(EINVAL);
1121                 break;
1122         }
1123
1124         return(0);
1125
1126 }
1127
1128 static void
1129 oltr_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1130 {
1131         struct oltr_softc       *sc = ifp->if_softc;
1132         struct ifmedia          *ifm = &sc->ifmedia;
1133
1134         /*printf("oltr%d: oltr_ifmedia_sts\n", sc->unit);*/
1135
1136         ifmr->ifm_active = IFM_TYPE(ifm->ifm_media)|IFM_SUBTYPE(ifm->ifm_media);
1137
1138 }
1139
1140 /*
1141  * ---------------------- PMW Callback Functions -----------------------
1142  */
1143
1144 void
1145 DriverStatistics(void *DriverHandle, TRlldStatistics_t *statistics)
1146 {
1147 #ifdef NOTYET
1148         struct oltr_softc               *sc = (struct oltr_softc *)DriverHandle;
1149
1150         if (sc->statistics.LineErrors != statistics->LineErrors)
1151                 printf("oltr%d: Line Errors %lu\n", sc->unit,
1152                     statistics->LineErrors);
1153         if (sc->statistics.InternalErrors != statistics->InternalErrors)
1154                 printf("oltr%d: Internal Errors %lu\n", sc->unit,
1155                     statistics->InternalErrors);
1156         if (sc->statistics.BurstErrors != statistics->BurstErrors)
1157                 printf("oltr%d: Burst Errors %lu\n", sc->unit,
1158                     statistics->BurstErrors);
1159         if (sc->statistics.AbortDelimiters != statistics->AbortDelimiters)
1160                 printf("oltr%d: Abort Delimiters %lu\n", sc->unit,
1161                     statistics->AbortDelimiters);
1162         if (sc->statistics.ARIFCIErrors != statistics->ARIFCIErrors)
1163                 printf("oltr%d: ARIFCI Errors %lu\n", sc->unit,
1164                     statistics->ARIFCIErrors);
1165         if (sc->statistics.LostFrames != statistics->LostFrames)
1166                 printf("oltr%d: Lost Frames %lu\n", sc->unit,
1167                     statistics->LostFrames);
1168         if (sc->statistics.CongestionErrors != statistics->CongestionErrors)
1169                 printf("oltr%d: Congestion Errors %lu\n", sc->unit,
1170                     statistics->CongestionErrors);
1171         if (sc->statistics.FrequencyErrors != statistics->FrequencyErrors)
1172                 printf("oltr%d: Frequency Errors %lu\n", sc->unit,
1173                     statistics->FrequencyErrors);
1174         if (sc->statistics.TokenErrors != statistics->TokenErrors)
1175                 printf("oltr%d: Token Errors %lu\n", sc->unit,
1176                     statistics->TokenErrors);
1177         if (sc->statistics.DMABusErrors != statistics->DMABusErrors)
1178                 printf("oltr%d: DMA Bus Errors %lu\n", sc->unit,
1179                     statistics->DMABusErrors);
1180         if (sc->statistics.DMAParityErrors != statistics->DMAParityErrors)
1181                 printf("oltr%d: DMA Parity Errors %lu\n", sc->unit,
1182                     statistics->DMAParityErrors);
1183         if (sc->statistics.ReceiveLongFrame != statistics->ReceiveLongFrame)
1184                 printf("oltr%d: Long frames received %lu\n", sc->unit,
1185                     statistics->ReceiveLongFrame);
1186         if (sc->statistics.ReceiveCRCErrors != statistics->ReceiveCRCErrors)
1187                 printf("oltr%d: Receive CRC Errors %lu\n", sc->unit,
1188                     statistics->ReceiveCRCErrors);
1189         if (sc->statistics.ReceiveOverflow != statistics->ReceiveOverflow)
1190                 printf("oltr%d: Recieve overflows %lu\n", sc->unit,
1191                     statistics->ReceiveOverflow);
1192         if (sc->statistics.TransmitUnderrun != statistics->TransmitUnderrun)
1193                 printf("oltr%d: Frequency Errors %lu\n", sc->unit,
1194                     statistics->TransmitUnderrun);
1195         bcopy(statistics, &sc->statistics, sizeof(TRlldStatistics_t));
1196 #endif
1197 }
1198
1199 static void
1200 DriverSuspend(unsigned short MicroSeconds)
1201 {
1202     DELAY(MicroSeconds);
1203 }
1204
1205
1206 static void
1207 DriverStatus(void *DriverHandle, TRlldStatus_t *Status)
1208 {
1209         struct oltr_softc       *sc = (struct oltr_softc *)DriverHandle;
1210         struct ifnet            *ifp = &sc->arpcom.ac_if;
1211
1212         char *Protocol[] = { /* 0 */ "Unknown",
1213                              /* 1 */ "TKP",
1214                              /* 2 */ "TXI" };
1215         char *Timeout[]  = { /* 0 */ "command",
1216                              /* 1 */ "transmit",
1217                              /* 2 */ "interrupt" };
1218         
1219         switch (Status->Type) {
1220
1221         case TRLLD_STS_ON_WIRE:
1222                 printf("oltr%d: ring insert (%d Mbps - %s)\n", sc->unit,
1223                     Status->Specification.OnWireInformation.Speed,
1224                     Protocol[Status->Specification.OnWireInformation.AccessProtocol]);
1225                 sc->state = OL_OPEN;
1226                 wakeup(sc);
1227                 break;
1228         case TRLLD_STS_SELFTEST_STATUS:
1229                 if (Status->Specification.SelftestStatus == TRLLD_ST_OK) {
1230                         sc->state = OL_CLOSED;
1231                         if (bootverbose)
1232                                 printf("oltr%d: self test complete\n", sc->unit);
1233                 }
1234                 if (Status->Specification.SelftestStatus & TRLLD_ST_ERROR) {
1235                         printf("oltr%d: Adapter self test error %d", sc->unit,
1236                         Status->Specification.SelftestStatus & ~TRLLD_ST_ERROR);
1237                         sc->state = OL_DEAD;
1238                 }
1239                 if (Status->Specification.SelftestStatus & TRLLD_ST_TIMEOUT) {
1240                         printf("oltr%d: Adapter self test timed out.\n", sc->unit);
1241                         sc->state = OL_DEAD;
1242                 }
1243                 break;
1244         case TRLLD_STS_INIT_STATUS:
1245                 if (Status->Specification.InitStatus == 0x800) {
1246                         oltr_stop(sc);
1247                         ifmedia_set(&sc->ifmedia, IFM_TOKEN|IFM_TOK_UTP16);
1248                         TRlldSetSpeed(sc->TRlldAdapter, TRLLD_SPEED_16MBPS);
1249                         oltr_init(sc);
1250                         break;
1251                 }
1252                 printf("oltr%d: adapter init failure 0x%03x\n", sc->unit,
1253                     Status->Specification.InitStatus);
1254                 oltr_stop(sc);
1255                 break;
1256         case TRLLD_STS_RING_STATUS:
1257                 if (Status->Specification.RingStatus) {
1258                         printf("oltr%d: Ring status change: ", sc->unit);
1259                         if (Status->Specification.RingStatus &
1260                             TRLLD_RS_SIGNAL_LOSS)
1261                                 printf(" [Signal Loss]");
1262                         if (Status->Specification.RingStatus &
1263                             TRLLD_RS_HARD_ERROR)
1264                                 printf(" [Hard Error]");
1265                         if (Status->Specification.RingStatus &
1266                             TRLLD_RS_SOFT_ERROR)
1267                                 printf(" [Soft Error]");
1268                         if (Status->Specification.RingStatus &
1269                             TRLLD_RS_TRANSMIT_BEACON)
1270                                 printf(" [Beacon]");
1271                         if (Status->Specification.RingStatus &
1272                             TRLLD_RS_LOBE_WIRE_FAULT)
1273                                 printf(" [Wire Fault]");
1274                         if (Status->Specification.RingStatus &
1275                             TRLLD_RS_AUTO_REMOVAL_ERROR)
1276                                 printf(" [Auto Removal]");
1277                         if (Status->Specification.RingStatus &
1278                             TRLLD_RS_REMOVE_RECEIVED)
1279                                 printf(" [Remove Received]");
1280                         if (Status->Specification.RingStatus &
1281                             TRLLD_RS_COUNTER_OVERFLOW)
1282                                 printf(" [Counter Overflow]");
1283                         if (Status->Specification.RingStatus &
1284                             TRLLD_RS_SINGLE_STATION)
1285                                 printf(" [Single Station]");
1286                         if (Status->Specification.RingStatus &
1287                                 TRLLD_RS_RING_RECOVERY)
1288                                 printf(" [Ring Recovery]");
1289                         printf("\n");   
1290                 }
1291                 break;
1292         case TRLLD_STS_ADAPTER_CHECK:
1293                 printf("oltr%d: adapter check (%04x %04x %04x %04x)\n", sc->unit,
1294                     Status->Specification.AdapterCheck[0],
1295                     Status->Specification.AdapterCheck[1],
1296                     Status->Specification.AdapterCheck[2],
1297                     Status->Specification.AdapterCheck[3]);
1298                 sc->state = OL_DEAD;
1299                 oltr_stop(sc);
1300                 break;
1301         case TRLLD_STS_PROMISCUOUS_STOPPED:
1302                 printf("oltr%d: promiscuous mode ", sc->unit);
1303                 if (Status->Specification.PromRemovedCause == 1)
1304                         printf("remove received.");
1305                 if (Status->Specification.PromRemovedCause == 2)
1306                         printf("poll failure.");
1307                 if (Status->Specification.PromRemovedCause == 2)
1308                         printf("buffer size failure.");
1309                 printf("\n");
1310                 ifp->if_flags &= ~IFF_PROMISC;
1311                 break;
1312         case TRLLD_STS_LLD_ERROR:
1313                 printf("oltr%d: low level driver internal error ", sc->unit);
1314                 printf("(%04x %04x %04x %04x).\n",
1315                     Status->Specification.InternalError[0],
1316                     Status->Specification.InternalError[1],
1317                     Status->Specification.InternalError[2],
1318                     Status->Specification.InternalError[3]);
1319                 sc->state = OL_DEAD;
1320                 oltr_stop(sc);
1321                 break;
1322         case TRLLD_STS_ADAPTER_TIMEOUT:
1323                 printf("oltr%d: adapter %s timeout.\n", sc->unit,
1324                     Timeout[Status->Specification.AdapterTimeout]);
1325                 break;
1326         default:
1327                 printf("oltr%d: driver status Type = %d\n", sc->unit, Status->Type);
1328                 break;
1329
1330         }
1331         if (Status->Closed) {
1332                 sc->state = OL_CLOSING;
1333                 oltr_stop(sc);
1334         }
1335
1336 }
1337
1338 static void
1339 DriverCloseCompleted(void *DriverHandle)
1340 {
1341         struct oltr_softc               *sc = (struct oltr_softc *)DriverHandle;
1342         
1343         printf("oltr%d: adapter closed\n", sc->unit);
1344         wakeup(sc);
1345         sc->state = OL_CLOSED;
1346 }
1347
1348 static void
1349 DriverTransmitFrameCompleted(void *DriverHandle, void *FrameHandle, int TransmitStatus)
1350 {
1351         struct oltr_softc       *sc = (struct oltr_softc *)DriverHandle;
1352         struct ifnet            *ifp = &sc->arpcom.ac_if;
1353         TRlldTransmit_t         *frame = (TRlldTransmit_t *)FrameHandle;
1354         
1355         /*printf("oltr%d: DriverTransmitFrameCompleted\n", sc->unit);*/
1356
1357         if (TransmitStatus != TRLLD_TRANSMIT_OK) {
1358                 ifp->if_oerrors++;
1359                 printf("oltr%d: transmit error %d\n", sc->unit, TransmitStatus);
1360         } else {
1361                 ifp->if_opackets++;
1362         }
1363         
1364         sc->tx_avail += frame->FragmentCount;
1365
1366         if (ifp->if_flags & IFF_OACTIVE) {
1367                 printf("oltr%d: queue restart\n", sc->unit);
1368                 ifp->if_flags &= ~IFF_OACTIVE;
1369                 oltr_start(ifp);
1370         }
1371
1372
1373 }
1374
1375 static void
1376 DriverReceiveFrameCompleted(void *DriverHandle, int ByteCount, int FragmentCount, void *FragmentHandle, int ReceiveStatus)
1377 {
1378         struct oltr_softc       *sc = (struct oltr_softc *)DriverHandle;
1379         struct ifnet            *ifp = (struct ifnet *)&sc->arpcom.ac_if;
1380         struct mbuf             *m0, *m1, *m;
1381         struct iso88025_header  *th;
1382         int                     frame_len = ByteCount, i = (int)FragmentHandle, rc, s;
1383         int                     mbuf_offset, mbuf_size, frag_offset, copy_length;
1384         char                    *fragment = sc->rx_ring[RING_BUFFER(i)].data;
1385         
1386         if (sc->state > OL_CLOSED) {
1387                 if (ReceiveStatus == TRLLD_RCV_OK) {
1388                         MGETHDR(m0, MB_DONTWAIT, MT_DATA);
1389                         mbuf_size = MHLEN - 2;
1390                         if (!m0) {
1391                                 ifp->if_ierrors++;
1392                                 goto dropped;
1393                         }
1394                         if (ByteCount + 2 > MHLEN) {
1395                                 MCLGET(m0, MB_DONTWAIT);
1396                                 mbuf_size = MCLBYTES - 2;
1397                                 if (!(m0->m_flags & M_EXT)) {
1398                                         m_freem(m0);
1399                                         ifp->if_ierrors++;
1400                                         goto dropped;
1401                                 }
1402                         }
1403                         m0->m_pkthdr.rcvif = ifp;
1404                         m0->m_pkthdr.len = ByteCount;
1405                         m0->m_len = 0;
1406                         m0->m_data += 2;
1407                         th = mtod(m0, struct iso88025_header *);
1408                         m0->m_pkthdr.header = (void *)th;
1409
1410                         m = m0;
1411                         mbuf_offset = 0;
1412                         frag_offset = 0;
1413                         while (frame_len) {
1414                                 copy_length = MIN3(frame_len,
1415                                     (RX_BUFFER_LEN - frag_offset),
1416                                     (mbuf_size - mbuf_offset));
1417                                 bcopy(fragment + frag_offset, mtod(m, char *) +
1418                                     mbuf_offset, copy_length);
1419                                 m->m_len += copy_length;
1420                                 mbuf_offset += copy_length;
1421                                 frag_offset += copy_length;
1422                                 frame_len -= copy_length;
1423                         
1424                                 if (frag_offset == RX_BUFFER_LEN) {
1425                                         fragment =
1426                                             sc->rx_ring[RING_BUFFER(++i)].data;
1427                                         frag_offset = 0;
1428                                 }
1429                                 if ((mbuf_offset == mbuf_size) && (frame_len > 0)) {
1430                                         MGET(m1, MB_DONTWAIT, MT_DATA);
1431                                         mbuf_size = MHLEN;
1432                                         if (!m1) {
1433                                                 ifp->if_ierrors++;
1434                                                 m_freem(m0);
1435                                                 goto dropped;
1436                                         }
1437                                         if (frame_len > MHLEN) {
1438                                                 MCLGET(m1, MB_DONTWAIT);
1439                                                 mbuf_size = MCLBYTES;
1440                                                 if (!(m1->m_flags & M_EXT)) {
1441                                                         m_freem(m0);
1442                                                         m_freem(m1);
1443                                                         ifp->if_ierrors++;
1444                                                         goto dropped;
1445                                                 }
1446                                         }
1447                                         m->m_next = m1;
1448                                         m = m1;
1449                                         mbuf_offset = 0;
1450                                         m->m_len = 0;
1451                                 }
1452                         }
1453 #if (NBPFILTER > 0) || defined(__DragonFly__) || (__FreeBSD_version > 400000)
1454                         if (ifp->if_bpf)
1455                                 bpf_mtap(ifp, m0);
1456 #endif
1457
1458                         /*if (ifp->if_flags & IFF_PROMISC) {*/
1459                                 if (bcmp(th->iso88025_dhost, ifp->if_broadcastaddr,
1460                                          ifp->if_addrlen)) {
1461                                         if ((bcmp(th->iso88025_dhost + 1, sc->arpcom.ac_enaddr + 1, ISO88025_ADDR_LEN - 1)) ||
1462                                             ((th->iso88025_dhost[0] & 0x7f) != sc->arpcom.ac_enaddr[0])) {
1463                                                 m_freem(m0);
1464                                                 goto dropped;
1465                                         }
1466                                 }
1467                         /*}*/
1468                         ifp->if_ipackets++;
1469
1470                         (*ifp->if_input)(ifp, m0);
1471
1472                 } else {        /* Receiver error */
1473                         if (ReceiveStatus != TRLLD_RCV_NO_DATA) {
1474                                 printf("oltr%d: receive error %d\n", sc->unit,
1475                                     ReceiveStatus);
1476                                 ifp->if_ierrors++;
1477                         }
1478                 }
1479
1480 dropped:
1481                 s = splimp();
1482                 i = (int)FragmentHandle;
1483                 while (FragmentCount--) {
1484                         rc = TRlldReceiveFragment(sc->TRlldAdapter,
1485                             (void *)sc->rx_ring[RING_BUFFER(i)].data,
1486                             sc->rx_ring[RING_BUFFER(i)].address,
1487                             RX_BUFFER_LEN, (void *)sc->rx_ring[RING_BUFFER(i)].index);
1488                         if (rc != TRLLD_RECEIVE_OK) {
1489                                 printf("oltr%d: adapter refused receive fragment %d (rc = %d)\n", sc->unit, i, rc);
1490                                 break;
1491                         }
1492                         i++;
1493                 }
1494                 (void)splx(s);
1495         }
1496 }
1497
1498
1499 /*
1500  * ---------------------------- PMW Glue -------------------------------
1501  */
1502
1503 #ifndef TRlldInlineIO
1504
1505 static void
1506 DriverOutByte(unsigned short IOAddress, unsigned char value)
1507 {
1508         outb(IOAddress, value);
1509 }
1510
1511 static void
1512 DriverOutWord(unsigned short IOAddress, unsigned short value)
1513 {
1514         outw(IOAddress, value);
1515 }
1516
1517 static void
1518 DriverOutDword(unsigned short IOAddress, unsigned long value)
1519 {
1520         outl(IOAddress, value);
1521 }
1522
1523 static void
1524 DriverRepOutByte(unsigned short IOAddress, unsigned char *DataPointer, int ByteCount)
1525 {
1526         outsb(IOAddress, (void *)DataPointer, ByteCount);
1527 }
1528
1529 static void
1530 DriverRepOutWord(unsigned short IOAddress, unsigned short *DataPointer, int WordCount)
1531 {
1532         outsw(IOAddress, (void *)DataPointer, WordCount);
1533 }
1534
1535 static void
1536 DriverRepOutDword(unsigned short IOAddress, unsigned long *DataPointer, int DWordCount)
1537 {
1538         outsl(IOAddress, (void *)DataPointer, DWordCount);
1539 }
1540
1541 static unsigned char
1542 DriverInByte(unsigned short IOAddress)
1543 {
1544         return(inb(IOAddress));
1545 }
1546
1547 static unsigned short
1548 DriverInWord(unsigned short IOAddress)
1549 {
1550         return(inw(IOAddress));
1551 }
1552
1553 static unsigned long
1554 DriverInDword(unsigned short IOAddress)
1555 {
1556         return(inl(IOAddress));
1557 }
1558
1559 static void
1560 DriverRepInByte(unsigned short IOAddress, unsigned char *DataPointer, int ByteCount)
1561 {
1562         insb(IOAddress, (void *)DataPointer, ByteCount);
1563 }
1564
1565 static void
1566 DriverRepInWord(unsigned short IOAddress, unsigned short *DataPointer, int WordCount)
1567 {
1568         insw(IOAddress, (void *)DataPointer, WordCount);
1569 }
1570 static void
1571 DriverRepInDword( unsigned short IOAddress, unsigned long *DataPointer, int DWordCount)
1572 {
1573         insl(IOAddress, (void *)DataPointer, DWordCount);
1574 }
1575 #endif /* TRlldInlineIO */