1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation.
8  */
9 
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/netdevice.h>
13 #include <linux/if_vlan.h>
14 #include <linux/interrupt.h>
15 #include <linux/etherdevice.h>
16 #include "bnxt_hsi.h"
17 #include "bnxt.h"
18 #include "bnxt_ulp.h"
19 #include "bnxt_sriov.h"
20 #include "bnxt_ethtool.h"
21 
22 #ifdef CONFIG_BNXT_SRIOV
23 static int bnxt_hwrm_fwd_async_event_cmpl(struct bnxt *bp,
24 					  struct bnxt_vf_info *vf, u16 event_id)
25 {
26 	struct hwrm_fwd_async_event_cmpl_output *resp = bp->hwrm_cmd_resp_addr;
27 	struct hwrm_fwd_async_event_cmpl_input req = {0};
28 	struct hwrm_async_event_cmpl *async_cmpl;
29 	int rc = 0;
30 
31 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_ASYNC_EVENT_CMPL, -1, -1);
32 	if (vf)
33 		req.encap_async_event_target_id = cpu_to_le16(vf->fw_fid);
34 	else
35 		/* broadcast this async event to all VFs */
36 		req.encap_async_event_target_id = cpu_to_le16(0xffff);
37 	async_cmpl = (struct hwrm_async_event_cmpl *)req.encap_async_event_cmpl;
38 	async_cmpl->type = cpu_to_le16(ASYNC_EVENT_CMPL_TYPE_HWRM_ASYNC_EVENT);
39 	async_cmpl->event_id = cpu_to_le16(event_id);
40 
41 	mutex_lock(&bp->hwrm_cmd_lock);
42 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
43 
44 	if (rc) {
45 		netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl failed. rc:%d\n",
46 			   rc);
47 		goto fwd_async_event_cmpl_exit;
48 	}
49 
50 	if (resp->error_code) {
51 		netdev_err(bp->dev, "hwrm_fwd_async_event_cmpl error %d\n",
52 			   resp->error_code);
53 		rc = -1;
54 	}
55 
56 fwd_async_event_cmpl_exit:
57 	mutex_unlock(&bp->hwrm_cmd_lock);
58 	return rc;
59 }
60 
61 static int bnxt_vf_ndo_prep(struct bnxt *bp, int vf_id)
62 {
63 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
64 		netdev_err(bp->dev, "vf ndo called though PF is down\n");
65 		return -EINVAL;
66 	}
67 	if (!bp->pf.active_vfs) {
68 		netdev_err(bp->dev, "vf ndo called though sriov is disabled\n");
69 		return -EINVAL;
70 	}
71 	if (vf_id >= bp->pf.max_vfs) {
72 		netdev_err(bp->dev, "Invalid VF id %d\n", vf_id);
73 		return -EINVAL;
74 	}
75 	return 0;
76 }
77 
78 int bnxt_set_vf_spoofchk(struct net_device *dev, int vf_id, bool setting)
79 {
80 	struct hwrm_func_cfg_input req = {0};
81 	struct bnxt *bp = netdev_priv(dev);
82 	struct bnxt_vf_info *vf;
83 	bool old_setting = false;
84 	u32 func_flags;
85 	int rc;
86 
87 	rc = bnxt_vf_ndo_prep(bp, vf_id);
88 	if (rc)
89 		return rc;
90 
91 	vf = &bp->pf.vf[vf_id];
92 	if (vf->flags & BNXT_VF_SPOOFCHK)
93 		old_setting = true;
94 	if (old_setting == setting)
95 		return 0;
96 
97 	func_flags = vf->func_flags;
98 	if (setting)
99 		func_flags |= FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK;
100 	else
101 		func_flags &= ~FUNC_CFG_REQ_FLAGS_SRC_MAC_ADDR_CHECK;
102 	/*TODO: if the driver supports VLAN filter on guest VLAN,
103 	 * the spoof check should also include vlan anti-spoofing
104 	 */
105 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
106 	req.fid = cpu_to_le16(vf->fw_fid);
107 	req.flags = cpu_to_le32(func_flags);
108 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
109 	if (!rc) {
110 		vf->func_flags = func_flags;
111 		if (setting)
112 			vf->flags |= BNXT_VF_SPOOFCHK;
113 		else
114 			vf->flags &= ~BNXT_VF_SPOOFCHK;
115 	}
116 	return rc;
117 }
118 
119 int bnxt_get_vf_config(struct net_device *dev, int vf_id,
120 		       struct ifla_vf_info *ivi)
121 {
122 	struct bnxt *bp = netdev_priv(dev);
123 	struct bnxt_vf_info *vf;
124 	int rc;
125 
126 	rc = bnxt_vf_ndo_prep(bp, vf_id);
127 	if (rc)
128 		return rc;
129 
130 	ivi->vf = vf_id;
131 	vf = &bp->pf.vf[vf_id];
132 
133 	memcpy(&ivi->mac, vf->mac_addr, ETH_ALEN);
134 	ivi->max_tx_rate = vf->max_tx_rate;
135 	ivi->min_tx_rate = vf->min_tx_rate;
136 	ivi->vlan = vf->vlan;
137 	ivi->qos = vf->flags & BNXT_VF_QOS;
138 	ivi->spoofchk = vf->flags & BNXT_VF_SPOOFCHK;
139 	if (!(vf->flags & BNXT_VF_LINK_FORCED))
140 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
141 	else if (vf->flags & BNXT_VF_LINK_UP)
142 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
143 	else
144 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
145 
146 	return 0;
147 }
148 
149 int bnxt_set_vf_mac(struct net_device *dev, int vf_id, u8 *mac)
150 {
151 	struct hwrm_func_cfg_input req = {0};
152 	struct bnxt *bp = netdev_priv(dev);
153 	struct bnxt_vf_info *vf;
154 	int rc;
155 
156 	rc = bnxt_vf_ndo_prep(bp, vf_id);
157 	if (rc)
158 		return rc;
159 	/* reject bc or mc mac addr, zero mac addr means allow
160 	 * VF to use its own mac addr
161 	 */
162 	if (is_multicast_ether_addr(mac)) {
163 		netdev_err(dev, "Invalid VF ethernet address\n");
164 		return -EINVAL;
165 	}
166 	vf = &bp->pf.vf[vf_id];
167 
168 	memcpy(vf->mac_addr, mac, ETH_ALEN);
169 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
170 	req.fid = cpu_to_le16(vf->fw_fid);
171 	req.flags = cpu_to_le32(vf->func_flags);
172 	req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
173 	memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
174 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
175 }
176 
177 int bnxt_set_vf_vlan(struct net_device *dev, int vf_id, u16 vlan_id, u8 qos,
178 		     __be16 vlan_proto)
179 {
180 	struct hwrm_func_cfg_input req = {0};
181 	struct bnxt *bp = netdev_priv(dev);
182 	struct bnxt_vf_info *vf;
183 	u16 vlan_tag;
184 	int rc;
185 
186 	if (bp->hwrm_spec_code < 0x10201)
187 		return -ENOTSUPP;
188 
189 	if (vlan_proto != htons(ETH_P_8021Q))
190 		return -EPROTONOSUPPORT;
191 
192 	rc = bnxt_vf_ndo_prep(bp, vf_id);
193 	if (rc)
194 		return rc;
195 
196 	/* TODO: needed to implement proper handling of user priority,
197 	 * currently fail the command if there is valid priority
198 	 */
199 	if (vlan_id > 4095 || qos)
200 		return -EINVAL;
201 
202 	vf = &bp->pf.vf[vf_id];
203 	vlan_tag = vlan_id;
204 	if (vlan_tag == vf->vlan)
205 		return 0;
206 
207 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
208 	req.fid = cpu_to_le16(vf->fw_fid);
209 	req.flags = cpu_to_le32(vf->func_flags);
210 	req.dflt_vlan = cpu_to_le16(vlan_tag);
211 	req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_DFLT_VLAN);
212 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
213 	if (!rc)
214 		vf->vlan = vlan_tag;
215 	return rc;
216 }
217 
218 int bnxt_set_vf_bw(struct net_device *dev, int vf_id, int min_tx_rate,
219 		   int max_tx_rate)
220 {
221 	struct hwrm_func_cfg_input req = {0};
222 	struct bnxt *bp = netdev_priv(dev);
223 	struct bnxt_vf_info *vf;
224 	u32 pf_link_speed;
225 	int rc;
226 
227 	rc = bnxt_vf_ndo_prep(bp, vf_id);
228 	if (rc)
229 		return rc;
230 
231 	vf = &bp->pf.vf[vf_id];
232 	pf_link_speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
233 	if (max_tx_rate > pf_link_speed) {
234 		netdev_info(bp->dev, "max tx rate %d exceed PF link speed for VF %d\n",
235 			    max_tx_rate, vf_id);
236 		return -EINVAL;
237 	}
238 
239 	if (min_tx_rate > pf_link_speed || min_tx_rate > max_tx_rate) {
240 		netdev_info(bp->dev, "min tx rate %d is invalid for VF %d\n",
241 			    min_tx_rate, vf_id);
242 		return -EINVAL;
243 	}
244 	if (min_tx_rate == vf->min_tx_rate && max_tx_rate == vf->max_tx_rate)
245 		return 0;
246 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
247 	req.fid = cpu_to_le16(vf->fw_fid);
248 	req.flags = cpu_to_le32(vf->func_flags);
249 	req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MAX_BW);
250 	req.max_bw = cpu_to_le32(max_tx_rate);
251 	req.enables |= cpu_to_le32(FUNC_CFG_REQ_ENABLES_MIN_BW);
252 	req.min_bw = cpu_to_le32(min_tx_rate);
253 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
254 	if (!rc) {
255 		vf->min_tx_rate = min_tx_rate;
256 		vf->max_tx_rate = max_tx_rate;
257 	}
258 	return rc;
259 }
260 
261 int bnxt_set_vf_link_state(struct net_device *dev, int vf_id, int link)
262 {
263 	struct bnxt *bp = netdev_priv(dev);
264 	struct bnxt_vf_info *vf;
265 	int rc;
266 
267 	rc = bnxt_vf_ndo_prep(bp, vf_id);
268 	if (rc)
269 		return rc;
270 
271 	vf = &bp->pf.vf[vf_id];
272 
273 	vf->flags &= ~(BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED);
274 	switch (link) {
275 	case IFLA_VF_LINK_STATE_AUTO:
276 		vf->flags |= BNXT_VF_LINK_UP;
277 		break;
278 	case IFLA_VF_LINK_STATE_DISABLE:
279 		vf->flags |= BNXT_VF_LINK_FORCED;
280 		break;
281 	case IFLA_VF_LINK_STATE_ENABLE:
282 		vf->flags |= BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED;
283 		break;
284 	default:
285 		netdev_err(bp->dev, "Invalid link option\n");
286 		rc = -EINVAL;
287 		break;
288 	}
289 	if (vf->flags & (BNXT_VF_LINK_UP | BNXT_VF_LINK_FORCED))
290 		rc = bnxt_hwrm_fwd_async_event_cmpl(bp, vf,
291 			ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE);
292 	return rc;
293 }
294 
295 static int bnxt_set_vf_attr(struct bnxt *bp, int num_vfs)
296 {
297 	int i;
298 	struct bnxt_vf_info *vf;
299 
300 	for (i = 0; i < num_vfs; i++) {
301 		vf = &bp->pf.vf[i];
302 		memset(vf, 0, sizeof(*vf));
303 		vf->flags = BNXT_VF_QOS | BNXT_VF_LINK_UP;
304 	}
305 	return 0;
306 }
307 
308 static int bnxt_hwrm_func_vf_resource_free(struct bnxt *bp, int num_vfs)
309 {
310 	int i, rc = 0;
311 	struct bnxt_pf_info *pf = &bp->pf;
312 	struct hwrm_func_vf_resc_free_input req = {0};
313 
314 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_RESC_FREE, -1, -1);
315 
316 	mutex_lock(&bp->hwrm_cmd_lock);
317 	for (i = pf->first_vf_id; i < pf->first_vf_id + num_vfs; i++) {
318 		req.vf_id = cpu_to_le16(i);
319 		rc = _hwrm_send_message(bp, &req, sizeof(req),
320 					HWRM_CMD_TIMEOUT);
321 		if (rc)
322 			break;
323 	}
324 	mutex_unlock(&bp->hwrm_cmd_lock);
325 	return rc;
326 }
327 
328 static void bnxt_free_vf_resources(struct bnxt *bp)
329 {
330 	struct pci_dev *pdev = bp->pdev;
331 	int i;
332 
333 	kfree(bp->pf.vf_event_bmap);
334 	bp->pf.vf_event_bmap = NULL;
335 
336 	for (i = 0; i < 4; i++) {
337 		if (bp->pf.hwrm_cmd_req_addr[i]) {
338 			dma_free_coherent(&pdev->dev, BNXT_PAGE_SIZE,
339 					  bp->pf.hwrm_cmd_req_addr[i],
340 					  bp->pf.hwrm_cmd_req_dma_addr[i]);
341 			bp->pf.hwrm_cmd_req_addr[i] = NULL;
342 		}
343 	}
344 
345 	kfree(bp->pf.vf);
346 	bp->pf.vf = NULL;
347 }
348 
349 static int bnxt_alloc_vf_resources(struct bnxt *bp, int num_vfs)
350 {
351 	struct pci_dev *pdev = bp->pdev;
352 	u32 nr_pages, size, i, j, k = 0;
353 
354 	bp->pf.vf = kcalloc(num_vfs, sizeof(struct bnxt_vf_info), GFP_KERNEL);
355 	if (!bp->pf.vf)
356 		return -ENOMEM;
357 
358 	bnxt_set_vf_attr(bp, num_vfs);
359 
360 	size = num_vfs * BNXT_HWRM_REQ_MAX_SIZE;
361 	nr_pages = size / BNXT_PAGE_SIZE;
362 	if (size & (BNXT_PAGE_SIZE - 1))
363 		nr_pages++;
364 
365 	for (i = 0; i < nr_pages; i++) {
366 		bp->pf.hwrm_cmd_req_addr[i] =
367 			dma_alloc_coherent(&pdev->dev, BNXT_PAGE_SIZE,
368 					   &bp->pf.hwrm_cmd_req_dma_addr[i],
369 					   GFP_KERNEL);
370 
371 		if (!bp->pf.hwrm_cmd_req_addr[i])
372 			return -ENOMEM;
373 
374 		for (j = 0; j < BNXT_HWRM_REQS_PER_PAGE && k < num_vfs; j++) {
375 			struct bnxt_vf_info *vf = &bp->pf.vf[k];
376 
377 			vf->hwrm_cmd_req_addr = bp->pf.hwrm_cmd_req_addr[i] +
378 						j * BNXT_HWRM_REQ_MAX_SIZE;
379 			vf->hwrm_cmd_req_dma_addr =
380 				bp->pf.hwrm_cmd_req_dma_addr[i] + j *
381 				BNXT_HWRM_REQ_MAX_SIZE;
382 			k++;
383 		}
384 	}
385 
386 	/* Max 128 VF's */
387 	bp->pf.vf_event_bmap = kzalloc(16, GFP_KERNEL);
388 	if (!bp->pf.vf_event_bmap)
389 		return -ENOMEM;
390 
391 	bp->pf.hwrm_cmd_req_pages = nr_pages;
392 	return 0;
393 }
394 
395 static int bnxt_hwrm_func_buf_rgtr(struct bnxt *bp)
396 {
397 	struct hwrm_func_buf_rgtr_input req = {0};
398 
399 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_BUF_RGTR, -1, -1);
400 
401 	req.req_buf_num_pages = cpu_to_le16(bp->pf.hwrm_cmd_req_pages);
402 	req.req_buf_page_size = cpu_to_le16(BNXT_PAGE_SHIFT);
403 	req.req_buf_len = cpu_to_le16(BNXT_HWRM_REQ_MAX_SIZE);
404 	req.req_buf_page_addr0 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[0]);
405 	req.req_buf_page_addr1 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[1]);
406 	req.req_buf_page_addr2 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[2]);
407 	req.req_buf_page_addr3 = cpu_to_le64(bp->pf.hwrm_cmd_req_dma_addr[3]);
408 
409 	return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
410 }
411 
412 /* only call by PF to reserve resources for VF */
413 static int bnxt_hwrm_func_cfg(struct bnxt *bp, int num_vfs)
414 {
415 	u32 rc = 0, mtu, i;
416 	u16 vf_tx_rings, vf_rx_rings, vf_cp_rings, vf_stat_ctx, vf_vnics;
417 	u16 vf_ring_grps;
418 	struct hwrm_func_cfg_input req = {0};
419 	struct bnxt_pf_info *pf = &bp->pf;
420 	int total_vf_tx_rings = 0;
421 
422 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
423 
424 	/* Remaining rings are distributed equally amongs VF's for now */
425 	vf_cp_rings = (pf->max_cp_rings - bp->cp_nr_rings) / num_vfs;
426 	vf_stat_ctx = (pf->max_stat_ctxs - bp->num_stat_ctxs) / num_vfs;
427 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
428 		vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings * 2) /
429 			      num_vfs;
430 	else
431 		vf_rx_rings = (pf->max_rx_rings - bp->rx_nr_rings) / num_vfs;
432 	vf_ring_grps = (bp->pf.max_hw_ring_grps - bp->rx_nr_rings) / num_vfs;
433 	vf_tx_rings = (pf->max_tx_rings - bp->tx_nr_rings) / num_vfs;
434 	vf_vnics = (pf->max_vnics - bp->nr_vnics) / num_vfs;
435 	vf_vnics = min_t(u16, vf_vnics, vf_rx_rings);
436 
437 	req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_MTU |
438 				  FUNC_CFG_REQ_ENABLES_MRU |
439 				  FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
440 				  FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS |
441 				  FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
442 				  FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS |
443 				  FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS |
444 				  FUNC_CFG_REQ_ENABLES_NUM_L2_CTXS |
445 				  FUNC_CFG_REQ_ENABLES_NUM_VNICS |
446 				  FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS);
447 
448 	mtu = bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
449 	req.mru = cpu_to_le16(mtu);
450 	req.mtu = cpu_to_le16(mtu);
451 
452 	req.num_rsscos_ctxs = cpu_to_le16(1);
453 	req.num_cmpl_rings = cpu_to_le16(vf_cp_rings);
454 	req.num_tx_rings = cpu_to_le16(vf_tx_rings);
455 	req.num_rx_rings = cpu_to_le16(vf_rx_rings);
456 	req.num_hw_ring_grps = cpu_to_le16(vf_ring_grps);
457 	req.num_l2_ctxs = cpu_to_le16(4);
458 
459 	req.num_vnics = cpu_to_le16(vf_vnics);
460 	/* FIXME spec currently uses 1 bit for stats ctx */
461 	req.num_stat_ctxs = cpu_to_le16(vf_stat_ctx);
462 
463 	mutex_lock(&bp->hwrm_cmd_lock);
464 	for (i = 0; i < num_vfs; i++) {
465 		int vf_tx_rsvd = vf_tx_rings;
466 
467 		req.fid = cpu_to_le16(pf->first_vf_id + i);
468 		rc = _hwrm_send_message(bp, &req, sizeof(req),
469 					HWRM_CMD_TIMEOUT);
470 		if (rc)
471 			break;
472 		pf->active_vfs = i + 1;
473 		pf->vf[i].fw_fid = le16_to_cpu(req.fid);
474 		rc = __bnxt_hwrm_get_tx_rings(bp, pf->vf[i].fw_fid,
475 					      &vf_tx_rsvd);
476 		if (rc)
477 			break;
478 		total_vf_tx_rings += vf_tx_rsvd;
479 	}
480 	mutex_unlock(&bp->hwrm_cmd_lock);
481 	if (!rc) {
482 		pf->max_tx_rings -= total_vf_tx_rings;
483 		pf->max_rx_rings -= vf_rx_rings * num_vfs;
484 		pf->max_hw_ring_grps -= vf_ring_grps * num_vfs;
485 		pf->max_cp_rings -= vf_cp_rings * num_vfs;
486 		pf->max_rsscos_ctxs -= num_vfs;
487 		pf->max_stat_ctxs -= vf_stat_ctx * num_vfs;
488 		pf->max_vnics -= vf_vnics * num_vfs;
489 	}
490 	return rc;
491 }
492 
493 static int bnxt_sriov_enable(struct bnxt *bp, int *num_vfs)
494 {
495 	int rc = 0, vfs_supported;
496 	int min_rx_rings, min_tx_rings, min_rss_ctxs;
497 	int tx_ok = 0, rx_ok = 0, rss_ok = 0;
498 
499 	/* Check if we can enable requested num of vf's. At a mininum
500 	 * we require 1 RX 1 TX rings for each VF. In this minimum conf
501 	 * features like TPA will not be available.
502 	 */
503 	vfs_supported = *num_vfs;
504 
505 	while (vfs_supported) {
506 		min_rx_rings = vfs_supported;
507 		min_tx_rings = vfs_supported;
508 		min_rss_ctxs = vfs_supported;
509 
510 		if (bp->flags & BNXT_FLAG_AGG_RINGS) {
511 			if (bp->pf.max_rx_rings - bp->rx_nr_rings * 2 >=
512 			    min_rx_rings)
513 				rx_ok = 1;
514 		} else {
515 			if (bp->pf.max_rx_rings - bp->rx_nr_rings >=
516 			    min_rx_rings)
517 				rx_ok = 1;
518 		}
519 		if (bp->pf.max_vnics - bp->nr_vnics < min_rx_rings)
520 			rx_ok = 0;
521 
522 		if (bp->pf.max_tx_rings - bp->tx_nr_rings >= min_tx_rings)
523 			tx_ok = 1;
524 
525 		if (bp->pf.max_rsscos_ctxs - bp->rsscos_nr_ctxs >= min_rss_ctxs)
526 			rss_ok = 1;
527 
528 		if (tx_ok && rx_ok && rss_ok)
529 			break;
530 
531 		vfs_supported--;
532 	}
533 
534 	if (!vfs_supported) {
535 		netdev_err(bp->dev, "Cannot enable VF's as all resources are used by PF\n");
536 		return -EINVAL;
537 	}
538 
539 	if (vfs_supported != *num_vfs) {
540 		netdev_info(bp->dev, "Requested VFs %d, can enable %d\n",
541 			    *num_vfs, vfs_supported);
542 		*num_vfs = vfs_supported;
543 	}
544 
545 	rc = bnxt_alloc_vf_resources(bp, *num_vfs);
546 	if (rc)
547 		goto err_out1;
548 
549 	/* Reserve resources for VFs */
550 	rc = bnxt_hwrm_func_cfg(bp, *num_vfs);
551 	if (rc)
552 		goto err_out2;
553 
554 	/* Register buffers for VFs */
555 	rc = bnxt_hwrm_func_buf_rgtr(bp);
556 	if (rc)
557 		goto err_out2;
558 
559 	bnxt_ulp_sriov_cfg(bp, *num_vfs);
560 
561 	rc = pci_enable_sriov(bp->pdev, *num_vfs);
562 	if (rc)
563 		goto err_out2;
564 
565 	return 0;
566 
567 err_out2:
568 	/* Free the resources reserved for various VF's */
569 	bnxt_hwrm_func_vf_resource_free(bp, *num_vfs);
570 
571 err_out1:
572 	bnxt_free_vf_resources(bp);
573 
574 	return rc;
575 }
576 
577 void bnxt_sriov_disable(struct bnxt *bp)
578 {
579 	u16 num_vfs = pci_num_vf(bp->pdev);
580 
581 	if (!num_vfs)
582 		return;
583 
584 	if (pci_vfs_assigned(bp->pdev)) {
585 		bnxt_hwrm_fwd_async_event_cmpl(
586 			bp, NULL, ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD);
587 		netdev_warn(bp->dev, "Unable to free %d VFs because some are assigned to VMs.\n",
588 			    num_vfs);
589 	} else {
590 		pci_disable_sriov(bp->pdev);
591 		/* Free the HW resources reserved for various VF's */
592 		bnxt_hwrm_func_vf_resource_free(bp, num_vfs);
593 	}
594 
595 	bnxt_free_vf_resources(bp);
596 
597 	bp->pf.active_vfs = 0;
598 	/* Reclaim all resources for the PF. */
599 	rtnl_lock();
600 	bnxt_restore_pf_fw_resources(bp);
601 	rtnl_unlock();
602 
603 	bnxt_ulp_sriov_cfg(bp, 0);
604 }
605 
606 int bnxt_sriov_configure(struct pci_dev *pdev, int num_vfs)
607 {
608 	struct net_device *dev = pci_get_drvdata(pdev);
609 	struct bnxt *bp = netdev_priv(dev);
610 
611 	if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
612 		netdev_warn(dev, "Not allow SRIOV if the irq mode is not MSIX\n");
613 		return 0;
614 	}
615 
616 	rtnl_lock();
617 	if (!netif_running(dev)) {
618 		netdev_warn(dev, "Reject SRIOV config request since if is down!\n");
619 		rtnl_unlock();
620 		return 0;
621 	}
622 	bp->sriov_cfg = true;
623 	rtnl_unlock();
624 
625 	if (pci_vfs_assigned(bp->pdev)) {
626 		netdev_warn(dev, "Unable to configure SRIOV since some VFs are assigned to VMs.\n");
627 		num_vfs = 0;
628 		goto sriov_cfg_exit;
629 	}
630 
631 	/* Check if enabled VFs is same as requested */
632 	if (num_vfs && num_vfs == bp->pf.active_vfs)
633 		goto sriov_cfg_exit;
634 
635 	/* if there are previous existing VFs, clean them up */
636 	bnxt_sriov_disable(bp);
637 	if (!num_vfs)
638 		goto sriov_cfg_exit;
639 
640 	bnxt_sriov_enable(bp, &num_vfs);
641 
642 sriov_cfg_exit:
643 	bp->sriov_cfg = false;
644 	wake_up(&bp->sriov_cfg_wait);
645 
646 	return num_vfs;
647 }
648 
649 static int bnxt_hwrm_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
650 			      void *encap_resp, __le64 encap_resp_addr,
651 			      __le16 encap_resp_cpr, u32 msg_size)
652 {
653 	int rc = 0;
654 	struct hwrm_fwd_resp_input req = {0};
655 	struct hwrm_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
656 
657 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FWD_RESP, -1, -1);
658 
659 	/* Set the new target id */
660 	req.target_id = cpu_to_le16(vf->fw_fid);
661 	req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
662 	req.encap_resp_len = cpu_to_le16(msg_size);
663 	req.encap_resp_addr = encap_resp_addr;
664 	req.encap_resp_cmpl_ring = encap_resp_cpr;
665 	memcpy(req.encap_resp, encap_resp, msg_size);
666 
667 	mutex_lock(&bp->hwrm_cmd_lock);
668 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
669 
670 	if (rc) {
671 		netdev_err(bp->dev, "hwrm_fwd_resp failed. rc:%d\n", rc);
672 		goto fwd_resp_exit;
673 	}
674 
675 	if (resp->error_code) {
676 		netdev_err(bp->dev, "hwrm_fwd_resp error %d\n",
677 			   resp->error_code);
678 		rc = -1;
679 	}
680 
681 fwd_resp_exit:
682 	mutex_unlock(&bp->hwrm_cmd_lock);
683 	return rc;
684 }
685 
686 static int bnxt_hwrm_fwd_err_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
687 				  u32 msg_size)
688 {
689 	int rc = 0;
690 	struct hwrm_reject_fwd_resp_input req = {0};
691 	struct hwrm_reject_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
692 
693 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_REJECT_FWD_RESP, -1, -1);
694 	/* Set the new target id */
695 	req.target_id = cpu_to_le16(vf->fw_fid);
696 	req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
697 	memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
698 
699 	mutex_lock(&bp->hwrm_cmd_lock);
700 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
701 
702 	if (rc) {
703 		netdev_err(bp->dev, "hwrm_fwd_err_resp failed. rc:%d\n", rc);
704 		goto fwd_err_resp_exit;
705 	}
706 
707 	if (resp->error_code) {
708 		netdev_err(bp->dev, "hwrm_fwd_err_resp error %d\n",
709 			   resp->error_code);
710 		rc = -1;
711 	}
712 
713 fwd_err_resp_exit:
714 	mutex_unlock(&bp->hwrm_cmd_lock);
715 	return rc;
716 }
717 
718 static int bnxt_hwrm_exec_fwd_resp(struct bnxt *bp, struct bnxt_vf_info *vf,
719 				   u32 msg_size)
720 {
721 	int rc = 0;
722 	struct hwrm_exec_fwd_resp_input req = {0};
723 	struct hwrm_exec_fwd_resp_output *resp = bp->hwrm_cmd_resp_addr;
724 
725 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_EXEC_FWD_RESP, -1, -1);
726 	/* Set the new target id */
727 	req.target_id = cpu_to_le16(vf->fw_fid);
728 	req.encap_resp_target_id = cpu_to_le16(vf->fw_fid);
729 	memcpy(req.encap_request, vf->hwrm_cmd_req_addr, msg_size);
730 
731 	mutex_lock(&bp->hwrm_cmd_lock);
732 	rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
733 
734 	if (rc) {
735 		netdev_err(bp->dev, "hwrm_exec_fw_resp failed. rc:%d\n", rc);
736 		goto exec_fwd_resp_exit;
737 	}
738 
739 	if (resp->error_code) {
740 		netdev_err(bp->dev, "hwrm_exec_fw_resp error %d\n",
741 			   resp->error_code);
742 		rc = -1;
743 	}
744 
745 exec_fwd_resp_exit:
746 	mutex_unlock(&bp->hwrm_cmd_lock);
747 	return rc;
748 }
749 
750 static int bnxt_vf_validate_set_mac(struct bnxt *bp, struct bnxt_vf_info *vf)
751 {
752 	u32 msg_size = sizeof(struct hwrm_cfa_l2_filter_alloc_input);
753 	struct hwrm_cfa_l2_filter_alloc_input *req =
754 		(struct hwrm_cfa_l2_filter_alloc_input *)vf->hwrm_cmd_req_addr;
755 
756 	if (!is_valid_ether_addr(vf->mac_addr) ||
757 	    ether_addr_equal((const u8 *)req->l2_addr, vf->mac_addr))
758 		return bnxt_hwrm_exec_fwd_resp(bp, vf, msg_size);
759 	else
760 		return bnxt_hwrm_fwd_err_resp(bp, vf, msg_size);
761 }
762 
763 static int bnxt_vf_set_link(struct bnxt *bp, struct bnxt_vf_info *vf)
764 {
765 	int rc = 0;
766 
767 	if (!(vf->flags & BNXT_VF_LINK_FORCED)) {
768 		/* real link */
769 		rc = bnxt_hwrm_exec_fwd_resp(
770 			bp, vf, sizeof(struct hwrm_port_phy_qcfg_input));
771 	} else {
772 		struct hwrm_port_phy_qcfg_output phy_qcfg_resp;
773 		struct hwrm_port_phy_qcfg_input *phy_qcfg_req;
774 
775 		phy_qcfg_req =
776 		(struct hwrm_port_phy_qcfg_input *)vf->hwrm_cmd_req_addr;
777 		mutex_lock(&bp->hwrm_cmd_lock);
778 		memcpy(&phy_qcfg_resp, &bp->link_info.phy_qcfg_resp,
779 		       sizeof(phy_qcfg_resp));
780 		mutex_unlock(&bp->hwrm_cmd_lock);
781 		phy_qcfg_resp.seq_id = phy_qcfg_req->seq_id;
782 
783 		if (vf->flags & BNXT_VF_LINK_UP) {
784 			/* if physical link is down, force link up on VF */
785 			if (phy_qcfg_resp.link !=
786 			    PORT_PHY_QCFG_RESP_LINK_LINK) {
787 				phy_qcfg_resp.link =
788 					PORT_PHY_QCFG_RESP_LINK_LINK;
789 				phy_qcfg_resp.link_speed = cpu_to_le16(
790 					PORT_PHY_QCFG_RESP_LINK_SPEED_10GB);
791 				phy_qcfg_resp.duplex =
792 					PORT_PHY_QCFG_RESP_DUPLEX_FULL;
793 				phy_qcfg_resp.pause =
794 					(PORT_PHY_QCFG_RESP_PAUSE_TX |
795 					 PORT_PHY_QCFG_RESP_PAUSE_RX);
796 			}
797 		} else {
798 			/* force link down */
799 			phy_qcfg_resp.link = PORT_PHY_QCFG_RESP_LINK_NO_LINK;
800 			phy_qcfg_resp.link_speed = 0;
801 			phy_qcfg_resp.duplex = PORT_PHY_QCFG_RESP_DUPLEX_HALF;
802 			phy_qcfg_resp.pause = 0;
803 		}
804 		rc = bnxt_hwrm_fwd_resp(bp, vf, &phy_qcfg_resp,
805 					phy_qcfg_req->resp_addr,
806 					phy_qcfg_req->cmpl_ring,
807 					sizeof(phy_qcfg_resp));
808 	}
809 	return rc;
810 }
811 
812 static int bnxt_vf_req_validate_snd(struct bnxt *bp, struct bnxt_vf_info *vf)
813 {
814 	int rc = 0;
815 	struct input *encap_req = vf->hwrm_cmd_req_addr;
816 	u32 req_type = le16_to_cpu(encap_req->req_type);
817 
818 	switch (req_type) {
819 	case HWRM_CFA_L2_FILTER_ALLOC:
820 		rc = bnxt_vf_validate_set_mac(bp, vf);
821 		break;
822 	case HWRM_FUNC_CFG:
823 		/* TODO Validate if VF is allowed to change mac address,
824 		 * mtu, num of rings etc
825 		 */
826 		rc = bnxt_hwrm_exec_fwd_resp(
827 			bp, vf, sizeof(struct hwrm_func_cfg_input));
828 		break;
829 	case HWRM_PORT_PHY_QCFG:
830 		rc = bnxt_vf_set_link(bp, vf);
831 		break;
832 	default:
833 		break;
834 	}
835 	return rc;
836 }
837 
838 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
839 {
840 	u32 i = 0, active_vfs = bp->pf.active_vfs, vf_id;
841 
842 	/* Scan through VF's and process commands */
843 	while (1) {
844 		vf_id = find_next_bit(bp->pf.vf_event_bmap, active_vfs, i);
845 		if (vf_id >= active_vfs)
846 			break;
847 
848 		clear_bit(vf_id, bp->pf.vf_event_bmap);
849 		bnxt_vf_req_validate_snd(bp, &bp->pf.vf[vf_id]);
850 		i = vf_id + 1;
851 	}
852 }
853 
854 void bnxt_update_vf_mac(struct bnxt *bp)
855 {
856 	struct hwrm_func_qcaps_input req = {0};
857 	struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
858 
859 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
860 	req.fid = cpu_to_le16(0xffff);
861 
862 	mutex_lock(&bp->hwrm_cmd_lock);
863 	if (_hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT))
864 		goto update_vf_mac_exit;
865 
866 	/* Store MAC address from the firmware.  There are 2 cases:
867 	 * 1. MAC address is valid.  It is assigned from the PF and we
868 	 *    need to override the current VF MAC address with it.
869 	 * 2. MAC address is zero.  The VF will use a random MAC address by
870 	 *    default but the stored zero MAC will allow the VF user to change
871 	 *    the random MAC address using ndo_set_mac_address() if he wants.
872 	 */
873 	if (!ether_addr_equal(resp->mac_address, bp->vf.mac_addr))
874 		memcpy(bp->vf.mac_addr, resp->mac_address, ETH_ALEN);
875 
876 	/* overwrite netdev dev_addr with admin VF MAC */
877 	if (is_valid_ether_addr(bp->vf.mac_addr))
878 		memcpy(bp->dev->dev_addr, bp->vf.mac_addr, ETH_ALEN);
879 update_vf_mac_exit:
880 	mutex_unlock(&bp->hwrm_cmd_lock);
881 }
882 
883 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)
884 {
885 	struct hwrm_func_vf_cfg_input req = {0};
886 	int rc = 0;
887 
888 	if (!BNXT_VF(bp))
889 		return 0;
890 
891 	if (bp->hwrm_spec_code < 0x10202) {
892 		if (is_valid_ether_addr(bp->vf.mac_addr))
893 			rc = -EADDRNOTAVAIL;
894 		goto mac_done;
895 	}
896 	bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
897 	req.enables = cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_DFLT_MAC_ADDR);
898 	memcpy(req.dflt_mac_addr, mac, ETH_ALEN);
899 	rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
900 mac_done:
901 	if (rc) {
902 		rc = -EADDRNOTAVAIL;
903 		netdev_warn(bp->dev, "VF MAC address %pM not approved by the PF\n",
904 			    mac);
905 	}
906 	return rc;
907 }
908 #else
909 
910 void bnxt_sriov_disable(struct bnxt *bp)
911 {
912 }
913 
914 void bnxt_hwrm_exec_fwd_req(struct bnxt *bp)
915 {
916 	netdev_err(bp->dev, "Invalid VF message received when SRIOV is not enable\n");
917 }
918 
919 void bnxt_update_vf_mac(struct bnxt *bp)
920 {
921 }
922 
923 int bnxt_approve_mac(struct bnxt *bp, u8 *mac)
924 {
925 	return 0;
926 }
927 #endif
928