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