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