xref: /freebsd/sys/dev/irdma/icrdma.c (revision 9088779e3c8b810afb701adb80be154a7b2e0523)
1 /*-
2  * SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB
3  *
4  * Copyright (c) 2021 - 2022 Intel Corporation
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenFabrics.org BSD license below:
11  *
12  *   Redistribution and use in source and binary forms, with or
13  *   without modification, are permitted provided that the following
14  *   conditions are met:
15  *
16  *    - Redistributions of source code must retain the above
17  *	copyright notice, this list of conditions and the following
18  *	disclaimer.
19  *
20  *    - Redistributions in binary form must reproduce the above
21  *	copyright notice, this list of conditions and the following
22  *	disclaimer in the documentation and/or other materials
23  *	provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 /*$FreeBSD$*/
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/sysctl.h>
42 #include <machine/bus.h>
43 #include <linux/device.h>
44 #include <sys/rman.h>
45 
46 #include "ice_rdma.h"
47 #include "irdma_main.h"
48 #include "icrdma_hw.h"
49 
50 #include "irdma_if.h"
51 #include "irdma_di_if.h"
52 
53 /**
54  *  Driver version
55  */
56 char irdma_driver_version[] = "1.1.11-k";
57 
58 #define pf_if_d(peer) peer->ifp->if_dunit
59 
60 /**
61  * irdma_init_tunable - prepare tunables
62  * @rf: RDMA PCI function
63  * @pf_id: id of the pf
64  */
65 static void
66 irdma_init_tunable(struct irdma_pci_f *rf, uint8_t pf_id)
67 {
68 	struct sysctl_oid_list *irdma_sysctl_oid_list;
69 	char pf_name[16];
70 
71 	snprintf(pf_name, 15, "irdma%d", pf_id);
72 	sysctl_ctx_init(&rf->tun_info.irdma_sysctl_ctx);
73 
74 	rf->tun_info.irdma_sysctl_tree = SYSCTL_ADD_NODE(&rf->tun_info.irdma_sysctl_ctx,
75 							 SYSCTL_STATIC_CHILDREN(_dev),
76 							 OID_AUTO, pf_name, CTLFLAG_RD,
77 							 NULL, "");
78 
79 	irdma_sysctl_oid_list = SYSCTL_CHILDREN(rf->tun_info.irdma_sysctl_tree);
80 
81 	/*
82 	 * debug mask setting
83 	 */
84 	SYSCTL_ADD_S32(&rf->tun_info.irdma_sysctl_ctx, irdma_sysctl_oid_list,
85 		       OID_AUTO, "debug", CTLFLAG_RWTUN, &rf->sc_dev.debug_mask,
86 		       0, "irdma debug");
87 
88 	/*
89 	 * RoCEv2/iWARP setting RoCEv2 the default mode
90 	 */
91 	rf->tun_info.roce_ena = 1;
92 	SYSCTL_ADD_U8(&rf->tun_info.irdma_sysctl_ctx, irdma_sysctl_oid_list, OID_AUTO,
93 		      "roce_enable", CTLFLAG_RDTUN, &rf->tun_info.roce_ena, 0,
94 		      "RoCEv2 mode enable");
95 
96 	rf->protocol_used = IRDMA_IWARP_PROTOCOL_ONLY;
97 	if (rf->tun_info.roce_ena == 1)
98 		rf->protocol_used = IRDMA_ROCE_PROTOCOL_ONLY;
99 	else if (rf->tun_info.roce_ena != 0)
100 		printf("%s:%d wrong roce_enable value (%d), using iWARP\n",
101 		       __func__, __LINE__, rf->tun_info.roce_ena);
102 	printf("%s:%d protocol: %s, roce_enable value: %d\n", __func__, __LINE__,
103 	       (rf->protocol_used == IRDMA_IWARP_PROTOCOL_ONLY) ? "iWARP" : "RoCEv2",
104 	       rf->tun_info.roce_ena);
105 
106 	snprintf(rf->tun_info.drv_ver, IRDMA_VER_LEN, "%s", irdma_driver_version);
107 	SYSCTL_ADD_STRING(&rf->tun_info.irdma_sysctl_ctx, irdma_sysctl_oid_list,
108 			  OID_AUTO, "drv_ver", CTLFLAG_RDTUN, rf->tun_info.drv_ver,
109 			  IRDMA_VER_LEN, "driver version");
110 
111 	irdma_dcqcn_tunables_init(rf);
112 }
113 
114 /**
115  * irdma_find_handler - obtain hdl object to identify pf
116  * @p_dev: the peer interface structure
117  */
118 static struct irdma_handler *
119 irdma_find_handler(struct ice_rdma_peer *p_dev)
120 {
121 	struct irdma_handler *hdl;
122 	unsigned long flags;
123 
124 	spin_lock_irqsave(&irdma_handler_lock, flags);
125 	list_for_each_entry(hdl, &irdma_handlers, list) {
126 		if (!hdl)
127 			continue;
128 		if (!hdl->iwdev->rf->peer_info)
129 			continue;
130 		if (hdl->iwdev->rf->peer_info->dev == p_dev->dev) {
131 			spin_unlock_irqrestore(&irdma_handler_lock, flags);
132 			return hdl;
133 		}
134 	}
135 	spin_unlock_irqrestore(&irdma_handler_lock, flags);
136 
137 	return NULL;
138 }
139 
140 /**
141  * peer_to_iwdev - return iwdev based on peer
142  * @peer: the peer interface structure
143  */
144 static struct irdma_device *
145 peer_to_iwdev(struct ice_rdma_peer *peer)
146 {
147 	struct irdma_handler *hdl;
148 
149 	hdl = irdma_find_handler(peer);
150 	if (!hdl) {
151 		printf("%s:%d rdma handler not found\n", __func__, __LINE__);
152 		return NULL;
153 	}
154 
155 	return hdl->iwdev;
156 }
157 
158 /**
159  * irdma_get_qos_info - save qos info from parameters to internal struct
160  * @l2params: destination, qos, tc, mtu info structure
161  * @qos_info: source, DCB settings structure
162  */
163 static void
164 irdma_get_qos_info(struct irdma_l2params *l2params, struct ice_qos_params *qos_info)
165 {
166 	int i;
167 
168 	l2params->num_tc = qos_info->num_tc;
169 	l2params->num_apps = qos_info->num_apps;
170 	l2params->vsi_prio_type = qos_info->vsi_priority_type;
171 	l2params->vsi_rel_bw = qos_info->vsi_relative_bw;
172 	for (i = 0; i < l2params->num_tc; i++) {
173 		l2params->tc_info[i].egress_virt_up =
174 		    qos_info->tc_info[i].egress_virt_up;
175 		l2params->tc_info[i].ingress_virt_up =
176 		    qos_info->tc_info[i].ingress_virt_up;
177 		l2params->tc_info[i].prio_type = qos_info->tc_info[i].prio_type;
178 		l2params->tc_info[i].rel_bw = qos_info->tc_info[i].rel_bw;
179 		l2params->tc_info[i].tc_ctx = qos_info->tc_info[i].tc_ctx;
180 	}
181 	for (i = 0; i < IRDMA_MAX_USER_PRIORITY; i++)
182 		l2params->up2tc[i] = qos_info->up2tc[i];
183 
184 	if (qos_info->pfc_mode == IRDMA_QOS_MODE_DSCP) {
185 		l2params->dscp_mode = true;
186 		memcpy(l2params->dscp_map, qos_info->dscp_map, sizeof(l2params->dscp_map));
187 	}
188 	printf("%s:%d: l2params settings:\n num_tc %d,\n num_apps %d,\n",
189 	       __func__, __LINE__, l2params->num_tc, l2params->num_apps);
190 	printf(" vsi_prio_type %d,\n vsi_rel_bw %d,\n egress_virt_up:",
191 	       l2params->vsi_prio_type, l2params->vsi_rel_bw);
192 	for (i = 0; i < l2params->num_tc; i++)
193 		printf(" %d", l2params->tc_info[i].egress_virt_up);
194 	printf("\n ingress_virt_up:");
195 	for (i = 0; i < l2params->num_tc; i++)
196 		printf(" %d", l2params->tc_info[i].ingress_virt_up);
197 	printf("\n prio_type:");
198 	for (i = 0; i < l2params->num_tc; i++)
199 		printf(" %d", l2params->tc_info[i].prio_type);
200 	printf("\n rel_bw:");
201 	for (i = 0; i < l2params->num_tc; i++)
202 		printf(" %d", l2params->tc_info[i].rel_bw);
203 	printf("\n tc_ctx:");
204 	for (i = 0; i < l2params->num_tc; i++)
205 		printf(" %lu", l2params->tc_info[i].tc_ctx);
206 	printf("\n up2tc:");
207 	for (i = 0; i < IRDMA_MAX_USER_PRIORITY; i++)
208 		printf(" %d", l2params->up2tc[i]);
209 	printf(" dscp_mode: %d,\n", l2params->dscp_mode);
210 	for (i = 0; i < IRDMA_DSCP_NUM_VAL; i++)
211 		printf(" %d", l2params->dscp_map[i]);
212 	printf("\n");
213 
214 	dump_struct(l2params, sizeof(*l2params), "l2params");
215 }
216 
217 /**
218  * irdma_log_invalid_mtu - check mtu setting validity
219  * @mtu: mtu value
220  * @dev: hardware control device structure
221  */
222 static void
223 irdma_log_invalid_mtu(u16 mtu, struct irdma_sc_dev *dev)
224 {
225 	if (mtu < IRDMA_MIN_MTU_IPV4)
226 		irdma_dev_warn(to_ibdev(dev),
227 			       "MTU setting [%d] too low for RDMA traffic. Minimum MTU is 576 for IPv4\n",
228 			       mtu);
229 	else if (mtu < IRDMA_MIN_MTU_IPV6)
230 		irdma_dev_warn(to_ibdev(dev),
231 			       "MTU setting [%d] too low for RDMA traffic. Minimum MTU is 1280 for IPv6\\n",
232 			       mtu);
233 }
234 
235 /**
236  * irdma_event_handler - handling events from lan driver
237  * @peer: the peer interface structure
238  * @event: event info structure
239  */
240 static void
241 irdma_event_handler(struct ice_rdma_peer *peer, struct ice_rdma_event *event)
242 {
243 	struct irdma_device *iwdev;
244 	struct irdma_l2params l2params = {};
245 
246 	printf("%s:%d event_handler %s (%x) on pf %d (%d)\n", __func__, __LINE__,
247 	       (event->type == 1) ? "LINK CHANGE" :
248 	       (event->type == 2) ? "MTU CHANGE" :
249 	       (event->type == 3) ? "TC CHANGE" : "UNKNOWN",
250 	       event->type, peer->pf_id, pf_if_d(peer));
251 	iwdev = peer_to_iwdev(peer);
252 	if (!iwdev) {
253 		printf("%s:%d rdma device not found\n", __func__, __LINE__);
254 		return;
255 	}
256 
257 	switch (event->type) {
258 	case ICE_RDMA_EVENT_LINK_CHANGE:
259 		printf("%s:%d PF: %x (%x), state: %d, speed: %lu\n", __func__, __LINE__,
260 		       peer->pf_id, pf_if_d(peer), event->linkstate, event->baudrate);
261 		break;
262 	case ICE_RDMA_EVENT_MTU_CHANGE:
263 		if (iwdev->vsi.mtu != event->mtu) {
264 			l2params.mtu = event->mtu;
265 			l2params.mtu_changed = true;
266 			irdma_log_invalid_mtu(l2params.mtu, &iwdev->rf->sc_dev);
267 			irdma_change_l2params(&iwdev->vsi, &l2params);
268 		}
269 		break;
270 	case ICE_RDMA_EVENT_TC_CHANGE:
271 		/*
272 		 * 1. check if it is pre or post 2. check if it is currently being done
273 		 */
274 		if (event->prep == iwdev->vsi.tc_change_pending) {
275 			printf("%s:%d can't process %s TC change if TC change is %spending\n",
276 			       __func__, __LINE__,
277 			       event->prep ? "pre" : "post",
278 			       event->prep ? " " : "not ");
279 			goto done;
280 		}
281 		if (event->prep) {
282 			iwdev->vsi.tc_change_pending = true;
283 			irdma_sc_suspend_resume_qps(&iwdev->vsi, IRDMA_OP_SUSPEND);
284 			wait_event_timeout(iwdev->suspend_wq,
285 					   !atomic_read(&iwdev->vsi.qp_suspend_reqs),
286 					   IRDMA_EVENT_TIMEOUT_MS * 10);
287 			irdma_ws_reset(&iwdev->vsi);
288 			printf("%s:%d TC change preparation done\n", __func__, __LINE__);
289 		} else {
290 			l2params.tc_changed = true;
291 			irdma_get_qos_info(&l2params, &event->port_qos);
292 			if (iwdev->rf->protocol_used != IRDMA_IWARP_PROTOCOL_ONLY)
293 				iwdev->dcb_vlan_mode = l2params.num_tc > 1 && !l2params.dscp_mode;
294 
295 			irdma_check_fc_for_tc_update(&iwdev->vsi, &l2params);
296 			irdma_change_l2params(&iwdev->vsi, &l2params);
297 			printf("%s:%d TC change done\n", __func__, __LINE__);
298 		}
299 		break;
300 	case ICE_RDMA_EVENT_CRIT_ERR:
301 		printf("%s:%d event type received: %d\n", __func__, __LINE__, event->type);
302 		break;
303 	default:
304 		printf("%s:%d event type unsupported: %d\n", __func__, __LINE__, event->type);
305 	}
306 done:
307 	return;
308 }
309 
310 /**
311  * irdma_link_change - Callback for link state change
312  * @peer: the peer interface structure
313  * @linkstate: state of the link
314  * @baudrate: speed of the link
315  */
316 static void
317 irdma_link_change(struct ice_rdma_peer *peer, int linkstate, uint64_t baudrate)
318 {
319 	printf("%s:%d PF: %x (%x), state: %d, speed: %lu\n", __func__, __LINE__,
320 	       peer->pf_id, pf_if_d(peer), linkstate, baudrate);
321 }
322 
323 /**
324  * irdma_finalize_task - Finish open or close phase in a separate thread
325  * @context: instance holding peer and iwdev information
326  *
327  * Triggered from irdma_open or irdma_close to perform rt_init_hw or
328  * rt_deinit_hw respectively. Does registration and unregistration of
329  * the device.
330  */
331 static void
332 irdma_finalize_task(void *context, int pending)
333 {
334 	struct irdma_task_arg *task_arg = (struct irdma_task_arg *)context;
335 	struct irdma_device *iwdev = task_arg->iwdev;
336 	struct irdma_pci_f *rf = iwdev->rf;
337 	struct ice_rdma_peer *peer = task_arg->peer;
338 	struct irdma_l2params l2params = {{{0}}};
339 	struct ice_rdma_request req = {0};
340 	int status = 0;
341 
342 	if (iwdev->iw_status) {
343 		irdma_debug(&rf->sc_dev, IRDMA_DEBUG_INIT,
344 			    "Starting deferred closing %d (%d)\n",
345 			    rf->peer_info->pf_id, pf_if_d(peer));
346 		irdma_dereg_ipaddr_event_cb(rf);
347 		irdma_ib_unregister_device(iwdev);
348 		req.type = ICE_RDMA_EVENT_VSI_FILTER_UPDATE;
349 		req.enable_filter = false;
350 		IRDMA_DI_REQ_HANDLER(peer, &req);
351 		irdma_cleanup_dead_qps(&iwdev->vsi);
352 		irdma_rt_deinit_hw(iwdev);
353 	} else {
354 		irdma_debug(&rf->sc_dev, IRDMA_DEBUG_INIT,
355 			    "Starting deferred opening %d (%d)\n",
356 			    rf->peer_info->pf_id, pf_if_d(peer));
357 		irdma_get_qos_info(&l2params, &peer->initial_qos_info);
358 		if (iwdev->rf->protocol_used != IRDMA_IWARP_PROTOCOL_ONLY)
359 			iwdev->dcb_vlan_mode = l2params.num_tc > 1 && !l2params.dscp_mode;
360 
361 		l2params.mtu = peer->mtu;
362 		status = irdma_rt_init_hw(iwdev, &l2params);
363 		if (status) {
364 			irdma_pr_err("RT init failed %d\n", status);
365 			ib_dealloc_device(&iwdev->ibdev);
366 			return;
367 		}
368 		status = irdma_ib_register_device(iwdev);
369 		if (status) {
370 			irdma_pr_err("Registration failed %d\n", status);
371 			irdma_rt_deinit_hw(iwdev);
372 			ib_dealloc_device(&iwdev->ibdev);
373 		}
374 		req.type = ICE_RDMA_EVENT_VSI_FILTER_UPDATE;
375 		req.enable_filter = true;
376 		IRDMA_DI_REQ_HANDLER(peer, &req);
377 		irdma_reg_ipaddr_event_cb(rf);
378 		irdma_debug(&rf->sc_dev, IRDMA_DEBUG_INIT,
379 			    "Deferred opening finished %d (%d)\n",
380 			    rf->peer_info->pf_id, pf_if_d(peer));
381 	}
382 }
383 
384 /**
385  * irdma_open - Callback for operation open for RDMA device
386  * @peer: the new peer interface structure
387  *
388  * Callback implementing the RDMA_OPEN function. Called by the ice driver to
389  * notify the RDMA client driver that a new device has been initialized.
390  */
391 static int
392 irdma_open(struct ice_rdma_peer *peer)
393 {
394 	struct ice_rdma_event event = {0};
395 
396 	event.type = ICE_RDMA_EVENT_MTU_CHANGE;
397 	event.mtu = peer->mtu;
398 
399 	irdma_event_handler(peer, &event);
400 
401 	return 0;
402 }
403 
404 /**
405  * irdma_close - Callback to notify that a peer device is down
406  * @peer: the RDMA peer device being stopped
407  *
408  * Callback implementing the RDMA_CLOSE function. Called by the ice driver to
409  * notify the RDMA client driver that a peer device is being stopped.
410  */
411 static int
412 irdma_close(struct ice_rdma_peer *peer)
413 {
414 	/*
415 	 * This is called when ifconfig down. Keeping it for compatibility with ice. This event might be usefull for
416 	 * future.
417 	 */
418 	return 0;
419 }
420 
421 /**
422  * irdma_alloc_pcidev - allocate memory for pcidev and populate data
423  * @peer: the new peer interface structure
424  * @rf: RDMA PCI function
425  */
426 static int
427 irdma_alloc_pcidev(struct ice_rdma_peer *peer, struct irdma_pci_f *rf)
428 {
429 	rf->pcidev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
430 	if (!rf->pcidev) {
431 		return -ENOMEM;
432 	}
433 	if (linux_pci_attach_device(rf->dev_ctx.dev, NULL, NULL, rf->pcidev))
434 		return -ENOMEM;
435 
436 	return 0;
437 }
438 
439 /**
440  * irdma_dealloc_pcidev - deallocate memory for pcidev
441  * @rf: RDMA PCI function
442  */
443 static void
444 irdma_dealloc_pcidev(struct irdma_pci_f *rf)
445 {
446 	linux_pci_detach_device(rf->pcidev);
447 	kfree(rf->pcidev);
448 }
449 
450 /**
451  * irdma_fill_device_info - assign initial values to rf variables
452  * @iwdev: irdma device
453  * @peer: the peer interface structure
454  */
455 static void
456 irdma_fill_device_info(struct irdma_device *iwdev,
457 		       struct ice_rdma_peer *peer)
458 {
459 	struct irdma_pci_f *rf = iwdev->rf;
460 
461 	rf->peer_info = peer;
462 	rf->gen_ops.register_qset = irdma_register_qset;
463 	rf->gen_ops.unregister_qset = irdma_unregister_qset;
464 
465 	rf->rdma_ver = IRDMA_GEN_2;
466 	rf->sc_dev.hw_attrs.uk_attrs.hw_rev = IRDMA_GEN_2;
467 	rf->rsrc_profile = IRDMA_HMC_PROFILE_DEFAULT;
468 	rf->rst_to = IRDMA_RST_TIMEOUT_HZ;
469 	rf->check_fc = irdma_check_fc_for_qp;
470 	rf->gen_ops.request_reset = irdma_request_reset;
471 	irdma_set_rf_user_cfg_params(rf);
472 
473 	rf->default_vsi.vsi_idx = peer->pf_vsi_num;
474 	rf->dev_ctx.dev = peer->dev;
475 	rf->dev_ctx.mem_bus_space_tag = rman_get_bustag(peer->pci_mem);
476 	rf->dev_ctx.mem_bus_space_handle = rman_get_bushandle(peer->pci_mem);
477 	rf->dev_ctx.mem_bus_space_size = rman_get_size(peer->pci_mem);
478 
479 	rf->hw.dev_context = &rf->dev_ctx;
480 	rf->hw.hw_addr = (u8 *)rman_get_virtual(peer->pci_mem);
481 	rf->msix_count = peer->msix.count;
482 	rf->msix_info.entry = peer->msix.base;
483 	rf->msix_info.vector = peer->msix.count;
484 	printf("%s:%d msix_info: %d %d %d\n", __func__, __LINE__,
485 	       rf->msix_count, rf->msix_info.entry, rf->msix_info.vector);
486 
487 	rf->iwdev = iwdev;
488 	iwdev->netdev = peer->ifp;
489 	iwdev->init_state = INITIAL_STATE;
490 	iwdev->vsi_num = peer->pf_vsi_num;
491 	iwdev->rcv_wnd = IRDMA_CM_DEFAULT_RCV_WND_SCALED;
492 	iwdev->rcv_wscale = IRDMA_CM_DEFAULT_RCV_WND_SCALE;
493 	iwdev->roce_cwnd = IRDMA_ROCE_CWND_DEFAULT;
494 	iwdev->roce_ackcreds = IRDMA_ROCE_ACKCREDS_DEFAULT;
495 	iwdev->roce_rtomin = 5;
496 
497 	if (rf->protocol_used == IRDMA_ROCE_PROTOCOL_ONLY) {
498 		iwdev->roce_mode = true;
499 	}
500 }
501 
502 /**
503  * irdma_probe - Callback to probe a new RDMA peer device
504  * @peer: the new peer interface structure
505  *
506  * Callback implementing the RDMA_PROBE function. Called by the ice driver to
507  * notify the RDMA client driver that a new device has been created
508  */
509 static int
510 irdma_probe(struct ice_rdma_peer *peer)
511 {
512 	struct irdma_device *iwdev;
513 	struct irdma_pci_f *rf;
514 	struct irdma_handler *hdl;
515 	int err = 0;
516 
517 	irdma_pr_info("probe: irdma-%s peer=%p, peer->pf_id=%d, peer->ifp=%p, peer->ifp->if_dunit=%d, peer->pci_mem->r_bustag=%p\n",
518 		      irdma_driver_version, peer, peer->pf_id, peer->ifp,
519 		      pf_if_d(peer), (void *)(uintptr_t)peer->pci_mem->r_bustag);
520 
521 	hdl = irdma_find_handler(peer);
522 	if (hdl)
523 		return -EBUSY;
524 
525 	hdl = kzalloc(sizeof(*hdl), GFP_KERNEL);
526 	if (!hdl)
527 		return -ENOMEM;
528 
529 	iwdev = (struct irdma_device *)ib_alloc_device(sizeof(*iwdev));
530 	if (!iwdev) {
531 		kfree(hdl);
532 		return -ENOMEM;
533 	}
534 
535 	iwdev->rf = kzalloc(sizeof(*rf), GFP_KERNEL);
536 	if (!iwdev->rf) {
537 		ib_dealloc_device(&iwdev->ibdev);
538 		kfree(hdl);
539 		return -ENOMEM;
540 	}
541 	hdl->iwdev = iwdev;
542 	iwdev->hdl = hdl;
543 
544 	irdma_init_tunable(iwdev->rf, pf_if_d(peer));
545 	irdma_fill_device_info(iwdev, peer);
546 	rf = iwdev->rf;
547 
548 	if (irdma_alloc_pcidev(peer, rf))
549 		goto err_pcidev;
550 
551 	irdma_add_handler(hdl);
552 
553 	if (irdma_ctrl_init_hw(rf)) {
554 		err = -EIO;
555 		goto err_ctrl_init;
556 	}
557 
558 	rf->dev_ctx.task_arg.peer = peer;
559 	rf->dev_ctx.task_arg.iwdev = iwdev;
560 	rf->dev_ctx.task_arg.peer = peer;
561 
562 	TASK_INIT(&hdl->deferred_task, 0, irdma_finalize_task, &rf->dev_ctx.task_arg);
563 	hdl->deferred_tq = taskqueue_create_fast("irdma_defer",
564 						 M_NOWAIT, taskqueue_thread_enqueue,
565 						 &hdl->deferred_tq);
566 	taskqueue_start_threads(&hdl->deferred_tq, 1, PI_NET, "irdma_defer_t");
567 
568 	taskqueue_enqueue(hdl->deferred_tq, &hdl->deferred_task);
569 
570 	return 0;
571 
572 err_ctrl_init:
573 	irdma_del_handler(hdl);
574 	irdma_dealloc_pcidev(rf);
575 err_pcidev:
576 	kfree(iwdev->rf);
577 	ib_dealloc_device(&iwdev->ibdev);
578 	kfree(hdl);
579 
580 	return err;
581 }
582 
583 /**
584  * irdma_remove - Callback to remove an RDMA peer device
585  * @peer: the new peer interface structure
586  *
587  * Callback implementing the RDMA_REMOVE function. Called by the ice driver to
588  * notify the RDMA client driver that the device wille be delated
589  */
590 static int
591 irdma_remove(struct ice_rdma_peer *peer)
592 {
593 	struct irdma_handler *hdl;
594 	struct irdma_device *iwdev;
595 
596 	irdma_debug((struct irdma_sc_dev *)NULL, IRDMA_DEBUG_INIT,
597 		    "removing %s irdma%d\n", __func__, pf_if_d(peer));
598 
599 	hdl = irdma_find_handler(peer);
600 	if (!hdl)
601 		return 0;
602 
603 	iwdev = hdl->iwdev;
604 
605 	if (iwdev->vsi.tc_change_pending) {
606 		iwdev->vsi.tc_change_pending = false;
607 		irdma_sc_suspend_resume_qps(&iwdev->vsi, IRDMA_OP_RESUME);
608 	}
609 
610 	taskqueue_enqueue(hdl->deferred_tq, &hdl->deferred_task);
611 
612 	taskqueue_drain(hdl->deferred_tq, &hdl->deferred_task);
613 	taskqueue_free(hdl->deferred_tq);
614 	hdl->iwdev->rf->dev_ctx.task_arg.iwdev = NULL;
615 	hdl->iwdev->rf->dev_ctx.task_arg.peer = NULL;
616 
617 	sysctl_ctx_free(&iwdev->rf->tun_info.irdma_sysctl_ctx);
618 	hdl->iwdev->rf->tun_info.irdma_sysctl_tree = NULL;
619 
620 	irdma_ctrl_deinit_hw(iwdev->rf);
621 
622 	irdma_dealloc_pcidev(iwdev->rf);
623 
624 	irdma_del_handler(iwdev->hdl);
625 	kfree(iwdev->hdl);
626 	kfree(iwdev->rf);
627 	ib_dealloc_device(&iwdev->ibdev);
628 	irdma_pr_info("IRDMA hardware deinitialization complete irdma%d\n",
629 		      pf_if_d(peer));
630 
631 	return 0;
632 }
633 
634 /**
635  * irdma_prep_for_unregister - ensure the driver is ready to unregister
636  */
637 static void
638 irdma_prep_for_unregister(void)
639 {
640 	struct irdma_handler *hdl;
641 	unsigned long flags;
642 	bool hdl_valid;
643 
644 	do {
645 		hdl_valid = false;
646 		spin_lock_irqsave(&irdma_handler_lock, flags);
647 		list_for_each_entry(hdl, &irdma_handlers, list) {
648 			if (!hdl)
649 				continue;
650 			if (!hdl->iwdev->rf->peer_info)
651 				continue;
652 			hdl_valid = true;
653 			break;
654 		}
655 		spin_unlock_irqrestore(&irdma_handler_lock, flags);
656 		if (!hdl || !hdl_valid)
657 			break;
658 		IRDMA_CLOSE(hdl->iwdev->rf->peer_info);
659 		IRDMA_REMOVE(hdl->iwdev->rf->peer_info);
660 	} while (1);
661 }
662 
663 static kobj_method_t irdma_methods[] = {
664 	KOBJMETHOD(irdma_probe, irdma_probe),
665 	    KOBJMETHOD(irdma_open, irdma_open),
666 	    KOBJMETHOD(irdma_close, irdma_close),
667 	    KOBJMETHOD(irdma_remove, irdma_remove),
668 	    KOBJMETHOD(irdma_link_change, irdma_link_change),
669 	    KOBJMETHOD(irdma_event_handler, irdma_event_handler),
670 	    KOBJMETHOD_END
671 };
672 
673 /* declare irdma_class which extends the ice_rdma_di class */
674 DEFINE_CLASS_1(irdma, irdma_class, irdma_methods, sizeof(struct ice_rdma_peer), ice_rdma_di_class);
675 
676 static struct ice_rdma_info irdma_info = {
677 	.major_version = ICE_RDMA_MAJOR_VERSION,
678 	.minor_version = ICE_RDMA_MINOR_VERSION,
679 	.patch_version = ICE_RDMA_PATCH_VERSION,
680 	.rdma_class = &irdma_class,
681 };
682 
683 /**
684  * irdma_module_event_handler - Module event handler callback
685  * @mod: unused mod argument
686  * @what: the module event to handle
687  * @arg: unused module event argument
688  *
689  * Callback used by the FreeBSD module stack to notify the driver of module
690  * events. Used to implement custom handling for certain module events such as
691  * load and unload.
692  */
693 static int
694 irdma_module_event_handler(module_t __unused mod, int what, void __unused * arg)
695 {
696 	switch (what) {
697 	case MOD_LOAD:
698 		printf("Loading irdma module\n");
699 		return ice_rdma_register(&irdma_info);
700 	case MOD_UNLOAD:
701 		printf("Unloading irdma module\n");
702 		irdma_prep_for_unregister();
703 		ice_rdma_unregister();
704 		return (0);
705 	default:
706 		return (EOPNOTSUPP);
707 	}
708 
709 	return (0);
710 }
711 
712 static moduledata_t irdma_moduledata = {
713 	"irdma",
714 	    irdma_module_event_handler,
715 	    NULL
716 };
717 
718 DECLARE_MODULE(irdma, irdma_moduledata, SI_SUB_LAST, SI_ORDER_ANY);
719 MODULE_VERSION(irdma, 1);
720 MODULE_DEPEND(irdma, ice, 1, 1, 1);
721 MODULE_DEPEND(irdma, ibcore, 1, 1, 1);
722