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 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
396 dev_err(&pf->pdev->dev, "Unable to add ucast filter\n");
398 goto error_alloc_vsi_res;
401 /* program mac filter */
402 ret = i40e_sync_vsi_filters(vsi);
404 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
405 goto error_alloc_vsi_res;
408 /* accept bcast pkts. by default */
409 ret = i40e_aq_set_vsi_broadcast(hw, vsi->seid, true, NULL);
411 dev_err(&pf->pdev->dev,
412 "set vsi bcast failed for vf %d, vsi %d, aq_err %d\n",
413 vf->vf_id, vsi->idx, pf->hw.aq.asq_last_status);
422 * i40e_enable_vf_mappings
423 * @vf: pointer to the vf info
427 static void i40e_enable_vf_mappings(struct i40e_vf *vf)
429 struct i40e_pf *pf = vf->pf;
430 struct i40e_hw *hw = &pf->hw;
431 u32 reg, total_queue_pairs = 0;
434 /* Tell the hardware we're using noncontiguous mapping. HW requires
435 * that VF queues be mapped using this method, even when they are
436 * contiguous in real life
438 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
439 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
441 /* enable VF vplan_qtable mappings */
442 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
443 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
445 /* map PF queues to VF queues */
446 for (j = 0; j < pf->vsi[vf->lan_vsi_index]->num_queue_pairs; j++) {
447 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index, j);
448 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
449 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
453 /* map PF queues to VSI */
454 for (j = 0; j < 7; j++) {
455 if (j * 2 >= pf->vsi[vf->lan_vsi_index]->num_queue_pairs) {
456 reg = 0x07FF07FF; /* unused */
458 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index,
461 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index,
465 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
472 * i40e_disable_vf_mappings
473 * @vf: pointer to the vf info
475 * disable vf mappings
477 static void i40e_disable_vf_mappings(struct i40e_vf *vf)
479 struct i40e_pf *pf = vf->pf;
480 struct i40e_hw *hw = &pf->hw;
483 /* disable qp mappings */
484 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
485 for (i = 0; i < I40E_MAX_VSI_QP; i++)
486 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
487 I40E_QUEUE_END_OF_LIST);
493 * @vf: pointer to the vf info
497 static void i40e_free_vf_res(struct i40e_vf *vf)
499 struct i40e_pf *pf = vf->pf;
500 struct i40e_hw *hw = &pf->hw;
504 /* free vsi & disconnect it from the parent uplink */
505 if (vf->lan_vsi_index) {
506 i40e_vsi_release(pf->vsi[vf->lan_vsi_index]);
507 vf->lan_vsi_index = 0;
510 msix_vf = pf->hw.func_caps.num_msix_vectors_vf + 1;
511 /* disable interrupts so the VF starts in a known state */
512 for (i = 0; i < msix_vf; i++) {
513 /* format is same for both registers */
515 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
517 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
520 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
524 /* clear the irq settings */
525 for (i = 0; i < msix_vf; i++) {
526 /* format is same for both registers */
528 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
530 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
533 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
534 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
535 wr32(hw, reg_idx, reg);
538 /* reset some of the state varibles keeping
539 * track of the resources
541 vf->num_queue_pairs = 0;
547 * @vf: pointer to the vf info
549 * allocate vf resources
551 static int i40e_alloc_vf_res(struct i40e_vf *vf)
553 struct i40e_pf *pf = vf->pf;
554 int total_queue_pairs = 0;
557 /* allocate hw vsi context & associated resources */
558 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
561 total_queue_pairs += pf->vsi[vf->lan_vsi_index]->num_queue_pairs;
562 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
564 /* store the total qps number for the runtime
567 vf->num_queue_pairs = total_queue_pairs;
569 /* vf is now completely initialized */
570 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
574 i40e_free_vf_res(vf);
579 #define VF_DEVICE_STATUS 0xAA
580 #define VF_TRANS_PENDING_MASK 0x20
582 * i40e_quiesce_vf_pci
583 * @vf: pointer to the vf structure
585 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
586 * if the transactions never clear.
588 static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
590 struct i40e_pf *pf = vf->pf;
591 struct i40e_hw *hw = &pf->hw;
595 reg = rd32(hw, I40E_PF_VT_PFALLOC);
596 vf_abs_id = vf->vf_id + (reg & I40E_PF_VT_PFALLOC_FIRSTVF_MASK);
598 wr32(hw, I40E_PF_PCI_CIAA,
599 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
600 for (i = 0; i < 100; i++) {
601 reg = rd32(hw, I40E_PF_PCI_CIAD);
602 if ((reg & VF_TRANS_PENDING_MASK) == 0)
611 * @vf: pointer to the vf structure
612 * @flr: VFLR was issued or not
616 void i40e_reset_vf(struct i40e_vf *vf, bool flr)
618 struct i40e_pf *pf = vf->pf;
619 struct i40e_hw *hw = &pf->hw;
625 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
627 /* In the case of a VFLR, the HW has already reset the VF and we
628 * just need to clean up, so don't hit the VFRTRIG register.
631 /* reset vf using VPGEN_VFRTRIG reg */
632 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
633 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
634 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
638 if (i40e_quiesce_vf_pci(vf))
639 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
642 /* poll VPGEN_VFRSTAT reg to make sure
643 * that reset is complete
645 for (i = 0; i < 100; i++) {
646 /* vf reset requires driver to first reset the
647 * vf & than poll the status register to make sure
648 * that the requested op was completed
652 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
653 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
660 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
662 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
663 /* clear the reset bit in the VPGEN_VFRTRIG reg */
664 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
665 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
666 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
668 /* On initial reset, we won't have any queues */
669 if (vf->lan_vsi_index == 0)
672 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_index], false);
674 /* reallocate vf resources to reset the VSI state */
675 i40e_free_vf_res(vf);
677 i40e_alloc_vf_res(vf);
678 i40e_enable_vf_mappings(vf);
680 /* tell the VF the reset is done */
681 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
686 * i40e_vfs_are_assigned
687 * @pf: pointer to the pf structure
689 * Determine if any VFs are assigned to VMs
691 static bool i40e_vfs_are_assigned(struct i40e_pf *pf)
693 struct pci_dev *pdev = pf->pdev;
694 struct pci_dev *vfdev;
696 /* loop through all the VFs to see if we own any that are assigned */
697 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, I40E_VF_DEVICE_ID , NULL);
699 /* if we don't own it we don't care */
700 if (vfdev->is_virtfn && pci_physfn(vfdev) == pdev) {
701 /* if it is assigned we cannot release it */
702 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
706 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL,
716 * @pf: pointer to the pf structure
720 void i40e_free_vfs(struct i40e_pf *pf)
727 /* Disable interrupt 0 so we don't try to handle the VFLR. */
728 i40e_irq_dynamic_disable_icr0(pf);
730 mdelay(10); /* let any messages in transit get finished up */
731 /* free up vf resources */
732 tmp = pf->num_alloc_vfs;
733 pf->num_alloc_vfs = 0;
734 for (i = 0; i < tmp; i++) {
735 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
736 i40e_free_vf_res(&pf->vf[i]);
737 /* disable qp mappings */
738 i40e_disable_vf_mappings(&pf->vf[i]);
744 if (!i40e_vfs_are_assigned(pf))
745 pci_disable_sriov(pf->pdev);
747 dev_warn(&pf->pdev->dev,
748 "unable to disable SR-IOV because VFs are assigned.\n");
750 /* Re-enable interrupt 0. */
751 i40e_irq_dynamic_enable_icr0(pf);
754 #ifdef CONFIG_PCI_IOV
757 * @pf: pointer to the pf structure
758 * @num_alloc_vfs: number of vfs to allocate
760 * allocate vf resources
762 static int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
767 /* Disable interrupt 0 so we don't try to handle the VFLR. */
768 i40e_irq_dynamic_disable_icr0(pf);
770 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
772 dev_err(&pf->pdev->dev,
773 "pci_enable_sriov failed with error %d!\n", ret);
774 pf->num_alloc_vfs = 0;
778 /* allocate memory */
779 vfs = kzalloc(num_alloc_vfs * sizeof(struct i40e_vf), GFP_KERNEL);
785 /* apply default profile */
786 for (i = 0; i < num_alloc_vfs; i++) {
788 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
791 /* assign default capabilities */
792 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
793 /* vf resources get allocated during reset */
794 i40e_reset_vf(&vfs[i], false);
796 /* enable vf vplan_qtable mappings */
797 i40e_enable_vf_mappings(&vfs[i]);
800 pf->num_alloc_vfs = num_alloc_vfs;
806 /* Re-enable interrupt 0. */
807 i40e_irq_dynamic_enable_icr0(pf);
813 * i40e_pci_sriov_enable
814 * @pdev: pointer to a pci_dev structure
815 * @num_vfs: number of vfs to allocate
817 * Enable or change the number of VFs
819 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
821 #ifdef CONFIG_PCI_IOV
822 struct i40e_pf *pf = pci_get_drvdata(pdev);
823 int pre_existing_vfs = pci_num_vf(pdev);
826 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
827 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
829 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
832 if (num_vfs > pf->num_req_vfs) {
837 err = i40e_alloc_vfs(pf, num_vfs);
839 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
853 * i40e_pci_sriov_configure
854 * @pdev: pointer to a pci_dev structure
855 * @num_vfs: number of vfs to allocate
857 * Enable or change the number of VFs. Called when the user updates the number
860 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
862 struct i40e_pf *pf = pci_get_drvdata(pdev);
865 return i40e_pci_sriov_enable(pdev, num_vfs);
871 /***********************virtual channel routines******************/
874 * i40e_vc_send_msg_to_vf
875 * @vf: pointer to the vf info
876 * @v_opcode: virtual channel opcode
877 * @v_retval: virtual channel return value
878 * @msg: pointer to the msg buffer
879 * @msglen: msg length
883 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
884 u32 v_retval, u8 *msg, u16 msglen)
886 struct i40e_pf *pf = vf->pf;
887 struct i40e_hw *hw = &pf->hw;
890 /* single place to detect unsuccessful return values */
892 vf->num_invalid_msgs++;
893 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
895 if (vf->num_invalid_msgs >
896 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
897 dev_err(&pf->pdev->dev,
898 "Number of invalid messages exceeded for VF %d\n",
900 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
901 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
904 vf->num_valid_msgs++;
907 aq_ret = i40e_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval,
910 dev_err(&pf->pdev->dev,
911 "Unable to send the message to VF %d aq_err %d\n",
912 vf->vf_id, pf->hw.aq.asq_last_status);
920 * i40e_vc_send_resp_to_vf
921 * @vf: pointer to the vf info
922 * @opcode: operation code
923 * @retval: return value
925 * send resp msg to vf
927 static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
928 enum i40e_virtchnl_ops opcode,
931 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
935 * i40e_vc_get_version_msg
936 * @vf: pointer to the vf info
938 * called from the vf to request the API version used by the PF
940 static int i40e_vc_get_version_msg(struct i40e_vf *vf)
942 struct i40e_virtchnl_version_info info = {
943 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
946 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
947 I40E_SUCCESS, (u8 *)&info,
949 i40e_virtchnl_version_info));
953 * i40e_vc_get_vf_resources_msg
954 * @vf: pointer to the vf info
955 * @msg: pointer to the msg buffer
956 * @msglen: msg length
958 * called from the vf to request its resources
960 static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf)
962 struct i40e_virtchnl_vf_resource *vfres = NULL;
963 struct i40e_pf *pf = vf->pf;
964 i40e_status aq_ret = 0;
965 struct i40e_vsi *vsi;
970 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
971 aq_ret = I40E_ERR_PARAM;
975 len = (sizeof(struct i40e_virtchnl_vf_resource) +
976 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
978 vfres = kzalloc(len, GFP_KERNEL);
980 aq_ret = I40E_ERR_NO_MEMORY;
985 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
986 vsi = pf->vsi[vf->lan_vsi_index];
988 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
990 vfres->num_vsis = num_vsis;
991 vfres->num_queue_pairs = vf->num_queue_pairs;
992 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
993 if (vf->lan_vsi_index) {
994 vfres->vsi_res[i].vsi_id = vf->lan_vsi_index;
995 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
996 vfres->vsi_res[i].num_queue_pairs =
997 pf->vsi[vf->lan_vsi_index]->num_queue_pairs;
998 memcpy(vfres->vsi_res[i].default_mac_addr,
999 vf->default_lan_addr.addr, ETH_ALEN);
1002 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1005 /* send the response back to the vf */
1006 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1007 aq_ret, (u8 *)vfres, len);
1014 * i40e_vc_reset_vf_msg
1015 * @vf: pointer to the vf info
1016 * @msg: pointer to the msg buffer
1017 * @msglen: msg length
1019 * called from the vf to reset itself,
1020 * unlike other virtchnl messages, pf driver
1021 * doesn't send the response back to the vf
1023 static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
1025 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1026 i40e_reset_vf(vf, false);
1030 * i40e_vc_config_promiscuous_mode_msg
1031 * @vf: pointer to the vf info
1032 * @msg: pointer to the msg buffer
1033 * @msglen: msg length
1035 * called from the vf to configure the promiscuous mode of
1038 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1039 u8 *msg, u16 msglen)
1041 struct i40e_virtchnl_promisc_info *info =
1042 (struct i40e_virtchnl_promisc_info *)msg;
1043 struct i40e_pf *pf = vf->pf;
1044 struct i40e_hw *hw = &pf->hw;
1045 bool allmulti = false;
1046 bool promisc = false;
1049 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1050 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1051 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1052 (pf->vsi[info->vsi_id]->type != I40E_VSI_FCOE)) {
1053 aq_ret = I40E_ERR_PARAM;
1057 if (info->flags & I40E_FLAG_VF_UNICAST_PROMISC)
1059 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, info->vsi_id,
1064 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1066 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, info->vsi_id,
1070 /* send the response to the vf */
1071 return i40e_vc_send_resp_to_vf(vf,
1072 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1077 * i40e_vc_config_queues_msg
1078 * @vf: pointer to the vf info
1079 * @msg: pointer to the msg buffer
1080 * @msglen: msg length
1082 * called from the vf to configure the rx/tx
1085 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1087 struct i40e_virtchnl_vsi_queue_config_info *qci =
1088 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1089 struct i40e_virtchnl_queue_pair_info *qpi;
1090 u16 vsi_id, vsi_queue_id;
1091 i40e_status aq_ret = 0;
1094 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1095 aq_ret = I40E_ERR_PARAM;
1099 vsi_id = qci->vsi_id;
1100 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1101 aq_ret = I40E_ERR_PARAM;
1104 for (i = 0; i < qci->num_queue_pairs; i++) {
1105 qpi = &qci->qpair[i];
1106 vsi_queue_id = qpi->txq.queue_id;
1107 if ((qpi->txq.vsi_id != vsi_id) ||
1108 (qpi->rxq.vsi_id != vsi_id) ||
1109 (qpi->rxq.queue_id != vsi_queue_id) ||
1110 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1111 aq_ret = I40E_ERR_PARAM;
1115 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1117 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1119 aq_ret = I40E_ERR_PARAM;
1125 /* send the response to the vf */
1126 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1131 * i40e_vc_config_irq_map_msg
1132 * @vf: pointer to the vf info
1133 * @msg: pointer to the msg buffer
1134 * @msglen: msg length
1136 * called from the vf to configure the irq to
1139 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1141 struct i40e_virtchnl_irq_map_info *irqmap_info =
1142 (struct i40e_virtchnl_irq_map_info *)msg;
1143 struct i40e_virtchnl_vector_map *map;
1144 u16 vsi_id, vsi_queue_id, vector_id;
1145 i40e_status aq_ret = 0;
1146 unsigned long tempmap;
1149 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1150 aq_ret = I40E_ERR_PARAM;
1154 for (i = 0; i < irqmap_info->num_vectors; i++) {
1155 map = &irqmap_info->vecmap[i];
1157 vector_id = map->vector_id;
1158 vsi_id = map->vsi_id;
1159 /* validate msg params */
1160 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1161 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1162 aq_ret = I40E_ERR_PARAM;
1166 /* lookout for the invalid queue index */
1167 tempmap = map->rxq_map;
1168 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1169 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1171 aq_ret = I40E_ERR_PARAM;
1176 tempmap = map->txq_map;
1177 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1178 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1180 aq_ret = I40E_ERR_PARAM;
1185 i40e_config_irq_link_list(vf, vsi_id, map);
1188 /* send the response to the vf */
1189 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1194 * i40e_vc_enable_queues_msg
1195 * @vf: pointer to the vf info
1196 * @msg: pointer to the msg buffer
1197 * @msglen: msg length
1199 * called from the vf to enable all or specific queue(s)
1201 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1203 struct i40e_virtchnl_queue_select *vqs =
1204 (struct i40e_virtchnl_queue_select *)msg;
1205 struct i40e_pf *pf = vf->pf;
1206 u16 vsi_id = vqs->vsi_id;
1207 i40e_status aq_ret = 0;
1209 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1210 aq_ret = I40E_ERR_PARAM;
1214 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1215 aq_ret = I40E_ERR_PARAM;
1219 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1220 aq_ret = I40E_ERR_PARAM;
1223 if (i40e_vsi_control_rings(pf->vsi[vsi_id], true))
1224 aq_ret = I40E_ERR_TIMEOUT;
1226 /* send the response to the vf */
1227 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1232 * i40e_vc_disable_queues_msg
1233 * @vf: pointer to the vf info
1234 * @msg: pointer to the msg buffer
1235 * @msglen: msg length
1237 * called from the vf to disable all or specific
1240 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1242 struct i40e_virtchnl_queue_select *vqs =
1243 (struct i40e_virtchnl_queue_select *)msg;
1244 struct i40e_pf *pf = vf->pf;
1245 u16 vsi_id = vqs->vsi_id;
1246 i40e_status aq_ret = 0;
1248 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1249 aq_ret = I40E_ERR_PARAM;
1253 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1254 aq_ret = I40E_ERR_PARAM;
1258 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1259 aq_ret = I40E_ERR_PARAM;
1262 if (i40e_vsi_control_rings(pf->vsi[vsi_id], false))
1263 aq_ret = I40E_ERR_TIMEOUT;
1266 /* send the response to the vf */
1267 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1272 * i40e_vc_get_stats_msg
1273 * @vf: pointer to the vf info
1274 * @msg: pointer to the msg buffer
1275 * @msglen: msg length
1277 * called from the vf to get vsi stats
1279 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1281 struct i40e_virtchnl_queue_select *vqs =
1282 (struct i40e_virtchnl_queue_select *)msg;
1283 struct i40e_pf *pf = vf->pf;
1284 struct i40e_eth_stats stats;
1285 i40e_status aq_ret = 0;
1286 struct i40e_vsi *vsi;
1288 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1290 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1291 aq_ret = I40E_ERR_PARAM;
1295 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1296 aq_ret = I40E_ERR_PARAM;
1300 vsi = pf->vsi[vqs->vsi_id];
1302 aq_ret = I40E_ERR_PARAM;
1305 i40e_update_eth_stats(vsi);
1306 memcpy(&stats, &vsi->eth_stats, sizeof(struct i40e_eth_stats));
1309 /* send the response back to the vf */
1310 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1311 (u8 *)&stats, sizeof(stats));
1315 * i40e_vc_add_mac_addr_msg
1316 * @vf: pointer to the vf info
1317 * @msg: pointer to the msg buffer
1318 * @msglen: msg length
1320 * add guest mac address filter
1322 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1324 struct i40e_virtchnl_ether_addr_list *al =
1325 (struct i40e_virtchnl_ether_addr_list *)msg;
1326 struct i40e_pf *pf = vf->pf;
1327 struct i40e_vsi *vsi = NULL;
1328 u16 vsi_id = al->vsi_id;
1329 i40e_status aq_ret = 0;
1332 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1333 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1334 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1335 aq_ret = I40E_ERR_PARAM;
1339 for (i = 0; i < al->num_elements; i++) {
1340 if (is_broadcast_ether_addr(al->list[i].addr) ||
1341 is_zero_ether_addr(al->list[i].addr)) {
1342 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pMAC\n",
1344 aq_ret = I40E_ERR_INVALID_MAC_ADDR;
1348 vsi = pf->vsi[vsi_id];
1350 /* add new addresses to the list */
1351 for (i = 0; i < al->num_elements; i++) {
1352 struct i40e_mac_filter *f;
1354 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
1356 if (i40e_is_vsi_in_vlan(vsi))
1357 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1360 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1365 dev_err(&pf->pdev->dev,
1366 "Unable to add VF MAC filter\n");
1367 aq_ret = I40E_ERR_PARAM;
1372 /* program the updated filter list */
1373 if (i40e_sync_vsi_filters(vsi))
1374 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1377 /* send the response to the vf */
1378 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
1383 * i40e_vc_del_mac_addr_msg
1384 * @vf: pointer to the vf info
1385 * @msg: pointer to the msg buffer
1386 * @msglen: msg length
1388 * remove guest mac address filter
1390 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1392 struct i40e_virtchnl_ether_addr_list *al =
1393 (struct i40e_virtchnl_ether_addr_list *)msg;
1394 struct i40e_pf *pf = vf->pf;
1395 struct i40e_vsi *vsi = NULL;
1396 u16 vsi_id = al->vsi_id;
1397 i40e_status aq_ret = 0;
1400 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1401 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1402 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1403 aq_ret = I40E_ERR_PARAM;
1406 vsi = pf->vsi[vsi_id];
1408 /* delete addresses from the list */
1409 for (i = 0; i < al->num_elements; i++)
1410 i40e_del_filter(vsi, al->list[i].addr,
1411 I40E_VLAN_ANY, true, false);
1413 /* program the updated filter list */
1414 if (i40e_sync_vsi_filters(vsi))
1415 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1418 /* send the response to the vf */
1419 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
1424 * i40e_vc_add_vlan_msg
1425 * @vf: pointer to the vf info
1426 * @msg: pointer to the msg buffer
1427 * @msglen: msg length
1429 * program guest vlan id
1431 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1433 struct i40e_virtchnl_vlan_filter_list *vfl =
1434 (struct i40e_virtchnl_vlan_filter_list *)msg;
1435 struct i40e_pf *pf = vf->pf;
1436 struct i40e_vsi *vsi = NULL;
1437 u16 vsi_id = vfl->vsi_id;
1438 i40e_status aq_ret = 0;
1441 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1442 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1443 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1444 aq_ret = I40E_ERR_PARAM;
1448 for (i = 0; i < vfl->num_elements; i++) {
1449 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1450 aq_ret = I40E_ERR_PARAM;
1451 dev_err(&pf->pdev->dev,
1452 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1456 vsi = pf->vsi[vsi_id];
1457 if (vsi->info.pvid) {
1458 aq_ret = I40E_ERR_PARAM;
1462 i40e_vlan_stripping_enable(vsi);
1463 for (i = 0; i < vfl->num_elements; i++) {
1464 /* add new VLAN filter */
1465 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1467 dev_err(&pf->pdev->dev,
1468 "Unable to add VF vlan filter %d, error %d\n",
1469 vfl->vlan_id[i], ret);
1473 /* send the response to the vf */
1474 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1478 * i40e_vc_remove_vlan_msg
1479 * @vf: pointer to the vf info
1480 * @msg: pointer to the msg buffer
1481 * @msglen: msg length
1483 * remove programmed guest vlan id
1485 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1487 struct i40e_virtchnl_vlan_filter_list *vfl =
1488 (struct i40e_virtchnl_vlan_filter_list *)msg;
1489 struct i40e_pf *pf = vf->pf;
1490 struct i40e_vsi *vsi = NULL;
1491 u16 vsi_id = vfl->vsi_id;
1492 i40e_status aq_ret = 0;
1495 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1496 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1497 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1498 aq_ret = I40E_ERR_PARAM;
1502 for (i = 0; i < vfl->num_elements; i++) {
1503 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1504 aq_ret = I40E_ERR_PARAM;
1509 vsi = pf->vsi[vsi_id];
1510 if (vsi->info.pvid) {
1511 aq_ret = I40E_ERR_PARAM;
1515 for (i = 0; i < vfl->num_elements; i++) {
1516 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1518 dev_err(&pf->pdev->dev,
1519 "Unable to delete VF vlan filter %d, error %d\n",
1520 vfl->vlan_id[i], ret);
1524 /* send the response to the vf */
1525 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1529 * i40e_vc_validate_vf_msg
1530 * @vf: pointer to the vf info
1531 * @msg: pointer to the msg buffer
1532 * @msglen: msg length
1533 * @msghndl: msg handle
1537 static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1538 u32 v_retval, u8 *msg, u16 msglen)
1540 bool err_msg_format = false;
1543 /* Check if VF is disabled. */
1544 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1545 return I40E_ERR_PARAM;
1547 /* Validate message length. */
1549 case I40E_VIRTCHNL_OP_VERSION:
1550 valid_len = sizeof(struct i40e_virtchnl_version_info);
1552 case I40E_VIRTCHNL_OP_RESET_VF:
1553 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1556 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1557 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1559 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1560 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1562 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1563 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1564 if (msglen >= valid_len) {
1565 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1566 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1567 valid_len += (vqc->num_queue_pairs *
1569 i40e_virtchnl_queue_pair_info));
1570 if (vqc->num_queue_pairs == 0)
1571 err_msg_format = true;
1574 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1575 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1576 if (msglen >= valid_len) {
1577 struct i40e_virtchnl_irq_map_info *vimi =
1578 (struct i40e_virtchnl_irq_map_info *)msg;
1579 valid_len += (vimi->num_vectors *
1580 sizeof(struct i40e_virtchnl_vector_map));
1581 if (vimi->num_vectors == 0)
1582 err_msg_format = true;
1585 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1586 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1587 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1589 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1590 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1591 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1592 if (msglen >= valid_len) {
1593 struct i40e_virtchnl_ether_addr_list *veal =
1594 (struct i40e_virtchnl_ether_addr_list *)msg;
1595 valid_len += veal->num_elements *
1596 sizeof(struct i40e_virtchnl_ether_addr);
1597 if (veal->num_elements == 0)
1598 err_msg_format = true;
1601 case I40E_VIRTCHNL_OP_ADD_VLAN:
1602 case I40E_VIRTCHNL_OP_DEL_VLAN:
1603 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1604 if (msglen >= valid_len) {
1605 struct i40e_virtchnl_vlan_filter_list *vfl =
1606 (struct i40e_virtchnl_vlan_filter_list *)msg;
1607 valid_len += vfl->num_elements * sizeof(u16);
1608 if (vfl->num_elements == 0)
1609 err_msg_format = true;
1612 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1613 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1615 case I40E_VIRTCHNL_OP_GET_STATS:
1616 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1618 /* These are always errors coming from the VF. */
1619 case I40E_VIRTCHNL_OP_EVENT:
1620 case I40E_VIRTCHNL_OP_UNKNOWN:
1625 /* few more checks */
1626 if ((valid_len != msglen) || (err_msg_format)) {
1627 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1635 * i40e_vc_process_vf_msg
1636 * @pf: pointer to the pf structure
1637 * @vf_id: source vf id
1638 * @msg: pointer to the msg buffer
1639 * @msglen: msg length
1640 * @msghndl: msg handle
1642 * called from the common aeq/arq handler to
1643 * process request from vf
1645 int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1646 u32 v_retval, u8 *msg, u16 msglen)
1648 struct i40e_hw *hw = &pf->hw;
1652 pf->vf_aq_requests++;
1653 if (vf_id >= pf->num_alloc_vfs)
1655 vf = &(pf->vf[vf_id]);
1656 /* perform basic checks on the msg */
1657 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1660 dev_err(&pf->pdev->dev, "Invalid message from vf %d, opcode %d, len %d\n",
1661 vf_id, v_opcode, msglen);
1664 wr32(hw, I40E_VFGEN_RSTAT1(vf_id), I40E_VFR_VFACTIVE);
1666 case I40E_VIRTCHNL_OP_VERSION:
1667 ret = i40e_vc_get_version_msg(vf);
1669 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1670 ret = i40e_vc_get_vf_resources_msg(vf);
1672 case I40E_VIRTCHNL_OP_RESET_VF:
1673 i40e_vc_reset_vf_msg(vf);
1676 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1677 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1679 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1680 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1682 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1683 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1685 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1686 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
1688 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1689 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1691 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1692 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1694 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1695 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1697 case I40E_VIRTCHNL_OP_ADD_VLAN:
1698 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1700 case I40E_VIRTCHNL_OP_DEL_VLAN:
1701 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1703 case I40E_VIRTCHNL_OP_GET_STATS:
1704 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1706 case I40E_VIRTCHNL_OP_UNKNOWN:
1708 dev_err(&pf->pdev->dev,
1709 "Unsupported opcode %d from vf %d\n", v_opcode, vf_id);
1710 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1711 I40E_ERR_NOT_IMPLEMENTED);
1719 * i40e_vc_process_vflr_event
1720 * @pf: pointer to the pf structure
1722 * called from the vlfr irq handler to
1723 * free up vf resources and state variables
1725 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1727 u32 reg, reg_idx, bit_idx, vf_id;
1728 struct i40e_hw *hw = &pf->hw;
1731 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1734 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1735 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1736 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1737 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1738 /* read GLGEN_VFLRSTAT register to find out the flr vfs */
1739 vf = &pf->vf[vf_id];
1740 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
1741 if (reg & (1 << bit_idx)) {
1742 /* clear the bit in GLGEN_VFLRSTAT */
1743 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
1745 i40e_reset_vf(vf, true);
1749 /* re-enable vflr interrupt cause */
1750 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1751 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1752 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1759 * i40e_vc_vf_broadcast
1760 * @pf: pointer to the pf structure
1761 * @opcode: operation code
1762 * @retval: return value
1763 * @msg: pointer to the msg buffer
1764 * @msglen: msg length
1766 * send a message to all VFs on a given PF
1768 static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
1769 enum i40e_virtchnl_ops v_opcode,
1770 i40e_status v_retval, u8 *msg,
1773 struct i40e_hw *hw = &pf->hw;
1774 struct i40e_vf *vf = pf->vf;
1777 for (i = 0; i < pf->num_alloc_vfs; i++) {
1778 /* Ignore return value on purpose - a given VF may fail, but
1779 * we need to keep going and send to all of them
1781 i40e_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval,
1788 * i40e_vc_notify_link_state
1789 * @pf: pointer to the pf structure
1791 * send a link status message to all VFs on a given PF
1793 void i40e_vc_notify_link_state(struct i40e_pf *pf)
1795 struct i40e_virtchnl_pf_event pfe;
1797 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
1798 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
1799 pfe.event_data.link_event.link_status =
1800 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
1801 pfe.event_data.link_event.link_speed = pf->hw.phy.link_info.link_speed;
1803 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
1804 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
1808 * i40e_vc_notify_reset
1809 * @pf: pointer to the pf structure
1811 * indicate a pending reset to all VFs on a given PF
1813 void i40e_vc_notify_reset(struct i40e_pf *pf)
1815 struct i40e_virtchnl_pf_event pfe;
1817 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1818 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
1819 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
1820 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
1824 * i40e_vc_notify_vf_reset
1825 * @vf: pointer to the vf structure
1827 * indicate a pending reset to the given VF
1829 void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
1831 struct i40e_virtchnl_pf_event pfe;
1833 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1834 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
1835 i40e_aq_send_msg_to_vf(&vf->pf->hw, vf->vf_id, I40E_VIRTCHNL_OP_EVENT,
1836 I40E_SUCCESS, (u8 *)&pfe,
1837 sizeof(struct i40e_virtchnl_pf_event), NULL);
1841 * i40e_ndo_set_vf_mac
1842 * @netdev: network interface device structure
1843 * @vf_id: vf identifier
1846 * program vf mac address
1848 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
1850 struct i40e_netdev_priv *np = netdev_priv(netdev);
1851 struct i40e_vsi *vsi = np->vsi;
1852 struct i40e_pf *pf = vsi->back;
1853 struct i40e_mac_filter *f;
1857 /* validate the request */
1858 if (vf_id >= pf->num_alloc_vfs) {
1859 dev_err(&pf->pdev->dev,
1860 "Invalid VF Identifier %d\n", vf_id);
1865 vf = &(pf->vf[vf_id]);
1866 vsi = pf->vsi[vf->lan_vsi_index];
1867 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1868 dev_err(&pf->pdev->dev,
1869 "Uninitialized VF %d\n", vf_id);
1874 if (!is_valid_ether_addr(mac)) {
1875 dev_err(&pf->pdev->dev,
1876 "Invalid VF ethernet address\n");
1881 /* delete the temporary mac address */
1882 i40e_del_filter(vsi, vf->default_lan_addr.addr, 0, true, false);
1884 /* add the new mac address */
1885 f = i40e_add_filter(vsi, mac, 0, true, false);
1887 dev_err(&pf->pdev->dev,
1888 "Unable to add VF ucast filter\n");
1893 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
1894 /* program mac filter */
1895 if (i40e_sync_vsi_filters(vsi)) {
1896 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
1900 memcpy(vf->default_lan_addr.addr, mac, ETH_ALEN);
1901 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
1909 * i40e_ndo_set_vf_port_vlan
1910 * @netdev: network interface device structure
1911 * @vf_id: vf identifier
1912 * @vlan_id: mac address
1913 * @qos: priority setting
1915 * program vf vlan id and/or qos
1917 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
1918 int vf_id, u16 vlan_id, u8 qos)
1920 struct i40e_netdev_priv *np = netdev_priv(netdev);
1921 struct i40e_pf *pf = np->vsi->back;
1922 struct i40e_vsi *vsi;
1926 /* validate the request */
1927 if (vf_id >= pf->num_alloc_vfs) {
1928 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
1933 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
1934 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
1939 vf = &(pf->vf[vf_id]);
1940 vsi = pf->vsi[vf->lan_vsi_index];
1941 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1942 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
1947 if (vsi->info.pvid) {
1949 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
1952 dev_info(&vsi->back->pdev->dev,
1953 "remove VLAN failed, ret=%d, aq_err=%d\n",
1954 ret, pf->hw.aq.asq_last_status);
1958 ret = i40e_vsi_add_pvid(vsi,
1959 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
1961 i40e_vlan_stripping_disable(vsi);
1964 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
1965 vlan_id, qos, vf_id);
1967 /* add new VLAN filter */
1968 ret = i40e_vsi_add_vlan(vsi, vlan_id);
1970 dev_info(&vsi->back->pdev->dev,
1971 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
1972 vsi->back->hw.aq.asq_last_status);
1978 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
1988 * i40e_ndo_set_vf_bw
1989 * @netdev: network interface device structure
1990 * @vf_id: vf identifier
1993 * configure vf tx rate
1995 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int tx_rate)
2001 * i40e_ndo_get_vf_config
2002 * @netdev: network interface device structure
2003 * @vf_id: vf identifier
2004 * @ivi: vf configuration structure
2006 * return vf configuration
2008 int i40e_ndo_get_vf_config(struct net_device *netdev,
2009 int vf_id, struct ifla_vf_info *ivi)
2011 struct i40e_netdev_priv *np = netdev_priv(netdev);
2012 struct i40e_vsi *vsi = np->vsi;
2013 struct i40e_pf *pf = vsi->back;
2017 /* validate the request */
2018 if (vf_id >= pf->num_alloc_vfs) {
2019 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2024 vf = &(pf->vf[vf_id]);
2025 /* first vsi is always the LAN vsi */
2026 vsi = pf->vsi[vf->lan_vsi_index];
2027 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2028 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2035 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
2038 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2039 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2040 I40E_VLAN_PRIORITY_SHIFT;