RDMA/core: Add ib_query_netdev_port() to query netdev port by IB device.
authorYuyu Li <liyuyu6@huawei.com>
Fri, 22 Nov 2024 10:52:57 +0000 (18:52 +0800)
committerLeon Romanovsky <leon@kernel.org>
Tue, 24 Dec 2024 10:21:45 +0000 (05:21 -0500)
Query the port number of a netdev associated with an ibdev.

Signed-off-by: Yuyu Li <liyuyu6@huawei.com>
Signed-off-by: Junxian Huang <huangjunxian6@hisilicon.com>
Signed-off-by: Leon Romanovsky <leon@kernel.org>
drivers/infiniband/core/device.c
include/rdma/ib_verbs.h

index a74e192b558850a2942ec7b0ba680fa43f1960f2..c2f048336c9137fd407af727807bfcc376e5834b 100644 (file)
@@ -2278,6 +2278,33 @@ struct net_device *ib_device_get_netdev(struct ib_device *ib_dev,
 }
 EXPORT_SYMBOL(ib_device_get_netdev);
 
+/**
+ * ib_query_netdev_port - Query the port number of a net_device
+ * associated with an ibdev
+ * @ibdev: IB device
+ * @ndev: Network device
+ * @port: IB port the net_device is connected to
+ */
+int ib_query_netdev_port(struct ib_device *ibdev, struct net_device *ndev,
+                        u32 *port)
+{
+       struct net_device *ib_ndev;
+       u32 port_num;
+
+       rdma_for_each_port(ibdev, port_num) {
+               ib_ndev = ib_device_get_netdev(ibdev, port_num);
+               if (ndev == ib_ndev) {
+                       *port = port_num;
+                       dev_put(ib_ndev);
+                       return 0;
+               }
+               dev_put(ib_ndev);
+       }
+
+       return -ENOENT;
+}
+EXPORT_SYMBOL(ib_query_netdev_port);
+
 /**
  * ib_device_get_by_netdev - Find an IB device associated with a netdev
  * @ndev: netdev to locate
@@ -2841,7 +2868,6 @@ static int ib_netdevice_event(struct notifier_block *this,
                              unsigned long event, void *ptr)
 {
        struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
-       struct net_device *ib_ndev;
        struct ib_device *ibdev;
        u32 port;
 
@@ -2851,13 +2877,12 @@ static int ib_netdevice_event(struct notifier_block *this,
                if (!ibdev)
                        return NOTIFY_DONE;
 
-               rdma_for_each_port(ibdev, port) {
-                       ib_ndev = ib_device_get_netdev(ibdev, port);
-                       if (ndev == ib_ndev)
-                               rdma_nl_notify_event(ibdev, port,
-                                                    RDMA_NETDEV_RENAME_EVENT);
-                       dev_put(ib_ndev);
+               if (ib_query_netdev_port(ibdev, ndev, &port)) {
+                       ib_device_put(ibdev);
+                       break;
                }
+
+               rdma_nl_notify_event(ibdev, port, RDMA_NETDEV_RENAME_EVENT);
                ib_device_put(ibdev);
                break;
        default:
index 77472e19ec0c5d6c4717b8ea59583b62c2e06dcb..c539a1706f66306e6c24e9e5c980c676bae31257 100644 (file)
@@ -4468,6 +4468,8 @@ int ib_device_set_netdev(struct ib_device *ib_dev, struct net_device *ndev,
                         unsigned int port);
 struct net_device *ib_device_get_netdev(struct ib_device *ib_dev,
                                        u32 port);
+int ib_query_netdev_port(struct ib_device *ibdev, struct net_device *ndev,
+                        u32 *port);
 struct ib_wq *ib_create_wq(struct ib_pd *pd,
                           struct ib_wq_init_attr *init_attr);
 int ib_destroy_wq_user(struct ib_wq *wq, struct ib_udata *udata);