162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * net/tipc/ib_media.c: Infiniband bearer support for TIPC
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (c) 2013 Patrick McHardy <kaber@trash.net>
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Based on eth_media.c, which carries the following copyright notice:
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Copyright (c) 2001-2007, Ericsson AB
962306a36Sopenharmony_ci * Copyright (c) 2005-2008, 2011, Wind River Systems
1062306a36Sopenharmony_ci * All rights reserved.
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
1362306a36Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
1662306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
1762306a36Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
1862306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
1962306a36Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
2062306a36Sopenharmony_ci * 3. Neither the names of the copyright holders nor the names of its
2162306a36Sopenharmony_ci *    contributors may be used to endorse or promote products derived from
2262306a36Sopenharmony_ci *    this software without specific prior written permission.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the
2562306a36Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free
2662306a36Sopenharmony_ci * Software Foundation.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2962306a36Sopenharmony_ci * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3062306a36Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3162306a36Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
3262306a36Sopenharmony_ci * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3362306a36Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3462306a36Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3562306a36Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3662306a36Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3762306a36Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3862306a36Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGE.
3962306a36Sopenharmony_ci */
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#include <linux/if_infiniband.h>
4262306a36Sopenharmony_ci#include "core.h"
4362306a36Sopenharmony_ci#include "bearer.h"
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#define TIPC_MAX_IB_LINK_WIN 500
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci/* convert InfiniBand address (media address format) media address to string */
4862306a36Sopenharmony_cistatic int tipc_ib_addr2str(struct tipc_media_addr *a, char *str_buf,
4962306a36Sopenharmony_ci			    int str_size)
5062306a36Sopenharmony_ci{
5162306a36Sopenharmony_ci	if (str_size < 60)	/* 60 = 19 * strlen("xx:") + strlen("xx\0") */
5262306a36Sopenharmony_ci		return 1;
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	sprintf(str_buf, "%20phC", a->value);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	return 0;
5762306a36Sopenharmony_ci}
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/* Convert from media address format to discovery message addr format */
6062306a36Sopenharmony_cistatic int tipc_ib_addr2msg(char *msg, struct tipc_media_addr *addr)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
6362306a36Sopenharmony_ci	memcpy(msg, addr->value, INFINIBAND_ALEN);
6462306a36Sopenharmony_ci	return 0;
6562306a36Sopenharmony_ci}
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci/* Convert raw InfiniBand address format to media addr format */
6862306a36Sopenharmony_cistatic int tipc_ib_raw2addr(struct tipc_bearer *b,
6962306a36Sopenharmony_ci			    struct tipc_media_addr *addr,
7062306a36Sopenharmony_ci			    const char *msg)
7162306a36Sopenharmony_ci{
7262306a36Sopenharmony_ci	memset(addr, 0, sizeof(*addr));
7362306a36Sopenharmony_ci	memcpy(addr->value, msg, INFINIBAND_ALEN);
7462306a36Sopenharmony_ci	addr->media_id = TIPC_MEDIA_TYPE_IB;
7562306a36Sopenharmony_ci	addr->broadcast = !memcmp(msg, b->bcast_addr.value,
7662306a36Sopenharmony_ci				  INFINIBAND_ALEN);
7762306a36Sopenharmony_ci	return 0;
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci/* Convert discovery msg addr format to InfiniBand media addr format */
8162306a36Sopenharmony_cistatic int tipc_ib_msg2addr(struct tipc_bearer *b,
8262306a36Sopenharmony_ci			    struct tipc_media_addr *addr,
8362306a36Sopenharmony_ci			    char *msg)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	return tipc_ib_raw2addr(b, addr, msg);
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/* InfiniBand media registration info */
8962306a36Sopenharmony_cistruct tipc_media ib_media_info = {
9062306a36Sopenharmony_ci	.send_msg	= tipc_l2_send_msg,
9162306a36Sopenharmony_ci	.enable_media	= tipc_enable_l2_media,
9262306a36Sopenharmony_ci	.disable_media	= tipc_disable_l2_media,
9362306a36Sopenharmony_ci	.addr2str	= tipc_ib_addr2str,
9462306a36Sopenharmony_ci	.addr2msg	= tipc_ib_addr2msg,
9562306a36Sopenharmony_ci	.msg2addr	= tipc_ib_msg2addr,
9662306a36Sopenharmony_ci	.raw2addr	= tipc_ib_raw2addr,
9762306a36Sopenharmony_ci	.priority	= TIPC_DEF_LINK_PRI,
9862306a36Sopenharmony_ci	.tolerance	= TIPC_DEF_LINK_TOL,
9962306a36Sopenharmony_ci	.min_win	= TIPC_DEF_LINK_WIN,
10062306a36Sopenharmony_ci	.max_win	= TIPC_MAX_IB_LINK_WIN,
10162306a36Sopenharmony_ci	.type_id	= TIPC_MEDIA_TYPE_IB,
10262306a36Sopenharmony_ci	.hwaddr_len	= INFINIBAND_ALEN,
10362306a36Sopenharmony_ci	.name		= "ib"
10462306a36Sopenharmony_ci};
105