11369Sdduvall /* 21369Sdduvall * CDDL HEADER START 31369Sdduvall * 41369Sdduvall * The contents of this file are subject to the terms of the 51369Sdduvall * Common Development and Distribution License (the "License"). 61369Sdduvall * You may not use this file except in compliance with the License. 71369Sdduvall * 81369Sdduvall * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 91369Sdduvall * or http://www.opensolaris.org/os/licensing. 101369Sdduvall * See the License for the specific language governing permissions 111369Sdduvall * and limitations under the License. 121369Sdduvall * 131369Sdduvall * When distributing Covered Code, include this CDDL HEADER in each 141369Sdduvall * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 151369Sdduvall * If applicable, add the following below this CDDL HEADER, with the 161369Sdduvall * fields enclosed by brackets "[]" replaced with your own identifying 171369Sdduvall * information: Portions Copyright [yyyy] [name of copyright owner] 181369Sdduvall * 191369Sdduvall * CDDL HEADER END 201369Sdduvall */ 211369Sdduvall 221369Sdduvall /* 231369Sdduvall * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 241369Sdduvall * Use is subject to license terms. 251369Sdduvall */ 261369Sdduvall 271369Sdduvall #pragma ident "%Z%%M% %I% %E% SMI" 281369Sdduvall 291369Sdduvall #include "sys/bge_impl2.h" 301369Sdduvall 311369Sdduvall #define U32TOPTR(x) ((void *)(uintptr_t)(uint32_t)(x)) 321369Sdduvall #define PTRTOU32(x) ((uint32_t)(uintptr_t)(void *)(x)) 331369Sdduvall 341369Sdduvall /* 351369Sdduvall * ========== RX side routines ========== 361369Sdduvall */ 371369Sdduvall 381369Sdduvall #define BGE_DBG BGE_DBG_RECV /* debug flag for this code */ 391369Sdduvall 401369Sdduvall static void bge_refill(bge_t *bgep, buff_ring_t *brp, sw_rbd_t *srbdp); 411369Sdduvall #pragma inline(bge_refill) 421369Sdduvall 431369Sdduvall /* 441369Sdduvall * Return the specified buffer (srbdp) to the ring it came from (brp). 451369Sdduvall * 461369Sdduvall * Note: 471369Sdduvall * If the driver is compiled with only one buffer ring *and* one 481369Sdduvall * return ring, then the buffers must be returned in sequence. 491369Sdduvall * In this case, we don't have to consider anything about the 501369Sdduvall * buffer at all; we can simply advance the cyclic counter. And 511369Sdduvall * we don't even need the refill mutex <rf_lock>, as the caller 521369Sdduvall * will already be holding the (one-and-only) <rx_lock>. 531369Sdduvall * 541369Sdduvall * If the driver supports multiple buffer rings, but only one 551369Sdduvall * return ring, the same still applies (to each buffer ring 561369Sdduvall * separately). 571369Sdduvall */ 581369Sdduvall static void 591369Sdduvall bge_refill(bge_t *bgep, buff_ring_t *brp, sw_rbd_t *srbdp) 601369Sdduvall { 611369Sdduvall uint64_t slot; 621369Sdduvall 631369Sdduvall _NOTE(ARGUNUSED(srbdp)) 641369Sdduvall 651369Sdduvall slot = brp->rf_next; 661369Sdduvall brp->rf_next = NEXT(slot, brp->desc.nslots); 671369Sdduvall bge_mbx_put(bgep, brp->chip_mbx_reg, slot); 681369Sdduvall } 691369Sdduvall 701369Sdduvall static mblk_t *bge_receive_packet(bge_t *bgep, bge_rbd_t *hw_rbd_p); 711369Sdduvall #pragma inline(bge_receive_packet) 721369Sdduvall 731369Sdduvall static mblk_t * 741369Sdduvall bge_receive_packet(bge_t *bgep, bge_rbd_t *hw_rbd_p) 751369Sdduvall { 761369Sdduvall bge_rbd_t hw_rbd; 771369Sdduvall buff_ring_t *brp; 781369Sdduvall sw_rbd_t *srbdp; 791369Sdduvall uchar_t *dp; 801369Sdduvall mblk_t *mp; 811369Sdduvall uint_t len; 821369Sdduvall uint_t minsize; 831369Sdduvall uint_t maxsize; 841369Sdduvall uint32_t pflags; 851369Sdduvall 861369Sdduvall mp = NULL; 871369Sdduvall hw_rbd = *hw_rbd_p; 881369Sdduvall 891369Sdduvall switch (hw_rbd.flags & (RBD_FLAG_MINI_RING|RBD_FLAG_JUMBO_RING)) { 901369Sdduvall case RBD_FLAG_MINI_RING|RBD_FLAG_JUMBO_RING: 911369Sdduvall default: 921369Sdduvall /* error, this shouldn't happen */ 931369Sdduvall BGE_PKTDUMP((bgep, &hw_rbd, NULL, "bad ring flags!")); 941369Sdduvall goto error; 951369Sdduvall 961369Sdduvall case RBD_FLAG_JUMBO_RING: 971369Sdduvall brp = &bgep->buff[BGE_JUMBO_BUFF_RING]; 981369Sdduvall break; 991369Sdduvall 1001369Sdduvall #if (BGE_BUFF_RINGS_USED > 2) 1011369Sdduvall case RBD_FLAG_MINI_RING: 1021369Sdduvall brp = &bgep->buff[BGE_MINI_BUFF_RING]; 1031369Sdduvall break; 1041369Sdduvall #endif /* BGE_BUFF_RINGS_USED > 2 */ 1051369Sdduvall 1061369Sdduvall case 0: 1071369Sdduvall brp = &bgep->buff[BGE_STD_BUFF_RING]; 1081369Sdduvall break; 1091369Sdduvall } 1101369Sdduvall 1111369Sdduvall if (hw_rbd.index >= brp->desc.nslots) { 1121369Sdduvall /* error, this shouldn't happen */ 1131369Sdduvall BGE_PKTDUMP((bgep, &hw_rbd, NULL, "bad ring index!")); 1141369Sdduvall goto error; 1151369Sdduvall } 1161369Sdduvall 1171369Sdduvall srbdp = &brp->sw_rbds[hw_rbd.index]; 1181369Sdduvall if (hw_rbd.opaque != srbdp->pbuf.token) { 1191369Sdduvall /* bogus, drop the packet */ 1201369Sdduvall BGE_PKTDUMP((bgep, &hw_rbd, srbdp, "bad ring token")); 1211369Sdduvall goto refill; 1221369Sdduvall } 1231369Sdduvall 1241369Sdduvall if ((hw_rbd.flags & RBD_FLAG_PACKET_END) == 0) { 1251369Sdduvall /* bogus, drop the packet */ 1261369Sdduvall BGE_PKTDUMP((bgep, &hw_rbd, srbdp, "unterminated packet")); 1271369Sdduvall goto refill; 1281369Sdduvall } 1291369Sdduvall 1301369Sdduvall if (hw_rbd.flags & RBD_FLAG_FRAME_HAS_ERROR) { 1311369Sdduvall /* bogus, drop the packet */ 1321369Sdduvall BGE_PKTDUMP((bgep, &hw_rbd, srbdp, "errored packet")); 1331369Sdduvall goto refill; 1341369Sdduvall } 1351369Sdduvall 1361369Sdduvall len = hw_rbd.len; 1371369Sdduvall 138*1408Srandyf #ifdef BGE_IPMI_ASF 1391369Sdduvall /* 140*1408Srandyf * When IPMI/ASF is enabled, VLAN tag must be stripped. 1411369Sdduvall */ 142*1408Srandyf if (bgep->asf_enabled && (hw_rbd.flags & RBD_FLAG_VLAN_TAG)) 143*1408Srandyf maxsize = bgep->chipid.ethmax_size + ETHERFCSL; 144*1408Srandyf else 145*1408Srandyf #endif 146*1408Srandyf /* 147*1408Srandyf * H/W will not strip the VLAN tag from incoming packet 148*1408Srandyf * now, as RECEIVE_MODE_KEEP_VLAN_TAG bit is set in 149*1408Srandyf * RECEIVE_MAC_MODE_REG register. 150*1408Srandyf */ 151*1408Srandyf maxsize = bgep->chipid.ethmax_size + VLAN_TAGSZ + ETHERFCSL; 1521369Sdduvall if (len > maxsize) { 1531369Sdduvall /* bogus, drop the packet */ 1541369Sdduvall BGE_PKTDUMP((bgep, &hw_rbd, srbdp, "oversize packet")); 1551369Sdduvall goto refill; 1561369Sdduvall } 1571369Sdduvall 158*1408Srandyf #ifdef BGE_IPMI_ASF 159*1408Srandyf if (bgep->asf_enabled && (hw_rbd.flags & RBD_FLAG_VLAN_TAG)) 160*1408Srandyf minsize = ETHERMIN + ETHERFCSL - VLAN_TAGSZ; 161*1408Srandyf else 162*1408Srandyf #endif 163*1408Srandyf minsize = ETHERMIN + ETHERFCSL; 1641369Sdduvall if (len < minsize) { 1651369Sdduvall /* bogus, drop the packet */ 1661369Sdduvall BGE_PKTDUMP((bgep, &hw_rbd, srbdp, "undersize packet")); 1671369Sdduvall goto refill; 1681369Sdduvall } 1691369Sdduvall 1701369Sdduvall /* 1711369Sdduvall * Packet looks good; get a buffer to copy it into. 1721369Sdduvall * We want to leave some space at the front of the allocated 1731369Sdduvall * buffer in case any upstream modules want to prepend some 1741369Sdduvall * sort of header. This also has the side-effect of making 1751369Sdduvall * the packet *contents* 4-byte aligned, as required by NCA! 1761369Sdduvall */ 177*1408Srandyf #ifdef BGE_IPMI_ASF 178*1408Srandyf if (bgep->asf_enabled && (hw_rbd.flags & RBD_FLAG_VLAN_TAG)) { 179*1408Srandyf mp = allocb(BGE_HEADROOM + len + VLAN_TAGSZ, 0); 180*1408Srandyf } else { 181*1408Srandyf #endif 182*1408Srandyf 183*1408Srandyf mp = allocb(BGE_HEADROOM + len, 0); 184*1408Srandyf #ifdef BGE_IPMI_ASF 185*1408Srandyf } 186*1408Srandyf #endif 1871369Sdduvall if (mp == NULL) { 1881369Sdduvall /* Nothing to do but drop the packet */ 1891369Sdduvall goto refill; 1901369Sdduvall } 1911369Sdduvall 1921369Sdduvall /* 1931369Sdduvall * Sync the data and copy it to the STREAMS buffer. 1941369Sdduvall */ 1951369Sdduvall DMA_SYNC(srbdp->pbuf, DDI_DMA_SYNC_FORKERNEL); 196*1408Srandyf #ifdef BGE_IPMI_ASF 197*1408Srandyf if (bgep->asf_enabled && (hw_rbd.flags & RBD_FLAG_VLAN_TAG)) { 198*1408Srandyf /* 199*1408Srandyf * As VLAN tag has been stripped from incoming packet in ASF 200*1408Srandyf * scenario, we insert it into this packet again. 201*1408Srandyf */ 202*1408Srandyf struct ether_vlan_header *ehp; 203*1408Srandyf mp->b_rptr = dp = mp->b_rptr + BGE_HEADROOM - VLAN_TAGSZ; 204*1408Srandyf bcopy(DMA_VPTR(srbdp->pbuf), dp, 2 * ETHERADDRL); 205*1408Srandyf ehp = (struct ether_vlan_header *)dp; 206*1408Srandyf ehp->ether_tpid = ntohs(VLAN_TPID); 207*1408Srandyf ehp->ether_tci = ntohs(hw_rbd.vlan_tci); 208*1408Srandyf bcopy(((uchar_t *)(DMA_VPTR(srbdp->pbuf))) + 2 * ETHERADDRL, 209*1408Srandyf dp + 2 * ETHERADDRL + VLAN_TAGSZ, 210*1408Srandyf len - 2 * ETHERADDRL); 211*1408Srandyf } else { 212*1408Srandyf #endif 213*1408Srandyf mp->b_rptr = dp = mp->b_rptr + BGE_HEADROOM; 214*1408Srandyf bcopy(DMA_VPTR(srbdp->pbuf), dp, len); 215*1408Srandyf #ifdef BGE_IPMI_ASF 216*1408Srandyf } 217*1408Srandyf 218*1408Srandyf if (bgep->asf_enabled && (hw_rbd.flags & RBD_FLAG_VLAN_TAG)) { 219*1408Srandyf mp->b_wptr = dp + len + VLAN_TAGSZ - ETHERFCSL; 220*1408Srandyf } else 221*1408Srandyf #endif 222*1408Srandyf mp->b_wptr = dp + len - ETHERFCSL; 2231369Sdduvall 2241369Sdduvall /* 2251369Sdduvall * Special check for one specific type of data corruption; 2261369Sdduvall * in a good packet, the first 8 bytes are *very* unlikely 2271369Sdduvall * to be the same as the second 8 bytes ... but we let the 2281369Sdduvall * packet through just in case. 2291369Sdduvall */ 2301369Sdduvall if (bcmp(dp, dp+8, 8) == 0) 2311369Sdduvall BGE_PKTDUMP((bgep, &hw_rbd, srbdp, "stuttered packet?")); 2321369Sdduvall 2331369Sdduvall pflags = 0; 2341369Sdduvall if (hw_rbd.flags & RBD_FLAG_TCP_UDP_CHECKSUM) 2351369Sdduvall pflags |= HCK_FULLCKSUM; 2361369Sdduvall if (hw_rbd.flags & RBD_FLAG_IP_CHECKSUM) 2371369Sdduvall pflags |= HCK_IPV4_HDRCKSUM; 2381369Sdduvall if (pflags != 0) 2391369Sdduvall (void) hcksum_assoc(mp, NULL, NULL, 0, 0, 0, 2401369Sdduvall hw_rbd.tcp_udp_cksum, pflags, 0); 2411369Sdduvall 2421369Sdduvall refill: 2431369Sdduvall /* 2441369Sdduvall * Replace the buffer in the ring it came from ... 2451369Sdduvall */ 2461369Sdduvall bge_refill(bgep, brp, srbdp); 2471369Sdduvall return (mp); 2481369Sdduvall 2491369Sdduvall error: 2501369Sdduvall /* 2511369Sdduvall * We come here if the integrity of the ring descriptors 2521369Sdduvall * (rather than merely packet data) appears corrupted. 2531369Sdduvall * The factotum will attempt to reset-and-recover. 2541369Sdduvall */ 2551369Sdduvall mutex_enter(bgep->genlock); 2561369Sdduvall bgep->bge_chip_state = BGE_CHIP_ERROR; 2571369Sdduvall mutex_exit(bgep->genlock); 2581369Sdduvall return (NULL); 2591369Sdduvall } 2601369Sdduvall 2611369Sdduvall /* 2621369Sdduvall * Accept the packets received in the specified ring up to 2631369Sdduvall * (but not including) the producer index in the status block. 2641369Sdduvall * 2651369Sdduvall * Returns a chain of mblks containing the received data, to be 2661369Sdduvall * passed up to gld_recv() (we can't call gld_recv() from here, 2671369Sdduvall * 'cos we're holding the per-ring receive lock at this point). 2681369Sdduvall * 2691369Sdduvall * This function must advance (rrp->rx_next) and write it back to 2701369Sdduvall * the chip to indicate the packets it has accepted from the ring. 2711369Sdduvall */ 2721369Sdduvall static mblk_t *bge_receive_ring(bge_t *bgep, recv_ring_t *rrp); 2731369Sdduvall #pragma inline(bge_receive_ring) 2741369Sdduvall 2751369Sdduvall static mblk_t * 2761369Sdduvall bge_receive_ring(bge_t *bgep, recv_ring_t *rrp) 2771369Sdduvall { 2781369Sdduvall bge_rbd_t *hw_rbd_p; 2791369Sdduvall uint64_t slot; 2801369Sdduvall mblk_t *head; 2811369Sdduvall mblk_t **tail; 2821369Sdduvall mblk_t *mp; 2831369Sdduvall 2841369Sdduvall ASSERT(mutex_owned(rrp->rx_lock)); 2851369Sdduvall 2861369Sdduvall /* 2871369Sdduvall * Sync (all) the receive ring descriptors 2881369Sdduvall * before accepting the packets they describe 2891369Sdduvall */ 2901369Sdduvall DMA_SYNC(rrp->desc, DDI_DMA_SYNC_FORKERNEL); 2911369Sdduvall hw_rbd_p = DMA_VPTR(rrp->desc); 2921369Sdduvall head = NULL; 2931369Sdduvall tail = &head; 2941369Sdduvall slot = rrp->rx_next; 2951369Sdduvall 2961369Sdduvall while (slot != *rrp->prod_index_p) { /* Note: volatile */ 2971369Sdduvall if ((mp = bge_receive_packet(bgep, &hw_rbd_p[slot])) != NULL) { 2981369Sdduvall *tail = mp; 2991369Sdduvall tail = &mp->b_next; 3001369Sdduvall } 3011369Sdduvall rrp->rx_next = slot = NEXT(slot, rrp->desc.nslots); 3021369Sdduvall } 3031369Sdduvall 3041369Sdduvall bge_mbx_put(bgep, rrp->chip_mbx_reg, rrp->rx_next); 3051369Sdduvall return (head); 3061369Sdduvall } 3071369Sdduvall 3081369Sdduvall /* 3091369Sdduvall * Receive all packets in all rings. 3101369Sdduvall * 3111369Sdduvall * To give priority to low-numbered rings, whenever we have received any 3121369Sdduvall * packets in any ring except 0, we restart scanning again from ring 0. 3131369Sdduvall * Thus, for example, if rings 0, 3, and 10 are carrying traffic, the 3141369Sdduvall * pattern of receives might go 0, 3, 10, 3, 0, 10, 0: 3151369Sdduvall * 3161369Sdduvall * 0 found some - receive them 3171369Sdduvall * 1..2 none found 3181369Sdduvall * 3 found some - receive them and restart scan 3191369Sdduvall * 0..9 none found 3201369Sdduvall * 10 found some - receive them and restart scan 3211369Sdduvall * 0..2 none found 3221369Sdduvall * 3 found some more - receive them and restart scan 3231369Sdduvall * 0 found some more - receive them 3241369Sdduvall * 1..9 none found 3251369Sdduvall * 10 found some more - receive them and restart scan 3261369Sdduvall * 0 found some more - receive them 3271369Sdduvall * 1..15 none found 3281369Sdduvall * 3291369Sdduvall * The routine returns only when a complete scan has been performed 3301369Sdduvall * without finding any packets to receive. 3311369Sdduvall * 3321369Sdduvall * Note that driver-defined locks may *NOT* be held across calls 3331369Sdduvall * to gld_recv(). 3341369Sdduvall * 3351369Sdduvall * Note: the expression (BGE_RECV_RINGS_USED > 1), yields a compile-time 3361369Sdduvall * constant and allows the compiler to optimise away the outer do-loop 3371369Sdduvall * if only one receive ring is being used. 3381369Sdduvall */ 3391369Sdduvall void bge_receive(bge_t *bgep, bge_status_t *bsp); 3401369Sdduvall #pragma no_inline(bge_receive) 3411369Sdduvall 3421369Sdduvall void 3431369Sdduvall bge_receive(bge_t *bgep, bge_status_t *bsp) 3441369Sdduvall { 3451369Sdduvall recv_ring_t *rrp; 3461369Sdduvall uint64_t ring; 3471369Sdduvall uint64_t rx_rings = bgep->chipid.rx_rings; 3481369Sdduvall mblk_t *mp; 3491369Sdduvall 3501369Sdduvall restart: 3511369Sdduvall ring = 0; 3521369Sdduvall rrp = &bgep->recv[ring]; 3531369Sdduvall do { 3541369Sdduvall /* 3551369Sdduvall * For each ring, (rrp->prod_index_p) points to the 3561369Sdduvall * proper index within the status block (which has 3571369Sdduvall * already been sync'd by the caller) 3581369Sdduvall */ 3591369Sdduvall ASSERT(rrp->prod_index_p == RECV_INDEX_P(bsp, ring)); 3601369Sdduvall 3611369Sdduvall if (*rrp->prod_index_p == rrp->rx_next) 3621369Sdduvall continue; /* no packets */ 3631369Sdduvall if (mutex_tryenter(rrp->rx_lock) == 0) 3641369Sdduvall continue; /* already in process */ 3651369Sdduvall mp = bge_receive_ring(bgep, rrp); 3661369Sdduvall mutex_exit(rrp->rx_lock); 3671369Sdduvall 3681369Sdduvall if (mp != NULL) { 3691369Sdduvall mac_rx(bgep->macp, rrp->handle, mp); 3701369Sdduvall 3711369Sdduvall /* 3721369Sdduvall * Restart from ring 0, if the driver is compiled 3731369Sdduvall * with multiple rings and we're not on ring 0 now 3741369Sdduvall */ 3751369Sdduvall if (rx_rings > 1 && ring > 0) 3761369Sdduvall goto restart; 3771369Sdduvall } 3781369Sdduvall 3791369Sdduvall /* 3801369Sdduvall * Loop over all rings (if there *are* multiple rings) 3811369Sdduvall */ 3821369Sdduvall } while (++rrp, ++ring < rx_rings); 3831369Sdduvall } 384