1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2022, Microsoft Corporation. All rights reserved. 4 */ 5 6 #include "mana_ib.h" 7 #include <net/mana/mana_auxiliary.h> 8 #include <net/addrconf.h> 9 10 MODULE_DESCRIPTION("Microsoft Azure Network Adapter IB driver"); 11 MODULE_LICENSE("GPL"); 12 MODULE_IMPORT_NS("NET_MANA"); 13 14 static const struct ib_device_ops mana_ib_dev_ops = { 15 .owner = THIS_MODULE, 16 .driver_id = RDMA_DRIVER_MANA, 17 .uverbs_abi_ver = MANA_IB_UVERBS_ABI_VERSION, 18 19 .add_gid = mana_ib_gd_add_gid, 20 .alloc_pd = mana_ib_alloc_pd, 21 .alloc_ucontext = mana_ib_alloc_ucontext, 22 .create_ah = mana_ib_create_ah, 23 .create_cq = mana_ib_create_cq, 24 .create_qp = mana_ib_create_qp, 25 .create_rwq_ind_table = mana_ib_create_rwq_ind_table, 26 .create_wq = mana_ib_create_wq, 27 .dealloc_pd = mana_ib_dealloc_pd, 28 .dealloc_ucontext = mana_ib_dealloc_ucontext, 29 .del_gid = mana_ib_gd_del_gid, 30 .dereg_mr = mana_ib_dereg_mr, 31 .destroy_ah = mana_ib_destroy_ah, 32 .destroy_cq = mana_ib_destroy_cq, 33 .destroy_qp = mana_ib_destroy_qp, 34 .destroy_rwq_ind_table = mana_ib_destroy_rwq_ind_table, 35 .destroy_wq = mana_ib_destroy_wq, 36 .disassociate_ucontext = mana_ib_disassociate_ucontext, 37 .get_dma_mr = mana_ib_get_dma_mr, 38 .get_link_layer = mana_ib_get_link_layer, 39 .get_port_immutable = mana_ib_get_port_immutable, 40 .mmap = mana_ib_mmap, 41 .modify_qp = mana_ib_modify_qp, 42 .modify_wq = mana_ib_modify_wq, 43 .poll_cq = mana_ib_poll_cq, 44 .post_recv = mana_ib_post_recv, 45 .post_send = mana_ib_post_send, 46 .query_device = mana_ib_query_device, 47 .query_gid = mana_ib_query_gid, 48 .query_pkey = mana_ib_query_pkey, 49 .query_port = mana_ib_query_port, 50 .reg_user_mr = mana_ib_reg_user_mr, 51 .reg_user_mr_dmabuf = mana_ib_reg_user_mr_dmabuf, 52 .req_notify_cq = mana_ib_arm_cq, 53 54 INIT_RDMA_OBJ_SIZE(ib_ah, mana_ib_ah, ibah), 55 INIT_RDMA_OBJ_SIZE(ib_cq, mana_ib_cq, ibcq), 56 INIT_RDMA_OBJ_SIZE(ib_pd, mana_ib_pd, ibpd), 57 INIT_RDMA_OBJ_SIZE(ib_qp, mana_ib_qp, ibqp), 58 INIT_RDMA_OBJ_SIZE(ib_ucontext, mana_ib_ucontext, ibucontext), 59 INIT_RDMA_OBJ_SIZE(ib_rwq_ind_table, mana_ib_rwq_ind_table, 60 ib_ind_table), 61 }; 62 63 static const struct ib_device_ops mana_ib_stats_ops = { 64 .alloc_hw_port_stats = mana_ib_alloc_hw_port_stats, 65 .get_hw_stats = mana_ib_get_hw_stats, 66 }; 67 68 static int mana_ib_netdev_event(struct notifier_block *this, 69 unsigned long event, void *ptr) 70 { 71 struct mana_ib_dev *dev = container_of(this, struct mana_ib_dev, nb); 72 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); 73 struct gdma_context *gc = dev->gdma_dev->gdma_context; 74 struct mana_context *mc = gc->mana.driver_data; 75 struct net_device *ndev; 76 77 /* Only process events from our parent device */ 78 if (event_dev != mc->ports[0]) 79 return NOTIFY_DONE; 80 81 switch (event) { 82 case NETDEV_CHANGEUPPER: 83 ndev = mana_get_primary_netdev(mc, 0, &dev->dev_tracker); 84 /* 85 * RDMA core will setup GID based on updated netdev. 86 * It's not possible to race with the core as rtnl lock is being 87 * held. 88 */ 89 ib_device_set_netdev(&dev->ib_dev, ndev, 1); 90 91 /* mana_get_primary_netdev() returns ndev with refcount held */ 92 netdev_put(ndev, &dev->dev_tracker); 93 94 return NOTIFY_OK; 95 default: 96 return NOTIFY_DONE; 97 } 98 } 99 100 static int mana_ib_probe(struct auxiliary_device *adev, 101 const struct auxiliary_device_id *id) 102 { 103 struct mana_adev *madev = container_of(adev, struct mana_adev, adev); 104 struct gdma_context *gc = madev->mdev->gdma_context; 105 struct mana_context *mc = gc->mana.driver_data; 106 struct gdma_dev *mdev = madev->mdev; 107 struct net_device *ndev; 108 struct mana_ib_dev *dev; 109 u8 mac_addr[ETH_ALEN]; 110 int ret; 111 112 dev = ib_alloc_device(mana_ib_dev, ib_dev); 113 if (!dev) 114 return -ENOMEM; 115 116 ib_set_device_ops(&dev->ib_dev, &mana_ib_dev_ops); 117 dev->ib_dev.node_type = RDMA_NODE_IB_CA; 118 dev->ib_dev.num_comp_vectors = gc->max_num_queues; 119 dev->ib_dev.dev.parent = gc->dev; 120 dev->gdma_dev = mdev; 121 xa_init_flags(&dev->qp_table_wq, XA_FLAGS_LOCK_IRQ); 122 123 if (mana_ib_is_rnic(dev)) { 124 dev->ib_dev.phys_port_cnt = 1; 125 ndev = mana_get_primary_netdev(mc, 0, &dev->dev_tracker); 126 if (!ndev) { 127 ret = -ENODEV; 128 ibdev_err(&dev->ib_dev, "Failed to get netdev for IB port 1"); 129 goto free_ib_device; 130 } 131 ether_addr_copy(mac_addr, ndev->dev_addr); 132 addrconf_addr_eui48((u8 *)&dev->ib_dev.node_guid, ndev->dev_addr); 133 ret = ib_device_set_netdev(&dev->ib_dev, ndev, 1); 134 /* mana_get_primary_netdev() returns ndev with refcount held */ 135 netdev_put(ndev, &dev->dev_tracker); 136 if (ret) { 137 ibdev_err(&dev->ib_dev, "Failed to set ib netdev, ret %d", ret); 138 goto free_ib_device; 139 } 140 141 dev->nb.notifier_call = mana_ib_netdev_event; 142 ret = register_netdevice_notifier(&dev->nb); 143 if (ret) { 144 ibdev_err(&dev->ib_dev, "Failed to register net notifier, %d", 145 ret); 146 goto free_ib_device; 147 } 148 149 ret = mana_ib_gd_query_adapter_caps(dev); 150 if (ret) { 151 ibdev_err(&dev->ib_dev, "Failed to query device caps, ret %d", ret); 152 goto deregister_net_notifier; 153 } 154 155 ib_set_device_ops(&dev->ib_dev, &mana_ib_stats_ops); 156 157 ret = mana_ib_create_eqs(dev); 158 if (ret) { 159 ibdev_err(&dev->ib_dev, "Failed to create EQs, ret %d", ret); 160 goto deregister_net_notifier; 161 } 162 163 ret = mana_ib_gd_create_rnic_adapter(dev); 164 if (ret) 165 goto destroy_eqs; 166 167 ret = mana_ib_gd_config_mac(dev, ADDR_OP_ADD, mac_addr); 168 if (ret) { 169 ibdev_err(&dev->ib_dev, "Failed to add Mac address, ret %d", ret); 170 goto destroy_rnic; 171 } 172 } else { 173 dev->ib_dev.phys_port_cnt = mc->num_ports; 174 ret = mana_eth_query_adapter_caps(dev); 175 if (ret) { 176 ibdev_err(&dev->ib_dev, "Failed to query ETH device caps, ret %d", ret); 177 goto free_ib_device; 178 } 179 } 180 181 dev->av_pool = dma_pool_create("mana_ib_av", gc->dev, MANA_AV_BUFFER_SIZE, 182 MANA_AV_BUFFER_SIZE, 0); 183 if (!dev->av_pool) { 184 ret = -ENOMEM; 185 goto destroy_rnic; 186 } 187 188 ibdev_dbg(&dev->ib_dev, "mdev=%p id=%d num_ports=%d\n", mdev, 189 mdev->dev_id.as_uint32, dev->ib_dev.phys_port_cnt); 190 191 ret = ib_register_device(&dev->ib_dev, mana_ib_is_rnic(dev) ? "mana_%d" : "manae_%d", 192 gc->dev); 193 if (ret) 194 goto deallocate_pool; 195 196 dev_set_drvdata(&adev->dev, dev); 197 198 return 0; 199 200 deallocate_pool: 201 dma_pool_destroy(dev->av_pool); 202 destroy_rnic: 203 if (mana_ib_is_rnic(dev)) 204 mana_ib_gd_destroy_rnic_adapter(dev); 205 destroy_eqs: 206 if (mana_ib_is_rnic(dev)) 207 mana_ib_destroy_eqs(dev); 208 deregister_net_notifier: 209 if (mana_ib_is_rnic(dev)) 210 unregister_netdevice_notifier(&dev->nb); 211 free_ib_device: 212 xa_destroy(&dev->qp_table_wq); 213 ib_dealloc_device(&dev->ib_dev); 214 return ret; 215 } 216 217 static void mana_ib_remove(struct auxiliary_device *adev) 218 { 219 struct mana_ib_dev *dev = dev_get_drvdata(&adev->dev); 220 221 ib_unregister_device(&dev->ib_dev); 222 dma_pool_destroy(dev->av_pool); 223 if (mana_ib_is_rnic(dev)) { 224 mana_ib_gd_destroy_rnic_adapter(dev); 225 mana_ib_destroy_eqs(dev); 226 unregister_netdevice_notifier(&dev->nb); 227 } 228 xa_destroy(&dev->qp_table_wq); 229 ib_dealloc_device(&dev->ib_dev); 230 } 231 232 static const struct auxiliary_device_id mana_id_table[] = { 233 { .name = "mana.rdma", }, 234 { .name = "mana.eth", }, 235 {}, 236 }; 237 238 MODULE_DEVICE_TABLE(auxiliary, mana_id_table); 239 240 static struct auxiliary_driver mana_driver = { 241 .probe = mana_ib_probe, 242 .remove = mana_ib_remove, 243 .id_table = mana_id_table, 244 }; 245 246 module_auxiliary_driver(mana_driver); 247