2 * Copyright (c) 2000, 2001 Richard Hodges and Matriplex, inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Matriplex, inc.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 ******************************************************************************
33 * This driver is derived from the Nicstar driver by Mark Tinguely, and
34 * some of the original driver still exists here. Those portions are...
35 * Copyright (c) 1996, 1997, 1998, 1999 Mark Tinguely
36 * All rights reserved.
38 ******************************************************************************
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/syslog.h>
56 #include <sys/module.h>
57 #include <machine/bus_memio.h>
58 #include <machine/bus.h>
59 #include <machine/resource.h>
63 #include <net/if_arp.h>
64 #include <net/netisr.h>
65 #include <net/if_var.h>
67 #include <netatm/port.h>
68 #include <netatm/queue.h>
69 #include <netatm/atm.h>
70 #include <netatm/atm_sys.h>
71 #include <netatm/atm_sap.h>
72 #include <netatm/atm_cm.h>
73 #include <netatm/atm_if.h>
74 #include <netatm/atm_stack.h>
75 #include <netatm/atm_pcb.h>
76 #include <netatm/atm_var.h>
77 #include <netatm/atm_vc.h>
79 #include <dev/idt/idtreg.h>
80 #include <dev/idt/idtvar.h>
82 /******************************************************************************
84 * HARP-specific definitions
88 #define IDT_DEV_NAME "idt"
90 #define IDT_IFF_MTU 9188
91 #define IDT_MAX_VCI 1023 /* 0 - 1023 */
94 #define iv_next iv_cmn.cv_next
95 #define iv_toku iv_cmn.cv_toku
96 #define iv_upper iv_cmn.cv_upper
97 #define iv_vccb iv_cmn.cv_connvc /* HARP 3.0 */
98 #define iv_state iv_cmn.cv_state
99 #define iu_pif iu_cmn.cu_pif
100 #define iu_unit iu_cmn.cu_unit
101 #define iu_flags iu_cmn.cu_flags
102 #define iu_mtu iu_cmn.cu_mtu
103 #define iu_open_vcc iu_cmn.cu_open_vcc
104 #define iu_instvcc iu_cmn.cu_instvcc /* HARP 3.0 */
105 #define iu_vcc iu_cmn.cu_vcc
106 #define iu_vcc_zone iu_cmn.cu_vcc_zone
107 #define iu_nif_zone iu_cmn.cu_nif_zone
108 #define iu_ioctl iu_cmn.cu_ioctl
109 #define iu_openvcc iu_cmn.cu_openvcc
110 #define iu_closevcc iu_cmn.cu_closevcc
111 #define iu_output iu_cmn.cu_output
112 #define iu_config iu_cmn.cu_config
113 #define iu_softc iu_cmn.cu_softc
116 * ATM Interface services
118 static struct stack_defn idt_svaal5 = {
127 static struct stack_defn idt_svaal4 = {
136 static struct stack_defn idt_svaal0 = {
145 struct stack_defn *idt_services = &idt_svaal0;
147 extern uma_zone_t idt_nif_zone;
148 extern uma_zone_t idt_vcc_zone;
150 static int idt_atm_bearerclass(struct attr_bearer *);
151 #ifdef T_ATM_BUFQUEUE
152 static CONNECTION *idt_atm_harpconn(Cmn_unit *, Cmn_vcc *);
154 static int idt_atm_ioctl(int, caddr_t, caddr_t);
156 static void idt_output(Cmn_unit *, Cmn_vcc *, KBuffer *);
157 static int idt_openvcc(Cmn_unit *, Cmn_vcc *);
158 static int idt_closevcc(Cmn_unit *, Cmn_vcc *);
159 static int idt_instvcc(Cmn_unit *, Cmn_vcc *);
161 static void idt_recv_stack(void *, KBuffer *);
163 /******************************************************************************
167 ******************************************************************************
169 * Handle netatm core service interface ioctl requests
174 * code ioctl function (sub)code
175 * data data to/from ioctl
176 * arg optional code-specific argument
179 * 0 request processed successfully
180 * error request failed - reason code
184 idt_atm_ioctl(int code, caddr_t addr, caddr_t arg)
186 #ifdef T_ATM_BUFQUEUE
187 CONNECTION *connection;
197 #ifdef T_ATM_BUFQUEUE
199 cup = (Cmn_unit *) addr;
200 cvp = (Cmn_vcc *) arg;
201 connection = idt_atm_harpconn(cup, cvp);
202 if (connection == NULL)
205 txq = connection->queue;
208 for (m = txq->mget; m != NULL; m = m->m_nextpkt)
209 retval += m->m_pkthdr.len;
217 #ifdef T_ATM_BUFQUEUE
218 /*******************************************************************************
220 * Get connection pointer from Cmn_unit and Cmn_vcc
222 * in: Cmn_unit and Cmn_vcc
223 * out: connection (NULL=error)
225 * Date first: 05/31/2001 last: 05/31/2001
229 idt_atm_harpconn(Cmn_unit * cup, Cmn_vcc * cvp)
231 struct vccb *vccinf; /* from HARP struct */
237 if (idt == NULL || cvp == NULL)
240 if (cvp->cv_connvc == NULL)
243 vccinf = cvp->cv_connvc->cvc_vcc;
248 vpi = vccinf->vc_vpi;
249 vci = vccinf->vc_vci;
251 return (idt_connect_find(idt, vpi, vci));
253 #endif /* T_ATM_BUFQUEUE */
255 /*******************************************************************************
257 * Get CBR/VBR/UBR class from bearer attribute
260 * out: NICCBR/NICVBR/NICABR/NICUBR
262 * Date first: 06/12/2001 last: 06/13/2001
266 idt_atm_bearerclass(struct attr_bearer * bearer)
268 switch (bearer->v.bearer_class) {
269 case T_ATM_CLASS_A:return (NICCBR);
271 if (idt_sysctl_vbriscbr)
272 return (NICCBR); /* use CBR slots for VBR VC's */
276 if (bearer->v.traffic_type == T_ATM_CBR)
278 if (bearer->v.traffic_type == T_ATM_VBR)
285 /* The flag idt_sysctl_vbriscbr allows us to set up a CBR VC as if it were
286 * VBR. This is primarily to avoid cell loss at a switch that cannot seem
287 * to buffer one or two cells of jitter. This jitter is created when many
288 * CBR slots have been taken, and a new CBR VC cannot use the optimally
289 * spaced slots, and has to use nearby slots instead.
291 * In this case, we want to use the VC SCR as the CBR value. The PCR and MBS
292 * is only of interest to the switch.
294 *******************************************************************************
296 * Initialize HARP service
297 * called from device attach
301 idt_harp_init(nicstar_reg_t *idt)
311 * Start initializing it
313 idt->iu_unit = device_get_unit(idt->dev);
314 idt->iu_mtu = IDT_IFF_MTU;
315 idt->iu_ioctl = idt_atm_ioctl;
316 idt->iu_openvcc = idt_openvcc;
317 idt->iu_instvcc = idt_instvcc;
318 idt->iu_closevcc = idt_closevcc;
319 idt->iu_output = idt_output;
320 idt->iu_vcc_zone = idt_vcc_zone;
321 idt->iu_nif_zone = idt_nif_zone;
322 idt->iu_softc = (void *)idt;
325 * Copy serial number into config space
327 idt->iu_config.ac_serial = 0;
329 idt->iu_config.ac_vendor = VENDOR_IDT;
330 idt->iu_config.ac_vendapi = VENDAPI_IDT_1;
331 idt->iu_config.ac_device = DEV_IDT_155;
332 idt->iu_config.ac_media = MEDIA_UNKNOWN;
333 idt->iu_config.ac_bustype = BUS_PCI;
335 idt->iu_pif.pif_pcr = idt->cellrate_rmax; /* ATM_PCR_OC3C; */
336 idt->iu_pif.pif_maxvpi = idt->conn_maxvpi;
337 idt->iu_pif.pif_maxvci = idt->conn_maxvci;
339 snprintf(idt->iu_config.ac_hard_vers,
340 sizeof(idt->iu_config.ac_hard_vers),
342 snprintf(idt->iu_config.ac_firm_vers,
343 sizeof(idt->iu_config.ac_firm_vers),
346 * Save device ram info for user-level programs NOTE: This really
347 * points to start of EEPROM and includes all the device registers
348 * in the lower 2 Megabytes.
350 idt->iu_config.ac_ram = NULL;
351 idt->iu_config.ac_ramsize = 0;
353 for (i = 0; i < 6; i++) {
354 idt_mac[i] = nicstar_eeprom_rd(idt, (0x6c + i));
357 /* looks like bad MAC */
358 if ((idt_mac[3] | idt_mac[4] | idt_mac[5]) == 0) {
359 GET_RDTSC(tsc_val); /* 24 bits on 500mhz CPU is about
363 idt_mac[2] = 0x48; /* use Fore prefix */
364 idt_mac[3] = (tsc_val >> 16) & 0xff;
365 idt_mac[4] = (tsc_val >> 8) & 0xff;
366 idt_mac[5] = (tsc_val) & 0xff;
367 device_printf(idt->dev,
368 "Cannot read MAC address from EEPROM, generating it.\n");
370 bcopy(&idt_mac, &idt->iu_pif.pif_macaddr.ma_data, sizeof(idt_mac));
372 device_printf(idt->dev, "MAC address %6D, HWrev=%d\n",
373 (u_int8_t *)&idt->iu_pif.pif_macaddr.ma_data, ":",
376 idt->iu_config.ac_macaddr = idt->iu_pif.pif_macaddr;
379 * Register this interface with ATM core services
381 error = atm_physif_register(&idt->iu_cmn, IDT_DEV_NAME, idt_services);
384 * Registration failed - back everything out
387 log(LOG_ERR, "%s(): atm_physif_register failed\n", __func__);
390 idt->iu_flags |= CUF_INITED;
394 * Add hook to out shutdown function at_shutdown (
395 * (bootlist_fn)idt_pci_shutdown, idt, SHUTDOWN_POST_SYNC );
402 /*******************************************************************************
408 idt_output(Cmn_unit * cmnunit, Cmn_vcc * cmnvcc, KBuffer * m)
410 struct vccb *vccinf; /* from HARP struct */
416 idt = (IDT *) cmnunit;
419 if (cmnvcc == NULL) {
420 device_printf(idt->dev, "idt_output arg error #1\n");
423 if (cmnvcc->cv_connvc == NULL) {
424 device_printf(idt->dev, "idt_output arg error #2\n");
427 vccinf = cmnvcc->cv_connvc->cvc_vcc;
428 if (vccinf == NULL) {
429 device_printf(idt->dev, "idt_output arg error #3\n");
432 vpi = vccinf->vc_vpi;
433 vci = vccinf->vc_vci;
435 #ifdef CVF_MPEG2TS /* option to split bufs into small TS bufs */
436 if (cmnvcc->cv_flags & CVF_MPEG2TS)
440 idt_transmit(idt, m, vpi, vci, flags);
448 /*******************************************************************************
454 idt_openvcc(Cmn_unit * cmnunit, Cmn_vcc * cmnvcc)
456 Atm_attributes *attrib; /* from HARP struct */
457 struct vccb *vccinf; /* from HARP struct */
458 CONNECTION *connection;
462 int class; /* NICCBR, NICVBR, or NICUBR */
464 idt = (IDT *) cmnunit;
466 if (cmnvcc == NULL || cmnvcc->cv_connvc == NULL) {
467 printf("idt_openvcc: bad request #1.\n");
470 attrib = &cmnvcc->cv_connvc->cvc_attr;
471 vccinf = cmnvcc->cv_connvc->cvc_vcc;
473 if (attrib == NULL || vccinf == NULL) {
474 printf("idt_openvcc: bad request #2.\n");
477 vpi = vccinf->vc_vpi;
478 vci = vccinf->vc_vci;
480 connection = idt_connect_find(idt, vpi, vci);
481 if (connection == NULL) {
482 printf("idt_openvcc: vpi/vci invalid: %d/%d\n", vpi, vci);
485 if (connection->status) {
486 printf("idt_openvcc: connection already open %d/%d\n", vpi, vci);
489 connection->status = 1;
490 connection->recv = NULL;
491 connection->rlen = 0;
492 connection->maxpdu = 20000;
493 connection->aal = IDTAAL5;
494 connection->traf_pcr = attrib->traffic.v.forward.PCR_all_traffic;
495 connection->traf_scr = attrib->traffic.v.forward.SCR_all_traffic;
496 connection->vccinf = vccinf; /* 12/15/2000 */
498 if (connection->traf_pcr <= 0)
499 connection->traf_pcr = connection->traf_scr;
500 if (connection->traf_scr <= 0)
501 connection->traf_scr = connection->traf_pcr;
503 class = idt_atm_bearerclass(&attrib->bearer);
504 if (vpi == 0 && vci == 5)
505 class = NICABR; /* higher priority than UBR */
506 if (vpi == 0 && vci == 16)
509 if (connection->traf_pcr < 0) { /* neither PCR nor SCR given */
510 connection->traf_pcr = 1;
511 connection->traf_scr = 1;
512 class = NICUBR; /* so give it lowest priority */
514 connection->class = class;
516 if (idt_connect_txopen(idt, connection)) {
517 device_printf(idt->dev, "cannot open connection for %d/%d\n",
521 if (idt_sysctl_logvcs)
522 printf("idt_openvcc: %d/%d, PCR=%d, SCR=%d\n", vpi, vci,
523 connection->traf_pcr, connection->traf_scr);
524 idt_connect_opencls(idt, connection, 1); /* open entry in rcv
530 /* We really don't handle ABR, but use it as a higher priority UBR. The
531 * idea is that a UBR connection that gives a PCR (like 0/16) should
532 * be given preference over a UBR connection that wants "everything else".
534 * Note that CLASS_X is typically UBR, but the traffic type information
535 * element may still specify CBR or VBR.
537 *******************************************************************************
543 idt_closevcc(Cmn_unit * cmnunit, Cmn_vcc * cmnvcc)
545 CONNECTION *connection;
546 nicstar_reg_t *idt = (nicstar_reg_t *) cmnunit;
550 if (cmnvcc && cmnvcc->cv_connvc && cmnvcc->cv_connvc->cvc_vcc) {
551 vpi = cmnvcc->cv_connvc->cvc_vcc->vc_vpi;
552 vci = cmnvcc->cv_connvc->cvc_vcc->vc_vci;
554 printf("idt_closevcc: bad vcivpi\n");
557 connection = idt_connect_find(idt, vpi, vci);
559 if (connection == NULL) {
560 printf("idt_closevcc: vpi/vci invalid: %d/%d\n", vpi, vci);
563 idt_connect_opencls(idt, connection, 0); /* close entry in rcv
566 if (connection->status == 0)
567 printf("idt_closevcc: close on empty connection %d/%d\n", vpi, vci);
568 if (connection->recv != NULL)
569 m_freem(connection->recv); /* recycle mbuf of partial PDU */
570 idt_connect_txclose(idt, connection);
571 connection->status = 0;
572 connection->recv = NULL;
573 connection->rlen = 0;
574 connection->maxpdu = 0;
576 connection->traf_pcr = 0;
577 connection->traf_scr = 0;
579 if (idt_sysctl_logvcs)
580 printf("idt_closevcc: vpi=%d vci=%d\n", vpi, vci);
587 * VCC Stack Instantiation
589 * This function is called via the common driver code during a device VCC
590 * stack instantiation. The common code has already validated some of
591 * the request so we just need to check a few more IDT-specific details.
596 * cup pointer to device common unit
597 * cvp pointer to common VCC entry
600 * 0 instantiation successful
601 * err instantiation failed - reason indicated
605 idt_instvcc(Cmn_unit * cmnunit, Cmn_vcc * cmnvcc)
607 Atm_attributes *attrib; /* from HARP struct */
610 int slots_vc, slots_cur, slots_max;
614 if (cmnvcc->cv_connvc == NULL)
617 idt = (IDT *) cmnunit;
621 attrib = &cmnvcc->cv_connvc->cvc_attr;
626 pcr = attrib->traffic.v.forward.PCR_all_traffic;
627 scr = attrib->traffic.v.forward.SCR_all_traffic;
630 pcr = scr; /* if PCR missing, default to SCR */
636 class = idt_atm_bearerclass(&attrib->bearer);
637 if (class == NICCBR) {
638 slots_max = idt->txslots_max;
639 slots_cur = idt->txslots_cur;
640 slots_vc = idt_slots_cbr(idt, scr); /* 06/13/2001: now using
642 if (slots_vc + slots_cur > slots_max) {
643 if (idt_sysctl_logvcs)
644 device_printf(idt->dev,
645 "Insufficient bandwidth (vc=%d cur=%d max=%d)\n",
646 slots_vc, slots_cur, slots_max);
650 /* This part was take from /sys/dev/hfa/fore_vcm.c */
652 switch (attrib->aal.type) {
656 if ((attrib->aal.v.aal4.forward_max_SDU_size > IDT_IFF_MTU) ||
657 (attrib->aal.v.aal4.backward_max_SDU_size > IDT_IFF_MTU))
661 if ((attrib->aal.v.aal5.forward_max_SDU_size > IDT_IFF_MTU) ||
662 (attrib->aal.v.aal5.backward_max_SDU_size > IDT_IFF_MTU))
672 * Pass Incoming PDU up Stack
674 * This function is called via the core ATM interrupt queue callback
675 * set in fore_recv_drain(). It will pass the supplied incoming
676 * PDU up the incoming VCC's stack.
681 * tok token to identify stack instantiation
682 * m pointer to incoming PDU buffer chain
688 idt_recv_stack(void *tok, KBuffer * m)
690 Idt_vcc *ivp = (Idt_vcc *) tok;
693 if ((m->m_flags & M_PKTHDR) == 0) {
694 printf("idt_recv_stack: Warning - mbuf chain has no header.\n");
699 * Send the data up the stack
701 STACK_CALL(CPCS_UNITDATA_SIG, ivp->iv_upper,
702 ivp->iv_toku, ivp->iv_vccb, (int)m, 0, err);
709 /******************************************************************************
711 * Enqueue received PDU for HARP to handle
713 * in: IDT device, mbuf, vpi, vci
715 * Date last: 12/14/2000
719 idt_receive(nicstar_reg_t * idt, struct mbuf * m, int vpi, int vci)
726 * The STACK_CALL needs to happen at splnet() in order for the stack
727 * sequence processing to work. Schedule an interrupt queue
728 * callback at splnet() since we are currently at device level.
732 * Prepend callback function pointer and token value to buffer. We
733 * have already guaranteed that the space is available in the first
738 * vcc = atm_dev_vcc_find(&idt->iu_cmn, (vpivci>> 16), vpivci &
742 vcc = atm_dev_vcc_find(&idt->iu_cmn, vpi, vci, VCC_IN);
744 if (vcc == NULL) { /* harp stack not ready or no vcc */
745 printf("idt_receive: no VCC %d/%d\n", vpi, vci);
749 space = m->m_data - idt_mbuf_base(m);
750 if (space < sizeof(atm_intr_func_t) + sizeof(int)) {
751 printf("idt_receive: NOT enough buffer space (%d).\n", space);
755 KB_HEADADJ(m, sizeof(atm_intr_func_t) + sizeof(int));
756 KB_DATASTART(m, cp, caddr_t);
757 *((atm_intr_func_t *) cp) = idt_recv_stack;
758 cp += sizeof(atm_intr_func_t);
760 *((void **)cp) = (void *)vcc;
765 if (! netisr_queue(NETISR_ATM, m))