blob: 0af5b4acca3045f6ac047286d4a84c53e4ee9a36 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Thomas Richter6812baa2017-01-09 16:55:15 +01002/*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * Generic netlink support functions to configure an SMC-R PNET table
6 *
7 * Copyright IBM Corp. 2016
8 *
9 * Author(s): Thomas Richter <tmricht@linux.vnet.ibm.com>
10 */
11
12#include <linux/module.h>
13#include <linux/list.h>
14#include <linux/ctype.h>
Ursula Braun92f3cb02020-07-08 17:05:13 +020015#include <linux/mutex.h>
Thomas Richter6812baa2017-01-09 16:55:15 +010016#include <net/netlink.h>
17#include <net/genetlink.h>
18
19#include <uapi/linux/if.h>
20#include <uapi/linux/smc.h>
21
22#include <rdma/ib_verbs.h>
23
Hans Wippel64e28b52019-02-21 13:01:02 +010024#include <net/netns/generic.h>
25#include "smc_netns.h"
26
Thomas Richter6812baa2017-01-09 16:55:15 +010027#include "smc_pnet.h"
28#include "smc_ib.h"
Hans Wippel1619f772018-06-28 19:05:08 +020029#include "smc_ism.h"
Karsten Graulbc36d2f2019-04-12 12:57:26 +020030#include "smc_core.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010031
Hans Wippel890a2cb2019-02-21 13:01:00 +010032static struct net_device *pnet_find_base_ndev(struct net_device *ndev);
33
Dmitry Vyukov09d03102020-05-25 17:31:58 +020034static const struct nla_policy smc_pnet_policy[SMC_PNETID_MAX + 1] = {
Thomas Richter6812baa2017-01-09 16:55:15 +010035 [SMC_PNETID_NAME] = {
36 .type = NLA_NUL_STRING,
Hans Wippelca8dc132019-01-30 18:51:01 +010037 .len = SMC_MAX_PNETID_LEN
Thomas Richter6812baa2017-01-09 16:55:15 +010038 },
39 [SMC_PNETID_ETHNAME] = {
40 .type = NLA_NUL_STRING,
41 .len = IFNAMSIZ - 1
42 },
43 [SMC_PNETID_IBNAME] = {
44 .type = NLA_NUL_STRING,
45 .len = IB_DEVICE_NAME_MAX - 1
46 },
47 [SMC_PNETID_IBPORT] = { .type = NLA_U8 }
48};
49
50static struct genl_family smc_pnet_nl_family;
51
Karsten Graulfdff7042020-04-29 17:10:37 +020052enum smc_pnet_nametype {
53 SMC_PNET_ETH = 1,
54 SMC_PNET_IB = 2,
Thomas Richter6812baa2017-01-09 16:55:15 +010055};
56
Hans Wippel890a2cb2019-02-21 13:01:00 +010057/* pnet entry stored in pnet table */
58struct smc_pnetentry {
59 struct list_head list;
60 char pnet_name[SMC_MAX_PNETID_LEN + 1];
Karsten Graulfdff7042020-04-29 17:10:37 +020061 enum smc_pnet_nametype type;
62 union {
63 struct {
64 char eth_name[IFNAMSIZ + 1];
65 struct net_device *ndev;
66 };
67 struct {
68 char ib_name[IB_DEVICE_NAME_MAX + 1];
69 u8 ib_port;
70 };
71 };
Hans Wippel890a2cb2019-02-21 13:01:00 +010072};
Thomas Richter6812baa2017-01-09 16:55:15 +010073
Karsten Graula304e292020-09-26 12:44:19 +020074/* Check if the pnetid is set */
75static bool smc_pnet_is_pnetid_set(u8 *pnetid)
76{
77 if (pnetid[0] == 0 || pnetid[0] == _S)
78 return false;
79 return true;
80}
81
Hans Wippel890a2cb2019-02-21 13:01:00 +010082/* Check if two given pnetids match */
83static bool smc_pnet_match(u8 *pnetid1, u8 *pnetid2)
Thomas Richter6812baa2017-01-09 16:55:15 +010084{
Hans Wippel890a2cb2019-02-21 13:01:00 +010085 int i;
Thomas Richter6812baa2017-01-09 16:55:15 +010086
Hans Wippel890a2cb2019-02-21 13:01:00 +010087 for (i = 0; i < SMC_MAX_PNETID_LEN; i++) {
Karsten Graula304e292020-09-26 12:44:19 +020088 if ((pnetid1[i] == 0 || pnetid1[i] == _S) &&
89 (pnetid2[i] == 0 || pnetid2[i] == _S))
Thomas Richter6812baa2017-01-09 16:55:15 +010090 break;
Hans Wippel890a2cb2019-02-21 13:01:00 +010091 if (pnetid1[i] != pnetid2[i])
92 return false;
Thomas Richter6812baa2017-01-09 16:55:15 +010093 }
Hans Wippel890a2cb2019-02-21 13:01:00 +010094 return true;
Thomas Richter6812baa2017-01-09 16:55:15 +010095}
96
97/* Remove a pnetid from the pnet table.
98 */
Hans Wippel64e28b52019-02-21 13:01:02 +010099static int smc_pnet_remove_by_pnetid(struct net *net, char *pnet_name)
Thomas Richter6812baa2017-01-09 16:55:15 +0100100{
101 struct smc_pnetentry *pnetelem, *tmp_pe;
Hans Wippel64e28b52019-02-21 13:01:02 +0100102 struct smc_pnettable *pnettable;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100103 struct smc_ib_device *ibdev;
Hans Wippelf3d74b22019-02-21 13:01:01 +0100104 struct smcd_dev *smcd_dev;
Hans Wippel64e28b52019-02-21 13:01:02 +0100105 struct smc_net *sn;
Thomas Richter6812baa2017-01-09 16:55:15 +0100106 int rc = -ENOENT;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100107 int ibport;
Thomas Richter6812baa2017-01-09 16:55:15 +0100108
Hans Wippel64e28b52019-02-21 13:01:02 +0100109 /* get pnettable for namespace */
110 sn = net_generic(net, smc_net_id);
111 pnettable = &sn->pnettable;
112
Karsten Graulfdff7042020-04-29 17:10:37 +0200113 /* remove table entry */
Hans Wippel64e28b52019-02-21 13:01:02 +0100114 write_lock(&pnettable->lock);
115 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist,
Thomas Richter6812baa2017-01-09 16:55:15 +0100116 list) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100117 if (!pnet_name ||
118 smc_pnet_match(pnetelem->pnet_name, pnet_name)) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100119 list_del(&pnetelem->list);
Karsten Graul0a99be42020-05-05 15:01:20 +0200120 if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200121 dev_put(pnetelem->ndev);
Karsten Graul0a99be42020-05-05 15:01:20 +0200122 pr_warn_ratelimited("smc: net device %s "
123 "erased user defined "
124 "pnetid %.16s\n",
125 pnetelem->eth_name,
126 pnetelem->pnet_name);
127 }
Thomas Richter6812baa2017-01-09 16:55:15 +0100128 kfree(pnetelem);
129 rc = 0;
Thomas Richter6812baa2017-01-09 16:55:15 +0100130 }
131 }
Hans Wippel64e28b52019-02-21 13:01:02 +0100132 write_unlock(&pnettable->lock);
133
134 /* if this is not the initial namespace, stop here */
135 if (net != &init_net)
136 return rc;
137
Hans Wippel890a2cb2019-02-21 13:01:00 +0100138 /* remove ib devices */
Ursula Braun92f3cb02020-07-08 17:05:13 +0200139 mutex_lock(&smc_ib_devices.mutex);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100140 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
141 for (ibport = 0; ibport < SMC_MAX_PORTS; ibport++) {
142 if (ibdev->pnetid_by_user[ibport] &&
143 (!pnet_name ||
144 smc_pnet_match(pnet_name,
145 ibdev->pnetid[ibport]))) {
Karsten Graul0a99be42020-05-05 15:01:20 +0200146 pr_warn_ratelimited("smc: ib device %s ibport "
147 "%d erased user defined "
148 "pnetid %.16s\n",
149 ibdev->ibdev->name,
150 ibport + 1,
151 ibdev->pnetid[ibport]);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100152 memset(ibdev->pnetid[ibport], 0,
153 SMC_MAX_PNETID_LEN);
154 ibdev->pnetid_by_user[ibport] = false;
155 rc = 0;
156 }
157 }
158 }
Ursula Braun92f3cb02020-07-08 17:05:13 +0200159 mutex_unlock(&smc_ib_devices.mutex);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100160 /* remove smcd devices */
Ursula Braun82087c02020-07-08 17:05:14 +0200161 mutex_lock(&smcd_dev_list.mutex);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100162 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) {
163 if (smcd_dev->pnetid_by_user &&
164 (!pnet_name ||
165 smc_pnet_match(pnet_name, smcd_dev->pnetid))) {
Karsten Graul0a99be42020-05-05 15:01:20 +0200166 pr_warn_ratelimited("smc: smcd device %s "
167 "erased user defined pnetid "
168 "%.16s\n", dev_name(&smcd_dev->dev),
169 smcd_dev->pnetid);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100170 memset(smcd_dev->pnetid, 0, SMC_MAX_PNETID_LEN);
171 smcd_dev->pnetid_by_user = false;
172 rc = 0;
173 }
174 }
Ursula Braun82087c02020-07-08 17:05:14 +0200175 mutex_unlock(&smcd_dev_list.mutex);
Thomas Richter6812baa2017-01-09 16:55:15 +0100176 return rc;
177}
178
Karsten Graulfdff7042020-04-29 17:10:37 +0200179/* Add the reference to a given network device to the pnet table.
180 */
181static int smc_pnet_add_by_ndev(struct net_device *ndev)
182{
183 struct smc_pnetentry *pnetelem, *tmp_pe;
184 struct smc_pnettable *pnettable;
185 struct net *net = dev_net(ndev);
186 struct smc_net *sn;
187 int rc = -ENOENT;
188
189 /* get pnettable for namespace */
190 sn = net_generic(net, smc_net_id);
191 pnettable = &sn->pnettable;
192
193 write_lock(&pnettable->lock);
194 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) {
195 if (pnetelem->type == SMC_PNET_ETH && !pnetelem->ndev &&
196 !strncmp(pnetelem->eth_name, ndev->name, IFNAMSIZ)) {
197 dev_hold(ndev);
198 pnetelem->ndev = ndev;
199 rc = 0;
Karsten Graul0a99be42020-05-05 15:01:20 +0200200 pr_warn_ratelimited("smc: adding net device %s with "
201 "user defined pnetid %.16s\n",
202 pnetelem->eth_name,
203 pnetelem->pnet_name);
Karsten Graulfdff7042020-04-29 17:10:37 +0200204 break;
205 }
206 }
207 write_unlock(&pnettable->lock);
208 return rc;
209}
210
211/* Remove the reference to a given network device from the pnet table.
Thomas Richter6812baa2017-01-09 16:55:15 +0100212 */
213static int smc_pnet_remove_by_ndev(struct net_device *ndev)
214{
215 struct smc_pnetentry *pnetelem, *tmp_pe;
Hans Wippel64e28b52019-02-21 13:01:02 +0100216 struct smc_pnettable *pnettable;
217 struct net *net = dev_net(ndev);
218 struct smc_net *sn;
Thomas Richter6812baa2017-01-09 16:55:15 +0100219 int rc = -ENOENT;
220
Hans Wippel64e28b52019-02-21 13:01:02 +0100221 /* get pnettable for namespace */
222 sn = net_generic(net, smc_net_id);
223 pnettable = &sn->pnettable;
224
225 write_lock(&pnettable->lock);
226 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200227 if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev == ndev) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100228 dev_put(pnetelem->ndev);
Karsten Graulfdff7042020-04-29 17:10:37 +0200229 pnetelem->ndev = NULL;
Thomas Richter6812baa2017-01-09 16:55:15 +0100230 rc = 0;
Karsten Graul0a99be42020-05-05 15:01:20 +0200231 pr_warn_ratelimited("smc: removing net device %s with "
232 "user defined pnetid %.16s\n",
233 pnetelem->eth_name,
234 pnetelem->pnet_name);
Thomas Richter6812baa2017-01-09 16:55:15 +0100235 break;
236 }
237 }
Hans Wippel64e28b52019-02-21 13:01:02 +0100238 write_unlock(&pnettable->lock);
Thomas Richter6812baa2017-01-09 16:55:15 +0100239 return rc;
240}
241
Karsten Graulfdff7042020-04-29 17:10:37 +0200242/* Apply pnetid to ib device when no pnetid is set.
Thomas Richter6812baa2017-01-09 16:55:15 +0100243 */
Karsten Graulfdff7042020-04-29 17:10:37 +0200244static bool smc_pnet_apply_ib(struct smc_ib_device *ib_dev, u8 ib_port,
245 char *pnet_name)
Thomas Richter6812baa2017-01-09 16:55:15 +0100246{
Karsten Graulfdff7042020-04-29 17:10:37 +0200247 bool applied = false;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100248
Ursula Braun92f3cb02020-07-08 17:05:13 +0200249 mutex_lock(&smc_ib_devices.mutex);
Karsten Graula304e292020-09-26 12:44:19 +0200250 if (!smc_pnet_is_pnetid_set(ib_dev->pnetid[ib_port - 1])) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200251 memcpy(ib_dev->pnetid[ib_port - 1], pnet_name,
252 SMC_MAX_PNETID_LEN);
253 ib_dev->pnetid_by_user[ib_port - 1] = true;
254 applied = true;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100255 }
Ursula Braun92f3cb02020-07-08 17:05:13 +0200256 mutex_unlock(&smc_ib_devices.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200257 return applied;
258}
Hans Wippelf3d74b22019-02-21 13:01:01 +0100259
Karsten Graulfdff7042020-04-29 17:10:37 +0200260/* Apply pnetid to smcd device when no pnetid is set.
261 */
262static bool smc_pnet_apply_smcd(struct smcd_dev *smcd_dev, char *pnet_name)
263{
Karsten Graulfdff7042020-04-29 17:10:37 +0200264 bool applied = false;
265
Ursula Braun82087c02020-07-08 17:05:14 +0200266 mutex_lock(&smcd_dev_list.mutex);
Karsten Graula304e292020-09-26 12:44:19 +0200267 if (!smc_pnet_is_pnetid_set(smcd_dev->pnetid)) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200268 memcpy(smcd_dev->pnetid, pnet_name, SMC_MAX_PNETID_LEN);
269 smcd_dev->pnetid_by_user = true;
270 applied = true;
Hans Wippelf3d74b22019-02-21 13:01:01 +0100271 }
Ursula Braun82087c02020-07-08 17:05:14 +0200272 mutex_unlock(&smcd_dev_list.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200273 return applied;
Thomas Richter6812baa2017-01-09 16:55:15 +0100274}
275
276/* The limit for pnetid is 16 characters.
277 * Valid characters should be (single-byte character set) a-z, A-Z, 0-9.
278 * Lower case letters are converted to upper case.
279 * Interior blanks should not be used.
280 */
281static bool smc_pnetid_valid(const char *pnet_name, char *pnetid)
282{
283 char *bf = skip_spaces(pnet_name);
284 size_t len = strlen(bf);
285 char *end = bf + len;
286
287 if (!len)
288 return false;
289 while (--end >= bf && isspace(*end))
290 ;
Ursula Braun0afff912018-06-28 19:05:05 +0200291 if (end - bf >= SMC_MAX_PNETID_LEN)
Thomas Richter6812baa2017-01-09 16:55:15 +0100292 return false;
293 while (bf <= end) {
294 if (!isalnum(*bf))
295 return false;
296 *pnetid++ = islower(*bf) ? toupper(*bf) : *bf;
297 bf++;
298 }
299 *pnetid = '\0';
300 return true;
301}
302
303/* Find an infiniband device by a given name. The device might not exist. */
Ursula Braun249633a2017-04-10 14:57:57 +0200304static struct smc_ib_device *smc_pnet_find_ib(char *ib_name)
Thomas Richter6812baa2017-01-09 16:55:15 +0100305{
306 struct smc_ib_device *ibdev;
307
Ursula Braun92f3cb02020-07-08 17:05:13 +0200308 mutex_lock(&smc_ib_devices.mutex);
Thomas Richter6812baa2017-01-09 16:55:15 +0100309 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
310 if (!strncmp(ibdev->ibdev->name, ib_name,
Hans Wippelaf5f60c2019-02-21 13:01:03 +0100311 sizeof(ibdev->ibdev->name)) ||
312 !strncmp(dev_name(ibdev->ibdev->dev.parent), ib_name,
313 IB_DEVICE_NAME_MAX - 1)) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100314 goto out;
315 }
316 }
317 ibdev = NULL;
318out:
Ursula Braun92f3cb02020-07-08 17:05:13 +0200319 mutex_unlock(&smc_ib_devices.mutex);
Thomas Richter6812baa2017-01-09 16:55:15 +0100320 return ibdev;
321}
322
Hans Wippelf3d74b22019-02-21 13:01:01 +0100323/* Find an smcd device by a given name. The device might not exist. */
324static struct smcd_dev *smc_pnet_find_smcd(char *smcd_name)
325{
326 struct smcd_dev *smcd_dev;
327
Ursula Braun82087c02020-07-08 17:05:14 +0200328 mutex_lock(&smcd_dev_list.mutex);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100329 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) {
330 if (!strncmp(dev_name(&smcd_dev->dev), smcd_name,
331 IB_DEVICE_NAME_MAX - 1))
332 goto out;
333 }
334 smcd_dev = NULL;
335out:
Ursula Braun82087c02020-07-08 17:05:14 +0200336 mutex_unlock(&smcd_dev_list.mutex);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100337 return smcd_dev;
338}
339
Karsten Graulfdff7042020-04-29 17:10:37 +0200340static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net,
341 char *eth_name, char *pnet_name)
Thomas Richter6812baa2017-01-09 16:55:15 +0100342{
Karsten Graulfdff7042020-04-29 17:10:37 +0200343 struct smc_pnetentry *tmp_pe, *new_pe;
344 struct net_device *ndev, *base_ndev;
345 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
346 bool new_netdev;
Eric Biggersd49baa72018-05-13 17:01:30 -0700347 int rc;
Thomas Richter6812baa2017-01-09 16:55:15 +0100348
Karsten Graulfdff7042020-04-29 17:10:37 +0200349 /* check if (base) netdev already has a pnetid. If there is one, we do
350 * not want to add a pnet table entry
351 */
352 rc = -EEXIST;
353 ndev = dev_get_by_name(net, eth_name); /* dev_hold() */
354 if (ndev) {
355 base_ndev = pnet_find_base_ndev(ndev);
356 if (!smc_pnetid_by_dev_port(base_ndev->dev.parent,
357 base_ndev->dev_port, ndev_pnetid))
358 goto out_put;
359 }
360
361 /* add a new netdev entry to the pnet table if there isn't one */
362 rc = -ENOMEM;
363 new_pe = kzalloc(sizeof(*new_pe), GFP_KERNEL);
364 if (!new_pe)
365 goto out_put;
366 new_pe->type = SMC_PNET_ETH;
367 memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN);
368 strncpy(new_pe->eth_name, eth_name, IFNAMSIZ);
369 new_pe->ndev = ndev;
370
371 rc = -EEXIST;
372 new_netdev = true;
373 write_lock(&pnettable->lock);
374 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
375 if (tmp_pe->type == SMC_PNET_ETH &&
376 !strncmp(tmp_pe->eth_name, eth_name, IFNAMSIZ)) {
377 new_netdev = false;
378 break;
379 }
380 }
381 if (new_netdev) {
382 list_add_tail(&new_pe->list, &pnettable->pnetlist);
383 write_unlock(&pnettable->lock);
384 } else {
385 write_unlock(&pnettable->lock);
386 kfree(new_pe);
387 goto out_put;
388 }
Karsten Graul0a99be42020-05-05 15:01:20 +0200389 if (ndev)
390 pr_warn_ratelimited("smc: net device %s "
391 "applied user defined pnetid %.16s\n",
392 new_pe->eth_name, new_pe->pnet_name);
Karsten Graulfdff7042020-04-29 17:10:37 +0200393 return 0;
394
395out_put:
396 if (ndev)
397 dev_put(ndev);
398 return rc;
399}
400
401static int smc_pnet_add_ib(struct smc_pnettable *pnettable, char *ib_name,
402 u8 ib_port, char *pnet_name)
403{
404 struct smc_pnetentry *tmp_pe, *new_pe;
405 struct smc_ib_device *ib_dev;
406 bool smcddev_applied = true;
407 bool ibdev_applied = true;
408 struct smcd_dev *smcd_dev;
409 bool new_ibdev;
410
411 /* try to apply the pnetid to active devices */
412 ib_dev = smc_pnet_find_ib(ib_name);
Karsten Graul0a99be42020-05-05 15:01:20 +0200413 if (ib_dev) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200414 ibdev_applied = smc_pnet_apply_ib(ib_dev, ib_port, pnet_name);
Karsten Graul0a99be42020-05-05 15:01:20 +0200415 if (ibdev_applied)
416 pr_warn_ratelimited("smc: ib device %s ibport %d "
417 "applied user defined pnetid "
418 "%.16s\n", ib_dev->ibdev->name,
419 ib_port,
420 ib_dev->pnetid[ib_port - 1]);
421 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200422 smcd_dev = smc_pnet_find_smcd(ib_name);
Karsten Graul0a99be42020-05-05 15:01:20 +0200423 if (smcd_dev) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200424 smcddev_applied = smc_pnet_apply_smcd(smcd_dev, pnet_name);
Karsten Graul0a99be42020-05-05 15:01:20 +0200425 if (smcddev_applied)
426 pr_warn_ratelimited("smc: smcd device %s "
427 "applied user defined pnetid "
428 "%.16s\n", dev_name(&smcd_dev->dev),
429 smcd_dev->pnetid);
430 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200431 /* Apply fails when a device has a hardware-defined pnetid set, do not
432 * add a pnet table entry in that case.
433 */
434 if (!ibdev_applied || !smcddev_applied)
435 return -EEXIST;
436
437 /* add a new ib entry to the pnet table if there isn't one */
438 new_pe = kzalloc(sizeof(*new_pe), GFP_KERNEL);
439 if (!new_pe)
440 return -ENOMEM;
441 new_pe->type = SMC_PNET_IB;
442 memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN);
443 strncpy(new_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX);
444 new_pe->ib_port = ib_port;
445
446 new_ibdev = true;
447 write_lock(&pnettable->lock);
448 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
449 if (tmp_pe->type == SMC_PNET_IB &&
450 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX)) {
451 new_ibdev = false;
452 break;
453 }
454 }
455 if (new_ibdev) {
456 list_add_tail(&new_pe->list, &pnettable->pnetlist);
457 write_unlock(&pnettable->lock);
458 } else {
459 write_unlock(&pnettable->lock);
460 kfree(new_pe);
461 }
462 return (new_ibdev) ? 0 : -EEXIST;
463}
464
465/* Append a pnetid to the end of the pnet table if not already on this list.
466 */
467static int smc_pnet_enter(struct net *net, struct nlattr *tb[])
468{
469 char pnet_name[SMC_MAX_PNETID_LEN + 1];
470 struct smc_pnettable *pnettable;
471 bool new_netdev = false;
472 bool new_ibdev = false;
473 struct smc_net *sn;
474 u8 ibport = 1;
475 char *string;
476 int rc;
477
478 /* get pnettable for namespace */
479 sn = net_generic(net, smc_net_id);
480 pnettable = &sn->pnettable;
Eric Biggersd49baa72018-05-13 17:01:30 -0700481
482 rc = -EINVAL;
483 if (!tb[SMC_PNETID_NAME])
484 goto error;
485 string = (char *)nla_data(tb[SMC_PNETID_NAME]);
Karsten Graulfdff7042020-04-29 17:10:37 +0200486 if (!smc_pnetid_valid(string, pnet_name))
Eric Biggersd49baa72018-05-13 17:01:30 -0700487 goto error;
488
Hans Wippel890a2cb2019-02-21 13:01:00 +0100489 if (tb[SMC_PNETID_ETHNAME]) {
490 string = (char *)nla_data(tb[SMC_PNETID_ETHNAME]);
Karsten Graulfdff7042020-04-29 17:10:37 +0200491 rc = smc_pnet_add_eth(pnettable, net, string, pnet_name);
492 if (!rc)
493 new_netdev = true;
494 else if (rc != -EEXIST)
Hans Wippel890a2cb2019-02-21 13:01:00 +0100495 goto error;
496 }
Eric Biggersd49baa72018-05-13 17:01:30 -0700497
Hans Wippel64e28b52019-02-21 13:01:02 +0100498 /* if this is not the initial namespace, stop here */
499 if (net != &init_net)
Karsten Graulfdff7042020-04-29 17:10:37 +0200500 return new_netdev ? 0 : -EEXIST;
Hans Wippel64e28b52019-02-21 13:01:02 +0100501
Eric Biggersd49baa72018-05-13 17:01:30 -0700502 rc = -EINVAL;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100503 if (tb[SMC_PNETID_IBNAME]) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200504 string = (char *)nla_data(tb[SMC_PNETID_IBNAME]);
505 string = strim(string);
506 if (tb[SMC_PNETID_IBPORT]) {
507 ibport = nla_get_u8(tb[SMC_PNETID_IBPORT]);
508 if (ibport < 1 || ibport > SMC_MAX_PORTS)
Hans Wippel890a2cb2019-02-21 13:01:00 +0100509 goto error;
510 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200511 rc = smc_pnet_add_ib(pnettable, string, ibport, pnet_name);
512 if (!rc)
513 new_ibdev = true;
514 else if (rc != -EEXIST)
515 goto error;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100516 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200517 return (new_netdev || new_ibdev) ? 0 : -EEXIST;
Thomas Richter6812baa2017-01-09 16:55:15 +0100518
519error:
Thomas Richter6812baa2017-01-09 16:55:15 +0100520 return rc;
521}
522
523/* Convert an smc_pnetentry to a netlink attribute sequence */
Hans Wippel890a2cb2019-02-21 13:01:00 +0100524static int smc_pnet_set_nla(struct sk_buff *msg,
Karsten Graulfdff7042020-04-29 17:10:37 +0200525 struct smc_pnetentry *pnetelem)
Thomas Richter6812baa2017-01-09 16:55:15 +0100526{
Hans Wippel890a2cb2019-02-21 13:01:00 +0100527 if (nla_put_string(msg, SMC_PNETID_NAME, pnetelem->pnet_name))
Thomas Richter6812baa2017-01-09 16:55:15 +0100528 return -1;
Karsten Graulfdff7042020-04-29 17:10:37 +0200529 if (pnetelem->type == SMC_PNET_ETH) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100530 if (nla_put_string(msg, SMC_PNETID_ETHNAME,
Karsten Graulfdff7042020-04-29 17:10:37 +0200531 pnetelem->eth_name))
Hans Wippel890a2cb2019-02-21 13:01:00 +0100532 return -1;
533 } else {
534 if (nla_put_string(msg, SMC_PNETID_ETHNAME, "n/a"))
535 return -1;
536 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200537 if (pnetelem->type == SMC_PNET_IB) {
538 if (nla_put_string(msg, SMC_PNETID_IBNAME, pnetelem->ib_name) ||
Hans Wippel890a2cb2019-02-21 13:01:00 +0100539 nla_put_u8(msg, SMC_PNETID_IBPORT, pnetelem->ib_port))
540 return -1;
541 } else {
542 if (nla_put_string(msg, SMC_PNETID_IBNAME, "n/a") ||
543 nla_put_u8(msg, SMC_PNETID_IBPORT, 0xff))
544 return -1;
545 }
546
Thomas Richter6812baa2017-01-09 16:55:15 +0100547 return 0;
548}
549
Thomas Richter6812baa2017-01-09 16:55:15 +0100550static int smc_pnet_add(struct sk_buff *skb, struct genl_info *info)
551{
552 struct net *net = genl_info_net(info);
Thomas Richter6812baa2017-01-09 16:55:15 +0100553
Karsten Graulfdff7042020-04-29 17:10:37 +0200554 return smc_pnet_enter(net, info->attrs);
Thomas Richter6812baa2017-01-09 16:55:15 +0100555}
556
557static int smc_pnet_del(struct sk_buff *skb, struct genl_info *info)
558{
Hans Wippel64e28b52019-02-21 13:01:02 +0100559 struct net *net = genl_info_net(info);
560
Eric Biggersd49baa72018-05-13 17:01:30 -0700561 if (!info->attrs[SMC_PNETID_NAME])
562 return -EINVAL;
Hans Wippel64e28b52019-02-21 13:01:02 +0100563 return smc_pnet_remove_by_pnetid(net,
Thomas Richter6812baa2017-01-09 16:55:15 +0100564 (char *)nla_data(info->attrs[SMC_PNETID_NAME]));
565}
566
567static int smc_pnet_dump_start(struct netlink_callback *cb)
568{
569 cb->args[0] = 0;
570 return 0;
571}
572
573static int smc_pnet_dumpinfo(struct sk_buff *skb,
574 u32 portid, u32 seq, u32 flags,
Karsten Graulfdff7042020-04-29 17:10:37 +0200575 struct smc_pnetentry *pnetelem)
Thomas Richter6812baa2017-01-09 16:55:15 +0100576{
577 void *hdr;
578
579 hdr = genlmsg_put(skb, portid, seq, &smc_pnet_nl_family,
580 flags, SMC_PNETID_GET);
581 if (!hdr)
582 return -ENOMEM;
583 if (smc_pnet_set_nla(skb, pnetelem) < 0) {
584 genlmsg_cancel(skb, hdr);
585 return -EMSGSIZE;
586 }
587 genlmsg_end(skb, hdr);
588 return 0;
589}
590
Hans Wippel64e28b52019-02-21 13:01:02 +0100591static int _smc_pnet_dump(struct net *net, struct sk_buff *skb, u32 portid,
592 u32 seq, u8 *pnetid, int start_idx)
Thomas Richter6812baa2017-01-09 16:55:15 +0100593{
Hans Wippel64e28b52019-02-21 13:01:02 +0100594 struct smc_pnettable *pnettable;
Thomas Richter6812baa2017-01-09 16:55:15 +0100595 struct smc_pnetentry *pnetelem;
Hans Wippel64e28b52019-02-21 13:01:02 +0100596 struct smc_net *sn;
Thomas Richter6812baa2017-01-09 16:55:15 +0100597 int idx = 0;
598
Hans Wippel64e28b52019-02-21 13:01:02 +0100599 /* get pnettable for namespace */
600 sn = net_generic(net, smc_net_id);
601 pnettable = &sn->pnettable;
602
Karsten Graulfdff7042020-04-29 17:10:37 +0200603 /* dump pnettable entries */
Hans Wippel64e28b52019-02-21 13:01:02 +0100604 read_lock(&pnettable->lock);
605 list_for_each_entry(pnetelem, &pnettable->pnetlist, list) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100606 if (pnetid && !smc_pnet_match(pnetelem->pnet_name, pnetid))
Thomas Richter6812baa2017-01-09 16:55:15 +0100607 continue;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100608 if (idx++ < start_idx)
609 continue;
Karsten Graulfdff7042020-04-29 17:10:37 +0200610 /* if this is not the initial namespace, dump only netdev */
611 if (net != &init_net && pnetelem->type != SMC_PNET_ETH)
612 continue;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100613 if (smc_pnet_dumpinfo(skb, portid, seq, NLM_F_MULTI,
Karsten Graulfdff7042020-04-29 17:10:37 +0200614 pnetelem)) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100615 --idx;
616 break;
617 }
618 }
Hans Wippel64e28b52019-02-21 13:01:02 +0100619 read_unlock(&pnettable->lock);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100620 return idx;
621}
622
623static int smc_pnet_dump(struct sk_buff *skb, struct netlink_callback *cb)
624{
Hans Wippel64e28b52019-02-21 13:01:02 +0100625 struct net *net = sock_net(skb->sk);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100626 int idx;
627
Hans Wippel64e28b52019-02-21 13:01:02 +0100628 idx = _smc_pnet_dump(net, skb, NETLINK_CB(cb->skb).portid,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100629 cb->nlh->nlmsg_seq, NULL, cb->args[0]);
630
631 cb->args[0] = idx;
Thomas Richter6812baa2017-01-09 16:55:15 +0100632 return skb->len;
633}
634
Hans Wippel890a2cb2019-02-21 13:01:00 +0100635/* Retrieve one PNETID entry */
636static int smc_pnet_get(struct sk_buff *skb, struct genl_info *info)
637{
Hans Wippel64e28b52019-02-21 13:01:02 +0100638 struct net *net = genl_info_net(info);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100639 struct sk_buff *msg;
640 void *hdr;
641
642 if (!info->attrs[SMC_PNETID_NAME])
643 return -EINVAL;
644
645 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
646 if (!msg)
647 return -ENOMEM;
648
Hans Wippel64e28b52019-02-21 13:01:02 +0100649 _smc_pnet_dump(net, msg, info->snd_portid, info->snd_seq,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100650 nla_data(info->attrs[SMC_PNETID_NAME]), 0);
651
652 /* finish multi part message and send it */
653 hdr = nlmsg_put(msg, info->snd_portid, info->snd_seq, NLMSG_DONE, 0,
654 NLM_F_MULTI);
655 if (!hdr) {
656 nlmsg_free(msg);
657 return -EMSGSIZE;
658 }
659 return genlmsg_reply(msg, info);
660}
661
Thomas Richter6812baa2017-01-09 16:55:15 +0100662/* Remove and delete all pnetids from pnet table.
663 */
664static int smc_pnet_flush(struct sk_buff *skb, struct genl_info *info)
665{
Hans Wippel64e28b52019-02-21 13:01:02 +0100666 struct net *net = genl_info_net(info);
667
Karsten Graul8ef659f2019-04-11 11:17:33 +0200668 smc_pnet_remove_by_pnetid(net, NULL);
669 return 0;
Thomas Richter6812baa2017-01-09 16:55:15 +0100670}
671
672/* SMC_PNETID generic netlink operation definition */
673static const struct genl_ops smc_pnet_ops[] = {
674 {
675 .cmd = SMC_PNETID_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200676 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Hans Wippel29237d22020-01-21 01:04:46 +0100677 /* can be retrieved by unprivileged users */
Thomas Richter6812baa2017-01-09 16:55:15 +0100678 .doit = smc_pnet_get,
679 .dumpit = smc_pnet_dump,
680 .start = smc_pnet_dump_start
681 },
682 {
683 .cmd = SMC_PNETID_ADD,
Johannes Bergef6243a2019-04-26 14:07:31 +0200684 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Thomas Richter6812baa2017-01-09 16:55:15 +0100685 .flags = GENL_ADMIN_PERM,
Thomas Richter6812baa2017-01-09 16:55:15 +0100686 .doit = smc_pnet_add
687 },
688 {
689 .cmd = SMC_PNETID_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +0200690 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Thomas Richter6812baa2017-01-09 16:55:15 +0100691 .flags = GENL_ADMIN_PERM,
Thomas Richter6812baa2017-01-09 16:55:15 +0100692 .doit = smc_pnet_del
693 },
694 {
695 .cmd = SMC_PNETID_FLUSH,
Johannes Bergef6243a2019-04-26 14:07:31 +0200696 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Thomas Richter6812baa2017-01-09 16:55:15 +0100697 .flags = GENL_ADMIN_PERM,
Thomas Richter6812baa2017-01-09 16:55:15 +0100698 .doit = smc_pnet_flush
699 }
700};
701
702/* SMC_PNETID family definition */
Johannes Berg56ce3c52018-09-20 09:27:30 +0200703static struct genl_family smc_pnet_nl_family __ro_after_init = {
Thomas Richter6812baa2017-01-09 16:55:15 +0100704 .hdrsize = 0,
705 .name = SMCR_GENL_FAMILY_NAME,
706 .version = SMCR_GENL_FAMILY_VERSION,
707 .maxattr = SMC_PNETID_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100708 .policy = smc_pnet_policy,
Thomas Richter6812baa2017-01-09 16:55:15 +0100709 .netnsok = true,
710 .module = THIS_MODULE,
711 .ops = smc_pnet_ops,
712 .n_ops = ARRAY_SIZE(smc_pnet_ops)
713};
714
715static int smc_pnet_netdev_event(struct notifier_block *this,
716 unsigned long event, void *ptr)
717{
718 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
719
720 switch (event) {
721 case NETDEV_REBOOT:
722 case NETDEV_UNREGISTER:
723 smc_pnet_remove_by_ndev(event_dev);
Ursula Braunbe6a3f32018-06-28 19:05:04 +0200724 return NOTIFY_OK;
Karsten Graulfdff7042020-04-29 17:10:37 +0200725 case NETDEV_REGISTER:
726 smc_pnet_add_by_ndev(event_dev);
727 return NOTIFY_OK;
Thomas Richter6812baa2017-01-09 16:55:15 +0100728 default:
Ursula Braunbe6a3f32018-06-28 19:05:04 +0200729 return NOTIFY_DONE;
Thomas Richter6812baa2017-01-09 16:55:15 +0100730 }
Thomas Richter6812baa2017-01-09 16:55:15 +0100731}
732
733static struct notifier_block smc_netdev_notifier = {
734 .notifier_call = smc_pnet_netdev_event
735};
736
Hans Wippel64e28b52019-02-21 13:01:02 +0100737/* init network namespace */
738int smc_pnet_net_init(struct net *net)
739{
740 struct smc_net *sn = net_generic(net, smc_net_id);
741 struct smc_pnettable *pnettable = &sn->pnettable;
742
743 INIT_LIST_HEAD(&pnettable->pnetlist);
744 rwlock_init(&pnettable->lock);
745
746 return 0;
747}
748
Thomas Richter6812baa2017-01-09 16:55:15 +0100749int __init smc_pnet_init(void)
750{
751 int rc;
752
753 rc = genl_register_family(&smc_pnet_nl_family);
754 if (rc)
755 return rc;
756 rc = register_netdevice_notifier(&smc_netdev_notifier);
757 if (rc)
758 genl_unregister_family(&smc_pnet_nl_family);
759 return rc;
760}
761
Hans Wippel64e28b52019-02-21 13:01:02 +0100762/* exit network namespace */
763void smc_pnet_net_exit(struct net *net)
764{
765 /* flush pnet table */
766 smc_pnet_remove_by_pnetid(net, NULL);
767}
768
Thomas Richter6812baa2017-01-09 16:55:15 +0100769void smc_pnet_exit(void)
770{
Thomas Richter6812baa2017-01-09 16:55:15 +0100771 unregister_netdevice_notifier(&smc_netdev_notifier);
772 genl_unregister_family(&smc_pnet_nl_family);
773}
774
Ursula Braun0afff912018-06-28 19:05:05 +0200775/* Determine one base device for stacked net devices.
776 * If the lower device level contains more than one devices
777 * (for instance with bonding slaves), just the first device
778 * is used to reach a base device.
Thomas Richter6812baa2017-01-09 16:55:15 +0100779 */
Ursula Braun0afff912018-06-28 19:05:05 +0200780static struct net_device *pnet_find_base_ndev(struct net_device *ndev)
Thomas Richter6812baa2017-01-09 16:55:15 +0100781{
Ursula Braun0afff912018-06-28 19:05:05 +0200782 int i, nest_lvl;
783
784 rtnl_lock();
Taehee Yoof3b0a182019-10-21 18:47:58 +0000785 nest_lvl = ndev->lower_level;
Ursula Braun0afff912018-06-28 19:05:05 +0200786 for (i = 0; i < nest_lvl; i++) {
787 struct list_head *lower = &ndev->adj_list.lower;
788
789 if (list_empty(lower))
790 break;
791 lower = lower->next;
792 ndev = netdev_lower_get_next(ndev, &lower);
793 }
794 rtnl_unlock();
795 return ndev;
796}
797
Hans Wippel64e28b52019-02-21 13:01:02 +0100798static int smc_pnet_find_ndev_pnetid_by_table(struct net_device *ndev,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100799 u8 *pnetid)
800{
Hans Wippel64e28b52019-02-21 13:01:02 +0100801 struct smc_pnettable *pnettable;
802 struct net *net = dev_net(ndev);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100803 struct smc_pnetentry *pnetelem;
Hans Wippel64e28b52019-02-21 13:01:02 +0100804 struct smc_net *sn;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100805 int rc = -ENOENT;
806
Hans Wippel64e28b52019-02-21 13:01:02 +0100807 /* get pnettable for namespace */
808 sn = net_generic(net, smc_net_id);
809 pnettable = &sn->pnettable;
810
811 read_lock(&pnettable->lock);
812 list_for_each_entry(pnetelem, &pnettable->pnetlist, list) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200813 if (pnetelem->type == SMC_PNET_ETH && ndev == pnetelem->ndev) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100814 /* get pnetid of netdev device */
815 memcpy(pnetid, pnetelem->pnet_name, SMC_MAX_PNETID_LEN);
816 rc = 0;
817 break;
818 }
819 }
Hans Wippel64e28b52019-02-21 13:01:02 +0100820 read_unlock(&pnettable->lock);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100821 return rc;
822}
823
Karsten Graulfdff7042020-04-29 17:10:37 +0200824/* find a roce device for the given pnetid */
825static void _smc_pnet_find_roce_by_pnetid(u8 *pnet_id,
Karsten Graul6c868a32020-05-01 12:48:11 +0200826 struct smc_init_info *ini,
827 struct smc_ib_device *known_dev)
Karsten Graulfdff7042020-04-29 17:10:37 +0200828{
829 struct smc_ib_device *ibdev;
830 int i;
831
832 ini->ib_dev = NULL;
Ursula Braun92f3cb02020-07-08 17:05:13 +0200833 mutex_lock(&smc_ib_devices.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200834 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
Karsten Graul6c868a32020-05-01 12:48:11 +0200835 if (ibdev == known_dev)
836 continue;
Karsten Graulfdff7042020-04-29 17:10:37 +0200837 for (i = 1; i <= SMC_MAX_PORTS; i++) {
838 if (!rdma_is_port_valid(ibdev->ibdev, i))
839 continue;
840 if (smc_pnet_match(ibdev->pnetid[i - 1], pnet_id) &&
841 smc_ib_port_active(ibdev, i) &&
842 !test_bit(i - 1, ibdev->ports_going_away) &&
843 !smc_ib_determine_gid(ibdev, i, ini->vlan_id,
844 ini->ib_gid, NULL)) {
845 ini->ib_dev = ibdev;
846 ini->ib_port = i;
847 goto out;
848 }
849 }
850 }
851out:
Ursula Braun92f3cb02020-07-08 17:05:13 +0200852 mutex_unlock(&smc_ib_devices.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200853}
854
Karsten Graul6c868a32020-05-01 12:48:11 +0200855/* find alternate roce device with same pnet_id and vlan_id */
856void smc_pnet_find_alt_roce(struct smc_link_group *lgr,
857 struct smc_init_info *ini,
858 struct smc_ib_device *known_dev)
859{
860 _smc_pnet_find_roce_by_pnetid(lgr->pnet_id, ini, known_dev);
861}
862
Ursula Braun54903572019-02-28 15:10:08 +0100863/* if handshake network device belongs to a roce device, return its
864 * IB device and port
865 */
866static void smc_pnet_find_rdma_dev(struct net_device *netdev,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200867 struct smc_init_info *ini)
Ursula Braun54903572019-02-28 15:10:08 +0100868{
869 struct smc_ib_device *ibdev;
870
Ursula Braun92f3cb02020-07-08 17:05:13 +0200871 mutex_lock(&smc_ib_devices.mutex);
Ursula Braun54903572019-02-28 15:10:08 +0100872 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
873 struct net_device *ndev;
874 int i;
875
876 for (i = 1; i <= SMC_MAX_PORTS; i++) {
877 if (!rdma_is_port_valid(ibdev->ibdev, i))
878 continue;
879 if (!ibdev->ibdev->ops.get_netdev)
880 continue;
881 ndev = ibdev->ibdev->ops.get_netdev(ibdev->ibdev, i);
882 if (!ndev)
883 continue;
884 dev_put(ndev);
885 if (netdev == ndev &&
886 smc_ib_port_active(ibdev, i) &&
Ursula Braunc3d94942019-10-09 10:07:46 +0200887 !test_bit(i - 1, ibdev->ports_going_away) &&
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200888 !smc_ib_determine_gid(ibdev, i, ini->vlan_id,
889 ini->ib_gid, NULL)) {
890 ini->ib_dev = ibdev;
891 ini->ib_port = i;
Ursula Braun54903572019-02-28 15:10:08 +0100892 break;
893 }
894 }
895 }
Ursula Braun92f3cb02020-07-08 17:05:13 +0200896 mutex_unlock(&smc_ib_devices.mutex);
Ursula Braun54903572019-02-28 15:10:08 +0100897}
898
Ursula Braun0afff912018-06-28 19:05:05 +0200899/* Determine the corresponding IB device port based on the hardware PNETID.
Ursula Braun7005ada2018-07-25 16:35:31 +0200900 * Searching stops at the first matching active IB device port with vlan_id
901 * configured.
Ursula Braun54903572019-02-28 15:10:08 +0100902 * If nothing found, check pnetid table.
903 * If nothing found, try to use handshake device
Ursula Braun0afff912018-06-28 19:05:05 +0200904 */
905static void smc_pnet_find_roce_by_pnetid(struct net_device *ndev,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200906 struct smc_init_info *ini)
Ursula Braun0afff912018-06-28 19:05:05 +0200907{
908 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
Ursula Braun0afff912018-06-28 19:05:05 +0200909
910 ndev = pnet_find_base_ndev(ndev);
911 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100912 ndev_pnetid) &&
Ursula Braun54903572019-02-28 15:10:08 +0100913 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid)) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200914 smc_pnet_find_rdma_dev(ndev, ini);
Ursula Braun0afff912018-06-28 19:05:05 +0200915 return; /* pnetid could not be determined */
Ursula Braun54903572019-02-28 15:10:08 +0100916 }
Karsten Graul6c868a32020-05-01 12:48:11 +0200917 _smc_pnet_find_roce_by_pnetid(ndev_pnetid, ini, NULL);
Ursula Braun0afff912018-06-28 19:05:05 +0200918}
919
Hans Wippel1619f772018-06-28 19:05:08 +0200920static void smc_pnet_find_ism_by_pnetid(struct net_device *ndev,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200921 struct smc_init_info *ini)
Hans Wippel1619f772018-06-28 19:05:08 +0200922{
923 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
924 struct smcd_dev *ismdev;
925
926 ndev = pnet_find_base_ndev(ndev);
927 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
Hans Wippelf3d74b22019-02-21 13:01:01 +0100928 ndev_pnetid) &&
929 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid))
Hans Wippel1619f772018-06-28 19:05:08 +0200930 return; /* pnetid could not be determined */
931
Ursula Braun82087c02020-07-08 17:05:14 +0200932 mutex_lock(&smcd_dev_list.mutex);
Hans Wippel1619f772018-06-28 19:05:08 +0200933 list_for_each_entry(ismdev, &smcd_dev_list.list, list) {
Ursula Braunc3d94942019-10-09 10:07:46 +0200934 if (smc_pnet_match(ismdev->pnetid, ndev_pnetid) &&
Ursula Braun7b2977d2020-09-10 18:48:24 +0200935 !ismdev->going_away &&
936 (!ini->ism_peer_gid ||
937 !smc_ism_cantalk(ini->ism_peer_gid, ini->vlan_id,
938 ismdev))) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200939 ini->ism_dev = ismdev;
Hans Wippel1619f772018-06-28 19:05:08 +0200940 break;
941 }
942 }
Ursula Braun82087c02020-07-08 17:05:14 +0200943 mutex_unlock(&smcd_dev_list.mutex);
Hans Wippel1619f772018-06-28 19:05:08 +0200944}
945
Ursula Braun0afff912018-06-28 19:05:05 +0200946/* PNET table analysis for a given sock:
947 * determine ib_device and port belonging to used internal TCP socket
948 * ethernet interface.
949 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200950void smc_pnet_find_roce_resource(struct sock *sk, struct smc_init_info *ini)
Ursula Braun0afff912018-06-28 19:05:05 +0200951{
952 struct dst_entry *dst = sk_dst_get(sk);
953
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200954 ini->ib_dev = NULL;
955 ini->ib_port = 0;
Ursula Braun0afff912018-06-28 19:05:05 +0200956 if (!dst)
957 goto out;
958 if (!dst->dev)
959 goto out_rel;
960
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200961 smc_pnet_find_roce_by_pnetid(dst->dev, ini);
Ursula Braun0afff912018-06-28 19:05:05 +0200962
Thomas Richter6812baa2017-01-09 16:55:15 +0100963out_rel:
964 dst_release(dst);
Ursula Braun0afff912018-06-28 19:05:05 +0200965out:
966 return;
Thomas Richter6812baa2017-01-09 16:55:15 +0100967}
Hans Wippel1619f772018-06-28 19:05:08 +0200968
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200969void smc_pnet_find_ism_resource(struct sock *sk, struct smc_init_info *ini)
Hans Wippel1619f772018-06-28 19:05:08 +0200970{
971 struct dst_entry *dst = sk_dst_get(sk);
972
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200973 ini->ism_dev = NULL;
Hans Wippel1619f772018-06-28 19:05:08 +0200974 if (!dst)
975 goto out;
976 if (!dst->dev)
977 goto out_rel;
978
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200979 smc_pnet_find_ism_by_pnetid(dst->dev, ini);
Hans Wippel1619f772018-06-28 19:05:08 +0200980
981out_rel:
982 dst_release(dst);
983out:
984 return;
985}
Karsten Graulfdff7042020-04-29 17:10:37 +0200986
987/* Lookup and apply a pnet table entry to the given ib device.
988 */
989int smc_pnetid_by_table_ib(struct smc_ib_device *smcibdev, u8 ib_port)
990{
991 char *ib_name = smcibdev->ibdev->name;
992 struct smc_pnettable *pnettable;
993 struct smc_pnetentry *tmp_pe;
994 struct smc_net *sn;
995 int rc = -ENOENT;
996
997 /* get pnettable for init namespace */
998 sn = net_generic(&init_net, smc_net_id);
999 pnettable = &sn->pnettable;
1000
1001 read_lock(&pnettable->lock);
1002 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
1003 if (tmp_pe->type == SMC_PNET_IB &&
1004 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX) &&
1005 tmp_pe->ib_port == ib_port) {
1006 smc_pnet_apply_ib(smcibdev, ib_port, tmp_pe->pnet_name);
1007 rc = 0;
1008 break;
1009 }
1010 }
1011 read_unlock(&pnettable->lock);
1012
1013 return rc;
1014}
1015
1016/* Lookup and apply a pnet table entry to the given smcd device.
1017 */
1018int smc_pnetid_by_table_smcd(struct smcd_dev *smcddev)
1019{
1020 const char *ib_name = dev_name(&smcddev->dev);
1021 struct smc_pnettable *pnettable;
1022 struct smc_pnetentry *tmp_pe;
1023 struct smc_net *sn;
1024 int rc = -ENOENT;
1025
1026 /* get pnettable for init namespace */
1027 sn = net_generic(&init_net, smc_net_id);
1028 pnettable = &sn->pnettable;
1029
1030 read_lock(&pnettable->lock);
1031 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
1032 if (tmp_pe->type == SMC_PNET_IB &&
1033 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX)) {
1034 smc_pnet_apply_smcd(smcddev, tmp_pe->pnet_name);
1035 rc = 0;
1036 break;
1037 }
1038 }
1039 read_unlock(&pnettable->lock);
1040
1041 return rc;
1042}