i40e: remove dead code
[deliverable/linux.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);
674 mdelay(10);
675 i40e_alloc_vf_res(vf);
676 i40e_enable_vf_mappings(vf);
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 **/
842static int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
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
5c3c48ac
JB
850 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
851 if (ret) {
852 dev_err(&pf->pdev->dev,
853 "pci_enable_sriov failed with error %d!\n", ret);
854 pf->num_alloc_vfs = 0;
855 goto err_iov;
856 }
857
858 /* allocate memory */
859 vfs = kzalloc(num_alloc_vfs * sizeof(struct i40e_vf), GFP_KERNEL);
860 if (!vfs) {
861 ret = -ENOMEM;
862 goto err_alloc;
863 }
864
865 /* apply default profile */
866 for (i = 0; i < num_alloc_vfs; i++) {
867 vfs[i].pf = pf;
868 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
869 vfs[i].vf_id = i;
870
871 /* assign default capabilities */
872 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
fc18eaa0
MW
873 /* vf resources get allocated during reset */
874 i40e_reset_vf(&vfs[i], false);
5c3c48ac
JB
875
876 /* enable vf vplan_qtable mappings */
877 i40e_enable_vf_mappings(&vfs[i]);
878 }
879 pf->vf = vfs;
880 pf->num_alloc_vfs = num_alloc_vfs;
881
c354229f 882 i40e_enable_pf_switch_lb(pf);
5c3c48ac
JB
883err_alloc:
884 if (ret)
885 i40e_free_vfs(pf);
886err_iov:
6c1b5bff 887 /* Re-enable interrupt 0. */
2ef28cfb 888 i40e_irq_dynamic_enable_icr0(pf);
5c3c48ac
JB
889 return ret;
890}
891
892#endif
893/**
894 * i40e_pci_sriov_enable
895 * @pdev: pointer to a pci_dev structure
896 * @num_vfs: number of vfs to allocate
897 *
898 * Enable or change the number of VFs
899 **/
900static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
901{
902#ifdef CONFIG_PCI_IOV
903 struct i40e_pf *pf = pci_get_drvdata(pdev);
904 int pre_existing_vfs = pci_num_vf(pdev);
905 int err = 0;
906
907 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
908 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
909 i40e_free_vfs(pf);
910 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
911 goto out;
912
913 if (num_vfs > pf->num_req_vfs) {
914 err = -EPERM;
915 goto err_out;
916 }
917
918 err = i40e_alloc_vfs(pf, num_vfs);
919 if (err) {
920 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
921 goto err_out;
922 }
923
924out:
925 return num_vfs;
926
927err_out:
928 return err;
929#endif
930 return 0;
931}
932
933/**
934 * i40e_pci_sriov_configure
935 * @pdev: pointer to a pci_dev structure
936 * @num_vfs: number of vfs to allocate
937 *
938 * Enable or change the number of VFs. Called when the user updates the number
939 * of VFs in sysfs.
940 **/
941int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
942{
943 struct i40e_pf *pf = pci_get_drvdata(pdev);
944
945 if (num_vfs)
946 return i40e_pci_sriov_enable(pdev, num_vfs);
947
948 i40e_free_vfs(pf);
949 return 0;
950}
951
952/***********************virtual channel routines******************/
953
954/**
955 * i40e_vc_send_msg_to_vf
956 * @vf: pointer to the vf info
957 * @v_opcode: virtual channel opcode
958 * @v_retval: virtual channel return value
959 * @msg: pointer to the msg buffer
960 * @msglen: msg length
961 *
962 * send msg to vf
963 **/
964static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
965 u32 v_retval, u8 *msg, u16 msglen)
966{
967 struct i40e_pf *pf = vf->pf;
968 struct i40e_hw *hw = &pf->hw;
7efa84b7 969 int true_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
5c3c48ac
JB
970 i40e_status aq_ret;
971
972 /* single place to detect unsuccessful return values */
973 if (v_retval) {
974 vf->num_invalid_msgs++;
975 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
976 v_opcode, v_retval);
977 if (vf->num_invalid_msgs >
978 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
979 dev_err(&pf->pdev->dev,
980 "Number of invalid messages exceeded for VF %d\n",
981 vf->vf_id);
982 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
983 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
984 }
985 } else {
986 vf->num_valid_msgs++;
987 }
988
7efa84b7
MW
989 aq_ret = i40e_aq_send_msg_to_vf(hw, true_vf_id, v_opcode, v_retval,
990 msg, msglen, NULL);
5c3c48ac
JB
991 if (aq_ret) {
992 dev_err(&pf->pdev->dev,
993 "Unable to send the message to VF %d aq_err %d\n",
994 vf->vf_id, pf->hw.aq.asq_last_status);
995 return -EIO;
996 }
997
998 return 0;
999}
1000
1001/**
1002 * i40e_vc_send_resp_to_vf
1003 * @vf: pointer to the vf info
1004 * @opcode: operation code
1005 * @retval: return value
1006 *
1007 * send resp msg to vf
1008 **/
1009static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1010 enum i40e_virtchnl_ops opcode,
1011 i40e_status retval)
1012{
1013 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1014}
1015
1016/**
1017 * i40e_vc_get_version_msg
1018 * @vf: pointer to the vf info
1019 *
1020 * called from the vf to request the API version used by the PF
1021 **/
1022static int i40e_vc_get_version_msg(struct i40e_vf *vf)
1023{
1024 struct i40e_virtchnl_version_info info = {
1025 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
1026 };
1027
1028 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
1029 I40E_SUCCESS, (u8 *)&info,
1030 sizeof(struct
1031 i40e_virtchnl_version_info));
1032}
1033
1034/**
1035 * i40e_vc_get_vf_resources_msg
1036 * @vf: pointer to the vf info
1037 * @msg: pointer to the msg buffer
1038 * @msglen: msg length
1039 *
1040 * called from the vf to request its resources
1041 **/
1042static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf)
1043{
1044 struct i40e_virtchnl_vf_resource *vfres = NULL;
1045 struct i40e_pf *pf = vf->pf;
1046 i40e_status aq_ret = 0;
1047 struct i40e_vsi *vsi;
1048 int i = 0, len = 0;
1049 int num_vsis = 1;
1050 int ret;
1051
1052 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1053 aq_ret = I40E_ERR_PARAM;
1054 goto err;
1055 }
1056
1057 len = (sizeof(struct i40e_virtchnl_vf_resource) +
1058 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1059
1060 vfres = kzalloc(len, GFP_KERNEL);
1061 if (!vfres) {
1062 aq_ret = I40E_ERR_NO_MEMORY;
1063 len = 0;
1064 goto err;
1065 }
1066
1067 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
1068 vsi = pf->vsi[vf->lan_vsi_index];
1069 if (!vsi->info.pvid)
1070 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1071
1072 vfres->num_vsis = num_vsis;
1073 vfres->num_queue_pairs = vf->num_queue_pairs;
1074 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1075 if (vf->lan_vsi_index) {
1076 vfres->vsi_res[i].vsi_id = vf->lan_vsi_index;
1077 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1078 vfres->vsi_res[i].num_queue_pairs =
1079 pf->vsi[vf->lan_vsi_index]->num_queue_pairs;
1080 memcpy(vfres->vsi_res[i].default_mac_addr,
1081 vf->default_lan_addr.addr, ETH_ALEN);
1082 i++;
1083 }
1084 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1085
1086err:
1087 /* send the response back to the vf */
1088 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1089 aq_ret, (u8 *)vfres, len);
1090
1091 kfree(vfres);
1092 return ret;
1093}
1094
1095/**
1096 * i40e_vc_reset_vf_msg
1097 * @vf: pointer to the vf info
1098 * @msg: pointer to the msg buffer
1099 * @msglen: msg length
1100 *
1101 * called from the vf to reset itself,
1102 * unlike other virtchnl messages, pf driver
1103 * doesn't send the response back to the vf
1104 **/
fc18eaa0 1105static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
5c3c48ac 1106{
fc18eaa0
MW
1107 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1108 i40e_reset_vf(vf, false);
5c3c48ac
JB
1109}
1110
1111/**
1112 * i40e_vc_config_promiscuous_mode_msg
1113 * @vf: pointer to the vf info
1114 * @msg: pointer to the msg buffer
1115 * @msglen: msg length
1116 *
1117 * called from the vf to configure the promiscuous mode of
1118 * vf vsis
1119 **/
1120static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1121 u8 *msg, u16 msglen)
1122{
1123 struct i40e_virtchnl_promisc_info *info =
1124 (struct i40e_virtchnl_promisc_info *)msg;
1125 struct i40e_pf *pf = vf->pf;
1126 struct i40e_hw *hw = &pf->hw;
1127 bool allmulti = false;
1128 bool promisc = false;
1129 i40e_status aq_ret;
1130
1131 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1132 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1133 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1134 (pf->vsi[info->vsi_id]->type != I40E_VSI_FCOE)) {
1135 aq_ret = I40E_ERR_PARAM;
1136 goto error_param;
1137 }
1138
1139 if (info->flags & I40E_FLAG_VF_UNICAST_PROMISC)
1140 promisc = true;
1141 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, info->vsi_id,
1142 promisc, NULL);
1143 if (aq_ret)
1144 goto error_param;
1145
1146 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1147 allmulti = true;
1148 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, info->vsi_id,
1149 allmulti, NULL);
1150
1151error_param:
1152 /* send the response to the vf */
1153 return i40e_vc_send_resp_to_vf(vf,
1154 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1155 aq_ret);
1156}
1157
1158/**
1159 * i40e_vc_config_queues_msg
1160 * @vf: pointer to the vf info
1161 * @msg: pointer to the msg buffer
1162 * @msglen: msg length
1163 *
1164 * called from the vf to configure the rx/tx
1165 * queues
1166 **/
1167static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1168{
1169 struct i40e_virtchnl_vsi_queue_config_info *qci =
1170 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1171 struct i40e_virtchnl_queue_pair_info *qpi;
1172 u16 vsi_id, vsi_queue_id;
1173 i40e_status aq_ret = 0;
1174 int i;
1175
1176 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1177 aq_ret = I40E_ERR_PARAM;
1178 goto error_param;
1179 }
1180
1181 vsi_id = qci->vsi_id;
1182 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1183 aq_ret = I40E_ERR_PARAM;
1184 goto error_param;
1185 }
1186 for (i = 0; i < qci->num_queue_pairs; i++) {
1187 qpi = &qci->qpair[i];
1188 vsi_queue_id = qpi->txq.queue_id;
1189 if ((qpi->txq.vsi_id != vsi_id) ||
1190 (qpi->rxq.vsi_id != vsi_id) ||
1191 (qpi->rxq.queue_id != vsi_queue_id) ||
1192 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1193 aq_ret = I40E_ERR_PARAM;
1194 goto error_param;
1195 }
1196
1197 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1198 &qpi->rxq) ||
1199 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1200 &qpi->txq)) {
1201 aq_ret = I40E_ERR_PARAM;
1202 goto error_param;
1203 }
1204 }
1205
1206error_param:
1207 /* send the response to the vf */
1208 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1209 aq_ret);
1210}
1211
1212/**
1213 * i40e_vc_config_irq_map_msg
1214 * @vf: pointer to the vf info
1215 * @msg: pointer to the msg buffer
1216 * @msglen: msg length
1217 *
1218 * called from the vf to configure the irq to
1219 * queue map
1220 **/
1221static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1222{
1223 struct i40e_virtchnl_irq_map_info *irqmap_info =
1224 (struct i40e_virtchnl_irq_map_info *)msg;
1225 struct i40e_virtchnl_vector_map *map;
1226 u16 vsi_id, vsi_queue_id, vector_id;
1227 i40e_status aq_ret = 0;
1228 unsigned long tempmap;
1229 int i;
1230
1231 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1232 aq_ret = I40E_ERR_PARAM;
1233 goto error_param;
1234 }
1235
1236 for (i = 0; i < irqmap_info->num_vectors; i++) {
1237 map = &irqmap_info->vecmap[i];
1238
1239 vector_id = map->vector_id;
1240 vsi_id = map->vsi_id;
1241 /* validate msg params */
1242 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1243 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1244 aq_ret = I40E_ERR_PARAM;
1245 goto error_param;
1246 }
1247
1248 /* lookout for the invalid queue index */
1249 tempmap = map->rxq_map;
4836650b 1250 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
5c3c48ac
JB
1251 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1252 vsi_queue_id)) {
1253 aq_ret = I40E_ERR_PARAM;
1254 goto error_param;
1255 }
5c3c48ac
JB
1256 }
1257
1258 tempmap = map->txq_map;
4836650b 1259 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
5c3c48ac
JB
1260 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1261 vsi_queue_id)) {
1262 aq_ret = I40E_ERR_PARAM;
1263 goto error_param;
1264 }
5c3c48ac
JB
1265 }
1266
1267 i40e_config_irq_link_list(vf, vsi_id, map);
1268 }
1269error_param:
1270 /* send the response to the vf */
1271 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1272 aq_ret);
1273}
1274
1275/**
1276 * i40e_vc_enable_queues_msg
1277 * @vf: pointer to the vf info
1278 * @msg: pointer to the msg buffer
1279 * @msglen: msg length
1280 *
1281 * called from the vf to enable all or specific queue(s)
1282 **/
1283static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1284{
1285 struct i40e_virtchnl_queue_select *vqs =
1286 (struct i40e_virtchnl_queue_select *)msg;
1287 struct i40e_pf *pf = vf->pf;
1288 u16 vsi_id = vqs->vsi_id;
1289 i40e_status aq_ret = 0;
5c3c48ac
JB
1290
1291 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1292 aq_ret = I40E_ERR_PARAM;
1293 goto error_param;
1294 }
1295
1296 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1297 aq_ret = I40E_ERR_PARAM;
1298 goto error_param;
1299 }
1300
1301 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1302 aq_ret = I40E_ERR_PARAM;
1303 goto error_param;
1304 }
88f6563d
MW
1305 if (i40e_vsi_control_rings(pf->vsi[vsi_id], true))
1306 aq_ret = I40E_ERR_TIMEOUT;
5c3c48ac
JB
1307error_param:
1308 /* send the response to the vf */
1309 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1310 aq_ret);
1311}
1312
1313/**
1314 * i40e_vc_disable_queues_msg
1315 * @vf: pointer to the vf info
1316 * @msg: pointer to the msg buffer
1317 * @msglen: msg length
1318 *
1319 * called from the vf to disable all or specific
1320 * queue(s)
1321 **/
1322static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1323{
1324 struct i40e_virtchnl_queue_select *vqs =
1325 (struct i40e_virtchnl_queue_select *)msg;
1326 struct i40e_pf *pf = vf->pf;
1327 u16 vsi_id = vqs->vsi_id;
1328 i40e_status aq_ret = 0;
5c3c48ac
JB
1329
1330 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1331 aq_ret = I40E_ERR_PARAM;
1332 goto error_param;
1333 }
1334
1335 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1336 aq_ret = I40E_ERR_PARAM;
1337 goto error_param;
1338 }
1339
1340 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1341 aq_ret = I40E_ERR_PARAM;
1342 goto error_param;
1343 }
88f6563d
MW
1344 if (i40e_vsi_control_rings(pf->vsi[vsi_id], false))
1345 aq_ret = I40E_ERR_TIMEOUT;
5c3c48ac
JB
1346
1347error_param:
1348 /* send the response to the vf */
1349 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1350 aq_ret);
1351}
1352
1353/**
1354 * i40e_vc_get_stats_msg
1355 * @vf: pointer to the vf info
1356 * @msg: pointer to the msg buffer
1357 * @msglen: msg length
1358 *
1359 * called from the vf to get vsi stats
1360 **/
1361static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1362{
1363 struct i40e_virtchnl_queue_select *vqs =
1364 (struct i40e_virtchnl_queue_select *)msg;
1365 struct i40e_pf *pf = vf->pf;
1366 struct i40e_eth_stats stats;
1367 i40e_status aq_ret = 0;
1368 struct i40e_vsi *vsi;
1369
1370 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1371
1372 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1373 aq_ret = I40E_ERR_PARAM;
1374 goto error_param;
1375 }
1376
1377 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1378 aq_ret = I40E_ERR_PARAM;
1379 goto error_param;
1380 }
1381
1382 vsi = pf->vsi[vqs->vsi_id];
1383 if (!vsi) {
1384 aq_ret = I40E_ERR_PARAM;
1385 goto error_param;
1386 }
1387 i40e_update_eth_stats(vsi);
5a9769c8 1388 stats = vsi->eth_stats;
5c3c48ac
JB
1389
1390error_param:
1391 /* send the response back to the vf */
1392 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1393 (u8 *)&stats, sizeof(stats));
1394}
1395
f657a6e1
GR
1396/**
1397 * i40e_check_vf_permission
1398 * @vf: pointer to the vf info
1399 * @macaddr: pointer to the MAC Address being checked
1400 *
1401 * Check if the VF has permission to add or delete unicast MAC address
1402 * filters and return error code -EPERM if not. Then check if the
1403 * address filter requested is broadcast or zero and if so return
1404 * an invalid MAC address error code.
1405 **/
1406static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1407{
1408 struct i40e_pf *pf = vf->pf;
1409 int ret = 0;
1410
1411 if (is_broadcast_ether_addr(macaddr) ||
1412 is_zero_ether_addr(macaddr)) {
1413 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1414 ret = I40E_ERR_INVALID_MAC_ADDR;
5017c2a8
GR
1415 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1416 !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
f657a6e1
GR
1417 /* If the host VMM administrator has set the VF MAC address
1418 * administratively via the ndo_set_vf_mac command then deny
1419 * permission to the VF to add or delete unicast MAC addresses.
5017c2a8
GR
1420 * The VF may request to set the MAC address filter already
1421 * assigned to it so do not return an error in that case.
f657a6e1
GR
1422 */
1423 dev_err(&pf->pdev->dev,
1424 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1425 ret = -EPERM;
1426 }
1427 return ret;
1428}
1429
5c3c48ac
JB
1430/**
1431 * i40e_vc_add_mac_addr_msg
1432 * @vf: pointer to the vf info
1433 * @msg: pointer to the msg buffer
1434 * @msglen: msg length
1435 *
1436 * add guest mac address filter
1437 **/
1438static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1439{
1440 struct i40e_virtchnl_ether_addr_list *al =
1441 (struct i40e_virtchnl_ether_addr_list *)msg;
1442 struct i40e_pf *pf = vf->pf;
1443 struct i40e_vsi *vsi = NULL;
1444 u16 vsi_id = al->vsi_id;
f657a6e1 1445 i40e_status ret = 0;
5c3c48ac
JB
1446 int i;
1447
1448 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1449 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1450 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
f657a6e1 1451 ret = I40E_ERR_PARAM;
5c3c48ac
JB
1452 goto error_param;
1453 }
1454
1455 for (i = 0; i < al->num_elements; i++) {
f657a6e1
GR
1456 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1457 if (ret)
5c3c48ac 1458 goto error_param;
5c3c48ac
JB
1459 }
1460 vsi = pf->vsi[vsi_id];
1461
1462 /* add new addresses to the list */
1463 for (i = 0; i < al->num_elements; i++) {
1464 struct i40e_mac_filter *f;
1465
1466 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
7e68edf9 1467 if (!f) {
5c3c48ac
JB
1468 if (i40e_is_vsi_in_vlan(vsi))
1469 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1470 true, false);
1471 else
1472 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1473 true, false);
1474 }
1475
1476 if (!f) {
1477 dev_err(&pf->pdev->dev,
1478 "Unable to add VF MAC filter\n");
f657a6e1 1479 ret = I40E_ERR_PARAM;
5c3c48ac
JB
1480 goto error_param;
1481 }
1482 }
1483
1484 /* program the updated filter list */
1485 if (i40e_sync_vsi_filters(vsi))
1486 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1487
1488error_param:
1489 /* send the response to the vf */
1490 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
f657a6e1 1491 ret);
5c3c48ac
JB
1492}
1493
1494/**
1495 * i40e_vc_del_mac_addr_msg
1496 * @vf: pointer to the vf info
1497 * @msg: pointer to the msg buffer
1498 * @msglen: msg length
1499 *
1500 * remove guest mac address filter
1501 **/
1502static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1503{
1504 struct i40e_virtchnl_ether_addr_list *al =
1505 (struct i40e_virtchnl_ether_addr_list *)msg;
1506 struct i40e_pf *pf = vf->pf;
1507 struct i40e_vsi *vsi = NULL;
1508 u16 vsi_id = al->vsi_id;
f657a6e1 1509 i40e_status ret = 0;
5c3c48ac
JB
1510 int i;
1511
1512 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1513 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1514 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
f657a6e1 1515 ret = I40E_ERR_PARAM;
5c3c48ac
JB
1516 goto error_param;
1517 }
f657a6e1
GR
1518
1519 for (i = 0; i < al->num_elements; i++) {
700bbf6c
MW
1520 if (is_broadcast_ether_addr(al->list[i].addr) ||
1521 is_zero_ether_addr(al->list[i].addr)) {
1522 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
1523 al->list[i].addr);
1524 ret = I40E_ERR_INVALID_MAC_ADDR;
f657a6e1 1525 goto error_param;
700bbf6c 1526 }
f657a6e1 1527 }
5c3c48ac
JB
1528 vsi = pf->vsi[vsi_id];
1529
1530 /* delete addresses from the list */
1531 for (i = 0; i < al->num_elements; i++)
1532 i40e_del_filter(vsi, al->list[i].addr,
1533 I40E_VLAN_ANY, true, false);
1534
1535 /* program the updated filter list */
1536 if (i40e_sync_vsi_filters(vsi))
1537 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1538
1539error_param:
1540 /* send the response to the vf */
1541 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
f657a6e1 1542 ret);
5c3c48ac
JB
1543}
1544
1545/**
1546 * i40e_vc_add_vlan_msg
1547 * @vf: pointer to the vf info
1548 * @msg: pointer to the msg buffer
1549 * @msglen: msg length
1550 *
1551 * program guest vlan id
1552 **/
1553static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1554{
1555 struct i40e_virtchnl_vlan_filter_list *vfl =
1556 (struct i40e_virtchnl_vlan_filter_list *)msg;
1557 struct i40e_pf *pf = vf->pf;
1558 struct i40e_vsi *vsi = NULL;
1559 u16 vsi_id = vfl->vsi_id;
1560 i40e_status aq_ret = 0;
1561 int i;
1562
1563 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1564 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1565 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1566 aq_ret = I40E_ERR_PARAM;
1567 goto error_param;
1568 }
1569
1570 for (i = 0; i < vfl->num_elements; i++) {
1571 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1572 aq_ret = I40E_ERR_PARAM;
1573 dev_err(&pf->pdev->dev,
1574 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1575 goto error_param;
1576 }
1577 }
1578 vsi = pf->vsi[vsi_id];
1579 if (vsi->info.pvid) {
1580 aq_ret = I40E_ERR_PARAM;
1581 goto error_param;
1582 }
1583
1584 i40e_vlan_stripping_enable(vsi);
1585 for (i = 0; i < vfl->num_elements; i++) {
1586 /* add new VLAN filter */
1587 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1588 if (ret)
1589 dev_err(&pf->pdev->dev,
1590 "Unable to add VF vlan filter %d, error %d\n",
1591 vfl->vlan_id[i], ret);
1592 }
1593
1594error_param:
1595 /* send the response to the vf */
1596 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1597}
1598
1599/**
1600 * i40e_vc_remove_vlan_msg
1601 * @vf: pointer to the vf info
1602 * @msg: pointer to the msg buffer
1603 * @msglen: msg length
1604 *
1605 * remove programmed guest vlan id
1606 **/
1607static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1608{
1609 struct i40e_virtchnl_vlan_filter_list *vfl =
1610 (struct i40e_virtchnl_vlan_filter_list *)msg;
1611 struct i40e_pf *pf = vf->pf;
1612 struct i40e_vsi *vsi = NULL;
1613 u16 vsi_id = vfl->vsi_id;
1614 i40e_status aq_ret = 0;
1615 int i;
1616
1617 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1618 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1619 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1620 aq_ret = I40E_ERR_PARAM;
1621 goto error_param;
1622 }
1623
1624 for (i = 0; i < vfl->num_elements; i++) {
1625 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1626 aq_ret = I40E_ERR_PARAM;
1627 goto error_param;
1628 }
1629 }
1630
1631 vsi = pf->vsi[vsi_id];
1632 if (vsi->info.pvid) {
1633 aq_ret = I40E_ERR_PARAM;
1634 goto error_param;
1635 }
1636
1637 for (i = 0; i < vfl->num_elements; i++) {
1638 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1639 if (ret)
1640 dev_err(&pf->pdev->dev,
1641 "Unable to delete VF vlan filter %d, error %d\n",
1642 vfl->vlan_id[i], ret);
1643 }
1644
1645error_param:
1646 /* send the response to the vf */
1647 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1648}
1649
5c3c48ac
JB
1650/**
1651 * i40e_vc_validate_vf_msg
1652 * @vf: pointer to the vf info
1653 * @msg: pointer to the msg buffer
1654 * @msglen: msg length
1655 * @msghndl: msg handle
1656 *
1657 * validate msg
1658 **/
1659static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1660 u32 v_retval, u8 *msg, u16 msglen)
1661{
1662 bool err_msg_format = false;
1663 int valid_len;
1664
1665 /* Check if VF is disabled. */
1666 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1667 return I40E_ERR_PARAM;
1668
1669 /* Validate message length. */
1670 switch (v_opcode) {
1671 case I40E_VIRTCHNL_OP_VERSION:
1672 valid_len = sizeof(struct i40e_virtchnl_version_info);
1673 break;
1674 case I40E_VIRTCHNL_OP_RESET_VF:
1675 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1676 valid_len = 0;
1677 break;
1678 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1679 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1680 break;
1681 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1682 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1683 break;
1684 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1685 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1686 if (msglen >= valid_len) {
1687 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1688 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1689 valid_len += (vqc->num_queue_pairs *
1690 sizeof(struct
1691 i40e_virtchnl_queue_pair_info));
1692 if (vqc->num_queue_pairs == 0)
1693 err_msg_format = true;
1694 }
1695 break;
1696 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1697 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1698 if (msglen >= valid_len) {
1699 struct i40e_virtchnl_irq_map_info *vimi =
1700 (struct i40e_virtchnl_irq_map_info *)msg;
1701 valid_len += (vimi->num_vectors *
1702 sizeof(struct i40e_virtchnl_vector_map));
1703 if (vimi->num_vectors == 0)
1704 err_msg_format = true;
1705 }
1706 break;
1707 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1708 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1709 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1710 break;
1711 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1712 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1713 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1714 if (msglen >= valid_len) {
1715 struct i40e_virtchnl_ether_addr_list *veal =
1716 (struct i40e_virtchnl_ether_addr_list *)msg;
1717 valid_len += veal->num_elements *
1718 sizeof(struct i40e_virtchnl_ether_addr);
1719 if (veal->num_elements == 0)
1720 err_msg_format = true;
1721 }
1722 break;
1723 case I40E_VIRTCHNL_OP_ADD_VLAN:
1724 case I40E_VIRTCHNL_OP_DEL_VLAN:
1725 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1726 if (msglen >= valid_len) {
1727 struct i40e_virtchnl_vlan_filter_list *vfl =
1728 (struct i40e_virtchnl_vlan_filter_list *)msg;
1729 valid_len += vfl->num_elements * sizeof(u16);
1730 if (vfl->num_elements == 0)
1731 err_msg_format = true;
1732 }
1733 break;
1734 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1735 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1736 break;
1737 case I40E_VIRTCHNL_OP_GET_STATS:
1738 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1739 break;
1740 /* These are always errors coming from the VF. */
1741 case I40E_VIRTCHNL_OP_EVENT:
1742 case I40E_VIRTCHNL_OP_UNKNOWN:
1743 default:
1744 return -EPERM;
1745 break;
1746 }
1747 /* few more checks */
1748 if ((valid_len != msglen) || (err_msg_format)) {
1749 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1750 return -EINVAL;
1751 } else {
1752 return 0;
1753 }
1754}
1755
1756/**
1757 * i40e_vc_process_vf_msg
1758 * @pf: pointer to the pf structure
1759 * @vf_id: source vf id
1760 * @msg: pointer to the msg buffer
1761 * @msglen: msg length
1762 * @msghndl: msg handle
1763 *
1764 * called from the common aeq/arq handler to
1765 * process request from vf
1766 **/
1767int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1768 u32 v_retval, u8 *msg, u16 msglen)
1769{
5c3c48ac 1770 struct i40e_hw *hw = &pf->hw;
7efa84b7 1771 int local_vf_id = vf_id - hw->func_caps.vf_base_id;
6c1b5bff 1772 struct i40e_vf *vf;
5c3c48ac
JB
1773 int ret;
1774
1775 pf->vf_aq_requests++;
7efa84b7 1776 if (local_vf_id >= pf->num_alloc_vfs)
6c1b5bff 1777 return -EINVAL;
7efa84b7 1778 vf = &(pf->vf[local_vf_id]);
5c3c48ac
JB
1779 /* perform basic checks on the msg */
1780 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1781
1782 if (ret) {
499ec80f 1783 dev_err(&pf->pdev->dev, "Invalid message from vf %d, opcode %d, len %d\n",
7efa84b7 1784 local_vf_id, v_opcode, msglen);
5c3c48ac
JB
1785 return ret;
1786 }
bae3cae4 1787
5c3c48ac
JB
1788 switch (v_opcode) {
1789 case I40E_VIRTCHNL_OP_VERSION:
1790 ret = i40e_vc_get_version_msg(vf);
1791 break;
1792 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1793 ret = i40e_vc_get_vf_resources_msg(vf);
1794 break;
1795 case I40E_VIRTCHNL_OP_RESET_VF:
fc18eaa0
MW
1796 i40e_vc_reset_vf_msg(vf);
1797 ret = 0;
5c3c48ac
JB
1798 break;
1799 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1800 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1801 break;
1802 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1803 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1804 break;
1805 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1806 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1807 break;
1808 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1809 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
1810 break;
1811 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1812 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1813 break;
1814 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1815 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1816 break;
1817 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1818 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1819 break;
1820 case I40E_VIRTCHNL_OP_ADD_VLAN:
1821 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1822 break;
1823 case I40E_VIRTCHNL_OP_DEL_VLAN:
1824 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1825 break;
1826 case I40E_VIRTCHNL_OP_GET_STATS:
1827 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1828 break;
5c3c48ac
JB
1829 case I40E_VIRTCHNL_OP_UNKNOWN:
1830 default:
7efa84b7
MW
1831 dev_err(&pf->pdev->dev, "Unsupported opcode %d from vf %d\n",
1832 v_opcode, local_vf_id);
5c3c48ac
JB
1833 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1834 I40E_ERR_NOT_IMPLEMENTED);
1835 break;
1836 }
1837
1838 return ret;
1839}
1840
1841/**
1842 * i40e_vc_process_vflr_event
1843 * @pf: pointer to the pf structure
1844 *
1845 * called from the vlfr irq handler to
1846 * free up vf resources and state variables
1847 **/
1848int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1849{
1850 u32 reg, reg_idx, bit_idx, vf_id;
1851 struct i40e_hw *hw = &pf->hw;
1852 struct i40e_vf *vf;
1853
1854 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1855 return 0;
1856
1857 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1858 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1859 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1860 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1861 /* read GLGEN_VFLRSTAT register to find out the flr vfs */
1862 vf = &pf->vf[vf_id];
1863 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
1864 if (reg & (1 << bit_idx)) {
1865 /* clear the bit in GLGEN_VFLRSTAT */
1866 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
1867
fc18eaa0 1868 i40e_reset_vf(vf, true);
5c3c48ac
JB
1869 }
1870 }
1871
1872 /* re-enable vflr interrupt cause */
1873 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1874 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1875 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1876 i40e_flush(hw);
1877
1878 return 0;
1879}
1880
1881/**
1882 * i40e_vc_vf_broadcast
1883 * @pf: pointer to the pf structure
1884 * @opcode: operation code
1885 * @retval: return value
1886 * @msg: pointer to the msg buffer
1887 * @msglen: msg length
1888 *
1889 * send a message to all VFs on a given PF
1890 **/
1891static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
1892 enum i40e_virtchnl_ops v_opcode,
1893 i40e_status v_retval, u8 *msg,
1894 u16 msglen)
1895{
1896 struct i40e_hw *hw = &pf->hw;
1897 struct i40e_vf *vf = pf->vf;
1898 int i;
1899
1900 for (i = 0; i < pf->num_alloc_vfs; i++) {
1901 /* Ignore return value on purpose - a given VF may fail, but
1902 * we need to keep going and send to all of them
1903 */
1904 i40e_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval,
1905 msg, msglen, NULL);
1906 vf++;
1907 }
1908}
1909
1910/**
1911 * i40e_vc_notify_link_state
1912 * @pf: pointer to the pf structure
1913 *
1914 * send a link status message to all VFs on a given PF
1915 **/
1916void i40e_vc_notify_link_state(struct i40e_pf *pf)
1917{
1918 struct i40e_virtchnl_pf_event pfe;
1919
1920 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
1921 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
1922 pfe.event_data.link_event.link_status =
1923 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
1924 pfe.event_data.link_event.link_speed = pf->hw.phy.link_info.link_speed;
1925
1926 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
1927 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
1928}
1929
1930/**
1931 * i40e_vc_notify_reset
1932 * @pf: pointer to the pf structure
1933 *
1934 * indicate a pending reset to all VFs on a given PF
1935 **/
1936void i40e_vc_notify_reset(struct i40e_pf *pf)
1937{
1938 struct i40e_virtchnl_pf_event pfe;
1939
1940 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1941 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
1942 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
1943 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
1944}
1945
1946/**
1947 * i40e_vc_notify_vf_reset
1948 * @vf: pointer to the vf structure
1949 *
1950 * indicate a pending reset to the given VF
1951 **/
1952void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
1953{
1954 struct i40e_virtchnl_pf_event pfe;
1955
1956 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1957 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
1958 i40e_aq_send_msg_to_vf(&vf->pf->hw, vf->vf_id, I40E_VIRTCHNL_OP_EVENT,
1959 I40E_SUCCESS, (u8 *)&pfe,
1960 sizeof(struct i40e_virtchnl_pf_event), NULL);
1961}
1962
1963/**
1964 * i40e_ndo_set_vf_mac
1965 * @netdev: network interface device structure
1966 * @vf_id: vf identifier
1967 * @mac: mac address
1968 *
1969 * program vf mac address
1970 **/
1971int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
1972{
1973 struct i40e_netdev_priv *np = netdev_priv(netdev);
1974 struct i40e_vsi *vsi = np->vsi;
1975 struct i40e_pf *pf = vsi->back;
1976 struct i40e_mac_filter *f;
1977 struct i40e_vf *vf;
1978 int ret = 0;
1979
1980 /* validate the request */
1981 if (vf_id >= pf->num_alloc_vfs) {
1982 dev_err(&pf->pdev->dev,
1983 "Invalid VF Identifier %d\n", vf_id);
1984 ret = -EINVAL;
1985 goto error_param;
1986 }
1987
1988 vf = &(pf->vf[vf_id]);
1989 vsi = pf->vsi[vf->lan_vsi_index];
1990 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1991 dev_err(&pf->pdev->dev,
1992 "Uninitialized VF %d\n", vf_id);
1993 ret = -EINVAL;
1994 goto error_param;
1995 }
1996
1997 if (!is_valid_ether_addr(mac)) {
1998 dev_err(&pf->pdev->dev,
1999 "Invalid VF ethernet address\n");
2000 ret = -EINVAL;
2001 goto error_param;
2002 }
2003
2004 /* delete the temporary mac address */
2005 i40e_del_filter(vsi, vf->default_lan_addr.addr, 0, true, false);
2006
2007 /* add the new mac address */
2008 f = i40e_add_filter(vsi, mac, 0, true, false);
2009 if (!f) {
2010 dev_err(&pf->pdev->dev,
2011 "Unable to add VF ucast filter\n");
2012 ret = -ENOMEM;
2013 goto error_param;
2014 }
2015
2016 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2017 /* program mac filter */
2018 if (i40e_sync_vsi_filters(vsi)) {
2019 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2020 ret = -EIO;
2021 goto error_param;
2022 }
2023 memcpy(vf->default_lan_addr.addr, mac, ETH_ALEN);
f657a6e1 2024 vf->pf_set_mac = true;
5c3c48ac
JB
2025 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
2026 ret = 0;
2027
2028error_param:
2029 return ret;
2030}
2031
2032/**
2033 * i40e_ndo_set_vf_port_vlan
2034 * @netdev: network interface device structure
2035 * @vf_id: vf identifier
2036 * @vlan_id: mac address
2037 * @qos: priority setting
2038 *
2039 * program vf vlan id and/or qos
2040 **/
2041int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2042 int vf_id, u16 vlan_id, u8 qos)
2043{
2044 struct i40e_netdev_priv *np = netdev_priv(netdev);
2045 struct i40e_pf *pf = np->vsi->back;
2046 struct i40e_vsi *vsi;
2047 struct i40e_vf *vf;
2048 int ret = 0;
2049
2050 /* validate the request */
2051 if (vf_id >= pf->num_alloc_vfs) {
2052 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2053 ret = -EINVAL;
2054 goto error_pvid;
2055 }
2056
2057 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2058 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2059 ret = -EINVAL;
2060 goto error_pvid;
2061 }
2062
2063 vf = &(pf->vf[vf_id]);
2064 vsi = pf->vsi[vf->lan_vsi_index];
2065 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2066 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2067 ret = -EINVAL;
2068 goto error_pvid;
2069 }
2070
99a4973c
GR
2071 if (vsi->info.pvid == 0 && i40e_is_vsi_in_vlan(vsi))
2072 dev_err(&pf->pdev->dev,
2073 "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",
2074 vf_id);
2075
8d82a7c5
GR
2076 /* Check for condition where there was already a port VLAN ID
2077 * filter set and now it is being deleted by setting it to zero.
2078 * Before deleting all the old VLAN filters we must add new ones
2079 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2080 * MAC addresses deleted.
2081 */
2082 if (!(vlan_id || qos) && vsi->info.pvid)
2083 ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2084
5c3c48ac
JB
2085 if (vsi->info.pvid) {
2086 /* kill old VLAN */
2087 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2088 VLAN_VID_MASK));
2089 if (ret) {
2090 dev_info(&vsi->back->pdev->dev,
2091 "remove VLAN failed, ret=%d, aq_err=%d\n",
2092 ret, pf->hw.aq.asq_last_status);
2093 }
2094 }
2095 if (vlan_id || qos)
2096 ret = i40e_vsi_add_pvid(vsi,
2097 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
2098 else
6c12fcbf 2099 i40e_vsi_remove_pvid(vsi);
5c3c48ac
JB
2100
2101 if (vlan_id) {
2102 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2103 vlan_id, qos, vf_id);
2104
2105 /* add new VLAN filter */
2106 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2107 if (ret) {
2108 dev_info(&vsi->back->pdev->dev,
2109 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2110 vsi->back->hw.aq.asq_last_status);
2111 goto error_pvid;
2112 }
8d82a7c5
GR
2113 /* Kill non-vlan MAC filters - ignore error return since
2114 * there might not be any non-vlan MAC filters.
2115 */
2116 i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
5c3c48ac
JB
2117 }
2118
2119 if (ret) {
2120 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2121 goto error_pvid;
2122 }
6c12fcbf
GR
2123 /* The Port VLAN needs to be saved across resets the same as the
2124 * default LAN MAC address.
2125 */
2126 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
5c3c48ac
JB
2127 ret = 0;
2128
2129error_pvid:
2130 return ret;
2131}
2132
2133/**
2134 * i40e_ndo_set_vf_bw
2135 * @netdev: network interface device structure
2136 * @vf_id: vf identifier
2137 * @tx_rate: tx rate
2138 *
2139 * configure vf tx rate
2140 **/
2141int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int tx_rate)
2142{
2143 return -EOPNOTSUPP;
2144}
2145
2146/**
2147 * i40e_ndo_get_vf_config
2148 * @netdev: network interface device structure
2149 * @vf_id: vf identifier
2150 * @ivi: vf configuration structure
2151 *
2152 * return vf configuration
2153 **/
2154int i40e_ndo_get_vf_config(struct net_device *netdev,
2155 int vf_id, struct ifla_vf_info *ivi)
2156{
2157 struct i40e_netdev_priv *np = netdev_priv(netdev);
5c3c48ac
JB
2158 struct i40e_vsi *vsi = np->vsi;
2159 struct i40e_pf *pf = vsi->back;
2160 struct i40e_vf *vf;
2161 int ret = 0;
2162
2163 /* validate the request */
2164 if (vf_id >= pf->num_alloc_vfs) {
2165 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2166 ret = -EINVAL;
2167 goto error_param;
2168 }
2169
2170 vf = &(pf->vf[vf_id]);
2171 /* first vsi is always the LAN vsi */
2172 vsi = pf->vsi[vf->lan_vsi_index];
2173 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2174 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2175 ret = -EINVAL;
2176 goto error_param;
2177 }
2178
2179 ivi->vf = vf_id;
2180
f4a1c5cf 2181 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
5c3c48ac
JB
2182
2183 ivi->tx_rate = 0;
2184 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2185 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2186 I40E_VLAN_PRIORITY_SHIFT;
2187 ret = 0;
2188
2189error_param:
2190 return ret;
2191}
This page took 0.207868 seconds and 5 git commands to generate.