xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt_sriov.c (revision 8a5f956a9fb7d74fff681145082acfad5afa6bb8)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2018 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/ethtool.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/if_vlan.h>
16 #include <linux/interrupt.h>
17 #include <linux/etherdevice.h>
18 #include <net/dcbnl.h>
19 #include <linux/bnxt/hsi.h>
20 #include "bnxt.h"
21 #include "bnxt_hwrm.h"
22 #include "bnxt_ulp.h"
23 #include "bnxt_sriov.h"
24 #include "bnxt_vfr.h"
25 #include "bnxt_ethtool.h"
26 
27 #ifdef CONFIG_BNXT_SRIOV
28 static int bnxt_hwrm_fwd_async_event_cmpl(struct bnxt *bp,
29 					  struct bnxt_vf_info *vf, u16 event_id)
30 {
31 	struct hwrm_fwd_async_event_cmpl_input *req;
32 	struct hwrm_async_event_cmpl *async_cmpl;
33 	int rc = 0;
34 
35 	rc = hwrm_req_init(bp, req, HWRM_FWD_ASYNC_EVENT_CMPL);
36 	if (rc)
37 		goto exit;
38 
39 	if (vf)
40 		req->encap_async_event_target_id = cpu_to_le16(vf->fw_fid);
41 	else
42 		/* broadcast this async event to all VFs */
43 		req->encap_async_event_target_id = cpu_to_le16(0xffff);
44 	async_cmpl =
45 		(struct hwrm_async_event_cmpl *)req->encap_async_event_cmpl;
46 	async_cmpl->type = cpu_to_le16(ASYNC_EVENT_CMPL_TYPE_HWRM_ASYNC_EVENT);
47 	async_cmpl->event_id = cpu_to_le16(event_id);
48 
49 	rc = hwrm_req_send(bp, req);
50 exit:
51 	if (rc)
52 		netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl failed. rc:%d\n",
53 			   rc);
54 	return rc;
55 }
56 
57 static int bnxt_vf_ndo_prep(struct bnxt *bp, int vf_id)
58 {
59 	if (!bp->pf.active_vfs) {
60 		netdev_err(bp->dev, "vf ndo called though sriov is disabled\n");
61 		return -EINVAL;
62 	}
63 	if (vf_id >= bp->pf.active_vfs) {
64 		netdev_err(bp->dev, "Invalid VF id %d\n", vf_id);
65 		return -EINVAL;
66 	}
67 	return 0;
68 }
69 
70 int bnxt_set_vf_spoofchk(struct net_device *dev, int vf_id, bool setting)
71 {
72 	struct bnxt *bp = netdev_priv(dev);
73 	struct hwrm_func_cfg_input *req;
74 	bool old_setting = false;
75 	struct bnxt_vf_info *vf;
76 	u32 func_flags;
77 	int rc;
78 
79 	if (bp->hwrm_spec_code < 0x10701)
80 		return -ENOTSUPP;
81 
82 	rc = bnxt_vf_ndo_prep(bp, vf_id);
83 	if (rc)
84 		return rc;
85 
86 	vf = &bp->pf.vf[vf_id];
87 	if (vf->flags & BNXT_VF_SPOOFCHK)
88 		old_setting = true;
89 	if (old_setting == setting)
90 		return 0;
91 
92 	if (setting)
93 		func_flags = FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK_ENABLE;
94 	else
95 		func_flags = FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK_DISABLE;
96 	/*TODO: if the driver supports VLAN filter on guest VLAN,
97 	 * the spoof check should also include vlan anti-spoofing
98 	 */
99 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
100 	if (!rc) {
101 		req->fid = cpu_to_le16(vf->fw_fid);
102 		req->flags = cpu_to_le32(func_flags);
103 		rc = hwrm_req_send(bp, req);
104 		if (!rc) {
105 			if (setting)
106 				vf->flags |= BNXT_VF_SPOOFCHK;
107 			else
108 				vf->flags &= ~BNXT_VF_SPOOFCHK;
109 		}
110 	}
111 	return rc;
112 }
113 
114 static int bnxt_hwrm_func_qcfg_flags(struct bnxt *bp, struct bnxt_vf_info *vf)
115 {
116 	struct hwrm_func_qcfg_output *resp;
117 	struct hwrm_func_qcfg_input *req;
118 	int rc;
119 
120 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
121 	if (rc)
122 		return rc;
123 
124 	req->fid = cpu_to_le16(BNXT_PF(bp) ? vf->fw_fid : 0xffff);
125 	resp = hwrm_req_hold(bp, req);
126 	rc = hwrm_req_send(bp, req);
127 	if (!rc)
128 		vf->func_qcfg_flags = le16_to_cpu(resp->flags);
129 	hwrm_req_drop(bp, req);
130 	return rc;
131 }
132 
133 bool bnxt_is_trusted_vf(struct bnxt *bp, struct bnxt_vf_info *vf)
134 {
135 	if (BNXT_PF(bp) && !(bp->fw_cap & BNXT_FW_CAP_TRUSTED_VF))
136 		return !!(vf->flags & BNXT_VF_TRUST);
137 
138 	bnxt_hwrm_func_qcfg_flags(bp, vf);
139 	return !!(vf->func_qcfg_flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF);
140 }
141 
142 static int bnxt_hwrm_set_trusted_vf(struct bnxt *bp, struct bnxt_vf_info *vf)
143 {
144 	struct hwrm_func_cfg_input *req;
145 	int rc;
146 
147 	if (!(bp->fw_cap & BNXT_FW_CAP_TRUSTED_VF))
148 		return 0;
149 
150 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
151 	if (rc)
152 		return rc;
153 
154 	req->fid = cpu_to_le16(vf->fw_fid);
155 	if (vf->flags & BNXT_VF_TRUST)
156 		req->flags = cpu_to_le32(FUNC_CFG_REQ_FLAGS_TRUSTED_VF_ENABLE);
157 	else
158 		req->flags = cpu_to_le32(FUNC_CFG_REQ_FLAGS_TRUSTED_VF_DISABLE);
159 	return hwrm_req_send(bp, req);
160 }
161 
162 int bnxt_set_vf_trust(struct net_device *dev, int vf_id, bool trusted)
163 {
164 	struct bnxt *bp = netdev_priv(dev);
165 	struct bnxt_vf_info *vf;
166 
167 	if (bnxt_vf_ndo_prep(bp, vf_id))
168 		return -EINVAL;
169 
170 	vf = &bp->pf.vf[vf_id];
171 	if (trusted)
172 		vf->flags |= BNXT_VF_TRUST;
173 	else
174 		vf->flags &= ~BNXT_VF_TRUST;
175 
176 	bnxt_hwrm_set_trusted_vf(bp, vf);
177 	return 0;
178 }
179 
180 int bnxt_get_vf_config(struct net_device *dev, int vf_id,
181 		       struct ifla_vf_info *ivi)
182 {
183 	struct bnxt *bp = netdev_priv(dev);
184 	struct bnxt_vf_info *vf;
185 	int rc;
186 
187 	rc = bnxt_vf_ndo_prep(bp, vf_id);
188 	if (rc)
189 		return rc;
190 
191 	ivi->vf = vf_id;
192 	vf = &bp->pf.vf[vf_id];
193 
194 	if (is_valid_ether_addr(vf->mac_addr))
195 		memcpy(&ivi->mac, vf->mac_addr, ETH_ALEN);
196 	else
197 		memcpy(&ivi->mac, vf->vf_mac_addr, ETH_ALEN);
198 	ivi->max_tx_rate = vf->max_tx_rate;
199 	ivi->min_tx_rate = vf->min_tx_rate;
200 	ivi->vlan = vf->vlan & VLAN_VID_MASK;
201 	ivi->qos = vf->vlan >> VLAN_PRIO_SHIFT;
202 	ivi->spoofchk = !!(vf->flags & BNXT_VF_SPOOFCHK);
203 	ivi->trusted = bnxt_is_trusted_vf(bp, vf);
204 	if (!(vf->flags & BNXT_VF_LINK_FORCED))
205 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
206 	else if (vf->flags & BNXT_VF_LINK_UP)
207 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
208 	else
209 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
210 
211 	return 0;
212 }
213 
214 int bnxt_set_vf_mac(struct net_device *dev, int vf_id, u8 *mac)
215 {
216 	struct bnxt *bp = netdev_priv(dev);
217 	struct hwrm_func_cfg_input *req;
218 	struct bnxt_vf_info *vf;
219 	int rc;
220 
221 	rc = bnxt_vf_ndo_prep(bp, vf_id);
222 	if (rc)
223 		return rc;
224 	/* reject bc or mc mac addr, zero mac addr means allow
225 	 * VF to use its own mac addr
226 	 */
227 	if (is_multicast_ether_addr(mac)) {
228 		netdev_err(dev, "Invalid VF ethernet address\n");
229 		return -EINVAL;
230 	}
231 	vf = &bp->pf.vf[vf_id];
232 
233 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
234 	if (rc)
235 		return rc;
236 
237 	memcpy(vf->mac_addr, mac, ETH_ALEN);
238 
239 	req->fid = cpu_to_le16(vf->fw_fid);
240 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
241 	memcpy(req->dflt_mac_addr, mac, ETH_ALEN);
242 	return hwrm_req_send(bp, req);
243 }
244 
245 int bnxt_set_vf_vlan(struct net_device *dev, int vf_id, u16 vlan_id, u8 qos,
246 		     __be16 vlan_proto)
247 {
248 	struct bnxt *bp = netdev_priv(dev);
249 	struct hwrm_func_cfg_input *req;
250 	struct bnxt_vf_info *vf;
251 	u16 vlan_tag;
252 	int rc;
253 
254 	if (bp->hwrm_spec_code < 0x10201)
255 		return -ENOTSUPP;
256 
257 	if (vlan_proto != htons(ETH_P_8021Q) &&
258 	    (vlan_proto != htons(ETH_P_8021AD) ||
259 	     !(bp->fw_cap & BNXT_FW_CAP_DFLT_VLAN_TPID_PCP)))
260 		return -EPROTONOSUPPORT;
261 
262 	rc = bnxt_vf_ndo_prep(bp, vf_id);
263 	if (rc)
264 		return rc;
265 
266 	if (vlan_id >= VLAN_N_VID || qos >= IEEE_8021Q_MAX_PRIORITIES ||
267 	    (!vlan_id && qos))
268 		return -EINVAL;
269 
270 	vf = &bp->pf.vf[vf_id];
271 	vlan_tag = vlan_id | (u16)qos << VLAN_PRIO_SHIFT;
272 	if (vlan_tag == vf->vlan)
273 		return 0;
274 
275 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
276 	if (!rc) {
277 		req->fid = cpu_to_le16(vf->fw_fid);
278 		req->dflt_vlan = cpu_to_le16(vlan_tag);
279 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_VLAN);
280 		if (bp->fw_cap & BNXT_FW_CAP_DFLT_VLAN_TPID_PCP) {
281 			req->enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_TPID);
282 			req->tpid = vlan_proto;
283 		}
284 		rc = hwrm_req_send(bp, req);
285 		if (!rc)
286 			vf->vlan = vlan_tag;
287 	}
288 	return rc;
289 }
290 
291 int bnxt_set_vf_bw(struct net_device *dev, int vf_id, int min_tx_rate,
292 		   int max_tx_rate)
293 {
294 	struct bnxt *bp = netdev_priv(dev);
295 	struct hwrm_func_cfg_input *req;
296 	struct bnxt_vf_info *vf;
297 	u32 pf_link_speed;
298 	int rc;
299 
300 	rc = bnxt_vf_ndo_prep(bp, vf_id);
301 	if (rc)
302 		return rc;
303 
304 	vf = &bp->pf.vf[vf_id];
305 	pf_link_speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
306 	if (max_tx_rate > pf_link_speed) {
307 		netdev_info(bp->dev, "max tx rate %d exceed PF link speed for VF %d\n",
308 			    max_tx_rate, vf_id);
309 		return -EINVAL;
310 	}
311 
312 	if (min_tx_rate > pf_link_speed) {
313 		netdev_info(bp->dev, "min tx rate %d is invalid for VF %d\n",
314 			    min_tx_rate, vf_id);
315 		return -EINVAL;
316 	}
317 	if (min_tx_rate == vf->min_tx_rate && max_tx_rate == vf->max_tx_rate)
318 		return 0;
319 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
320 	if (!rc) {
321 		req->fid = cpu_to_le16(vf->fw_fid);
322 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MAX_BW |
323 					   FUNC_CFG_REQ_ENABLES_MIN_BW);
324 		req->max_bw = cpu_to_le32(max_tx_rate);
325 		req->min_bw = cpu_to_le32(min_tx_rate);
326 		rc = hwrm_req_send(bp, req);
327 		if (!rc) {
328 			vf->min_tx_rate = min_tx_rate;
329 			vf->max_tx_rate = max_tx_rate;
330 		}
331 	}
332 	return rc;
333 }
334 
335 int bnxt_set_vf_link_state(struct net_device *dev, int vf_id, int link)
336 {
337 	struct bnxt *bp = netdev_priv(dev);
338 	struct bnxt_vf_info *vf;
339 	int rc;
340 
341 	rc = bnxt_vf_ndo_prep(bp, vf_id);
342 	if (rc)
343 		return rc;
344 
345 	vf = &bp->pf.vf[vf_id];
346 
347 	vf->flags &= ~(BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED);
348 	switch (link) {
349 	case IFLA_VF_LINK_STATE_AUTO:
350 		vf->flags |= BNXT_VF_LINK_UP;
351 		break;
352 	case IFLA_VF_LINK_STATE_DISABLE:
353 		vf->flags |= BNXT_VF_LINK_FORCED;
354 		break;
355 	case IFLA_VF_LINK_STATE_ENABLE:
356 		vf->flags |= BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED;
357 		break;
358 	default:
359 		netdev_err(bp->dev, "Invalid link option\n");
360 		rc = -EINVAL;
361 		break;
362 	}
363 	if (vf->flags & (BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED))
364 		rc = bnxt_hwrm_fwd_async_event_cmpl(bp, vf,
365 			ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE);
366 	return rc;
367 }
368 
369 static int bnxt_set_vf_attr(struct bnxt *bp, int num_vfs)
370 {
371 	int i;
372 	struct bnxt_vf_info *vf;
373 
374 	for (i = 0; i < num_vfs; i++) {
375 		vf = &bp->pf.vf[i];
376 		memset(vf, 0, sizeof(*vf));
377 	}
378 	return 0;
379 }
380 
381 static int bnxt_hwrm_func_vf_resource_free(struct bnxt *bp, int num_vfs)
382 {
383 	struct hwrm_func_vf_resc_free_input *req;
384 	struct bnxt_pf_info *pf = &bp->pf;
385 	int i, rc;
386 
387 	rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_RESC_FREE);
388 	if (rc)
389 		return rc;
390 
391 	hwrm_req_hold(bp, req);
392 	for (i = pf->first_vf_id; i < pf->first_vf_id + num_vfs; i++) {
393 		req->vf_id = cpu_to_le16(i);
394 		rc = hwrm_req_send(bp, req);
395 		if (rc)
396 			break;
397 	}
398 	hwrm_req_drop(bp, req);
399 	return rc;
400 }
401 
402 static void bnxt_free_vf_resources(struct bnxt *bp)
403 {
404 	struct pci_dev *pdev = bp->pdev;
405 	int i;
406 
407 	kfree(bp->pf.vf_event_bmap);
408 	bp->pf.vf_event_bmap = NULL;
409 
410 	for (i = 0; i < 4; i++) {
411 		if (bp->pf.hwrm_cmd_req_addr[i]) {
412 			dma_free_coherent(&pdev->dev, BNXT_PAGE_SIZE,
413 					  bp->pf.hwrm_cmd_req_addr[i],
414 					  bp->pf.hwrm_cmd_req_dma_addr[i]);
415 			bp->pf.hwrm_cmd_req_addr[i] = NULL;
416 		}
417 	}
418 
419 	bp->pf.active_vfs = 0;
420 	kfree(bp->pf.vf);
421 	bp->pf.vf = NULL;
422 }
423 
424 static int bnxt_alloc_vf_resources(struct bnxt *bp, int num_vfs)
425 {
426 	struct pci_dev *pdev = bp->pdev;
427 	u32 nr_pages, size, i, j, k = 0;
428 
429 	bp->pf.vf = kcalloc(num_vfs, sizeof(struct bnxt_vf_info), GFP_KERNEL);
430 	if (!bp->pf.vf)
431 		return -ENOMEM;
432 
433 	bnxt_set_vf_attr(bp, num_vfs);
434 
435 	size = num_vfs * BNXT_HWRM_REQ_MAX_SIZE;
436 	nr_pages = size / BNXT_PAGE_SIZE;
437 	if (size & (BNXT_PAGE_SIZE - 1))
438 		nr_pages++;
439 
440 	for (i = 0; i < nr_pages; i++) {
441 		bp->pf.hwrm_cmd_req_addr[i] =
442 			dma_alloc_coherent(&pdev->dev, BNXT_PAGE_SIZE,
443 					   &bp->pf.hwrm_cmd_req_dma_addr[i],
444 					   GFP_KERNEL);
445 
446 		if (!bp->pf.hwrm_cmd_req_addr[i])
447 			return -ENOMEM;
448 
449 		for (j = 0; j < BNXT_HWRM_REQS_PER_PAGE && k < num_vfs; j++) {
450 			struct bnxt_vf_info *vf = &bp->pf.vf[k];
451 
452 			vf->hwrm_cmd_req_addr = bp->pf.hwrm_cmd_req_addr[i] +
453 						j * BNXT_HWRM_REQ_MAX_SIZE;
454 			vf->hwrm_cmd_req_dma_addr =
455 				bp->pf.hwrm_cmd_req_dma_addr[i] + j *
456 				BNXT_HWRM_REQ_MAX_SIZE;
457 			k++;
458 		}
459 	}
460 
461 	/* Max 128 VF's */
462 	bp->pf.vf_event_bmap = kzalloc(16, GFP_KERNEL);
463 	if (!bp->pf.vf_event_bmap)
464 		return -ENOMEM;
465 
466 	bp->pf.hwrm_cmd_req_pages = nr_pages;
467 	return 0;
468 }
469 
470 static int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
471 {
472 	struct hwrm_func_buf_rgtr_input *req;
473 	int rc;
474 
475 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BUF_RGTR);
476 	if (rc)
477 		return rc;
478 
479 	req->req_buf_num_pages = cpu_to_le16(bp->pf.hwrm_cmd_req_pages);
480 	req->req_buf_page_size = cpu_to_le16(BNXT_PAGE_SHIFT);
481 	req->req_buf_len = cpu_to_le16(BNXT_HWRM_REQ_MAX_SIZE);
482 	req->req_buf_page_addr0 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[0]);
483 	req->req_buf_page_addr1 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[1]);
484 	req->req_buf_page_addr2 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[2]);
485 	req->req_buf_page_addr3 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[3]);
486 
487 	return hwrm_req_send(bp, req);
488 }
489 
490 static int __bnxt_set_vf_params(struct bnxt *bp, int vf_id)
491 {
492 	struct hwrm_func_cfg_input *req;
493 	struct bnxt_vf_info *vf;
494 	int rc;
495 
496 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
497 	if (rc)
498 		return rc;
499 
500 	vf = &bp->pf.vf[vf_id];
501 	req->fid = cpu_to_le16(vf->fw_fid);
502 
503 	if (is_valid_ether_addr(vf->mac_addr)) {
504 		req->enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
505 		memcpy(req->dflt_mac_addr, vf->mac_addr, ETH_ALEN);
506 	}
507 	if (vf->vlan) {
508 		req->enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_VLAN);
509 		req->dflt_vlan = cpu_to_le16(vf->vlan);
510 	}
511 	if (vf->max_tx_rate) {
512 		req->enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_MAX_BW |
513 					    FUNC_CFG_REQ_ENABLES_MIN_BW);
514 		req->max_bw = cpu_to_le32(vf->max_tx_rate);
515 		req->min_bw = cpu_to_le32(vf->min_tx_rate);
516 	}
517 	if (vf->flags & BNXT_VF_TRUST)
518 		req->flags |= cpu_to_le32(FUNC_CFG_REQ_FLAGS_TRUSTED_VF_ENABLE);
519 
520 	return hwrm_req_send(bp, req);
521 }
522 
523 static void bnxt_hwrm_roce_sriov_cfg(struct bnxt *bp, int num_vfs)
524 {
525 	struct hwrm_func_qcaps_output *resp;
526 	struct hwrm_func_cfg_input *cfg_req;
527 	struct hwrm_func_qcaps_input *req;
528 	int rc;
529 
530 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
531 	if (rc)
532 		return;
533 
534 	req->fid = cpu_to_le16(0xffff);
535 	resp = hwrm_req_hold(bp, req);
536 	rc = hwrm_req_send(bp, req);
537 	if (rc)
538 		goto err;
539 
540 	rc = hwrm_req_init(bp, cfg_req, HWRM_FUNC_CFG);
541 	if (rc)
542 		goto err;
543 
544 	/* In case of VF Dynamic resource allocation, driver will provision
545 	 * maximum resources to all the VFs. FW will dynamically allocate
546 	 * resources to VFs on the fly, so always divide the resources by 1.
547 	 */
548 	if (BNXT_ROCE_VF_DYN_ALLOC_CAP(bp))
549 		num_vfs = 1;
550 
551 	cfg_req->fid = cpu_to_le16(0xffff);
552 	cfg_req->enables2 =
553 		cpu_to_le32(FUNC_CFG_REQ_ENABLES2_ROCE_MAX_AV_PER_VF |
554 			    FUNC_CFG_REQ_ENABLES2_ROCE_MAX_CQ_PER_VF |
555 			    FUNC_CFG_REQ_ENABLES2_ROCE_MAX_MRW_PER_VF |
556 			    FUNC_CFG_REQ_ENABLES2_ROCE_MAX_QP_PER_VF |
557 			    FUNC_CFG_REQ_ENABLES2_ROCE_MAX_SRQ_PER_VF |
558 			    FUNC_CFG_REQ_ENABLES2_ROCE_MAX_GID_PER_VF);
559 	cfg_req->roce_max_av_per_vf =
560 		cpu_to_le32(le32_to_cpu(resp->roce_vf_max_av) / num_vfs);
561 	cfg_req->roce_max_cq_per_vf =
562 		cpu_to_le32(le32_to_cpu(resp->roce_vf_max_cq) / num_vfs);
563 	cfg_req->roce_max_mrw_per_vf =
564 		cpu_to_le32(le32_to_cpu(resp->roce_vf_max_mrw) / num_vfs);
565 	cfg_req->roce_max_qp_per_vf =
566 		cpu_to_le32(le32_to_cpu(resp->roce_vf_max_qp) / num_vfs);
567 	cfg_req->roce_max_srq_per_vf =
568 		cpu_to_le32(le32_to_cpu(resp->roce_vf_max_srq) / num_vfs);
569 	cfg_req->roce_max_gid_per_vf =
570 		cpu_to_le32(le32_to_cpu(resp->roce_vf_max_gid) / num_vfs);
571 
572 	rc = hwrm_req_send(bp, cfg_req);
573 
574 err:
575 	hwrm_req_drop(bp, req);
576 	if (rc)
577 		netdev_err(bp->dev, "RoCE sriov configuration failed\n");
578 }
579 
580 /* Only called by PF to reserve resources for VFs, returns actual number of
581  * VFs configured, or < 0 on error.
582  */
583 static int bnxt_hwrm_func_vf_resc_cfg(struct bnxt *bp, int num_vfs, bool reset)
584 {
585 	struct hwrm_func_vf_resource_cfg_input *req;
586 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
587 	u16 vf_tx_rings, vf_rx_rings, vf_cp_rings;
588 	u16 vf_stat_ctx, vf_vnics, vf_ring_grps;
589 	struct bnxt_pf_info *pf = &bp->pf;
590 	int i, rc = 0, min = 1;
591 	u16 vf_msix = 0;
592 	u16 vf_rss;
593 
594 	rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_RESOURCE_CFG);
595 	if (rc)
596 		return rc;
597 
598 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
599 		vf_msix = hw_resc->max_nqs - bnxt_nq_rings_in_use(bp);
600 		vf_ring_grps = 0;
601 	} else {
602 		vf_ring_grps = hw_resc->max_hw_ring_grps - bp->rx_nr_rings;
603 	}
604 	vf_cp_rings = bnxt_get_avail_cp_rings_for_en(bp);
605 	vf_stat_ctx = bnxt_get_avail_stat_ctxs_for_en(bp);
606 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
607 		vf_rx_rings = hw_resc->max_rx_rings - bp->rx_nr_rings * 2;
608 	else
609 		vf_rx_rings = hw_resc->max_rx_rings - bp->rx_nr_rings;
610 	vf_tx_rings = hw_resc->max_tx_rings - bp->tx_nr_rings;
611 	vf_vnics = hw_resc->max_vnics - bp->nr_vnics;
612 	vf_rss = hw_resc->max_rsscos_ctxs - bp->rsscos_nr_ctxs;
613 
614 	req->min_rsscos_ctx = cpu_to_le16(BNXT_VF_MIN_RSS_CTX);
615 	if (pf->vf_resv_strategy == BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) {
616 		min = 0;
617 		req->min_rsscos_ctx = cpu_to_le16(min);
618 	}
619 	if (pf->vf_resv_strategy == BNXT_VF_RESV_STRATEGY_MINIMAL ||
620 	    pf->vf_resv_strategy == BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC) {
621 		req->min_cmpl_rings = cpu_to_le16(min);
622 		req->min_tx_rings = cpu_to_le16(min);
623 		req->min_rx_rings = cpu_to_le16(min);
624 		req->min_l2_ctxs = cpu_to_le16(min);
625 		req->min_vnics = cpu_to_le16(min);
626 		req->min_stat_ctx = cpu_to_le16(min);
627 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
628 			req->min_hw_ring_grps = cpu_to_le16(min);
629 	} else {
630 		vf_cp_rings /= num_vfs;
631 		vf_tx_rings /= num_vfs;
632 		vf_rx_rings /= num_vfs;
633 		if ((bp->fw_cap & BNXT_FW_CAP_PRE_RESV_VNICS) &&
634 		    vf_vnics >= pf->max_vfs) {
635 			/* Take into account that FW has pre-reserved 1 VNIC for
636 			 * each pf->max_vfs.
637 			 */
638 			vf_vnics = (vf_vnics - pf->max_vfs + num_vfs) / num_vfs;
639 		} else {
640 			vf_vnics /= num_vfs;
641 		}
642 		vf_stat_ctx /= num_vfs;
643 		vf_ring_grps /= num_vfs;
644 		vf_rss /= num_vfs;
645 
646 		vf_vnics = min_t(u16, vf_vnics, vf_rx_rings);
647 		req->min_cmpl_rings = cpu_to_le16(vf_cp_rings);
648 		req->min_tx_rings = cpu_to_le16(vf_tx_rings);
649 		req->min_rx_rings = cpu_to_le16(vf_rx_rings);
650 		req->min_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
651 		req->min_vnics = cpu_to_le16(vf_vnics);
652 		req->min_stat_ctx = cpu_to_le16(vf_stat_ctx);
653 		req->min_hw_ring_grps = cpu_to_le16(vf_ring_grps);
654 		req->min_rsscos_ctx = cpu_to_le16(vf_rss);
655 	}
656 	req->max_cmpl_rings = cpu_to_le16(vf_cp_rings);
657 	req->max_tx_rings = cpu_to_le16(vf_tx_rings);
658 	req->max_rx_rings = cpu_to_le16(vf_rx_rings);
659 	req->max_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
660 	req->max_vnics = cpu_to_le16(vf_vnics);
661 	req->max_stat_ctx = cpu_to_le16(vf_stat_ctx);
662 	req->max_hw_ring_grps = cpu_to_le16(vf_ring_grps);
663 	req->max_rsscos_ctx = cpu_to_le16(vf_rss);
664 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
665 		req->max_msix = cpu_to_le16(vf_msix / num_vfs);
666 
667 	hwrm_req_hold(bp, req);
668 	for (i = 0; i < num_vfs; i++) {
669 		if (reset)
670 			__bnxt_set_vf_params(bp, i);
671 
672 		req->vf_id = cpu_to_le16(pf->first_vf_id + i);
673 		rc = hwrm_req_send(bp, req);
674 		if (rc)
675 			break;
676 		pf->active_vfs = i + 1;
677 		pf->vf[i].fw_fid = pf->first_vf_id + i;
678 	}
679 
680 	if (pf->active_vfs) {
681 		u16 n = pf->active_vfs;
682 
683 		hw_resc->max_tx_rings -= le16_to_cpu(req->min_tx_rings) * n;
684 		hw_resc->max_rx_rings -= le16_to_cpu(req->min_rx_rings) * n;
685 		hw_resc->max_hw_ring_grps -=
686 			le16_to_cpu(req->min_hw_ring_grps) * n;
687 		hw_resc->max_cp_rings -= le16_to_cpu(req->min_cmpl_rings) * n;
688 		hw_resc->max_rsscos_ctxs -=
689 			le16_to_cpu(req->min_rsscos_ctx) * n;
690 		hw_resc->max_stat_ctxs -= le16_to_cpu(req->min_stat_ctx) * n;
691 		hw_resc->max_vnics -= le16_to_cpu(req->min_vnics) * n;
692 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
693 			hw_resc->max_nqs -= vf_msix;
694 
695 		rc = pf->active_vfs;
696 	}
697 	hwrm_req_drop(bp, req);
698 	return rc;
699 }
700 
701 /* Only called by PF to reserve resources for VFs, returns actual number of
702  * VFs configured, or < 0 on error.
703  */
704 static int bnxt_hwrm_func_cfg(struct bnxt *bp, int num_vfs)
705 {
706 	u16 vf_tx_rings, vf_rx_rings, vf_cp_rings, vf_stat_ctx, vf_vnics;
707 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
708 	struct bnxt_pf_info *pf = &bp->pf;
709 	struct hwrm_func_cfg_input *req;
710 	int total_vf_tx_rings = 0;
711 	u16 vf_ring_grps;
712 	u32 mtu, i;
713 	int rc;
714 
715 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
716 	if (rc)
717 		return rc;
718 
719 	/* Remaining rings are distributed equally amongs VF's for now */
720 	vf_cp_rings = bnxt_get_avail_cp_rings_for_en(bp) / num_vfs;
721 	vf_stat_ctx = bnxt_get_avail_stat_ctxs_for_en(bp) / num_vfs;
722 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
723 		vf_rx_rings = (hw_resc->max_rx_rings - bp->rx_nr_rings * 2) /
724 			      num_vfs;
725 	else
726 		vf_rx_rings = (hw_resc->max_rx_rings - bp->rx_nr_rings) /
727 			      num_vfs;
728 	vf_ring_grps = (hw_resc->max_hw_ring_grps - bp->rx_nr_rings) / num_vfs;
729 	vf_tx_rings = (hw_resc->max_tx_rings - bp->tx_nr_rings) / num_vfs;
730 	vf_vnics = (hw_resc->max_vnics - bp->nr_vnics) / num_vfs;
731 	vf_vnics = min_t(u16, vf_vnics, vf_rx_rings);
732 
733 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ADMIN_MTU |
734 				   FUNC_CFG_REQ_ENABLES_MRU |
735 				   FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
736 				   FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS |
737 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
738 				   FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS |
739 				   FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS |
740 				   FUNC_CFG_REQ_ENABLES_NUM_L2_CTXS |
741 				   FUNC_CFG_REQ_ENABLES_NUM_VNICS |
742 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS);
743 
744 	mtu = bp->dev->mtu + VLAN_ETH_HLEN;
745 	req->mru = cpu_to_le16(mtu);
746 	req->admin_mtu = cpu_to_le16(mtu);
747 
748 	req->num_rsscos_ctxs = cpu_to_le16(1);
749 	req->num_cmpl_rings = cpu_to_le16(vf_cp_rings);
750 	req->num_tx_rings = cpu_to_le16(vf_tx_rings);
751 	req->num_rx_rings = cpu_to_le16(vf_rx_rings);
752 	req->num_hw_ring_grps = cpu_to_le16(vf_ring_grps);
753 	req->num_l2_ctxs = cpu_to_le16(4);
754 
755 	req->num_vnics = cpu_to_le16(vf_vnics);
756 	/* FIXME spec currently uses 1 bit for stats ctx */
757 	req->num_stat_ctxs = cpu_to_le16(vf_stat_ctx);
758 
759 	hwrm_req_hold(bp, req);
760 	for (i = 0; i < num_vfs; i++) {
761 		int vf_tx_rsvd = vf_tx_rings;
762 
763 		req->fid = cpu_to_le16(pf->first_vf_id + i);
764 		rc = hwrm_req_send(bp, req);
765 		if (rc)
766 			break;
767 		pf->active_vfs = i + 1;
768 		pf->vf[i].fw_fid = le16_to_cpu(req->fid);
769 		rc = __bnxt_hwrm_get_tx_rings(bp, pf->vf[i].fw_fid,
770 					      &vf_tx_rsvd);
771 		if (rc)
772 			break;
773 		total_vf_tx_rings += vf_tx_rsvd;
774 	}
775 	hwrm_req_drop(bp, req);
776 	if (pf->active_vfs) {
777 		hw_resc->max_tx_rings -= total_vf_tx_rings;
778 		hw_resc->max_rx_rings -= vf_rx_rings * num_vfs;
779 		hw_resc->max_hw_ring_grps -= vf_ring_grps * num_vfs;
780 		hw_resc->max_cp_rings -= vf_cp_rings * num_vfs;
781 		hw_resc->max_rsscos_ctxs -= num_vfs;
782 		hw_resc->max_stat_ctxs -= vf_stat_ctx * num_vfs;
783 		hw_resc->max_vnics -= vf_vnics * num_vfs;
784 		rc = pf->active_vfs;
785 	}
786 	return rc;
787 }
788 
789 static int bnxt_func_cfg(struct bnxt *bp, int num_vfs, bool reset)
790 {
791 	if (BNXT_NEW_RM(bp))
792 		return bnxt_hwrm_func_vf_resc_cfg(bp, num_vfs, reset);
793 	else
794 		return bnxt_hwrm_func_cfg(bp, num_vfs);
795 }
796 
797 int bnxt_cfg_hw_sriov(struct bnxt *bp, int *num_vfs, bool reset)
798 {
799 	int rc;
800 
801 	/* Register buffers for VFs */
802 	rc = bnxt_hwrm_func_buf_rgtr(bp);
803 	if (rc)
804 		return rc;
805 
806 	/* Reserve resources for VFs */
807 	rc = bnxt_func_cfg(bp, *num_vfs, reset);
808 	if (rc != *num_vfs) {
809 		if (rc <= 0) {
810 			netdev_warn(bp->dev, "Unable to reserve resources for SRIOV.\n");
811 			*num_vfs = 0;
812 			return rc;
813 		}
814 		netdev_warn(bp->dev, "Only able to reserve resources for %d VFs.\n",
815 			    rc);
816 		*num_vfs = rc;
817 	}
818 
819 	if (BNXT_RDMA_SRIOV_EN(bp) && BNXT_ROCE_VF_RESC_CAP(bp))
820 		bnxt_hwrm_roce_sriov_cfg(bp, *num_vfs);
821 
822 	return 0;
823 }
824 
825 static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs)
826 {
827 	int rc = 0, vfs_supported;
828 	int min_rx_rings, min_tx_rings, min_rss_ctxs;
829 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
830 	int tx_ok = 0, rx_ok = 0, rss_ok = 0;
831 	int avail_cp, avail_stat;
832 
833 	/* Check if we can enable requested num of vf's. At a minimum
834 	 * we require 1 RX 1 TX rings for each VF. In this minimum conf
835 	 * features like TPA will not be available.
836 	 */
837 	vfs_supported = *num_vfs;
838 
839 	avail_cp = bnxt_get_avail_cp_rings_for_en(bp);
840 	avail_stat = bnxt_get_avail_stat_ctxs_for_en(bp);
841 	avail_cp = min_t(int, avail_cp, avail_stat);
842 
843 	while (vfs_supported) {
844 		min_rx_rings = vfs_supported;
845 		min_tx_rings = vfs_supported;
846 		min_rss_ctxs = vfs_supported;
847 
848 		if (bp->flags & BNXT_FLAG_AGG_RINGS) {
849 			if (hw_resc->max_rx_rings - bp->rx_nr_rings * 2 >=
850 			    min_rx_rings)
851 				rx_ok = 1;
852 		} else {
853 			if (hw_resc->max_rx_rings - bp->rx_nr_rings >=
854 			    min_rx_rings)
855 				rx_ok = 1;
856 		}
857 		if (hw_resc->max_vnics - bp->nr_vnics < min_rx_rings ||
858 		    avail_cp < min_rx_rings)
859 			rx_ok = 0;
860 
861 		if (hw_resc->max_tx_rings - bp->tx_nr_rings >= min_tx_rings &&
862 		    avail_cp >= min_tx_rings)
863 			tx_ok = 1;
864 
865 		if (hw_resc->max_rsscos_ctxs - bp->rsscos_nr_ctxs >=
866 		    min_rss_ctxs)
867 			rss_ok = 1;
868 
869 		if (tx_ok && rx_ok && rss_ok)
870 			break;
871 
872 		vfs_supported--;
873 	}
874 
875 	if (!vfs_supported) {
876 		netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n");
877 		return -EINVAL;
878 	}
879 
880 	if (vfs_supported != *num_vfs) {
881 		netdev_info(bp->dev, "Requested VFs %d, can enable %d\n",
882 			    *num_vfs, vfs_supported);
883 		*num_vfs = vfs_supported;
884 	}
885 
886 	rc = bnxt_alloc_vf_resources(bp, *num_vfs);
887 	if (rc)
888 		goto err_out1;
889 
890 	rc = bnxt_cfg_hw_sriov(bp, num_vfs, false);
891 	if (rc)
892 		goto err_out2;
893 
894 	rc = pci_enable_sriov(bp->pdev, *num_vfs);
895 	if (rc)
896 		goto err_out2;
897 
898 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
899 		return 0;
900 
901 	/* Create representors for VFs in switchdev mode */
902 	devl_lock(bp->dl);
903 	rc = bnxt_vf_reps_create(bp);
904 	devl_unlock(bp->dl);
905 	if (rc) {
906 		netdev_info(bp->dev, "Cannot enable VFS as representors cannot be created\n");
907 		goto err_out3;
908 	}
909 
910 	return 0;
911 
912 err_out3:
913 	/* Disable SR-IOV */
914 	pci_disable_sriov(bp->pdev);
915 
916 err_out2:
917 	/* Free the resources reserved for various VF's */
918 	bnxt_hwrm_func_vf_resource_free(bp, *num_vfs);
919 
920 	/* Restore the max resources */
921 	bnxt_hwrm_func_qcaps(bp);
922 
923 err_out1:
924 	bnxt_free_vf_resources(bp);
925 
926 	return rc;
927 }
928 
929 void __bnxt_sriov_disable(struct bnxt *bp)
930 {
931 	u16 num_vfs = pci_num_vf(bp->pdev);
932 
933 	if (!num_vfs)
934 		return;
935 
936 	/* synchronize VF and VF-rep create and destroy */
937 	devl_lock(bp->dl);
938 	bnxt_vf_reps_destroy(bp);
939 
940 	if (pci_vfs_assigned(bp->pdev)) {
941 		bnxt_hwrm_fwd_async_event_cmpl(
942 			bp, NULL, ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD);
943 		netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n",
944 			    num_vfs);
945 	} else {
946 		pci_disable_sriov(bp->pdev);
947 		/* Free the HW resources reserved for various VF's */
948 		bnxt_hwrm_func_vf_resource_free(bp, num_vfs);
949 	}
950 	devl_unlock(bp->dl);
951 
952 	bnxt_free_vf_resources(bp);
953 }
954 
955 static void bnxt_sriov_disable(struct bnxt *bp)
956 {
957 	if (!pci_num_vf(bp->pdev))
958 		return;
959 
960 	__bnxt_sriov_disable(bp);
961 
962 	/* Reclaim all resources for the PF. */
963 	rtnl_lock();
964 	netdev_lock(bp->dev);
965 	bnxt_restore_pf_fw_resources(bp);
966 	netdev_unlock(bp->dev);
967 	rtnl_unlock();
968 }
969 
970 int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)
971 {
972 	struct net_device *dev = pci_get_drvdata(pdev);
973 	struct bnxt *bp = netdev_priv(dev);
974 
975 	rtnl_lock();
976 	netdev_lock(dev);
977 	if (!netif_running(dev)) {
978 		netdev_warn(dev, "Reject SRIOV config request since if is down!\n");
979 		netdev_unlock(dev);
980 		rtnl_unlock();
981 		return 0;
982 	}
983 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
984 		netdev_warn(dev, "Reject SRIOV config request when FW reset is in progress\n");
985 		netdev_unlock(dev);
986 		rtnl_unlock();
987 		return 0;
988 	}
989 	bp->sriov_cfg = true;
990 	netdev_unlock(dev);
991 	rtnl_unlock();
992 
993 	if (pci_vfs_assigned(bp->pdev)) {
994 		netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n");
995 		num_vfs = 0;
996 		goto sriov_cfg_exit;
997 	}
998 
999 	/* Check if enabled VFs is same as requested */
1000 	if (num_vfs && num_vfs == bp->pf.active_vfs)
1001 		goto sriov_cfg_exit;
1002 
1003 	/* if there are previous existing VFs, clean them up */
1004 	bnxt_sriov_disable(bp);
1005 	if (!num_vfs)
1006 		goto sriov_cfg_exit;
1007 
1008 	bnxt_sriov_enable(bp, &num_vfs);
1009 
1010 sriov_cfg_exit:
1011 	bp->sriov_cfg = false;
1012 	wake_up(&bp->sriov_cfg_wait);
1013 
1014 	return num_vfs;
1015 }
1016 
1017 static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
1018 			      void *encap_resp, __le64 encap_resp_addr,
1019 			      __le16 encap_resp_cpr, u32 msg_size)
1020 {
1021 	struct hwrm_fwd_resp_input *req;
1022 	int rc;
1023 
1024 	if (BNXT_FWD_RESP_SIZE_ERR(msg_size)) {
1025 		netdev_warn_once(bp->dev, "HWRM fwd response too big (%d bytes)\n",
1026 				 msg_size);
1027 		return -EINVAL;
1028 	}
1029 
1030 	rc = hwrm_req_init(bp, req, HWRM_FWD_RESP);
1031 	if (!rc) {
1032 		/* Set the new target id */
1033 		req->target_id = cpu_to_le16(vf->fw_fid);
1034 		req->encap_resp_target_id = cpu_to_le16(vf->fw_fid);
1035 		req->encap_resp_len = cpu_to_le16(msg_size);
1036 		req->encap_resp_addr = encap_resp_addr;
1037 		req->encap_resp_cmpl_ring = encap_resp_cpr;
1038 		memcpy(req->encap_resp, encap_resp, msg_size);
1039 
1040 		rc = hwrm_req_send(bp, req);
1041 	}
1042 	if (rc)
1043 		netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc);
1044 	return rc;
1045 }
1046 
1047 static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
1048 				  u32 msg_size)
1049 {
1050 	struct hwrm_reject_fwd_resp_input *req;
1051 	int rc;
1052 
1053 	if (BNXT_REJ_FWD_RESP_SIZE_ERR(msg_size))
1054 		return -EINVAL;
1055 
1056 	rc = hwrm_req_init(bp, req, HWRM_REJECT_FWD_RESP);
1057 	if (!rc) {
1058 		/* Set the new target id */
1059 		req->target_id = cpu_to_le16(vf->fw_fid);
1060 		req->encap_resp_target_id = cpu_to_le16(vf->fw_fid);
1061 		memcpy(req->encap_request, vf->hwrm_cmd_req_addr, msg_size);
1062 
1063 		rc = hwrm_req_send(bp, req);
1064 	}
1065 	if (rc)
1066 		netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc);
1067 	return rc;
1068 }
1069 
1070 static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
1071 				   u32 msg_size)
1072 {
1073 	struct hwrm_exec_fwd_resp_input *req;
1074 	int rc;
1075 
1076 	if (BNXT_EXEC_FWD_RESP_SIZE_ERR(msg_size))
1077 		return -EINVAL;
1078 
1079 	rc = hwrm_req_init(bp, req, HWRM_EXEC_FWD_RESP);
1080 	if (!rc) {
1081 		/* Set the new target id */
1082 		req->target_id = cpu_to_le16(vf->fw_fid);
1083 		req->encap_resp_target_id = cpu_to_le16(vf->fw_fid);
1084 		memcpy(req->encap_request, vf->hwrm_cmd_req_addr, msg_size);
1085 
1086 		rc = hwrm_req_send(bp, req);
1087 	}
1088 	if (rc)
1089 		netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc);
1090 	return rc;
1091 }
1092 
1093 static int bnxt_vf_configure_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
1094 {
1095 	u32 msg_size = sizeof(struct hwrm_func_vf_cfg_input);
1096 	struct hwrm_func_vf_cfg_input *req =
1097 		(struct hwrm_func_vf_cfg_input *)vf->hwrm_cmd_req_addr;
1098 
1099 	/* Allow VF to set a valid MAC address, if trust is set to on or
1100 	 * if the PF assigned MAC address is zero
1101 	 */
1102 	if (req->enables & cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR)) {
1103 		bool trust = bnxt_is_trusted_vf(bp, vf);
1104 
1105 		if (is_valid_ether_addr(req->dflt_mac_addr) &&
1106 		    (trust || !is_valid_ether_addr(vf->mac_addr) ||
1107 		     ether_addr_equal(req->dflt_mac_addr, vf->mac_addr))) {
1108 			ether_addr_copy(vf->vf_mac_addr, req->dflt_mac_addr);
1109 			return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
1110 		}
1111 		return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
1112 	}
1113 	return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
1114 }
1115 
1116 static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
1117 {
1118 	u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input);
1119 	struct hwrm_cfa_l2_filter_alloc_input *req =
1120 		(struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr;
1121 	bool mac_ok = false;
1122 
1123 	if (!is_valid_ether_addr((const u8 *)req->l2_addr))
1124 		return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
1125 
1126 	/* Allow VF to set a valid MAC address, if trust is set to on.
1127 	 * Or VF MAC address must first match MAC address in PF's context.
1128 	 * Otherwise, it must match the VF MAC address if firmware spec >=
1129 	 * 1.2.2
1130 	 */
1131 	if (bnxt_is_trusted_vf(bp, vf)) {
1132 		mac_ok = true;
1133 	} else if (is_valid_ether_addr(vf->mac_addr)) {
1134 		if (ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr))
1135 			mac_ok = true;
1136 	} else if (is_valid_ether_addr(vf->vf_mac_addr)) {
1137 		if (ether_addr_equal((const u8 *)req->l2_addr, vf->vf_mac_addr))
1138 			mac_ok = true;
1139 	} else {
1140 		/* There are two cases:
1141 		 * 1.If firmware spec < 0x10202,VF MAC address is not forwarded
1142 		 *   to the PF and so it doesn't have to match
1143 		 * 2.Allow VF to modify its own MAC when PF has not assigned a
1144 		 *   valid MAC address and firmware spec >= 0x10202
1145 		 */
1146 		mac_ok = true;
1147 	}
1148 	if (mac_ok)
1149 		return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
1150 	return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
1151 }
1152 
1153 static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf)
1154 {
1155 	int rc = 0;
1156 
1157 	if (!(vf->flags & BNXT_VF_LINK_FORCED)) {
1158 		/* real link */
1159 		rc = bnxt_hwrm_exec_fwd_resp(
1160 			bp, vf, sizeof(struct hwrm_port_phy_qcfg_input));
1161 	} else {
1162 		struct hwrm_port_phy_qcfg_output_compat phy_qcfg_resp = {};
1163 		struct hwrm_port_phy_qcfg_input *phy_qcfg_req;
1164 
1165 		phy_qcfg_req =
1166 		(struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr;
1167 		mutex_lock(&bp->link_lock);
1168 		memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp,
1169 		       sizeof(phy_qcfg_resp));
1170 		mutex_unlock(&bp->link_lock);
1171 		phy_qcfg_resp.resp_len = cpu_to_le16(sizeof(phy_qcfg_resp));
1172 		phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id;
1173 		/* New SPEEDS2 fields are beyond the legacy structure, so
1174 		 * clear the SPEEDS2_SUPPORTED flag.
1175 		 */
1176 		phy_qcfg_resp.option_flags &=
1177 			~PORT_PHY_QCAPS_RESP_FLAGS2_SPEEDS2_SUPPORTED;
1178 		phy_qcfg_resp.valid = 1;
1179 
1180 		if (vf->flags & BNXT_VF_LINK_UP) {
1181 			/* if physical link is down, force link up on VF */
1182 			if (phy_qcfg_resp.link !=
1183 			    PORT_PHY_QCFG_RESP_LINK_LINK) {
1184 				phy_qcfg_resp.link =
1185 					PORT_PHY_QCFG_RESP_LINK_LINK;
1186 				phy_qcfg_resp.link_speed = cpu_to_le16(
1187 					PORT_PHY_QCFG_RESP_LINK_SPEED_10GB);
1188 				phy_qcfg_resp.duplex_cfg =
1189 					PORT_PHY_QCFG_RESP_DUPLEX_CFG_FULL;
1190 				phy_qcfg_resp.duplex_state =
1191 					PORT_PHY_QCFG_RESP_DUPLEX_STATE_FULL;
1192 				phy_qcfg_resp.pause =
1193 					(PORT_PHY_QCFG_RESP_PAUSE_TX |
1194 					 PORT_PHY_QCFG_RESP_PAUSE_RX);
1195 			}
1196 		} else {
1197 			/* force link down */
1198 			phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK;
1199 			phy_qcfg_resp.link_speed = 0;
1200 			phy_qcfg_resp.duplex_state =
1201 				PORT_PHY_QCFG_RESP_DUPLEX_STATE_HALF;
1202 			phy_qcfg_resp.pause = 0;
1203 		}
1204 		rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp,
1205 					phy_qcfg_req->resp_addr,
1206 					phy_qcfg_req->cmpl_ring,
1207 					sizeof(phy_qcfg_resp));
1208 	}
1209 	return rc;
1210 }
1211 
1212 static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf)
1213 {
1214 	int rc = 0;
1215 	struct input *encap_req = vf->hwrm_cmd_req_addr;
1216 	u32 req_type = le16_to_cpu(encap_req->req_type);
1217 
1218 	switch (req_type) {
1219 	case HWRM_FUNC_VF_CFG:
1220 		rc = bnxt_vf_configure_mac(bp, vf);
1221 		break;
1222 	case HWRM_CFA_L2_FILTER_ALLOC:
1223 		rc = bnxt_vf_validate_set_mac(bp, vf);
1224 		break;
1225 	case HWRM_FUNC_CFG:
1226 		/* TODO Validate if VF is allowed to change mac address,
1227 		 * mtu, num of rings etc
1228 		 */
1229 		rc = bnxt_hwrm_exec_fwd_resp(
1230 			bp, vf, sizeof(struct hwrm_func_cfg_input));
1231 		break;
1232 	case HWRM_PORT_PHY_QCFG:
1233 		rc = bnxt_vf_set_link(bp, vf);
1234 		break;
1235 	default:
1236 		break;
1237 	}
1238 	return rc;
1239 }
1240 
1241 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
1242 {
1243 	u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id;
1244 
1245 	/* Scan through VF's and process commands */
1246 	while (1) {
1247 		vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i);
1248 		if (vf_id >= active_vfs)
1249 			break;
1250 
1251 		clear_bit(vf_id, bp->pf.vf_event_bmap);
1252 		bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]);
1253 		i = vf_id + 1;
1254 	}
1255 }
1256 
1257 int bnxt_approve_mac(struct bnxt *bp, const u8 *mac, bool strict)
1258 {
1259 	struct hwrm_func_vf_cfg_input *req;
1260 	int rc = 0;
1261 
1262 	if (!BNXT_VF(bp))
1263 		return 0;
1264 
1265 	if (bp->hwrm_spec_code < 0x10202) {
1266 		if (is_valid_ether_addr(bp->vf.mac_addr))
1267 			rc = -EADDRNOTAVAIL;
1268 		goto mac_done;
1269 	}
1270 
1271 	rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
1272 	if (rc)
1273 		goto mac_done;
1274 
1275 	req->enables = cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
1276 	memcpy(req->dflt_mac_addr, mac, ETH_ALEN);
1277 	if (!strict)
1278 		hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT);
1279 	rc = hwrm_req_send(bp, req);
1280 mac_done:
1281 	if (rc && strict) {
1282 		rc = -EADDRNOTAVAIL;
1283 		netdev_warn(bp->dev, "VF MAC address %pM not approved by the PF\n",
1284 			    mac);
1285 		return rc;
1286 	}
1287 	return 0;
1288 }
1289 
1290 void bnxt_update_vf_mac(struct bnxt *bp)
1291 {
1292 	struct hwrm_func_qcaps_output *resp;
1293 	struct hwrm_func_qcaps_input *req;
1294 	bool inform_pf = false;
1295 
1296 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCAPS))
1297 		return;
1298 
1299 	req->fid = cpu_to_le16(0xffff);
1300 
1301 	resp = hwrm_req_hold(bp, req);
1302 	if (hwrm_req_send(bp, req))
1303 		goto update_vf_mac_exit;
1304 
1305 	/* Store MAC address from the firmware.  There are 2 cases:
1306 	 * 1. MAC address is valid.  It is assigned from the PF and we
1307 	 *    need to override the current VF MAC address with it.
1308 	 * 2. MAC address is zero.  The VF will use a random MAC address by
1309 	 *    default but the stored zero MAC will allow the VF user to change
1310 	 *    the random MAC address using ndo_set_mac_address() if he wants.
1311 	 */
1312 	if (!ether_addr_equal(resp->mac_address, bp->vf.mac_addr)) {
1313 		memcpy(bp->vf.mac_addr, resp->mac_address, ETH_ALEN);
1314 		/* This means we are now using our own MAC address, let
1315 		 * the PF know about this MAC address.
1316 		 */
1317 		if (!is_valid_ether_addr(bp->vf.mac_addr))
1318 			inform_pf = true;
1319 	}
1320 
1321 	/* overwrite netdev dev_addr with admin VF MAC */
1322 	if (is_valid_ether_addr(bp->vf.mac_addr))
1323 		eth_hw_addr_set(bp->dev, bp->vf.mac_addr);
1324 update_vf_mac_exit:
1325 	hwrm_req_drop(bp, req);
1326 	if (inform_pf)
1327 		bnxt_approve_mac(bp, bp->dev->dev_addr, false);
1328 }
1329 
1330 #else
1331 
1332 int bnxt_cfg_hw_sriov(struct bnxt *bp, int *num_vfs, bool reset)
1333 {
1334 	if (*num_vfs)
1335 		return -EOPNOTSUPP;
1336 	return 0;
1337 }
1338 
1339 void __bnxt_sriov_disable(struct bnxt *bp)
1340 {
1341 }
1342 
1343 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
1344 {
1345 	netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n");
1346 }
1347 
1348 void bnxt_update_vf_mac(struct bnxt *bp)
1349 {
1350 }
1351 
1352 int bnxt_approve_mac(struct bnxt *bp, const u8 *mac, bool strict)
1353 {
1354 	return 0;
1355 }
1356 #endif
1357