i40e: add 20G speed for Tx bandwidth calculations
[deliverable/linux.git] / drivers / net / ethernet / intel / i40e / i40e_virtchnl_pf.c
CommitLineData
5c3c48ac
JB
1/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
51616018 4 * Copyright(c) 2013 - 2015 Intel Corporation.
5c3c48ac
JB
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
dc641b73
GR
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
5c3c48ac
JB
17 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27#include "i40e.h"
28
532b0455
MW
29/*********************notification routines***********************/
30
31/**
32 * i40e_vc_vf_broadcast
33 * @pf: pointer to the PF structure
34 * @opcode: operation code
35 * @retval: return value
36 * @msg: pointer to the msg buffer
37 * @msglen: msg length
38 *
39 * send a message to all VFs on a given PF
40 **/
41static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
42 enum i40e_virtchnl_ops v_opcode,
43 i40e_status v_retval, u8 *msg,
44 u16 msglen)
45{
46 struct i40e_hw *hw = &pf->hw;
47 struct i40e_vf *vf = pf->vf;
48 int i;
49
50 for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
51 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
52 /* Not all vfs are enabled so skip the ones that are not */
53 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
54 !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
55 continue;
56
57 /* Ignore return value on purpose - a given VF may fail, but
58 * we need to keep going and send to all of them
59 */
60 i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
61 msg, msglen, NULL);
62 }
63}
64
65/**
66 * i40e_vc_notify_link_state
67 * @vf: pointer to the VF structure
68 *
69 * send a link status message to a single VF
70 **/
71static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
72{
73 struct i40e_virtchnl_pf_event pfe;
74 struct i40e_pf *pf = vf->pf;
75 struct i40e_hw *hw = &pf->hw;
76 struct i40e_link_status *ls = &pf->hw.phy.link_info;
77 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
78
79 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
80 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
81 if (vf->link_forced) {
82 pfe.event_data.link_event.link_status = vf->link_up;
83 pfe.event_data.link_event.link_speed =
84 (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
85 } else {
86 pfe.event_data.link_event.link_status =
87 ls->link_info & I40E_AQ_LINK_UP;
88 pfe.event_data.link_event.link_speed = ls->link_speed;
89 }
90 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
91 0, (u8 *)&pfe, sizeof(pfe), NULL);
92}
93
94/**
95 * i40e_vc_notify_link_state
96 * @pf: pointer to the PF structure
97 *
98 * send a link status message to all VFs on a given PF
99 **/
100void i40e_vc_notify_link_state(struct i40e_pf *pf)
101{
102 int i;
103
104 for (i = 0; i < pf->num_alloc_vfs; i++)
105 i40e_vc_notify_vf_link_state(&pf->vf[i]);
106}
107
108/**
109 * i40e_vc_notify_reset
110 * @pf: pointer to the PF structure
111 *
112 * indicate a pending reset to all VFs on a given PF
113 **/
114void i40e_vc_notify_reset(struct i40e_pf *pf)
115{
116 struct i40e_virtchnl_pf_event pfe;
117
118 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
119 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
120 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, 0,
121 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
122}
123
124/**
125 * i40e_vc_notify_vf_reset
126 * @vf: pointer to the VF structure
127 *
128 * indicate a pending reset to the given VF
129 **/
130void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
131{
132 struct i40e_virtchnl_pf_event pfe;
133 int abs_vf_id;
134
135 /* validate the request */
136 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
137 return;
138
139 /* verify if the VF is in either init or active before proceeding */
140 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
141 !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
142 return;
143
144 abs_vf_id = vf->vf_id + vf->pf->hw.func_caps.vf_base_id;
145
146 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
147 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
148 i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
149 0, (u8 *)&pfe,
150 sizeof(struct i40e_virtchnl_pf_event), NULL);
151}
5c3c48ac
JB
152/***********************misc routines*****************************/
153
f9b4b627
GR
154/**
155 * i40e_vc_disable_vf
b40c82e6
JK
156 * @pf: pointer to the PF info
157 * @vf: pointer to the VF info
f9b4b627
GR
158 *
159 * Disable the VF through a SW reset
160 **/
161static inline void i40e_vc_disable_vf(struct i40e_pf *pf, struct i40e_vf *vf)
162{
54f455ee
MW
163 i40e_vc_notify_vf_reset(vf);
164 i40e_reset_vf(vf, false);
f9b4b627
GR
165}
166
5c3c48ac
JB
167/**
168 * i40e_vc_isvalid_vsi_id
b40c82e6
JK
169 * @vf: pointer to the VF info
170 * @vsi_id: VF relative VSI id
5c3c48ac 171 *
b40c82e6 172 * check for the valid VSI id
5c3c48ac 173 **/
fdf0e0bf 174static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
5c3c48ac
JB
175{
176 struct i40e_pf *pf = vf->pf;
fdf0e0bf 177 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
5c3c48ac 178
fdf0e0bf 179 return (vsi && (vsi->vf_id == vf->vf_id));
5c3c48ac
JB
180}
181
182/**
183 * i40e_vc_isvalid_queue_id
b40c82e6 184 * @vf: pointer to the VF info
5c3c48ac
JB
185 * @vsi_id: vsi id
186 * @qid: vsi relative queue id
187 *
188 * check for the valid queue id
189 **/
fdf0e0bf 190static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
5c3c48ac
JB
191 u8 qid)
192{
193 struct i40e_pf *pf = vf->pf;
fdf0e0bf 194 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
5c3c48ac 195
fdf0e0bf 196 return (vsi && (qid < vsi->alloc_queue_pairs));
5c3c48ac
JB
197}
198
199/**
200 * i40e_vc_isvalid_vector_id
b40c82e6
JK
201 * @vf: pointer to the VF info
202 * @vector_id: VF relative vector id
5c3c48ac
JB
203 *
204 * check for the valid vector id
205 **/
206static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
207{
208 struct i40e_pf *pf = vf->pf;
209
9347eb77 210 return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
5c3c48ac
JB
211}
212
213/***********************vf resource mgmt routines*****************/
214
215/**
216 * i40e_vc_get_pf_queue_id
b40c82e6 217 * @vf: pointer to the VF info
fdf0e0bf 218 * @vsi_id: id of VSI as provided by the FW
5c3c48ac
JB
219 * @vsi_queue_id: vsi relative queue id
220 *
b40c82e6 221 * return PF relative queue id
5c3c48ac 222 **/
fdf0e0bf 223static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
5c3c48ac
JB
224 u8 vsi_queue_id)
225{
226 struct i40e_pf *pf = vf->pf;
fdf0e0bf 227 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
5c3c48ac
JB
228 u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
229
fdf0e0bf
ASJ
230 if (!vsi)
231 return pf_queue_id;
232
5c3c48ac
JB
233 if (le16_to_cpu(vsi->info.mapping_flags) &
234 I40E_AQ_VSI_QUE_MAP_NONCONTIG)
235 pf_queue_id =
236 le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
237 else
238 pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
239 vsi_queue_id;
240
241 return pf_queue_id;
242}
243
5c3c48ac
JB
244/**
245 * i40e_config_irq_link_list
b40c82e6 246 * @vf: pointer to the VF info
fdf0e0bf 247 * @vsi_id: id of VSI as given by the FW
5c3c48ac
JB
248 * @vecmap: irq map info
249 *
250 * configure irq link list from the map
251 **/
fdf0e0bf 252static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
5c3c48ac
JB
253 struct i40e_virtchnl_vector_map *vecmap)
254{
255 unsigned long linklistmap = 0, tempmap;
256 struct i40e_pf *pf = vf->pf;
257 struct i40e_hw *hw = &pf->hw;
258 u16 vsi_queue_id, pf_queue_id;
259 enum i40e_queue_type qtype;
260 u16 next_q, vector_id;
261 u32 reg, reg_idx;
262 u16 itr_idx = 0;
263
264 vector_id = vecmap->vector_id;
265 /* setup the head */
266 if (0 == vector_id)
267 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
268 else
269 reg_idx = I40E_VPINT_LNKLSTN(
9347eb77
MW
270 ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
271 (vector_id - 1));
5c3c48ac
JB
272
273 if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
274 /* Special case - No queues mapped on this vector */
275 wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
276 goto irq_list_done;
277 }
278 tempmap = vecmap->rxq_map;
4836650b 279 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
41a1d04b
JB
280 linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
281 vsi_queue_id));
5c3c48ac
JB
282 }
283
284 tempmap = vecmap->txq_map;
4836650b 285 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
41a1d04b
JB
286 linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
287 vsi_queue_id + 1));
5c3c48ac
JB
288 }
289
290 next_q = find_first_bit(&linklistmap,
291 (I40E_MAX_VSI_QP *
292 I40E_VIRTCHNL_SUPPORTED_QTYPES));
b82bc49e
MW
293 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
294 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
fdf0e0bf 295 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
5c3c48ac
JB
296 reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
297
298 wr32(hw, reg_idx, reg);
299
300 while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
301 switch (qtype) {
302 case I40E_QUEUE_TYPE_RX:
303 reg_idx = I40E_QINT_RQCTL(pf_queue_id);
304 itr_idx = vecmap->rxitr_idx;
305 break;
306 case I40E_QUEUE_TYPE_TX:
307 reg_idx = I40E_QINT_TQCTL(pf_queue_id);
308 itr_idx = vecmap->txitr_idx;
309 break;
310 default:
311 break;
312 }
313
314 next_q = find_next_bit(&linklistmap,
315 (I40E_MAX_VSI_QP *
316 I40E_VIRTCHNL_SUPPORTED_QTYPES),
317 next_q + 1);
829af3ac
MW
318 if (next_q <
319 (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
5c3c48ac
JB
320 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
321 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
fdf0e0bf 322 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
5c3c48ac
JB
323 vsi_queue_id);
324 } else {
325 pf_queue_id = I40E_QUEUE_END_OF_LIST;
326 qtype = 0;
327 }
328
329 /* format for the RQCTL & TQCTL regs is same */
330 reg = (vector_id) |
331 (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
332 (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
41a1d04b 333 BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
5c3c48ac
JB
334 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
335 wr32(hw, reg_idx, reg);
336 }
337
b8262a6d
ASJ
338 /* if the vf is running in polling mode and using interrupt zero,
339 * need to disable auto-mask on enabling zero interrupt for VFs.
340 */
341 if ((vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
342 (vector_id == 0)) {
343 reg = rd32(hw, I40E_GLINT_CTL);
344 if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
345 reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
346 wr32(hw, I40E_GLINT_CTL, reg);
347 }
348 }
349
5c3c48ac
JB
350irq_list_done:
351 i40e_flush(hw);
352}
353
354/**
355 * i40e_config_vsi_tx_queue
b40c82e6 356 * @vf: pointer to the VF info
fdf0e0bf 357 * @vsi_id: id of VSI as provided by the FW
5c3c48ac
JB
358 * @vsi_queue_id: vsi relative queue index
359 * @info: config. info
360 *
361 * configure tx queue
362 **/
fdf0e0bf 363static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
5c3c48ac
JB
364 u16 vsi_queue_id,
365 struct i40e_virtchnl_txq_info *info)
366{
367 struct i40e_pf *pf = vf->pf;
368 struct i40e_hw *hw = &pf->hw;
369 struct i40e_hmc_obj_txq tx_ctx;
fdf0e0bf 370 struct i40e_vsi *vsi;
5c3c48ac
JB
371 u16 pf_queue_id;
372 u32 qtx_ctl;
373 int ret = 0;
374
fdf0e0bf
ASJ
375 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
376 vsi = i40e_find_vsi_from_id(pf, vsi_id);
5c3c48ac
JB
377
378 /* clear the context structure first */
379 memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
380
381 /* only set the required fields */
382 tx_ctx.base = info->dma_ring_addr / 128;
383 tx_ctx.qlen = info->ring_len;
fdf0e0bf 384 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
5c3c48ac 385 tx_ctx.rdylist_act = 0;
5d29896a
AS
386 tx_ctx.head_wb_ena = info->headwb_enabled;
387 tx_ctx.head_wb_addr = info->dma_headwb_addr;
5c3c48ac
JB
388
389 /* clear the context in the HMC */
390 ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
391 if (ret) {
392 dev_err(&pf->pdev->dev,
393 "Failed to clear VF LAN Tx queue context %d, error: %d\n",
394 pf_queue_id, ret);
395 ret = -ENOENT;
396 goto error_context;
397 }
398
399 /* set the context in the HMC */
400 ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
401 if (ret) {
402 dev_err(&pf->pdev->dev,
403 "Failed to set VF LAN Tx queue context %d error: %d\n",
404 pf_queue_id, ret);
405 ret = -ENOENT;
406 goto error_context;
407 }
408
409 /* associate this queue with the PCI VF function */
410 qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
13fd9774 411 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
5c3c48ac
JB
412 & I40E_QTX_CTL_PF_INDX_MASK);
413 qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
414 << I40E_QTX_CTL_VFVM_INDX_SHIFT)
415 & I40E_QTX_CTL_VFVM_INDX_MASK);
416 wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
417 i40e_flush(hw);
418
419error_context:
420 return ret;
421}
422
423/**
424 * i40e_config_vsi_rx_queue
b40c82e6 425 * @vf: pointer to the VF info
fdf0e0bf 426 * @vsi_id: id of VSI as provided by the FW
5c3c48ac
JB
427 * @vsi_queue_id: vsi relative queue index
428 * @info: config. info
429 *
430 * configure rx queue
431 **/
fdf0e0bf 432static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
5c3c48ac
JB
433 u16 vsi_queue_id,
434 struct i40e_virtchnl_rxq_info *info)
435{
436 struct i40e_pf *pf = vf->pf;
437 struct i40e_hw *hw = &pf->hw;
438 struct i40e_hmc_obj_rxq rx_ctx;
439 u16 pf_queue_id;
440 int ret = 0;
441
fdf0e0bf 442 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
5c3c48ac
JB
443
444 /* clear the context structure first */
445 memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
446
447 /* only set the required fields */
448 rx_ctx.base = info->dma_ring_addr / 128;
449 rx_ctx.qlen = info->ring_len;
450
451 if (info->splithdr_enabled) {
452 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
453 I40E_RX_SPLIT_IP |
454 I40E_RX_SPLIT_TCP_UDP |
455 I40E_RX_SPLIT_SCTP;
456 /* header length validation */
457 if (info->hdr_size > ((2 * 1024) - 64)) {
458 ret = -EINVAL;
459 goto error_param;
460 }
461 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
462
463 /* set splitalways mode 10b */
464 rx_ctx.dtype = 0x2;
465 }
466
467 /* databuffer length validation */
468 if (info->databuffer_size > ((16 * 1024) - 128)) {
469 ret = -EINVAL;
470 goto error_param;
471 }
472 rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
473
474 /* max pkt. length validation */
475 if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
476 ret = -EINVAL;
477 goto error_param;
478 }
479 rx_ctx.rxmax = info->max_pkt_size;
480
481 /* enable 32bytes desc always */
482 rx_ctx.dsize = 1;
483
484 /* default values */
5c3c48ac
JB
485 rx_ctx.lrxqthresh = 2;
486 rx_ctx.crcstrip = 1;
50d41659 487 rx_ctx.prefena = 1;
c1d11cef 488 rx_ctx.l2tsel = 1;
5c3c48ac
JB
489
490 /* clear the context in the HMC */
491 ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
492 if (ret) {
493 dev_err(&pf->pdev->dev,
494 "Failed to clear VF LAN Rx queue context %d, error: %d\n",
495 pf_queue_id, ret);
496 ret = -ENOENT;
497 goto error_param;
498 }
499
500 /* set the context in the HMC */
501 ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
502 if (ret) {
503 dev_err(&pf->pdev->dev,
504 "Failed to set VF LAN Rx queue context %d error: %d\n",
505 pf_queue_id, ret);
506 ret = -ENOENT;
507 goto error_param;
508 }
509
510error_param:
511 return ret;
512}
513
514/**
515 * i40e_alloc_vsi_res
b40c82e6 516 * @vf: pointer to the VF info
5c3c48ac
JB
517 * @type: type of VSI to allocate
518 *
b40c82e6 519 * alloc VF vsi context & resources
5c3c48ac
JB
520 **/
521static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
522{
523 struct i40e_mac_filter *f = NULL;
524 struct i40e_pf *pf = vf->pf;
5c3c48ac
JB
525 struct i40e_vsi *vsi;
526 int ret = 0;
527
528 vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
529
530 if (!vsi) {
531 dev_err(&pf->pdev->dev,
b40c82e6 532 "add vsi failed for VF %d, aq_err %d\n",
5c3c48ac
JB
533 vf->vf_id, pf->hw.aq.asq_last_status);
534 ret = -ENOENT;
535 goto error_alloc_vsi_res;
536 }
537 if (type == I40E_VSI_SRIOV) {
1a10370a 538 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
6995b36c 539
fdf0e0bf 540 vf->lan_vsi_idx = vsi->idx;
5c3c48ac 541 vf->lan_vsi_id = vsi->id;
6c12fcbf
GR
542 /* If the port VLAN has been configured and then the
543 * VF driver was removed then the VSI port VLAN
544 * configuration was destroyed. Check if there is
545 * a port VLAN and restore the VSI configuration if
546 * needed.
547 */
548 if (vf->port_vlan_id)
549 i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
21659035
KP
550
551 spin_lock_bh(&vsi->mac_filter_list_lock);
b7b713a8
MW
552 if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
553 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
554 vf->port_vlan_id ? vf->port_vlan_id : -1,
555 true, false);
556 if (!f)
557 dev_info(&pf->pdev->dev,
558 "Could not add MAC filter %pM for VF %d\n",
559 vf->default_lan_addr.addr, vf->vf_id);
560 }
e995163c
MW
561 f = i40e_add_filter(vsi, brdcast,
562 vf->port_vlan_id ? vf->port_vlan_id : -1,
1a10370a
GR
563 true, false);
564 if (!f)
565 dev_info(&pf->pdev->dev,
566 "Could not allocate VF broadcast filter\n");
21659035 567 spin_unlock_bh(&vsi->mac_filter_list_lock);
5c3c48ac 568 }
6dbbbfb2 569
5c3c48ac 570 /* program mac filter */
17652c63 571 ret = i40e_sync_vsi_filters(vsi);
fd1646ee 572 if (ret)
5c3c48ac 573 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
5c3c48ac 574
6b192891
MW
575 /* Set VF bandwidth if specified */
576 if (vf->tx_rate) {
577 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
578 vf->tx_rate / 50, 0, NULL);
579 if (ret)
580 dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
581 vf->vf_id, ret);
582 }
583
5c3c48ac
JB
584error_alloc_vsi_res:
585 return ret;
586}
587
805bd5bd
MW
588/**
589 * i40e_enable_vf_mappings
b40c82e6 590 * @vf: pointer to the VF info
805bd5bd 591 *
b40c82e6 592 * enable VF mappings
805bd5bd
MW
593 **/
594static void i40e_enable_vf_mappings(struct i40e_vf *vf)
595{
596 struct i40e_pf *pf = vf->pf;
597 struct i40e_hw *hw = &pf->hw;
598 u32 reg, total_queue_pairs = 0;
599 int j;
600
601 /* Tell the hardware we're using noncontiguous mapping. HW requires
602 * that VF queues be mapped using this method, even when they are
603 * contiguous in real life
604 */
605 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
606 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
607
608 /* enable VF vplan_qtable mappings */
609 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
610 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
611
612 /* map PF queues to VF queues */
fdf0e0bf
ASJ
613 for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
614 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
6995b36c 615
805bd5bd
MW
616 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
617 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
618 total_queue_pairs++;
619 }
620
621 /* map PF queues to VSI */
622 for (j = 0; j < 7; j++) {
fdf0e0bf 623 if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
805bd5bd
MW
624 reg = 0x07FF07FF; /* unused */
625 } else {
fdf0e0bf 626 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
805bd5bd
MW
627 j * 2);
628 reg = qid;
fdf0e0bf 629 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
805bd5bd
MW
630 (j * 2) + 1);
631 reg |= qid << 16;
632 }
633 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
634 }
635
636 i40e_flush(hw);
637}
638
639/**
640 * i40e_disable_vf_mappings
b40c82e6 641 * @vf: pointer to the VF info
805bd5bd 642 *
b40c82e6 643 * disable VF mappings
805bd5bd
MW
644 **/
645static void i40e_disable_vf_mappings(struct i40e_vf *vf)
646{
647 struct i40e_pf *pf = vf->pf;
648 struct i40e_hw *hw = &pf->hw;
649 int i;
650
651 /* disable qp mappings */
652 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
653 for (i = 0; i < I40E_MAX_VSI_QP; i++)
654 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
655 I40E_QUEUE_END_OF_LIST);
656 i40e_flush(hw);
657}
658
659/**
660 * i40e_free_vf_res
b40c82e6 661 * @vf: pointer to the VF info
805bd5bd 662 *
b40c82e6 663 * free VF resources
805bd5bd
MW
664 **/
665static void i40e_free_vf_res(struct i40e_vf *vf)
666{
667 struct i40e_pf *pf = vf->pf;
fc18eaa0
MW
668 struct i40e_hw *hw = &pf->hw;
669 u32 reg_idx, reg;
670 int i, msix_vf;
805bd5bd
MW
671
672 /* free vsi & disconnect it from the parent uplink */
fdf0e0bf
ASJ
673 if (vf->lan_vsi_idx) {
674 i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
675 vf->lan_vsi_idx = 0;
805bd5bd
MW
676 vf->lan_vsi_id = 0;
677 }
9347eb77
MW
678 msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
679
fc18eaa0
MW
680 /* disable interrupts so the VF starts in a known state */
681 for (i = 0; i < msix_vf; i++) {
682 /* format is same for both registers */
683 if (0 == i)
684 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
685 else
686 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
687 (vf->vf_id))
688 + (i - 1));
689 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
690 i40e_flush(hw);
691 }
805bd5bd 692
fc18eaa0
MW
693 /* clear the irq settings */
694 for (i = 0; i < msix_vf; i++) {
695 /* format is same for both registers */
696 if (0 == i)
697 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
698 else
699 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
700 (vf->vf_id))
701 + (i - 1));
702 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
703 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
704 wr32(hw, reg_idx, reg);
705 i40e_flush(hw);
706 }
805bd5bd
MW
707 /* reset some of the state varibles keeping
708 * track of the resources
709 */
710 vf->num_queue_pairs = 0;
711 vf->vf_states = 0;
21be99ec 712 clear_bit(I40E_VF_STAT_INIT, &vf->vf_states);
805bd5bd
MW
713}
714
715/**
716 * i40e_alloc_vf_res
b40c82e6 717 * @vf: pointer to the VF info
805bd5bd 718 *
b40c82e6 719 * allocate VF resources
805bd5bd
MW
720 **/
721static int i40e_alloc_vf_res(struct i40e_vf *vf)
722{
723 struct i40e_pf *pf = vf->pf;
724 int total_queue_pairs = 0;
725 int ret;
726
727 /* allocate hw vsi context & associated resources */
728 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
729 if (ret)
730 goto error_alloc;
fdf0e0bf 731 total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
805bd5bd
MW
732 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
733
734 /* store the total qps number for the runtime
b40c82e6 735 * VF req validation
805bd5bd
MW
736 */
737 vf->num_queue_pairs = total_queue_pairs;
738
b40c82e6 739 /* VF is now completely initialized */
805bd5bd
MW
740 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
741
742error_alloc:
743 if (ret)
744 i40e_free_vf_res(vf);
745
746 return ret;
747}
748
fc18eaa0
MW
749#define VF_DEVICE_STATUS 0xAA
750#define VF_TRANS_PENDING_MASK 0x20
751/**
752 * i40e_quiesce_vf_pci
b40c82e6 753 * @vf: pointer to the VF structure
fc18eaa0
MW
754 *
755 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
756 * if the transactions never clear.
757 **/
758static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
759{
760 struct i40e_pf *pf = vf->pf;
761 struct i40e_hw *hw = &pf->hw;
762 int vf_abs_id, i;
763 u32 reg;
764
b141d619 765 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
fc18eaa0
MW
766
767 wr32(hw, I40E_PF_PCI_CIAA,
768 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
769 for (i = 0; i < 100; i++) {
770 reg = rd32(hw, I40E_PF_PCI_CIAD);
771 if ((reg & VF_TRANS_PENDING_MASK) == 0)
772 return 0;
773 udelay(1);
774 }
775 return -EIO;
776}
777
5c3c48ac
JB
778/**
779 * i40e_reset_vf
b40c82e6 780 * @vf: pointer to the VF structure
5c3c48ac
JB
781 * @flr: VFLR was issued or not
782 *
b40c82e6 783 * reset the VF
5c3c48ac 784 **/
fc18eaa0 785void i40e_reset_vf(struct i40e_vf *vf, bool flr)
5c3c48ac 786{
5c3c48ac
JB
787 struct i40e_pf *pf = vf->pf;
788 struct i40e_hw *hw = &pf->hw;
5c3c48ac 789 bool rsd = false;
fc18eaa0
MW
790 int i;
791 u32 reg;
5c3c48ac 792
3ba9bcb4
MW
793 if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
794 return;
795
5c3c48ac 796 /* warn the VF */
5c3c48ac
JB
797 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
798
fc18eaa0
MW
799 /* In the case of a VFLR, the HW has already reset the VF and we
800 * just need to clean up, so don't hit the VFRTRIG register.
5c3c48ac
JB
801 */
802 if (!flr) {
b40c82e6 803 /* reset VF using VPGEN_VFRTRIG reg */
fc18eaa0
MW
804 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
805 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
5c3c48ac
JB
806 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
807 i40e_flush(hw);
808 }
809
fc18eaa0
MW
810 if (i40e_quiesce_vf_pci(vf))
811 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
812 vf->vf_id);
813
5c3c48ac
JB
814 /* poll VPGEN_VFRSTAT reg to make sure
815 * that reset is complete
816 */
1750a22f
MW
817 for (i = 0; i < 10; i++) {
818 /* VF reset requires driver to first reset the VF and then
819 * poll the status register to make sure that the reset
820 * completed successfully. Due to internal HW FIFO flushes,
821 * we must wait 10ms before the register will be valid.
5c3c48ac 822 */
1750a22f 823 usleep_range(10000, 20000);
5c3c48ac
JB
824 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
825 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
826 rsd = true;
827 break;
828 }
829 }
830
57175ac1
ASJ
831 if (flr)
832 usleep_range(10000, 20000);
833
5c3c48ac 834 if (!rsd)
fc18eaa0 835 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
5c3c48ac 836 vf->vf_id);
fc18eaa0 837 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
5c3c48ac
JB
838 /* clear the reset bit in the VPGEN_VFRTRIG reg */
839 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
840 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
841 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
fc18eaa0
MW
842
843 /* On initial reset, we won't have any queues */
fdf0e0bf 844 if (vf->lan_vsi_idx == 0)
fc18eaa0
MW
845 goto complete_reset;
846
fdf0e0bf 847 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false);
fc18eaa0 848complete_reset:
b40c82e6 849 /* reallocate VF resources to reset the VSI state */
fc18eaa0 850 i40e_free_vf_res(vf);
21be99ec
MW
851 if (!i40e_alloc_vf_res(vf)) {
852 i40e_enable_vf_mappings(vf);
853 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
854 clear_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
855 }
5c3c48ac 856 /* tell the VF the reset is done */
fc18eaa0 857 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
5c3c48ac 858 i40e_flush(hw);
3ba9bcb4 859 clear_bit(__I40E_VF_DISABLE, &pf->state);
5c3c48ac 860}
c354229f 861
5c3c48ac
JB
862/**
863 * i40e_free_vfs
b40c82e6 864 * @pf: pointer to the PF structure
5c3c48ac 865 *
b40c82e6 866 * free VF resources
5c3c48ac
JB
867 **/
868void i40e_free_vfs(struct i40e_pf *pf)
869{
f7414531
MW
870 struct i40e_hw *hw = &pf->hw;
871 u32 reg_idx, bit_idx;
872 int i, tmp, vf_id;
5c3c48ac
JB
873
874 if (!pf->vf)
875 return;
3ba9bcb4
MW
876 while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
877 usleep_range(1000, 2000);
5c3c48ac 878
44434638
MW
879 for (i = 0; i < pf->num_alloc_vfs; i++)
880 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
881 i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
882 false);
0325fca7
MW
883
884 for (i = 0; i < pf->num_alloc_vfs; i++)
885 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
886 i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
887 false);
44434638 888
6a9ddb36
MW
889 /* Disable IOV before freeing resources. This lets any VF drivers
890 * running in the host get themselves cleaned up before we yank
891 * the carpet out from underneath their feet.
892 */
893 if (!pci_vfs_assigned(pf->pdev))
894 pci_disable_sriov(pf->pdev);
6d7b967d
MW
895 else
896 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
6a9ddb36
MW
897
898 msleep(20); /* let any messages in transit get finished up */
899
b40c82e6 900 /* free up VF resources */
6c1b5bff
MW
901 tmp = pf->num_alloc_vfs;
902 pf->num_alloc_vfs = 0;
903 for (i = 0; i < tmp; i++) {
5c3c48ac
JB
904 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
905 i40e_free_vf_res(&pf->vf[i]);
906 /* disable qp mappings */
907 i40e_disable_vf_mappings(&pf->vf[i]);
908 }
909
910 kfree(pf->vf);
911 pf->vf = NULL;
5c3c48ac 912
9e5634df
MW
913 /* This check is for when the driver is unloaded while VFs are
914 * assigned. Setting the number of VFs to 0 through sysfs is caught
915 * before this function ever gets called.
916 */
c24817b6 917 if (!pci_vfs_assigned(pf->pdev)) {
f7414531
MW
918 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
919 * work correctly when SR-IOV gets re-enabled.
920 */
921 for (vf_id = 0; vf_id < tmp; vf_id++) {
922 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
923 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
41a1d04b 924 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
f7414531 925 }
c354229f 926 }
3ba9bcb4 927 clear_bit(__I40E_VF_DISABLE, &pf->state);
5c3c48ac
JB
928}
929
930#ifdef CONFIG_PCI_IOV
931/**
932 * i40e_alloc_vfs
b40c82e6
JK
933 * @pf: pointer to the PF structure
934 * @num_alloc_vfs: number of VFs to allocate
5c3c48ac 935 *
b40c82e6 936 * allocate VF resources
5c3c48ac 937 **/
4aeec010 938int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
5c3c48ac
JB
939{
940 struct i40e_vf *vfs;
941 int i, ret = 0;
942
6c1b5bff 943 /* Disable interrupt 0 so we don't try to handle the VFLR. */
2ef28cfb
MW
944 i40e_irq_dynamic_disable_icr0(pf);
945
4aeec010
MW
946 /* Check to see if we're just allocating resources for extant VFs */
947 if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
948 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
949 if (ret) {
de445b3d 950 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
4aeec010
MW
951 pf->num_alloc_vfs = 0;
952 goto err_iov;
953 }
5c3c48ac 954 }
5c3c48ac 955 /* allocate memory */
cc6456af 956 vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
5c3c48ac
JB
957 if (!vfs) {
958 ret = -ENOMEM;
959 goto err_alloc;
960 }
c674d125 961 pf->vf = vfs;
5c3c48ac
JB
962
963 /* apply default profile */
964 for (i = 0; i < num_alloc_vfs; i++) {
965 vfs[i].pf = pf;
966 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
967 vfs[i].vf_id = i;
968
969 /* assign default capabilities */
970 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
c674d125 971 vfs[i].spoofchk = true;
b40c82e6 972 /* VF resources get allocated during reset */
fc18eaa0 973 i40e_reset_vf(&vfs[i], false);
5c3c48ac 974
5c3c48ac 975 }
5c3c48ac
JB
976 pf->num_alloc_vfs = num_alloc_vfs;
977
978err_alloc:
979 if (ret)
980 i40e_free_vfs(pf);
981err_iov:
6c1b5bff 982 /* Re-enable interrupt 0. */
2ef28cfb 983 i40e_irq_dynamic_enable_icr0(pf);
5c3c48ac
JB
984 return ret;
985}
986
987#endif
988/**
989 * i40e_pci_sriov_enable
990 * @pdev: pointer to a pci_dev structure
b40c82e6 991 * @num_vfs: number of VFs to allocate
5c3c48ac
JB
992 *
993 * Enable or change the number of VFs
994 **/
995static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
996{
997#ifdef CONFIG_PCI_IOV
998 struct i40e_pf *pf = pci_get_drvdata(pdev);
999 int pre_existing_vfs = pci_num_vf(pdev);
1000 int err = 0;
1001
6995b36c 1002 if (test_bit(__I40E_TESTING, &pf->state)) {
e17bc411
GR
1003 dev_warn(&pdev->dev,
1004 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
1005 err = -EPERM;
1006 goto err_out;
1007 }
1008
5c3c48ac
JB
1009 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1010 i40e_free_vfs(pf);
1011 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1012 goto out;
1013
1014 if (num_vfs > pf->num_req_vfs) {
96c8d073
MW
1015 dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
1016 num_vfs, pf->num_req_vfs);
5c3c48ac
JB
1017 err = -EPERM;
1018 goto err_out;
1019 }
1020
96c8d073 1021 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
5c3c48ac
JB
1022 err = i40e_alloc_vfs(pf, num_vfs);
1023 if (err) {
1024 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1025 goto err_out;
1026 }
1027
1028out:
1029 return num_vfs;
1030
1031err_out:
1032 return err;
1033#endif
1034 return 0;
1035}
1036
1037/**
1038 * i40e_pci_sriov_configure
1039 * @pdev: pointer to a pci_dev structure
b40c82e6 1040 * @num_vfs: number of VFs to allocate
5c3c48ac
JB
1041 *
1042 * Enable or change the number of VFs. Called when the user updates the number
1043 * of VFs in sysfs.
1044 **/
1045int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1046{
1047 struct i40e_pf *pf = pci_get_drvdata(pdev);
1048
fc60861e
ASJ
1049 if (num_vfs) {
1050 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1051 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1052 i40e_do_reset_safe(pf,
1053 BIT_ULL(__I40E_PF_RESET_REQUESTED));
1054 }
5c3c48ac 1055 return i40e_pci_sriov_enable(pdev, num_vfs);
fc60861e 1056 }
5c3c48ac 1057
c24817b6 1058 if (!pci_vfs_assigned(pf->pdev)) {
9e5634df 1059 i40e_free_vfs(pf);
fc60861e
ASJ
1060 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1061 i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
9e5634df
MW
1062 } else {
1063 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1064 return -EINVAL;
1065 }
5c3c48ac
JB
1066 return 0;
1067}
1068
1069/***********************virtual channel routines******************/
1070
1071/**
1072 * i40e_vc_send_msg_to_vf
b40c82e6 1073 * @vf: pointer to the VF info
5c3c48ac
JB
1074 * @v_opcode: virtual channel opcode
1075 * @v_retval: virtual channel return value
1076 * @msg: pointer to the msg buffer
1077 * @msglen: msg length
1078 *
b40c82e6 1079 * send msg to VF
5c3c48ac
JB
1080 **/
1081static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1082 u32 v_retval, u8 *msg, u16 msglen)
1083{
6e7b5bd3
ASJ
1084 struct i40e_pf *pf;
1085 struct i40e_hw *hw;
1086 int abs_vf_id;
5c3c48ac
JB
1087 i40e_status aq_ret;
1088
6e7b5bd3
ASJ
1089 /* validate the request */
1090 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1091 return -EINVAL;
1092
1093 pf = vf->pf;
1094 hw = &pf->hw;
1095 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1096
5c3c48ac
JB
1097 /* single place to detect unsuccessful return values */
1098 if (v_retval) {
1099 vf->num_invalid_msgs++;
e7ffb72d
MW
1100 dev_err(&pf->pdev->dev, "VF %d failed opcode %d, error: %d\n",
1101 vf->vf_id, v_opcode, v_retval);
5c3c48ac
JB
1102 if (vf->num_invalid_msgs >
1103 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1104 dev_err(&pf->pdev->dev,
1105 "Number of invalid messages exceeded for VF %d\n",
1106 vf->vf_id);
1107 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1108 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1109 }
1110 } else {
1111 vf->num_valid_msgs++;
5d38c93e
JW
1112 /* reset the invalid counter, if a valid message is received. */
1113 vf->num_invalid_msgs = 0;
5c3c48ac
JB
1114 }
1115
f19efbb5 1116 aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
7efa84b7 1117 msg, msglen, NULL);
5c3c48ac
JB
1118 if (aq_ret) {
1119 dev_err(&pf->pdev->dev,
1120 "Unable to send the message to VF %d aq_err %d\n",
1121 vf->vf_id, pf->hw.aq.asq_last_status);
1122 return -EIO;
1123 }
1124
1125 return 0;
1126}
1127
1128/**
1129 * i40e_vc_send_resp_to_vf
b40c82e6 1130 * @vf: pointer to the VF info
5c3c48ac
JB
1131 * @opcode: operation code
1132 * @retval: return value
1133 *
b40c82e6 1134 * send resp msg to VF
5c3c48ac
JB
1135 **/
1136static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1137 enum i40e_virtchnl_ops opcode,
1138 i40e_status retval)
1139{
1140 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1141}
1142
1143/**
1144 * i40e_vc_get_version_msg
b40c82e6 1145 * @vf: pointer to the VF info
5c3c48ac 1146 *
b40c82e6 1147 * called from the VF to request the API version used by the PF
5c3c48ac 1148 **/
f4ca1a22 1149static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
5c3c48ac
JB
1150{
1151 struct i40e_virtchnl_version_info info = {
1152 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
1153 };
1154
f4ca1a22 1155 vf->vf_ver = *(struct i40e_virtchnl_version_info *)msg;
606a5488
MW
1156 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1157 if (VF_IS_V10(vf))
1158 info.minor = I40E_VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
5c3c48ac
JB
1159 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
1160 I40E_SUCCESS, (u8 *)&info,
1161 sizeof(struct
1162 i40e_virtchnl_version_info));
1163}
1164
1165/**
1166 * i40e_vc_get_vf_resources_msg
b40c82e6 1167 * @vf: pointer to the VF info
5c3c48ac
JB
1168 * @msg: pointer to the msg buffer
1169 * @msglen: msg length
1170 *
b40c82e6 1171 * called from the VF to request its resources
5c3c48ac 1172 **/
f4ca1a22 1173static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
5c3c48ac
JB
1174{
1175 struct i40e_virtchnl_vf_resource *vfres = NULL;
1176 struct i40e_pf *pf = vf->pf;
1177 i40e_status aq_ret = 0;
1178 struct i40e_vsi *vsi;
1179 int i = 0, len = 0;
1180 int num_vsis = 1;
1181 int ret;
1182
1183 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1184 aq_ret = I40E_ERR_PARAM;
1185 goto err;
1186 }
1187
1188 len = (sizeof(struct i40e_virtchnl_vf_resource) +
1189 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1190
1191 vfres = kzalloc(len, GFP_KERNEL);
1192 if (!vfres) {
1193 aq_ret = I40E_ERR_NO_MEMORY;
1194 len = 0;
1195 goto err;
1196 }
f4ca1a22
MW
1197 if (VF_IS_V11(vf))
1198 vf->driver_caps = *(u32 *)msg;
1199 else
1200 vf->driver_caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
1201 I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
1202 I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
5c3c48ac
JB
1203
1204 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
fdf0e0bf 1205 vsi = pf->vsi[vf->lan_vsi_idx];
5c3c48ac 1206 if (!vsi->info.pvid)
e25d00b8
ASJ
1207 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1208 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
1209 if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ)
1210 vfres->vf_offload_flags |=
1211 I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1212 } else {
1213 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG;
1214 }
1f012279 1215
3d0da5b7
ASJ
1216 if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
1217 if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1218 vfres->vf_offload_flags |=
1219 I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1220 }
1221
1f012279
ASJ
1222 if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING)
1223 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1224
f6d83d13
ASJ
1225 if (pf->flags & I40E_FLAG_WB_ON_ITR_CAPABLE) {
1226 if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1227 vfres->vf_offload_flags |=
1228 I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1229 }
1230
5c3c48ac
JB
1231 vfres->num_vsis = num_vsis;
1232 vfres->num_queue_pairs = vf->num_queue_pairs;
1233 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
fdf0e0bf
ASJ
1234 if (vf->lan_vsi_idx) {
1235 vfres->vsi_res[i].vsi_id = vf->lan_vsi_id;
5c3c48ac 1236 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
f578f5f4
MW
1237 vfres->vsi_res[i].num_queue_pairs = vsi->alloc_queue_pairs;
1238 /* VFs only use TC 0 */
1239 vfres->vsi_res[i].qset_handle
1240 = le16_to_cpu(vsi->info.qs_handle[0]);
6995b36c
JB
1241 ether_addr_copy(vfres->vsi_res[i].default_mac_addr,
1242 vf->default_lan_addr.addr);
5c3c48ac
JB
1243 i++;
1244 }
1245 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1246
1247err:
b40c82e6 1248 /* send the response back to the VF */
5c3c48ac
JB
1249 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1250 aq_ret, (u8 *)vfres, len);
1251
1252 kfree(vfres);
1253 return ret;
1254}
1255
1256/**
1257 * i40e_vc_reset_vf_msg
b40c82e6 1258 * @vf: pointer to the VF info
5c3c48ac
JB
1259 * @msg: pointer to the msg buffer
1260 * @msglen: msg length
1261 *
b40c82e6
JK
1262 * called from the VF to reset itself,
1263 * unlike other virtchnl messages, PF driver
1264 * doesn't send the response back to the VF
5c3c48ac 1265 **/
fc18eaa0 1266static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
5c3c48ac 1267{
fc18eaa0
MW
1268 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1269 i40e_reset_vf(vf, false);
5c3c48ac
JB
1270}
1271
1272/**
1273 * i40e_vc_config_promiscuous_mode_msg
b40c82e6 1274 * @vf: pointer to the VF info
5c3c48ac
JB
1275 * @msg: pointer to the msg buffer
1276 * @msglen: msg length
1277 *
b40c82e6
JK
1278 * called from the VF to configure the promiscuous mode of
1279 * VF vsis
5c3c48ac
JB
1280 **/
1281static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1282 u8 *msg, u16 msglen)
1283{
1284 struct i40e_virtchnl_promisc_info *info =
1285 (struct i40e_virtchnl_promisc_info *)msg;
1286 struct i40e_pf *pf = vf->pf;
1287 struct i40e_hw *hw = &pf->hw;
89cb86c3 1288 struct i40e_vsi *vsi;
5c3c48ac 1289 bool allmulti = false;
5c3c48ac
JB
1290 i40e_status aq_ret;
1291
fdf0e0bf 1292 vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
5c3c48ac
JB
1293 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1294 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1295 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
fdf0e0bf 1296 (vsi->type != I40E_VSI_FCOE)) {
5c3c48ac
JB
1297 aq_ret = I40E_ERR_PARAM;
1298 goto error_param;
1299 }
5c3c48ac
JB
1300 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1301 allmulti = true;
89cb86c3 1302 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
5c3c48ac
JB
1303 allmulti, NULL);
1304
1305error_param:
b40c82e6 1306 /* send the response to the VF */
5c3c48ac
JB
1307 return i40e_vc_send_resp_to_vf(vf,
1308 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1309 aq_ret);
1310}
1311
1312/**
1313 * i40e_vc_config_queues_msg
b40c82e6 1314 * @vf: pointer to the VF info
5c3c48ac
JB
1315 * @msg: pointer to the msg buffer
1316 * @msglen: msg length
1317 *
b40c82e6 1318 * called from the VF to configure the rx/tx
5c3c48ac
JB
1319 * queues
1320 **/
1321static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1322{
1323 struct i40e_virtchnl_vsi_queue_config_info *qci =
1324 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1325 struct i40e_virtchnl_queue_pair_info *qpi;
5f5e33b6 1326 struct i40e_pf *pf = vf->pf;
5c3c48ac
JB
1327 u16 vsi_id, vsi_queue_id;
1328 i40e_status aq_ret = 0;
1329 int i;
1330
1331 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1332 aq_ret = I40E_ERR_PARAM;
1333 goto error_param;
1334 }
1335
1336 vsi_id = qci->vsi_id;
1337 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1338 aq_ret = I40E_ERR_PARAM;
1339 goto error_param;
1340 }
1341 for (i = 0; i < qci->num_queue_pairs; i++) {
1342 qpi = &qci->qpair[i];
1343 vsi_queue_id = qpi->txq.queue_id;
1344 if ((qpi->txq.vsi_id != vsi_id) ||
1345 (qpi->rxq.vsi_id != vsi_id) ||
1346 (qpi->rxq.queue_id != vsi_queue_id) ||
1347 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1348 aq_ret = I40E_ERR_PARAM;
1349 goto error_param;
1350 }
1351
1352 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1353 &qpi->rxq) ||
1354 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1355 &qpi->txq)) {
1356 aq_ret = I40E_ERR_PARAM;
1357 goto error_param;
1358 }
1359 }
b40c82e6 1360 /* set vsi num_queue_pairs in use to num configured by VF */
fdf0e0bf 1361 pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
5c3c48ac
JB
1362
1363error_param:
b40c82e6 1364 /* send the response to the VF */
5c3c48ac
JB
1365 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1366 aq_ret);
1367}
1368
1369/**
1370 * i40e_vc_config_irq_map_msg
b40c82e6 1371 * @vf: pointer to the VF info
5c3c48ac
JB
1372 * @msg: pointer to the msg buffer
1373 * @msglen: msg length
1374 *
b40c82e6 1375 * called from the VF to configure the irq to
5c3c48ac
JB
1376 * queue map
1377 **/
1378static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1379{
1380 struct i40e_virtchnl_irq_map_info *irqmap_info =
1381 (struct i40e_virtchnl_irq_map_info *)msg;
1382 struct i40e_virtchnl_vector_map *map;
1383 u16 vsi_id, vsi_queue_id, vector_id;
1384 i40e_status aq_ret = 0;
1385 unsigned long tempmap;
1386 int i;
1387
1388 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1389 aq_ret = I40E_ERR_PARAM;
1390 goto error_param;
1391 }
1392
1393 for (i = 0; i < irqmap_info->num_vectors; i++) {
1394 map = &irqmap_info->vecmap[i];
1395
1396 vector_id = map->vector_id;
1397 vsi_id = map->vsi_id;
1398 /* validate msg params */
1399 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1400 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1401 aq_ret = I40E_ERR_PARAM;
1402 goto error_param;
1403 }
1404
1405 /* lookout for the invalid queue index */
1406 tempmap = map->rxq_map;
4836650b 1407 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
5c3c48ac
JB
1408 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1409 vsi_queue_id)) {
1410 aq_ret = I40E_ERR_PARAM;
1411 goto error_param;
1412 }
5c3c48ac
JB
1413 }
1414
1415 tempmap = map->txq_map;
4836650b 1416 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
5c3c48ac
JB
1417 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1418 vsi_queue_id)) {
1419 aq_ret = I40E_ERR_PARAM;
1420 goto error_param;
1421 }
5c3c48ac
JB
1422 }
1423
1424 i40e_config_irq_link_list(vf, vsi_id, map);
1425 }
1426error_param:
b40c82e6 1427 /* send the response to the VF */
5c3c48ac
JB
1428 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1429 aq_ret);
1430}
1431
1432/**
1433 * i40e_vc_enable_queues_msg
b40c82e6 1434 * @vf: pointer to the VF info
5c3c48ac
JB
1435 * @msg: pointer to the msg buffer
1436 * @msglen: msg length
1437 *
b40c82e6 1438 * called from the VF to enable all or specific queue(s)
5c3c48ac
JB
1439 **/
1440static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1441{
1442 struct i40e_virtchnl_queue_select *vqs =
1443 (struct i40e_virtchnl_queue_select *)msg;
1444 struct i40e_pf *pf = vf->pf;
1445 u16 vsi_id = vqs->vsi_id;
1446 i40e_status aq_ret = 0;
5c3c48ac
JB
1447
1448 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1449 aq_ret = I40E_ERR_PARAM;
1450 goto error_param;
1451 }
1452
1453 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1454 aq_ret = I40E_ERR_PARAM;
1455 goto error_param;
1456 }
1457
1458 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1459 aq_ret = I40E_ERR_PARAM;
1460 goto error_param;
1461 }
fdf0e0bf
ASJ
1462
1463 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], true))
88f6563d 1464 aq_ret = I40E_ERR_TIMEOUT;
5c3c48ac 1465error_param:
b40c82e6 1466 /* send the response to the VF */
5c3c48ac
JB
1467 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1468 aq_ret);
1469}
1470
1471/**
1472 * i40e_vc_disable_queues_msg
b40c82e6 1473 * @vf: pointer to the VF info
5c3c48ac
JB
1474 * @msg: pointer to the msg buffer
1475 * @msglen: msg length
1476 *
b40c82e6 1477 * called from the VF to disable all or specific
5c3c48ac
JB
1478 * queue(s)
1479 **/
1480static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1481{
1482 struct i40e_virtchnl_queue_select *vqs =
1483 (struct i40e_virtchnl_queue_select *)msg;
1484 struct i40e_pf *pf = vf->pf;
5c3c48ac 1485 i40e_status aq_ret = 0;
5c3c48ac
JB
1486
1487 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1488 aq_ret = I40E_ERR_PARAM;
1489 goto error_param;
1490 }
1491
1492 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1493 aq_ret = I40E_ERR_PARAM;
1494 goto error_param;
1495 }
1496
1497 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1498 aq_ret = I40E_ERR_PARAM;
1499 goto error_param;
1500 }
fdf0e0bf
ASJ
1501
1502 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false))
88f6563d 1503 aq_ret = I40E_ERR_TIMEOUT;
5c3c48ac
JB
1504
1505error_param:
b40c82e6 1506 /* send the response to the VF */
5c3c48ac
JB
1507 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1508 aq_ret);
1509}
1510
1511/**
1512 * i40e_vc_get_stats_msg
b40c82e6 1513 * @vf: pointer to the VF info
5c3c48ac
JB
1514 * @msg: pointer to the msg buffer
1515 * @msglen: msg length
1516 *
b40c82e6 1517 * called from the VF to get vsi stats
5c3c48ac
JB
1518 **/
1519static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1520{
1521 struct i40e_virtchnl_queue_select *vqs =
1522 (struct i40e_virtchnl_queue_select *)msg;
1523 struct i40e_pf *pf = vf->pf;
1524 struct i40e_eth_stats stats;
1525 i40e_status aq_ret = 0;
1526 struct i40e_vsi *vsi;
1527
1528 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1529
1530 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1531 aq_ret = I40E_ERR_PARAM;
1532 goto error_param;
1533 }
1534
1535 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1536 aq_ret = I40E_ERR_PARAM;
1537 goto error_param;
1538 }
1539
fdf0e0bf 1540 vsi = pf->vsi[vf->lan_vsi_idx];
5c3c48ac
JB
1541 if (!vsi) {
1542 aq_ret = I40E_ERR_PARAM;
1543 goto error_param;
1544 }
1545 i40e_update_eth_stats(vsi);
5a9769c8 1546 stats = vsi->eth_stats;
5c3c48ac
JB
1547
1548error_param:
b40c82e6 1549 /* send the response back to the VF */
5c3c48ac
JB
1550 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1551 (u8 *)&stats, sizeof(stats));
1552}
1553
f657a6e1
GR
1554/**
1555 * i40e_check_vf_permission
b40c82e6 1556 * @vf: pointer to the VF info
f657a6e1
GR
1557 * @macaddr: pointer to the MAC Address being checked
1558 *
1559 * Check if the VF has permission to add or delete unicast MAC address
1560 * filters and return error code -EPERM if not. Then check if the
1561 * address filter requested is broadcast or zero and if so return
1562 * an invalid MAC address error code.
1563 **/
1564static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1565{
1566 struct i40e_pf *pf = vf->pf;
1567 int ret = 0;
1568
1569 if (is_broadcast_ether_addr(macaddr) ||
1570 is_zero_ether_addr(macaddr)) {
1571 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1572 ret = I40E_ERR_INVALID_MAC_ADDR;
5017c2a8
GR
1573 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1574 !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
f657a6e1
GR
1575 /* If the host VMM administrator has set the VF MAC address
1576 * administratively via the ndo_set_vf_mac command then deny
1577 * permission to the VF to add or delete unicast MAC addresses.
5017c2a8
GR
1578 * The VF may request to set the MAC address filter already
1579 * assigned to it so do not return an error in that case.
f657a6e1
GR
1580 */
1581 dev_err(&pf->pdev->dev,
1582 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1583 ret = -EPERM;
1584 }
1585 return ret;
1586}
1587
5c3c48ac
JB
1588/**
1589 * i40e_vc_add_mac_addr_msg
b40c82e6 1590 * @vf: pointer to the VF info
5c3c48ac
JB
1591 * @msg: pointer to the msg buffer
1592 * @msglen: msg length
1593 *
1594 * add guest mac address filter
1595 **/
1596static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1597{
1598 struct i40e_virtchnl_ether_addr_list *al =
1599 (struct i40e_virtchnl_ether_addr_list *)msg;
1600 struct i40e_pf *pf = vf->pf;
1601 struct i40e_vsi *vsi = NULL;
1602 u16 vsi_id = al->vsi_id;
f657a6e1 1603 i40e_status ret = 0;
5c3c48ac
JB
1604 int i;
1605
1606 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1607 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1608 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
f657a6e1 1609 ret = I40E_ERR_PARAM;
5c3c48ac
JB
1610 goto error_param;
1611 }
1612
1613 for (i = 0; i < al->num_elements; i++) {
f657a6e1
GR
1614 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1615 if (ret)
5c3c48ac 1616 goto error_param;
5c3c48ac 1617 }
fdf0e0bf 1618 vsi = pf->vsi[vf->lan_vsi_idx];
5c3c48ac 1619
21659035
KP
1620 /* Lock once, because all function inside for loop accesses VSI's
1621 * MAC filter list which needs to be protected using same lock.
1622 */
1623 spin_lock_bh(&vsi->mac_filter_list_lock);
1624
5c3c48ac
JB
1625 /* add new addresses to the list */
1626 for (i = 0; i < al->num_elements; i++) {
1627 struct i40e_mac_filter *f;
1628
1629 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
7e68edf9 1630 if (!f) {
5c3c48ac
JB
1631 if (i40e_is_vsi_in_vlan(vsi))
1632 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1633 true, false);
1634 else
1635 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1636 true, false);
1637 }
1638
1639 if (!f) {
1640 dev_err(&pf->pdev->dev,
8d8f2295
MW
1641 "Unable to add MAC filter %pM for VF %d\n",
1642 al->list[i].addr, vf->vf_id);
f657a6e1 1643 ret = I40E_ERR_PARAM;
21659035 1644 spin_unlock_bh(&vsi->mac_filter_list_lock);
5c3c48ac
JB
1645 goto error_param;
1646 }
1647 }
21659035 1648 spin_unlock_bh(&vsi->mac_filter_list_lock);
5c3c48ac
JB
1649
1650 /* program the updated filter list */
ea02e90b
MW
1651 ret = i40e_sync_vsi_filters(vsi);
1652 if (ret)
1653 dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
1654 vf->vf_id, ret);
5c3c48ac
JB
1655
1656error_param:
b40c82e6 1657 /* send the response to the VF */
5c3c48ac 1658 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
f657a6e1 1659 ret);
5c3c48ac
JB
1660}
1661
1662/**
1663 * i40e_vc_del_mac_addr_msg
b40c82e6 1664 * @vf: pointer to the VF info
5c3c48ac
JB
1665 * @msg: pointer to the msg buffer
1666 * @msglen: msg length
1667 *
1668 * remove guest mac address filter
1669 **/
1670static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1671{
1672 struct i40e_virtchnl_ether_addr_list *al =
1673 (struct i40e_virtchnl_ether_addr_list *)msg;
1674 struct i40e_pf *pf = vf->pf;
1675 struct i40e_vsi *vsi = NULL;
1676 u16 vsi_id = al->vsi_id;
f657a6e1 1677 i40e_status ret = 0;
5c3c48ac
JB
1678 int i;
1679
1680 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1681 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1682 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
f657a6e1 1683 ret = I40E_ERR_PARAM;
5c3c48ac
JB
1684 goto error_param;
1685 }
f657a6e1
GR
1686
1687 for (i = 0; i < al->num_elements; i++) {
700bbf6c
MW
1688 if (is_broadcast_ether_addr(al->list[i].addr) ||
1689 is_zero_ether_addr(al->list[i].addr)) {
8d8f2295
MW
1690 dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
1691 al->list[i].addr, vf->vf_id);
700bbf6c 1692 ret = I40E_ERR_INVALID_MAC_ADDR;
f657a6e1 1693 goto error_param;
700bbf6c 1694 }
f657a6e1 1695 }
fdf0e0bf 1696 vsi = pf->vsi[vf->lan_vsi_idx];
5c3c48ac 1697
21659035 1698 spin_lock_bh(&vsi->mac_filter_list_lock);
5c3c48ac
JB
1699 /* delete addresses from the list */
1700 for (i = 0; i < al->num_elements; i++)
b36e9ab5
MW
1701 if (i40e_del_mac_all_vlan(vsi, al->list[i].addr, true, false)) {
1702 ret = I40E_ERR_INVALID_MAC_ADDR;
1703 spin_unlock_bh(&vsi->mac_filter_list_lock);
1704 goto error_param;
1705 }
1706
21659035 1707 spin_unlock_bh(&vsi->mac_filter_list_lock);
5c3c48ac
JB
1708
1709 /* program the updated filter list */
ea02e90b
MW
1710 ret = i40e_sync_vsi_filters(vsi);
1711 if (ret)
1712 dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
1713 vf->vf_id, ret);
5c3c48ac
JB
1714
1715error_param:
b40c82e6 1716 /* send the response to the VF */
5c3c48ac 1717 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
f657a6e1 1718 ret);
5c3c48ac
JB
1719}
1720
1721/**
1722 * i40e_vc_add_vlan_msg
b40c82e6 1723 * @vf: pointer to the VF info
5c3c48ac
JB
1724 * @msg: pointer to the msg buffer
1725 * @msglen: msg length
1726 *
1727 * program guest vlan id
1728 **/
1729static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1730{
1731 struct i40e_virtchnl_vlan_filter_list *vfl =
1732 (struct i40e_virtchnl_vlan_filter_list *)msg;
1733 struct i40e_pf *pf = vf->pf;
1734 struct i40e_vsi *vsi = NULL;
1735 u16 vsi_id = vfl->vsi_id;
1736 i40e_status aq_ret = 0;
1737 int i;
1738
1739 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1740 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1741 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1742 aq_ret = I40E_ERR_PARAM;
1743 goto error_param;
1744 }
1745
1746 for (i = 0; i < vfl->num_elements; i++) {
1747 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1748 aq_ret = I40E_ERR_PARAM;
1749 dev_err(&pf->pdev->dev,
1750 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1751 goto error_param;
1752 }
1753 }
fdf0e0bf 1754 vsi = pf->vsi[vf->lan_vsi_idx];
5c3c48ac
JB
1755 if (vsi->info.pvid) {
1756 aq_ret = I40E_ERR_PARAM;
1757 goto error_param;
1758 }
1759
1760 i40e_vlan_stripping_enable(vsi);
1761 for (i = 0; i < vfl->num_elements; i++) {
1762 /* add new VLAN filter */
1763 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
6995b36c 1764
5c3c48ac
JB
1765 if (ret)
1766 dev_err(&pf->pdev->dev,
8d8f2295
MW
1767 "Unable to add VLAN filter %d for VF %d, error %d\n",
1768 vfl->vlan_id[i], vf->vf_id, ret);
5c3c48ac
JB
1769 }
1770
1771error_param:
b40c82e6 1772 /* send the response to the VF */
5c3c48ac
JB
1773 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1774}
1775
1776/**
1777 * i40e_vc_remove_vlan_msg
b40c82e6 1778 * @vf: pointer to the VF info
5c3c48ac
JB
1779 * @msg: pointer to the msg buffer
1780 * @msglen: msg length
1781 *
1782 * remove programmed guest vlan id
1783 **/
1784static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1785{
1786 struct i40e_virtchnl_vlan_filter_list *vfl =
1787 (struct i40e_virtchnl_vlan_filter_list *)msg;
1788 struct i40e_pf *pf = vf->pf;
1789 struct i40e_vsi *vsi = NULL;
1790 u16 vsi_id = vfl->vsi_id;
1791 i40e_status aq_ret = 0;
1792 int i;
1793
1794 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1795 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1796 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1797 aq_ret = I40E_ERR_PARAM;
1798 goto error_param;
1799 }
1800
1801 for (i = 0; i < vfl->num_elements; i++) {
1802 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1803 aq_ret = I40E_ERR_PARAM;
1804 goto error_param;
1805 }
1806 }
1807
fdf0e0bf 1808 vsi = pf->vsi[vf->lan_vsi_idx];
5c3c48ac
JB
1809 if (vsi->info.pvid) {
1810 aq_ret = I40E_ERR_PARAM;
1811 goto error_param;
1812 }
1813
1814 for (i = 0; i < vfl->num_elements; i++) {
1815 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
6995b36c 1816
5c3c48ac
JB
1817 if (ret)
1818 dev_err(&pf->pdev->dev,
8d8f2295
MW
1819 "Unable to delete VLAN filter %d for VF %d, error %d\n",
1820 vfl->vlan_id[i], vf->vf_id, ret);
5c3c48ac
JB
1821 }
1822
1823error_param:
b40c82e6 1824 /* send the response to the VF */
5c3c48ac
JB
1825 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1826}
1827
5c3c48ac
JB
1828/**
1829 * i40e_vc_validate_vf_msg
b40c82e6 1830 * @vf: pointer to the VF info
5c3c48ac
JB
1831 * @msg: pointer to the msg buffer
1832 * @msglen: msg length
1833 * @msghndl: msg handle
1834 *
1835 * validate msg
1836 **/
1837static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1838 u32 v_retval, u8 *msg, u16 msglen)
1839{
1840 bool err_msg_format = false;
1841 int valid_len;
1842
1843 /* Check if VF is disabled. */
1844 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1845 return I40E_ERR_PARAM;
1846
1847 /* Validate message length. */
1848 switch (v_opcode) {
1849 case I40E_VIRTCHNL_OP_VERSION:
1850 valid_len = sizeof(struct i40e_virtchnl_version_info);
1851 break;
1852 case I40E_VIRTCHNL_OP_RESET_VF:
5c3c48ac
JB
1853 valid_len = 0;
1854 break;
f4ca1a22
MW
1855 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1856 if (VF_IS_V11(vf))
1857 valid_len = sizeof(u32);
1858 else
1859 valid_len = 0;
1860 break;
5c3c48ac
JB
1861 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1862 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1863 break;
1864 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1865 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1866 break;
1867 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1868 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1869 if (msglen >= valid_len) {
1870 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1871 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1872 valid_len += (vqc->num_queue_pairs *
1873 sizeof(struct
1874 i40e_virtchnl_queue_pair_info));
1875 if (vqc->num_queue_pairs == 0)
1876 err_msg_format = true;
1877 }
1878 break;
1879 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1880 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1881 if (msglen >= valid_len) {
1882 struct i40e_virtchnl_irq_map_info *vimi =
1883 (struct i40e_virtchnl_irq_map_info *)msg;
1884 valid_len += (vimi->num_vectors *
1885 sizeof(struct i40e_virtchnl_vector_map));
1886 if (vimi->num_vectors == 0)
1887 err_msg_format = true;
1888 }
1889 break;
1890 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1891 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1892 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1893 break;
1894 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1895 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1896 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1897 if (msglen >= valid_len) {
1898 struct i40e_virtchnl_ether_addr_list *veal =
1899 (struct i40e_virtchnl_ether_addr_list *)msg;
1900 valid_len += veal->num_elements *
1901 sizeof(struct i40e_virtchnl_ether_addr);
1902 if (veal->num_elements == 0)
1903 err_msg_format = true;
1904 }
1905 break;
1906 case I40E_VIRTCHNL_OP_ADD_VLAN:
1907 case I40E_VIRTCHNL_OP_DEL_VLAN:
1908 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1909 if (msglen >= valid_len) {
1910 struct i40e_virtchnl_vlan_filter_list *vfl =
1911 (struct i40e_virtchnl_vlan_filter_list *)msg;
1912 valid_len += vfl->num_elements * sizeof(u16);
1913 if (vfl->num_elements == 0)
1914 err_msg_format = true;
1915 }
1916 break;
1917 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1918 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1919 break;
1920 case I40E_VIRTCHNL_OP_GET_STATS:
1921 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1922 break;
1923 /* These are always errors coming from the VF. */
1924 case I40E_VIRTCHNL_OP_EVENT:
1925 case I40E_VIRTCHNL_OP_UNKNOWN:
1926 default:
1927 return -EPERM;
5c3c48ac
JB
1928 }
1929 /* few more checks */
1930 if ((valid_len != msglen) || (err_msg_format)) {
1931 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1932 return -EINVAL;
1933 } else {
1934 return 0;
1935 }
1936}
1937
1938/**
1939 * i40e_vc_process_vf_msg
b40c82e6
JK
1940 * @pf: pointer to the PF structure
1941 * @vf_id: source VF id
5c3c48ac
JB
1942 * @msg: pointer to the msg buffer
1943 * @msglen: msg length
1944 * @msghndl: msg handle
1945 *
1946 * called from the common aeq/arq handler to
b40c82e6 1947 * process request from VF
5c3c48ac
JB
1948 **/
1949int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1950 u32 v_retval, u8 *msg, u16 msglen)
1951{
5c3c48ac 1952 struct i40e_hw *hw = &pf->hw;
c243e963 1953 unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
6c1b5bff 1954 struct i40e_vf *vf;
5c3c48ac
JB
1955 int ret;
1956
1957 pf->vf_aq_requests++;
7efa84b7 1958 if (local_vf_id >= pf->num_alloc_vfs)
6c1b5bff 1959 return -EINVAL;
7efa84b7 1960 vf = &(pf->vf[local_vf_id]);
5c3c48ac
JB
1961 /* perform basic checks on the msg */
1962 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1963
1964 if (ret) {
b40c82e6 1965 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
7efa84b7 1966 local_vf_id, v_opcode, msglen);
5c3c48ac
JB
1967 return ret;
1968 }
bae3cae4 1969
5c3c48ac
JB
1970 switch (v_opcode) {
1971 case I40E_VIRTCHNL_OP_VERSION:
f4ca1a22 1972 ret = i40e_vc_get_version_msg(vf, msg);
5c3c48ac
JB
1973 break;
1974 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
f4ca1a22 1975 ret = i40e_vc_get_vf_resources_msg(vf, msg);
5c3c48ac
JB
1976 break;
1977 case I40E_VIRTCHNL_OP_RESET_VF:
fc18eaa0
MW
1978 i40e_vc_reset_vf_msg(vf);
1979 ret = 0;
5c3c48ac
JB
1980 break;
1981 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1982 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1983 break;
1984 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1985 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1986 break;
1987 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1988 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1989 break;
1990 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1991 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
055b295d 1992 i40e_vc_notify_vf_link_state(vf);
5c3c48ac
JB
1993 break;
1994 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1995 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1996 break;
1997 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1998 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1999 break;
2000 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
2001 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
2002 break;
2003 case I40E_VIRTCHNL_OP_ADD_VLAN:
2004 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
2005 break;
2006 case I40E_VIRTCHNL_OP_DEL_VLAN:
2007 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
2008 break;
2009 case I40E_VIRTCHNL_OP_GET_STATS:
2010 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
2011 break;
5c3c48ac
JB
2012 case I40E_VIRTCHNL_OP_UNKNOWN:
2013 default:
b40c82e6 2014 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
7efa84b7 2015 v_opcode, local_vf_id);
5c3c48ac
JB
2016 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
2017 I40E_ERR_NOT_IMPLEMENTED);
2018 break;
2019 }
2020
2021 return ret;
2022}
2023
2024/**
2025 * i40e_vc_process_vflr_event
b40c82e6 2026 * @pf: pointer to the PF structure
5c3c48ac
JB
2027 *
2028 * called from the vlfr irq handler to
b40c82e6 2029 * free up VF resources and state variables
5c3c48ac
JB
2030 **/
2031int i40e_vc_process_vflr_event(struct i40e_pf *pf)
2032{
2033 u32 reg, reg_idx, bit_idx, vf_id;
2034 struct i40e_hw *hw = &pf->hw;
2035 struct i40e_vf *vf;
2036
2037 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
2038 return 0;
2039
c5c2f7c3
MW
2040 /* re-enable vflr interrupt cause */
2041 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
2042 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
2043 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
2044 i40e_flush(hw);
2045
5c3c48ac
JB
2046 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2047 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
2048 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
2049 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
b40c82e6 2050 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
5c3c48ac
JB
2051 vf = &pf->vf[vf_id];
2052 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
41a1d04b 2053 if (reg & BIT(bit_idx)) {
5c3c48ac 2054 /* clear the bit in GLGEN_VFLRSTAT */
41a1d04b 2055 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
5c3c48ac 2056
eb2d80bc
MW
2057 if (!test_bit(__I40E_DOWN, &pf->state))
2058 i40e_reset_vf(vf, true);
5c3c48ac
JB
2059 }
2060 }
2061
5c3c48ac
JB
2062 return 0;
2063}
2064
5c3c48ac
JB
2065/**
2066 * i40e_ndo_set_vf_mac
2067 * @netdev: network interface device structure
b40c82e6 2068 * @vf_id: VF identifier
5c3c48ac
JB
2069 * @mac: mac address
2070 *
b40c82e6 2071 * program VF mac address
5c3c48ac
JB
2072 **/
2073int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2074{
2075 struct i40e_netdev_priv *np = netdev_priv(netdev);
2076 struct i40e_vsi *vsi = np->vsi;
2077 struct i40e_pf *pf = vsi->back;
2078 struct i40e_mac_filter *f;
2079 struct i40e_vf *vf;
2080 int ret = 0;
2081
2082 /* validate the request */
2083 if (vf_id >= pf->num_alloc_vfs) {
2084 dev_err(&pf->pdev->dev,
2085 "Invalid VF Identifier %d\n", vf_id);
2086 ret = -EINVAL;
2087 goto error_param;
2088 }
2089
2090 vf = &(pf->vf[vf_id]);
fdf0e0bf 2091 vsi = pf->vsi[vf->lan_vsi_idx];
5c3c48ac 2092 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2d166c30
MW
2093 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2094 vf_id);
2095 ret = -EAGAIN;
5c3c48ac
JB
2096 goto error_param;
2097 }
2098
efd8e39a 2099 if (is_multicast_ether_addr(mac)) {
5c3c48ac 2100 dev_err(&pf->pdev->dev,
efd8e39a 2101 "Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
5c3c48ac
JB
2102 ret = -EINVAL;
2103 goto error_param;
2104 }
2105
21659035
KP
2106 /* Lock once because below invoked function add/del_filter requires
2107 * mac_filter_list_lock to be held
2108 */
2109 spin_lock_bh(&vsi->mac_filter_list_lock);
2110
5c3c48ac 2111 /* delete the temporary mac address */
efd8e39a
MW
2112 if (!is_zero_ether_addr(vf->default_lan_addr.addr))
2113 i40e_del_filter(vsi, vf->default_lan_addr.addr,
2114 vf->port_vlan_id ? vf->port_vlan_id : -1,
2115 true, false);
5c3c48ac 2116
29f71bb0
GR
2117 /* Delete all the filters for this VSI - we're going to kill it
2118 * anyway.
2119 */
2120 list_for_each_entry(f, &vsi->mac_filter_list, list)
2121 i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
5c3c48ac 2122
21659035
KP
2123 spin_unlock_bh(&vsi->mac_filter_list_lock);
2124
5c3c48ac
JB
2125 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2126 /* program mac filter */
17652c63 2127 if (i40e_sync_vsi_filters(vsi)) {
5c3c48ac
JB
2128 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2129 ret = -EIO;
2130 goto error_param;
2131 }
9a173901 2132 ether_addr_copy(vf->default_lan_addr.addr, mac);
f657a6e1 2133 vf->pf_set_mac = true;
17413a80
GR
2134 /* Force the VF driver stop so it has to reload with new MAC address */
2135 i40e_vc_disable_vf(pf, vf);
5c3c48ac 2136 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
5c3c48ac
JB
2137
2138error_param:
2139 return ret;
2140}
2141
2142/**
2143 * i40e_ndo_set_vf_port_vlan
2144 * @netdev: network interface device structure
b40c82e6 2145 * @vf_id: VF identifier
5c3c48ac
JB
2146 * @vlan_id: mac address
2147 * @qos: priority setting
2148 *
b40c82e6 2149 * program VF vlan id and/or qos
5c3c48ac
JB
2150 **/
2151int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2152 int vf_id, u16 vlan_id, u8 qos)
2153{
f7fc2f2e 2154 u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
5c3c48ac
JB
2155 struct i40e_netdev_priv *np = netdev_priv(netdev);
2156 struct i40e_pf *pf = np->vsi->back;
21659035 2157 bool is_vsi_in_vlan = false;
5c3c48ac
JB
2158 struct i40e_vsi *vsi;
2159 struct i40e_vf *vf;
2160 int ret = 0;
2161
2162 /* validate the request */
2163 if (vf_id >= pf->num_alloc_vfs) {
2164 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2165 ret = -EINVAL;
2166 goto error_pvid;
2167 }
2168
2169 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2170 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2171 ret = -EINVAL;
2172 goto error_pvid;
2173 }
2174
2175 vf = &(pf->vf[vf_id]);
fdf0e0bf 2176 vsi = pf->vsi[vf->lan_vsi_idx];
5c3c48ac 2177 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2d166c30
MW
2178 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2179 vf_id);
2180 ret = -EAGAIN;
5c3c48ac
JB
2181 goto error_pvid;
2182 }
2183
f7fc2f2e 2184 if (le16_to_cpu(vsi->info.pvid) == vlanprio)
85927ec1
MW
2185 /* duplicate request, so just return success */
2186 goto error_pvid;
2187
21659035
KP
2188 spin_lock_bh(&vsi->mac_filter_list_lock);
2189 is_vsi_in_vlan = i40e_is_vsi_in_vlan(vsi);
2190 spin_unlock_bh(&vsi->mac_filter_list_lock);
2191
2192 if (le16_to_cpu(vsi->info.pvid) == 0 && is_vsi_in_vlan) {
99a4973c
GR
2193 dev_err(&pf->pdev->dev,
2194 "VF %d has already configured VLAN filters and the administrator is requesting a port VLAN override.\nPlease unload and reload the VF driver for this change to take effect.\n",
2195 vf_id);
f9b4b627
GR
2196 /* Administrator Error - knock the VF offline until he does
2197 * the right thing by reconfiguring his network correctly
2198 * and then reloading the VF driver.
2199 */
2200 i40e_vc_disable_vf(pf, vf);
2201 }
99a4973c 2202
8d82a7c5
GR
2203 /* Check for condition where there was already a port VLAN ID
2204 * filter set and now it is being deleted by setting it to zero.
1315f7c3
GR
2205 * Additionally check for the condition where there was a port
2206 * VLAN but now there is a new and different port VLAN being set.
8d82a7c5
GR
2207 * Before deleting all the old VLAN filters we must add new ones
2208 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2209 * MAC addresses deleted.
2210 */
1315f7c3 2211 if ((!(vlan_id || qos) ||
f7fc2f2e 2212 vlanprio != le16_to_cpu(vsi->info.pvid)) &&
1315f7c3 2213 vsi->info.pvid)
8d82a7c5
GR
2214 ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2215
5c3c48ac
JB
2216 if (vsi->info.pvid) {
2217 /* kill old VLAN */
2218 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2219 VLAN_VID_MASK));
2220 if (ret) {
2221 dev_info(&vsi->back->pdev->dev,
2222 "remove VLAN failed, ret=%d, aq_err=%d\n",
2223 ret, pf->hw.aq.asq_last_status);
2224 }
2225 }
2226 if (vlan_id || qos)
f7fc2f2e 2227 ret = i40e_vsi_add_pvid(vsi, vlanprio);
5c3c48ac 2228 else
6c12fcbf 2229 i40e_vsi_remove_pvid(vsi);
5c3c48ac
JB
2230
2231 if (vlan_id) {
2232 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2233 vlan_id, qos, vf_id);
2234
2235 /* add new VLAN filter */
2236 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2237 if (ret) {
2238 dev_info(&vsi->back->pdev->dev,
2239 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2240 vsi->back->hw.aq.asq_last_status);
2241 goto error_pvid;
2242 }
8d82a7c5
GR
2243 /* Kill non-vlan MAC filters - ignore error return since
2244 * there might not be any non-vlan MAC filters.
2245 */
2246 i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
5c3c48ac
JB
2247 }
2248
2249 if (ret) {
2250 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2251 goto error_pvid;
2252 }
6c12fcbf
GR
2253 /* The Port VLAN needs to be saved across resets the same as the
2254 * default LAN MAC address.
2255 */
2256 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
5c3c48ac
JB
2257 ret = 0;
2258
2259error_pvid:
2260 return ret;
2261}
2262
84590fd9
MW
2263#define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
2264#define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
5c3c48ac
JB
2265/**
2266 * i40e_ndo_set_vf_bw
2267 * @netdev: network interface device structure
b40c82e6
JK
2268 * @vf_id: VF identifier
2269 * @tx_rate: Tx rate
5c3c48ac 2270 *
b40c82e6 2271 * configure VF Tx rate
5c3c48ac 2272 **/
ed616689
SC
2273int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2274 int max_tx_rate)
5c3c48ac 2275{
6b192891
MW
2276 struct i40e_netdev_priv *np = netdev_priv(netdev);
2277 struct i40e_pf *pf = np->vsi->back;
2278 struct i40e_vsi *vsi;
2279 struct i40e_vf *vf;
2280 int speed = 0;
2281 int ret = 0;
2282
2283 /* validate the request */
2284 if (vf_id >= pf->num_alloc_vfs) {
2285 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2286 ret = -EINVAL;
2287 goto error;
2288 }
2289
ed616689 2290 if (min_tx_rate) {
b40c82e6 2291 dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
ed616689
SC
2292 min_tx_rate, vf_id);
2293 return -EINVAL;
2294 }
2295
6b192891 2296 vf = &(pf->vf[vf_id]);
fdf0e0bf 2297 vsi = pf->vsi[vf->lan_vsi_idx];
6b192891 2298 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2d166c30
MW
2299 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2300 vf_id);
2301 ret = -EAGAIN;
6b192891
MW
2302 goto error;
2303 }
2304
2305 switch (pf->hw.phy.link_info.link_speed) {
2306 case I40E_LINK_SPEED_40GB:
2307 speed = 40000;
2308 break;
07f169c3
MW
2309 case I40E_LINK_SPEED_20GB:
2310 speed = 20000;
2311 break;
6b192891
MW
2312 case I40E_LINK_SPEED_10GB:
2313 speed = 10000;
2314 break;
2315 case I40E_LINK_SPEED_1GB:
2316 speed = 1000;
2317 break;
2318 default:
2319 break;
2320 }
2321
ed616689 2322 if (max_tx_rate > speed) {
b40c82e6 2323 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
ed616689 2324 max_tx_rate, vf->vf_id);
6b192891
MW
2325 ret = -EINVAL;
2326 goto error;
2327 }
2328
dac9b31a
MW
2329 if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2330 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2331 max_tx_rate = 50;
2332 }
2333
6b192891 2334 /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
84590fd9
MW
2335 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2336 max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2337 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
6b192891 2338 if (ret) {
ed616689 2339 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
6b192891
MW
2340 ret);
2341 ret = -EIO;
2342 goto error;
2343 }
ed616689 2344 vf->tx_rate = max_tx_rate;
6b192891
MW
2345error:
2346 return ret;
5c3c48ac
JB
2347}
2348
2349/**
2350 * i40e_ndo_get_vf_config
2351 * @netdev: network interface device structure
b40c82e6
JK
2352 * @vf_id: VF identifier
2353 * @ivi: VF configuration structure
5c3c48ac 2354 *
b40c82e6 2355 * return VF configuration
5c3c48ac
JB
2356 **/
2357int i40e_ndo_get_vf_config(struct net_device *netdev,
2358 int vf_id, struct ifla_vf_info *ivi)
2359{
2360 struct i40e_netdev_priv *np = netdev_priv(netdev);
5c3c48ac
JB
2361 struct i40e_vsi *vsi = np->vsi;
2362 struct i40e_pf *pf = vsi->back;
2363 struct i40e_vf *vf;
2364 int ret = 0;
2365
2366 /* validate the request */
2367 if (vf_id >= pf->num_alloc_vfs) {
2368 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2369 ret = -EINVAL;
2370 goto error_param;
2371 }
2372
2373 vf = &(pf->vf[vf_id]);
2374 /* first vsi is always the LAN vsi */
fdf0e0bf 2375 vsi = pf->vsi[vf->lan_vsi_idx];
5c3c48ac 2376 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2d166c30
MW
2377 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2378 vf_id);
2379 ret = -EAGAIN;
5c3c48ac
JB
2380 goto error_param;
2381 }
2382
2383 ivi->vf = vf_id;
2384
6995b36c 2385 ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
5c3c48ac 2386
ed616689
SC
2387 ivi->max_tx_rate = vf->tx_rate;
2388 ivi->min_tx_rate = 0;
5c3c48ac
JB
2389 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2390 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2391 I40E_VLAN_PRIORITY_SHIFT;
84ca55a0
MW
2392 if (vf->link_forced == false)
2393 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2394 else if (vf->link_up == true)
2395 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2396 else
2397 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
c674d125 2398 ivi->spoofchk = vf->spoofchk;
5c3c48ac
JB
2399 ret = 0;
2400
2401error_param:
2402 return ret;
2403}
588aefa0
MW
2404
2405/**
2406 * i40e_ndo_set_vf_link_state
2407 * @netdev: network interface device structure
b40c82e6 2408 * @vf_id: VF identifier
588aefa0
MW
2409 * @link: required link state
2410 *
2411 * Set the link state of a specified VF, regardless of physical link state
2412 **/
2413int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
2414{
2415 struct i40e_netdev_priv *np = netdev_priv(netdev);
2416 struct i40e_pf *pf = np->vsi->back;
2417 struct i40e_virtchnl_pf_event pfe;
2418 struct i40e_hw *hw = &pf->hw;
2419 struct i40e_vf *vf;
f19efbb5 2420 int abs_vf_id;
588aefa0
MW
2421 int ret = 0;
2422
2423 /* validate the request */
2424 if (vf_id >= pf->num_alloc_vfs) {
2425 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2426 ret = -EINVAL;
2427 goto error_out;
2428 }
2429
2430 vf = &pf->vf[vf_id];
f19efbb5 2431 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
588aefa0
MW
2432
2433 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
2434 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
2435
2436 switch (link) {
2437 case IFLA_VF_LINK_STATE_AUTO:
2438 vf->link_forced = false;
2439 pfe.event_data.link_event.link_status =
2440 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
2441 pfe.event_data.link_event.link_speed =
2442 pf->hw.phy.link_info.link_speed;
2443 break;
2444 case IFLA_VF_LINK_STATE_ENABLE:
2445 vf->link_forced = true;
2446 vf->link_up = true;
2447 pfe.event_data.link_event.link_status = true;
2448 pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
2449 break;
2450 case IFLA_VF_LINK_STATE_DISABLE:
2451 vf->link_forced = true;
2452 vf->link_up = false;
2453 pfe.event_data.link_event.link_status = false;
2454 pfe.event_data.link_event.link_speed = 0;
2455 break;
2456 default:
2457 ret = -EINVAL;
2458 goto error_out;
2459 }
2460 /* Notify the VF of its new link state */
f19efbb5 2461 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
588aefa0
MW
2462 0, (u8 *)&pfe, sizeof(pfe), NULL);
2463
2464error_out:
2465 return ret;
2466}
c674d125
MW
2467
2468/**
2469 * i40e_ndo_set_vf_spoofchk
2470 * @netdev: network interface device structure
b40c82e6 2471 * @vf_id: VF identifier
c674d125
MW
2472 * @enable: flag to enable or disable feature
2473 *
2474 * Enable or disable VF spoof checking
2475 **/
e6d9004d 2476int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
c674d125
MW
2477{
2478 struct i40e_netdev_priv *np = netdev_priv(netdev);
2479 struct i40e_vsi *vsi = np->vsi;
2480 struct i40e_pf *pf = vsi->back;
2481 struct i40e_vsi_context ctxt;
2482 struct i40e_hw *hw = &pf->hw;
2483 struct i40e_vf *vf;
2484 int ret = 0;
2485
2486 /* validate the request */
2487 if (vf_id >= pf->num_alloc_vfs) {
2488 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2489 ret = -EINVAL;
2490 goto out;
2491 }
2492
2493 vf = &(pf->vf[vf_id]);
2d166c30
MW
2494 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2495 dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2496 vf_id);
2497 ret = -EAGAIN;
2498 goto out;
2499 }
c674d125
MW
2500
2501 if (enable == vf->spoofchk)
2502 goto out;
2503
2504 vf->spoofchk = enable;
2505 memset(&ctxt, 0, sizeof(ctxt));
fdf0e0bf 2506 ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
c674d125
MW
2507 ctxt.pf_num = pf->hw.pf_id;
2508 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
2509 if (enable)
30d71af5
GR
2510 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
2511 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
c674d125
MW
2512 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
2513 if (ret) {
2514 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
2515 ret);
2516 ret = -EIO;
2517 }
2518out:
2519 return ret;
2520}
This page took 0.355975 seconds and 5 git commands to generate.