1 // SPDX-License-Identifier: GPL-2.0
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * Generic netlink support functions to configure an SMC-R PNET table
7 * Copyright IBM Corp. 2016
9 * Author(s): Thomas Richter <tmricht@linux.vnet.ibm.com>
12 #include <linux/module.h>
13 #include <linux/list.h>
14 #include <linux/ctype.h>
15 #include <linux/mutex.h>
16 #include <net/netlink.h>
17 #include <net/genetlink.h>
19 #include <uapi/linux/if.h>
20 #include <uapi/linux/smc.h>
22 #include <rdma/ib_verbs.h>
24 #include <net/netns/generic.h>
25 #include "smc_netns.h"
32 #define SMC_ASCII_BLANK 32
34 static struct net_device *pnet_find_base_ndev(struct net_device *ndev);
36 static const struct nla_policy smc_pnet_policy[SMC_PNETID_MAX + 1] = {
38 .type = NLA_NUL_STRING,
39 .len = SMC_MAX_PNETID_LEN
41 [SMC_PNETID_ETHNAME] = {
42 .type = NLA_NUL_STRING,
45 [SMC_PNETID_IBNAME] = {
46 .type = NLA_NUL_STRING,
47 .len = IB_DEVICE_NAME_MAX - 1
49 [SMC_PNETID_IBPORT] = { .type = NLA_U8 }
52 static struct genl_family smc_pnet_nl_family;
54 enum smc_pnet_nametype {
59 /* pnet entry stored in pnet table */
60 struct smc_pnetentry {
61 struct list_head list;
62 char pnet_name[SMC_MAX_PNETID_LEN + 1];
63 enum smc_pnet_nametype type;
66 char eth_name[IFNAMSIZ + 1];
67 struct net_device *ndev;
70 char ib_name[IB_DEVICE_NAME_MAX + 1];
76 /* Check if two given pnetids match */
77 static bool smc_pnet_match(u8 *pnetid1, u8 *pnetid2)
81 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))
85 if (pnetid1[i] != pnetid2[i])
91 /* Remove a pnetid from the pnet table.
93 static int smc_pnet_remove_by_pnetid(struct net *net, char *pnet_name)
95 struct smc_pnetentry *pnetelem, *tmp_pe;
96 struct smc_pnettable *pnettable;
97 struct smc_ib_device *ibdev;
98 struct smcd_dev *smcd_dev;
103 /* get pnettable for namespace */
104 sn = net_generic(net, smc_net_id);
105 pnettable = &sn->pnettable;
107 /* remove table entry */
108 write_lock(&pnettable->lock);
109 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist,
112 smc_pnet_match(pnetelem->pnet_name, pnet_name)) {
113 list_del(&pnetelem->list);
114 if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev) {
115 dev_put(pnetelem->ndev);
116 pr_warn_ratelimited("smc: net device %s "
117 "erased user defined "
120 pnetelem->pnet_name);
126 write_unlock(&pnettable->lock);
128 /* if this is not the initial namespace, stop here */
129 if (net != &init_net)
132 /* remove ib devices */
133 mutex_lock(&smc_ib_devices.mutex);
134 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] &&
138 smc_pnet_match(pnet_name,
139 ibdev->pnetid[ibport]))) {
140 pr_warn_ratelimited("smc: ib device %s ibport "
141 "%d erased user defined "
145 ibdev->pnetid[ibport]);
146 memset(ibdev->pnetid[ibport], 0,
148 ibdev->pnetid_by_user[ibport] = false;
153 mutex_unlock(&smc_ib_devices.mutex);
154 /* remove smcd devices */
155 mutex_lock(&smcd_dev_list.mutex);
156 list_for_each_entry(smcd_dev, &smcd_dev_list.list, list) {
157 if (smcd_dev->pnetid_by_user &&
159 smc_pnet_match(pnet_name, smcd_dev->pnetid))) {
160 pr_warn_ratelimited("smc: smcd device %s "
161 "erased user defined pnetid "
162 "%.16s\n", dev_name(&smcd_dev->dev),
164 memset(smcd_dev->pnetid, 0, SMC_MAX_PNETID_LEN);
165 smcd_dev->pnetid_by_user = false;
169 mutex_unlock(&smcd_dev_list.mutex);
173 /* Add the reference to a given network device to the pnet table.
175 static int smc_pnet_add_by_ndev(struct net_device *ndev)
177 struct smc_pnetentry *pnetelem, *tmp_pe;
178 struct smc_pnettable *pnettable;
179 struct net *net = dev_net(ndev);
183 /* get pnettable for namespace */
184 sn = net_generic(net, smc_net_id);
185 pnettable = &sn->pnettable;
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)) {
192 pnetelem->ndev = ndev;
194 pr_warn_ratelimited("smc: adding net device %s with "
195 "user defined pnetid %.16s\n",
197 pnetelem->pnet_name);
201 write_unlock(&pnettable->lock);
205 /* Remove the reference to a given network device from the pnet table.
207 static int smc_pnet_remove_by_ndev(struct net_device *ndev)
209 struct smc_pnetentry *pnetelem, *tmp_pe;
210 struct smc_pnettable *pnettable;
211 struct net *net = dev_net(ndev);
215 /* get pnettable for namespace */
216 sn = net_generic(net, smc_net_id);
217 pnettable = &sn->pnettable;
219 write_lock(&pnettable->lock);
220 list_for_each_entry_safe(pnetelem, tmp_pe, &pnettable->pnetlist, list) {
221 if (pnetelem->type == SMC_PNET_ETH && pnetelem->ndev == ndev) {
222 dev_put(pnetelem->ndev);
223 pnetelem->ndev = NULL;
225 pr_warn_ratelimited("smc: removing net device %s with "
226 "user defined pnetid %.16s\n",
228 pnetelem->pnet_name);
232 write_unlock(&pnettable->lock);
236 /* Apply pnetid to ib device when no pnetid is set.
238 static bool smc_pnet_apply_ib(struct smc_ib_device *ib_dev, u8 ib_port,
241 u8 pnet_null[SMC_MAX_PNETID_LEN] = {0};
242 bool applied = false;
244 mutex_lock(&smc_ib_devices.mutex);
245 if (smc_pnet_match(ib_dev->pnetid[ib_port - 1], pnet_null)) {
246 memcpy(ib_dev->pnetid[ib_port - 1], pnet_name,
248 ib_dev->pnetid_by_user[ib_port - 1] = true;
251 mutex_unlock(&smc_ib_devices.mutex);
255 /* Apply pnetid to smcd device when no pnetid is set.
257 static bool smc_pnet_apply_smcd(struct smcd_dev *smcd_dev, char *pnet_name)
259 u8 pnet_null[SMC_MAX_PNETID_LEN] = {0};
260 bool applied = false;
262 mutex_lock(&smcd_dev_list.mutex);
263 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;
268 mutex_unlock(&smcd_dev_list.mutex);
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.
277 static bool smc_pnetid_valid(const char *pnet_name, char *pnetid)
279 char *bf = skip_spaces(pnet_name);
280 size_t len = strlen(bf);
281 char *end = bf + len;
285 while (--end >= bf && isspace(*end))
287 if (end - bf >= SMC_MAX_PNETID_LEN)
292 *pnetid++ = islower(*bf) ? toupper(*bf) : *bf;
299 /* Find an infiniband device by a given name. The device might not exist. */
300 static struct smc_ib_device *smc_pnet_find_ib(char *ib_name)
302 struct smc_ib_device *ibdev;
304 mutex_lock(&smc_ib_devices.mutex);
305 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
306 if (!strncmp(ibdev->ibdev->name, ib_name,
307 sizeof(ibdev->ibdev->name)) ||
308 !strncmp(dev_name(ibdev->ibdev->dev.parent), ib_name,
309 IB_DEVICE_NAME_MAX - 1)) {
315 mutex_unlock(&smc_ib_devices.mutex);
319 /* Find an smcd device by a given name. The device might not exist. */
320 static struct smcd_dev *smc_pnet_find_smcd(char *smcd_name)
322 struct smcd_dev *smcd_dev;
324 mutex_lock(&smcd_dev_list.mutex);
325 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))
332 mutex_unlock(&smcd_dev_list.mutex);
336 static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net,
337 char *eth_name, char *pnet_name)
339 struct smc_pnetentry *tmp_pe, *new_pe;
340 struct net_device *ndev, *base_ndev;
341 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
345 /* check if (base) netdev already has a pnetid. If there is one, we do
346 * not want to add a pnet table entry
349 ndev = dev_get_by_name(net, eth_name); /* dev_hold() */
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))
357 /* add a new netdev entry to the pnet table if there isn't one */
359 new_pe = kzalloc(sizeof(*new_pe), GFP_KERNEL);
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);
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)) {
378 list_add_tail(&new_pe->list, &pnettable->pnetlist);
379 write_unlock(&pnettable->lock);
381 write_unlock(&pnettable->lock);
386 pr_warn_ratelimited("smc: net device %s "
387 "applied user defined pnetid %.16s\n",
388 new_pe->eth_name, new_pe->pnet_name);
397 static int smc_pnet_add_ib(struct smc_pnettable *pnettable, char *ib_name,
398 u8 ib_port, char *pnet_name)
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;
407 /* try to apply the pnetid to active devices */
408 ib_dev = smc_pnet_find_ib(ib_name);
410 ibdev_applied = smc_pnet_apply_ib(ib_dev, ib_port, pnet_name);
412 pr_warn_ratelimited("smc: ib device %s ibport %d "
413 "applied user defined pnetid "
414 "%.16s\n", ib_dev->ibdev->name,
416 ib_dev->pnetid[ib_port - 1]);
418 smcd_dev = smc_pnet_find_smcd(ib_name);
420 smcddev_applied = smc_pnet_apply_smcd(smcd_dev, pnet_name);
422 pr_warn_ratelimited("smc: smcd device %s "
423 "applied user defined pnetid "
424 "%.16s\n", dev_name(&smcd_dev->dev),
427 /* Apply fails when a device has a hardware-defined pnetid set, do not
428 * add a pnet table entry in that case.
430 if (!ibdev_applied || !smcddev_applied)
433 /* add a new ib entry to the pnet table if there isn't one */
434 new_pe = kzalloc(sizeof(*new_pe), GFP_KERNEL);
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;
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)) {
452 list_add_tail(&new_pe->list, &pnettable->pnetlist);
453 write_unlock(&pnettable->lock);
455 write_unlock(&pnettable->lock);
458 return (new_ibdev) ? 0 : -EEXIST;
461 /* Append a pnetid to the end of the pnet table if not already on this list.
463 static int smc_pnet_enter(struct net *net, struct nlattr *tb[])
465 char pnet_name[SMC_MAX_PNETID_LEN + 1];
466 struct smc_pnettable *pnettable;
467 bool new_netdev = false;
468 bool new_ibdev = false;
474 /* get pnettable for namespace */
475 sn = net_generic(net, smc_net_id);
476 pnettable = &sn->pnettable;
479 if (!tb[SMC_PNETID_NAME])
481 string = (char *)nla_data(tb[SMC_PNETID_NAME]);
482 if (!smc_pnetid_valid(string, pnet_name))
485 if (tb[SMC_PNETID_ETHNAME]) {
486 string = (char *)nla_data(tb[SMC_PNETID_ETHNAME]);
487 rc = smc_pnet_add_eth(pnettable, net, string, pnet_name);
490 else if (rc != -EEXIST)
494 /* if this is not the initial namespace, stop here */
495 if (net != &init_net)
496 return new_netdev ? 0 : -EEXIST;
499 if (tb[SMC_PNETID_IBNAME]) {
500 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)
507 rc = smc_pnet_add_ib(pnettable, string, ibport, pnet_name);
510 else if (rc != -EEXIST)
513 return (new_netdev || new_ibdev) ? 0 : -EEXIST;
519 /* Convert an smc_pnetentry to a netlink attribute sequence */
520 static int smc_pnet_set_nla(struct sk_buff *msg,
521 struct smc_pnetentry *pnetelem)
523 if (nla_put_string(msg, SMC_PNETID_NAME, pnetelem->pnet_name))
525 if (pnetelem->type == SMC_PNET_ETH) {
526 if (nla_put_string(msg, SMC_PNETID_ETHNAME,
530 if (nla_put_string(msg, SMC_PNETID_ETHNAME, "n/a"))
533 if (pnetelem->type == SMC_PNET_IB) {
534 if (nla_put_string(msg, SMC_PNETID_IBNAME, pnetelem->ib_name) ||
535 nla_put_u8(msg, SMC_PNETID_IBPORT, pnetelem->ib_port))
538 if (nla_put_string(msg, SMC_PNETID_IBNAME, "n/a") ||
539 nla_put_u8(msg, SMC_PNETID_IBPORT, 0xff))
546 static int smc_pnet_add(struct sk_buff *skb, struct genl_info *info)
548 struct net *net = genl_info_net(info);
550 return smc_pnet_enter(net, info->attrs);
553 static int smc_pnet_del(struct sk_buff *skb, struct genl_info *info)
555 struct net *net = genl_info_net(info);
557 if (!info->attrs[SMC_PNETID_NAME])
559 return smc_pnet_remove_by_pnetid(net,
560 (char *)nla_data(info->attrs[SMC_PNETID_NAME]));
563 static int smc_pnet_dump_start(struct netlink_callback *cb)
569 static int smc_pnet_dumpinfo(struct sk_buff *skb,
570 u32 portid, u32 seq, u32 flags,
571 struct smc_pnetentry *pnetelem)
575 hdr = genlmsg_put(skb, portid, seq, &smc_pnet_nl_family,
576 flags, SMC_PNETID_GET);
579 if (smc_pnet_set_nla(skb, pnetelem) < 0) {
580 genlmsg_cancel(skb, hdr);
583 genlmsg_end(skb, hdr);
587 static int _smc_pnet_dump(struct net *net, struct sk_buff *skb, u32 portid,
588 u32 seq, u8 *pnetid, int start_idx)
590 struct smc_pnettable *pnettable;
591 struct smc_pnetentry *pnetelem;
595 /* get pnettable for namespace */
596 sn = net_generic(net, smc_net_id);
597 pnettable = &sn->pnettable;
599 /* dump pnettable entries */
600 read_lock(&pnettable->lock);
601 list_for_each_entry(pnetelem, &pnettable->pnetlist, list) {
602 if (pnetid && !smc_pnet_match(pnetelem->pnet_name, pnetid))
604 if (idx++ < start_idx)
606 /* if this is not the initial namespace, dump only netdev */
607 if (net != &init_net && pnetelem->type != SMC_PNET_ETH)
609 if (smc_pnet_dumpinfo(skb, portid, seq, NLM_F_MULTI,
615 read_unlock(&pnettable->lock);
619 static int smc_pnet_dump(struct sk_buff *skb, struct netlink_callback *cb)
621 struct net *net = sock_net(skb->sk);
624 idx = _smc_pnet_dump(net, skb, NETLINK_CB(cb->skb).portid,
625 cb->nlh->nlmsg_seq, NULL, cb->args[0]);
631 /* Retrieve one PNETID entry */
632 static int smc_pnet_get(struct sk_buff *skb, struct genl_info *info)
634 struct net *net = genl_info_net(info);
638 if (!info->attrs[SMC_PNETID_NAME])
641 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
645 _smc_pnet_dump(net, msg, info->snd_portid, info->snd_seq,
646 nla_data(info->attrs[SMC_PNETID_NAME]), 0);
648 /* finish multi part message and send it */
649 hdr = nlmsg_put(msg, info->snd_portid, info->snd_seq, NLMSG_DONE, 0,
655 return genlmsg_reply(msg, info);
658 /* Remove and delete all pnetids from pnet table.
660 static int smc_pnet_flush(struct sk_buff *skb, struct genl_info *info)
662 struct net *net = genl_info_net(info);
664 smc_pnet_remove_by_pnetid(net, NULL);
668 /* SMC_PNETID generic netlink operation definition */
669 static const struct genl_ops smc_pnet_ops[] = {
671 .cmd = SMC_PNETID_GET,
672 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
673 /* can be retrieved by unprivileged users */
674 .doit = smc_pnet_get,
675 .dumpit = smc_pnet_dump,
676 .start = smc_pnet_dump_start
679 .cmd = SMC_PNETID_ADD,
680 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
681 .flags = GENL_ADMIN_PERM,
685 .cmd = SMC_PNETID_DEL,
686 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
687 .flags = GENL_ADMIN_PERM,
691 .cmd = SMC_PNETID_FLUSH,
692 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
693 .flags = GENL_ADMIN_PERM,
694 .doit = smc_pnet_flush
698 /* SMC_PNETID family definition */
699 static struct genl_family smc_pnet_nl_family __ro_after_init = {
701 .name = SMCR_GENL_FAMILY_NAME,
702 .version = SMCR_GENL_FAMILY_VERSION,
703 .maxattr = SMC_PNETID_MAX,
704 .policy = smc_pnet_policy,
706 .module = THIS_MODULE,
708 .n_ops = ARRAY_SIZE(smc_pnet_ops)
711 static int smc_pnet_netdev_event(struct notifier_block *this,
712 unsigned long event, void *ptr)
714 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
718 case NETDEV_UNREGISTER:
719 smc_pnet_remove_by_ndev(event_dev);
721 case NETDEV_REGISTER:
722 smc_pnet_add_by_ndev(event_dev);
729 static struct notifier_block smc_netdev_notifier = {
730 .notifier_call = smc_pnet_netdev_event
733 /* init network namespace */
734 int smc_pnet_net_init(struct net *net)
736 struct smc_net *sn = net_generic(net, smc_net_id);
737 struct smc_pnettable *pnettable = &sn->pnettable;
739 INIT_LIST_HEAD(&pnettable->pnetlist);
740 rwlock_init(&pnettable->lock);
745 int __init smc_pnet_init(void)
749 rc = genl_register_family(&smc_pnet_nl_family);
752 rc = register_netdevice_notifier(&smc_netdev_notifier);
754 genl_unregister_family(&smc_pnet_nl_family);
758 /* exit network namespace */
759 void smc_pnet_net_exit(struct net *net)
761 /* flush pnet table */
762 smc_pnet_remove_by_pnetid(net, NULL);
765 void smc_pnet_exit(void)
767 unregister_netdevice_notifier(&smc_netdev_notifier);
768 genl_unregister_family(&smc_pnet_nl_family);
771 /* 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.
776 static struct net_device *pnet_find_base_ndev(struct net_device *ndev)
781 nest_lvl = ndev->lower_level;
782 for (i = 0; i < nest_lvl; i++) {
783 struct list_head *lower = &ndev->adj_list.lower;
785 if (list_empty(lower))
788 ndev = netdev_lower_get_next(ndev, &lower);
794 static int smc_pnet_find_ndev_pnetid_by_table(struct net_device *ndev,
797 struct smc_pnettable *pnettable;
798 struct net *net = dev_net(ndev);
799 struct smc_pnetentry *pnetelem;
803 /* get pnettable for namespace */
804 sn = net_generic(net, smc_net_id);
805 pnettable = &sn->pnettable;
807 read_lock(&pnettable->lock);
808 list_for_each_entry(pnetelem, &pnettable->pnetlist, list) {
809 if (pnetelem->type == SMC_PNET_ETH && ndev == pnetelem->ndev) {
810 /* get pnetid of netdev device */
811 memcpy(pnetid, pnetelem->pnet_name, SMC_MAX_PNETID_LEN);
816 read_unlock(&pnettable->lock);
820 /* find a roce device for the given pnetid */
821 static void _smc_pnet_find_roce_by_pnetid(u8 *pnet_id,
822 struct smc_init_info *ini,
823 struct smc_ib_device *known_dev)
825 struct smc_ib_device *ibdev;
829 mutex_lock(&smc_ib_devices.mutex);
830 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
831 if (ibdev == known_dev)
833 for (i = 1; i <= SMC_MAX_PORTS; i++) {
834 if (!rdma_is_port_valid(ibdev->ibdev, i))
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)) {
848 mutex_unlock(&smc_ib_devices.mutex);
851 /* find alternate roce device with same pnet_id and vlan_id */
852 void smc_pnet_find_alt_roce(struct smc_link_group *lgr,
853 struct smc_init_info *ini,
854 struct smc_ib_device *known_dev)
856 _smc_pnet_find_roce_by_pnetid(lgr->pnet_id, ini, known_dev);
859 /* if handshake network device belongs to a roce device, return its
862 static void smc_pnet_find_rdma_dev(struct net_device *netdev,
863 struct smc_init_info *ini)
865 struct smc_ib_device *ibdev;
867 mutex_lock(&smc_ib_devices.mutex);
868 list_for_each_entry(ibdev, &smc_ib_devices.list, list) {
869 struct net_device *ndev;
872 for (i = 1; i <= SMC_MAX_PORTS; i++) {
873 if (!rdma_is_port_valid(ibdev->ibdev, i))
875 if (!ibdev->ibdev->ops.get_netdev)
877 ndev = ibdev->ibdev->ops.get_netdev(ibdev->ibdev, i);
881 if (netdev == ndev &&
882 smc_ib_port_active(ibdev, i) &&
883 !test_bit(i - 1, ibdev->ports_going_away) &&
884 !smc_ib_determine_gid(ibdev, i, ini->vlan_id,
885 ini->ib_gid, NULL)) {
892 mutex_unlock(&smc_ib_devices.mutex);
895 /* Determine the corresponding IB device port based on the hardware PNETID.
896 * Searching stops at the first matching active IB device port with vlan_id
898 * If nothing found, check pnetid table.
899 * If nothing found, try to use handshake device
901 static void smc_pnet_find_roce_by_pnetid(struct net_device *ndev,
902 struct smc_init_info *ini)
904 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
906 ndev = pnet_find_base_ndev(ndev);
907 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
909 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid)) {
910 smc_pnet_find_rdma_dev(ndev, ini);
911 return; /* pnetid could not be determined */
913 _smc_pnet_find_roce_by_pnetid(ndev_pnetid, ini, NULL);
916 static void smc_pnet_find_ism_by_pnetid(struct net_device *ndev,
917 struct smc_init_info *ini)
919 u8 ndev_pnetid[SMC_MAX_PNETID_LEN];
920 struct smcd_dev *ismdev;
922 ndev = pnet_find_base_ndev(ndev);
923 if (smc_pnetid_by_dev_port(ndev->dev.parent, ndev->dev_port,
925 smc_pnet_find_ndev_pnetid_by_table(ndev, ndev_pnetid))
926 return; /* pnetid could not be determined */
928 mutex_lock(&smcd_dev_list.mutex);
929 list_for_each_entry(ismdev, &smcd_dev_list.list, list) {
930 if (smc_pnet_match(ismdev->pnetid, ndev_pnetid) &&
931 !ismdev->going_away) {
932 ini->ism_dev = ismdev;
936 mutex_unlock(&smcd_dev_list.mutex);
939 /* PNET table analysis for a given sock:
940 * determine ib_device and port belonging to used internal TCP socket
941 * ethernet interface.
943 void smc_pnet_find_roce_resource(struct sock *sk, struct smc_init_info *ini)
945 struct dst_entry *dst = sk_dst_get(sk);
954 smc_pnet_find_roce_by_pnetid(dst->dev, ini);
962 void smc_pnet_find_ism_resource(struct sock *sk, struct smc_init_info *ini)
964 struct dst_entry *dst = sk_dst_get(sk);
972 smc_pnet_find_ism_by_pnetid(dst->dev, ini);
980 /* Lookup and apply a pnet table entry to the given ib device.
982 int smc_pnetid_by_table_ib(struct smc_ib_device *smcibdev, u8 ib_port)
984 char *ib_name = smcibdev->ibdev->name;
985 struct smc_pnettable *pnettable;
986 struct smc_pnetentry *tmp_pe;
990 /* get pnettable for init namespace */
991 sn = net_generic(&init_net, smc_net_id);
992 pnettable = &sn->pnettable;
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);
1004 read_unlock(&pnettable->lock);
1009 /* Lookup and apply a pnet table entry to the given smcd device.
1011 int smc_pnetid_by_table_smcd(struct smcd_dev *smcddev)
1013 const char *ib_name = dev_name(&smcddev->dev);
1014 struct smc_pnettable *pnettable;
1015 struct smc_pnetentry *tmp_pe;
1019 /* get pnettable for init namespace */
1020 sn = net_generic(&init_net, smc_net_id);
1021 pnettable = &sn->pnettable;
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);
1032 read_unlock(&pnettable->lock);