1 /*******************************************************************************
3 * Intel Ethernet Controller XL710 Family Linux Driver
4 * Copyright(c) 2013 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
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
22 * Contact Information:
23 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 ******************************************************************************/
30 /***********************misc routines*****************************/
33 * i40e_vc_isvalid_vsi_id
34 * @vf: pointer to the vf info
35 * @vsi_id: vf relative vsi id
37 * check for the valid vsi id
39 static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u8 vsi_id)
41 struct i40e_pf *pf = vf->pf;
43 return pf->vsi[vsi_id]->vf_id == vf->vf_id;
47 * i40e_vc_isvalid_queue_id
48 * @vf: pointer to the vf info
50 * @qid: vsi relative queue id
52 * check for the valid queue id
54 static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u8 vsi_id,
57 struct i40e_pf *pf = vf->pf;
59 return qid < pf->vsi[vsi_id]->num_queue_pairs;
63 * i40e_vc_isvalid_vector_id
64 * @vf: pointer to the vf info
65 * @vector_id: vf relative vector id
67 * check for the valid vector id
69 static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
71 struct i40e_pf *pf = vf->pf;
73 return vector_id <= pf->hw.func_caps.num_msix_vectors_vf;
76 /***********************vf resource mgmt routines*****************/
79 * i40e_vc_get_pf_queue_id
80 * @vf: pointer to the vf info
81 * @vsi_idx: index of VSI in PF struct
82 * @vsi_queue_id: vsi relative queue id
84 * return pf relative queue id
86 static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u8 vsi_idx,
89 struct i40e_pf *pf = vf->pf;
90 struct i40e_vsi *vsi = pf->vsi[vsi_idx];
91 u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
93 if (le16_to_cpu(vsi->info.mapping_flags) &
94 I40E_AQ_VSI_QUE_MAP_NONCONTIG)
96 le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
98 pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
105 * i40e_config_irq_link_list
106 * @vf: pointer to the vf info
107 * @vsi_idx: index of VSI in PF struct
108 * @vecmap: irq map info
110 * configure irq link list from the map
112 static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_idx,
113 struct i40e_virtchnl_vector_map *vecmap)
115 unsigned long linklistmap = 0, tempmap;
116 struct i40e_pf *pf = vf->pf;
117 struct i40e_hw *hw = &pf->hw;
118 u16 vsi_queue_id, pf_queue_id;
119 enum i40e_queue_type qtype;
120 u16 next_q, vector_id;
124 vector_id = vecmap->vector_id;
127 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
129 reg_idx = I40E_VPINT_LNKLSTN(
130 (pf->hw.func_caps.num_msix_vectors_vf
131 * vf->vf_id) + (vector_id - 1));
133 if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
134 /* Special case - No queues mapped on this vector */
135 wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
138 tempmap = vecmap->rxq_map;
139 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
141 (I40E_VIRTCHNL_SUPPORTED_QTYPES *
145 tempmap = vecmap->txq_map;
146 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
148 (I40E_VIRTCHNL_SUPPORTED_QTYPES * vsi_queue_id
152 next_q = find_first_bit(&linklistmap,
154 I40E_VIRTCHNL_SUPPORTED_QTYPES));
155 vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
156 qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
157 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx, vsi_queue_id);
158 reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
160 wr32(hw, reg_idx, reg);
162 while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
164 case I40E_QUEUE_TYPE_RX:
165 reg_idx = I40E_QINT_RQCTL(pf_queue_id);
166 itr_idx = vecmap->rxitr_idx;
168 case I40E_QUEUE_TYPE_TX:
169 reg_idx = I40E_QINT_TQCTL(pf_queue_id);
170 itr_idx = vecmap->txitr_idx;
176 next_q = find_next_bit(&linklistmap,
178 I40E_VIRTCHNL_SUPPORTED_QTYPES),
180 if (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
181 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
182 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
183 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx,
186 pf_queue_id = I40E_QUEUE_END_OF_LIST;
190 /* format for the RQCTL & TQCTL regs is same */
192 (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
193 (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
194 (1 << I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
195 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
196 wr32(hw, reg_idx, reg);
204 * i40e_config_vsi_tx_queue
205 * @vf: pointer to the vf info
206 * @vsi_idx: index of VSI in PF struct
207 * @vsi_queue_id: vsi relative queue index
208 * @info: config. info
212 static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_idx,
214 struct i40e_virtchnl_txq_info *info)
216 struct i40e_pf *pf = vf->pf;
217 struct i40e_hw *hw = &pf->hw;
218 struct i40e_hmc_obj_txq tx_ctx;
223 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx, vsi_queue_id);
225 /* clear the context structure first */
226 memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
228 /* only set the required fields */
229 tx_ctx.base = info->dma_ring_addr / 128;
230 tx_ctx.qlen = info->ring_len;
231 tx_ctx.rdylist = le16_to_cpu(pf->vsi[vsi_idx]->info.qs_handle[0]);
232 tx_ctx.rdylist_act = 0;
234 /* clear the context in the HMC */
235 ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
237 dev_err(&pf->pdev->dev,
238 "Failed to clear VF LAN Tx queue context %d, error: %d\n",
244 /* set the context in the HMC */
245 ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
247 dev_err(&pf->pdev->dev,
248 "Failed to set VF LAN Tx queue context %d error: %d\n",
254 /* associate this queue with the PCI VF function */
255 qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
256 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
257 & I40E_QTX_CTL_PF_INDX_MASK);
258 qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
259 << I40E_QTX_CTL_VFVM_INDX_SHIFT)
260 & I40E_QTX_CTL_VFVM_INDX_MASK);
261 wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
269 * i40e_config_vsi_rx_queue
270 * @vf: pointer to the vf info
271 * @vsi_idx: index of VSI in PF struct
272 * @vsi_queue_id: vsi relative queue index
273 * @info: config. info
277 static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_idx,
279 struct i40e_virtchnl_rxq_info *info)
281 struct i40e_pf *pf = vf->pf;
282 struct i40e_hw *hw = &pf->hw;
283 struct i40e_hmc_obj_rxq rx_ctx;
287 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx, vsi_queue_id);
289 /* clear the context structure first */
290 memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
292 /* only set the required fields */
293 rx_ctx.base = info->dma_ring_addr / 128;
294 rx_ctx.qlen = info->ring_len;
296 if (info->splithdr_enabled) {
297 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
299 I40E_RX_SPLIT_TCP_UDP |
301 /* header length validation */
302 if (info->hdr_size > ((2 * 1024) - 64)) {
306 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
308 /* set splitalways mode 10b */
312 /* databuffer length validation */
313 if (info->databuffer_size > ((16 * 1024) - 128)) {
317 rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
319 /* max pkt. length validation */
320 if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
324 rx_ctx.rxmax = info->max_pkt_size;
326 /* enable 32bytes desc always */
330 rx_ctx.tphrdesc_ena = 1;
331 rx_ctx.tphwdesc_ena = 1;
332 rx_ctx.tphdata_ena = 1;
333 rx_ctx.tphhead_ena = 1;
334 rx_ctx.lrxqthresh = 2;
337 /* clear the context in the HMC */
338 ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
340 dev_err(&pf->pdev->dev,
341 "Failed to clear VF LAN Rx queue context %d, error: %d\n",
347 /* set the context in the HMC */
348 ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
350 dev_err(&pf->pdev->dev,
351 "Failed to set VF LAN Rx queue context %d error: %d\n",
363 * @vf: pointer to the vf info
364 * @type: type of VSI to allocate
366 * alloc vf vsi context & resources
368 static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
370 struct i40e_mac_filter *f = NULL;
371 struct i40e_pf *pf = vf->pf;
372 struct i40e_hw *hw = &pf->hw;
373 struct i40e_vsi *vsi;
376 vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
379 dev_err(&pf->pdev->dev,
380 "add vsi failed for vf %d, aq_err %d\n",
381 vf->vf_id, pf->hw.aq.asq_last_status);
383 goto error_alloc_vsi_res;
385 if (type == I40E_VSI_SRIOV) {
386 vf->lan_vsi_index = vsi->idx;
387 vf->lan_vsi_id = vsi->id;
388 dev_info(&pf->pdev->dev,
389 "LAN VSI index %d, VSI id %d\n",
391 /* If the port VLAN has been configured and then the
392 * VF driver was removed then the VSI port VLAN
393 * configuration was destroyed. Check if there is
394 * a port VLAN and restore the VSI configuration if
397 if (vf->port_vlan_id)
398 i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
399 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
400 vf->port_vlan_id, true, false);
404 dev_err(&pf->pdev->dev, "Unable to add ucast filter\n");
406 goto error_alloc_vsi_res;
409 /* program mac filter */
410 ret = i40e_sync_vsi_filters(vsi);
412 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
413 goto error_alloc_vsi_res;
416 /* accept bcast pkts. by default */
417 ret = i40e_aq_set_vsi_broadcast(hw, vsi->seid, true, NULL);
419 dev_err(&pf->pdev->dev,
420 "set vsi bcast failed for vf %d, vsi %d, aq_err %d\n",
421 vf->vf_id, vsi->idx, pf->hw.aq.asq_last_status);
430 * i40e_enable_vf_mappings
431 * @vf: pointer to the vf info
435 static void i40e_enable_vf_mappings(struct i40e_vf *vf)
437 struct i40e_pf *pf = vf->pf;
438 struct i40e_hw *hw = &pf->hw;
439 u32 reg, total_queue_pairs = 0;
442 /* Tell the hardware we're using noncontiguous mapping. HW requires
443 * that VF queues be mapped using this method, even when they are
444 * contiguous in real life
446 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
447 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
449 /* enable VF vplan_qtable mappings */
450 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
451 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
453 /* map PF queues to VF queues */
454 for (j = 0; j < pf->vsi[vf->lan_vsi_index]->num_queue_pairs; j++) {
455 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index, j);
456 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
457 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
461 /* map PF queues to VSI */
462 for (j = 0; j < 7; j++) {
463 if (j * 2 >= pf->vsi[vf->lan_vsi_index]->num_queue_pairs) {
464 reg = 0x07FF07FF; /* unused */
466 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index,
469 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index,
473 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
480 * i40e_disable_vf_mappings
481 * @vf: pointer to the vf info
483 * disable vf mappings
485 static void i40e_disable_vf_mappings(struct i40e_vf *vf)
487 struct i40e_pf *pf = vf->pf;
488 struct i40e_hw *hw = &pf->hw;
491 /* disable qp mappings */
492 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
493 for (i = 0; i < I40E_MAX_VSI_QP; i++)
494 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
495 I40E_QUEUE_END_OF_LIST);
501 * @vf: pointer to the vf info
505 static void i40e_free_vf_res(struct i40e_vf *vf)
507 struct i40e_pf *pf = vf->pf;
508 struct i40e_hw *hw = &pf->hw;
512 /* free vsi & disconnect it from the parent uplink */
513 if (vf->lan_vsi_index) {
514 i40e_vsi_release(pf->vsi[vf->lan_vsi_index]);
515 vf->lan_vsi_index = 0;
518 msix_vf = pf->hw.func_caps.num_msix_vectors_vf + 1;
519 /* disable interrupts so the VF starts in a known state */
520 for (i = 0; i < msix_vf; i++) {
521 /* format is same for both registers */
523 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
525 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
528 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
532 /* clear the irq settings */
533 for (i = 0; i < msix_vf; i++) {
534 /* format is same for both registers */
536 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
538 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
541 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
542 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
543 wr32(hw, reg_idx, reg);
546 /* reset some of the state varibles keeping
547 * track of the resources
549 vf->num_queue_pairs = 0;
555 * @vf: pointer to the vf info
557 * allocate vf resources
559 static int i40e_alloc_vf_res(struct i40e_vf *vf)
561 struct i40e_pf *pf = vf->pf;
562 int total_queue_pairs = 0;
565 /* allocate hw vsi context & associated resources */
566 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
569 total_queue_pairs += pf->vsi[vf->lan_vsi_index]->num_queue_pairs;
570 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
572 /* store the total qps number for the runtime
575 vf->num_queue_pairs = total_queue_pairs;
577 /* vf is now completely initialized */
578 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
582 i40e_free_vf_res(vf);
587 #define VF_DEVICE_STATUS 0xAA
588 #define VF_TRANS_PENDING_MASK 0x20
590 * i40e_quiesce_vf_pci
591 * @vf: pointer to the vf structure
593 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
594 * if the transactions never clear.
596 static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
598 struct i40e_pf *pf = vf->pf;
599 struct i40e_hw *hw = &pf->hw;
603 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
605 wr32(hw, I40E_PF_PCI_CIAA,
606 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
607 for (i = 0; i < 100; i++) {
608 reg = rd32(hw, I40E_PF_PCI_CIAD);
609 if ((reg & VF_TRANS_PENDING_MASK) == 0)
618 * @vf: pointer to the vf structure
619 * @flr: VFLR was issued or not
623 void i40e_reset_vf(struct i40e_vf *vf, bool flr)
625 struct i40e_pf *pf = vf->pf;
626 struct i40e_hw *hw = &pf->hw;
632 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
634 /* In the case of a VFLR, the HW has already reset the VF and we
635 * just need to clean up, so don't hit the VFRTRIG register.
638 /* reset vf using VPGEN_VFRTRIG reg */
639 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
640 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
641 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
645 if (i40e_quiesce_vf_pci(vf))
646 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
649 /* poll VPGEN_VFRSTAT reg to make sure
650 * that reset is complete
652 for (i = 0; i < 100; i++) {
653 /* vf reset requires driver to first reset the
654 * vf & than poll the status register to make sure
655 * that the requested op was completed
659 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
660 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
667 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
669 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
670 /* clear the reset bit in the VPGEN_VFRTRIG reg */
671 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
672 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
673 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
675 /* On initial reset, we won't have any queues */
676 if (vf->lan_vsi_index == 0)
679 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_index], false);
681 /* reallocate vf resources to reset the VSI state */
682 i40e_free_vf_res(vf);
684 i40e_alloc_vf_res(vf);
685 i40e_enable_vf_mappings(vf);
687 /* tell the VF the reset is done */
688 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
693 * i40e_vfs_are_assigned
694 * @pf: pointer to the pf structure
696 * Determine if any VFs are assigned to VMs
698 static bool i40e_vfs_are_assigned(struct i40e_pf *pf)
700 struct pci_dev *pdev = pf->pdev;
701 struct pci_dev *vfdev;
703 /* loop through all the VFs to see if we own any that are assigned */
704 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, I40E_VF_DEVICE_ID , NULL);
706 /* if we don't own it we don't care */
707 if (vfdev->is_virtfn && pci_physfn(vfdev) == pdev) {
708 /* if it is assigned we cannot release it */
709 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
713 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL,
723 * @pf: pointer to the pf structure
727 void i40e_free_vfs(struct i40e_pf *pf)
729 struct i40e_hw *hw = &pf->hw;
730 u32 reg_idx, bit_idx;
736 /* Disable interrupt 0 so we don't try to handle the VFLR. */
737 i40e_irq_dynamic_disable_icr0(pf);
739 mdelay(10); /* let any messages in transit get finished up */
740 /* free up vf resources */
741 tmp = pf->num_alloc_vfs;
742 pf->num_alloc_vfs = 0;
743 for (i = 0; i < tmp; i++) {
744 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
745 i40e_free_vf_res(&pf->vf[i]);
746 /* disable qp mappings */
747 i40e_disable_vf_mappings(&pf->vf[i]);
753 if (!i40e_vfs_are_assigned(pf)) {
754 pci_disable_sriov(pf->pdev);
755 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
756 * work correctly when SR-IOV gets re-enabled.
758 for (vf_id = 0; vf_id < tmp; vf_id++) {
759 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
760 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
761 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
765 dev_warn(&pf->pdev->dev,
766 "unable to disable SR-IOV because VFs are assigned.\n");
768 /* Re-enable interrupt 0. */
769 i40e_irq_dynamic_enable_icr0(pf);
772 #ifdef CONFIG_PCI_IOV
775 * @pf: pointer to the pf structure
776 * @num_alloc_vfs: number of vfs to allocate
778 * allocate vf resources
780 static int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
785 /* Disable interrupt 0 so we don't try to handle the VFLR. */
786 i40e_irq_dynamic_disable_icr0(pf);
788 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
790 dev_err(&pf->pdev->dev,
791 "pci_enable_sriov failed with error %d!\n", ret);
792 pf->num_alloc_vfs = 0;
796 /* allocate memory */
797 vfs = kzalloc(num_alloc_vfs * sizeof(struct i40e_vf), GFP_KERNEL);
803 /* apply default profile */
804 for (i = 0; i < num_alloc_vfs; i++) {
806 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
809 /* assign default capabilities */
810 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
811 /* vf resources get allocated during reset */
812 i40e_reset_vf(&vfs[i], false);
814 /* enable vf vplan_qtable mappings */
815 i40e_enable_vf_mappings(&vfs[i]);
818 pf->num_alloc_vfs = num_alloc_vfs;
824 /* Re-enable interrupt 0. */
825 i40e_irq_dynamic_enable_icr0(pf);
831 * i40e_pci_sriov_enable
832 * @pdev: pointer to a pci_dev structure
833 * @num_vfs: number of vfs to allocate
835 * Enable or change the number of VFs
837 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
839 #ifdef CONFIG_PCI_IOV
840 struct i40e_pf *pf = pci_get_drvdata(pdev);
841 int pre_existing_vfs = pci_num_vf(pdev);
844 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
845 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
847 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
850 if (num_vfs > pf->num_req_vfs) {
855 err = i40e_alloc_vfs(pf, num_vfs);
857 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
871 * i40e_pci_sriov_configure
872 * @pdev: pointer to a pci_dev structure
873 * @num_vfs: number of vfs to allocate
875 * Enable or change the number of VFs. Called when the user updates the number
878 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
880 struct i40e_pf *pf = pci_get_drvdata(pdev);
883 return i40e_pci_sriov_enable(pdev, num_vfs);
889 /***********************virtual channel routines******************/
892 * i40e_vc_send_msg_to_vf
893 * @vf: pointer to the vf info
894 * @v_opcode: virtual channel opcode
895 * @v_retval: virtual channel return value
896 * @msg: pointer to the msg buffer
897 * @msglen: msg length
901 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
902 u32 v_retval, u8 *msg, u16 msglen)
904 struct i40e_pf *pf = vf->pf;
905 struct i40e_hw *hw = &pf->hw;
906 int true_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
909 /* single place to detect unsuccessful return values */
911 vf->num_invalid_msgs++;
912 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
914 if (vf->num_invalid_msgs >
915 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
916 dev_err(&pf->pdev->dev,
917 "Number of invalid messages exceeded for VF %d\n",
919 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
920 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
923 vf->num_valid_msgs++;
926 aq_ret = i40e_aq_send_msg_to_vf(hw, true_vf_id, v_opcode, v_retval,
929 dev_err(&pf->pdev->dev,
930 "Unable to send the message to VF %d aq_err %d\n",
931 vf->vf_id, pf->hw.aq.asq_last_status);
939 * i40e_vc_send_resp_to_vf
940 * @vf: pointer to the vf info
941 * @opcode: operation code
942 * @retval: return value
944 * send resp msg to vf
946 static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
947 enum i40e_virtchnl_ops opcode,
950 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
954 * i40e_vc_get_version_msg
955 * @vf: pointer to the vf info
957 * called from the vf to request the API version used by the PF
959 static int i40e_vc_get_version_msg(struct i40e_vf *vf)
961 struct i40e_virtchnl_version_info info = {
962 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
965 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
966 I40E_SUCCESS, (u8 *)&info,
968 i40e_virtchnl_version_info));
972 * i40e_vc_get_vf_resources_msg
973 * @vf: pointer to the vf info
974 * @msg: pointer to the msg buffer
975 * @msglen: msg length
977 * called from the vf to request its resources
979 static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf)
981 struct i40e_virtchnl_vf_resource *vfres = NULL;
982 struct i40e_pf *pf = vf->pf;
983 i40e_status aq_ret = 0;
984 struct i40e_vsi *vsi;
989 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
990 aq_ret = I40E_ERR_PARAM;
994 len = (sizeof(struct i40e_virtchnl_vf_resource) +
995 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
997 vfres = kzalloc(len, GFP_KERNEL);
999 aq_ret = I40E_ERR_NO_MEMORY;
1004 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
1005 vsi = pf->vsi[vf->lan_vsi_index];
1006 if (!vsi->info.pvid)
1007 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1009 vfres->num_vsis = num_vsis;
1010 vfres->num_queue_pairs = vf->num_queue_pairs;
1011 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1012 if (vf->lan_vsi_index) {
1013 vfres->vsi_res[i].vsi_id = vf->lan_vsi_index;
1014 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1015 vfres->vsi_res[i].num_queue_pairs =
1016 pf->vsi[vf->lan_vsi_index]->num_queue_pairs;
1017 memcpy(vfres->vsi_res[i].default_mac_addr,
1018 vf->default_lan_addr.addr, ETH_ALEN);
1021 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1024 /* send the response back to the vf */
1025 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1026 aq_ret, (u8 *)vfres, len);
1033 * i40e_vc_reset_vf_msg
1034 * @vf: pointer to the vf info
1035 * @msg: pointer to the msg buffer
1036 * @msglen: msg length
1038 * called from the vf to reset itself,
1039 * unlike other virtchnl messages, pf driver
1040 * doesn't send the response back to the vf
1042 static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
1044 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1045 i40e_reset_vf(vf, false);
1049 * i40e_vc_config_promiscuous_mode_msg
1050 * @vf: pointer to the vf info
1051 * @msg: pointer to the msg buffer
1052 * @msglen: msg length
1054 * called from the vf to configure the promiscuous mode of
1057 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1058 u8 *msg, u16 msglen)
1060 struct i40e_virtchnl_promisc_info *info =
1061 (struct i40e_virtchnl_promisc_info *)msg;
1062 struct i40e_pf *pf = vf->pf;
1063 struct i40e_hw *hw = &pf->hw;
1064 bool allmulti = false;
1065 bool promisc = false;
1068 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1069 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1070 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1071 (pf->vsi[info->vsi_id]->type != I40E_VSI_FCOE)) {
1072 aq_ret = I40E_ERR_PARAM;
1076 if (info->flags & I40E_FLAG_VF_UNICAST_PROMISC)
1078 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, info->vsi_id,
1083 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1085 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, info->vsi_id,
1089 /* send the response to the vf */
1090 return i40e_vc_send_resp_to_vf(vf,
1091 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1096 * i40e_vc_config_queues_msg
1097 * @vf: pointer to the vf info
1098 * @msg: pointer to the msg buffer
1099 * @msglen: msg length
1101 * called from the vf to configure the rx/tx
1104 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1106 struct i40e_virtchnl_vsi_queue_config_info *qci =
1107 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1108 struct i40e_virtchnl_queue_pair_info *qpi;
1109 u16 vsi_id, vsi_queue_id;
1110 i40e_status aq_ret = 0;
1113 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1114 aq_ret = I40E_ERR_PARAM;
1118 vsi_id = qci->vsi_id;
1119 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1120 aq_ret = I40E_ERR_PARAM;
1123 for (i = 0; i < qci->num_queue_pairs; i++) {
1124 qpi = &qci->qpair[i];
1125 vsi_queue_id = qpi->txq.queue_id;
1126 if ((qpi->txq.vsi_id != vsi_id) ||
1127 (qpi->rxq.vsi_id != vsi_id) ||
1128 (qpi->rxq.queue_id != vsi_queue_id) ||
1129 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1130 aq_ret = I40E_ERR_PARAM;
1134 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1136 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1138 aq_ret = I40E_ERR_PARAM;
1144 /* send the response to the vf */
1145 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1150 * i40e_vc_config_irq_map_msg
1151 * @vf: pointer to the vf info
1152 * @msg: pointer to the msg buffer
1153 * @msglen: msg length
1155 * called from the vf to configure the irq to
1158 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1160 struct i40e_virtchnl_irq_map_info *irqmap_info =
1161 (struct i40e_virtchnl_irq_map_info *)msg;
1162 struct i40e_virtchnl_vector_map *map;
1163 u16 vsi_id, vsi_queue_id, vector_id;
1164 i40e_status aq_ret = 0;
1165 unsigned long tempmap;
1168 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1169 aq_ret = I40E_ERR_PARAM;
1173 for (i = 0; i < irqmap_info->num_vectors; i++) {
1174 map = &irqmap_info->vecmap[i];
1176 vector_id = map->vector_id;
1177 vsi_id = map->vsi_id;
1178 /* validate msg params */
1179 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1180 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1181 aq_ret = I40E_ERR_PARAM;
1185 /* lookout for the invalid queue index */
1186 tempmap = map->rxq_map;
1187 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1188 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1190 aq_ret = I40E_ERR_PARAM;
1195 tempmap = map->txq_map;
1196 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1197 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1199 aq_ret = I40E_ERR_PARAM;
1204 i40e_config_irq_link_list(vf, vsi_id, map);
1207 /* send the response to the vf */
1208 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1213 * i40e_vc_enable_queues_msg
1214 * @vf: pointer to the vf info
1215 * @msg: pointer to the msg buffer
1216 * @msglen: msg length
1218 * called from the vf to enable all or specific queue(s)
1220 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1222 struct i40e_virtchnl_queue_select *vqs =
1223 (struct i40e_virtchnl_queue_select *)msg;
1224 struct i40e_pf *pf = vf->pf;
1225 u16 vsi_id = vqs->vsi_id;
1226 i40e_status aq_ret = 0;
1228 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1229 aq_ret = I40E_ERR_PARAM;
1233 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1234 aq_ret = I40E_ERR_PARAM;
1238 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1239 aq_ret = I40E_ERR_PARAM;
1242 if (i40e_vsi_control_rings(pf->vsi[vsi_id], true))
1243 aq_ret = I40E_ERR_TIMEOUT;
1245 /* send the response to the vf */
1246 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1251 * i40e_vc_disable_queues_msg
1252 * @vf: pointer to the vf info
1253 * @msg: pointer to the msg buffer
1254 * @msglen: msg length
1256 * called from the vf to disable all or specific
1259 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1261 struct i40e_virtchnl_queue_select *vqs =
1262 (struct i40e_virtchnl_queue_select *)msg;
1263 struct i40e_pf *pf = vf->pf;
1264 u16 vsi_id = vqs->vsi_id;
1265 i40e_status aq_ret = 0;
1267 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1268 aq_ret = I40E_ERR_PARAM;
1272 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1273 aq_ret = I40E_ERR_PARAM;
1277 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1278 aq_ret = I40E_ERR_PARAM;
1281 if (i40e_vsi_control_rings(pf->vsi[vsi_id], false))
1282 aq_ret = I40E_ERR_TIMEOUT;
1285 /* send the response to the vf */
1286 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1291 * i40e_vc_get_stats_msg
1292 * @vf: pointer to the vf info
1293 * @msg: pointer to the msg buffer
1294 * @msglen: msg length
1296 * called from the vf to get vsi stats
1298 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1300 struct i40e_virtchnl_queue_select *vqs =
1301 (struct i40e_virtchnl_queue_select *)msg;
1302 struct i40e_pf *pf = vf->pf;
1303 struct i40e_eth_stats stats;
1304 i40e_status aq_ret = 0;
1305 struct i40e_vsi *vsi;
1307 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1309 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1310 aq_ret = I40E_ERR_PARAM;
1314 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1315 aq_ret = I40E_ERR_PARAM;
1319 vsi = pf->vsi[vqs->vsi_id];
1321 aq_ret = I40E_ERR_PARAM;
1324 i40e_update_eth_stats(vsi);
1325 stats = vsi->eth_stats;
1328 /* send the response back to the vf */
1329 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1330 (u8 *)&stats, sizeof(stats));
1334 * i40e_check_vf_permission
1335 * @vf: pointer to the vf info
1336 * @macaddr: pointer to the MAC Address being checked
1338 * Check if the VF has permission to add or delete unicast MAC address
1339 * filters and return error code -EPERM if not. Then check if the
1340 * address filter requested is broadcast or zero and if so return
1341 * an invalid MAC address error code.
1343 static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1345 struct i40e_pf *pf = vf->pf;
1348 if (is_broadcast_ether_addr(macaddr) ||
1349 is_zero_ether_addr(macaddr)) {
1350 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1351 ret = I40E_ERR_INVALID_MAC_ADDR;
1352 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr)) {
1353 /* If the host VMM administrator has set the VF MAC address
1354 * administratively via the ndo_set_vf_mac command then deny
1355 * permission to the VF to add or delete unicast MAC addresses.
1357 dev_err(&pf->pdev->dev,
1358 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1365 * i40e_vc_add_mac_addr_msg
1366 * @vf: pointer to the vf info
1367 * @msg: pointer to the msg buffer
1368 * @msglen: msg length
1370 * add guest mac address filter
1372 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1374 struct i40e_virtchnl_ether_addr_list *al =
1375 (struct i40e_virtchnl_ether_addr_list *)msg;
1376 struct i40e_pf *pf = vf->pf;
1377 struct i40e_vsi *vsi = NULL;
1378 u16 vsi_id = al->vsi_id;
1379 i40e_status ret = 0;
1382 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1383 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1384 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1385 ret = I40E_ERR_PARAM;
1389 for (i = 0; i < al->num_elements; i++) {
1390 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1394 vsi = pf->vsi[vsi_id];
1396 /* add new addresses to the list */
1397 for (i = 0; i < al->num_elements; i++) {
1398 struct i40e_mac_filter *f;
1400 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
1402 if (i40e_is_vsi_in_vlan(vsi))
1403 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1406 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1411 dev_err(&pf->pdev->dev,
1412 "Unable to add VF MAC filter\n");
1413 ret = I40E_ERR_PARAM;
1418 /* program the updated filter list */
1419 if (i40e_sync_vsi_filters(vsi))
1420 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1423 /* send the response to the vf */
1424 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
1429 * i40e_vc_del_mac_addr_msg
1430 * @vf: pointer to the vf info
1431 * @msg: pointer to the msg buffer
1432 * @msglen: msg length
1434 * remove guest mac address filter
1436 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1438 struct i40e_virtchnl_ether_addr_list *al =
1439 (struct i40e_virtchnl_ether_addr_list *)msg;
1440 struct i40e_pf *pf = vf->pf;
1441 struct i40e_vsi *vsi = NULL;
1442 u16 vsi_id = al->vsi_id;
1443 i40e_status ret = 0;
1446 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1447 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1448 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1449 ret = I40E_ERR_PARAM;
1453 for (i = 0; i < al->num_elements; i++) {
1454 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1458 vsi = pf->vsi[vsi_id];
1460 /* delete addresses from the list */
1461 for (i = 0; i < al->num_elements; i++)
1462 i40e_del_filter(vsi, al->list[i].addr,
1463 I40E_VLAN_ANY, true, false);
1465 /* program the updated filter list */
1466 if (i40e_sync_vsi_filters(vsi))
1467 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1470 /* send the response to the vf */
1471 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
1476 * i40e_vc_add_vlan_msg
1477 * @vf: pointer to the vf info
1478 * @msg: pointer to the msg buffer
1479 * @msglen: msg length
1481 * program guest vlan id
1483 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1485 struct i40e_virtchnl_vlan_filter_list *vfl =
1486 (struct i40e_virtchnl_vlan_filter_list *)msg;
1487 struct i40e_pf *pf = vf->pf;
1488 struct i40e_vsi *vsi = NULL;
1489 u16 vsi_id = vfl->vsi_id;
1490 i40e_status aq_ret = 0;
1493 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1494 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1495 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1496 aq_ret = I40E_ERR_PARAM;
1500 for (i = 0; i < vfl->num_elements; i++) {
1501 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1502 aq_ret = I40E_ERR_PARAM;
1503 dev_err(&pf->pdev->dev,
1504 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1508 vsi = pf->vsi[vsi_id];
1509 if (vsi->info.pvid) {
1510 aq_ret = I40E_ERR_PARAM;
1514 i40e_vlan_stripping_enable(vsi);
1515 for (i = 0; i < vfl->num_elements; i++) {
1516 /* add new VLAN filter */
1517 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1519 dev_err(&pf->pdev->dev,
1520 "Unable to add VF vlan filter %d, error %d\n",
1521 vfl->vlan_id[i], ret);
1525 /* send the response to the vf */
1526 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1530 * i40e_vc_remove_vlan_msg
1531 * @vf: pointer to the vf info
1532 * @msg: pointer to the msg buffer
1533 * @msglen: msg length
1535 * remove programmed guest vlan id
1537 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1539 struct i40e_virtchnl_vlan_filter_list *vfl =
1540 (struct i40e_virtchnl_vlan_filter_list *)msg;
1541 struct i40e_pf *pf = vf->pf;
1542 struct i40e_vsi *vsi = NULL;
1543 u16 vsi_id = vfl->vsi_id;
1544 i40e_status aq_ret = 0;
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 aq_ret = I40E_ERR_PARAM;
1554 for (i = 0; i < vfl->num_elements; i++) {
1555 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1556 aq_ret = I40E_ERR_PARAM;
1561 vsi = pf->vsi[vsi_id];
1562 if (vsi->info.pvid) {
1563 aq_ret = I40E_ERR_PARAM;
1567 for (i = 0; i < vfl->num_elements; i++) {
1568 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1570 dev_err(&pf->pdev->dev,
1571 "Unable to delete VF vlan filter %d, error %d\n",
1572 vfl->vlan_id[i], ret);
1576 /* send the response to the vf */
1577 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1581 * i40e_vc_validate_vf_msg
1582 * @vf: pointer to the vf info
1583 * @msg: pointer to the msg buffer
1584 * @msglen: msg length
1585 * @msghndl: msg handle
1589 static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1590 u32 v_retval, u8 *msg, u16 msglen)
1592 bool err_msg_format = false;
1595 /* Check if VF is disabled. */
1596 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1597 return I40E_ERR_PARAM;
1599 /* Validate message length. */
1601 case I40E_VIRTCHNL_OP_VERSION:
1602 valid_len = sizeof(struct i40e_virtchnl_version_info);
1604 case I40E_VIRTCHNL_OP_RESET_VF:
1605 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1608 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1609 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1611 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1612 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1614 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1615 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1616 if (msglen >= valid_len) {
1617 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1618 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1619 valid_len += (vqc->num_queue_pairs *
1621 i40e_virtchnl_queue_pair_info));
1622 if (vqc->num_queue_pairs == 0)
1623 err_msg_format = true;
1626 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1627 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1628 if (msglen >= valid_len) {
1629 struct i40e_virtchnl_irq_map_info *vimi =
1630 (struct i40e_virtchnl_irq_map_info *)msg;
1631 valid_len += (vimi->num_vectors *
1632 sizeof(struct i40e_virtchnl_vector_map));
1633 if (vimi->num_vectors == 0)
1634 err_msg_format = true;
1637 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1638 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1639 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1641 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1642 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1643 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1644 if (msglen >= valid_len) {
1645 struct i40e_virtchnl_ether_addr_list *veal =
1646 (struct i40e_virtchnl_ether_addr_list *)msg;
1647 valid_len += veal->num_elements *
1648 sizeof(struct i40e_virtchnl_ether_addr);
1649 if (veal->num_elements == 0)
1650 err_msg_format = true;
1653 case I40E_VIRTCHNL_OP_ADD_VLAN:
1654 case I40E_VIRTCHNL_OP_DEL_VLAN:
1655 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1656 if (msglen >= valid_len) {
1657 struct i40e_virtchnl_vlan_filter_list *vfl =
1658 (struct i40e_virtchnl_vlan_filter_list *)msg;
1659 valid_len += vfl->num_elements * sizeof(u16);
1660 if (vfl->num_elements == 0)
1661 err_msg_format = true;
1664 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1665 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1667 case I40E_VIRTCHNL_OP_GET_STATS:
1668 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1670 /* These are always errors coming from the VF. */
1671 case I40E_VIRTCHNL_OP_EVENT:
1672 case I40E_VIRTCHNL_OP_UNKNOWN:
1677 /* few more checks */
1678 if ((valid_len != msglen) || (err_msg_format)) {
1679 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1687 * i40e_vc_process_vf_msg
1688 * @pf: pointer to the pf structure
1689 * @vf_id: source vf id
1690 * @msg: pointer to the msg buffer
1691 * @msglen: msg length
1692 * @msghndl: msg handle
1694 * called from the common aeq/arq handler to
1695 * process request from vf
1697 int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1698 u32 v_retval, u8 *msg, u16 msglen)
1700 struct i40e_hw *hw = &pf->hw;
1701 int local_vf_id = vf_id - hw->func_caps.vf_base_id;
1705 pf->vf_aq_requests++;
1706 if (local_vf_id >= pf->num_alloc_vfs)
1708 vf = &(pf->vf[local_vf_id]);
1709 /* perform basic checks on the msg */
1710 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1713 dev_err(&pf->pdev->dev, "Invalid message from vf %d, opcode %d, len %d\n",
1714 local_vf_id, v_opcode, msglen);
1717 wr32(hw, I40E_VFGEN_RSTAT1(local_vf_id), I40E_VFR_VFACTIVE);
1719 case I40E_VIRTCHNL_OP_VERSION:
1720 ret = i40e_vc_get_version_msg(vf);
1722 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1723 ret = i40e_vc_get_vf_resources_msg(vf);
1725 case I40E_VIRTCHNL_OP_RESET_VF:
1726 i40e_vc_reset_vf_msg(vf);
1729 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1730 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1732 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1733 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1735 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1736 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1738 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1739 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
1741 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1742 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1744 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1745 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1747 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1748 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1750 case I40E_VIRTCHNL_OP_ADD_VLAN:
1751 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1753 case I40E_VIRTCHNL_OP_DEL_VLAN:
1754 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1756 case I40E_VIRTCHNL_OP_GET_STATS:
1757 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1759 case I40E_VIRTCHNL_OP_UNKNOWN:
1761 dev_err(&pf->pdev->dev, "Unsupported opcode %d from vf %d\n",
1762 v_opcode, local_vf_id);
1763 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1764 I40E_ERR_NOT_IMPLEMENTED);
1772 * i40e_vc_process_vflr_event
1773 * @pf: pointer to the pf structure
1775 * called from the vlfr irq handler to
1776 * free up vf resources and state variables
1778 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1780 u32 reg, reg_idx, bit_idx, vf_id;
1781 struct i40e_hw *hw = &pf->hw;
1784 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1787 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1788 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1789 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1790 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1791 /* read GLGEN_VFLRSTAT register to find out the flr vfs */
1792 vf = &pf->vf[vf_id];
1793 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
1794 if (reg & (1 << bit_idx)) {
1795 /* clear the bit in GLGEN_VFLRSTAT */
1796 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
1798 i40e_reset_vf(vf, true);
1802 /* re-enable vflr interrupt cause */
1803 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1804 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1805 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1812 * i40e_vc_vf_broadcast
1813 * @pf: pointer to the pf structure
1814 * @opcode: operation code
1815 * @retval: return value
1816 * @msg: pointer to the msg buffer
1817 * @msglen: msg length
1819 * send a message to all VFs on a given PF
1821 static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
1822 enum i40e_virtchnl_ops v_opcode,
1823 i40e_status v_retval, u8 *msg,
1826 struct i40e_hw *hw = &pf->hw;
1827 struct i40e_vf *vf = pf->vf;
1830 for (i = 0; i < pf->num_alloc_vfs; i++) {
1831 /* Ignore return value on purpose - a given VF may fail, but
1832 * we need to keep going and send to all of them
1834 i40e_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval,
1841 * i40e_vc_notify_link_state
1842 * @pf: pointer to the pf structure
1844 * send a link status message to all VFs on a given PF
1846 void i40e_vc_notify_link_state(struct i40e_pf *pf)
1848 struct i40e_virtchnl_pf_event pfe;
1850 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
1851 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
1852 pfe.event_data.link_event.link_status =
1853 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
1854 pfe.event_data.link_event.link_speed = pf->hw.phy.link_info.link_speed;
1856 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
1857 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
1861 * i40e_vc_notify_reset
1862 * @pf: pointer to the pf structure
1864 * indicate a pending reset to all VFs on a given PF
1866 void i40e_vc_notify_reset(struct i40e_pf *pf)
1868 struct i40e_virtchnl_pf_event pfe;
1870 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1871 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
1872 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
1873 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
1877 * i40e_vc_notify_vf_reset
1878 * @vf: pointer to the vf structure
1880 * indicate a pending reset to the given VF
1882 void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
1884 struct i40e_virtchnl_pf_event pfe;
1886 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1887 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
1888 i40e_aq_send_msg_to_vf(&vf->pf->hw, vf->vf_id, I40E_VIRTCHNL_OP_EVENT,
1889 I40E_SUCCESS, (u8 *)&pfe,
1890 sizeof(struct i40e_virtchnl_pf_event), NULL);
1894 * i40e_ndo_set_vf_mac
1895 * @netdev: network interface device structure
1896 * @vf_id: vf identifier
1899 * program vf mac address
1901 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
1903 struct i40e_netdev_priv *np = netdev_priv(netdev);
1904 struct i40e_vsi *vsi = np->vsi;
1905 struct i40e_pf *pf = vsi->back;
1906 struct i40e_mac_filter *f;
1910 /* validate the request */
1911 if (vf_id >= pf->num_alloc_vfs) {
1912 dev_err(&pf->pdev->dev,
1913 "Invalid VF Identifier %d\n", vf_id);
1918 vf = &(pf->vf[vf_id]);
1919 vsi = pf->vsi[vf->lan_vsi_index];
1920 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1921 dev_err(&pf->pdev->dev,
1922 "Uninitialized VF %d\n", vf_id);
1927 if (!is_valid_ether_addr(mac)) {
1928 dev_err(&pf->pdev->dev,
1929 "Invalid VF ethernet address\n");
1934 /* delete the temporary mac address */
1935 i40e_del_filter(vsi, vf->default_lan_addr.addr, 0, true, false);
1937 /* add the new mac address */
1938 f = i40e_add_filter(vsi, mac, 0, true, false);
1940 dev_err(&pf->pdev->dev,
1941 "Unable to add VF ucast filter\n");
1946 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
1947 /* program mac filter */
1948 if (i40e_sync_vsi_filters(vsi)) {
1949 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
1953 memcpy(vf->default_lan_addr.addr, mac, ETH_ALEN);
1954 vf->pf_set_mac = true;
1955 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
1963 * i40e_ndo_set_vf_port_vlan
1964 * @netdev: network interface device structure
1965 * @vf_id: vf identifier
1966 * @vlan_id: mac address
1967 * @qos: priority setting
1969 * program vf vlan id and/or qos
1971 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
1972 int vf_id, u16 vlan_id, u8 qos)
1974 struct i40e_netdev_priv *np = netdev_priv(netdev);
1975 struct i40e_pf *pf = np->vsi->back;
1976 struct i40e_vsi *vsi;
1980 /* validate the request */
1981 if (vf_id >= pf->num_alloc_vfs) {
1982 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
1987 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
1988 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
1993 vf = &(pf->vf[vf_id]);
1994 vsi = pf->vsi[vf->lan_vsi_index];
1995 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1996 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2001 if (vsi->info.pvid) {
2003 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2006 dev_info(&vsi->back->pdev->dev,
2007 "remove VLAN failed, ret=%d, aq_err=%d\n",
2008 ret, pf->hw.aq.asq_last_status);
2012 ret = i40e_vsi_add_pvid(vsi,
2013 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
2015 i40e_vsi_remove_pvid(vsi);
2018 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2019 vlan_id, qos, vf_id);
2021 /* add new VLAN filter */
2022 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2024 dev_info(&vsi->back->pdev->dev,
2025 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2026 vsi->back->hw.aq.asq_last_status);
2032 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2035 /* The Port VLAN needs to be saved across resets the same as the
2036 * default LAN MAC address.
2038 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
2046 * i40e_ndo_set_vf_bw
2047 * @netdev: network interface device structure
2048 * @vf_id: vf identifier
2051 * configure vf tx rate
2053 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int tx_rate)
2059 * i40e_ndo_get_vf_config
2060 * @netdev: network interface device structure
2061 * @vf_id: vf identifier
2062 * @ivi: vf configuration structure
2064 * return vf configuration
2066 int i40e_ndo_get_vf_config(struct net_device *netdev,
2067 int vf_id, struct ifla_vf_info *ivi)
2069 struct i40e_netdev_priv *np = netdev_priv(netdev);
2070 struct i40e_vsi *vsi = np->vsi;
2071 struct i40e_pf *pf = vsi->back;
2075 /* validate the request */
2076 if (vf_id >= pf->num_alloc_vfs) {
2077 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2082 vf = &(pf->vf[vf_id]);
2083 /* first vsi is always the LAN vsi */
2084 vsi = pf->vsi[vf->lan_vsi_index];
2085 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2086 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2093 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
2096 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2097 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2098 I40E_VLAN_PRIORITY_SHIFT;