i40e: un-disable VF after reset
[linux-2.6/btrfs-unstable.git] / drivers / net / ethernet / intel / i40e / i40e_virtchnl_pf.c
blobfdd7f5e3a66b11c672ef3f8ebbf293d4fb7e56b7
1 /*******************************************************************************
3 * Intel Ethernet Controller XL710 Family Linux Driver
4 * Copyright(c) 2013 - 2015 Intel Corporation.
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.
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.
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/>.
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
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
25 ******************************************************************************/
27 #include "i40e.h"
29 /*********************notification routines***********************/
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
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)
46 struct i40e_hw *hw = &pf->hw;
47 struct i40e_vf *vf = pf->vf;
48 int i;
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;
57 /* Ignore return value on purpose - a given VF may fail, but
58 * we need to keep going and send to all of them
60 i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
61 msg, msglen, NULL);
65 /**
66 * i40e_vc_notify_link_state
67 * @vf: pointer to the VF structure
69 * send a link status message to a single VF
70 **/
71 static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
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;
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;
90 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
91 0, (u8 *)&pfe, sizeof(pfe), NULL);
94 /**
95 * i40e_vc_notify_link_state
96 * @pf: pointer to the PF structure
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)
102 int i;
104 for (i = 0; i < pf->num_alloc_vfs; i++)
105 i40e_vc_notify_vf_link_state(&pf->vf[i]);
109 * i40e_vc_notify_reset
110 * @pf: pointer to the PF structure
112 * indicate a pending reset to all VFs on a given PF
114 void i40e_vc_notify_reset(struct i40e_pf *pf)
116 struct i40e_virtchnl_pf_event pfe;
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));
125 * i40e_vc_notify_vf_reset
126 * @vf: pointer to the VF structure
128 * indicate a pending reset to the given VF
130 void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
132 struct i40e_virtchnl_pf_event pfe;
133 int abs_vf_id;
135 /* validate the request */
136 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
137 return;
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;
144 abs_vf_id = vf->vf_id + vf->pf->hw.func_caps.vf_base_id;
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);
152 /***********************misc routines*****************************/
155 * i40e_vc_disable_vf
156 * @pf: pointer to the PF info
157 * @vf: pointer to the VF info
159 * Disable the VF through a SW reset
161 static inline void i40e_vc_disable_vf(struct i40e_pf *pf, struct i40e_vf *vf)
163 i40e_vc_notify_vf_reset(vf);
164 i40e_reset_vf(vf, false);
168 * i40e_vc_isvalid_vsi_id
169 * @vf: pointer to the VF info
170 * @vsi_id: VF relative VSI id
172 * check for the valid VSI id
174 static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
176 struct i40e_pf *pf = vf->pf;
177 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
179 return (vsi && (vsi->vf_id == vf->vf_id));
183 * i40e_vc_isvalid_queue_id
184 * @vf: pointer to the VF info
185 * @vsi_id: vsi id
186 * @qid: vsi relative queue id
188 * check for the valid queue id
190 static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
191 u8 qid)
193 struct i40e_pf *pf = vf->pf;
194 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
196 return (vsi && (qid < vsi->alloc_queue_pairs));
200 * i40e_vc_isvalid_vector_id
201 * @vf: pointer to the VF info
202 * @vector_id: VF relative vector id
204 * check for the valid vector id
206 static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
208 struct i40e_pf *pf = vf->pf;
210 return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
213 /***********************vf resource mgmt routines*****************/
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
221 * return PF relative queue id
223 static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
224 u8 vsi_queue_id)
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;
230 if (!vsi)
231 return pf_queue_id;
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;
241 return pf_queue_id;
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
250 * configure irq link list from the map
252 static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
253 struct i40e_virtchnl_vector_map *vecmap)
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;
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));
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;
278 tempmap = vecmap->rxq_map;
279 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
280 linklistmap |= (1 <<
281 (I40E_VIRTCHNL_SUPPORTED_QTYPES *
282 vsi_queue_id));
285 tempmap = vecmap->txq_map;
286 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
287 linklistmap |= (1 <<
288 (I40E_VIRTCHNL_SUPPORTED_QTYPES * vsi_queue_id
289 + 1));
292 next_q = find_first_bit(&linklistmap,
293 (I40E_MAX_VSI_QP *
294 I40E_VIRTCHNL_SUPPORTED_QTYPES));
295 vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
296 qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
297 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
298 reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
300 wr32(hw, reg_idx, reg);
302 while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
303 switch (qtype) {
304 case I40E_QUEUE_TYPE_RX:
305 reg_idx = I40E_QINT_RQCTL(pf_queue_id);
306 itr_idx = vecmap->rxitr_idx;
307 break;
308 case I40E_QUEUE_TYPE_TX:
309 reg_idx = I40E_QINT_TQCTL(pf_queue_id);
310 itr_idx = vecmap->txitr_idx;
311 break;
312 default:
313 break;
316 next_q = find_next_bit(&linklistmap,
317 (I40E_MAX_VSI_QP *
318 I40E_VIRTCHNL_SUPPORTED_QTYPES),
319 next_q + 1);
320 if (next_q <
321 (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
322 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
323 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
324 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
325 vsi_queue_id);
326 } else {
327 pf_queue_id = I40E_QUEUE_END_OF_LIST;
328 qtype = 0;
331 /* format for the RQCTL & TQCTL regs is same */
332 reg = (vector_id) |
333 (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
334 (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
335 (1 << I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
336 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
337 wr32(hw, reg_idx, reg);
340 irq_list_done:
341 i40e_flush(hw);
345 * i40e_config_vsi_tx_queue
346 * @vf: pointer to the VF info
347 * @vsi_id: id of VSI as provided by the FW
348 * @vsi_queue_id: vsi relative queue index
349 * @info: config. info
351 * configure tx queue
353 static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
354 u16 vsi_queue_id,
355 struct i40e_virtchnl_txq_info *info)
357 struct i40e_pf *pf = vf->pf;
358 struct i40e_hw *hw = &pf->hw;
359 struct i40e_hmc_obj_txq tx_ctx;
360 struct i40e_vsi *vsi;
361 u16 pf_queue_id;
362 u32 qtx_ctl;
363 int ret = 0;
365 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
366 vsi = i40e_find_vsi_from_id(pf, vsi_id);
368 /* clear the context structure first */
369 memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
371 /* only set the required fields */
372 tx_ctx.base = info->dma_ring_addr / 128;
373 tx_ctx.qlen = info->ring_len;
374 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
375 tx_ctx.rdylist_act = 0;
376 tx_ctx.head_wb_ena = info->headwb_enabled;
377 tx_ctx.head_wb_addr = info->dma_headwb_addr;
379 /* clear the context in the HMC */
380 ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
381 if (ret) {
382 dev_err(&pf->pdev->dev,
383 "Failed to clear VF LAN Tx queue context %d, error: %d\n",
384 pf_queue_id, ret);
385 ret = -ENOENT;
386 goto error_context;
389 /* set the context in the HMC */
390 ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
391 if (ret) {
392 dev_err(&pf->pdev->dev,
393 "Failed to set VF LAN Tx queue context %d error: %d\n",
394 pf_queue_id, ret);
395 ret = -ENOENT;
396 goto error_context;
399 /* associate this queue with the PCI VF function */
400 qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
401 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
402 & I40E_QTX_CTL_PF_INDX_MASK);
403 qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
404 << I40E_QTX_CTL_VFVM_INDX_SHIFT)
405 & I40E_QTX_CTL_VFVM_INDX_MASK);
406 wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
407 i40e_flush(hw);
409 error_context:
410 return ret;
414 * i40e_config_vsi_rx_queue
415 * @vf: pointer to the VF info
416 * @vsi_id: id of VSI as provided by the FW
417 * @vsi_queue_id: vsi relative queue index
418 * @info: config. info
420 * configure rx queue
422 static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
423 u16 vsi_queue_id,
424 struct i40e_virtchnl_rxq_info *info)
426 struct i40e_pf *pf = vf->pf;
427 struct i40e_hw *hw = &pf->hw;
428 struct i40e_hmc_obj_rxq rx_ctx;
429 u16 pf_queue_id;
430 int ret = 0;
432 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
434 /* clear the context structure first */
435 memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
437 /* only set the required fields */
438 rx_ctx.base = info->dma_ring_addr / 128;
439 rx_ctx.qlen = info->ring_len;
441 if (info->splithdr_enabled) {
442 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
443 I40E_RX_SPLIT_IP |
444 I40E_RX_SPLIT_TCP_UDP |
445 I40E_RX_SPLIT_SCTP;
446 /* header length validation */
447 if (info->hdr_size > ((2 * 1024) - 64)) {
448 ret = -EINVAL;
449 goto error_param;
451 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
453 /* set splitalways mode 10b */
454 rx_ctx.dtype = 0x2;
457 /* databuffer length validation */
458 if (info->databuffer_size > ((16 * 1024) - 128)) {
459 ret = -EINVAL;
460 goto error_param;
462 rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
464 /* max pkt. length validation */
465 if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
466 ret = -EINVAL;
467 goto error_param;
469 rx_ctx.rxmax = info->max_pkt_size;
471 /* enable 32bytes desc always */
472 rx_ctx.dsize = 1;
474 /* default values */
475 rx_ctx.lrxqthresh = 2;
476 rx_ctx.crcstrip = 1;
477 rx_ctx.prefena = 1;
478 rx_ctx.l2tsel = 1;
480 /* clear the context in the HMC */
481 ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
482 if (ret) {
483 dev_err(&pf->pdev->dev,
484 "Failed to clear VF LAN Rx queue context %d, error: %d\n",
485 pf_queue_id, ret);
486 ret = -ENOENT;
487 goto error_param;
490 /* set the context in the HMC */
491 ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
492 if (ret) {
493 dev_err(&pf->pdev->dev,
494 "Failed to set VF LAN Rx queue context %d error: %d\n",
495 pf_queue_id, ret);
496 ret = -ENOENT;
497 goto error_param;
500 error_param:
501 return ret;
505 * i40e_alloc_vsi_res
506 * @vf: pointer to the VF info
507 * @type: type of VSI to allocate
509 * alloc VF vsi context & resources
511 static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
513 struct i40e_mac_filter *f = NULL;
514 struct i40e_pf *pf = vf->pf;
515 struct i40e_vsi *vsi;
516 int ret = 0;
518 vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
520 if (!vsi) {
521 dev_err(&pf->pdev->dev,
522 "add vsi failed for VF %d, aq_err %d\n",
523 vf->vf_id, pf->hw.aq.asq_last_status);
524 ret = -ENOENT;
525 goto error_alloc_vsi_res;
527 if (type == I40E_VSI_SRIOV) {
528 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
529 vf->lan_vsi_idx = vsi->idx;
530 vf->lan_vsi_id = vsi->id;
531 /* If the port VLAN has been configured and then the
532 * VF driver was removed then the VSI port VLAN
533 * configuration was destroyed. Check if there is
534 * a port VLAN and restore the VSI configuration if
535 * needed.
537 if (vf->port_vlan_id)
538 i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
539 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
540 vf->port_vlan_id ? vf->port_vlan_id : -1,
541 true, false);
542 if (!f)
543 dev_info(&pf->pdev->dev,
544 "Could not allocate VF MAC addr\n");
545 f = i40e_add_filter(vsi, brdcast,
546 vf->port_vlan_id ? vf->port_vlan_id : -1,
547 true, false);
548 if (!f)
549 dev_info(&pf->pdev->dev,
550 "Could not allocate VF broadcast filter\n");
553 /* program mac filter */
554 ret = i40e_sync_vsi_filters(vsi);
555 if (ret)
556 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
558 /* Set VF bandwidth if specified */
559 if (vf->tx_rate) {
560 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
561 vf->tx_rate / 50, 0, NULL);
562 if (ret)
563 dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
564 vf->vf_id, ret);
567 error_alloc_vsi_res:
568 return ret;
572 * i40e_enable_vf_mappings
573 * @vf: pointer to the VF info
575 * enable VF mappings
577 static void i40e_enable_vf_mappings(struct i40e_vf *vf)
579 struct i40e_pf *pf = vf->pf;
580 struct i40e_hw *hw = &pf->hw;
581 u32 reg, total_queue_pairs = 0;
582 int j;
584 /* Tell the hardware we're using noncontiguous mapping. HW requires
585 * that VF queues be mapped using this method, even when they are
586 * contiguous in real life
588 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
589 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
591 /* enable VF vplan_qtable mappings */
592 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
593 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
595 /* map PF queues to VF queues */
596 for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
597 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
598 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
599 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
600 total_queue_pairs++;
603 /* map PF queues to VSI */
604 for (j = 0; j < 7; j++) {
605 if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
606 reg = 0x07FF07FF; /* unused */
607 } else {
608 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
609 j * 2);
610 reg = qid;
611 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
612 (j * 2) + 1);
613 reg |= qid << 16;
615 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
618 i40e_flush(hw);
622 * i40e_disable_vf_mappings
623 * @vf: pointer to the VF info
625 * disable VF mappings
627 static void i40e_disable_vf_mappings(struct i40e_vf *vf)
629 struct i40e_pf *pf = vf->pf;
630 struct i40e_hw *hw = &pf->hw;
631 int i;
633 /* disable qp mappings */
634 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
635 for (i = 0; i < I40E_MAX_VSI_QP; i++)
636 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
637 I40E_QUEUE_END_OF_LIST);
638 i40e_flush(hw);
642 * i40e_free_vf_res
643 * @vf: pointer to the VF info
645 * free VF resources
647 static void i40e_free_vf_res(struct i40e_vf *vf)
649 struct i40e_pf *pf = vf->pf;
650 struct i40e_hw *hw = &pf->hw;
651 u32 reg_idx, reg;
652 int i, msix_vf;
654 /* free vsi & disconnect it from the parent uplink */
655 if (vf->lan_vsi_idx) {
656 i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
657 vf->lan_vsi_idx = 0;
658 vf->lan_vsi_id = 0;
660 msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
662 /* disable interrupts so the VF starts in a known state */
663 for (i = 0; i < msix_vf; i++) {
664 /* format is same for both registers */
665 if (0 == i)
666 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
667 else
668 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
669 (vf->vf_id))
670 + (i - 1));
671 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
672 i40e_flush(hw);
675 /* clear the irq settings */
676 for (i = 0; i < msix_vf; i++) {
677 /* format is same for both registers */
678 if (0 == i)
679 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
680 else
681 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
682 (vf->vf_id))
683 + (i - 1));
684 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
685 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
686 wr32(hw, reg_idx, reg);
687 i40e_flush(hw);
689 /* reset some of the state varibles keeping
690 * track of the resources
692 vf->num_queue_pairs = 0;
693 vf->vf_states = 0;
697 * i40e_alloc_vf_res
698 * @vf: pointer to the VF info
700 * allocate VF resources
702 static int i40e_alloc_vf_res(struct i40e_vf *vf)
704 struct i40e_pf *pf = vf->pf;
705 int total_queue_pairs = 0;
706 int ret;
708 /* allocate hw vsi context & associated resources */
709 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
710 if (ret)
711 goto error_alloc;
712 total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
713 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
715 /* store the total qps number for the runtime
716 * VF req validation
718 vf->num_queue_pairs = total_queue_pairs;
720 /* VF is now completely initialized */
721 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
723 error_alloc:
724 if (ret)
725 i40e_free_vf_res(vf);
727 return ret;
730 #define VF_DEVICE_STATUS 0xAA
731 #define VF_TRANS_PENDING_MASK 0x20
733 * i40e_quiesce_vf_pci
734 * @vf: pointer to the VF structure
736 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
737 * if the transactions never clear.
739 static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
741 struct i40e_pf *pf = vf->pf;
742 struct i40e_hw *hw = &pf->hw;
743 int vf_abs_id, i;
744 u32 reg;
746 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
748 wr32(hw, I40E_PF_PCI_CIAA,
749 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
750 for (i = 0; i < 100; i++) {
751 reg = rd32(hw, I40E_PF_PCI_CIAD);
752 if ((reg & VF_TRANS_PENDING_MASK) == 0)
753 return 0;
754 udelay(1);
756 return -EIO;
760 * i40e_reset_vf
761 * @vf: pointer to the VF structure
762 * @flr: VFLR was issued or not
764 * reset the VF
766 void i40e_reset_vf(struct i40e_vf *vf, bool flr)
768 struct i40e_pf *pf = vf->pf;
769 struct i40e_hw *hw = &pf->hw;
770 bool rsd = false;
771 int i;
772 u32 reg;
774 if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
775 return;
777 /* warn the VF */
778 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
780 /* In the case of a VFLR, the HW has already reset the VF and we
781 * just need to clean up, so don't hit the VFRTRIG register.
783 if (!flr) {
784 /* reset VF using VPGEN_VFRTRIG reg */
785 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
786 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
787 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
788 i40e_flush(hw);
791 if (i40e_quiesce_vf_pci(vf))
792 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
793 vf->vf_id);
795 /* poll VPGEN_VFRSTAT reg to make sure
796 * that reset is complete
798 for (i = 0; i < 10; i++) {
799 /* VF reset requires driver to first reset the VF and then
800 * poll the status register to make sure that the reset
801 * completed successfully. Due to internal HW FIFO flushes,
802 * we must wait 10ms before the register will be valid.
804 usleep_range(10000, 20000);
805 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
806 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
807 rsd = true;
808 break;
812 if (flr)
813 usleep_range(10000, 20000);
815 if (!rsd)
816 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
817 vf->vf_id);
818 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
819 /* clear the reset bit in the VPGEN_VFRTRIG reg */
820 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
821 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
822 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
824 /* On initial reset, we won't have any queues */
825 if (vf->lan_vsi_idx == 0)
826 goto complete_reset;
828 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false);
829 complete_reset:
830 /* reallocate VF resources to reset the VSI state */
831 i40e_free_vf_res(vf);
832 i40e_alloc_vf_res(vf);
833 i40e_enable_vf_mappings(vf);
834 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
835 clear_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
837 /* tell the VF the reset is done */
838 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
839 i40e_flush(hw);
840 clear_bit(__I40E_VF_DISABLE, &pf->state);
844 * i40e_free_vfs
845 * @pf: pointer to the PF structure
847 * free VF resources
849 void i40e_free_vfs(struct i40e_pf *pf)
851 struct i40e_hw *hw = &pf->hw;
852 u32 reg_idx, bit_idx;
853 int i, tmp, vf_id;
855 if (!pf->vf)
856 return;
857 while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
858 usleep_range(1000, 2000);
860 for (i = 0; i < pf->num_alloc_vfs; i++)
861 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
862 i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
863 false);
865 /* Disable IOV before freeing resources. This lets any VF drivers
866 * running in the host get themselves cleaned up before we yank
867 * the carpet out from underneath their feet.
869 if (!pci_vfs_assigned(pf->pdev))
870 pci_disable_sriov(pf->pdev);
871 else
872 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
874 msleep(20); /* let any messages in transit get finished up */
876 /* free up VF resources */
877 tmp = pf->num_alloc_vfs;
878 pf->num_alloc_vfs = 0;
879 for (i = 0; i < tmp; i++) {
880 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
881 i40e_free_vf_res(&pf->vf[i]);
882 /* disable qp mappings */
883 i40e_disable_vf_mappings(&pf->vf[i]);
886 kfree(pf->vf);
887 pf->vf = NULL;
889 /* This check is for when the driver is unloaded while VFs are
890 * assigned. Setting the number of VFs to 0 through sysfs is caught
891 * before this function ever gets called.
893 if (!pci_vfs_assigned(pf->pdev)) {
894 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
895 * work correctly when SR-IOV gets re-enabled.
897 for (vf_id = 0; vf_id < tmp; vf_id++) {
898 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
899 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
900 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
903 clear_bit(__I40E_VF_DISABLE, &pf->state);
906 #ifdef CONFIG_PCI_IOV
908 * i40e_alloc_vfs
909 * @pf: pointer to the PF structure
910 * @num_alloc_vfs: number of VFs to allocate
912 * allocate VF resources
914 int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
916 struct i40e_vf *vfs;
917 int i, ret = 0;
919 /* Disable interrupt 0 so we don't try to handle the VFLR. */
920 i40e_irq_dynamic_disable_icr0(pf);
922 /* Check to see if we're just allocating resources for extant VFs */
923 if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
924 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
925 if (ret) {
926 dev_err(&pf->pdev->dev,
927 "Failed to enable SR-IOV, error %d.\n", ret);
928 pf->num_alloc_vfs = 0;
929 goto err_iov;
932 /* allocate memory */
933 vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
934 if (!vfs) {
935 ret = -ENOMEM;
936 goto err_alloc;
938 pf->vf = vfs;
940 /* apply default profile */
941 for (i = 0; i < num_alloc_vfs; i++) {
942 vfs[i].pf = pf;
943 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
944 vfs[i].vf_id = i;
946 /* assign default capabilities */
947 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
948 vfs[i].spoofchk = true;
949 /* VF resources get allocated during reset */
950 i40e_reset_vf(&vfs[i], false);
952 /* enable VF vplan_qtable mappings */
953 i40e_enable_vf_mappings(&vfs[i]);
955 pf->num_alloc_vfs = num_alloc_vfs;
957 err_alloc:
958 if (ret)
959 i40e_free_vfs(pf);
960 err_iov:
961 /* Re-enable interrupt 0. */
962 i40e_irq_dynamic_enable_icr0(pf);
963 return ret;
966 #endif
968 * i40e_pci_sriov_enable
969 * @pdev: pointer to a pci_dev structure
970 * @num_vfs: number of VFs to allocate
972 * Enable or change the number of VFs
974 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
976 #ifdef CONFIG_PCI_IOV
977 struct i40e_pf *pf = pci_get_drvdata(pdev);
978 int pre_existing_vfs = pci_num_vf(pdev);
979 int err = 0;
981 if (pf->state & __I40E_TESTING) {
982 dev_warn(&pdev->dev,
983 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
984 err = -EPERM;
985 goto err_out;
988 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
989 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
990 i40e_free_vfs(pf);
991 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
992 goto out;
994 if (num_vfs > pf->num_req_vfs) {
995 err = -EPERM;
996 goto err_out;
999 err = i40e_alloc_vfs(pf, num_vfs);
1000 if (err) {
1001 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1002 goto err_out;
1005 out:
1006 return num_vfs;
1008 err_out:
1009 return err;
1010 #endif
1011 return 0;
1015 * i40e_pci_sriov_configure
1016 * @pdev: pointer to a pci_dev structure
1017 * @num_vfs: number of VFs to allocate
1019 * Enable or change the number of VFs. Called when the user updates the number
1020 * of VFs in sysfs.
1022 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1024 struct i40e_pf *pf = pci_get_drvdata(pdev);
1026 if (num_vfs) {
1027 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1028 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1029 i40e_do_reset_safe(pf,
1030 BIT_ULL(__I40E_PF_RESET_REQUESTED));
1032 return i40e_pci_sriov_enable(pdev, num_vfs);
1035 if (!pci_vfs_assigned(pf->pdev)) {
1036 i40e_free_vfs(pf);
1037 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1038 i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
1039 } else {
1040 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1041 return -EINVAL;
1043 return 0;
1046 /***********************virtual channel routines******************/
1049 * i40e_vc_send_msg_to_vf
1050 * @vf: pointer to the VF info
1051 * @v_opcode: virtual channel opcode
1052 * @v_retval: virtual channel return value
1053 * @msg: pointer to the msg buffer
1054 * @msglen: msg length
1056 * send msg to VF
1058 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1059 u32 v_retval, u8 *msg, u16 msglen)
1061 struct i40e_pf *pf;
1062 struct i40e_hw *hw;
1063 int abs_vf_id;
1064 i40e_status aq_ret;
1066 /* validate the request */
1067 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1068 return -EINVAL;
1070 pf = vf->pf;
1071 hw = &pf->hw;
1072 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1074 /* single place to detect unsuccessful return values */
1075 if (v_retval) {
1076 vf->num_invalid_msgs++;
1077 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
1078 v_opcode, v_retval);
1079 if (vf->num_invalid_msgs >
1080 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1081 dev_err(&pf->pdev->dev,
1082 "Number of invalid messages exceeded for VF %d\n",
1083 vf->vf_id);
1084 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1085 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1087 } else {
1088 vf->num_valid_msgs++;
1091 aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
1092 msg, msglen, NULL);
1093 if (aq_ret) {
1094 dev_err(&pf->pdev->dev,
1095 "Unable to send the message to VF %d aq_err %d\n",
1096 vf->vf_id, pf->hw.aq.asq_last_status);
1097 return -EIO;
1100 return 0;
1104 * i40e_vc_send_resp_to_vf
1105 * @vf: pointer to the VF info
1106 * @opcode: operation code
1107 * @retval: return value
1109 * send resp msg to VF
1111 static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1112 enum i40e_virtchnl_ops opcode,
1113 i40e_status retval)
1115 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1119 * i40e_vc_get_version_msg
1120 * @vf: pointer to the VF info
1122 * called from the VF to request the API version used by the PF
1124 static int i40e_vc_get_version_msg(struct i40e_vf *vf)
1126 struct i40e_virtchnl_version_info info = {
1127 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
1130 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
1131 I40E_SUCCESS, (u8 *)&info,
1132 sizeof(struct
1133 i40e_virtchnl_version_info));
1137 * i40e_vc_get_vf_resources_msg
1138 * @vf: pointer to the VF info
1139 * @msg: pointer to the msg buffer
1140 * @msglen: msg length
1142 * called from the VF to request its resources
1144 static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf)
1146 struct i40e_virtchnl_vf_resource *vfres = NULL;
1147 struct i40e_pf *pf = vf->pf;
1148 i40e_status aq_ret = 0;
1149 struct i40e_vsi *vsi;
1150 int i = 0, len = 0;
1151 int num_vsis = 1;
1152 int ret;
1154 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1155 aq_ret = I40E_ERR_PARAM;
1156 goto err;
1159 len = (sizeof(struct i40e_virtchnl_vf_resource) +
1160 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1162 vfres = kzalloc(len, GFP_KERNEL);
1163 if (!vfres) {
1164 aq_ret = I40E_ERR_NO_MEMORY;
1165 len = 0;
1166 goto err;
1169 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
1170 vsi = pf->vsi[vf->lan_vsi_idx];
1171 if (!vsi->info.pvid)
1172 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1174 vfres->num_vsis = num_vsis;
1175 vfres->num_queue_pairs = vf->num_queue_pairs;
1176 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1177 if (vf->lan_vsi_idx) {
1178 vfres->vsi_res[i].vsi_id = vf->lan_vsi_id;
1179 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1180 vfres->vsi_res[i].num_queue_pairs =
1181 pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
1182 memcpy(vfres->vsi_res[i].default_mac_addr,
1183 vf->default_lan_addr.addr, ETH_ALEN);
1184 i++;
1186 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1188 err:
1189 /* send the response back to the VF */
1190 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1191 aq_ret, (u8 *)vfres, len);
1193 kfree(vfres);
1194 return ret;
1198 * i40e_vc_reset_vf_msg
1199 * @vf: pointer to the VF info
1200 * @msg: pointer to the msg buffer
1201 * @msglen: msg length
1203 * called from the VF to reset itself,
1204 * unlike other virtchnl messages, PF driver
1205 * doesn't send the response back to the VF
1207 static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
1209 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1210 i40e_reset_vf(vf, false);
1214 * i40e_vc_config_promiscuous_mode_msg
1215 * @vf: pointer to the VF info
1216 * @msg: pointer to the msg buffer
1217 * @msglen: msg length
1219 * called from the VF to configure the promiscuous mode of
1220 * VF vsis
1222 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1223 u8 *msg, u16 msglen)
1225 struct i40e_virtchnl_promisc_info *info =
1226 (struct i40e_virtchnl_promisc_info *)msg;
1227 struct i40e_pf *pf = vf->pf;
1228 struct i40e_hw *hw = &pf->hw;
1229 struct i40e_vsi *vsi;
1230 bool allmulti = false;
1231 i40e_status aq_ret;
1233 vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
1234 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1235 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1236 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1237 (vsi->type != I40E_VSI_FCOE)) {
1238 aq_ret = I40E_ERR_PARAM;
1239 goto error_param;
1241 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1242 allmulti = true;
1243 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
1244 allmulti, NULL);
1246 error_param:
1247 /* send the response to the VF */
1248 return i40e_vc_send_resp_to_vf(vf,
1249 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1250 aq_ret);
1254 * i40e_vc_config_queues_msg
1255 * @vf: pointer to the VF info
1256 * @msg: pointer to the msg buffer
1257 * @msglen: msg length
1259 * called from the VF to configure the rx/tx
1260 * queues
1262 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1264 struct i40e_virtchnl_vsi_queue_config_info *qci =
1265 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1266 struct i40e_virtchnl_queue_pair_info *qpi;
1267 struct i40e_pf *pf = vf->pf;
1268 u16 vsi_id, vsi_queue_id;
1269 i40e_status aq_ret = 0;
1270 int i;
1272 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1273 aq_ret = I40E_ERR_PARAM;
1274 goto error_param;
1277 vsi_id = qci->vsi_id;
1278 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1279 aq_ret = I40E_ERR_PARAM;
1280 goto error_param;
1282 for (i = 0; i < qci->num_queue_pairs; i++) {
1283 qpi = &qci->qpair[i];
1284 vsi_queue_id = qpi->txq.queue_id;
1285 if ((qpi->txq.vsi_id != vsi_id) ||
1286 (qpi->rxq.vsi_id != vsi_id) ||
1287 (qpi->rxq.queue_id != vsi_queue_id) ||
1288 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1289 aq_ret = I40E_ERR_PARAM;
1290 goto error_param;
1293 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1294 &qpi->rxq) ||
1295 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1296 &qpi->txq)) {
1297 aq_ret = I40E_ERR_PARAM;
1298 goto error_param;
1301 /* set vsi num_queue_pairs in use to num configured by VF */
1302 pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
1304 error_param:
1305 /* send the response to the VF */
1306 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1307 aq_ret);
1311 * i40e_vc_config_irq_map_msg
1312 * @vf: pointer to the VF info
1313 * @msg: pointer to the msg buffer
1314 * @msglen: msg length
1316 * called from the VF to configure the irq to
1317 * queue map
1319 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1321 struct i40e_virtchnl_irq_map_info *irqmap_info =
1322 (struct i40e_virtchnl_irq_map_info *)msg;
1323 struct i40e_virtchnl_vector_map *map;
1324 u16 vsi_id, vsi_queue_id, vector_id;
1325 i40e_status aq_ret = 0;
1326 unsigned long tempmap;
1327 int i;
1329 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1330 aq_ret = I40E_ERR_PARAM;
1331 goto error_param;
1334 for (i = 0; i < irqmap_info->num_vectors; i++) {
1335 map = &irqmap_info->vecmap[i];
1337 vector_id = map->vector_id;
1338 vsi_id = map->vsi_id;
1339 /* validate msg params */
1340 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1341 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1342 aq_ret = I40E_ERR_PARAM;
1343 goto error_param;
1346 /* lookout for the invalid queue index */
1347 tempmap = map->rxq_map;
1348 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1349 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1350 vsi_queue_id)) {
1351 aq_ret = I40E_ERR_PARAM;
1352 goto error_param;
1356 tempmap = map->txq_map;
1357 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1358 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1359 vsi_queue_id)) {
1360 aq_ret = I40E_ERR_PARAM;
1361 goto error_param;
1365 i40e_config_irq_link_list(vf, vsi_id, map);
1367 error_param:
1368 /* send the response to the VF */
1369 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1370 aq_ret);
1374 * i40e_vc_enable_queues_msg
1375 * @vf: pointer to the VF info
1376 * @msg: pointer to the msg buffer
1377 * @msglen: msg length
1379 * called from the VF to enable all or specific queue(s)
1381 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1383 struct i40e_virtchnl_queue_select *vqs =
1384 (struct i40e_virtchnl_queue_select *)msg;
1385 struct i40e_pf *pf = vf->pf;
1386 u16 vsi_id = vqs->vsi_id;
1387 i40e_status aq_ret = 0;
1389 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1390 aq_ret = I40E_ERR_PARAM;
1391 goto error_param;
1394 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1395 aq_ret = I40E_ERR_PARAM;
1396 goto error_param;
1399 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1400 aq_ret = I40E_ERR_PARAM;
1401 goto error_param;
1404 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], true))
1405 aq_ret = I40E_ERR_TIMEOUT;
1406 error_param:
1407 /* send the response to the VF */
1408 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1409 aq_ret);
1413 * i40e_vc_disable_queues_msg
1414 * @vf: pointer to the VF info
1415 * @msg: pointer to the msg buffer
1416 * @msglen: msg length
1418 * called from the VF to disable all or specific
1419 * queue(s)
1421 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1423 struct i40e_virtchnl_queue_select *vqs =
1424 (struct i40e_virtchnl_queue_select *)msg;
1425 struct i40e_pf *pf = vf->pf;
1426 i40e_status aq_ret = 0;
1428 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1429 aq_ret = I40E_ERR_PARAM;
1430 goto error_param;
1433 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1434 aq_ret = I40E_ERR_PARAM;
1435 goto error_param;
1438 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1439 aq_ret = I40E_ERR_PARAM;
1440 goto error_param;
1443 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false))
1444 aq_ret = I40E_ERR_TIMEOUT;
1446 error_param:
1447 /* send the response to the VF */
1448 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1449 aq_ret);
1453 * i40e_vc_get_stats_msg
1454 * @vf: pointer to the VF info
1455 * @msg: pointer to the msg buffer
1456 * @msglen: msg length
1458 * called from the VF to get vsi stats
1460 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1462 struct i40e_virtchnl_queue_select *vqs =
1463 (struct i40e_virtchnl_queue_select *)msg;
1464 struct i40e_pf *pf = vf->pf;
1465 struct i40e_eth_stats stats;
1466 i40e_status aq_ret = 0;
1467 struct i40e_vsi *vsi;
1469 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1471 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1472 aq_ret = I40E_ERR_PARAM;
1473 goto error_param;
1476 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1477 aq_ret = I40E_ERR_PARAM;
1478 goto error_param;
1481 vsi = pf->vsi[vf->lan_vsi_idx];
1482 if (!vsi) {
1483 aq_ret = I40E_ERR_PARAM;
1484 goto error_param;
1486 i40e_update_eth_stats(vsi);
1487 stats = vsi->eth_stats;
1489 error_param:
1490 /* send the response back to the VF */
1491 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1492 (u8 *)&stats, sizeof(stats));
1496 * i40e_check_vf_permission
1497 * @vf: pointer to the VF info
1498 * @macaddr: pointer to the MAC Address being checked
1500 * Check if the VF has permission to add or delete unicast MAC address
1501 * filters and return error code -EPERM if not. Then check if the
1502 * address filter requested is broadcast or zero and if so return
1503 * an invalid MAC address error code.
1505 static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1507 struct i40e_pf *pf = vf->pf;
1508 int ret = 0;
1510 if (is_broadcast_ether_addr(macaddr) ||
1511 is_zero_ether_addr(macaddr)) {
1512 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1513 ret = I40E_ERR_INVALID_MAC_ADDR;
1514 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1515 !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
1516 /* If the host VMM administrator has set the VF MAC address
1517 * administratively via the ndo_set_vf_mac command then deny
1518 * permission to the VF to add or delete unicast MAC addresses.
1519 * The VF may request to set the MAC address filter already
1520 * assigned to it so do not return an error in that case.
1522 dev_err(&pf->pdev->dev,
1523 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1524 ret = -EPERM;
1526 return ret;
1530 * i40e_vc_add_mac_addr_msg
1531 * @vf: pointer to the VF info
1532 * @msg: pointer to the msg buffer
1533 * @msglen: msg length
1535 * add guest mac address filter
1537 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1539 struct i40e_virtchnl_ether_addr_list *al =
1540 (struct i40e_virtchnl_ether_addr_list *)msg;
1541 struct i40e_pf *pf = vf->pf;
1542 struct i40e_vsi *vsi = NULL;
1543 u16 vsi_id = al->vsi_id;
1544 i40e_status ret = 0;
1545 int i;
1547 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1548 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1549 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1550 ret = I40E_ERR_PARAM;
1551 goto error_param;
1554 for (i = 0; i < al->num_elements; i++) {
1555 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1556 if (ret)
1557 goto error_param;
1559 vsi = pf->vsi[vf->lan_vsi_idx];
1561 /* add new addresses to the list */
1562 for (i = 0; i < al->num_elements; i++) {
1563 struct i40e_mac_filter *f;
1565 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
1566 if (!f) {
1567 if (i40e_is_vsi_in_vlan(vsi))
1568 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1569 true, false);
1570 else
1571 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1572 true, false);
1575 if (!f) {
1576 dev_err(&pf->pdev->dev,
1577 "Unable to add VF MAC filter\n");
1578 ret = I40E_ERR_PARAM;
1579 goto error_param;
1583 /* program the updated filter list */
1584 if (i40e_sync_vsi_filters(vsi))
1585 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1587 error_param:
1588 /* send the response to the VF */
1589 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
1590 ret);
1594 * i40e_vc_del_mac_addr_msg
1595 * @vf: pointer to the VF info
1596 * @msg: pointer to the msg buffer
1597 * @msglen: msg length
1599 * remove guest mac address filter
1601 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1603 struct i40e_virtchnl_ether_addr_list *al =
1604 (struct i40e_virtchnl_ether_addr_list *)msg;
1605 struct i40e_pf *pf = vf->pf;
1606 struct i40e_vsi *vsi = NULL;
1607 u16 vsi_id = al->vsi_id;
1608 i40e_status ret = 0;
1609 int i;
1611 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1612 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1613 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1614 ret = I40E_ERR_PARAM;
1615 goto error_param;
1618 for (i = 0; i < al->num_elements; i++) {
1619 if (is_broadcast_ether_addr(al->list[i].addr) ||
1620 is_zero_ether_addr(al->list[i].addr)) {
1621 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
1622 al->list[i].addr);
1623 ret = I40E_ERR_INVALID_MAC_ADDR;
1624 goto error_param;
1627 vsi = pf->vsi[vf->lan_vsi_idx];
1629 /* delete addresses from the list */
1630 for (i = 0; i < al->num_elements; i++)
1631 i40e_del_filter(vsi, al->list[i].addr,
1632 I40E_VLAN_ANY, true, false);
1634 /* program the updated filter list */
1635 if (i40e_sync_vsi_filters(vsi))
1636 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1638 error_param:
1639 /* send the response to the VF */
1640 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
1641 ret);
1645 * i40e_vc_add_vlan_msg
1646 * @vf: pointer to the VF info
1647 * @msg: pointer to the msg buffer
1648 * @msglen: msg length
1650 * program guest vlan id
1652 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1654 struct i40e_virtchnl_vlan_filter_list *vfl =
1655 (struct i40e_virtchnl_vlan_filter_list *)msg;
1656 struct i40e_pf *pf = vf->pf;
1657 struct i40e_vsi *vsi = NULL;
1658 u16 vsi_id = vfl->vsi_id;
1659 i40e_status aq_ret = 0;
1660 int i;
1662 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1663 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1664 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1665 aq_ret = I40E_ERR_PARAM;
1666 goto error_param;
1669 for (i = 0; i < vfl->num_elements; i++) {
1670 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1671 aq_ret = I40E_ERR_PARAM;
1672 dev_err(&pf->pdev->dev,
1673 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1674 goto error_param;
1677 vsi = pf->vsi[vf->lan_vsi_idx];
1678 if (vsi->info.pvid) {
1679 aq_ret = I40E_ERR_PARAM;
1680 goto error_param;
1683 i40e_vlan_stripping_enable(vsi);
1684 for (i = 0; i < vfl->num_elements; i++) {
1685 /* add new VLAN filter */
1686 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1687 if (ret)
1688 dev_err(&pf->pdev->dev,
1689 "Unable to add VF vlan filter %d, error %d\n",
1690 vfl->vlan_id[i], ret);
1693 error_param:
1694 /* send the response to the VF */
1695 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1699 * i40e_vc_remove_vlan_msg
1700 * @vf: pointer to the VF info
1701 * @msg: pointer to the msg buffer
1702 * @msglen: msg length
1704 * remove programmed guest vlan id
1706 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1708 struct i40e_virtchnl_vlan_filter_list *vfl =
1709 (struct i40e_virtchnl_vlan_filter_list *)msg;
1710 struct i40e_pf *pf = vf->pf;
1711 struct i40e_vsi *vsi = NULL;
1712 u16 vsi_id = vfl->vsi_id;
1713 i40e_status aq_ret = 0;
1714 int i;
1716 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1717 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1718 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1719 aq_ret = I40E_ERR_PARAM;
1720 goto error_param;
1723 for (i = 0; i < vfl->num_elements; i++) {
1724 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1725 aq_ret = I40E_ERR_PARAM;
1726 goto error_param;
1730 vsi = pf->vsi[vf->lan_vsi_idx];
1731 if (vsi->info.pvid) {
1732 aq_ret = I40E_ERR_PARAM;
1733 goto error_param;
1736 for (i = 0; i < vfl->num_elements; i++) {
1737 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1738 if (ret)
1739 dev_err(&pf->pdev->dev,
1740 "Unable to delete VF vlan filter %d, error %d\n",
1741 vfl->vlan_id[i], ret);
1744 error_param:
1745 /* send the response to the VF */
1746 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1750 * i40e_vc_validate_vf_msg
1751 * @vf: pointer to the VF info
1752 * @msg: pointer to the msg buffer
1753 * @msglen: msg length
1754 * @msghndl: msg handle
1756 * validate msg
1758 static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1759 u32 v_retval, u8 *msg, u16 msglen)
1761 bool err_msg_format = false;
1762 int valid_len;
1764 /* Check if VF is disabled. */
1765 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1766 return I40E_ERR_PARAM;
1768 /* Validate message length. */
1769 switch (v_opcode) {
1770 case I40E_VIRTCHNL_OP_VERSION:
1771 valid_len = sizeof(struct i40e_virtchnl_version_info);
1772 break;
1773 case I40E_VIRTCHNL_OP_RESET_VF:
1774 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1775 valid_len = 0;
1776 break;
1777 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1778 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1779 break;
1780 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1781 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1782 break;
1783 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1784 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1785 if (msglen >= valid_len) {
1786 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1787 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1788 valid_len += (vqc->num_queue_pairs *
1789 sizeof(struct
1790 i40e_virtchnl_queue_pair_info));
1791 if (vqc->num_queue_pairs == 0)
1792 err_msg_format = true;
1794 break;
1795 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1796 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1797 if (msglen >= valid_len) {
1798 struct i40e_virtchnl_irq_map_info *vimi =
1799 (struct i40e_virtchnl_irq_map_info *)msg;
1800 valid_len += (vimi->num_vectors *
1801 sizeof(struct i40e_virtchnl_vector_map));
1802 if (vimi->num_vectors == 0)
1803 err_msg_format = true;
1805 break;
1806 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1807 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1808 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1809 break;
1810 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1811 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1812 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1813 if (msglen >= valid_len) {
1814 struct i40e_virtchnl_ether_addr_list *veal =
1815 (struct i40e_virtchnl_ether_addr_list *)msg;
1816 valid_len += veal->num_elements *
1817 sizeof(struct i40e_virtchnl_ether_addr);
1818 if (veal->num_elements == 0)
1819 err_msg_format = true;
1821 break;
1822 case I40E_VIRTCHNL_OP_ADD_VLAN:
1823 case I40E_VIRTCHNL_OP_DEL_VLAN:
1824 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1825 if (msglen >= valid_len) {
1826 struct i40e_virtchnl_vlan_filter_list *vfl =
1827 (struct i40e_virtchnl_vlan_filter_list *)msg;
1828 valid_len += vfl->num_elements * sizeof(u16);
1829 if (vfl->num_elements == 0)
1830 err_msg_format = true;
1832 break;
1833 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1834 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1835 break;
1836 case I40E_VIRTCHNL_OP_GET_STATS:
1837 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1838 break;
1839 /* These are always errors coming from the VF. */
1840 case I40E_VIRTCHNL_OP_EVENT:
1841 case I40E_VIRTCHNL_OP_UNKNOWN:
1842 default:
1843 return -EPERM;
1844 break;
1846 /* few more checks */
1847 if ((valid_len != msglen) || (err_msg_format)) {
1848 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1849 return -EINVAL;
1850 } else {
1851 return 0;
1856 * i40e_vc_process_vf_msg
1857 * @pf: pointer to the PF structure
1858 * @vf_id: source VF id
1859 * @msg: pointer to the msg buffer
1860 * @msglen: msg length
1861 * @msghndl: msg handle
1863 * called from the common aeq/arq handler to
1864 * process request from VF
1866 int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1867 u32 v_retval, u8 *msg, u16 msglen)
1869 struct i40e_hw *hw = &pf->hw;
1870 unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
1871 struct i40e_vf *vf;
1872 int ret;
1874 pf->vf_aq_requests++;
1875 if (local_vf_id >= pf->num_alloc_vfs)
1876 return -EINVAL;
1877 vf = &(pf->vf[local_vf_id]);
1878 /* perform basic checks on the msg */
1879 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1881 if (ret) {
1882 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
1883 local_vf_id, v_opcode, msglen);
1884 return ret;
1887 switch (v_opcode) {
1888 case I40E_VIRTCHNL_OP_VERSION:
1889 ret = i40e_vc_get_version_msg(vf);
1890 break;
1891 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1892 ret = i40e_vc_get_vf_resources_msg(vf);
1893 break;
1894 case I40E_VIRTCHNL_OP_RESET_VF:
1895 i40e_vc_reset_vf_msg(vf);
1896 ret = 0;
1897 break;
1898 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1899 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1900 break;
1901 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1902 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1903 break;
1904 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1905 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1906 break;
1907 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1908 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
1909 i40e_vc_notify_vf_link_state(vf);
1910 break;
1911 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1912 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1913 break;
1914 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1915 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1916 break;
1917 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1918 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1919 break;
1920 case I40E_VIRTCHNL_OP_ADD_VLAN:
1921 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1922 break;
1923 case I40E_VIRTCHNL_OP_DEL_VLAN:
1924 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1925 break;
1926 case I40E_VIRTCHNL_OP_GET_STATS:
1927 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1928 break;
1929 case I40E_VIRTCHNL_OP_UNKNOWN:
1930 default:
1931 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
1932 v_opcode, local_vf_id);
1933 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1934 I40E_ERR_NOT_IMPLEMENTED);
1935 break;
1938 return ret;
1942 * i40e_vc_process_vflr_event
1943 * @pf: pointer to the PF structure
1945 * called from the vlfr irq handler to
1946 * free up VF resources and state variables
1948 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1950 u32 reg, reg_idx, bit_idx, vf_id;
1951 struct i40e_hw *hw = &pf->hw;
1952 struct i40e_vf *vf;
1954 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1955 return 0;
1957 /* re-enable vflr interrupt cause */
1958 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1959 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1960 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1961 i40e_flush(hw);
1963 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1964 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1965 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1966 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1967 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
1968 vf = &pf->vf[vf_id];
1969 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
1970 if (reg & (1 << bit_idx)) {
1971 /* clear the bit in GLGEN_VFLRSTAT */
1972 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
1974 if (!test_bit(__I40E_DOWN, &pf->state))
1975 i40e_reset_vf(vf, true);
1979 return 0;
1983 * i40e_ndo_set_vf_mac
1984 * @netdev: network interface device structure
1985 * @vf_id: VF identifier
1986 * @mac: mac address
1988 * program VF mac address
1990 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
1992 struct i40e_netdev_priv *np = netdev_priv(netdev);
1993 struct i40e_vsi *vsi = np->vsi;
1994 struct i40e_pf *pf = vsi->back;
1995 struct i40e_mac_filter *f;
1996 struct i40e_vf *vf;
1997 int ret = 0;
1999 /* validate the request */
2000 if (vf_id >= pf->num_alloc_vfs) {
2001 dev_err(&pf->pdev->dev,
2002 "Invalid VF Identifier %d\n", vf_id);
2003 ret = -EINVAL;
2004 goto error_param;
2007 vf = &(pf->vf[vf_id]);
2008 vsi = pf->vsi[vf->lan_vsi_idx];
2009 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2010 dev_err(&pf->pdev->dev,
2011 "Uninitialized VF %d\n", vf_id);
2012 ret = -EINVAL;
2013 goto error_param;
2016 if (!is_valid_ether_addr(mac)) {
2017 dev_err(&pf->pdev->dev,
2018 "Invalid VF ethernet address\n");
2019 ret = -EINVAL;
2020 goto error_param;
2023 /* delete the temporary mac address */
2024 i40e_del_filter(vsi, vf->default_lan_addr.addr,
2025 vf->port_vlan_id ? vf->port_vlan_id : -1,
2026 true, false);
2028 /* Delete all the filters for this VSI - we're going to kill it
2029 * anyway.
2031 list_for_each_entry(f, &vsi->mac_filter_list, list)
2032 i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
2034 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2035 /* program mac filter */
2036 if (i40e_sync_vsi_filters(vsi)) {
2037 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2038 ret = -EIO;
2039 goto error_param;
2041 ether_addr_copy(vf->default_lan_addr.addr, mac);
2042 vf->pf_set_mac = true;
2043 /* Force the VF driver stop so it has to reload with new MAC address */
2044 i40e_vc_disable_vf(pf, vf);
2045 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
2047 error_param:
2048 return ret;
2052 * i40e_ndo_set_vf_port_vlan
2053 * @netdev: network interface device structure
2054 * @vf_id: VF identifier
2055 * @vlan_id: mac address
2056 * @qos: priority setting
2058 * program VF vlan id and/or qos
2060 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2061 int vf_id, u16 vlan_id, u8 qos)
2063 struct i40e_netdev_priv *np = netdev_priv(netdev);
2064 struct i40e_pf *pf = np->vsi->back;
2065 struct i40e_vsi *vsi;
2066 struct i40e_vf *vf;
2067 int ret = 0;
2069 /* validate the request */
2070 if (vf_id >= pf->num_alloc_vfs) {
2071 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2072 ret = -EINVAL;
2073 goto error_pvid;
2076 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2077 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2078 ret = -EINVAL;
2079 goto error_pvid;
2082 vf = &(pf->vf[vf_id]);
2083 vsi = pf->vsi[vf->lan_vsi_idx];
2084 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2085 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2086 ret = -EINVAL;
2087 goto error_pvid;
2090 if (vsi->info.pvid == (vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT)))
2091 /* duplicate request, so just return success */
2092 goto error_pvid;
2094 if (vsi->info.pvid == 0 && i40e_is_vsi_in_vlan(vsi)) {
2095 dev_err(&pf->pdev->dev,
2096 "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",
2097 vf_id);
2098 /* Administrator Error - knock the VF offline until he does
2099 * the right thing by reconfiguring his network correctly
2100 * and then reloading the VF driver.
2102 i40e_vc_disable_vf(pf, vf);
2105 /* Check for condition where there was already a port VLAN ID
2106 * filter set and now it is being deleted by setting it to zero.
2107 * Additionally check for the condition where there was a port
2108 * VLAN but now there is a new and different port VLAN being set.
2109 * Before deleting all the old VLAN filters we must add new ones
2110 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2111 * MAC addresses deleted.
2113 if ((!(vlan_id || qos) ||
2114 (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
2115 vsi->info.pvid)
2116 ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2118 if (vsi->info.pvid) {
2119 /* kill old VLAN */
2120 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2121 VLAN_VID_MASK));
2122 if (ret) {
2123 dev_info(&vsi->back->pdev->dev,
2124 "remove VLAN failed, ret=%d, aq_err=%d\n",
2125 ret, pf->hw.aq.asq_last_status);
2128 if (vlan_id || qos)
2129 ret = i40e_vsi_add_pvid(vsi,
2130 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
2131 else
2132 i40e_vsi_remove_pvid(vsi);
2134 if (vlan_id) {
2135 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2136 vlan_id, qos, vf_id);
2138 /* add new VLAN filter */
2139 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2140 if (ret) {
2141 dev_info(&vsi->back->pdev->dev,
2142 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2143 vsi->back->hw.aq.asq_last_status);
2144 goto error_pvid;
2146 /* Kill non-vlan MAC filters - ignore error return since
2147 * there might not be any non-vlan MAC filters.
2149 i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
2152 if (ret) {
2153 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2154 goto error_pvid;
2156 /* The Port VLAN needs to be saved across resets the same as the
2157 * default LAN MAC address.
2159 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
2160 ret = 0;
2162 error_pvid:
2163 return ret;
2166 #define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
2167 #define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
2169 * i40e_ndo_set_vf_bw
2170 * @netdev: network interface device structure
2171 * @vf_id: VF identifier
2172 * @tx_rate: Tx rate
2174 * configure VF Tx rate
2176 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2177 int max_tx_rate)
2179 struct i40e_netdev_priv *np = netdev_priv(netdev);
2180 struct i40e_pf *pf = np->vsi->back;
2181 struct i40e_vsi *vsi;
2182 struct i40e_vf *vf;
2183 int speed = 0;
2184 int ret = 0;
2186 /* validate the request */
2187 if (vf_id >= pf->num_alloc_vfs) {
2188 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2189 ret = -EINVAL;
2190 goto error;
2193 if (min_tx_rate) {
2194 dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
2195 min_tx_rate, vf_id);
2196 return -EINVAL;
2199 vf = &(pf->vf[vf_id]);
2200 vsi = pf->vsi[vf->lan_vsi_idx];
2201 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2202 dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
2203 ret = -EINVAL;
2204 goto error;
2207 switch (pf->hw.phy.link_info.link_speed) {
2208 case I40E_LINK_SPEED_40GB:
2209 speed = 40000;
2210 break;
2211 case I40E_LINK_SPEED_10GB:
2212 speed = 10000;
2213 break;
2214 case I40E_LINK_SPEED_1GB:
2215 speed = 1000;
2216 break;
2217 default:
2218 break;
2221 if (max_tx_rate > speed) {
2222 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
2223 max_tx_rate, vf->vf_id);
2224 ret = -EINVAL;
2225 goto error;
2228 if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2229 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2230 max_tx_rate = 50;
2233 /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
2234 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2235 max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2236 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
2237 if (ret) {
2238 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
2239 ret);
2240 ret = -EIO;
2241 goto error;
2243 vf->tx_rate = max_tx_rate;
2244 error:
2245 return ret;
2249 * i40e_ndo_get_vf_config
2250 * @netdev: network interface device structure
2251 * @vf_id: VF identifier
2252 * @ivi: VF configuration structure
2254 * return VF configuration
2256 int i40e_ndo_get_vf_config(struct net_device *netdev,
2257 int vf_id, struct ifla_vf_info *ivi)
2259 struct i40e_netdev_priv *np = netdev_priv(netdev);
2260 struct i40e_vsi *vsi = np->vsi;
2261 struct i40e_pf *pf = vsi->back;
2262 struct i40e_vf *vf;
2263 int ret = 0;
2265 /* validate the request */
2266 if (vf_id >= pf->num_alloc_vfs) {
2267 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2268 ret = -EINVAL;
2269 goto error_param;
2272 vf = &(pf->vf[vf_id]);
2273 /* first vsi is always the LAN vsi */
2274 vsi = pf->vsi[vf->lan_vsi_idx];
2275 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2276 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2277 ret = -EINVAL;
2278 goto error_param;
2281 ivi->vf = vf_id;
2283 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
2285 ivi->max_tx_rate = vf->tx_rate;
2286 ivi->min_tx_rate = 0;
2287 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2288 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2289 I40E_VLAN_PRIORITY_SHIFT;
2290 if (vf->link_forced == false)
2291 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2292 else if (vf->link_up == true)
2293 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2294 else
2295 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
2296 ivi->spoofchk = vf->spoofchk;
2297 ret = 0;
2299 error_param:
2300 return ret;
2304 * i40e_ndo_set_vf_link_state
2305 * @netdev: network interface device structure
2306 * @vf_id: VF identifier
2307 * @link: required link state
2309 * Set the link state of a specified VF, regardless of physical link state
2311 int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
2313 struct i40e_netdev_priv *np = netdev_priv(netdev);
2314 struct i40e_pf *pf = np->vsi->back;
2315 struct i40e_virtchnl_pf_event pfe;
2316 struct i40e_hw *hw = &pf->hw;
2317 struct i40e_vf *vf;
2318 int abs_vf_id;
2319 int ret = 0;
2321 /* validate the request */
2322 if (vf_id >= pf->num_alloc_vfs) {
2323 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2324 ret = -EINVAL;
2325 goto error_out;
2328 vf = &pf->vf[vf_id];
2329 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
2331 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
2332 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
2334 switch (link) {
2335 case IFLA_VF_LINK_STATE_AUTO:
2336 vf->link_forced = false;
2337 pfe.event_data.link_event.link_status =
2338 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
2339 pfe.event_data.link_event.link_speed =
2340 pf->hw.phy.link_info.link_speed;
2341 break;
2342 case IFLA_VF_LINK_STATE_ENABLE:
2343 vf->link_forced = true;
2344 vf->link_up = true;
2345 pfe.event_data.link_event.link_status = true;
2346 pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
2347 break;
2348 case IFLA_VF_LINK_STATE_DISABLE:
2349 vf->link_forced = true;
2350 vf->link_up = false;
2351 pfe.event_data.link_event.link_status = false;
2352 pfe.event_data.link_event.link_speed = 0;
2353 break;
2354 default:
2355 ret = -EINVAL;
2356 goto error_out;
2358 /* Notify the VF of its new link state */
2359 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
2360 0, (u8 *)&pfe, sizeof(pfe), NULL);
2362 error_out:
2363 return ret;
2367 * i40e_ndo_set_vf_spoofchk
2368 * @netdev: network interface device structure
2369 * @vf_id: VF identifier
2370 * @enable: flag to enable or disable feature
2372 * Enable or disable VF spoof checking
2374 int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
2376 struct i40e_netdev_priv *np = netdev_priv(netdev);
2377 struct i40e_vsi *vsi = np->vsi;
2378 struct i40e_pf *pf = vsi->back;
2379 struct i40e_vsi_context ctxt;
2380 struct i40e_hw *hw = &pf->hw;
2381 struct i40e_vf *vf;
2382 int ret = 0;
2384 /* validate the request */
2385 if (vf_id >= pf->num_alloc_vfs) {
2386 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2387 ret = -EINVAL;
2388 goto out;
2391 vf = &(pf->vf[vf_id]);
2393 if (enable == vf->spoofchk)
2394 goto out;
2396 vf->spoofchk = enable;
2397 memset(&ctxt, 0, sizeof(ctxt));
2398 ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
2399 ctxt.pf_num = pf->hw.pf_id;
2400 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
2401 if (enable)
2402 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
2403 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
2404 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
2405 if (ret) {
2406 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
2407 ret);
2408 ret = -EIO;
2410 out:
2411 return ret;