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