xref: /onnv-gate/usr/src/uts/common/io/mac/plugins/mac_ib.c (revision 6990:d24af98bb8ea)
15766Sgg161487 /*
25766Sgg161487  * CDDL HEADER START
35766Sgg161487  *
45766Sgg161487  * The contents of this file are subject to the terms of the
55766Sgg161487  * Common Development and Distribution License (the "License").
65766Sgg161487  * You may not use this file except in compliance with the License.
75766Sgg161487  *
85766Sgg161487  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95766Sgg161487  * or http://www.opensolaris.org/os/licensing.
105766Sgg161487  * See the License for the specific language governing permissions
115766Sgg161487  * and limitations under the License.
125766Sgg161487  *
135766Sgg161487  * When distributing Covered Code, include this CDDL HEADER in each
145766Sgg161487  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155766Sgg161487  * If applicable, add the following below this CDDL HEADER, with the
165766Sgg161487  * fields enclosed by brackets "[]" replaced with your own identifying
175766Sgg161487  * information: Portions Copyright [yyyy] [name of copyright owner]
185766Sgg161487  *
195766Sgg161487  * CDDL HEADER END
205766Sgg161487  */
215766Sgg161487 /*
225766Sgg161487  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235766Sgg161487  * Use is subject to license terms.
245766Sgg161487  */
255766Sgg161487 
265766Sgg161487 #pragma ident	"%Z%%M%	%I%	%E% SMI"
275766Sgg161487 
285766Sgg161487 /*
295766Sgg161487  * DL_IB MAC Type plugin for the Nemo mac module
305766Sgg161487  */
315766Sgg161487 
325766Sgg161487 #include <sys/types.h>
335766Sgg161487 #include <sys/modctl.h>
345766Sgg161487 #include <sys/dlpi.h>
355766Sgg161487 #include <sys/ib/clients/ibd/ibd.h>
365766Sgg161487 #include <sys/mac.h>
375766Sgg161487 #include <sys/mac_ib.h>
385766Sgg161487 #include <sys/dls.h>
395766Sgg161487 #include <sys/byteorder.h>
405766Sgg161487 #include <sys/strsun.h>
415766Sgg161487 #include <inet/common.h>
425766Sgg161487 #include <sys/note.h>
435766Sgg161487 
445766Sgg161487 static uint8_t ib_brdcst[] = { 0x00, 0xff, 0xff, 0xff,
455766Sgg161487     0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
465766Sgg161487     0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff };
475766Sgg161487 
485766Sgg161487 static struct modlmisc mac_ib_modlmisc = {
495766Sgg161487 	&mod_miscops,
505766Sgg161487 	"Infiniband MAC Type plugin 1.0"
515766Sgg161487 };
525766Sgg161487 
535766Sgg161487 static struct modlinkage mac_ib_modlinkage = {
545766Sgg161487 	MODREV_1,
555766Sgg161487 	&mac_ib_modlmisc,
565766Sgg161487 	NULL
575766Sgg161487 };
585766Sgg161487 
595766Sgg161487 static mactype_ops_t mac_ib_type_ops;
605766Sgg161487 
615766Sgg161487 int
_init(void)625766Sgg161487 _init(void)
635766Sgg161487 {
645766Sgg161487 	mactype_register_t *mtrp;
655766Sgg161487 	int	err;
665766Sgg161487 
675766Sgg161487 	if ((mtrp = mactype_alloc(MACTYPE_VERSION)) == NULL)
685766Sgg161487 		return (ENOTSUP);
695766Sgg161487 	mtrp->mtr_ident = MAC_PLUGIN_IDENT_IB;
705766Sgg161487 	mtrp->mtr_ops = &mac_ib_type_ops;
715766Sgg161487 	mtrp->mtr_mactype = DL_IB;
725895Syz147064 	mtrp->mtr_nativetype = DL_IB;
735766Sgg161487 	mtrp->mtr_addrlen = IPOIB_ADDRL;
745766Sgg161487 	mtrp->mtr_brdcst_addr = ib_brdcst;
755766Sgg161487 
765766Sgg161487 	/*
775766Sgg161487 	 * So far, generic stats maintained by GLDv3 are sufficient for IB.
785766Sgg161487 	 */
795766Sgg161487 	mtrp->mtr_stats = NULL;
805766Sgg161487 	mtrp->mtr_statcount = 0;
815766Sgg161487 	if ((err = mactype_register(mtrp)) == 0) {
825766Sgg161487 		if ((err = mod_install(&mac_ib_modlinkage)) != 0)
835766Sgg161487 			(void) mactype_unregister(MAC_PLUGIN_IDENT_IB);
845766Sgg161487 	}
855766Sgg161487 	mactype_free(mtrp);
865766Sgg161487 	return (err);
875766Sgg161487 }
885766Sgg161487 
895766Sgg161487 int
_fini(void)905766Sgg161487 _fini(void)
915766Sgg161487 {
925766Sgg161487 	int	err;
935766Sgg161487 
945766Sgg161487 	if ((err = mactype_unregister(MAC_PLUGIN_IDENT_IB)) != 0)
955766Sgg161487 		return (err);
965766Sgg161487 	return (mod_remove(&mac_ib_modlinkage));
975766Sgg161487 }
985766Sgg161487 
995766Sgg161487 int
_info(struct modinfo * modinfop)1005766Sgg161487 _info(struct modinfo *modinfop)
1015766Sgg161487 {
1025766Sgg161487 	return (mod_info(&mac_ib_modlinkage, modinfop));
1035766Sgg161487 }
1045766Sgg161487 
1055766Sgg161487 /*
1065766Sgg161487  * MAC Type plugin operations
1075766Sgg161487  */
1085766Sgg161487 
1095766Sgg161487 /* ARGSUSED */
1105766Sgg161487 int
mac_ib_unicst_verify(const void * addr,void * mac_pdata)1115766Sgg161487 mac_ib_unicst_verify(const void *addr, void *mac_pdata)
1125766Sgg161487 {
1135766Sgg161487 	ipoib_mac_t *ibaddr = (ipoib_mac_t *)addr;
1145766Sgg161487 
1155766Sgg161487 	/*
1165766Sgg161487 	 * The address must not be a multicast address.
1175766Sgg161487 	 */
1185766Sgg161487 	return (ntohl(ibaddr->ipoib_qpn) == IB_MC_QPN ? EINVAL : 0);
1195766Sgg161487 }
1205766Sgg161487 
1215766Sgg161487 int
mac_ib_multicst_verify(const void * addr,void * mac_pdata)1225766Sgg161487 mac_ib_multicst_verify(const void *addr, void *mac_pdata)
1235766Sgg161487 {
1245766Sgg161487 	ipoib_mac_t *ibaddr = (ipoib_mac_t *)addr;
1255766Sgg161487 	uint8_t *p_gid = (uint8_t *)addr + sizeof (ipoib_mac_t)
1265766Sgg161487 	    - MAC_IB_GID_SIZE;
1275766Sgg161487 	uint32_t bcst_gid[3] = { 0x0, 0x0, MAC_IB_BROADCAST_GID };
1285766Sgg161487 
1295766Sgg161487 	_NOTE(ARGUNUSED(mac_pdata));
1305766Sgg161487 
1315766Sgg161487 	/*
1325766Sgg161487 	 * The address must be a multicast address.
1335766Sgg161487 	 */
1345766Sgg161487 	if ((ntohl(ibaddr->ipoib_qpn) & IB_QPN_MASK) != IB_MC_QPN)
1355766Sgg161487 		return (EINVAL);
1365766Sgg161487 
1375766Sgg161487 	/*
1385766Sgg161487 	 * The address must not be the broadcast address.
1395766Sgg161487 	 */
1405766Sgg161487 	if (bcmp(p_gid, (uint8_t *)bcst_gid + sizeof (bcst_gid) -
1415766Sgg161487 	    MAC_IB_GID_SIZE, MAC_IB_GID_SIZE) == 0)
1425766Sgg161487 		return (EINVAL);
1435766Sgg161487 
1445766Sgg161487 	return (0);
1455766Sgg161487 }
1465766Sgg161487 
1475766Sgg161487 /*
1485766Sgg161487  * Check the legality of a SAP value. The following values are
1495766Sgg161487  * allowed, as specified by PSARC 2003/150:
1505766Sgg161487  *
1515766Sgg161487  * min-ethertype-sap (256).. EtherType max(65535)	ethertype semantics
1525766Sgg161487  *        (0)             .. max-802-sap(255)		IEEE 802 semantics
1535766Sgg161487  */
1545766Sgg161487 boolean_t
mac_ib_sap_verify(uint32_t sap,uint32_t * bind_sap,void * mac_pdata)1555766Sgg161487 mac_ib_sap_verify(uint32_t sap, uint32_t *bind_sap, void *mac_pdata)
1565766Sgg161487 {
1575766Sgg161487 	_NOTE(ARGUNUSED(mac_pdata));
1585766Sgg161487 
1595766Sgg161487 	if (sap > MAC_IB_MAX_802_SAP && sap <= MAC_IB_ETHERTYPE_MAX) {
1605766Sgg161487 		if (bind_sap != NULL)
1615766Sgg161487 			*bind_sap = sap;
1625766Sgg161487 		return (B_TRUE);
1635766Sgg161487 	}
1645766Sgg161487 
1655766Sgg161487 	if (sap <= MAC_IB_MAX_802_SAP) {
1665766Sgg161487 		if (bind_sap != NULL)
1675766Sgg161487 			*bind_sap = DLS_SAP_LLC;
1685766Sgg161487 		return (B_TRUE);
1695766Sgg161487 	}
1705766Sgg161487 
1715766Sgg161487 	return (B_FALSE);
1725766Sgg161487 }
1735766Sgg161487 
1745766Sgg161487 /* ARGSUSED */
1755766Sgg161487 mblk_t *
mac_ib_header(const void * saddr,const void * daddr,uint32_t sap,void * mac_pdata,mblk_t * payload,size_t extra_len)1765766Sgg161487 mac_ib_header(const void *saddr, const void *daddr, uint32_t sap,
1775766Sgg161487     void *mac_pdata, mblk_t *payload, size_t extra_len)
1785766Sgg161487 {
1795766Sgg161487 	ib_header_info_t	*ibhp;
1805766Sgg161487 	mblk_t			*mp;
1815766Sgg161487 
1825766Sgg161487 	if (!mac_ib_sap_verify(sap, NULL, NULL))
1835766Sgg161487 		return (NULL);
1845766Sgg161487 
1855766Sgg161487 	mp = allocb(sizeof (ib_header_info_t) + extra_len, BPRI_HI);
1865766Sgg161487 	if (mp == NULL)
1875766Sgg161487 		return (NULL);
1885766Sgg161487 
189*6990Sgd78059 	ibhp = (void *)mp->b_rptr;
1905766Sgg161487 	ibhp->ipib_rhdr.ipoib_type = htons(sap);
1915766Sgg161487 	ibhp->ipib_rhdr.ipoib_mbz = 0;
1925766Sgg161487 	bcopy(daddr, &ibhp->ib_dst, IPOIB_ADDRL);
1935766Sgg161487 	mp->b_wptr += sizeof (ib_header_info_t);
1945766Sgg161487 	return (mp);
1955766Sgg161487 }
1965766Sgg161487 
1975766Sgg161487 int
mac_ib_header_info(mblk_t * mp,void * mac_pdata,mac_header_info_t * hdr_info)1985766Sgg161487 mac_ib_header_info(mblk_t *mp, void *mac_pdata, mac_header_info_t *hdr_info)
1995766Sgg161487 {
2005766Sgg161487 	ib_header_info_t	*ibhp;
2015766Sgg161487 	uint16_t	sap;
2025766Sgg161487 
2035766Sgg161487 	if (MBLKL(mp) < sizeof (ib_header_info_t))
2045766Sgg161487 		return (EINVAL);
2055766Sgg161487 
206*6990Sgd78059 	ibhp = (void *)mp->b_rptr;
2075766Sgg161487 
2085766Sgg161487 	hdr_info->mhi_hdrsize = sizeof (ib_header_info_t);
2095766Sgg161487 	hdr_info->mhi_daddr = (const uint8_t *)&(ibhp->ib_dst);
2105766Sgg161487 	if (ibhp->ib_grh.ipoib_vertcflow != 0)
2115766Sgg161487 		hdr_info->mhi_saddr = (const uint8_t *)&(ibhp->ib_src);
2125766Sgg161487 	else
2135766Sgg161487 		hdr_info->mhi_saddr = NULL;
2145766Sgg161487 
2155766Sgg161487 	if (mac_ib_unicst_verify(hdr_info->mhi_daddr, mac_pdata) == 0) {
2165766Sgg161487 		hdr_info->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
2175766Sgg161487 	} else if (mac_ib_multicst_verify(hdr_info->mhi_daddr,
2185766Sgg161487 	    mac_pdata) == 0) {
2195766Sgg161487 		hdr_info->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
2205766Sgg161487 	} else {
2215766Sgg161487 		hdr_info->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
2225766Sgg161487 	}
2235766Sgg161487 
2245766Sgg161487 	sap = ntohs(ibhp->ipib_rhdr.ipoib_type);
2255766Sgg161487 	hdr_info->mhi_origsap = hdr_info->mhi_bindsap = sap;
2265766Sgg161487 	hdr_info->mhi_pktsize = 0;
2275766Sgg161487 
2285766Sgg161487 	return (0);
2295766Sgg161487 }
2305766Sgg161487 
2315766Sgg161487 /*
2325766Sgg161487  * Take the provided `mp' (which is expected to have a header "dst + type"),
2335766Sgg161487  * and return a pointer to an mblk_t with a header "GRH + type".
2345766Sgg161487  * If the conversion cannot be performed, return NULL.
2355766Sgg161487  */
2365766Sgg161487 static mblk_t *
mac_ib_header_cook(mblk_t * mp,void * pdata)2375766Sgg161487 mac_ib_header_cook(mblk_t *mp, void *pdata)
2385766Sgg161487 {
2395766Sgg161487 	ipoib_ptxhdr_t	*orig_hp;
2405766Sgg161487 	mblk_t			*llmp;
2415766Sgg161487 
2425766Sgg161487 	if (MBLKL(mp) < sizeof (ipoib_ptxhdr_t))
2435766Sgg161487 		return (NULL);
2445766Sgg161487 
245*6990Sgd78059 	orig_hp = (void *)mp->b_rptr;
2465766Sgg161487 	llmp = mac_ib_header(NULL, &orig_hp->ipoib_dest,
2475766Sgg161487 	    ntohs(orig_hp->ipoib_rhdr.ipoib_type), pdata, NULL, 0);
2485766Sgg161487 	if (llmp == NULL)
2495766Sgg161487 		return (NULL);
2505766Sgg161487 
2515766Sgg161487 	/*
2525766Sgg161487 	 * The plugin framework guarantees that we have the only reference
2535766Sgg161487 	 * to the mblk_t, so we can safely modify it.
2545766Sgg161487 	 */
2555766Sgg161487 	ASSERT(DB_REF(mp) == 1);
2565766Sgg161487 	mp->b_rptr += sizeof (ipoib_ptxhdr_t);
2575766Sgg161487 	llmp->b_cont = mp;
2585766Sgg161487 	return (llmp);
2595766Sgg161487 }
2605766Sgg161487 
2615766Sgg161487 /*
2625766Sgg161487  * Take the provided `mp' (which is expected to have a header "GRH + type"),
2635766Sgg161487  * and return a pointer to an mblk_t with a header "type". If the conversion
2645766Sgg161487  * cannot be performed, return NULL.
2655766Sgg161487  */
2665766Sgg161487 static mblk_t *
mac_ib_header_uncook(mblk_t * mp,void * pdata)2675766Sgg161487 mac_ib_header_uncook(mblk_t *mp, void *pdata)
2685766Sgg161487 {
2695766Sgg161487 	_NOTE(ARGUNUSED(pdata));
2705766Sgg161487 
2715766Sgg161487 	/*
2725766Sgg161487 	 * The plugin framework guarantees that we have the only reference to
2735766Sgg161487 	 * the mblk_t and the underlying dblk_t, so we can safely modify it.
2745766Sgg161487 	 */
2755766Sgg161487 	ASSERT(DB_REF(mp) == 1);
2765766Sgg161487 
2775766Sgg161487 	mp->b_rptr += sizeof (ib_addrs_t);
2785766Sgg161487 	return (mp);
2795766Sgg161487 }
2805766Sgg161487 
2815766Sgg161487 void
mac_ib_link_details(char * buf,size_t sz,mac_handle_t mh,void * mac_pdata)2825766Sgg161487 mac_ib_link_details(char *buf, size_t sz, mac_handle_t mh, void *mac_pdata)
2835766Sgg161487 {
2845766Sgg161487 	uint64_t	speed;
2855766Sgg161487 
2865766Sgg161487 	_NOTE(ARGUNUSED(mac_pdata));
2875766Sgg161487 
2885766Sgg161487 	speed = mac_stat_get(mh, MAC_STAT_IFSPEED);
2895766Sgg161487 
2905766Sgg161487 	/* convert to Mbps */
2915766Sgg161487 	speed /= 1000000;
2925766Sgg161487 
2935766Sgg161487 	buf[0] = 0;
2945766Sgg161487 	(void) snprintf(buf, sz, "%u Mbps", (uint32_t)speed);
2955766Sgg161487 }
2965766Sgg161487 
2975766Sgg161487 static mactype_ops_t mac_ib_type_ops = {
2985766Sgg161487 	MTOPS_HEADER_COOK | MTOPS_HEADER_UNCOOK | MTOPS_LINK_DETAILS,
2995766Sgg161487 	mac_ib_unicst_verify,
3005766Sgg161487 	mac_ib_multicst_verify,
3015766Sgg161487 	mac_ib_sap_verify,
3025766Sgg161487 	mac_ib_header,
3035766Sgg161487 	mac_ib_header_info,
3045766Sgg161487 	NULL,
3055766Sgg161487 	mac_ib_header_cook,
3065766Sgg161487 	mac_ib_header_uncook,
3075766Sgg161487 	mac_ib_link_details
3085766Sgg161487 };
309