xref: /onnv-gate/usr/src/uts/sun4v/io/vnet_common.c (revision 6845:7eabb77ee113)
15462Swentaoy /*
25462Swentaoy  * CDDL HEADER START
35462Swentaoy  *
45462Swentaoy  * The contents of this file are subject to the terms of the
55462Swentaoy  * Common Development and Distribution License (the "License").
65462Swentaoy  * You may not use this file except in compliance with the License.
75462Swentaoy  *
85462Swentaoy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95462Swentaoy  * or http://www.opensolaris.org/os/licensing.
105462Swentaoy  * See the License for the specific language governing permissions
115462Swentaoy  * and limitations under the License.
125462Swentaoy  *
135462Swentaoy  * When distributing Covered Code, include this CDDL HEADER in each
145462Swentaoy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155462Swentaoy  * If applicable, add the following below this CDDL HEADER, with the
165462Swentaoy  * fields enclosed by brackets "[]" replaced with your own identifying
175462Swentaoy  * information: Portions Copyright [yyyy] [name of copyright owner]
185462Swentaoy  *
195462Swentaoy  * CDDL HEADER END
205462Swentaoy  */
215462Swentaoy 
225462Swentaoy /*
236419Ssb155480  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
245462Swentaoy  * Use is subject to license terms.
255462Swentaoy  */
265462Swentaoy 
275462Swentaoy #pragma ident	"%Z%%M%	%I%	%E% SMI"
285462Swentaoy 
295462Swentaoy #include <sys/types.h>
305462Swentaoy #include <sys/ethernet.h>
315462Swentaoy #include <sys/vnet_common.h>
326419Ssb155480 #include <sys/vlan.h>
336419Ssb155480 #include <sys/stream.h>
346419Ssb155480 #include <sys/strsun.h>
356419Ssb155480 #include <sys/byteorder.h>
365462Swentaoy 
375462Swentaoy /* convert mac address from string to uint64_t */
385462Swentaoy uint64_t
vnet_macaddr_strtoul(const uint8_t * macaddr)395462Swentaoy vnet_macaddr_strtoul(const uint8_t *macaddr)
405462Swentaoy {
415462Swentaoy 	uint64_t val = 0;
425462Swentaoy 	int i;
435462Swentaoy 
445462Swentaoy 	for (i = 0; i < ETHERADDRL; i++) {
455462Swentaoy 		val <<= 8;
465462Swentaoy 		val |= macaddr[i];
475462Swentaoy 	}
485462Swentaoy 
495462Swentaoy 	return (val);
505462Swentaoy }
515462Swentaoy 
525462Swentaoy /* convert mac address from uint64_t to string */
535462Swentaoy void
vnet_macaddr_ultostr(uint64_t val,uint8_t * macaddr)545462Swentaoy vnet_macaddr_ultostr(uint64_t val, uint8_t *macaddr)
555462Swentaoy {
565462Swentaoy 	int i;
575462Swentaoy 	uint64_t value;
585462Swentaoy 
595462Swentaoy 	value = val;
605462Swentaoy 	for (i = ETHERADDRL - 1; i >= 0; i--) {
615462Swentaoy 		macaddr[i] = value & 0xFF;
625462Swentaoy 		value >>= 8;
635462Swentaoy 	}
645462Swentaoy }
656419Ssb155480 
666419Ssb155480 mblk_t *
vnet_vlan_insert_tag(mblk_t * mp,uint16_t vid)676419Ssb155480 vnet_vlan_insert_tag(mblk_t *mp, uint16_t vid)
686419Ssb155480 {
696419Ssb155480 	struct ether_vlan_header	*evhp;
706419Ssb155480 	mblk_t				*nmp;
716419Ssb155480 	size_t				n;
726419Ssb155480 	uint_t				pri = 0;
736419Ssb155480 
746419Ssb155480 	if (DB_REF(mp) == 1 && MBLKHEAD(mp) >= VLAN_TAGSZ) {
756419Ssb155480 
766419Ssb155480 		/* mblk has space to insert tag */
776419Ssb155480 
786419Ssb155480 		/*
796419Ssb155480 		 * move src and dst mac addrs in the header back by VLAN_TAGSZ.
806419Ssb155480 		 */
816419Ssb155480 		ovbcopy(mp->b_rptr, mp->b_rptr - VLAN_TAGSZ, 2 * ETHERADDRL);
826419Ssb155480 		mp->b_rptr -= VLAN_TAGSZ;
836419Ssb155480 
846419Ssb155480 		/* now insert tpid and tci */
856419Ssb155480 		evhp = (struct ether_vlan_header *)mp->b_rptr;
866419Ssb155480 		evhp->ether_tpid = htons(ETHERTYPE_VLAN);
876419Ssb155480 		evhp->ether_tci = htons(VLAN_TCI(pri, ETHER_CFI, vid));
886419Ssb155480 
896419Ssb155480 	} else { /* no space in the mblk for tag */
906419Ssb155480 
916419Ssb155480 		/*
926419Ssb155480 		 * allocate a mblk to create a new frame hdr with the tag
936419Ssb155480 		 */
946419Ssb155480 		nmp = allocb(sizeof (struct  ether_vlan_header),
956419Ssb155480 		    BPRI_MED);
966419Ssb155480 		if (nmp == NULL) {
976419Ssb155480 			freemsg(mp);
986419Ssb155480 			return (NULL);
996419Ssb155480 		}
1006419Ssb155480 
1016419Ssb155480 		/*
1026419Ssb155480 		 * copy the src and dst mac addrs in the header to the new mblk
1036419Ssb155480 		 */
1046419Ssb155480 		n = 2 * ETHERADDRL;
1056419Ssb155480 		bcopy(mp->b_rptr, nmp->b_rptr, n);
1066419Ssb155480 
1076419Ssb155480 		/* initialize the vlan tag in the new mblk */
1086419Ssb155480 		evhp = (struct ether_vlan_header *)nmp->b_rptr;
1096419Ssb155480 		evhp->ether_tpid = htons(ETHERTYPE_VLAN);
1106419Ssb155480 		evhp->ether_tci = htons(VLAN_TCI(pri, ETHER_CFI, vid));
1116419Ssb155480 
1126419Ssb155480 		/* copy ethertype to new mblk */
1136419Ssb155480 		bcopy(mp->b_rptr + n, nmp->b_rptr + n + VLAN_TAGSZ,
1146419Ssb155480 		    sizeof (evhp->ether_type));
1156419Ssb155480 
1166419Ssb155480 		/* skip over the header in the original mblk */
1176419Ssb155480 		mp->b_rptr += sizeof (struct ether_header);
1186419Ssb155480 
1196419Ssb155480 		/* fix the end of frame header in the new mblk */
1206419Ssb155480 		nmp->b_wptr += sizeof (struct ether_vlan_header);
1216419Ssb155480 
1226419Ssb155480 		/*
1236419Ssb155480 		 * now link the new mblk which contains just the frame
1246419Ssb155480 		 * header with the original mblk which contains rest of
1256419Ssb155480 		 * the frame.
1266419Ssb155480 		 */
1276419Ssb155480 		nmp->b_cont = mp;
1286419Ssb155480 		mp = nmp;
1296419Ssb155480 
1306419Ssb155480 	}
1316419Ssb155480 
1326419Ssb155480 	return (mp);
1336419Ssb155480 }
1346419Ssb155480 
1356419Ssb155480 mblk_t *
vnet_vlan_remove_tag(mblk_t * mp)1366419Ssb155480 vnet_vlan_remove_tag(mblk_t *mp)
1376419Ssb155480 {
1386419Ssb155480 	size_t				n;
1396419Ssb155480 	mblk_t				*nmp;
1406419Ssb155480 
1416419Ssb155480 	if (DB_REF(mp) == 1) { /* mblk can be modified to untag(not shared) */
1426419Ssb155480 
1436419Ssb155480 		/* move src & dst addrs in the header forward by VLAN_TAGSZ */
1446419Ssb155480 		ovbcopy(mp->b_rptr, mp->b_rptr + VLAN_TAGSZ, 2 * ETHERADDRL);
1456419Ssb155480 		mp->b_rptr += VLAN_TAGSZ;
1466419Ssb155480 
1476419Ssb155480 	} else {
1486419Ssb155480 
1496419Ssb155480 		/* allocate a new header */
1506419Ssb155480 		nmp = allocb(sizeof (struct  ether_header), BPRI_MED);
1516419Ssb155480 		if (nmp == NULL) {
1526419Ssb155480 			freemsg(mp);
1536419Ssb155480 			return (NULL);
1546419Ssb155480 		}
1556419Ssb155480 
1566419Ssb155480 		/*
1576419Ssb155480 		 * copy the src and dst mac addrs in the header to the new mblk
1586419Ssb155480 		 */
1596419Ssb155480 		n = 2 * ETHERADDRL;
1606419Ssb155480 		bcopy(mp->b_rptr, nmp->b_rptr, n);
1616419Ssb155480 
1626419Ssb155480 		/* skip over vlan tag and copy ethertype to new mblk */
1636419Ssb155480 		bcopy(mp->b_rptr + n + VLAN_TAGSZ, nmp->b_rptr + n,
1646419Ssb155480 		    sizeof (uint16_t));
1656419Ssb155480 
1666419Ssb155480 		/* skip over the header in the original mblk */
1676419Ssb155480 		mp->b_rptr += sizeof (struct ether_vlan_header);
1686419Ssb155480 
1696419Ssb155480 		/* fix the end of frame header in the new mblk */
1706419Ssb155480 		nmp->b_wptr += sizeof (struct ether_header);
1716419Ssb155480 
1726419Ssb155480 		/*
1736419Ssb155480 		 * now link the new mblk which contains the frame header
1746419Ssb155480 		 * without vlan tag and the original mblk which contains rest
1756419Ssb155480 		 * of the frame.
1766419Ssb155480 		 */
1776419Ssb155480 		nmp->b_cont = mp;
1786419Ssb155480 		mp = nmp;
1796419Ssb155480 
1806419Ssb155480 	}
1816419Ssb155480 
1826419Ssb155480 	return (mp);
1836419Ssb155480 }
184*6845Sha137994 
185*6845Sha137994 int
vnet_dring_entry_copy(vnet_public_desc_t * from,vnet_public_desc_t * to,uint8_t mtype,ldc_dring_handle_t handle,uint64_t start,uint64_t stop)186*6845Sha137994 vnet_dring_entry_copy(vnet_public_desc_t *from, vnet_public_desc_t *to,
187*6845Sha137994     uint8_t mtype, ldc_dring_handle_t handle, uint64_t start, uint64_t stop)
188*6845Sha137994 {
189*6845Sha137994 	int rv;
190*6845Sha137994 	on_trap_data_t otd;
191*6845Sha137994 
192*6845Sha137994 	if ((rv = VIO_DRING_ACQUIRE(&otd, mtype, handle, start, stop)) != 0)
193*6845Sha137994 		return (rv);
194*6845Sha137994 
195*6845Sha137994 	*to = *from;
196*6845Sha137994 
197*6845Sha137994 	rv = VIO_DRING_RELEASE_NOCOPYOUT(mtype);
198*6845Sha137994 
199*6845Sha137994 	return (rv);
200*6845Sha137994 }
201*6845Sha137994 
202*6845Sha137994 int
vnet_dring_entry_set_dstate(vnet_public_desc_t * descp,uint8_t mtype,ldc_dring_handle_t handle,uint64_t start,uint64_t stop,uint8_t dstate)203*6845Sha137994 vnet_dring_entry_set_dstate(vnet_public_desc_t *descp, uint8_t mtype,
204*6845Sha137994     ldc_dring_handle_t handle, uint64_t start, uint64_t stop, uint8_t dstate)
205*6845Sha137994 {
206*6845Sha137994 	int rv;
207*6845Sha137994 	on_trap_data_t otd;
208*6845Sha137994 
209*6845Sha137994 	rv = VIO_DRING_ACQUIRE_NOCOPYIN(&otd, mtype);
210*6845Sha137994 	if (rv)
211*6845Sha137994 		return (rv);
212*6845Sha137994 
213*6845Sha137994 	descp->hdr.dstate = dstate;
214*6845Sha137994 
215*6845Sha137994 	rv = VIO_DRING_RELEASE(mtype, handle, start, stop);
216*6845Sha137994 
217*6845Sha137994 	return (rv);
218*6845Sha137994 }
219