]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/tipc/ib_media.c
tipc: relocate common functions from media to bearer
[mirror_ubuntu-jammy-kernel.git] / net / tipc / ib_media.c
CommitLineData
a29a194a
PM
1/*
2 * net/tipc/ib_media.c: Infiniband bearer support for TIPC
3 *
4 * Copyright (c) 2013 Patrick McHardy <kaber@trash.net>
5 *
6 * Based on eth_media.c, which carries the following copyright notice:
7 *
8 * Copyright (c) 2001-2007, Ericsson AB
9 * Copyright (c) 2005-2008, 2011, Wind River Systems
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the names of the copyright holders nor the names of its
21 * contributors may be used to endorse or promote products derived from
22 * this software without specific prior written permission.
23 *
24 * Alternatively, this software may be distributed under the terms of the
25 * GNU General Public License ("GPL") version 2 as published by the Free
26 * Software Foundation.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41#include <linux/if_infiniband.h>
42#include "core.h"
43#include "bearer.h"
44
a29a194a
PM
45/**
46 * ib_media_addr_set - initialize Infiniband media address structure
47 *
48 * Media-dependent "value" field stores MAC address in first 6 bytes
49 * and zeroes out the remaining bytes.
50 */
51static void ib_media_addr_set(const struct tipc_bearer *tb_ptr,
52 struct tipc_media_addr *a, char *mac)
53{
54 BUILD_BUG_ON(sizeof(a->value) < INFINIBAND_ALEN);
55 memcpy(a->value, mac, INFINIBAND_ALEN);
56 a->media_id = TIPC_MEDIA_TYPE_IB;
57 a->broadcast = !memcmp(mac, tb_ptr->bcast_addr.value, INFINIBAND_ALEN);
58}
59
60/**
61 * send_msg - send a TIPC message out over an InfiniBand interface
62 */
63static int send_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr,
64 struct tipc_media_addr *dest)
65{
66 struct sk_buff *clone;
a29a194a 67 int delta;
6e967adf 68 struct net_device *dev = tb_ptr->dev;
a29a194a
PM
69
70 clone = skb_clone(buf, GFP_ATOMIC);
71 if (!clone)
72 return 0;
73
a29a194a 74 delta = dev->hard_header_len - skb_headroom(buf);
a29a194a
PM
75 if ((delta > 0) &&
76 pskb_expand_head(clone, SKB_DATA_ALIGN(delta), 0, GFP_ATOMIC)) {
77 kfree_skb(clone);
78 return 0;
79 }
80
81 skb_reset_network_header(clone);
82 clone->dev = dev;
83 clone->protocol = htons(ETH_P_TIPC);
84 dev_hard_header(clone, dev, ETH_P_TIPC, dest->value,
85 dev->dev_addr, clone->len);
86 dev_queue_xmit(clone);
87 return 0;
88}
89
a29a194a 90/**
4babbaa8 91 * enable_media - attach TIPC bearer to an InfiniBand interface
a29a194a 92 */
4babbaa8 93static int enable_media(struct tipc_bearer *tb_ptr)
a29a194a 94{
2537af9d 95 struct net_device *dev;
a29a194a 96 char *driver_name = strchr((const char *)tb_ptr->name, ':') + 1;
a29a194a
PM
97
98 /* Find device with specified name */
2537af9d 99 dev = dev_get_by_name(&init_net, driver_name);
a29a194a
PM
100 if (!dev)
101 return -ENODEV;
102
a29a194a 103 /* Associate TIPC bearer with InfiniBand bearer */
37cb0620 104 tb_ptr->dev = dev;
6e967adf 105 tb_ptr->usr_handle = NULL;
a29a194a
PM
106 memset(tb_ptr->bcast_addr.value, 0, sizeof(tb_ptr->bcast_addr.value));
107 memcpy(tb_ptr->bcast_addr.value, dev->broadcast, INFINIBAND_ALEN);
108 tb_ptr->bcast_addr.media_id = TIPC_MEDIA_TYPE_IB;
109 tb_ptr->bcast_addr.broadcast = 1;
110 tb_ptr->mtu = dev->mtu;
a29a194a 111 ib_media_addr_set(tb_ptr, &tb_ptr->addr, (char *)dev->dev_addr);
37cb0620 112 rcu_assign_pointer(dev->tipc_ptr, tb_ptr);
a29a194a
PM
113 return 0;
114}
115
a29a194a 116/**
4babbaa8 117 * disable_media - detach TIPC bearer from an InfiniBand interface
a29a194a
PM
118 *
119 * Mark InfiniBand bearer as inactive so that incoming buffers are thrown away,
120 * then get worker thread to complete bearer cleanup. (Can't do cleanup
121 * here because cleanup code needs to sleep and caller holds spinlocks.)
122 */
4babbaa8 123static void disable_media(struct tipc_bearer *tb_ptr)
a29a194a 124{
37cb0620 125 RCU_INIT_POINTER(tb_ptr->dev->tipc_ptr, NULL);
6e967adf 126 dev_put(tb_ptr->dev);
a29a194a
PM
127}
128
a29a194a
PM
129/**
130 * ib_addr2str - convert InfiniBand address to string
131 */
132static int ib_addr2str(struct tipc_media_addr *a, char *str_buf, int str_size)
133{
134 if (str_size < 60) /* 60 = 19 * strlen("xx:") + strlen("xx\0") */
135 return 1;
136
d77e41e1 137 sprintf(str_buf, "%20phC", a->value);
a29a194a
PM
138
139 return 0;
140}
141
142/**
143 * ib_addr2msg - convert InfiniBand address format to message header format
144 */
145static int ib_addr2msg(struct tipc_media_addr *a, char *msg_area)
146{
147 memset(msg_area, 0, TIPC_MEDIA_ADDR_SIZE);
148 msg_area[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_IB;
149 memcpy(msg_area, a->value, INFINIBAND_ALEN);
150 return 0;
151}
152
153/**
154 * ib_msg2addr - convert message header address format to InfiniBand format
155 */
156static int ib_msg2addr(const struct tipc_bearer *tb_ptr,
157 struct tipc_media_addr *a, char *msg_area)
158{
159 ib_media_addr_set(tb_ptr, a, msg_area);
160 return 0;
161}
162
163/*
164 * InfiniBand media registration info
165 */
5702dbab 166struct tipc_media ib_media_info = {
a29a194a 167 .send_msg = send_msg,
4babbaa8
YX
168 .enable_media = enable_media,
169 .disable_media = disable_media,
a29a194a
PM
170 .addr2str = ib_addr2str,
171 .addr2msg = ib_addr2msg,
172 .msg2addr = ib_msg2addr,
173 .priority = TIPC_DEF_LINK_PRI,
174 .tolerance = TIPC_DEF_LINK_TOL,
175 .window = TIPC_DEF_LINK_WIN,
176 .type_id = TIPC_MEDIA_TYPE_IB,
177 .name = "ib"
178};
179