187ba907dSgg /*
287ba907dSgg  * CDDL HEADER START
387ba907dSgg  *
487ba907dSgg  * The contents of this file are subject to the terms of the
587ba907dSgg  * Common Development and Distribution License (the "License").
687ba907dSgg  * You may not use this file except in compliance with the License.
787ba907dSgg  *
887ba907dSgg  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
987ba907dSgg  * or http://www.opensolaris.org/os/licensing.
1087ba907dSgg  * See the License for the specific language governing permissions
1187ba907dSgg  * and limitations under the License.
1287ba907dSgg  *
1387ba907dSgg  * When distributing Covered Code, include this CDDL HEADER in each
1487ba907dSgg  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1587ba907dSgg  * If applicable, add the following below this CDDL HEADER, with the
1687ba907dSgg  * fields enclosed by brackets "[]" replaced with your own identifying
1787ba907dSgg  * information: Portions Copyright [yyyy] [name of copyright owner]
1887ba907dSgg  *
1987ba907dSgg  * CDDL HEADER END
2087ba907dSgg  */
2187ba907dSgg /*
2287ba907dSgg  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2387ba907dSgg  * Use is subject to license terms.
2487ba907dSgg  */
2587ba907dSgg 
2687ba907dSgg /*
2787ba907dSgg  * DL_IB MAC Type plugin for the Nemo mac module
2887ba907dSgg  */
2987ba907dSgg 
3087ba907dSgg #include <sys/types.h>
3187ba907dSgg #include <sys/modctl.h>
3287ba907dSgg #include <sys/dlpi.h>
3387ba907dSgg #include <sys/ib/clients/ibd/ibd.h>
3487ba907dSgg #include <sys/mac.h>
3587ba907dSgg #include <sys/mac_ib.h>
3687ba907dSgg #include <sys/dls.h>
3787ba907dSgg #include <sys/byteorder.h>
3887ba907dSgg #include <sys/strsun.h>
3987ba907dSgg #include <inet/common.h>
4087ba907dSgg #include <sys/note.h>
4187ba907dSgg 
4287ba907dSgg static uint8_t ib_brdcst[] = { 0x00, 0xff, 0xff, 0xff,
4387ba907dSgg     0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
4487ba907dSgg     0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff };
4587ba907dSgg 
4687ba907dSgg static struct modlmisc mac_ib_modlmisc = {
4787ba907dSgg 	&mod_miscops,
4887ba907dSgg 	"Infiniband MAC Type plugin 1.0"
4987ba907dSgg };
5087ba907dSgg 
5187ba907dSgg static struct modlinkage mac_ib_modlinkage = {
5287ba907dSgg 	MODREV_1,
5387ba907dSgg 	&mac_ib_modlmisc,
5487ba907dSgg 	NULL
5587ba907dSgg };
5687ba907dSgg 
5787ba907dSgg static mactype_ops_t mac_ib_type_ops;
5887ba907dSgg 
5987ba907dSgg int
_init(void)6087ba907dSgg _init(void)
6187ba907dSgg {
6287ba907dSgg 	mactype_register_t *mtrp;
6387ba907dSgg 	int	err;
6487ba907dSgg 
6587ba907dSgg 	if ((mtrp = mactype_alloc(MACTYPE_VERSION)) == NULL)
6687ba907dSgg 		return (ENOTSUP);
6787ba907dSgg 	mtrp->mtr_ident = MAC_PLUGIN_IDENT_IB;
6887ba907dSgg 	mtrp->mtr_ops = &mac_ib_type_ops;
6987ba907dSgg 	mtrp->mtr_mactype = DL_IB;
70d62bc4baSyz 	mtrp->mtr_nativetype = DL_IB;
7187ba907dSgg 	mtrp->mtr_addrlen = IPOIB_ADDRL;
7287ba907dSgg 	mtrp->mtr_brdcst_addr = ib_brdcst;
7387ba907dSgg 
7487ba907dSgg 	/*
7587ba907dSgg 	 * So far, generic stats maintained by GLDv3 are sufficient for IB.
7687ba907dSgg 	 */
7787ba907dSgg 	mtrp->mtr_stats = NULL;
7887ba907dSgg 	mtrp->mtr_statcount = 0;
7987ba907dSgg 	if ((err = mactype_register(mtrp)) == 0) {
8087ba907dSgg 		if ((err = mod_install(&mac_ib_modlinkage)) != 0)
8187ba907dSgg 			(void) mactype_unregister(MAC_PLUGIN_IDENT_IB);
8287ba907dSgg 	}
8387ba907dSgg 	mactype_free(mtrp);
8487ba907dSgg 	return (err);
8587ba907dSgg }
8687ba907dSgg 
8787ba907dSgg int
_fini(void)8887ba907dSgg _fini(void)
8987ba907dSgg {
9087ba907dSgg 	int	err;
9187ba907dSgg 
9287ba907dSgg 	if ((err = mactype_unregister(MAC_PLUGIN_IDENT_IB)) != 0)
9387ba907dSgg 		return (err);
9487ba907dSgg 	return (mod_remove(&mac_ib_modlinkage));
9587ba907dSgg }
9687ba907dSgg 
9787ba907dSgg int
_info(struct modinfo * modinfop)9887ba907dSgg _info(struct modinfo *modinfop)
9987ba907dSgg {
10087ba907dSgg 	return (mod_info(&mac_ib_modlinkage, modinfop));
10187ba907dSgg }
10287ba907dSgg 
10387ba907dSgg /*
10487ba907dSgg  * MAC Type plugin operations
10587ba907dSgg  */
10687ba907dSgg 
10787ba907dSgg /* ARGSUSED */
10887ba907dSgg int
mac_ib_unicst_verify(const void * addr,void * mac_pdata)10987ba907dSgg mac_ib_unicst_verify(const void *addr, void *mac_pdata)
11087ba907dSgg {
11187ba907dSgg 	ipoib_mac_t *ibaddr = (ipoib_mac_t *)addr;
11287ba907dSgg 
11387ba907dSgg 	/*
11487ba907dSgg 	 * The address must not be a multicast address.
11587ba907dSgg 	 */
11687ba907dSgg 	return (ntohl(ibaddr->ipoib_qpn) == IB_MC_QPN ? EINVAL : 0);
11787ba907dSgg }
11887ba907dSgg 
11987ba907dSgg int
mac_ib_multicst_verify(const void * addr,void * mac_pdata)12087ba907dSgg mac_ib_multicst_verify(const void *addr, void *mac_pdata)
12187ba907dSgg {
12287ba907dSgg 	ipoib_mac_t *ibaddr = (ipoib_mac_t *)addr;
12387ba907dSgg 	uint8_t *p_gid = (uint8_t *)addr + sizeof (ipoib_mac_t)
12487ba907dSgg 	    - MAC_IB_GID_SIZE;
12587ba907dSgg 	uint32_t bcst_gid[3] = { 0x0, 0x0, MAC_IB_BROADCAST_GID };
12687ba907dSgg 
12787ba907dSgg 	_NOTE(ARGUNUSED(mac_pdata));
12887ba907dSgg 
12987ba907dSgg 	/*
13087ba907dSgg 	 * The address must be a multicast address.
13187ba907dSgg 	 */
13287ba907dSgg 	if ((ntohl(ibaddr->ipoib_qpn) & IB_QPN_MASK) != IB_MC_QPN)
13387ba907dSgg 		return (EINVAL);
13487ba907dSgg 
13587ba907dSgg 	/*
13687ba907dSgg 	 * The address must not be the broadcast address.
13787ba907dSgg 	 */
13887ba907dSgg 	if (bcmp(p_gid, (uint8_t *)bcst_gid + sizeof (bcst_gid) -
13987ba907dSgg 	    MAC_IB_GID_SIZE, MAC_IB_GID_SIZE) == 0)
14087ba907dSgg 		return (EINVAL);
14187ba907dSgg 
14287ba907dSgg 	return (0);
14387ba907dSgg }
14487ba907dSgg 
14587ba907dSgg /*
14687ba907dSgg  * Check the legality of a SAP value. The following values are
14787ba907dSgg  * allowed, as specified by PSARC 2003/150:
14887ba907dSgg  *
14987ba907dSgg  * min-ethertype-sap (256).. EtherType max(65535)	ethertype semantics
15087ba907dSgg  *        (0)             .. max-802-sap(255)		IEEE 802 semantics
15187ba907dSgg  */
15287ba907dSgg boolean_t
mac_ib_sap_verify(uint32_t sap,uint32_t * bind_sap,void * mac_pdata)15387ba907dSgg mac_ib_sap_verify(uint32_t sap, uint32_t *bind_sap, void *mac_pdata)
15487ba907dSgg {
15587ba907dSgg 	_NOTE(ARGUNUSED(mac_pdata));
15687ba907dSgg 
15787ba907dSgg 	if (sap > MAC_IB_MAX_802_SAP && sap <= MAC_IB_ETHERTYPE_MAX) {
15887ba907dSgg 		if (bind_sap != NULL)
15987ba907dSgg 			*bind_sap = sap;
16087ba907dSgg 		return (B_TRUE);
16187ba907dSgg 	}
16287ba907dSgg 
16387ba907dSgg 	if (sap <= MAC_IB_MAX_802_SAP) {
16487ba907dSgg 		if (bind_sap != NULL)
16587ba907dSgg 			*bind_sap = DLS_SAP_LLC;
16687ba907dSgg 		return (B_TRUE);
16787ba907dSgg 	}
16887ba907dSgg 
16987ba907dSgg 	return (B_FALSE);
17087ba907dSgg }
17187ba907dSgg 
17287ba907dSgg /* ARGSUSED */
17387ba907dSgg mblk_t *
mac_ib_header(const void * saddr,const void * daddr,uint32_t sap,void * mac_pdata,mblk_t * payload,size_t extra_len)17487ba907dSgg mac_ib_header(const void *saddr, const void *daddr, uint32_t sap,
17587ba907dSgg     void *mac_pdata, mblk_t *payload, size_t extra_len)
17687ba907dSgg {
17787ba907dSgg 	ib_header_info_t	*ibhp;
17887ba907dSgg 	mblk_t			*mp;
17987ba907dSgg 
18087ba907dSgg 	if (!mac_ib_sap_verify(sap, NULL, NULL))
18187ba907dSgg 		return (NULL);
18287ba907dSgg 
18387ba907dSgg 	mp = allocb(sizeof (ib_header_info_t) + extra_len, BPRI_HI);
18487ba907dSgg 	if (mp == NULL)
18587ba907dSgg 		return (NULL);
18687ba907dSgg 
187*22eb7cb5Sgd 	ibhp = (void *)mp->b_rptr;
18887ba907dSgg 	ibhp->ipib_rhdr.ipoib_type = htons(sap);
18987ba907dSgg 	ibhp->ipib_rhdr.ipoib_mbz = 0;
19087ba907dSgg 	bcopy(daddr, &ibhp->ib_dst, IPOIB_ADDRL);
19187ba907dSgg 	mp->b_wptr += sizeof (ib_header_info_t);
19287ba907dSgg 	return (mp);
19387ba907dSgg }
19487ba907dSgg 
19587ba907dSgg int
mac_ib_header_info(mblk_t * mp,void * mac_pdata,mac_header_info_t * hdr_info)19687ba907dSgg mac_ib_header_info(mblk_t *mp, void *mac_pdata, mac_header_info_t *hdr_info)
19787ba907dSgg {
19887ba907dSgg 	ib_header_info_t	*ibhp;
19987ba907dSgg 	uint16_t	sap;
20087ba907dSgg 
20187ba907dSgg 	if (MBLKL(mp) < sizeof (ib_header_info_t))
20287ba907dSgg 		return (EINVAL);
20387ba907dSgg 
204*22eb7cb5Sgd 	ibhp = (void *)mp->b_rptr;
20587ba907dSgg 
20687ba907dSgg 	hdr_info->mhi_hdrsize = sizeof (ib_header_info_t);
20787ba907dSgg 	hdr_info->mhi_daddr = (const uint8_t *)&(ibhp->ib_dst);
20887ba907dSgg 	if (ibhp->ib_grh.ipoib_vertcflow != 0)
20987ba907dSgg 		hdr_info->mhi_saddr = (const uint8_t *)&(ibhp->ib_src);
21087ba907dSgg 	else
21187ba907dSgg 		hdr_info->mhi_saddr = NULL;
21287ba907dSgg 
21387ba907dSgg 	if (mac_ib_unicst_verify(hdr_info->mhi_daddr, mac_pdata) == 0) {
21487ba907dSgg 		hdr_info->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
21587ba907dSgg 	} else if (mac_ib_multicst_verify(hdr_info->mhi_daddr,
21687ba907dSgg 	    mac_pdata) == 0) {
21787ba907dSgg 		hdr_info->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
21887ba907dSgg 	} else {
21987ba907dSgg 		hdr_info->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
22087ba907dSgg 	}
22187ba907dSgg 
22287ba907dSgg 	sap = ntohs(ibhp->ipib_rhdr.ipoib_type);
22387ba907dSgg 	hdr_info->mhi_origsap = hdr_info->mhi_bindsap = sap;
22487ba907dSgg 	hdr_info->mhi_pktsize = 0;
22587ba907dSgg 
22687ba907dSgg 	return (0);
22787ba907dSgg }
22887ba907dSgg 
22987ba907dSgg /*
23087ba907dSgg  * Take the provided `mp' (which is expected to have a header "dst + type"),
23187ba907dSgg  * and return a pointer to an mblk_t with a header "GRH + type".
23287ba907dSgg  * If the conversion cannot be performed, return NULL.
23387ba907dSgg  */
23487ba907dSgg static mblk_t *
mac_ib_header_cook(mblk_t * mp,void * pdata)23587ba907dSgg mac_ib_header_cook(mblk_t *mp, void *pdata)
23687ba907dSgg {
23787ba907dSgg 	ipoib_ptxhdr_t	*orig_hp;
23887ba907dSgg 	mblk_t			*llmp;
23987ba907dSgg 
24087ba907dSgg 	if (MBLKL(mp) < sizeof (ipoib_ptxhdr_t))
24187ba907dSgg 		return (NULL);
24287ba907dSgg 
243*22eb7cb5Sgd 	orig_hp = (void *)mp->b_rptr;
24487ba907dSgg 	llmp = mac_ib_header(NULL, &orig_hp->ipoib_dest,
24587ba907dSgg 	    ntohs(orig_hp->ipoib_rhdr.ipoib_type), pdata, NULL, 0);
24687ba907dSgg 	if (llmp == NULL)
24787ba907dSgg 		return (NULL);
24887ba907dSgg 
24987ba907dSgg 	/*
25087ba907dSgg 	 * The plugin framework guarantees that we have the only reference
25187ba907dSgg 	 * to the mblk_t, so we can safely modify it.
25287ba907dSgg 	 */
25387ba907dSgg 	ASSERT(DB_REF(mp) == 1);
25487ba907dSgg 	mp->b_rptr += sizeof (ipoib_ptxhdr_t);
25587ba907dSgg 	llmp->b_cont = mp;
25687ba907dSgg 	return (llmp);
25787ba907dSgg }
25887ba907dSgg 
25987ba907dSgg /*
26087ba907dSgg  * Take the provided `mp' (which is expected to have a header "GRH + type"),
26187ba907dSgg  * and return a pointer to an mblk_t with a header "type". If the conversion
26287ba907dSgg  * cannot be performed, return NULL.
26387ba907dSgg  */
26487ba907dSgg static mblk_t *
mac_ib_header_uncook(mblk_t * mp,void * pdata)26587ba907dSgg mac_ib_header_uncook(mblk_t *mp, void *pdata)
26687ba907dSgg {
26787ba907dSgg 	_NOTE(ARGUNUSED(pdata));
26887ba907dSgg 
26987ba907dSgg 	/*
27087ba907dSgg 	 * The plugin framework guarantees that we have the only reference to
27187ba907dSgg 	 * the mblk_t and the underlying dblk_t, so we can safely modify it.
27287ba907dSgg 	 */
27387ba907dSgg 	ASSERT(DB_REF(mp) == 1);
27487ba907dSgg 
27587ba907dSgg 	mp->b_rptr += sizeof (ib_addrs_t);
27687ba907dSgg 	return (mp);
27787ba907dSgg }
27887ba907dSgg 
27987ba907dSgg void
mac_ib_link_details(char * buf,size_t sz,mac_handle_t mh,void * mac_pdata)28087ba907dSgg mac_ib_link_details(char *buf, size_t sz, mac_handle_t mh, void *mac_pdata)
28187ba907dSgg {
28287ba907dSgg 	uint64_t	speed;
28387ba907dSgg 
28487ba907dSgg 	_NOTE(ARGUNUSED(mac_pdata));
28587ba907dSgg 
28687ba907dSgg 	speed = mac_stat_get(mh, MAC_STAT_IFSPEED);
28787ba907dSgg 
28887ba907dSgg 	/* convert to Mbps */
28987ba907dSgg 	speed /= 1000000;
29087ba907dSgg 
29187ba907dSgg 	buf[0] = 0;
29287ba907dSgg 	(void) snprintf(buf, sz, "%u Mbps", (uint32_t)speed);
29387ba907dSgg }
29487ba907dSgg 
29587ba907dSgg static mactype_ops_t mac_ib_type_ops = {
29687ba907dSgg 	MTOPS_HEADER_COOK | MTOPS_HEADER_UNCOOK | MTOPS_LINK_DETAILS,
29787ba907dSgg 	mac_ib_unicst_verify,
29887ba907dSgg 	mac_ib_multicst_verify,
29987ba907dSgg 	mac_ib_sap_verify,
30087ba907dSgg 	mac_ib_header,
30187ba907dSgg 	mac_ib_header_info,
30287ba907dSgg 	NULL,
30387ba907dSgg 	mac_ib_header_cook,
30487ba907dSgg 	mac_ib_header_uncook,
30587ba907dSgg 	mac_ib_link_details
30687ba907dSgg };
307