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_dev *mdev = madev->mdev; 105 struct net_device *ndev; 106 struct mana_context *mc; 107 struct mana_ib_dev *dev; 108 u8 mac_addr[ETH_ALEN]; 109 int ret; 110 111 mc = mdev->driver_data; 112 113 dev = ib_alloc_device(mana_ib_dev, ib_dev); 114 if (!dev) 115 return -ENOMEM; 116 117 ib_set_device_ops(&dev->ib_dev, &mana_ib_dev_ops); 118 119 dev->ib_dev.phys_port_cnt = mc->num_ports; 120 121 ibdev_dbg(&dev->ib_dev, "mdev=%p id=%d num_ports=%d\n", mdev, 122 mdev->dev_id.as_uint32, dev->ib_dev.phys_port_cnt); 123 124 dev->ib_dev.node_type = RDMA_NODE_IB_CA; 125 126 /* 127 * num_comp_vectors needs to set to the max MSIX index 128 * when interrupts and event queues are implemented 129 */ 130 dev->ib_dev.num_comp_vectors = mdev->gdma_context->max_num_queues; 131 dev->ib_dev.dev.parent = mdev->gdma_context->dev; 132 133 ndev = mana_get_primary_netdev(mc, 0, &dev->dev_tracker); 134 if (!ndev) { 135 ret = -ENODEV; 136 ibdev_err(&dev->ib_dev, "Failed to get netdev for IB port 1"); 137 goto free_ib_device; 138 } 139 ether_addr_copy(mac_addr, ndev->dev_addr); 140 addrconf_addr_eui48((u8 *)&dev->ib_dev.node_guid, ndev->dev_addr); 141 ret = ib_device_set_netdev(&dev->ib_dev, ndev, 1); 142 /* mana_get_primary_netdev() returns ndev with refcount held */ 143 netdev_put(ndev, &dev->dev_tracker); 144 if (ret) { 145 ibdev_err(&dev->ib_dev, "Failed to set ib netdev, ret %d", ret); 146 goto free_ib_device; 147 } 148 149 ret = mana_gd_register_device(&mdev->gdma_context->mana_ib); 150 if (ret) { 151 ibdev_err(&dev->ib_dev, "Failed to register device, ret %d", 152 ret); 153 goto free_ib_device; 154 } 155 dev->gdma_dev = &mdev->gdma_context->mana_ib; 156 157 dev->nb.notifier_call = mana_ib_netdev_event; 158 ret = register_netdevice_notifier(&dev->nb); 159 if (ret) { 160 ibdev_err(&dev->ib_dev, "Failed to register net notifier, %d", 161 ret); 162 goto deregister_device; 163 } 164 165 ret = mana_ib_gd_query_adapter_caps(dev); 166 if (ret) { 167 ibdev_err(&dev->ib_dev, "Failed to query device caps, ret %d", 168 ret); 169 goto deregister_net_notifier; 170 } 171 172 ib_set_device_ops(&dev->ib_dev, &mana_ib_stats_ops); 173 174 ret = mana_ib_create_eqs(dev); 175 if (ret) { 176 ibdev_err(&dev->ib_dev, "Failed to create EQs, ret %d", ret); 177 goto deregister_net_notifier; 178 } 179 180 ret = mana_ib_gd_create_rnic_adapter(dev); 181 if (ret) 182 goto destroy_eqs; 183 184 xa_init_flags(&dev->qp_table_wq, XA_FLAGS_LOCK_IRQ); 185 ret = mana_ib_gd_config_mac(dev, ADDR_OP_ADD, mac_addr); 186 if (ret) { 187 ibdev_err(&dev->ib_dev, "Failed to add Mac address, ret %d", 188 ret); 189 goto destroy_rnic; 190 } 191 192 dev->av_pool = dma_pool_create("mana_ib_av", mdev->gdma_context->dev, 193 MANA_AV_BUFFER_SIZE, MANA_AV_BUFFER_SIZE, 0); 194 if (!dev->av_pool) { 195 ret = -ENOMEM; 196 goto destroy_rnic; 197 } 198 199 ret = ib_register_device(&dev->ib_dev, "mana_%d", 200 mdev->gdma_context->dev); 201 if (ret) 202 goto deallocate_pool; 203 204 dev_set_drvdata(&adev->dev, dev); 205 206 return 0; 207 208 deallocate_pool: 209 dma_pool_destroy(dev->av_pool); 210 destroy_rnic: 211 xa_destroy(&dev->qp_table_wq); 212 mana_ib_gd_destroy_rnic_adapter(dev); 213 destroy_eqs: 214 mana_ib_destroy_eqs(dev); 215 deregister_net_notifier: 216 unregister_netdevice_notifier(&dev->nb); 217 deregister_device: 218 mana_gd_deregister_device(dev->gdma_dev); 219 free_ib_device: 220 ib_dealloc_device(&dev->ib_dev); 221 return ret; 222 } 223 224 static void mana_ib_remove(struct auxiliary_device *adev) 225 { 226 struct mana_ib_dev *dev = dev_get_drvdata(&adev->dev); 227 228 ib_unregister_device(&dev->ib_dev); 229 dma_pool_destroy(dev->av_pool); 230 xa_destroy(&dev->qp_table_wq); 231 mana_ib_gd_destroy_rnic_adapter(dev); 232 mana_ib_destroy_eqs(dev); 233 unregister_netdevice_notifier(&dev->nb); 234 mana_gd_deregister_device(dev->gdma_dev); 235 ib_dealloc_device(&dev->ib_dev); 236 } 237 238 static const struct auxiliary_device_id mana_id_table[] = { 239 { 240 .name = "mana.rdma", 241 }, 242 {}, 243 }; 244 245 MODULE_DEVICE_TABLE(auxiliary, mana_id_table); 246 247 static struct auxiliary_driver mana_driver = { 248 .name = "rdma", 249 .probe = mana_ib_probe, 250 .remove = mana_ib_remove, 251 .id_table = mana_id_table, 252 }; 253 254 module_auxiliary_driver(mana_driver); 255