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);
2531 if (mbuf->m_data[0]>>4 == 4)
2532 netisr_dispatch(NETISR_IP, mbuf);
2536 if (mbuf->m_data[0]>>4 == 6)
2537 netisr_dispatch(NETISR_IPV6, mbuf);
2542 sc->status.cntrs.idiscards++;
2544 printf("%s: lmc_raw_input: rx pkt discarded: not IPv4 or IPv6\n",
2551 /* There are TWO VERSIONS of interrupt/DMA code: Linux & BSD.
2552 * Handling Linux and the BSDs with CPP directives would
2553 * make the code unreadable, so there are two versions.
2554 * Conceptually, the two versions do the same thing and
2555 * core_interrupt() doesn't know they are different.
2557 * We are "standing on the head of a pin" in these routines.
2558 * Tulip CSRs can be accessed, but nothing else is interrupt-safe!
2559 * Do NOT access: MII, GPIO, SROM, BIOSROM, XILINX, SYNTH, or DAC.
2562 #if BSD /* BSD version of interrupt/DMA code */
2564 /* Singly-linked tail-queues hold mbufs with active DMA.
2565 * For RX, single mbuf clusters; for TX, mbuf chains are queued.
2566 * NB: mbufs are linked through their m_nextpkt field.
2567 * Callers must hold sc->bottom_lock; not otherwise locked.
2570 /* Put an mbuf (chain) on the tail of the descriptor ring queue. */
2571 static void /* BSD version */
2572 mbuf_enqueue(struct desc_ring *ring, struct mbuf *m)
2574 m->m_nextpkt = NULL;
2575 if (ring->tail == NULL)
2578 ring->tail->m_nextpkt = m;
2582 /* Get an mbuf (chain) from the head of the descriptor ring queue. */
2583 static struct mbuf* /* BSD version */
2584 mbuf_dequeue(struct desc_ring *ring)
2586 struct mbuf *m = ring->head;
2588 if ((ring->head = m->m_nextpkt) == NULL)
2594 static void /* *** FreeBSD ONLY *** Callout from bus_dmamap_load() */
2595 fbsd_dmamap_load(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2597 struct desc_ring *ring = arg;
2598 ring->nsegs = error ? 0 : nsegs;
2599 ring->segs[0] = segs[0];
2600 ring->segs[1] = segs[1];
2604 /* Initialize a DMA descriptor ring. */
2605 static int /* BSD version */
2606 create_ring(softc_t *sc, struct desc_ring *ring, int num_descs)
2608 struct dma_desc *descs;
2609 int size_descs = sizeof(struct dma_desc)*num_descs;
2612 /* The DMA descriptor array must not cross a page boundary. */
2613 if (size_descs > PAGE_SIZE)
2615 printf("%s: DMA descriptor array > PAGE_SIZE (%d)\n", NAME_UNIT,
2622 /* Create a DMA tag for descriptors and buffers. */
2623 if ((error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
2624 BUS_SPACE_MAXADDR, NULL, NULL, PAGE_SIZE, 2, PAGE_SIZE, BUS_DMA_ALLOCNOW,
2625 # if (__FreeBSD_version >= 502000)
2630 printf("%s: bus_dma_tag_create() failed: error %d\n", NAME_UNIT, error);
2634 /* Allocate wired physical memory for DMA descriptor array */
2635 /* and map physical address to kernel virtual address. */
2636 if ((error = bus_dmamem_alloc(ring->tag, (void**)&ring->first,
2637 BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &ring->map)))
2639 printf("%s: bus_dmamem_alloc() failed; error %d\n", NAME_UNIT, error);
2642 descs = ring->first;
2644 /* Map kernel virtual address to PCI address for DMA descriptor array. */
2645 if ((error = bus_dmamap_load(ring->tag, ring->map, descs, size_descs,
2646 fbsd_dmamap_load, ring, 0)))
2648 printf("%s: bus_dmamap_load() failed; error %d\n", NAME_UNIT, error);
2651 ring->dma_addr = ring->segs[0].ds_addr;
2653 /* Allocate dmamaps for each DMA descriptor. */
2654 for (i=0; i<num_descs; i++)
2655 if ((error = bus_dmamap_create(ring->tag, 0, &descs[i].map)))
2657 printf("%s: bus_dmamap_create() failed; error %d\n", NAME_UNIT, error);
2661 #elif (defined(__NetBSD__) || defined(__OpenBSD__))
2663 /* Use the DMA tag passed to attach() for descriptors and buffers. */
2664 ring->tag = sc->pa_dmat;
2666 /* Allocate wired physical memory for DMA descriptor array. */
2667 if ((error = bus_dmamem_alloc(ring->tag, size_descs, PAGE_SIZE, 0,
2668 ring->segs, 1, &ring->nsegs, BUS_DMA_NOWAIT)))
2670 printf("%s: bus_dmamem_alloc() failed; error %d\n", NAME_UNIT, error);
2674 /* Map physical address to kernel virtual address. */
2675 if ((error = bus_dmamem_map(ring->tag, ring->segs, ring->nsegs,
2676 size_descs, (caddr_t *)&ring->first, BUS_DMA_NOWAIT | BUS_DMA_COHERENT)))
2678 printf("%s: bus_dmamem_map() failed; error %d\n", NAME_UNIT, error);
2681 descs = ring->first; /* suppress compiler warning about aliasing */
2682 memset(descs, 0, size_descs);
2684 /* Allocate dmamap for PCI access to DMA descriptor array. */
2685 if ((error = bus_dmamap_create(ring->tag, size_descs, 1,
2686 size_descs, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &ring->map)))
2688 printf("%s: bus_dmamap_create() failed; error %d\n", NAME_UNIT, error);
2692 /* Map kernel virtual address to PCI address for DMA descriptor array. */
2693 if ((error = bus_dmamap_load(ring->tag, ring->map, descs, size_descs,
2694 0, BUS_DMA_NOWAIT)))
2696 printf("%s: bus_dmamap_load() failed; error %d\n", NAME_UNIT, error);
2699 ring->dma_addr = ring->map->dm_segs[0].ds_addr;
2701 /* Allocate dmamaps for each DMA descriptor. */
2702 for (i=0; i<num_descs; i++)
2703 if ((error = bus_dmamap_create(ring->tag, MAX_DESC_LEN, 2,
2704 MAX_CHUNK_LEN, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &descs[i].map)))
2706 printf("%s: bus_dmamap_create() failed; error %d\n", NAME_UNIT, error);
2710 #elif defined(__bsdi__)
2712 /* Allocate wired physical memory for DMA descriptor array. */
2713 if ((ring->first = malloc(size_descs, M_DEVBUF, M_NOWAIT)) == NULL)
2715 printf("%s: malloc() failed for DMA descriptor array\n", NAME_UNIT);
2718 descs = ring->first;
2719 memset(descs, 0, size_descs);
2721 /* Map kernel virtual address to PCI address for DMA descriptor array. */
2722 ring->dma_addr = vtophys(descs); /* Relax! BSD/OS only. */
2727 ring->write = descs;
2728 ring->first = descs;
2729 ring->last = descs + num_descs -1;
2730 ring->last->control = TLP_DCTL_END_RING;
2731 ring->num_descs = num_descs;
2732 ring->size_descs = size_descs;
2739 /* Destroy a DMA descriptor ring */
2740 static void /* BSD version */
2741 destroy_ring(softc_t *sc, struct desc_ring *ring)
2743 struct dma_desc *desc;
2746 /* Free queued mbufs. */
2747 while ((m = mbuf_dequeue(ring)) != NULL)
2750 /* TX may have one pkt that is not on any queue. */
2751 if (sc->tx_mbuf != NULL)
2753 m_freem(sc->tx_mbuf);
2757 /* Unmap active DMA descriptors. */
2758 while (ring->read != ring->write)
2760 bus_dmamap_unload(ring->tag, ring->read->map);
2761 if (ring->read++ == ring->last) ring->read = ring->first;
2766 /* Free the dmamaps of all DMA descriptors. */
2767 for (desc=ring->first; desc!=ring->last+1; desc++)
2768 if (desc->map != NULL)
2769 bus_dmamap_destroy(ring->tag, desc->map);
2771 /* Unmap PCI address for DMA descriptor array. */
2772 if (ring->dma_addr != 0)
2773 bus_dmamap_unload(ring->tag, ring->map);
2774 /* Free kernel memory for DMA descriptor array. */
2775 if (ring->first != NULL)
2776 bus_dmamem_free(ring->tag, ring->first, ring->map);
2777 /* Free the DMA tag created for this ring. */
2778 if (ring->tag != NULL)
2779 bus_dma_tag_destroy(ring->tag);
2781 #elif (defined(__NetBSD__) || defined(__OpenBSD__))
2783 /* Free the dmamaps of all DMA descriptors. */
2784 for (desc=ring->first; desc!=ring->last+1; desc++)
2785 if (desc->map != NULL)
2786 bus_dmamap_destroy(ring->tag, desc->map);
2788 /* Unmap PCI address for DMA descriptor array. */
2789 if (ring->dma_addr != 0)
2790 bus_dmamap_unload(ring->tag, ring->map);
2791 /* Free dmamap for DMA descriptor array. */
2792 if (ring->map != NULL)
2793 bus_dmamap_destroy(ring->tag, ring->map);
2794 /* Unmap kernel address for DMA descriptor array. */
2795 if (ring->first != NULL)
2796 bus_dmamem_unmap(ring->tag, (caddr_t)ring->first, ring->size_descs);
2797 /* Free kernel memory for DMA descriptor array. */
2798 if (ring->segs[0].ds_addr != 0)
2799 bus_dmamem_free(ring->tag, ring->segs, ring->nsegs);
2801 #elif defined(__bsdi__)
2803 /* Free kernel memory for DMA descriptor array. */
2804 if (ring->first != NULL)
2805 free(ring->first, M_DEVBUF);
2810 /* Clean up after a packet has been received. */
2811 static int /* BSD version */
2812 rxintr_cleanup(softc_t *sc)
2814 struct desc_ring *ring = &sc->rxring;
2815 struct dma_desc *first_desc, *last_desc;
2816 struct mbuf *first_mbuf=NULL, *last_mbuf=NULL;
2817 struct mbuf *new_mbuf;
2818 int pkt_len, desc_len;
2820 #if (defined(__FreeBSD__) && defined(DEVICE_POLLING))
2821 /* Input packet flow control (livelock prevention): */
2822 /* Give pkts to higher levels only if quota is > 0. */
2823 if (sc->quota <= 0) return 0;
2826 /* This looks complicated, but remember: typically packets up */
2827 /* to 2048 bytes long fit in one mbuf and use one descriptor. */
2829 first_desc = last_desc = ring->read;
2831 /* ASSERTION: If there is a descriptor in the ring and the hardware has */
2832 /* finished with it, then that descriptor will have RX_FIRST_DESC set. */
2833 if ((ring->read != ring->write) && /* descriptor ring not empty */
2834 ((ring->read->status & TLP_DSTS_OWNER) == 0) && /* hardware done */
2835 ((ring->read->status & TLP_DSTS_RX_FIRST_DESC) == 0)) /* should be set */
2836 panic("%s: rxintr_cleanup: rx-first-descriptor not set.\n", NAME_UNIT);
2838 /* First decide if a complete packet has arrived. */
2839 /* Run down DMA descriptors looking for one marked "last". */
2840 /* Bail out if an active descriptor is encountered. */
2841 /* Accumulate most significant bits of packet length. */
2845 if (last_desc == ring->write) return 0; /* no more descs */
2846 if (last_desc->status & TLP_DSTS_OWNER) return 0; /* still active */
2847 if (last_desc->status & TLP_DSTS_RX_LAST_DESC) break; /* end of packet */
2848 pkt_len += last_desc->length1 + last_desc->length2; /* entire desc filled */
2849 if (last_desc++->control & TLP_DCTL_END_RING) last_desc = ring->first; /* ring wrap */
2852 /* A complete packet has arrived; how long is it? */
2853 /* H/w ref man shows RX pkt length as a 14-bit field. */
2854 /* An experiment found that only the 12 LSBs work. */
2855 if (((last_desc->status>>16)&0xFFF) == 0) pkt_len += 4096; /* carry-bit */
2856 pkt_len = (pkt_len & 0xF000) + ((last_desc->status>>16) & 0x0FFF);
2857 /* Subtract the CRC length unless doing so would underflow. */
2858 if (pkt_len >= sc->config.crc_len) pkt_len -= sc->config.crc_len;
2860 /* Run down DMA descriptors again doing the following:
2861 * 1) put pkt info in pkthdr of first mbuf,
2863 * 3) set mbuf lengths.
2865 first_desc = ring->read;
2868 /* Read a DMA descriptor from the ring. */
2869 last_desc = ring->read;
2870 /* Advance the ring read pointer. */
2871 if (ring->read++ == ring->last) ring->read = ring->first;
2873 /* Dequeue the corresponding cluster mbuf. */
2874 new_mbuf = mbuf_dequeue(ring);
2875 if (new_mbuf == NULL)
2876 panic("%s: rxintr_cleanup: expected an mbuf\n", NAME_UNIT);
2878 desc_len = last_desc->length1 + last_desc->length2;
2879 /* If bouncing, copy bounce buf to mbuf. */
2880 DMA_SYNC(last_desc->map, desc_len, BUS_DMASYNC_POSTREAD);
2881 /* Unmap kernel virtual address to PCI address. */
2882 bus_dmamap_unload(ring->tag, last_desc->map);
2884 /* 1) Put pkt info in pkthdr of first mbuf. */
2885 if (last_desc == first_desc)
2887 first_mbuf = new_mbuf;
2888 first_mbuf->m_pkthdr.len = pkt_len; /* total pkt length */
2890 first_mbuf->m_pkthdr.rcvif = sc->ifp; /* how it got here */
2892 first_mbuf->m_pkthdr.rcvif = NULL;
2895 else /* 2) link mbufs. */
2897 last_mbuf->m_next = new_mbuf;
2898 /* M_PKTHDR should be set in the first mbuf only. */
2899 new_mbuf->m_flags &= ~M_PKTHDR;
2901 last_mbuf = new_mbuf;
2903 /* 3) Set mbuf lengths. */
2904 new_mbuf->m_len = (pkt_len >= desc_len) ? desc_len : pkt_len;
2905 pkt_len -= new_mbuf->m_len;
2906 } while ((last_desc->status & TLP_DSTS_RX_LAST_DESC) == 0);
2908 /* Decide whether to accept or to discard this packet. */
2909 /* RxHDLC sets MIIERR for bad CRC, abort and partial byte at pkt end. */
2910 if (((last_desc->status & TLP_DSTS_RX_BAD) == 0) &&
2911 (sc->status.oper_status == STATUS_UP) &&
2912 (first_mbuf->m_pkthdr.len > 0))
2914 /* Optimization: copy a small pkt into a small mbuf. */
2915 if (first_mbuf->m_pkthdr.len <= COPY_BREAK)
2917 MGETHDR(new_mbuf, M_DONTWAIT, MT_DATA);
2918 if (new_mbuf != NULL)
2920 new_mbuf->m_pkthdr.rcvif = first_mbuf->m_pkthdr.rcvif;
2921 new_mbuf->m_pkthdr.len = first_mbuf->m_pkthdr.len;
2922 new_mbuf->m_len = first_mbuf->m_len;
2923 memcpy(new_mbuf->m_data, first_mbuf->m_data,
2924 first_mbuf->m_pkthdr.len);
2925 m_freem(first_mbuf);
2926 first_mbuf = new_mbuf;
2929 /* Include CRC and one flag byte in input byte count. */
2930 sc->status.cntrs.ibytes += first_mbuf->m_pkthdr.len + sc->config.crc_len +1;
2931 sc->status.cntrs.ipackets++;
2933 sc->ifp->if_ipackets++;
2934 LMC_BPF_MTAP(first_mbuf);
2936 #if (defined(__FreeBSD__) && defined(DEVICE_POLLING))
2940 /* Give this good packet to the network stacks. */
2942 if (sc->ng_hook != NULL) /* is hook connected? */
2944 # if (__FreeBSD_version >= 500000)
2945 int error; /* ignore error */
2946 NG_SEND_DATA_ONLY(error, sc->ng_hook, first_mbuf);
2947 # else /* FreeBSD-4 */
2948 ng_queue_data(sc->ng_hook, first_mbuf, NULL);
2950 return 1; /* did something */
2952 #endif /* NETGRAPH */
2953 if (sc->config.line_pkg == PKG_RAWIP)
2954 lmc_raw_input(sc->ifp, first_mbuf);
2958 sppp_input(sc->ifp, first_mbuf);
2960 new_mbuf = first_mbuf;
2961 while (new_mbuf != NULL)
2963 sc->p2p->p2p_hdrinput(sc->p2p, new_mbuf->m_data, new_mbuf->m_len);
2964 new_mbuf = new_mbuf->m_next;
2966 sc->p2p->p2p_input(sc->p2p, NULL);
2967 m_freem(first_mbuf);
2969 m_freem(first_mbuf);
2970 sc->status.cntrs.idiscards++;
2974 else if (sc->status.oper_status != STATUS_UP)
2976 /* If the link is down, this packet is probably noise. */
2977 m_freem(first_mbuf);
2978 sc->status.cntrs.idiscards++;
2980 printf("%s: rxintr_cleanup: rx pkt discarded: link down\n", NAME_UNIT);
2982 else /* Log and discard this bad packet. */
2985 printf("%s: RX bad pkt; len=%d %s%s%s%s\n",
2986 NAME_UNIT, first_mbuf->m_pkthdr.len,
2987 (last_desc->status & TLP_DSTS_RX_MII_ERR) ? " miierr" : "",
2988 (last_desc->status & TLP_DSTS_RX_DRIBBLE) ? " dribble" : "",
2989 (last_desc->status & TLP_DSTS_RX_DESC_ERR) ? " descerr" : "",
2990 (last_desc->status & TLP_DSTS_RX_OVERRUN) ? " overrun" : "");
2991 if (last_desc->status & TLP_DSTS_RX_OVERRUN)
2992 sc->status.cntrs.fifo_over++;
2994 sc->status.cntrs.ierrors++;
2995 m_freem(first_mbuf);
2998 return 1; /* did something */
3001 /* Setup (prepare) to receive a packet. */
3002 /* Try to keep the RX descriptor ring full of empty buffers. */
3003 static int /* BSD version */
3004 rxintr_setup(softc_t *sc)
3006 struct desc_ring *ring = &sc->rxring;
3007 struct dma_desc *desc;
3012 /* Ring is full if (wrap(write+1)==read) */
3013 if (((ring->write == ring->last) ? ring->first : ring->write+1) == ring->read)
3014 return 0; /* ring is full; nothing to do */
3016 /* Allocate a small mbuf and attach an mbuf cluster. */
3017 MGETHDR(m, M_DONTWAIT, MT_DATA);
3020 sc->status.cntrs.rxdma++;
3022 printf("%s: rxintr_setup: MGETHDR() failed\n", NAME_UNIT);
3025 MCLGET(m, M_DONTWAIT);
3026 if ((m->m_flags & M_EXT) == 0)
3029 sc->status.cntrs.rxdma++;
3031 printf("%s: rxintr_setup: MCLGET() failed\n", NAME_UNIT);
3035 /* Queue the mbuf for later processing by rxintr_cleanup. */
3036 mbuf_enqueue(ring, m);
3038 /* Write a DMA descriptor into the ring. */
3039 /* Hardware won't see it until the OWNER bit is set. */
3041 /* Advance the ring write pointer. */
3042 if (ring->write++ == ring->last) ring->write = ring->first;
3044 desc_len = (MCLBYTES < MAX_DESC_LEN) ? MCLBYTES : MAX_DESC_LEN;
3045 /* Map kernel virtual address to PCI address. */
3046 if ((error = DMA_LOAD(desc->map, m->m_data, desc_len)))
3047 printf("%s: bus_dmamap_load(rx) failed; error %d\n", NAME_UNIT, error);
3048 /* Invalidate the cache for this mbuf. */
3049 DMA_SYNC(desc->map, desc_len, BUS_DMASYNC_PREREAD);
3051 /* Set up the DMA descriptor. */
3053 desc->address1 = ring->segs[0].ds_addr;
3054 #elif (defined(__NetBSD__) || defined(__OpenBSD__))
3055 desc->address1 = desc->map->dm_segs[0].ds_addr;
3056 #elif defined(__bsdi__)
3057 desc->address1 = vtophys(m->m_data); /* Relax! BSD/OS only. */
3059 desc->length1 = desc_len>>1;
3060 desc->address2 = desc->address1 + desc->length1;
3061 desc->length2 = desc_len>>1;
3063 /* Before setting the OWNER bit, flush the cache (memory barrier). */
3064 DMA_SYNC(ring->map, ring->size_descs, BUS_DMASYNC_PREWRITE);
3066 /* Commit the DMA descriptor to the hardware. */
3067 desc->status = TLP_DSTS_OWNER;
3069 /* Notify the receiver that there is another buffer available. */
3070 WRITE_CSR(TLP_RX_POLL, 1);
3072 return 1; /* did something */
3075 /* Clean up after a packet has been transmitted. */
3076 /* Free the mbuf chain and update the DMA descriptor ring. */
3077 static int /* BSD version */
3078 txintr_cleanup(softc_t *sc)
3080 struct desc_ring *ring = &sc->txring;
3081 struct dma_desc *desc;
3083 while ((ring->read != ring->write) && /* while ring is not empty */
3084 ((ring->read->status & TLP_DSTS_OWNER) == 0))
3086 /* Read a DMA descriptor from the ring. */
3088 /* Advance the ring read pointer. */
3089 if (ring->read++ == ring->last) ring->read = ring->first;
3091 /* This is a no-op on most architectures. */
3092 DMA_SYNC(desc->map, desc->length1 + desc->length2, BUS_DMASYNC_POSTWRITE);
3093 /* Unmap kernel virtual address to PCI address. */
3094 bus_dmamap_unload(ring->tag, desc->map);
3096 /* If this descriptor is the last segment of a packet, */
3097 /* then dequeue and free the corresponding mbuf chain. */
3098 if ((desc->control & TLP_DCTL_TX_LAST_SEG) != 0)
3101 if ((m = mbuf_dequeue(ring)) == NULL)
3102 panic("%s: txintr_cleanup: expected an mbuf\n", NAME_UNIT);
3104 /* Include CRC and one flag byte in output byte count. */
3105 sc->status.cntrs.obytes += m->m_pkthdr.len + sc->config.crc_len +1;
3106 sc->status.cntrs.opackets++;
3108 sc->ifp->if_opackets++;
3111 /* The only bad TX status is fifo underrun. */
3112 if ((desc->status & TLP_DSTS_TX_UNDERRUN) != 0)
3113 sc->status.cntrs.fifo_under++;
3116 return 1; /* did something */
3123 /* Build DMA descriptors for a transmit packet mbuf chain. */
3124 static int /* 0=success; 1=error */ /* BSD version */
3125 txintr_setup_mbuf(softc_t *sc, struct mbuf *m)
3127 struct desc_ring *ring = &sc->txring;
3128 struct dma_desc *desc;
3129 unsigned int desc_len;
3131 /* build DMA descriptors for a chain of mbufs. */
3134 char *data = m->m_data;
3135 int length = m->m_len; /* zero length mbufs happen! */
3137 /* Build DMA descriptors for one mbuf. */
3142 /* Ring is full if (wrap(write+1)==read) */
3143 if (((ring->temp==ring->last) ? ring->first : ring->temp+1) == ring->read)
3144 { /* Not enough DMA descriptors; try later. */
3145 for (; ring->temp!=ring->write;
3146 ring->temp = (ring->temp==ring->first)? ring->last : ring->temp-1)
3147 bus_dmamap_unload(ring->tag, ring->temp->map);
3148 sc->status.cntrs.txdma++;
3152 /* Provisionally, write a descriptor into the ring. */
3153 /* But don't change the REAL ring write pointer. */
3154 /* Hardware won't see it until the OWNER bit is set. */
3156 /* Advance the temporary ring write pointer. */
3157 if (ring->temp++ == ring->last) ring->temp = ring->first;
3159 /* Clear all control bits except the END_RING bit. */
3160 desc->control &= TLP_DCTL_END_RING;
3161 /* Don't pad short packets up to 64 bytes. */
3162 desc->control |= TLP_DCTL_TX_NO_PAD;
3163 /* Use Tulip's CRC-32 generator, if appropriate. */
3164 if (sc->config.crc_len != CFG_CRC_32)
3165 desc->control |= TLP_DCTL_TX_NO_CRC;
3166 /* Set the OWNER bit, except in the first descriptor. */
3167 if (desc != ring->write)
3168 desc->status = TLP_DSTS_OWNER;
3170 desc_len = (length > MAX_CHUNK_LEN) ? MAX_CHUNK_LEN : length;
3171 /* Map kernel virtual address to PCI address. */
3172 if ((error = DMA_LOAD(desc->map, data, desc_len)))
3173 printf("%s: bus_dmamap_load(tx) failed; error %d\n", NAME_UNIT, error);
3174 /* Flush the cache and if bouncing, copy mbuf to bounce buf. */
3175 DMA_SYNC(desc->map, desc_len, BUS_DMASYNC_PREWRITE);
3177 /* Prevent wild fetches if mapping fails (nsegs==0). */
3178 desc->length1 = desc->length2 = 0;
3179 desc->address1 = desc->address2 = 0;
3180 #if (defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__))
3183 bus_dma_segment_t *segs = ring->segs;
3184 int nsegs = ring->nsegs;
3185 # elif (defined(__NetBSD__) || defined(__OpenBSD__))
3186 bus_dma_segment_t *segs = desc->map->dm_segs;
3187 int nsegs = desc->map->dm_nsegs;
3191 desc->address1 = segs[0].ds_addr;
3192 desc->length1 = segs[0].ds_len;
3196 desc->address2 = segs[1].ds_addr;
3197 desc->length2 = segs[1].ds_len;
3200 #elif defined(__bsdi__)
3201 desc->address1 = vtophys(data); /* Relax! BSD/OS only. */
3202 desc->length1 = desc_len;
3207 } /* while (length > 0) */
3210 } /* while (m != NULL) */
3212 return 0; /* success */
3215 /* Setup (prepare) to transmit a packet. */
3216 /* Select a packet, build DMA descriptors and give packet to hardware. */
3217 /* If DMA descriptors run out, abandon the attempt and return 0. */
3218 static int /* BSD version */
3219 txintr_setup(softc_t *sc)
3221 struct desc_ring *ring = &sc->txring;
3222 struct dma_desc *first_desc, *last_desc;
3224 /* Protect against half-up links: Don't transmit */
3225 /* if the receiver can't hear the far end. */
3226 if (sc->status.oper_status != STATUS_UP) return 0;
3228 /* Pick a packet to transmit. */
3230 if ((sc->ng_hook != NULL) && (sc->tx_mbuf == NULL))
3232 if (!IFQ_IS_EMPTY(&sc->ng_fastq))
3233 IFQ_DEQUEUE(&sc->ng_fastq, sc->tx_mbuf);
3235 IFQ_DEQUEUE(&sc->ng_sndq, sc->tx_mbuf);
3239 if (sc->tx_mbuf == NULL)
3241 if (sc->config.line_pkg == PKG_RAWIP)
3242 IFQ_DEQUEUE(&sc->ifp->if_snd, sc->tx_mbuf);
3246 sc->tx_mbuf = sppp_dequeue(sc->ifp);
3248 if (!IFQ_IS_EMPTY(&sc->p2p->p2p_isnd))
3249 IFQ_DEQUEUE(&sc->p2p->p2p_isnd, sc->tx_mbuf);
3251 IFQ_DEQUEUE(&sc->ifp->if_snd, sc->tx_mbuf);
3255 if (sc->tx_mbuf == NULL) return 0; /* no pkt to transmit */
3257 /* Build DMA descriptors for an outgoing mbuf chain. */
3258 ring->temp = ring->write; /* temporary ring write pointer */
3259 if (txintr_setup_mbuf(sc, sc->tx_mbuf) != 0) return 0;
3261 /* Enqueue the mbuf; txintr_cleanup will free it. */
3262 mbuf_enqueue(ring, sc->tx_mbuf);
3264 /* The transmitter has room for another packet. */
3267 /* Set first & last segment bits. */
3268 /* last_desc is the desc BEFORE the one pointed to by ring->temp. */
3269 first_desc = ring->write;
3270 first_desc->control |= TLP_DCTL_TX_FIRST_SEG;
3271 last_desc = (ring->temp==ring->first)? ring->last : ring->temp-1;
3272 last_desc->control |= TLP_DCTL_TX_LAST_SEG;
3273 /* Interrupt at end-of-transmission? Why bother the poor computer! */
3274 /* last_desc->control |= TLP_DCTL_TX_INTERRUPT; */
3276 /* Make sure the OWNER bit is not set in the next descriptor. */
3277 /* The OWNER bit may have been set if a previous call aborted. */
3278 ring->temp->status = 0;
3280 /* Commit the DMA descriptors to the software. */
3281 ring->write = ring->temp;
3283 /* Before setting the OWNER bit, flush the cache (memory barrier). */
3284 DMA_SYNC(ring->map, ring->size_descs, BUS_DMASYNC_PREWRITE);
3286 /* Commit the DMA descriptors to the hardware. */
3287 first_desc->status = TLP_DSTS_OWNER;
3289 /* Notify the transmitter that there is another packet to send. */
3290 WRITE_CSR(TLP_TX_POLL, 1);
3292 return 1; /* did something */
3298 /* NOTE: this is the LINUX version of the interrupt/DMA code, */
3300 /* Singly-linked tail-queues hold sk_buffs with active DMA.
3301 * skbuffs are linked through their sk_buff.next field.
3302 * Callers must hold sc->bottom_lock; not otherwise locked.
3305 /* Put an skbuff on the tail of the descriptor ring queue. */
3306 static void /* Linux version */
3307 skbuff_enqueue(struct desc_ring *ring, struct sk_buff *skb)
3310 if (ring->tail == NULL)
3313 ring->tail->next = skb;
3317 /* Get an skbuff from the head of the descriptor ring queue. */
3318 static struct sk_buff* /* Linux version */
3319 skbuff_dequeue(struct desc_ring *ring)
3321 struct sk_buff *skb = ring->head;
3323 if ((ring->head = skb->next) == NULL)
3328 /* Initialize a DMA descriptor ring. */
3329 static int /* Linux version */
3330 create_ring(softc_t *sc, struct desc_ring *ring, int num_descs)
3332 struct dma_desc *descs;
3333 int size_descs = sizeof(struct dma_desc)*num_descs;
3335 /* Allocate and map memory for DMA descriptor array. */
3336 if ((descs = pci_alloc_consistent(sc->pci_dev, size_descs,
3337 &ring->dma_addr)) == NULL)
3339 printk("%s: pci_alloc_consistent() failed\n", NAME_UNIT);
3342 memset(descs, 0, size_descs);
3345 ring->write = descs;
3346 ring->first = descs;
3347 ring->last = descs + num_descs -1;
3348 ring->last->control = TLP_DCTL_END_RING;
3349 ring->num_descs = num_descs;
3350 ring->size_descs = size_descs;
3357 /* Destroy a DMA descriptor ring */
3358 static void /* Linux version */
3359 destroy_ring(softc_t *sc, struct desc_ring *ring)
3361 struct sk_buff *skb;
3363 /* Free queued skbuffs. */
3364 while ((skb = skbuff_dequeue(ring)) != NULL)
3367 /* TX may have one pkt that is not on any queue. */
3368 if (sc->tx_skb != NULL)
3370 dev_kfree_skb(sc->tx_skb);
3374 if (ring->first != NULL)
3376 /* Unmap active DMA descriptors. */
3377 while (ring->read != ring->write)
3379 pci_unmap_single(sc->pci_dev, ring->read->address1,
3380 ring->read->length1 + ring->read->length2, PCI_DMA_BIDIRECTIONAL);
3381 if (ring->read++ == ring->last) ring->read = ring->first;
3384 /* Unmap and free memory for DMA descriptor array. */
3385 pci_free_consistent(sc->pci_dev, ring->size_descs, ring->first,
3390 static int /* Linux version */
3391 rxintr_cleanup(softc_t *sc)
3393 struct desc_ring *ring = &sc->rxring;
3394 struct dma_desc *first_desc, *last_desc;
3395 struct sk_buff *first_skb=NULL, *last_skb=NULL;
3396 struct sk_buff *new_skb;
3397 int pkt_len, desc_len;
3399 /* Input packet flow control (livelock prevention): */
3400 /* Give pkts to higher levels only if quota is > 0. */
3401 if (sc->quota <= 0) return 0;
3403 /* This looks complicated, but remember: packets up to 4032 */
3404 /* bytes long fit in one skbuff and use one DMA descriptor. */
3406 first_desc = last_desc = ring->read;
3408 /* ASSERTION: If there is a descriptor in the ring and the hardware has */
3409 /* finished with it, then that descriptor will have RX_FIRST_DESC set. */
3410 if ((ring->read != ring->write) && /* descriptor ring not empty */
3411 ((ring->read->status & TLP_DSTS_OWNER) == 0) && /* hardware done */
3412 ((ring->read->status & TLP_DSTS_RX_FIRST_DESC) == 0)) /* should be set */
3413 panic("%s: rxintr_cleanup: rx-first-descriptor not set.\n", NAME_UNIT);
3415 /* First decide if a complete packet has arrived. */
3416 /* Run down DMA descriptors looking for one marked "last". */
3417 /* Bail out if an active descriptor is encountered. */
3418 /* Accumulate most significant bits of packet length. */
3422 if (last_desc == ring->write) return 0; /* no more descs */
3423 if (last_desc->status & TLP_DSTS_OWNER) return 0; /* still active */
3424 if (last_desc->status & TLP_DSTS_RX_LAST_DESC) break; /* end of packet */
3425 pkt_len += last_desc->length1 + last_desc->length2; /* entire desc filled */
3426 if (last_desc++->control & TLP_DCTL_END_RING) last_desc = ring->first; /* ring wrap */
3429 /* A complete packet has arrived; how long is it? */
3430 /* H/w ref man shows RX pkt length as a 14-bit field. */
3431 /* An experiment found that only the 12 LSBs work. */
3432 if (((last_desc->status>>16)&0xFFF) == 0) pkt_len += 4096; /* carry-bit */
3433 pkt_len = (pkt_len & 0xF000) + ((last_desc->status>>16) & 0x0FFF);
3434 /* Subtract the CRC length unless doing so would underflow. */
3435 if (pkt_len >= sc->config.crc_len) pkt_len -= sc->config.crc_len;
3437 /* Run down DMA descriptors again doing the following:
3438 * 1) put pkt info in hdr of first skbuff.
3439 * 2) put additional skbuffs on frag_list.
3440 * 3) set skbuff lengths.
3442 first_desc = ring->read;
3445 /* Read a DMA descriptor from the ring. */
3446 last_desc = ring->read;
3447 /* Advance the ring read pointer. */
3448 if (ring->read++ == ring->last) ring->read = ring->first;
3450 /* Dequeue the corresponding skbuff. */
3451 new_skb = skbuff_dequeue(ring);
3452 if (new_skb == NULL)
3453 panic("%s: rxintr_cleanup: expected an skbuff\n", NAME_UNIT);
3455 desc_len = last_desc->length1 + last_desc->length2;
3456 /* Unmap kernel virtual addresss to PCI address. */
3457 pci_unmap_single(sc->pci_dev, last_desc->address1,
3458 desc_len, PCI_DMA_FROMDEVICE);
3460 /* Set skbuff length. */
3461 skb_put(new_skb, (pkt_len >= desc_len) ? desc_len : pkt_len);
3462 pkt_len -= new_skb->len;
3464 /* 1) Put pkt info in hdr of first skbuff. */
3465 if (last_desc == first_desc)
3467 first_skb = new_skb;
3468 if (sc->config.line_pkg == PKG_RAWIP)
3470 if (first_skb->data[0]>>4 == 4)
3471 first_skb->protocol = htons(ETH_P_IP);
3472 else if (first_skb->data[0]>>4 == 6)
3473 first_skb->protocol = htons(ETH_P_IPV6);
3477 first_skb->protocol = hdlc_type_trans(first_skb, sc->net_dev);
3479 first_skb->protocol = htons(ETH_P_HDLC);
3481 first_skb->mac.raw = first_skb->data;
3482 first_skb->dev = sc->net_dev;
3483 do_gettimeofday(&first_skb->stamp);
3484 sc->net_dev->last_rx = jiffies;
3486 else /* 2) link skbuffs. */
3488 /* Put this skbuff on the frag_list of the first skbuff. */
3489 new_skb->next = NULL;
3490 if (skb_shinfo(first_skb)->frag_list == NULL)
3491 skb_shinfo(first_skb)->frag_list = new_skb;
3493 last_skb->next = new_skb;
3494 /* 3) set skbuff lengths. */
3495 first_skb->len += new_skb->len;
3496 first_skb->data_len += new_skb->len;
3499 } while ((last_desc->status & TLP_DSTS_RX_LAST_DESC) == 0);
3501 /* Decide whether to accept or to discard this packet. */
3502 /* RxHDLC sets MIIERR for bad CRC, abort and partial byte at pkt end. */
3503 if (((last_desc->status & TLP_DSTS_RX_BAD) == 0) &&
3504 (sc->status.oper_status == STATUS_UP) &&
3505 (first_skb->len > 0))
3507 /* Optimization: copy a small pkt into a small skbuff. */
3508 if (first_skb->len <= COPY_BREAK)
3509 if ((new_skb = skb_copy(first_skb, GFP_ATOMIC)) != NULL)
3511 dev_kfree_skb_any(first_skb);
3512 first_skb = new_skb;
3515 /* Include CRC and one flag byte in input byte count. */
3516 sc->status.cntrs.ibytes += first_skb->len + sc->config.crc_len +1;
3517 sc->status.cntrs.ipackets++;
3519 /* Give this good packet to the network stacks. */
3520 netif_receive_skb(first_skb); /* NAPI */
3523 else if (sc->status.oper_status != STATUS_UP)
3525 /* If the link is down, this packet is probably noise. */
3526 sc->status.cntrs.idiscards++;
3527 dev_kfree_skb_any(first_skb);
3529 printk("%s: rxintr_cleanup: rx pkt discarded: link down\n", NAME_UNIT);
3531 else /* Log and discard this bad packet. */
3534 printk("%s: RX bad pkt; len=%d %s%s%s%s\n",
3535 NAME_UNIT, first_skb->len,
3536 (last_desc->status & TLP_DSTS_RX_MII_ERR) ? " miierr" : "",
3537 (last_desc->status & TLP_DSTS_RX_DRIBBLE) ? " dribble" : "",
3538 (last_desc->status & TLP_DSTS_RX_DESC_ERR) ? " descerr" : "",
3539 (last_desc->status & TLP_DSTS_RX_OVERRUN) ? " overrun" : "");
3540 if (last_desc->status & TLP_DSTS_RX_OVERRUN)
3541 sc->status.cntrs.fifo_over++;
3543 sc->status.cntrs.ierrors++;
3544 dev_kfree_skb_any(first_skb);
3547 return 1; /* did something */
3550 /* Setup (prepare) to receive a packet. */
3551 /* Try to keep the RX descriptor ring full of empty buffers. */
3552 static int /* Linux version */
3553 rxintr_setup(softc_t *sc)
3555 struct desc_ring *ring = &sc->rxring;
3556 struct dma_desc *desc;
3557 struct sk_buff *skb;
3560 /* Ring is full if (wrap(write+1)==read) */
3561 if (((ring->write == ring->last) ? ring->first : ring->write+1) == ring->read)
3562 return 0; /* ring is full; nothing to do */
3564 /* Allocate an skbuff. */
3565 if ((skb = dev_alloc_skb(MAX_DESC_LEN)) == NULL)
3567 sc->status.cntrs.rxdma++;
3569 printk("%s: rxintr_setup: dev_alloc_skb() failed\n", NAME_UNIT);
3572 skb->dev = sc->net_dev;
3574 /* Queue the skbuff for later processing by rxintr_cleanup. */
3575 skbuff_enqueue(ring, skb);
3577 /* Write a DMA descriptor into the ring. */
3578 /* Hardware won't see it until the OWNER bit is set. */
3580 /* Advance the ring write pointer. */
3581 if (ring->write++ == ring->last) ring->write = ring->first;
3583 /* Map kernel virtual addresses to PCI addresses. */
3584 dma_addr = pci_map_single(sc->pci_dev, skb->data,
3585 MAX_DESC_LEN, PCI_DMA_FROMDEVICE);
3586 /* Set up the DMA descriptor. */
3587 desc->address1 = dma_addr;
3588 desc->length1 = MAX_CHUNK_LEN;
3589 desc->address2 = desc->address1 + desc->length1;
3590 desc->length2 = MAX_CHUNK_LEN;
3592 /* Before setting the OWNER bit, flush the cache (memory barrier). */
3593 wmb(); /* write memory barrier */
3595 /* Commit the DMA descriptor to the hardware. */
3596 desc->status = TLP_DSTS_OWNER;
3598 /* Notify the receiver that there is another buffer available. */
3599 WRITE_CSR(TLP_RX_POLL, 1);
3601 return 1; /* did something */
3604 /* Clean up after a packet has been transmitted. */
3605 /* Free the sk_buff and update the DMA descriptor ring. */
3606 static int /* Linux version */
3607 txintr_cleanup(softc_t *sc)
3609 struct desc_ring *ring = &sc->txring;
3610 struct dma_desc *desc;
3612 while ((ring->read != ring->write) && /* ring is not empty */
3613 ((ring->read->status & TLP_DSTS_OWNER) == 0))
3615 /* Read a DMA descriptor from the ring. */
3617 /* Advance the ring read pointer. */
3618 if (ring->read++ == ring->last) ring->read = ring->first;
3619 /* Unmap kernel virtual address to PCI address. */
3620 pci_unmap_single(sc->pci_dev, desc->address1,
3621 desc->length1 + desc->length2, PCI_DMA_TODEVICE);
3623 /* If this descriptor is the last segment of a packet, */
3624 /* then dequeue and free the corresponding skbuff. */
3625 if ((desc->control & TLP_DCTL_TX_LAST_SEG) != 0)
3627 struct sk_buff *skb;
3628 if ((skb = skbuff_dequeue(ring)) == NULL)
3629 panic("%s: txintr_cleanup: expected an sk_buff\n", NAME_UNIT);
3631 /* Include CRC and one flag byte in output byte count. */
3632 sc->status.cntrs.obytes += skb->len + sc->config.crc_len +1;
3633 sc->status.cntrs.opackets++;
3635 /* The only bad TX status is fifo underrun. */
3636 if ((desc->status & TLP_DSTS_TX_UNDERRUN) != 0)
3638 sc->status.cntrs.fifo_under++; /* also increment oerrors? */
3640 printk("%s: txintr_cleanup: tx fifo underrun\n", NAME_UNIT);
3643 dev_kfree_skb_any(skb);
3644 return 1; /* did something */
3651 /* Build DMA descriptors for a tranmit packet fragment, */
3652 /* Assertion: fragment is contiguous in physical memory. */
3653 static int /* 0=success; 1=error */ /* linux version */
3654 txintr_setup_frag(softc_t *sc, char *data, int length)
3656 struct desc_ring *ring = &sc->txring;
3657 struct dma_desc *desc;
3658 unsigned int desc_len;
3663 /* Ring is full if (wrap(write+1)==read) */
3664 if (((ring->temp==ring->last) ? ring->first : ring->temp+1) == ring->read)
3665 { /* Not enough DMA descriptors; try later. */
3666 for (; ring->temp!=ring->write;
3667 ring->temp = (ring->temp==ring->first)? ring->last : ring->temp-1)
3668 pci_unmap_single(sc->pci_dev, ring->temp->address1,
3669 ring->temp->length1 + ring->temp->length2, PCI_DMA_FROMDEVICE);
3670 sc->status.cntrs.txdma++;
3674 /* Provisionally, write a DMA descriptor into the ring. */
3675 /* But don't change the REAL ring write pointer. */
3676 /* Hardware won't see it until the OWNER bit is set. */
3678 /* Advance the temporary ring write pointer. */
3679 if (ring->temp++ == ring->last) ring->temp = ring->first;
3681 /* Clear all control bits except the END_RING bit. */
3682 desc->control &= TLP_DCTL_END_RING;
3683 /* Don't pad short packets up to 64 bytes */
3684 desc->control |= TLP_DCTL_TX_NO_PAD;
3685 /* Use Tulip's CRC-32 generator, if appropriate. */
3686 if (sc->config.crc_len != CFG_CRC_32)
3687 desc->control |= TLP_DCTL_TX_NO_CRC;
3688 /* Set the OWNER bit, except in the first descriptor. */
3689 if (desc != ring->write)
3690 desc->status = TLP_DSTS_OWNER;
3692 desc_len = (length >= MAX_DESC_LEN) ? MAX_DESC_LEN : length;
3693 /* Map kernel virtual address to PCI address. */
3694 dma_addr = pci_map_single(sc->pci_dev, data, desc_len, PCI_DMA_TODEVICE);
3695 /* If it will fit in one chunk, do so, otherwise split it. */
3696 if (desc_len <= MAX_CHUNK_LEN)
3698 desc->address1 = dma_addr;
3699 desc->length1 = desc_len;
3705 desc->address1 = dma_addr;
3706 desc->length1 = desc_len>>1;
3707 desc->address2 = desc->address1 + desc->length1;
3708 desc->length2 = desc_len>>1;
3709 if (desc_len & 1) desc->length2++;
3714 } /* while (length > 0) */
3716 return 0; /* success */
3719 /* NB: this procedure is recursive! */
3720 static int /* 0=success; 1=error */
3721 txintr_setup_skb(softc_t *sc, struct sk_buff *skb)
3723 struct sk_buff *list;
3726 /* First, handle the data in the skbuff itself. */
3727 if (txintr_setup_frag(sc, skb->data, skb_headlen(skb)))
3730 /* Next, handle the VM pages in the Scatter/Gather list. */
3731 if (skb_shinfo(skb)->nr_frags != 0)
3732 for (i=0; i<skb_shinfo(skb)->nr_frags; i++)
3734 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3735 if (txintr_setup_frag(sc, page_address(frag->page) +
3736 frag->page_offset, frag->size))
3740 /* Finally, handle the skbuffs in the frag_list. */
3741 if ((list = skb_shinfo(skb)->frag_list) != NULL)
3742 for (; list; list=list->next)
3743 if (txintr_setup_skb(sc, list)) /* recursive! */
3749 /* Setup (prepare) to transmit a packet. */
3750 /* Select a packet, build DMA descriptors and give packet to hardware. */
3751 /* If DMA descriptors run out, abandon the attempt and return 0. */
3752 static int /* Linux version */
3753 txintr_setup(softc_t *sc)
3755 struct desc_ring *ring = &sc->txring;
3756 struct dma_desc *first_desc, *last_desc;
3758 /* Protect against half-up links: Don't transmit */
3759 /* if the receiver can't hear the far end. */
3760 if (sc->status.oper_status != STATUS_UP) return 0;
3762 /* Pick a packet to transmit. */
3763 /* linux_start() puts packets in sc->tx_skb. */
3764 if (sc->tx_skb == NULL)
3766 if (netif_queue_stopped(sc->net_dev) != 0)
3767 netif_wake_queue(sc->net_dev);
3768 return 0; /* no pkt to transmit */
3771 /* Build DMA descriptors for an outgoing skbuff. */
3772 ring->temp = ring->write; /* temporary ring write pointer */
3773 if (txintr_setup_skb(sc, sc->tx_skb) != 0) return 0;
3775 /* Enqueue the skbuff; txintr_cleanup will free it. */
3776 skbuff_enqueue(ring, sc->tx_skb);
3778 /* The transmitter has room for another packet. */
3781 /* Set first & last segment bits. */
3782 /* last_desc is the desc BEFORE the one pointed to by ring->temp. */
3783 first_desc = ring->write;
3784 first_desc->control |= TLP_DCTL_TX_FIRST_SEG;
3785 last_desc = (ring->temp==ring->first)? ring->last : ring->temp-1;
3786 last_desc->control |= TLP_DCTL_TX_LAST_SEG;
3787 /* Interrupt at end-of-transmission? Why bother the poor computer! */
3788 /* last_desc->control |= TLP_DCTL_TX_INTERRUPT; */
3790 /* Make sure the OWNER bit is not set in the next descriptor. */
3791 /* The OWNER bit may have been set if a previous call aborted. */
3792 ring->temp->status = 0;
3794 /* Commit the DMA descriptors to the software. */
3795 ring->write = ring->temp;
3797 /* Before setting the OWNER bit, flush the cache (memory barrier). */
3798 wmb(); /* write memory barrier */
3800 /* Commit the DMA descriptors to the hardware. */
3801 first_desc->status = TLP_DSTS_OWNER;
3803 /* Notify the transmitter that there is another packet to send. */
3804 WRITE_CSR(TLP_TX_POLL, 1);
3806 sc->net_dev->trans_start = jiffies;
3808 return 1; /* did something */
3811 #endif /* __linux__ */
3814 check_intr_status(softc_t *sc)
3816 u_int32_t status, cfcs, op_mode;
3817 u_int32_t missed, overruns;
3819 /* Check for four unusual events:
3820 * 1) fatal PCI bus errors - some are recoverable
3821 * 2) transmitter FIFO underruns - increase fifo threshold
3822 * 3) receiver FIFO overruns - clear potential hangup
3823 * 4) no receive descs or bufs - count missed packets
3826 /* 1) A fatal bus error causes a Tulip to stop initiating bus cycles. */
3827 /* Module unload/load or boot are the only fixes for Parity Errors. */
3828 /* Master and Target Aborts can be cleared and life may continue. */
3829 status = READ_CSR(TLP_STATUS);
3830 if ((status & TLP_STAT_FATAL_ERROR) != 0)
3832 u_int32_t fatal = (status & TLP_STAT_FATAL_BITS)>>TLP_STAT_FATAL_SHIFT;
3833 printf("%s: FATAL PCI BUS ERROR: %s%s%s%s\n", NAME_UNIT,
3834 (fatal == 0) ? "PARITY ERROR" : "",
3835 (fatal == 1) ? "MASTER ABORT" : "",
3836 (fatal == 2) ? "TARGET ABORT" : "",
3837 (fatal >= 3) ? "RESERVED (?)" : "");
3838 cfcs = READ_PCI_CFG(sc, TLP_CFCS); /* try to clear it */
3839 cfcs &= ~(TLP_CFCS_MSTR_ABORT | TLP_CFCS_TARG_ABORT);
3840 WRITE_PCI_CFG(sc, TLP_CFCS, cfcs);
3843 /* 2) If the transmitter fifo underruns, increase the transmit fifo */
3844 /* threshold: the number of bytes required to be in the fifo */
3845 /* before starting the transmitter (cost: increased tx delay). */
3846 /* The TX_FSM must be stopped to change this parameter. */
3847 if ((status & TLP_STAT_TX_UNDERRUN) != 0)
3849 op_mode = READ_CSR(TLP_OP_MODE);
3850 /* enable store-and-forward mode if tx_threshold tops out? */
3851 if ((op_mode & TLP_OP_TX_THRESH) < TLP_OP_TX_THRESH)
3853 op_mode += 0x4000; /* increment TX_THRESH field; can't overflow */
3854 WRITE_CSR(TLP_OP_MODE, op_mode & ~TLP_OP_TX_RUN);
3855 /* Wait for the TX FSM to stop; it might be processing a pkt. */
3856 while (READ_CSR(TLP_STATUS) & TLP_STAT_TX_FSM); /* XXX HANG */
3857 WRITE_CSR(TLP_OP_MODE, op_mode); /* restart tx */
3859 printf("%s: tx underrun; tx fifo threshold now %d bytes\n",
3860 NAME_UNIT, 128<<((op_mode>>TLP_OP_TR_SHIFT)&3));
3864 /* 3) Errata memo from Digital Equipment Corp warns that 21140A */
3865 /* receivers through rev 2.2 can hang if the fifo overruns. */
3866 /* Recommended fix: stop and start the RX FSM after an overrun. */
3867 missed = READ_CSR(TLP_MISSED);
3868 if ((overruns = ((missed & TLP_MISS_OVERRUN)>>TLP_OVERRUN_SHIFT)) != 0)
3871 printf("%s: rx overrun cntr=%d\n", NAME_UNIT, overruns);
3872 sc->status.cntrs.overruns += overruns;
3873 if ((READ_PCI_CFG(sc, TLP_CFRV) & 0xFF) <= 0x22)
3875 op_mode = READ_CSR(TLP_OP_MODE);
3876 WRITE_CSR(TLP_OP_MODE, op_mode & ~TLP_OP_RX_RUN);
3877 /* Wait for the RX FSM to stop; it might be processing a pkt. */
3878 while (READ_CSR(TLP_STATUS) & TLP_STAT_RX_FSM); /* XXX HANG */
3879 WRITE_CSR(TLP_OP_MODE, op_mode); /* restart rx */
3883 /* 4) When the receiver is enabled and a packet arrives, but no DMA */
3884 /* descriptor is available, the packet is counted as 'missed'. */
3885 /* The receiver should never miss packets; warn if it happens. */
3886 if ((missed = (missed & TLP_MISS_MISSED)) != 0)
3889 printf("%s: rx missed %d pkts\n", NAME_UNIT, missed);
3890 sc->status.cntrs.missed += missed;
3894 static void /* This is where the work gets done. */
3895 core_interrupt(void *arg, int check_status)
3900 /* If any CPU is inside this critical section, then */
3901 /* other CPUs should go away without doing anything. */
3902 if (BOTTOM_TRYLOCK == 0)
3904 sc->status.cntrs.lck_intr++;
3908 /* Clear pending card interrupts. */
3909 WRITE_CSR(TLP_STATUS, READ_CSR(TLP_STATUS));
3911 /* In Linux, pci_alloc_consistent() means DMA descriptors */
3912 /* don't need explicit syncing. */
3915 struct desc_ring *ring = &sc->txring;
3916 DMA_SYNC(sc->txring.map, sc->txring.size_descs,
3917 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3919 DMA_SYNC(sc->rxring.map, sc->rxring.size_descs,
3920 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3924 do /* This is the main loop for interrupt processing. */
3926 activity = txintr_cleanup(sc);
3927 activity += txintr_setup(sc);
3928 activity += rxintr_cleanup(sc);
3929 activity += rxintr_setup(sc);
3934 struct desc_ring *ring = &sc->txring;
3935 DMA_SYNC(sc->txring.map, sc->txring.size_descs,
3936 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3938 DMA_SYNC(sc->rxring.map, sc->rxring.size_descs,
3939 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3943 /* As the interrupt is dismissed, check for four unusual events. */
3944 if (check_status) check_intr_status(sc);
3949 /* user_interrupt() may be called from a syscall or a softirq */
3951 user_interrupt(softc_t *sc, int check_status)
3953 DISABLE_INTR; /* noop on FreeBSD-5 and Linux */
3954 core_interrupt(sc, check_status);
3955 ENABLE_INTR; /* noop on FreeBSD-5 and Linux */
3960 # if (defined(__FreeBSD__) && defined(DEVICE_POLLING))
3962 /* Service the card from the kernel idle loop without interrupts. */
3964 fbsd_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3966 softc_t *sc = IFP2SC(ifp);
3968 #if (__FreeBSD_version < 700000)
3969 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
3971 ether_poll_deregister(ifp);
3972 cmd = POLL_DEREGISTER;
3975 if (cmd == POLL_DEREGISTER)
3977 /* Last call -- reenable card interrupts. */
3978 WRITE_CSR(TLP_INT_ENBL, TLP_INT_TXRX);
3984 core_interrupt(sc, (cmd==POLL_AND_CHECK_STATUS));
3988 # endif /* (__FreeBSD__ && DEVICE_POLLING) */
3990 /* BSD kernels call this procedure when an interrupt happens. */
3991 static intr_return_t
3992 bsd_interrupt(void *arg)
3996 /* Cut losses early if this is not our interrupt. */
3997 if ((READ_CSR(TLP_STATUS) & TLP_INT_TXRX) == 0)
4000 # if (defined(__FreeBSD__) && defined(DEVICE_POLLING))
4001 if (sc->ifp->if_capenable & IFCAP_POLLING)
4004 if ((sc->ifp->if_capabilities & IFCAP_POLLING) &&
4005 (ether_poll_register(fbsd_poll, sc->ifp)))
4007 WRITE_CSR(TLP_INT_ENBL, TLP_INT_DISABLE);
4011 sc->quota = sc->rxring.num_descs; /* input flow control */
4012 # endif /* (__FreeBSD__ && DEVICE_POLLING) */
4014 /* Disable card interrupts. */
4015 WRITE_CSR(TLP_INT_ENBL, TLP_INT_DISABLE);
4017 core_interrupt(sc, 0);
4019 /* Enable card interrupts. */
4020 WRITE_CSR(TLP_INT_ENBL, TLP_INT_TXRX);
4027 /* Administrative status of the driver (UP or DOWN) has changed. */
4028 /* A card-specific action may be required: T1 and T3 cards: no-op. */
4029 /* HSSI and SSI cards change the state of modem ready signals. */
4031 set_status(softc_t *sc, int status)
4035 ioctl.cmd = IOCTL_SET_STATUS;
4036 ioctl.data = status;
4038 sc->card->ioctl(sc, &ioctl);
4043 /* Callout from P2P: */
4044 /* Get the state of DCD (Data Carrier Detect). */
4046 p2p_getmdm(struct p2pcom *p2p, caddr_t result)
4048 softc_t *sc = IFP2SC(&p2p->p2p_if);
4050 /* Non-zero isn't good enough; TIOCM_CAR is 0x40. */
4051 *(int *)result = (sc->status.oper_status==STATUS_UP) ? TIOCM_CAR : 0;
4056 /* Callout from P2P: */
4057 /* Set the state of DTR (Data Terminal Ready). */
4059 p2p_mdmctl(struct p2pcom *p2p, int flag)
4061 softc_t *sc = IFP2SC(&p2p->p2p_if);
4063 set_status(sc, flag);
4076 /* Callout from SPPP: */
4078 sppp_tls(struct sppp *sppp)
4081 if (!(sppp->pp_mode & IFF_LINK2) &&
4082 !(sppp->pp_flags & PP_FR))
4083 # elif defined(__NetBSD__) || defined(__OpenBSD__)
4084 if (!(sppp->pp_flags & PP_CISCO))
4089 /* Callout from SPPP: */
4091 sppp_tlf(struct sppp *sppp)
4094 if (!(sppp->pp_mode & IFF_LINK2) &&
4095 !(sppp->pp_flags & PP_FR))
4096 # elif defined(__NetBSD__) || defined(__OpenBSD__)
4097 if (!(sppp->pp_flags & PP_CISCO))
4099 sppp->pp_down(sppp);
4104 /* Configure line protocol stuff.
4105 * Called by attach_card() during module init.
4106 * Called by core_ioctl() when lmcconfig writes sc->config.
4107 * Called by detach_card() during module shutdown.
4110 config_proto(softc_t *sc, struct config *config)
4112 /* Use line protocol stack instead of RAWIP mode. */
4113 if ((sc->config.line_pkg == PKG_RAWIP) &&
4114 (config->line_pkg != PKG_RAWIP))
4118 sppp_attach(sc->ifp);
4119 LMC_BPF_ATTACH(DLT_PPP, 4);
4120 sc->sppp->pp_tls = sppp_tls;
4121 sc->sppp->pp_tlf = sppp_tlf;
4122 /* Force reconfiguration of SPPP params. */
4123 sc->config.line_prot = 0;
4124 sc->config.keep_alive = config->keep_alive ? 0:1;
4127 sc->p2p->p2p_proto = 0; /* force p2p_attach */
4128 if ((error = p2p_attach(sc->p2p))) /* calls bpfattach() */
4130 printf("%s: p2p_attach() failed; error %d\n", NAME_UNIT, error);
4131 config->line_pkg = PKG_RAWIP; /* still in RAWIP mode */
4135 sc->p2p->p2p_mdmctl = p2p_mdmctl; /* set DTR */
4136 sc->p2p->p2p_getmdm = p2p_getmdm; /* get DCD */
4140 sc->net_dev->mtu = HDLC_MAX_MTU;
4141 if ((error = hdlc_open(sc->net_dev)))
4143 printf("%s: hdlc_open() failed; error %d\n", NAME_UNIT, error);
4144 printf("%s: Try 'sethdlc %s ppp'\n", NAME_UNIT, NAME_UNIT);
4145 config->line_pkg = PKG_RAWIP; /* still in RAWIP mode */
4147 #else /* no line protocol stack was configured */
4148 config->line_pkg = PKG_RAWIP; /* still in RAWIP mode */
4152 /* Bypass line protocol stack and return to RAWIP mode. */
4153 if ((sc->config.line_pkg != PKG_RAWIP) &&
4154 (config->line_pkg == PKG_RAWIP))
4158 sppp_flush(sc->ifp);
4159 sppp_detach(sc->ifp);
4160 setup_ifnet(sc->ifp);
4161 LMC_BPF_ATTACH(DLT_RAW, 0);
4164 if_qflush(&sc->p2p->p2p_isnd);
4165 if ((error = p2p_detach(sc->p2p)))
4167 printf("%s: p2p_detach() failed; error %d\n", NAME_UNIT, error);
4168 printf("%s: Try 'ifconfig %s down -remove'\n", NAME_UNIT, NAME_UNIT);
4169 config->line_pkg = PKG_P2P; /* not in RAWIP mode; still attached to P2P */
4173 setup_ifnet(sc->ifp);
4174 LMC_BPF_ATTACH(DLT_RAW, 0);
4177 hdlc_proto_detach(sc->hdlc_dev);
4178 hdlc_close(sc->net_dev);
4179 setup_netdev(sc->net_dev);
4185 if (config->line_pkg != PKG_RAWIP)
4187 /* Check for change to PPP protocol. */
4188 if ((sc->config.line_prot != PROT_PPP) &&
4189 (config->line_prot == PROT_PPP))
4192 # if (defined(__NetBSD__) || defined(__OpenBSD__))
4193 sc->sppp->pp_flags &= ~PP_CISCO;
4194 # elif defined(__FreeBSD__)
4195 sc->ifp->if_flags &= ~IFF_LINK2;
4196 sc->sppp->pp_flags &= ~PP_FR;
4198 LMC_BPF_ATTACH(DLT_PPP, 4);
4199 sppp_ioctl(sc->ifp, SIOCSIFFLAGS, NULL);
4203 # define DLT_C_HDLC DLT_PPP
4206 /* Check for change to C_HDLC protocol. */
4207 if ((sc->config.line_prot != PROT_C_HDLC) &&
4208 (config->line_prot == PROT_C_HDLC))
4211 # if (defined(__NetBSD__) || defined(__OpenBSD__))
4212 sc->sppp->pp_flags |= PP_CISCO;
4213 # elif defined(__FreeBSD__)
4214 sc->ifp->if_flags |= IFF_LINK2;
4215 sc->sppp->pp_flags &= ~PP_FR;
4217 LMC_BPF_ATTACH(DLT_C_HDLC, 4);
4218 sppp_ioctl(sc->ifp, SIOCSIFFLAGS, NULL);
4221 /* Check for change to Frame Relay protocol. */
4222 if ((sc->config.line_prot != PROT_FRM_RLY) &&
4223 (config->line_prot == PROT_FRM_RLY))
4226 # if (defined(__NetBSD__) || defined(__OpenBSD__))
4227 sc->sppp->pp_flags &= ~PP_CISCO;
4228 # elif defined(__FreeBSD__)
4229 sc->ifp->if_flags &= ~IFF_LINK2;
4230 sc->sppp->pp_flags |= PP_FR;
4232 LMC_BPF_ATTACH(DLT_FRELAY, 4);
4233 sppp_ioctl(sc->ifp, SIOCSIFFLAGS, NULL);
4236 /* Check for disabling keep-alives. */
4237 if ((sc->config.keep_alive != 0) &&
4238 (config->keep_alive == 0))
4239 sc->sppp->pp_flags &= ~PP_KEEPALIVE;
4241 /* Check for enabling keep-alives. */
4242 if ((sc->config.keep_alive == 0) &&
4243 (config->keep_alive != 0))
4244 sc->sppp->pp_flags |= PP_KEEPALIVE;
4249 /* Loop back through the TULIP Ethernet chip; (no CRC). */
4250 /* Data sheet says stop DMA before changing OPMODE register. */
4251 /* But that's not as simple as it sounds; works anyway. */
4252 /* Check for enabling loopback thru Tulip chip. */
4253 if ((sc->config.loop_back != CFG_LOOP_TULIP) &&
4254 (config->loop_back == CFG_LOOP_TULIP))
4256 u_int32_t op_mode = READ_CSR(TLP_OP_MODE);
4257 op_mode |= TLP_OP_INT_LOOP;
4258 WRITE_CSR(TLP_OP_MODE, op_mode);
4259 config->crc_len = CFG_CRC_0;
4262 /* Check for disabling loopback thru Tulip chip. */
4263 if ((sc->config.loop_back == CFG_LOOP_TULIP) &&
4264 (config->loop_back != CFG_LOOP_TULIP))
4266 u_int32_t op_mode = READ_CSR(TLP_OP_MODE);
4267 op_mode &= ~TLP_OP_LOOP_MODE;
4268 WRITE_CSR(TLP_OP_MODE, op_mode);
4269 config->crc_len = CFG_CRC_16;
4273 /* This is the core ioctl procedure. */
4274 /* It handles IOCTLs from lmcconfig(8). */
4275 /* It must not run when card watchdogs run. */
4276 /* Called from a syscall (user context; no spinlocks). */
4277 /* This procedure can SLEEP. */
4279 core_ioctl(softc_t *sc, u_long cmd, caddr_t data)
4281 struct iohdr *iohdr = (struct iohdr *) data;
4282 struct ioctl *ioctl = (struct ioctl *) data;
4283 struct status *status = (struct status *) data;
4284 struct config *config = (struct config *) data;
4287 /* All structs start with a string and a cookie. */
4288 if (((struct iohdr *)data)->cookie != NGM_LMC_COOKIE)
4291 while (TOP_TRYLOCK == 0)
4293 sc->status.cntrs.lck_ioctl++;
4294 SLEEP(10000); /* yield? */
4300 *status = sc->status;
4301 iohdr->cookie = NGM_LMC_COOKIE;
4306 *config = sc->config;
4307 iohdr->cookie = NGM_LMC_COOKIE;
4312 if ((error = CHECK_CAP)) break;
4313 config_proto(sc, config);
4314 sc->config = *config;
4315 sc->card->config(sc);
4320 if (ioctl->cmd == IOCTL_RW_PCI)
4322 if (ioctl->address > 252) { error = EFAULT; break; }
4323 ioctl->data = READ_PCI_CFG(sc, ioctl->address);
4325 else if (ioctl->cmd == IOCTL_RW_CSR)
4327 if (ioctl->address > 15) { error = EFAULT; break; }
4328 ioctl->data = READ_CSR(ioctl->address*TLP_CSR_STRIDE);
4330 else if (ioctl->cmd == IOCTL_RW_SROM)
4332 if (ioctl->address > 63) { error = EFAULT; break; }
4333 ioctl->data = read_srom(sc, ioctl->address);
4335 else if (ioctl->cmd == IOCTL_RW_BIOS)
4336 ioctl->data = read_bios(sc, ioctl->address);
4337 else if (ioctl->cmd == IOCTL_RW_MII)
4338 ioctl->data = read_mii(sc, ioctl->address);
4339 else if (ioctl->cmd == IOCTL_RW_FRAME)
4340 ioctl->data = read_framer(sc, ioctl->address);
4347 if ((error = CHECK_CAP)) break;
4348 if (ioctl->cmd == IOCTL_RW_PCI)
4350 if (ioctl->address > 252) { error = EFAULT; break; }
4351 WRITE_PCI_CFG(sc, ioctl->address, ioctl->data);
4353 else if (ioctl->cmd == IOCTL_RW_CSR)
4355 if (ioctl->address > 15) { error = EFAULT; break; }
4356 WRITE_CSR(ioctl->address*TLP_CSR_STRIDE, ioctl->data);
4358 else if (ioctl->cmd == IOCTL_RW_SROM)
4360 if (ioctl->address > 63) { error = EFAULT; break; }
4361 write_srom(sc, ioctl->address, ioctl->data); /* can sleep */
4363 else if (ioctl->cmd == IOCTL_RW_BIOS)
4365 if (ioctl->address == 0) erase_bios(sc);
4366 write_bios(sc, ioctl->address, ioctl->data); /* can sleep */
4368 else if (ioctl->cmd == IOCTL_RW_MII)
4369 write_mii(sc, ioctl->address, ioctl->data);
4370 else if (ioctl->cmd == IOCTL_RW_FRAME)
4371 write_framer(sc, ioctl->address, ioctl->data);
4372 else if (ioctl->cmd == IOCTL_WO_SYNTH)
4373 write_synth(sc, (struct synth *)&ioctl->data);
4374 else if (ioctl->cmd == IOCTL_WO_DAC)
4376 write_dac(sc, 0x9002); /* set Vref = 2.048 volts */
4377 write_dac(sc, ioctl->data & 0xFFF);
4385 if ((error = CHECK_CAP)) break;
4386 if (ioctl->cmd == IOCTL_XILINX_RESET)
4389 sc->card->config(sc);
4391 else if (ioctl->cmd == IOCTL_XILINX_ROM)
4393 load_xilinx_from_rom(sc); /* can sleep */
4394 sc->card->config(sc);
4396 else if (ioctl->cmd == IOCTL_XILINX_FILE)
4398 /* load_xilinx_from_file() can sleep. */
4399 error = load_xilinx_from_file(sc, ioctl->ucode, ioctl->data);
4400 if (error != 0) load_xilinx_from_rom(sc); /* try the rom */
4401 sc->card->config(sc);
4402 set_status(sc, (error==0)); /* XXX */
4404 else if (ioctl->cmd == IOCTL_RESET_CNTRS)
4406 memset(&sc->status.cntrs, 0, sizeof(struct event_cntrs));
4407 microtime(&sc->status.cntrs.reset_time);
4410 error = sc->card->ioctl(sc, ioctl); /* can sleep */
4422 /* This is the core watchdog procedure. */
4423 /* It calculates link speed, and calls the card-specific watchdog code. */
4424 /* Calls interrupt() in case one got lost; also kick-starts the device. */
4425 /* ioctl syscalls and card watchdog routines must be interlocked. */
4426 /* This procedure must not sleep. */
4428 core_watchdog(softc_t *sc)
4430 /* Read and restart the Tulip timer. */
4431 u_int32_t tx_speed = READ_CSR(TLP_TIMER);
4432 WRITE_CSR(TLP_TIMER, 0xFFFF);
4434 /* Measure MII clock using a timer in the Tulip chip.
4435 * This timer counts transmitter bits divided by 4096.
4436 * Since this is called once a second the math is easy.
4437 * This is only correct when the link is NOT sending pkts.
4438 * On a fully-loaded link, answer will be HALF actual rate.
4439 * Clock rate during pkt is HALF clk rate between pkts.
4440 * Measuring clock rate really measures link utilization!
4442 sc->status.tx_speed = (0xFFFF - (tx_speed & 0xFFFF)) << 12;
4444 /* The first status reset time is when the calendar clock is set. */
4445 if (sc->status.cntrs.reset_time.tv_sec < 1000)
4446 microtime(&sc->status.cntrs.reset_time);
4448 /* Update hardware (operational) status. */
4449 /* Call the card-specific watchdog routines. */
4450 if (TOP_TRYLOCK != 0)
4452 sc->status.oper_status = sc->card->watchdog(sc);
4454 /* Increment a counter which tells user-land */
4455 /* observers that SNMP state has been updated. */
4461 sc->status.cntrs.lck_watch++;
4463 /* In case an interrupt gets lost... */
4464 user_interrupt(sc, 1);
4469 /* Called from a syscall (user context; no spinlocks). */
4471 lmc_raw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
4473 struct ifreq *ifr = (struct ifreq *) data;
4478 # if (defined(__FreeBSD__) && defined(DEVICE_POLLING)) /* XXX necessary? */
4481 case SIOCSIFDSTADDR:
4490 ifp->if_flags |= IFF_UP; /* a Unix tradition */
4493 ifp->if_mtu = ifr->ifr_mtu;
4502 /* Called from a syscall (user context; no spinlocks). */
4504 lmc_ifnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
4506 softc_t *sc = IFP2SC(ifp);
4508 struct ifreq *ifr = (struct ifreq *) data;
4514 /* Catch the IOCTLs used by lmcconfig. */
4521 error = core_ioctl(sc, cmd, data);
4524 /* Catch the IOCTLs used by ifconfig. */
4526 if ((error = CHECK_CAP)) break;
4528 error = ifmedia_ioctl(ifp, ifr, &sc->ifm, cmd);
4530 case SIOCSIFTIMESLOT:
4531 if ((error = CHECK_CAP)) break;
4532 if (sc->status.card_type == TLP_CSID_T1E1)
4534 struct config config = sc->config;
4535 if ((error = copyin(ifr->ifr_data, &config.time_slots,
4536 sizeof config.time_slots))) break;
4537 config.iohdr.cookie = NGM_LMC_COOKIE;
4538 error = core_ioctl(sc, LMCIOCSCFG, (caddr_t)&config);
4543 case SIOCGIFTIMESLOT:
4544 if (sc->status.card_type == TLP_CSID_T1E1)
4545 error = copyout(&sc->config.time_slots, ifr->ifr_data,
4546 sizeof sc->config.time_slots);
4551 /* Pass the rest to the line protocol. */
4553 if (sc->config.line_pkg == PKG_RAWIP)
4554 error = lmc_raw_ioctl(ifp, cmd, data);
4557 error = sppp_ioctl(ifp, cmd, data);
4559 error = p2p_ioctl(ifp, cmd, data);
4566 if (DRIVER_DEBUG && (error!=0))
4567 printf("%s: lmc_ifnet_ioctl; cmd=0x%08lx error=%d\n",
4568 NAME_UNIT, cmd, error);
4573 /* Called from a syscall (user context; no spinlocks). */
4575 lmc_ifnet_start(struct ifnet *ifp)
4577 softc_t *sc = IFP2SC(ifp);
4579 /* Start the transmitter; incoming pkts are NOT processed. */
4580 user_interrupt(sc, 0);
4583 /* sppp and p2p replace this with their own proc. */
4584 /* RAWIP mode is the only time this is used. */
4585 /* Called from a syscall (user context; no spinlocks). */
4587 lmc_raw_output(struct ifnet *ifp, struct mbuf *m,
4588 struct sockaddr *dst, struct route *ro)
4590 softc_t *sc = IFP2SC(ifp);
4593 /* Fail if the link is down. */
4594 if (sc->status.oper_status != STATUS_UP)
4597 sc->status.cntrs.odiscards++;
4599 printf("%s: lmc_raw_output: tx pkt discarded: link down\n", NAME_UNIT);
4604 /* Netgraph has priority over the ifnet kernel interface. */
4605 if (sc->ng_hook != NULL)
4608 sc->status.cntrs.odiscards++;
4610 printf("%s: lmc_raw_output: tx pkt discarded: netgraph active\n",
4616 /* lmc_raw_output() ENQUEUEs in a syscall or softirq. */
4617 /* txintr_setup() DEQUEUEs in a hard interrupt. */
4618 /* Some BSD QUEUE routines are not interrupt-safe. */
4621 # if (__FreeBSD_version >= 503000)
4622 IFQ_ENQUEUE(&ifp->if_snd, m, error);
4624 IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
4630 user_interrupt(sc, 0); /* start the transmitter */
4634 sc->status.cntrs.odiscards++;
4636 printf("%s: lmc_raw_output: IFQ_ENQUEUE() failed; error %d\n",
4643 /* Called from a softirq once a second. */
4645 lmc_ifnet_watchdog(struct ifnet *ifp)
4647 softc_t *sc = IFP2SC(ifp);
4648 u_int8_t old_oper_status = sc->status.oper_status;
4649 struct event_cntrs *cntrs = &sc->status.cntrs;
4651 core_watchdog(sc); /* updates oper_status */
4654 if (sc->ng_hook != NULL)
4656 sc->status.line_pkg = PKG_NG;
4657 sc->status.line_prot = 0;
4661 if (sc->config.line_pkg == PKG_RAWIP)
4663 sc->status.line_pkg = PKG_RAWIP;
4664 sc->status.line_prot = PROT_IP_HDLC;
4669 /* Notice change in link status. */
4670 if ((old_oper_status != sc->status.oper_status) && (sc->p2p->p2p_modem))
4671 (*sc->p2p->p2p_modem)(sc->p2p, sc->status.oper_status==STATUS_UP);
4673 /* Notice change in line protocol. */
4674 sc->status.line_pkg = PKG_P2P;
4675 switch (sc->ifp->if_type)
4678 sc->status.line_prot = PROT_PPP;
4681 sc->status.line_prot = PROT_C_HDLC;
4684 sc->status.line_prot = PROT_FRM_RLY;
4687 sc->status.line_prot = 0;
4692 /* Notice change in link status. */
4693 if ((old_oper_status != STATUS_UP) &&
4694 (sc->status.oper_status == STATUS_UP)) /* link came up */
4696 if ((old_oper_status == STATUS_UP) &&
4697 (sc->status.oper_status != STATUS_UP)) /* link went down */
4700 /* Notice change in line protocol. */
4701 sc->status.line_pkg = PKG_SPPP;
4703 if (sc->sppp->pp_flags & PP_FR)
4704 sc->status.line_prot = PROT_FRM_RLY;
4705 else if (sc->ifp->if_flags & IFF_LINK2)
4706 # elif (defined(__NetBSD__) || defined(__OpenBSD__))
4707 if (sc->sppp->pp_flags & PP_CISCO)
4709 sc->status.line_prot = PROT_C_HDLC;
4711 sc->status.line_prot = PROT_PPP;
4714 /* Suppress compiler warning. */
4715 if (old_oper_status == STATUS_UP);
4719 /* Copy statistics from sc to ifp. */
4720 ifp->if_baudrate = sc->status.tx_speed;
4721 ifp->if_ipackets = cntrs->ipackets;
4722 ifp->if_opackets = cntrs->opackets;
4723 ifp->if_ibytes = cntrs->ibytes;
4724 ifp->if_obytes = cntrs->obytes;
4725 ifp->if_ierrors = cntrs->ierrors;
4726 ifp->if_oerrors = cntrs->oerrors;
4727 ifp->if_iqdrops = cntrs->idiscards;
4729 # if ((__FreeBSD_version >= 500000) || defined(__OpenBSD__) || defined(__NetBSD__))
4730 if (sc->status.oper_status == STATUS_UP)
4731 ifp->if_link_state = LINK_STATE_UP;
4733 ifp->if_link_state = LINK_STATE_DOWN;
4736 /* Call this procedure again after one second. */
4742 /* Callback from ifmedia. */
4744 ifmedia_change(struct ifnet *ifp)
4746 softc_t *sc = IFP2SC(ifp);
4747 struct config config = sc->config;
4748 int media = sc->ifm.ifm_media;
4751 /* ifconfig lmc0 media t1 */
4752 if (sc->status.card_type == TLP_CSID_T3)
4754 if ((media & IFM_TMASK) == IFM_TDM_T3)
4755 config.format = CFG_FORMAT_T3CPAR;
4756 else if ((media & IFM_TMASK) == IFM_TDM_T3_M13)
4757 config.format = CFG_FORMAT_T3M13;
4759 else if (sc->status.card_type == TLP_CSID_T1E1)
4761 if ((media & IFM_TMASK) == IFM_TDM_T1)
4762 config.format = CFG_FORMAT_T1ESF;
4763 else if ((media & IFM_TMASK) == IFM_TDM_T1_AMI)
4764 config.format = CFG_FORMAT_T1SF;
4765 else if ((media & IFM_TMASK) == IFM_TDM_E1)
4766 config.format = CFG_FORMAT_E1NONE;
4767 else if ((media & IFM_TMASK) == IFM_TDM_E1_G704)
4768 config.format = CFG_FORMAT_E1FASCRC;
4771 /* ifconfig lmc0 mediaopt loopback */
4772 if (media & IFM_LOOP)
4773 config.loop_back = CFG_LOOP_TULIP;
4775 config.loop_back = CFG_LOOP_NONE;
4777 /* ifconfig lmc0 mediaopt crc16 */
4778 if (media & IFM_TDM_HDLC_CRC16)
4779 config.crc_len = CFG_CRC_16;
4781 config.crc_len = CFG_CRC_32;
4783 /* Set ConFiGuration. */
4784 config.iohdr.cookie = NGM_LMC_COOKIE;
4785 error = core_ioctl(sc, LMCIOCSCFG, (caddr_t)&config);
4790 /* Callback from ifmedia. */
4792 ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
4794 softc_t *sc = IFP2SC(ifp);
4796 /* ifconfig wants to know if the hardware link is up. */
4797 ifmr->ifm_status = IFM_AVALID;
4798 if (sc->status.oper_status == STATUS_UP)
4799 ifmr->ifm_status |= IFM_ACTIVE;
4801 ifmr->ifm_active = sc->ifm.ifm_cur->ifm_media;
4803 if (sc->config.loop_back != CFG_LOOP_NONE)
4804 ifmr->ifm_active |= IFM_LOOP;
4806 if (sc->config.crc_len == CFG_CRC_16)
4807 ifmr->ifm_active |= IFM_TDM_HDLC_CRC16;
4810 # endif /* __OpenBSD__ */
4813 setup_ifnet(struct ifnet *ifp)
4815 softc_t *sc = ifp->if_softc;
4817 /* Initialize the generic network interface. */
4818 /* Note similarity to linux's setup_netdev(). */
4819 ifp->if_flags = IFF_POINTOPOINT;
4820 ifp->if_flags |= IFF_RUNNING;
4821 ifp->if_ioctl = lmc_ifnet_ioctl;
4822 ifp->if_start = lmc_ifnet_start; /* sppp changes this */
4823 ifp->if_output = lmc_raw_output; /* sppp & p2p change this */
4824 ifp->if_input = lmc_raw_input;
4825 ifp->if_watchdog = lmc_ifnet_watchdog;
4827 ifp->if_mtu = MAX_DESC_LEN; /* sppp & p2p change this */
4828 ifp->if_type = IFT_PTPSERIAL; /* p2p changes this */
4830 # if (defined(__FreeBSD__) && defined(DEVICE_POLLING))
4831 ifp->if_capabilities |= IFCAP_POLLING;
4832 ifp->if_capenable |= IFCAP_POLLING_NOCOUNT;
4833 # if (__FreeBSD_version < 500000)
4834 ifp->if_capenable |= IFCAP_POLLING;
4838 /* Every OS does it differently! */
4839 # if (defined(__FreeBSD__) && (__FreeBSD_version < 502000))
4840 (const char *)ifp->if_name = device_get_name(sc->dev);
4841 ifp->if_unit = device_get_unit(sc->dev);
4842 # elif (__FreeBSD_version >= 502000)
4843 if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
4844 # elif defined(__NetBSD__)
4845 strcpy(ifp->if_xname, sc->dev.dv_xname);
4847 bcopy(sc->dev.dv_xname, ifp->if_xname, IFNAMSIZ);
4848 # elif defined(__bsdi__)
4849 ifp->if_name = sc->dev.dv_cfdata->cf_driver->cd_name;
4850 ifp->if_unit = sc->dev.dv_unit;
4855 lmc_ifnet_attach(softc_t *sc)
4857 # if (__FreeBSD_version >= 600000)
4858 sc->ifp = if_alloc(NSPPP ? IFT_PPP : IFT_OTHER);
4859 if (sc->ifp == NULL) return ENOMEM;
4862 # if (__FreeBSD_version >= 600000)
4863 sc->sppp = sc->ifp->if_l2com;
4865 sc->ifp = &sc->spppcom.pp_if;
4866 sc->sppp = &sc->spppcom;
4869 sc->ifp = &sc->p2pcom.p2p_if;
4870 sc->p2p = &sc->p2pcom;
4871 # elif (__FreeBSD_version < 600000)
4872 sc->ifp = &sc->ifnet;
4875 /* Initialize the network interface struct. */
4876 sc->ifp->if_softc = sc;
4877 setup_ifnet(sc->ifp);
4879 /* ALTQ output queue initialization. */
4880 IFQ_SET_MAXLEN(&sc->ifp->if_snd, SNDQ_MAXLEN);
4881 IFQ_SET_READY(&sc->ifp->if_snd);
4883 /* Attach to the ifnet kernel interface. */
4886 # if ((defined(__NetBSD__) && __NetBSD_Version__ >= 106000000) || \
4887 (defined(__OpenBSD__) && OpenBSD >= 200211))
4888 if_alloc_sadl(sc->ifp);
4891 /* Attach Berkeley Packet Filter. */
4892 LMC_BPF_ATTACH(DLT_RAW, 0);
4895 /* Initialize ifmedia mechanism. */
4896 ifmedia_init(&sc->ifm, IFM_OMASK | IFM_GMASK | IFM_IMASK,
4897 ifmedia_change, ifmedia_status);
4898 if (sc->status.card_type == TLP_CSID_T3)
4900 ifmedia_add(&sc->ifm, IFM_TDM | IFM_TDM_T3, 0, NULL);
4901 ifmedia_add(&sc->ifm, IFM_TDM | IFM_TDM_T3_M13, 0, NULL);
4902 ifmedia_set(&sc->ifm, IFM_TDM | IFM_TDM_T3);
4904 else if (sc->status.card_type == TLP_CSID_T1E1)
4906 ifmedia_add(&sc->ifm, IFM_TDM | IFM_TDM_T1, 0, NULL);
4907 ifmedia_add(&sc->ifm, IFM_TDM | IFM_TDM_T1_AMI, 0, NULL);
4908 ifmedia_add(&sc->ifm, IFM_TDM | IFM_TDM_E1, 0, NULL);
4909 ifmedia_add(&sc->ifm, IFM_TDM | IFM_TDM_E1_G704, 0, NULL);
4910 ifmedia_set(&sc->ifm, IFM_TDM | IFM_TDM_T1);
4912 else if ((sc->status.card_type == TLP_CSID_HSSI) ||
4913 (sc->status.card_type == TLP_CSID_SSI))
4915 ifmedia_add(&sc->ifm, IFM_TDM | IFM_NONE, 0, NULL);
4916 ifmedia_set(&sc->ifm, IFM_TDM | IFM_NONE);
4918 # endif /* __OpenBSD__ */
4924 lmc_ifnet_detach(softc_t *sc)
4927 ifmedia_delete_instance(&sc->ifm, IFM_INST_ANY);
4930 # if (defined(__FreeBSD__) && defined(DEVICE_POLLING))
4931 if (sc->ifp->if_capenable & IFCAP_POLLING)
4932 ether_poll_deregister(sc->ifp);
4935 /* Detach Berkeley Packet Filter. */
4938 # if ((defined(__NetBSD__) && __NetBSD_Version__ >= 106000000) || \
4939 (defined(__OpenBSD__) && OpenBSD >= 200211))
4940 if_free_sadl(sc->ifp);
4943 /* Detach from the ifnet kernel interface. */
4946 # if (__FreeBSD_version >= 600000)
4947 if_free_type(sc->ifp, NSPPP ? IFT_PPP : IFT_OTHER);
4955 /* Netgraph changed significantly between FreeBSD-4 and -5. */
4956 /* These are backward compatibility hacks for FreeBSD-4. */
4957 # if (__FreeBSD_version >= 500000)
4958 /* These next two macros should be added to netgraph */
4959 # define NG_TYPE_REF(type) atomic_add_int(&(type)->refs, 1)
4960 # define NG_TYPE_UNREF(type) \
4962 if ((type)->refs == 1) \
4965 atomic_subtract_int(&(type)->refs, 1); \
4967 # else /* FreeBSD-4 */
4968 # define NGI_GET_MSG(item, msg) /* nothing */
4969 # define NG_HOOK_FORCE_QUEUE(hook) /* nothing */
4970 # define NG_TYPE_REF(type) atomic_add_int(&(type)->refs, 1)
4971 # define NG_TYPE_UNREF(type) \
4973 if ((type)->refs == 1) \
4974 LIST_REMOVE(type, types); \
4976 atomic_subtract_int(&(type)->refs, 1); \
4980 /* It is an error to construct new copies of this Netgraph node. */
4981 /* All instances are constructed by ng_attach and are persistent. */
4982 # if (__FreeBSD_version >= 500000)
4983 static int ng_constructor(node_p node) { return EINVAL; }
4984 # else /* FreeBSD-4 */
4985 static int ng_constructor(node_p *node) { return EINVAL; }
4988 /* Incoming Netgraph control message. */
4989 # if (__FreeBSD_version >= 500000)
4991 ng_rcvmsg(node_p node, item_p item, hook_p lasthook)
4993 struct ng_mesg *msg;
4994 # else /* FreeBSD-4 */
4996 ng_rcvmsg(node_p node, struct ng_mesg *msg,
4997 const char *retaddr, struct ng_mesg **rptr)
5000 struct ng_mesg *resp = NULL;
5001 softc_t *sc = NG_NODE_PRIVATE(node);
5004 NGI_GET_MSG(item, msg);
5005 if (msg->header.typecookie == NGM_LMC_COOKIE)
5007 switch (msg->header.cmd)
5016 /* Call the core ioctl procedure. */
5017 error = core_ioctl(sc, msg->header.cmd, msg->data);
5018 if ((msg->header.cmd & IOC_OUT) != 0)
5019 { /* synchronous response */
5020 NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg) +
5021 IOCPARM_LEN(msg->header.cmd), M_NOWAIT);
5025 memcpy(resp->data, msg->data, IOCPARM_LEN(msg->header.cmd));
5034 else if ((msg->header.typecookie == NGM_GENERIC_COOKIE) &&
5035 (msg->header.cmd == NGM_TEXT_STATUS))
5036 { /* synchronous response */
5037 NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg) +
5038 NG_TEXTRESPONSE, M_NOWAIT);
5043 char *s = resp->data;
5044 sprintf(s, "Card type = <%s>\n"
5045 "This driver considers the link to be %s.\n"
5046 "Use lmcconfig to configure this interface.\n",
5047 sc->dev_desc, (sc->status.oper_status==STATUS_UP) ? "UP" : "DOWN");
5048 resp->header.arglen = strlen(s) +1;
5052 /* Netgraph should be able to read and write these
5053 * parameters with text-format control messages:
5056 * loop loop loop loop
5058 * dte dte format format
5059 * synth synth cablen cablen
5060 * cable timeslot scram
5064 * Someday I'll implement this...
5068 /* Handle synchronous response. */
5069 # if (__FreeBSD_version >= 500000)
5070 NG_RESPOND_MSG(error, node, item, resp);
5072 # else /* FreeBSD-4 */
5075 else if (resp != NULL)
5076 free(resp, M_NETGRAPH);
5077 free(msg, M_NETGRAPH);
5083 /* This is a persistent netgraph node. */
5085 ng_shutdown(node_p node)
5087 # if (__FreeBSD_version >= 500000)
5088 /* unless told to really die, bounce back to life */
5089 if ((node->nd_flags & NG_REALLY_DIE)==0)
5090 node->nd_flags &= ~NG_INVALID; /* bounce back to life */
5091 # else /* FreeBSD-4 */
5093 node->flags &= ~NG_INVALID; /* bounce back to life */
5099 /* ng_disconnect is the opposite of this procedure. */
5101 ng_newhook(node_p node, hook_p hook, const char *name)
5103 softc_t *sc = NG_NODE_PRIVATE(node);
5105 /* Hook name must be 'rawdata'. */
5106 if (strncmp(name, "rawdata", 7) != 0) return EINVAL;
5108 /* Is our hook connected? */
5109 if (sc->ng_hook != NULL) return EBUSY;
5111 /* Accept the hook. */
5117 /* Both ends have accepted their hooks and the links have been made. */
5118 /* This is the last chance to reject the connection request. */
5120 ng_connect(hook_p hook)
5122 /* Probably not at splnet, force outward queueing. (huh?) */
5123 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
5124 return 0; /* always accept */
5127 /* Receive data in mbufs from another Netgraph node. */
5128 /* Transmit an mbuf-chain on the communication link. */
5129 /* This procedure is very similar to lmc_raw_output(). */
5130 /* Called from a syscall (user context; no spinlocks). */
5131 # if (__FreeBSD_version >= 500000)
5133 ng_rcvdata(hook_p hook, item_p item)
5135 softc_t *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
5141 NGI_GET_META(item, meta);
5143 # else /* FreeBSD-4 */
5145 ng_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
5147 softc_t *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
5151 /* This macro must not store into meta! */
5154 /* Fail if the link is down. */
5155 if (sc->status.oper_status != STATUS_UP)
5158 sc->status.cntrs.odiscards++;
5160 printf("%s: ng_rcvdata: tx pkt discarded: link down\n", NAME_UNIT);
5164 /* ng_rcvdata() ENQUEUEs in a syscall or softirq. */
5165 /* txintr_setup() DEQUEUEs in a hard interrupt. */
5166 /* Some BSD QUEUE routines are not interrupt-safe. */
5169 # if (__FreeBSD_version >= 503000)
5171 IFQ_ENQUEUE(&sc->ng_sndq, m, error);
5173 IFQ_ENQUEUE(&sc->ng_fastq, m, error);
5176 IFQ_ENQUEUE(&sc->ng_sndq, m, NULL, error);
5178 IFQ_ENQUEUE(&sc->ng_fastq, m, NULL, error);
5184 user_interrupt(sc, 0); /* start the transmitter */
5188 sc->status.cntrs.odiscards++;
5190 printf("%s: ng_rcvdata: IFQ_ENQUEUE() failed; error %d\n",
5197 /* ng_newhook is the opposite of this procedure, not */
5198 /* ng_connect, as you might expect from the names. */
5200 ng_disconnect(hook_p hook)
5202 softc_t *sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
5204 /* Disconnect the hook. */
5211 struct ng_type ng_type =
5213 .version = NG_ABI_VERSION,
5214 .name = NG_LMC_NODE_TYPE,
5216 .constructor = ng_constructor,
5217 .rcvmsg = ng_rcvmsg,
5218 # if (__FreeBSD_version >=503000)
5221 .shutdown = ng_shutdown,
5222 .newhook = ng_newhook,
5224 .connect = ng_connect,
5225 .rcvdata = ng_rcvdata,
5226 # if (defined(__FreeBSD__) && (__FreeBSD_version < 500000))
5227 .rcvdataq = ng_rcvdata,
5229 .disconnect = ng_disconnect,
5233 /* Called from a softirq once a second. */
5235 ng_watchdog(void *arg)
5239 /* Call the core watchdog procedure. */
5242 /* Set line protocol and package status. */
5243 sc->status.line_pkg = PKG_NG;
5244 sc->status.line_prot = 0;
5246 /* Call this procedure again after one second. */
5247 callout_reset(&sc->ng_callout, hz, ng_watchdog, sc);
5251 /* Attach to the Netgraph kernel interface (/sys/netgraph).
5252 * It is called once for each physical card during device attach.
5253 * This is effectively ng_constructor.
5256 ng_attach(softc_t *sc)
5260 /* If this node type is not known to Netgraph then register it. */
5261 if (ng_type.refs == 0) /* or: if (ng_findtype(&ng_type) == NULL) */
5263 if ((error = ng_newtype(&ng_type)))
5265 printf("%s: ng_newtype() failed; error %d\n", NAME_UNIT, error);
5270 NG_TYPE_REF(&ng_type);
5272 /* Call the superclass node constructor. */
5273 if ((error = ng_make_node_common(&ng_type, &sc->ng_node)))
5275 NG_TYPE_UNREF(&ng_type);
5276 printf("%s: ng_make_node_common() failed; error %d\n", NAME_UNIT, error);
5280 /* Associate a name with this netgraph node. */
5281 if ((error = ng_name_node(sc->ng_node, NAME_UNIT)))
5283 NG_NODE_UNREF(sc->ng_node);
5284 NG_TYPE_UNREF(&ng_type);
5285 printf("%s: ng_name_node() failed; error %d\n", NAME_UNIT, error);
5289 # if (__FreeBSD_version >= 500000)
5290 /* Initialize the send queue mutexes. */
5291 mtx_init(&sc->ng_sndq.ifq_mtx, NAME_UNIT, "sndq", MTX_DEF);
5292 mtx_init(&sc->ng_fastq.ifq_mtx, NAME_UNIT, "fastq", MTX_DEF);
5295 /* Put a backpointer to the softc in the netgraph node. */
5296 NG_NODE_SET_PRIVATE(sc->ng_node, sc);
5298 /* ALTQ output queue initialization. */
5299 IFQ_SET_MAXLEN(&sc->ng_fastq, SNDQ_MAXLEN);
5300 IFQ_SET_READY(&sc->ng_fastq);
5301 IFQ_SET_MAXLEN(&sc->ng_sndq, SNDQ_MAXLEN);
5302 IFQ_SET_READY(&sc->ng_sndq);
5304 /* If ifnet is present, it will call watchdog. */
5305 /* Otherwise, arrange to call watchdog here. */
5307 /* Arrange to call ng_watchdog() once a second. */
5308 # if (__FreeBSD_version >= 500000)
5309 callout_init(&sc->ng_callout, 0);
5310 # else /* FreeBSD-4 */
5311 callout_init(&sc->ng_callout);
5313 callout_reset(&sc->ng_callout, hz, ng_watchdog, sc);
5320 ng_detach(softc_t *sc)
5323 callout_stop(&sc->ng_callout);
5325 # if (__FreeBSD_version >= 500000)
5326 mtx_destroy(&sc->ng_sndq.ifq_mtx);
5327 mtx_destroy(&sc->ng_fastq.ifq_mtx);
5328 ng_rmnode_self(sc->ng_node); /* free hook */
5329 NG_NODE_UNREF(sc->ng_node); /* free node */
5330 NG_TYPE_UNREF(&ng_type);
5331 # else /* FreeBSD-4 */
5332 ng_unname(sc->ng_node); /* free name */
5333 ng_cutlinks(sc->ng_node); /* free hook */
5334 NG_NODE_UNREF(sc->ng_node); /* free node */
5335 NG_TYPE_UNREF(&ng_type);
5339 #endif /* NETGRAPH */
5341 /* The next few procedures initialize the card. */
5343 /* Returns 0 on success; error code on failure. */
5345 startup_card(softc_t *sc)
5347 int num_rx_descs, error = 0;
5348 u_int32_t tlp_bus_pbl, tlp_bus_cal, tlp_op_tr;
5349 u_int32_t tlp_cfdd, tlp_cfcs;
5350 u_int32_t tlp_cflt, tlp_csid, tlp_cfit;
5352 /* Make sure the COMMAND bits are reasonable. */
5353 tlp_cfcs = READ_PCI_CFG(sc, TLP_CFCS);
5354 tlp_cfcs &= ~TLP_CFCS_MWI_ENABLE;
5355 tlp_cfcs |= TLP_CFCS_BUS_MASTER;
5356 tlp_cfcs |= TLP_CFCS_MEM_ENABLE;
5357 tlp_cfcs |= TLP_CFCS_IO_ENABLE;
5358 tlp_cfcs |= TLP_CFCS_PAR_ERROR;
5359 tlp_cfcs |= TLP_CFCS_SYS_ERROR;
5360 WRITE_PCI_CFG(sc, TLP_CFCS, tlp_cfcs);
5362 /* Set the LATENCY TIMER to the recommended value, */
5363 /* and make sure the CACHE LINE SIZE is reasonable. */
5364 tlp_cfit = READ_PCI_CFG(sc, TLP_CFIT);
5365 tlp_cflt = READ_PCI_CFG(sc, TLP_CFLT);
5366 tlp_cflt &= ~TLP_CFLT_LATENCY;
5367 tlp_cflt |= (tlp_cfit & TLP_CFIT_MAX_LAT)>>16;
5368 /* "prgmbl burst length" and "cache alignment" used below. */
5369 switch(tlp_cflt & TLP_CFLT_CACHE)
5371 case 8: /* 8 bytes per cache line */
5372 { tlp_bus_pbl = 32; tlp_bus_cal = 1; break; }
5374 { tlp_bus_pbl = 32; tlp_bus_cal = 2; break; }
5376 { tlp_bus_pbl = 32; tlp_bus_cal = 3; break; }
5379 tlp_bus_pbl = 32; tlp_bus_cal = 1;
5380 tlp_cflt &= ~TLP_CFLT_CACHE;
5385 WRITE_PCI_CFG(sc, TLP_CFLT, tlp_cflt);
5387 /* Make sure SNOOZE and SLEEP modes are disabled. */
5388 tlp_cfdd = READ_PCI_CFG(sc, TLP_CFDD);
5389 tlp_cfdd &= ~TLP_CFDD_SLEEP;
5390 tlp_cfdd &= ~TLP_CFDD_SNOOZE;
5391 WRITE_PCI_CFG(sc, TLP_CFDD, tlp_cfdd);
5392 DELAY(11*1000); /* Tulip wakes up in 10 ms max */
5394 /* Software Reset the Tulip chip; stops DMA and Interrupts. */
5395 /* This does not change the PCI config regs just set above. */
5396 WRITE_CSR(TLP_BUS_MODE, TLP_BUS_RESET); /* self-clearing */
5397 DELAY(5); /* Tulip is dead for 50 PCI cycles after reset. */
5399 /* Reset the Xilinx Field Programmable Gate Array. */
5400 reset_xilinx(sc); /* side effect: turns on all four LEDs */
5402 /* Configure card-specific stuff (framers, line interfaces, etc.). */
5403 sc->card->config(sc);
5405 /* Initializing cards can glitch clocks and upset fifos. */
5406 /* Reset the FIFOs between the Tulip and Xilinx chips. */
5407 set_mii16_bits(sc, MII16_FIFO);
5408 clr_mii16_bits(sc, MII16_FIFO);
5410 /* Initialize the PCI busmode register. */
5411 /* The PCI bus cycle type "Memory Write and Invalidate" does NOT */
5412 /* work cleanly in any version of the 21140A, so don't enable it! */
5413 WRITE_CSR(TLP_BUS_MODE,
5414 (tlp_bus_cal ? TLP_BUS_READ_LINE : 0) |
5415 (tlp_bus_cal ? TLP_BUS_READ_MULT : 0) |
5416 (tlp_bus_pbl<<TLP_BUS_PBL_SHIFT) |
5417 (tlp_bus_cal<<TLP_BUS_CAL_SHIFT) |
5418 ((BYTE_ORDER == BIG_ENDIAN) ? TLP_BUS_DESC_BIGEND : 0) |
5419 ((BYTE_ORDER == BIG_ENDIAN) ? TLP_BUS_DATA_BIGEND : 0) |
5423 /* Pick number of RX descriptors and TX fifo threshold. */
5424 /* tx_threshold in bytes: 0=128, 1=256, 2=512, 3=1024 */
5425 tlp_csid = READ_PCI_CFG(sc, TLP_CSID);
5428 case TLP_CSID_HSSI: /* 52 Mb/s */
5429 case TLP_CSID_HSSIc: /* 52 Mb/s */
5430 case TLP_CSID_T3: /* 45 Mb/s */
5431 { num_rx_descs = 48; tlp_op_tr = 2; break; }
5432 case TLP_CSID_SSI: /* 10 Mb/s */
5433 { num_rx_descs = 32; tlp_op_tr = 1; break; }
5434 case TLP_CSID_T1E1: /* 2 Mb/s */
5435 { num_rx_descs = 16; tlp_op_tr = 0; break; }
5437 { num_rx_descs = 16; tlp_op_tr = 0; break; }
5440 /* Create DMA descriptors and initialize list head registers. */
5441 if ((error = create_ring(sc, &sc->txring, NUM_TX_DESCS))) return error;
5442 WRITE_CSR(TLP_TX_LIST, sc->txring.dma_addr);
5443 if ((error = create_ring(sc, &sc->rxring, num_rx_descs))) return error;
5444 WRITE_CSR(TLP_RX_LIST, sc->rxring.dma_addr);
5446 /* Initialize the operating mode register. */
5447 WRITE_CSR(TLP_OP_MODE, TLP_OP_INIT | (tlp_op_tr<<TLP_OP_TR_SHIFT));
5449 /* Read the missed frame register (result ignored) to zero it. */
5450 error = READ_CSR( TLP_MISSED); /* error is used as a bit-dump */
5452 /* Disable rx watchdog and tx jabber features. */
5453 WRITE_CSR(TLP_WDOG, TLP_WDOG_INIT);
5455 /* Enable card interrupts. */
5456 WRITE_CSR(TLP_INT_ENBL, TLP_INT_TXRX);
5461 /* Stop DMA and Interrupts; free descriptors and buffers. */
5463 shutdown_card(void *arg)
5467 /* Leave the LEDs in the state they were in after power-on. */
5468 led_on(sc, MII16_LED_ALL);
5470 /* Software reset the Tulip chip; stops DMA and Interrupts */
5471 WRITE_CSR(TLP_BUS_MODE, TLP_BUS_RESET); /* self-clearing */
5472 DELAY(5); /* Tulip is dead for 50 PCI cycles after reset. */
5474 /* Disconnect from the PCI bus except for config cycles. */
5475 /* Hmmm; Linux syslogs a warning that IO and MEM are disabled. */
5476 WRITE_PCI_CFG(sc, TLP_CFCS, TLP_CFCS_MEM_ENABLE | TLP_CFCS_IO_ENABLE);
5478 /* Free the DMA descriptor rings. */
5479 destroy_ring(sc, &sc->txring);
5480 destroy_ring(sc, &sc->rxring);
5483 /* Start the card and attach a kernel interface and line protocol. */
5485 attach_card(softc_t *sc, const char *intrstr)
5487 struct config config;
5493 /* Start the card. */
5494 if ((error = startup_card(sc))) return error;
5496 /* Attach a kernel interface. */
5498 if ((error = ng_attach(sc))) return error;
5499 sc->flags |= FLAG_NETGRAPH;
5502 if ((error = lmc_ifnet_attach(sc))) return error;
5503 sc->flags |= FLAG_IFNET;
5506 /* Attach a line protocol stack. */
5507 sc->config.line_pkg = PKG_RAWIP;
5508 config = sc->config; /* get current config */
5509 config.line_pkg = 0; /* select external stack */
5510 config.line_prot = PROT_C_HDLC;
5511 config.keep_alive = 1;
5512 config_proto(sc, &config); /* reconfigure */
5513 sc->config = config; /* save new configuration */
5515 /* Print interesting hardware-related things. */
5516 mii3 = read_mii(sc, 3);
5517 tlp_cfrv = READ_PCI_CFG(sc, TLP_CFRV);
5518 printf("%s: PCI rev %d.%d, MII rev %d.%d", NAME_UNIT,
5519 (tlp_cfrv>>4) & 0xF, tlp_cfrv & 0xF, (mii3>>4) & 0xF, mii3 & 0xF);
5520 ieee = (u_int8_t *)sc->status.ieee;
5521 for (i=0; i<3; i++) sc->status.ieee[i] = read_srom(sc, 10+i);
5522 printf(", IEEE addr %02x:%02x:%02x:%02x:%02x:%02x",
5523 ieee[0], ieee[1], ieee[2], ieee[3], ieee[4], ieee[5]);
5524 sc->card->ident(sc);
5525 printf(" %s\n", intrstr);
5527 /* Print interesting software-related things. */
5528 printf("%s: Driver rev %d.%d.%d", NAME_UNIT,
5529 DRIVER_MAJOR_VERSION, DRIVER_MINOR_VERSION, DRIVER_SUB_VERSION);
5530 printf(", Options %s%s%s%s%s%s%s%s%s\n",
5531 NETGRAPH ? "NETGRAPH " : "", GEN_HDLC ? "GEN_HDLC " : "",
5532 NSPPP ? "SPPP " : "", P2P ? "P2P " : "",
5533 ALTQ_PRESENT ? "ALTQ " : "", NBPFILTER ? "BPF " : "",
5534 DEV_POLL ? "POLL " : "", IOREF_CSR ? "IO_CSR " : "MEM_CSR ",
5535 (BYTE_ORDER == BIG_ENDIAN) ? "BIG_END " : "LITTLE_END ");
5537 /* Make the local hardware ready. */
5543 /* Detach from the kernel in all ways. */
5545 detach_card(softc_t *sc)
5547 struct config config;
5549 /* Make the local hardware NOT ready. */
5552 /* Detach external line protocol stack. */
5553 if (sc->config.line_pkg != PKG_RAWIP)
5555 config = sc->config;
5556 config.line_pkg = PKG_RAWIP;
5557 config_proto(sc, &config);
5558 sc->config = config;
5561 /* Detach kernel interfaces. */
5563 if (sc->flags & FLAG_NETGRAPH)
5565 IFQ_PURGE(&sc->ng_fastq);
5566 IFQ_PURGE(&sc->ng_sndq);
5568 sc->flags &= ~FLAG_NETGRAPH;
5572 if (sc->flags & FLAG_IFNET)
5574 IFQ_PURGE(&sc->ifp->if_snd);
5575 lmc_ifnet_detach(sc);
5576 sc->flags &= ~FLAG_IFNET;
5580 /* Reset the Tulip chip; stops DMA and Interrupts. */
5584 /* This is the I/O configuration interface for FreeBSD */
5589 fbsd_probe(device_t dev)
5591 u_int32_t cfid = pci_read_config(dev, TLP_CFID, 4);
5592 u_int32_t csid = pci_read_config(dev, TLP_CSID, 4);
5594 /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
5595 if (cfid != TLP_CFID_TULIP) return ENXIO;
5599 case TLP_CSID_HSSIc:
5600 device_set_desc(dev, HSSI_DESC);
5603 device_set_desc(dev, T3_DESC);
5606 device_set_desc(dev, SSI_DESC);
5609 device_set_desc(dev, T1E1_DESC);
5618 fbsd_detach(device_t dev)
5620 softc_t *sc = device_get_softc(dev);
5622 /* Stop the card and detach from the kernel. */
5625 /* Release resources. */
5626 if (sc->irq_cookie != NULL)
5628 bus_teardown_intr(dev, sc->irq_res, sc->irq_cookie);
5629 sc->irq_cookie = NULL;
5631 if (sc->irq_res != NULL)
5633 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_res_id, sc->irq_res);
5636 if (sc->csr_res != NULL)
5638 bus_release_resource(dev, sc->csr_res_type, sc->csr_res_id, sc->csr_res);
5642 # if (__FreeBSD_version >= 500000)
5643 mtx_destroy(&sc->top_mtx);
5644 mtx_destroy(&sc->bottom_mtx);
5646 return 0; /* no error */
5650 fbsd_shutdown(device_t dev)
5652 shutdown_card(device_get_softc(dev));
5657 fbsd_attach(device_t dev)
5659 softc_t *sc = device_get_softc(dev);
5662 /* READ/WRITE_PCI_CFG need this. */
5665 /* What kind of card are we driving? */
5666 switch (READ_PCI_CFG(sc, TLP_CSID))
5669 case TLP_CSID_HSSIc:
5670 sc->card = &hssi_card;
5673 sc->card = &t3_card;
5676 sc->card = &ssi_card;
5679 sc->card = &t1_card;
5684 sc->dev_desc = device_get_desc(dev);
5686 /* Allocate PCI memory or IO resources to access the Tulip chip CSRs. */
5688 sc->csr_res_id = TLP_CBIO;
5689 sc->csr_res_type = SYS_RES_IOPORT;
5691 sc->csr_res_id = TLP_CBMA;
5692 sc->csr_res_type = SYS_RES_MEMORY;
5694 sc->csr_res = bus_alloc_resource(dev, sc->csr_res_type, &sc->csr_res_id,
5695 0, ~0, 1, RF_ACTIVE);
5696 if (sc->csr_res == NULL)
5698 printf("%s: bus_alloc_resource(csr) failed.\n", NAME_UNIT);
5701 sc->csr_tag = rman_get_bustag(sc->csr_res);
5702 sc->csr_handle = rman_get_bushandle(sc->csr_res);
5704 /* Allocate PCI interrupt resources for the card. */
5706 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_res_id,
5707 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
5708 if (sc->irq_res == NULL)
5710 printf("%s: bus_alloc_resource(irq) failed.\n", NAME_UNIT);
5714 if ((error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
5715 NULL, bsd_interrupt, sc, &sc->irq_cookie)))
5717 printf("%s: bus_setup_intr() failed; error %d\n", NAME_UNIT, error);
5722 # if (__FreeBSD_version >= 500000)
5723 /* Initialize the top-half and bottom-half locks. */
5724 mtx_init(&sc->top_mtx, NAME_UNIT, "top half lock", MTX_DEF);
5725 mtx_init(&sc->bottom_mtx, NAME_UNIT, "bottom half lock", MTX_DEF);
5728 /* Start the card and attach a kernel interface and line protocol. */
5729 if ((error = attach_card(sc, ""))) detach_card(sc);
5733 static device_method_t methods[] =
5735 DEVMETHOD(device_probe, fbsd_probe),
5736 DEVMETHOD(device_attach, fbsd_attach),
5737 DEVMETHOD(device_detach, fbsd_detach),
5738 DEVMETHOD(device_shutdown, fbsd_shutdown),
5739 /* This driver does not suspend and resume. */
5743 static driver_t driver =
5745 .name = DEVICE_NAME,
5747 # if (__FreeBSD_version >= 500000)
5748 .size = sizeof(softc_t),
5749 # else /* FreeBSD-4 */
5750 .softc = sizeof(softc_t),
5754 static devclass_t devclass;
5756 DRIVER_MODULE(lmc, pci, driver, devclass, 0, 0);
5757 MODULE_VERSION(lmc, 2);
5758 MODULE_DEPEND(lmc, pci, 1, 1, 1);
5760 MODULE_DEPEND(lmc, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
5763 MODULE_DEPEND(lmc, sppp, 1, 1, 1);
5766 #endif /* __FreeBSD__ */
5768 /* This is the I/O configuration interface for NetBSD. */
5773 nbsd_match(struct device *parent, struct cfdata *match, void *aux)
5775 struct pci_attach_args *pa = aux;
5776 u_int32_t cfid = pci_conf_read(pa->pa_pc, pa->pa_tag, TLP_CFID);
5777 u_int32_t csid = pci_conf_read(pa->pa_pc, pa->pa_tag, TLP_CSID);
5779 /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
5780 if (cfid != TLP_CFID_TULIP) return 0;
5784 case TLP_CSID_HSSIc:
5795 nbsd_detach(struct device *self, int flags)
5797 softc_t *sc = (softc_t *)self; /* device is first in softc */
5799 /* Stop the card and detach from the kernel. */
5802 /* Release resources. */
5803 if (sc->sdh_cookie != NULL)
5805 shutdownhook_disestablish(sc->sdh_cookie);
5806 sc->sdh_cookie = NULL;
5808 if (sc->irq_cookie != NULL)
5810 pci_intr_disestablish(sc->pa_pc, sc->irq_cookie);
5811 sc->irq_cookie = NULL;
5815 bus_space_unmap(sc->csr_tag, sc->csr_handle, TLP_CSR_SIZE);
5819 return 0; /* no error */
5823 nbsd_attach(struct device *parent, struct device *self, void *aux)
5825 softc_t *sc = (softc_t *)self; /* device is first in softc */
5826 struct pci_attach_args *pa = aux;
5827 const char *intrstr;
5828 bus_addr_t csr_addr;
5831 /* READ/WRITE_PCI_CFG need these. */
5832 sc->pa_pc = pa->pa_pc;
5833 sc->pa_tag = pa->pa_tag;
5834 /* bus_dma needs this. */
5835 sc->pa_dmat = pa->pa_dmat;
5837 /* What kind of card are we driving? */
5838 switch (READ_PCI_CFG(sc, TLP_CSID))
5841 case TLP_CSID_HSSIc:
5842 sc->dev_desc = HSSI_DESC;
5843 sc->card = &hssi_card;
5846 sc->dev_desc = T3_DESC;
5847 sc->card = &t3_card;
5850 sc->dev_desc = SSI_DESC;
5851 sc->card = &ssi_card;
5854 sc->dev_desc = T1E1_DESC;
5855 sc->card = &t1_card;
5860 printf(": %s\n", sc->dev_desc);
5862 /* Allocate PCI resources to access the Tulip chip CSRs. */
5864 csr_addr = (bus_addr_t)READ_PCI_CFG(sc, TLP_CBIO) & -2;
5865 sc->csr_tag = pa->pa_iot; /* bus_space tag for IO refs */
5867 csr_addr = (bus_addr_t)READ_PCI_CFG(sc, TLP_CBMA);
5868 sc->csr_tag = pa->pa_memt; /* bus_space tag for MEM refs */
5870 if ((error = bus_space_map(sc->csr_tag, csr_addr,
5871 TLP_CSR_SIZE, 0, &sc->csr_handle)))
5873 printf("%s: bus_space_map() failed; error %d\n", NAME_UNIT, error);
5877 /* Allocate PCI interrupt resources. */
5878 if ((error = pci_intr_map(pa, &sc->intr_handle)))
5880 printf("%s: pci_intr_map() failed; error %d\n", NAME_UNIT, error);
5881 nbsd_detach(self, 0);
5884 sc->irq_cookie = pci_intr_establish(pa->pa_pc, sc->intr_handle,
5885 IPL_NET, bsd_interrupt, sc);
5886 if (sc->irq_cookie == NULL)
5888 printf("%s: pci_intr_establish() failed\n", NAME_UNIT);
5889 nbsd_detach(self, 0);
5892 intrstr = pci_intr_string(pa->pa_pc, sc->intr_handle);
5894 /* Install a shutdown hook. */
5895 sc->sdh_cookie = shutdownhook_establish(shutdown_card, sc);
5896 if (sc->sdh_cookie == NULL)
5898 printf("%s: shutdown_hook_establish() failed\n", NAME_UNIT);
5899 nbsd_detach(self, 0);
5903 /* Initialize the top-half and bottom-half locks. */
5904 simple_lock_init(&sc->top_lock);
5905 simple_lock_init(&sc->bottom_lock);
5907 /* Start the card and attach a kernel interface and line protocol. */
5908 if ((error = attach_card(sc, intrstr))) detach_card(sc);
5911 # if (__NetBSD_Version__ >= 106080000) /* 1.6H */
5912 CFATTACH_DECL(lmc, sizeof(softc_t),
5913 nbsd_match, nbsd_attach, nbsd_detach, NULL);
5915 struct cfattach lmc_ca =
5917 /*.ca_name = DEVICE_NAME, */
5918 .ca_devsize = sizeof(softc_t),
5919 .ca_match = nbsd_match,
5920 .ca_attach = nbsd_attach,
5921 .ca_detach = nbsd_detach,
5922 .ca_activate = NULL,
5926 # if (__NetBSD_Version__ >= 106080000)
5927 CFDRIVER_DECL(lmc, DV_IFNET, NULL);
5929 static struct cfdriver lmc_cd =
5931 .cd_name = DEVICE_NAME,
5932 .cd_class = DV_IFNET,
5938 /* cfdata is declared static, unseen outside this module. */
5939 /* It is used for LKM; config builds its own in ioconf.c. */
5940 static struct cfdata lmc_cf =
5942 # if (__NetBSD_Version__ >= 106080000)
5943 .cf_name = DEVICE_NAME,
5944 .cf_atname = DEVICE_NAME,
5946 .cf_driver = &lmc_cd,
5947 .cf_attach = &lmc_ca,
5950 .cf_fstate = FSTATE_STAR,
5953 # if (__NetBSD_Version__ >= 106080000)
5954 MOD_MISC(DEVICE_NAME)
5956 static struct lkm_misc _module =
5958 .lkm_name = DEVICE_NAME,
5959 .lkm_type = LM_MISC,
5961 .lkm_ver = LKM_VERSION,
5965 /* From /sys/dev/pci/pci.c (no public prototype). */
5966 int pciprint(void *, const char *);
5968 static int lkm_nbsd_match(struct pci_attach_args *pa)
5969 { return nbsd_match(0, 0, pa); }
5971 /* LKM loader finds this by appending "_lkmentry" to filename "if_lmc". */
5972 int if_lmc_lkmentry(struct lkm_table *lkmtp, int cmd, int ver)
5976 if (ver != LKM_VERSION) return EINVAL;
5981 struct cfdriver* pcicd;
5983 lkmtp->private.lkm_misc = &_module;
5984 if ((pcicd = config_cfdriver_lookup("pci")) == NULL)
5986 printf("%s: config_cfdriver_lookup(pci) failed; error %d\n",
5987 lmc_cd.cd_name, error);
5990 # if (__NetBSD_Version__ >= 106080000)
5991 if ((error = config_cfdriver_attach(&lmc_cd)))
5993 printf("%s: config_cfdriver_attach() failed; error %d\n",
5994 lmc_cd.cd_name, error);
5997 if ((error = config_cfattach_attach(lmc_cd.cd_name, &lmc_ca)))
5999 printf("%s: config_cfattach_attach() failed; error %d\n",
6000 lmc_cd.cd_name, error);
6001 config_cfdriver_detach(&lmc_cd);
6005 for (i=0; i<pcicd->cd_ndevs; i++)
6008 /* A pointer to a device is a pointer to its softc. */
6009 struct pci_softc *sc = pcicd->cd_devs[i];
6010 if (sc == NULL) continue;
6011 for (dev=0; dev<sc->sc_maxndevs; dev++)
6013 struct pci_attach_args pa;
6014 pcitag_t tag = pci_make_tag(sc->sc_pc, sc->sc_bus, dev, 0);
6015 if (pci_probe_device(sc, tag, lkm_nbsd_match, &pa) != 0)
6016 config_attach(pcicd->cd_devs[i], &lmc_cf, &pa, pciprint);
6017 /* config_attach doesn't return on failure; it calls panic. */
6024 for (i=lmc_cd.cd_ndevs-1; i>=0; i--)
6026 struct device *dev = lmc_cd.cd_devs[i];
6027 if (dev == NULL) continue;
6028 if ((error = config_detach(dev, 0)))
6030 printf("%s: config_detach() failed; error %d\n",
6031 dev->dv_xname, error);
6035 # if (__NetBSD_Version__ >= 106080000)
6036 if ((error = config_cfattach_detach(lmc_cd.cd_name, &lmc_ca)))
6038 printf("%s: config_cfattach_detach() failed; error %d\n",
6039 lmc_cd.cd_name, error);
6042 if ((error = config_cfdriver_detach(&lmc_cd)))
6044 printf("%s: config_cfdriver_detach() failed; error %d\n",
6045 lmc_cd.cd_name, error);
6058 #endif /* __NetBSD__ */
6060 /* This is the I/O configuration interface for OpenBSD. */
6065 obsd_match(struct device *parent, void *match, void *aux)
6067 struct pci_attach_args *pa = aux;
6068 u_int32_t cfid = pci_conf_read(pa->pa_pc, pa->pa_tag, TLP_CFID);
6069 u_int32_t csid = pci_conf_read(pa->pa_pc, pa->pa_tag, TLP_CSID);
6071 /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
6072 if (cfid != TLP_CFID_TULIP) return 0;
6076 case TLP_CSID_HSSIc:
6080 return 100; /* match better than other 21140 drivers */
6087 obsd_detach(struct device *self, int flags)
6089 softc_t *sc = (softc_t *)self; /* device is first in softc */
6091 /* Stop the card and detach from the kernel. */
6094 /* Release resources. */
6095 if (sc->sdh_cookie != NULL)
6097 shutdownhook_disestablish(sc->sdh_cookie);
6098 sc->sdh_cookie = NULL;
6100 if (sc->irq_cookie != NULL)
6102 pci_intr_disestablish(sc->pa_pc, sc->irq_cookie);
6103 sc->irq_cookie = NULL;
6107 bus_space_unmap(sc->csr_tag, sc->csr_handle, TLP_CSR_SIZE);
6111 return 0; /* no error */
6115 obsd_attach(struct device *parent, struct device *self, void *aux)
6117 softc_t *sc = (softc_t *)self; /* device is first in softc */
6118 struct pci_attach_args *pa = aux;
6119 const char *intrstr;
6120 bus_addr_t csr_addr;
6123 /* READ/WRITE_PCI_CFG need these. */
6124 sc->pa_pc = pa->pa_pc;
6125 sc->pa_tag = pa->pa_tag;
6126 /* bus_dma needs this. */
6127 sc->pa_dmat = pa->pa_dmat;
6129 /* What kind of card are we driving? */
6130 switch (READ_PCI_CFG(sc, TLP_CSID))
6133 case TLP_CSID_HSSIc:
6134 sc->dev_desc = HSSI_DESC;
6135 sc->card = &hssi_card;
6138 sc->dev_desc = T3_DESC;
6139 sc->card = &t3_card;
6142 sc->dev_desc = SSI_DESC;
6143 sc->card = &ssi_card;
6146 sc->dev_desc = T1E1_DESC;
6147 sc->card = &t1_card;
6152 printf(": %s\n", sc->dev_desc);
6154 /* Allocate PCI resources to access the Tulip chip CSRs. */
6156 csr_addr = (bus_addr_t)READ_PCI_CFG(sc, TLP_CBIO) & -2;
6157 sc->csr_tag = pa->pa_iot; /* bus_space tag for IO refs */
6159 csr_addr = (bus_addr_t)READ_PCI_CFG(sc, TLP_CBMA);
6160 sc->csr_tag = pa->pa_memt; /* bus_space tag for MEM refs */
6162 if ((error = bus_space_map(sc->csr_tag, csr_addr,
6163 TLP_CSR_SIZE, 0, &sc->csr_handle)))
6165 printf("%s: bus_space_map() failed; error %d\n", NAME_UNIT, error);
6169 /* Allocate PCI interrupt resources. */
6170 if ((error = pci_intr_map(pa, &sc->intr_handle)))
6172 printf("%s: pci_intr_map() failed; error %d\n", NAME_UNIT, error);
6173 obsd_detach(self, 0);
6176 sc->irq_cookie = pci_intr_establish(pa->pa_pc, sc->intr_handle,
6177 IPL_NET, bsd_interrupt, sc, self->dv_xname);
6178 if (sc->irq_cookie == NULL)
6180 printf("%s: pci_intr_establish() failed\n", NAME_UNIT);
6181 obsd_detach(self, 0);
6184 intrstr = pci_intr_string(pa->pa_pc, sc->intr_handle);
6186 /* Install a shutdown hook. */
6187 sc->sdh_cookie = shutdownhook_establish(shutdown_card, sc);
6188 if (sc->sdh_cookie == NULL)
6190 printf("%s: shutdown_hook_establish() failed\n", NAME_UNIT);
6191 obsd_detach(self, 0);
6195 /* Initialize the top-half and bottom-half locks. */
6196 simple_lock_init(&sc->top_lock);
6197 simple_lock_init(&sc->bottom_lock);
6199 /* Start the card and attach a kernel interface and line protocol. */
6200 if ((error = attach_card(sc, intrstr))) detach_card(sc);
6203 struct cfattach lmc_ca =
6205 .ca_devsize = sizeof(softc_t),
6206 .ca_match = obsd_match,
6207 .ca_attach = obsd_attach,
6208 .ca_detach = obsd_detach,
6209 .ca_activate = NULL,
6212 struct cfdriver lmc_cd =
6214 .cd_name = DEVICE_NAME,
6216 .cd_class = DV_IFNET,
6221 /* cfdata is declared static, unseen outside this module. */
6222 /* It is used for LKM; config builds its own in ioconf.c. */
6223 static struct cfdata lmc_cfdata =
6225 .cf_attach = &lmc_ca,
6226 .cf_driver = &lmc_cd,
6228 .cf_fstate = FSTATE_STAR,
6231 static struct lkm_any _module =
6233 .lkm_name = DEVICE_NAME,
6234 .lkm_type = LM_MISC,
6236 .lkm_ver = LKM_VERSION,
6239 /* From /sys/dev/pci/pci.c (no public prototype). */
6240 int pciprint(void *, const char *);
6242 extern struct cfdriver pci_cd;
6244 /* LKM loader finds this by appending "_lkmentry" to filename "if_lmc". */
6245 int if_lmc_lkmentry(struct lkm_table *lkmtp, int cmd, int ver)
6249 if (ver != LKM_VERSION) return EINVAL;
6253 { /* XXX This works for ONE card on pci0 of a i386 machine! XXX */
6254 lkmtp->private.lkm_any = &_module;
6255 for (i=0; i<pci_cd.cd_ndevs; i++)
6257 struct pci_attach_args pa;
6258 struct device *parent = pci_cd.cd_devs[i];
6259 if (parent == NULL) continue; /* dead clone? */
6260 if ((parent->dv_unit)!=0) continue; /* only bus zero */
6261 /* XXX For machine independence, need: pcibus_attach_args. XXX */
6262 /* XXX See NetBSD's sys/dev/pci/pci.c/pci_probe_device. XXX */
6263 /* XXX Why isn't there an LKM network interface module? XXX */
6264 pa.pa_pc = NULL; /* XXX */
6265 pa.pa_bus = 0; /* XXX */
6266 pa.pa_iot = I386_BUS_SPACE_IO; /* XXX */
6267 pa.pa_memt = I386_BUS_SPACE_MEM; /* XXX */
6268 pa.pa_dmat = &pci_bus_dma_tag; /* XXX */
6269 for (pa.pa_device=0; pa.pa_device<32; pa.pa_device++) /* XXX */
6272 pa.pa_function = 0; /* DEC-21140A has function 0 only XXX */
6273 pa.pa_tag = pci_make_tag(pa.pa_pc, pa.pa_bus, pa.pa_device, 0);
6274 pa.pa_id = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_ID_REG);
6275 if ((pa.pa_id & 0xFFFF) == 0xFFFF) continue;
6276 if ((pa.pa_id & 0xFFFF) == 0) continue;
6277 /* XXX this only works for pci0 -- no swizzelling XXX */
6279 pa.pa_intrtag = pa.pa_tag;
6280 intr = pci_conf_read(pa.pa_pc, pa.pa_tag, PCI_INTERRUPT_REG);
6281 pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
6282 pa.pa_intrpin = ((PCI_INTERRUPT_PIN(intr) -1) % 4) +1;
6283 if (obsd_match(parent, &lmc_cfdata, &pa))
6284 config_attach(parent, &lmc_cfdata, &pa, pciprint);
6285 /* config_attach doesn't return on failure; it calls panic. */
6292 for (i=lmc_cd.cd_ndevs-1; i>=0; i--)
6294 struct device *dev = lmc_cd.cd_devs[i];
6295 if (dev == NULL) continue;
6296 if ((error = config_detach(dev, 0)))
6297 printf("%s: config_detach() failed; error %d\n", dev->dv_xname, error);
6308 #endif /* __OpenBSD__ */
6310 /* This is the I/O configuration interface for BSD/OS. */
6315 bsdi_match(pci_devaddr_t *pa)
6317 u_int32_t cfid = pci_inl(pa, TLP_CFID);
6318 u_int32_t csid = pci_inl(pa, TLP_CSID);
6320 /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
6321 if (cfid != TLP_CFID_TULIP) return 0;
6325 case TLP_CSID_HSSIc:
6336 bsdi_probe(struct device *parent, struct cfdata *cf, void *aux)
6338 struct isa_attach_args *ia = aux;
6339 pci_devaddr_t *pa = NULL;
6342 /* This must be a PCI bus. */
6343 if (ia->ia_bustype != BUS_PCI) return 0;
6345 /* Scan PCI bus for our boards. */
6346 if ((pa = pci_scan(bsdi_match)) == 0) return 0;
6348 /* Scan config space for IO and MEM base registers and IRQ info. */
6349 pci_getres(pa, &res, 1, ia);
6351 /* Crucial: pass pci_devaddr to bsdi_attach in ia_aux. */
6352 ia->ia_aux = (void *)pa;
6358 bsdi_attach(struct device *parent, struct device *self, void *aux)
6360 softc_t *sc = (softc_t *)self; /* device is first in softc */
6361 struct isa_attach_args *ia = aux;
6362 pci_devaddr_t *pa = ia->ia_aux; /* this is crucial! */
6365 /* READ/WRITE_PCI_CFG need this. */
6368 /* What kind of card are we driving? */
6369 switch (READ_PCI_CFG(sc, TLP_CSID))
6372 case TLP_CSID_HSSIc:
6373 sc->dev_desc = HSSI_DESC;
6374 sc->card = &hssi_card;
6377 sc->dev_desc = T3_DESC;
6378 sc->card = &t3_card;
6381 sc->dev_desc = SSI_DESC;
6382 sc->card = &ssi_card;
6385 sc->dev_desc = T1E1_DESC;
6386 sc->card = &t1_card;
6391 printf(": %s\n", sc->dev_desc);
6393 /* Allocate PCI memory or IO resources to access the Tulip chip CSRs. */
6394 sc->csr_iobase = ia->ia_iobase;
6395 sc->csr_membase = (u_int32_t *)mapphys((vm_offset_t)ia->ia_maddr, TLP_CSR_SIZE);
6397 /* Attach to the PCI bus. */
6398 isa_establish(&sc->id, &sc->dev);
6400 /* Allocate PCI interrupt resources for the card. */
6401 sc->ih.ih_fun = bsd_interrupt;
6403 intr_establish(ia->ia_irq, &sc->ih, DV_NET);
6405 /* Install a shutdown hook. */
6406 sc->ats.func = shutdown_card;
6408 atshutdown(&sc->ats, ATSH_ADD);
6410 /* Initialize the top-half and bottom-half locks. */
6411 simple_lock_init(&sc->top_lock);
6412 simple_lock_init(&sc->bottom_lock);
6414 /* Start the card and attach a kernel interface and line protocol. */
6415 if ((error = attach_card(sc, ""))) detach_card(sc);
6418 struct cfdriver lmccd =
6421 .cd_name = DEVICE_NAME,
6422 .cd_match = bsdi_probe,
6423 .cd_attach = bsdi_attach,
6424 .cd_class = DV_IFNET,
6425 .cd_devsize = sizeof(softc_t),
6427 #endif /* __bsdi__ */
6431 /* The kernel calls this procedure when an interrupt happens. */
6433 linux_interrupt(int irq, void *dev, struct pt_regs *regs)
6435 struct net_device *net_dev = dev;
6436 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6438 /* Cut losses early if this is not our interrupt. */
6439 if ((READ_CSR(TLP_STATUS) & TLP_INT_TXRX) == 0)
6442 /* Disable card interrupts. */
6443 WRITE_CSR(TLP_INT_ENBL, TLP_INT_DISABLE);
6445 /* Handle the card interrupt with the dev->poll method. */
6446 if (netif_rx_schedule_prep(net_dev))
6447 __netif_rx_schedule(net_dev); /* NAPI - add to poll list */
6449 printk("%s: interrupt while on poll list\n", NAME_UNIT);
6454 /* This net_device method services interrupts in a softirq. */
6455 /* With rxintr_cleanup(), it implements input flow control. */
6457 linux_poll(struct net_device *net_dev, int *budget)
6459 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6462 /* Yes, we do NAPI. */
6463 /* Allow processing up to net_dev->quota incoming packets. */
6464 /* This is the ONLY time core_interrupt() may process rx pkts. */
6465 /* Otherwise (sc->quota == 0) and rxintr_cleanup() is a NOOP. */
6466 sc->quota = net_dev->quota;
6468 /* Handle the card interrupt with kernel ints enabled. */
6469 /* Process rx pkts (and tx pkts, too). */
6470 /* Card interrupts are disabled. */
6471 core_interrupt(sc, 0);
6473 /* Report number of rx packets processed. */
6474 received = net_dev->quota - sc->quota;
6475 net_dev->quota -= received;
6476 *budget -= received;
6478 /* if quota prevented processing all rx pkts, leave rx ints disabled */
6479 if (sc->quota == 0) /* this is off by one...but harmless */
6481 WRITE_CSR(TLP_INT_ENBL, TLP_INT_TX);
6482 return 1; /* more pkts to handle -- reschedule */
6485 sc->quota = 0; /* disable rx pkt processing by rxintr_cleanup() */
6486 netif_rx_complete(net_dev); /* NAPI - remove from poll list */
6488 /* Enable card interrupts. */
6489 WRITE_CSR(TLP_INT_ENBL, TLP_INT_TXRX);
6493 /* These next routines are similar to BSD's ifnet kernel/driver interface. */
6495 /* This net_device method hands outgoing packets to the transmitter. */
6496 /* With txintr_setup(), it implements output flow control. */
6497 /* Called from a syscall (user context; no spinlocks). */
6499 linux_start(struct sk_buff *skb, struct net_device *net_dev)
6501 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6503 if (sc->tx_skb == NULL)
6505 /* Put this skb where the transmitter will see it. */
6508 /* Start the transmitter; incoming pkts are NOT processed. */
6509 user_interrupt(sc, 0);
6511 /* If the tx didn't take the skb then stop the queue. */
6512 /* This can happen if another CPU is in core_interrupt(). */
6513 if (sc->tx_skb != NULL) netif_stop_queue(net_dev);
6518 /* This shouldn't happen; skb is NOT consumed. */
6519 if (netif_queue_stopped(net_dev))
6520 printk("%s: dev->start() called with queue stopped\n", NAME_UNIT);
6522 netif_stop_queue(net_dev);
6527 /* This net_device method restarts the transmitter if it hangs. */
6528 /* Called from a softirq. */
6530 linux_timeout(struct net_device *net_dev)
6532 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6534 /* Start the transmitter; incoming packets are NOT processed. */
6535 user_interrupt(sc, 1);
6538 /* This net_device method handles IOCTL syscalls. */
6539 /* Called from a syscall (user context; no spinlocks; can sleep). */
6541 linux_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
6543 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6546 if ((cmd >= SIOCDEVPRIVATE) && (cmd <= SIOCDEVPRIVATE+15))
6548 struct iohdr *iohdr = (struct iohdr *)ifr;
6549 u_int16_t direction = iohdr->direction;
6550 u_int16_t length = iohdr->length;
6551 char *user_addr = (char *)iohdr->iohdr;
6554 if (iohdr->cookie != NGM_LMC_COOKIE) return -EINVAL;
6556 /* Emulate a BSD-style IOCTL syscall. */
6557 kern_addr = kmalloc(length, GFP_KERNEL);
6558 if (kern_addr == NULL)
6560 if ((error == 0) && ((direction & DIR_IOW) != 0))
6561 error = copy_from_user(kern_addr, user_addr, length);
6563 error = -core_ioctl(sc, (unsigned long)cmd, kern_addr);
6564 if ((error == 0) && ((direction & DIR_IOR) != 0))
6565 error = copy_to_user(user_addr, kern_addr, length);
6569 else if (cmd == SIOCWANDEV)
6571 const size_t size = sizeof(sync_serial_settings);
6573 switch (ifr->ifr_settings.type)
6575 case IF_GET_IFACE: /* get interface config */
6577 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
6578 if (ifr->ifr_settings.size < size)
6580 ifr->ifr_settings.size = size;
6585 if (sc->config.tx_clk_src == CFG_CLKMUX_ST)
6586 sc->hdlc_settings.clock_type = CLOCK_EXT;
6587 if (sc->config.tx_clk_src == CFG_CLKMUX_INT)
6588 sc->hdlc_settings.clock_type = CLOCK_TXINT;
6589 if (sc->config.tx_clk_src == CFG_CLKMUX_RT)
6590 sc->hdlc_settings.clock_type = CLOCK_TXFROMRX;
6591 sc->hdlc_settings.loopback = (sc->config.loop_back != CFG_LOOP_NONE) ? 1:0;
6592 sc->hdlc_settings.clock_rate = sc->status.tx_speed;
6593 error = copy_to_user(ifr->ifr_settings.ifs_ifsu.sync,
6594 &sc->hdlc_settings, size);
6598 case IF_IFACE_SYNC_SERIAL: /* set interface config */
6600 if (!capable(CAP_NET_ADMIN))
6603 error = copy_from_user(&sc->hdlc_settings,
6604 ifr->ifr_settings.ifs_ifsu.sync, size);
6605 /* hdlc_settings are currently ignored. */
6608 default: /* Pass the rest to the line protocol code. */
6610 error = hdlc_ioctl(net_dev, ifr, cmd);
6615 # endif /* GEN_HDLC */
6616 else /* unknown IOCTL command */
6620 printk("%s: linux_ioctl; cmd=0x%08x error=%d\n",
6621 NAME_UNIT, cmd, error);
6626 /* This net_device method returns a pointer to device statistics. */
6627 static struct net_device_stats *
6628 linux_stats(struct net_device *net_dev)
6631 return &dev_to_hdlc(net_dev)->stats;
6633 softc_t *sc = net_dev->priv;
6634 return &sc->net_stats;
6638 /* Called from a softirq once a second. */
6640 linux_watchdog(unsigned long softc)
6642 softc_t *sc = (softc_t *)softc;
6643 u_int8_t old_oper_status = sc->status.oper_status;
6644 struct event_cntrs *cntrs = &sc->status.cntrs;
6645 struct net_device_stats *stats = linux_stats(sc->net_dev);
6647 core_watchdog(sc); /* updates oper_status */
6649 /* Notice change in link status. */
6650 if ((old_oper_status != STATUS_UP) &&
6651 (sc->status.oper_status == STATUS_UP)) /* link came up */
6653 hdlc_set_carrier(1, sc->net_dev);
6654 netif_wake_queue(sc->net_dev);
6656 if ((old_oper_status == STATUS_UP) &&
6657 (sc->status.oper_status != STATUS_UP)) /* link went down */
6659 hdlc_set_carrier(0, sc->net_dev);
6660 netif_stop_queue(sc->net_dev);
6663 /* Notice change in line protocol. */
6664 if (sc->config.line_pkg == PKG_RAWIP)
6666 sc->status.line_pkg = PKG_RAWIP;
6667 sc->status.line_prot = PROT_IP_HDLC;
6672 sc->status.line_pkg = PKG_GEN_HDLC;
6673 switch (sc->hdlc_dev->proto.id)
6676 sc->status.line_prot = PROT_PPP;
6678 case IF_PROTO_CISCO:
6679 sc->status.line_prot = PROT_C_HDLC;
6682 sc->status.line_prot = PROT_FRM_RLY;
6685 sc->status.line_prot = PROT_IP_HDLC;
6688 sc->status.line_prot = PROT_X25;
6690 case IF_PROTO_HDLC_ETH:
6691 sc->status.line_prot = PROT_ETH_HDLC;
6694 sc->status.line_prot = 0;
6698 # endif /* GEN_HDLC */
6700 /* Copy statistics from sc to net_dev for get_stats(). */
6701 stats->rx_packets = cntrs->ipackets;
6702 stats->tx_packets = cntrs->opackets;
6703 stats->rx_bytes = cntrs->ibytes;
6704 stats->tx_bytes = cntrs->obytes;
6705 stats->rx_errors = cntrs->ierrors;
6706 stats->tx_errors = cntrs->oerrors;
6707 stats->rx_dropped = cntrs->idiscards;
6708 stats->tx_dropped = cntrs->odiscards;
6709 stats->rx_fifo_errors = cntrs->fifo_over;
6710 stats->tx_fifo_errors = cntrs->fifo_under;
6711 stats->rx_missed_errors = cntrs->missed;
6712 stats->rx_over_errors = cntrs->overruns;
6714 /* Call this procedure again after one second. */
6715 sc->wd_timer.expires = jiffies + HZ; /* now plus one second */
6716 add_timer(&sc->wd_timer);
6719 /* This is the I/O configuration interface for Linux. */
6721 /* This net_device method is called when IFF_UP goes false. */
6723 linux_stop(struct net_device *net_dev)
6725 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6727 /* Stop the card and detach from the kernel. */
6728 detach_card(sc); /* doesn't fail */
6730 free_irq(net_dev->irq, net_dev); /* doesn't fail */
6732 del_timer(&sc->wd_timer); /* return value ignored */
6737 /* This net_device method is called when IFF_UP goes true. */
6739 linux_open(struct net_device *net_dev)
6741 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6744 /* Allocate PCI interrupt resources for the card. */
6745 if ((error = request_irq(net_dev->irq, &linux_interrupt, SA_SHIRQ,
6746 NAME_UNIT, net_dev)))
6748 printk("%s: request_irq() failed; error %d\n", NAME_UNIT, error);
6752 /* Arrange to call linux_watchdog() once a second. */
6753 init_timer(&sc->wd_timer);
6754 sc->wd_timer.expires = jiffies + HZ; /* now plus one second */
6755 sc->wd_timer.function = &linux_watchdog;
6756 sc->wd_timer.data = (unsigned long) sc;
6757 add_timer(&sc->wd_timer);
6759 /* Start the card and attach a kernel interface and line protocol. */
6760 if ((error = -attach_card(sc, "")))
6761 linux_stop(net_dev);
6764 net_dev->weight = sc->rxring.num_descs; /* input flow control */
6765 netif_start_queue(net_dev); /* output flow control */
6773 hdlc_attach(struct net_device *net_dev,
6774 unsigned short encoding, unsigned short parity)
6778 /* This pci_driver method is called during shutdown or module-unload. */
6779 /* This is called from user context; can sleep; no spinlocks! */
6781 linux_remove(struct pci_dev *pci_dev)
6783 struct net_device *net_dev = (struct net_device *)pci_get_drvdata(pci_dev);
6784 softc_t *sc = dev_to_hdlc(net_dev)->priv;
6786 if (net_dev == NULL) return;
6788 /* Assume that linux_stop() has already been called. */
6789 if (sc->flags & FLAG_NETDEV)
6791 unregister_hdlc_device(net_dev);
6793 unregister_netdev(net_dev);
6796 # if (IOREF_CSR == 0)
6797 if (sc->csr_membase != NULL)
6798 iounmap(sc->csr_membase);
6801 pci_disable_device(pci_dev);
6803 if (sc->csr_iobase != 0)
6804 pci_release_regions(pci_dev);
6806 pci_set_drvdata(pci_dev, NULL);
6809 free_netdev(net_dev);
6813 setup_netdev(struct net_device *net_dev)
6815 /* Initialize the generic network device. */
6816 /* Note similarity to BSD's lmc_ifnet_attach(). */
6817 net_dev->flags = IFF_POINTOPOINT;
6818 net_dev->flags |= IFF_RUNNING;
6819 net_dev->open = linux_open;
6820 net_dev->stop = linux_stop;
6821 net_dev->hard_start_xmit = linux_start;
6822 net_dev->do_ioctl = linux_ioctl;
6823 net_dev->get_stats = linux_stats;
6824 net_dev->tx_timeout = linux_timeout;
6825 net_dev->poll = linux_poll;
6826 net_dev->watchdog_timeo = 1 * HZ;
6827 net_dev->tx_queue_len = SNDQ_MAXLEN;
6828 net_dev->mtu = MAX_DESC_LEN;
6829 net_dev->type = ARPHRD_RAWHDLC;
6830 /* The receiver generates frag-lists for packets >4032 bytes. */
6831 /* The transmitter accepts scatter/gather lists and frag-lists. */
6832 /* However Linux linearizes outgoing packets since our hardware */
6833 /* doesn't compute soft checksums. All that work for nothing! */
6834 /*net_dev->features |= NETIF_F_SG; */
6835 /*net_dev->features |= NETIF_F_FRAGLIST; */
6838 /* This pci_driver method is called during boot or module-load. */
6839 /* This is called from user context; can sleep; no spinlocks! */
6841 linux_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
6843 u_int32_t cfid, csid;
6844 struct net_device *net_dev;
6848 /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
6849 pci_read_config_dword(pci_dev, TLP_CFID, &cfid);
6850 if (cfid != TLP_CFID_TULIP) return -ENXIO;
6851 pci_read_config_dword(pci_dev, TLP_CSID, &csid);
6855 case TLP_CSID_HSSIc:
6864 /* Declare that these cards use 32-bit single-address PCI cycles. */
6865 if ((error = pci_set_dma_mask(pci_dev, DMA_32BIT_MASK)))
6867 printk("%s: pci_set_dma_mask() failed; error %d\n", DEVICE_NAME, error);
6870 pci_set_consistent_dma_mask(pci_dev, DMA_32BIT_MASK); /* can't fail */
6872 # if GEN_HDLC /* generic-hdlc line protocols */
6874 /* device driver instance data, aka Soft Context or sc */
6875 if ((sc = kmalloc(sizeof(softc_t), GFP_KERNEL)) == NULL)
6877 printk("%s: kmalloc() failed\n", DEVICE_NAME);
6880 memset(sc, 0, sizeof(softc_t));
6882 /* Allocate space for the HDLC network device struct. */
6883 if ((net_dev = alloc_hdlcdev(sc)) == NULL)
6885 printk("%s: alloc_hdlcdev() failed\n", DEVICE_NAME);
6890 /* Initialize the network device struct. */
6891 setup_netdev(net_dev);
6893 /* Initialize the HDLC extension to the network device. */
6894 sc->hdlc_dev = dev_to_hdlc(net_dev);
6895 sc->hdlc_dev->attach = hdlc_attach; /* noop for this driver */
6896 sc->hdlc_dev->xmit = linux_start; /* the REAL hard_start_xmit() */
6898 # else /* GEN_HDLC */ /* no line protocol. */
6900 /* Allocate space for the bare network device struct. */
6901 net_dev = alloc_netdev(sizeof(softc_t), DEVICE_NAME"%d", setup_netdev);
6902 if (net_dev == NULL)
6904 printk("%s: alloc_netdev() failed\n", DEVICE_NAME);
6907 /* device driver instance data, aka Soft Context or sc */
6910 # endif /* GEN_HDLC */
6912 sc->net_dev = net_dev; /* NAME_UNIT macro needs this */
6913 sc->pci_dev = pci_dev; /* READ/WRITE_PCI_CFG macros need this */
6915 /* Cross-link pci_dev and net_dev. */
6916 pci_set_drvdata(pci_dev, net_dev); /* pci_dev->driver_data = net_dev */
6917 SET_NETDEV_DEV(net_dev, &pci_dev->dev); /* net_dev->class_dev.dev = &pci_dev->dev */
6918 SET_MODULE_OWNER(net_dev); /* ??? NOOP in linux-2.6.3. ??? */
6920 /* Sets cfcs.io and cfcs.mem; sets pci_dev->irq based on cfit.int */
6921 if ((error = pci_enable_device(pci_dev)))
6923 printk("%s: pci_enable_device() failed; error %d\n", DEVICE_NAME, error);
6924 linux_remove(pci_dev);
6927 net_dev->irq = pci_dev->irq; /* linux_open/stop need this */
6929 /* Allocate PCI memory and IO resources to access the Tulip chip CSRs. */
6930 if ((error = pci_request_regions(pci_dev, DEVICE_NAME)))
6932 printk("%s: pci_request_regions() failed; error %d\n", DEVICE_NAME, error);
6933 linux_remove(pci_dev);
6936 net_dev->base_addr = pci_resource_start(pci_dev, 0);
6937 net_dev->mem_start = pci_resource_start(pci_dev, 1);
6938 net_dev->mem_end = pci_resource_end(pci_dev, 1);
6939 sc->csr_iobase = net_dev->base_addr;
6941 # if (IOREF_CSR == 0)
6942 sc->csr_membase = ioremap_nocache(net_dev->mem_start, TLP_CSR_SIZE);
6943 if (sc->csr_membase == NULL)
6945 printk("%s: ioremap_nocache() failed\n", DEVICE_NAME);
6946 linux_remove(pci_dev);
6951 /* Sets cfcs.master, enabling PCI DMA; checks latency timer value. */
6952 pci_set_master(pci_dev); /* Later, attach_card() does this too. */
6954 /* Initialize the top-half and bottom-half locks. */
6955 /* Top_lock must be initialized before net_dev is registered. */
6956 init_MUTEX(&sc->top_lock);
6957 spin_lock_init(&sc->bottom_lock);
6960 if ((error = register_hdlc_device(net_dev)))
6962 printk("%s: register_hdlc_device() failed; error %d\n", DEVICE_NAME, error);
6963 linux_remove(pci_dev);
6967 if ((error = register_netdev(net_dev)))
6969 printk("%s: register_netdev() failed; error %d\n", DEVICE_NAME, error);
6970 linux_remove(pci_dev);
6974 /* The NAME_UNIT macro now works. Use DEVICE_NAME before this. */
6975 sc->flags |= FLAG_NETDEV;
6977 /* What kind of card are we driving? */
6978 switch (READ_PCI_CFG(sc, TLP_CSID))
6981 case TLP_CSID_HSSIc:
6982 sc->dev_desc = HSSI_DESC;
6983 sc->card = &hssi_card;
6986 sc->dev_desc = T3_DESC;
6987 sc->card = &t3_card;
6990 sc->dev_desc = SSI_DESC;
6991 sc->card = &ssi_card;
6994 sc->dev_desc = T1E1_DESC;
6995 sc->card = &t1_card;
6997 default: /* shouldn't happen! */
6998 linux_remove(pci_dev);
7002 /* Announce the hardware on the console. */
7003 printk("%s: <%s> io 0x%04lx/9 mem 0x%08lx/25 rom 0x%08lx/14 irq %d pci %s\n",
7004 NAME_UNIT, sc->dev_desc, pci_resource_start(pci_dev, 0),
7005 pci_resource_start(pci_dev, 1), pci_resource_start(pci_dev, 6),
7006 pci_dev->irq, pci_name(pci_dev));
7011 /* This pci driver knows how to drive these devices: */
7012 static __initdata struct pci_device_id pci_device_id_tbl[] =
7014 /* Looking for a DEC 21140A chip on any Lan Media Corp card. */
7015 { 0x1011, 0x0009, 0x1376, PCI_ANY_ID, 0, 0, 0 },
7016 { 0, 0, 0, 0, 0, 0, 0 }
7018 MODULE_DEVICE_TABLE(pci, pci_device_id_tbl);
7020 static struct pci_driver pci_driver =
7022 .name = DEVICE_NAME,
7023 .id_table = pci_device_id_tbl,
7024 .probe = linux_probe,
7025 .remove = __devexit_p(linux_remove),
7026 /* This driver does not suspend and resume. */
7029 /* This ultimately calls our pci_driver.probe() method. */
7030 static int __init linux_modload(void)
7031 { return pci_module_init(&pci_driver); }
7032 module_init(linux_modload);
7034 /* This ultimately calls our pci_driver.remove() method. */
7035 static void __exit linux_modunload(void)
7036 { pci_unregister_driver(&pci_driver); }
7037 module_exit(linux_modunload);
7039 MODULE_LICENSE("Dual BSD/GPL");
7040 MODULE_DESCRIPTION("Device driver for SBE/LMC Wide-Area Network cards");
7041 MODULE_AUTHOR("David Boggs <boggs@boggs.palo-alto.ca.us>");
7043 #endif /* __linux__ */