4 * Copyright (c) 2002-2004 David Boggs. <boggs@boggs.palo-alto.ca.us>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
30 * GNU General Public License:
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)
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
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.
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.
55 * History and Authors:
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.
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.
70 * Responsible Individual:
72 * Send bug reports and improvements to <boggs@boggs.palo-alto.ca.us>.
75 # include <sys/param.h> /* OS version */
77 # include "opt_inet.h" /* INET */
78 # include "opt_inet6.h" /* INET6 */
79 # include "opt_netgraph.h" /* NETGRAPH */
80 # ifdef HAVE_KERNEL_OPTION_HEADERS
81 # include "opt_device_polling.h" /* DEVICE_POLLING */
92 # define P2P 0 /* not in FreeBSD */
93 # if (__FreeBSD_version >= 500000)
94 # define NSPPP 1 /* No count devices in FreeBSD 5 */
95 # include "opt_bpf.h" /* DEV_BPF */
96 # define NBPFILTER DEV_BPF
97 # else /* FreeBSD-4 */
98 # include "sppp.h" /* NSPPP */
99 # include "bpf.h" /* NBPF */
100 # define NBPFILTER NBPF
102 # define GEN_HDLC 0 /* not in FreeBSD */
104 # include <sys/systm.h>
105 # include <sys/kernel.h>
106 # include <sys/malloc.h>
107 # include <sys/mbuf.h>
108 # include <sys/socket.h>
109 # include <sys/sockio.h>
110 # include <sys/module.h>
111 # include <sys/bus.h>
112 # include <sys/lock.h>
114 # include <net/if_types.h>
115 # include <net/if_media.h>
116 # include <net/netisr.h>
117 # include <net/route.h>
118 # include <machine/bus.h>
119 # include <machine/resource.h>
120 # include <sys/rman.h>
122 # include <vm/pmap.h>
123 # if (__FreeBSD_version >= 700000)
124 # include <sys/priv.h>
126 # if (__FreeBSD_version >= 500000)
127 # include <sys/mutex.h>
128 # include <dev/pci/pcivar.h>
129 # else /* FreeBSD-4 */
130 # include <sys/proc.h>
131 # include <pci/pcivar.h>
134 # include <netgraph/ng_message.h>
135 # include <netgraph/netgraph.h>
138 # include <netinet/in.h>
139 # include <netinet/in_var.h>
142 # include <net/if_sppp.h>
145 # include <net/bpf.h>
148 # include <dev/lmc/if_lmc.h>
149 #endif /*__FreeBSD__*/
152 # include <sys/param.h> /* OS version */
154 # include "opt_inet.h" /* INET6, INET */
155 # define NETGRAPH 0 /* not in NetBSD */
156 # include "sppp.h" /* NSPPP */
157 # define P2P 0 /* not in NetBSD */
158 # include "opt_altq_enabled.h" /* ALTQ */
159 # include "bpfilter.h" /* NBPFILTER */
160 # define GEN_HDLC 0 /* not in NetBSD */
162 # include <sys/systm.h>
163 # include <sys/kernel.h>
164 # include <sys/lkm.h>
165 # include <sys/mbuf.h>
166 # include <sys/socket.h>
167 # include <sys/sockio.h>
168 # include <sys/device.h>
169 # include <sys/lock.h>
171 # include <net/if_types.h>
172 # include <net/if_media.h>
173 # include <net/netisr.h>
174 # include <machine/bus.h>
175 # include <machine/intr.h>
176 # include <dev/pci/pcivar.h>
177 # if (__NetBSD_Version__ >= 106000000)
178 # include <uvm/uvm_extern.h>
183 # include <netinet/in.h>
184 # include <netinet/in_var.h>
187 # if (__NetBSD_Version__ >= 106000000)
188 # include <net/if_spppvar.h>
190 # include <net/if_sppp.h>
194 # include <net/bpf.h>
198 #endif /*__NetBSD__*/
201 # include <sys/param.h> /* OS version */
203 /* -DINET is passed on the compiler command line */
204 /* -DINET6 is passed on the compiler command line */
205 # define NETGRAPH 0 /* not in OpenBSD */
206 # include "sppp.h" /* NSPPP */
207 # define P2P 0 /* not in OpenBSD */
208 /* -DALTQ is passed on the compiler command line */
209 # include "bpfilter.h" /* NBPFILTER */
210 # define GEN_HDLC 0 /* not in OpenBSD */
212 # include <sys/systm.h>
213 # include <sys/kernel.h>
214 # include <sys/conf.h>
215 # include <sys/exec.h>
216 # include <sys/lkm.h>
217 # include <sys/mbuf.h>
218 # include <sys/socket.h>
219 # include <sys/sockio.h>
220 # include <sys/device.h>
221 # include <sys/lock.h>
223 # include <net/if_types.h>
224 # include <net/if_media.h>
225 # include <net/netisr.h>
226 # include <machine/bus.h>
227 # include <machine/intr.h>
228 # include <dev/pci/pcivar.h>
229 # if (OpenBSD >= 200206)
230 # include <uvm/uvm_extern.h>
235 # include <netinet/in.h>
236 # include <netinet/in_var.h>
239 # include <net/if_sppp.h>
242 # include <net/bpf.h>
246 #endif /*__OpenBSD__*/
249 # include <sys/param.h> /* OS version */
251 /* -DINET is passed on the compiler command line */
252 /* -DINET6 is passed on the compiler command line */
253 # define NETGRAPH 0 /* not in BSD/OS */
254 # define NSPPP 0 /* not in BSD/OS */
255 /* -DPPP is passed on the compiler command line */
256 /* -DCISCO_HDLC is passed on the compiler command line */
257 /* -DFR is passed on the compiler command line */
258 # if (PPP || CISCO_HDLC || FR)
263 # define ALTQ 0 /* not in BSD/OS */
264 # include "bpfilter.h" /* NBPFILTER */
265 # define GEN_HDLC 0 /* not in BSD/OS */
267 # include <sys/kernel.h>
268 # include <sys/malloc.h>
269 # include <sys/mbuf.h>
270 # include <sys/socket.h>
271 # include <sys/sockio.h>
272 # include <sys/device.h>
273 # include <sys/lock.h>
275 # include <net/if_types.h>
276 # include <net/if_media.h>
277 # include <net/netisr.h>
279 # include <i386/isa/dma.h>
280 # include <i386/isa/isavar.h>
281 # include <i386/include/cpu.h>
282 # include <i386/pci/pci.h>
284 # include <netinet/in.h>
285 # include <netinet/in_var.h>
288 # include <net/if_p2p.h>
289 # include <sys/ttycom.h>
292 # include <net/bpf.h>
299 # include <linux/config.h>
300 # if (CONFIG_HDLC || CONFIG_HDLC_MODULE)
305 # define IFNET 0 /* different in Linux */
306 # define NETGRAPH 0 /* not in Linux */
307 # define NSPPP 0 /* different in Linux */
308 # define P2P 0 /* not in Linux */
309 # define ALTQ 0 /* different in Linux */
310 # define NBPFILTER 0 /* different in Linux */
312 # include <linux/pci.h>
313 # include <linux/delay.h>
314 # include <linux/netdevice.h>
315 # include <linux/if_arp.h>
317 # include <linux/hdlc.h>
321 #endif /* __linux__ */
323 /* The SROM is a generic 93C46 serial EEPROM (64 words by 16 bits). */
324 /* Data is set up before the RISING edge of CLK; CLK is parked low. */
326 shift_srom_bits(softc_t *sc, u_int32_t data, u_int32_t len)
328 u_int32_t csr = READ_CSR(TLP_SROM_MII);
331 if (data & (1<<(len-1)))
332 csr |= TLP_SROM_DIN; /* DIN setup */
334 csr &= ~TLP_SROM_DIN; /* DIN setup */
335 WRITE_CSR(TLP_SROM_MII, csr);
336 csr |= TLP_SROM_CLK; /* CLK rising edge */
337 WRITE_CSR(TLP_SROM_MII, csr);
338 csr &= ~TLP_SROM_CLK; /* CLK falling edge */
339 WRITE_CSR(TLP_SROM_MII, csr);
343 /* Data is sampled on the RISING edge of CLK; CLK is parked low. */
345 read_srom(softc_t *sc, u_int8_t addr)
351 /* Enable SROM access. */
352 csr = (TLP_SROM_SEL | TLP_SROM_RD | TLP_MII_MDOE);
353 WRITE_CSR(TLP_SROM_MII, csr);
354 /* CS rising edge prepares SROM for a new cycle. */
356 WRITE_CSR(TLP_SROM_MII, csr); /* assert CS */
357 shift_srom_bits(sc, 6, 4); /* issue read cmd */
358 shift_srom_bits(sc, addr, 6); /* issue address */
359 for (data=0, i=16; i>=0; i--) /* read ->17<- bits of data */
361 csr = READ_CSR(TLP_SROM_MII); /* DOUT sampled */
362 data = (data<<1) | ((csr & TLP_SROM_DOUT) ? 1:0);
363 csr |= TLP_SROM_CLK; /* CLK rising edge */
364 WRITE_CSR(TLP_SROM_MII, csr);
365 csr &= ~TLP_SROM_CLK; /* CLK falling edge */
366 WRITE_CSR(TLP_SROM_MII, csr);
368 /* Disable SROM access. */
369 WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOE);
374 /* The SROM is formatted by the mfgr and should NOT be written! */
375 /* But lmcconfig can rewrite it in case it gets overwritten somehow. */
376 /* IOCTL SYSCALL: can sleep. */
378 write_srom(softc_t *sc, u_int8_t addr, u_int16_t data)
383 /* Enable SROM access. */
384 csr = (TLP_SROM_SEL | TLP_SROM_RD | TLP_MII_MDOE);
385 WRITE_CSR(TLP_SROM_MII, csr);
387 /* Issue write-enable command. */
389 WRITE_CSR(TLP_SROM_MII, csr); /* assert CS */
390 shift_srom_bits(sc, 4, 4); /* issue write enable cmd */
391 shift_srom_bits(sc, 63, 6); /* issue address */
393 WRITE_CSR(TLP_SROM_MII, csr); /* deassert CS */
395 /* Issue erase command. */
397 WRITE_CSR(TLP_SROM_MII, csr); /* assert CS */
398 shift_srom_bits(sc, 7, 4); /* issue erase cmd */
399 shift_srom_bits(sc, addr, 6); /* issue address */
401 WRITE_CSR(TLP_SROM_MII, csr); /* deassert CS */
403 /* Issue write command. */
405 WRITE_CSR(TLP_SROM_MII, csr); /* assert CS */
406 for (i=0; i<10; i++) /* 100 ms max wait */
407 if ((READ_CSR(TLP_SROM_MII) & TLP_SROM_DOUT)==0) SLEEP(10000);
408 shift_srom_bits(sc, 5, 4); /* issue write cmd */
409 shift_srom_bits(sc, addr, 6); /* issue address */
410 shift_srom_bits(sc, data, 16); /* issue data */
412 WRITE_CSR(TLP_SROM_MII, csr); /* deassert CS */
414 /* Issue write-disable command. */
416 WRITE_CSR(TLP_SROM_MII, csr); /* assert CS */
417 for (i=0; i<10; i++) /* 100 ms max wait */
418 if ((READ_CSR(TLP_SROM_MII) & TLP_SROM_DOUT)==0) SLEEP(10000);
419 shift_srom_bits(sc, 4, 4); /* issue write disable cmd */
420 shift_srom_bits(sc, 0, 6); /* issue address */
422 WRITE_CSR(TLP_SROM_MII, csr); /* deassert CS */
424 /* Disable SROM access. */
425 WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOE);
428 /* Not all boards have BIOS roms. */
429 /* The BIOS ROM is an AMD 29F010 1Mbit (128K by 8) EEPROM. */
431 read_bios(softc_t *sc, u_int32_t addr)
435 /* Load the BIOS rom address register. */
436 WRITE_CSR(TLP_BIOS_ROM, addr);
438 /* Enable the BIOS rom. */
439 srom_mii = TLP_BIOS_SEL | TLP_BIOS_RD | TLP_MII_MDOE;
440 WRITE_CSR(TLP_SROM_MII, srom_mii);
442 /* Wait at least 20 PCI cycles. */
445 /* Read the BIOS rom data. */
446 srom_mii = READ_CSR(TLP_SROM_MII);
448 /* Disable the BIOS rom. */
449 WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOE);
451 return (u_int8_t)srom_mii & 0xFF;
455 write_bios_phys(softc_t *sc, u_int32_t addr, u_int8_t data)
459 /* Load the BIOS rom address register. */
460 WRITE_CSR(TLP_BIOS_ROM, addr);
462 /* Enable the BIOS rom. */
463 srom_mii = TLP_BIOS_SEL | TLP_BIOS_WR | TLP_MII_MDOE;
465 /* Load the data into the data register. */
466 srom_mii = (srom_mii & 0xFFFFFF00) | (data & 0xFF);
467 WRITE_CSR(TLP_SROM_MII, srom_mii);
469 /* Wait at least 20 PCI cycles. */
472 /* Disable the BIOS rom. */
473 WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOE);
476 /* IOCTL SYSCALL: can sleep. */
478 write_bios(softc_t *sc, u_int32_t addr, u_int8_t data)
482 /* this sequence enables writing */
483 write_bios_phys(sc, 0x5555, 0xAA);
484 write_bios_phys(sc, 0x2AAA, 0x55);
485 write_bios_phys(sc, 0x5555, 0xA0);
486 write_bios_phys(sc, addr, data);
488 /* Wait for the write operation to complete. */
489 for (;;) /* interruptable syscall */
493 read_data = read_bios(sc, addr);
494 if ((read_data & 0x80) == (data & 0x80)) break;
495 if (read_data & 0x20)
496 { /* Data sheet says read it again. */
497 read_data = read_bios(sc, addr);
498 if ((read_data & 0x80) == (data & 0x80)) break;
500 printf("%s: write_bios() failed; rom addr=0x%x\n",
505 read_data = read_bios(sc, addr);
506 if (read_data == data) break;
510 /* IOCTL SYSCALL: can sleep. */
512 erase_bios(softc_t *sc)
514 unsigned char read_data;
516 /* This sequence enables erasing: */
517 write_bios_phys(sc, 0x5555, 0xAA);
518 write_bios_phys(sc, 0x2AAA, 0x55);
519 write_bios_phys(sc, 0x5555, 0x80);
520 write_bios_phys(sc, 0x5555, 0xAA);
521 write_bios_phys(sc, 0x2AAA, 0x55);
522 write_bios_phys(sc, 0x5555, 0x10);
524 /* Wait for the erase operation to complete. */
525 for (;;) /* interruptable syscall */
529 read_data = read_bios(sc, 0);
530 if (read_data & 0x80) break;
531 if (read_data & 0x20)
532 { /* Data sheet says read it again. */
533 read_data = read_bios(sc, 0);
534 if (read_data & 0x80) break;
536 printf("%s: erase_bios() failed\n", NAME_UNIT);
540 read_data = read_bios(sc, 0);
541 if (read_data == 0xFF) break;
545 /* MDIO is 3-stated between tranactions. */
546 /* MDIO is set up before the RISING edge of MDC; MDC is parked low. */
548 shift_mii_bits(softc_t *sc, u_int32_t data, u_int32_t len)
550 u_int32_t csr = READ_CSR(TLP_SROM_MII);
553 if (data & (1<<(len-1)))
554 csr |= TLP_MII_MDOUT; /* MDOUT setup */
556 csr &= ~TLP_MII_MDOUT; /* MDOUT setup */
557 WRITE_CSR(TLP_SROM_MII, csr);
558 csr |= TLP_MII_MDC; /* MDC rising edge */
559 WRITE_CSR(TLP_SROM_MII, csr);
560 csr &= ~TLP_MII_MDC; /* MDC falling edge */
561 WRITE_CSR(TLP_SROM_MII, csr);
565 /* The specification for the MII is IEEE Std 802.3 clause 22. */
566 /* MDIO is sampled on the RISING edge of MDC; MDC is parked low. */
568 read_mii(softc_t *sc, u_int8_t regad)
574 WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOUT);
576 shift_mii_bits(sc, 0xFFFFF, 20); /* preamble */
577 shift_mii_bits(sc, 0xFFFFF, 20); /* preamble */
578 shift_mii_bits(sc, 1, 2); /* start symbol */
579 shift_mii_bits(sc, 2, 2); /* read op */
580 shift_mii_bits(sc, 0, 5); /* phyad=0 */
581 shift_mii_bits(sc, regad, 5); /* regad */
582 csr = READ_CSR(TLP_SROM_MII);
584 WRITE_CSR(TLP_SROM_MII, csr);
585 shift_mii_bits(sc, 0, 2); /* turn-around */
586 for (i=15; i>=0; i--) /* data */
588 csr = READ_CSR(TLP_SROM_MII); /* MDIN sampled */
589 data = (data<<1) | ((csr & TLP_MII_MDIN) ? 1:0);
590 csr |= TLP_MII_MDC; /* MDC rising edge */
591 WRITE_CSR(TLP_SROM_MII, csr);
592 csr &= ~TLP_MII_MDC; /* MDC falling edge */
593 WRITE_CSR(TLP_SROM_MII, csr);
599 write_mii(softc_t *sc, u_int8_t regad, u_int16_t data)
601 WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOUT);
602 shift_mii_bits(sc, 0xFFFFF, 20); /* preamble */
603 shift_mii_bits(sc, 0xFFFFF, 20); /* preamble */
604 shift_mii_bits(sc, 1, 2); /* start symbol */
605 shift_mii_bits(sc, 1, 2); /* write op */
606 shift_mii_bits(sc, 0, 5); /* phyad=0 */
607 shift_mii_bits(sc, regad, 5); /* regad */
608 shift_mii_bits(sc, 2, 2); /* turn-around */
609 shift_mii_bits(sc, data, 16); /* data */
610 WRITE_CSR(TLP_SROM_MII, TLP_MII_MDOE);
611 if (regad == 16) sc->led_state = data; /* a small optimization */
615 set_mii16_bits(softc_t *sc, u_int16_t bits)
617 u_int16_t mii16 = read_mii(sc, 16);
619 write_mii(sc, 16, mii16);
623 clr_mii16_bits(softc_t *sc, u_int16_t bits)
625 u_int16_t mii16 = read_mii(sc, 16);
627 write_mii(sc, 16, mii16);
631 set_mii17_bits(softc_t *sc, u_int16_t bits)
633 u_int16_t mii17 = read_mii(sc, 17);
635 write_mii(sc, 17, mii17);
639 clr_mii17_bits(softc_t *sc, u_int16_t bits)
641 u_int16_t mii17 = read_mii(sc, 17);
643 write_mii(sc, 17, mii17);
647 * Watchdog code is more readable if it refreshes LEDs
648 * once a second whether they need it or not.
649 * But MII refs take 150 uSecs each, so remember the last value
650 * written to MII16 and avoid LED writes that do nothing.
654 led_off(softc_t *sc, u_int16_t led)
656 if ((led & sc->led_state) == led) return;
657 set_mii16_bits(sc, led);
661 led_on(softc_t *sc, u_int16_t led)
663 if ((led & sc->led_state) == 0) return;
664 clr_mii16_bits(sc, led);
668 led_inv(softc_t *sc, u_int16_t led)
670 u_int16_t mii16 = read_mii(sc, 16);
672 write_mii(sc, 16, mii16);
676 * T1 & T3 framer registers are accessed through MII regs 17 & 18.
677 * Write the address to MII reg 17 then R/W data through MII reg 18.
678 * The hardware interface is an Intel-style 8-bit muxed A/D bus.
681 write_framer(softc_t *sc, u_int16_t addr, u_int8_t data)
683 write_mii(sc, 17, addr);
684 write_mii(sc, 18, data);
688 read_framer(softc_t *sc, u_int16_t addr)
690 write_mii(sc, 17, addr);
691 return (u_int8_t)read_mii(sc, 18);
694 /* Tulip's hardware implementation of General Purpose IO
695 * (GPIO) pins makes life difficult for software.
696 * Bits 7-0 in the Tulip GPIO CSR are used for two purposes
697 * depending on the state of bit 8.
698 * If bit 8 is 0 then bits 7-0 are "data" bits.
699 * If bit 8 is 1 then bits 7-0 are "direction" bits.
700 * If a direction bit is one, the data bit is an output.
701 * The problem is that the direction bits are WRITE-ONLY.
702 * Software must remember the direction bits in a shadow copy.
703 * (sc->gpio_dir) in order to change some but not all of the bits.
704 * All accesses to the Tulip GPIO register use these five procedures.
708 make_gpio_input(softc_t *sc, u_int32_t bits)
710 sc->gpio_dir &= ~bits;
711 WRITE_CSR(TLP_GPIO, TLP_GPIO_DIR | (sc->gpio_dir));
715 make_gpio_output(softc_t *sc, u_int32_t bits)
717 sc->gpio_dir |= bits;
718 WRITE_CSR(TLP_GPIO, TLP_GPIO_DIR | (sc->gpio_dir));
722 read_gpio(softc_t *sc)
724 return READ_CSR(TLP_GPIO);
728 set_gpio_bits(softc_t *sc, u_int32_t bits)
730 WRITE_CSR(TLP_GPIO, (read_gpio(sc) | bits) & 0xFF);
734 clr_gpio_bits(softc_t *sc, u_int32_t bits)
736 WRITE_CSR(TLP_GPIO, (read_gpio(sc) & ~bits) & 0xFF);
739 /* Reset ALL of the flip-flops in the gate array to zero. */
740 /* This does NOT change the gate array programming. */
741 /* Called during initialization so it must not sleep. */
743 reset_xilinx(softc_t *sc)
745 /* Drive RESET low to force initialization. */
746 clr_gpio_bits(sc, GPIO_RESET);
747 make_gpio_output(sc, GPIO_RESET);
749 /* Hold RESET low for more than 10 uSec. */
752 /* Done with RESET; make it an input. */
753 make_gpio_input(sc, GPIO_RESET);
756 /* Load Xilinx gate array program from on-board rom. */
757 /* This changes the gate array programming. */
758 /* IOCTL SYSCALL: can sleep. */
760 load_xilinx_from_rom(softc_t *sc)
764 /* Drive MODE low to load from ROM rather than GPIO. */
765 clr_gpio_bits(sc, GPIO_MODE);
766 make_gpio_output(sc, GPIO_MODE);
768 /* Drive DP & RESET low to force configuration. */
769 clr_gpio_bits(sc, GPIO_RESET | GPIO_DP);
770 make_gpio_output(sc, GPIO_RESET | GPIO_DP);
772 /* Hold RESET & DP low for more than 10 uSec. */
775 /* Done with RESET & DP; make them inputs. */
776 make_gpio_input(sc, GPIO_DP | GPIO_RESET);
778 /* BUSY-WAIT for Xilinx chip to configure itself from ROM bits. */
779 for (i=0; i<100; i++) /* 1 sec max delay */
780 if ((read_gpio(sc) & GPIO_DP) == 0) SLEEP(10000);
782 /* Done with MODE; make it an input. */
783 make_gpio_input(sc, GPIO_MODE);
786 /* Load the Xilinx gate array program from userland bits. */
787 /* This changes the gate array programming. */
788 /* IOCTL SYSCALL: can sleep. */
790 load_xilinx_from_file(softc_t *sc, char *addr, u_int32_t len)
795 /* Get some pages to hold the Xilinx bits; biggest file is < 6 KB. */
796 if (len > 8192) return EFBIG; /* too big */
797 data = malloc(len, M_DEVBUF, M_WAITOK);
798 if (data == NULL) return ENOMEM;
800 /* Copy the Xilinx bits from userland. */
801 if ((error = copyin(addr, data, len)))
803 free(data, M_DEVBUF);
807 /* Drive MODE high to load from GPIO rather than ROM. */
808 set_gpio_bits(sc, GPIO_MODE);
809 make_gpio_output(sc, GPIO_MODE);
811 /* Drive DP & RESET low to force configuration. */
812 clr_gpio_bits(sc, GPIO_RESET | GPIO_DP);
813 make_gpio_output(sc, GPIO_RESET | GPIO_DP);
815 /* Hold RESET & DP low for more than 10 uSec. */
818 /* Done with RESET & DP; make them inputs. */
819 make_gpio_input(sc, GPIO_RESET | GPIO_DP);
821 /* BUSY-WAIT for Xilinx chip to clear its config memory. */
822 make_gpio_input(sc, GPIO_INIT);
823 for (i=0; i<10000; i++) /* 1 sec max delay */
824 if ((read_gpio(sc) & GPIO_INIT)==0) SLEEP(10000);
826 /* Configure CLK and DATA as outputs. */
827 set_gpio_bits(sc, GPIO_CLK); /* park CLK high */
828 make_gpio_output(sc, GPIO_CLK | GPIO_DATA);
830 /* Write bits to Xilinx; CLK is parked HIGH. */
831 /* DATA is set up before the RISING edge of CLK. */
832 for (i=0; i<len; i++)
835 if ((data[i] & (1<<j)) != 0)
836 set_gpio_bits(sc, GPIO_DATA); /* DATA setup */
838 clr_gpio_bits(sc, GPIO_DATA); /* DATA setup */
839 clr_gpio_bits(sc, GPIO_CLK); /* CLK falling edge */
840 set_gpio_bits(sc, GPIO_CLK); /* CLK rising edge */
843 /* Stop driving all Xilinx-related signals. */
844 /* Pullup and pulldown resistors take over. */
845 make_gpio_input(sc, GPIO_CLK | GPIO_DATA | GPIO_MODE);
847 free(data, M_DEVBUF);
851 /* Write fragments of a command into the synthesized oscillator. */
852 /* DATA is set up before the RISING edge of CLK. CLK is parked low. */
854 shift_synth_bits(softc_t *sc, u_int32_t data, u_int32_t len)
858 for (i=0; i<len; i++)
860 if ((data & (1<<i)) != 0)
861 set_gpio_bits(sc, GPIO_DATA); /* DATA setup */
863 clr_gpio_bits(sc, GPIO_DATA); /* DATA setup */
864 set_gpio_bits(sc, GPIO_CLK); /* CLK rising edge */
865 clr_gpio_bits(sc, GPIO_CLK); /* CLK falling edge */
869 /* Write a command to the synthesized oscillator on SSI and HSSIc. */
871 write_synth(softc_t *sc, struct synth *synth)
873 /* SSI cards have a programmable prescaler */
874 if (sc->status.card_type == TLP_CSID_SSI)
876 if (synth->prescale == 9) /* divide by 512 */
877 set_mii17_bits(sc, MII17_SSI_PRESCALE);
878 else /* divide by 32 */
879 clr_mii17_bits(sc, MII17_SSI_PRESCALE);
882 clr_gpio_bits(sc, GPIO_DATA | GPIO_CLK);
883 make_gpio_output(sc, GPIO_DATA | GPIO_CLK);
885 /* SYNTH is a low-true chip enable for the AV9110 chip. */
886 set_gpio_bits(sc, GPIO_SSI_SYNTH);
887 make_gpio_output(sc, GPIO_SSI_SYNTH);
888 clr_gpio_bits(sc, GPIO_SSI_SYNTH);
890 /* Serially shift the command into the AV9110 chip. */
891 shift_synth_bits(sc, synth->n, 7);
892 shift_synth_bits(sc, synth->m, 7);
893 shift_synth_bits(sc, synth->v, 1);
894 shift_synth_bits(sc, synth->x, 2);
895 shift_synth_bits(sc, synth->r, 2);
896 shift_synth_bits(sc, 0x16, 5); /* enable clk/x output */
898 /* SYNTH (chip enable) going high ends the command. */
899 set_gpio_bits(sc, GPIO_SSI_SYNTH);
900 make_gpio_input(sc, GPIO_SSI_SYNTH);
902 /* Stop driving serial-related signals; pullups/pulldowns take over. */
903 make_gpio_input(sc, GPIO_DATA | GPIO_CLK);
905 /* remember the new synthesizer parameters */
906 if (&sc->config.synth != synth) sc->config.synth = *synth;
909 /* Write a command to the DAC controlling the VCXO on some T3 adapters. */
910 /* The DAC is a TI-TLV5636: 12-bit resolution and a serial interface. */
911 /* DATA is set up before the FALLING edge of CLK. CLK is parked HIGH. */
913 write_dac(softc_t *sc, u_int16_t data)
917 /* Prepare to use DATA and CLK. */
918 set_gpio_bits(sc, GPIO_DATA | GPIO_CLK);
919 make_gpio_output(sc, GPIO_DATA | GPIO_CLK);
921 /* High-to-low transition prepares DAC for new value. */
922 set_gpio_bits(sc, GPIO_T3_DAC);
923 make_gpio_output(sc, GPIO_T3_DAC);
924 clr_gpio_bits(sc, GPIO_T3_DAC);
926 /* Serially shift command bits into DAC. */
929 if ((data & (1<<(15-i))) != 0)
930 set_gpio_bits(sc, GPIO_DATA); /* DATA setup */
932 clr_gpio_bits(sc, GPIO_DATA); /* DATA setup */
933 clr_gpio_bits(sc, GPIO_CLK); /* CLK falling edge */
934 set_gpio_bits(sc, GPIO_CLK); /* CLK rising edge */
937 /* Done with DAC; make it an input; loads new value into DAC. */
938 set_gpio_bits(sc, GPIO_T3_DAC);
939 make_gpio_input(sc, GPIO_T3_DAC);
941 /* Stop driving serial-related signals; pullups/pulldowns take over. */
942 make_gpio_input(sc, GPIO_DATA | GPIO_CLK);
945 /* begin HSSI card code */
947 /* Must not sleep. */
949 hssi_config(softc_t *sc)
951 if (sc->status.card_type == 0)
953 sc->status.card_type = READ_PCI_CFG(sc, TLP_CSID);
954 sc->config.crc_len = CFG_CRC_16;
955 sc->config.loop_back = CFG_LOOP_NONE;
956 sc->config.tx_clk_src = CFG_CLKMUX_ST;
957 sc->config.dte_dce = CFG_DTE;
958 sc->config.synth.n = 52; /* 52.000 Mbs */
959 sc->config.synth.m = 5;
960 sc->config.synth.v = 0;
961 sc->config.synth.x = 0;
962 sc->config.synth.r = 0;
963 sc->config.synth.prescale = 2;
967 if (sc->config.crc_len == CFG_CRC_32)
968 set_mii16_bits(sc, MII16_HSSI_CRC32);
970 clr_mii16_bits(sc, MII16_HSSI_CRC32);
972 /* Assert pin LA in HSSI conn: ask modem for local loop. */
973 if (sc->config.loop_back == CFG_LOOP_LL)
974 set_mii16_bits(sc, MII16_HSSI_LA);
976 clr_mii16_bits(sc, MII16_HSSI_LA);
978 /* Assert pin LB in HSSI conn: ask modem for remote loop. */
979 if (sc->config.loop_back == CFG_LOOP_RL)
980 set_mii16_bits(sc, MII16_HSSI_LB);
982 clr_mii16_bits(sc, MII16_HSSI_LB);
984 if (sc->status.card_type == TLP_CSID_HSSI)
987 if (sc->config.tx_clk_src == CFG_CLKMUX_ST)
988 set_gpio_bits(sc, GPIO_HSSI_TXCLK);
990 clr_gpio_bits(sc, GPIO_HSSI_TXCLK);
991 make_gpio_output(sc, GPIO_HSSI_TXCLK);
993 else if (sc->status.card_type == TLP_CSID_HSSIc)
994 { /* cPCI HSSI rev C has extra features */
995 /* Set TXCLK source. */
996 u_int16_t mii16 = read_mii(sc, 16);
997 mii16 &= ~MII16_HSSI_CLKMUX;
998 mii16 |= (sc->config.tx_clk_src&3)<<13;
999 write_mii(sc, 16, mii16);
1001 /* cPCI HSSI implements loopback towards the net. */
1002 if (sc->config.loop_back == CFG_LOOP_LINE)
1003 set_mii16_bits(sc, MII16_HSSI_LOOP);
1005 clr_mii16_bits(sc, MII16_HSSI_LOOP);
1007 /* Set DTE/DCE mode. */
1008 if (sc->config.dte_dce == CFG_DCE)
1009 set_gpio_bits(sc, GPIO_HSSI_DCE);
1011 clr_gpio_bits(sc, GPIO_HSSI_DCE);
1012 make_gpio_output(sc, GPIO_HSSI_DCE);
1014 /* Program the synthesized oscillator. */
1015 write_synth(sc, &sc->config.synth);
1020 hssi_ident(softc_t *sc)
1024 /* Called once a second; must not sleep. */
1026 hssi_watchdog(softc_t *sc)
1028 u_int16_t mii16 = read_mii(sc, 16) & MII16_HSSI_MODEM;
1029 int link_status = STATUS_UP;
1031 led_inv(sc, MII16_HSSI_LED_UL); /* Software is alive. */
1032 led_on(sc, MII16_HSSI_LED_LL); /* always on (SSI cable) */
1034 /* Check the transmit clock. */
1035 if (sc->status.tx_speed == 0)
1037 led_on(sc, MII16_HSSI_LED_UR);
1038 link_status = STATUS_DOWN;
1041 led_off(sc, MII16_HSSI_LED_UR);
1043 /* Is the modem ready? */
1044 if ((mii16 & MII16_HSSI_CA) == 0)
1046 led_off(sc, MII16_HSSI_LED_LR);
1047 link_status = STATUS_DOWN;
1050 led_on(sc, MII16_HSSI_LED_LR);
1052 /* Print the modem control signals if they changed. */
1053 if ((DRIVER_DEBUG) && (mii16 != sc->last_mii16))
1055 char *on = "ON ", *off = "OFF";
1056 printf("%s: TA=%s CA=%s LA=%s LB=%s LC=%s TM=%s\n", NAME_UNIT,
1057 (mii16 & MII16_HSSI_TA) ? on : off,
1058 (mii16 & MII16_HSSI_CA) ? on : off,
1059 (mii16 & MII16_HSSI_LA) ? on : off,
1060 (mii16 & MII16_HSSI_LB) ? on : off,
1061 (mii16 & MII16_HSSI_LC) ? on : off,
1062 (mii16 & MII16_HSSI_TM) ? on : off);
1065 /* SNMP one-second-report */
1066 sc->status.snmp.hssi.sigs = mii16 & MII16_HSSI_MODEM;
1068 /* Remember this state until next time. */
1069 sc->last_mii16 = mii16;
1071 /* If a loop back is in effect, link status is UP */
1072 if (sc->config.loop_back != CFG_LOOP_NONE)
1073 link_status = STATUS_UP;
1078 /* IOCTL SYSCALL: can sleep (but doesn't). */
1080 hssi_ioctl(softc_t *sc, struct ioctl *ioctl)
1084 if (ioctl->cmd == IOCTL_SNMP_SIGS)
1086 u_int16_t mii16 = read_mii(sc, 16);
1087 mii16 &= ~MII16_HSSI_MODEM;
1088 mii16 |= (MII16_HSSI_MODEM & ioctl->data);
1089 write_mii(sc, 16, mii16);
1091 else if (ioctl->cmd == IOCTL_SET_STATUS)
1093 if (ioctl->data != 0)
1094 set_mii16_bits(sc, MII16_HSSI_TA);
1096 clr_mii16_bits(sc, MII16_HSSI_TA);
1104 /* begin DS3 card code */
1106 /* Must not sleep. */
1108 t3_config(softc_t *sc)
1113 if (sc->status.card_type == 0)
1115 sc->status.card_type = TLP_CSID_T3;
1116 sc->config.crc_len = CFG_CRC_16;
1117 sc->config.loop_back = CFG_LOOP_NONE;
1118 sc->config.format = CFG_FORMAT_T3CPAR;
1119 sc->config.cable_len = 10; /* meters */
1120 sc->config.scrambler = CFG_SCRAM_DL_KEN;
1121 sc->config.tx_clk_src = CFG_CLKMUX_INT;
1123 /* Center the VCXO -- get within 20 PPM of 44736000. */
1124 write_dac(sc, 0x9002); /* set Vref = 2.048 volts */
1125 write_dac(sc, 2048); /* range is 0..4095 */
1128 /* Set cable length. */
1129 if (sc->config.cable_len > 30)
1130 clr_mii16_bits(sc, MII16_DS3_ZERO);
1132 set_mii16_bits(sc, MII16_DS3_ZERO);
1134 /* Set payload scrambler polynomial. */
1135 if (sc->config.scrambler == CFG_SCRAM_LARS)
1136 set_mii16_bits(sc, MII16_DS3_POLY);
1138 clr_mii16_bits(sc, MII16_DS3_POLY);
1140 /* Set payload scrambler on/off. */
1141 if (sc->config.scrambler == CFG_SCRAM_OFF)
1142 clr_mii16_bits(sc, MII16_DS3_SCRAM);
1144 set_mii16_bits(sc, MII16_DS3_SCRAM);
1146 /* Set CRC length. */
1147 if (sc->config.crc_len == CFG_CRC_32)
1148 set_mii16_bits(sc, MII16_DS3_CRC32);
1150 clr_mii16_bits(sc, MII16_DS3_CRC32);
1152 /* Loopback towards host thru the line interface. */
1153 if (sc->config.loop_back == CFG_LOOP_OTHER)
1154 set_mii16_bits(sc, MII16_DS3_TRLBK);
1156 clr_mii16_bits(sc, MII16_DS3_TRLBK);
1158 /* Loopback towards network thru the line interface. */
1159 if (sc->config.loop_back == CFG_LOOP_LINE)
1160 set_mii16_bits(sc, MII16_DS3_LNLBK);
1161 else if (sc->config.loop_back == CFG_LOOP_DUAL)
1162 set_mii16_bits(sc, MII16_DS3_LNLBK);
1164 clr_mii16_bits(sc, MII16_DS3_LNLBK);
1166 /* Configure T3 framer chip; write EVERY writeable register. */
1167 ctl1 = CTL1_SER | CTL1_XTX;
1168 if (sc->config.loop_back == CFG_LOOP_INWARD) ctl1 |= CTL1_3LOOP;
1169 if (sc->config.loop_back == CFG_LOOP_DUAL) ctl1 |= CTL1_3LOOP;
1170 if (sc->config.format == CFG_FORMAT_T3M13) ctl1 |= CTL1_M13MODE;
1171 write_framer(sc, T3CSR_CTL1, ctl1);
1172 write_framer(sc, T3CSR_TX_FEAC, CTL5_EMODE);
1173 write_framer(sc, T3CSR_CTL8, CTL8_FBEC);
1174 write_framer(sc, T3CSR_CTL12, CTL12_DLCB1 | CTL12_C21 | CTL12_MCB1);
1175 write_framer(sc, T3CSR_DBL_FEAC, 0);
1176 write_framer(sc, T3CSR_CTL14, CTL14_RGCEN | CTL14_TGCEN);
1177 write_framer(sc, T3CSR_INTEN, 0);
1178 write_framer(sc, T3CSR_CTL20, CTL20_CVEN);
1180 /* Clear error counters and latched error bits */
1181 /* that may have happened while initializing. */
1182 for (i=0; i<21; i++) read_framer(sc, i);
1186 t3_ident(softc_t *sc)
1188 printf(", TXC03401 rev B");
1191 /* Called once a second; must not sleep. */
1193 t3_watchdog(softc_t *sc)
1196 u_int8_t CERR, PERR, MERR, FERR, FEBE;
1197 u_int8_t ctl1, stat16, feac;
1198 int link_status = STATUS_UP;
1201 /* Read the alarm registers. */
1202 ctl1 = read_framer(sc, T3CSR_CTL1);
1203 stat16 = read_framer(sc, T3CSR_STAT16);
1204 mii16 = read_mii(sc, 16);
1206 /* Always ignore the RTLOC alarm bit. */
1207 stat16 &= ~STAT16_RTLOC;
1209 /* Software is alive. */
1210 led_inv(sc, MII16_DS3_LED_GRN);
1212 /* Receiving Alarm Indication Signal (AIS). */
1213 if ((stat16 & STAT16_RAIS) != 0) /* receiving ais */
1214 led_on(sc, MII16_DS3_LED_BLU);
1215 else if (ctl1 & CTL1_TXAIS) /* sending ais */
1216 led_inv(sc, MII16_DS3_LED_BLU);
1218 led_off(sc, MII16_DS3_LED_BLU);
1220 /* Receiving Remote Alarm Indication (RAI). */
1221 if ((stat16 & STAT16_XERR) != 0) /* receiving rai */
1222 led_on(sc, MII16_DS3_LED_YEL);
1223 else if ((ctl1 & CTL1_XTX) == 0) /* sending rai */
1224 led_inv(sc, MII16_DS3_LED_YEL);
1226 led_off(sc, MII16_DS3_LED_YEL);
1228 /* If certain status bits are set then the link is 'down'. */
1229 /* The bad bits are: rxlos rxoof rxais rxidl xerr. */
1230 if ((stat16 & ~(STAT16_FEAC | STAT16_SEF)) != 0)
1231 link_status = STATUS_DOWN;
1233 /* Declare local Red Alarm if the link is down. */
1234 if (link_status == STATUS_DOWN)
1235 led_on(sc, MII16_DS3_LED_RED);
1236 else if (sc->loop_timer != 0) /* loopback is active */
1237 led_inv(sc, MII16_DS3_LED_RED);
1239 led_off(sc, MII16_DS3_LED_RED);
1241 /* Print latched error bits if they changed. */
1242 if ((DRIVER_DEBUG) && ((stat16 & ~STAT16_FEAC) != sc->last_stat16))
1244 char *on = "ON ", *off = "OFF";
1245 printf("%s: RLOS=%s ROOF=%s RAIS=%s RIDL=%s SEF=%s XERR=%s\n",
1247 (stat16 & STAT16_RLOS) ? on : off,
1248 (stat16 & STAT16_ROOF) ? on : off,
1249 (stat16 & STAT16_RAIS) ? on : off,
1250 (stat16 & STAT16_RIDL) ? on : off,
1251 (stat16 & STAT16_SEF) ? on : off,
1252 (stat16 & STAT16_XERR) ? on : off);
1255 /* Check and print error counters if non-zero. */
1256 CV = read_framer(sc, T3CSR_CVHI)<<8;
1257 CV += read_framer(sc, T3CSR_CVLO);
1258 PERR = read_framer(sc, T3CSR_PERR);
1259 CERR = read_framer(sc, T3CSR_CERR);
1260 FERR = read_framer(sc, T3CSR_FERR);
1261 MERR = read_framer(sc, T3CSR_MERR);
1262 FEBE = read_framer(sc, T3CSR_FEBE);
1264 /* CV is invalid during LOS. */
1265 if ((stat16 & STAT16_RLOS)!=0) CV = 0;
1266 /* CERR & FEBE are invalid in M13 mode */
1267 if (sc->config.format == CFG_FORMAT_T3M13) CERR = FEBE = 0;
1268 /* FEBE is invalid during AIS. */
1269 if ((stat16 & STAT16_RAIS)!=0) FEBE = 0;
1270 if (DRIVER_DEBUG && (CV || PERR || CERR || FERR || MERR || FEBE))
1271 printf("%s: CV=%u PERR=%u CERR=%u FERR=%u MERR=%u FEBE=%u\n",
1272 NAME_UNIT, CV, PERR, CERR, FERR, MERR, FEBE);
1274 /* Driver keeps crude link-level error counters (SNMP is better). */
1275 sc->status.cntrs.lcv_errs += CV;
1276 sc->status.cntrs.par_errs += PERR;
1277 sc->status.cntrs.cpar_errs += CERR;
1278 sc->status.cntrs.frm_errs += FERR;
1279 sc->status.cntrs.mfrm_errs += MERR;
1280 sc->status.cntrs.febe_errs += FEBE;
1282 /* Check for FEAC messages (FEAC not defined in M13 mode). */
1283 if (FORMAT_T3CPAR && (stat16 & STAT16_FEAC)) do
1285 feac = read_framer(sc, T3CSR_FEAC_STK);
1286 if ((feac & FEAC_STK_VALID)==0) break;
1287 /* Ignore RxFEACs while a far end loopback has been requested. */
1288 if ((sc->status.snmp.t3.line & TLOOP_FAR_LINE)!=0) continue;
1289 switch (feac & FEAC_STK_FEAC)
1291 case T3BOP_LINE_UP: break;
1292 case T3BOP_LINE_DOWN: break;
1293 case T3BOP_LOOP_DS3:
1295 if (sc->last_FEAC == T3BOP_LINE_DOWN)
1298 printf("%s: Received a 'line loopback deactivate' FEAC msg\n", NAME_UNIT);
1299 clr_mii16_bits(sc, MII16_DS3_LNLBK);
1302 if (sc->last_FEAC == T3BOP_LINE_UP)
1305 printf("%s: Received a 'line loopback activate' FEAC msg\n", NAME_UNIT);
1306 set_mii16_bits(sc, MII16_DS3_LNLBK);
1307 sc->loop_timer = 300;
1314 printf("%s: Received a 'far end LOF' FEAC msg\n", NAME_UNIT);
1320 printf("%s: Received a 'far end IDL' FEAC msg\n", NAME_UNIT);
1326 printf("%s: Received a 'far end AIS' FEAC msg\n", NAME_UNIT);
1332 printf("%s: Received a 'far end LOS' FEAC msg\n", NAME_UNIT);
1338 printf("%s: Received a 'type 0x%02X' FEAC msg\n", NAME_UNIT, feac & FEAC_STK_FEAC);
1342 sc->last_FEAC = feac & FEAC_STK_FEAC;
1343 } while ((feac & FEAC_STK_MORE) != 0);
1344 stat16 &= ~STAT16_FEAC;
1346 /* Send Service-Affecting priority FEAC messages */
1347 if (((sc->last_stat16 ^ stat16) & 0xF0) && (FORMAT_T3CPAR))
1349 /* Transmit continuous FEACs */
1350 write_framer(sc, T3CSR_CTL14,
1351 read_framer(sc, T3CSR_CTL14) & ~CTL14_FEAC10);
1352 if ((stat16 & STAT16_RLOS)!=0)
1353 write_framer(sc, T3CSR_TX_FEAC, 0xC0 + T3BOP_LOS);
1354 else if ((stat16 & STAT16_ROOF)!=0)
1355 write_framer(sc, T3CSR_TX_FEAC, 0xC0 + T3BOP_OOF);
1356 else if ((stat16 & STAT16_RAIS)!=0)
1357 write_framer(sc, T3CSR_TX_FEAC, 0xC0 + T3BOP_AIS);
1358 else if ((stat16 & STAT16_RIDL)!=0)
1359 write_framer(sc, T3CSR_TX_FEAC, 0xC0 + T3BOP_IDLE);
1361 write_framer(sc, T3CSR_TX_FEAC, CTL5_EMODE);
1364 /* Start sending RAI, Remote Alarm Indication. */
1365 if (((stat16 & STAT16_ROOF)!=0) && ((stat16 & STAT16_RLOS)==0) &&
1366 ((sc->last_stat16 & STAT16_ROOF)==0))
1367 write_framer(sc, T3CSR_CTL1, ctl1 &= ~CTL1_XTX);
1368 /* Stop sending RAI, Remote Alarm Indication. */
1369 else if (((stat16 & STAT16_ROOF)==0) && ((sc->last_stat16 & STAT16_ROOF)!=0))
1370 write_framer(sc, T3CSR_CTL1, ctl1 |= CTL1_XTX);
1372 /* Start sending AIS, Alarm Indication Signal */
1373 if (((stat16 & STAT16_RLOS)!=0) && ((sc->last_stat16 & STAT16_RLOS)==0))
1375 set_mii16_bits(sc, MII16_DS3_FRAME);
1376 write_framer(sc, T3CSR_CTL1, ctl1 | CTL1_TXAIS);
1378 /* Stop sending AIS, Alarm Indication Signal */
1379 else if (((stat16 & STAT16_RLOS)==0) && ((sc->last_stat16 & STAT16_RLOS)!=0))
1381 clr_mii16_bits(sc, MII16_DS3_FRAME);
1382 write_framer(sc, T3CSR_CTL1, ctl1 & ~CTL1_TXAIS);
1385 /* Time out loopback requests. */
1386 if (sc->loop_timer != 0)
1387 if (--sc->loop_timer == 0)
1388 if ((mii16 & MII16_DS3_LNLBK)!=0)
1391 printf("%s: Timeout: Loop Down after 300 seconds\n", NAME_UNIT);
1392 clr_mii16_bits(sc, MII16_DS3_LNLBK); /* line loopback off */
1395 /* SNMP error counters */
1396 sc->status.snmp.t3.lcv = CV;
1397 sc->status.snmp.t3.pcv = PERR;
1398 sc->status.snmp.t3.ccv = CERR;
1399 sc->status.snmp.t3.febe = FEBE;
1401 /* SNMP Line Status */
1402 sc->status.snmp.t3.line = 0;
1403 if ((ctl1 & CTL1_XTX)==0) sc->status.snmp.t3.line |= TLINE_TX_RAI;
1404 if (stat16 & STAT16_XERR) sc->status.snmp.t3.line |= TLINE_RX_RAI;
1405 if (ctl1 & CTL1_TXAIS) sc->status.snmp.t3.line |= TLINE_TX_AIS;
1406 if (stat16 & STAT16_RAIS) sc->status.snmp.t3.line |= TLINE_RX_AIS;
1407 if (stat16 & STAT16_ROOF) sc->status.snmp.t3.line |= TLINE_LOF;
1408 if (stat16 & STAT16_RLOS) sc->status.snmp.t3.line |= TLINE_LOS;
1409 if (stat16 & STAT16_SEF) sc->status.snmp.t3.line |= T3LINE_SEF;
1411 /* SNMP Loopback Status */
1412 sc->status.snmp.t3.loop &= ~TLOOP_FAR_LINE;
1413 if (sc->config.loop_back == CFG_LOOP_TULIP)
1414 sc->status.snmp.t3.loop |= TLOOP_NEAR_OTHER;
1415 if (ctl1 & CTL1_3LOOP) sc->status.snmp.t3.loop |= TLOOP_NEAR_INWARD;
1416 if (mii16 & MII16_DS3_TRLBK) sc->status.snmp.t3.loop |= TLOOP_NEAR_OTHER;
1417 if (mii16 & MII16_DS3_LNLBK) sc->status.snmp.t3.loop |= TLOOP_NEAR_LINE;
1418 /*if (ctl12 & CTL12_RTPLOOP) sc->status.snmp.t3.loop |= TLOOP_NEAR_PAYLOAD; */
1420 /* Remember this state until next time. */
1421 sc->last_stat16 = stat16;
1423 /* If an INWARD loopback is in effect, link status is UP */
1424 if (sc->config.loop_back != CFG_LOOP_NONE) /* XXX INWARD ONLY */
1425 link_status = STATUS_UP;
1430 /* IOCTL SYSCALL: can sleep. */
1432 t3_send_dbl_feac(softc_t *sc, int feac1, int feac2)
1437 /* The FEAC transmitter could be sending a continuous */
1438 /* FEAC msg when told to send a double FEAC message. */
1439 /* So save the current state of the FEAC transmitter. */
1440 tx_feac = read_framer(sc, T3CSR_TX_FEAC);
1441 /* Load second FEAC code and stop FEAC transmitter. */
1442 write_framer(sc, T3CSR_TX_FEAC, CTL5_EMODE + feac2);
1443 /* FEAC transmitter sends 10 more FEACs and then stops. */
1444 SLEEP(20000); /* sending one FEAC takes 1700 uSecs */
1445 /* Load first FEAC code and start FEAC transmitter. */
1446 write_framer(sc, T3CSR_DBL_FEAC, CTL13_DFEXEC + feac1);
1447 /* Wait for double FEAC sequence to complete -- about 70 ms. */
1448 for (i=0; i<10; i++) /* max delay 100 ms */
1449 if (read_framer(sc, T3CSR_DBL_FEAC) & CTL13_DFEXEC) SLEEP(10000);
1450 /* Flush received FEACS; don't respond to our own loop cmd! */
1451 while (read_framer(sc, T3CSR_FEAC_STK) & FEAC_STK_VALID) DELAY(1); /* XXX HANG */
1452 /* Restore previous state of the FEAC transmitter. */
1453 /* If it was sending a continous FEAC, it will resume. */
1454 write_framer(sc, T3CSR_TX_FEAC, tx_feac);
1457 /* IOCTL SYSCALL: can sleep. */
1459 t3_ioctl(softc_t *sc, struct ioctl *ioctl)
1465 case IOCTL_SNMP_SEND: /* set opstatus? */
1467 if (sc->config.format != CFG_FORMAT_T3CPAR)
1469 else if (ioctl->data == TSEND_LINE)
1471 sc->status.snmp.t3.loop |= TLOOP_FAR_LINE;
1472 t3_send_dbl_feac(sc, T3BOP_LINE_UP, T3BOP_LOOP_DS3);
1474 else if (ioctl->data == TSEND_RESET)
1476 t3_send_dbl_feac(sc, T3BOP_LINE_DOWN, T3BOP_LOOP_DS3);
1477 sc->status.snmp.t3.loop &= ~TLOOP_FAR_LINE;
1483 case IOCTL_SNMP_LOOP: /* set opstatus = test? */
1485 if (ioctl->data == CFG_LOOP_NONE)
1487 clr_mii16_bits(sc, MII16_DS3_FRAME);
1488 clr_mii16_bits(sc, MII16_DS3_TRLBK);
1489 clr_mii16_bits(sc, MII16_DS3_LNLBK);
1490 write_framer(sc, T3CSR_CTL1,
1491 read_framer(sc, T3CSR_CTL1) & ~CTL1_3LOOP);
1492 write_framer(sc, T3CSR_CTL12,
1493 read_framer(sc, T3CSR_CTL12) & ~(CTL12_RTPLOOP | CTL12_RTPLLEN));
1495 else if (ioctl->data == CFG_LOOP_LINE)
1496 set_mii16_bits(sc, MII16_DS3_LNLBK);
1497 else if (ioctl->data == CFG_LOOP_OTHER)
1498 set_mii16_bits(sc, MII16_DS3_TRLBK);
1499 else if (ioctl->data == CFG_LOOP_INWARD)
1500 write_framer(sc, T3CSR_CTL1,
1501 read_framer(sc, T3CSR_CTL1) | CTL1_3LOOP);
1502 else if (ioctl->data == CFG_LOOP_DUAL)
1504 set_mii16_bits(sc, MII16_DS3_LNLBK);
1505 write_framer(sc, T3CSR_CTL1,
1506 read_framer(sc, T3CSR_CTL1) | CTL1_3LOOP);
1508 else if (ioctl->data == CFG_LOOP_PAYLOAD)
1510 set_mii16_bits(sc, MII16_DS3_FRAME);
1511 write_framer(sc, T3CSR_CTL12,
1512 read_framer(sc, T3CSR_CTL12) | CTL12_RTPLOOP);
1513 write_framer(sc, T3CSR_CTL12,
1514 read_framer(sc, T3CSR_CTL12) | CTL12_RTPLLEN);
1515 DELAY(25); /* at least two frames (22 uS) */
1516 write_framer(sc, T3CSR_CTL12,
1517 read_framer(sc, T3CSR_CTL12) & ~CTL12_RTPLLEN);
1531 /* begin SSI card code */
1533 /* Must not sleep. */
1535 ssi_config(softc_t *sc)
1537 if (sc->status.card_type == 0)
1539 sc->status.card_type = TLP_CSID_SSI;
1540 sc->config.crc_len = CFG_CRC_16;
1541 sc->config.loop_back = CFG_LOOP_NONE;
1542 sc->config.tx_clk_src = CFG_CLKMUX_ST;
1543 sc->config.dte_dce = CFG_DTE;
1544 sc->config.synth.n = 51; /* 1.536 MHz */
1545 sc->config.synth.m = 83;
1546 sc->config.synth.v = 1;
1547 sc->config.synth.x = 1;
1548 sc->config.synth.r = 1;
1549 sc->config.synth.prescale = 4;
1552 /* Disable the TX clock driver while programming the oscillator. */
1553 clr_gpio_bits(sc, GPIO_SSI_DCE);
1554 make_gpio_output(sc, GPIO_SSI_DCE);
1556 /* Program the synthesized oscillator. */
1557 write_synth(sc, &sc->config.synth);
1559 /* Set DTE/DCE mode. */
1560 /* If DTE mode then DCD & TXC are received. */
1561 /* If DCE mode then DCD & TXC are driven. */
1562 /* Boards with MII rev=4.0 don't drive DCD. */
1563 if (sc->config.dte_dce == CFG_DCE)
1564 set_gpio_bits(sc, GPIO_SSI_DCE);
1566 clr_gpio_bits(sc, GPIO_SSI_DCE);
1567 make_gpio_output(sc, GPIO_SSI_DCE);
1569 /* Set CRC length. */
1570 if (sc->config.crc_len == CFG_CRC_32)
1571 set_mii16_bits(sc, MII16_SSI_CRC32);
1573 clr_mii16_bits(sc, MII16_SSI_CRC32);
1575 /* Loop towards host thru cable drivers and receivers. */
1576 /* Asserts DCD at the far end of a null modem cable. */
1577 if (sc->config.loop_back == CFG_LOOP_PINS)
1578 set_mii16_bits(sc, MII16_SSI_LOOP);
1580 clr_mii16_bits(sc, MII16_SSI_LOOP);
1582 /* Assert pin LL in modem conn: ask modem for local loop. */
1583 /* Asserts TM at the far end of a null modem cable. */
1584 if (sc->config.loop_back == CFG_LOOP_LL)
1585 set_mii16_bits(sc, MII16_SSI_LL);
1587 clr_mii16_bits(sc, MII16_SSI_LL);
1589 /* Assert pin RL in modem conn: ask modem for remote loop. */
1590 if (sc->config.loop_back == CFG_LOOP_RL)
1591 set_mii16_bits(sc, MII16_SSI_RL);
1593 clr_mii16_bits(sc, MII16_SSI_RL);
1597 ssi_ident(softc_t *sc)
1599 printf(", LTC1343/44");
1602 /* Called once a second; must not sleep. */
1604 ssi_watchdog(softc_t *sc)
1607 u_int16_t mii16 = read_mii(sc, 16) & MII16_SSI_MODEM;
1608 int link_status = STATUS_UP;
1610 /* Software is alive. */
1611 led_inv(sc, MII16_SSI_LED_UL);
1613 /* Check the transmit clock. */
1614 if (sc->status.tx_speed == 0)
1616 led_on(sc, MII16_SSI_LED_UR);
1617 link_status = STATUS_DOWN;
1620 led_off(sc, MII16_SSI_LED_UR);
1622 /* Check the external cable. */
1623 cable = read_mii(sc, 17);
1624 cable = cable & MII17_SSI_CABLE_MASK;
1625 cable = cable >> MII17_SSI_CABLE_SHIFT;
1628 led_off(sc, MII16_SSI_LED_LL); /* no cable */
1629 link_status = STATUS_DOWN;
1632 led_on(sc, MII16_SSI_LED_LL);
1634 /* The unit at the other end of the cable is ready if: */
1635 /* DTE mode and DCD pin is asserted */
1636 /* DCE mode and DSR pin is asserted */
1637 if (((sc->config.dte_dce == CFG_DTE) && ((mii16 & MII16_SSI_DCD)==0)) ||
1638 ((sc->config.dte_dce == CFG_DCE) && ((mii16 & MII16_SSI_DSR)==0)))
1640 led_off(sc, MII16_SSI_LED_LR);
1641 link_status = STATUS_DOWN;
1644 led_on(sc, MII16_SSI_LED_LR);
1646 if (DRIVER_DEBUG && (cable != sc->status.cable_type))
1647 printf("%s: SSI cable type changed to '%s'\n",
1648 NAME_UNIT, ssi_cables[cable]);
1649 sc->status.cable_type = cable;
1651 /* Print the modem control signals if they changed. */
1652 if ((DRIVER_DEBUG) && (mii16 != sc->last_mii16))
1654 char *on = "ON ", *off = "OFF";
1655 printf("%s: DTR=%s DSR=%s RTS=%s CTS=%s DCD=%s RI=%s LL=%s RL=%s TM=%s\n",
1657 (mii16 & MII16_SSI_DTR) ? on : off,
1658 (mii16 & MII16_SSI_DSR) ? on : off,
1659 (mii16 & MII16_SSI_RTS) ? on : off,
1660 (mii16 & MII16_SSI_CTS) ? on : off,
1661 (mii16 & MII16_SSI_DCD) ? on : off,
1662 (mii16 & MII16_SSI_RI) ? on : off,
1663 (mii16 & MII16_SSI_LL) ? on : off,
1664 (mii16 & MII16_SSI_RL) ? on : off,
1665 (mii16 & MII16_SSI_TM) ? on : off);
1668 /* SNMP one-second report */
1669 sc->status.snmp.ssi.sigs = mii16 & MII16_SSI_MODEM;
1671 /* Remember this state until next time. */
1672 sc->last_mii16 = mii16;
1674 /* If a loop back is in effect, link status is UP */
1675 if (sc->config.loop_back != CFG_LOOP_NONE)
1676 link_status = STATUS_UP;
1681 /* IOCTL SYSCALL: can sleep (but doesn't). */
1683 ssi_ioctl(softc_t *sc, struct ioctl *ioctl)
1687 if (ioctl->cmd == IOCTL_SNMP_SIGS)
1689 u_int16_t mii16 = read_mii(sc, 16);
1690 mii16 &= ~MII16_SSI_MODEM;
1691 mii16 |= (MII16_SSI_MODEM & ioctl->data);
1692 write_mii(sc, 16, mii16);
1694 else if (ioctl->cmd == IOCTL_SET_STATUS)
1696 if (ioctl->data != 0)
1697 set_mii16_bits(sc, (MII16_SSI_DTR | MII16_SSI_RTS | MII16_SSI_DCD));
1699 clr_mii16_bits(sc, (MII16_SSI_DTR | MII16_SSI_RTS | MII16_SSI_DCD));
1707 /* begin T1E1 card code */
1709 /* Must not sleep. */
1711 t1_config(softc_t *sc)
1714 u_int8_t pulse, lbo, gain;
1716 if (sc->status.card_type == 0)
1718 sc->status.card_type = TLP_CSID_T1E1;
1719 sc->config.crc_len = CFG_CRC_16;
1720 sc->config.loop_back = CFG_LOOP_NONE;
1721 sc->config.tx_clk_src = CFG_CLKMUX_INT;
1722 sc->config.format = CFG_FORMAT_T1ESF;
1723 sc->config.cable_len = 10;
1724 sc->config.time_slots = 0x01FFFFFE;
1725 sc->config.tx_pulse = CFG_PULSE_AUTO;
1726 sc->config.rx_gain = CFG_GAIN_AUTO;
1727 sc->config.tx_lbo = CFG_LBO_AUTO;
1729 /* Bt8370 occasionally powers up in a loopback mode. */
1730 /* Data sheet says zero LOOP reg and do a s/w reset. */
1731 write_framer(sc, Bt8370_LOOP, 0x00); /* no loopback */
1732 write_framer(sc, Bt8370_CR0, 0x80); /* s/w reset */
1733 for (i=0; i<10; i++) /* max delay 10 ms */
1734 if (read_framer(sc, Bt8370_CR0) & 0x80) DELAY(1000);
1737 /* Set CRC length. */
1738 if (sc->config.crc_len == CFG_CRC_32)
1739 set_mii16_bits(sc, MII16_T1_CRC32);
1741 clr_mii16_bits(sc, MII16_T1_CRC32);
1743 /* Invert HDLC payload data in SF/AMI mode. */
1744 /* HDLC stuff bits satisfy T1 pulse density. */
1746 set_mii16_bits(sc, MII16_T1_INVERT);
1748 clr_mii16_bits(sc, MII16_T1_INVERT);
1750 /* Set the transmitter output impedance. */
1751 if (FORMAT_E1ANY) set_mii16_bits(sc, MII16_T1_Z);
1753 /* 001:CR0 -- Control Register 0 - T1/E1 and frame format */
1754 write_framer(sc, Bt8370_CR0, sc->config.format);
1756 /* 002:JAT_CR -- Jitter Attenuator Control Register */
1757 if (sc->config.tx_clk_src == CFG_CLKMUX_RT) /* loop timing */
1758 write_framer(sc, Bt8370_JAT_CR, 0xA3); /* JAT in RX path */
1760 { /* 64-bit elastic store; free-running JCLK and CLADO */
1761 write_framer(sc, Bt8370_JAT_CR, 0x4B); /* assert jcenter */
1762 write_framer(sc, Bt8370_JAT_CR, 0x43); /* release jcenter */
1765 /* 00C-013:IERn -- Interrupt Enable Registers */
1766 for (i=Bt8370_IER7; i<=Bt8370_IER0; i++)
1767 write_framer(sc, i, 0); /* no interrupts; polled */
1769 /* 014:LOOP -- loopbacks */
1770 if (sc->config.loop_back == CFG_LOOP_PAYLOAD)
1771 write_framer(sc, Bt8370_LOOP, LOOP_PAYLOAD);
1772 else if (sc->config.loop_back == CFG_LOOP_LINE)
1773 write_framer(sc, Bt8370_LOOP, LOOP_LINE);
1774 else if (sc->config.loop_back == CFG_LOOP_OTHER)
1775 write_framer(sc, Bt8370_LOOP, LOOP_ANALOG);
1776 else if (sc->config.loop_back == CFG_LOOP_INWARD)
1777 write_framer(sc, Bt8370_LOOP, LOOP_FRAMER);
1778 else if (sc->config.loop_back == CFG_LOOP_DUAL)
1779 write_framer(sc, Bt8370_LOOP, LOOP_DUAL);
1781 write_framer(sc, Bt8370_LOOP, 0x00); /* no loopback */
1783 /* 015:DL3_TS -- Data Link 3 */
1784 write_framer(sc, Bt8370_DL3_TS, 0x00); /* disabled */
1786 /* 018:PIO -- Programmable I/O */
1787 write_framer(sc, Bt8370_PIO, 0xFF); /* all pins are outputs */
1789 /* 019:POE -- Programmable Output Enable */
1790 write_framer(sc, Bt8370_POE, 0x00); /* all outputs are enabled */
1792 /* 01A;CMUX -- Clock Input Mux */
1793 if (sc->config.tx_clk_src == CFG_CLKMUX_EXT)
1794 write_framer(sc, Bt8370_CMUX, 0x0C); /* external timing */
1796 write_framer(sc, Bt8370_CMUX, 0x0F); /* internal timing */
1798 /* 020:LIU_CR -- Line Interface Unit Config Register */
1799 write_framer(sc, Bt8370_LIU_CR, 0xC1); /* reset LIU, squelch */
1801 /* 022:RLIU_CR -- RX Line Interface Unit Config Reg */
1802 /* Errata sheet says don't use freeze-short, but we do anyway! */
1803 write_framer(sc, Bt8370_RLIU_CR, 0xB1); /* AGC=2048, Long Eye */
1805 /* Select Rx sensitivity based on cable length. */
1806 if ((gain = sc->config.rx_gain) == CFG_GAIN_AUTO)
1808 if (sc->config.cable_len > 2000)
1809 gain = CFG_GAIN_EXTEND;
1810 else if (sc->config.cable_len > 1000)
1811 gain = CFG_GAIN_LONG;
1812 else if (sc->config.cable_len > 100)
1813 gain = CFG_GAIN_MEDIUM;
1815 gain = CFG_GAIN_SHORT;
1818 /* 024:VGA_MAX -- Variable Gain Amplifier Max gain */
1819 write_framer(sc, Bt8370_VGA_MAX, gain);
1821 /* 028:PRE_EQ -- Pre Equalizer */
1822 if (gain == CFG_GAIN_EXTEND)
1823 write_framer(sc, Bt8370_PRE_EQ, 0xE6); /* ON; thresh 6 */
1825 write_framer(sc, Bt8370_PRE_EQ, 0xA6); /* OFF; thresh 6 */
1827 /* 038-03C:GAINn -- RX Equalizer gain thresholds */
1828 write_framer(sc, Bt8370_GAIN0, 0x24);
1829 write_framer(sc, Bt8370_GAIN1, 0x28);
1830 write_framer(sc, Bt8370_GAIN2, 0x2C);
1831 write_framer(sc, Bt8370_GAIN3, 0x30);
1832 write_framer(sc, Bt8370_GAIN4, 0x34);
1834 /* 040:RCR0 -- Receiver Control Register 0 */
1836 write_framer(sc, Bt8370_RCR0, 0x05); /* B8ZS, 2/5 FErrs */
1837 else if (FORMAT_T1SF)
1838 write_framer(sc, Bt8370_RCR0, 0x84); /* AMI, 2/5 FErrs */
1839 else if (FORMAT_E1NONE)
1840 write_framer(sc, Bt8370_RCR0, 0x41); /* HDB3, rabort */
1841 else if (FORMAT_E1CRC)
1842 write_framer(sc, Bt8370_RCR0, 0x09); /* HDB3, 3 FErrs or 915 CErrs */
1843 else /* E1 no CRC */
1844 write_framer(sc, Bt8370_RCR0, 0x19); /* HDB3, 3 FErrs */
1846 /* 041:RPATT -- Receive Test Pattern configuration */
1847 write_framer(sc, Bt8370_RPATT, 0x3E); /* looking for framed QRSS */
1849 /* 042:RLB -- Receive Loop Back code detector config */
1850 write_framer(sc, Bt8370_RLB, 0x09); /* 6 bits down; 5 bits up */
1852 /* 043:LBA -- Loop Back Activate code */
1853 write_framer(sc, Bt8370_LBA, 0x08); /* 10000 10000 10000 ... */
1855 /* 044:LBD -- Loop Back Deactivate code */
1856 write_framer(sc, Bt8370_LBD, 0x24); /* 100100 100100 100100 ... */
1858 /* 045:RALM -- Receive Alarm signal configuration */
1859 write_framer(sc, Bt8370_RALM, 0x0C); /* yel_intg rlof_intg */
1861 /* 046:LATCH -- Alarm/Error/Counter Latch register */
1862 write_framer(sc, Bt8370_LATCH, 0x1F); /* stop_cnt latch_{cnt,err,alm} */
1864 /* Select Pulse Shape based on cable length (T1 only). */
1865 if ((pulse = sc->config.tx_pulse) == CFG_PULSE_AUTO)
1869 if (sc->config.cable_len > 200)
1870 pulse = CFG_PULSE_T1CSU;
1871 else if (sc->config.cable_len > 160)
1872 pulse = CFG_PULSE_T1DSX4;
1873 else if (sc->config.cable_len > 120)
1874 pulse = CFG_PULSE_T1DSX3;
1875 else if (sc->config.cable_len > 80)
1876 pulse = CFG_PULSE_T1DSX2;
1877 else if (sc->config.cable_len > 40)
1878 pulse = CFG_PULSE_T1DSX1;
1880 pulse = CFG_PULSE_T1DSX0;
1883 pulse = CFG_PULSE_E1TWIST;
1886 /* Select Line Build Out based on cable length (T1CSU only). */
1887 if ((lbo = sc->config.tx_lbo) == CFG_LBO_AUTO)
1889 if (pulse == CFG_PULSE_T1CSU)
1891 if (sc->config.cable_len > 1500)
1893 else if (sc->config.cable_len > 1000)
1895 else if (sc->config.cable_len > 500)
1904 /* 068:TLIU_CR -- Transmit LIU Control Register */
1905 write_framer(sc, Bt8370_TLIU_CR, (0x40 | (lbo & 0x30) | (pulse & 0x0E)));
1907 /* 070:TCR0 -- Transmit Framer Configuration */
1908 write_framer(sc, Bt8370_TCR0, sc->config.format>>1);
1910 /* 071:TCR1 -- Transmitter Configuration */
1912 write_framer(sc, Bt8370_TCR1, 0x43); /* tabort, AMI PDV enforced */
1914 write_framer(sc, Bt8370_TCR1, 0x41); /* tabort, B8ZS or HDB3 */
1916 /* 072:TFRM -- Transmit Frame format MYEL YEL MF FE CRC FBIT */
1917 if (sc->config.format == CFG_FORMAT_T1ESF)
1918 write_framer(sc, Bt8370_TFRM, 0x0B); /* - YEL MF - CRC FBIT */
1919 else if (sc->config.format == CFG_FORMAT_T1SF)
1920 write_framer(sc, Bt8370_TFRM, 0x19); /* - YEL MF - - FBIT */
1921 else if (sc->config.format == CFG_FORMAT_E1FAS)
1922 write_framer(sc, Bt8370_TFRM, 0x11); /* - YEL - - - FBIT */
1923 else if (sc->config.format == CFG_FORMAT_E1FASCRC)
1924 write_framer(sc, Bt8370_TFRM, 0x1F); /* - YEL MF FE CRC FBIT */
1925 else if (sc->config.format == CFG_FORMAT_E1FASCAS)
1926 write_framer(sc, Bt8370_TFRM, 0x31); /* MYEL YEL - - - FBIT */
1927 else if (sc->config.format == CFG_FORMAT_E1FASCRCCAS)
1928 write_framer(sc, Bt8370_TFRM, 0x3F); /* MYEL YEL MF FE CRC FBIT */
1929 else if (sc->config.format == CFG_FORMAT_E1NONE)
1930 write_framer(sc, Bt8370_TFRM, 0x00); /* NO FRAMING BITS AT ALL! */
1932 /* 073:TERROR -- Transmit Error Insert */
1933 write_framer(sc, Bt8370_TERROR, 0x00); /* no errors, please! */
1935 /* 074:TMAN -- Transmit Manual Sa-byte/FEBE configuration */
1936 write_framer(sc, Bt8370_TMAN, 0x00); /* none */
1938 /* 075:TALM -- Transmit Alarm Signal Configuration */
1940 write_framer(sc, Bt8370_TALM, 0x38); /* auto_myel auto_yel auto_ais */
1941 else if (FORMAT_T1ANY)
1942 write_framer(sc, Bt8370_TALM, 0x18); /* auto_yel auto_ais */
1944 /* 076:TPATT -- Transmit Test Pattern Configuration */
1945 write_framer(sc, Bt8370_TPATT, 0x00); /* disabled */
1947 /* 077:TLB -- Transmit Inband Loopback Code Configuration */
1948 write_framer(sc, Bt8370_TLB, 0x00); /* disabled */
1950 /* 090:CLAD_CR -- Clack Rate Adapter Configuration */
1952 write_framer(sc, Bt8370_CLAD_CR, 0x06); /* loop filter gain 1/2^6 */
1954 write_framer(sc, Bt8370_CLAD_CR, 0x08); /* loop filter gain 1/2^8 */
1956 /* 091:CSEL -- CLAD frequency Select */
1958 write_framer(sc, Bt8370_CSEL, 0x55); /* 1544 kHz */
1960 write_framer(sc, Bt8370_CSEL, 0x11); /* 2048 kHz */
1962 /* 092:CPHASE -- CLAD Phase detector */
1964 write_framer(sc, Bt8370_CPHASE, 0x22); /* phase compare @ 386 kHz */
1966 write_framer(sc, Bt8370_CPHASE, 0x00); /* phase compare @ 2048 kHz */
1968 if (FORMAT_T1ESF) /* BOP & PRM are enabled in T1ESF mode only. */
1970 /* 0A0:BOP -- Bit Oriented Protocol messages */
1971 write_framer(sc, Bt8370_BOP, RBOP_25 | TBOP_OFF);
1972 /* 0A4:DL1_TS -- Data Link 1 Time Slot Enable */
1973 write_framer(sc, Bt8370_DL1_TS, 0x40); /* FDL bits in odd frames */
1974 /* 0A6:DL1_CTL -- Data Link 1 Control */
1975 write_framer(sc, Bt8370_DL1_CTL, 0x03); /* FCS mode, TX on, RX on */
1976 /* 0A7:RDL1_FFC -- Rx Data Link 1 Fifo Fill Control */
1977 write_framer(sc, Bt8370_RDL1_FFC, 0x30); /* assert "near full" at 48 */
1978 /* 0AA:PRM -- Performance Report Messages */
1979 write_framer(sc, Bt8370_PRM, 0x80);
1982 /* 0D0:SBI_CR -- System Bus Interface Configuration Register */
1984 write_framer(sc, Bt8370_SBI_CR, 0x47); /* 1.544 with 24 TS +Fbits */
1986 write_framer(sc, Bt8370_SBI_CR, 0x46); /* 2.048 with 32 TS */
1988 /* 0D1:RSB_CR -- Receive System Bus Configuration Register */
1989 /* Change RINDO & RFSYNC on falling edge of RSBCLKI. */
1990 write_framer(sc, Bt8370_RSB_CR, 0x70);
1992 /* 0D2,0D3:RSYNC_{TS,BIT} -- Receive frame Sync offset */
1993 write_framer(sc, Bt8370_RSYNC_BIT, 0x00);
1994 write_framer(sc, Bt8370_RSYNC_TS, 0x00);
1996 /* 0D4:TSB_CR -- Transmit System Bus Configuration Register */
1997 /* Change TINDO & TFSYNC on falling edge of TSBCLKI. */
1998 write_framer(sc, Bt8370_TSB_CR, 0x30);
2000 /* 0D5,0D6:TSYNC_{TS,BIT} -- Transmit frame Sync offset */
2001 write_framer(sc, Bt8370_TSYNC_BIT, 0x00);
2002 write_framer(sc, Bt8370_TSYNC_TS, 0x00);
2004 /* 0D7:RSIG_CR -- Receive SIGnalling Configuratin Register */
2005 write_framer(sc, Bt8370_RSIG_CR, 0x00);
2007 /* Assign and configure 64Kb TIME SLOTS. */
2008 /* TS24..TS1 must be assigned for T1, TS31..TS0 for E1. */
2009 /* Timeslots with no user data have RINDO and TINDO off. */
2010 for (i=0; i<32; i++)
2012 /* 0E0-0FF:SBCn -- System Bus Per-Channel Control */
2013 if (FORMAT_T1ANY && (i==0 || i>24))
2014 write_framer(sc, Bt8370_SBCn +i, 0x00); /* not assigned in T1 mode */
2015 else if (FORMAT_E1ANY && (i==0) && !FORMAT_E1NONE)
2016 write_framer(sc, Bt8370_SBCn +i, 0x01); /* assigned, TS0 o/h bits */
2017 else if (FORMAT_E1CAS && (i==16) && !FORMAT_E1NONE)
2018 write_framer(sc, Bt8370_SBCn +i, 0x01); /* assigned, TS16 o/h bits */
2019 else if ((sc->config.time_slots & (1<<i)) != 0)
2020 write_framer(sc, Bt8370_SBCn +i, 0x0D); /* assigned, RINDO, TINDO */
2022 write_framer(sc, Bt8370_SBCn +i, 0x01); /* assigned, idle */
2024 /* 100-11F:TPCn -- Transmit Per-Channel Control */
2025 if (FORMAT_E1CAS && (i==0))
2026 write_framer(sc, Bt8370_TPCn +i, 0x30); /* tidle, sig=0000 (MAS) */
2027 else if (FORMAT_E1CAS && (i==16))
2028 write_framer(sc, Bt8370_TPCn +i, 0x3B); /* tidle, sig=1011 (XYXX) */
2029 else if ((sc->config.time_slots & (1<<i)) == 0)
2030 write_framer(sc, Bt8370_TPCn +i, 0x20); /* tidle: use TSLIP_LOn */
2032 write_framer(sc, Bt8370_TPCn +i, 0x00); /* nothing special */
2034 /* 140-15F:TSLIP_LOn -- Transmit PCM Slip Buffer */
2035 write_framer(sc, Bt8370_TSLIP_LOn +i, 0x7F); /* idle chan data */
2036 /* 180-19F:RPCn -- Receive Per-Channel Control */
2037 write_framer(sc, Bt8370_RPCn +i, 0x00); /* nothing special */
2040 /* Enable transmitter output drivers. */
2041 set_mii16_bits(sc, MII16_T1_XOE);
2045 t1_ident(softc_t *sc)
2047 printf(", Bt837%x rev %x",
2048 read_framer(sc, Bt8370_DID)>>4,
2049 read_framer(sc, Bt8370_DID)&0x0F);
2052 /* Called once a second; must not sleep. */
2054 t1_watchdog(softc_t *sc)
2056 u_int16_t LCV = 0, FERR = 0, CRC = 0, FEBE = 0;
2057 u_int8_t alm1, alm3, loop, isr0;
2058 int link_status = STATUS_UP;
2061 /* Read the alarm registers */
2062 alm1 = read_framer(sc, Bt8370_ALM1);
2063 alm3 = read_framer(sc, Bt8370_ALM3);
2064 loop = read_framer(sc, Bt8370_LOOP);
2065 isr0 = read_framer(sc, Bt8370_ISR0);
2067 /* Always ignore the SIGFRZ alarm bit, */
2068 alm1 &= ~ALM1_SIGFRZ;
2069 if (FORMAT_T1ANY) /* ignore RYEL in T1 modes */
2071 else if (FORMAT_E1NONE) /* ignore all alarms except LOS */
2074 /* Software is alive. */
2075 led_inv(sc, MII16_T1_LED_GRN);
2077 /* Receiving Alarm Indication Signal (AIS). */
2078 if ((alm1 & ALM1_RAIS)!=0) /* receiving ais */
2079 led_on(sc, MII16_T1_LED_BLU);
2080 else if ((alm1 & ALM1_RLOS)!=0) /* sending ais */
2081 led_inv(sc, MII16_T1_LED_BLU);
2083 led_off(sc, MII16_T1_LED_BLU);
2085 /* Receiving Remote Alarm Indication (RAI). */
2086 if ((alm1 & (ALM1_RMYEL | ALM1_RYEL))!=0) /* receiving rai */
2087 led_on(sc, MII16_T1_LED_YEL);
2088 else if ((alm1 & ALM1_RLOF)!=0) /* sending rai */
2089 led_inv(sc, MII16_T1_LED_YEL);
2091 led_off(sc, MII16_T1_LED_YEL);
2093 /* If any alarm bits are set then the link is 'down'. */
2094 /* The bad bits are: rmyel ryel rais ralos rlos rlof. */
2095 /* Some alarm bits have been masked by this point. */
2096 if (alm1 != 0) link_status = STATUS_DOWN;
2098 /* Declare local Red Alarm if the link is down. */
2099 if (link_status == STATUS_DOWN)
2100 led_on(sc, MII16_T1_LED_RED);
2101 else if (sc->loop_timer != 0) /* loopback is active */
2102 led_inv(sc, MII16_T1_LED_RED);
2104 led_off(sc, MII16_T1_LED_RED);
2106 /* Print latched error bits if they changed. */
2107 if ((DRIVER_DEBUG) && (alm1 != sc->last_alm1))
2109 char *on = "ON ", *off = "OFF";
2110 printf("%s: RLOF=%s RLOS=%s RALOS=%s RAIS=%s RYEL=%s RMYEL=%s\n",
2112 (alm1 & ALM1_RLOF) ? on : off,
2113 (alm1 & ALM1_RLOS) ? on : off,
2114 (alm1 & ALM1_RALOS) ? on : off,
2115 (alm1 & ALM1_RAIS) ? on : off,
2116 (alm1 & ALM1_RYEL) ? on : off,
2117 (alm1 & ALM1_RMYEL) ? on : off);
2120 /* Check and print error counters if non-zero. */
2121 LCV = read_framer(sc, Bt8370_LCV_LO) +
2122 (read_framer(sc, Bt8370_LCV_HI)<<8);
2124 FERR = read_framer(sc, Bt8370_FERR_LO) +
2125 (read_framer(sc, Bt8370_FERR_HI)<<8);
2126 if (FORMAT_E1CRC || FORMAT_T1ESF)
2127 CRC = read_framer(sc, Bt8370_CRC_LO) +
2128 (read_framer(sc, Bt8370_CRC_HI)<<8);
2130 FEBE = read_framer(sc, Bt8370_FEBE_LO) +
2131 (read_framer(sc, Bt8370_FEBE_HI)<<8);
2132 /* Only LCV is valid if Out-Of-Frame */
2133 if (FORMAT_E1NONE) FERR = CRC = FEBE = 0;
2134 if ((DRIVER_DEBUG) && (LCV || FERR || CRC || FEBE))
2135 printf("%s: LCV=%u FERR=%u CRC=%u FEBE=%u\n",
2136 NAME_UNIT, LCV, FERR, CRC, FEBE);
2138 /* Driver keeps crude link-level error counters (SNMP is better). */
2139 sc->status.cntrs.lcv_errs += LCV;
2140 sc->status.cntrs.frm_errs += FERR;
2141 sc->status.cntrs.crc_errs += CRC;
2142 sc->status.cntrs.febe_errs += FEBE;
2144 /* Check for BOP messages in the ESF Facility Data Link. */
2145 if ((FORMAT_T1ESF) && (read_framer(sc, Bt8370_ISR1) & 0x80))
2147 u_int8_t bop_code = read_framer(sc, Bt8370_RBOP) & 0x3F;
2153 if ((DRIVER_DEBUG) && ((sc->last_alm1 & ALM1_RMYEL)==0))
2154 printf("%s: Receiving a 'yellow alarm' BOP msg\n", NAME_UNIT);
2160 printf("%s: Received a 'line loopback activate' BOP msg\n", NAME_UNIT);
2161 write_framer(sc, Bt8370_LOOP, LOOP_LINE);
2162 sc->loop_timer = 305;
2165 case T1BOP_LINE_DOWN:
2168 printf("%s: Received a 'line loopback deactivate' BOP msg\n", NAME_UNIT);
2169 write_framer(sc, Bt8370_LOOP,
2170 read_framer(sc, Bt8370_LOOP) & ~LOOP_LINE);
2177 printf("%s: Received a 'payload loopback activate' BOP msg\n", NAME_UNIT);
2178 write_framer(sc, Bt8370_LOOP, LOOP_PAYLOAD);
2179 sc->loop_timer = 305;
2182 case T1BOP_PAY_DOWN:
2185 printf("%s: Received a 'payload loopback deactivate' BOP msg\n", NAME_UNIT);
2186 write_framer(sc, Bt8370_LOOP,
2187 read_framer(sc, Bt8370_LOOP) & ~LOOP_PAYLOAD);
2194 printf("%s: Received a type 0x%02X BOP msg\n", NAME_UNIT, bop_code);
2200 /* Check for HDLC pkts in the ESF Facility Data Link. */
2201 if ((FORMAT_T1ESF) && (read_framer(sc, Bt8370_ISR2) & 0x70))
2203 /* while (not fifo-empty && not start-of-msg) flush fifo */
2204 while ((read_framer(sc, Bt8370_RDL1_STAT) & 0x0C) == 0)
2205 read_framer(sc, Bt8370_RDL1);
2206 /* If (not fifo-empty), then begin processing fifo contents. */
2207 if ((read_framer(sc, Bt8370_RDL1_STAT) & 0x0C) == 0x08)
2210 u_int8_t stat = read_framer(sc, Bt8370_RDL1);
2211 sc->status.cntrs.fdl_pkts++;
2212 for (i=0; i<(stat & 0x3F); i++)
2213 msg[i] = read_framer(sc, Bt8370_RDL1);
2214 /* Is this FDL message a T1.403 performance report? */
2215 if (((stat & 0x3F)==11) &&
2216 ((msg[0]==0x38) || (msg[0]==0x3A)) &&
2217 (msg[1]==1) && (msg[2]==3))
2218 /* Copy 4 PRs from FDL pkt to SNMP struct. */
2219 memcpy(sc->status.snmp.t1.prm, msg+3, 8);
2223 /* Check for inband loop up/down commands. */
2226 u_int8_t isr6 = read_framer(sc, Bt8370_ISR6);
2227 u_int8_t alarm2 = read_framer(sc, Bt8370_ALM2);
2228 u_int8_t tlb = read_framer(sc, Bt8370_TLB);
2230 /* Inband Code == Loop Up && On Transition && Inband Tx Inactive */
2231 if ((isr6 & 0x40) && (alarm2 & 0x40) && ((tlb & 1)==0))
2232 { /* CSU loop up is 10000 10000 ... */
2234 printf("%s: Received a 'CSU Loop Up' inband msg\n", NAME_UNIT);
2235 write_framer(sc, Bt8370_LOOP, LOOP_LINE); /* Loop up */
2236 sc->loop_timer = 305;
2238 /* Inband Code == Loop Down && On Transition && Inband Tx Inactive */
2239 if ((isr6 & 0x80) && (alarm2 & 0x80) && ((tlb & 1)==0))
2240 { /* CSU loop down is 100 100 100 ... */
2242 printf("%s: Received a 'CSU Loop Down' inband msg\n", NAME_UNIT);
2243 write_framer(sc, Bt8370_LOOP,
2244 read_framer(sc, Bt8370_LOOP) & ~LOOP_LINE); /* loop down */
2249 /* Manually send Yellow Alarm BOP msgs. */
2252 u_int8_t isr7 = read_framer(sc, Bt8370_ISR7);
2254 if ((isr7 & 0x02) && (alm1 & 0x02)) /* RLOF on-transition */
2255 { /* Start sending continuous Yellow Alarm BOP messages. */
2256 write_framer(sc, Bt8370_BOP, RBOP_25 | TBOP_CONT);
2257 write_framer(sc, Bt8370_TBOP, 0x00); /* send BOP; order matters */
2259 else if ((isr7 & 0x02) && ((alm1 & 0x02)==0)) /* RLOF off-transition */
2260 { /* Stop sending continuous Yellow Alarm BOP messages. */
2261 write_framer(sc, Bt8370_BOP, RBOP_25 | TBOP_OFF);
2265 /* Time out loopback requests. */
2266 if (sc->loop_timer != 0)
2267 if (--sc->loop_timer == 0)
2271 printf("%s: Timeout: Loop Down after 300 seconds\n", NAME_UNIT);
2272 write_framer(sc, Bt8370_LOOP, loop & ~(LOOP_PAYLOAD | LOOP_LINE));
2275 /* RX Test Pattern status */
2276 if ((DRIVER_DEBUG) && (isr0 & 0x10))
2277 printf("%s: RX Test Pattern Sync\n", NAME_UNIT);
2279 /* SNMP Error Counters */
2280 sc->status.snmp.t1.lcv = LCV;
2281 sc->status.snmp.t1.fe = FERR;
2282 sc->status.snmp.t1.crc = CRC;
2283 sc->status.snmp.t1.febe = FEBE;
2285 /* SNMP Line Status */
2286 sc->status.snmp.t1.line = 0;
2287 if (alm1 & ALM1_RMYEL) sc->status.snmp.t1.line |= TLINE_RX_RAI;
2288 if (alm1 & ALM1_RYEL) sc->status.snmp.t1.line |= TLINE_RX_RAI;
2289 if (alm1 & ALM1_RLOF) sc->status.snmp.t1.line |= TLINE_TX_RAI;
2290 if (alm1 & ALM1_RAIS) sc->status.snmp.t1.line |= TLINE_RX_AIS;
2291 if (alm1 & ALM1_RLOS) sc->status.snmp.t1.line |= TLINE_TX_AIS;
2292 if (alm1 & ALM1_RLOF) sc->status.snmp.t1.line |= TLINE_LOF;
2293 if (alm1 & ALM1_RLOS) sc->status.snmp.t1.line |= TLINE_LOS;
2294 if (alm3 & ALM3_RMAIS) sc->status.snmp.t1.line |= T1LINE_RX_TS16_AIS;
2295 if (alm3 & ALM3_SRED) sc->status.snmp.t1.line |= T1LINE_TX_TS16_LOMF;
2296 if (alm3 & ALM3_SEF) sc->status.snmp.t1.line |= T1LINE_SEF;
2297 if (isr0 & 0x10) sc->status.snmp.t1.line |= T1LINE_RX_TEST;
2298 if ((alm1 & ALM1_RMYEL) && (FORMAT_E1CAS))
2299 sc->status.snmp.t1.line |= T1LINE_RX_TS16_LOMF;
2301 /* SNMP Loopback Status */
2302 sc->status.snmp.t1.loop &= ~(TLOOP_FAR_LINE | TLOOP_FAR_PAYLOAD);
2303 if (sc->config.loop_back == CFG_LOOP_TULIP)
2304 sc->status.snmp.t1.loop |= TLOOP_NEAR_OTHER;
2305 if (loop & LOOP_PAYLOAD) sc->status.snmp.t1.loop |= TLOOP_NEAR_PAYLOAD;
2306 if (loop & LOOP_LINE) sc->status.snmp.t1.loop |= TLOOP_NEAR_LINE;
2307 if (loop & LOOP_ANALOG) sc->status.snmp.t1.loop |= TLOOP_NEAR_OTHER;
2308 if (loop & LOOP_FRAMER) sc->status.snmp.t1.loop |= TLOOP_NEAR_INWARD;
2310 /* Remember this state until next time. */
2311 sc->last_alm1 = alm1;
2313 /* If an INWARD loopback is in effect, link status is UP */
2314 if (sc->config.loop_back != CFG_LOOP_NONE) /* XXX INWARD ONLY */
2315 link_status = STATUS_UP;
2320 /* IOCTL SYSCALL: can sleep. */
2322 t1_send_bop(softc_t *sc, int bop_code)
2327 /* The BOP transmitter could be sending a continuous */
2328 /* BOP msg when told to send this BOP_25 message. */
2329 /* So save and restore the state of the BOP machine. */
2330 bop = read_framer(sc, Bt8370_BOP);
2331 write_framer(sc, Bt8370_BOP, RBOP_OFF | TBOP_OFF);
2332 for (i=0; i<40; i++) /* max delay 400 ms. */
2333 if (read_framer(sc, Bt8370_BOP_STAT) & 0x80) SLEEP(10000);
2334 /* send 25 repetitions of bop_code */
2335 write_framer(sc, Bt8370_BOP, RBOP_OFF | TBOP_25);
2336 write_framer(sc, Bt8370_TBOP, bop_code); /* order matters */
2337 /* wait for tx to stop */
2338 for (i=0; i<40; i++) /* max delay 400 ms. */
2339 if (read_framer(sc, Bt8370_BOP_STAT) & 0x80) SLEEP(10000);
2340 /* Restore previous state of the BOP machine. */
2341 write_framer(sc, Bt8370_BOP, bop);
2344 /* IOCTL SYSCALL: can sleep. */
2346 t1_ioctl(softc_t *sc, struct ioctl *ioctl)
2352 case IOCTL_SNMP_SEND: /* set opstatus? */
2354 switch (ioctl->data)
2358 write_framer(sc, Bt8370_TPATT, 0x00); /* tx pattern generator off */
2359 write_framer(sc, Bt8370_RPATT, 0x00); /* rx pattern detector off */
2360 write_framer(sc, Bt8370_TLB, 0x00); /* tx inband generator off */
2366 t1_send_bop(sc, T1BOP_LINE_UP);
2367 else if (FORMAT_T1SF)
2369 write_framer(sc, Bt8370_LBP, 0x08); /* 10000 10000 ... */
2370 write_framer(sc, Bt8370_TLB, 0x05); /* 5 bits, framed, start */
2372 sc->status.snmp.t1.loop |= TLOOP_FAR_LINE;
2377 t1_send_bop(sc, T1BOP_PAY_UP);
2378 sc->status.snmp.t1.loop |= TLOOP_FAR_PAYLOAD;
2383 if (sc->status.snmp.t1.loop == TLOOP_FAR_LINE)
2386 t1_send_bop(sc, T1BOP_LINE_DOWN);
2387 else if (FORMAT_T1SF)
2389 write_framer(sc, Bt8370_LBP, 0x24); /* 100100 100100 ... */
2390 write_framer(sc, Bt8370_TLB, 0x09); /* 6 bits, framed, start */
2392 sc->status.snmp.t1.loop &= ~TLOOP_FAR_LINE;
2394 if (sc->status.snmp.t1.loop == TLOOP_FAR_PAYLOAD)
2396 t1_send_bop(sc, T1BOP_PAY_DOWN);
2397 sc->status.snmp.t1.loop &= ~TLOOP_FAR_PAYLOAD;
2403 write_framer(sc, Bt8370_TPATT, 0x1E); /* framed QRSS */
2414 case IOCTL_SNMP_LOOP: /* set opstatus = test? */
2416 u_int8_t new_loop = 0;
2418 if (ioctl->data == CFG_LOOP_NONE)
2420 else if (ioctl->data == CFG_LOOP_PAYLOAD)
2421 new_loop = LOOP_PAYLOAD;
2422 else if (ioctl->data == CFG_LOOP_LINE)
2423 new_loop = LOOP_LINE;
2424 else if (ioctl->data == CFG_LOOP_OTHER)
2425 new_loop = LOOP_ANALOG;
2426 else if (ioctl->data == CFG_LOOP_INWARD)
2427 new_loop = LOOP_FRAMER;
2428 else if (ioctl->data == CFG_LOOP_DUAL)
2429 new_loop = LOOP_DUAL;
2434 write_framer(sc, Bt8370_LOOP, new_loop);
2435 sc->config.loop_back = ioctl->data;
2448 struct card hssi_card =
2450 .config = hssi_config,
2451 .ident = hssi_ident,
2452 .watchdog = hssi_watchdog,
2453 .ioctl = hssi_ioctl,
2457 struct card t3_card =
2459 .config = t3_config,
2461 .watchdog = t3_watchdog,
2466 struct card ssi_card =
2468 .config = ssi_config,
2470 .watchdog = ssi_watchdog,
2475 struct card t1_card =
2477 .config = t1_config,
2479 .watchdog = t1_watchdog,
2483 /* RAWIP is raw IP packets (v4 or v6) in HDLC frames with NO HEADERS. */
2484 /* No HDLC Address/Control fields! No line control protocol at all! */
2485 /* This code is BSD/ifnet-specific; Linux and Netgraph also do RAWIP. */
2489 # if ((defined(__FreeBSD__) && (__FreeBSD_version < 500000)) ||\
2490 defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__))
2492 netisr_dispatch(int isr, struct mbuf *mbuf)
2494 struct ifqueue *intrq = NULL;
2498 if (isr == NETISR_IP) intrq = &ipintrq;
2501 if (isr == NETISR_IPV6) intrq = &ip6intrq;
2504 if ((intrq != NULL) && ((qfull = IF_QFULL(intrq)) == 0))
2506 /* rxintr_cleanup() ENQUEUES in a hard interrupt. */
2507 /* networking code DEQUEUES in a soft interrupt. */
2508 /* Some BSD QUEUE routines are not interrupt-safe. */
2509 DISABLE_INTR; /* noop in FreeBSD */
2510 IF_ENQUEUE(intrq, mbuf);
2512 schednetisr(isr); /* schedule a soft interrupt */
2517 if ((intrq != NULL) && (qfull != 0))
2521 # endif /* ((__FreeBSD__ && (__FreeBSD_version < 500000)) || */
2522 /* __NetBSD__ || __OpenBSD__ || __bsdi__) */
2524 /* rxintr_cleanup calls this to give a newly arrived pkt to higher levels. */
2526 lmc_raw_input(struct ifnet *ifp, struct mbuf *mbuf)
2528 softc_t *sc = IFP2SC(ifp);
2530 M_SETFIB(mbuf, ifp->if_fib);
2532 if (mbuf->m_data[0]>>4 == 4)
2533 netisr_dispatch(NETISR_IP, mbuf);
2537 if (mbuf->m_data[0]>>4 == 6)
2538 netisr_dispatch(NETISR_IPV6, mbuf);
2543 sc->status.cntrs.idiscards++;
2545 printf("%s: lmc_raw_input: rx pkt discarded: not IPv4 or IPv6\n",
2552 /* There are TWO VERSIONS of interrupt/DMA code: Linux & BSD.
2553 * Handling Linux and the BSDs with CPP directives would
2554 * make the code unreadable, so there are two versions.
2555 * Conceptually, the two versions do the same thing and
2556 * core_interrupt() doesn't know they are different.
2558 * We are "standing on the head of a pin" in these routines.
2559 * Tulip CSRs can be accessed, but nothing else is interrupt-safe!
2560 * Do NOT access: MII, GPIO, SROM, BIOSROM, XILINX, SYNTH, or DAC.
2563 #if BSD /* BSD version of interrupt/DMA code */
2565 /* Singly-linked tail-queues hold mbufs with active DMA.
2566 * For RX, single mbuf clusters; for TX, mbuf chains are queued.
2567 * NB: mbufs are linked through their m_nextpkt field.
2568 * Callers must hold sc->bottom_lock; not otherwise locked.
2571 /* Put an mbuf (chain) on the tail of the descriptor ring queue. */
2572 static void /* BSD version */
2573 mbuf_enqueue(struct desc_ring *ring, struct mbuf *m)
2575 m->m_nextpkt = NULL;
2576 if (ring->tail == NULL)
2579 ring->tail->m_nextpkt = m;
2583 /* Get an mbuf (chain) from the head of the descriptor ring queue. */
2584 static struct mbuf* /* BSD version */
2585 mbuf_dequeue(struct desc_ring *ring)
2587 struct mbuf *m = ring->head;
2589 if ((ring->head = m->m_nextpkt) == NULL)
2595 static void /* *** FreeBSD ONLY *** Callout from bus_dmamap_load() */
2596 fbsd_dmamap_load(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2598 struct desc_ring *ring = arg;
2599 ring->nsegs = error ? 0 : nsegs;
2600 ring->segs[0] = segs[0];
2601 ring->segs[1] = segs[1];
2605 /* Initialize a DMA descriptor ring. */
2606 static int /* BSD version */
2607 create_ring(softc_t *sc, struct desc_ring *ring, int num_descs)
2609 struct dma_desc *descs;
2610 int size_descs = sizeof(struct dma_desc)*num_descs;
2613 /* The DMA descriptor array must not cross a page boundary. */
2614 if (size_descs > PAGE_SIZE)
2616 printf("%s: DMA descriptor array > PAGE_SIZE (%d)\n", NAME_UNIT,
2623 /* Create a DMA tag for descriptors and buffers. */
2624 if ((error = bus_dma_tag_create(bus_get_dma_tag(sc->dev),
2625 4, 0, BUS_SPACE_MAXADDR_32BIT,
2626 BUS_SPACE_MAXADDR, NULL, NULL, PAGE_SIZE, 2, PAGE_SIZE, BUS_DMA_ALLOCNOW,
2627 # if (__FreeBSD_version >= 502000)
2632 printf("%s: bus_dma_tag_create() failed: error %d\n", NAME_UNIT, error);
2636 /* Allocate wired physical memory for DMA descriptor array */
2637 /* and map physical address to kernel virtual address. */
2638 if ((error = bus_dmamem_alloc(ring->tag, (void**)&ring->first,
2639 BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &ring->map)))
2641 printf("%s: bus_dmamem_alloc() failed; error %d\n", NAME_UNIT, error);
2644 descs = ring->first;
2646 /* Map kernel virtual address to PCI address for DMA descriptor array. */
2647 if ((error = bus_dmamap_load(ring->tag, ring->map, descs, size_descs,
2648 fbsd_dmamap_load, ring, 0)))
2650 printf("%s: bus_dmamap_load() failed; error %d\n", NAME_UNIT, error);
2653 ring->dma_addr = ring->segs[0].ds_addr;
2655 /* Allocate dmamaps for each DMA descriptor. */
2656 for (i=0; i<num_descs; i++)
2657 if ((error = bus_dmamap_create(ring->tag, 0, &descs[i].map)))
2659 printf("%s: bus_dmamap_create() failed; error %d\n", NAME_UNIT, error);
2663 #elif (defined(__NetBSD__) || defined(__OpenBSD__))
2665 /* Use the DMA tag passed to attach() for descriptors and buffers. */
2666 ring->tag = sc->pa_dmat;
2668 /* Allocate wired physical memory for DMA descriptor array. */
2669 if ((error = bus_dmamem_alloc(ring->tag, size_descs, PAGE_SIZE, 0,
2670 ring->segs, 1, &ring->nsegs, BUS_DMA_NOWAIT)))
2672 printf("%s: bus_dmamem_alloc() failed; error %d\n", NAME_UNIT, error);
2676 /* Map physical address to kernel virtual address. */
2677 if ((error = bus_dmamem_map(ring->tag, ring->segs, ring->nsegs,
2678 size_descs, (caddr_t *)&ring->first, BUS_DMA_NOWAIT | BUS_DMA_COHERENT)))
2680 printf("%s: bus_dmamem_map() failed; error %d\n", NAME_UNIT, error);
2683 descs = ring->first; /* suppress compiler warning about aliasing */
2684 memset(descs, 0, size_descs);
2686 /* Allocate dmamap for PCI access to DMA descriptor array. */
2687 if ((error = bus_dmamap_create(ring->tag, size_descs, 1,
2688 size_descs, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ring->map)))
2690 printf("%s: bus_dmamap_create() failed; error %d\n", NAME_UNIT, error);
2694 /* Map kernel virtual address to PCI address for DMA descriptor array. */
2695 if ((error = bus_dmamap_load(ring->tag, ring->map, descs, size_descs,
2696 0, BUS_DMA_NOWAIT)))
2698 printf("%s: bus_dmamap_load() failed; error %d\n", NAME_UNIT, error);
2701 ring->dma_addr = ring->map->dm_segs[0].ds_addr;
2703 /* Allocate dmamaps for each DMA descriptor. */
2704 for (i=0; i<num_descs; i++)
2705 if ((error = bus_dmamap_create(ring->tag, MAX_DESC_LEN, 2,
2706 MAX_CHUNK_LEN, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &descs[i].map)))
2708 printf("%s: bus_dmamap_create() failed; error %d\n", NAME_UNIT, error);
2712 #elif defined(__bsdi__)
2714 /* Allocate wired physical memory for DMA descriptor array. */
2715 if ((ring->first = malloc(size_descs, M_DEVBUF, M_NOWAIT)) == NULL)
2717 printf("%s: malloc() failed for DMA descriptor array\n", NAME_UNIT);
2720 descs = ring->first;
2721 memset(descs, 0, size_descs);
2723 /* Map kernel virtual address to PCI address for DMA descriptor array. */
2724 ring->dma_addr = vtophys(descs); /* Relax! BSD/OS only. */
2729 ring->write = descs;
2730 ring->first = descs;
2731 ring->last = descs + num_descs -1;
2732 ring->last->control = TLP_DCTL_END_RING;
2733 ring->num_descs = num_descs;
2734 ring->size_descs = size_descs;
2741 /* Destroy a DMA descriptor ring */
2742 static void /* BSD version */
2743 destroy_ring(softc_t *sc, struct desc_ring *ring)
2745 struct dma_desc *desc;
2748 /* Free queued mbufs. */
2749 while ((m = mbuf_dequeue(ring)) != NULL)
2752 /* TX may have one pkt that is not on any queue. */
2753 if (sc->tx_mbuf != NULL)
2755 m_freem(sc->tx_mbuf);
2759 /* Unmap active DMA descriptors. */
2760 while (ring->read != ring->write)
2762 bus_dmamap_unload(ring->tag, ring->read->map);
2763 if (ring->read++ == ring->last) ring->read = ring->first;
2768 /* Free the dmamaps of all DMA descriptors. */
2769 for (desc=ring->first; desc!=ring->last+1; desc++)
2770 if (desc->map != NULL)
2771 bus_dmamap_destroy(ring->tag, desc->map);
2773 /* Unmap PCI address for DMA descriptor array. */
2774 if (ring->dma_addr != 0)
2775 bus_dmamap_unload(ring->tag, ring->map);
2776 /* Free kernel memory for DMA descriptor array. */
2777 if (ring->first != NULL)
2778 bus_dmamem_free(ring->tag, ring->first, ring->map);
2779 /* Free the DMA tag created for this ring. */
2780 if (ring->tag != NULL)
2781 bus_dma_tag_destroy(ring->tag);
2783 #elif (defined(__NetBSD__) || defined(__OpenBSD__))
2785 /* Free the dmamaps of all DMA descriptors. */
2786 for (desc=ring->first; desc!=ring->last+1; desc++)
2787 if (desc->map != NULL)
2788 bus_dmamap_destroy(ring->tag, desc->map);
2790 /* Unmap PCI address for DMA descriptor array. */
2791 if (ring->dma_addr != 0)
2792 bus_dmamap_unload(ring->tag, ring->map);
2793 /* Free dmamap for DMA descriptor array. */
2794 if (ring->map != NULL)
2795 bus_dmamap_destroy(ring->tag, ring->map);
2796 /* Unmap kernel address for DMA descriptor array. */
2797 if (ring->first != NULL)
2798 bus_dmamem_unmap(ring->tag, (caddr_t)ring->first, ring->size_descs);
2799 /* Free kernel memory for DMA descriptor array. */
2800 if (ring->segs[0].ds_addr != 0)
2801 bus_dmamem_free(ring->tag, ring->segs, ring->nsegs);
2803 #elif defined(__bsdi__)
2805 /* Free kernel memory for DMA descriptor array. */
2806 if (ring->first != NULL)
2807 free(ring->first, M_DEVBUF);
2812 /* Clean up after a packet has been received. */
2813 static int /* BSD version */
2814 rxintr_cleanup(softc_t *sc)
2816 struct desc_ring *ring = &sc->rxring;
2817 struct dma_desc *first_desc, *last_desc;
2818 struct mbuf *first_mbuf=NULL, *last_mbuf=NULL;
2819 struct mbuf *new_mbuf;
2820 int pkt_len, desc_len;
2822 #if (defined(__FreeBSD__) && defined(DEVICE_POLLING))
2823 /* Input packet flow control (livelock prevention): */
2824 /* Give pkts to higher levels only if quota is > 0. */
2825 if (sc->quota <= 0) return 0;
2828 /* This looks complicated, but remember: typically packets up */
2829 /* to 2048 bytes long fit in one mbuf and use one descriptor. */
2831 first_desc = last_desc = ring->read;
2833 /* ASSERTION: If there is a descriptor in the ring and the hardware has */
2834 /* finished with it, then that descriptor will have RX_FIRST_DESC set. */
2835 if ((ring->read != ring->write) && /* descriptor ring not empty */
2836 ((ring->read->status & TLP_DSTS_OWNER) == 0) && /* hardware done */
2837 ((ring->read->status & TLP_DSTS_RX_FIRST_DESC) == 0)) /* should be set */
2838 panic("%s: rxintr_cleanup: rx-first-descriptor not set.\n", NAME_UNIT);
2840 /* First decide if a complete packet has arrived. */
2841 /* Run down DMA descriptors looking for one marked "last". */
2842 /* Bail out if an active descriptor is encountered. */
2843 /* Accumulate most significant bits of packet length. */
2847 if (last_desc == ring->write) return 0; /* no more descs */
2848 if (last_desc->status & TLP_DSTS_OWNER) return 0; /* still active */
2849 if (last_desc->status & TLP_DSTS_RX_LAST_DESC) break; /* end of packet */
2850 pkt_len += last_desc->length1 + last_desc->length2; /* entire desc filled */
2851 if (last_desc++->control & TLP_DCTL_END_RING) last_desc = ring->first; /* ring wrap */
2854 /* A complete packet has arrived; how long is it? */
2855 /* H/w ref man shows RX pkt length as a 14-bit field. */
2856 /* An experiment found that only the 12 LSBs work. */
2857 if (((last_desc->status>>16)&0xFFF) == 0) pkt_len += 4096; /* carry-bit */
2858 pkt_len = (pkt_len & 0xF000) + ((last_desc->status>>16) & 0x0FFF);
2859 /* Subtract the CRC length unless doing so would underflow. */
2860 if (pkt_len >= sc->config.crc_len) pkt_len -= sc->config.crc_len;
2862 /* Run down DMA descriptors again doing the following:
2863 * 1) put pkt info in pkthdr of first mbuf,
2865 * 3) set mbuf lengths.
2867 first_desc = ring->read;
2870 /* Read a DMA descriptor from the ring. */
2871 last_desc = ring->read;
2872 /* Advance the ring read pointer. */
2873 if (ring->read++ == ring->last) ring->read = ring->first;
2875 /* Dequeue the corresponding cluster mbuf. */
2876 new_mbuf = mbuf_dequeue(ring);
2877 if (new_mbuf == NULL)
2878 panic("%s: rxintr_cleanup: expected an mbuf\n", NAME_UNIT);
2880 desc_len = last_desc->length1 + last_desc->length2;
2881 /* If bouncing, copy bounce buf to mbuf. */
2882 DMA_SYNC(last_desc->map, desc_len, BUS_DMASYNC_POSTREAD);
2883 /* Unmap kernel virtual address to PCI address. */
2884 bus_dmamap_unload(ring->tag, last_desc->map);
2886 /* 1) Put pkt info in pkthdr of first mbuf. */
2887 if (last_desc == first_desc)
2889 first_mbuf = new_mbuf;
2890 first_mbuf->m_pkthdr.len = pkt_len; /* total pkt length */
2892 first_mbuf->m_pkthdr.rcvif = sc->ifp; /* how it got here */
2894 first_mbuf->m_pkthdr.rcvif = NULL;
2897 else /* 2) link mbufs. */
2899 last_mbuf->m_next = new_mbuf;
2900 /* M_PKTHDR should be set in the first mbuf only. */
2901 new_mbuf->m_flags &= ~M_PKTHDR;
2903 last_mbuf = new_mbuf;
2905 /* 3) Set mbuf lengths. */
2906 new_mbuf->m_len = (pkt_len >= desc_len) ? desc_len : pkt_len;
2907 pkt_len -= new_mbuf->m_len;
2908 } while ((last_desc->status & TLP_DSTS_RX_LAST_DESC) == 0);
2910 /* Decide whether to accept or to discard this packet. */
2911 /* RxHDLC sets MIIERR for bad CRC, abort and partial byte at pkt end. */
2912 if (((last_desc->status & TLP_DSTS_RX_BAD) == 0) &&
2913 (sc->status.oper_status == STATUS_UP) &&
2914 (first_mbuf->m_pkthdr.len > 0))
2916 /* Optimization: copy a small pkt into a small mbuf. */
2917 if (first_mbuf->m_pkthdr.len <= COPY_BREAK)
2919 MGETHDR(new_mbuf, M_NOWAIT, MT_DATA);
2920 if (new_mbuf != NULL)
2922 new_mbuf->m_pkthdr.rcvif = first_mbuf->m_pkthdr.rcvif;
2923 new_mbuf->m_pkthdr.len = first_mbuf->m_pkthdr.len;
2924 new_mbuf->m_len = first_mbuf->m_len;
2925 memcpy(new_mbuf->m_data, first_mbuf->m_data,
2926 first_mbuf->m_pkthdr.len);
2927 m_freem(first_mbuf);
2928 first_mbuf = new_mbuf;
2931 /* Include CRC and one flag byte in input byte count. */
2932 sc->status.cntrs.ibytes += first_mbuf->m_pkthdr.len + sc->config.crc_len +1;
2933 sc->status.cntrs.ipackets++;
2935 sc->ifp->if_ipackets++;
2936 LMC_BPF_MTAP(first_mbuf);
2938 #if (defined(__FreeBSD__) && defined(DEVICE_POLLING))
2942 /* Give this good packet to the network stacks. */
2944 if (sc->ng_hook != NULL) /* is hook connected? */
2946 # if (__FreeBSD_version >= 500000)
2947 int error; /* ignore error */
2948 NG_SEND_DATA_ONLY(error, sc->ng_hook, first_mbuf);
2949 # else /* FreeBSD-4 */
2950 ng_queue_data(sc->ng_hook, first_mbuf, NULL);
2952 return 1; /* did something */
2954 #endif /* NETGRAPH */
2955 if (sc->config.line_pkg == PKG_RAWIP)
2956 lmc_raw_input(sc->ifp, first_mbuf);
2960 sppp_input(sc->ifp, first_mbuf);
2962 new_mbuf = first_mbuf;
2963 while (new_mbuf != NULL)
2965 sc->p2p->p2p_hdrinput(sc->p2p, new_mbuf->m_data, new_mbuf->m_len);
2966 new_mbuf = new_mbuf->m_next;
2968 sc->p2p->p2p_input(sc->p2p, NULL);
2969 m_freem(first_mbuf);
2971 m_freem(first_mbuf);
2972 sc->status.cntrs.idiscards++;
2976 else if (sc->status.oper_status != STATUS_UP)
2978 /* If the link is down, this packet is probably noise. */
2979 m_freem(first_mbuf);
2980 sc->status.cntrs.idiscards++;
2982 printf("%s: rxintr_cleanup: rx pkt discarded: link down\n", NAME_UNIT);
2984 else /* Log and discard this bad packet. */
2987 printf("%s: RX bad pkt; len=%d %s%s%s%s\n",
2988 NAME_UNIT, first_mbuf->m_pkthdr.len,
2989 (last_desc->status & TLP_DSTS_RX_MII_ERR) ? " miierr" : "",
2990 (last_desc->status & TLP_DSTS_RX_DRIBBLE) ? " dribble" : "",
2991 (last_desc->status & TLP_DSTS_RX_DESC_ERR) ? " descerr" : "",
2992 (last_desc->status & TLP_DSTS_RX_OVERRUN) ? " overrun" : "");
2993 if (last_desc->status & TLP_DSTS_RX_OVERRUN)
2994 sc->status.cntrs.fifo_over++;
2996 sc->status.cntrs.ierrors++;
2997 m_freem(first_mbuf);
3000 return 1; /* did something */
3003 /* Setup (prepare) to receive a packet. */
3004 /* Try to keep the RX descriptor ring full of empty buffers. */
3005 static int /* BSD version */
3006 rxintr_setup(softc_t *sc)
3008 struct desc_ring *ring = &sc->rxring;
3009 struct dma_desc *desc;
3014 /* Ring is full if (wrap(write+1)==read) */
3015 if (((ring->write == ring->last) ? ring->first : ring->write+1) == ring->read)
3016 return 0; /* ring is full; nothing to do */
3018 /* Allocate a small mbuf and attach an mbuf cluster. */
3019 MGETHDR(m, M_NOWAIT, MT_DATA);
3022 sc->status.cntrs.rxdma++;
3024 printf("%s: rxintr_setup: MGETHDR() failed\n", NAME_UNIT);
3027 MCLGET(m, M_NOWAIT);
3028 if ((m->m_flags & M_EXT) == 0)
3031 sc->status.cntrs.rxdma++;
3033 printf("%s: rxintr_setup: MCLGET() failed\n", NAME_UNIT);
3037 /* Queue the mbuf for later processing by rxintr_cleanup. */
3038 mbuf_enqueue(ring, m);
3040 /* Write a DMA descriptor into the ring. */
3041 /* Hardware won't see it until the OWNER bit is set. */
3043 /* Advance the ring write pointer. */
3044 if (ring->write++ == ring->last) ring->write = ring->first;
3046 desc_len = (MCLBYTES < MAX_DESC_LEN) ? MCLBYTES : MAX_DESC_LEN;
3047 /* Map kernel virtual address to PCI address. */
3048 if ((error = DMA_LOAD(desc->map, m->m_data, desc_len)))
3049 printf("%s: bus_dmamap_load(rx) failed; error %d\n", NAME_UNIT, error);
3050 /* Invalidate the cache for this mbuf. */
3051 DMA_SYNC(desc->map, desc_len, BUS_DMASYNC_PREREAD);
3053 /* Set up the DMA descriptor. */
3055 desc->address1 = ring->segs[0].ds_addr;
3056 #elif (defined(__NetBSD__) || defined(__OpenBSD__))
3057 desc->address1 = desc->map->dm_segs[0].ds_addr;
3058 #elif defined(__bsdi__)
3059 desc->address1 = vtophys(m->m_data); /* Relax! BSD/OS only. */
3061 desc->length1 = desc_len>>1;
3062 desc->address2 = desc->address1 + desc->length1;
3063 desc->length2 = desc_len>>1;
3065 /* Before setting the OWNER bit, flush the cache (memory barrier). */
3066 DMA_SYNC(ring->map, ring->size_descs, BUS_DMASYNC_PREWRITE);
3068 /* Commit the DMA descriptor to the hardware. */
3069 desc->status = TLP_DSTS_OWNER;
3071 /* Notify the receiver that there is another buffer available. */
3072 WRITE_CSR(TLP_RX_POLL, 1);
3074 return 1; /* did something */
3077 /* Clean up after a packet has been transmitted. */
3078 /* Free the mbuf chain and update the DMA descriptor ring. */
3079 static int /* BSD version */
3080 txintr_cleanup(softc_t *sc)
3082 struct desc_ring *ring = &sc->txring;
3083 struct dma_desc *desc;
3085 while ((ring->read != ring->write) && /* while ring is not empty */
3086 ((ring->read->status & TLP_DSTS_OWNER) == 0))
3088 /* Read a DMA descriptor from the ring. */
3090 /* Advance the ring read pointer. */
3091 if (ring->read++ == ring->last) ring->read = ring->first;
3093 /* This is a no-op on most architectures. */
3094 DMA_SYNC(desc->map, desc->length1 + desc->length2, BUS_DMASYNC_POSTWRITE);
3095 /* Unmap kernel virtual address to PCI address. */
3096 bus_dmamap_unload(ring->tag, desc->map);
3098 /* If this descriptor is the last segment of a packet, */
3099 /* then dequeue and free the corresponding mbuf chain. */
3100 if ((desc->control & TLP_DCTL_TX_LAST_SEG) != 0)
3103 if ((m = mbuf_dequeue(ring)) == NULL)
3104 panic("%s: txintr_cleanup: expected an mbuf\n", NAME_UNIT);
3106 /* Include CRC and one flag byte in output byte count. */
3107 sc->status.cntrs.obytes += m->m_pkthdr.len + sc->config.crc_len +1;
3108 sc->status.cntrs.opackets++;
3110 sc->ifp->if_opackets++;
3113 /* The only bad TX status is fifo underrun. */
3114 if ((desc->status & TLP_DSTS_TX_UNDERRUN) != 0)
3115 sc->status.cntrs.fifo_under++;
3118 return 1; /* did something */
3125 /* Build DMA descriptors for a transmit packet mbuf chain. */
3126 static int /* 0=success; 1=error */ /* BSD version */
3127 txintr_setup_mbuf(softc_t *sc, struct mbuf *m)
3129 struct desc_ring *ring = &sc->txring;
3130 struct dma_desc *desc;
3131 unsigned int desc_len;
3133 /* build DMA descriptors for a chain of mbufs. */
3136 char *data = m->m_data;
3137 int length = m->m_len; /* zero length mbufs happen! */
3139 /* Build DMA descriptors for one mbuf. */
3144 /* Ring is full if (wrap(write+1)==read) */
3145 if (((ring->temp==ring->last) ? ring->first : ring->temp+1) == ring->read)
3146 { /* Not enough DMA descriptors; try later. */
3147 for (; ring->temp!=ring->write;
3148 ring->temp = (ring->temp==ring->first)? ring->last : ring->temp-1)
3149 bus_dmamap_unload(ring->tag, ring->temp->map);
3150 sc->status.cntrs.txdma++;
3154 /* Provisionally, write a descriptor into the ring. */
3155 /* But don't change the REAL ring write pointer. */
3156 /* Hardware won't see it until the OWNER bit is set. */
3158 /* Advance the temporary ring write pointer. */
3159 if (ring->temp++ == ring->last) ring->temp = ring->first;
3161 /* Clear all control bits except the END_RING bit. */
3162 desc->control &= TLP_DCTL_END_RING;
3163 /* Don't pad short packets up to 64 bytes. */
3164 desc->control |= TLP_DCTL_TX_NO_PAD;
3165 /* Use Tulip's CRC-32 generator, if appropriate. */
3166 if (sc->config.crc_len != CFG_CRC_32)
3167 desc->control |= TLP_DCTL_TX_NO_CRC;
3168 /* Set the OWNER bit, except in the first descriptor. */
3169 if (desc != ring->write)
3170 desc->status = TLP_DSTS_OWNER;
3172 desc_len = (length > MAX_CHUNK_LEN) ? MAX_CHUNK_LEN : length;
3173 /* Map kernel virtual address to PCI address. */
3174 if ((error = DMA_LOAD(desc->map, data, desc_len)))
3175 printf("%s: bus_dmamap_load(tx) failed; error %d\n", NAME_UNIT, error);
3176 /* Flush the cache and if bouncing, copy mbuf to bounce buf. */
3177 DMA_SYNC(desc->map, desc_len, BUS_DMASYNC_PREWRITE);
3179 /* Prevent wild fetches if mapping fails (nsegs==0). */
3180 desc->length1 = desc->length2 = 0;
3181 desc->address1 = desc->address2 = 0;
3182 #if (defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__))
3185 bus_dma_segment_t *segs = ring->segs;
3186 int nsegs = ring->nsegs;
3187 # elif (defined(__NetBSD__) || defined(__OpenBSD__))
3188 bus_dma_segment_t *segs = desc->map->dm_segs;
3189 int nsegs = desc->map->dm_nsegs;
3193 desc->address1 = segs[0].ds_addr;
3194 desc->length1 = segs[0].ds_len;
3198 desc->address2 = segs[1].ds_addr;
3199 desc->length2 = segs[1].ds_len;
3202 #elif defined(__bsdi__)
3203 desc->address1 = vtophys(data); /* Relax! BSD/OS only. */
3204 desc->length1 = desc_len;
3209 } /* while (length > 0) */
3212 } /* while (m != NULL) */
3214 return 0; /* success */
3217 /* Setup (prepare) to transmit a packet. */
3218 /* Select a packet, build DMA descriptors and give packet to hardware. */
3219 /* If DMA descriptors run out, abandon the attempt and return 0. */
3220 static int /* BSD version */
3221 txintr_setup(softc_t *sc)
3223 struct desc_ring *ring = &sc->txring;
3224 struct dma_desc *first_desc, *last_desc;
3226 /* Protect against half-up links: Don't transmit */
3227 /* if the receiver can't hear the far end. */
3228 if (sc->status.oper_status != STATUS_UP) return 0;
3230 /* Pick a packet to transmit. */
3232 if ((sc->ng_hook != NULL) && (sc->tx_mbuf == NULL))
3234 if (!IFQ_IS_EMPTY(&sc->ng_fastq))
3235 IFQ_DEQUEUE(&sc->ng_fastq, sc->tx_mbuf);
3237 IFQ_DEQUEUE(&sc->ng_sndq, sc->tx_mbuf);
3241 if (sc->tx_mbuf == NULL)
3243 if (sc->config.line_pkg == PKG_RAWIP)
3244 IFQ_DEQUEUE(&sc->ifp->if_snd, sc->tx_mbuf);
3248 sc->tx_mbuf = sppp_dequeue(sc->ifp);
3250 if (!IFQ_IS_EMPTY(&sc->p2p->p2p_isnd))
3251 IFQ_DEQUEUE(&sc->p2p->p2p_isnd, sc->tx_mbuf);
3253 IFQ_DEQUEUE(&sc->ifp->if_snd, sc->tx_mbuf);
3257 if (sc->tx_mbuf == NULL) return 0; /* no pkt to transmit */
3259 /* Build DMA descriptors for an outgoing mbuf chain. */
3260 ring->temp = ring->write; /* temporary ring write pointer */
3261 if (txintr_setup_mbuf(sc, sc->tx_mbuf) != 0) return 0;
3263 /* Enqueue the mbuf; txintr_cleanup will free it. */
3264 mbuf_enqueue(ring, sc->tx_mbuf);
3266 /* The transmitter has room for another packet. */
3269 /* Set first & last segment bits. */
3270 /* last_desc is the desc BEFORE the one pointed to by ring->temp. */
3271 first_desc = ring->write;
3272 first_desc->control |= TLP_DCTL_TX_FIRST_SEG;
3273 last_desc = (ring->temp==ring->first)? ring->last : ring->temp-1;
3274 last_desc->control |= TLP_DCTL_TX_LAST_SEG;
3275 /* Interrupt at end-of-transmission? Why bother the poor computer! */
3276 /* last_desc->control |= TLP_DCTL_TX_INTERRUPT; */
3278 /* Make sure the OWNER bit is not set in the next descriptor. */
3279 /* The OWNER bit may have been set if a previous call aborted. */
3280 ring->temp->status = 0;
3282 /* Commit the DMA descriptors to the software. */
3283 ring->write = ring->temp;
3285 /* Before setting the OWNER bit, flush the cache (memory barrier). */
3286 DMA_SYNC(ring->map, ring->size_descs, BUS_DMASYNC_PREWRITE);
3288 /* Commit the DMA descriptors to the hardware. */
3289 first_desc->status = TLP_DSTS_OWNER;
3291 /* Notify the transmitter that there is another packet to send. */
3292 WRITE_CSR(TLP_TX_POLL, 1);
3294 return 1; /* did something */
3300 /* NOTE: this is the LINUX version of the interrupt/DMA code, */
3302 /* Singly-linked tail-queues hold sk_buffs with active DMA.
3303 * skbuffs are linked through their sk_buff.next field.
3304 * Callers must hold sc->bottom_lock; not otherwise locked.
3307 /* Put an skbuff on the tail of the descriptor ring queue. */
3308 static void /* Linux version */
3309 skbuff_enqueue(struct desc_ring *ring, struct sk_buff *skb)
3312 if (ring->tail == NULL)
3315 ring->tail->next = skb;
3319 /* Get an skbuff from the head of the descriptor ring queue. */
3320 static struct sk_buff* /* Linux version */
3321 skbuff_dequeue(struct desc_ring *ring)
3323 struct sk_buff *skb = ring->head;
3325 if ((ring->head = skb->next) == NULL)
3330 /* Initialize a DMA descriptor ring. */
3331 static int /* Linux version */
3332 create_ring(softc_t *sc, struct desc_ring *ring, int num_descs)
3334 struct dma_desc *descs;
3335 int size_descs = sizeof(struct dma_desc)*num_descs;
3337 /* Allocate and map memory for DMA descriptor array. */
3338 if ((descs = pci_alloc_consistent(sc->pci_dev, size_descs,
3339 &ring->dma_addr)) == NULL)
3341 printk("%s: pci_alloc_consistent() failed\n", NAME_UNIT);
3344 memset(descs, 0, size_descs);
3347 ring->write = descs;
3348 ring->first = descs;
3349 ring->last = descs + num_descs -1;
3350 ring->last->control = TLP_DCTL_END_RING;
3351 ring->num_descs = num_descs;
3352 ring->size_descs = size_descs;
3359 /* Destroy a DMA descriptor ring */
3360 static void /* Linux version */
3361 destroy_ring(softc_t *sc, struct desc_ring *ring)
3363 struct sk_buff *skb;
3365 /* Free queued skbuffs. */
3366 while ((skb = skbuff_dequeue(ring)) != NULL)
3369 /* TX may have one pkt that is not on any queue. */
3370 if (sc->tx_skb != NULL)
3372 dev_kfree_skb(sc->tx_skb);
3376 if (ring->first != NULL)
3378 /* Unmap active DMA descriptors. */
3379 while (ring->read != ring->write)
3381 pci_unmap_single(sc->pci_dev, ring->read->address1,
3382 ring->read->length1 + ring->read->length2, PCI_DMA_BIDIRECTIONAL);
3383 if (ring->read++ == ring->last) ring->read = ring->first;
3386 /* Unmap and free memory for DMA descriptor array. */
3387 pci_free_consistent(sc->pci_dev, ring->size_descs, ring->first,
3392 static int /* Linux version */
3393 rxintr_cleanup(softc_t *sc)
3395 struct desc_ring *ring = &sc->rxring;
3396 struct dma_desc *first_desc, *last_desc;
3397 struct sk_buff *first_skb=NULL, *last_skb=NULL;
3398 struct sk_buff *new_skb;
3399 int pkt_len, desc_len;
3401 /* Input packet flow control (livelock prevention): */
3402 /* Give pkts to higher levels only if quota is > 0. */
3403 if (sc->quota <= 0) return 0;
3405 /* This looks complicated, but remember: packets up to 4032 */
3406 /* bytes long fit in one skbuff and use one DMA descriptor. */
3408 first_desc = last_desc = ring->read;
3410 /* ASSERTION: If there is a descriptor in the ring and the hardware has */
3411 /* finished with it, then that descriptor will have RX_FIRST_DESC set. */
3412 if ((ring->read != ring->write) && /* descriptor ring not empty */
3413 ((ring->read->status & TLP_DSTS_OWNER) == 0) && /* hardware done */
3414 ((ring->read->status & TLP_DSTS_RX_FIRST_DESC) == 0)) /* should be set */
3415 panic("%s: rxintr_cleanup: rx-first-descriptor not set.\n", NAME_UNIT);
3417 /* First decide if a complete packet has arrived. */
3418 /* Run down DMA descriptors looking for one marked "last". */
3419 /* Bail out if an active descriptor is encountered. */
3420 /* Accumulate most significant bits of packet length. */
3424 if (last_desc == ring->write) return 0; /* no more descs */
3425 if (last_desc->status & TLP_DSTS_OWNER) return 0; /* still active */
3426 if (last_desc->status & TLP_DSTS_RX_LAST_DESC) break; /* end of packet */
3427 pkt_len += last_desc->length1 + last_desc->length2; /* entire desc filled */
3428 if (last_desc++->control & TLP_DCTL_END_RING) last_desc = ring->first; /* ring wrap */
3431 /* A complete packet has arrived; how long is it? */
3432 /* H/w ref man shows RX pkt length as a 14-bit field. */
3433 /* An experiment found that only the 12 LSBs work. */
3434 if (((last_desc->status>>16)&0xFFF) == 0) pkt_len += 4096; /* carry-bit */
3435 pkt_len = (pkt_len & 0xF000) + ((last_desc->status>>16) & 0x0FFF);
3436 /* Subtract the CRC length unless doing so would underflow. */
3437 if (pkt_len >= sc->config.crc_len) pkt_len -= sc->config.crc_len;
3439 /* Run down DMA descriptors again doing the following:
3440 * 1) put pkt info in hdr of first skbuff.
3441 * 2) put additional skbuffs on frag_list.
3442 * 3) set skbuff lengths.
3444 first_desc = ring->read;
3447 /* Read a DMA descriptor from the ring. */
3448 last_desc = ring->read;
3449 /* Advance the ring read pointer. */
3450 if (ring->read++ == ring->last) ring->read = ring->first;
3452 /* Dequeue the corresponding skbuff. */
3453 new_skb = skbuff_dequeue(ring);
3454 if (new_skb == NULL)
3455 panic("%s: rxintr_cleanup: expected an skbuff\n", NAME_UNIT);
3457 desc_len = last_desc->length1 + last_desc->length2;
3458 /* Unmap kernel virtual addresss to PCI address. */
3459 pci_unmap_single(sc->pci_dev, last_desc->address1,
3460 desc_len, PCI_DMA_FROMDEVICE);
3462 /* Set skbuff length. */
3463 skb_put(new_skb, (pkt_len >= desc_len) ? desc_len : pkt_len);
3464 pkt_len -= new_skb->len;
3466 /* 1) Put pkt info in hdr of first skbuff. */
3467 if (last_desc == first_desc)
3469 first_skb = new_skb;
3470 if (sc->config.line_pkg == PKG_RAWIP)
3472 if (first_skb->data[0]>>4 == 4)
3473 first_skb->protocol = htons(ETH_P_IP);
3474 else if (first_skb->data[0]>>4 == 6)
3475 first_skb->protocol = htons(ETH_P_IPV6);
3479 first_skb->protocol = hdlc_type_trans(first_skb, sc->net_dev);
3481 first_skb->protocol = htons(ETH_P_HDLC);
3483 first_skb->mac.raw = first_skb->data;
3484 first_skb->dev = sc->net_dev;
3485 do_gettimeofday(&first_skb->stamp);
3486 sc->net_dev->last_rx = jiffies;
3488 else /* 2) link skbuffs. */
3490 /* Put this skbuff on the frag_list of the first skbuff. */
3491 new_skb->next = NULL;
3492 if (skb_shinfo(first_skb)->frag_list == NULL)
3493 skb_shinfo(first_skb)->frag_list = new_skb;
3495 last_skb->next = new_skb;
3496 /* 3) set skbuff lengths. */
3497 first_skb->len += new_skb->len;
3498 first_skb->data_len += new_skb->len;
3501 } while ((last_desc->status & TLP_DSTS_RX_LAST_DESC) == 0);
3503 /* Decide whether to accept or to discard this packet. */
3504 /* RxHDLC sets MIIERR for bad CRC, abort and partial byte at pkt end. */
3505 if (((last_desc->status & TLP_DSTS_RX_BAD) == 0) &&
3506 (sc->status.oper_status == STATUS_UP) &&
3507 (first_skb->len > 0))
3509 /* Optimization: copy a small pkt into a small skbuff. */
3510 if (first_skb->len <= COPY_BREAK)
3511 if ((new_skb = skb_copy(first_skb, GFP_ATOMIC)) != NULL)
3513 dev_kfree_skb_any(first_skb);
3514 first_skb = new_skb;
3517 /* Include CRC and one flag byte in input byte count. */
3518 sc->status.cntrs.ibytes += first_skb->len + sc->config.crc_len +1;
3519 sc->status.cntrs.ipackets++;
3521 /* Give this good packet to the network stacks. */
3522 netif_receive_skb(first_skb); /* NAPI */
3525 else if (sc->status.oper_status != STATUS_UP)
3527 /* If the link is down, this packet is probably noise. */
3528 sc->status.cntrs.idiscards++;
3529 dev_kfree_skb_any(first_skb);
3531 printk("%s: rxintr_cleanup: rx pkt discarded: link down\n", NAME_UNIT);
3533 else /* Log and discard this bad packet. */
3536 printk("%s: RX bad pkt; len=%d %s%s%s%s\n",
3537 NAME_UNIT, first_skb->len,
3538 (last_desc->status & TLP_DSTS_RX_MII_ERR) ? " miierr" : "",
3539 (last_desc->status & TLP_DSTS_RX_DRIBBLE) ? " dribble" : "",
3540 (last_desc->status & TLP_DSTS_RX_DESC_ERR) ? " descerr" : "",
3541 (last_desc->status & TLP_DSTS_RX_OVERRUN) ? " overrun" : "");
3542 if (last_desc->status & TLP_DSTS_RX_OVERRUN)
3543 sc->status.cntrs.fifo_over++;
3545 sc->status.cntrs.ierrors++;
3546 dev_kfree_skb_any(first_skb);
3549 return 1; /* did something */
3552 /* Setup (prepare) to receive a packet. */
3553 /* Try to keep the RX descriptor ring full of empty buffers. */
3554 static int /* Linux version */
3555 rxintr_setup(softc_t *sc)
3557 struct desc_ring *ring = &sc->rxring;
3558 struct dma_desc *desc;
3559 struct sk_buff *skb;
3562 /* Ring is full if (wrap(write+1)==read) */
3563 if (((ring->write == ring->last) ? ring->first : ring->write+1) == ring->read)
3564 return 0; /* ring is full; nothing to do */
3566 /* Allocate an skbuff. */
3567 if ((skb = dev_alloc_skb(MAX_DESC_LEN)) == NULL)
3569 sc->status.cntrs.rxdma++;
3571 printk("%s: rxintr_setup: dev_alloc_skb() failed\n", NAME_UNIT);
3574 skb->dev = sc->net_dev;
3576 /* Queue the skbuff for later processing by rxintr_cleanup. */
3577 skbuff_enqueue(ring, skb);
3579 /* Write a DMA descriptor into the ring. */
3580 /* Hardware won't see it until the OWNER bit is set. */
3582 /* Advance the ring write pointer. */
3583 if (ring->write++ == ring->last) ring->write = ring->first;
3585 /* Map kernel virtual addresses to PCI addresses. */
3586 dma_addr = pci_map_single(sc->pci_dev, skb->data,
3587 MAX_DESC_LEN, PCI_DMA_FROMDEVICE);
3588 /* Set up the DMA descriptor. */
3589 desc->address1 = dma_addr;
3590 desc->length1 = MAX_CHUNK_LEN;
3591 desc->address2 = desc->address1 + desc->length1;
3592 desc->length2 = MAX_CHUNK_LEN;
3594 /* Before setting the OWNER bit, flush the cache (memory barrier). */
3595 wmb(); /* write memory barrier */
3597 /* Commit the DMA descriptor to the hardware. */
3598 desc->status = TLP_DSTS_OWNER;
3600 /* Notify the receiver that there is another buffer available. */
3601 WRITE_CSR(TLP_RX_POLL, 1);
3603 return 1; /* did something */
3606 /* Clean up after a packet has been transmitted. */
3607 /* Free the sk_buff and update the DMA descriptor ring. */
3608 static int /* Linux version */
3609 txintr_cleanup(softc_t *sc)
3611 struct desc_ring *ring = &sc->txring;
3612 struct dma_desc *desc;
3614 while ((ring->read != ring->write) && /* ring is not empty */
3615 ((ring->read->status & TLP_DSTS_OWNER) == 0))
3617 /* Read a DMA descriptor from the ring. */
3619 /* Advance the ring read pointer. */
3620 if (ring->read++ == ring->last) ring->read = ring->first;
3621 /* Unmap kernel virtual address to PCI address. */
3622 pci_unmap_single(sc->pci_dev, desc->address1,
3623 desc->length1 + desc->length2, PCI_DMA_TODEVICE);
3625 /* If this descriptor is the last segment of a packet, */
3626 /* then dequeue and free the corresponding skbuff. */
3627 if ((desc->control & TLP_DCTL_TX_LAST_SEG) != 0)
3629 struct sk_buff *skb;
3630 if ((skb = skbuff_dequeue(ring)) == NULL)
3631 panic("%s: txintr_cleanup: expected an sk_buff\n", NAME_UNIT);
3633 /* Include CRC and one flag byte in output byte count. */
3634 sc->status.cntrs.obytes += skb->len + sc->config.crc_len +1;
3635 sc->status.cntrs.opackets++;
3637 /* The only bad TX status is fifo underrun. */
3638 if ((desc->status & TLP_DSTS_TX_UNDERRUN) != 0)
3640 sc->status.cntrs.fifo_under++; /* also increment oerrors? */
3642 printk("%s: txintr_cleanup: tx fifo underrun\n", NAME_UNIT);
3645 dev_kfree_skb_any(skb);
3646 return 1; /* did something */
3653 /* Build DMA descriptors for a tranmit packet fragment, */
3654 /* Assertion: fragment is contiguous in physical memory. */
3655 static int /* 0=success; 1=error */ /* linux version */
3656 txintr_setup_frag(softc_t *sc, char *data, int length)
3658 struct desc_ring *ring = &sc->txring;
3659 struct dma_desc *desc;
3660 unsigned int desc_len;
3665 /* Ring is full if (wrap(write+1)==read) */
3666 if (((ring->temp==ring->last) ? ring->first : ring->temp+1) == ring->read)
3667 { /* Not enough DMA descriptors; try later. */
3668 for (; ring->temp!=ring->write;
3669 ring->temp = (ring->temp==ring->first)? ring->last : ring->temp-1)
3670 pci_unmap_single(sc->pci_dev, ring->temp->address1,
3671 ring->temp->length1 + ring->temp->length2, PCI_DMA_FROMDEVICE);
3672 sc->status.cntrs.txdma++;
3676 /* Provisionally, write a DMA descriptor into the ring. */
3677 /* But don't change the REAL ring write pointer. */
3678 /* Hardware won't see it until the OWNER bit is set. */
3680 /* Advance the temporary ring write pointer. */
3681 if (ring->temp++ == ring->last) ring->temp = ring->first;
3683 /* Clear all control bits except the END_RING bit. */
3684 desc->control &= TLP_DCTL_END_RING;
3685 /* Don't pad short packets up to 64 bytes */
3686 desc->control |= TLP_DCTL_TX_NO_PAD;
3687 /* Use Tulip's CRC-32 generator, if appropriate. */
3688 if (sc->config.crc_len != CFG_CRC_32)
3689 desc->control |= TLP_DCTL_TX_NO_CRC;
3690 /* Set the OWNER bit, except in the first descriptor. */
3691 if (desc != ring->write)
3692 desc->status = TLP_DSTS_OWNER;
3694 desc_len = (length >= MAX_DESC_LEN) ? MAX_DESC_LEN : length;
3695 /* Map kernel virtual address to PCI address. */
3696 dma_addr = pci_map_single(sc->pci_dev, data, desc_len, PCI_DMA_TODEVICE);
3697 /* If it will fit in one chunk, do so, otherwise split it. */
3698 if (desc_len <= MAX_CHUNK_LEN)
3700 desc->address1 = dma_addr;
3701 desc->length1 = desc_len;
3707 desc->address1 = dma_addr;
3708 desc->length1 = desc_len>>1;
3709 desc->address2 = desc->address1 + desc->length1;
3710 desc->length2 = desc_len>>1;
3711 if (desc_len & 1) desc->length2++;
3716 } /* while (length > 0) */
3718 return 0; /* success */
3721 /* NB: this procedure is recursive! */
3722 static int /* 0=success; 1=error */
3723 txintr_setup_skb(softc_t *sc, struct sk_buff *skb)
3725 struct sk_buff *list;
3728 /* First, handle the data in the skbuff itself. */
3729 if (txintr_setup_frag(sc, skb->data, skb_headlen(skb)))
3732 /* Next, handle the VM pages in the Scatter/Gather list. */
3733 if (skb_shinfo(skb)->nr_frags != 0)
3734 for (i=0; i<skb_shinfo(skb)->nr_frags; i++)
3736 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3737 if (txintr_setup_frag(sc, page_address(frag->page) +
3738 frag->page_offset, frag->size))
3742 /* Finally, handle the skbuffs in the frag_list. */
3743 if ((list = skb_shinfo(skb)->frag_list) != NULL)
3744 for (; list; list=list->next)
3745 if (txintr_setup_skb(sc, list)) /* recursive! */
3751 /* Setup (prepare) to transmit a packet. */
3752 /* Select a packet, build DMA descriptors and give packet to hardware. */
3753 /* If DMA descriptors run out, abandon the attempt and return 0. */
3754 static int /* Linux version */
3755 txintr_setup(softc_t *sc)
3757 struct desc_ring *ring = &sc->txring;
3758 struct dma_desc *first_desc, *last_desc;
3760 /* Protect against half-up links: Don't transmit */
3761 /* if the receiver can't hear the far end. */
3762 if (sc->status.oper_status != STATUS_UP) return 0;
3764 /* Pick a packet to transmit. */
3765 /* linux_start() puts packets in sc->tx_skb. */
3766 if (sc->tx_skb == NULL)
3768 if (netif_queue_stopped(sc->net_dev) != 0)
3769 netif_wake_queue(sc->net_dev);
3770 return 0; /* no pkt to transmit */
3773 /* Build DMA descriptors for an outgoing skbuff. */
3774 ring->temp = ring->write; /* temporary ring write pointer */
3775 if (txintr_setup_skb(sc, sc->tx_skb) != 0) return 0;
3777 /* Enqueue the skbuff; txintr_cleanup will free it. */
3778 skbuff_enqueue(ring, sc->tx_skb);
3780 /* The transmitter has room for another packet. */
3783 /* Set first & last segment bits. */
3784 /* last_desc is the desc BEFORE the one pointed to by ring->temp. */
3785 first_desc = ring->write;
3786 first_desc->control |= TLP_DCTL_TX_FIRST_SEG;
3787 last_desc = (ring->temp==ring->first)? ring->last : ring->temp-1;
3788 last_desc->control |= TLP_DCTL_TX_LAST_SEG;
3789 /* Interrupt at end-of-transmission? Why bother the poor computer! */
3790 /* last_desc->control |= TLP_DCTL_TX_INTERRUPT; */
3792 /* Make sure the OWNER bit is not set in the next descriptor. */
3793 /* The OWNER bit may have been set if a previous call aborted. */
3794 ring->temp->status = 0;
3796 /* Commit the DMA descriptors to the software. */
3797 ring->write = ring->temp;
3799 /* Before setting the OWNER bit, flush the cache (memory barrier). */
3800 wmb(); /* write memory barrier */
3802 /* Commit the DMA descriptors to the hardware. */
3803 first_desc->status = TLP_DSTS_OWNER;
3805 /* Notify the transmitter that there is another packet to send. */
3806 WRITE_CSR(TLP_TX_POLL, 1);
3808 sc->net_dev->trans_start = jiffies;
3810 return 1; /* did something */
3813 #endif /* __linux__ */
3816 check_intr_status(softc_t *sc)
3818 u_int32_t status, cfcs, op_mode;
3819 u_int32_t missed, overruns;
3821 /* Check for four unusual events:
3822 * 1) fatal PCI bus errors - some are recoverable
3823 * 2) transmitter FIFO underruns - increase fifo threshold
3824 * 3) receiver FIFO overruns - clear potential hangup
3825 * 4) no receive descs or bufs - count missed packets
3828 /* 1) A fatal bus error causes a Tulip to stop initiating bus cycles. */
3829 /* Module unload/load or boot are the only fixes for Parity Errors. */
3830 /* Master and Target Aborts can be cleared and life may continue. */
3831 status = READ_CSR(TLP_STATUS);
3832 if ((status & TLP_STAT_FATAL_ERROR) != 0)
3834 u_int32_t fatal = (status & TLP_STAT_FATAL_BITS)>>TLP_STAT_FATAL_SHIFT;
3835 printf("%s: FATAL PCI BUS ERROR: %s%s%s%s\n", NAME_UNIT,
3836 (fatal == 0) ? "PARITY ERROR" : "",
3837 (fatal == 1) ? "MASTER ABORT" : "",
3838 (fatal == 2) ? "TARGET ABORT" : "",
3839 (fatal >= 3) ? "RESERVED (?)" : "");
3840 cfcs = READ_PCI_CFG(sc, TLP_CFCS); /* try to clear it */
3841 cfcs &= ~(TLP_CFCS_MSTR_ABORT | TLP_CFCS_TARG_ABORT);
3842 WRITE_PCI_CFG(sc, TLP_CFCS, cfcs);
3845 /* 2) If the transmitter fifo underruns, increase the transmit fifo */
3846 /* threshold: the number of bytes required to be in the fifo */
3847 /* before starting the transmitter (cost: increased tx delay). */
3848 /* The TX_FSM must be stopped to change this parameter. */
3849 if ((status & TLP_STAT_TX_UNDERRUN) != 0)
3851 op_mode = READ_CSR(TLP_OP_MODE);
3852 /* enable store-and-forward mode if tx_threshold tops out? */
3853 if ((op_mode & TLP_OP_TX_THRESH) < TLP_OP_TX_THRESH)
3855 op_mode += 0x4000; /* increment TX_THRESH field; can't overflow */
3856 WRITE_CSR(TLP_OP_MODE, op_mode & ~TLP_OP_TX_RUN);
3857 /* Wait for the TX FSM to stop; it might be processing a pkt. */
3858 while (READ_CSR(TLP_STATUS) & TLP_STAT_TX_FSM); /* XXX HANG */
3859 WRITE_CSR(TLP_OP_MODE, op_mode); /* restart tx */
3861 printf("%s: tx underrun; tx fifo threshold now %d bytes\n",
3862 NAME_UNIT, 128<<((op_mode>>TLP_OP_TR_SHIFT)&3));
3866 /* 3) Errata memo from Digital Equipment Corp warns that 21140A */
3867 /* receivers through rev 2.2 can hang if the fifo overruns. */
3868 /* Recommended fix: stop and start the RX FSM after an overrun. */
3869 missed = READ_CSR(TLP_MISSED);
3870 if ((overruns = ((missed & TLP_MISS_OVERRUN)>>TLP_OVERRUN_SHIFT)) != 0)
3873 printf("%s: rx overrun cntr=%d\n", NAME_UNIT, overruns);
3874 sc->status.cntrs.overruns += overruns;
3875 if ((READ_PCI_CFG(sc, TLP_CFRV) & 0xFF) <= 0x22)
3877 op_mode = READ_CSR(TLP_OP_MODE);
3878 WRITE_CSR(TLP_OP_MODE, op_mode & ~TLP_OP_RX_RUN);
3879 /* Wait for the RX FSM to stop; it might be processing a pkt. */
3880 while (READ_CSR(TLP_STATUS) & TLP_STAT_RX_FSM); /* XXX HANG */
3881 WRITE_CSR(TLP_OP_MODE, op_mode); /* restart rx */
3885 /* 4) When the receiver is enabled and a packet arrives, but no DMA */
3886 /* descriptor is available, the packet is counted as 'missed'. */
3887 /* The receiver should never miss packets; warn if it happens. */
3888 if ((missed = (missed & TLP_MISS_MISSED)) != 0)
3891 printf("%s: rx missed %d pkts\n", NAME_UNIT, missed);
3892 sc->status.cntrs.missed += missed;
3896 static void /* This is where the work gets done. */
3897 core_interrupt(void *arg, int check_status)
3902 /* If any CPU is inside this critical section, then */
3903 /* other CPUs should go away without doing anything. */
3904 if (BOTTOM_TRYLOCK == 0)
3906 sc->status.cntrs.lck_intr++;
3910 /* Clear pending card interrupts. */
3911 WRITE_CSR(TLP_STATUS, READ_CSR(TLP_STATUS));
3913 /* In Linux, pci_alloc_consistent() means DMA descriptors */
3914 /* don't need explicit syncing. */
3917 struct desc_ring *ring = &sc->txring;
3918 DMA_SYNC(sc->txring.map, sc->txring.size_descs,
3919 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3921 DMA_SYNC(sc->rxring.map, sc->rxring.size_descs,
3922 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3926 do /* This is the main loop for interrupt processing. */
3928 activity = txintr_cleanup(sc);
3929 activity += txintr_setup(sc);
3930 activity += rxintr_cleanup(sc);
3931 activity += rxintr_setup(sc);
3936 struct desc_ring *ring = &sc->txring;
3937 DMA_SYNC(sc->txring.map, sc->txring.size_descs,
3938 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3940 DMA_SYNC(sc->rxring.map, sc->rxring.size_descs,
3941 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3945 /* As the interrupt is dismissed, check for four unusual events. */
3946 if (check_status) check_intr_status(sc);
3951 /* user_interrupt() may be called from a syscall or a softirq */
3953 user_interrupt(softc_t *sc, int check_status)
3955 DISABLE_INTR; /* noop on FreeBSD-5 and Linux */
3956 core_interrupt(sc, check_status);
3957 ENABLE_INTR; /* noop on FreeBSD-5 and Linux */
3962 # if (defined(__FreeBSD__) && defined(DEVICE_POLLING))
3964 /* Service the card from the kernel idle loop without interrupts. */
3966 fbsd_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3968 softc_t *sc = IFP2SC(ifp);
3970 #if (__FreeBSD_version < 700000)
3971 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
3973 ether_poll_deregister(ifp);
3974 cmd = POLL_DEREGISTER;
3977 if (cmd == POLL_DEREGISTER)
3979 /* Last call -- reenable card interrupts. */
3980 WRITE_CSR(TLP_INT_ENBL, TLP_INT_TXRX);
3986 core_interrupt(sc, (cmd==POLL_AND_CHECK_STATUS));
3990 # endif /* (__FreeBSD__ && DEVICE_POLLING) */
3992 /* BSD kernels call this procedure when an interrupt happens. */
3993 static intr_return_t
3994 bsd_interrupt(void *arg)
3998 /* Cut losses early if this is not our interrupt. */
3999 if ((READ_CSR(TLP_STATUS) & TLP_INT_TXRX) == 0)
4002 # if (defined(__FreeBSD__) && defined(DEVICE_POLLING))
4003 if (sc->ifp->if_capenable & IFCAP_POLLING)
4006 if ((sc->ifp->if_capabilities & IFCAP_POLLING) &&
4007 (ether_poll_register(fbsd_poll, sc->ifp)))
4009 WRITE_CSR(TLP_INT_ENBL, TLP_INT_DISABLE);
4013 sc->quota = sc->rxring.num_descs; /* input flow control */
4014 # endif /* (__FreeBSD__ && DEVICE_POLLING) */
4016 /* Disable card interrupts. */
4017 WRITE_CSR(TLP_INT_ENBL, TLP_INT_DISABLE);
4019 core_interrupt(sc, 0);
4021 /* Enable card interrupts. */
4022 WRITE_CSR(TLP_INT_ENBL, TLP_INT_TXRX);
4029 /* Administrative status of the driver (UP or DOWN) has changed. */
4030 /* A card-specific action may be required: T1 and T3 cards: no-op. */
4031 /* HSSI and SSI cards change the state of modem ready signals. */
4033 set_status(softc_t *sc, int status)
4037 ioctl.cmd = IOCTL_SET_STATUS;
4038 ioctl.data = status;
4040 sc->card->ioctl(sc, &ioctl);
4045 /* Callout from P2P: */
4046 /* Get the state of DCD (Data Carrier Detect). */
4048 p2p_getmdm(struct p2pcom *p2p, caddr_t result)
4050 softc_t *sc = IFP2SC(&p2p->p2p_if);
4052 /* Non-zero isn't good enough; TIOCM_CAR is 0x40. */
4053 *(int *)result = (sc->status.oper_status==STATUS_UP) ? TIOCM_CAR : 0;
4058 /* Callout from P2P: */
4059 /* Set the state of DTR (Data Terminal Ready). */
4061 p2p_mdmctl(struct p2pcom *p2p, int flag)
4063 softc_t *sc = IFP2SC(&p2p->p2p_if);
4065 set_status(sc, flag);
4078 /* Callout from SPPP: */
4080 sppp_tls(struct sppp *sppp)
4083 if (!(sppp->pp_mode & IFF_LINK2) &&
4084 !(sppp->pp_flags & PP_FR))
4085 # elif defined(__NetBSD__) || defined(__OpenBSD__)
4086 if (!(sppp->pp_flags & PP_CISCO))
4091 /* Callout from SPPP: */
4093 sppp_tlf(struct sppp *sppp)
4096 if (!(sppp->pp_mode & IFF_LINK2) &&
4097 !(sppp->pp_flags & PP_FR))
4098 # elif defined(__NetBSD__) || defined(__OpenBSD__)
4099 if (!(sppp->pp_flags & PP_CISCO))
4101 sppp->pp_down(sppp);
4106 /* Configure line protocol stuff.
4107 * Called by attach_card() during module init.
4108 * Called by core_ioctl() when lmcconfig writes sc->config.
4109 * Called by detach_card() during module shutdown.
4112 config_proto(softc_t *sc, struct config *config)
4114 /* Use line protocol stack instead of RAWIP mode. */
4115 if ((sc->config.line_pkg == PKG_RAWIP) &&
4116 (config->line_pkg != PKG_RAWIP))
4120 sppp_attach(sc->ifp);
4121 LMC_BPF_ATTACH(DLT_PPP, 4);
4122 sc->sppp->pp_tls = sppp_tls;
4123 sc->sppp->pp_tlf = sppp_tlf;
4124 /* Force reconfiguration of SPPP params. */
4125 sc->config.line_prot = 0;
4126 sc->config.keep_alive = config->keep_alive ? 0:1;
4129 sc->p2p->p2p_proto = 0; /* force p2p_attach */
4130 if ((error = p2p_attach(sc->p2p))) /* calls bpfattach() */
4132 printf("%s: p2p_attach() failed; error %d\n", NAME_UNIT, error);
4133 config->line_pkg = PKG_RAWIP; /* still in RAWIP mode */
4137 sc->p2p->p2p_mdmctl = p2p_mdmctl; /* set DTR */
4138 sc->p2p->p2p_getmdm = p2p_getmdm; /* get DCD */
4142 sc->net_dev->mtu = HDLC_MAX_MTU;
4143 if ((error = hdlc_open(sc->net_dev)))
4145 printf("%s: hdlc_open() failed; error %d\n", NAME_UNIT, error);
4146 printf("%s: Try 'sethdlc %s ppp'\n", NAME_UNIT, NAME_UNIT);
4147 config->line_pkg = PKG_RAWIP; /* still in RAWIP mode */
4149 #else /* no line protocol stack was configured */
4150 config->line_pkg = PKG_RAWIP; /* still in RAWIP mode */
4154 /* Bypass line protocol stack and return to RAWIP mode. */
4155 if ((sc->config.line_pkg != PKG_RAWIP) &&
4156 (config->line_pkg == PKG_RAWIP))
4160 sppp_flush(sc->ifp);
4161 sppp_detach(sc->ifp);
4162 setup_ifnet(sc->ifp);
4163 LMC_BPF_ATTACH(DLT_RAW, 0);
4166 if_qflush(&sc->p2p->p2p_isnd);
4167 if ((error = p2p_detach(sc->p2p)))
4169 printf("%s: p2p_detach() failed; error %d\n", NAME_UNIT, error);
4170 printf("%s: Try 'ifconfig %s down -remove'\n", NAME_UNIT, NAME_UNIT);
4171 config->line_pkg = PKG_P2P; /* not in RAWIP mode; still attached to P2P */
4175 setup_ifnet(sc->ifp);
4176 LMC_BPF_ATTACH(DLT_RAW, 0);
4179 hdlc_proto_detach(sc->hdlc_dev);
4180 hdlc_close(sc->net_dev);
4181 setup_netdev(sc->net_dev);
4187 if (config->line_pkg != PKG_RAWIP)
4189 /* Check for change to PPP protocol. */
4190 if ((sc->config.line_prot != PROT_PPP) &&
4191 (config->line_prot == PROT_PPP))
4194 # if (defined(__NetBSD__) || defined(__OpenBSD__))
4195 sc->sppp->pp_flags &= ~PP_CISCO;
4196 # elif defined(__FreeBSD__)
4197 sc->ifp->if_flags &= ~IFF_LINK2;
4198 sc->sppp->pp_flags &= ~PP_FR;
4200 LMC_BPF_ATTACH(DLT_PPP, 4);
4201 sppp_ioctl(sc->ifp, SIOCSIFFLAGS, NULL);
4205 # define DLT_C_HDLC DLT_PPP
4208 /* Check for change to C_HDLC protocol. */
4209 if ((sc->config.line_prot != PROT_C_HDLC) &&
4210 (config->line_prot == PROT_C_HDLC))
4213 # if (defined(__NetBSD__) || defined(__OpenBSD__))
4214 sc->sppp->pp_flags |= PP_CISCO;
4215 # elif defined(__FreeBSD__)
4216 sc->ifp->if_flags |= IFF_LINK2;
4217 sc->sppp->pp_flags &= ~PP_FR;
4219 LMC_BPF_ATTACH(DLT_C_HDLC, 4);
4220 sppp_ioctl(sc->ifp, SIOCSIFFLAGS, NULL);
4223 /* Check for change to Frame Relay protocol. */
4224 if ((sc->config.line_prot != PROT_FRM_RLY) &&
4225 (config->line_prot == PROT_FRM_RLY))
4228 # if (defined(__NetBSD__) || defined(__OpenBSD__))
4229 sc->sppp->pp_flags &= ~PP_CISCO;
4230 # elif defined(__FreeBSD__)
4231 sc->ifp->if_flags &= ~IFF_LINK2;
4232 sc->sppp->pp_flags |= PP_FR;
4234 LMC_BPF_ATTACH(DLT_FRELAY, 4);
4235 sppp_ioctl(sc->ifp, SIOCSIFFLAGS, NULL);
4238 /* Check for disabling keep-alives. */
4239 if ((sc->config.keep_alive != 0) &&
4240 (config->keep_alive == 0))
4241 sc->sppp->pp_flags &= ~PP_KEEPALIVE;
4243 /* Check for enabling keep-alives. */
4244 if ((sc->config.keep_alive == 0) &&
4245 (config->keep_alive != 0))
4246 sc->sppp->pp_flags |= PP_KEEPALIVE;
4251 /* Loop back through the TULIP Ethernet chip; (no CRC). */
4252 /* Data sheet says stop DMA before changing OPMODE register. */
4253 /* But that's not as simple as it sounds; works anyway. */
4254 /* Check for enabling loopback thru Tulip chip. */
4255 if ((sc->config.loop_back != CFG_LOOP_TULIP) &&
4256 (config->loop_back == CFG_LOOP_TULIP))
4258 u_int32_t op_mode = READ_CSR(TLP_OP_MODE);
4259 op_mode |= TLP_OP_INT_LOOP;
4260 WRITE_CSR(TLP_OP_MODE, op_mode);
4261 config->crc_len = CFG_CRC_0;
4264 /* Check for disabling loopback thru Tulip chip. */
4265 if ((sc->config.loop_back == CFG_LOOP_TULIP) &&
4266 (config->loop_back != CFG_LOOP_TULIP))
4268 u_int32_t op_mode = READ_CSR(TLP_OP_MODE);
4269 op_mode &= ~TLP_OP_LOOP_MODE;
4270 WRITE_CSR(TLP_OP_MODE, op_mode);
4271 config->crc_len = CFG_CRC_16;
4275 /* This is the core ioctl procedure. */
4276 /* It handles IOCTLs from lmcconfig(8). */
4277 /* It must not run when card watchdogs run. */
4278 /* Called from a syscall (user context; no spinlocks). */
4279 /* This procedure can SLEEP. */
4281 core_ioctl(softc_t *sc, u_long cmd, caddr_t data)
4283 struct iohdr *iohdr = (struct iohdr *) data;
4284 struct ioctl *ioctl = (struct ioctl *) data;
4285 struct status *status = (struct status *) data;
4286 struct config *config = (struct config *) data;
4289 /* All structs start with a string and a cookie. */
4290 if (((struct iohdr *)data)->cookie != NGM_LMC_COOKIE)
4293 while (TOP_TRYLOCK == 0)
4295 sc->status.cntrs.lck_ioctl++;
4296 SLEEP(10000); /* yield? */
4302 *status = sc->status;
4303 iohdr->cookie = NGM_LMC_COOKIE;
4308 *config = sc->config;
4309 iohdr->cookie = NGM_LMC_COOKIE;
4314 if ((error = CHECK_CAP)) break;
4315 config_proto(sc, config);
4316 sc->config = *config;
4317 sc->card->config(sc);
4322 if (ioctl->cmd == IOCTL_RW_PCI)
4324 if (ioctl->address > 252) { error = EFAULT; break; }
4325 ioctl->data = READ_PCI_CFG(sc, ioctl->address);
4327 else if (ioctl->cmd == IOCTL_RW_CSR)
4329 if (ioctl->address > 15) { error = EFAULT; break; }
4330 ioctl->data = READ_CSR(ioctl->address*TLP_CSR_STRIDE);
4332 else if (ioctl->cmd == IOCTL_RW_SROM)
4334 if (ioctl->address > 63) { error = EFAULT; break; }
4335 ioctl->data = read_srom(sc, ioctl->address);
4337 else if (ioctl->cmd == IOCTL_RW_BIOS)
4338 ioctl->data = read_bios(sc, ioctl->address);
4339 else if (ioctl->cmd == IOCTL_RW_MII)
4340 ioctl->data = read_mii(sc, ioctl->address);
4341 else if (ioctl->cmd == IOCTL_RW_FRAME)
4342 ioctl->data = read_framer(sc, ioctl->address);
4349 if ((error = CHECK_CAP)) break;
4350 if (ioctl->cmd == IOCTL_RW_PCI)
4352 if (ioctl->address > 252) { error = EFAULT; break; }
4353 WRITE_PCI_CFG(sc, ioctl->address, ioctl->data);
4355 else if (ioctl->cmd == IOCTL_RW_CSR)
4357 if (ioctl->address > 15) { error = EFAULT; break; }
4358 WRITE_CSR(ioctl->address*TLP_CSR_STRIDE, ioctl->data);
4360 else if (ioctl->cmd == IOCTL_RW_SROM)
4362 if (ioctl->address > 63) { error = EFAULT; break; }
4363 write_srom(sc, ioctl->address, ioctl->data); /* can sleep */
4365 else if (ioctl->cmd == IOCTL_RW_BIOS)
4367 if (ioctl->address == 0) erase_bios(sc);
4368 write_bios(sc, ioctl->address, ioctl->data); /* can sleep */
4370 else if (ioctl->cmd == IOCTL_RW_MII)
4371 write_mii(sc, ioctl->address, ioctl->data);
4372 else if (ioctl->cmd == IOCTL_RW_FRAME)
4373 write_framer(sc, ioctl->address, ioctl->data);
4374 else if (ioctl->cmd == IOCTL_WO_SYNTH)
4375 write_synth(sc, (struct synth *)&ioctl->data);
4376 else if (ioctl->cmd == IOCTL_WO_DAC)
4378 write_dac(sc, 0x9002); /* set Vref = 2.048 volts */
4379 write_dac(sc, ioctl->data & 0xFFF);
4387 if ((error = CHECK_CAP)) break;
4388 if (ioctl->cmd == IOCTL_XILINX_RESET)
4391 sc->card->config(sc);
4393 else if (ioctl->cmd == IOCTL_XILINX_ROM)
4395 load_xilinx_from_rom(sc); /* can sleep */
4396 sc->card->config(sc);
4398 else if (ioctl->cmd == IOCTL_XILINX_FILE)
4400 /* load_xilinx_from_file() can sleep. */
4401 error = load_xilinx_from_file(sc, ioctl->ucode, ioctl->data);
4402 if (error != 0) load_xilinx_from_rom(sc); /* try the rom */
4403 sc->card->config(sc);
4404 set_status(sc, (error==0)); /* XXX */
4406 else if (ioctl->cmd == IOCTL_RESET_CNTRS)
4408 memset(&sc->status.cntrs, 0, sizeof(struct event_cntrs));
4409 microtime(&sc->status.cntrs.reset_time);
4412 error = sc->card->ioctl(sc, ioctl); /* can sleep */
4424 /* This is the core watchdog procedure. */
4425 /* It calculates link speed, and calls the card-specific watchdog code. */
4426 /* Calls interrupt() in case one got lost; also kick-starts the device. */
4427 /* ioctl syscalls and card watchdog routines must be interlocked. */
4428 /* This procedure must not sleep. */
4430 core_watchdog(softc_t *sc)
4432 /* Read and restart the Tulip timer. */
4433 u_int32_t tx_speed = READ_CSR(TLP_TIMER);
4434 WRITE_CSR(TLP_TIMER, 0xFFFF);
4436 /* Measure MII clock using a timer in the Tulip chip.
4437 * This timer counts transmitter bits divided by 4096.
4438 * Since this is called once a second the math is easy.
4439 * This is only correct when the link is NOT sending pkts.
4440 * On a fully-loaded link, answer will be HALF actual rate.
4441 * Clock rate during pkt is HALF clk rate between pkts.
4442 * Measuring clock rate really measures link utilization!
4444 sc->status.tx_speed = (0xFFFF - (tx_speed & 0xFFFF)) << 12;
4446 /* The first status reset time is when the calendar clock is set. */
4447 if (sc->status.cntrs.reset_time.tv_sec < 1000)
4448 microtime(&sc->status.cntrs.reset_time);
4450 /* Update hardware (operational) status. */
4451 /* Call the card-specific watchdog routines. */
4452 if (TOP_TRYLOCK != 0)
4454 sc->status.oper_status = sc->card->watchdog(sc);
4456 /* Increment a counter which tells user-land */
4457 /* observers that SNMP state has been updated. */
4463 sc->status.cntrs.lck_watch++;
4465 /* In case an interrupt gets lost... */
4466 user_interrupt(sc, 1);
4471 /* Called from a syscall (user context; no spinlocks). */
4473 lmc_raw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
4475 struct ifreq *ifr = (struct ifreq *) data;
4480 # if (defined(__FreeBSD__) && defined(DEVICE_POLLING)) /* XXX necessary? */
4483 case SIOCSIFDSTADDR:
4492 ifp->if_flags |= IFF_UP; /* a Unix tradition */
4495 ifp->if_mtu = ifr->ifr_mtu;
4504 /* Called from a syscall (user context; no spinlocks). */
4506 lmc_ifnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
4508 softc_t *sc = IFP2SC(ifp);
4510 struct ifreq *ifr = (struct ifreq *) data;
4516 /* Catch the IOCTLs used by lmcconfig. */
4523 error = core_ioctl(sc, cmd, data);
4526 /* Catch the IOCTLs used by ifconfig. */
4528 if ((error = CHECK_CAP)) break;
4530 error = ifmedia_ioctl(ifp, ifr, &sc->ifm, cmd);
4532 case SIOCSIFTIMESLOT:
4533 if ((error = CHECK_CAP)) break;
4534 if (sc->status.card_type == TLP_CSID_T1E1)
4536 struct config config = sc->config;
4537 if ((error = copyin(ifr->ifr_data, &config.time_slots,
4538 sizeof config.time_slots))) break;
4539 config.iohdr.cookie = NGM_LMC_COOKIE;
4540 error = core_ioctl(sc, LMCIOCSCFG, (caddr_t)&config);
4545 case SIOCGIFTIMESLOT:
4546 if (sc->status.card_type == TLP_CSID_T1E1)
4547 error = copyout(&sc->config.time_slots, ifr->ifr_data,
4548 sizeof sc->config.time_slots);
4553 /* Pass the rest to the line protocol. */
4555 if (sc->config.line_pkg == PKG_RAWIP)
4556 error = lmc_raw_ioctl(ifp, cmd, data);
4559 error = sppp_ioctl(ifp, cmd, data);
4561 error = p2p_ioctl(ifp, cmd, data);
4568 if (DRIVER_DEBUG && (error!=0))
4569 printf("%s: lmc_ifnet_ioctl; cmd=0x%08lx error=%d\n",
4570 NAME_UNIT, cmd, error);
4575 /* Called from a syscall (user context; no spinlocks). */
4577 lmc_ifnet_start(struct ifnet *ifp)
4579 softc_t *sc = IFP2SC(ifp);
4581 /* Start the transmitter; incoming pkts are NOT processed. */
4582 user_interrupt(sc, 0);
4585 /* sppp and p2p replace this with their own proc. */
4586 /* RAWIP mode is the only time this is used. */
4587 /* Called from a syscall (user context; no spinlocks). */
4589 lmc_raw_output(struct ifnet *ifp, struct mbuf *m,
4590 struct sockaddr *dst, struct route *ro)
4592 softc_t *sc = IFP2SC(ifp);
4595 /* Fail if the link is down. */
4596 if (sc->status.oper_status != STATUS_UP)
4599 sc->status.cntrs.odiscards++;
4601 printf("%s: lmc_raw_output: tx pkt discarded: link down\n", NAME_UNIT);
4606 /* Netgraph has priority over the ifnet kernel interface. */
4607 if (sc->ng_hook != NULL)
4610 sc->status.cntrs.odiscards++;
4612 printf("%s: lmc_raw_output: tx pkt discarded: netgraph active\n",
4618 /* lmc_raw_output() ENQUEUEs in a syscall or softirq. */
4619 /* txintr_setup() DEQUEUEs in a hard interrupt. */
4620 /* Some BSD QUEUE routines are not interrupt-safe. */
4623 # if (__FreeBSD_version >= 503000)
4624 IFQ_ENQUEUE(&ifp->if_snd, m, error);
4626 IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
4632 user_interrupt(sc, 0); /* start the transmitter */
4636 sc->status.cntrs.odiscards++;
4638 printf("%s: lmc_raw_output: IFQ_ENQUEUE() failed; error %d\n",
4645 /* Called from a softirq once a second. */
4647 lmc_watchdog(void *arg)
4649 struct ifnet *ifp = arg;
4650 softc_t *sc = IFP2SC(ifp);
4651 u_int8_t old_oper_status = sc->status.oper_status;
4652 struct event_cntrs *cntrs = &sc->status.cntrs;
4654 core_watchdog(sc); /* updates oper_status */
4657 if (sc->ng_hook != NULL)
4659 sc->status.line_pkg = PKG_NG;
4660 sc->status.line_prot = 0;
4664 if (sc->config.line_pkg == PKG_RAWIP)
4666 sc->status.line_pkg = PKG_RAWIP;
4667 sc->status.line_prot = PROT_IP_HDLC;
4672 /* Notice change in link status. */
4673 if ((old_oper_status != sc->status.oper_status) && (sc->p2p->p2p_modem))
4674 (*sc->p2p->p2p_modem)(sc->p2p, sc->status.oper_status==STATUS_UP);
4676 /* Notice change in line protocol. */
4677 sc->status.line_pkg = PKG_P2P;
4678 switch (sc->ifp->if_type)
4681 sc->status.line_prot = PROT_PPP;
4684 sc->status.line_prot = PROT_C_HDLC;
4687 sc->status.line_prot = PROT_FRM_RLY;
4690 sc->status.line_prot = 0;
4695 /* Notice change in link status. */
4696 if ((old_oper_status != STATUS_UP) &&
4697 (sc->status.oper_status == STATUS_UP)) /* link came up */
4699 if ((old_oper_status == STATUS_UP) &&
4700 (sc->status.oper_status != STATUS_UP)) /* link went down */
4703 /* Notice change in line protocol. */
4704 sc->status.line_pkg = PKG_SPPP;
4706 if (sc->sppp->pp_flags & PP_FR)
4707 sc->status.line_prot = PROT_FRM_RLY;
4708 else if (sc->ifp->if_flags & IFF_LINK2)
4709 # elif (defined(__NetBSD__) || defined(__OpenBSD__))
4710 if (sc->sppp->pp_flags & PP_CISCO)
4712 sc->status.line_prot = PROT_C_HDLC;
4714 sc->status.line_prot = PROT_PPP;
4717 /* Suppress compiler warning. */
4718 if (old_oper_status == STATUS_UP);
4722 /* Copy statistics from sc to ifp. */
4723 ifp->if_baudrate = sc->status.tx_speed;
4724 ifp->if_ipackets = cntrs->ipackets;
4725 ifp->if_opackets = cntrs->opackets;
4726 ifp->if_ibytes = cntrs->ibytes;
4727 ifp->if_obytes = cntrs->obytes;
4728 ifp->if_ierrors = cntrs->ierrors;
4729 ifp->if_oerrors = cntrs->oerrors;
4730 ifp->if_iqdrops = cntrs->idiscards;
4732 # if ((__FreeBSD_version >= 500000) || defined(__OpenBSD__) || defined(__NetBSD__))
4733 if (sc->status.oper_status == STATUS_UP)
4734 ifp->if_link_state = LINK_STATE_UP;
4736 ifp->if_link_state = LINK_STATE_DOWN;
4739 /* Call this procedure again after one second. */
4740 callout_reset(&sc->callout, hz, lmc_watchdog, ifp);
4745 /* Callback from ifmedia. */
4747 ifmedia_change(struct ifnet *ifp)
4749 softc_t *sc = IFP2SC(ifp);
4750 struct config config = sc->config;
4751 int media = sc->ifm.ifm_media;
4754 /* ifconfig lmc0 media t1 */
4755 if (sc->status.card_type == TLP_CSID_T3)
4757 if ((media & IFM_TMASK) == IFM_TDM_T3)
4758 config.format = CFG_FORMAT_T3CPAR;
4759 else if ((media & IFM_TMASK) == IFM_TDM_T3_M13)
4760 config.format = CFG_FORMAT_T3M13;
4762 else if (sc->status.card_type == TLP_CSID_T1E1)
4764 if ((media & IFM_TMASK) == IFM_TDM_T1)
4765 config.format = CFG_FORMAT_T1ESF;
4766 else if ((media & IFM_TMASK) == IFM_TDM_T1_AMI)
4767 config.format = CFG_FORMAT_T1SF;
4768 else if ((media & IFM_TMASK) == IFM_TDM_E1)
4769 config.format = CFG_FORMAT_E1NONE;
4770 else if ((media & IFM_TMASK) == IFM_TDM_E1_G704)
4771 config.format = CFG_FORMAT_E1FASCRC;
4774 /* ifconfig lmc0 mediaopt loopback */
4775 if (media & IFM_LOOP)
4776 config.loop_back = CFG_LOOP_TULIP;
4778 config.loop_back = CFG_LOOP_NONE;
4780 /* ifconfig lmc0 mediaopt crc16 */
4781 if (media & IFM_TDM_HDLC_CRC16)
4782 config.crc_len = CFG_CRC_16;
4784 config.crc_len = CFG_CRC_32;
4786 /* Set ConFiGuration. */
4787 config.iohdr.cookie = NGM_LMC_COOKIE;
4788 error = core_ioctl(sc, LMCIOCSCFG, (caddr_t)&config);
4793 /* Callback from ifmedia. */
4795 ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
4797 softc_t *sc = IFP2SC(ifp);
4799 /* ifconfig wants to know if the hardware link is up. */
4800 ifmr->ifm_status = IFM_AVALID;
4801 if (sc->status.oper_status == STATUS_UP)
4802 ifmr->ifm_status |= IFM_ACTIVE;
4804 ifmr->ifm_active = sc->ifm.ifm_cur->ifm_media;
4806 if (sc->config.loop_back != CFG_LOOP_NONE)
4807 ifmr->ifm_active |= IFM_LOOP;
4809 if (sc->config.crc_len == CFG_CRC_16)
4810 ifmr->ifm_active |= IFM_TDM_HDLC_CRC16;
4813 # endif /* __OpenBSD__ */
4816 setup_ifnet(struct ifnet *ifp)
4818 softc_t *sc = ifp->if_softc;
4820 /* Initialize the generic network interface. */
4821 /* Note similarity to linux's setup_netdev(). */
4822 ifp->if_flags = IFF_POINTOPOINT;
4823 ifp->if_flags |= IFF_RUNNING;
4824 ifp->if_ioctl = lmc_ifnet_ioctl;
4825 ifp->if_start = lmc_ifnet_start; /* sppp changes this */
4826 ifp->if_output = lmc_raw_output; /* sppp & p2p change this */
4827 ifp->if_input = lmc_raw_input;
4828 ifp->if_mtu = MAX_DESC_LEN; /* sppp & p2p change this */
4829 ifp->if_type = IFT_PTPSERIAL; /* p2p changes this */
4831 # if (defined(__FreeBSD__) && defined(DEVICE_POLLING))
4832 ifp->if_capabilities |= IFCAP_POLLING;
4833 ifp->if_capenable |= IFCAP_POLLING_NOCOUNT;
4834 # if (__FreeBSD_version < 500000)
4835 ifp->if_capenable |= IFCAP_POLLING;
4839 /* Every OS does it differently! */
4840 # if (defined(__FreeBSD__) && (__FreeBSD_version < 502000))
4841 (const char *)ifp->if_name = device_get_name(sc->dev);
4842 ifp->if_unit = device_get_unit(sc->dev);
4843 # elif (__FreeBSD_version >= 502000)
4844 if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
4845 # elif defined(__NetBSD__)
4846 strcpy(ifp->if_xname, sc->dev.dv_xname);
4847 # elif defined(__OpenBSD__)
4848 bcopy(sc->dev.dv_xname, ifp->if_xname, IFNAMSIZ);
4849 # elif defined(__bsdi__)
4850 ifp->if_name = sc->dev.dv_cfdata->cf_driver->cd_name;
4851 ifp->if_unit = sc->dev.dv_unit;
4856 lmc_ifnet_attach(softc_t *sc)
4858 # if (__FreeBSD_version >= 600000)
4859 sc->ifp = if_alloc(NSPPP ? IFT_PPP : IFT_OTHER);
4860 if (sc->ifp == NULL) return ENOMEM;
4863 # if (__FreeBSD_version >= 600000)
4864 sc->sppp = sc->ifp->if_l2com;
4866 sc->ifp = &sc->spppcom.pp_if;
4867 sc->sppp = &sc->spppcom;
4870 sc->ifp = &sc->p2pcom.p2p_if;
4871 sc->p2p = &sc->p2pcom;
4872 # elif (__FreeBSD_version < 600000)
4873 sc->ifp = &sc->ifnet;
4876 /* Initialize the network interface struct. */
4877 sc->ifp->if_softc = sc;
4878 setup_ifnet(sc->ifp);
4880 /* ALTQ output queue initialization. */
4881 IFQ_SET_MAXLEN(&sc->ifp->if_snd, SNDQ_MAXLEN);
4882 IFQ_SET_READY(&sc->ifp->if_snd);
4884 /* Attach to the ifnet kernel interface. */
4887 # if ((defined(__NetBSD__) && __NetBSD_Version__ >= 106000000) || \
4888 (defined(__OpenBSD__) && OpenBSD >= 200211))
4889 if_alloc_sadl(sc->ifp);
4892 /* Attach Berkeley Packet Filter. */
4893 LMC_BPF_ATTACH(DLT_RAW, 0);
4896 /* Initialize ifmedia mechanism. */
4897 ifmedia_init(&sc->ifm, IFM_OMASK | IFM_GMASK | IFM_IMASK,
4898 ifmedia_change, ifmedia_status);
4899 if (sc->status.card_type == TLP_CSID_T3)
4901 ifmedia_add(&sc->ifm, IFM_TDM | IFM_TDM_T3, 0, NULL);
4902 ifmedia_add(&sc->ifm, IFM_TDM | IFM_TDM_T3_M13, 0, NULL);
4903 ifmedia_set(&sc->ifm, IFM_TDM | IFM_TDM_T3);
4905 else if (sc->status.card_type == TLP_CSID_T1E1)
4907 ifmedia_add(&sc->ifm, IFM_TDM | IFM_TDM_T1, 0, NULL);
4908 ifmedia_add(&sc->ifm, IFM_TDM | IFM_TDM_T1_AMI, 0, NULL);
4909 ifmedia_add(&sc->ifm, IFM_TDM | IFM_TDM_E1, 0, NULL);
4910 ifmedia_add(&sc->ifm, IFM_TDM | IFM_TDM_E1_G704, 0, NULL);
4911 ifmedia_set(&sc->ifm, IFM_TDM | IFM_TDM_T1);
4913 else if ((sc->status.card_type == TLP_CSID_HSSI) ||
4914 (sc->status.card_type == TLP_CSID_SSI))
4916 ifmedia_add(&sc->ifm, IFM_TDM | IFM_NONE, 0, NULL);
4917 ifmedia_set(&sc->ifm, IFM_TDM | IFM_NONE);
4919 # endif /* __OpenBSD__ */
4921 callout_reset(&sc->callout, hz, lmc_watchdog, sc);
4927 lmc_ifnet_detach(softc_t *sc)
4930 ifmedia_delete_instance(&sc->ifm, IFM_INST_ANY);
4933 # if (defined(__FreeBSD__) && defined(DEVICE_POLLING))
4934 if (sc->ifp->if_capenable & IFCAP_POLLING)
4935 ether_poll_deregister(sc->ifp);
4938 /* Detach Berkeley Packet Filter. */
4941 # if ((defined(__NetBSD__) && __NetBSD_Version__ >= 106000000) || \
4942 (defined(__OpenBSD__) && OpenBSD >= 200211))
4943 if_free_sadl(sc->ifp);
4946 /* Detach from the ifnet kernel interface. */
4949 # if (defined(__FreeBSD__) && __FreeBSD_version >= 800082)
4951 # elif (defined(__FreeBSD__) && __FreeBSD_version >= 600000)
4952 if_free_type(sc->ifp, NSPPP ? IFT_PPP : IFT_OTHER);
4960 /* Netgraph changed significantly between FreeBSD-4 and -5. */
4961 /* These are backward compatibility hacks for FreeBSD-4. */
4962 # if (__FreeBSD_version >= 500000)
4963 /* These next two macros should be added to netgraph */
4964 # define NG_TYPE_REF(type) atomic_add_int(&(type)->refs, 1)
4965 # define NG_TYPE_UNREF(type) \
4967 if ((type)->refs == 1) \
4970 atomic_subtract_int(&(type)->refs, 1); \
4972 # else /* FreeBSD-4 */
4973 # define NGI_GET_MSG(item, msg) /* nothing */
4974 # define NG_HOOK_FORCE_QUEUE(hook) /* nothing */
4975 # define NG_TYPE_REF(type) atomic_add_int(&(type)->refs, 1)
4976 # define NG_TYPE_UNREF(type) \
4978 if ((type)->refs == 1) \
4979 LIST_REMOVE(type, types); \
4981 atomic_subtract_int(&(type)->refs, 1); \
4985 /* It is an error to construct new copies of this Netgraph node. */
4986 /* All instances are constructed by ng_attach and are persistent. */
4987 # if (__FreeBSD_version >= 500000)
4988 static int ng_constructor(node_p node) { return EINVAL; }
4989 # else /* FreeBSD-4 */
4990 static int ng_constructor(node_p *node) { return EINVAL; }
4993 /* Incoming Netgraph control message. */
4994 # if (__FreeBSD_version >= 500000)
4996 ng_rcvmsg(node_p node, item_p item, hook_p lasthook)
4998 struct ng_mesg *msg;
4999 # else /* FreeBSD-4 */
5001 ng_rcvmsg(node_p node, struct ng_mesg *msg,
5002 const char *retaddr, struct ng_mesg **rptr)
5005 struct ng_mesg *resp = NULL;
5006 softc_t *sc = NG_NODE_PRIVATE(node);
5009 NGI_GET_MSG(item, msg);
5010 if (msg->header.typecookie == NGM_LMC_COOKIE)
5012 switch (msg->header.cmd)
5021 /* Call the core ioctl procedure. */
5022 error = core_ioctl(sc, msg->header.cmd, msg->data);
5023 if ((msg->header.cmd & IOC_OUT) != 0)
5024 { /* synchronous response */
5025 NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg) +
5026 IOCPARM_LEN(msg->header.cmd), M_NOWAIT);
5030 memcpy(resp->data, msg->data, IOCPARM_LEN(msg->header.cmd));
5039 else if ((msg->header.typecookie == NGM_GENERIC_COOKIE) &&
5040 (msg->header.cmd == NGM_TEXT_STATUS))
5041 { /* synchronous response */
5042 NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg) +
5043 NG_TEXTRESPONSE, M_NOWAIT);
5048 char *s = resp->data;
5049 sprintf(s, "Card type = <%s>\n"
5050 "This driver considers the link to be %s.\n"
5051 "Use lmcconfig to configure this interface.\n",
5052 sc->dev_desc, (sc->status.oper_status==STATUS_UP) ? "UP" : "DOWN");
5053 resp->header.arglen = strlen(s) +1;
5057 /* Netgraph should be able to read and write these
5058 * parameters with text-format control messages:
5061 * loop loop loop loop
5063 * dte dte format format
5064 * synth synth cablen cablen
5065 * cable timeslot scram
5069 * Someday I'll implement this...
5073 /* Handle synchronous response. */
5074 # if (__FreeBSD_version >= 500000)
5075 NG_RESPOND_MSG(error, node, item, resp);
5077 # else /* FreeBSD-4 */
5080 else if (resp != NULL)
5081 free(resp, M_NETGRAPH);
5082 free(msg, M_NETGRAPH);
5088 /* This is a persistent netgraph node. */
5090 ng_shutdown(node_p node)
5092 # if (__FreeBSD_version >= 500000)
5093 /* unless told to really die, bounce back to life */
5094 if ((node->nd_flags & NG_REALLY_DIE)==0)
5095 node->nd_flags &= ~NG_INVALID; /* bounce back to life */
5096 # else /* FreeBSD-4 */
5098 node->flags &= ~NG_INVALID; /* bounce back to life */
5104 /* ng_disconnect is the opposite of this procedure. */
5106 ng_newhook(node_p node, hook_p hook, const char *name)
5108 softc_t *sc = NG_NODE_PRIVATE(node);
5110 /* Hook name must be 'rawdata'. */
5111 if (strncmp(name, "rawdata", 7) != 0) return EINVAL;
5113 /* Is our hook connected? */
5114 if (sc->ng_hook != NULL) return EBUSY;
5116 /* Accept the hook. */
5122 /* Both ends have accepted their hooks and the links have been made. */
5123 /* This is the last chance to reject the connection request. */
5125 ng_connect(hook_p hook)
5127 /* Probably not at splnet, force outward queueing. (huh?) */
5128 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
5129 return 0; /* always accept */
5132 /* Receive data in mbufs from another Netgraph node. */
5133 /* Transmit an mbuf-chain on the communication link. */
5134 /* This procedure is very similar to lmc_raw_output(). */
5135 /* Called from a syscall (user context; no spinlocks). */
5136 # if (__FreeBSD_version >= 500000)
5138 ng_rcvdata(hook_p hook, item_p item)
5140 softc_t *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
5146 NGI_GET_META(item, meta);
5148 # else /* FreeBSD-4 */
5150 ng_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
5152 softc_t *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
5156 /* This macro must not store into meta! */
5159 /* Fail if the link is down. */
5160 if (sc->status.oper_status != STATUS_UP)
5163 sc->status.cntrs.odiscards++;
5165 printf("%s: ng_rcvdata: tx pkt discarded: link down\n", NAME_UNIT);
5169 /* ng_rcvdata() ENQUEUEs in a syscall or softirq. */
5170 /* txintr_setup() DEQUEUEs in a hard interrupt. */
5171 /* Some BSD QUEUE routines are not interrupt-safe. */
5174 # if (__FreeBSD_version >= 503000)
5176 IFQ_ENQUEUE(&sc->ng_sndq, m, error);
5178 IFQ_ENQUEUE(&sc->ng_fastq, m, error);
5181 IFQ_ENQUEUE(&sc->ng_sndq, m, NULL, error);
5183 IFQ_ENQUEUE(&sc->ng_fastq, m, NULL, error);
5189 user_interrupt(sc, 0); /* start the transmitter */
5193 sc->status.cntrs.odiscards++;
5195 printf("%s: ng_rcvdata: IFQ_ENQUEUE() failed; error %d\n",
5202 /* ng_newhook is the opposite of this procedure, not */
5203 /* ng_connect, as you might expect from the names. */
5205 ng_disconnect(hook_p hook)
5207 softc_t *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
5209 /* Disconnect the hook. */
5216 struct ng_type ng_type =
5218 .version = NG_ABI_VERSION,
5219 .name = NG_LMC_NODE_TYPE,
5221 .constructor = ng_constructor,
5222 .rcvmsg = ng_rcvmsg,
5223 # if (__FreeBSD_version >=503000)
5226 .shutdown = ng_shutdown,
5227 .newhook = ng_newhook,
5229 .connect = ng_connect,
5230 .rcvdata = ng_rcvdata,
5231 # if (defined(__FreeBSD__) && (__FreeBSD_version < 500000))
5232 .rcvdataq = ng_rcvdata,
5234 .disconnect = ng_disconnect,
5238 /* Called from a softirq once a second. */
5240 ng_watchdog(void *arg)
5244 /* Call the core watchdog procedure. */
5247 /* Set line protocol and package status. */
5248 sc->status.line_pkg = PKG_NG;
5249 sc->status.line_prot = 0;
5251 /* Call this procedure again after one second. */
5252 callout_reset(&sc->callout, hz, ng_watchdog, sc);
5256 /* Attach to the Netgraph kernel interface (/sys/netgraph).
5257 * It is called once for each physical card during device attach.
5258 * This is effectively ng_constructor.
5261 ng_attach(softc_t *sc)
5265 /* If this node type is not known to Netgraph then register it. */
5266 if (ng_type.refs == 0) /* or: if (ng_findtype(&ng_type) == NULL) */
5268 if ((error = ng_newtype(&ng_type)))
5270 printf("%s: ng_newtype() failed; error %d\n", NAME_UNIT, error);
5275 NG_TYPE_REF(&ng_type);
5277 /* Call the superclass node constructor. */
5278 if ((error = ng_make_node_common(&ng_type, &sc->ng_node)))
5280 NG_TYPE_UNREF(&ng_type);
5281 printf("%s: ng_make_node_common() failed; error %d\n", NAME_UNIT, error);
5285 /* Associate a name with this netgraph node. */
5286 if ((error = ng_name_node(sc->ng_node, NAME_UNIT)))
5288 NG_NODE_UNREF(sc->ng_node);
5289 NG_TYPE_UNREF(&ng_type);
5290 printf("%s: ng_name_node() failed; error %d\n", NAME_UNIT, error);
5294 # if (__FreeBSD_version >= 500000)
5295 /* Initialize the send queue mutexes. */
5296 mtx_init(&sc->ng_sndq.ifq_mtx, NAME_UNIT, "sndq", MTX_DEF);
5297 mtx_init(&sc->ng_fastq.ifq_mtx, NAME_UNIT, "fastq", MTX_DEF);
5300 /* Put a backpointer to the softc in the netgraph node. */
5301 NG_NODE_SET_PRIVATE(sc->ng_node, sc);
5303 /* ALTQ output queue initialization. */
5304 IFQ_SET_MAXLEN(&sc->ng_fastq, SNDQ_MAXLEN);
5305 IFQ_SET_READY(&sc->ng_fastq);
5306 IFQ_SET_MAXLEN(&sc->ng_sndq, SNDQ_MAXLEN);
5307 IFQ_SET_READY(&sc->ng_sndq);
5310 /* Arrange to call ng_watchdog() once a second. */
5311 callout_reset(&sc->callout, hz, ng_watchdog, sc);
5318 ng_detach(softc_t *sc)
5320 callout_drain(&sc->callout);
5321 # if (__FreeBSD_version >= 500000)
5322 mtx_destroy(&sc->ng_sndq.ifq_mtx);
5323 mtx_destroy(&sc->ng_fastq.ifq_mtx);
5324 ng_rmnode_self(sc->ng_node); /* free hook */
5325 NG_NODE_UNREF(sc->ng_node); /* free node */
5326 NG_TYPE_UNREF(&ng_type);
5327 # else /* FreeBSD-4 */
5328 ng_unname(sc->ng_node); /* free name */
5329 ng_cutlinks(sc->ng_node); /* free hook */
5330 NG_NODE_UNREF(sc->ng_node); /* free node */
5331 NG_TYPE_UNREF(&ng_type);
5335 #endif /* NETGRAPH */
5337 /* The next few procedures initialize the card. */
5339 /* Returns 0 on success; error code on failure. */
5341 startup_card(softc_t *sc)
5343 int num_rx_descs, error = 0;
5344 u_int32_t tlp_bus_pbl, tlp_bus_cal, tlp_op_tr;
5345 u_int32_t tlp_cfdd, tlp_cfcs;
5346 u_int32_t tlp_cflt, tlp_csid, tlp_cfit;
5348 /* Make sure the COMMAND bits are reasonable. */
5349 tlp_cfcs = READ_PCI_CFG(sc, TLP_CFCS);
5350 tlp_cfcs &= ~TLP_CFCS_MWI_ENABLE;
5351 tlp_cfcs |= TLP_CFCS_BUS_MASTER;
5352 tlp_cfcs |= TLP_CFCS_MEM_ENABLE;
5353 tlp_cfcs |= TLP_CFCS_IO_ENABLE;
5354 tlp_cfcs |= TLP_CFCS_PAR_ERROR;
5355 tlp_cfcs |= TLP_CFCS_SYS_ERROR;
5356 WRITE_PCI_CFG(sc, TLP_CFCS, tlp_cfcs);
5358 /* Set the LATENCY TIMER to the recommended value, */
5359 /* and make sure the CACHE LINE SIZE is reasonable. */
5360 tlp_cfit = READ_PCI_CFG(sc, TLP_CFIT);
5361 tlp_cflt = READ_PCI_CFG(sc, TLP_CFLT);
5362 tlp_cflt &= ~TLP_CFLT_LATENCY;
5363 tlp_cflt |= (tlp_cfit & TLP_CFIT_MAX_LAT)>>16;
5364 /* "prgmbl burst length" and "cache alignment" used below. */
5365 switch(tlp_cflt & TLP_CFLT_CACHE)
5367 case 8: /* 8 bytes per cache line */
5368 { tlp_bus_pbl = 32; tlp_bus_cal = 1; break; }
5370 { tlp_bus_pbl = 32; tlp_bus_cal = 2; break; }
5372 { tlp_bus_pbl = 32; tlp_bus_cal = 3; break; }
5375 tlp_bus_pbl = 32; tlp_bus_cal = 1;
5376 tlp_cflt &= ~TLP_CFLT_CACHE;
5381 WRITE_PCI_CFG(sc, TLP_CFLT, tlp_cflt);
5383 /* Make sure SNOOZE and SLEEP modes are disabled. */
5384 tlp_cfdd = READ_PCI_CFG(sc, TLP_CFDD);
5385 tlp_cfdd &= ~TLP_CFDD_SLEEP;
5386 tlp_cfdd &= ~TLP_CFDD_SNOOZE;
5387 WRITE_PCI_CFG(sc, TLP_CFDD, tlp_cfdd);
5388 DELAY(11*1000); /* Tulip wakes up in 10 ms max */
5390 /* Software Reset the Tulip chip; stops DMA and Interrupts. */
5391 /* This does not change the PCI config regs just set above. */
5392 WRITE_CSR(TLP_BUS_MODE, TLP_BUS_RESET); /* self-clearing */
5393 DELAY(5); /* Tulip is dead for 50 PCI cycles after reset. */
5395 /* Reset the Xilinx Field Programmable Gate Array. */
5396 reset_xilinx(sc); /* side effect: turns on all four LEDs */
5398 /* Configure card-specific stuff (framers, line interfaces, etc.). */
5399 sc->card->config(sc);
5401 /* Initializing cards can glitch clocks and upset fifos. */
5402 /* Reset the FIFOs between the Tulip and Xilinx chips. */
5403 set_mii16_bits(sc, MII16_FIFO);
5404 clr_mii16_bits(sc, MII16_FIFO);
5406 /* Initialize the PCI busmode register. */
5407 /* The PCI bus cycle type "Memory Write and Invalidate" does NOT */
5408 /* work cleanly in any version of the 21140A, so don't enable it! */
5409 WRITE_CSR(TLP_BUS_MODE,
5410 (tlp_bus_cal ? TLP_BUS_READ_LINE : 0) |
5411 (tlp_bus_cal ? TLP_BUS_READ_MULT : 0) |
5412 (tlp_bus_pbl<<TLP_BUS_PBL_SHIFT) |
5413 (tlp_bus_cal<<TLP_BUS_CAL_SHIFT) |
5414 ((BYTE_ORDER == BIG_ENDIAN) ? TLP_BUS_DESC_BIGEND : 0) |
5415 ((BYTE_ORDER == BIG_ENDIAN) ? TLP_BUS_DATA_BIGEND : 0) |
5419 /* Pick number of RX descriptors and TX fifo threshold. */
5420 /* tx_threshold in bytes: 0=128, 1=256, 2=512, 3=1024 */
5421 tlp_csid = READ_PCI_CFG(sc, TLP_CSID);
5424 case TLP_CSID_HSSI: /* 52 Mb/s */
5425 case TLP_CSID_HSSIc: /* 52 Mb/s */
5426 case TLP_CSID_T3: /* 45 Mb/s */
5427 { num_rx_descs = 48; tlp_op_tr = 2; break; }
5428 case TLP_CSID_SSI: /* 10 Mb/s */
5429 { num_rx_descs = 32; tlp_op_tr = 1; break; }
5430 case TLP_CSID_T1E1: /* 2 Mb/s */
5431 { num_rx_descs = 16; tlp_op_tr = 0; break; }
5433 { num_rx_descs = 16; tlp_op_tr = 0; break; }
5436 /* Create DMA descriptors and initialize list head registers. */
5437 if ((error = create_ring(sc, &sc->txring, NUM_TX_DESCS))) return error;
5438 WRITE_CSR(TLP_TX_LIST, sc->txring.dma_addr);
5439 if ((error = create_ring(sc, &sc->rxring, num_rx_descs))) return error;
5440 WRITE_CSR(TLP_RX_LIST, sc->rxring.dma_addr);
5442 /* Initialize the operating mode register. */
5443 WRITE_CSR(TLP_OP_MODE, TLP_OP_INIT | (tlp_op_tr<<TLP_OP_TR_SHIFT));
5445 /* Read the missed frame register (result ignored) to zero it. */
5446 error = READ_CSR( TLP_MISSED); /* error is used as a bit-dump */
5448 /* Disable rx watchdog and tx jabber features. */
5449 WRITE_CSR(TLP_WDOG, TLP_WDOG_INIT);
5451 /* Enable card interrupts. */
5452 WRITE_CSR(TLP_INT_ENBL, TLP_INT_TXRX);
5457 /* Stop DMA and Interrupts; free descriptors and buffers. */
5459 shutdown_card(void *arg)
5463 /* Leave the LEDs in the state they were in after power-on. */
5464 led_on(sc, MII16_LED_ALL);
5466 /* Software reset the Tulip chip; stops DMA and Interrupts */
5467 WRITE_CSR(TLP_BUS_MODE, TLP_BUS_RESET); /* self-clearing */
5468 DELAY(5); /* Tulip is dead for 50 PCI cycles after reset. */
5470 /* Disconnect from the PCI bus except for config cycles. */
5471 /* Hmmm; Linux syslogs a warning that IO and MEM are disabled. */
5472 WRITE_PCI_CFG(sc, TLP_CFCS, TLP_CFCS_MEM_ENABLE | TLP_CFCS_IO_ENABLE);
5474 /* Free the DMA descriptor rings. */
5475 destroy_ring(sc, &sc->txring);
5476 destroy_ring(sc, &sc->rxring);
5479 /* Start the card and attach a kernel interface and line protocol. */
5481 attach_card(softc_t *sc, const char *intrstr)
5483 struct config config;
5489 /* Start the card. */
5490 if ((error = startup_card(sc))) return error;
5492 # if (__FreeBSD_version >= 500000)
5493 callout_init(&sc->callout, 0);
5494 # else /* FreeBSD-4 */
5495 callout_init(&sc->callout);
5498 /* Attach a kernel interface. */
5500 if ((error = ng_attach(sc))) return error;
5501 sc->flags |= FLAG_NETGRAPH;
5504 if ((error = lmc_ifnet_attach(sc))) return error;
5505 sc->flags |= FLAG_IFNET;
5508 /* Attach a line protocol stack. */
5509 sc->config.line_pkg = PKG_RAWIP;
5510 config = sc->config; /* get current config */
5511 config.line_pkg = 0; /* select external stack */
5512 config.line_prot = PROT_C_HDLC;
5513 config.keep_alive = 1;
5514 config_proto(sc, &config); /* reconfigure */
5515 sc->config = config; /* save new configuration */
5517 /* Print interesting hardware-related things. */
5518 mii3 = read_mii(sc, 3);
5519 tlp_cfrv = READ_PCI_CFG(sc, TLP_CFRV);
5520 printf("%s: PCI rev %d.%d, MII rev %d.%d", NAME_UNIT,
5521 (tlp_cfrv>>4) & 0xF, tlp_cfrv & 0xF, (mii3>>4) & 0xF, mii3 & 0xF);
5522 ieee = (u_int8_t *)sc->status.ieee;
5523 for (i=0; i<3; i++) sc->status.ieee[i] = read_srom(sc, 10+i);
5524 printf(", IEEE addr %02x:%02x:%02x:%02x:%02x:%02x",
5525 ieee[0], ieee[1], ieee[2], ieee[3], ieee[4], ieee[5]);
5526 sc->card->ident(sc);
5527 printf(" %s\n", intrstr);
5529 /* Print interesting software-related things. */
5530 printf("%s: Driver rev %d.%d.%d", NAME_UNIT,
5531 DRIVER_MAJOR_VERSION, DRIVER_MINOR_VERSION, DRIVER_SUB_VERSION);
5532 printf(", Options %s%s%s%s%s%s%s%s%s\n",
5533 NETGRAPH ? "NETGRAPH " : "", GEN_HDLC ? "GEN_HDLC " : "",
5534 NSPPP ? "SPPP " : "", P2P ? "P2P " : "",
5535 ALTQ_PRESENT ? "ALTQ " : "", NBPFILTER ? "BPF " : "",
5536 DEV_POLL ? "POLL " : "", IOREF_CSR ? "IO_CSR " : "MEM_CSR ",
5537 (BYTE_ORDER == BIG_ENDIAN) ? "BIG_END " : "LITTLE_END ");
5539 /* Make the local hardware ready. */
5545 /* Detach from the kernel in all ways. */
5547 detach_card(softc_t *sc)
5549 struct config config;
5551 /* Make the local hardware NOT ready. */
5554 /* Detach external line protocol stack. */
5555 if (sc->config.line_pkg != PKG_RAWIP)
5557 config = sc->config;
5558 config.line_pkg = PKG_RAWIP;
5559 config_proto(sc, &config);
5560 sc->config = config;
5563 /* Detach kernel interfaces. */
5565 if (sc->flags & FLAG_NETGRAPH)
5567 IFQ_PURGE(&sc->ng_fastq);
5568 IFQ_PURGE(&sc->ng_sndq);
5570 sc->flags &= ~FLAG_NETGRAPH;
5574 if (sc->flags & FLAG_IFNET)
5576 IFQ_PURGE(&sc->ifp->if_snd);
5577 lmc_ifnet_detach(sc);
5578 sc->flags &= ~FLAG_IFNET;
5582 /* Reset the Tulip chip; stops DMA and Interrupts. */
5586 /* This is the I/O configuration interface for FreeBSD */
5591 fbsd_probe(device_t dev)
5593 u_int32_t cfid = pci_read_config(dev, TLP_CFID, 4);
5594 u_int32_t csid = pci_read_config(dev, TLP_CSID, 4);
5596 /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
5597 if (cfid != TLP_CFID_TULIP) return ENXIO;
5601 case TLP_CSID_HSSIc:
5602 device_set_desc(dev, HSSI_DESC);
5605 device_set_desc(dev, T3_DESC);
5608 device_set_desc(dev, SSI_DESC);
5611 device_set_desc(dev, T1E1_DESC);
5620 fbsd_detach(device_t dev)
5622 softc_t *sc = device_get_softc(dev);
5624 /* Stop the card and detach from the kernel. */
5627 /* Release resources. */
5628 if (sc->irq_cookie != NULL)
5630 bus_teardown_intr(dev, sc->irq_res, sc->irq_cookie);
5631 sc->irq_cookie = NULL;
5633 if (sc->irq_res != NULL)
5635 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_res_id, sc->irq_res);
5638 if (sc->csr_res != NULL)
5640 bus_release_resource(dev, sc->csr_res_type, sc->csr_res_id, sc->csr_res);
5644 # if (__FreeBSD_version >= 500000)
5645 mtx_destroy(&sc->top_mtx);
5646 mtx_destroy(&sc->bottom_mtx);
5648 return 0; /* no error */
5652 fbsd_shutdown(device_t dev)
5654 shutdown_card(device_get_softc(dev));
5659 fbsd_attach(device_t dev)
5661 softc_t *sc = device_get_softc(dev);
5664 /* READ/WRITE_PCI_CFG need this. */
5667 /* What kind of card are we driving? */
5668 switch (READ_PCI_CFG(sc, TLP_CSID))
5671 case TLP_CSID_HSSIc:
5672 sc->card = &hssi_card;
5675 sc->card = &t3_card;
5678 sc->card = &ssi_card;
5681 sc->card = &t1_card;
5686 sc->dev_desc = device_get_desc(dev);
5688 /* Allocate PCI memory or IO resources to access the Tulip chip CSRs. */
5690 sc->csr_res_id = TLP_CBIO;
5691 sc->csr_res_type = SYS_RES_IOPORT;
5693 sc->csr_res_id = TLP_CBMA;
5694 sc->csr_res_type = SYS_RES_MEMORY;
5696 sc->csr_res = bus_alloc_resource(dev, sc->csr_res_type, &sc->csr_res_id,
5697 0, ~0, 1, RF_ACTIVE);
5698 if (sc->csr_res == NULL)
5700 printf("%s: bus_alloc_resource(csr) failed.\n", NAME_UNIT);
5703 sc->csr_tag = rman_get_bustag(sc->csr_res);
5704 sc->csr_handle = rman_get_bushandle(sc->csr_res);
5706 /* Allocate PCI interrupt resources for the card. */
5708 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_res_id,
5709 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
5710 if (sc->irq_res == NULL)
5712 printf("%s: bus_alloc_resource(irq) failed.\n", NAME_UNIT);
5716 if ((error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
5717 NULL, bsd_interrupt, sc, &sc->irq_cookie)))
5719 printf("%s: bus_setup_intr() failed; error %d\n", NAME_UNIT, error);
5724 # if (__FreeBSD_version >= 500000)
5725 /* Initialize the top-half and bottom-half locks. */
5726 mtx_init(&sc->top_mtx, NAME_UNIT, "top half lock", MTX_DEF);
5727 mtx_init(&sc->bottom_mtx, NAME_UNIT, "bottom half lock", MTX_DEF);
5730 /* Start the card and attach a kernel interface and line protocol. */
5731 if ((error = attach_card(sc, ""))) detach_card(sc);
5735 static device_method_t methods[] =
5737 DEVMETHOD(device_probe, fbsd_probe),
5738 DEVMETHOD(device_attach, fbsd_attach),
5739 DEVMETHOD(device_detach, fbsd_detach),
5740 DEVMETHOD(device_shutdown, fbsd_shutdown),
5741 /* This driver does not suspend and resume. */
5745 static driver_t driver =
5747 .name = DEVICE_NAME,
5749 # if (__FreeBSD_version >= 500000)
5750 .size = sizeof(softc_t),
5751 # else /* FreeBSD-4 */
5752 .softc = sizeof(softc_t),
5756 static devclass_t devclass;
5758 DRIVER_MODULE(lmc, pci, driver, devclass, 0, 0);
5759 MODULE_VERSION(lmc, 2);
5760 MODULE_DEPEND(lmc, pci, 1, 1, 1);
5762 MODULE_DEPEND(lmc, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
5765 MODULE_DEPEND(lmc, sppp, 1, 1, 1);
5768 #endif /* __FreeBSD__ */
5770 /* This is the I/O configuration interface for NetBSD. */
5775 nbsd_match(struct device *parent, struct cfdata *match, void *aux)
5777 struct pci_attach_args *pa = aux;
5778 u_int32_t cfid = pci_conf_read(pa->pa_pc, pa->pa_tag, TLP_CFID);
5779 u_int32_t csid = pci_conf_read(pa->pa_pc, pa->pa_tag, TLP_CSID);
5781 /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
5782 if (cfid != TLP_CFID_TULIP) return 0;
5786 case TLP_CSID_HSSIc:
5797 nbsd_detach(struct device *self, int flags)
5799 softc_t *sc = (softc_t *)self; /* device is first in softc */
5801 /* Stop the card and detach from the kernel. */
5804 /* Release resources. */
5805 if (sc->sdh_cookie != NULL)
5807 shutdownhook_disestablish(sc->sdh_cookie);
5808 sc->sdh_cookie = NULL;
5810 if (sc->irq_cookie != NULL)
5812 pci_intr_disestablish(sc->pa_pc, sc->irq_cookie);
5813 sc->irq_cookie = NULL;
5817 bus_space_unmap(sc->csr_tag, sc->csr_handle, TLP_CSR_SIZE);
5821 return 0; /* no error */
5825 nbsd_attach(struct device *parent, struct device *self, void *aux)
5827 softc_t *sc = (softc_t *)self; /* device is first in softc */
5828 struct pci_attach_args *pa = aux;
5829 const char *intrstr;
5830 bus_addr_t csr_addr;
5833 /* READ/WRITE_PCI_CFG need these. */
5834 sc->pa_pc = pa->pa_pc;
5835 sc->pa_tag = pa->pa_tag;
5836 /* bus_dma needs this. */
5837 sc->pa_dmat = pa->pa_dmat;
5839 /* What kind of card are we driving? */
5840 switch (READ_PCI_CFG(sc, TLP_CSID))
5843 case TLP_CSID_HSSIc:
5844 sc->dev_desc = HSSI_DESC;
5845 sc->card = &hssi_card;
5848 sc->dev_desc = T3_DESC;
5849 sc->card = &t3_card;
5852 sc->dev_desc = SSI_DESC;
5853 sc->card = &ssi_card;
5856 sc->dev_desc = T1E1_DESC;
5857 sc->card = &t1_card;
5862 printf(": %s\n", sc->dev_desc);
5864 /* Allocate PCI resources to access the Tulip chip CSRs. */
5866 csr_addr = (bus_addr_t)READ_PCI_CFG(sc, TLP_CBIO) & -2;
5867 sc->csr_tag = pa->pa_iot; /* bus_space tag for IO refs */
5869 csr_addr = (bus_addr_t)READ_PCI_CFG(sc, TLP_CBMA);
5870 sc->csr_tag = pa->pa_memt; /* bus_space tag for MEM refs */
5872 if ((error = bus_space_map(sc->csr_tag, csr_addr,
5873 TLP_CSR_SIZE, 0, &sc->csr_handle)))
5875 printf("%s: bus_space_map() failed; error %d\n", NAME_UNIT, error);
5879 /* Allocate PCI interrupt resources. */
5880 if ((error = pci_intr_map(pa, &sc->intr_handle)))
5882 printf("%s: pci_intr_map() failed; error %d\n", NAME_UNIT, error);
5883 nbsd_detach(self, 0);
5886 sc->irq_cookie = pci_intr_establish(pa->pa_pc, sc->intr_handle,
5887 IPL_NET, bsd_interrupt, sc);
5888 if (sc->irq_cookie == NULL)
5890 printf("%s: pci_intr_establish() failed\n", NAME_UNIT);
5891 nbsd_detach(self, 0);
5894 intrstr = pci_intr_string(pa->pa_pc, sc->intr_handle);
5896 /* Install a shutdown hook. */
5897 sc->sdh_cookie = shutdownhook_establish(shutdown_card, sc);
5898 if (sc->sdh_cookie == NULL)
5900 printf("%s: shutdown_hook_establish() failed\n", NAME_UNIT);
5901 nbsd_detach(self, 0);
5905 /* Initialize the top-half and bottom-half locks. */
5906 simple_lock_init(&sc->top_lock);
5907 simple_lock_init(&sc->bottom_lock);
5909 /* Start the card and attach a kernel interface and line protocol. */
5910 if ((error = attach_card(sc, intrstr))) detach_card(sc);
5913 # if (__NetBSD_Version__ >= 106080000) /* 1.6H */
5914 CFATTACH_DECL(lmc, sizeof(softc_t),
5915 nbsd_match, nbsd_attach, nbsd_detach, NULL);
5917 struct cfattach lmc_ca =
5919 /*.ca_name = DEVICE_NAME, */
5920 .ca_devsize = sizeof(softc_t),
5921 .ca_match = nbsd_match,
5922 .ca_attach = nbsd_attach,
5923 .ca_detach = nbsd_detach,
5924 .ca_activate = NULL,
5928 # if (__NetBSD_Version__ >= 106080000)
5929 CFDRIVER_DECL(lmc, DV_IFNET, NULL);
5931 static struct cfdriver lmc_cd =
5933 .cd_name = DEVICE_NAME,
5934 .cd_class = DV_IFNET,
5940 /* cfdata is declared static, unseen outside this module. */
5941 /* It is used for LKM; config builds its own in ioconf.c. */
5942 static struct cfdata lmc_cf =
5944 # if (__NetBSD_Version__ >= 106080000)
5945 .cf_name = DEVICE_NAME,
5946 .cf_atname = DEVICE_NAME,
5948 .cf_driver = &lmc_cd,
5949 .cf_attach = &lmc_ca,
5952 .cf_fstate = FSTATE_STAR,
5955 # if (__NetBSD_Version__ >= 106080000)
5956 MOD_MISC(DEVICE_NAME)
5958 static struct lkm_misc _module =
5960 .lkm_name = DEVICE_NAME,
5961 .lkm_type = LM_MISC,
5963 .lkm_ver = LKM_VERSION,
5967 /* From /sys/dev/pci/pci.c (no public prototype). */
5968 int pciprint(void *, const char *);
5970 static int lkm_nbsd_match(struct pci_attach_args *pa)
5971 { return nbsd_match(0, 0, pa); }
5973 /* LKM loader finds this by appending "_lkmentry" to filename "if_lmc". */
5974 int if_lmc_lkmentry(struct lkm_table *lkmtp, int cmd, int ver)
5978 if (ver != LKM_VERSION) return EINVAL;
5983 struct cfdriver* pcicd;
5985 lkmtp->private.lkm_misc = &_module;
5986 if ((pcicd = config_cfdriver_lookup("pci")) == NULL)
5988 printf("%s: config_cfdriver_lookup(pci) failed; error %d\n",
5989 lmc_cd.cd_name, error);
5992 # if (__NetBSD_Version__ >= 106080000)
5993 if ((error = config_cfdriver_attach(&lmc_cd)))
5995 printf("%s: config_cfdriver_attach() failed; error %d\n",
5996 lmc_cd.cd_name, error);
5999 if ((error = config_cfattach_attach(lmc_cd.cd_name, &lmc_ca)))
6001 printf("%s: config_cfattach_attach() failed; error %d\n",
6002 lmc_cd.cd_name, error);
6003 config_cfdriver_detach(&lmc_cd);
6007 for (i=0; i<pcicd->cd_ndevs; i++)
6010 /* A pointer to a device is a pointer to its softc. */
6011 struct pci_softc *sc = pcicd->cd_devs[i];
6012 if (sc == NULL) continue;
6013 for (dev=0; dev<sc->sc_maxndevs; dev++)
6015 struct pci_attach_args pa;
6016 pcitag_t tag = pci_make_tag(sc->sc_pc, sc->sc_bus, dev, 0);
6017 if (pci_probe_device(sc, tag, lkm_nbsd_match, &pa) != 0)
6018 config_attach(pcicd->cd_devs[i], &lmc_cf, &pa, pciprint);
6019 /* config_attach doesn't return on failure; it calls panic. */
6026 for (i=lmc_cd.cd_ndevs-1; i>=0; i--)
6028 struct device *dev = lmc_cd.cd_devs[i];
6029 if (dev == NULL) continue;
6030 if ((error = config_detach(dev, 0)))
6032 printf("%s: config_detach() failed; error %d\n",
6033 dev->dv_xname, error);
6037 # if (__NetBSD_Version__ >= 106080000)
6038 if ((error = config_cfattach_detach(lmc_cd.cd_name, &lmc_ca)))
6040 printf("%s: config_cfattach_detach() failed; error %d\n",
6041 lmc_cd.cd_name, error);
6044 if ((error = config_cfdriver_detach(&lmc_cd)))
6046 printf("%s: config_cfdriver_detach() failed; error %d\n",
6047 lmc_cd.cd_name, error);
6060 #endif /* __NetBSD__ */
6062 /* This is the I/O configuration interface for OpenBSD. */
6067 obsd_match(struct device *parent, void *match, void *aux)
6069 struct pci_attach_args *pa = aux;
6070 u_int32_t cfid = pci_conf_read(pa->pa_pc, pa->pa_tag, TLP_CFID);
6071 u_int32_t csid = pci_conf_read(pa->pa_pc, pa->pa_tag, TLP_CSID);
6073 /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
6074 if (cfid != TLP_CFID_TULIP) return 0;
6078 case TLP_CSID_HSSIc:
6082 return 100; /* match better than other 21140 drivers */
6089 obsd_detach(struct device *self, int flags)
6091 softc_t *sc = (softc_t *)self; /* device is first in softc */
6093 /* Stop the card and detach from the kernel. */
6096 /* Release resources. */
6097 if (sc->sdh_cookie != NULL)
6099 shutdownhook_disestablish(sc->sdh_cookie);
6100 sc->sdh_cookie = NULL;
6102 if (sc->irq_cookie != NULL)
6104 pci_intr_disestablish(sc->pa_pc, sc->irq_cookie);
6105 sc->irq_cookie = NULL;
6109 bus_space_unmap(sc->csr_tag, sc->csr_handle, TLP_CSR_SIZE);
6113 return 0; /* no error */
6117 obsd_attach(struct device *parent, struct device *self, void *aux)
6119 softc_t *sc = (softc_t *)self; /* device is first in softc */
6120 struct pci_attach_args *pa = aux;
6121 const char *intrstr;
6122 bus_addr_t csr_addr;
6125 /* READ/WRITE_PCI_CFG need these. */
6126 sc->pa_pc = pa->pa_pc;
6127 sc->pa_tag = pa->pa_tag;
6128 /* bus_dma needs this. */
6129 sc->pa_dmat = pa->pa_dmat;
6131 /* What kind of card are we driving? */
6132 switch (READ_PCI_CFG(sc, TLP_CSID))
6135 case TLP_CSID_HSSIc:
6136 sc->dev_desc = HSSI_DESC;
6137 sc->card = &hssi_card;
6140 sc->dev_desc = T3_DESC;
6141 sc->card = &t3_card;
6144 sc->dev_desc = SSI_DESC;
6145 sc->card = &ssi_card;
6148 sc->dev_desc = T1E1_DESC;
6149 sc->card = &t1_card;
6154 printf(": %s\n", sc->dev_desc);
6156 /* Allocate PCI resources to access the Tulip chip CSRs. */
6158 csr_addr = (bus_addr_t)READ_PCI_CFG(sc, TLP_CBIO) & -2;
6159 sc->csr_tag = pa->pa_iot; /* bus_space tag for IO refs */
6161 csr_addr = (bus_addr_t)READ_PCI_CFG(sc, TLP_CBMA);
6162 sc->csr_tag = pa->pa_memt; /* bus_space tag for MEM refs */
6164 if ((error = bus_space_map(sc->csr_tag, csr_addr,
6165 TLP_CSR_SIZE, 0, &sc->csr_handle)))
6167 printf("%s: bus_space_map() failed; error %d\n", NAME_UNIT, error);
6171 /* Allocate PCI interrupt resources. */
6172 if ((error = pci_intr_map(pa, &sc->intr_handle)))
6174 printf("%s: pci_intr_map() failed; error %d\n", NAME_UNIT, error);
6175 obsd_detach(self, 0);
6178 sc->irq_cookie = pci_intr_establish(pa->pa_pc, sc->intr_handle,
6179 IPL_NET, bsd_interrupt, sc, self->dv_xname);
6180 if (sc->irq_cookie == NULL)
6182 printf("%s: pci_intr_establish() failed\n", NAME_UNIT);
6183 obsd_detach(self, 0);
6186 intrstr = pci_intr_string(pa->pa_pc, sc->intr_handle);
6188 /* Install a shutdown hook. */
6189 sc->sdh_cookie = shutdownhook_establish(shutdown_card, sc);
6190 if (sc->sdh_cookie == NULL)
6192 printf("%s: shutdown_hook_establish() failed\n", NAME_UNIT);
6193 obsd_detach(self, 0);
6197 /* Initialize the top-half and bottom-half locks. */
6198 simple_lock_init(&sc->top_lock);
6199 simple_lock_init(&sc->bottom_lock);
6201 /* Start the card and attach a kernel interface and line protocol. */
6202 if ((error = attach_card(sc, intrstr))) detach_card(sc);
6205 struct cfattach lmc_ca =
6207 .ca_devsize = sizeof(softc_t),
6208 .ca_match = obsd_match,
6209 .ca_attach = obsd_attach,
6210 .ca_detach = obsd_detach,
6211 .ca_activate = NULL,
6214 struct cfdriver lmc_cd =
6216 .cd_name = DEVICE_NAME,
6218 .cd_class = DV_IFNET,
6223 /* cfdata is declared static, unseen outside this module. */
6224 /* It is used for LKM; config builds its own in ioconf.c. */
6225 static struct cfdata lmc_cfdata =
6227 .cf_attach = &lmc_ca,
6228 .cf_driver = &lmc_cd,
6230 .cf_fstate = FSTATE_STAR,
6233 static struct lkm_any _module =
6235 .lkm_name = DEVICE_NAME,
6236 .lkm_type = LM_MISC,
6238 .lkm_ver = LKM_VERSION,
6241 /* From /sys/dev/pci/pci.c (no public prototype). */
6242 int pciprint(void *, const char *);
6244 extern struct cfdriver pci_cd;
6246 /* LKM loader finds this by appending "_lkmentry" to filename "if_lmc". */
6247 int if_lmc_lkmentry(struct lkm_table *lkmtp, int cmd, int ver)
6251 if (ver != LKM_VERSION) return EINVAL;
6255 { /* XXX This works for ONE card on pci0 of a i386 machine! XXX */
6256 lkmtp->private.lkm_any = &_module;
6257 for (i=0; i<pci_cd.cd_ndevs; i++)
6259 struct pci_attach_args pa;
6260 struct device *parent = pci_cd.cd_devs[i];
6261 if (parent == NULL) continue; /* dead clone? */
6262 if ((parent->dv_unit)!=0) continue; /* only bus zero */
6263 /* XXX For machine independence, need: pcibus_attach_args. XXX */
6264 /* XXX See NetBSD's sys/dev/pci/pci.c/pci_probe_device. XXX */
6265 /* XXX Why isn't there an LKM network interface module? XXX */
6266 pa.pa_pc = NULL; /* XXX */
6267 pa.pa_bus = 0; /* XXX */
6268 pa.pa_iot = X86_BUS_SPACE_IO; /* XXX */
6269 pa.pa_memt = X86_BUS_SPACE_MEM; /* XXX */
6270 pa.pa_dmat = &pci_bus_dma_tag; /* XXX */
6271 for (pa.pa_device=0; pa.pa_device<32; pa.pa_device++) /* XXX */
6274 pa.pa_function = 0; /* DEC-21140A has function 0 only XXX */
6275 pa.pa_tag = pci_make_tag(pa.pa_pc, pa.pa_bus, pa.pa_device, 0);
6276 pa.pa_id = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_ID_REG);
6277 if ((pa.pa_id & 0xFFFF) == 0xFFFF) continue;
6278 if ((pa.pa_id & 0xFFFF) == 0) continue;
6279 /* XXX this only works for pci0 -- no swizzelling XXX */
6281 pa.pa_intrtag = pa.pa_tag;
6282 intr = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_INTERRUPT_REG);
6283 pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
6284 pa.pa_intrpin = ((PCI_INTERRUPT_PIN(intr) -1) % 4) +1;
6285 if (obsd_match(parent, &lmc_cfdata, &pa))
6286 config_attach(parent, &lmc_cfdata, &pa, pciprint);
6287 /* config_attach doesn't return on failure; it calls panic. */
6294 for (i=lmc_cd.cd_ndevs-1; i>=0; i--)
6296 struct device *dev = lmc_cd.cd_devs[i];
6297 if (dev == NULL) continue;
6298 if ((error = config_detach(dev, 0)))
6299 printf("%s: config_detach() failed; error %d\n", dev->dv_xname, error);
6310 #endif /* __OpenBSD__ */
6312 /* This is the I/O configuration interface for BSD/OS. */
6317 bsdi_match(pci_devaddr_t *pa)
6319 u_int32_t cfid = pci_inl(pa, TLP_CFID);
6320 u_int32_t csid = pci_inl(pa, TLP_CSID);
6322 /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
6323 if (cfid != TLP_CFID_TULIP) return 0;
6327 case TLP_CSID_HSSIc:
6338 bsdi_probe(struct device *parent, struct cfdata *cf, void *aux)
6340 struct isa_attach_args *ia = aux;
6341 pci_devaddr_t *pa = NULL;
6344 /* This must be a PCI bus. */
6345 if (ia->ia_bustype != BUS_PCI) return 0;
6347 /* Scan PCI bus for our boards. */
6348 if ((pa = pci_scan(bsdi_match)) == 0) return 0;
6350 /* Scan config space for IO and MEM base registers and IRQ info. */
6351 pci_getres(pa, &res, 1, ia);
6353 /* Crucial: pass pci_devaddr to bsdi_attach in ia_aux. */
6354 ia->ia_aux = (void *)pa;
6360 bsdi_attach(struct device *parent, struct device *self, void *aux)
6362 softc_t *sc = (softc_t *)self; /* device is first in softc */
6363 struct isa_attach_args *ia = aux;
6364 pci_devaddr_t *pa = ia->ia_aux; /* this is crucial! */
6367 /* READ/WRITE_PCI_CFG need this. */
6370 /* What kind of card are we driving? */
6371 switch (READ_PCI_CFG(sc, TLP_CSID))
6374 case TLP_CSID_HSSIc:
6375 sc->dev_desc = HSSI_DESC;
6376 sc->card = &hssi_card;
6379 sc->dev_desc = T3_DESC;
6380 sc->card = &t3_card;
6383 sc->dev_desc = SSI_DESC;
6384 sc->card = &ssi_card;
6387 sc->dev_desc = T1E1_DESC;
6388 sc->card = &t1_card;
6393 printf(": %s\n", sc->dev_desc);
6395 /* Allocate PCI memory or IO resources to access the Tulip chip CSRs. */
6396 sc->csr_iobase = ia->ia_iobase;
6397 sc->csr_membase = (u_int32_t *)mapphys((vm_offset_t)ia->ia_maddr, TLP_CSR_SIZE);
6399 /* Attach to the PCI bus. */
6400 isa_establish(&sc->id, &sc->dev);
6402 /* Allocate PCI interrupt resources for the card. */
6403 sc->ih.ih_fun = bsd_interrupt;
6405 intr_establish(ia->ia_irq, &sc->ih, DV_NET);
6407 /* Install a shutdown hook. */
6408 sc->ats.func = shutdown_card;
6410 atshutdown(&sc->ats, ATSH_ADD);
6412 /* Initialize the top-half and bottom-half locks. */
6413 simple_lock_init(&sc->top_lock);
6414 simple_lock_init(&sc->bottom_lock);
6416 /* Start the card and attach a kernel interface and line protocol. */
6417 if ((error = attach_card(sc, ""))) detach_card(sc);
6420 struct cfdriver lmccd =
6423 .cd_name = DEVICE_NAME,
6424 .cd_match = bsdi_probe,
6425 .cd_attach = bsdi_attach,
6426 .cd_class = DV_IFNET,
6427 .cd_devsize = sizeof(softc_t),
6429 #endif /* __bsdi__ */
6433 /* The kernel calls this procedure when an interrupt happens. */
6435 linux_interrupt(int irq, void *dev, struct pt_regs *regs)
6437 struct net_device *net_dev = dev;
6438 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6440 /* Cut losses early if this is not our interrupt. */
6441 if ((READ_CSR(TLP_STATUS) & TLP_INT_TXRX) == 0)
6444 /* Disable card interrupts. */
6445 WRITE_CSR(TLP_INT_ENBL, TLP_INT_DISABLE);
6447 /* Handle the card interrupt with the dev->poll method. */
6448 if (netif_rx_schedule_prep(net_dev))
6449 __netif_rx_schedule(net_dev); /* NAPI - add to poll list */
6451 printk("%s: interrupt while on poll list\n", NAME_UNIT);
6456 /* This net_device method services interrupts in a softirq. */
6457 /* With rxintr_cleanup(), it implements input flow control. */
6459 linux_poll(struct net_device *net_dev, int *budget)
6461 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6464 /* Yes, we do NAPI. */
6465 /* Allow processing up to net_dev->quota incoming packets. */
6466 /* This is the ONLY time core_interrupt() may process rx pkts. */
6467 /* Otherwise (sc->quota == 0) and rxintr_cleanup() is a NOOP. */
6468 sc->quota = net_dev->quota;
6470 /* Handle the card interrupt with kernel ints enabled. */
6471 /* Process rx pkts (and tx pkts, too). */
6472 /* Card interrupts are disabled. */
6473 core_interrupt(sc, 0);
6475 /* Report number of rx packets processed. */
6476 received = net_dev->quota - sc->quota;
6477 net_dev->quota -= received;
6478 *budget -= received;
6480 /* if quota prevented processing all rx pkts, leave rx ints disabled */
6481 if (sc->quota == 0) /* this is off by one...but harmless */
6483 WRITE_CSR(TLP_INT_ENBL, TLP_INT_TX);
6484 return 1; /* more pkts to handle -- reschedule */
6487 sc->quota = 0; /* disable rx pkt processing by rxintr_cleanup() */
6488 netif_rx_complete(net_dev); /* NAPI - remove from poll list */
6490 /* Enable card interrupts. */
6491 WRITE_CSR(TLP_INT_ENBL, TLP_INT_TXRX);
6495 /* These next routines are similar to BSD's ifnet kernel/driver interface. */
6497 /* This net_device method hands outgoing packets to the transmitter. */
6498 /* With txintr_setup(), it implements output flow control. */
6499 /* Called from a syscall (user context; no spinlocks). */
6501 linux_start(struct sk_buff *skb, struct net_device *net_dev)
6503 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6505 if (sc->tx_skb == NULL)
6507 /* Put this skb where the transmitter will see it. */
6510 /* Start the transmitter; incoming pkts are NOT processed. */
6511 user_interrupt(sc, 0);
6513 /* If the tx didn't take the skb then stop the queue. */
6514 /* This can happen if another CPU is in core_interrupt(). */
6515 if (sc->tx_skb != NULL) netif_stop_queue(net_dev);
6520 /* This shouldn't happen; skb is NOT consumed. */
6521 if (netif_queue_stopped(net_dev))
6522 printk("%s: dev->start() called with queue stopped\n", NAME_UNIT);
6524 netif_stop_queue(net_dev);
6529 /* This net_device method restarts the transmitter if it hangs. */
6530 /* Called from a softirq. */
6532 linux_timeout(struct net_device *net_dev)
6534 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6536 /* Start the transmitter; incoming packets are NOT processed. */
6537 user_interrupt(sc, 1);
6540 /* This net_device method handles IOCTL syscalls. */
6541 /* Called from a syscall (user context; no spinlocks; can sleep). */
6543 linux_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
6545 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6548 if ((cmd >= SIOCDEVPRIVATE) && (cmd <= SIOCDEVPRIVATE+15))
6550 struct iohdr *iohdr = (struct iohdr *)ifr;
6551 u_int16_t direction = iohdr->direction;
6552 u_int16_t length = iohdr->length;
6553 char *user_addr = (char *)iohdr->iohdr;
6556 if (iohdr->cookie != NGM_LMC_COOKIE) return -EINVAL;
6558 /* Emulate a BSD-style IOCTL syscall. */
6559 kern_addr = kmalloc(length, GFP_KERNEL);
6560 if (kern_addr == NULL)
6562 if ((error == 0) && ((direction & DIR_IOW) != 0))
6563 error = copy_from_user(kern_addr, user_addr, length);
6565 error = -core_ioctl(sc, (unsigned long)cmd, kern_addr);
6566 if ((error == 0) && ((direction & DIR_IOR) != 0))
6567 error = copy_to_user(user_addr, kern_addr, length);
6571 else if (cmd == SIOCWANDEV)
6573 const size_t size = sizeof(sync_serial_settings);
6575 switch (ifr->ifr_settings.type)
6577 case IF_GET_IFACE: /* get interface config */
6579 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
6580 if (ifr->ifr_settings.size < size)
6582 ifr->ifr_settings.size = size;
6587 if (sc->config.tx_clk_src == CFG_CLKMUX_ST)
6588 sc->hdlc_settings.clock_type = CLOCK_EXT;
6589 if (sc->config.tx_clk_src == CFG_CLKMUX_INT)
6590 sc->hdlc_settings.clock_type = CLOCK_TXINT;
6591 if (sc->config.tx_clk_src == CFG_CLKMUX_RT)
6592 sc->hdlc_settings.clock_type = CLOCK_TXFROMRX;
6593 sc->hdlc_settings.loopback = (sc->config.loop_back != CFG_LOOP_NONE) ? 1:0;
6594 sc->hdlc_settings.clock_rate = sc->status.tx_speed;
6595 error = copy_to_user(ifr->ifr_settings.ifs_ifsu.sync,
6596 &sc->hdlc_settings, size);
6600 case IF_IFACE_SYNC_SERIAL: /* set interface config */
6602 if (!capable(CAP_NET_ADMIN))
6605 error = copy_from_user(&sc->hdlc_settings,
6606 ifr->ifr_settings.ifs_ifsu.sync, size);
6607 /* hdlc_settings are currently ignored. */
6610 default: /* Pass the rest to the line protocol code. */
6612 error = hdlc_ioctl(net_dev, ifr, cmd);
6617 # endif /* GEN_HDLC */
6618 else /* unknown IOCTL command */
6622 printk("%s: linux_ioctl; cmd=0x%08x error=%d\n",
6623 NAME_UNIT, cmd, error);
6628 /* This net_device method returns a pointer to device statistics. */
6629 static struct net_device_stats *
6630 linux_stats(struct net_device *net_dev)
6633 return &dev_to_hdlc(net_dev)->stats;
6635 softc_t *sc = net_dev->priv;
6636 return &sc->net_stats;
6640 /* Called from a softirq once a second. */
6642 linux_watchdog(unsigned long softc)
6644 softc_t *sc = (softc_t *)softc;
6645 u_int8_t old_oper_status = sc->status.oper_status;
6646 struct event_cntrs *cntrs = &sc->status.cntrs;
6647 struct net_device_stats *stats = linux_stats(sc->net_dev);
6649 core_watchdog(sc); /* updates oper_status */
6651 /* Notice change in link status. */
6652 if ((old_oper_status != STATUS_UP) &&
6653 (sc->status.oper_status == STATUS_UP)) /* link came up */
6655 hdlc_set_carrier(1, sc->net_dev);
6656 netif_wake_queue(sc->net_dev);
6658 if ((old_oper_status == STATUS_UP) &&
6659 (sc->status.oper_status != STATUS_UP)) /* link went down */
6661 hdlc_set_carrier(0, sc->net_dev);
6662 netif_stop_queue(sc->net_dev);
6665 /* Notice change in line protocol. */
6666 if (sc->config.line_pkg == PKG_RAWIP)
6668 sc->status.line_pkg = PKG_RAWIP;
6669 sc->status.line_prot = PROT_IP_HDLC;
6674 sc->status.line_pkg = PKG_GEN_HDLC;
6675 switch (sc->hdlc_dev->proto.id)
6678 sc->status.line_prot = PROT_PPP;
6680 case IF_PROTO_CISCO:
6681 sc->status.line_prot = PROT_C_HDLC;
6684 sc->status.line_prot = PROT_FRM_RLY;
6687 sc->status.line_prot = PROT_IP_HDLC;
6690 sc->status.line_prot = PROT_X25;
6692 case IF_PROTO_HDLC_ETH:
6693 sc->status.line_prot = PROT_ETH_HDLC;
6696 sc->status.line_prot = 0;
6700 # endif /* GEN_HDLC */
6702 /* Copy statistics from sc to net_dev for get_stats(). */
6703 stats->rx_packets = cntrs->ipackets;
6704 stats->tx_packets = cntrs->opackets;
6705 stats->rx_bytes = cntrs->ibytes;
6706 stats->tx_bytes = cntrs->obytes;
6707 stats->rx_errors = cntrs->ierrors;
6708 stats->tx_errors = cntrs->oerrors;
6709 stats->rx_dropped = cntrs->idiscards;
6710 stats->tx_dropped = cntrs->odiscards;
6711 stats->rx_fifo_errors = cntrs->fifo_over;
6712 stats->tx_fifo_errors = cntrs->fifo_under;
6713 stats->rx_missed_errors = cntrs->missed;
6714 stats->rx_over_errors = cntrs->overruns;
6716 /* Call this procedure again after one second. */
6717 sc->wd_timer.expires = jiffies + HZ; /* now plus one second */
6718 add_timer(&sc->wd_timer);
6721 /* This is the I/O configuration interface for Linux. */
6723 /* This net_device method is called when IFF_UP goes false. */
6725 linux_stop(struct net_device *net_dev)
6727 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6729 /* Stop the card and detach from the kernel. */
6730 detach_card(sc); /* doesn't fail */
6732 free_irq(net_dev->irq, net_dev); /* doesn't fail */
6734 del_timer(&sc->wd_timer); /* return value ignored */
6739 /* This net_device method is called when IFF_UP goes true. */
6741 linux_open(struct net_device *net_dev)
6743 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6746 /* Allocate PCI interrupt resources for the card. */
6747 if ((error = request_irq(net_dev->irq, &linux_interrupt, SA_SHIRQ,
6748 NAME_UNIT, net_dev)))
6750 printk("%s: request_irq() failed; error %d\n", NAME_UNIT, error);
6754 /* Arrange to call linux_watchdog() once a second. */
6755 init_timer(&sc->wd_timer);
6756 sc->wd_timer.expires = jiffies + HZ; /* now plus one second */
6757 sc->wd_timer.function = &linux_watchdog;
6758 sc->wd_timer.data = (unsigned long) sc;
6759 add_timer(&sc->wd_timer);
6761 /* Start the card and attach a kernel interface and line protocol. */
6762 if ((error = -attach_card(sc, "")))
6763 linux_stop(net_dev);
6766 net_dev->weight = sc->rxring.num_descs; /* input flow control */
6767 netif_start_queue(net_dev); /* output flow control */
6775 hdlc_attach(struct net_device *net_dev,
6776 unsigned short encoding, unsigned short parity)
6780 /* This pci_driver method is called during shutdown or module-unload. */
6781 /* This is called from user context; can sleep; no spinlocks! */
6783 linux_remove(struct pci_dev *pci_dev)
6785 struct net_device *net_dev = (struct net_device *)pci_get_drvdata(pci_dev);
6786 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6788 if (net_dev == NULL) return;
6790 /* Assume that linux_stop() has already been called. */
6791 if (sc->flags & FLAG_NETDEV)
6793 unregister_hdlc_device(net_dev);
6795 unregister_netdev(net_dev);
6798 # if (IOREF_CSR == 0)
6799 if (sc->csr_membase != NULL)
6800 iounmap(sc->csr_membase);
6803 pci_disable_device(pci_dev);
6805 if (sc->csr_iobase != 0)
6806 pci_release_regions(pci_dev);
6808 pci_set_drvdata(pci_dev, NULL);
6811 free_netdev(net_dev);
6815 setup_netdev(struct net_device *net_dev)
6817 /* Initialize the generic network device. */
6818 /* Note similarity to BSD's lmc_ifnet_attach(). */
6819 net_dev->flags = IFF_POINTOPOINT;
6820 net_dev->flags |= IFF_RUNNING;
6821 net_dev->open = linux_open;
6822 net_dev->stop = linux_stop;
6823 net_dev->hard_start_xmit = linux_start;
6824 net_dev->do_ioctl = linux_ioctl;
6825 net_dev->get_stats = linux_stats;
6826 net_dev->tx_timeout = linux_timeout;
6827 net_dev->poll = linux_poll;
6828 net_dev->watchdog_timeo = 1 * HZ;
6829 net_dev->tx_queue_len = SNDQ_MAXLEN;
6830 net_dev->mtu = MAX_DESC_LEN;
6831 net_dev->type = ARPHRD_RAWHDLC;
6832 /* The receiver generates frag-lists for packets >4032 bytes. */
6833 /* The transmitter accepts scatter/gather lists and frag-lists. */
6834 /* However Linux linearizes outgoing packets since our hardware */
6835 /* doesn't compute soft checksums. All that work for nothing! */
6836 /*net_dev->features |= NETIF_F_SG; */
6837 /*net_dev->features |= NETIF_F_FRAGLIST; */
6840 /* This pci_driver method is called during boot or module-load. */
6841 /* This is called from user context; can sleep; no spinlocks! */
6843 linux_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
6845 u_int32_t cfid, csid;
6846 struct net_device *net_dev;
6850 /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
6851 pci_read_config_dword(pci_dev, TLP_CFID, &cfid);
6852 if (cfid != TLP_CFID_TULIP) return -ENXIO;
6853 pci_read_config_dword(pci_dev, TLP_CSID, &csid);
6857 case TLP_CSID_HSSIc:
6866 /* Declare that these cards use 32-bit single-address PCI cycles. */
6867 if ((error = pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)))
6869 printk("%s: pci_set_dma_mask() failed; error %d\n", DEVICE_NAME, error);
6872 pci_set_consistent_dma_mask(pci_dev, DMA_32BIT_MASK); /* can't fail */
6874 # if GEN_HDLC /* generic-hdlc line protocols */
6876 /* device driver instance data, aka Soft Context or sc */
6877 if ((sc = kmalloc(sizeof(softc_t), GFP_KERNEL)) == NULL)
6879 printk("%s: kmalloc() failed\n", DEVICE_NAME);
6882 memset(sc, 0, sizeof(softc_t));
6884 /* Allocate space for the HDLC network device struct. */
6885 if ((net_dev = alloc_hdlcdev(sc)) == NULL)
6887 printk("%s: alloc_hdlcdev() failed\n", DEVICE_NAME);
6892 /* Initialize the network device struct. */
6893 setup_netdev(net_dev);
6895 /* Initialize the HDLC extension to the network device. */
6896 sc->hdlc_dev = dev_to_hdlc(net_dev);
6897 sc->hdlc_dev->attach = hdlc_attach; /* noop for this driver */
6898 sc->hdlc_dev->xmit = linux_start; /* the REAL hard_start_xmit() */
6900 # else /* GEN_HDLC */ /* no line protocol. */
6902 /* Allocate space for the bare network device struct. */
6903 net_dev = alloc_netdev(sizeof(softc_t), DEVICE_NAME"%d", setup_netdev);
6904 if (net_dev == NULL)
6906 printk("%s: alloc_netdev() failed\n", DEVICE_NAME);
6909 /* device driver instance data, aka Soft Context or sc */
6912 # endif /* GEN_HDLC */
6914 sc->net_dev = net_dev; /* NAME_UNIT macro needs this */
6915 sc->pci_dev = pci_dev; /* READ/WRITE_PCI_CFG macros need this */
6917 /* Cross-link pci_dev and net_dev. */
6918 pci_set_drvdata(pci_dev, net_dev); /* pci_dev->driver_data = net_dev */
6919 SET_NETDEV_DEV(net_dev, &pci_dev->dev); /* net_dev->class_dev.dev = &pci_dev->dev */
6920 SET_MODULE_OWNER(net_dev); /* ??? NOOP in linux-2.6.3. ??? */
6922 /* Sets cfcs.io and cfcs.mem; sets pci_dev->irq based on cfit.int */
6923 if ((error = pci_enable_device(pci_dev)))
6925 printk("%s: pci_enable_device() failed; error %d\n", DEVICE_NAME, error);
6926 linux_remove(pci_dev);
6929 net_dev->irq = pci_dev->irq; /* linux_open/stop need this */
6931 /* Allocate PCI memory and IO resources to access the Tulip chip CSRs. */
6932 if ((error = pci_request_regions(pci_dev, DEVICE_NAME)))
6934 printk("%s: pci_request_regions() failed; error %d\n", DEVICE_NAME, error);
6935 linux_remove(pci_dev);
6938 net_dev->base_addr = pci_resource_start(pci_dev, 0);
6939 net_dev->mem_start = pci_resource_start(pci_dev, 1);
6940 net_dev->mem_end = pci_resource_end(pci_dev, 1);
6941 sc->csr_iobase = net_dev->base_addr;
6943 # if (IOREF_CSR == 0)
6944 sc->csr_membase = ioremap_nocache(net_dev->mem_start, TLP_CSR_SIZE);
6945 if (sc->csr_membase == NULL)
6947 printk("%s: ioremap_nocache() failed\n", DEVICE_NAME);
6948 linux_remove(pci_dev);
6953 /* Sets cfcs.master, enabling PCI DMA; checks latency timer value. */
6954 pci_set_master(pci_dev); /* Later, attach_card() does this too. */
6956 /* Initialize the top-half and bottom-half locks. */
6957 /* Top_lock must be initialized before net_dev is registered. */
6958 init_MUTEX(&sc->top_lock);
6959 spin_lock_init(&sc->bottom_lock);
6962 if ((error = register_hdlc_device(net_dev)))
6964 printk("%s: register_hdlc_device() failed; error %d\n", DEVICE_NAME, error);
6965 linux_remove(pci_dev);
6969 if ((error = register_netdev(net_dev)))
6971 printk("%s: register_netdev() failed; error %d\n", DEVICE_NAME, error);
6972 linux_remove(pci_dev);
6976 /* The NAME_UNIT macro now works. Use DEVICE_NAME before this. */
6977 sc->flags |= FLAG_NETDEV;
6979 /* What kind of card are we driving? */
6980 switch (READ_PCI_CFG(sc, TLP_CSID))
6983 case TLP_CSID_HSSIc:
6984 sc->dev_desc = HSSI_DESC;
6985 sc->card = &hssi_card;
6988 sc->dev_desc = T3_DESC;
6989 sc->card = &t3_card;
6992 sc->dev_desc = SSI_DESC;
6993 sc->card = &ssi_card;
6996 sc->dev_desc = T1E1_DESC;
6997 sc->card = &t1_card;
6999 default: /* shouldn't happen! */
7000 linux_remove(pci_dev);
7004 /* Announce the hardware on the console. */
7005 printk("%s: <%s> io 0x%04lx/9 mem 0x%08lx/25 rom 0x%08lx/14 irq %d pci %s\n",
7006 NAME_UNIT, sc->dev_desc, pci_resource_start(pci_dev, 0),
7007 pci_resource_start(pci_dev, 1), pci_resource_start(pci_dev, 6),
7008 pci_dev->irq, pci_name(pci_dev));
7013 /* This pci driver knows how to drive these devices: */
7014 static __initdata struct pci_device_id pci_device_id_tbl[] =
7016 /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
7017 { 0x1011, 0x0009, 0x1376, PCI_ANY_ID, 0, 0, 0 },
7018 { 0, 0, 0, 0, 0, 0, 0 }
7020 MODULE_DEVICE_TABLE(pci, pci_device_id_tbl);
7022 static struct pci_driver pci_driver =
7024 .name = DEVICE_NAME,
7025 .id_table = pci_device_id_tbl,
7026 .probe = linux_probe,
7027 .remove = __devexit_p(linux_remove),
7028 /* This driver does not suspend and resume. */
7031 /* This ultimately calls our pci_driver.probe() method. */
7032 static int __init linux_modload(void)
7033 { return pci_module_init(&pci_driver); }
7034 module_init(linux_modload);
7036 /* This ultimately calls our pci_driver.remove() method. */
7037 static void __exit linux_modunload(void)
7038 { pci_unregister_driver(&pci_driver); }
7039 module_exit(linux_modunload);
7041 MODULE_LICENSE("Dual BSD/GPL");
7042 MODULE_DESCRIPTION("Device driver for SBE/LMC Wide-Area Network cards");
7043 MODULE_AUTHOR("David Boggs <boggs@boggs.palo-alto.ca.us>");
7045 #endif /* __linux__ */