blob: 30e5fac7034e1b9ea1d49c8a1251db59ff4ff87c [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 +010032#define SMC_ASCII_BLANK 32
33
34static struct net_device *pnet_find_base_ndev(struct net_device *ndev);
35
Dmitry Vyukov09d03102020-05-25 17:31:58 +020036static const struct nla_policy smc_pnet_policy[SMC_PNETID_MAX + 1] = {
Thomas Richter6812baa2017-01-09 16:55:15 +010037 [SMC_PNETID_NAME] = {
38 .type = NLA_NUL_STRING,
Hans Wippelca8dc132019-01-30 18:51:01 +010039 .len = SMC_MAX_PNETID_LEN
Thomas Richter6812baa2017-01-09 16:55:15 +010040 },
41 [SMC_PNETID_ETHNAME] = {
42 .type = NLA_NUL_STRING,
43 .len = IFNAMSIZ - 1
44 },
45 [SMC_PNETID_IBNAME] = {
46 .type = NLA_NUL_STRING,
47 .len = IB_DEVICE_NAME_MAX - 1
48 },
49 [SMC_PNETID_IBPORT] = { .type = NLA_U8 }
50};
51
52static struct genl_family smc_pnet_nl_family;
53
Karsten Graulfdff7042020-04-29 17:10:37 +020054enum smc_pnet_nametype {
55 SMC_PNET_ETH = 1,
56 SMC_PNET_IB = 2,
Thomas Richter6812baa2017-01-09 16:55:15 +010057};
58
Hans Wippel890a2cb2019-02-21 13:01:00 +010059/* pnet entry stored in pnet table */
60struct smc_pnetentry {
61 struct list_head list;
62 char pnet_name[SMC_MAX_PNETID_LEN + 1];
Karsten Graulfdff7042020-04-29 17:10:37 +020063 enum smc_pnet_nametype type;
64 union {
65 struct {
66 char eth_name[IFNAMSIZ + 1];
67 struct net_device *ndev;
68 };
69 struct {
70 char ib_name[IB_DEVICE_NAME_MAX + 1];
71 u8 ib_port;
72 };
73 };
Hans Wippel890a2cb2019-02-21 13:01:00 +010074};
Thomas Richter6812baa2017-01-09 16:55:15 +010075
Hans Wippel890a2cb2019-02-21 13:01:00 +010076/* Check if two given pnetids match */
77static bool smc_pnet_match(u8 *pnetid1, u8 *pnetid2)
Thomas Richter6812baa2017-01-09 16:55:15 +010078{
Hans Wippel890a2cb2019-02-21 13:01:00 +010079 int i;
Thomas Richter6812baa2017-01-09 16:55:15 +010080
Hans Wippel890a2cb2019-02-21 13:01:00 +010081 for (i = 0; i < SMC_MAX_PNETID_LEN; i++) {
82 if ((pnetid1[i] == 0 || pnetid1[i] == SMC_ASCII_BLANK) &&
83 (pnetid2[i] == 0 || pnetid2[i] == SMC_ASCII_BLANK))
Thomas Richter6812baa2017-01-09 16:55:15 +010084 break;
Hans Wippel890a2cb2019-02-21 13:01:00 +010085 if (pnetid1[i] != pnetid2[i])
86 return false;
Thomas Richter6812baa2017-01-09 16:55:15 +010087 }
Hans Wippel890a2cb2019-02-21 13:01:00 +010088 return true;
Thomas Richter6812baa2017-01-09 16:55:15 +010089}
90
91/* Remove a pnetid from the pnet table.
92 */
Hans Wippel64e28b52019-02-21 13:01:02 +010093static int smc_pnet_remove_by_pnetid(struct net *net, char *pnet_name)
Thomas Richter6812baa2017-01-09 16:55:15 +010094{
95 struct smc_pnetentry *pnetelem, *tmp_pe;
Hans Wippel64e28b52019-02-21 13:01:02 +010096 struct smc_pnettable *pnettable;
Hans Wippel890a2cb2019-02-21 13:01:00 +010097 struct smc_ib_device *ibdev;
Hans Wippelf3d74b22019-02-21 13:01:01 +010098 struct smcd_dev *smcd_dev;
Hans Wippel64e28b52019-02-21 13:01:02 +010099 struct smc_net *sn;
Thomas Richter6812baa2017-01-09 16:55:15 +0100100 int rc = -ENOENT;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100101 int ibport;
Thomas Richter6812baa2017-01-09 16:55:15 +0100102
Hans Wippel64e28b52019-02-21 13:01:02 +0100103 /* get pnettable for namespace */
104 sn = net_generic(net, smc_net_id);
105 pnettable = &sn->pnettable;
106
Karsten Graulfdff7042020-04-29 17:10:37 +0200107 /* remove table entry */
Hans Wippel64e28b52019-02-21 13:01:02 +0100108 write_lock(&pnettable->lock);
109 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist,
Thomas Richter6812baa2017-01-09 16:55:15 +0100110 list) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100111 if (!pnet_name ||
112 smc_pnet_match(pnetelem->pnet_name, pnet_name)) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100113 list_del(&pnetelem->list);
Karsten Graul0a99be42020-05-05 15:01:20 +0200114 if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200115 dev_put(pnetelem->ndev);
Karsten Graul0a99be42020-05-05 15:01:20 +0200116 pr_warn_ratelimited("smc: net device %s "
117 "erased user defined "
118 "pnetid %.16s\n",
119 pnetelem->eth_name,
120 pnetelem->pnet_name);
121 }
Thomas Richter6812baa2017-01-09 16:55:15 +0100122 kfree(pnetelem);
123 rc = 0;
Thomas Richter6812baa2017-01-09 16:55:15 +0100124 }
125 }
Hans Wippel64e28b52019-02-21 13:01:02 +0100126 write_unlock(&pnettable->lock);
127
128 /* if this is not the initial namespace, stop here */
129 if (net != &init_net)
130 return rc;
131
Hans Wippel890a2cb2019-02-21 13:01:00 +0100132 /* remove ib devices */
Ursula Braun92f3cb02020-07-08 17:05:13 +0200133 mutex_lock(&smc_ib_devices.mutex);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100134 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
135 for (ibport = 0; ibport < SMC_MAX_PORTS; ibport++) {
136 if (ibdev->pnetid_by_user[ibport] &&
137 (!pnet_name ||
138 smc_pnet_match(pnet_name,
139 ibdev->pnetid[ibport]))) {
Karsten Graul0a99be42020-05-05 15:01:20 +0200140 pr_warn_ratelimited("smc: ib device %s ibport "
141 "%d erased user defined "
142 "pnetid %.16s\n",
143 ibdev->ibdev->name,
144 ibport + 1,
145 ibdev->pnetid[ibport]);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100146 memset(ibdev->pnetid[ibport], 0,
147 SMC_MAX_PNETID_LEN);
148 ibdev->pnetid_by_user[ibport] = false;
149 rc = 0;
150 }
151 }
152 }
Ursula Braun92f3cb02020-07-08 17:05:13 +0200153 mutex_unlock(&smc_ib_devices.mutex);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100154 /* remove smcd devices */
Ursula Braun82087c02020-07-08 17:05:14 +0200155 mutex_lock(&smcd_dev_list.mutex);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100156 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) {
157 if (smcd_dev->pnetid_by_user &&
158 (!pnet_name ||
159 smc_pnet_match(pnet_name, smcd_dev->pnetid))) {
Karsten Graul0a99be42020-05-05 15:01:20 +0200160 pr_warn_ratelimited("smc: smcd device %s "
161 "erased user defined pnetid "
162 "%.16s\n", dev_name(&smcd_dev->dev),
163 smcd_dev->pnetid);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100164 memset(smcd_dev->pnetid, 0, SMC_MAX_PNETID_LEN);
165 smcd_dev->pnetid_by_user = false;
166 rc = 0;
167 }
168 }
Ursula Braun82087c02020-07-08 17:05:14 +0200169 mutex_unlock(&smcd_dev_list.mutex);
Thomas Richter6812baa2017-01-09 16:55:15 +0100170 return rc;
171}
172
Karsten Graulfdff7042020-04-29 17:10:37 +0200173/* Add the reference to a given network device to the pnet table.
174 */
175static int smc_pnet_add_by_ndev(struct net_device *ndev)
176{
177 struct smc_pnetentry *pnetelem, *tmp_pe;
178 struct smc_pnettable *pnettable;
179 struct net *net = dev_net(ndev);
180 struct smc_net *sn;
181 int rc = -ENOENT;
182
183 /* get pnettable for namespace */
184 sn = net_generic(net, smc_net_id);
185 pnettable = &sn->pnettable;
186
187 write_lock(&pnettable->lock);
188 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) {
189 if (pnetelem->type == SMC_PNET_ETH && !pnetelem->ndev &&
190 !strncmp(pnetelem->eth_name, ndev->name, IFNAMSIZ)) {
191 dev_hold(ndev);
192 pnetelem->ndev = ndev;
193 rc = 0;
Karsten Graul0a99be42020-05-05 15:01:20 +0200194 pr_warn_ratelimited("smc: adding net device %s with "
195 "user defined pnetid %.16s\n",
196 pnetelem->eth_name,
197 pnetelem->pnet_name);
Karsten Graulfdff7042020-04-29 17:10:37 +0200198 break;
199 }
200 }
201 write_unlock(&pnettable->lock);
202 return rc;
203}
204
205/* Remove the reference to a given network device from the pnet table.
Thomas Richter6812baa2017-01-09 16:55:15 +0100206 */
207static int smc_pnet_remove_by_ndev(struct net_device *ndev)
208{
209 struct smc_pnetentry *pnetelem, *tmp_pe;
Hans Wippel64e28b52019-02-21 13:01:02 +0100210 struct smc_pnettable *pnettable;
211 struct net *net = dev_net(ndev);
212 struct smc_net *sn;
Thomas Richter6812baa2017-01-09 16:55:15 +0100213 int rc = -ENOENT;
214
Hans Wippel64e28b52019-02-21 13:01:02 +0100215 /* get pnettable for namespace */
216 sn = net_generic(net, smc_net_id);
217 pnettable = &sn->pnettable;
218
219 write_lock(&pnettable->lock);
220 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200221 if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev == ndev) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100222 dev_put(pnetelem->ndev);
Karsten Graulfdff7042020-04-29 17:10:37 +0200223 pnetelem->ndev = NULL;
Thomas Richter6812baa2017-01-09 16:55:15 +0100224 rc = 0;
Karsten Graul0a99be42020-05-05 15:01:20 +0200225 pr_warn_ratelimited("smc: removing net device %s with "
226 "user defined pnetid %.16s\n",
227 pnetelem->eth_name,
228 pnetelem->pnet_name);
Thomas Richter6812baa2017-01-09 16:55:15 +0100229 break;
230 }
231 }
Hans Wippel64e28b52019-02-21 13:01:02 +0100232 write_unlock(&pnettable->lock);
Thomas Richter6812baa2017-01-09 16:55:15 +0100233 return rc;
234}
235
Karsten Graulfdff7042020-04-29 17:10:37 +0200236/* Apply pnetid to ib device when no pnetid is set.
Thomas Richter6812baa2017-01-09 16:55:15 +0100237 */
Karsten Graulfdff7042020-04-29 17:10:37 +0200238static bool smc_pnet_apply_ib(struct smc_ib_device *ib_dev, u8 ib_port,
239 char *pnet_name)
Thomas Richter6812baa2017-01-09 16:55:15 +0100240{
Hans Wippel890a2cb2019-02-21 13:01:00 +0100241 u8 pnet_null[SMC_MAX_PNETID_LEN] = {0};
Karsten Graulfdff7042020-04-29 17:10:37 +0200242 bool applied = false;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100243
Ursula Braun92f3cb02020-07-08 17:05:13 +0200244 mutex_lock(&smc_ib_devices.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200245 if (smc_pnet_match(ib_dev->pnetid[ib_port - 1], pnet_null)) {
246 memcpy(ib_dev->pnetid[ib_port - 1], pnet_name,
247 SMC_MAX_PNETID_LEN);
248 ib_dev->pnetid_by_user[ib_port - 1] = true;
249 applied = true;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100250 }
Ursula Braun92f3cb02020-07-08 17:05:13 +0200251 mutex_unlock(&smc_ib_devices.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200252 return applied;
253}
Hans Wippelf3d74b22019-02-21 13:01:01 +0100254
Karsten Graulfdff7042020-04-29 17:10:37 +0200255/* Apply pnetid to smcd device when no pnetid is set.
256 */
257static bool smc_pnet_apply_smcd(struct smcd_dev *smcd_dev, char *pnet_name)
258{
259 u8 pnet_null[SMC_MAX_PNETID_LEN] = {0};
260 bool applied = false;
261
Ursula Braun82087c02020-07-08 17:05:14 +0200262 mutex_lock(&smcd_dev_list.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200263 if (smc_pnet_match(smcd_dev->pnetid, pnet_null)) {
264 memcpy(smcd_dev->pnetid, pnet_name, SMC_MAX_PNETID_LEN);
265 smcd_dev->pnetid_by_user = true;
266 applied = true;
Hans Wippelf3d74b22019-02-21 13:01:01 +0100267 }
Ursula Braun82087c02020-07-08 17:05:14 +0200268 mutex_unlock(&smcd_dev_list.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200269 return applied;
Thomas Richter6812baa2017-01-09 16:55:15 +0100270}
271
272/* The limit for pnetid is 16 characters.
273 * Valid characters should be (single-byte character set) a-z, A-Z, 0-9.
274 * Lower case letters are converted to upper case.
275 * Interior blanks should not be used.
276 */
277static bool smc_pnetid_valid(const char *pnet_name, char *pnetid)
278{
279 char *bf = skip_spaces(pnet_name);
280 size_t len = strlen(bf);
281 char *end = bf + len;
282
283 if (!len)
284 return false;
285 while (--end >= bf && isspace(*end))
286 ;
Ursula Braun0afff912018-06-28 19:05:05 +0200287 if (end - bf >= SMC_MAX_PNETID_LEN)
Thomas Richter6812baa2017-01-09 16:55:15 +0100288 return false;
289 while (bf <= end) {
290 if (!isalnum(*bf))
291 return false;
292 *pnetid++ = islower(*bf) ? toupper(*bf) : *bf;
293 bf++;
294 }
295 *pnetid = '\0';
296 return true;
297}
298
299/* Find an infiniband device by a given name. The device might not exist. */
Ursula Braun249633a2017-04-10 14:57:57 +0200300static struct smc_ib_device *smc_pnet_find_ib(char *ib_name)
Thomas Richter6812baa2017-01-09 16:55:15 +0100301{
302 struct smc_ib_device *ibdev;
303
Ursula Braun92f3cb02020-07-08 17:05:13 +0200304 mutex_lock(&smc_ib_devices.mutex);
Thomas Richter6812baa2017-01-09 16:55:15 +0100305 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
306 if (!strncmp(ibdev->ibdev->name, ib_name,
Hans Wippelaf5f60c2019-02-21 13:01:03 +0100307 sizeof(ibdev->ibdev->name)) ||
308 !strncmp(dev_name(ibdev->ibdev->dev.parent), ib_name,
309 IB_DEVICE_NAME_MAX - 1)) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100310 goto out;
311 }
312 }
313 ibdev = NULL;
314out:
Ursula Braun92f3cb02020-07-08 17:05:13 +0200315 mutex_unlock(&smc_ib_devices.mutex);
Thomas Richter6812baa2017-01-09 16:55:15 +0100316 return ibdev;
317}
318
Hans Wippelf3d74b22019-02-21 13:01:01 +0100319/* Find an smcd device by a given name. The device might not exist. */
320static struct smcd_dev *smc_pnet_find_smcd(char *smcd_name)
321{
322 struct smcd_dev *smcd_dev;
323
Ursula Braun82087c02020-07-08 17:05:14 +0200324 mutex_lock(&smcd_dev_list.mutex);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100325 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) {
326 if (!strncmp(dev_name(&smcd_dev->dev), smcd_name,
327 IB_DEVICE_NAME_MAX - 1))
328 goto out;
329 }
330 smcd_dev = NULL;
331out:
Ursula Braun82087c02020-07-08 17:05:14 +0200332 mutex_unlock(&smcd_dev_list.mutex);
Hans Wippelf3d74b22019-02-21 13:01:01 +0100333 return smcd_dev;
334}
335
Karsten Graulfdff7042020-04-29 17:10:37 +0200336static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net,
337 char *eth_name, char *pnet_name)
Thomas Richter6812baa2017-01-09 16:55:15 +0100338{
Karsten Graulfdff7042020-04-29 17:10:37 +0200339 struct smc_pnetentry *tmp_pe, *new_pe;
340 struct net_device *ndev, *base_ndev;
341 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
342 bool new_netdev;
Eric Biggersd49baa72018-05-13 17:01:30 -0700343 int rc;
Thomas Richter6812baa2017-01-09 16:55:15 +0100344
Karsten Graulfdff7042020-04-29 17:10:37 +0200345 /* check if (base) netdev already has a pnetid. If there is one, we do
346 * not want to add a pnet table entry
347 */
348 rc = -EEXIST;
349 ndev = dev_get_by_name(net, eth_name); /* dev_hold() */
350 if (ndev) {
351 base_ndev = pnet_find_base_ndev(ndev);
352 if (!smc_pnetid_by_dev_port(base_ndev->dev.parent,
353 base_ndev->dev_port, ndev_pnetid))
354 goto out_put;
355 }
356
357 /* add a new netdev entry to the pnet table if there isn't one */
358 rc = -ENOMEM;
359 new_pe = kzalloc(sizeof(*new_pe), GFP_KERNEL);
360 if (!new_pe)
361 goto out_put;
362 new_pe->type = SMC_PNET_ETH;
363 memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN);
364 strncpy(new_pe->eth_name, eth_name, IFNAMSIZ);
365 new_pe->ndev = ndev;
366
367 rc = -EEXIST;
368 new_netdev = true;
369 write_lock(&pnettable->lock);
370 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
371 if (tmp_pe->type == SMC_PNET_ETH &&
372 !strncmp(tmp_pe->eth_name, eth_name, IFNAMSIZ)) {
373 new_netdev = false;
374 break;
375 }
376 }
377 if (new_netdev) {
378 list_add_tail(&new_pe->list, &pnettable->pnetlist);
379 write_unlock(&pnettable->lock);
380 } else {
381 write_unlock(&pnettable->lock);
382 kfree(new_pe);
383 goto out_put;
384 }
Karsten Graul0a99be42020-05-05 15:01:20 +0200385 if (ndev)
386 pr_warn_ratelimited("smc: net device %s "
387 "applied user defined pnetid %.16s\n",
388 new_pe->eth_name, new_pe->pnet_name);
Karsten Graulfdff7042020-04-29 17:10:37 +0200389 return 0;
390
391out_put:
392 if (ndev)
393 dev_put(ndev);
394 return rc;
395}
396
397static int smc_pnet_add_ib(struct smc_pnettable *pnettable, char *ib_name,
398 u8 ib_port, char *pnet_name)
399{
400 struct smc_pnetentry *tmp_pe, *new_pe;
401 struct smc_ib_device *ib_dev;
402 bool smcddev_applied = true;
403 bool ibdev_applied = true;
404 struct smcd_dev *smcd_dev;
405 bool new_ibdev;
406
407 /* try to apply the pnetid to active devices */
408 ib_dev = smc_pnet_find_ib(ib_name);
Karsten Graul0a99be42020-05-05 15:01:20 +0200409 if (ib_dev) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200410 ibdev_applied = smc_pnet_apply_ib(ib_dev, ib_port, pnet_name);
Karsten Graul0a99be42020-05-05 15:01:20 +0200411 if (ibdev_applied)
412 pr_warn_ratelimited("smc: ib device %s ibport %d "
413 "applied user defined pnetid "
414 "%.16s\n", ib_dev->ibdev->name,
415 ib_port,
416 ib_dev->pnetid[ib_port - 1]);
417 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200418 smcd_dev = smc_pnet_find_smcd(ib_name);
Karsten Graul0a99be42020-05-05 15:01:20 +0200419 if (smcd_dev) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200420 smcddev_applied = smc_pnet_apply_smcd(smcd_dev, pnet_name);
Karsten Graul0a99be42020-05-05 15:01:20 +0200421 if (smcddev_applied)
422 pr_warn_ratelimited("smc: smcd device %s "
423 "applied user defined pnetid "
424 "%.16s\n", dev_name(&smcd_dev->dev),
425 smcd_dev->pnetid);
426 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200427 /* Apply fails when a device has a hardware-defined pnetid set, do not
428 * add a pnet table entry in that case.
429 */
430 if (!ibdev_applied || !smcddev_applied)
431 return -EEXIST;
432
433 /* add a new ib entry to the pnet table if there isn't one */
434 new_pe = kzalloc(sizeof(*new_pe), GFP_KERNEL);
435 if (!new_pe)
436 return -ENOMEM;
437 new_pe->type = SMC_PNET_IB;
438 memcpy(new_pe->pnet_name, pnet_name, SMC_MAX_PNETID_LEN);
439 strncpy(new_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX);
440 new_pe->ib_port = ib_port;
441
442 new_ibdev = true;
443 write_lock(&pnettable->lock);
444 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
445 if (tmp_pe->type == SMC_PNET_IB &&
446 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX)) {
447 new_ibdev = false;
448 break;
449 }
450 }
451 if (new_ibdev) {
452 list_add_tail(&new_pe->list, &pnettable->pnetlist);
453 write_unlock(&pnettable->lock);
454 } else {
455 write_unlock(&pnettable->lock);
456 kfree(new_pe);
457 }
458 return (new_ibdev) ? 0 : -EEXIST;
459}
460
461/* Append a pnetid to the end of the pnet table if not already on this list.
462 */
463static int smc_pnet_enter(struct net *net, struct nlattr *tb[])
464{
465 char pnet_name[SMC_MAX_PNETID_LEN + 1];
466 struct smc_pnettable *pnettable;
467 bool new_netdev = false;
468 bool new_ibdev = false;
469 struct smc_net *sn;
470 u8 ibport = 1;
471 char *string;
472 int rc;
473
474 /* get pnettable for namespace */
475 sn = net_generic(net, smc_net_id);
476 pnettable = &sn->pnettable;
Eric Biggersd49baa72018-05-13 17:01:30 -0700477
478 rc = -EINVAL;
479 if (!tb[SMC_PNETID_NAME])
480 goto error;
481 string = (char *)nla_data(tb[SMC_PNETID_NAME]);
Karsten Graulfdff7042020-04-29 17:10:37 +0200482 if (!smc_pnetid_valid(string, pnet_name))
Eric Biggersd49baa72018-05-13 17:01:30 -0700483 goto error;
484
Hans Wippel890a2cb2019-02-21 13:01:00 +0100485 if (tb[SMC_PNETID_ETHNAME]) {
486 string = (char *)nla_data(tb[SMC_PNETID_ETHNAME]);
Karsten Graulfdff7042020-04-29 17:10:37 +0200487 rc = smc_pnet_add_eth(pnettable, net, string, pnet_name);
488 if (!rc)
489 new_netdev = true;
490 else if (rc != -EEXIST)
Hans Wippel890a2cb2019-02-21 13:01:00 +0100491 goto error;
492 }
Eric Biggersd49baa72018-05-13 17:01:30 -0700493
Hans Wippel64e28b52019-02-21 13:01:02 +0100494 /* if this is not the initial namespace, stop here */
495 if (net != &init_net)
Karsten Graulfdff7042020-04-29 17:10:37 +0200496 return new_netdev ? 0 : -EEXIST;
Hans Wippel64e28b52019-02-21 13:01:02 +0100497
Eric Biggersd49baa72018-05-13 17:01:30 -0700498 rc = -EINVAL;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100499 if (tb[SMC_PNETID_IBNAME]) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200500 string = (char *)nla_data(tb[SMC_PNETID_IBNAME]);
501 string = strim(string);
502 if (tb[SMC_PNETID_IBPORT]) {
503 ibport = nla_get_u8(tb[SMC_PNETID_IBPORT]);
504 if (ibport < 1 || ibport > SMC_MAX_PORTS)
Hans Wippel890a2cb2019-02-21 13:01:00 +0100505 goto error;
506 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200507 rc = smc_pnet_add_ib(pnettable, string, ibport, pnet_name);
508 if (!rc)
509 new_ibdev = true;
510 else if (rc != -EEXIST)
511 goto error;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100512 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200513 return (new_netdev || new_ibdev) ? 0 : -EEXIST;
Thomas Richter6812baa2017-01-09 16:55:15 +0100514
515error:
Thomas Richter6812baa2017-01-09 16:55:15 +0100516 return rc;
517}
518
519/* Convert an smc_pnetentry to a netlink attribute sequence */
Hans Wippel890a2cb2019-02-21 13:01:00 +0100520static int smc_pnet_set_nla(struct sk_buff *msg,
Karsten Graulfdff7042020-04-29 17:10:37 +0200521 struct smc_pnetentry *pnetelem)
Thomas Richter6812baa2017-01-09 16:55:15 +0100522{
Hans Wippel890a2cb2019-02-21 13:01:00 +0100523 if (nla_put_string(msg, SMC_PNETID_NAME, pnetelem->pnet_name))
Thomas Richter6812baa2017-01-09 16:55:15 +0100524 return -1;
Karsten Graulfdff7042020-04-29 17:10:37 +0200525 if (pnetelem->type == SMC_PNET_ETH) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100526 if (nla_put_string(msg, SMC_PNETID_ETHNAME,
Karsten Graulfdff7042020-04-29 17:10:37 +0200527 pnetelem->eth_name))
Hans Wippel890a2cb2019-02-21 13:01:00 +0100528 return -1;
529 } else {
530 if (nla_put_string(msg, SMC_PNETID_ETHNAME, "n/a"))
531 return -1;
532 }
Karsten Graulfdff7042020-04-29 17:10:37 +0200533 if (pnetelem->type == SMC_PNET_IB) {
534 if (nla_put_string(msg, SMC_PNETID_IBNAME, pnetelem->ib_name) ||
Hans Wippel890a2cb2019-02-21 13:01:00 +0100535 nla_put_u8(msg, SMC_PNETID_IBPORT, pnetelem->ib_port))
536 return -1;
537 } else {
538 if (nla_put_string(msg, SMC_PNETID_IBNAME, "n/a") ||
539 nla_put_u8(msg, SMC_PNETID_IBPORT, 0xff))
540 return -1;
541 }
542
Thomas Richter6812baa2017-01-09 16:55:15 +0100543 return 0;
544}
545
Thomas Richter6812baa2017-01-09 16:55:15 +0100546static int smc_pnet_add(struct sk_buff *skb, struct genl_info *info)
547{
548 struct net *net = genl_info_net(info);
Thomas Richter6812baa2017-01-09 16:55:15 +0100549
Karsten Graulfdff7042020-04-29 17:10:37 +0200550 return smc_pnet_enter(net, info->attrs);
Thomas Richter6812baa2017-01-09 16:55:15 +0100551}
552
553static int smc_pnet_del(struct sk_buff *skb, struct genl_info *info)
554{
Hans Wippel64e28b52019-02-21 13:01:02 +0100555 struct net *net = genl_info_net(info);
556
Eric Biggersd49baa72018-05-13 17:01:30 -0700557 if (!info->attrs[SMC_PNETID_NAME])
558 return -EINVAL;
Hans Wippel64e28b52019-02-21 13:01:02 +0100559 return smc_pnet_remove_by_pnetid(net,
Thomas Richter6812baa2017-01-09 16:55:15 +0100560 (char *)nla_data(info->attrs[SMC_PNETID_NAME]));
561}
562
563static int smc_pnet_dump_start(struct netlink_callback *cb)
564{
565 cb->args[0] = 0;
566 return 0;
567}
568
569static int smc_pnet_dumpinfo(struct sk_buff *skb,
570 u32 portid, u32 seq, u32 flags,
Karsten Graulfdff7042020-04-29 17:10:37 +0200571 struct smc_pnetentry *pnetelem)
Thomas Richter6812baa2017-01-09 16:55:15 +0100572{
573 void *hdr;
574
575 hdr = genlmsg_put(skb, portid, seq, &smc_pnet_nl_family,
576 flags, SMC_PNETID_GET);
577 if (!hdr)
578 return -ENOMEM;
579 if (smc_pnet_set_nla(skb, pnetelem) < 0) {
580 genlmsg_cancel(skb, hdr);
581 return -EMSGSIZE;
582 }
583 genlmsg_end(skb, hdr);
584 return 0;
585}
586
Hans Wippel64e28b52019-02-21 13:01:02 +0100587static int _smc_pnet_dump(struct net *net, struct sk_buff *skb, u32 portid,
588 u32 seq, u8 *pnetid, int start_idx)
Thomas Richter6812baa2017-01-09 16:55:15 +0100589{
Hans Wippel64e28b52019-02-21 13:01:02 +0100590 struct smc_pnettable *pnettable;
Thomas Richter6812baa2017-01-09 16:55:15 +0100591 struct smc_pnetentry *pnetelem;
Hans Wippel64e28b52019-02-21 13:01:02 +0100592 struct smc_net *sn;
Thomas Richter6812baa2017-01-09 16:55:15 +0100593 int idx = 0;
594
Hans Wippel64e28b52019-02-21 13:01:02 +0100595 /* get pnettable for namespace */
596 sn = net_generic(net, smc_net_id);
597 pnettable = &sn->pnettable;
598
Karsten Graulfdff7042020-04-29 17:10:37 +0200599 /* dump pnettable entries */
Hans Wippel64e28b52019-02-21 13:01:02 +0100600 read_lock(&pnettable->lock);
601 list_for_each_entry(pnetelem, &pnettable->pnetlist, list) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100602 if (pnetid && !smc_pnet_match(pnetelem->pnet_name, pnetid))
Thomas Richter6812baa2017-01-09 16:55:15 +0100603 continue;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100604 if (idx++ < start_idx)
605 continue;
Karsten Graulfdff7042020-04-29 17:10:37 +0200606 /* if this is not the initial namespace, dump only netdev */
607 if (net != &init_net && pnetelem->type != SMC_PNET_ETH)
608 continue;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100609 if (smc_pnet_dumpinfo(skb, portid, seq, NLM_F_MULTI,
Karsten Graulfdff7042020-04-29 17:10:37 +0200610 pnetelem)) {
Thomas Richter6812baa2017-01-09 16:55:15 +0100611 --idx;
612 break;
613 }
614 }
Hans Wippel64e28b52019-02-21 13:01:02 +0100615 read_unlock(&pnettable->lock);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100616 return idx;
617}
618
619static int smc_pnet_dump(struct sk_buff *skb, struct netlink_callback *cb)
620{
Hans Wippel64e28b52019-02-21 13:01:02 +0100621 struct net *net = sock_net(skb->sk);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100622 int idx;
623
Hans Wippel64e28b52019-02-21 13:01:02 +0100624 idx = _smc_pnet_dump(net, skb, NETLINK_CB(cb->skb).portid,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100625 cb->nlh->nlmsg_seq, NULL, cb->args[0]);
626
627 cb->args[0] = idx;
Thomas Richter6812baa2017-01-09 16:55:15 +0100628 return skb->len;
629}
630
Hans Wippel890a2cb2019-02-21 13:01:00 +0100631/* Retrieve one PNETID entry */
632static int smc_pnet_get(struct sk_buff *skb, struct genl_info *info)
633{
Hans Wippel64e28b52019-02-21 13:01:02 +0100634 struct net *net = genl_info_net(info);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100635 struct sk_buff *msg;
636 void *hdr;
637
638 if (!info->attrs[SMC_PNETID_NAME])
639 return -EINVAL;
640
641 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
642 if (!msg)
643 return -ENOMEM;
644
Hans Wippel64e28b52019-02-21 13:01:02 +0100645 _smc_pnet_dump(net, msg, info->snd_portid, info->snd_seq,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100646 nla_data(info->attrs[SMC_PNETID_NAME]), 0);
647
648 /* finish multi part message and send it */
649 hdr = nlmsg_put(msg, info->snd_portid, info->snd_seq, NLMSG_DONE, 0,
650 NLM_F_MULTI);
651 if (!hdr) {
652 nlmsg_free(msg);
653 return -EMSGSIZE;
654 }
655 return genlmsg_reply(msg, info);
656}
657
Thomas Richter6812baa2017-01-09 16:55:15 +0100658/* Remove and delete all pnetids from pnet table.
659 */
660static int smc_pnet_flush(struct sk_buff *skb, struct genl_info *info)
661{
Hans Wippel64e28b52019-02-21 13:01:02 +0100662 struct net *net = genl_info_net(info);
663
Karsten Graul8ef659f2019-04-11 11:17:33 +0200664 smc_pnet_remove_by_pnetid(net, NULL);
665 return 0;
Thomas Richter6812baa2017-01-09 16:55:15 +0100666}
667
668/* SMC_PNETID generic netlink operation definition */
669static const struct genl_ops smc_pnet_ops[] = {
670 {
671 .cmd = SMC_PNETID_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +0200672 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Hans Wippel29237d22020-01-21 01:04:46 +0100673 /* can be retrieved by unprivileged users */
Thomas Richter6812baa2017-01-09 16:55:15 +0100674 .doit = smc_pnet_get,
675 .dumpit = smc_pnet_dump,
676 .start = smc_pnet_dump_start
677 },
678 {
679 .cmd = SMC_PNETID_ADD,
Johannes Bergef6243a2019-04-26 14:07:31 +0200680 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Thomas Richter6812baa2017-01-09 16:55:15 +0100681 .flags = GENL_ADMIN_PERM,
Thomas Richter6812baa2017-01-09 16:55:15 +0100682 .doit = smc_pnet_add
683 },
684 {
685 .cmd = SMC_PNETID_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +0200686 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Thomas Richter6812baa2017-01-09 16:55:15 +0100687 .flags = GENL_ADMIN_PERM,
Thomas Richter6812baa2017-01-09 16:55:15 +0100688 .doit = smc_pnet_del
689 },
690 {
691 .cmd = SMC_PNETID_FLUSH,
Johannes Bergef6243a2019-04-26 14:07:31 +0200692 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Thomas Richter6812baa2017-01-09 16:55:15 +0100693 .flags = GENL_ADMIN_PERM,
Thomas Richter6812baa2017-01-09 16:55:15 +0100694 .doit = smc_pnet_flush
695 }
696};
697
698/* SMC_PNETID family definition */
Johannes Berg56ce3c52018-09-20 09:27:30 +0200699static struct genl_family smc_pnet_nl_family __ro_after_init = {
Thomas Richter6812baa2017-01-09 16:55:15 +0100700 .hdrsize = 0,
701 .name = SMCR_GENL_FAMILY_NAME,
702 .version = SMCR_GENL_FAMILY_VERSION,
703 .maxattr = SMC_PNETID_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100704 .policy = smc_pnet_policy,
Thomas Richter6812baa2017-01-09 16:55:15 +0100705 .netnsok = true,
706 .module = THIS_MODULE,
707 .ops = smc_pnet_ops,
708 .n_ops = ARRAY_SIZE(smc_pnet_ops)
709};
710
711static int smc_pnet_netdev_event(struct notifier_block *this,
712 unsigned long event, void *ptr)
713{
714 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
715
716 switch (event) {
717 case NETDEV_REBOOT:
718 case NETDEV_UNREGISTER:
719 smc_pnet_remove_by_ndev(event_dev);
Ursula Braunbe6a3f32018-06-28 19:05:04 +0200720 return NOTIFY_OK;
Karsten Graulfdff7042020-04-29 17:10:37 +0200721 case NETDEV_REGISTER:
722 smc_pnet_add_by_ndev(event_dev);
723 return NOTIFY_OK;
Thomas Richter6812baa2017-01-09 16:55:15 +0100724 default:
Ursula Braunbe6a3f32018-06-28 19:05:04 +0200725 return NOTIFY_DONE;
Thomas Richter6812baa2017-01-09 16:55:15 +0100726 }
Thomas Richter6812baa2017-01-09 16:55:15 +0100727}
728
729static struct notifier_block smc_netdev_notifier = {
730 .notifier_call = smc_pnet_netdev_event
731};
732
Hans Wippel64e28b52019-02-21 13:01:02 +0100733/* init network namespace */
734int smc_pnet_net_init(struct net *net)
735{
736 struct smc_net *sn = net_generic(net, smc_net_id);
737 struct smc_pnettable *pnettable = &sn->pnettable;
738
739 INIT_LIST_HEAD(&pnettable->pnetlist);
740 rwlock_init(&pnettable->lock);
741
742 return 0;
743}
744
Thomas Richter6812baa2017-01-09 16:55:15 +0100745int __init smc_pnet_init(void)
746{
747 int rc;
748
749 rc = genl_register_family(&smc_pnet_nl_family);
750 if (rc)
751 return rc;
752 rc = register_netdevice_notifier(&smc_netdev_notifier);
753 if (rc)
754 genl_unregister_family(&smc_pnet_nl_family);
755 return rc;
756}
757
Hans Wippel64e28b52019-02-21 13:01:02 +0100758/* exit network namespace */
759void smc_pnet_net_exit(struct net *net)
760{
761 /* flush pnet table */
762 smc_pnet_remove_by_pnetid(net, NULL);
763}
764
Thomas Richter6812baa2017-01-09 16:55:15 +0100765void smc_pnet_exit(void)
766{
Thomas Richter6812baa2017-01-09 16:55:15 +0100767 unregister_netdevice_notifier(&smc_netdev_notifier);
768 genl_unregister_family(&smc_pnet_nl_family);
769}
770
Ursula Braun0afff912018-06-28 19:05:05 +0200771/* Determine one base device for stacked net devices.
772 * If the lower device level contains more than one devices
773 * (for instance with bonding slaves), just the first device
774 * is used to reach a base device.
Thomas Richter6812baa2017-01-09 16:55:15 +0100775 */
Ursula Braun0afff912018-06-28 19:05:05 +0200776static struct net_device *pnet_find_base_ndev(struct net_device *ndev)
Thomas Richter6812baa2017-01-09 16:55:15 +0100777{
Ursula Braun0afff912018-06-28 19:05:05 +0200778 int i, nest_lvl;
779
780 rtnl_lock();
Taehee Yoof3b0a182019-10-21 18:47:58 +0000781 nest_lvl = ndev->lower_level;
Ursula Braun0afff912018-06-28 19:05:05 +0200782 for (i = 0; i < nest_lvl; i++) {
783 struct list_head *lower = &ndev->adj_list.lower;
784
785 if (list_empty(lower))
786 break;
787 lower = lower->next;
788 ndev = netdev_lower_get_next(ndev, &lower);
789 }
790 rtnl_unlock();
791 return ndev;
792}
793
Hans Wippel64e28b52019-02-21 13:01:02 +0100794static int smc_pnet_find_ndev_pnetid_by_table(struct net_device *ndev,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100795 u8 *pnetid)
796{
Hans Wippel64e28b52019-02-21 13:01:02 +0100797 struct smc_pnettable *pnettable;
798 struct net *net = dev_net(ndev);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100799 struct smc_pnetentry *pnetelem;
Hans Wippel64e28b52019-02-21 13:01:02 +0100800 struct smc_net *sn;
Hans Wippel890a2cb2019-02-21 13:01:00 +0100801 int rc = -ENOENT;
802
Hans Wippel64e28b52019-02-21 13:01:02 +0100803 /* get pnettable for namespace */
804 sn = net_generic(net, smc_net_id);
805 pnettable = &sn->pnettable;
806
807 read_lock(&pnettable->lock);
808 list_for_each_entry(pnetelem, &pnettable->pnetlist, list) {
Karsten Graulfdff7042020-04-29 17:10:37 +0200809 if (pnetelem->type == SMC_PNET_ETH && ndev == pnetelem->ndev) {
Hans Wippel890a2cb2019-02-21 13:01:00 +0100810 /* get pnetid of netdev device */
811 memcpy(pnetid, pnetelem->pnet_name, SMC_MAX_PNETID_LEN);
812 rc = 0;
813 break;
814 }
815 }
Hans Wippel64e28b52019-02-21 13:01:02 +0100816 read_unlock(&pnettable->lock);
Hans Wippel890a2cb2019-02-21 13:01:00 +0100817 return rc;
818}
819
Karsten Graulfdff7042020-04-29 17:10:37 +0200820/* find a roce device for the given pnetid */
821static void _smc_pnet_find_roce_by_pnetid(u8 *pnet_id,
Karsten Graul6c868a32020-05-01 12:48:11 +0200822 struct smc_init_info *ini,
823 struct smc_ib_device *known_dev)
Karsten Graulfdff7042020-04-29 17:10:37 +0200824{
825 struct smc_ib_device *ibdev;
826 int i;
827
828 ini->ib_dev = NULL;
Ursula Braun92f3cb02020-07-08 17:05:13 +0200829 mutex_lock(&smc_ib_devices.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200830 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
Karsten Graul6c868a32020-05-01 12:48:11 +0200831 if (ibdev == known_dev)
832 continue;
Karsten Graulfdff7042020-04-29 17:10:37 +0200833 for (i = 1; i <= SMC_MAX_PORTS; i++) {
834 if (!rdma_is_port_valid(ibdev->ibdev, i))
835 continue;
836 if (smc_pnet_match(ibdev->pnetid[i - 1], pnet_id) &&
837 smc_ib_port_active(ibdev, i) &&
838 !test_bit(i - 1, ibdev->ports_going_away) &&
839 !smc_ib_determine_gid(ibdev, i, ini->vlan_id,
840 ini->ib_gid, NULL)) {
841 ini->ib_dev = ibdev;
842 ini->ib_port = i;
843 goto out;
844 }
845 }
846 }
847out:
Ursula Braun92f3cb02020-07-08 17:05:13 +0200848 mutex_unlock(&smc_ib_devices.mutex);
Karsten Graulfdff7042020-04-29 17:10:37 +0200849}
850
Karsten Graul6c868a32020-05-01 12:48:11 +0200851/* find alternate roce device with same pnet_id and vlan_id */
852void smc_pnet_find_alt_roce(struct smc_link_group *lgr,
853 struct smc_init_info *ini,
854 struct smc_ib_device *known_dev)
855{
856 _smc_pnet_find_roce_by_pnetid(lgr->pnet_id, ini, known_dev);
857}
858
Ursula Braun54903572019-02-28 15:10:08 +0100859/* if handshake network device belongs to a roce device, return its
860 * IB device and port
861 */
862static void smc_pnet_find_rdma_dev(struct net_device *netdev,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200863 struct smc_init_info *ini)
Ursula Braun54903572019-02-28 15:10:08 +0100864{
865 struct smc_ib_device *ibdev;
866
Ursula Braun92f3cb02020-07-08 17:05:13 +0200867 mutex_lock(&smc_ib_devices.mutex);
Ursula Braun54903572019-02-28 15:10:08 +0100868 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
869 struct net_device *ndev;
870 int i;
871
872 for (i = 1; i <= SMC_MAX_PORTS; i++) {
873 if (!rdma_is_port_valid(ibdev->ibdev, i))
874 continue;
875 if (!ibdev->ibdev->ops.get_netdev)
876 continue;
877 ndev = ibdev->ibdev->ops.get_netdev(ibdev->ibdev, i);
878 if (!ndev)
879 continue;
880 dev_put(ndev);
881 if (netdev == ndev &&
882 smc_ib_port_active(ibdev, i) &&
Ursula Braunc3d94942019-10-09 10:07:46 +0200883 !test_bit(i - 1, ibdev->ports_going_away) &&
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200884 !smc_ib_determine_gid(ibdev, i, ini->vlan_id,
885 ini->ib_gid, NULL)) {
886 ini->ib_dev = ibdev;
887 ini->ib_port = i;
Ursula Braun54903572019-02-28 15:10:08 +0100888 break;
889 }
890 }
891 }
Ursula Braun92f3cb02020-07-08 17:05:13 +0200892 mutex_unlock(&smc_ib_devices.mutex);
Ursula Braun54903572019-02-28 15:10:08 +0100893}
894
Ursula Braun0afff912018-06-28 19:05:05 +0200895/* Determine the corresponding IB device port based on the hardware PNETID.
Ursula Braun7005ada2018-07-25 16:35:31 +0200896 * Searching stops at the first matching active IB device port with vlan_id
897 * configured.
Ursula Braun54903572019-02-28 15:10:08 +0100898 * If nothing found, check pnetid table.
899 * If nothing found, try to use handshake device
Ursula Braun0afff912018-06-28 19:05:05 +0200900 */
901static void smc_pnet_find_roce_by_pnetid(struct net_device *ndev,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200902 struct smc_init_info *ini)
Ursula Braun0afff912018-06-28 19:05:05 +0200903{
904 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
Ursula Braun0afff912018-06-28 19:05:05 +0200905
906 ndev = pnet_find_base_ndev(ndev);
907 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
Hans Wippel890a2cb2019-02-21 13:01:00 +0100908 ndev_pnetid) &&
Ursula Braun54903572019-02-28 15:10:08 +0100909 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid)) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200910 smc_pnet_find_rdma_dev(ndev, ini);
Ursula Braun0afff912018-06-28 19:05:05 +0200911 return; /* pnetid could not be determined */
Ursula Braun54903572019-02-28 15:10:08 +0100912 }
Karsten Graul6c868a32020-05-01 12:48:11 +0200913 _smc_pnet_find_roce_by_pnetid(ndev_pnetid, ini, NULL);
Ursula Braun0afff912018-06-28 19:05:05 +0200914}
915
Hans Wippel1619f772018-06-28 19:05:08 +0200916static void smc_pnet_find_ism_by_pnetid(struct net_device *ndev,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200917 struct smc_init_info *ini)
Hans Wippel1619f772018-06-28 19:05:08 +0200918{
919 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
920 struct smcd_dev *ismdev;
921
922 ndev = pnet_find_base_ndev(ndev);
923 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
Hans Wippelf3d74b22019-02-21 13:01:01 +0100924 ndev_pnetid) &&
925 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid))
Hans Wippel1619f772018-06-28 19:05:08 +0200926 return; /* pnetid could not be determined */
927
Ursula Braun82087c02020-07-08 17:05:14 +0200928 mutex_lock(&smcd_dev_list.mutex);
Hans Wippel1619f772018-06-28 19:05:08 +0200929 list_for_each_entry(ismdev, &smcd_dev_list.list, list) {
Ursula Braunc3d94942019-10-09 10:07:46 +0200930 if (smc_pnet_match(ismdev->pnetid, ndev_pnetid) &&
931 !ismdev->going_away) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200932 ini->ism_dev = ismdev;
Hans Wippel1619f772018-06-28 19:05:08 +0200933 break;
934 }
935 }
Ursula Braun82087c02020-07-08 17:05:14 +0200936 mutex_unlock(&smcd_dev_list.mutex);
Hans Wippel1619f772018-06-28 19:05:08 +0200937}
938
Ursula Braun0afff912018-06-28 19:05:05 +0200939/* PNET table analysis for a given sock:
940 * determine ib_device and port belonging to used internal TCP socket
941 * ethernet interface.
942 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200943void smc_pnet_find_roce_resource(struct sock *sk, struct smc_init_info *ini)
Ursula Braun0afff912018-06-28 19:05:05 +0200944{
945 struct dst_entry *dst = sk_dst_get(sk);
946
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200947 ini->ib_dev = NULL;
948 ini->ib_port = 0;
Ursula Braun0afff912018-06-28 19:05:05 +0200949 if (!dst)
950 goto out;
951 if (!dst->dev)
952 goto out_rel;
953
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200954 smc_pnet_find_roce_by_pnetid(dst->dev, ini);
Ursula Braun0afff912018-06-28 19:05:05 +0200955
Thomas Richter6812baa2017-01-09 16:55:15 +0100956out_rel:
957 dst_release(dst);
Ursula Braun0afff912018-06-28 19:05:05 +0200958out:
959 return;
Thomas Richter6812baa2017-01-09 16:55:15 +0100960}
Hans Wippel1619f772018-06-28 19:05:08 +0200961
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200962void smc_pnet_find_ism_resource(struct sock *sk, struct smc_init_info *ini)
Hans Wippel1619f772018-06-28 19:05:08 +0200963{
964 struct dst_entry *dst = sk_dst_get(sk);
965
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200966 ini->ism_dev = NULL;
Hans Wippel1619f772018-06-28 19:05:08 +0200967 if (!dst)
968 goto out;
969 if (!dst->dev)
970 goto out_rel;
971
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200972 smc_pnet_find_ism_by_pnetid(dst->dev, ini);
Hans Wippel1619f772018-06-28 19:05:08 +0200973
974out_rel:
975 dst_release(dst);
976out:
977 return;
978}
Karsten Graulfdff7042020-04-29 17:10:37 +0200979
980/* Lookup and apply a pnet table entry to the given ib device.
981 */
982int smc_pnetid_by_table_ib(struct smc_ib_device *smcibdev, u8 ib_port)
983{
984 char *ib_name = smcibdev->ibdev->name;
985 struct smc_pnettable *pnettable;
986 struct smc_pnetentry *tmp_pe;
987 struct smc_net *sn;
988 int rc = -ENOENT;
989
990 /* get pnettable for init namespace */
991 sn = net_generic(&init_net, smc_net_id);
992 pnettable = &sn->pnettable;
993
994 read_lock(&pnettable->lock);
995 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
996 if (tmp_pe->type == SMC_PNET_IB &&
997 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX) &&
998 tmp_pe->ib_port == ib_port) {
999 smc_pnet_apply_ib(smcibdev, ib_port, tmp_pe->pnet_name);
1000 rc = 0;
1001 break;
1002 }
1003 }
1004 read_unlock(&pnettable->lock);
1005
1006 return rc;
1007}
1008
1009/* Lookup and apply a pnet table entry to the given smcd device.
1010 */
1011int smc_pnetid_by_table_smcd(struct smcd_dev *smcddev)
1012{
1013 const char *ib_name = dev_name(&smcddev->dev);
1014 struct smc_pnettable *pnettable;
1015 struct smc_pnetentry *tmp_pe;
1016 struct smc_net *sn;
1017 int rc = -ENOENT;
1018
1019 /* get pnettable for init namespace */
1020 sn = net_generic(&init_net, smc_net_id);
1021 pnettable = &sn->pnettable;
1022
1023 read_lock(&pnettable->lock);
1024 list_for_each_entry(tmp_pe, &pnettable->pnetlist, list) {
1025 if (tmp_pe->type == SMC_PNET_IB &&
1026 !strncmp(tmp_pe->ib_name, ib_name, IB_DEVICE_NAME_MAX)) {
1027 smc_pnet_apply_smcd(smcddev, tmp_pe->pnet_name);
1028 rc = 0;
1029 break;
1030 }
1031 }
1032 read_unlock(&pnettable->lock);
1033
1034 return rc;
1035}