xref: /openbmc/linux/drivers/infiniband/hw/irdma/main.c (revision 2cf1c348)
1 // SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB
2 /* Copyright (c) 2015 - 2021 Intel Corporation */
3 #include "main.h"
4 #include "../../../net/ethernet/intel/ice/ice.h"
5 
6 MODULE_ALIAS("i40iw");
7 MODULE_AUTHOR("Intel Corporation, <e1000-rdma@lists.sourceforge.net>");
8 MODULE_DESCRIPTION("Intel(R) Ethernet Protocol Driver for RDMA");
9 MODULE_LICENSE("Dual BSD/GPL");
10 
11 static struct notifier_block irdma_inetaddr_notifier = {
12 	.notifier_call = irdma_inetaddr_event
13 };
14 
15 static struct notifier_block irdma_inetaddr6_notifier = {
16 	.notifier_call = irdma_inet6addr_event
17 };
18 
19 static struct notifier_block irdma_net_notifier = {
20 	.notifier_call = irdma_net_event
21 };
22 
23 static struct notifier_block irdma_netdevice_notifier = {
24 	.notifier_call = irdma_netdevice_event
25 };
26 
27 static void irdma_register_notifiers(void)
28 {
29 	register_inetaddr_notifier(&irdma_inetaddr_notifier);
30 	register_inet6addr_notifier(&irdma_inetaddr6_notifier);
31 	register_netevent_notifier(&irdma_net_notifier);
32 	register_netdevice_notifier(&irdma_netdevice_notifier);
33 }
34 
35 static void irdma_unregister_notifiers(void)
36 {
37 	unregister_netevent_notifier(&irdma_net_notifier);
38 	unregister_inetaddr_notifier(&irdma_inetaddr_notifier);
39 	unregister_inet6addr_notifier(&irdma_inetaddr6_notifier);
40 	unregister_netdevice_notifier(&irdma_netdevice_notifier);
41 }
42 
43 static void irdma_prep_tc_change(struct irdma_device *iwdev)
44 {
45 	iwdev->vsi.tc_change_pending = true;
46 	irdma_sc_suspend_resume_qps(&iwdev->vsi, IRDMA_OP_SUSPEND);
47 
48 	/* Wait for all qp's to suspend */
49 	wait_event_timeout(iwdev->suspend_wq,
50 			   !atomic_read(&iwdev->vsi.qp_suspend_reqs),
51 			   IRDMA_EVENT_TIMEOUT);
52 	irdma_ws_reset(&iwdev->vsi);
53 }
54 
55 static void irdma_log_invalid_mtu(u16 mtu, struct irdma_sc_dev *dev)
56 {
57 	if (mtu < IRDMA_MIN_MTU_IPV4)
58 		ibdev_warn(to_ibdev(dev), "MTU setting [%d] too low for RDMA traffic. Minimum MTU is 576 for IPv4\n", mtu);
59 	else if (mtu < IRDMA_MIN_MTU_IPV6)
60 		ibdev_warn(to_ibdev(dev), "MTU setting [%d] too low for RDMA traffic. Minimum MTU is 1280 for IPv6\\n", mtu);
61 }
62 
63 static void irdma_fill_qos_info(struct irdma_l2params *l2params,
64 				struct iidc_qos_params *qos_info)
65 {
66 	int i;
67 
68 	l2params->num_tc = qos_info->num_tc;
69 	l2params->vsi_prio_type = qos_info->vport_priority_type;
70 	l2params->vsi_rel_bw = qos_info->vport_relative_bw;
71 	for (i = 0; i < l2params->num_tc; i++) {
72 		l2params->tc_info[i].egress_virt_up =
73 			qos_info->tc_info[i].egress_virt_up;
74 		l2params->tc_info[i].ingress_virt_up =
75 			qos_info->tc_info[i].ingress_virt_up;
76 		l2params->tc_info[i].prio_type = qos_info->tc_info[i].prio_type;
77 		l2params->tc_info[i].rel_bw = qos_info->tc_info[i].rel_bw;
78 		l2params->tc_info[i].tc_ctx = qos_info->tc_info[i].tc_ctx;
79 	}
80 	for (i = 0; i < IIDC_MAX_USER_PRIORITY; i++)
81 		l2params->up2tc[i] = qos_info->up2tc[i];
82 }
83 
84 static void irdma_iidc_event_handler(struct ice_pf *pf, struct iidc_event *event)
85 {
86 	struct irdma_device *iwdev = dev_get_drvdata(&pf->adev->dev);
87 	struct irdma_l2params l2params = {};
88 
89 	if (*event->type & BIT(IIDC_EVENT_AFTER_MTU_CHANGE)) {
90 		ibdev_dbg(&iwdev->ibdev, "CLNT: new MTU = %d\n", iwdev->netdev->mtu);
91 		if (iwdev->vsi.mtu != iwdev->netdev->mtu) {
92 			l2params.mtu = iwdev->netdev->mtu;
93 			l2params.mtu_changed = true;
94 			irdma_log_invalid_mtu(l2params.mtu, &iwdev->rf->sc_dev);
95 			irdma_change_l2params(&iwdev->vsi, &l2params);
96 		}
97 	} else if (*event->type & BIT(IIDC_EVENT_BEFORE_TC_CHANGE)) {
98 		if (iwdev->vsi.tc_change_pending)
99 			return;
100 
101 		irdma_prep_tc_change(iwdev);
102 	} else if (*event->type & BIT(IIDC_EVENT_AFTER_TC_CHANGE)) {
103 		struct iidc_qos_params qos_info = {};
104 
105 		if (!iwdev->vsi.tc_change_pending)
106 			return;
107 
108 		l2params.tc_changed = true;
109 		ibdev_dbg(&iwdev->ibdev, "CLNT: TC Change\n");
110 		ice_get_qos_params(pf, &qos_info);
111 		iwdev->dcb = qos_info.num_tc > 1;
112 		irdma_fill_qos_info(&l2params, &qos_info);
113 		irdma_change_l2params(&iwdev->vsi, &l2params);
114 	} else if (*event->type & BIT(IIDC_EVENT_CRIT_ERR)) {
115 		ibdev_warn(&iwdev->ibdev, "ICE OICR event notification: oicr = 0x%08x\n",
116 			   event->reg);
117 		if (event->reg & IRDMAPFINT_OICR_PE_CRITERR_M) {
118 			u32 pe_criterr;
119 
120 			pe_criterr = readl(iwdev->rf->sc_dev.hw_regs[IRDMA_GLPE_CRITERR]);
121 #define IRDMA_Q1_RESOURCE_ERR 0x0001024d
122 			if (pe_criterr != IRDMA_Q1_RESOURCE_ERR) {
123 				ibdev_err(&iwdev->ibdev, "critical PE Error, GLPE_CRITERR=0x%08x\n",
124 					  pe_criterr);
125 				iwdev->rf->reset = true;
126 			} else {
127 				ibdev_warn(&iwdev->ibdev, "Q1 Resource Check\n");
128 			}
129 		}
130 		if (event->reg & IRDMAPFINT_OICR_HMC_ERR_M) {
131 			ibdev_err(&iwdev->ibdev, "HMC Error\n");
132 			iwdev->rf->reset = true;
133 		}
134 		if (event->reg & IRDMAPFINT_OICR_PE_PUSH_M) {
135 			ibdev_err(&iwdev->ibdev, "PE Push Error\n");
136 			iwdev->rf->reset = true;
137 		}
138 		if (iwdev->rf->reset)
139 			iwdev->rf->gen_ops.request_reset(iwdev->rf);
140 	}
141 }
142 
143 /**
144  * irdma_request_reset - Request a reset
145  * @rf: RDMA PCI function
146  */
147 static void irdma_request_reset(struct irdma_pci_f *rf)
148 {
149 	struct ice_pf *pf = rf->cdev;
150 
151 	ibdev_warn(&rf->iwdev->ibdev, "Requesting a reset\n");
152 	ice_rdma_request_reset(pf, IIDC_PFR);
153 }
154 
155 /**
156  * irdma_lan_register_qset - Register qset with LAN driver
157  * @vsi: vsi structure
158  * @tc_node: Traffic class node
159  */
160 static enum irdma_status_code irdma_lan_register_qset(struct irdma_sc_vsi *vsi,
161 						      struct irdma_ws_node *tc_node)
162 {
163 	struct irdma_device *iwdev = vsi->back_vsi;
164 	struct ice_pf *pf = iwdev->rf->cdev;
165 	struct iidc_rdma_qset_params qset = {};
166 	int ret;
167 
168 	qset.qs_handle = tc_node->qs_handle;
169 	qset.tc = tc_node->traffic_class;
170 	qset.vport_id = vsi->vsi_idx;
171 	ret = ice_add_rdma_qset(pf, &qset);
172 	if (ret) {
173 		ibdev_dbg(&iwdev->ibdev, "WS: LAN alloc_res for rdma qset failed.\n");
174 		return IRDMA_ERR_REG_QSET;
175 	}
176 
177 	tc_node->l2_sched_node_id = qset.teid;
178 	vsi->qos[tc_node->user_pri].l2_sched_node_id = qset.teid;
179 
180 	return 0;
181 }
182 
183 /**
184  * irdma_lan_unregister_qset - Unregister qset with LAN driver
185  * @vsi: vsi structure
186  * @tc_node: Traffic class node
187  */
188 static void irdma_lan_unregister_qset(struct irdma_sc_vsi *vsi,
189 				      struct irdma_ws_node *tc_node)
190 {
191 	struct irdma_device *iwdev = vsi->back_vsi;
192 	struct ice_pf *pf = iwdev->rf->cdev;
193 	struct iidc_rdma_qset_params qset = {};
194 
195 	qset.qs_handle = tc_node->qs_handle;
196 	qset.tc = tc_node->traffic_class;
197 	qset.vport_id = vsi->vsi_idx;
198 	qset.teid = tc_node->l2_sched_node_id;
199 
200 	if (ice_del_rdma_qset(pf, &qset))
201 		ibdev_dbg(&iwdev->ibdev, "WS: LAN free_res for rdma qset failed.\n");
202 }
203 
204 static void irdma_remove(struct auxiliary_device *aux_dev)
205 {
206 	struct iidc_auxiliary_dev *iidc_adev = container_of(aux_dev,
207 							    struct iidc_auxiliary_dev,
208 							    adev);
209 	struct ice_pf *pf = iidc_adev->pf;
210 	struct irdma_device *iwdev = auxiliary_get_drvdata(aux_dev);
211 
212 	irdma_ib_unregister_device(iwdev);
213 	ice_rdma_update_vsi_filter(pf, iwdev->vsi_num, false);
214 
215 	pr_debug("INIT: Gen2 PF[%d] device remove success\n", PCI_FUNC(pf->pdev->devfn));
216 }
217 
218 static void irdma_fill_device_info(struct irdma_device *iwdev, struct ice_pf *pf,
219 				   struct ice_vsi *vsi)
220 {
221 	struct irdma_pci_f *rf = iwdev->rf;
222 
223 	rf->cdev = pf;
224 	rf->gen_ops.register_qset = irdma_lan_register_qset;
225 	rf->gen_ops.unregister_qset = irdma_lan_unregister_qset;
226 	rf->hw.hw_addr = pf->hw.hw_addr;
227 	rf->pcidev = pf->pdev;
228 	rf->msix_count =  pf->num_rdma_msix;
229 	rf->msix_entries = &pf->msix_entries[pf->rdma_base_vector];
230 	rf->default_vsi.vsi_idx = vsi->vsi_num;
231 	rf->protocol_used = pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2 ?
232 			    IRDMA_ROCE_PROTOCOL_ONLY : IRDMA_IWARP_PROTOCOL_ONLY;
233 	rf->rdma_ver = IRDMA_GEN_2;
234 	rf->rsrc_profile = IRDMA_HMC_PROFILE_DEFAULT;
235 	rf->rst_to = IRDMA_RST_TIMEOUT_HZ;
236 	rf->gen_ops.request_reset = irdma_request_reset;
237 	rf->limits_sel = 7;
238 	rf->iwdev = iwdev;
239 
240 	iwdev->netdev = vsi->netdev;
241 	iwdev->vsi_num = vsi->vsi_num;
242 	iwdev->init_state = INITIAL_STATE;
243 	iwdev->roce_cwnd = IRDMA_ROCE_CWND_DEFAULT;
244 	iwdev->roce_ackcreds = IRDMA_ROCE_ACKCREDS_DEFAULT;
245 	iwdev->rcv_wnd = IRDMA_CM_DEFAULT_RCV_WND_SCALED;
246 	iwdev->rcv_wscale = IRDMA_CM_DEFAULT_RCV_WND_SCALE;
247 	if (rf->protocol_used == IRDMA_ROCE_PROTOCOL_ONLY)
248 		iwdev->roce_mode = true;
249 }
250 
251 static int irdma_probe(struct auxiliary_device *aux_dev, const struct auxiliary_device_id *id)
252 {
253 	struct iidc_auxiliary_dev *iidc_adev = container_of(aux_dev,
254 							    struct iidc_auxiliary_dev,
255 							    adev);
256 	struct ice_pf *pf = iidc_adev->pf;
257 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
258 	struct iidc_qos_params qos_info = {};
259 	struct irdma_device *iwdev;
260 	struct irdma_pci_f *rf;
261 	struct irdma_l2params l2params = {};
262 	int err;
263 
264 	if (!vsi)
265 		return -EIO;
266 	iwdev = ib_alloc_device(irdma_device, ibdev);
267 	if (!iwdev)
268 		return -ENOMEM;
269 	iwdev->rf = kzalloc(sizeof(*rf), GFP_KERNEL);
270 	if (!iwdev->rf) {
271 		ib_dealloc_device(&iwdev->ibdev);
272 		return -ENOMEM;
273 	}
274 
275 	irdma_fill_device_info(iwdev, pf, vsi);
276 	rf = iwdev->rf;
277 
278 	if (irdma_ctrl_init_hw(rf)) {
279 		err = -EIO;
280 		goto err_ctrl_init;
281 	}
282 
283 	l2params.mtu = iwdev->netdev->mtu;
284 	ice_get_qos_params(pf, &qos_info);
285 	irdma_fill_qos_info(&l2params, &qos_info);
286 	if (irdma_rt_init_hw(iwdev, &l2params)) {
287 		err = -EIO;
288 		goto err_rt_init;
289 	}
290 
291 	err = irdma_ib_register_device(iwdev);
292 	if (err)
293 		goto err_ibreg;
294 
295 	ice_rdma_update_vsi_filter(pf, iwdev->vsi_num, true);
296 
297 	ibdev_dbg(&iwdev->ibdev, "INIT: Gen2 PF[%d] device probe success\n", PCI_FUNC(rf->pcidev->devfn));
298 	auxiliary_set_drvdata(aux_dev, iwdev);
299 
300 	return 0;
301 
302 err_ibreg:
303 	irdma_rt_deinit_hw(iwdev);
304 err_rt_init:
305 	irdma_ctrl_deinit_hw(rf);
306 err_ctrl_init:
307 	kfree(iwdev->rf);
308 	ib_dealloc_device(&iwdev->ibdev);
309 
310 	return err;
311 }
312 
313 static const struct auxiliary_device_id irdma_auxiliary_id_table[] = {
314 	{.name = "ice.iwarp", },
315 	{.name = "ice.roce", },
316 	{},
317 };
318 
319 MODULE_DEVICE_TABLE(auxiliary, irdma_auxiliary_id_table);
320 
321 static struct iidc_auxiliary_drv irdma_auxiliary_drv = {
322 	.adrv = {
323 	    .id_table = irdma_auxiliary_id_table,
324 	    .probe = irdma_probe,
325 	    .remove = irdma_remove,
326 	},
327 	.event_handler = irdma_iidc_event_handler,
328 };
329 
330 static int __init irdma_init_module(void)
331 {
332 	int ret;
333 
334 	ret = auxiliary_driver_register(&i40iw_auxiliary_drv);
335 	if (ret) {
336 		pr_err("Failed i40iw(gen_1) auxiliary_driver_register() ret=%d\n",
337 		       ret);
338 		return ret;
339 	}
340 
341 	ret = auxiliary_driver_register(&irdma_auxiliary_drv.adrv);
342 	if (ret) {
343 		auxiliary_driver_unregister(&i40iw_auxiliary_drv);
344 		pr_err("Failed irdma auxiliary_driver_register() ret=%d\n",
345 		       ret);
346 		return ret;
347 	}
348 
349 	irdma_register_notifiers();
350 
351 	return 0;
352 }
353 
354 static void __exit irdma_exit_module(void)
355 {
356 	irdma_unregister_notifiers();
357 	auxiliary_driver_unregister(&irdma_auxiliary_drv.adrv);
358 	auxiliary_driver_unregister(&i40iw_auxiliary_drv);
359 }
360 
361 module_init(irdma_init_module);
362 module_exit(irdma_exit_module);
363