]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/lmc/if_lmc.c
MFV r272851:
[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   MCLGET(m, M_NOWAIT);
2693   if ((m->m_flags & M_EXT) == 0)
2694     {
2695     m_freem(m);
2696     sc->status.cntrs.rxdma++;
2697     if (DRIVER_DEBUG)
2698       printf("%s: rxintr_setup: MCLGET() failed\n", NAME_UNIT);
2699     return 0;
2700     }
2701
2702   /* Queue the mbuf for later processing by rxintr_cleanup. */
2703   mbuf_enqueue(ring, m);
2704
2705   /* Write a DMA descriptor into the ring. */
2706   /* Hardware won't see it until the OWNER bit is set. */
2707   desc = ring->write;
2708   /* Advance the ring write pointer. */
2709   if (ring->write++ == ring->last) ring->write = ring->first;
2710
2711   desc_len = (MCLBYTES < MAX_DESC_LEN) ? MCLBYTES : MAX_DESC_LEN;
2712   /* Map kernel virtual address to PCI address. */
2713   if ((error = DMA_LOAD(desc->map, m->m_data, desc_len)))
2714     printf("%s: bus_dmamap_load(rx) failed; error %d\n", NAME_UNIT, error);
2715   /* Invalidate the cache for this mbuf. */
2716   DMA_SYNC(desc->map, desc_len, BUS_DMASYNC_PREREAD);
2717
2718   /* Set up the DMA descriptor. */
2719   desc->address1 = ring->segs[0].ds_addr;
2720   desc->length1  = desc_len>>1;
2721   desc->address2 = desc->address1 + desc->length1;
2722   desc->length2  = desc_len>>1;
2723
2724   /* Before setting the OWNER bit, flush the cache (memory barrier). */
2725   DMA_SYNC(ring->map, ring->size_descs, BUS_DMASYNC_PREWRITE);
2726
2727   /* Commit the DMA descriptor to the hardware. */
2728   desc->status = TLP_DSTS_OWNER;
2729
2730   /* Notify the receiver that there is another buffer available. */
2731   WRITE_CSR(TLP_RX_POLL, 1);
2732
2733   return 1; /* did something */
2734   }
2735
2736 /* Clean up after a packet has been transmitted. */
2737 /* Free the mbuf chain and update the DMA descriptor ring. */
2738 static int  /* BSD version */
2739 txintr_cleanup(softc_t *sc)
2740   {
2741   struct desc_ring *ring = &sc->txring;
2742   struct dma_desc *desc;
2743
2744   while ((ring->read != ring->write) && /* while ring is not empty */
2745         ((ring->read->status & TLP_DSTS_OWNER) == 0))
2746     {
2747     /* Read a DMA descriptor from the ring. */
2748     desc = ring->read;
2749     /* Advance the ring read pointer. */
2750     if (ring->read++ == ring->last) ring->read = ring->first;
2751
2752     /* This is a no-op on most architectures. */
2753     DMA_SYNC(desc->map, desc->length1 + desc->length2, BUS_DMASYNC_POSTWRITE);
2754     /* Unmap kernel virtual address to PCI address. */
2755     bus_dmamap_unload(ring->tag, desc->map);
2756
2757     /* If this descriptor is the last segment of a packet, */
2758     /*  then dequeue and free the corresponding mbuf chain. */
2759     if ((desc->control & TLP_DCTL_TX_LAST_SEG) != 0)
2760       {
2761       struct mbuf *m;
2762       if ((m = mbuf_dequeue(ring)) == NULL)
2763         panic("%s: txintr_cleanup: expected an mbuf\n", NAME_UNIT);
2764
2765       /* Include CRC and one flag byte in output byte count. */
2766       sc->status.cntrs.obytes += m->m_pkthdr.len + sc->config.crc_len +1;
2767       sc->status.cntrs.opackets++;
2768       if_inc_counter(sc->ifp, IFCOUNTER_OPACKETS, 1);
2769       LMC_BPF_MTAP(m);
2770       /* The only bad TX status is fifo underrun. */
2771       if ((desc->status & TLP_DSTS_TX_UNDERRUN) != 0)
2772         sc->status.cntrs.fifo_under++;
2773
2774       m_freem(m);
2775       return 1;  /* did something */
2776       }
2777     }
2778
2779   return 0;
2780   }
2781
2782 /* Build DMA descriptors for a transmit packet mbuf chain. */
2783 static int /* 0=success; 1=error */ /* BSD version */
2784 txintr_setup_mbuf(softc_t *sc, struct mbuf *m)
2785   {
2786   struct desc_ring *ring = &sc->txring;
2787   struct dma_desc *desc;
2788   unsigned int desc_len;
2789
2790   /* build DMA descriptors for a chain of mbufs. */
2791   while (m != NULL)
2792     {
2793     char *data = m->m_data;
2794     int length = m->m_len; /* zero length mbufs happen! */
2795
2796     /* Build DMA descriptors for one mbuf. */
2797     while (length > 0)
2798       {
2799       int error;
2800
2801       /* Ring is full if (wrap(write+1)==read) */
2802       if (((ring->temp==ring->last) ? ring->first : ring->temp+1) == ring->read)
2803         { /* Not enough DMA descriptors; try later. */
2804         for (; ring->temp!=ring->write;
2805          ring->temp = (ring->temp==ring->first)? ring->last : ring->temp-1)
2806           bus_dmamap_unload(ring->tag, ring->temp->map);
2807         sc->status.cntrs.txdma++;
2808         return 1;
2809         }
2810
2811       /* Provisionally, write a descriptor into the ring. */
2812       /* But don't change the REAL ring write pointer. */
2813       /* Hardware won't see it until the OWNER bit is set. */
2814       desc = ring->temp;
2815       /* Advance the temporary ring write pointer. */
2816       if (ring->temp++ == ring->last) ring->temp = ring->first;
2817
2818       /* Clear all control bits except the END_RING bit. */
2819       desc->control &= TLP_DCTL_END_RING;
2820       /* Don't pad short packets up to 64 bytes. */
2821       desc->control |= TLP_DCTL_TX_NO_PAD;
2822       /* Use Tulip's CRC-32 generator, if appropriate. */
2823       if (sc->config.crc_len != CFG_CRC_32)
2824         desc->control |= TLP_DCTL_TX_NO_CRC;
2825       /* Set the OWNER bit, except in the first descriptor. */
2826       if (desc != ring->write)
2827         desc->status = TLP_DSTS_OWNER;
2828
2829       desc_len = (length > MAX_CHUNK_LEN) ? MAX_CHUNK_LEN : length;
2830       /* Map kernel virtual address to PCI address. */
2831       if ((error = DMA_LOAD(desc->map, data, desc_len)))
2832         printf("%s: bus_dmamap_load(tx) failed; error %d\n", NAME_UNIT, error);
2833       /* Flush the cache and if bouncing, copy mbuf to bounce buf. */
2834       DMA_SYNC(desc->map, desc_len, BUS_DMASYNC_PREWRITE);
2835
2836       /* Prevent wild fetches if mapping fails (nsegs==0). */
2837       desc->length1  = desc->length2  = 0;
2838       desc->address1 = desc->address2 = 0;
2839         {
2840         bus_dma_segment_t *segs = ring->segs;
2841         int nsegs = ring->nsegs;
2842         if (nsegs >= 1)
2843           {
2844           desc->address1 = segs[0].ds_addr;
2845           desc->length1  = segs[0].ds_len;
2846           }
2847         if (nsegs == 2)
2848           {
2849           desc->address2 = segs[1].ds_addr;
2850           desc->length2  = segs[1].ds_len;
2851           }
2852         }
2853
2854       data   += desc_len;
2855       length -= desc_len;
2856       } /* while (length > 0) */
2857
2858     m = m->m_next;
2859     } /* while (m != NULL) */
2860
2861   return 0; /* success */
2862   }
2863
2864 /* Setup (prepare) to transmit a packet. */
2865 /* Select a packet, build DMA descriptors and give packet to hardware. */
2866 /* If DMA descriptors run out, abandon the attempt and return 0. */
2867 static int  /* BSD version */
2868 txintr_setup(softc_t *sc)
2869   {
2870   struct desc_ring *ring = &sc->txring;
2871   struct dma_desc *first_desc, *last_desc;
2872
2873   /* Protect against half-up links: Don't transmit */
2874   /*  if the receiver can't hear the far end. */
2875   if (sc->status.oper_status != STATUS_UP) return 0;
2876
2877   /* Pick a packet to transmit. */
2878 #if NETGRAPH
2879   if ((sc->ng_hook != NULL) && (sc->tx_mbuf == NULL))
2880     {
2881     if (!IFQ_IS_EMPTY(&sc->ng_fastq))
2882       IFQ_DEQUEUE(&sc->ng_fastq, sc->tx_mbuf);
2883     else
2884       IFQ_DEQUEUE(&sc->ng_sndq,  sc->tx_mbuf);
2885     }
2886   else
2887 #endif
2888   if (sc->tx_mbuf == NULL)
2889     {
2890     if (sc->config.line_pkg == PKG_RAWIP)
2891       IFQ_DEQUEUE(&sc->ifp->if_snd, sc->tx_mbuf);
2892     else
2893       {
2894 #if NSPPP
2895       sc->tx_mbuf = sppp_dequeue(sc->ifp);
2896 #elif P2P
2897       if (!IFQ_IS_EMPTY(&sc->p2p->p2p_isnd))
2898         IFQ_DEQUEUE(&sc->p2p->p2p_isnd, sc->tx_mbuf);
2899       else
2900         IFQ_DEQUEUE(&sc->ifp->if_snd, sc->tx_mbuf);
2901 #endif
2902       }
2903     }
2904   if (sc->tx_mbuf == NULL) return 0;  /* no pkt to transmit */
2905
2906   /* Build DMA descriptors for an outgoing mbuf chain. */
2907   ring->temp = ring->write; /* temporary ring write pointer */
2908   if (txintr_setup_mbuf(sc, sc->tx_mbuf) != 0) return 0;
2909
2910   /* Enqueue the mbuf; txintr_cleanup will free it. */
2911   mbuf_enqueue(ring, sc->tx_mbuf);
2912
2913   /* The transmitter has room for another packet. */
2914   sc->tx_mbuf = NULL;
2915
2916   /* Set first & last segment bits. */
2917   /* last_desc is the desc BEFORE the one pointed to by ring->temp. */
2918   first_desc = ring->write;
2919   first_desc->control |= TLP_DCTL_TX_FIRST_SEG;
2920   last_desc = (ring->temp==ring->first)? ring->last : ring->temp-1;
2921    last_desc->control |= TLP_DCTL_TX_LAST_SEG;
2922   /* Interrupt at end-of-transmission?  Why bother the poor computer! */
2923 /* last_desc->control |= TLP_DCTL_TX_INTERRUPT; */
2924
2925   /* Make sure the OWNER bit is not set in the next descriptor. */
2926   /* The OWNER bit may have been set if a previous call aborted. */
2927   ring->temp->status = 0;
2928
2929   /* Commit the DMA descriptors to the software. */
2930   ring->write = ring->temp;
2931
2932   /* Before setting the OWNER bit, flush the cache (memory barrier). */
2933   DMA_SYNC(ring->map, ring->size_descs, BUS_DMASYNC_PREWRITE);
2934
2935   /* Commit the DMA descriptors to the hardware. */
2936   first_desc->status = TLP_DSTS_OWNER;
2937
2938   /* Notify the transmitter that there is another packet to send. */
2939   WRITE_CSR(TLP_TX_POLL, 1);
2940
2941   return 1; /* did something */
2942   }
2943
2944
2945
2946 static void
2947 check_intr_status(softc_t *sc)
2948   {
2949   u_int32_t status, cfcs, op_mode;
2950   u_int32_t missed, overruns;
2951
2952   /* Check for four unusual events:
2953    *  1) fatal PCI bus errors       - some are recoverable
2954    *  2) transmitter FIFO underruns - increase fifo threshold
2955    *  3) receiver FIFO overruns     - clear potential hangup
2956    *  4) no receive descs or bufs   - count missed packets
2957    */
2958
2959   /* 1) A fatal bus error causes a Tulip to stop initiating bus cycles. */
2960   /* Module unload/load or boot are the only fixes for Parity Errors. */
2961   /* Master and Target Aborts can be cleared and life may continue. */
2962   status = READ_CSR(TLP_STATUS);
2963   if ((status & TLP_STAT_FATAL_ERROR) != 0)
2964     {
2965     u_int32_t fatal = (status & TLP_STAT_FATAL_BITS)>>TLP_STAT_FATAL_SHIFT;
2966     printf("%s: FATAL PCI BUS ERROR: %s%s%s%s\n", NAME_UNIT,
2967      (fatal == 0) ? "PARITY ERROR" : "",
2968      (fatal == 1) ? "MASTER ABORT" : "",
2969      (fatal == 2) ? "TARGET ABORT" : "",
2970      (fatal >= 3) ? "RESERVED (?)" : "");
2971     cfcs = READ_PCI_CFG(sc, TLP_CFCS);  /* try to clear it */
2972     cfcs &= ~(TLP_CFCS_MSTR_ABORT | TLP_CFCS_TARG_ABORT);
2973     WRITE_PCI_CFG(sc, TLP_CFCS, cfcs);
2974     }
2975
2976   /* 2) If the transmitter fifo underruns, increase the transmit fifo */
2977   /*  threshold: the number of bytes required to be in the fifo */
2978   /*  before starting the transmitter (cost: increased tx delay). */
2979   /* The TX_FSM must be stopped to change this parameter. */
2980   if ((status & TLP_STAT_TX_UNDERRUN) != 0)
2981     {
2982     op_mode = READ_CSR(TLP_OP_MODE);
2983     /* enable store-and-forward mode if tx_threshold tops out? */
2984     if ((op_mode & TLP_OP_TX_THRESH) < TLP_OP_TX_THRESH)
2985       {
2986       op_mode += 0x4000;  /* increment TX_THRESH field; can't overflow */
2987       WRITE_CSR(TLP_OP_MODE, op_mode & ~TLP_OP_TX_RUN);
2988       /* Wait for the TX FSM to stop; it might be processing a pkt. */
2989       while (READ_CSR(TLP_STATUS) & TLP_STAT_TX_FSM); /* XXX HANG */
2990       WRITE_CSR(TLP_OP_MODE, op_mode); /* restart tx */
2991       if (DRIVER_DEBUG)
2992         printf("%s: tx underrun; tx fifo threshold now %d bytes\n",
2993          NAME_UNIT, 128<<((op_mode>>TLP_OP_TR_SHIFT)&3));
2994       }
2995     }
2996
2997   /* 3) Errata memo from Digital Equipment Corp warns that 21140A */
2998   /* receivers through rev 2.2 can hang if the fifo overruns. */
2999   /* Recommended fix: stop and start the RX FSM after an overrun. */
3000   missed = READ_CSR(TLP_MISSED);
3001   if ((overruns = ((missed & TLP_MISS_OVERRUN)>>TLP_OVERRUN_SHIFT)) != 0)
3002     {
3003     if (DRIVER_DEBUG)
3004       printf("%s: rx overrun cntr=%d\n", NAME_UNIT, overruns);
3005     sc->status.cntrs.overruns += overruns;
3006     if ((READ_PCI_CFG(sc, TLP_CFRV) & 0xFF) <= 0x22)
3007       {
3008       op_mode = READ_CSR(TLP_OP_MODE);
3009       WRITE_CSR(TLP_OP_MODE, op_mode & ~TLP_OP_RX_RUN);
3010       /* Wait for the RX FSM to stop; it might be processing a pkt. */
3011       while (READ_CSR(TLP_STATUS) & TLP_STAT_RX_FSM); /* XXX HANG */
3012       WRITE_CSR(TLP_OP_MODE, op_mode);  /* restart rx */
3013       }
3014     }
3015
3016   /* 4) When the receiver is enabled and a packet arrives, but no DMA */
3017   /*  descriptor is available, the packet is counted as 'missed'. */
3018   /* The receiver should never miss packets; warn if it happens. */
3019   if ((missed = (missed & TLP_MISS_MISSED)) != 0)
3020     {
3021     if (DRIVER_DEBUG)
3022       printf("%s: rx missed %d pkts\n", NAME_UNIT, missed);
3023     sc->status.cntrs.missed += missed;
3024     }
3025   }
3026
3027 static void /* This is where the work gets done. */
3028 core_interrupt(void *arg, int check_status)
3029   {
3030   softc_t *sc = arg;
3031   int activity;
3032
3033   /* If any CPU is inside this critical section, then */
3034   /* other CPUs should go away without doing anything. */
3035   if (BOTTOM_TRYLOCK == 0)
3036     {
3037     sc->status.cntrs.lck_intr++;
3038     return;
3039     }
3040
3041   /* Clear pending card interrupts. */
3042   WRITE_CSR(TLP_STATUS, READ_CSR(TLP_STATUS));
3043
3044   /* In Linux, pci_alloc_consistent() means DMA descriptors */
3045   /*  don't need explicit syncing. */
3046   {
3047   struct desc_ring *ring = &sc->txring;
3048   DMA_SYNC(sc->txring.map, sc->txring.size_descs,
3049    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3050   ring = &sc->rxring;
3051   DMA_SYNC(sc->rxring.map, sc->rxring.size_descs,
3052    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3053   }
3054
3055   do  /* This is the main loop for interrupt processing. */
3056     {
3057     activity  = txintr_cleanup(sc);
3058     activity += txintr_setup(sc);
3059     activity += rxintr_cleanup(sc);
3060     activity += rxintr_setup(sc);
3061     } while (activity);
3062
3063   {
3064   struct desc_ring *ring = &sc->txring;
3065   DMA_SYNC(sc->txring.map, sc->txring.size_descs,
3066    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3067   ring = &sc->rxring;
3068   DMA_SYNC(sc->rxring.map, sc->rxring.size_descs,
3069    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3070   }
3071
3072   /* As the interrupt is dismissed, check for four unusual events. */
3073   if (check_status) check_intr_status(sc);
3074
3075   BOTTOM_UNLOCK;
3076   }
3077
3078 /* user_interrupt() may be called from a syscall or a softirq */
3079 static void
3080 user_interrupt(softc_t *sc, int check_status)
3081   {
3082   DISABLE_INTR; /* noop on FreeBSD-5 and Linux */
3083   core_interrupt(sc, check_status);
3084   ENABLE_INTR;  /* noop on FreeBSD-5 and Linux */
3085   }
3086
3087
3088 # if defined(DEVICE_POLLING)
3089
3090 /* Service the card from the kernel idle loop without interrupts. */
3091 static int
3092 fbsd_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3093   {
3094   softc_t *sc = IFP2SC(ifp);
3095
3096
3097   sc->quota = count;
3098   core_interrupt(sc, (cmd==POLL_AND_CHECK_STATUS));
3099   return 0;
3100   }
3101
3102 # endif  /* DEVICE_POLLING */
3103
3104 /* BSD kernels call this procedure when an interrupt happens. */
3105 static intr_return_t
3106 bsd_interrupt(void *arg)
3107   {
3108   softc_t *sc = arg;
3109
3110   /* Cut losses early if this is not our interrupt. */
3111   if ((READ_CSR(TLP_STATUS) & TLP_INT_TXRX) == 0)
3112     return IRQ_NONE;
3113
3114 # if defined(DEVICE_POLLING)
3115   if (sc->ifp->if_capenable & IFCAP_POLLING)
3116     return IRQ_NONE;
3117
3118   if ((sc->ifp->if_capabilities & IFCAP_POLLING) &&
3119    (ether_poll_register(fbsd_poll, sc->ifp)))
3120     {
3121     WRITE_CSR(TLP_INT_ENBL, TLP_INT_DISABLE);
3122     return IRQ_NONE;
3123     }
3124   else
3125     sc->quota = sc->rxring.num_descs; /* input flow control */
3126 # endif  /* DEVICE_POLLING */
3127
3128   /* Disable card interrupts. */
3129   WRITE_CSR(TLP_INT_ENBL, TLP_INT_DISABLE);
3130
3131   core_interrupt(sc, 0);
3132
3133   /* Enable card interrupts. */
3134   WRITE_CSR(TLP_INT_ENBL, TLP_INT_TXRX);
3135
3136   return IRQ_HANDLED;
3137   }
3138
3139
3140 /* Administrative status of the driver (UP or DOWN) has changed. */
3141 /* A card-specific action may be required: T1 and T3 cards: no-op. */
3142 /* HSSI and SSI cards change the state of modem ready signals. */
3143 static void
3144 set_status(softc_t *sc, int status)
3145   {
3146   struct ioctl ioctl;
3147
3148   ioctl.cmd = IOCTL_SET_STATUS;
3149   ioctl.data = status;
3150
3151   sc->card->ioctl(sc, &ioctl);
3152   }
3153
3154 #if P2P
3155
3156 /* Callout from P2P: */
3157 /* Get the state of DCD (Data Carrier Detect). */
3158 static int
3159 p2p_getmdm(struct p2pcom *p2p, caddr_t result)
3160   {
3161   softc_t *sc = IFP2SC(&p2p->p2p_if);
3162
3163   /* Non-zero isn't good enough; TIOCM_CAR is 0x40. */
3164   *(int *)result = (sc->status.oper_status==STATUS_UP) ? TIOCM_CAR : 0;
3165
3166   return 0;
3167   }
3168
3169 /* Callout from P2P: */
3170 /* Set the state of DTR (Data Terminal Ready). */
3171 static int
3172 p2p_mdmctl(struct p2pcom *p2p, int flag)
3173   {
3174   softc_t *sc = IFP2SC(&p2p->p2p_if);
3175
3176   set_status(sc, flag);
3177
3178   return 0;
3179   }
3180
3181 #endif /* P2P */
3182
3183 #if NSPPP
3184
3185 # ifndef PP_FR
3186 #  define PP_FR 0
3187 # endif
3188
3189 /* Callout from SPPP: */
3190 static void
3191 sppp_tls(struct sppp *sppp)
3192   {
3193   if (!(sppp->pp_mode  & IFF_LINK2) &&
3194       !(sppp->pp_flags & PP_FR))
3195     sppp->pp_up(sppp);
3196   }
3197
3198 /* Callout from SPPP: */
3199 static void
3200 sppp_tlf(struct sppp *sppp)
3201   {
3202   if (!(sppp->pp_mode  & IFF_LINK2) &&
3203       !(sppp->pp_flags & PP_FR))
3204     sppp->pp_down(sppp);
3205   }
3206
3207 #endif /* NSPPP */
3208
3209 /* Configure line protocol stuff.
3210  * Called by attach_card() during module init.
3211  * Called by core_ioctl()  when lmcconfig writes sc->config.
3212  * Called by detach_card() during module shutdown.
3213  */
3214 static void
3215 config_proto(softc_t *sc, struct config *config)
3216   {
3217   /* Use line protocol stack instead of RAWIP mode. */
3218   if ((sc->config.line_pkg == PKG_RAWIP) &&
3219          (config->line_pkg != PKG_RAWIP))
3220     {
3221 #if NSPPP
3222     LMC_BPF_DETACH;
3223     sppp_attach(sc->ifp);
3224     LMC_BPF_ATTACH(DLT_PPP, 4);
3225     sc->sppp->pp_tls = sppp_tls;
3226     sc->sppp->pp_tlf = sppp_tlf;
3227     /* Force reconfiguration of SPPP params. */
3228     sc->config.line_prot = 0;
3229     sc->config.keep_alive = config->keep_alive ? 0:1;
3230 #elif P2P
3231     int error = 0;
3232     sc->p2p->p2p_proto = 0; /* force p2p_attach */
3233     if ((error = p2p_attach(sc->p2p))) /* calls bpfattach() */
3234       {
3235       printf("%s: p2p_attach() failed; error %d\n", NAME_UNIT, error);
3236       config->line_pkg = PKG_RAWIP;  /* still in RAWIP mode */
3237       }
3238     else
3239       {
3240       sc->p2p->p2p_mdmctl = p2p_mdmctl; /* set DTR */
3241       sc->p2p->p2p_getmdm = p2p_getmdm; /* get DCD */
3242       }
3243 #elif GEN_HDLC
3244     int error = 0;
3245     sc->net_dev->mtu = HDLC_MAX_MTU;
3246     if ((error = hdlc_open(sc->net_dev)))
3247       {
3248       printf("%s: hdlc_open() failed; error %d\n", NAME_UNIT, error);
3249       printf("%s: Try 'sethdlc %s ppp'\n", NAME_UNIT, NAME_UNIT);
3250       config->line_pkg = PKG_RAWIP;  /* still in RAWIP mode */
3251       }
3252 #else /* no line protocol stack was configured */
3253     config->line_pkg = PKG_RAWIP;  /* still in RAWIP mode */
3254 #endif
3255     }
3256
3257   /* Bypass line protocol stack and return to RAWIP mode. */
3258   if ((sc->config.line_pkg != PKG_RAWIP) &&
3259          (config->line_pkg == PKG_RAWIP))
3260     {
3261 #if NSPPP
3262     LMC_BPF_DETACH;
3263     sppp_flush(sc->ifp);
3264     sppp_detach(sc->ifp);
3265     setup_ifnet(sc->ifp);
3266     LMC_BPF_ATTACH(DLT_RAW, 0);
3267 #elif P2P
3268     int error = 0;
3269     if_qflush(&sc->p2p->p2p_isnd);
3270     if ((error = p2p_detach(sc->p2p)))
3271       {
3272       printf("%s: p2p_detach() failed; error %d\n",  NAME_UNIT, error);
3273       printf("%s: Try 'ifconfig %s down -remove'\n", NAME_UNIT, NAME_UNIT);
3274       config->line_pkg = PKG_P2P; /* not in RAWIP mode; still attached to P2P */
3275       }
3276     else
3277       {
3278       setup_ifnet(sc->ifp);
3279       LMC_BPF_ATTACH(DLT_RAW, 0);
3280       }
3281 #elif GEN_HDLC
3282     hdlc_proto_detach(sc->hdlc_dev);
3283     hdlc_close(sc->net_dev);
3284     setup_netdev(sc->net_dev);
3285 #endif
3286     }
3287
3288 #if NSPPP
3289
3290   if (config->line_pkg != PKG_RAWIP)
3291     {
3292     /* Check for change to PPP protocol. */
3293     if ((sc->config.line_prot != PROT_PPP) &&
3294            (config->line_prot == PROT_PPP))
3295       {
3296       LMC_BPF_DETACH;
3297       sc->ifp->if_flags  &= ~IFF_LINK2;
3298       sc->sppp->pp_flags &= ~PP_FR;
3299       LMC_BPF_ATTACH(DLT_PPP, 4);
3300       sppp_ioctl(sc->ifp, SIOCSIFFLAGS, NULL);
3301       }
3302
3303 # ifndef DLT_C_HDLC
3304 #  define DLT_C_HDLC DLT_PPP
3305 # endif
3306
3307     /* Check for change to C_HDLC protocol. */
3308     if ((sc->config.line_prot != PROT_C_HDLC) &&
3309            (config->line_prot == PROT_C_HDLC))
3310       {
3311       LMC_BPF_DETACH;
3312       sc->ifp->if_flags  |=  IFF_LINK2;
3313       sc->sppp->pp_flags &= ~PP_FR;
3314       LMC_BPF_ATTACH(DLT_C_HDLC, 4);
3315       sppp_ioctl(sc->ifp, SIOCSIFFLAGS, NULL);
3316       }
3317
3318     /* Check for change to Frame Relay protocol. */
3319     if ((sc->config.line_prot != PROT_FRM_RLY) &&
3320            (config->line_prot == PROT_FRM_RLY))
3321       {
3322       LMC_BPF_DETACH;
3323       sc->ifp->if_flags  &= ~IFF_LINK2;
3324       sc->sppp->pp_flags |= PP_FR;
3325       LMC_BPF_ATTACH(DLT_FRELAY, 4);
3326       sppp_ioctl(sc->ifp, SIOCSIFFLAGS, NULL);
3327       }
3328
3329     /* Check for disabling keep-alives. */
3330     if ((sc->config.keep_alive != 0) &&
3331            (config->keep_alive == 0))
3332       sc->sppp->pp_flags &= ~PP_KEEPALIVE;
3333
3334     /* Check for enabling keep-alives. */
3335     if ((sc->config.keep_alive == 0) &&
3336            (config->keep_alive != 0))
3337       sc->sppp->pp_flags |=  PP_KEEPALIVE;      
3338     }
3339
3340 #endif /* NSPPP */
3341
3342   /* Loop back through the TULIP Ethernet chip; (no CRC). */
3343   /* Data sheet says stop DMA before changing OPMODE register. */
3344   /* But that's not as simple as it sounds; works anyway. */
3345   /* Check for enabling loopback thru Tulip chip. */
3346   if ((sc->config.loop_back != CFG_LOOP_TULIP) &&
3347          (config->loop_back == CFG_LOOP_TULIP))
3348     {
3349     u_int32_t op_mode = READ_CSR(TLP_OP_MODE);
3350     op_mode |= TLP_OP_INT_LOOP;
3351     WRITE_CSR(TLP_OP_MODE, op_mode);
3352     config->crc_len = CFG_CRC_0;
3353     }
3354
3355   /* Check for disabling loopback thru Tulip chip. */
3356   if ((sc->config.loop_back == CFG_LOOP_TULIP) &&
3357          (config->loop_back != CFG_LOOP_TULIP))
3358     {
3359     u_int32_t op_mode = READ_CSR(TLP_OP_MODE);
3360     op_mode &= ~TLP_OP_LOOP_MODE;
3361     WRITE_CSR(TLP_OP_MODE, op_mode);
3362     config->crc_len = CFG_CRC_16;
3363     }
3364   }
3365
3366 /* This is the core ioctl procedure. */
3367 /* It handles IOCTLs from lmcconfig(8). */
3368 /* It must not run when card watchdogs run. */
3369 /* Called from a syscall (user context; no spinlocks). */
3370 /* This procedure can SLEEP. */
3371 static int
3372 core_ioctl(softc_t *sc, u_long cmd, caddr_t data)
3373   {
3374   struct iohdr  *iohdr  = (struct iohdr  *) data;
3375   struct ioctl  *ioctl  = (struct ioctl  *) data;
3376   struct status *status = (struct status *) data;
3377   struct config *config = (struct config *) data;
3378   int error = 0;
3379
3380   /* All structs start with a string and a cookie. */
3381   if (((struct iohdr *)data)->cookie != NGM_LMC_COOKIE)
3382     return EINVAL;
3383
3384   while (TOP_TRYLOCK == 0)
3385     {
3386     sc->status.cntrs.lck_ioctl++;
3387     SLEEP(10000); /* yield? */
3388     }
3389   switch (cmd)
3390     {
3391     case LMCIOCGSTAT:
3392       {
3393       *status = sc->status;
3394       iohdr->cookie = NGM_LMC_COOKIE;
3395       break;
3396       }
3397     case LMCIOCGCFG:
3398       {
3399       *config = sc->config;
3400       iohdr->cookie = NGM_LMC_COOKIE;
3401       break;
3402       }
3403     case LMCIOCSCFG:
3404       {
3405       if ((error = CHECK_CAP)) break;
3406       config_proto(sc, config);
3407       sc->config = *config;
3408       sc->card->config(sc);
3409       break;
3410       }
3411     case LMCIOCREAD:
3412       {
3413       if (ioctl->cmd == IOCTL_RW_PCI)
3414         {
3415         if (ioctl->address > 252) { error = EFAULT; break; }
3416         ioctl->data = READ_PCI_CFG(sc, ioctl->address);
3417         }
3418       else if (ioctl->cmd == IOCTL_RW_CSR)
3419         {
3420         if (ioctl->address > 15) { error = EFAULT; break; }
3421         ioctl->data = READ_CSR(ioctl->address*TLP_CSR_STRIDE);
3422         }
3423       else if (ioctl->cmd == IOCTL_RW_SROM)
3424         {
3425         if (ioctl->address > 63)  { error = EFAULT; break; }
3426         ioctl->data = read_srom(sc, ioctl->address);
3427         }
3428       else if (ioctl->cmd == IOCTL_RW_BIOS)
3429         ioctl->data = read_bios(sc, ioctl->address);
3430       else if (ioctl->cmd == IOCTL_RW_MII)
3431         ioctl->data = read_mii(sc, ioctl->address);
3432       else if (ioctl->cmd == IOCTL_RW_FRAME)
3433         ioctl->data = read_framer(sc, ioctl->address);
3434       else
3435         error = EINVAL;
3436       break;
3437       }
3438     case LMCIOCWRITE:
3439       {
3440       if ((error = CHECK_CAP)) break;
3441       if (ioctl->cmd == IOCTL_RW_PCI)
3442         {
3443         if (ioctl->address > 252) { error = EFAULT; break; }
3444         WRITE_PCI_CFG(sc, ioctl->address, ioctl->data);
3445         }
3446       else if (ioctl->cmd == IOCTL_RW_CSR)
3447         {
3448         if (ioctl->address > 15) { error = EFAULT; break; }
3449         WRITE_CSR(ioctl->address*TLP_CSR_STRIDE, ioctl->data);
3450         }
3451       else if (ioctl->cmd == IOCTL_RW_SROM)
3452         {
3453         if (ioctl->address > 63)  { error = EFAULT; break; }
3454         write_srom(sc, ioctl->address, ioctl->data); /* can sleep */
3455         }
3456       else if (ioctl->cmd == IOCTL_RW_BIOS)
3457         {
3458         if (ioctl->address == 0) erase_bios(sc);
3459         write_bios(sc, ioctl->address, ioctl->data); /* can sleep */
3460         }
3461       else if (ioctl->cmd == IOCTL_RW_MII)
3462         write_mii(sc, ioctl->address, ioctl->data);
3463       else if (ioctl->cmd == IOCTL_RW_FRAME)
3464         write_framer(sc, ioctl->address, ioctl->data);
3465       else if (ioctl->cmd == IOCTL_WO_SYNTH)
3466         write_synth(sc, (struct synth *)&ioctl->data);
3467       else if (ioctl->cmd == IOCTL_WO_DAC)
3468         {
3469         write_dac(sc, 0x9002); /* set Vref = 2.048 volts */
3470         write_dac(sc, ioctl->data & 0xFFF);
3471         }
3472       else
3473         error = EINVAL;
3474       break;
3475       }
3476     case LMCIOCTL:
3477       {
3478       if ((error = CHECK_CAP)) break;
3479       if (ioctl->cmd == IOCTL_XILINX_RESET)
3480         {
3481         reset_xilinx(sc);
3482         sc->card->config(sc);
3483         }
3484       else if (ioctl->cmd == IOCTL_XILINX_ROM)
3485         {
3486         load_xilinx_from_rom(sc); /* can sleep */
3487         sc->card->config(sc);
3488         }
3489       else if (ioctl->cmd == IOCTL_XILINX_FILE)
3490         {
3491         /* load_xilinx_from_file() can sleep. */
3492         error = load_xilinx_from_file(sc, ioctl->ucode, ioctl->data);
3493         if (error != 0) load_xilinx_from_rom(sc); /* try the rom */
3494         sc->card->config(sc);
3495         set_status(sc, (error==0));  /* XXX */
3496         }
3497       else if (ioctl->cmd == IOCTL_RESET_CNTRS)
3498         {
3499         memset(&sc->status.cntrs, 0, sizeof(struct event_cntrs));
3500         microtime(&sc->status.cntrs.reset_time);
3501         }
3502       else
3503         error = sc->card->ioctl(sc, ioctl); /* can sleep */
3504       break;
3505       }
3506     default:
3507       error = EINVAL;
3508       break;
3509     }
3510   TOP_UNLOCK;
3511
3512   return error;
3513   }
3514
3515 /* This is the core watchdog procedure. */
3516 /* It calculates link speed, and calls the card-specific watchdog code. */
3517 /* Calls interrupt() in case one got lost; also kick-starts the device. */
3518 /* ioctl syscalls and card watchdog routines must be interlocked.       */
3519 /* This procedure must not sleep. */
3520 static void
3521 core_watchdog(softc_t *sc)
3522   {
3523   /* Read and restart the Tulip timer. */
3524   u_int32_t tx_speed = READ_CSR(TLP_TIMER);
3525   WRITE_CSR(TLP_TIMER, 0xFFFF);
3526
3527   /* Measure MII clock using a timer in the Tulip chip.
3528    * This timer counts transmitter bits divided by 4096.
3529    * Since this is called once a second the math is easy.
3530    * This is only correct when the link is NOT sending pkts.
3531    * On a fully-loaded link, answer will be HALF actual rate.
3532    * Clock rate during pkt is HALF clk rate between pkts.
3533    * Measuring clock rate really measures link utilization!
3534    */
3535   sc->status.tx_speed = (0xFFFF - (tx_speed & 0xFFFF)) << 12;
3536
3537   /* The first status reset time is when the calendar clock is set. */
3538   if (sc->status.cntrs.reset_time.tv_sec < 1000)
3539     microtime(&sc->status.cntrs.reset_time);
3540
3541   /* Update hardware (operational) status. */
3542   /* Call the card-specific watchdog routines. */
3543   if (TOP_TRYLOCK != 0)
3544     {
3545     sc->status.oper_status = sc->card->watchdog(sc);
3546
3547     /* Increment a counter which tells user-land */
3548     /*  observers that SNMP state has been updated. */
3549     sc->status.ticks++;
3550
3551     TOP_UNLOCK;
3552     }
3553   else
3554     sc->status.cntrs.lck_watch++;
3555
3556   /* In case an interrupt gets lost... */
3557   user_interrupt(sc, 1);
3558   }
3559
3560
3561 /* Called from a syscall (user context; no spinlocks). */
3562 static int
3563 lmc_raw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3564   {
3565   struct ifreq *ifr = (struct ifreq *) data;
3566   int error = 0;
3567
3568   switch (cmd)
3569     {
3570     case SIOCAIFADDR:
3571     case SIOCSIFFLAGS:
3572     case SIOCSIFADDR:
3573       ifp->if_flags |= IFF_UP;  /* a Unix tradition */
3574       break;
3575     case SIOCSIFMTU:
3576       ifp->if_mtu = ifr->ifr_mtu;
3577       break;
3578     default:
3579       error = EINVAL;
3580       break;
3581     }
3582   return error;
3583   }
3584
3585 /* Called from a syscall (user context; no spinlocks). */
3586 static int
3587 lmc_ifnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3588   {
3589   softc_t *sc = IFP2SC(ifp);
3590   int error = 0;
3591
3592   switch (cmd)
3593     {
3594     /* Catch the IOCTLs used by lmcconfig. */
3595     case LMCIOCGSTAT:
3596     case LMCIOCGCFG:
3597     case LMCIOCSCFG:
3598     case LMCIOCREAD:
3599     case LMCIOCWRITE:
3600     case LMCIOCTL:
3601       error = core_ioctl(sc, cmd, data);
3602       break;
3603     /* Pass the rest to the line protocol. */
3604     default:
3605       if (sc->config.line_pkg == PKG_RAWIP)
3606         error =  lmc_raw_ioctl(ifp, cmd, data);
3607       else
3608 # if NSPPP
3609         error = sppp_ioctl(ifp, cmd, data);
3610 # elif P2P
3611         error =  p2p_ioctl(ifp, cmd, data);
3612 # else
3613         error = EINVAL;
3614 # endif
3615       break;
3616     }
3617
3618   if (DRIVER_DEBUG && (error!=0))
3619     printf("%s: lmc_ifnet_ioctl; cmd=0x%08lx error=%d\n",
3620      NAME_UNIT, cmd, error);
3621
3622   return error;
3623   }
3624
3625 /* Called from a syscall (user context; no spinlocks). */
3626 static void
3627 lmc_ifnet_start(struct ifnet *ifp)
3628   {
3629   softc_t *sc = IFP2SC(ifp);
3630
3631   /* Start the transmitter; incoming pkts are NOT processed. */
3632   user_interrupt(sc, 0);
3633   }
3634
3635 /* sppp and p2p replace this with their own proc. */
3636 /* RAWIP mode is the only time this is used. */
3637 /* Called from a syscall (user context; no spinlocks). */
3638 static int
3639 lmc_raw_output(struct ifnet *ifp, struct mbuf *m,
3640  const struct sockaddr *dst, struct route *ro)
3641   {
3642   softc_t *sc = IFP2SC(ifp);
3643   int error = 0;
3644
3645   /* Fail if the link is down. */
3646   if (sc->status.oper_status != STATUS_UP)
3647     {
3648     m_freem(m);
3649     sc->status.cntrs.odiscards++;
3650     if (DRIVER_DEBUG)
3651       printf("%s: lmc_raw_output: tx pkt discarded: link down\n", NAME_UNIT);
3652     return ENETDOWN;
3653     }
3654
3655 # if NETGRAPH
3656   /* Netgraph has priority over the ifnet kernel interface. */
3657   if (sc->ng_hook != NULL)
3658     {
3659     m_freem(m);
3660     sc->status.cntrs.odiscards++;
3661     if (DRIVER_DEBUG)
3662       printf("%s: lmc_raw_output: tx pkt discarded: netgraph active\n",
3663         NAME_UNIT);
3664     return EBUSY;
3665     }
3666 # endif
3667
3668   /* lmc_raw_output() ENQUEUEs in a syscall or softirq. */
3669   /* txintr_setup() DEQUEUEs in a hard interrupt. */
3670   /* Some BSD QUEUE routines are not interrupt-safe. */
3671   {
3672   DISABLE_INTR;
3673   IFQ_ENQUEUE(&ifp->if_snd, m, error);
3674   ENABLE_INTR;
3675   }
3676
3677   if (error==0)
3678     user_interrupt(sc, 0); /* start the transmitter */
3679   else
3680     {
3681     m_freem(m);
3682     sc->status.cntrs.odiscards++;
3683     if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3684     if (DRIVER_DEBUG)
3685       printf("%s: lmc_raw_output: IFQ_ENQUEUE() failed; error %d\n",
3686        NAME_UNIT, error);
3687     }
3688
3689   return error;
3690   }
3691
3692 /* Called from a softirq once a second. */
3693 static void
3694 lmc_watchdog(void *arg)
3695 {
3696   struct ifnet *ifp = arg;
3697   softc_t *sc = IFP2SC(ifp);
3698   u_int8_t old_oper_status = sc->status.oper_status;
3699
3700   core_watchdog(sc); /* updates oper_status */
3701
3702 #if NETGRAPH
3703   if (sc->ng_hook != NULL)
3704     {
3705     sc->status.line_pkg  = PKG_NG;
3706     sc->status.line_prot = 0;
3707     }
3708   else
3709 #endif
3710   if (sc->config.line_pkg == PKG_RAWIP)
3711     {
3712     sc->status.line_pkg  = PKG_RAWIP;
3713     sc->status.line_prot = PROT_IP_HDLC;
3714     }
3715   else
3716     {
3717 # if P2P
3718     /* Notice change in link status. */
3719     if ((old_oper_status != sc->status.oper_status) && (sc->p2p->p2p_modem))
3720       (*sc->p2p->p2p_modem)(sc->p2p, sc->status.oper_status==STATUS_UP);
3721
3722     /* Notice change in line protocol. */
3723     sc->status.line_pkg = PKG_P2P;
3724     switch (sc->ifp->if_type)
3725       {
3726       case IFT_PPP:
3727         sc->status.line_prot = PROT_PPP;
3728         break;
3729       case IFT_PTPSERIAL:
3730         sc->status.line_prot = PROT_C_HDLC;
3731         break;
3732       case IFT_FRELAY:
3733         sc->status.line_prot = PROT_FRM_RLY;
3734         break;
3735       default:
3736         sc->status.line_prot = 0;
3737         break;
3738       }
3739
3740 # elif NSPPP
3741     /* Notice change in link status. */
3742     if     ((old_oper_status != STATUS_UP) &&
3743      (sc->status.oper_status == STATUS_UP))  /* link came up */
3744       sppp_tls(sc->sppp);
3745     if     ((old_oper_status == STATUS_UP) &&
3746      (sc->status.oper_status != STATUS_UP))  /* link went down */
3747       sppp_tlf(sc->sppp);
3748
3749     /* Notice change in line protocol. */
3750     sc->status.line_pkg = PKG_SPPP;
3751     if (sc->sppp->pp_flags & PP_FR)
3752       sc->status.line_prot = PROT_FRM_RLY;
3753     else if (sc->ifp->if_flags  & IFF_LINK2)
3754       sc->status.line_prot = PROT_C_HDLC;
3755     else
3756       sc->status.line_prot = PROT_PPP;
3757
3758 # else
3759     /* Suppress compiler warning. */
3760     if (old_oper_status == STATUS_UP);
3761 # endif
3762     }
3763
3764   ifp->if_baudrate = sc->status.tx_speed;
3765   if (sc->status.oper_status == STATUS_UP)
3766     ifp->if_link_state = LINK_STATE_UP;
3767   else
3768     ifp->if_link_state = LINK_STATE_DOWN;
3769
3770   /* Call this procedure again after one second. */
3771   callout_reset(&sc->callout, hz, lmc_watchdog, ifp);
3772 }
3773
3774 static uint64_t
3775 lmc_get_counter(struct ifnet *ifp, ift_counter cnt)
3776 {
3777         softc_t *sc;
3778         struct event_cntrs *cntrs;
3779
3780         sc = if_getsoftc(ifp);
3781         cntrs = &sc->status.cntrs;
3782
3783         switch (cnt) {
3784         case IFCOUNTER_IPACKETS:
3785                 return (cntrs->ipackets);
3786         case IFCOUNTER_OPACKETS:
3787                 return (cntrs->opackets);
3788         case IFCOUNTER_IBYTES:
3789                 return (cntrs->ibytes);
3790         case IFCOUNTER_OBYTES:
3791                 return (cntrs->obytes);
3792         case IFCOUNTER_IERRORS:
3793                 return (cntrs->ierrors);
3794         case IFCOUNTER_OERRORS:
3795                 return (cntrs->oerrors);
3796         case IFCOUNTER_IQDROPS:
3797                 return (cntrs->idiscards);
3798         default:
3799                 return (if_get_counter_default(ifp, cnt));
3800         }
3801 }
3802
3803 static void
3804 setup_ifnet(struct ifnet *ifp)
3805   {
3806   softc_t *sc = ifp->if_softc;
3807
3808   /* Initialize the generic network interface. */
3809   ifp->if_flags    = IFF_POINTOPOINT;
3810   ifp->if_flags   |= IFF_RUNNING;
3811   ifp->if_ioctl    = lmc_ifnet_ioctl;
3812   ifp->if_start    = lmc_ifnet_start;   /* sppp changes this */
3813   ifp->if_output   = lmc_raw_output;    /* sppp & p2p change this */
3814   ifp->if_input    = lmc_raw_input;
3815   ifp->if_get_counter = lmc_get_counter;
3816   ifp->if_mtu      = MAX_DESC_LEN;      /* sppp & p2p change this */
3817   ifp->if_type     = IFT_PTPSERIAL;     /* p2p changes this */
3818
3819 # if defined(DEVICE_POLLING)
3820   ifp->if_capabilities |= IFCAP_POLLING;
3821   ifp->if_capenable    |= IFCAP_POLLING_NOCOUNT;
3822 # endif
3823
3824   if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
3825   }
3826
3827 static int
3828 lmc_ifnet_attach(softc_t *sc)
3829   {
3830   sc->ifp  = if_alloc(NSPPP ? IFT_PPP : IFT_OTHER);
3831   if (sc->ifp == NULL) return ENOMEM;
3832 # if NSPPP
3833   sc->sppp = sc->ifp->if_l2com;
3834 # elif P2P
3835   sc->ifp  = &sc->p2pcom.p2p_if;
3836   sc->p2p  = &sc->p2pcom;
3837 # endif
3838
3839   /* Initialize the network interface struct. */
3840   sc->ifp->if_softc = sc;
3841   setup_ifnet(sc->ifp);
3842
3843   /* ALTQ output queue initialization. */
3844   IFQ_SET_MAXLEN(&sc->ifp->if_snd, SNDQ_MAXLEN);
3845   IFQ_SET_READY(&sc->ifp->if_snd);
3846
3847   /* Attach to the ifnet kernel interface. */
3848   if_attach(sc->ifp);
3849
3850   /* Attach Berkeley Packet Filter. */
3851   LMC_BPF_ATTACH(DLT_RAW, 0);
3852
3853   callout_reset(&sc->callout, hz, lmc_watchdog, sc);
3854
3855   return 0;
3856   }
3857
3858 static void
3859 lmc_ifnet_detach(softc_t *sc)
3860   {
3861
3862 # if defined(DEVICE_POLLING)
3863   if (sc->ifp->if_capenable & IFCAP_POLLING)
3864     ether_poll_deregister(sc->ifp);
3865 # endif
3866
3867   /* Detach Berkeley Packet Filter. */
3868   LMC_BPF_DETACH;
3869
3870   /* Detach from the ifnet kernel interface. */
3871   if_detach(sc->ifp);
3872
3873   if_free(sc->ifp);
3874   }
3875
3876
3877 #if NETGRAPH
3878
3879 /* These next two macros should be added to netgraph */
3880 #  define NG_TYPE_REF(type) atomic_add_int(&(type)->refs, 1)
3881 #  define NG_TYPE_UNREF(type)   \
3882 do {                            \
3883   if ((type)->refs == 1)        \
3884     ng_rmtype(type);            \
3885   else                          \
3886     atomic_subtract_int(&(type)->refs, 1); \
3887    } while (0)
3888
3889 /* It is an error to construct new copies of this Netgraph node. */
3890 /* All instances are constructed by ng_attach and are persistent. */
3891 static int ng_constructor(node_p  node) { return EINVAL; }
3892
3893 /* Incoming Netgraph control message. */
3894 static int
3895 ng_rcvmsg(node_p node, item_p item, hook_p lasthook)
3896   {
3897   struct ng_mesg *msg;
3898   struct ng_mesg *resp = NULL;
3899   softc_t *sc = NG_NODE_PRIVATE(node);
3900   int error = 0;
3901
3902   NGI_GET_MSG(item, msg);
3903   if (msg->header.typecookie == NGM_LMC_COOKIE)
3904     {
3905     switch (msg->header.cmd)
3906       {
3907       case LMCIOCGSTAT:
3908       case LMCIOCGCFG:
3909       case LMCIOCSCFG:
3910       case LMCIOCREAD:
3911       case LMCIOCWRITE:
3912       case LMCIOCTL:
3913         {
3914         /* Call the core ioctl procedure. */
3915         error = core_ioctl(sc, msg->header.cmd, msg->data);
3916         if ((msg->header.cmd & IOC_OUT) != 0)
3917           { /* synchronous response */
3918           NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg) +
3919            IOCPARM_LEN(msg->header.cmd), M_NOWAIT);
3920           if (resp == NULL)
3921             error = ENOMEM;
3922           else
3923             memcpy(resp->data, msg->data, IOCPARM_LEN(msg->header.cmd));
3924           }
3925         break;
3926         }
3927       default:
3928         error = EINVAL;
3929         break;
3930       }
3931     }
3932   else if ((msg->header.typecookie == NGM_GENERIC_COOKIE) &&
3933            (msg->header.cmd == NGM_TEXT_STATUS))
3934     {  /* synchronous response */
3935     NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg) +
3936      NG_TEXTRESPONSE, M_NOWAIT);
3937     if (resp == NULL)
3938       error = ENOMEM;
3939     else
3940       {
3941       char *s = resp->data;
3942       sprintf(s, "Card type = <%s>\n"
3943        "This driver considers the link to be %s.\n"
3944        "Use lmcconfig to configure this interface.\n",
3945        sc->dev_desc, (sc->status.oper_status==STATUS_UP) ? "UP" : "DOWN");
3946       resp->header.arglen = strlen(s) +1;
3947       }
3948     }
3949   else
3950 /* Netgraph should be able to read and write these
3951  *  parameters with text-format control messages:
3952  *  SSI      HSSI     T1E1     T3
3953  *  crc      crc      crc      crc      
3954  *  loop     loop     loop     loop
3955  *           clksrc   clksrc
3956  *  dte      dte      format   format
3957  *  synth    synth    cablen   cablen
3958  *  cable             timeslot scram
3959  *                    gain
3960  *                    pulse
3961  *                    lbo
3962  * Someday I'll implement this...
3963  */
3964     error = EINVAL;
3965
3966   /* Handle synchronous response. */
3967   NG_RESPOND_MSG(error, node, item, resp);
3968   NG_FREE_MSG(msg);
3969
3970   return error;
3971   }
3972
3973 /* This is a persistent netgraph node. */
3974 static int
3975 ng_shutdown(node_p node)
3976   {
3977   /* unless told to really die, bounce back to life */
3978   if ((node->nd_flags & NG_REALLY_DIE)==0)
3979     node->nd_flags &= ~NG_INVALID; /* bounce back to life */
3980
3981   return 0;
3982   }
3983
3984 /* ng_disconnect is the opposite of this procedure. */
3985 static int
3986 ng_newhook(node_p node, hook_p hook, const char *name)
3987   {
3988   softc_t *sc = NG_NODE_PRIVATE(node);
3989
3990   /* Hook name must be 'rawdata'. */
3991   if (strncmp(name, "rawdata", 7) != 0) return EINVAL;
3992
3993   /* Is our hook connected? */
3994   if (sc->ng_hook != NULL) return EBUSY;
3995
3996   /* Accept the hook. */
3997   sc->ng_hook = hook;
3998
3999   return 0;
4000   }
4001
4002 /* Both ends have accepted their hooks and the links have been made. */
4003 /* This is the last chance to reject the connection request. */
4004 static int
4005 ng_connect(hook_p hook)
4006   {
4007   /* Probably not at splnet, force outward queueing. (huh?) */
4008   NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
4009   return 0; /* always accept */
4010   }
4011
4012 /* Receive data in mbufs from another Netgraph node. */
4013 /* Transmit an mbuf-chain on the communication link. */
4014 /* This procedure is very similar to lmc_raw_output(). */
4015 /* Called from a syscall (user context; no spinlocks). */
4016 static int
4017 ng_rcvdata(hook_p hook, item_p item)
4018   {
4019   softc_t *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
4020   int error = 0;
4021   struct mbuf *m;
4022   meta_p meta = NULL;
4023
4024   NGI_GET_M(item, m);
4025   NGI_GET_META(item, meta);
4026   NG_FREE_ITEM(item);
4027
4028   /* This macro must not store into meta! */
4029   NG_FREE_META(meta);
4030
4031   /* Fail if the link is down. */
4032   if (sc->status.oper_status  != STATUS_UP)
4033     {
4034     m_freem(m);
4035     sc->status.cntrs.odiscards++;
4036     if (DRIVER_DEBUG)
4037       printf("%s: ng_rcvdata: tx pkt discarded: link down\n", NAME_UNIT);
4038     return ENETDOWN;
4039     }
4040
4041   /* ng_rcvdata() ENQUEUEs in a syscall or softirq. */
4042   /* txintr_setup() DEQUEUEs in a hard interrupt. */
4043   /* Some BSD QUEUE routines are not interrupt-safe. */
4044   {
4045   DISABLE_INTR;
4046   if (meta==NULL)
4047     IFQ_ENQUEUE(&sc->ng_sndq, m, error);
4048   else
4049     IFQ_ENQUEUE(&sc->ng_fastq, m, error);
4050   ENABLE_INTR;
4051   }
4052
4053   if (error==0)
4054     user_interrupt(sc, 0); /* start the transmitter */
4055   else
4056     {
4057     m_freem(m);
4058     sc->status.cntrs.odiscards++;
4059     if (DRIVER_DEBUG)
4060       printf("%s: ng_rcvdata: IFQ_ENQUEUE() failed; error %d\n",
4061        NAME_UNIT, error);
4062     }
4063
4064   return error;
4065   }
4066
4067 /* ng_newhook is the opposite of this procedure, not */
4068 /*  ng_connect, as you might expect from the names. */
4069 static int
4070 ng_disconnect(hook_p hook)
4071   {
4072   softc_t *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
4073
4074   /* Disconnect the hook. */
4075   sc->ng_hook = NULL;
4076
4077   return 0;
4078   }
4079
4080 static
4081 struct ng_type ng_type =
4082   {
4083   .version      = NG_ABI_VERSION,
4084   .name         = NG_LMC_NODE_TYPE,
4085   .mod_event    = NULL,
4086   .constructor  = ng_constructor,
4087   .rcvmsg       = ng_rcvmsg,
4088   .close        = NULL,
4089   .shutdown     = ng_shutdown,
4090   .newhook      = ng_newhook,
4091   .findhook     = NULL,
4092   .connect      = ng_connect,
4093   .rcvdata      = ng_rcvdata,
4094   .disconnect   = ng_disconnect,
4095   };
4096
4097
4098 /* Attach to the Netgraph kernel interface (/sys/netgraph).
4099  * It is called once for each physical card during device attach.
4100  * This is effectively ng_constructor.
4101  */
4102 static int
4103 ng_attach(softc_t *sc)
4104   {
4105   int error;
4106
4107   /* If this node type is not known to Netgraph then register it. */
4108   if (ng_type.refs == 0) /* or: if (ng_findtype(&ng_type) == NULL) */
4109     {
4110     if ((error = ng_newtype(&ng_type)))
4111       {
4112       printf("%s: ng_newtype() failed; error %d\n", NAME_UNIT, error);
4113       return error;
4114       }
4115     }
4116   else
4117     NG_TYPE_REF(&ng_type);
4118
4119   /* Call the superclass node constructor. */
4120   if ((error = ng_make_node_common(&ng_type, &sc->ng_node)))
4121     {
4122     NG_TYPE_UNREF(&ng_type);
4123     printf("%s: ng_make_node_common() failed; error %d\n", NAME_UNIT, error);
4124     return error;
4125     }
4126
4127   /* Associate a name with this netgraph node. */
4128   if ((error = ng_name_node(sc->ng_node, NAME_UNIT)))
4129     {
4130     NG_NODE_UNREF(sc->ng_node);
4131     NG_TYPE_UNREF(&ng_type);
4132     printf("%s: ng_name_node() failed; error %d\n", NAME_UNIT, error);
4133     return error;
4134     }
4135
4136   /* Initialize the send queue mutexes. */
4137   mtx_init(&sc->ng_sndq.ifq_mtx,  NAME_UNIT, "sndq",  MTX_DEF);
4138   mtx_init(&sc->ng_fastq.ifq_mtx, NAME_UNIT, "fastq", MTX_DEF);
4139
4140   /* Put a backpointer to the softc in the netgraph node. */
4141   NG_NODE_SET_PRIVATE(sc->ng_node, sc);
4142
4143   /* ALTQ output queue initialization. */
4144   IFQ_SET_MAXLEN(&sc->ng_fastq, SNDQ_MAXLEN);
4145   IFQ_SET_READY(&sc->ng_fastq);
4146   IFQ_SET_MAXLEN(&sc->ng_sndq,  SNDQ_MAXLEN);
4147   IFQ_SET_READY(&sc->ng_sndq);
4148
4149
4150   return 0;
4151   }
4152
4153 static void
4154 ng_detach(softc_t *sc)
4155   {
4156   callout_drain(&sc->callout);
4157   mtx_destroy(&sc->ng_sndq.ifq_mtx);
4158   mtx_destroy(&sc->ng_fastq.ifq_mtx);
4159   ng_rmnode_self(sc->ng_node); /* free hook */
4160   NG_NODE_UNREF(sc->ng_node);  /* free node */
4161   NG_TYPE_UNREF(&ng_type);
4162   }
4163
4164 #endif /* NETGRAPH */
4165
4166 /* The next few procedures initialize the card. */
4167
4168 /* Returns 0 on success; error code on failure. */
4169 static int
4170 startup_card(softc_t *sc)
4171   {
4172   int num_rx_descs, error = 0;
4173   u_int32_t tlp_bus_pbl, tlp_bus_cal, tlp_op_tr;
4174   u_int32_t tlp_cfdd, tlp_cfcs;
4175   u_int32_t tlp_cflt, tlp_csid, tlp_cfit;
4176
4177   /* Make sure the COMMAND bits are reasonable. */
4178   tlp_cfcs = READ_PCI_CFG(sc, TLP_CFCS);
4179   tlp_cfcs &= ~TLP_CFCS_MWI_ENABLE;
4180   tlp_cfcs |=  TLP_CFCS_BUS_MASTER;
4181   tlp_cfcs |=  TLP_CFCS_MEM_ENABLE;
4182   tlp_cfcs |=  TLP_CFCS_IO_ENABLE;
4183   tlp_cfcs |=  TLP_CFCS_PAR_ERROR;
4184   tlp_cfcs |=  TLP_CFCS_SYS_ERROR;
4185   WRITE_PCI_CFG(sc, TLP_CFCS, tlp_cfcs);
4186
4187   /* Set the LATENCY TIMER to the recommended value, */
4188   /*  and make sure the CACHE LINE SIZE is reasonable. */
4189   tlp_cfit = READ_PCI_CFG(sc, TLP_CFIT);
4190   tlp_cflt = READ_PCI_CFG(sc, TLP_CFLT);
4191   tlp_cflt &= ~TLP_CFLT_LATENCY;
4192   tlp_cflt |= (tlp_cfit & TLP_CFIT_MAX_LAT)>>16;
4193   /* "prgmbl burst length" and "cache alignment" used below. */
4194   switch(tlp_cflt & TLP_CFLT_CACHE)
4195     {
4196     case 8: /* 8 bytes per cache line */
4197       { tlp_bus_pbl = 32; tlp_bus_cal = 1; break; }
4198     case 16:
4199       { tlp_bus_pbl = 32; tlp_bus_cal = 2; break; }
4200     case 32:
4201       { tlp_bus_pbl = 32; tlp_bus_cal = 3; break; }
4202     default:
4203       {
4204       tlp_bus_pbl = 32; tlp_bus_cal = 1;
4205       tlp_cflt &= ~TLP_CFLT_CACHE;
4206       tlp_cflt |= 8;
4207       break;
4208       }
4209     }
4210   WRITE_PCI_CFG(sc, TLP_CFLT, tlp_cflt);
4211
4212   /* Make sure SNOOZE and SLEEP modes are disabled. */
4213   tlp_cfdd = READ_PCI_CFG(sc, TLP_CFDD);
4214   tlp_cfdd &= ~TLP_CFDD_SLEEP;
4215   tlp_cfdd &= ~TLP_CFDD_SNOOZE;
4216   WRITE_PCI_CFG(sc, TLP_CFDD, tlp_cfdd);
4217   DELAY(11*1000); /* Tulip wakes up in 10 ms max */
4218
4219   /* Software Reset the Tulip chip; stops DMA and Interrupts. */
4220   /* This does not change the PCI config regs just set above. */
4221   WRITE_CSR(TLP_BUS_MODE, TLP_BUS_RESET); /* self-clearing */
4222   DELAY(5);  /* Tulip is dead for 50 PCI cycles after reset. */
4223
4224   /* Reset the Xilinx Field Programmable Gate Array. */
4225   reset_xilinx(sc); /* side effect: turns on all four LEDs */
4226
4227   /* Configure card-specific stuff (framers, line interfaces, etc.). */
4228   sc->card->config(sc);
4229
4230   /* Initializing cards can glitch clocks and upset fifos. */
4231   /* Reset the FIFOs between the Tulip and Xilinx chips. */
4232   set_mii16_bits(sc, MII16_FIFO);
4233   clr_mii16_bits(sc, MII16_FIFO);
4234
4235   /* Initialize the PCI busmode register. */
4236   /* The PCI bus cycle type "Memory Write and Invalidate" does NOT */
4237   /*  work cleanly in any version of the 21140A, so don't enable it! */
4238   WRITE_CSR(TLP_BUS_MODE,
4239         (tlp_bus_cal ? TLP_BUS_READ_LINE : 0) |
4240         (tlp_bus_cal ? TLP_BUS_READ_MULT : 0) |
4241         (tlp_bus_pbl<<TLP_BUS_PBL_SHIFT) |
4242         (tlp_bus_cal<<TLP_BUS_CAL_SHIFT) |
4243    ((BYTE_ORDER == BIG_ENDIAN) ? TLP_BUS_DESC_BIGEND : 0) |
4244    ((BYTE_ORDER == BIG_ENDIAN) ? TLP_BUS_DATA_BIGEND : 0) |
4245                 TLP_BUS_DSL_VAL |
4246                 TLP_BUS_ARB);
4247
4248   /* Pick number of RX descriptors and TX fifo threshold. */
4249   /* tx_threshold in bytes: 0=128, 1=256, 2=512, 3=1024 */
4250   tlp_csid = READ_PCI_CFG(sc, TLP_CSID);
4251   switch(tlp_csid)
4252     {
4253     case TLP_CSID_HSSI:         /* 52 Mb/s */
4254     case TLP_CSID_HSSIc:        /* 52 Mb/s */
4255     case TLP_CSID_T3:           /* 45 Mb/s */
4256       { num_rx_descs = 48; tlp_op_tr = 2; break; }
4257     case TLP_CSID_SSI:          /* 10 Mb/s */
4258       { num_rx_descs = 32; tlp_op_tr = 1; break; }
4259     case TLP_CSID_T1E1:         /*  2 Mb/s */
4260       { num_rx_descs = 16; tlp_op_tr = 0; break; }
4261     default:
4262       { num_rx_descs = 16; tlp_op_tr = 0; break; }
4263     }
4264
4265   /* Create DMA descriptors and initialize list head registers. */
4266   if ((error = create_ring(sc, &sc->txring, NUM_TX_DESCS))) return error;
4267   WRITE_CSR(TLP_TX_LIST, sc->txring.dma_addr);
4268   if ((error = create_ring(sc, &sc->rxring, num_rx_descs))) return error;
4269   WRITE_CSR(TLP_RX_LIST, sc->rxring.dma_addr);
4270
4271   /* Initialize the operating mode register. */
4272   WRITE_CSR(TLP_OP_MODE, TLP_OP_INIT | (tlp_op_tr<<TLP_OP_TR_SHIFT));
4273
4274   /* Read the missed frame register (result ignored) to zero it. */
4275   error = READ_CSR( TLP_MISSED); /* error is used as a bit-dump */
4276
4277   /* Disable rx watchdog and tx jabber features. */
4278   WRITE_CSR(TLP_WDOG, TLP_WDOG_INIT);
4279
4280   /* Enable card interrupts. */
4281   WRITE_CSR(TLP_INT_ENBL, TLP_INT_TXRX);
4282
4283   return 0;
4284   }
4285
4286 /* Stop DMA and Interrupts; free descriptors and buffers. */
4287 static void
4288 shutdown_card(void *arg)
4289   {
4290   softc_t *sc = arg;
4291
4292   /* Leave the LEDs in the state they were in after power-on. */
4293   led_on(sc, MII16_LED_ALL);
4294
4295   /* Software reset the Tulip chip; stops DMA and Interrupts */
4296   WRITE_CSR(TLP_BUS_MODE, TLP_BUS_RESET); /* self-clearing */
4297   DELAY(5);  /* Tulip is dead for 50 PCI cycles after reset. */
4298
4299   /* Disconnect from the PCI bus except for config cycles. */
4300   /* Hmmm; Linux syslogs a warning that IO and MEM are disabled. */
4301   WRITE_PCI_CFG(sc, TLP_CFCS, TLP_CFCS_MEM_ENABLE | TLP_CFCS_IO_ENABLE);
4302
4303   /* Free the DMA descriptor rings. */
4304   destroy_ring(sc, &sc->txring);
4305   destroy_ring(sc, &sc->rxring);
4306   }
4307
4308 /* Start the card and attach a kernel interface and line protocol. */
4309 static int
4310 attach_card(softc_t *sc, const char *intrstr)
4311   {
4312   struct config config;
4313   u_int32_t tlp_cfrv;
4314   u_int16_t mii3;
4315   u_int8_t *ieee;
4316   int i, error = 0;
4317
4318   /* Start the card. */
4319   if ((error = startup_card(sc))) return error;
4320
4321   callout_init(&sc->callout, 0);
4322
4323   /* Attach a kernel interface. */
4324 #if NETGRAPH
4325   if ((error = ng_attach(sc))) return error;
4326   sc->flags |= FLAG_NETGRAPH;
4327 #endif
4328   if ((error = lmc_ifnet_attach(sc))) return error;
4329   sc->flags |= FLAG_IFNET;
4330
4331   /* Attach a line protocol stack. */
4332   sc->config.line_pkg = PKG_RAWIP;
4333   config = sc->config;  /* get current config */
4334   config.line_pkg = 0;  /* select external stack */
4335   config.line_prot = PROT_C_HDLC;
4336   config.keep_alive = 1;
4337   config_proto(sc, &config); /* reconfigure */
4338   sc->config = config;  /* save new configuration */
4339
4340   /* Print interesting hardware-related things. */
4341   mii3 = read_mii(sc, 3);
4342   tlp_cfrv = READ_PCI_CFG(sc, TLP_CFRV);
4343   printf("%s: PCI rev %d.%d, MII rev %d.%d", NAME_UNIT,
4344    (tlp_cfrv>>4) & 0xF, tlp_cfrv & 0xF, (mii3>>4) & 0xF, mii3 & 0xF);
4345   ieee = (u_int8_t *)sc->status.ieee;
4346   for (i=0; i<3; i++) sc->status.ieee[i] = read_srom(sc, 10+i);
4347   printf(", IEEE addr %02x:%02x:%02x:%02x:%02x:%02x",
4348    ieee[0], ieee[1], ieee[2], ieee[3], ieee[4], ieee[5]);
4349   sc->card->ident(sc);
4350   printf(" %s\n", intrstr);
4351
4352   /* Print interesting software-related things. */
4353   printf("%s: Driver rev %d.%d.%d", NAME_UNIT,
4354    DRIVER_MAJOR_VERSION, DRIVER_MINOR_VERSION, DRIVER_SUB_VERSION);
4355   printf(", Options %s%s%s%s%s%s%s%s%s\n",
4356    NETGRAPH ? "NETGRAPH " : "", GEN_HDLC ? "GEN_HDLC " : "",
4357    NSPPP ? "SPPP " : "", P2P ? "P2P " : "",
4358    ALTQ_PRESENT ? "ALTQ " : "", NBPFILTER ? "BPF " : "",
4359    DEV_POLL ? "POLL " : "", IOREF_CSR ? "IO_CSR " : "MEM_CSR ",
4360    (BYTE_ORDER == BIG_ENDIAN) ? "BIG_END " : "LITTLE_END ");
4361
4362   /* Make the local hardware ready. */
4363   set_status(sc, 1);
4364
4365   return 0;
4366   }
4367
4368 /* Detach from the kernel in all ways. */
4369 static void
4370 detach_card(softc_t *sc)
4371   {
4372   struct config config;
4373
4374   /* Make the local hardware NOT ready. */
4375   set_status(sc, 0);
4376
4377   /* Detach external line protocol stack. */
4378   if (sc->config.line_pkg != PKG_RAWIP)
4379     {
4380     config = sc->config;
4381     config.line_pkg = PKG_RAWIP;
4382     config_proto(sc, &config);
4383     sc->config = config;
4384     }
4385
4386   /* Detach kernel interfaces. */
4387 #if NETGRAPH
4388   if (sc->flags & FLAG_NETGRAPH)
4389     {
4390     IFQ_PURGE(&sc->ng_fastq);
4391     IFQ_PURGE(&sc->ng_sndq);
4392     ng_detach(sc);
4393     sc->flags &= ~FLAG_NETGRAPH;
4394     }
4395 #endif
4396   if (sc->flags & FLAG_IFNET)
4397     {
4398     IFQ_PURGE(&sc->ifp->if_snd);
4399     lmc_ifnet_detach(sc);
4400     sc->flags &= ~FLAG_IFNET;
4401     }
4402
4403   /* Reset the Tulip chip; stops DMA and Interrupts. */
4404   shutdown_card(sc);
4405   }
4406
4407 /* This is the I/O configuration interface for FreeBSD */
4408
4409
4410 static int
4411 fbsd_probe(device_t dev)
4412   {
4413   u_int32_t cfid = pci_read_config(dev, TLP_CFID, 4);
4414   u_int32_t csid = pci_read_config(dev, TLP_CSID, 4);
4415
4416   /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
4417   if (cfid != TLP_CFID_TULIP) return ENXIO;
4418   switch (csid)
4419     {
4420     case TLP_CSID_HSSI:
4421     case TLP_CSID_HSSIc:
4422       device_set_desc(dev, HSSI_DESC);
4423       break;
4424     case TLP_CSID_T3:
4425       device_set_desc(dev,   T3_DESC);
4426       break;
4427     case TLP_CSID_SSI:
4428       device_set_desc(dev,  SSI_DESC);
4429       break;
4430     case TLP_CSID_T1E1:
4431       device_set_desc(dev, T1E1_DESC);
4432       break;
4433     default:
4434       return ENXIO;
4435     }
4436   return 0;
4437   }
4438
4439 static int
4440 fbsd_detach(device_t dev)
4441   {
4442   softc_t *sc = device_get_softc(dev);
4443
4444   /* Stop the card and detach from the kernel. */
4445   detach_card(sc);
4446
4447   /* Release resources. */
4448   if (sc->irq_cookie != NULL)
4449     {
4450     bus_teardown_intr(dev, sc->irq_res, sc->irq_cookie);
4451     sc->irq_cookie = NULL;
4452     }
4453   if (sc->irq_res != NULL)
4454     {
4455     bus_release_resource(dev, SYS_RES_IRQ, sc->irq_res_id, sc->irq_res);
4456     sc->irq_res = NULL;
4457     }
4458   if (sc->csr_res != NULL)
4459     {
4460     bus_release_resource(dev, sc->csr_res_type, sc->csr_res_id, sc->csr_res);
4461     sc->csr_res = NULL;
4462     }
4463
4464   mtx_destroy(&sc->top_mtx);
4465   mtx_destroy(&sc->bottom_mtx);
4466   return 0; /* no error */
4467   }
4468
4469 static int
4470 fbsd_shutdown(device_t dev)
4471   {
4472   shutdown_card(device_get_softc(dev));
4473   return 0;
4474   }
4475
4476 static int
4477 fbsd_attach(device_t dev)
4478   {
4479   softc_t *sc = device_get_softc(dev);
4480   int error;
4481
4482   /* READ/WRITE_PCI_CFG need this. */
4483   sc->dev = dev;
4484
4485   /* What kind of card are we driving? */
4486   switch (READ_PCI_CFG(sc, TLP_CSID))
4487     {
4488     case TLP_CSID_HSSI:
4489     case TLP_CSID_HSSIc:
4490       sc->card = &hssi_card;
4491       break;
4492     case TLP_CSID_T3:
4493       sc->card =   &t3_card;
4494       break;
4495     case TLP_CSID_SSI:
4496       sc->card =  &ssi_card;
4497       break;
4498     case TLP_CSID_T1E1:
4499       sc->card =   &t1_card;
4500       break;
4501     default:
4502       return ENXIO;
4503     }
4504   sc->dev_desc = device_get_desc(dev);
4505
4506   /* Allocate PCI memory or IO resources to access the Tulip chip CSRs. */
4507 # if IOREF_CSR
4508   sc->csr_res_id   = TLP_CBIO;
4509   sc->csr_res_type = SYS_RES_IOPORT;
4510 # else
4511   sc->csr_res_id   = TLP_CBMA;
4512   sc->csr_res_type = SYS_RES_MEMORY;
4513 # endif
4514   sc->csr_res = bus_alloc_resource(dev, sc->csr_res_type, &sc->csr_res_id,
4515    0, ~0, 1, RF_ACTIVE);
4516   if (sc->csr_res == NULL)
4517     {
4518     printf("%s: bus_alloc_resource(csr) failed.\n", NAME_UNIT);
4519     return ENXIO;
4520     }
4521   sc->csr_tag    = rman_get_bustag(sc->csr_res);
4522   sc->csr_handle = rman_get_bushandle(sc->csr_res); 
4523
4524   /* Allocate PCI interrupt resources for the card. */
4525   sc->irq_res_id = 0;
4526   sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_res_id,
4527    0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
4528   if (sc->irq_res == NULL)
4529     {
4530     printf("%s: bus_alloc_resource(irq) failed.\n", NAME_UNIT);
4531     fbsd_detach(dev);
4532     return ENXIO;
4533     }
4534   if ((error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
4535    NULL, bsd_interrupt, sc, &sc->irq_cookie)))
4536     {
4537     printf("%s: bus_setup_intr() failed; error %d\n", NAME_UNIT, error);
4538     fbsd_detach(dev);
4539     return error;
4540     }
4541
4542   /* Initialize the top-half and bottom-half locks. */
4543   mtx_init(&sc->top_mtx,    NAME_UNIT, "top half lock",    MTX_DEF);
4544   mtx_init(&sc->bottom_mtx, NAME_UNIT, "bottom half lock", MTX_DEF);
4545
4546   /* Start the card and attach a kernel interface and line protocol. */
4547   if ((error = attach_card(sc, ""))) detach_card(sc);
4548   return error;
4549   }
4550
4551 static device_method_t methods[] =
4552   {
4553   DEVMETHOD(device_probe,    fbsd_probe),
4554   DEVMETHOD(device_attach,   fbsd_attach),
4555   DEVMETHOD(device_detach,   fbsd_detach),
4556   DEVMETHOD(device_shutdown, fbsd_shutdown),
4557   /* This driver does not suspend and resume. */
4558   { 0, 0 }
4559   };
4560
4561 static driver_t driver =
4562   {
4563   .name    = DEVICE_NAME,
4564   .methods = methods,
4565   .size    = sizeof(softc_t),
4566   };
4567
4568 static devclass_t devclass;
4569
4570 DRIVER_MODULE(lmc, pci, driver, devclass, 0, 0);
4571 MODULE_VERSION(lmc, 2);
4572 MODULE_DEPEND(lmc, pci, 1, 1, 1);
4573 # if NETGRAPH
4574 MODULE_DEPEND(lmc, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
4575 # endif
4576 # if NSPPP
4577 MODULE_DEPEND(lmc, sppp, 1, 1, 1);
4578 # endif
4579
4580
4581 /* This is the I/O configuration interface for NetBSD. */
4582
4583
4584 /* This is the I/O configuration interface for OpenBSD. */
4585
4586
4587 /* This is the I/O configuration interface for BSD/OS. */
4588
4589