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