]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/lmc/if_lmc.c
Merge latest (commit c8c1b3a77934768c7f7a4a9c10140c8bec529059) files
[FreeBSD/FreeBSD.git] / sys / dev / lmc / if_lmc.c
1 /*
2  * $FreeBSD$
3  *
4  * Copyright (c) 2002-2004 David Boggs. <boggs@boggs.palo-alto.ca.us>
5  * All rights reserved.
6  *
7  * BSD License:
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * GNU General Public License:
31  *
32  * This program is free software; you can redistribute it and/or modify it 
33  * under the terms of the GNU General Public License as published by the Free 
34  * Software Foundation; either version 2 of the License, or (at your option) 
35  * any later version.
36  * 
37  * This program is distributed in the hope that it will be useful, but WITHOUT 
38  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
39  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
40  * more details.
41  *
42  * You should have received a copy of the GNU General Public License along with
43  * this program; if not, write to the Free Software Foundation, Inc., 59 
44  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
45  *
46  * Description:
47  *
48  * This is an open-source Unix device driver for PCI-bus WAN interface cards.
49  * It sends and receives packets in HDLC frames over synchronous links.
50  * A generic PC plus Unix plus some SBE/LMC cards makes an OPEN router.
51  * This driver works with FreeBSD, NetBSD, OpenBSD, BSD/OS and Linux.
52  * It has been tested on i386 (32-bit little-end), Sparc (64-bit big-end),
53  * and Alpha (64-bit little-end) architectures.
54  *
55  * History and Authors:
56  *
57  * Ron Crane had the neat idea to use a Fast Ethernet chip as a PCI
58  * interface and add an Ethernet-to-HDLC gate array to make a WAN card.
59  * David Boggs designed the Ethernet-to-HDLC gate arrays and PC cards.
60  * We did this at our company, LAN Media Corporation (LMC).
61  * SBE Corp acquired LMC and continues to make the cards.
62  *
63  * Since the cards use Tulip Ethernet chips, we started with Matt Thomas'
64  * ubiquitous "de" driver.  Michael Graff stripped out the Ethernet stuff
65  * and added HSSI stuff.  Basil Gunn ported it to Solaris (lost) and
66  * Rob Braun ported it to Linux.  Andrew Stanley-Jones added support
67  * for three more cards and wrote the first version of lmcconfig.
68  * During 2002-5 David Boggs rewrote it and now feels responsible for it.
69  *
70  * Responsible Individual:
71  *
72  * Send bug reports and improvements to <boggs@boggs.palo-alto.ca.us>.
73  */
74 # include <sys/param.h> /* OS version */
75 # define  IFNET 1
76 # include "opt_inet.h"  /* INET */
77 # include "opt_inet6.h" /* INET6 */
78 # include "opt_netgraph.h" /* NETGRAPH */
79 # ifdef HAVE_KERNEL_OPTION_HEADERS
80 # include "opt_device_polling.h" /* DEVICE_POLLING */
81 # endif
82 # ifndef INET
83 #  define INET 0
84 # endif
85 # ifndef INET6
86 #  define INET6 0
87 # endif
88 # ifndef NETGRAPH
89 #  define NETGRAPH 0
90 # endif
91 # define  P2P 0         /* not in FreeBSD */
92 # define NSPPP 1        /* No count devices in FreeBSD 5 */
93 # include "opt_bpf.h"   /* DEV_BPF */
94 # define NBPFILTER DEV_BPF
95 # define  GEN_HDLC 0    /* not in FreeBSD */
96 #
97 # include <sys/systm.h>
98 # include <sys/kernel.h>
99 # include <sys/malloc.h>
100 # include <sys/mbuf.h>
101 # include <sys/socket.h>
102 # include <sys/sockio.h>
103 # include <sys/module.h>
104 # include <sys/bus.h>
105 # include <sys/lock.h>
106 # include <net/if.h>
107 # include <net/if_var.h>
108 # include <net/if_types.h>
109 # include <net/if_media.h>
110 # include <net/netisr.h>
111 # include <net/route.h>
112 # include <machine/bus.h>
113 # include <machine/resource.h>
114 # include <sys/rman.h>
115 # include <vm/vm.h>
116 # include <vm/pmap.h>
117 # include <sys/priv.h>
118 #  include <sys/mutex.h>
119 #  include <dev/pci/pcivar.h>
120 # if NETGRAPH
121 #  include <netgraph/ng_message.h>
122 #  include <netgraph/netgraph.h>
123 # endif
124 # if (INET || INET6)
125 #  include <netinet/in.h>
126 #  include <netinet/in_var.h>
127 # endif
128 # if NSPPP
129 #  include <net/if_sppp.h>
130 # endif
131 # if NBPFILTER
132 #  include <net/bpf.h>
133 # endif
134 /* and finally... */
135 # include <dev/lmc/if_lmc.h>
136
137
138
139
140
141 /* The SROM is a generic 93C46 serial EEPROM (64 words by 16 bits). */
142 /* Data is set up before the RISING edge of CLK; CLK is parked low. */
143 static void
144 shift_srom_bits(softc_t *sc, u_int32_t data, u_int32_t len)
145   {
146   u_int32_t csr = READ_CSR(TLP_SROM_MII);
147   for (; len>0; len--)
148     {  /* MSB first */
149     if (data & (1<<(len-1)))
150       csr |=  TLP_SROM_DIN;     /* DIN setup */
151     else
152       csr &= ~TLP_SROM_DIN;     /* DIN setup */
153     WRITE_CSR(TLP_SROM_MII, csr);
154     csr |=  TLP_SROM_CLK;       /* CLK rising edge */
155     WRITE_CSR(TLP_SROM_MII, csr);
156     csr &= ~TLP_SROM_CLK;       /* CLK falling edge */
157     WRITE_CSR(TLP_SROM_MII, csr);
158     }
159   }
160
161 /* Data is sampled on the RISING edge of CLK; CLK is parked low. */
162 static u_int16_t
163 read_srom(softc_t *sc, u_int8_t addr)
164   {
165   int i;
166   u_int32_t csr;
167   u_int16_t data;
168
169   /* Enable SROM access. */
170   csr = (TLP_SROM_SEL | TLP_SROM_RD | TLP_MII_MDOE);
171   WRITE_CSR(TLP_SROM_MII, csr);
172   /* CS rising edge prepares SROM for a new cycle. */
173   csr |= TLP_SROM_CS;
174   WRITE_CSR(TLP_SROM_MII, csr); /* assert CS */
175   shift_srom_bits(sc,  6,   4);         /* issue read cmd */
176   shift_srom_bits(sc, addr, 6);         /* issue address */
177   for (data=0, i=16; i>=0; i--)         /* read ->17<- bits of data */
178     {  /* MSB first */
179     csr = READ_CSR(TLP_SROM_MII);       /* DOUT sampled */
180     data = (data<<1) | ((csr & TLP_SROM_DOUT) ? 1:0);
181     csr |=  TLP_SROM_CLK;               /* CLK rising edge */
182     WRITE_CSR(TLP_SROM_MII, csr);
183     csr &= ~TLP_SROM_CLK;               /* CLK falling edge */
184     WRITE_CSR(TLP_SROM_MII, csr);
185     }
186   /* Disable SROM access. */
187   WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOE);
188
189   return data;
190   }
191
192 /* The SROM is formatted by the mfgr and should NOT be written! */
193 /* But lmcconfig can rewrite it in case it gets overwritten somehow. */
194 /* IOCTL SYSCALL: can sleep. */
195 static void
196 write_srom(softc_t *sc, u_int8_t addr, u_int16_t data)
197   {
198   u_int32_t csr;
199   int i;
200
201   /* Enable SROM access. */
202   csr = (TLP_SROM_SEL | TLP_SROM_RD | TLP_MII_MDOE);
203   WRITE_CSR(TLP_SROM_MII, csr);
204
205   /* Issue write-enable command. */
206   csr |= TLP_SROM_CS;
207   WRITE_CSR(TLP_SROM_MII, csr); /* assert CS */
208   shift_srom_bits(sc,  4, 4);           /* issue write enable cmd */
209   shift_srom_bits(sc, 63, 6);           /* issue address */
210   csr &= ~TLP_SROM_CS;
211   WRITE_CSR(TLP_SROM_MII, csr); /* deassert CS */
212
213   /* Issue erase command. */
214   csr |= TLP_SROM_CS;
215   WRITE_CSR(TLP_SROM_MII, csr); /* assert CS */
216   shift_srom_bits(sc, 7, 4);            /* issue erase cmd */
217   shift_srom_bits(sc, addr, 6);         /* issue address */
218   csr &= ~TLP_SROM_CS;
219   WRITE_CSR(TLP_SROM_MII, csr); /* deassert CS */
220
221   /* Issue write command. */
222   csr |= TLP_SROM_CS;
223   WRITE_CSR(TLP_SROM_MII, csr); /* assert CS */
224   for (i=0; i<10; i++)  /* 100 ms max wait */
225     if ((READ_CSR(TLP_SROM_MII) & TLP_SROM_DOUT)==0) SLEEP(10000);
226   shift_srom_bits(sc, 5, 4);            /* issue write cmd */
227   shift_srom_bits(sc, addr, 6);         /* issue address */
228   shift_srom_bits(sc, data, 16);        /* issue data */
229   csr &= ~TLP_SROM_CS;
230   WRITE_CSR(TLP_SROM_MII, csr); /* deassert CS */
231
232   /* Issue write-disable command. */
233   csr |= TLP_SROM_CS;
234   WRITE_CSR(TLP_SROM_MII, csr); /* assert CS */
235   for (i=0; i<10; i++)  /* 100 ms max wait */
236     if ((READ_CSR(TLP_SROM_MII) & TLP_SROM_DOUT)==0) SLEEP(10000);
237   shift_srom_bits(sc, 4, 4);            /* issue write disable cmd */
238   shift_srom_bits(sc, 0, 6);            /* issue address */
239   csr &= ~TLP_SROM_CS;
240   WRITE_CSR(TLP_SROM_MII, csr); /* deassert CS */
241
242   /* Disable SROM access. */
243   WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOE);
244   }
245
246 /* Not all boards have BIOS roms. */
247 /* The BIOS ROM is an AMD 29F010 1Mbit (128K by 8) EEPROM. */
248 static u_int8_t
249 read_bios(softc_t *sc, u_int32_t addr)
250   {
251   u_int32_t srom_mii;
252
253   /* Load the BIOS rom address register. */
254   WRITE_CSR(TLP_BIOS_ROM, addr);
255
256   /* Enable the BIOS rom. */
257   srom_mii = TLP_BIOS_SEL | TLP_BIOS_RD | TLP_MII_MDOE;
258   WRITE_CSR(TLP_SROM_MII, srom_mii);
259
260   /* Wait at least 20 PCI cycles. */
261   DELAY(20);
262
263   /* Read the BIOS rom data. */
264   srom_mii = READ_CSR(TLP_SROM_MII);
265
266   /* Disable the BIOS rom. */
267   WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOE);
268
269   return (u_int8_t)srom_mii & 0xFF;
270   }
271
272 static void
273 write_bios_phys(softc_t *sc, u_int32_t addr, u_int8_t data)
274   {
275   u_int32_t srom_mii;
276
277   /* Load the BIOS rom address register. */
278   WRITE_CSR(TLP_BIOS_ROM, addr);
279
280   /* Enable the BIOS rom. */
281   srom_mii = TLP_BIOS_SEL | TLP_BIOS_WR | TLP_MII_MDOE;
282
283   /* Load the data into the data register. */
284   srom_mii = (srom_mii & 0xFFFFFF00) | (data & 0xFF);
285   WRITE_CSR(TLP_SROM_MII, srom_mii);
286
287   /* Wait at least 20 PCI cycles. */
288   DELAY(20);
289
290   /* Disable the BIOS rom. */
291   WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOE);
292   }
293
294 /* IOCTL SYSCALL: can sleep. */
295 static void
296 write_bios(softc_t *sc, u_int32_t addr, u_int8_t data)
297   {
298   u_int8_t read_data;
299
300   /* this sequence enables writing */
301   write_bios_phys(sc, 0x5555, 0xAA);
302   write_bios_phys(sc, 0x2AAA, 0x55);
303   write_bios_phys(sc, 0x5555, 0xA0);
304   write_bios_phys(sc, addr,   data);
305
306   /* Wait for the write operation to complete. */
307   for (;;)  /* interruptable syscall */
308     {
309     for (;;)
310       {
311       read_data = read_bios(sc, addr);
312       if ((read_data & 0x80) == (data & 0x80)) break;
313       if  (read_data & 0x20)
314         {  /* Data sheet says read it again. */
315         read_data = read_bios(sc, addr);
316         if ((read_data & 0x80) == (data & 0x80)) break;
317         if (DRIVER_DEBUG)
318           printf("%s: write_bios() failed; rom addr=0x%x\n",
319            NAME_UNIT, addr);
320         return;
321         }
322       }
323     read_data = read_bios(sc, addr);
324     if (read_data == data) break;
325     }
326   }
327
328 /* IOCTL SYSCALL: can sleep. */
329 static void
330 erase_bios(softc_t *sc)
331   {
332   unsigned char read_data;
333
334   /* This sequence enables erasing: */
335   write_bios_phys(sc, 0x5555, 0xAA);
336   write_bios_phys(sc, 0x2AAA, 0x55);
337   write_bios_phys(sc, 0x5555, 0x80);
338   write_bios_phys(sc, 0x5555, 0xAA);
339   write_bios_phys(sc, 0x2AAA, 0x55);
340   write_bios_phys(sc, 0x5555, 0x10);
341
342   /* Wait for the erase operation to complete. */
343   for (;;) /* interruptable syscall */
344     {
345     for (;;)
346       {
347       read_data = read_bios(sc, 0);
348       if (read_data & 0x80) break;
349       if (read_data & 0x20)
350         {  /* Data sheet says read it again. */
351         read_data = read_bios(sc, 0);
352         if (read_data & 0x80) break;
353         if (DRIVER_DEBUG)
354           printf("%s: erase_bios() failed\n", NAME_UNIT);
355         return;
356         }
357       }
358     read_data = read_bios(sc, 0);
359     if (read_data == 0xFF) break;
360     }
361   }
362
363 /* MDIO is 3-stated between tranactions. */
364 /* MDIO is set up before the RISING edge of MDC; MDC is parked low. */
365 static void
366 shift_mii_bits(softc_t *sc, u_int32_t data, u_int32_t len)
367   {
368   u_int32_t csr = READ_CSR(TLP_SROM_MII);
369   for (; len>0; len--)
370     {  /* MSB first */
371     if (data & (1<<(len-1)))
372       csr |=  TLP_MII_MDOUT; /* MDOUT setup */
373     else
374       csr &= ~TLP_MII_MDOUT; /* MDOUT setup */
375     WRITE_CSR(TLP_SROM_MII, csr);
376     csr |=  TLP_MII_MDC;     /* MDC rising edge */
377     WRITE_CSR(TLP_SROM_MII, csr);
378     csr &= ~TLP_MII_MDC;     /* MDC falling edge */
379     WRITE_CSR(TLP_SROM_MII, csr);
380     }
381   }
382
383 /* The specification for the MII is IEEE Std 802.3 clause 22. */
384 /* MDIO is sampled on the RISING edge of MDC; MDC is parked low. */
385 static u_int16_t
386 read_mii(softc_t *sc, u_int8_t regad)
387   {
388   int i;
389   u_int32_t csr;
390   u_int16_t data = 0;
391  
392   WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOUT);
393
394   shift_mii_bits(sc, 0xFFFFF, 20);      /* preamble */
395   shift_mii_bits(sc, 0xFFFFF, 20);      /* preamble */
396   shift_mii_bits(sc, 1, 2);             /* start symbol */
397   shift_mii_bits(sc, 2, 2);             /* read op */
398   shift_mii_bits(sc, 0, 5);             /* phyad=0 */
399   shift_mii_bits(sc, regad, 5);         /* regad */
400   csr = READ_CSR(TLP_SROM_MII);
401   csr |= TLP_MII_MDOE;
402   WRITE_CSR(TLP_SROM_MII, csr);
403   shift_mii_bits(sc, 0, 2);             /* turn-around */
404   for (i=15; i>=0; i--)                 /* data */
405     {  /* MSB first */
406     csr = READ_CSR(TLP_SROM_MII);       /* MDIN sampled */
407     data = (data<<1) | ((csr & TLP_MII_MDIN) ? 1:0);
408     csr |=  TLP_MII_MDC;                /* MDC rising edge */
409     WRITE_CSR(TLP_SROM_MII, csr);
410     csr &= ~TLP_MII_MDC;                /* MDC falling edge */
411     WRITE_CSR(TLP_SROM_MII, csr);
412     }
413   return data;
414   }
415
416 static void
417 write_mii(softc_t *sc, u_int8_t regad, u_int16_t data)
418   {
419   WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOUT);
420   shift_mii_bits(sc, 0xFFFFF, 20);      /* preamble */
421   shift_mii_bits(sc, 0xFFFFF, 20);      /* preamble */
422   shift_mii_bits(sc, 1, 2);             /* start symbol */
423   shift_mii_bits(sc, 1, 2);             /* write op */
424   shift_mii_bits(sc, 0, 5);             /* phyad=0 */
425   shift_mii_bits(sc, regad, 5);         /* regad */
426   shift_mii_bits(sc, 2, 2);             /* turn-around */
427   shift_mii_bits(sc, data, 16);         /* data */
428   WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOE);
429   if (regad == 16) sc->led_state = data; /* a small optimization */
430   }
431
432 static void
433 set_mii16_bits(softc_t *sc, u_int16_t bits)
434   {
435   u_int16_t mii16 = read_mii(sc, 16);
436   mii16 |= bits;
437   write_mii(sc, 16, mii16);
438   }
439
440 static void
441 clr_mii16_bits(softc_t *sc, u_int16_t bits)
442   {
443   u_int16_t mii16 = read_mii(sc, 16);
444   mii16 &= ~bits;
445   write_mii(sc, 16, mii16);
446   }
447
448 static void
449 set_mii17_bits(softc_t *sc, u_int16_t bits)
450   {
451   u_int16_t mii17 = read_mii(sc, 17);
452   mii17 |= bits;
453   write_mii(sc, 17, mii17);
454   }
455
456 static void
457 clr_mii17_bits(softc_t *sc, u_int16_t bits)
458   {
459   u_int16_t mii17 = read_mii(sc, 17);
460   mii17 &= ~bits;
461   write_mii(sc, 17, mii17);
462   }
463
464 /*
465  * Watchdog code is more readable if it refreshes LEDs
466  *  once a second whether they need it or not.
467  * But MII refs take 150 uSecs each, so remember the last value
468  *  written to MII16 and avoid LED writes that do nothing.
469  */
470
471 static void
472 led_off(softc_t *sc, u_int16_t led)
473   {
474   if ((led & sc->led_state) == led) return;
475   set_mii16_bits(sc, led);
476   }
477
478 static void
479 led_on(softc_t *sc, u_int16_t led)
480   {
481   if ((led & sc->led_state) == 0) return;
482   clr_mii16_bits(sc, led);
483   }
484
485 static void
486 led_inv(softc_t *sc, u_int16_t led)
487   {
488   u_int16_t mii16 = read_mii(sc, 16);
489   mii16 ^= led;
490   write_mii(sc, 16, mii16);
491   }
492
493 /*
494  * T1 & T3 framer registers are accessed through MII regs 17 & 18.
495  * Write the address to MII reg 17 then R/W data through MII reg 18.
496  * The hardware interface is an Intel-style 8-bit muxed A/D bus.
497  */
498 static void
499 write_framer(softc_t *sc, u_int16_t addr, u_int8_t data)
500   {
501   write_mii(sc, 17, addr);
502   write_mii(sc, 18, data);
503   }
504
505 static u_int8_t
506 read_framer(softc_t *sc, u_int16_t addr)
507   {
508   write_mii(sc, 17, addr);
509   return (u_int8_t)read_mii(sc, 18);
510   }
511
512 /* Tulip's hardware implementation of General Purpose IO
513  *   (GPIO) pins makes life difficult for software.
514  * Bits 7-0 in the Tulip GPIO CSR are used for two purposes
515  *   depending on the state of bit 8.
516  * If bit 8 is 0 then bits 7-0 are "data" bits.
517  * If bit 8 is 1 then bits 7-0 are "direction" bits.
518  * If a direction bit is one, the data bit is an output.
519  * The problem is that the direction bits are WRITE-ONLY.
520  * Software must remember the direction bits in a shadow copy.
521  * (sc->gpio_dir) in order to change some but not all of the bits.
522  * All accesses to the Tulip GPIO register use these five procedures.
523  */
524
525 static void
526 make_gpio_input(softc_t *sc, u_int32_t bits)
527   {
528   sc->gpio_dir &= ~bits;
529   WRITE_CSR(TLP_GPIO, TLP_GPIO_DIR | (sc->gpio_dir));
530   }
531
532 static void
533 make_gpio_output(softc_t *sc, u_int32_t bits)
534   {
535   sc->gpio_dir |= bits;
536   WRITE_CSR(TLP_GPIO, TLP_GPIO_DIR | (sc->gpio_dir));
537   }
538
539 static u_int32_t
540 read_gpio(softc_t *sc)
541   {
542   return READ_CSR(TLP_GPIO);
543   }
544
545 static void
546 set_gpio_bits(softc_t *sc, u_int32_t bits)
547   {
548   WRITE_CSR(TLP_GPIO, (read_gpio(sc) |  bits) & 0xFF);
549   }
550
551 static void
552 clr_gpio_bits(softc_t *sc, u_int32_t bits)
553   {
554   WRITE_CSR(TLP_GPIO, (read_gpio(sc) & ~bits) & 0xFF);
555   }
556
557 /* Reset ALL of the flip-flops in the gate array to zero. */
558 /* This does NOT change the gate array programming. */
559 /* Called during initialization so it must not sleep. */
560 static void
561 reset_xilinx(softc_t *sc)
562   {
563   /* Drive RESET low to force initialization. */
564   clr_gpio_bits(sc, GPIO_RESET);
565   make_gpio_output(sc, GPIO_RESET);
566
567   /* Hold RESET low for more than 10 uSec. */
568   DELAY(50);
569
570   /* Done with RESET; make it an input. */
571   make_gpio_input(sc,  GPIO_RESET);
572   }
573
574 /* Load Xilinx gate array program from on-board rom. */
575 /* This changes the gate array programming. */
576 /* IOCTL SYSCALL: can sleep. */
577 static void
578 load_xilinx_from_rom(softc_t *sc)
579   {
580   int i;
581
582   /* Drive MODE low to load from ROM rather than GPIO. */
583   clr_gpio_bits(sc, GPIO_MODE);
584   make_gpio_output(sc, GPIO_MODE);
585
586   /* Drive DP & RESET low to force configuration. */
587   clr_gpio_bits(sc, GPIO_RESET | GPIO_DP);
588   make_gpio_output(sc, GPIO_RESET | GPIO_DP);
589
590   /* Hold RESET & DP low for more than 10 uSec. */
591   DELAY(50);
592
593   /* Done with RESET & DP; make them inputs. */
594   make_gpio_input(sc, GPIO_DP | GPIO_RESET);
595
596   /* BUSY-WAIT for Xilinx chip to configure itself from ROM bits. */
597   for (i=0; i<100; i++) /* 1 sec max delay */
598     if ((read_gpio(sc) & GPIO_DP) == 0) SLEEP(10000);
599
600   /* Done with MODE; make it an input. */
601   make_gpio_input(sc, GPIO_MODE);
602   }
603
604 /* Load the Xilinx gate array program from userland bits. */
605 /* This changes the gate array programming. */
606 /* IOCTL SYSCALL: can sleep. */
607 static int
608 load_xilinx_from_file(softc_t *sc, char *addr, u_int32_t len)
609   {
610   char *data;
611   int i, j, error;
612
613   /* Get some pages to hold the Xilinx bits; biggest file is < 6 KB. */
614   if (len > 8192) return EFBIG;  /* too big */
615   data = malloc(len, M_DEVBUF, M_WAITOK);
616   if (data == NULL) return ENOMEM;
617
618   /* Copy the Xilinx bits from userland. */
619   if ((error = copyin(addr, data, len)))
620     {
621     free(data, M_DEVBUF);
622     return error;
623     }
624
625   /* Drive MODE high to load from GPIO rather than ROM. */
626   set_gpio_bits(sc, GPIO_MODE);
627   make_gpio_output(sc, GPIO_MODE);
628
629   /* Drive DP & RESET low to force configuration. */
630   clr_gpio_bits(sc, GPIO_RESET | GPIO_DP);
631   make_gpio_output(sc, GPIO_RESET | GPIO_DP);
632
633   /* Hold RESET & DP low for more than 10 uSec. */
634   DELAY(50);
635   
636   /* Done with RESET & DP; make them inputs. */
637   make_gpio_input(sc, GPIO_RESET | GPIO_DP);
638
639   /* BUSY-WAIT for Xilinx chip to clear its config memory. */
640   make_gpio_input(sc, GPIO_INIT);
641   for (i=0; i<10000; i++) /* 1 sec max delay */
642     if ((read_gpio(sc) & GPIO_INIT)==0) SLEEP(10000);
643
644   /* Configure CLK and DATA as outputs. */
645   set_gpio_bits(sc, GPIO_CLK);  /* park CLK high */
646   make_gpio_output(sc, GPIO_CLK | GPIO_DATA);
647
648   /* Write bits to Xilinx; CLK is parked HIGH. */
649   /* DATA is set up before the RISING edge of CLK. */
650   for (i=0; i<len; i++)
651     for (j=0; j<8; j++)
652       {  /* LSB first */
653       if ((data[i] & (1<<j)) != 0)
654         set_gpio_bits(sc, GPIO_DATA); /* DATA setup */
655       else
656         clr_gpio_bits(sc, GPIO_DATA); /* DATA setup */
657       clr_gpio_bits(sc, GPIO_CLK); /* CLK falling edge */
658       set_gpio_bits(sc, GPIO_CLK); /* CLK rising edge */
659       }
660
661   /* Stop driving all Xilinx-related signals. */
662   /* Pullup and pulldown resistors take over. */
663   make_gpio_input(sc, GPIO_CLK | GPIO_DATA | GPIO_MODE);
664
665   free(data, M_DEVBUF);
666   return 0;
667   }
668
669 /* Write fragments of a command into the synthesized oscillator. */
670 /* DATA is set up before the RISING edge of CLK.  CLK is parked low. */
671 static void
672 shift_synth_bits(softc_t *sc, u_int32_t data, u_int32_t len)
673   {
674   int i;
675
676   for (i=0; i<len; i++)
677     { /* LSB first */
678     if ((data & (1<<i)) != 0)
679       set_gpio_bits(sc, GPIO_DATA); /* DATA setup */
680     else
681       clr_gpio_bits(sc, GPIO_DATA); /* DATA setup */
682     set_gpio_bits(sc, GPIO_CLK);    /* CLK rising edge */
683     clr_gpio_bits(sc, GPIO_CLK);    /* CLK falling edge */
684     }
685   }
686
687 /* Write a command to the synthesized oscillator on SSI and HSSIc. */
688 static void
689 write_synth(softc_t *sc, struct synth *synth)
690   {
691   /* SSI cards have a programmable prescaler */
692   if (sc->status.card_type == TLP_CSID_SSI)
693     {
694     if (synth->prescale == 9) /* divide by 512 */
695       set_mii17_bits(sc, MII17_SSI_PRESCALE);
696     else                      /* divide by  32 */
697       clr_mii17_bits(sc, MII17_SSI_PRESCALE);
698     }
699
700   clr_gpio_bits(sc,    GPIO_DATA | GPIO_CLK);
701   make_gpio_output(sc, GPIO_DATA | GPIO_CLK);
702
703   /* SYNTH is a low-true chip enable for the AV9110 chip. */
704   set_gpio_bits(sc,    GPIO_SSI_SYNTH);
705   make_gpio_output(sc, GPIO_SSI_SYNTH);
706   clr_gpio_bits(sc,    GPIO_SSI_SYNTH);
707
708   /* Serially shift the command into the AV9110 chip. */
709   shift_synth_bits(sc, synth->n, 7);
710   shift_synth_bits(sc, synth->m, 7);
711   shift_synth_bits(sc, synth->v, 1);
712   shift_synth_bits(sc, synth->x, 2);
713   shift_synth_bits(sc, synth->r, 2);
714   shift_synth_bits(sc, 0x16, 5); /* enable clk/x output */
715
716   /* SYNTH (chip enable) going high ends the command. */
717   set_gpio_bits(sc,   GPIO_SSI_SYNTH);
718   make_gpio_input(sc, GPIO_SSI_SYNTH);
719
720   /* Stop driving serial-related signals; pullups/pulldowns take over. */
721   make_gpio_input(sc, GPIO_DATA | GPIO_CLK);
722
723   /* remember the new synthesizer parameters */
724   if (&sc->config.synth != synth) sc->config.synth = *synth;
725   }
726
727 /* Write a command to the DAC controlling the VCXO on some T3 adapters. */
728 /* The DAC is a TI-TLV5636: 12-bit resolution and a serial interface. */
729 /* DATA is set up before the FALLING edge of CLK.  CLK is parked HIGH. */
730 static void
731 write_dac(softc_t *sc, u_int16_t data)
732   {
733   int i;
734
735   /* Prepare to use DATA and CLK. */
736   set_gpio_bits(sc,    GPIO_DATA | GPIO_CLK);
737   make_gpio_output(sc, GPIO_DATA | GPIO_CLK);
738
739   /* High-to-low transition prepares DAC for new value. */
740   set_gpio_bits(sc,    GPIO_T3_DAC);
741   make_gpio_output(sc, GPIO_T3_DAC);
742   clr_gpio_bits(sc,    GPIO_T3_DAC);
743
744   /* Serially shift command bits into DAC. */
745   for (i=0; i<16; i++)
746     { /* MSB first */
747     if ((data & (1<<(15-i))) != 0)
748       set_gpio_bits(sc, GPIO_DATA); /* DATA setup */
749     else
750       clr_gpio_bits(sc, GPIO_DATA); /* DATA setup */
751     clr_gpio_bits(sc, GPIO_CLK);    /* CLK falling edge */
752     set_gpio_bits(sc, GPIO_CLK);    /* CLK rising edge */
753     }
754
755   /* Done with DAC; make it an input; loads new value into DAC. */
756   set_gpio_bits(sc,   GPIO_T3_DAC);
757   make_gpio_input(sc, GPIO_T3_DAC);
758
759   /* Stop driving serial-related signals; pullups/pulldowns take over. */
760   make_gpio_input(sc, GPIO_DATA | GPIO_CLK);
761   }
762
763 /* begin HSSI card code */
764
765 /* Must not sleep. */
766 static void
767 hssi_config(softc_t *sc)
768   {
769   if (sc->status.card_type == 0)
770     { /* defaults */
771     sc->status.card_type  = READ_PCI_CFG(sc, TLP_CSID);
772     sc->config.crc_len    = CFG_CRC_16;
773     sc->config.loop_back  = CFG_LOOP_NONE;
774     sc->config.tx_clk_src = CFG_CLKMUX_ST;
775     sc->config.dte_dce    = CFG_DTE;
776     sc->config.synth.n    = 52; /* 52.000 Mbs */
777     sc->config.synth.m    = 5;
778     sc->config.synth.v    = 0;
779     sc->config.synth.x    = 0;
780     sc->config.synth.r    = 0;
781     sc->config.synth.prescale = 2;
782     }
783
784   /* set CRC length */
785   if (sc->config.crc_len == CFG_CRC_32)
786     set_mii16_bits(sc, MII16_HSSI_CRC32);
787   else
788     clr_mii16_bits(sc, MII16_HSSI_CRC32);
789
790   /* Assert pin LA in HSSI conn: ask modem for local loop. */
791   if (sc->config.loop_back == CFG_LOOP_LL)
792     set_mii16_bits(sc, MII16_HSSI_LA);
793   else
794     clr_mii16_bits(sc, MII16_HSSI_LA);
795
796   /* Assert pin LB in HSSI conn: ask modem for remote loop. */
797   if (sc->config.loop_back == CFG_LOOP_RL)
798     set_mii16_bits(sc, MII16_HSSI_LB);
799   else
800     clr_mii16_bits(sc, MII16_HSSI_LB);
801
802   if (sc->status.card_type == TLP_CSID_HSSI)
803     {
804     /* set TXCLK src */
805     if (sc->config.tx_clk_src == CFG_CLKMUX_ST)
806       set_gpio_bits(sc, GPIO_HSSI_TXCLK);
807     else
808       clr_gpio_bits(sc, GPIO_HSSI_TXCLK);
809     make_gpio_output(sc, GPIO_HSSI_TXCLK);
810     }
811   else if (sc->status.card_type == TLP_CSID_HSSIc)
812     {  /* cPCI HSSI rev C has extra features */
813     /* Set TXCLK source. */
814     u_int16_t mii16 = read_mii(sc, 16);
815     mii16 &= ~MII16_HSSI_CLKMUX;
816     mii16 |= (sc->config.tx_clk_src&3)<<13;
817     write_mii(sc, 16, mii16);
818
819     /* cPCI HSSI implements loopback towards the net. */
820     if (sc->config.loop_back == CFG_LOOP_LINE)
821       set_mii16_bits(sc, MII16_HSSI_LOOP);
822     else
823       clr_mii16_bits(sc, MII16_HSSI_LOOP);
824
825     /* Set DTE/DCE mode. */
826     if (sc->config.dte_dce == CFG_DCE)
827       set_gpio_bits(sc, GPIO_HSSI_DCE);
828     else
829       clr_gpio_bits(sc, GPIO_HSSI_DCE);
830     make_gpio_output(sc, GPIO_HSSI_DCE);
831
832     /* Program the synthesized oscillator. */
833     write_synth(sc, &sc->config.synth);
834     }
835   }
836
837 static void
838 hssi_ident(softc_t *sc)
839   {
840   }
841
842 /* Called once a second; must not sleep. */
843 static int
844 hssi_watchdog(softc_t *sc)
845   {
846   u_int16_t mii16 = read_mii(sc, 16) & MII16_HSSI_MODEM;
847   int link_status = STATUS_UP;
848
849   led_inv(sc, MII16_HSSI_LED_UL);  /* Software is alive. */
850   led_on(sc, MII16_HSSI_LED_LL);  /* always on (SSI cable) */
851
852   /* Check the transmit clock. */
853   if (sc->status.tx_speed == 0)
854     {
855     led_on(sc, MII16_HSSI_LED_UR);
856     link_status = STATUS_DOWN;
857     }
858   else
859     led_off(sc, MII16_HSSI_LED_UR);
860
861   /* Is the modem ready? */
862   if ((mii16 & MII16_HSSI_CA) == 0)
863     {
864     led_off(sc, MII16_HSSI_LED_LR);
865     link_status = STATUS_DOWN;
866     }
867   else
868     led_on(sc, MII16_HSSI_LED_LR);
869
870   /* Print the modem control signals if they changed. */
871   if ((DRIVER_DEBUG) && (mii16 != sc->last_mii16))
872     {
873     char *on = "ON ", *off = "OFF";
874     printf("%s: TA=%s CA=%s LA=%s LB=%s LC=%s TM=%s\n", NAME_UNIT,
875      (mii16 & MII16_HSSI_TA) ? on : off,
876      (mii16 & MII16_HSSI_CA) ? on : off,
877      (mii16 & MII16_HSSI_LA) ? on : off,
878      (mii16 & MII16_HSSI_LB) ? on : off,
879      (mii16 & MII16_HSSI_LC) ? on : off,
880      (mii16 & MII16_HSSI_TM) ? on : off);
881     }
882
883   /* SNMP one-second-report */
884   sc->status.snmp.hssi.sigs = mii16 & MII16_HSSI_MODEM;
885
886   /* Remember this state until next time. */
887   sc->last_mii16 = mii16;
888
889   /* If a loop back is in effect, link status is UP */
890   if (sc->config.loop_back != CFG_LOOP_NONE)
891     link_status = STATUS_UP;
892
893   return link_status;
894   }
895
896 /* IOCTL SYSCALL: can sleep (but doesn't). */
897 static int
898 hssi_ioctl(softc_t *sc, struct ioctl *ioctl)
899   {
900   int error = 0;
901
902   if (ioctl->cmd == IOCTL_SNMP_SIGS)
903     {
904     u_int16_t mii16 = read_mii(sc, 16);
905     mii16 &= ~MII16_HSSI_MODEM;
906     mii16 |= (MII16_HSSI_MODEM & ioctl->data);
907     write_mii(sc, 16, mii16);
908     }
909   else if (ioctl->cmd == IOCTL_SET_STATUS)
910     {
911     if (ioctl->data != 0)
912       set_mii16_bits(sc, MII16_HSSI_TA);
913     else
914       clr_mii16_bits(sc, MII16_HSSI_TA);
915     }
916   else
917     error = EINVAL;
918
919   return error;
920   }
921
922 /* begin DS3 card code */
923
924 /* Must not sleep. */
925 static void
926 t3_config(softc_t *sc)
927   {
928   int i;
929   u_int8_t ctl1;
930
931   if (sc->status.card_type == 0)
932     { /* defaults */
933     sc->status.card_type  = TLP_CSID_T3;
934     sc->config.crc_len    = CFG_CRC_16;
935     sc->config.loop_back  = CFG_LOOP_NONE;
936     sc->config.format     = CFG_FORMAT_T3CPAR;
937     sc->config.cable_len  = 10; /* meters */
938     sc->config.scrambler  = CFG_SCRAM_DL_KEN;
939     sc->config.tx_clk_src = CFG_CLKMUX_INT;
940
941     /* Center the VCXO -- get within 20 PPM of 44736000. */
942     write_dac(sc, 0x9002); /* set Vref = 2.048 volts */
943     write_dac(sc, 2048); /* range is 0..4095 */
944     }
945
946   /* Set cable length. */
947   if (sc->config.cable_len > 30)
948     clr_mii16_bits(sc, MII16_DS3_ZERO);
949   else
950     set_mii16_bits(sc, MII16_DS3_ZERO);
951
952   /* Set payload scrambler polynomial. */
953   if (sc->config.scrambler == CFG_SCRAM_LARS)
954     set_mii16_bits(sc, MII16_DS3_POLY);
955   else
956     clr_mii16_bits(sc, MII16_DS3_POLY);
957
958   /* Set payload scrambler on/off. */
959   if (sc->config.scrambler == CFG_SCRAM_OFF)
960     clr_mii16_bits(sc, MII16_DS3_SCRAM);
961   else
962     set_mii16_bits(sc, MII16_DS3_SCRAM);
963
964   /* Set CRC length. */
965   if (sc->config.crc_len == CFG_CRC_32)
966     set_mii16_bits(sc, MII16_DS3_CRC32);
967   else
968     clr_mii16_bits(sc, MII16_DS3_CRC32);
969
970   /* Loopback towards host thru the line interface. */
971   if (sc->config.loop_back == CFG_LOOP_OTHER)
972     set_mii16_bits(sc, MII16_DS3_TRLBK);
973   else
974     clr_mii16_bits(sc, MII16_DS3_TRLBK);
975
976   /* Loopback towards network thru the line interface. */
977   if (sc->config.loop_back == CFG_LOOP_LINE)
978     set_mii16_bits(sc, MII16_DS3_LNLBK);
979   else if (sc->config.loop_back == CFG_LOOP_DUAL)
980     set_mii16_bits(sc, MII16_DS3_LNLBK);
981   else
982     clr_mii16_bits(sc, MII16_DS3_LNLBK);
983
984   /* Configure T3 framer chip; write EVERY writeable register. */
985   ctl1 = CTL1_SER | CTL1_XTX;
986   if (sc->config.loop_back == CFG_LOOP_INWARD) ctl1 |= CTL1_3LOOP;
987   if (sc->config.loop_back == CFG_LOOP_DUAL)   ctl1 |= CTL1_3LOOP;
988   if (sc->config.format == CFG_FORMAT_T3M13)   ctl1 |= CTL1_M13MODE;
989   write_framer(sc, T3CSR_CTL1,     ctl1);
990   write_framer(sc, T3CSR_TX_FEAC,  CTL5_EMODE);
991   write_framer(sc, T3CSR_CTL8,     CTL8_FBEC);
992   write_framer(sc, T3CSR_CTL12,    CTL12_DLCB1 | CTL12_C21 | CTL12_MCB1);
993   write_framer(sc, T3CSR_DBL_FEAC, 0);
994   write_framer(sc, T3CSR_CTL14,    CTL14_RGCEN | CTL14_TGCEN);
995   write_framer(sc, T3CSR_INTEN,    0);
996   write_framer(sc, T3CSR_CTL20,    CTL20_CVEN);
997
998   /* Clear error counters and latched error bits */
999   /*  that may have happened while initializing. */
1000   for (i=0; i<21; i++) read_framer(sc, i);
1001   }
1002
1003 static void
1004 t3_ident(softc_t *sc)
1005   {
1006   printf(", TXC03401 rev B");
1007   }
1008
1009 /* Called once a second; must not sleep. */
1010 static int
1011 t3_watchdog(softc_t *sc)
1012   {
1013   u_int16_t CV;
1014   u_int8_t CERR, PERR, MERR, FERR, FEBE;
1015   u_int8_t ctl1, stat16, feac;
1016   int link_status = STATUS_UP;
1017   u_int16_t mii16;
1018
1019   /* Read the alarm registers. */
1020   ctl1   = read_framer(sc, T3CSR_CTL1);
1021   stat16 = read_framer(sc, T3CSR_STAT16);
1022   mii16  = read_mii(sc, 16);
1023
1024   /* Always ignore the RTLOC alarm bit. */
1025   stat16 &= ~STAT16_RTLOC;
1026
1027   /* Software is alive. */
1028   led_inv(sc, MII16_DS3_LED_GRN);
1029
1030   /* Receiving Alarm Indication Signal (AIS). */
1031   if ((stat16 & STAT16_RAIS) != 0) /* receiving ais */
1032     led_on(sc, MII16_DS3_LED_BLU);
1033   else if (ctl1 & CTL1_TXAIS) /* sending ais */
1034     led_inv(sc, MII16_DS3_LED_BLU);
1035   else
1036     led_off(sc, MII16_DS3_LED_BLU);
1037
1038   /* Receiving Remote Alarm Indication (RAI). */
1039   if ((stat16 & STAT16_XERR) != 0) /* receiving rai */
1040     led_on(sc, MII16_DS3_LED_YEL);
1041   else if ((ctl1 & CTL1_XTX) == 0) /* sending rai */
1042     led_inv(sc, MII16_DS3_LED_YEL);
1043   else
1044     led_off(sc, MII16_DS3_LED_YEL);
1045
1046   /* If certain status bits are set then the link is 'down'. */
1047   /* The bad bits are: rxlos rxoof rxais rxidl xerr. */
1048   if ((stat16 & ~(STAT16_FEAC | STAT16_SEF)) != 0)
1049     link_status = STATUS_DOWN;
1050
1051   /* Declare local Red Alarm if the link is down. */
1052   if (link_status == STATUS_DOWN)
1053     led_on(sc, MII16_DS3_LED_RED);
1054   else if (sc->loop_timer != 0) /* loopback is active */
1055     led_inv(sc, MII16_DS3_LED_RED);
1056   else
1057     led_off(sc, MII16_DS3_LED_RED);
1058
1059   /* Print latched error bits if they changed. */
1060   if ((DRIVER_DEBUG) && ((stat16 & ~STAT16_FEAC) != sc->last_stat16))
1061     {
1062     char *on = "ON ", *off = "OFF";
1063     printf("%s: RLOS=%s ROOF=%s RAIS=%s RIDL=%s SEF=%s XERR=%s\n",
1064      NAME_UNIT,
1065      (stat16 & STAT16_RLOS) ? on : off,
1066      (stat16 & STAT16_ROOF) ? on : off,
1067      (stat16 & STAT16_RAIS) ? on : off,
1068      (stat16 & STAT16_RIDL) ? on : off,
1069      (stat16 & STAT16_SEF)  ? on : off,
1070      (stat16 & STAT16_XERR) ? on : off);
1071     }
1072
1073   /* Check and print error counters if non-zero. */
1074   CV   = read_framer(sc, T3CSR_CVHI)<<8;
1075   CV  += read_framer(sc, T3CSR_CVLO);
1076   PERR = read_framer(sc, T3CSR_PERR);
1077   CERR = read_framer(sc, T3CSR_CERR);
1078   FERR = read_framer(sc, T3CSR_FERR);
1079   MERR = read_framer(sc, T3CSR_MERR);
1080   FEBE = read_framer(sc, T3CSR_FEBE);
1081
1082   /* CV is invalid during LOS. */
1083   if ((stat16 & STAT16_RLOS)!=0) CV = 0;
1084   /* CERR & FEBE are invalid in M13 mode */
1085   if (sc->config.format == CFG_FORMAT_T3M13) CERR = FEBE = 0;
1086   /* FEBE is invalid during AIS. */
1087   if ((stat16 & STAT16_RAIS)!=0) FEBE = 0;
1088   if (DRIVER_DEBUG && (CV || PERR || CERR || FERR || MERR || FEBE))
1089     printf("%s: CV=%u PERR=%u CERR=%u FERR=%u MERR=%u FEBE=%u\n",
1090      NAME_UNIT, CV,   PERR,   CERR,   FERR,   MERR,   FEBE);
1091
1092   /* Driver keeps crude link-level error counters (SNMP is better). */
1093   sc->status.cntrs.lcv_errs  += CV;
1094   sc->status.cntrs.par_errs  += PERR;
1095   sc->status.cntrs.cpar_errs += CERR;
1096   sc->status.cntrs.frm_errs  += FERR;
1097   sc->status.cntrs.mfrm_errs += MERR;
1098   sc->status.cntrs.febe_errs += FEBE;
1099
1100   /* Check for FEAC messages (FEAC not defined in M13 mode). */
1101   if (FORMAT_T3CPAR && (stat16 & STAT16_FEAC)) do
1102     {
1103     feac = read_framer(sc, T3CSR_FEAC_STK);
1104     if ((feac & FEAC_STK_VALID)==0) break;
1105     /* Ignore RxFEACs while a far end loopback has been requested. */
1106     if ((sc->status.snmp.t3.line & TLOOP_FAR_LINE)!=0) continue;
1107     switch (feac & FEAC_STK_FEAC)
1108       {
1109       case T3BOP_LINE_UP:   break;
1110       case T3BOP_LINE_DOWN: break;
1111       case T3BOP_LOOP_DS3:
1112         {
1113         if (sc->last_FEAC == T3BOP_LINE_DOWN)
1114           {
1115           if (DRIVER_DEBUG)
1116             printf("%s: Received a 'line loopback deactivate' FEAC msg\n", NAME_UNIT);
1117           clr_mii16_bits(sc, MII16_DS3_LNLBK);
1118           sc->loop_timer = 0;
1119           }
1120         if (sc->last_FEAC == T3BOP_LINE_UP)
1121           {
1122           if (DRIVER_DEBUG)
1123             printf("%s: Received a 'line loopback activate' FEAC msg\n", NAME_UNIT);
1124           set_mii16_bits(sc, MII16_DS3_LNLBK);
1125           sc->loop_timer = 300;
1126           }
1127         break;
1128         }
1129       case T3BOP_OOF:
1130         {
1131         if (DRIVER_DEBUG)
1132           printf("%s: Received a 'far end LOF' FEAC msg\n", NAME_UNIT);
1133         break;
1134         }
1135       case T3BOP_IDLE:
1136         {
1137         if (DRIVER_DEBUG)
1138           printf("%s: Received a 'far end IDL' FEAC msg\n", NAME_UNIT);
1139         break;
1140         }
1141       case T3BOP_AIS:
1142         {
1143         if (DRIVER_DEBUG)
1144           printf("%s: Received a 'far end AIS' FEAC msg\n", NAME_UNIT);
1145         break;
1146         }
1147       case T3BOP_LOS:
1148         {
1149         if (DRIVER_DEBUG)
1150           printf("%s: Received a 'far end LOS' FEAC msg\n", NAME_UNIT);
1151         break;
1152         }
1153       default:
1154         {
1155         if (DRIVER_DEBUG)
1156           printf("%s: Received a 'type 0x%02X' FEAC msg\n", NAME_UNIT, feac & FEAC_STK_FEAC);
1157         break;
1158         }
1159       }
1160     sc->last_FEAC = feac & FEAC_STK_FEAC;
1161     } while ((feac & FEAC_STK_MORE) != 0);
1162   stat16 &= ~STAT16_FEAC;
1163
1164   /* Send Service-Affecting priority FEAC messages */
1165   if (((sc->last_stat16 ^ stat16) & 0xF0) && (FORMAT_T3CPAR))
1166     {
1167     /* Transmit continuous FEACs */
1168     write_framer(sc, T3CSR_CTL14,
1169      read_framer(sc, T3CSR_CTL14) & ~CTL14_FEAC10);
1170     if      ((stat16 & STAT16_RLOS)!=0)
1171       write_framer(sc, T3CSR_TX_FEAC, 0xC0 + T3BOP_LOS);
1172     else if ((stat16 & STAT16_ROOF)!=0)
1173       write_framer(sc, T3CSR_TX_FEAC, 0xC0 + T3BOP_OOF);
1174     else if ((stat16 & STAT16_RAIS)!=0)
1175       write_framer(sc, T3CSR_TX_FEAC, 0xC0 + T3BOP_AIS);
1176     else if ((stat16 & STAT16_RIDL)!=0)
1177       write_framer(sc, T3CSR_TX_FEAC, 0xC0 + T3BOP_IDLE);
1178     else
1179       write_framer(sc, T3CSR_TX_FEAC, CTL5_EMODE);
1180     }
1181
1182   /* Start sending RAI, Remote Alarm Indication. */
1183   if (((stat16 & STAT16_ROOF)!=0) && ((stat16 & STAT16_RLOS)==0) &&
1184    ((sc->last_stat16 & STAT16_ROOF)==0))
1185     write_framer(sc, T3CSR_CTL1, ctl1 &= ~CTL1_XTX);
1186   /* Stop sending RAI, Remote Alarm Indication. */
1187   else if (((stat16 & STAT16_ROOF)==0) && ((sc->last_stat16 & STAT16_ROOF)!=0))
1188     write_framer(sc, T3CSR_CTL1, ctl1 |=  CTL1_XTX);
1189
1190   /* Start sending AIS, Alarm Indication Signal */
1191   if (((stat16 & STAT16_RLOS)!=0) && ((sc->last_stat16 & STAT16_RLOS)==0))
1192     {
1193     set_mii16_bits(sc, MII16_DS3_FRAME);
1194     write_framer(sc, T3CSR_CTL1, ctl1 |  CTL1_TXAIS);
1195     }
1196   /* Stop sending AIS, Alarm Indication Signal */
1197   else if (((stat16 & STAT16_RLOS)==0) && ((sc->last_stat16 & STAT16_RLOS)!=0))
1198     {
1199     clr_mii16_bits(sc, MII16_DS3_FRAME);
1200     write_framer(sc, T3CSR_CTL1, ctl1 & ~CTL1_TXAIS);
1201     }
1202
1203   /* Time out loopback requests. */
1204   if (sc->loop_timer != 0)
1205     if (--sc->loop_timer == 0)
1206       if ((mii16 & MII16_DS3_LNLBK)!=0)
1207         {
1208         if (DRIVER_DEBUG)
1209           printf("%s: Timeout: Loop Down after 300 seconds\n", NAME_UNIT);
1210         clr_mii16_bits(sc, MII16_DS3_LNLBK); /* line loopback off */
1211         }
1212
1213   /* SNMP error counters */
1214   sc->status.snmp.t3.lcv  = CV;
1215   sc->status.snmp.t3.pcv  = PERR;
1216   sc->status.snmp.t3.ccv  = CERR;
1217   sc->status.snmp.t3.febe = FEBE;
1218
1219   /* SNMP Line Status */
1220   sc->status.snmp.t3.line = 0;
1221   if ((ctl1  & CTL1_XTX)==0)   sc->status.snmp.t3.line |= TLINE_TX_RAI;
1222   if (stat16 & STAT16_XERR)    sc->status.snmp.t3.line |= TLINE_RX_RAI;
1223   if (ctl1   & CTL1_TXAIS)     sc->status.snmp.t3.line |= TLINE_TX_AIS;
1224   if (stat16 & STAT16_RAIS)    sc->status.snmp.t3.line |= TLINE_RX_AIS;
1225   if (stat16 & STAT16_ROOF)    sc->status.snmp.t3.line |= TLINE_LOF;
1226   if (stat16 & STAT16_RLOS)    sc->status.snmp.t3.line |= TLINE_LOS;
1227   if (stat16 & STAT16_SEF)     sc->status.snmp.t3.line |= T3LINE_SEF;
1228
1229   /* SNMP Loopback Status */
1230   sc->status.snmp.t3.loop &= ~TLOOP_FAR_LINE;
1231   if (sc->config.loop_back == CFG_LOOP_TULIP)
1232                                sc->status.snmp.t3.loop |= TLOOP_NEAR_OTHER;
1233   if (ctl1  & CTL1_3LOOP)      sc->status.snmp.t3.loop |= TLOOP_NEAR_INWARD;
1234   if (mii16 & MII16_DS3_TRLBK) sc->status.snmp.t3.loop |= TLOOP_NEAR_OTHER;
1235   if (mii16 & MII16_DS3_LNLBK) sc->status.snmp.t3.loop |= TLOOP_NEAR_LINE;
1236 /*if (ctl12 & CTL12_RTPLOOP)   sc->status.snmp.t3.loop |= TLOOP_NEAR_PAYLOAD; */
1237
1238   /* Remember this state until next time. */
1239   sc->last_stat16 = stat16;
1240
1241   /* If an INWARD loopback is in effect, link status is UP */
1242   if (sc->config.loop_back != CFG_LOOP_NONE) /* XXX INWARD ONLY */
1243     link_status = STATUS_UP;
1244
1245   return link_status;
1246   }
1247
1248 /* IOCTL SYSCALL: can sleep. */
1249 static void
1250 t3_send_dbl_feac(softc_t *sc, int feac1, int feac2)
1251   {
1252   u_int8_t tx_feac;
1253   int i;
1254
1255   /* The FEAC transmitter could be sending a continuous */
1256   /*  FEAC msg when told to send a double FEAC message. */
1257   /* So save the current state of the FEAC transmitter. */
1258   tx_feac = read_framer(sc, T3CSR_TX_FEAC);
1259   /* Load second FEAC code and stop FEAC transmitter. */
1260   write_framer(sc, T3CSR_TX_FEAC,  CTL5_EMODE + feac2);
1261   /* FEAC transmitter sends 10 more FEACs and then stops. */
1262   SLEEP(20000); /* sending one FEAC takes 1700 uSecs */
1263   /* Load first FEAC code and start FEAC transmitter. */
1264   write_framer(sc, T3CSR_DBL_FEAC, CTL13_DFEXEC + feac1);
1265   /* Wait for double FEAC sequence to complete -- about 70 ms. */
1266   for (i=0; i<10; i++) /* max delay 100 ms */
1267     if (read_framer(sc, T3CSR_DBL_FEAC) & CTL13_DFEXEC) SLEEP(10000);
1268   /* Flush received FEACS; don't respond to our own loop cmd! */
1269   while (read_framer(sc, T3CSR_FEAC_STK) & FEAC_STK_VALID) DELAY(1); /* XXX HANG */
1270   /* Restore previous state of the FEAC transmitter. */
1271   /* If it was sending a continous FEAC, it will resume. */
1272   write_framer(sc, T3CSR_TX_FEAC, tx_feac);
1273   }
1274
1275 /* IOCTL SYSCALL: can sleep. */
1276 static int
1277 t3_ioctl(softc_t *sc, struct ioctl *ioctl)
1278   {
1279   int error = 0;
1280
1281   switch (ioctl->cmd)
1282     {
1283     case IOCTL_SNMP_SEND:  /* set opstatus? */
1284       {
1285       if (sc->config.format != CFG_FORMAT_T3CPAR)
1286         error = EINVAL;
1287       else if (ioctl->data == TSEND_LINE)
1288         {
1289         sc->status.snmp.t3.loop |= TLOOP_FAR_LINE;
1290         t3_send_dbl_feac(sc, T3BOP_LINE_UP, T3BOP_LOOP_DS3);
1291         }
1292       else if (ioctl->data == TSEND_RESET)
1293         {
1294         t3_send_dbl_feac(sc, T3BOP_LINE_DOWN, T3BOP_LOOP_DS3);
1295         sc->status.snmp.t3.loop &= ~TLOOP_FAR_LINE;
1296         }
1297       else
1298         error = EINVAL;
1299       break;
1300       }
1301     case IOCTL_SNMP_LOOP:  /* set opstatus = test? */
1302       {
1303       if (ioctl->data == CFG_LOOP_NONE)
1304         {
1305         clr_mii16_bits(sc, MII16_DS3_FRAME);
1306         clr_mii16_bits(sc, MII16_DS3_TRLBK);
1307         clr_mii16_bits(sc, MII16_DS3_LNLBK);
1308         write_framer(sc, T3CSR_CTL1,
1309          read_framer(sc, T3CSR_CTL1) & ~CTL1_3LOOP);
1310         write_framer(sc, T3CSR_CTL12,
1311          read_framer(sc, T3CSR_CTL12) & ~(CTL12_RTPLOOP | CTL12_RTPLLEN));
1312         }
1313       else if (ioctl->data == CFG_LOOP_LINE)
1314         set_mii16_bits(sc, MII16_DS3_LNLBK);
1315       else if (ioctl->data == CFG_LOOP_OTHER)
1316         set_mii16_bits(sc, MII16_DS3_TRLBK);
1317       else if (ioctl->data == CFG_LOOP_INWARD)
1318         write_framer(sc, T3CSR_CTL1,
1319          read_framer(sc, T3CSR_CTL1) | CTL1_3LOOP);
1320       else if (ioctl->data == CFG_LOOP_DUAL)
1321         {
1322         set_mii16_bits(sc, MII16_DS3_LNLBK);
1323         write_framer(sc, T3CSR_CTL1,
1324          read_framer(sc, T3CSR_CTL1) | CTL1_3LOOP);
1325         }
1326       else if (ioctl->data == CFG_LOOP_PAYLOAD)
1327         {
1328         set_mii16_bits(sc, MII16_DS3_FRAME);
1329         write_framer(sc, T3CSR_CTL12,
1330          read_framer(sc, T3CSR_CTL12) |  CTL12_RTPLOOP);
1331         write_framer(sc, T3CSR_CTL12,
1332          read_framer(sc, T3CSR_CTL12) |  CTL12_RTPLLEN);
1333         DELAY(25); /* at least two frames (22 uS) */
1334         write_framer(sc, T3CSR_CTL12,
1335          read_framer(sc, T3CSR_CTL12) & ~CTL12_RTPLLEN);
1336         }
1337       else
1338         error = EINVAL;
1339       break;
1340       }
1341     default:
1342       error = EINVAL;
1343       break;
1344     }
1345
1346   return error;
1347   }
1348
1349 /* begin SSI card code */
1350
1351 /* Must not sleep. */
1352 static void
1353 ssi_config(softc_t *sc)
1354   {
1355   if (sc->status.card_type == 0)
1356     { /* defaults */
1357     sc->status.card_type  = TLP_CSID_SSI;
1358     sc->config.crc_len    = CFG_CRC_16;
1359     sc->config.loop_back  = CFG_LOOP_NONE;
1360     sc->config.tx_clk_src = CFG_CLKMUX_ST;
1361     sc->config.dte_dce    = CFG_DTE;
1362     sc->config.synth.n    = 51; /* 1.536 MHz */
1363     sc->config.synth.m    = 83;
1364     sc->config.synth.v    =  1;
1365     sc->config.synth.x    =  1;
1366     sc->config.synth.r    =  1;
1367     sc->config.synth.prescale = 4;
1368     }
1369
1370   /* Disable the TX clock driver while programming the oscillator. */
1371   clr_gpio_bits(sc, GPIO_SSI_DCE);
1372   make_gpio_output(sc, GPIO_SSI_DCE);
1373
1374   /* Program the synthesized oscillator. */
1375   write_synth(sc, &sc->config.synth);
1376
1377   /* Set DTE/DCE mode. */
1378   /* If DTE mode then DCD & TXC are received. */
1379   /* If DCE mode then DCD & TXC are driven. */
1380   /* Boards with MII rev=4.0 don't drive DCD. */
1381   if (sc->config.dte_dce == CFG_DCE)
1382     set_gpio_bits(sc, GPIO_SSI_DCE);
1383   else
1384     clr_gpio_bits(sc, GPIO_SSI_DCE);
1385   make_gpio_output(sc, GPIO_SSI_DCE);
1386
1387   /* Set CRC length. */
1388   if (sc->config.crc_len == CFG_CRC_32)
1389     set_mii16_bits(sc, MII16_SSI_CRC32);
1390   else
1391     clr_mii16_bits(sc, MII16_SSI_CRC32);
1392
1393   /* Loop towards host thru cable drivers and receivers. */
1394   /* Asserts DCD at the far end of a null modem cable. */
1395   if (sc->config.loop_back == CFG_LOOP_PINS)
1396     set_mii16_bits(sc, MII16_SSI_LOOP);
1397   else
1398     clr_mii16_bits(sc, MII16_SSI_LOOP);
1399
1400   /* Assert pin LL in modem conn: ask modem for local loop. */
1401   /* Asserts TM at the far end of a null modem cable. */
1402   if (sc->config.loop_back == CFG_LOOP_LL)
1403     set_mii16_bits(sc, MII16_SSI_LL);
1404   else
1405     clr_mii16_bits(sc, MII16_SSI_LL);
1406
1407   /* Assert pin RL in modem conn: ask modem for remote loop. */
1408   if (sc->config.loop_back == CFG_LOOP_RL)
1409     set_mii16_bits(sc, MII16_SSI_RL);
1410   else
1411     clr_mii16_bits(sc, MII16_SSI_RL);
1412   }
1413
1414 static void
1415 ssi_ident(softc_t *sc)
1416   {
1417   printf(", LTC1343/44");
1418   }
1419
1420 /* Called once a second; must not sleep. */
1421 static int
1422 ssi_watchdog(softc_t *sc)
1423   {
1424   u_int16_t cable;
1425   u_int16_t mii16 = read_mii(sc, 16) & MII16_SSI_MODEM;
1426   int link_status = STATUS_UP;
1427
1428   /* Software is alive. */
1429   led_inv(sc, MII16_SSI_LED_UL);
1430
1431   /* Check the transmit clock. */
1432   if (sc->status.tx_speed == 0)
1433     {
1434     led_on(sc, MII16_SSI_LED_UR);
1435     link_status = STATUS_DOWN;
1436     }
1437   else
1438     led_off(sc, MII16_SSI_LED_UR);
1439
1440   /* Check the external cable. */
1441   cable = read_mii(sc, 17);
1442   cable = cable &  MII17_SSI_CABLE_MASK;
1443   cable = cable >> MII17_SSI_CABLE_SHIFT;
1444   if (cable == 7)
1445     {
1446     led_off(sc, MII16_SSI_LED_LL); /* no cable */
1447     link_status = STATUS_DOWN;
1448     }
1449   else
1450     led_on(sc, MII16_SSI_LED_LL);
1451
1452   /* The unit at the other end of the cable is ready if: */
1453   /*  DTE mode and DCD pin is asserted */
1454   /*  DCE mode and DSR pin is asserted */
1455   if (((sc->config.dte_dce == CFG_DTE) && ((mii16 & MII16_SSI_DCD)==0)) ||
1456       ((sc->config.dte_dce == CFG_DCE) && ((mii16 & MII16_SSI_DSR)==0)))
1457     {
1458     led_off(sc, MII16_SSI_LED_LR);
1459     link_status = STATUS_DOWN;
1460     }
1461   else
1462     led_on(sc, MII16_SSI_LED_LR);
1463
1464   if (DRIVER_DEBUG && (cable != sc->status.cable_type))
1465     printf("%s: SSI cable type changed to '%s'\n",
1466      NAME_UNIT, ssi_cables[cable]);
1467   sc->status.cable_type = cable;
1468
1469   /* Print the modem control signals if they changed. */
1470   if ((DRIVER_DEBUG) && (mii16 != sc->last_mii16))
1471     {
1472     char *on = "ON ", *off = "OFF";
1473     printf("%s: DTR=%s DSR=%s RTS=%s CTS=%s DCD=%s RI=%s LL=%s RL=%s TM=%s\n",
1474      NAME_UNIT,
1475      (mii16 & MII16_SSI_DTR) ? on : off,
1476      (mii16 & MII16_SSI_DSR) ? on : off,
1477      (mii16 & MII16_SSI_RTS) ? on : off,
1478      (mii16 & MII16_SSI_CTS) ? on : off,
1479      (mii16 & MII16_SSI_DCD) ? on : off,
1480      (mii16 & MII16_SSI_RI)  ? on : off,
1481      (mii16 & MII16_SSI_LL)  ? on : off,
1482      (mii16 & MII16_SSI_RL)  ? on : off,
1483      (mii16 & MII16_SSI_TM)  ? on : off);
1484     }
1485
1486   /* SNMP one-second report */
1487   sc->status.snmp.ssi.sigs = mii16 & MII16_SSI_MODEM;
1488
1489   /* Remember this state until next time. */
1490   sc->last_mii16 = mii16;
1491
1492   /* If a loop back is in effect, link status is UP */
1493   if (sc->config.loop_back != CFG_LOOP_NONE)
1494     link_status = STATUS_UP;
1495
1496   return link_status;
1497   }
1498
1499 /* IOCTL SYSCALL: can sleep (but doesn't). */
1500 static int
1501 ssi_ioctl(softc_t *sc, struct ioctl *ioctl)
1502   {
1503   int error = 0;
1504
1505   if (ioctl->cmd == IOCTL_SNMP_SIGS)
1506     {
1507     u_int16_t mii16 = read_mii(sc, 16);
1508     mii16 &= ~MII16_SSI_MODEM;
1509     mii16 |= (MII16_SSI_MODEM & ioctl->data);
1510     write_mii(sc, 16, mii16);
1511     }
1512   else if (ioctl->cmd == IOCTL_SET_STATUS)
1513     {
1514     if (ioctl->data != 0)
1515       set_mii16_bits(sc, (MII16_SSI_DTR | MII16_SSI_RTS | MII16_SSI_DCD));
1516     else
1517       clr_mii16_bits(sc, (MII16_SSI_DTR | MII16_SSI_RTS | MII16_SSI_DCD));
1518     }
1519   else
1520     error = EINVAL;
1521
1522   return error;
1523   }
1524
1525 /* begin T1E1 card code */
1526
1527 /* Must not sleep. */
1528 static void
1529 t1_config(softc_t *sc)
1530   {
1531   int i;
1532   u_int8_t pulse, lbo, gain;
1533
1534   if (sc->status.card_type == 0)
1535     {  /* defaults */
1536     sc->status.card_type   = TLP_CSID_T1E1;
1537     sc->config.crc_len     = CFG_CRC_16;
1538     sc->config.loop_back   = CFG_LOOP_NONE;
1539     sc->config.tx_clk_src  = CFG_CLKMUX_INT;
1540     sc->config.format      = CFG_FORMAT_T1ESF;
1541     sc->config.cable_len   = 10;
1542     sc->config.time_slots  = 0x01FFFFFE;
1543     sc->config.tx_pulse    = CFG_PULSE_AUTO;
1544     sc->config.rx_gain     = CFG_GAIN_AUTO;
1545     sc->config.tx_lbo      = CFG_LBO_AUTO;
1546
1547     /* Bt8370 occasionally powers up in a loopback mode. */
1548     /* Data sheet says zero LOOP reg and do a s/w reset. */
1549     write_framer(sc, Bt8370_LOOP, 0x00); /* no loopback */
1550     write_framer(sc, Bt8370_CR0,  0x80); /* s/w reset */
1551     for (i=0; i<10; i++) /* max delay 10 ms */
1552       if (read_framer(sc, Bt8370_CR0) & 0x80) DELAY(1000);
1553     }
1554
1555   /* Set CRC length. */
1556   if (sc->config.crc_len == CFG_CRC_32)
1557     set_mii16_bits(sc, MII16_T1_CRC32);
1558   else
1559     clr_mii16_bits(sc, MII16_T1_CRC32);
1560
1561   /* Invert HDLC payload data in SF/AMI mode. */
1562   /* HDLC stuff bits satisfy T1 pulse density. */
1563   if (FORMAT_T1SF)
1564     set_mii16_bits(sc, MII16_T1_INVERT);
1565   else
1566     clr_mii16_bits(sc, MII16_T1_INVERT);
1567
1568   /* Set the transmitter output impedance. */
1569   if (FORMAT_E1ANY) set_mii16_bits(sc, MII16_T1_Z);
1570
1571   /* 001:CR0 -- Control Register 0 - T1/E1 and frame format */
1572   write_framer(sc, Bt8370_CR0, sc->config.format);
1573
1574   /* 002:JAT_CR -- Jitter Attenuator Control Register */
1575   if (sc->config.tx_clk_src == CFG_CLKMUX_RT) /* loop timing */
1576     write_framer(sc, Bt8370_JAT_CR, 0xA3); /* JAT in RX path */
1577   else
1578     { /* 64-bit elastic store; free-running JCLK and CLADO */
1579     write_framer(sc, Bt8370_JAT_CR, 0x4B); /* assert jcenter */
1580     write_framer(sc, Bt8370_JAT_CR, 0x43); /* release jcenter */
1581     }
1582
1583   /* 00C-013:IERn -- Interrupt Enable Registers */
1584   for (i=Bt8370_IER7; i<=Bt8370_IER0; i++)
1585     write_framer(sc, i, 0); /* no interrupts; polled */
1586
1587   /* 014:LOOP -- loopbacks */
1588   if      (sc->config.loop_back == CFG_LOOP_PAYLOAD)
1589     write_framer(sc, Bt8370_LOOP, LOOP_PAYLOAD);
1590   else if (sc->config.loop_back == CFG_LOOP_LINE)
1591     write_framer(sc, Bt8370_LOOP, LOOP_LINE);
1592   else if (sc->config.loop_back == CFG_LOOP_OTHER)
1593     write_framer(sc, Bt8370_LOOP, LOOP_ANALOG);
1594   else if (sc->config.loop_back == CFG_LOOP_INWARD)
1595     write_framer(sc, Bt8370_LOOP, LOOP_FRAMER);
1596   else if (sc->config.loop_back == CFG_LOOP_DUAL)
1597     write_framer(sc, Bt8370_LOOP, LOOP_DUAL);
1598   else
1599     write_framer(sc, Bt8370_LOOP, 0x00); /* no loopback */
1600
1601   /* 015:DL3_TS -- Data Link 3 */
1602   write_framer(sc, Bt8370_DL3_TS, 0x00); /* disabled */
1603
1604   /* 018:PIO -- Programmable I/O */
1605   write_framer(sc, Bt8370_PIO, 0xFF); /* all pins are outputs */
1606
1607   /* 019:POE -- Programmable Output Enable */
1608   write_framer(sc, Bt8370_POE, 0x00); /* all outputs are enabled */
1609
1610   /* 01A;CMUX -- Clock Input Mux */
1611   if (sc->config.tx_clk_src == CFG_CLKMUX_EXT)
1612     write_framer(sc, Bt8370_CMUX, 0x0C); /* external timing */
1613   else
1614     write_framer(sc, Bt8370_CMUX, 0x0F); /* internal timing */
1615
1616   /* 020:LIU_CR -- Line Interface Unit Config Register */
1617   write_framer(sc, Bt8370_LIU_CR, 0xC1); /* reset LIU, squelch */
1618
1619   /* 022:RLIU_CR -- RX Line Interface Unit Config Reg */
1620   /* Errata sheet says don't use freeze-short, but we do anyway! */
1621   write_framer(sc, Bt8370_RLIU_CR, 0xB1); /* AGC=2048, Long Eye */
1622
1623   /* Select Rx sensitivity based on cable length. */
1624   if ((gain = sc->config.rx_gain) == CFG_GAIN_AUTO)
1625     {
1626     if      (sc->config.cable_len > 2000)
1627       gain = CFG_GAIN_EXTEND;
1628     else if (sc->config.cable_len > 1000)
1629       gain = CFG_GAIN_LONG;
1630     else if (sc->config.cable_len > 100)
1631       gain = CFG_GAIN_MEDIUM;
1632     else
1633       gain = CFG_GAIN_SHORT;
1634     }
1635
1636   /* 024:VGA_MAX -- Variable Gain Amplifier Max gain */
1637   write_framer(sc, Bt8370_VGA_MAX, gain);
1638
1639   /* 028:PRE_EQ -- Pre Equalizer */
1640   if (gain == CFG_GAIN_EXTEND)
1641     write_framer(sc, Bt8370_PRE_EQ, 0xE6);  /* ON; thresh 6 */
1642   else
1643     write_framer(sc, Bt8370_PRE_EQ, 0xA6);  /* OFF; thresh 6 */
1644
1645   /* 038-03C:GAINn -- RX Equalizer gain thresholds */
1646   write_framer(sc, Bt8370_GAIN0, 0x24);
1647   write_framer(sc, Bt8370_GAIN1, 0x28);
1648   write_framer(sc, Bt8370_GAIN2, 0x2C);
1649   write_framer(sc, Bt8370_GAIN3, 0x30);
1650   write_framer(sc, Bt8370_GAIN4, 0x34);
1651
1652   /* 040:RCR0 -- Receiver Control Register 0 */
1653   if      (FORMAT_T1ESF)
1654     write_framer(sc, Bt8370_RCR0, 0x05); /* B8ZS, 2/5 FErrs */
1655   else if (FORMAT_T1SF)
1656     write_framer(sc, Bt8370_RCR0, 0x84); /* AMI,  2/5 FErrs */
1657   else if (FORMAT_E1NONE)
1658     write_framer(sc, Bt8370_RCR0, 0x41); /* HDB3, rabort */
1659   else if (FORMAT_E1CRC)
1660     write_framer(sc, Bt8370_RCR0, 0x09); /* HDB3, 3 FErrs or 915 CErrs */
1661   else  /* E1 no CRC */
1662     write_framer(sc, Bt8370_RCR0, 0x19); /* HDB3, 3 FErrs */
1663
1664   /* 041:RPATT -- Receive Test Pattern configuration */
1665   write_framer(sc, Bt8370_RPATT, 0x3E); /* looking for framed QRSS */
1666
1667   /* 042:RLB -- Receive Loop Back code detector config */
1668   write_framer(sc, Bt8370_RLB, 0x09); /* 6 bits down; 5 bits up */
1669
1670   /* 043:LBA -- Loop Back Activate code */
1671   write_framer(sc, Bt8370_LBA, 0x08); /* 10000 10000 10000 ... */
1672
1673   /* 044:LBD -- Loop Back Deactivate code */
1674   write_framer(sc, Bt8370_LBD, 0x24); /* 100100 100100 100100 ... */
1675
1676   /* 045:RALM -- Receive Alarm signal configuration */
1677   write_framer(sc, Bt8370_RALM, 0x0C); /* yel_intg rlof_intg */
1678
1679   /* 046:LATCH -- Alarm/Error/Counter Latch register */
1680   write_framer(sc, Bt8370_LATCH, 0x1F); /* stop_cnt latch_{cnt,err,alm} */
1681
1682   /* Select Pulse Shape based on cable length (T1 only). */
1683   if ((pulse = sc->config.tx_pulse) == CFG_PULSE_AUTO)
1684     {
1685     if (FORMAT_T1ANY)
1686       {
1687       if      (sc->config.cable_len > 200)
1688         pulse = CFG_PULSE_T1CSU;
1689       else if (sc->config.cable_len > 160)
1690         pulse = CFG_PULSE_T1DSX4;
1691       else if (sc->config.cable_len > 120)
1692         pulse = CFG_PULSE_T1DSX3;
1693       else if (sc->config.cable_len > 80)
1694         pulse = CFG_PULSE_T1DSX2;
1695       else if (sc->config.cable_len > 40)
1696         pulse = CFG_PULSE_T1DSX1;
1697       else
1698         pulse = CFG_PULSE_T1DSX0;
1699       }
1700     else
1701       pulse = CFG_PULSE_E1TWIST;
1702     }
1703
1704   /* Select Line Build Out based on cable length (T1CSU only). */
1705   if ((lbo = sc->config.tx_lbo) == CFG_LBO_AUTO)
1706     {
1707     if (pulse == CFG_PULSE_T1CSU)
1708       {
1709       if      (sc->config.cable_len > 1500)
1710         lbo = CFG_LBO_0DB;
1711       else if (sc->config.cable_len > 1000)
1712         lbo = CFG_LBO_7DB;
1713       else if (sc->config.cable_len >  500)
1714         lbo = CFG_LBO_15DB;
1715       else
1716         lbo = CFG_LBO_22DB;
1717       }
1718     else
1719       lbo = 0;
1720     }
1721
1722   /* 068:TLIU_CR -- Transmit LIU Control Register */
1723   write_framer(sc, Bt8370_TLIU_CR, (0x40 | (lbo & 0x30) | (pulse & 0x0E)));
1724
1725   /* 070:TCR0 -- Transmit Framer Configuration */
1726   write_framer(sc, Bt8370_TCR0, sc->config.format>>1);
1727
1728   /* 071:TCR1 -- Transmitter Configuration */
1729   if (FORMAT_T1SF)
1730     write_framer(sc, Bt8370_TCR1, 0x43); /* tabort, AMI PDV enforced */
1731   else
1732     write_framer(sc, Bt8370_TCR1, 0x41); /* tabort, B8ZS or HDB3 */
1733
1734   /* 072:TFRM -- Transmit Frame format       MYEL YEL MF FE CRC FBIT */
1735   if      (sc->config.format == CFG_FORMAT_T1ESF)
1736     write_framer(sc, Bt8370_TFRM, 0x0B); /*  -   YEL MF -  CRC FBIT */
1737   else if (sc->config.format == CFG_FORMAT_T1SF)
1738     write_framer(sc, Bt8370_TFRM, 0x19); /*  -   YEL MF -   -  FBIT */
1739   else if (sc->config.format == CFG_FORMAT_E1FAS)
1740     write_framer(sc, Bt8370_TFRM, 0x11); /*  -   YEL -  -   -  FBIT */
1741   else if (sc->config.format == CFG_FORMAT_E1FASCRC)
1742     write_framer(sc, Bt8370_TFRM, 0x1F); /*  -   YEL MF FE CRC FBIT */
1743   else if (sc->config.format == CFG_FORMAT_E1FASCAS)
1744     write_framer(sc, Bt8370_TFRM, 0x31); /* MYEL YEL -  -   -  FBIT */
1745   else if (sc->config.format == CFG_FORMAT_E1FASCRCCAS)
1746     write_framer(sc, Bt8370_TFRM, 0x3F); /* MYEL YEL MF FE CRC FBIT */
1747   else if (sc->config.format == CFG_FORMAT_E1NONE)
1748     write_framer(sc, Bt8370_TFRM, 0x00); /* NO FRAMING BITS AT ALL! */
1749
1750   /* 073:TERROR -- Transmit Error Insert */
1751   write_framer(sc, Bt8370_TERROR, 0x00); /* no errors, please! */
1752
1753   /* 074:TMAN -- Transmit Manual Sa-byte/FEBE configuration */
1754   write_framer(sc, Bt8370_TMAN, 0x00); /* none */
1755
1756   /* 075:TALM -- Transmit Alarm Signal Configuration */
1757   if (FORMAT_E1ANY)
1758     write_framer(sc, Bt8370_TALM, 0x38); /* auto_myel auto_yel auto_ais */
1759   else if (FORMAT_T1ANY)
1760     write_framer(sc, Bt8370_TALM, 0x18); /* auto_yel auto_ais */
1761
1762   /* 076:TPATT -- Transmit Test Pattern Configuration */
1763   write_framer(sc, Bt8370_TPATT, 0x00); /* disabled */
1764
1765   /* 077:TLB -- Transmit Inband Loopback Code Configuration */
1766   write_framer(sc, Bt8370_TLB, 0x00); /* disabled */
1767
1768   /* 090:CLAD_CR -- Clack Rate Adapter Configuration */
1769   if (FORMAT_T1ANY)
1770     write_framer(sc, Bt8370_CLAD_CR, 0x06); /* loop filter gain 1/2^6 */
1771   else
1772     write_framer(sc, Bt8370_CLAD_CR, 0x08); /* loop filter gain 1/2^8 */
1773
1774   /* 091:CSEL -- CLAD frequency Select */
1775   if (FORMAT_T1ANY)
1776     write_framer(sc, Bt8370_CSEL, 0x55); /* 1544 kHz */
1777   else
1778     write_framer(sc, Bt8370_CSEL, 0x11); /* 2048 kHz */
1779
1780   /* 092:CPHASE -- CLAD Phase detector */
1781   if (FORMAT_T1ANY)
1782     write_framer(sc, Bt8370_CPHASE, 0x22); /* phase compare @  386 kHz */
1783   else
1784     write_framer(sc, Bt8370_CPHASE, 0x00); /* phase compare @ 2048 kHz */
1785
1786   if (FORMAT_T1ESF) /* BOP & PRM are enabled in T1ESF mode only. */
1787     {
1788     /* 0A0:BOP -- Bit Oriented Protocol messages */
1789     write_framer(sc, Bt8370_BOP, RBOP_25 | TBOP_OFF);
1790     /* 0A4:DL1_TS -- Data Link 1 Time Slot Enable */
1791     write_framer(sc, Bt8370_DL1_TS, 0x40); /* FDL bits in odd frames */
1792     /* 0A6:DL1_CTL -- Data Link 1 Control */
1793     write_framer(sc, Bt8370_DL1_CTL, 0x03); /* FCS mode, TX on, RX on */
1794     /* 0A7:RDL1_FFC -- Rx Data Link 1 Fifo Fill Control */
1795     write_framer(sc, Bt8370_RDL1_FFC, 0x30); /* assert "near full" at 48 */
1796     /* 0AA:PRM -- Performance Report Messages */
1797     write_framer(sc, Bt8370_PRM, 0x80);
1798     }
1799
1800   /* 0D0:SBI_CR -- System Bus Interface Configuration Register */
1801   if (FORMAT_T1ANY)
1802     write_framer(sc, Bt8370_SBI_CR, 0x47); /* 1.544 with 24 TS +Fbits */
1803   else
1804     write_framer(sc, Bt8370_SBI_CR, 0x46); /* 2.048 with 32 TS */
1805
1806   /* 0D1:RSB_CR -- Receive System Bus Configuration Register */
1807   /* Change RINDO & RFSYNC on falling edge of RSBCLKI. */
1808   write_framer(sc, Bt8370_RSB_CR, 0x70);
1809
1810   /* 0D2,0D3:RSYNC_{TS,BIT} -- Receive frame Sync offset */
1811   write_framer(sc, Bt8370_RSYNC_BIT, 0x00);
1812   write_framer(sc, Bt8370_RSYNC_TS,  0x00);
1813
1814   /* 0D4:TSB_CR -- Transmit System Bus Configuration Register */
1815   /* Change TINDO & TFSYNC on falling edge of TSBCLKI. */
1816   write_framer(sc, Bt8370_TSB_CR, 0x30);
1817
1818   /* 0D5,0D6:TSYNC_{TS,BIT} -- Transmit frame Sync offset */
1819   write_framer(sc, Bt8370_TSYNC_BIT, 0x00);
1820   write_framer(sc, Bt8370_TSYNC_TS,  0x00);
1821
1822   /* 0D7:RSIG_CR -- Receive SIGnalling Configuratin Register */
1823   write_framer(sc, Bt8370_RSIG_CR, 0x00);
1824
1825   /* Assign and configure 64Kb TIME SLOTS. */
1826   /* TS24..TS1 must be assigned for T1, TS31..TS0 for E1. */
1827   /* Timeslots with no user data have RINDO and TINDO off. */
1828   for (i=0; i<32; i++)
1829     {
1830     /* 0E0-0FF:SBCn -- System Bus Per-Channel Control */
1831     if      (FORMAT_T1ANY && (i==0 || i>24))
1832       write_framer(sc, Bt8370_SBCn +i, 0x00); /* not assigned in T1 mode */
1833     else if (FORMAT_E1ANY && (i==0)  && !FORMAT_E1NONE)
1834       write_framer(sc, Bt8370_SBCn +i, 0x01); /* assigned, TS0  o/h bits */
1835     else if (FORMAT_E1CAS && (i==16) && !FORMAT_E1NONE)
1836       write_framer(sc, Bt8370_SBCn +i, 0x01); /* assigned, TS16 o/h bits */
1837     else if ((sc->config.time_slots & (1<<i)) != 0)
1838       write_framer(sc, Bt8370_SBCn +i, 0x0D); /* assigned, RINDO, TINDO */
1839     else
1840       write_framer(sc, Bt8370_SBCn +i, 0x01); /* assigned, idle */
1841
1842     /* 100-11F:TPCn -- Transmit Per-Channel Control */
1843     if      (FORMAT_E1CAS && (i==0))
1844       write_framer(sc, Bt8370_TPCn +i, 0x30); /* tidle, sig=0000 (MAS) */
1845     else if (FORMAT_E1CAS && (i==16))
1846       write_framer(sc, Bt8370_TPCn +i, 0x3B); /* tidle, sig=1011 (XYXX) */
1847     else if ((sc->config.time_slots & (1<<i)) == 0)
1848       write_framer(sc, Bt8370_TPCn +i, 0x20); /* tidle: use TSLIP_LOn */
1849     else
1850       write_framer(sc, Bt8370_TPCn +i, 0x00); /* nothing special */
1851
1852     /* 140-15F:TSLIP_LOn -- Transmit PCM Slip Buffer */
1853     write_framer(sc, Bt8370_TSLIP_LOn +i, 0x7F); /* idle chan data */
1854     /* 180-19F:RPCn -- Receive Per-Channel Control */
1855     write_framer(sc, Bt8370_RPCn +i, 0x00);   /* nothing special */
1856     }
1857
1858   /* Enable transmitter output drivers. */
1859   set_mii16_bits(sc, MII16_T1_XOE);
1860   }
1861
1862 static void
1863 t1_ident(softc_t *sc)
1864   {
1865   printf(", Bt837%x rev %x",
1866    read_framer(sc, Bt8370_DID)>>4,
1867    read_framer(sc, Bt8370_DID)&0x0F);
1868   }
1869
1870 /* Called once a second; must not sleep. */
1871 static int
1872 t1_watchdog(softc_t *sc)
1873   {
1874   u_int16_t LCV = 0, FERR = 0, CRC = 0, FEBE = 0;
1875   u_int8_t alm1, alm3, loop, isr0;
1876   int link_status = STATUS_UP;
1877   int i;
1878
1879   /* Read the alarm registers */
1880   alm1 = read_framer(sc, Bt8370_ALM1);
1881   alm3 = read_framer(sc, Bt8370_ALM3);
1882   loop = read_framer(sc, Bt8370_LOOP);
1883   isr0 = read_framer(sc, Bt8370_ISR0);
1884
1885   /* Always ignore the SIGFRZ alarm bit, */
1886   alm1 &= ~ALM1_SIGFRZ;
1887   if (FORMAT_T1ANY)  /* ignore RYEL in T1 modes */
1888     alm1 &= ~ALM1_RYEL;
1889   else if (FORMAT_E1NONE) /* ignore all alarms except LOS */
1890     alm1 &= ALM1_RLOS;
1891
1892   /* Software is alive. */
1893   led_inv(sc, MII16_T1_LED_GRN);
1894
1895   /* Receiving Alarm Indication Signal (AIS). */
1896   if ((alm1 & ALM1_RAIS)!=0) /* receiving ais */
1897     led_on(sc, MII16_T1_LED_BLU);
1898   else if ((alm1 & ALM1_RLOS)!=0) /* sending ais */
1899     led_inv(sc, MII16_T1_LED_BLU);
1900   else
1901     led_off(sc, MII16_T1_LED_BLU);
1902
1903   /* Receiving Remote Alarm Indication (RAI). */
1904   if ((alm1 & (ALM1_RMYEL | ALM1_RYEL))!=0) /* receiving rai */
1905     led_on(sc, MII16_T1_LED_YEL);
1906   else if ((alm1 & ALM1_RLOF)!=0) /* sending rai */
1907     led_inv(sc, MII16_T1_LED_YEL);
1908   else
1909     led_off(sc, MII16_T1_LED_YEL);
1910
1911   /* If any alarm bits are set then the link is 'down'. */
1912   /* The bad bits are: rmyel ryel rais ralos rlos rlof. */
1913   /* Some alarm bits have been masked by this point. */
1914   if (alm1 != 0) link_status = STATUS_DOWN;
1915
1916   /* Declare local Red Alarm if the link is down. */
1917   if (link_status == STATUS_DOWN)
1918     led_on(sc, MII16_T1_LED_RED);
1919   else if (sc->loop_timer != 0) /* loopback is active */
1920     led_inv(sc, MII16_T1_LED_RED);
1921   else
1922     led_off(sc, MII16_T1_LED_RED);
1923
1924   /* Print latched error bits if they changed. */
1925   if ((DRIVER_DEBUG) && (alm1 != sc->last_alm1))
1926     {
1927     char *on = "ON ", *off = "OFF";
1928     printf("%s: RLOF=%s RLOS=%s RALOS=%s RAIS=%s RYEL=%s RMYEL=%s\n",
1929      NAME_UNIT,
1930      (alm1 & ALM1_RLOF)  ? on : off,
1931      (alm1 & ALM1_RLOS)  ? on : off,
1932      (alm1 & ALM1_RALOS) ? on : off,
1933      (alm1 & ALM1_RAIS)  ? on : off,
1934      (alm1 & ALM1_RYEL)  ? on : off,
1935      (alm1 & ALM1_RMYEL) ? on : off);
1936     }
1937
1938   /* Check and print error counters if non-zero. */
1939   LCV = read_framer(sc, Bt8370_LCV_LO)  +
1940         (read_framer(sc, Bt8370_LCV_HI)<<8);
1941   if (!FORMAT_E1NONE)
1942     FERR = read_framer(sc, Bt8370_FERR_LO) +
1943           (read_framer(sc, Bt8370_FERR_HI)<<8);
1944   if (FORMAT_E1CRC || FORMAT_T1ESF)
1945     CRC  = read_framer(sc, Bt8370_CRC_LO)  +
1946           (read_framer(sc, Bt8370_CRC_HI)<<8);
1947   if (FORMAT_E1CRC)
1948     FEBE = read_framer(sc, Bt8370_FEBE_LO) +
1949           (read_framer(sc, Bt8370_FEBE_HI)<<8);
1950   /* Only LCV is valid if Out-Of-Frame */
1951   if (FORMAT_E1NONE) FERR = CRC = FEBE = 0;
1952   if ((DRIVER_DEBUG) && (LCV || FERR || CRC || FEBE))
1953     printf("%s: LCV=%u FERR=%u CRC=%u FEBE=%u\n",
1954      NAME_UNIT, LCV,   FERR,   CRC,   FEBE);
1955
1956   /* Driver keeps crude link-level error counters (SNMP is better). */
1957   sc->status.cntrs.lcv_errs  += LCV;
1958   sc->status.cntrs.frm_errs  += FERR;
1959   sc->status.cntrs.crc_errs  += CRC;
1960   sc->status.cntrs.febe_errs += FEBE;
1961
1962   /* Check for BOP messages in the ESF Facility Data Link. */
1963   if ((FORMAT_T1ESF) && (read_framer(sc, Bt8370_ISR1) & 0x80))
1964     {
1965     u_int8_t bop_code = read_framer(sc, Bt8370_RBOP) & 0x3F;
1966
1967     switch (bop_code)
1968       {
1969       case T1BOP_OOF:
1970         {
1971         if ((DRIVER_DEBUG) && ((sc->last_alm1 & ALM1_RMYEL)==0))
1972           printf("%s: Receiving a 'yellow alarm' BOP msg\n", NAME_UNIT);
1973         break;
1974         }
1975       case T1BOP_LINE_UP:
1976         {
1977         if (DRIVER_DEBUG)
1978           printf("%s: Received a 'line loopback activate' BOP msg\n", NAME_UNIT);
1979         write_framer(sc, Bt8370_LOOP, LOOP_LINE);
1980         sc->loop_timer = 305;
1981         break;
1982         }
1983       case T1BOP_LINE_DOWN:
1984         {
1985         if (DRIVER_DEBUG)
1986           printf("%s: Received a 'line loopback deactivate' BOP msg\n", NAME_UNIT);
1987         write_framer(sc, Bt8370_LOOP,
1988          read_framer(sc, Bt8370_LOOP) & ~LOOP_LINE);
1989         sc->loop_timer = 0;
1990         break;
1991         }
1992       case T1BOP_PAY_UP:
1993         {
1994         if (DRIVER_DEBUG)
1995           printf("%s: Received a 'payload loopback activate' BOP msg\n", NAME_UNIT);
1996         write_framer(sc, Bt8370_LOOP, LOOP_PAYLOAD);
1997         sc->loop_timer = 305;
1998         break;
1999         }
2000       case T1BOP_PAY_DOWN:
2001         {
2002         if (DRIVER_DEBUG)
2003           printf("%s: Received a 'payload loopback deactivate' BOP msg\n", NAME_UNIT);
2004         write_framer(sc, Bt8370_LOOP,
2005          read_framer(sc, Bt8370_LOOP) & ~LOOP_PAYLOAD);
2006         sc->loop_timer = 0;
2007         break;
2008         }
2009       default:
2010         {
2011         if (DRIVER_DEBUG)
2012           printf("%s: Received a type 0x%02X BOP msg\n", NAME_UNIT, bop_code);
2013         break;
2014         }
2015       }
2016     }
2017
2018   /* Check for HDLC pkts in the ESF Facility Data Link. */
2019   if ((FORMAT_T1ESF) && (read_framer(sc, Bt8370_ISR2) & 0x70))
2020     {
2021     /* while (not fifo-empty && not start-of-msg) flush fifo */
2022     while ((read_framer(sc, Bt8370_RDL1_STAT) & 0x0C) == 0)
2023       read_framer(sc, Bt8370_RDL1);
2024     /* If (not fifo-empty), then begin processing fifo contents. */
2025     if ((read_framer(sc, Bt8370_RDL1_STAT) & 0x0C) == 0x08)
2026       {
2027       u_int8_t msg[64];
2028       u_int8_t stat = read_framer(sc, Bt8370_RDL1);
2029       sc->status.cntrs.fdl_pkts++;
2030       for (i=0; i<(stat & 0x3F); i++)
2031         msg[i] = read_framer(sc, Bt8370_RDL1);
2032       /* Is this FDL message a T1.403 performance report? */
2033       if (((stat & 0x3F)==11) &&
2034           ((msg[0]==0x38) || (msg[0]==0x3A)) &&
2035            (msg[1]==1)   &&  (msg[2]==3))
2036         /* Copy 4 PRs from FDL pkt to SNMP struct. */
2037         memcpy(sc->status.snmp.t1.prm, msg+3, 8);
2038       }
2039     }
2040
2041   /* Check for inband loop up/down commands. */
2042   if (FORMAT_T1ANY)
2043     {
2044     u_int8_t isr6   = read_framer(sc, Bt8370_ISR6);
2045     u_int8_t alarm2 = read_framer(sc, Bt8370_ALM2);
2046     u_int8_t tlb    = read_framer(sc, Bt8370_TLB);
2047
2048     /* Inband Code == Loop Up && On Transition && Inband Tx Inactive */
2049     if ((isr6 & 0x40) && (alarm2 & 0x40) && ((tlb & 1)==0))
2050       { /* CSU loop up is 10000 10000 ... */
2051       if (DRIVER_DEBUG)
2052         printf("%s: Received a 'CSU Loop Up' inband msg\n", NAME_UNIT);
2053       write_framer(sc, Bt8370_LOOP, LOOP_LINE); /* Loop up */
2054       sc->loop_timer = 305;
2055       }
2056     /* Inband Code == Loop Down && On Transition && Inband Tx Inactive */
2057     if ((isr6 & 0x80) && (alarm2 & 0x80) && ((tlb & 1)==0))
2058       { /* CSU loop down is 100 100 100 ... */
2059       if (DRIVER_DEBUG)
2060         printf("%s: Received a 'CSU Loop Down' inband msg\n", NAME_UNIT);
2061       write_framer(sc, Bt8370_LOOP,
2062        read_framer(sc, Bt8370_LOOP) & ~LOOP_LINE); /* loop down */
2063       sc->loop_timer = 0;
2064       }
2065     }
2066
2067   /* Manually send Yellow Alarm BOP msgs. */
2068   if (FORMAT_T1ESF)
2069     {
2070     u_int8_t isr7 = read_framer(sc, Bt8370_ISR7);
2071
2072     if ((isr7 & 0x02) && (alm1 & 0x02)) /* RLOF on-transition */
2073       { /* Start sending continuous Yellow Alarm BOP messages. */
2074       write_framer(sc, Bt8370_BOP,  RBOP_25 | TBOP_CONT);
2075       write_framer(sc, Bt8370_TBOP, 0x00); /* send BOP; order matters */
2076       }
2077     else if ((isr7 & 0x02) && ((alm1 & 0x02)==0)) /* RLOF off-transition */
2078       { /* Stop sending continuous Yellow Alarm BOP messages. */
2079       write_framer(sc, Bt8370_BOP,  RBOP_25 | TBOP_OFF);
2080       }
2081     }
2082
2083   /* Time out loopback requests. */
2084   if (sc->loop_timer != 0)
2085     if (--sc->loop_timer == 0)
2086       if (loop != 0)
2087         {
2088         if (DRIVER_DEBUG)
2089           printf("%s: Timeout: Loop Down after 300 seconds\n", NAME_UNIT);
2090         write_framer(sc, Bt8370_LOOP, loop & ~(LOOP_PAYLOAD | LOOP_LINE));
2091         }
2092
2093   /* RX Test Pattern status */
2094   if ((DRIVER_DEBUG) && (isr0 & 0x10))
2095     printf("%s: RX Test Pattern Sync\n", NAME_UNIT);
2096
2097   /* SNMP Error Counters */
2098   sc->status.snmp.t1.lcv  = LCV;
2099   sc->status.snmp.t1.fe   = FERR;
2100   sc->status.snmp.t1.crc  = CRC;
2101   sc->status.snmp.t1.febe = FEBE;
2102
2103   /* SNMP Line Status */
2104   sc->status.snmp.t1.line = 0;
2105   if  (alm1 & ALM1_RMYEL)  sc->status.snmp.t1.line |= TLINE_RX_RAI;
2106   if  (alm1 & ALM1_RYEL)   sc->status.snmp.t1.line |= TLINE_RX_RAI;
2107   if  (alm1 & ALM1_RLOF)   sc->status.snmp.t1.line |= TLINE_TX_RAI;
2108   if  (alm1 & ALM1_RAIS)   sc->status.snmp.t1.line |= TLINE_RX_AIS;
2109   if  (alm1 & ALM1_RLOS)   sc->status.snmp.t1.line |= TLINE_TX_AIS;
2110   if  (alm1 & ALM1_RLOF)   sc->status.snmp.t1.line |= TLINE_LOF;
2111   if  (alm1 & ALM1_RLOS)   sc->status.snmp.t1.line |= TLINE_LOS;
2112   if  (alm3 & ALM3_RMAIS)  sc->status.snmp.t1.line |= T1LINE_RX_TS16_AIS;
2113   if  (alm3 & ALM3_SRED)   sc->status.snmp.t1.line |= T1LINE_TX_TS16_LOMF;
2114   if  (alm3 & ALM3_SEF)    sc->status.snmp.t1.line |= T1LINE_SEF;
2115   if  (isr0 & 0x10)        sc->status.snmp.t1.line |= T1LINE_RX_TEST;
2116   if ((alm1 & ALM1_RMYEL) && (FORMAT_E1CAS))
2117                            sc->status.snmp.t1.line |= T1LINE_RX_TS16_LOMF;
2118
2119   /* SNMP Loopback Status */
2120   sc->status.snmp.t1.loop &= ~(TLOOP_FAR_LINE | TLOOP_FAR_PAYLOAD);
2121   if (sc->config.loop_back == CFG_LOOP_TULIP)
2122                            sc->status.snmp.t1.loop |= TLOOP_NEAR_OTHER;
2123   if (loop & LOOP_PAYLOAD) sc->status.snmp.t1.loop |= TLOOP_NEAR_PAYLOAD;
2124   if (loop & LOOP_LINE)    sc->status.snmp.t1.loop |= TLOOP_NEAR_LINE;
2125   if (loop & LOOP_ANALOG)  sc->status.snmp.t1.loop |= TLOOP_NEAR_OTHER;
2126   if (loop & LOOP_FRAMER)  sc->status.snmp.t1.loop |= TLOOP_NEAR_INWARD;
2127
2128   /* Remember this state until next time. */
2129   sc->last_alm1 = alm1;
2130
2131   /* If an INWARD loopback is in effect, link status is UP */
2132   if (sc->config.loop_back != CFG_LOOP_NONE) /* XXX INWARD ONLY */
2133     link_status = STATUS_UP;
2134
2135   return link_status;
2136   }
2137
2138 /* IOCTL SYSCALL: can sleep. */
2139 static void
2140 t1_send_bop(softc_t *sc, int bop_code)
2141   {
2142   u_int8_t bop;
2143   int i;
2144
2145   /* The BOP transmitter could be sending a continuous */
2146   /*  BOP msg when told to send this BOP_25 message. */
2147   /* So save and restore the state of the BOP machine. */
2148   bop = read_framer(sc, Bt8370_BOP);
2149   write_framer(sc, Bt8370_BOP, RBOP_OFF | TBOP_OFF);
2150   for (i=0; i<40; i++) /* max delay 400 ms. */
2151     if (read_framer(sc, Bt8370_BOP_STAT) & 0x80) SLEEP(10000);
2152   /* send 25 repetitions of bop_code */
2153   write_framer(sc, Bt8370_BOP, RBOP_OFF | TBOP_25);
2154   write_framer(sc, Bt8370_TBOP, bop_code); /* order matters */
2155   /* wait for tx to stop */
2156   for (i=0; i<40; i++) /* max delay 400 ms. */
2157     if (read_framer(sc, Bt8370_BOP_STAT) & 0x80) SLEEP(10000);
2158   /* Restore previous state of the BOP machine. */
2159   write_framer(sc, Bt8370_BOP, bop);
2160   }
2161
2162 /* IOCTL SYSCALL: can sleep. */
2163 static int
2164 t1_ioctl(softc_t *sc, struct ioctl *ioctl)
2165   {
2166   int error = 0;
2167
2168   switch (ioctl->cmd)
2169     {
2170     case IOCTL_SNMP_SEND:  /* set opstatus? */
2171       {
2172       switch (ioctl->data)
2173         {
2174         case TSEND_NORMAL:
2175           {
2176           write_framer(sc, Bt8370_TPATT, 0x00); /* tx pattern generator off */
2177           write_framer(sc, Bt8370_RPATT, 0x00); /* rx pattern detector off */
2178           write_framer(sc, Bt8370_TLB,   0x00); /* tx inband generator off */
2179           break;
2180           }
2181         case TSEND_LINE:
2182           {
2183           if (FORMAT_T1ESF)
2184             t1_send_bop(sc, T1BOP_LINE_UP);
2185           else if (FORMAT_T1SF)
2186             {
2187             write_framer(sc, Bt8370_LBP, 0x08); /* 10000 10000 ... */
2188             write_framer(sc, Bt8370_TLB, 0x05); /* 5 bits, framed, start */
2189             }
2190           sc->status.snmp.t1.loop |= TLOOP_FAR_LINE;
2191           break;
2192           }
2193         case TSEND_PAYLOAD:
2194           {
2195           t1_send_bop(sc, T1BOP_PAY_UP);
2196           sc->status.snmp.t1.loop |= TLOOP_FAR_PAYLOAD;
2197           break;
2198           }
2199         case TSEND_RESET:
2200           {
2201           if (sc->status.snmp.t1.loop == TLOOP_FAR_LINE)
2202             {
2203             if (FORMAT_T1ESF)
2204               t1_send_bop(sc, T1BOP_LINE_DOWN);
2205             else if (FORMAT_T1SF)
2206               {
2207               write_framer(sc, Bt8370_LBP, 0x24); /* 100100 100100 ... */
2208               write_framer(sc, Bt8370_TLB, 0x09); /* 6 bits, framed, start */
2209               }
2210             sc->status.snmp.t1.loop &= ~TLOOP_FAR_LINE;
2211             }
2212           if (sc->status.snmp.t1.loop == TLOOP_FAR_PAYLOAD)
2213             {
2214             t1_send_bop(sc, T1BOP_PAY_DOWN);
2215             sc->status.snmp.t1.loop &= ~TLOOP_FAR_PAYLOAD;
2216             }
2217           break;
2218           }
2219         case TSEND_QRS:
2220           {
2221           write_framer(sc, Bt8370_TPATT, 0x1E); /* framed QRSS */
2222           break;
2223           }
2224         default:
2225           {
2226           error = EINVAL;
2227           break;
2228           }
2229         }
2230       break;
2231       }
2232     case IOCTL_SNMP_LOOP:  /* set opstatus = test? */
2233       {
2234       u_int8_t new_loop = 0;
2235
2236       if (ioctl->data == CFG_LOOP_NONE)
2237         new_loop = 0;
2238       else if (ioctl->data == CFG_LOOP_PAYLOAD)
2239         new_loop = LOOP_PAYLOAD;
2240       else if (ioctl->data == CFG_LOOP_LINE)
2241         new_loop = LOOP_LINE;
2242       else if (ioctl->data == CFG_LOOP_OTHER)
2243         new_loop = LOOP_ANALOG;
2244       else if (ioctl->data == CFG_LOOP_INWARD)
2245         new_loop = LOOP_FRAMER;
2246       else if (ioctl->data == CFG_LOOP_DUAL)
2247         new_loop = LOOP_DUAL;
2248       else
2249         error = EINVAL;
2250       if (error == 0)
2251         {
2252         write_framer(sc, Bt8370_LOOP, new_loop);
2253         sc->config.loop_back = ioctl->data;
2254         }
2255       break;
2256       }
2257     default:
2258       error = EINVAL;
2259       break;
2260     }
2261
2262   return error;
2263   }
2264
2265 static
2266 struct card hssi_card =
2267   {
2268   .config   = hssi_config,
2269   .ident    = hssi_ident,
2270   .watchdog = hssi_watchdog,
2271   .ioctl    = hssi_ioctl,
2272   };
2273
2274 static
2275 struct card t3_card =
2276   {
2277   .config   = t3_config,
2278   .ident    = t3_ident,
2279   .watchdog = t3_watchdog,
2280   .ioctl    = t3_ioctl,
2281   };
2282
2283 static
2284 struct card ssi_card =
2285   {
2286   .config   = ssi_config,
2287   .ident    = ssi_ident,
2288   .watchdog = ssi_watchdog,
2289   .ioctl    = ssi_ioctl,
2290   };
2291
2292 static
2293 struct card t1_card =
2294   {
2295   .config   = t1_config,
2296   .ident    = t1_ident,
2297   .watchdog = t1_watchdog,
2298   .ioctl    = t1_ioctl,
2299   };
2300
2301 /* RAWIP is raw IP packets (v4 or v6) in HDLC frames with NO HEADERS. */
2302 /* No HDLC Address/Control fields!  No line control protocol at all!  */
2303
2304 /* rxintr_cleanup calls this to give a newly arrived pkt to higher levels. */
2305 static void
2306 lmc_raw_input(struct ifnet *ifp, struct mbuf *mbuf)
2307   {
2308   softc_t *sc = IFP2SC(ifp);
2309
2310   M_SETFIB(mbuf, ifp->if_fib);
2311 # if INET
2312   if (mbuf->m_data[0]>>4 == 4)
2313     netisr_dispatch(NETISR_IP,   mbuf);
2314   else
2315 # endif
2316 # if INET6
2317   if (mbuf->m_data[0]>>4 == 6)
2318     netisr_dispatch(NETISR_IPV6, mbuf);
2319   else
2320 # endif
2321     {
2322     m_freem(mbuf);
2323     sc->status.cntrs.idiscards++;
2324     if (DRIVER_DEBUG)
2325       printf("%s: lmc_raw_input: rx pkt discarded: not IPv4 or IPv6\n",
2326         NAME_UNIT);
2327     }
2328   }
2329
2330 /*
2331  * We are "standing on the head of a pin" in these routines.
2332  * Tulip CSRs can be accessed, but nothing else is interrupt-safe!
2333  * Do NOT access: MII, GPIO, SROM, BIOSROM, XILINX, SYNTH, or DAC.
2334  */
2335
2336
2337 /* Singly-linked tail-queues hold mbufs with active DMA.
2338  * For RX, single mbuf clusters; for TX, mbuf chains are queued.
2339  * NB: mbufs are linked through their m_nextpkt field.
2340  * Callers must hold sc->bottom_lock; not otherwise locked.
2341  */
2342
2343 /* Put an mbuf (chain) on the tail of the descriptor ring queue. */
2344 static void  /* BSD version */
2345 mbuf_enqueue(struct desc_ring *ring, struct mbuf *m)
2346   {
2347   m->m_nextpkt = NULL;
2348   if (ring->tail == NULL)
2349     ring->head = m;
2350   else
2351     ring->tail->m_nextpkt = m;
2352   ring->tail = m;
2353   }
2354
2355 /* Get an mbuf (chain) from the head of the descriptor ring queue. */
2356 static struct mbuf*  /* BSD version */
2357 mbuf_dequeue(struct desc_ring *ring)
2358   {
2359   struct mbuf *m = ring->head;
2360   if (m != NULL)
2361     if ((ring->head = m->m_nextpkt) == NULL)
2362       ring->tail = NULL;
2363   return m;
2364   }
2365
2366 static void /* *** FreeBSD ONLY *** Callout from bus_dmamap_load() */
2367 fbsd_dmamap_load(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2368   {
2369   struct desc_ring *ring = arg;
2370   ring->nsegs = error ? 0 : nsegs;
2371   ring->segs[0] = segs[0];
2372   ring->segs[1] = segs[1];
2373   }
2374
2375 /* Initialize a DMA descriptor ring. */
2376 static int  /* BSD version */
2377 create_ring(softc_t *sc, struct desc_ring *ring, int num_descs)
2378   {
2379   struct dma_desc *descs;
2380   int size_descs = sizeof(struct dma_desc)*num_descs;
2381   int i, error = 0;
2382
2383   /* The DMA descriptor array must not cross a page boundary. */
2384   if (size_descs > PAGE_SIZE)
2385     {
2386     printf("%s: DMA descriptor array > PAGE_SIZE (%d)\n", NAME_UNIT, 
2387      (u_int)PAGE_SIZE);
2388     return EINVAL;
2389     }
2390
2391
2392   /* Create a DMA tag for descriptors and buffers. */
2393   if ((error = bus_dma_tag_create(bus_get_dma_tag(sc->dev),
2394    4, 0, BUS_SPACE_MAXADDR_32BIT,
2395    BUS_SPACE_MAXADDR, NULL, NULL, PAGE_SIZE, 2, PAGE_SIZE, BUS_DMA_ALLOCNOW,
2396    NULL, NULL,
2397    &ring->tag)))
2398     {
2399     printf("%s: bus_dma_tag_create() failed: error %d\n", NAME_UNIT, error);
2400     return error;
2401     }
2402
2403   /* Allocate wired physical memory for DMA descriptor array */
2404   /*  and map physical address to kernel virtual address. */
2405   if ((error = bus_dmamem_alloc(ring->tag, (void**)&ring->first,
2406    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &ring->map)))
2407     {
2408     printf("%s: bus_dmamem_alloc() failed; error %d\n", NAME_UNIT, error);
2409     return error;
2410     }
2411   descs = ring->first;
2412
2413   /* Map kernel virtual address to PCI address for DMA descriptor array. */
2414   if ((error = bus_dmamap_load(ring->tag, ring->map, descs, size_descs,
2415    fbsd_dmamap_load, ring, 0)))
2416     {
2417     printf("%s: bus_dmamap_load() failed; error %d\n", NAME_UNIT, error);
2418     return error;
2419     }
2420   ring->dma_addr = ring->segs[0].ds_addr;
2421
2422   /* Allocate dmamaps for each DMA descriptor. */
2423   for (i=0; i<num_descs; i++)
2424     if ((error = bus_dmamap_create(ring->tag, 0, &descs[i].map)))
2425       {
2426       printf("%s: bus_dmamap_create() failed; error %d\n", NAME_UNIT, error);
2427       return error;
2428       }
2429
2430
2431   ring->read  = descs;
2432   ring->write = descs;
2433   ring->first = descs;
2434   ring->last  = descs + num_descs -1;
2435   ring->last->control = TLP_DCTL_END_RING;
2436   ring->num_descs = num_descs;
2437   ring->size_descs = size_descs;
2438   ring->head = NULL;
2439   ring->tail = NULL;
2440
2441   return 0;
2442   }
2443
2444 /* Destroy a DMA descriptor ring */
2445 static void  /* BSD version */
2446 destroy_ring(softc_t *sc, struct desc_ring *ring)
2447   {
2448   struct dma_desc *desc;
2449   struct mbuf *m;
2450
2451   /* Free queued mbufs. */
2452   while ((m = mbuf_dequeue(ring)) != NULL)
2453     m_freem(m);
2454
2455   /* TX may have one pkt that is not on any queue. */
2456   if (sc->tx_mbuf != NULL)
2457     {
2458     m_freem(sc->tx_mbuf);
2459     sc->tx_mbuf = NULL;
2460     }
2461
2462   /* Unmap active DMA descriptors. */
2463   while (ring->read != ring->write)
2464     {
2465     bus_dmamap_unload(ring->tag, ring->read->map);
2466     if (ring->read++ == ring->last) ring->read = ring->first;
2467     }
2468
2469
2470   /* Free the dmamaps of all DMA descriptors. */
2471   for (desc=ring->first; desc!=ring->last+1; desc++)
2472     if (desc->map != NULL)
2473       bus_dmamap_destroy(ring->tag, desc->map);
2474
2475   /* Unmap PCI address for DMA descriptor array. */
2476   if (ring->dma_addr != 0)
2477     bus_dmamap_unload(ring->tag, ring->map);
2478   /* Free kernel memory for DMA descriptor array. */
2479   if (ring->first != NULL)
2480     bus_dmamem_free(ring->tag, ring->first, ring->map);
2481   /* Free the DMA tag created for this ring. */
2482   if (ring->tag != NULL)
2483     bus_dma_tag_destroy(ring->tag);
2484
2485   }
2486
2487 /* Clean up after a packet has been received. */
2488 static int  /* BSD version */
2489 rxintr_cleanup(softc_t *sc)
2490   {
2491   struct desc_ring *ring = &sc->rxring;
2492   struct dma_desc *first_desc, *last_desc;
2493   struct mbuf *first_mbuf=NULL, *last_mbuf=NULL;
2494   struct mbuf *new_mbuf;
2495   int pkt_len, desc_len;
2496
2497 #if defined(DEVICE_POLLING)
2498   /* Input packet flow control (livelock prevention): */
2499   /* Give pkts to higher levels only if quota is > 0. */
2500   if (sc->quota <= 0) return 0;
2501 #endif
2502
2503   /* This looks complicated, but remember: typically packets up */
2504   /*  to 2048 bytes long fit in one mbuf and use one descriptor. */
2505
2506   first_desc = last_desc = ring->read;
2507
2508   /* ASSERTION: If there is a descriptor in the ring and the hardware has */
2509   /*  finished with it, then that descriptor will have RX_FIRST_DESC set. */
2510   if ((ring->read != ring->write) && /* descriptor ring not empty */
2511      ((ring->read->status & TLP_DSTS_OWNER) == 0) && /* hardware done */
2512      ((ring->read->status & TLP_DSTS_RX_FIRST_DESC) == 0)) /* should be set */
2513     panic("%s: rxintr_cleanup: rx-first-descriptor not set.\n", NAME_UNIT);
2514
2515   /* First decide if a complete packet has arrived. */
2516   /* Run down DMA descriptors looking for one marked "last". */
2517   /* Bail out if an active descriptor is encountered. */
2518   /* Accumulate most significant bits of packet length. */
2519   pkt_len = 0;
2520   for (;;)
2521     {
2522     if (last_desc == ring->write) return 0;  /* no more descs */
2523     if (last_desc->status & TLP_DSTS_OWNER) return 0; /* still active */
2524     if (last_desc->status & TLP_DSTS_RX_LAST_DESC) break; /* end of packet */
2525     pkt_len += last_desc->length1 + last_desc->length2; /* entire desc filled */
2526     if (last_desc++->control & TLP_DCTL_END_RING) last_desc = ring->first; /* ring wrap */
2527     }
2528
2529   /* A complete packet has arrived; how long is it? */
2530   /* H/w ref man shows RX pkt length as a 14-bit field. */
2531   /* An experiment found that only the 12 LSBs work. */
2532   if (((last_desc->status>>16)&0xFFF) == 0) pkt_len += 4096; /* carry-bit */
2533   pkt_len = (pkt_len & 0xF000) + ((last_desc->status>>16) & 0x0FFF);
2534   /* Subtract the CRC length unless doing so would underflow. */
2535   if (pkt_len >= sc->config.crc_len) pkt_len -= sc->config.crc_len;
2536
2537   /* Run down DMA descriptors again doing the following:
2538    *  1) put pkt info in pkthdr of first mbuf,
2539    *  2) link mbufs,
2540    *  3) set mbuf lengths.
2541    */
2542   first_desc = ring->read;
2543   do
2544     {
2545     /* Read a DMA descriptor from the ring. */
2546     last_desc = ring->read;
2547     /* Advance the ring read pointer. */
2548     if (ring->read++ == ring->last) ring->read = ring->first;
2549
2550     /* Dequeue the corresponding cluster mbuf. */
2551     new_mbuf = mbuf_dequeue(ring);
2552     if (new_mbuf == NULL)
2553       panic("%s: rxintr_cleanup: expected an mbuf\n", NAME_UNIT);
2554
2555     desc_len = last_desc->length1 + last_desc->length2;
2556     /* If bouncing, copy bounce buf to mbuf. */
2557     DMA_SYNC(last_desc->map, desc_len, BUS_DMASYNC_POSTREAD);
2558     /* Unmap kernel virtual address to PCI address. */
2559     bus_dmamap_unload(ring->tag, last_desc->map);
2560
2561     /* 1) Put pkt info in pkthdr of first mbuf. */
2562     if (last_desc == first_desc)
2563       {
2564       first_mbuf = new_mbuf;
2565       first_mbuf->m_pkthdr.len   = pkt_len; /* total pkt length */
2566       first_mbuf->m_pkthdr.rcvif = sc->ifp; /* how it got here */
2567       }
2568     else /* 2) link mbufs. */
2569       {
2570       last_mbuf->m_next = new_mbuf;
2571       /* M_PKTHDR should be set in the first mbuf only. */
2572       new_mbuf->m_flags &= ~M_PKTHDR;
2573       }
2574     last_mbuf = new_mbuf;
2575
2576     /* 3) Set mbuf lengths. */
2577     new_mbuf->m_len = (pkt_len >= desc_len) ? desc_len : pkt_len;
2578     pkt_len -= new_mbuf->m_len;
2579     } while ((last_desc->status & TLP_DSTS_RX_LAST_DESC) == 0);
2580
2581   /* Decide whether to accept or to discard this packet. */
2582   /* RxHDLC sets MIIERR for bad CRC, abort and partial byte at pkt end. */
2583   if (((last_desc->status & TLP_DSTS_RX_BAD) == 0) &&
2584    (sc->status.oper_status == STATUS_UP) &&
2585    (first_mbuf->m_pkthdr.len > 0))
2586     {
2587     /* Optimization: copy a small pkt into a small mbuf. */
2588     if (first_mbuf->m_pkthdr.len <= COPY_BREAK)
2589       {
2590       MGETHDR(new_mbuf, M_NOWAIT, MT_DATA);
2591       if (new_mbuf != NULL)
2592         {
2593         new_mbuf->m_pkthdr.rcvif = first_mbuf->m_pkthdr.rcvif;
2594         new_mbuf->m_pkthdr.len   = first_mbuf->m_pkthdr.len;
2595         new_mbuf->m_len          = first_mbuf->m_len;
2596         memcpy(new_mbuf->m_data,   first_mbuf->m_data,
2597          first_mbuf->m_pkthdr.len);
2598         m_freem(first_mbuf);
2599         first_mbuf = new_mbuf;
2600         }
2601       }
2602     /* Include CRC and one flag byte in input byte count. */
2603     sc->status.cntrs.ibytes += first_mbuf->m_pkthdr.len + sc->config.crc_len +1;
2604     sc->status.cntrs.ipackets++;
2605     if_inc_counter(sc->ifp, IFCOUNTER_IPACKETS, 1);
2606     LMC_BPF_MTAP(first_mbuf);
2607 #if defined(DEVICE_POLLING)
2608     sc->quota--;
2609 #endif
2610
2611     /* Give this good packet to the network stacks. */
2612 #if NETGRAPH
2613     if (sc->ng_hook != NULL) /* is hook connected? */
2614       {
2615       int error;  /* ignore error */
2616       NG_SEND_DATA_ONLY(error, sc->ng_hook, first_mbuf);
2617       return 1;  /* did something */
2618       }
2619 #endif /* NETGRAPH */
2620     if (sc->config.line_pkg == PKG_RAWIP)
2621       lmc_raw_input(sc->ifp, first_mbuf);
2622     else
2623       {
2624 #if NSPPP
2625       sppp_input(sc->ifp, first_mbuf);
2626 #elif P2P
2627       new_mbuf = first_mbuf;
2628       while (new_mbuf != NULL)
2629         {
2630         sc->p2p->p2p_hdrinput(sc->p2p, new_mbuf->m_data, new_mbuf->m_len);
2631         new_mbuf = new_mbuf->m_next;
2632         }
2633       sc->p2p->p2p_input(sc->p2p, NULL);
2634       m_freem(first_mbuf);
2635 #else
2636       m_freem(first_mbuf);
2637       sc->status.cntrs.idiscards++;
2638 #endif
2639       }
2640     }
2641   else if (sc->status.oper_status != STATUS_UP)
2642     {
2643     /* If the link is down, this packet is probably noise. */
2644     m_freem(first_mbuf);
2645     sc->status.cntrs.idiscards++;
2646     if (DRIVER_DEBUG)
2647       printf("%s: rxintr_cleanup: rx pkt discarded: link down\n", NAME_UNIT);
2648     }
2649   else /* Log and discard this bad packet. */
2650     {
2651     if (DRIVER_DEBUG)
2652       printf("%s: RX bad pkt; len=%d %s%s%s%s\n",
2653        NAME_UNIT, first_mbuf->m_pkthdr.len,
2654        (last_desc->status & TLP_DSTS_RX_MII_ERR)  ? " miierr"  : "",
2655        (last_desc->status & TLP_DSTS_RX_DRIBBLE)  ? " dribble" : "",
2656        (last_desc->status & TLP_DSTS_RX_DESC_ERR) ? " descerr" : "",
2657        (last_desc->status & TLP_DSTS_RX_OVERRUN)  ? " overrun" : "");
2658     if (last_desc->status & TLP_DSTS_RX_OVERRUN)
2659       sc->status.cntrs.fifo_over++;
2660     else
2661       sc->status.cntrs.ierrors++;
2662     m_freem(first_mbuf);
2663     }
2664
2665   return 1; /* did something */
2666   }
2667
2668 /* Setup (prepare) to receive a packet. */
2669 /* Try to keep the RX descriptor ring full of empty buffers. */
2670 static int  /* BSD version */
2671 rxintr_setup(softc_t *sc)
2672   {
2673   struct desc_ring *ring = &sc->rxring;
2674   struct dma_desc *desc;
2675   struct mbuf *m;
2676   int desc_len;
2677   int error;
2678
2679   /* Ring is full if (wrap(write+1)==read) */
2680   if (((ring->write == ring->last) ? ring->first : ring->write+1) == ring->read)
2681     return 0;  /* ring is full; nothing to do */
2682
2683   /* Allocate a small mbuf and attach an mbuf cluster. */
2684   MGETHDR(m, M_NOWAIT, MT_DATA);
2685   if (m == NULL)
2686     {
2687     sc->status.cntrs.rxdma++;
2688     if (DRIVER_DEBUG)
2689       printf("%s: rxintr_setup: MGETHDR() failed\n", NAME_UNIT);
2690     return 0;
2691     }
2692   if (!(MCLGET(m, M_NOWAIT)))
2693     {
2694     m_freem(m);
2695     sc->status.cntrs.rxdma++;
2696     if (DRIVER_DEBUG)
2697       printf("%s: rxintr_setup: MCLGET() failed\n", NAME_UNIT);
2698     return 0;
2699     }
2700
2701   /* Queue the mbuf for later processing by rxintr_cleanup. */
2702   mbuf_enqueue(ring, m);
2703
2704   /* Write a DMA descriptor into the ring. */
2705   /* Hardware won't see it until the OWNER bit is set. */
2706   desc = ring->write;
2707   /* Advance the ring write pointer. */
2708   if (ring->write++ == ring->last) ring->write = ring->first;
2709
2710   desc_len = (MCLBYTES < MAX_DESC_LEN) ? MCLBYTES : MAX_DESC_LEN;
2711   /* Map kernel virtual address to PCI address. */
2712   if ((error = DMA_LOAD(desc->map, m->m_data, desc_len)))
2713     printf("%s: bus_dmamap_load(rx) failed; error %d\n", NAME_UNIT, error);
2714   /* Invalidate the cache for this mbuf. */
2715   DMA_SYNC(desc->map, desc_len, BUS_DMASYNC_PREREAD);
2716
2717   /* Set up the DMA descriptor. */
2718   desc->address1 = ring->segs[0].ds_addr;
2719   desc->length1  = desc_len>>1;
2720   desc->address2 = desc->address1 + desc->length1;
2721   desc->length2  = desc_len>>1;
2722
2723   /* Before setting the OWNER bit, flush the cache (memory barrier). */
2724   DMA_SYNC(ring->map, ring->size_descs, BUS_DMASYNC_PREWRITE);
2725
2726   /* Commit the DMA descriptor to the hardware. */
2727   desc->status = TLP_DSTS_OWNER;
2728
2729   /* Notify the receiver that there is another buffer available. */
2730   WRITE_CSR(TLP_RX_POLL, 1);
2731
2732   return 1; /* did something */
2733   }
2734
2735 /* Clean up after a packet has been transmitted. */
2736 /* Free the mbuf chain and update the DMA descriptor ring. */
2737 static int  /* BSD version */
2738 txintr_cleanup(softc_t *sc)
2739   {
2740   struct desc_ring *ring = &sc->txring;
2741   struct dma_desc *desc;
2742
2743   while ((ring->read != ring->write) && /* while ring is not empty */
2744         ((ring->read->status & TLP_DSTS_OWNER) == 0))
2745     {
2746     /* Read a DMA descriptor from the ring. */
2747     desc = ring->read;
2748     /* Advance the ring read pointer. */
2749     if (ring->read++ == ring->last) ring->read = ring->first;
2750
2751     /* This is a no-op on most architectures. */
2752     DMA_SYNC(desc->map, desc->length1 + desc->length2, BUS_DMASYNC_POSTWRITE);
2753     /* Unmap kernel virtual address to PCI address. */
2754     bus_dmamap_unload(ring->tag, desc->map);
2755
2756     /* If this descriptor is the last segment of a packet, */
2757     /*  then dequeue and free the corresponding mbuf chain. */
2758     if ((desc->control & TLP_DCTL_TX_LAST_SEG) != 0)
2759       {
2760       struct mbuf *m;
2761       if ((m = mbuf_dequeue(ring)) == NULL)
2762         panic("%s: txintr_cleanup: expected an mbuf\n", NAME_UNIT);
2763
2764       /* Include CRC and one flag byte in output byte count. */
2765       sc->status.cntrs.obytes += m->m_pkthdr.len + sc->config.crc_len +1;
2766       sc->status.cntrs.opackets++;
2767       if_inc_counter(sc->ifp, IFCOUNTER_OPACKETS, 1);
2768       LMC_BPF_MTAP(m);
2769       /* The only bad TX status is fifo underrun. */
2770       if ((desc->status & TLP_DSTS_TX_UNDERRUN) != 0)
2771         sc->status.cntrs.fifo_under++;
2772
2773       m_freem(m);
2774       return 1;  /* did something */
2775       }
2776     }
2777
2778   return 0;
2779   }
2780
2781 /* Build DMA descriptors for a transmit packet mbuf chain. */
2782 static int /* 0=success; 1=error */ /* BSD version */
2783 txintr_setup_mbuf(softc_t *sc, struct mbuf *m)
2784   {
2785   struct desc_ring *ring = &sc->txring;
2786   struct dma_desc *desc;
2787   unsigned int desc_len;
2788
2789   /* build DMA descriptors for a chain of mbufs. */
2790   while (m != NULL)
2791     {
2792     char *data = m->m_data;
2793     int length = m->m_len; /* zero length mbufs happen! */
2794
2795     /* Build DMA descriptors for one mbuf. */
2796     while (length > 0)
2797       {
2798       int error;
2799
2800       /* Ring is full if (wrap(write+1)==read) */
2801       if (((ring->temp==ring->last) ? ring->first : ring->temp+1) == ring->read)
2802         { /* Not enough DMA descriptors; try later. */
2803         for (; ring->temp!=ring->write;
2804          ring->temp = (ring->temp==ring->first)? ring->last : ring->temp-1)
2805           bus_dmamap_unload(ring->tag, ring->temp->map);
2806         sc->status.cntrs.txdma++;
2807         return 1;
2808         }
2809
2810       /* Provisionally, write a descriptor into the ring. */
2811       /* But don't change the REAL ring write pointer. */
2812       /* Hardware won't see it until the OWNER bit is set. */
2813       desc = ring->temp;
2814       /* Advance the temporary ring write pointer. */
2815       if (ring->temp++ == ring->last) ring->temp = ring->first;
2816
2817       /* Clear all control bits except the END_RING bit. */
2818       desc->control &= TLP_DCTL_END_RING;
2819       /* Don't pad short packets up to 64 bytes. */
2820       desc->control |= TLP_DCTL_TX_NO_PAD;
2821       /* Use Tulip's CRC-32 generator, if appropriate. */
2822       if (sc->config.crc_len != CFG_CRC_32)
2823         desc->control |= TLP_DCTL_TX_NO_CRC;
2824       /* Set the OWNER bit, except in the first descriptor. */
2825       if (desc != ring->write)
2826         desc->status = TLP_DSTS_OWNER;
2827
2828       desc_len = (length > MAX_CHUNK_LEN) ? MAX_CHUNK_LEN : length;
2829       /* Map kernel virtual address to PCI address. */
2830       if ((error = DMA_LOAD(desc->map, data, desc_len)))
2831         printf("%s: bus_dmamap_load(tx) failed; error %d\n", NAME_UNIT, error);
2832       /* Flush the cache and if bouncing, copy mbuf to bounce buf. */
2833       DMA_SYNC(desc->map, desc_len, BUS_DMASYNC_PREWRITE);
2834
2835       /* Prevent wild fetches if mapping fails (nsegs==0). */
2836       desc->length1  = desc->length2  = 0;
2837       desc->address1 = desc->address2 = 0;
2838         {
2839         bus_dma_segment_t *segs = ring->segs;
2840         int nsegs = ring->nsegs;
2841         if (nsegs >= 1)
2842           {
2843           desc->address1 = segs[0].ds_addr;
2844           desc->length1  = segs[0].ds_len;
2845           }
2846         if (nsegs == 2)
2847           {
2848           desc->address2 = segs[1].ds_addr;
2849           desc->length2  = segs[1].ds_len;
2850           }
2851         }
2852
2853       data   += desc_len;
2854       length -= desc_len;
2855       } /* while (length > 0) */
2856
2857     m = m->m_next;
2858     } /* while (m != NULL) */
2859
2860   return 0; /* success */
2861   }
2862
2863 /* Setup (prepare) to transmit a packet. */
2864 /* Select a packet, build DMA descriptors and give packet to hardware. */
2865 /* If DMA descriptors run out, abandon the attempt and return 0. */
2866 static int  /* BSD version */
2867 txintr_setup(softc_t *sc)
2868   {
2869   struct desc_ring *ring = &sc->txring;
2870   struct dma_desc *first_desc, *last_desc;
2871
2872   /* Protect against half-up links: Don't transmit */
2873   /*  if the receiver can't hear the far end. */
2874   if (sc->status.oper_status != STATUS_UP) return 0;
2875
2876   /* Pick a packet to transmit. */
2877 #if NETGRAPH
2878   if ((sc->ng_hook != NULL) && (sc->tx_mbuf == NULL))
2879     {
2880     if (!IFQ_IS_EMPTY(&sc->ng_fastq))
2881       IFQ_DEQUEUE(&sc->ng_fastq, sc->tx_mbuf);
2882     else
2883       IFQ_DEQUEUE(&sc->ng_sndq,  sc->tx_mbuf);
2884     }
2885   else
2886 #endif
2887   if (sc->tx_mbuf == NULL)
2888     {
2889     if (sc->config.line_pkg == PKG_RAWIP)
2890       IFQ_DEQUEUE(&sc->ifp->if_snd, sc->tx_mbuf);
2891     else
2892       {
2893 #if NSPPP
2894       sc->tx_mbuf = sppp_dequeue(sc->ifp);
2895 #elif P2P
2896       if (!IFQ_IS_EMPTY(&sc->p2p->p2p_isnd))
2897         IFQ_DEQUEUE(&sc->p2p->p2p_isnd, sc->tx_mbuf);
2898       else
2899         IFQ_DEQUEUE(&sc->ifp->if_snd, sc->tx_mbuf);
2900 #endif
2901       }
2902     }
2903   if (sc->tx_mbuf == NULL) return 0;  /* no pkt to transmit */
2904
2905   /* Build DMA descriptors for an outgoing mbuf chain. */
2906   ring->temp = ring->write; /* temporary ring write pointer */
2907   if (txintr_setup_mbuf(sc, sc->tx_mbuf) != 0) return 0;
2908
2909   /* Enqueue the mbuf; txintr_cleanup will free it. */
2910   mbuf_enqueue(ring, sc->tx_mbuf);
2911
2912   /* The transmitter has room for another packet. */
2913   sc->tx_mbuf = NULL;
2914
2915   /* Set first & last segment bits. */
2916   /* last_desc is the desc BEFORE the one pointed to by ring->temp. */
2917   first_desc = ring->write;
2918   first_desc->control |= TLP_DCTL_TX_FIRST_SEG;
2919   last_desc = (ring->temp==ring->first)? ring->last : ring->temp-1;
2920    last_desc->control |= TLP_DCTL_TX_LAST_SEG;
2921   /* Interrupt at end-of-transmission?  Why bother the poor computer! */
2922 /* last_desc->control |= TLP_DCTL_TX_INTERRUPT; */
2923
2924   /* Make sure the OWNER bit is not set in the next descriptor. */
2925   /* The OWNER bit may have been set if a previous call aborted. */
2926   ring->temp->status = 0;
2927
2928   /* Commit the DMA descriptors to the software. */
2929   ring->write = ring->temp;
2930
2931   /* Before setting the OWNER bit, flush the cache (memory barrier). */
2932   DMA_SYNC(ring->map, ring->size_descs, BUS_DMASYNC_PREWRITE);
2933
2934   /* Commit the DMA descriptors to the hardware. */
2935   first_desc->status = TLP_DSTS_OWNER;
2936
2937   /* Notify the transmitter that there is another packet to send. */
2938   WRITE_CSR(TLP_TX_POLL, 1);
2939
2940   return 1; /* did something */
2941   }
2942
2943
2944
2945 static void
2946 check_intr_status(softc_t *sc)
2947   {
2948   u_int32_t status, cfcs, op_mode;
2949   u_int32_t missed, overruns;
2950
2951   /* Check for four unusual events:
2952    *  1) fatal PCI bus errors       - some are recoverable
2953    *  2) transmitter FIFO underruns - increase fifo threshold
2954    *  3) receiver FIFO overruns     - clear potential hangup
2955    *  4) no receive descs or bufs   - count missed packets
2956    */
2957
2958   /* 1) A fatal bus error causes a Tulip to stop initiating bus cycles. */
2959   /* Module unload/load or boot are the only fixes for Parity Errors. */
2960   /* Master and Target Aborts can be cleared and life may continue. */
2961   status = READ_CSR(TLP_STATUS);
2962   if ((status & TLP_STAT_FATAL_ERROR) != 0)
2963     {
2964     u_int32_t fatal = (status & TLP_STAT_FATAL_BITS)>>TLP_STAT_FATAL_SHIFT;
2965     printf("%s: FATAL PCI BUS ERROR: %s%s%s%s\n", NAME_UNIT,
2966      (fatal == 0) ? "PARITY ERROR" : "",
2967      (fatal == 1) ? "MASTER ABORT" : "",
2968      (fatal == 2) ? "TARGET ABORT" : "",
2969      (fatal >= 3) ? "RESERVED (?)" : "");
2970     cfcs = READ_PCI_CFG(sc, TLP_CFCS);  /* try to clear it */
2971     cfcs &= ~(TLP_CFCS_MSTR_ABORT | TLP_CFCS_TARG_ABORT);
2972     WRITE_PCI_CFG(sc, TLP_CFCS, cfcs);
2973     }
2974
2975   /* 2) If the transmitter fifo underruns, increase the transmit fifo */
2976   /*  threshold: the number of bytes required to be in the fifo */
2977   /*  before starting the transmitter (cost: increased tx delay). */
2978   /* The TX_FSM must be stopped to change this parameter. */
2979   if ((status & TLP_STAT_TX_UNDERRUN) != 0)
2980     {
2981     op_mode = READ_CSR(TLP_OP_MODE);
2982     /* enable store-and-forward mode if tx_threshold tops out? */
2983     if ((op_mode & TLP_OP_TX_THRESH) < TLP_OP_TX_THRESH)
2984       {
2985       op_mode += 0x4000;  /* increment TX_THRESH field; can't overflow */
2986       WRITE_CSR(TLP_OP_MODE, op_mode & ~TLP_OP_TX_RUN);
2987       /* Wait for the TX FSM to stop; it might be processing a pkt. */
2988       while (READ_CSR(TLP_STATUS) & TLP_STAT_TX_FSM); /* XXX HANG */
2989       WRITE_CSR(TLP_OP_MODE, op_mode); /* restart tx */
2990       if (DRIVER_DEBUG)
2991         printf("%s: tx underrun; tx fifo threshold now %d bytes\n",
2992          NAME_UNIT, 128<<((op_mode>>TLP_OP_TR_SHIFT)&3));
2993       }
2994     }
2995
2996   /* 3) Errata memo from Digital Equipment Corp warns that 21140A */
2997   /* receivers through rev 2.2 can hang if the fifo overruns. */
2998   /* Recommended fix: stop and start the RX FSM after an overrun. */
2999   missed = READ_CSR(TLP_MISSED);
3000   if ((overruns = ((missed & TLP_MISS_OVERRUN)>>TLP_OVERRUN_SHIFT)) != 0)
3001     {
3002     if (DRIVER_DEBUG)
3003       printf("%s: rx overrun cntr=%d\n", NAME_UNIT, overruns);
3004     sc->status.cntrs.overruns += overruns;
3005     if ((READ_PCI_CFG(sc, TLP_CFRV) & 0xFF) <= 0x22)
3006       {
3007       op_mode = READ_CSR(TLP_OP_MODE);
3008       WRITE_CSR(TLP_OP_MODE, op_mode & ~TLP_OP_RX_RUN);
3009       /* Wait for the RX FSM to stop; it might be processing a pkt. */
3010       while (READ_CSR(TLP_STATUS) & TLP_STAT_RX_FSM); /* XXX HANG */
3011       WRITE_CSR(TLP_OP_MODE, op_mode);  /* restart rx */
3012       }
3013     }
3014
3015   /* 4) When the receiver is enabled and a packet arrives, but no DMA */
3016   /*  descriptor is available, the packet is counted as 'missed'. */
3017   /* The receiver should never miss packets; warn if it happens. */
3018   if ((missed = (missed & TLP_MISS_MISSED)) != 0)
3019     {
3020     if (DRIVER_DEBUG)
3021       printf("%s: rx missed %d pkts\n", NAME_UNIT, missed);
3022     sc->status.cntrs.missed += missed;
3023     }
3024   }
3025
3026 static void /* This is where the work gets done. */
3027 core_interrupt(void *arg, int check_status)
3028   {
3029   softc_t *sc = arg;
3030   int activity;
3031
3032   /* If any CPU is inside this critical section, then */
3033   /* other CPUs should go away without doing anything. */
3034   if (BOTTOM_TRYLOCK == 0)
3035     {
3036     sc->status.cntrs.lck_intr++;
3037     return;
3038     }
3039
3040   /* Clear pending card interrupts. */
3041   WRITE_CSR(TLP_STATUS, READ_CSR(TLP_STATUS));
3042
3043   /* In Linux, pci_alloc_consistent() means DMA descriptors */
3044   /*  don't need explicit syncing. */
3045   {
3046   struct desc_ring *ring = &sc->txring;
3047   DMA_SYNC(sc->txring.map, sc->txring.size_descs,
3048    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3049   ring = &sc->rxring;
3050   DMA_SYNC(sc->rxring.map, sc->rxring.size_descs,
3051    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3052   }
3053
3054   do  /* This is the main loop for interrupt processing. */
3055     {
3056     activity  = txintr_cleanup(sc);
3057     activity += txintr_setup(sc);
3058     activity += rxintr_cleanup(sc);
3059     activity += rxintr_setup(sc);
3060     } while (activity);
3061
3062   {
3063   struct desc_ring *ring = &sc->txring;
3064   DMA_SYNC(sc->txring.map, sc->txring.size_descs,
3065    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3066   ring = &sc->rxring;
3067   DMA_SYNC(sc->rxring.map, sc->rxring.size_descs,
3068    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3069   }
3070
3071   /* As the interrupt is dismissed, check for four unusual events. */
3072   if (check_status) check_intr_status(sc);
3073
3074   BOTTOM_UNLOCK;
3075   }
3076
3077 /* user_interrupt() may be called from a syscall or a softirq */
3078 static void
3079 user_interrupt(softc_t *sc, int check_status)
3080   {
3081   DISABLE_INTR; /* noop on FreeBSD-5 and Linux */
3082   core_interrupt(sc, check_status);
3083   ENABLE_INTR;  /* noop on FreeBSD-5 and Linux */
3084   }
3085
3086
3087 # if defined(DEVICE_POLLING)
3088
3089 /* Service the card from the kernel idle loop without interrupts. */
3090 static int
3091 fbsd_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3092   {
3093   softc_t *sc = IFP2SC(ifp);
3094
3095
3096   sc->quota = count;
3097   core_interrupt(sc, (cmd==POLL_AND_CHECK_STATUS));
3098   return 0;
3099   }
3100
3101 # endif  /* DEVICE_POLLING */
3102
3103 /* BSD kernels call this procedure when an interrupt happens. */
3104 static intr_return_t
3105 bsd_interrupt(void *arg)
3106   {
3107   softc_t *sc = arg;
3108
3109   /* Cut losses early if this is not our interrupt. */
3110   if ((READ_CSR(TLP_STATUS) & TLP_INT_TXRX) == 0)
3111     return IRQ_NONE;
3112
3113 # if defined(DEVICE_POLLING)
3114   if (sc->ifp->if_capenable & IFCAP_POLLING)
3115     return IRQ_NONE;
3116
3117   if ((sc->ifp->if_capabilities & IFCAP_POLLING) &&
3118    (ether_poll_register(fbsd_poll, sc->ifp)))
3119     {
3120     WRITE_CSR(TLP_INT_ENBL, TLP_INT_DISABLE);
3121     return IRQ_NONE;
3122     }
3123   else
3124     sc->quota = sc->rxring.num_descs; /* input flow control */
3125 # endif  /* DEVICE_POLLING */
3126
3127   /* Disable card interrupts. */
3128   WRITE_CSR(TLP_INT_ENBL, TLP_INT_DISABLE);
3129
3130   core_interrupt(sc, 0);
3131
3132   /* Enable card interrupts. */
3133   WRITE_CSR(TLP_INT_ENBL, TLP_INT_TXRX);
3134
3135   return IRQ_HANDLED;
3136   }
3137
3138
3139 /* Administrative status of the driver (UP or DOWN) has changed. */
3140 /* A card-specific action may be required: T1 and T3 cards: no-op. */
3141 /* HSSI and SSI cards change the state of modem ready signals. */
3142 static void
3143 set_status(softc_t *sc, int status)
3144   {
3145   struct ioctl ioctl;
3146
3147   ioctl.cmd = IOCTL_SET_STATUS;
3148   ioctl.data = status;
3149
3150   sc->card->ioctl(sc, &ioctl);
3151   }
3152
3153 #if P2P
3154
3155 /* Callout from P2P: */
3156 /* Get the state of DCD (Data Carrier Detect). */
3157 static int
3158 p2p_getmdm(struct p2pcom *p2p, caddr_t result)
3159   {
3160   softc_t *sc = IFP2SC(&p2p->p2p_if);
3161
3162   /* Non-zero isn't good enough; TIOCM_CAR is 0x40. */
3163   *(int *)result = (sc->status.oper_status==STATUS_UP) ? TIOCM_CAR : 0;
3164
3165   return 0;
3166   }
3167
3168 /* Callout from P2P: */
3169 /* Set the state of DTR (Data Terminal Ready). */
3170 static int
3171 p2p_mdmctl(struct p2pcom *p2p, int flag)
3172   {
3173   softc_t *sc = IFP2SC(&p2p->p2p_if);
3174
3175   set_status(sc, flag);
3176
3177   return 0;
3178   }
3179
3180 #endif /* P2P */
3181
3182 #if NSPPP
3183
3184 # ifndef PP_FR
3185 #  define PP_FR 0
3186 # endif
3187
3188 /* Callout from SPPP: */
3189 static void
3190 sppp_tls(struct sppp *sppp)
3191   {
3192   if (!(sppp->pp_mode  & IFF_LINK2) &&
3193       !(sppp->pp_flags & PP_FR))
3194     sppp->pp_up(sppp);
3195   }
3196
3197 /* Callout from SPPP: */
3198 static void
3199 sppp_tlf(struct sppp *sppp)
3200   {
3201   if (!(sppp->pp_mode  & IFF_LINK2) &&
3202       !(sppp->pp_flags & PP_FR))
3203     sppp->pp_down(sppp);
3204   }
3205
3206 #endif /* NSPPP */
3207
3208 /* Configure line protocol stuff.
3209  * Called by attach_card() during module init.
3210  * Called by core_ioctl()  when lmcconfig writes sc->config.
3211  * Called by detach_card() during module shutdown.
3212  */
3213 static void
3214 config_proto(softc_t *sc, struct config *config)
3215   {
3216   /* Use line protocol stack instead of RAWIP mode. */
3217   if ((sc->config.line_pkg == PKG_RAWIP) &&
3218          (config->line_pkg != PKG_RAWIP))
3219     {
3220 #if NSPPP
3221     LMC_BPF_DETACH;
3222     sppp_attach(sc->ifp);
3223     LMC_BPF_ATTACH(DLT_PPP, 4);
3224     sc->sppp->pp_tls = sppp_tls;
3225     sc->sppp->pp_tlf = sppp_tlf;
3226     /* Force reconfiguration of SPPP params. */
3227     sc->config.line_prot = 0;
3228     sc->config.keep_alive = config->keep_alive ? 0:1;
3229 #elif P2P
3230     int error = 0;
3231     sc->p2p->p2p_proto = 0; /* force p2p_attach */
3232     if ((error = p2p_attach(sc->p2p))) /* calls bpfattach() */
3233       {
3234       printf("%s: p2p_attach() failed; error %d\n", NAME_UNIT, error);
3235       config->line_pkg = PKG_RAWIP;  /* still in RAWIP mode */
3236       }
3237     else
3238       {
3239       sc->p2p->p2p_mdmctl = p2p_mdmctl; /* set DTR */
3240       sc->p2p->p2p_getmdm = p2p_getmdm; /* get DCD */
3241       }
3242 #elif GEN_HDLC
3243     int error = 0;
3244     sc->net_dev->mtu = HDLC_MAX_MTU;
3245     if ((error = hdlc_open(sc->net_dev)))
3246       {
3247       printf("%s: hdlc_open() failed; error %d\n", NAME_UNIT, error);
3248       printf("%s: Try 'sethdlc %s ppp'\n", NAME_UNIT, NAME_UNIT);
3249       config->line_pkg = PKG_RAWIP;  /* still in RAWIP mode */
3250       }
3251 #else /* no line protocol stack was configured */
3252     config->line_pkg = PKG_RAWIP;  /* still in RAWIP mode */
3253 #endif
3254     }
3255
3256   /* Bypass line protocol stack and return to RAWIP mode. */
3257   if ((sc->config.line_pkg != PKG_RAWIP) &&
3258          (config->line_pkg == PKG_RAWIP))
3259     {
3260 #if NSPPP
3261     LMC_BPF_DETACH;
3262     sppp_flush(sc->ifp);
3263     sppp_detach(sc->ifp);
3264     setup_ifnet(sc->ifp);
3265     LMC_BPF_ATTACH(DLT_RAW, 0);
3266 #elif P2P
3267     int error = 0;
3268     if_qflush(&sc->p2p->p2p_isnd);
3269     if ((error = p2p_detach(sc->p2p)))
3270       {
3271       printf("%s: p2p_detach() failed; error %d\n",  NAME_UNIT, error);
3272       printf("%s: Try 'ifconfig %s down -remove'\n", NAME_UNIT, NAME_UNIT);
3273       config->line_pkg = PKG_P2P; /* not in RAWIP mode; still attached to P2P */
3274       }
3275     else
3276       {
3277       setup_ifnet(sc->ifp);
3278       LMC_BPF_ATTACH(DLT_RAW, 0);
3279       }
3280 #elif GEN_HDLC
3281     hdlc_proto_detach(sc->hdlc_dev);
3282     hdlc_close(sc->net_dev);
3283     setup_netdev(sc->net_dev);
3284 #endif
3285     }
3286
3287 #if NSPPP
3288
3289   if (config->line_pkg != PKG_RAWIP)
3290     {
3291     /* Check for change to PPP protocol. */
3292     if ((sc->config.line_prot != PROT_PPP) &&
3293            (config->line_prot == PROT_PPP))
3294       {
3295       LMC_BPF_DETACH;
3296       sc->ifp->if_flags  &= ~IFF_LINK2;
3297       sc->sppp->pp_flags &= ~PP_FR;
3298       LMC_BPF_ATTACH(DLT_PPP, 4);
3299       sppp_ioctl(sc->ifp, SIOCSIFFLAGS, NULL);
3300       }
3301
3302 # ifndef DLT_C_HDLC
3303 #  define DLT_C_HDLC DLT_PPP
3304 # endif
3305
3306     /* Check for change to C_HDLC protocol. */
3307     if ((sc->config.line_prot != PROT_C_HDLC) &&
3308            (config->line_prot == PROT_C_HDLC))
3309       {
3310       LMC_BPF_DETACH;
3311       sc->ifp->if_flags  |=  IFF_LINK2;
3312       sc->sppp->pp_flags &= ~PP_FR;
3313       LMC_BPF_ATTACH(DLT_C_HDLC, 4);
3314       sppp_ioctl(sc->ifp, SIOCSIFFLAGS, NULL);
3315       }
3316
3317     /* Check for change to Frame Relay protocol. */
3318     if ((sc->config.line_prot != PROT_FRM_RLY) &&
3319            (config->line_prot == PROT_FRM_RLY))
3320       {
3321       LMC_BPF_DETACH;
3322       sc->ifp->if_flags  &= ~IFF_LINK2;
3323       sc->sppp->pp_flags |= PP_FR;
3324       LMC_BPF_ATTACH(DLT_FRELAY, 4);
3325       sppp_ioctl(sc->ifp, SIOCSIFFLAGS, NULL);
3326       }
3327
3328     /* Check for disabling keep-alives. */
3329     if ((sc->config.keep_alive != 0) &&
3330            (config->keep_alive == 0))
3331       sc->sppp->pp_flags &= ~PP_KEEPALIVE;
3332
3333     /* Check for enabling keep-alives. */
3334     if ((sc->config.keep_alive == 0) &&
3335            (config->keep_alive != 0))
3336       sc->sppp->pp_flags |=  PP_KEEPALIVE;      
3337     }
3338
3339 #endif /* NSPPP */
3340
3341   /* Loop back through the TULIP Ethernet chip; (no CRC). */
3342   /* Data sheet says stop DMA before changing OPMODE register. */
3343   /* But that's not as simple as it sounds; works anyway. */
3344   /* Check for enabling loopback thru Tulip chip. */
3345   if ((sc->config.loop_back != CFG_LOOP_TULIP) &&
3346          (config->loop_back == CFG_LOOP_TULIP))
3347     {
3348     u_int32_t op_mode = READ_CSR(TLP_OP_MODE);
3349     op_mode |= TLP_OP_INT_LOOP;
3350     WRITE_CSR(TLP_OP_MODE, op_mode);
3351     config->crc_len = CFG_CRC_0;
3352     }
3353
3354   /* Check for disabling loopback thru Tulip chip. */
3355   if ((sc->config.loop_back == CFG_LOOP_TULIP) &&
3356          (config->loop_back != CFG_LOOP_TULIP))
3357     {
3358     u_int32_t op_mode = READ_CSR(TLP_OP_MODE);
3359     op_mode &= ~TLP_OP_LOOP_MODE;
3360     WRITE_CSR(TLP_OP_MODE, op_mode);
3361     config->crc_len = CFG_CRC_16;
3362     }
3363   }
3364
3365 /* This is the core ioctl procedure. */
3366 /* It handles IOCTLs from lmcconfig(8). */
3367 /* It must not run when card watchdogs run. */
3368 /* Called from a syscall (user context; no spinlocks). */
3369 /* This procedure can SLEEP. */
3370 static int
3371 core_ioctl(softc_t *sc, u_long cmd, caddr_t data)
3372   {
3373   struct iohdr  *iohdr  = (struct iohdr  *) data;
3374   struct ioctl  *ioctl  = (struct ioctl  *) data;
3375   struct status *status = (struct status *) data;
3376   struct config *config = (struct config *) data;
3377   int error = 0;
3378
3379   /* All structs start with a string and a cookie. */
3380   if (((struct iohdr *)data)->cookie != NGM_LMC_COOKIE)
3381     return EINVAL;
3382
3383   while (TOP_TRYLOCK == 0)
3384     {
3385     sc->status.cntrs.lck_ioctl++;
3386     SLEEP(10000); /* yield? */
3387     }
3388   switch (cmd)
3389     {
3390     case LMCIOCGSTAT:
3391       {
3392       *status = sc->status;
3393       iohdr->cookie = NGM_LMC_COOKIE;
3394       break;
3395       }
3396     case LMCIOCGCFG:
3397       {
3398       *config = sc->config;
3399       iohdr->cookie = NGM_LMC_COOKIE;
3400       break;
3401       }
3402     case LMCIOCSCFG:
3403       {
3404       if ((error = CHECK_CAP)) break;
3405       config_proto(sc, config);
3406       sc->config = *config;
3407       sc->card->config(sc);
3408       break;
3409       }
3410     case LMCIOCREAD:
3411       {
3412       if (ioctl->cmd == IOCTL_RW_PCI)
3413         {
3414         if (ioctl->address > 252) { error = EFAULT; break; }
3415         ioctl->data = READ_PCI_CFG(sc, ioctl->address);
3416         }
3417       else if (ioctl->cmd == IOCTL_RW_CSR)
3418         {
3419         if (ioctl->address > 15) { error = EFAULT; break; }
3420         ioctl->data = READ_CSR(ioctl->address*TLP_CSR_STRIDE);
3421         }
3422       else if (ioctl->cmd == IOCTL_RW_SROM)
3423         {
3424         if (ioctl->address > 63)  { error = EFAULT; break; }
3425         ioctl->data = read_srom(sc, ioctl->address);
3426         }
3427       else if (ioctl->cmd == IOCTL_RW_BIOS)
3428         ioctl->data = read_bios(sc, ioctl->address);
3429       else if (ioctl->cmd == IOCTL_RW_MII)
3430         ioctl->data = read_mii(sc, ioctl->address);
3431       else if (ioctl->cmd == IOCTL_RW_FRAME)
3432         ioctl->data = read_framer(sc, ioctl->address);
3433       else
3434         error = EINVAL;
3435       break;
3436       }
3437     case LMCIOCWRITE:
3438       {
3439       if ((error = CHECK_CAP)) break;
3440       if (ioctl->cmd == IOCTL_RW_PCI)
3441         {
3442         if (ioctl->address > 252) { error = EFAULT; break; }
3443         WRITE_PCI_CFG(sc, ioctl->address, ioctl->data);
3444         }
3445       else if (ioctl->cmd == IOCTL_RW_CSR)
3446         {
3447         if (ioctl->address > 15) { error = EFAULT; break; }
3448         WRITE_CSR(ioctl->address*TLP_CSR_STRIDE, ioctl->data);
3449         }
3450       else if (ioctl->cmd == IOCTL_RW_SROM)
3451         {
3452         if (ioctl->address > 63)  { error = EFAULT; break; }
3453         write_srom(sc, ioctl->address, ioctl->data); /* can sleep */
3454         }
3455       else if (ioctl->cmd == IOCTL_RW_BIOS)
3456         {
3457         if (ioctl->address == 0) erase_bios(sc);
3458         write_bios(sc, ioctl->address, ioctl->data); /* can sleep */
3459         }
3460       else if (ioctl->cmd == IOCTL_RW_MII)
3461         write_mii(sc, ioctl->address, ioctl->data);
3462       else if (ioctl->cmd == IOCTL_RW_FRAME)
3463         write_framer(sc, ioctl->address, ioctl->data);
3464       else if (ioctl->cmd == IOCTL_WO_SYNTH)
3465         write_synth(sc, (struct synth *)&ioctl->data);
3466       else if (ioctl->cmd == IOCTL_WO_DAC)
3467         {
3468         write_dac(sc, 0x9002); /* set Vref = 2.048 volts */
3469         write_dac(sc, ioctl->data & 0xFFF);
3470         }
3471       else
3472         error = EINVAL;
3473       break;
3474       }
3475     case LMCIOCTL:
3476       {
3477       if ((error = CHECK_CAP)) break;
3478       if (ioctl->cmd == IOCTL_XILINX_RESET)
3479         {
3480         reset_xilinx(sc);
3481         sc->card->config(sc);
3482         }
3483       else if (ioctl->cmd == IOCTL_XILINX_ROM)
3484         {
3485         load_xilinx_from_rom(sc); /* can sleep */
3486         sc->card->config(sc);
3487         }
3488       else if (ioctl->cmd == IOCTL_XILINX_FILE)
3489         {
3490         /* load_xilinx_from_file() can sleep. */
3491         error = load_xilinx_from_file(sc, ioctl->ucode, ioctl->data);
3492         if (error != 0) load_xilinx_from_rom(sc); /* try the rom */
3493         sc->card->config(sc);
3494         set_status(sc, (error==0));  /* XXX */
3495         }
3496       else if (ioctl->cmd == IOCTL_RESET_CNTRS)
3497         {
3498         memset(&sc->status.cntrs, 0, sizeof(struct event_cntrs));
3499         microtime(&sc->status.cntrs.reset_time);
3500         }
3501       else
3502         error = sc->card->ioctl(sc, ioctl); /* can sleep */
3503       break;
3504       }
3505     default:
3506       error = EINVAL;
3507       break;
3508     }
3509   TOP_UNLOCK;
3510
3511   return error;
3512   }
3513
3514 /* This is the core watchdog procedure. */
3515 /* It calculates link speed, and calls the card-specific watchdog code. */
3516 /* Calls interrupt() in case one got lost; also kick-starts the device. */
3517 /* ioctl syscalls and card watchdog routines must be interlocked.       */
3518 /* This procedure must not sleep. */
3519 static void
3520 core_watchdog(softc_t *sc)
3521   {
3522   /* Read and restart the Tulip timer. */
3523   u_int32_t tx_speed = READ_CSR(TLP_TIMER);
3524   WRITE_CSR(TLP_TIMER, 0xFFFF);
3525
3526   /* Measure MII clock using a timer in the Tulip chip.
3527    * This timer counts transmitter bits divided by 4096.
3528    * Since this is called once a second the math is easy.
3529    * This is only correct when the link is NOT sending pkts.
3530    * On a fully-loaded link, answer will be HALF actual rate.
3531    * Clock rate during pkt is HALF clk rate between pkts.
3532    * Measuring clock rate really measures link utilization!
3533    */
3534   sc->status.tx_speed = (0xFFFF - (tx_speed & 0xFFFF)) << 12;
3535
3536   /* The first status reset time is when the calendar clock is set. */
3537   if (sc->status.cntrs.reset_time.tv_sec < 1000)
3538     microtime(&sc->status.cntrs.reset_time);
3539
3540   /* Update hardware (operational) status. */
3541   /* Call the card-specific watchdog routines. */
3542   if (TOP_TRYLOCK != 0)
3543     {
3544     sc->status.oper_status = sc->card->watchdog(sc);
3545
3546     /* Increment a counter which tells user-land */
3547     /*  observers that SNMP state has been updated. */
3548     sc->status.ticks++;
3549
3550     TOP_UNLOCK;
3551     }
3552   else
3553     sc->status.cntrs.lck_watch++;
3554
3555   /* In case an interrupt gets lost... */
3556   user_interrupt(sc, 1);
3557   }
3558
3559
3560 /* Called from a syscall (user context; no spinlocks). */
3561 static int
3562 lmc_raw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3563   {
3564   struct ifreq *ifr = (struct ifreq *) data;
3565   int error = 0;
3566
3567   switch (cmd)
3568     {
3569     case SIOCAIFADDR:
3570     case SIOCSIFFLAGS:
3571     case SIOCSIFADDR:
3572       ifp->if_flags |= IFF_UP;  /* a Unix tradition */
3573       break;
3574     case SIOCSIFMTU:
3575       ifp->if_mtu = ifr->ifr_mtu;
3576       break;
3577     default:
3578       error = EINVAL;
3579       break;
3580     }
3581   return error;
3582   }
3583
3584 /* Called from a syscall (user context; no spinlocks). */
3585 static int
3586 lmc_ifnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3587   {
3588   softc_t *sc = IFP2SC(ifp);
3589   int error = 0;
3590
3591   switch (cmd)
3592     {
3593     /* Catch the IOCTLs used by lmcconfig. */
3594     case LMCIOCGSTAT:
3595     case LMCIOCGCFG:
3596     case LMCIOCSCFG:
3597     case LMCIOCREAD:
3598     case LMCIOCWRITE:
3599     case LMCIOCTL:
3600       error = core_ioctl(sc, cmd, data);
3601       break;
3602     /* Pass the rest to the line protocol. */
3603     default:
3604       if (sc->config.line_pkg == PKG_RAWIP)
3605         error =  lmc_raw_ioctl(ifp, cmd, data);
3606       else
3607 # if NSPPP
3608         error = sppp_ioctl(ifp, cmd, data);
3609 # elif P2P
3610         error =  p2p_ioctl(ifp, cmd, data);
3611 # else
3612         error = EINVAL;
3613 # endif
3614       break;
3615     }
3616
3617   if (DRIVER_DEBUG && (error!=0))
3618     printf("%s: lmc_ifnet_ioctl; cmd=0x%08lx error=%d\n",
3619      NAME_UNIT, cmd, error);
3620
3621   return error;
3622   }
3623
3624 /* Called from a syscall (user context; no spinlocks). */
3625 static void
3626 lmc_ifnet_start(struct ifnet *ifp)
3627   {
3628   softc_t *sc = IFP2SC(ifp);
3629
3630   /* Start the transmitter; incoming pkts are NOT processed. */
3631   user_interrupt(sc, 0);
3632   }
3633
3634 /* sppp and p2p replace this with their own proc. */
3635 /* RAWIP mode is the only time this is used. */
3636 /* Called from a syscall (user context; no spinlocks). */
3637 static int
3638 lmc_raw_output(struct ifnet *ifp, struct mbuf *m,
3639  const struct sockaddr *dst, struct route *ro)
3640   {
3641   softc_t *sc = IFP2SC(ifp);
3642   int error = 0;
3643
3644   /* Fail if the link is down. */
3645   if (sc->status.oper_status != STATUS_UP)
3646     {
3647     m_freem(m);
3648     sc->status.cntrs.odiscards++;
3649     if (DRIVER_DEBUG)
3650       printf("%s: lmc_raw_output: tx pkt discarded: link down\n", NAME_UNIT);
3651     return ENETDOWN;
3652     }
3653
3654 # if NETGRAPH
3655   /* Netgraph has priority over the ifnet kernel interface. */
3656   if (sc->ng_hook != NULL)
3657     {
3658     m_freem(m);
3659     sc->status.cntrs.odiscards++;
3660     if (DRIVER_DEBUG)
3661       printf("%s: lmc_raw_output: tx pkt discarded: netgraph active\n",
3662         NAME_UNIT);
3663     return EBUSY;
3664     }
3665 # endif
3666
3667   /* lmc_raw_output() ENQUEUEs in a syscall or softirq. */
3668   /* txintr_setup() DEQUEUEs in a hard interrupt. */
3669   /* Some BSD QUEUE routines are not interrupt-safe. */
3670   {
3671   DISABLE_INTR;
3672   IFQ_ENQUEUE(&ifp->if_snd, m, error);
3673   ENABLE_INTR;
3674   }
3675
3676   if (error==0)
3677     user_interrupt(sc, 0); /* start the transmitter */
3678   else
3679     {
3680     m_freem(m);
3681     sc->status.cntrs.odiscards++;
3682     if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3683     if (DRIVER_DEBUG)
3684       printf("%s: lmc_raw_output: IFQ_ENQUEUE() failed; error %d\n",
3685        NAME_UNIT, error);
3686     }
3687
3688   return error;
3689   }
3690
3691 /* Called from a softirq once a second. */
3692 static void
3693 lmc_watchdog(void *arg)
3694 {
3695   struct ifnet *ifp = arg;
3696   softc_t *sc = IFP2SC(ifp);
3697   u_int8_t old_oper_status = sc->status.oper_status;
3698
3699   core_watchdog(sc); /* updates oper_status */
3700
3701 #if NETGRAPH
3702   if (sc->ng_hook != NULL)
3703     {
3704     sc->status.line_pkg  = PKG_NG;
3705     sc->status.line_prot = 0;
3706     }
3707   else
3708 #endif
3709   if (sc->config.line_pkg == PKG_RAWIP)
3710     {
3711     sc->status.line_pkg  = PKG_RAWIP;
3712     sc->status.line_prot = PROT_IP_HDLC;
3713     }
3714   else
3715     {
3716 # if P2P
3717     /* Notice change in link status. */
3718     if ((old_oper_status != sc->status.oper_status) && (sc->p2p->p2p_modem))
3719       (*sc->p2p->p2p_modem)(sc->p2p, sc->status.oper_status==STATUS_UP);
3720
3721     /* Notice change in line protocol. */
3722     sc->status.line_pkg = PKG_P2P;
3723     switch (sc->ifp->if_type)
3724       {
3725       case IFT_PPP:
3726         sc->status.line_prot = PROT_PPP;
3727         break;
3728       case IFT_PTPSERIAL:
3729         sc->status.line_prot = PROT_C_HDLC;
3730         break;
3731       case IFT_FRELAY:
3732         sc->status.line_prot = PROT_FRM_RLY;
3733         break;
3734       default:
3735         sc->status.line_prot = 0;
3736         break;
3737       }
3738
3739 # elif NSPPP
3740     /* Notice change in link status. */
3741     if     ((old_oper_status != STATUS_UP) &&
3742      (sc->status.oper_status == STATUS_UP))  /* link came up */
3743       sppp_tls(sc->sppp);
3744     if     ((old_oper_status == STATUS_UP) &&
3745      (sc->status.oper_status != STATUS_UP))  /* link went down */
3746       sppp_tlf(sc->sppp);
3747
3748     /* Notice change in line protocol. */
3749     sc->status.line_pkg = PKG_SPPP;
3750     if (sc->sppp->pp_flags & PP_FR)
3751       sc->status.line_prot = PROT_FRM_RLY;
3752     else if (sc->ifp->if_flags  & IFF_LINK2)
3753       sc->status.line_prot = PROT_C_HDLC;
3754     else
3755       sc->status.line_prot = PROT_PPP;
3756
3757 # else
3758     /* Suppress compiler warning. */
3759     if (old_oper_status == STATUS_UP);
3760 # endif
3761     }
3762
3763   ifp->if_baudrate = sc->status.tx_speed;
3764   if (sc->status.oper_status == STATUS_UP)
3765     ifp->if_link_state = LINK_STATE_UP;
3766   else
3767     ifp->if_link_state = LINK_STATE_DOWN;
3768
3769   /* Call this procedure again after one second. */
3770   callout_reset(&sc->callout, hz, lmc_watchdog, ifp);
3771 }
3772
3773 static uint64_t
3774 lmc_get_counter(struct ifnet *ifp, ift_counter cnt)
3775 {
3776         softc_t *sc;
3777         struct event_cntrs *cntrs;
3778
3779         sc = if_getsoftc(ifp);
3780         cntrs = &sc->status.cntrs;
3781
3782         switch (cnt) {
3783         case IFCOUNTER_IPACKETS:
3784                 return (cntrs->ipackets);
3785         case IFCOUNTER_OPACKETS:
3786                 return (cntrs->opackets);
3787         case IFCOUNTER_IBYTES:
3788                 return (cntrs->ibytes);
3789         case IFCOUNTER_OBYTES:
3790                 return (cntrs->obytes);
3791         case IFCOUNTER_IERRORS:
3792                 return (cntrs->ierrors);
3793         case IFCOUNTER_OERRORS:
3794                 return (cntrs->oerrors);
3795         case IFCOUNTER_IQDROPS:
3796                 return (cntrs->idiscards);
3797         default:
3798                 return (if_get_counter_default(ifp, cnt));
3799         }
3800 }
3801
3802 static void
3803 setup_ifnet(struct ifnet *ifp)
3804   {
3805   softc_t *sc = ifp->if_softc;
3806
3807   /* Initialize the generic network interface. */
3808   ifp->if_flags    = IFF_POINTOPOINT;
3809   ifp->if_flags   |= IFF_RUNNING;
3810   ifp->if_ioctl    = lmc_ifnet_ioctl;
3811   ifp->if_start    = lmc_ifnet_start;   /* sppp changes this */
3812   ifp->if_output   = lmc_raw_output;    /* sppp & p2p change this */
3813   ifp->if_input    = lmc_raw_input;
3814   ifp->if_get_counter = lmc_get_counter;
3815   ifp->if_mtu      = MAX_DESC_LEN;      /* sppp & p2p change this */
3816   ifp->if_type     = IFT_PTPSERIAL;     /* p2p changes this */
3817
3818 # if defined(DEVICE_POLLING)
3819   ifp->if_capabilities |= IFCAP_POLLING;
3820   ifp->if_capenable    |= IFCAP_POLLING_NOCOUNT;
3821 # endif
3822
3823   if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
3824   }
3825
3826 static int
3827 lmc_ifnet_attach(softc_t *sc)
3828   {
3829   sc->ifp  = if_alloc(NSPPP ? IFT_PPP : IFT_OTHER);
3830   if (sc->ifp == NULL) return ENOMEM;
3831 # if NSPPP
3832   sc->sppp = sc->ifp->if_l2com;
3833 # elif P2P
3834   sc->ifp  = &sc->p2pcom.p2p_if;
3835   sc->p2p  = &sc->p2pcom;
3836 # endif
3837
3838   /* Initialize the network interface struct. */
3839   sc->ifp->if_softc = sc;
3840   setup_ifnet(sc->ifp);
3841
3842   /* ALTQ output queue initialization. */
3843   IFQ_SET_MAXLEN(&sc->ifp->if_snd, SNDQ_MAXLEN);
3844   IFQ_SET_READY(&sc->ifp->if_snd);
3845
3846   /* Attach to the ifnet kernel interface. */
3847   if_attach(sc->ifp);
3848
3849   /* Attach Berkeley Packet Filter. */
3850   LMC_BPF_ATTACH(DLT_RAW, 0);
3851
3852   callout_reset(&sc->callout, hz, lmc_watchdog, sc);
3853
3854   return 0;
3855   }
3856
3857 static void
3858 lmc_ifnet_detach(softc_t *sc)
3859   {
3860
3861 # if defined(DEVICE_POLLING)
3862   if (sc->ifp->if_capenable & IFCAP_POLLING)
3863     ether_poll_deregister(sc->ifp);
3864 # endif
3865
3866   /* Detach Berkeley Packet Filter. */
3867   LMC_BPF_DETACH;
3868
3869   /* Detach from the ifnet kernel interface. */
3870   if_detach(sc->ifp);
3871
3872   if_free(sc->ifp);
3873   }
3874
3875
3876 #if NETGRAPH
3877
3878 /* These next two macros should be added to netgraph */
3879 #  define NG_TYPE_REF(type) atomic_add_int(&(type)->refs, 1)
3880 #  define NG_TYPE_UNREF(type)   \
3881 do {                            \
3882   if ((type)->refs == 1)        \
3883     ng_rmtype(type);            \
3884   else                          \
3885     atomic_subtract_int(&(type)->refs, 1); \
3886    } while (0)
3887
3888 /* It is an error to construct new copies of this Netgraph node. */
3889 /* All instances are constructed by ng_attach and are persistent. */
3890 static int ng_constructor(node_p  node) { return EINVAL; }
3891
3892 /* Incoming Netgraph control message. */
3893 static int
3894 ng_rcvmsg(node_p node, item_p item, hook_p lasthook)
3895   {
3896   struct ng_mesg *msg;
3897   struct ng_mesg *resp = NULL;
3898   softc_t *sc = NG_NODE_PRIVATE(node);
3899   int error = 0;
3900
3901   NGI_GET_MSG(item, msg);
3902   if (msg->header.typecookie == NGM_LMC_COOKIE)
3903     {
3904     switch (msg->header.cmd)
3905       {
3906       case LMCIOCGSTAT:
3907       case LMCIOCGCFG:
3908       case LMCIOCSCFG:
3909       case LMCIOCREAD:
3910       case LMCIOCWRITE:
3911       case LMCIOCTL:
3912         {
3913         /* Call the core ioctl procedure. */
3914         error = core_ioctl(sc, msg->header.cmd, msg->data);
3915         if ((msg->header.cmd & IOC_OUT) != 0)
3916           { /* synchronous response */
3917           NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg) +
3918            IOCPARM_LEN(msg->header.cmd), M_NOWAIT);
3919           if (resp == NULL)
3920             error = ENOMEM;
3921           else
3922             memcpy(resp->data, msg->data, IOCPARM_LEN(msg->header.cmd));
3923           }
3924         break;
3925         }
3926       default:
3927         error = EINVAL;
3928         break;
3929       }
3930     }
3931   else if ((msg->header.typecookie == NGM_GENERIC_COOKIE) &&
3932            (msg->header.cmd == NGM_TEXT_STATUS))
3933     {  /* synchronous response */
3934     NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg) +
3935      NG_TEXTRESPONSE, M_NOWAIT);
3936     if (resp == NULL)
3937       error = ENOMEM;
3938     else
3939       {
3940       char *s = resp->data;
3941       sprintf(s, "Card type = <%s>\n"
3942        "This driver considers the link to be %s.\n"
3943        "Use lmcconfig to configure this interface.\n",
3944        sc->dev_desc, (sc->status.oper_status==STATUS_UP) ? "UP" : "DOWN");
3945       resp->header.arglen = strlen(s) +1;
3946       }
3947     }
3948   else
3949 /* Netgraph should be able to read and write these
3950  *  parameters with text-format control messages:
3951  *  SSI      HSSI     T1E1     T3
3952  *  crc      crc      crc      crc      
3953  *  loop     loop     loop     loop
3954  *           clksrc   clksrc
3955  *  dte      dte      format   format
3956  *  synth    synth    cablen   cablen
3957  *  cable             timeslot scram
3958  *                    gain
3959  *                    pulse
3960  *                    lbo
3961  * Someday I'll implement this...
3962  */
3963     error = EINVAL;
3964
3965   /* Handle synchronous response. */
3966   NG_RESPOND_MSG(error, node, item, resp);
3967   NG_FREE_MSG(msg);
3968
3969   return error;
3970   }
3971
3972 /* This is a persistent netgraph node. */
3973 static int
3974 ng_shutdown(node_p node)
3975   {
3976   /* unless told to really die, bounce back to life */
3977   if ((node->nd_flags & NG_REALLY_DIE)==0)
3978     node->nd_flags &= ~NG_INVALID; /* bounce back to life */
3979
3980   return 0;
3981   }
3982
3983 /* ng_disconnect is the opposite of this procedure. */
3984 static int
3985 ng_newhook(node_p node, hook_p hook, const char *name)
3986   {
3987   softc_t *sc = NG_NODE_PRIVATE(node);
3988
3989   /* Hook name must be 'rawdata'. */
3990   if (strncmp(name, "rawdata", 7) != 0) return EINVAL;
3991
3992   /* Is our hook connected? */
3993   if (sc->ng_hook != NULL) return EBUSY;
3994
3995   /* Accept the hook. */
3996   sc->ng_hook = hook;
3997
3998   return 0;
3999   }
4000
4001 /* Both ends have accepted their hooks and the links have been made. */
4002 /* This is the last chance to reject the connection request. */
4003 static int
4004 ng_connect(hook_p hook)
4005   {
4006   /* Probably not at splnet, force outward queueing. (huh?) */
4007   NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
4008   return 0; /* always accept */
4009   }
4010
4011 /* Receive data in mbufs from another Netgraph node. */
4012 /* Transmit an mbuf-chain on the communication link. */
4013 /* This procedure is very similar to lmc_raw_output(). */
4014 /* Called from a syscall (user context; no spinlocks). */
4015 static int
4016 ng_rcvdata(hook_p hook, item_p item)
4017   {
4018   softc_t *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
4019   int error = 0;
4020   struct mbuf *m;
4021   meta_p meta = NULL;
4022
4023   NGI_GET_M(item, m);
4024   NGI_GET_META(item, meta);
4025   NG_FREE_ITEM(item);
4026
4027   /* This macro must not store into meta! */
4028   NG_FREE_META(meta);
4029
4030   /* Fail if the link is down. */
4031   if (sc->status.oper_status  != STATUS_UP)
4032     {
4033     m_freem(m);
4034     sc->status.cntrs.odiscards++;
4035     if (DRIVER_DEBUG)
4036       printf("%s: ng_rcvdata: tx pkt discarded: link down\n", NAME_UNIT);
4037     return ENETDOWN;
4038     }
4039
4040   /* ng_rcvdata() ENQUEUEs in a syscall or softirq. */
4041   /* txintr_setup() DEQUEUEs in a hard interrupt. */
4042   /* Some BSD QUEUE routines are not interrupt-safe. */
4043   {
4044   DISABLE_INTR;
4045   if (meta==NULL)
4046     IFQ_ENQUEUE(&sc->ng_sndq, m, error);
4047   else
4048     IFQ_ENQUEUE(&sc->ng_fastq, m, error);
4049   ENABLE_INTR;
4050   }
4051
4052   if (error==0)
4053     user_interrupt(sc, 0); /* start the transmitter */
4054   else
4055     {
4056     m_freem(m);
4057     sc->status.cntrs.odiscards++;
4058     if (DRIVER_DEBUG)
4059       printf("%s: ng_rcvdata: IFQ_ENQUEUE() failed; error %d\n",
4060        NAME_UNIT, error);
4061     }
4062
4063   return error;
4064   }
4065
4066 /* ng_newhook is the opposite of this procedure, not */
4067 /*  ng_connect, as you might expect from the names. */
4068 static int
4069 ng_disconnect(hook_p hook)
4070   {
4071   softc_t *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
4072
4073   /* Disconnect the hook. */
4074   sc->ng_hook = NULL;
4075
4076   return 0;
4077   }
4078
4079 static
4080 struct ng_type ng_type =
4081   {
4082   .version      = NG_ABI_VERSION,
4083   .name         = NG_LMC_NODE_TYPE,
4084   .mod_event    = NULL,
4085   .constructor  = ng_constructor,
4086   .rcvmsg       = ng_rcvmsg,
4087   .close        = NULL,
4088   .shutdown     = ng_shutdown,
4089   .newhook      = ng_newhook,
4090   .findhook     = NULL,
4091   .connect      = ng_connect,
4092   .rcvdata      = ng_rcvdata,
4093   .disconnect   = ng_disconnect,
4094   };
4095
4096
4097 /* Attach to the Netgraph kernel interface (/sys/netgraph).
4098  * It is called once for each physical card during device attach.
4099  * This is effectively ng_constructor.
4100  */
4101 static int
4102 ng_attach(softc_t *sc)
4103   {
4104   int error;
4105
4106   /* If this node type is not known to Netgraph then register it. */
4107   if (ng_type.refs == 0) /* or: if (ng_findtype(&ng_type) == NULL) */
4108     {
4109     if ((error = ng_newtype(&ng_type)))
4110       {
4111       printf("%s: ng_newtype() failed; error %d\n", NAME_UNIT, error);
4112       return error;
4113       }
4114     }
4115   else
4116     NG_TYPE_REF(&ng_type);
4117
4118   /* Call the superclass node constructor. */
4119   if ((error = ng_make_node_common(&ng_type, &sc->ng_node)))
4120     {
4121     NG_TYPE_UNREF(&ng_type);
4122     printf("%s: ng_make_node_common() failed; error %d\n", NAME_UNIT, error);
4123     return error;
4124     }
4125
4126   /* Associate a name with this netgraph node. */
4127   if ((error = ng_name_node(sc->ng_node, NAME_UNIT)))
4128     {
4129     NG_NODE_UNREF(sc->ng_node);
4130     NG_TYPE_UNREF(&ng_type);
4131     printf("%s: ng_name_node() failed; error %d\n", NAME_UNIT, error);
4132     return error;
4133     }
4134
4135   /* Initialize the send queue mutexes. */
4136   mtx_init(&sc->ng_sndq.ifq_mtx,  NAME_UNIT, "sndq",  MTX_DEF);
4137   mtx_init(&sc->ng_fastq.ifq_mtx, NAME_UNIT, "fastq", MTX_DEF);
4138
4139   /* Put a backpointer to the softc in the netgraph node. */
4140   NG_NODE_SET_PRIVATE(sc->ng_node, sc);
4141
4142   /* ALTQ output queue initialization. */
4143   IFQ_SET_MAXLEN(&sc->ng_fastq, SNDQ_MAXLEN);
4144   IFQ_SET_READY(&sc->ng_fastq);
4145   IFQ_SET_MAXLEN(&sc->ng_sndq,  SNDQ_MAXLEN);
4146   IFQ_SET_READY(&sc->ng_sndq);
4147
4148
4149   return 0;
4150   }
4151
4152 static void
4153 ng_detach(softc_t *sc)
4154   {
4155   callout_drain(&sc->callout);
4156   mtx_destroy(&sc->ng_sndq.ifq_mtx);
4157   mtx_destroy(&sc->ng_fastq.ifq_mtx);
4158   ng_rmnode_self(sc->ng_node); /* free hook */
4159   NG_NODE_UNREF(sc->ng_node);  /* free node */
4160   NG_TYPE_UNREF(&ng_type);
4161   }
4162
4163 #endif /* NETGRAPH */
4164
4165 /* The next few procedures initialize the card. */
4166
4167 /* Returns 0 on success; error code on failure. */
4168 static int
4169 startup_card(softc_t *sc)
4170   {
4171   int num_rx_descs, error = 0;
4172   u_int32_t tlp_bus_pbl, tlp_bus_cal, tlp_op_tr;
4173   u_int32_t tlp_cfdd, tlp_cfcs;
4174   u_int32_t tlp_cflt, tlp_csid, tlp_cfit;
4175
4176   /* Make sure the COMMAND bits are reasonable. */
4177   tlp_cfcs = READ_PCI_CFG(sc, TLP_CFCS);
4178   tlp_cfcs &= ~TLP_CFCS_MWI_ENABLE;
4179   tlp_cfcs |=  TLP_CFCS_BUS_MASTER;
4180   tlp_cfcs |=  TLP_CFCS_MEM_ENABLE;
4181   tlp_cfcs |=  TLP_CFCS_IO_ENABLE;
4182   tlp_cfcs |=  TLP_CFCS_PAR_ERROR;
4183   tlp_cfcs |=  TLP_CFCS_SYS_ERROR;
4184   WRITE_PCI_CFG(sc, TLP_CFCS, tlp_cfcs);
4185
4186   /* Set the LATENCY TIMER to the recommended value, */
4187   /*  and make sure the CACHE LINE SIZE is reasonable. */
4188   tlp_cfit = READ_PCI_CFG(sc, TLP_CFIT);
4189   tlp_cflt = READ_PCI_CFG(sc, TLP_CFLT);
4190   tlp_cflt &= ~TLP_CFLT_LATENCY;
4191   tlp_cflt |= (tlp_cfit & TLP_CFIT_MAX_LAT)>>16;
4192   /* "prgmbl burst length" and "cache alignment" used below. */
4193   switch(tlp_cflt & TLP_CFLT_CACHE)
4194     {
4195     case 8: /* 8 bytes per cache line */
4196       { tlp_bus_pbl = 32; tlp_bus_cal = 1; break; }
4197     case 16:
4198       { tlp_bus_pbl = 32; tlp_bus_cal = 2; break; }
4199     case 32:
4200       { tlp_bus_pbl = 32; tlp_bus_cal = 3; break; }
4201     default:
4202       {
4203       tlp_bus_pbl = 32; tlp_bus_cal = 1;
4204       tlp_cflt &= ~TLP_CFLT_CACHE;
4205       tlp_cflt |= 8;
4206       break;
4207       }
4208     }
4209   WRITE_PCI_CFG(sc, TLP_CFLT, tlp_cflt);
4210
4211   /* Make sure SNOOZE and SLEEP modes are disabled. */
4212   tlp_cfdd = READ_PCI_CFG(sc, TLP_CFDD);
4213   tlp_cfdd &= ~TLP_CFDD_SLEEP;
4214   tlp_cfdd &= ~TLP_CFDD_SNOOZE;
4215   WRITE_PCI_CFG(sc, TLP_CFDD, tlp_cfdd);
4216   DELAY(11*1000); /* Tulip wakes up in 10 ms max */
4217
4218   /* Software Reset the Tulip chip; stops DMA and Interrupts. */
4219   /* This does not change the PCI config regs just set above. */
4220   WRITE_CSR(TLP_BUS_MODE, TLP_BUS_RESET); /* self-clearing */
4221   DELAY(5);  /* Tulip is dead for 50 PCI cycles after reset. */
4222
4223   /* Reset the Xilinx Field Programmable Gate Array. */
4224   reset_xilinx(sc); /* side effect: turns on all four LEDs */
4225
4226   /* Configure card-specific stuff (framers, line interfaces, etc.). */
4227   sc->card->config(sc);
4228
4229   /* Initializing cards can glitch clocks and upset fifos. */
4230   /* Reset the FIFOs between the Tulip and Xilinx chips. */
4231   set_mii16_bits(sc, MII16_FIFO);
4232   clr_mii16_bits(sc, MII16_FIFO);
4233
4234   /* Initialize the PCI busmode register. */
4235   /* The PCI bus cycle type "Memory Write and Invalidate" does NOT */
4236   /*  work cleanly in any version of the 21140A, so don't enable it! */
4237   WRITE_CSR(TLP_BUS_MODE,
4238         (tlp_bus_cal ? TLP_BUS_READ_LINE : 0) |
4239         (tlp_bus_cal ? TLP_BUS_READ_MULT : 0) |
4240         (tlp_bus_pbl<<TLP_BUS_PBL_SHIFT) |
4241         (tlp_bus_cal<<TLP_BUS_CAL_SHIFT) |
4242    ((BYTE_ORDER == BIG_ENDIAN) ? TLP_BUS_DESC_BIGEND : 0) |
4243    ((BYTE_ORDER == BIG_ENDIAN) ? TLP_BUS_DATA_BIGEND : 0) |
4244                 TLP_BUS_DSL_VAL |
4245                 TLP_BUS_ARB);
4246
4247   /* Pick number of RX descriptors and TX fifo threshold. */
4248   /* tx_threshold in bytes: 0=128, 1=256, 2=512, 3=1024 */
4249   tlp_csid = READ_PCI_CFG(sc, TLP_CSID);
4250   switch(tlp_csid)
4251     {
4252     case TLP_CSID_HSSI:         /* 52 Mb/s */
4253     case TLP_CSID_HSSIc:        /* 52 Mb/s */
4254     case TLP_CSID_T3:           /* 45 Mb/s */
4255       { num_rx_descs = 48; tlp_op_tr = 2; break; }
4256     case TLP_CSID_SSI:          /* 10 Mb/s */
4257       { num_rx_descs = 32; tlp_op_tr = 1; break; }
4258     case TLP_CSID_T1E1:         /*  2 Mb/s */
4259       { num_rx_descs = 16; tlp_op_tr = 0; break; }
4260     default:
4261       { num_rx_descs = 16; tlp_op_tr = 0; break; }
4262     }
4263
4264   /* Create DMA descriptors and initialize list head registers. */
4265   if ((error = create_ring(sc, &sc->txring, NUM_TX_DESCS))) return error;
4266   WRITE_CSR(TLP_TX_LIST, sc->txring.dma_addr);
4267   if ((error = create_ring(sc, &sc->rxring, num_rx_descs))) return error;
4268   WRITE_CSR(TLP_RX_LIST, sc->rxring.dma_addr);
4269
4270   /* Initialize the operating mode register. */
4271   WRITE_CSR(TLP_OP_MODE, TLP_OP_INIT | (tlp_op_tr<<TLP_OP_TR_SHIFT));
4272
4273   /* Read the missed frame register (result ignored) to zero it. */
4274   error = READ_CSR( TLP_MISSED); /* error is used as a bit-dump */
4275
4276   /* Disable rx watchdog and tx jabber features. */
4277   WRITE_CSR(TLP_WDOG, TLP_WDOG_INIT);
4278
4279   /* Enable card interrupts. */
4280   WRITE_CSR(TLP_INT_ENBL, TLP_INT_TXRX);
4281
4282   return 0;
4283   }
4284
4285 /* Stop DMA and Interrupts; free descriptors and buffers. */
4286 static void
4287 shutdown_card(void *arg)
4288   {
4289   softc_t *sc = arg;
4290
4291   /* Leave the LEDs in the state they were in after power-on. */
4292   led_on(sc, MII16_LED_ALL);
4293
4294   /* Software reset the Tulip chip; stops DMA and Interrupts */
4295   WRITE_CSR(TLP_BUS_MODE, TLP_BUS_RESET); /* self-clearing */
4296   DELAY(5);  /* Tulip is dead for 50 PCI cycles after reset. */
4297
4298   /* Disconnect from the PCI bus except for config cycles. */
4299   /* Hmmm; Linux syslogs a warning that IO and MEM are disabled. */
4300   WRITE_PCI_CFG(sc, TLP_CFCS, TLP_CFCS_MEM_ENABLE | TLP_CFCS_IO_ENABLE);
4301
4302   /* Free the DMA descriptor rings. */
4303   destroy_ring(sc, &sc->txring);
4304   destroy_ring(sc, &sc->rxring);
4305   }
4306
4307 /* Start the card and attach a kernel interface and line protocol. */
4308 static int
4309 attach_card(softc_t *sc, const char *intrstr)
4310   {
4311   struct config config;
4312   u_int32_t tlp_cfrv;
4313   u_int16_t mii3;
4314   u_int8_t *ieee;
4315   int i, error = 0;
4316
4317   /* Start the card. */
4318   if ((error = startup_card(sc))) return error;
4319
4320   callout_init(&sc->callout, 0);
4321
4322   /* Attach a kernel interface. */
4323 #if NETGRAPH
4324   if ((error = ng_attach(sc))) return error;
4325   sc->flags |= FLAG_NETGRAPH;
4326 #endif
4327   if ((error = lmc_ifnet_attach(sc))) return error;
4328   sc->flags |= FLAG_IFNET;
4329
4330   /* Attach a line protocol stack. */
4331   sc->config.line_pkg = PKG_RAWIP;
4332   config = sc->config;  /* get current config */
4333   config.line_pkg = 0;  /* select external stack */
4334   config.line_prot = PROT_C_HDLC;
4335   config.keep_alive = 1;
4336   config_proto(sc, &config); /* reconfigure */
4337   sc->config = config;  /* save new configuration */
4338
4339   /* Print interesting hardware-related things. */
4340   mii3 = read_mii(sc, 3);
4341   tlp_cfrv = READ_PCI_CFG(sc, TLP_CFRV);
4342   printf("%s: PCI rev %d.%d, MII rev %d.%d", NAME_UNIT,
4343    (tlp_cfrv>>4) & 0xF, tlp_cfrv & 0xF, (mii3>>4) & 0xF, mii3 & 0xF);
4344   ieee = (u_int8_t *)sc->status.ieee;
4345   for (i=0; i<3; i++) sc->status.ieee[i] = read_srom(sc, 10+i);
4346   printf(", IEEE addr %02x:%02x:%02x:%02x:%02x:%02x",
4347    ieee[0], ieee[1], ieee[2], ieee[3], ieee[4], ieee[5]);
4348   sc->card->ident(sc);
4349   printf(" %s\n", intrstr);
4350
4351   /* Print interesting software-related things. */
4352   printf("%s: Driver rev %d.%d.%d", NAME_UNIT,
4353    DRIVER_MAJOR_VERSION, DRIVER_MINOR_VERSION, DRIVER_SUB_VERSION);
4354   printf(", Options %s%s%s%s%s%s%s%s%s\n",
4355    NETGRAPH ? "NETGRAPH " : "", GEN_HDLC ? "GEN_HDLC " : "",
4356    NSPPP ? "SPPP " : "", P2P ? "P2P " : "",
4357    ALTQ_PRESENT ? "ALTQ " : "", NBPFILTER ? "BPF " : "",
4358    DEV_POLL ? "POLL " : "", IOREF_CSR ? "IO_CSR " : "MEM_CSR ",
4359    (BYTE_ORDER == BIG_ENDIAN) ? "BIG_END " : "LITTLE_END ");
4360
4361   /* Make the local hardware ready. */
4362   set_status(sc, 1);
4363
4364   return 0;
4365   }
4366
4367 /* Detach from the kernel in all ways. */
4368 static void
4369 detach_card(softc_t *sc)
4370   {
4371   struct config config;
4372
4373   /* Make the local hardware NOT ready. */
4374   set_status(sc, 0);
4375
4376   /* Detach external line protocol stack. */
4377   if (sc->config.line_pkg != PKG_RAWIP)
4378     {
4379     config = sc->config;
4380     config.line_pkg = PKG_RAWIP;
4381     config_proto(sc, &config);
4382     sc->config = config;
4383     }
4384
4385   /* Detach kernel interfaces. */
4386 #if NETGRAPH
4387   if (sc->flags & FLAG_NETGRAPH)
4388     {
4389     IFQ_PURGE(&sc->ng_fastq);
4390     IFQ_PURGE(&sc->ng_sndq);
4391     ng_detach(sc);
4392     sc->flags &= ~FLAG_NETGRAPH;
4393     }
4394 #endif
4395   if (sc->flags & FLAG_IFNET)
4396     {
4397     IFQ_PURGE(&sc->ifp->if_snd);
4398     lmc_ifnet_detach(sc);
4399     sc->flags &= ~FLAG_IFNET;
4400     }
4401
4402   /* Reset the Tulip chip; stops DMA and Interrupts. */
4403   shutdown_card(sc);
4404   }
4405
4406 /* This is the I/O configuration interface for FreeBSD */
4407
4408
4409 static int
4410 fbsd_probe(device_t dev)
4411   {
4412   u_int32_t cfid = pci_read_config(dev, TLP_CFID, 4);
4413   u_int32_t csid = pci_read_config(dev, TLP_CSID, 4);
4414
4415   /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
4416   if (cfid != TLP_CFID_TULIP) return ENXIO;
4417   switch (csid)
4418     {
4419     case TLP_CSID_HSSI:
4420     case TLP_CSID_HSSIc:
4421       device_set_desc(dev, HSSI_DESC);
4422       break;
4423     case TLP_CSID_T3:
4424       device_set_desc(dev,   T3_DESC);
4425       break;
4426     case TLP_CSID_SSI:
4427       device_set_desc(dev,  SSI_DESC);
4428       break;
4429     case TLP_CSID_T1E1:
4430       device_set_desc(dev, T1E1_DESC);
4431       break;
4432     default:
4433       return ENXIO;
4434     }
4435   return 0;
4436   }
4437
4438 static int
4439 fbsd_detach(device_t dev)
4440   {
4441   softc_t *sc = device_get_softc(dev);
4442
4443   /* Stop the card and detach from the kernel. */
4444   detach_card(sc);
4445
4446   /* Release resources. */
4447   if (sc->irq_cookie != NULL)
4448     {
4449     bus_teardown_intr(dev, sc->irq_res, sc->irq_cookie);
4450     sc->irq_cookie = NULL;
4451     }
4452   if (sc->irq_res != NULL)
4453     {
4454     bus_release_resource(dev, SYS_RES_IRQ, sc->irq_res_id, sc->irq_res);
4455     sc->irq_res = NULL;
4456     }
4457   if (sc->csr_res != NULL)
4458     {
4459     bus_release_resource(dev, sc->csr_res_type, sc->csr_res_id, sc->csr_res);
4460     sc->csr_res = NULL;
4461     }
4462
4463   mtx_destroy(&sc->top_mtx);
4464   mtx_destroy(&sc->bottom_mtx);
4465   return 0; /* no error */
4466   }
4467
4468 static int
4469 fbsd_shutdown(device_t dev)
4470   {
4471   shutdown_card(device_get_softc(dev));
4472   return 0;
4473   }
4474
4475 static int
4476 fbsd_attach(device_t dev)
4477   {
4478   softc_t *sc = device_get_softc(dev);
4479   int error;
4480
4481   /* READ/WRITE_PCI_CFG need this. */
4482   sc->dev = dev;
4483
4484   /* What kind of card are we driving? */
4485   switch (READ_PCI_CFG(sc, TLP_CSID))
4486     {
4487     case TLP_CSID_HSSI:
4488     case TLP_CSID_HSSIc:
4489       sc->card = &hssi_card;
4490       break;
4491     case TLP_CSID_T3:
4492       sc->card =   &t3_card;
4493       break;
4494     case TLP_CSID_SSI:
4495       sc->card =  &ssi_card;
4496       break;
4497     case TLP_CSID_T1E1:
4498       sc->card =   &t1_card;
4499       break;
4500     default:
4501       return ENXIO;
4502     }
4503   sc->dev_desc = device_get_desc(dev);
4504
4505   /* Allocate PCI memory or IO resources to access the Tulip chip CSRs. */
4506 # if IOREF_CSR
4507   sc->csr_res_id   = TLP_CBIO;
4508   sc->csr_res_type = SYS_RES_IOPORT;
4509 # else
4510   sc->csr_res_id   = TLP_CBMA;
4511   sc->csr_res_type = SYS_RES_MEMORY;
4512 # endif
4513   sc->csr_res = bus_alloc_resource(dev, sc->csr_res_type, &sc->csr_res_id,
4514    0, ~0, 1, RF_ACTIVE);
4515   if (sc->csr_res == NULL)
4516     {
4517     printf("%s: bus_alloc_resource(csr) failed.\n", NAME_UNIT);
4518     return ENXIO;
4519     }
4520   sc->csr_tag    = rman_get_bustag(sc->csr_res);
4521   sc->csr_handle = rman_get_bushandle(sc->csr_res); 
4522
4523   /* Allocate PCI interrupt resources for the card. */
4524   sc->irq_res_id = 0;
4525   sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_res_id,
4526    0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
4527   if (sc->irq_res == NULL)
4528     {
4529     printf("%s: bus_alloc_resource(irq) failed.\n", NAME_UNIT);
4530     fbsd_detach(dev);
4531     return ENXIO;
4532     }
4533   if ((error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
4534    NULL, bsd_interrupt, sc, &sc->irq_cookie)))
4535     {
4536     printf("%s: bus_setup_intr() failed; error %d\n", NAME_UNIT, error);
4537     fbsd_detach(dev);
4538     return error;
4539     }
4540
4541   /* Initialize the top-half and bottom-half locks. */
4542   mtx_init(&sc->top_mtx,    NAME_UNIT, "top half lock",    MTX_DEF);
4543   mtx_init(&sc->bottom_mtx, NAME_UNIT, "bottom half lock", MTX_DEF);
4544
4545   /* Start the card and attach a kernel interface and line protocol. */
4546   if ((error = attach_card(sc, ""))) detach_card(sc);
4547   return error;
4548   }
4549
4550 static device_method_t methods[] =
4551   {
4552   DEVMETHOD(device_probe,    fbsd_probe),
4553   DEVMETHOD(device_attach,   fbsd_attach),
4554   DEVMETHOD(device_detach,   fbsd_detach),
4555   DEVMETHOD(device_shutdown, fbsd_shutdown),
4556   /* This driver does not suspend and resume. */
4557   { 0, 0 }
4558   };
4559
4560 static driver_t driver =
4561   {
4562   .name    = DEVICE_NAME,
4563   .methods = methods,
4564   .size    = sizeof(softc_t),
4565   };
4566
4567 static devclass_t devclass;
4568
4569 DRIVER_MODULE(lmc, pci, driver, devclass, 0, 0);
4570 MODULE_VERSION(lmc, 2);
4571 MODULE_DEPEND(lmc, pci, 1, 1, 1);
4572 # if NETGRAPH
4573 MODULE_DEPEND(lmc, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
4574 # endif
4575 # if NSPPP
4576 MODULE_DEPEND(lmc, sppp, 1, 1, 1);
4577 # endif
4578
4579
4580 /* This is the I/O configuration interface for NetBSD. */
4581
4582
4583 /* This is the I/O configuration interface for OpenBSD. */
4584
4585
4586 /* This is the I/O configuration interface for BSD/OS. */
4587
4588