blob: bed60220e6924ea313e728211f639a9df50adc67 [file] [log] [blame]
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
4 * Copyright(c) 2013 Intel Corporation.
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 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
21 *
22 * Contact Information:
23 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 *
26 ******************************************************************************/
27
28#include "i40e.h"
29
30/***********************misc routines*****************************/
31
32/**
33 * i40e_vc_isvalid_vsi_id
34 * @vf: pointer to the vf info
35 * @vsi_id: vf relative vsi id
36 *
37 * check for the valid vsi id
38 **/
39static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u8 vsi_id)
40{
41 struct i40e_pf *pf = vf->pf;
42
43 return pf->vsi[vsi_id]->vf_id == vf->vf_id;
44}
45
46/**
47 * i40e_vc_isvalid_queue_id
48 * @vf: pointer to the vf info
49 * @vsi_id: vsi id
50 * @qid: vsi relative queue id
51 *
52 * check for the valid queue id
53 **/
54static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u8 vsi_id,
55 u8 qid)
56{
57 struct i40e_pf *pf = vf->pf;
58
59 return qid < pf->vsi[vsi_id]->num_queue_pairs;
60}
61
62/**
63 * i40e_vc_isvalid_vector_id
64 * @vf: pointer to the vf info
65 * @vector_id: vf relative vector id
66 *
67 * check for the valid vector id
68 **/
69static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
70{
71 struct i40e_pf *pf = vf->pf;
72
Mitch Williams54692b42013-11-16 10:00:38 +000073 return vector_id <= pf->hw.func_caps.num_msix_vectors_vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000074}
75
76/***********************vf resource mgmt routines*****************/
77
78/**
79 * i40e_vc_get_pf_queue_id
80 * @vf: pointer to the vf info
81 * @vsi_idx: index of VSI in PF struct
82 * @vsi_queue_id: vsi relative queue id
83 *
84 * return pf relative queue id
85 **/
86static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u8 vsi_idx,
87 u8 vsi_queue_id)
88{
89 struct i40e_pf *pf = vf->pf;
90 struct i40e_vsi *vsi = pf->vsi[vsi_idx];
91 u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
92
93 if (le16_to_cpu(vsi->info.mapping_flags) &
94 I40E_AQ_VSI_QUE_MAP_NONCONTIG)
95 pf_queue_id =
96 le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
97 else
98 pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
99 vsi_queue_id;
100
101 return pf_queue_id;
102}
103
104/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000105 * i40e_config_irq_link_list
106 * @vf: pointer to the vf info
107 * @vsi_idx: index of VSI in PF struct
108 * @vecmap: irq map info
109 *
110 * configure irq link list from the map
111 **/
112static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_idx,
113 struct i40e_virtchnl_vector_map *vecmap)
114{
115 unsigned long linklistmap = 0, tempmap;
116 struct i40e_pf *pf = vf->pf;
117 struct i40e_hw *hw = &pf->hw;
118 u16 vsi_queue_id, pf_queue_id;
119 enum i40e_queue_type qtype;
120 u16 next_q, vector_id;
121 u32 reg, reg_idx;
122 u16 itr_idx = 0;
123
124 vector_id = vecmap->vector_id;
125 /* setup the head */
126 if (0 == vector_id)
127 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
128 else
129 reg_idx = I40E_VPINT_LNKLSTN(
Mitch Williams13c60b92013-09-28 07:13:18 +0000130 (pf->hw.func_caps.num_msix_vectors_vf
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000131 * vf->vf_id) + (vector_id - 1));
132
133 if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
134 /* Special case - No queues mapped on this vector */
135 wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
136 goto irq_list_done;
137 }
138 tempmap = vecmap->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000139 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000140 linklistmap |= (1 <<
141 (I40E_VIRTCHNL_SUPPORTED_QTYPES *
142 vsi_queue_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000143 }
144
145 tempmap = vecmap->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000146 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000147 linklistmap |= (1 <<
148 (I40E_VIRTCHNL_SUPPORTED_QTYPES * vsi_queue_id
149 + 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000150 }
151
152 next_q = find_first_bit(&linklistmap,
153 (I40E_MAX_VSI_QP *
154 I40E_VIRTCHNL_SUPPORTED_QTYPES));
155 vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
156 qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
157 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx, vsi_queue_id);
158 reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
159
160 wr32(hw, reg_idx, reg);
161
162 while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
163 switch (qtype) {
164 case I40E_QUEUE_TYPE_RX:
165 reg_idx = I40E_QINT_RQCTL(pf_queue_id);
166 itr_idx = vecmap->rxitr_idx;
167 break;
168 case I40E_QUEUE_TYPE_TX:
169 reg_idx = I40E_QINT_TQCTL(pf_queue_id);
170 itr_idx = vecmap->txitr_idx;
171 break;
172 default:
173 break;
174 }
175
176 next_q = find_next_bit(&linklistmap,
177 (I40E_MAX_VSI_QP *
178 I40E_VIRTCHNL_SUPPORTED_QTYPES),
179 next_q + 1);
180 if (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
181 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
182 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
183 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx,
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;
Shannon Nelson13fd9772013-09-28 07:14:19 +0000256 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000257 & 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;
372 struct i40e_hw *hw = &pf->hw;
373 struct i40e_vsi *vsi;
374 int ret = 0;
375
376 vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
377
378 if (!vsi) {
379 dev_err(&pf->pdev->dev,
380 "add vsi failed for vf %d, aq_err %d\n",
381 vf->vf_id, pf->hw.aq.asq_last_status);
382 ret = -ENOENT;
383 goto error_alloc_vsi_res;
384 }
385 if (type == I40E_VSI_SRIOV) {
386 vf->lan_vsi_index = vsi->idx;
387 vf->lan_vsi_id = vsi->id;
388 dev_info(&pf->pdev->dev,
389 "LAN VSI index %d, VSI id %d\n",
390 vsi->idx, vsi->id);
Greg Rose6c12fcb2013-11-28 06:39:34 +0000391 /* 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);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000399 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
Greg Rose6c12fcb2013-11-28 06:39:34 +0000400 vf->port_vlan_id, true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000401 }
Neerav Parikh6dbbbfb2013-11-26 10:49:24 +0000402
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000403 if (!f) {
404 dev_err(&pf->pdev->dev, "Unable to add ucast filter\n");
405 ret = -ENOMEM;
406 goto error_alloc_vsi_res;
407 }
408
409 /* program mac filter */
410 ret = i40e_sync_vsi_filters(vsi);
411 if (ret) {
412 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
413 goto error_alloc_vsi_res;
414 }
415
416 /* accept bcast pkts. by default */
417 ret = i40e_aq_set_vsi_broadcast(hw, vsi->seid, true, NULL);
418 if (ret) {
419 dev_err(&pf->pdev->dev,
420 "set vsi bcast failed for vf %d, vsi %d, aq_err %d\n",
421 vf->vf_id, vsi->idx, pf->hw.aq.asq_last_status);
422 ret = -EINVAL;
423 }
424
425error_alloc_vsi_res:
426 return ret;
427}
428
429/**
Mitch Williams805bd5b2013-11-28 06:39:26 +0000430 * i40e_enable_vf_mappings
431 * @vf: pointer to the vf info
432 *
433 * enable vf mappings
434 **/
435static void i40e_enable_vf_mappings(struct i40e_vf *vf)
436{
437 struct i40e_pf *pf = vf->pf;
438 struct i40e_hw *hw = &pf->hw;
439 u32 reg, total_queue_pairs = 0;
440 int j;
441
442 /* Tell the hardware we're using noncontiguous mapping. HW requires
443 * that VF queues be mapped using this method, even when they are
444 * contiguous in real life
445 */
446 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
447 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
448
449 /* enable VF vplan_qtable mappings */
450 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
451 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
452
453 /* map PF queues to VF queues */
454 for (j = 0; j < pf->vsi[vf->lan_vsi_index]->num_queue_pairs; j++) {
455 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index, j);
456 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
457 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
458 total_queue_pairs++;
459 }
460
461 /* map PF queues to VSI */
462 for (j = 0; j < 7; j++) {
463 if (j * 2 >= pf->vsi[vf->lan_vsi_index]->num_queue_pairs) {
464 reg = 0x07FF07FF; /* unused */
465 } else {
466 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index,
467 j * 2);
468 reg = qid;
469 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index,
470 (j * 2) + 1);
471 reg |= qid << 16;
472 }
473 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
474 }
475
476 i40e_flush(hw);
477}
478
479/**
480 * i40e_disable_vf_mappings
481 * @vf: pointer to the vf info
482 *
483 * disable vf mappings
484 **/
485static void i40e_disable_vf_mappings(struct i40e_vf *vf)
486{
487 struct i40e_pf *pf = vf->pf;
488 struct i40e_hw *hw = &pf->hw;
489 int i;
490
491 /* disable qp mappings */
492 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
493 for (i = 0; i < I40E_MAX_VSI_QP; i++)
494 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
495 I40E_QUEUE_END_OF_LIST);
496 i40e_flush(hw);
497}
498
499/**
500 * i40e_free_vf_res
501 * @vf: pointer to the vf info
502 *
503 * free vf resources
504 **/
505static void i40e_free_vf_res(struct i40e_vf *vf)
506{
507 struct i40e_pf *pf = vf->pf;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000508 struct i40e_hw *hw = &pf->hw;
509 u32 reg_idx, reg;
510 int i, msix_vf;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000511
512 /* free vsi & disconnect it from the parent uplink */
513 if (vf->lan_vsi_index) {
514 i40e_vsi_release(pf->vsi[vf->lan_vsi_index]);
515 vf->lan_vsi_index = 0;
516 vf->lan_vsi_id = 0;
517 }
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000518 msix_vf = pf->hw.func_caps.num_msix_vectors_vf + 1;
519 /* disable interrupts so the VF starts in a known state */
520 for (i = 0; i < msix_vf; i++) {
521 /* format is same for both registers */
522 if (0 == i)
523 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
524 else
525 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
526 (vf->vf_id))
527 + (i - 1));
528 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
529 i40e_flush(hw);
530 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000531
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000532 /* clear the irq settings */
533 for (i = 0; i < msix_vf; i++) {
534 /* format is same for both registers */
535 if (0 == i)
536 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
537 else
538 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
539 (vf->vf_id))
540 + (i - 1));
541 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
542 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
543 wr32(hw, reg_idx, reg);
544 i40e_flush(hw);
545 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000546 /* reset some of the state varibles keeping
547 * track of the resources
548 */
549 vf->num_queue_pairs = 0;
550 vf->vf_states = 0;
551}
552
553/**
554 * i40e_alloc_vf_res
555 * @vf: pointer to the vf info
556 *
557 * allocate vf resources
558 **/
559static int i40e_alloc_vf_res(struct i40e_vf *vf)
560{
561 struct i40e_pf *pf = vf->pf;
562 int total_queue_pairs = 0;
563 int ret;
564
565 /* allocate hw vsi context & associated resources */
566 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
567 if (ret)
568 goto error_alloc;
569 total_queue_pairs += pf->vsi[vf->lan_vsi_index]->num_queue_pairs;
570 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
571
572 /* store the total qps number for the runtime
573 * vf req validation
574 */
575 vf->num_queue_pairs = total_queue_pairs;
576
577 /* vf is now completely initialized */
578 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
579
580error_alloc:
581 if (ret)
582 i40e_free_vf_res(vf);
583
584 return ret;
585}
586
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000587#define VF_DEVICE_STATUS 0xAA
588#define VF_TRANS_PENDING_MASK 0x20
589/**
590 * i40e_quiesce_vf_pci
591 * @vf: pointer to the vf structure
592 *
593 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
594 * if the transactions never clear.
595 **/
596static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
597{
598 struct i40e_pf *pf = vf->pf;
599 struct i40e_hw *hw = &pf->hw;
600 int vf_abs_id, i;
601 u32 reg;
602
Mitch Williamsb141d612013-11-28 06:39:36 +0000603 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000604
605 wr32(hw, I40E_PF_PCI_CIAA,
606 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
607 for (i = 0; i < 100; i++) {
608 reg = rd32(hw, I40E_PF_PCI_CIAD);
609 if ((reg & VF_TRANS_PENDING_MASK) == 0)
610 return 0;
611 udelay(1);
612 }
613 return -EIO;
614}
615
Mitch Williams805bd5b2013-11-28 06:39:26 +0000616/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000617 * i40e_reset_vf
618 * @vf: pointer to the vf structure
619 * @flr: VFLR was issued or not
620 *
621 * reset the vf
622 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000623void i40e_reset_vf(struct i40e_vf *vf, bool flr)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000624{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000625 struct i40e_pf *pf = vf->pf;
626 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000627 bool rsd = false;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000628 int i;
629 u32 reg;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000630
631 /* warn the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000632 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
633
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000634 /* In the case of a VFLR, the HW has already reset the VF and we
635 * just need to clean up, so don't hit the VFRTRIG register.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000636 */
637 if (!flr) {
638 /* reset vf using VPGEN_VFRTRIG reg */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000639 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
640 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000641 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
642 i40e_flush(hw);
643 }
644
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000645 if (i40e_quiesce_vf_pci(vf))
646 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
647 vf->vf_id);
648
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000649 /* poll VPGEN_VFRSTAT reg to make sure
650 * that reset is complete
651 */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000652 for (i = 0; i < 100; i++) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000653 /* vf reset requires driver to first reset the
654 * vf & than poll the status register to make sure
655 * that the requested op was completed
656 * successfully
657 */
658 udelay(10);
659 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
660 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
661 rsd = true;
662 break;
663 }
664 }
665
666 if (!rsd)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000667 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000668 vf->vf_id);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000669 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000670 /* clear the reset bit in the VPGEN_VFRTRIG reg */
671 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
672 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
673 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000674
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000675 /* On initial reset, we won't have any queues */
676 if (vf->lan_vsi_index == 0)
677 goto complete_reset;
678
679 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_index], false);
680complete_reset:
681 /* reallocate vf resources to reset the VSI state */
682 i40e_free_vf_res(vf);
683 mdelay(10);
684 i40e_alloc_vf_res(vf);
685 i40e_enable_vf_mappings(vf);
686
687 /* tell the VF the reset is done */
688 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
689 i40e_flush(hw);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000690}
691
692/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000693 * i40e_vfs_are_assigned
694 * @pf: pointer to the pf structure
695 *
696 * Determine if any VFs are assigned to VMs
697 **/
698static bool i40e_vfs_are_assigned(struct i40e_pf *pf)
699{
700 struct pci_dev *pdev = pf->pdev;
701 struct pci_dev *vfdev;
702
703 /* loop through all the VFs to see if we own any that are assigned */
704 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, I40E_VF_DEVICE_ID , NULL);
705 while (vfdev) {
706 /* if we don't own it we don't care */
707 if (vfdev->is_virtfn && pci_physfn(vfdev) == pdev) {
708 /* if it is assigned we cannot release it */
709 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
710 return true;
711 }
712
713 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL,
714 I40E_VF_DEVICE_ID,
715 vfdev);
716 }
717
718 return false;
719}
720
721/**
722 * i40e_free_vfs
723 * @pf: pointer to the pf structure
724 *
725 * free vf resources
726 **/
727void i40e_free_vfs(struct i40e_pf *pf)
728{
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000729 int i, tmp;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000730
731 if (!pf->vf)
732 return;
733
734 /* Disable interrupt 0 so we don't try to handle the VFLR. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000735 i40e_irq_dynamic_disable_icr0(pf);
736
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000737 mdelay(10); /* let any messages in transit get finished up */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000738 /* free up vf resources */
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000739 tmp = pf->num_alloc_vfs;
740 pf->num_alloc_vfs = 0;
741 for (i = 0; i < tmp; i++) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000742 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
743 i40e_free_vf_res(&pf->vf[i]);
744 /* disable qp mappings */
745 i40e_disable_vf_mappings(&pf->vf[i]);
746 }
747
748 kfree(pf->vf);
749 pf->vf = NULL;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000750
751 if (!i40e_vfs_are_assigned(pf))
752 pci_disable_sriov(pf->pdev);
753 else
754 dev_warn(&pf->pdev->dev,
755 "unable to disable SR-IOV because VFs are assigned.\n");
756
757 /* Re-enable interrupt 0. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000758 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000759}
760
761#ifdef CONFIG_PCI_IOV
762/**
763 * i40e_alloc_vfs
764 * @pf: pointer to the pf structure
765 * @num_alloc_vfs: number of vfs to allocate
766 *
767 * allocate vf resources
768 **/
769static int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
770{
771 struct i40e_vf *vfs;
772 int i, ret = 0;
773
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000774 /* Disable interrupt 0 so we don't try to handle the VFLR. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000775 i40e_irq_dynamic_disable_icr0(pf);
776
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000777 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
778 if (ret) {
779 dev_err(&pf->pdev->dev,
780 "pci_enable_sriov failed with error %d!\n", ret);
781 pf->num_alloc_vfs = 0;
782 goto err_iov;
783 }
784
785 /* allocate memory */
786 vfs = kzalloc(num_alloc_vfs * sizeof(struct i40e_vf), GFP_KERNEL);
787 if (!vfs) {
788 ret = -ENOMEM;
789 goto err_alloc;
790 }
791
792 /* apply default profile */
793 for (i = 0; i < num_alloc_vfs; i++) {
794 vfs[i].pf = pf;
795 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
796 vfs[i].vf_id = i;
797
798 /* assign default capabilities */
799 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000800 /* vf resources get allocated during reset */
801 i40e_reset_vf(&vfs[i], false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000802
803 /* enable vf vplan_qtable mappings */
804 i40e_enable_vf_mappings(&vfs[i]);
805 }
806 pf->vf = vfs;
807 pf->num_alloc_vfs = num_alloc_vfs;
808
809err_alloc:
810 if (ret)
811 i40e_free_vfs(pf);
812err_iov:
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000813 /* Re-enable interrupt 0. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000814 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000815 return ret;
816}
817
818#endif
819/**
820 * i40e_pci_sriov_enable
821 * @pdev: pointer to a pci_dev structure
822 * @num_vfs: number of vfs to allocate
823 *
824 * Enable or change the number of VFs
825 **/
826static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
827{
828#ifdef CONFIG_PCI_IOV
829 struct i40e_pf *pf = pci_get_drvdata(pdev);
830 int pre_existing_vfs = pci_num_vf(pdev);
831 int err = 0;
832
833 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
834 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
835 i40e_free_vfs(pf);
836 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
837 goto out;
838
839 if (num_vfs > pf->num_req_vfs) {
840 err = -EPERM;
841 goto err_out;
842 }
843
844 err = i40e_alloc_vfs(pf, num_vfs);
845 if (err) {
846 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
847 goto err_out;
848 }
849
850out:
851 return num_vfs;
852
853err_out:
854 return err;
855#endif
856 return 0;
857}
858
859/**
860 * i40e_pci_sriov_configure
861 * @pdev: pointer to a pci_dev structure
862 * @num_vfs: number of vfs to allocate
863 *
864 * Enable or change the number of VFs. Called when the user updates the number
865 * of VFs in sysfs.
866 **/
867int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
868{
869 struct i40e_pf *pf = pci_get_drvdata(pdev);
870
871 if (num_vfs)
872 return i40e_pci_sriov_enable(pdev, num_vfs);
873
874 i40e_free_vfs(pf);
875 return 0;
876}
877
878/***********************virtual channel routines******************/
879
880/**
881 * i40e_vc_send_msg_to_vf
882 * @vf: pointer to the vf info
883 * @v_opcode: virtual channel opcode
884 * @v_retval: virtual channel return value
885 * @msg: pointer to the msg buffer
886 * @msglen: msg length
887 *
888 * send msg to vf
889 **/
890static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
891 u32 v_retval, u8 *msg, u16 msglen)
892{
893 struct i40e_pf *pf = vf->pf;
894 struct i40e_hw *hw = &pf->hw;
895 i40e_status aq_ret;
896
897 /* single place to detect unsuccessful return values */
898 if (v_retval) {
899 vf->num_invalid_msgs++;
900 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
901 v_opcode, v_retval);
902 if (vf->num_invalid_msgs >
903 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
904 dev_err(&pf->pdev->dev,
905 "Number of invalid messages exceeded for VF %d\n",
906 vf->vf_id);
907 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
908 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
909 }
910 } else {
911 vf->num_valid_msgs++;
912 }
913
914 aq_ret = i40e_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval,
915 msg, msglen, NULL);
916 if (aq_ret) {
917 dev_err(&pf->pdev->dev,
918 "Unable to send the message to VF %d aq_err %d\n",
919 vf->vf_id, pf->hw.aq.asq_last_status);
920 return -EIO;
921 }
922
923 return 0;
924}
925
926/**
927 * i40e_vc_send_resp_to_vf
928 * @vf: pointer to the vf info
929 * @opcode: operation code
930 * @retval: return value
931 *
932 * send resp msg to vf
933 **/
934static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
935 enum i40e_virtchnl_ops opcode,
936 i40e_status retval)
937{
938 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
939}
940
941/**
942 * i40e_vc_get_version_msg
943 * @vf: pointer to the vf info
944 *
945 * called from the vf to request the API version used by the PF
946 **/
947static int i40e_vc_get_version_msg(struct i40e_vf *vf)
948{
949 struct i40e_virtchnl_version_info info = {
950 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
951 };
952
953 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
954 I40E_SUCCESS, (u8 *)&info,
955 sizeof(struct
956 i40e_virtchnl_version_info));
957}
958
959/**
960 * i40e_vc_get_vf_resources_msg
961 * @vf: pointer to the vf info
962 * @msg: pointer to the msg buffer
963 * @msglen: msg length
964 *
965 * called from the vf to request its resources
966 **/
967static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf)
968{
969 struct i40e_virtchnl_vf_resource *vfres = NULL;
970 struct i40e_pf *pf = vf->pf;
971 i40e_status aq_ret = 0;
972 struct i40e_vsi *vsi;
973 int i = 0, len = 0;
974 int num_vsis = 1;
975 int ret;
976
977 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
978 aq_ret = I40E_ERR_PARAM;
979 goto err;
980 }
981
982 len = (sizeof(struct i40e_virtchnl_vf_resource) +
983 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
984
985 vfres = kzalloc(len, GFP_KERNEL);
986 if (!vfres) {
987 aq_ret = I40E_ERR_NO_MEMORY;
988 len = 0;
989 goto err;
990 }
991
992 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
993 vsi = pf->vsi[vf->lan_vsi_index];
994 if (!vsi->info.pvid)
995 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
996
997 vfres->num_vsis = num_vsis;
998 vfres->num_queue_pairs = vf->num_queue_pairs;
999 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1000 if (vf->lan_vsi_index) {
1001 vfres->vsi_res[i].vsi_id = vf->lan_vsi_index;
1002 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1003 vfres->vsi_res[i].num_queue_pairs =
1004 pf->vsi[vf->lan_vsi_index]->num_queue_pairs;
1005 memcpy(vfres->vsi_res[i].default_mac_addr,
1006 vf->default_lan_addr.addr, ETH_ALEN);
1007 i++;
1008 }
1009 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1010
1011err:
1012 /* send the response back to the vf */
1013 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1014 aq_ret, (u8 *)vfres, len);
1015
1016 kfree(vfres);
1017 return ret;
1018}
1019
1020/**
1021 * i40e_vc_reset_vf_msg
1022 * @vf: pointer to the vf info
1023 * @msg: pointer to the msg buffer
1024 * @msglen: msg length
1025 *
1026 * called from the vf to reset itself,
1027 * unlike other virtchnl messages, pf driver
1028 * doesn't send the response back to the vf
1029 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001030static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001031{
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001032 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1033 i40e_reset_vf(vf, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001034}
1035
1036/**
1037 * i40e_vc_config_promiscuous_mode_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 configure the promiscuous mode of
1043 * vf vsis
1044 **/
1045static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1046 u8 *msg, u16 msglen)
1047{
1048 struct i40e_virtchnl_promisc_info *info =
1049 (struct i40e_virtchnl_promisc_info *)msg;
1050 struct i40e_pf *pf = vf->pf;
1051 struct i40e_hw *hw = &pf->hw;
1052 bool allmulti = false;
1053 bool promisc = false;
1054 i40e_status aq_ret;
1055
1056 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1057 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1058 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1059 (pf->vsi[info->vsi_id]->type != I40E_VSI_FCOE)) {
1060 aq_ret = I40E_ERR_PARAM;
1061 goto error_param;
1062 }
1063
1064 if (info->flags & I40E_FLAG_VF_UNICAST_PROMISC)
1065 promisc = true;
1066 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, info->vsi_id,
1067 promisc, NULL);
1068 if (aq_ret)
1069 goto error_param;
1070
1071 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1072 allmulti = true;
1073 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, info->vsi_id,
1074 allmulti, NULL);
1075
1076error_param:
1077 /* send the response to the vf */
1078 return i40e_vc_send_resp_to_vf(vf,
1079 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1080 aq_ret);
1081}
1082
1083/**
1084 * i40e_vc_config_queues_msg
1085 * @vf: pointer to the vf info
1086 * @msg: pointer to the msg buffer
1087 * @msglen: msg length
1088 *
1089 * called from the vf to configure the rx/tx
1090 * queues
1091 **/
1092static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1093{
1094 struct i40e_virtchnl_vsi_queue_config_info *qci =
1095 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1096 struct i40e_virtchnl_queue_pair_info *qpi;
1097 u16 vsi_id, vsi_queue_id;
1098 i40e_status aq_ret = 0;
1099 int i;
1100
1101 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1102 aq_ret = I40E_ERR_PARAM;
1103 goto error_param;
1104 }
1105
1106 vsi_id = qci->vsi_id;
1107 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1108 aq_ret = I40E_ERR_PARAM;
1109 goto error_param;
1110 }
1111 for (i = 0; i < qci->num_queue_pairs; i++) {
1112 qpi = &qci->qpair[i];
1113 vsi_queue_id = qpi->txq.queue_id;
1114 if ((qpi->txq.vsi_id != vsi_id) ||
1115 (qpi->rxq.vsi_id != vsi_id) ||
1116 (qpi->rxq.queue_id != vsi_queue_id) ||
1117 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1118 aq_ret = I40E_ERR_PARAM;
1119 goto error_param;
1120 }
1121
1122 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1123 &qpi->rxq) ||
1124 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1125 &qpi->txq)) {
1126 aq_ret = I40E_ERR_PARAM;
1127 goto error_param;
1128 }
1129 }
1130
1131error_param:
1132 /* send the response to the vf */
1133 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1134 aq_ret);
1135}
1136
1137/**
1138 * i40e_vc_config_irq_map_msg
1139 * @vf: pointer to the vf info
1140 * @msg: pointer to the msg buffer
1141 * @msglen: msg length
1142 *
1143 * called from the vf to configure the irq to
1144 * queue map
1145 **/
1146static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1147{
1148 struct i40e_virtchnl_irq_map_info *irqmap_info =
1149 (struct i40e_virtchnl_irq_map_info *)msg;
1150 struct i40e_virtchnl_vector_map *map;
1151 u16 vsi_id, vsi_queue_id, vector_id;
1152 i40e_status aq_ret = 0;
1153 unsigned long tempmap;
1154 int i;
1155
1156 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1157 aq_ret = I40E_ERR_PARAM;
1158 goto error_param;
1159 }
1160
1161 for (i = 0; i < irqmap_info->num_vectors; i++) {
1162 map = &irqmap_info->vecmap[i];
1163
1164 vector_id = map->vector_id;
1165 vsi_id = map->vsi_id;
1166 /* validate msg params */
1167 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1168 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1169 aq_ret = I40E_ERR_PARAM;
1170 goto error_param;
1171 }
1172
1173 /* lookout for the invalid queue index */
1174 tempmap = map->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001175 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001176 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1177 vsi_queue_id)) {
1178 aq_ret = I40E_ERR_PARAM;
1179 goto error_param;
1180 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001181 }
1182
1183 tempmap = map->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001184 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001185 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1186 vsi_queue_id)) {
1187 aq_ret = I40E_ERR_PARAM;
1188 goto error_param;
1189 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001190 }
1191
1192 i40e_config_irq_link_list(vf, vsi_id, map);
1193 }
1194error_param:
1195 /* send the response to the vf */
1196 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1197 aq_ret);
1198}
1199
1200/**
1201 * i40e_vc_enable_queues_msg
1202 * @vf: pointer to the vf info
1203 * @msg: pointer to the msg buffer
1204 * @msglen: msg length
1205 *
1206 * called from the vf to enable all or specific queue(s)
1207 **/
1208static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1209{
1210 struct i40e_virtchnl_queue_select *vqs =
1211 (struct i40e_virtchnl_queue_select *)msg;
1212 struct i40e_pf *pf = vf->pf;
1213 u16 vsi_id = vqs->vsi_id;
1214 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001215
1216 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1217 aq_ret = I40E_ERR_PARAM;
1218 goto error_param;
1219 }
1220
1221 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1222 aq_ret = I40E_ERR_PARAM;
1223 goto error_param;
1224 }
1225
1226 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1227 aq_ret = I40E_ERR_PARAM;
1228 goto error_param;
1229 }
Mitch Williams88f65632013-11-28 06:39:28 +00001230 if (i40e_vsi_control_rings(pf->vsi[vsi_id], true))
1231 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001232error_param:
1233 /* send the response to the vf */
1234 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1235 aq_ret);
1236}
1237
1238/**
1239 * i40e_vc_disable_queues_msg
1240 * @vf: pointer to the vf info
1241 * @msg: pointer to the msg buffer
1242 * @msglen: msg length
1243 *
1244 * called from the vf to disable all or specific
1245 * queue(s)
1246 **/
1247static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1248{
1249 struct i40e_virtchnl_queue_select *vqs =
1250 (struct i40e_virtchnl_queue_select *)msg;
1251 struct i40e_pf *pf = vf->pf;
1252 u16 vsi_id = vqs->vsi_id;
1253 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001254
1255 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1256 aq_ret = I40E_ERR_PARAM;
1257 goto error_param;
1258 }
1259
1260 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1261 aq_ret = I40E_ERR_PARAM;
1262 goto error_param;
1263 }
1264
1265 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1266 aq_ret = I40E_ERR_PARAM;
1267 goto error_param;
1268 }
Mitch Williams88f65632013-11-28 06:39:28 +00001269 if (i40e_vsi_control_rings(pf->vsi[vsi_id], false))
1270 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001271
1272error_param:
1273 /* send the response to the vf */
1274 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1275 aq_ret);
1276}
1277
1278/**
1279 * i40e_vc_get_stats_msg
1280 * @vf: pointer to the vf info
1281 * @msg: pointer to the msg buffer
1282 * @msglen: msg length
1283 *
1284 * called from the vf to get vsi stats
1285 **/
1286static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1287{
1288 struct i40e_virtchnl_queue_select *vqs =
1289 (struct i40e_virtchnl_queue_select *)msg;
1290 struct i40e_pf *pf = vf->pf;
1291 struct i40e_eth_stats stats;
1292 i40e_status aq_ret = 0;
1293 struct i40e_vsi *vsi;
1294
1295 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1296
1297 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1298 aq_ret = I40E_ERR_PARAM;
1299 goto error_param;
1300 }
1301
1302 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1303 aq_ret = I40E_ERR_PARAM;
1304 goto error_param;
1305 }
1306
1307 vsi = pf->vsi[vqs->vsi_id];
1308 if (!vsi) {
1309 aq_ret = I40E_ERR_PARAM;
1310 goto error_param;
1311 }
1312 i40e_update_eth_stats(vsi);
1313 memcpy(&stats, &vsi->eth_stats, sizeof(struct i40e_eth_stats));
1314
1315error_param:
1316 /* send the response back to the vf */
1317 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1318 (u8 *)&stats, sizeof(stats));
1319}
1320
1321/**
1322 * i40e_vc_add_mac_addr_msg
1323 * @vf: pointer to the vf info
1324 * @msg: pointer to the msg buffer
1325 * @msglen: msg length
1326 *
1327 * add guest mac address filter
1328 **/
1329static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1330{
1331 struct i40e_virtchnl_ether_addr_list *al =
1332 (struct i40e_virtchnl_ether_addr_list *)msg;
1333 struct i40e_pf *pf = vf->pf;
1334 struct i40e_vsi *vsi = NULL;
1335 u16 vsi_id = al->vsi_id;
1336 i40e_status aq_ret = 0;
1337 int i;
1338
1339 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1340 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1341 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1342 aq_ret = I40E_ERR_PARAM;
1343 goto error_param;
1344 }
1345
1346 for (i = 0; i < al->num_elements; i++) {
1347 if (is_broadcast_ether_addr(al->list[i].addr) ||
1348 is_zero_ether_addr(al->list[i].addr)) {
1349 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pMAC\n",
1350 al->list[i].addr);
Mitch Williamsadaf3562013-11-28 06:39:30 +00001351 aq_ret = I40E_ERR_INVALID_MAC_ADDR;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001352 goto error_param;
1353 }
1354 }
1355 vsi = pf->vsi[vsi_id];
1356
1357 /* add new addresses to the list */
1358 for (i = 0; i < al->num_elements; i++) {
1359 struct i40e_mac_filter *f;
1360
1361 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
Mitch Williams7e68edf92013-11-16 10:00:41 +00001362 if (!f) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001363 if (i40e_is_vsi_in_vlan(vsi))
1364 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1365 true, false);
1366 else
1367 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1368 true, false);
1369 }
1370
1371 if (!f) {
1372 dev_err(&pf->pdev->dev,
1373 "Unable to add VF MAC filter\n");
1374 aq_ret = I40E_ERR_PARAM;
1375 goto error_param;
1376 }
1377 }
1378
1379 /* program the updated filter list */
1380 if (i40e_sync_vsi_filters(vsi))
1381 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1382
1383error_param:
1384 /* send the response to the vf */
1385 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
1386 aq_ret);
1387}
1388
1389/**
1390 * i40e_vc_del_mac_addr_msg
1391 * @vf: pointer to the vf info
1392 * @msg: pointer to the msg buffer
1393 * @msglen: msg length
1394 *
1395 * remove guest mac address filter
1396 **/
1397static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1398{
1399 struct i40e_virtchnl_ether_addr_list *al =
1400 (struct i40e_virtchnl_ether_addr_list *)msg;
1401 struct i40e_pf *pf = vf->pf;
1402 struct i40e_vsi *vsi = NULL;
1403 u16 vsi_id = al->vsi_id;
1404 i40e_status aq_ret = 0;
1405 int i;
1406
1407 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1408 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1409 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1410 aq_ret = I40E_ERR_PARAM;
1411 goto error_param;
1412 }
1413 vsi = pf->vsi[vsi_id];
1414
1415 /* delete addresses from the list */
1416 for (i = 0; i < al->num_elements; i++)
1417 i40e_del_filter(vsi, al->list[i].addr,
1418 I40E_VLAN_ANY, true, false);
1419
1420 /* program the updated filter list */
1421 if (i40e_sync_vsi_filters(vsi))
1422 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1423
1424error_param:
1425 /* send the response to the vf */
1426 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
1427 aq_ret);
1428}
1429
1430/**
1431 * i40e_vc_add_vlan_msg
1432 * @vf: pointer to the vf info
1433 * @msg: pointer to the msg buffer
1434 * @msglen: msg length
1435 *
1436 * program guest vlan id
1437 **/
1438static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1439{
1440 struct i40e_virtchnl_vlan_filter_list *vfl =
1441 (struct i40e_virtchnl_vlan_filter_list *)msg;
1442 struct i40e_pf *pf = vf->pf;
1443 struct i40e_vsi *vsi = NULL;
1444 u16 vsi_id = vfl->vsi_id;
1445 i40e_status aq_ret = 0;
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)) {
1451 aq_ret = I40E_ERR_PARAM;
1452 goto error_param;
1453 }
1454
1455 for (i = 0; i < vfl->num_elements; i++) {
1456 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1457 aq_ret = I40E_ERR_PARAM;
1458 dev_err(&pf->pdev->dev,
1459 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1460 goto error_param;
1461 }
1462 }
1463 vsi = pf->vsi[vsi_id];
1464 if (vsi->info.pvid) {
1465 aq_ret = I40E_ERR_PARAM;
1466 goto error_param;
1467 }
1468
1469 i40e_vlan_stripping_enable(vsi);
1470 for (i = 0; i < vfl->num_elements; i++) {
1471 /* add new VLAN filter */
1472 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1473 if (ret)
1474 dev_err(&pf->pdev->dev,
1475 "Unable to add VF vlan filter %d, error %d\n",
1476 vfl->vlan_id[i], ret);
1477 }
1478
1479error_param:
1480 /* send the response to the vf */
1481 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1482}
1483
1484/**
1485 * i40e_vc_remove_vlan_msg
1486 * @vf: pointer to the vf info
1487 * @msg: pointer to the msg buffer
1488 * @msglen: msg length
1489 *
1490 * remove programmed guest vlan id
1491 **/
1492static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1493{
1494 struct i40e_virtchnl_vlan_filter_list *vfl =
1495 (struct i40e_virtchnl_vlan_filter_list *)msg;
1496 struct i40e_pf *pf = vf->pf;
1497 struct i40e_vsi *vsi = NULL;
1498 u16 vsi_id = vfl->vsi_id;
1499 i40e_status aq_ret = 0;
1500 int i;
1501
1502 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1503 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1504 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1505 aq_ret = I40E_ERR_PARAM;
1506 goto error_param;
1507 }
1508
1509 for (i = 0; i < vfl->num_elements; i++) {
1510 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1511 aq_ret = I40E_ERR_PARAM;
1512 goto error_param;
1513 }
1514 }
1515
1516 vsi = pf->vsi[vsi_id];
1517 if (vsi->info.pvid) {
1518 aq_ret = I40E_ERR_PARAM;
1519 goto error_param;
1520 }
1521
1522 for (i = 0; i < vfl->num_elements; i++) {
1523 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1524 if (ret)
1525 dev_err(&pf->pdev->dev,
1526 "Unable to delete VF vlan filter %d, error %d\n",
1527 vfl->vlan_id[i], ret);
1528 }
1529
1530error_param:
1531 /* send the response to the vf */
1532 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1533}
1534
1535/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001536 * i40e_vc_validate_vf_msg
1537 * @vf: pointer to the vf info
1538 * @msg: pointer to the msg buffer
1539 * @msglen: msg length
1540 * @msghndl: msg handle
1541 *
1542 * validate msg
1543 **/
1544static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1545 u32 v_retval, u8 *msg, u16 msglen)
1546{
1547 bool err_msg_format = false;
1548 int valid_len;
1549
1550 /* Check if VF is disabled. */
1551 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1552 return I40E_ERR_PARAM;
1553
1554 /* Validate message length. */
1555 switch (v_opcode) {
1556 case I40E_VIRTCHNL_OP_VERSION:
1557 valid_len = sizeof(struct i40e_virtchnl_version_info);
1558 break;
1559 case I40E_VIRTCHNL_OP_RESET_VF:
1560 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1561 valid_len = 0;
1562 break;
1563 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1564 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1565 break;
1566 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1567 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1568 break;
1569 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1570 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1571 if (msglen >= valid_len) {
1572 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1573 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1574 valid_len += (vqc->num_queue_pairs *
1575 sizeof(struct
1576 i40e_virtchnl_queue_pair_info));
1577 if (vqc->num_queue_pairs == 0)
1578 err_msg_format = true;
1579 }
1580 break;
1581 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1582 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1583 if (msglen >= valid_len) {
1584 struct i40e_virtchnl_irq_map_info *vimi =
1585 (struct i40e_virtchnl_irq_map_info *)msg;
1586 valid_len += (vimi->num_vectors *
1587 sizeof(struct i40e_virtchnl_vector_map));
1588 if (vimi->num_vectors == 0)
1589 err_msg_format = true;
1590 }
1591 break;
1592 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1593 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1594 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1595 break;
1596 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1597 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1598 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1599 if (msglen >= valid_len) {
1600 struct i40e_virtchnl_ether_addr_list *veal =
1601 (struct i40e_virtchnl_ether_addr_list *)msg;
1602 valid_len += veal->num_elements *
1603 sizeof(struct i40e_virtchnl_ether_addr);
1604 if (veal->num_elements == 0)
1605 err_msg_format = true;
1606 }
1607 break;
1608 case I40E_VIRTCHNL_OP_ADD_VLAN:
1609 case I40E_VIRTCHNL_OP_DEL_VLAN:
1610 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1611 if (msglen >= valid_len) {
1612 struct i40e_virtchnl_vlan_filter_list *vfl =
1613 (struct i40e_virtchnl_vlan_filter_list *)msg;
1614 valid_len += vfl->num_elements * sizeof(u16);
1615 if (vfl->num_elements == 0)
1616 err_msg_format = true;
1617 }
1618 break;
1619 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1620 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1621 break;
1622 case I40E_VIRTCHNL_OP_GET_STATS:
1623 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1624 break;
1625 /* These are always errors coming from the VF. */
1626 case I40E_VIRTCHNL_OP_EVENT:
1627 case I40E_VIRTCHNL_OP_UNKNOWN:
1628 default:
1629 return -EPERM;
1630 break;
1631 }
1632 /* few more checks */
1633 if ((valid_len != msglen) || (err_msg_format)) {
1634 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1635 return -EINVAL;
1636 } else {
1637 return 0;
1638 }
1639}
1640
1641/**
1642 * i40e_vc_process_vf_msg
1643 * @pf: pointer to the pf structure
1644 * @vf_id: source vf id
1645 * @msg: pointer to the msg buffer
1646 * @msglen: msg length
1647 * @msghndl: msg handle
1648 *
1649 * called from the common aeq/arq handler to
1650 * process request from vf
1651 **/
1652int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1653 u32 v_retval, u8 *msg, u16 msglen)
1654{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001655 struct i40e_hw *hw = &pf->hw;
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001656 struct i40e_vf *vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001657 int ret;
1658
1659 pf->vf_aq_requests++;
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001660 if (vf_id >= pf->num_alloc_vfs)
1661 return -EINVAL;
1662 vf = &(pf->vf[vf_id]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001663 /* perform basic checks on the msg */
1664 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1665
1666 if (ret) {
Mitch Williams499ec802013-11-28 06:39:31 +00001667 dev_err(&pf->pdev->dev, "Invalid message from vf %d, opcode %d, len %d\n",
1668 vf_id, v_opcode, msglen);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001669 return ret;
1670 }
1671 wr32(hw, I40E_VFGEN_RSTAT1(vf_id), I40E_VFR_VFACTIVE);
1672 switch (v_opcode) {
1673 case I40E_VIRTCHNL_OP_VERSION:
1674 ret = i40e_vc_get_version_msg(vf);
1675 break;
1676 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1677 ret = i40e_vc_get_vf_resources_msg(vf);
1678 break;
1679 case I40E_VIRTCHNL_OP_RESET_VF:
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001680 i40e_vc_reset_vf_msg(vf);
1681 ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001682 break;
1683 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1684 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1685 break;
1686 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1687 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1688 break;
1689 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1690 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1691 break;
1692 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1693 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
1694 break;
1695 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1696 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1697 break;
1698 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1699 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1700 break;
1701 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1702 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1703 break;
1704 case I40E_VIRTCHNL_OP_ADD_VLAN:
1705 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1706 break;
1707 case I40E_VIRTCHNL_OP_DEL_VLAN:
1708 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1709 break;
1710 case I40E_VIRTCHNL_OP_GET_STATS:
1711 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1712 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001713 case I40E_VIRTCHNL_OP_UNKNOWN:
1714 default:
1715 dev_err(&pf->pdev->dev,
1716 "Unsupported opcode %d from vf %d\n", v_opcode, vf_id);
1717 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1718 I40E_ERR_NOT_IMPLEMENTED);
1719 break;
1720 }
1721
1722 return ret;
1723}
1724
1725/**
1726 * i40e_vc_process_vflr_event
1727 * @pf: pointer to the pf structure
1728 *
1729 * called from the vlfr irq handler to
1730 * free up vf resources and state variables
1731 **/
1732int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1733{
1734 u32 reg, reg_idx, bit_idx, vf_id;
1735 struct i40e_hw *hw = &pf->hw;
1736 struct i40e_vf *vf;
1737
1738 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1739 return 0;
1740
1741 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1742 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1743 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1744 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1745 /* read GLGEN_VFLRSTAT register to find out the flr vfs */
1746 vf = &pf->vf[vf_id];
1747 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
1748 if (reg & (1 << bit_idx)) {
1749 /* clear the bit in GLGEN_VFLRSTAT */
1750 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
1751
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001752 i40e_reset_vf(vf, true);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001753 }
1754 }
1755
1756 /* re-enable vflr interrupt cause */
1757 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1758 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1759 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1760 i40e_flush(hw);
1761
1762 return 0;
1763}
1764
1765/**
1766 * i40e_vc_vf_broadcast
1767 * @pf: pointer to the pf structure
1768 * @opcode: operation code
1769 * @retval: return value
1770 * @msg: pointer to the msg buffer
1771 * @msglen: msg length
1772 *
1773 * send a message to all VFs on a given PF
1774 **/
1775static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
1776 enum i40e_virtchnl_ops v_opcode,
1777 i40e_status v_retval, u8 *msg,
1778 u16 msglen)
1779{
1780 struct i40e_hw *hw = &pf->hw;
1781 struct i40e_vf *vf = pf->vf;
1782 int i;
1783
1784 for (i = 0; i < pf->num_alloc_vfs; i++) {
1785 /* Ignore return value on purpose - a given VF may fail, but
1786 * we need to keep going and send to all of them
1787 */
1788 i40e_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval,
1789 msg, msglen, NULL);
1790 vf++;
1791 }
1792}
1793
1794/**
1795 * i40e_vc_notify_link_state
1796 * @pf: pointer to the pf structure
1797 *
1798 * send a link status message to all VFs on a given PF
1799 **/
1800void i40e_vc_notify_link_state(struct i40e_pf *pf)
1801{
1802 struct i40e_virtchnl_pf_event pfe;
1803
1804 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
1805 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
1806 pfe.event_data.link_event.link_status =
1807 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
1808 pfe.event_data.link_event.link_speed = pf->hw.phy.link_info.link_speed;
1809
1810 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
1811 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
1812}
1813
1814/**
1815 * i40e_vc_notify_reset
1816 * @pf: pointer to the pf structure
1817 *
1818 * indicate a pending reset to all VFs on a given PF
1819 **/
1820void i40e_vc_notify_reset(struct i40e_pf *pf)
1821{
1822 struct i40e_virtchnl_pf_event pfe;
1823
1824 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1825 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
1826 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
1827 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
1828}
1829
1830/**
1831 * i40e_vc_notify_vf_reset
1832 * @vf: pointer to the vf structure
1833 *
1834 * indicate a pending reset to the given VF
1835 **/
1836void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
1837{
1838 struct i40e_virtchnl_pf_event pfe;
1839
1840 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1841 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
1842 i40e_aq_send_msg_to_vf(&vf->pf->hw, vf->vf_id, I40E_VIRTCHNL_OP_EVENT,
1843 I40E_SUCCESS, (u8 *)&pfe,
1844 sizeof(struct i40e_virtchnl_pf_event), NULL);
1845}
1846
1847/**
1848 * i40e_ndo_set_vf_mac
1849 * @netdev: network interface device structure
1850 * @vf_id: vf identifier
1851 * @mac: mac address
1852 *
1853 * program vf mac address
1854 **/
1855int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
1856{
1857 struct i40e_netdev_priv *np = netdev_priv(netdev);
1858 struct i40e_vsi *vsi = np->vsi;
1859 struct i40e_pf *pf = vsi->back;
1860 struct i40e_mac_filter *f;
1861 struct i40e_vf *vf;
1862 int ret = 0;
1863
1864 /* validate the request */
1865 if (vf_id >= pf->num_alloc_vfs) {
1866 dev_err(&pf->pdev->dev,
1867 "Invalid VF Identifier %d\n", vf_id);
1868 ret = -EINVAL;
1869 goto error_param;
1870 }
1871
1872 vf = &(pf->vf[vf_id]);
1873 vsi = pf->vsi[vf->lan_vsi_index];
1874 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1875 dev_err(&pf->pdev->dev,
1876 "Uninitialized VF %d\n", vf_id);
1877 ret = -EINVAL;
1878 goto error_param;
1879 }
1880
1881 if (!is_valid_ether_addr(mac)) {
1882 dev_err(&pf->pdev->dev,
1883 "Invalid VF ethernet address\n");
1884 ret = -EINVAL;
1885 goto error_param;
1886 }
1887
1888 /* delete the temporary mac address */
1889 i40e_del_filter(vsi, vf->default_lan_addr.addr, 0, true, false);
1890
1891 /* add the new mac address */
1892 f = i40e_add_filter(vsi, mac, 0, true, false);
1893 if (!f) {
1894 dev_err(&pf->pdev->dev,
1895 "Unable to add VF ucast filter\n");
1896 ret = -ENOMEM;
1897 goto error_param;
1898 }
1899
1900 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
1901 /* program mac filter */
1902 if (i40e_sync_vsi_filters(vsi)) {
1903 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
1904 ret = -EIO;
1905 goto error_param;
1906 }
1907 memcpy(vf->default_lan_addr.addr, mac, ETH_ALEN);
1908 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
1909 ret = 0;
1910
1911error_param:
1912 return ret;
1913}
1914
1915/**
1916 * i40e_ndo_set_vf_port_vlan
1917 * @netdev: network interface device structure
1918 * @vf_id: vf identifier
1919 * @vlan_id: mac address
1920 * @qos: priority setting
1921 *
1922 * program vf vlan id and/or qos
1923 **/
1924int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
1925 int vf_id, u16 vlan_id, u8 qos)
1926{
1927 struct i40e_netdev_priv *np = netdev_priv(netdev);
1928 struct i40e_pf *pf = np->vsi->back;
1929 struct i40e_vsi *vsi;
1930 struct i40e_vf *vf;
1931 int ret = 0;
1932
1933 /* validate the request */
1934 if (vf_id >= pf->num_alloc_vfs) {
1935 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
1936 ret = -EINVAL;
1937 goto error_pvid;
1938 }
1939
1940 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
1941 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
1942 ret = -EINVAL;
1943 goto error_pvid;
1944 }
1945
1946 vf = &(pf->vf[vf_id]);
1947 vsi = pf->vsi[vf->lan_vsi_index];
1948 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1949 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
1950 ret = -EINVAL;
1951 goto error_pvid;
1952 }
1953
1954 if (vsi->info.pvid) {
1955 /* kill old VLAN */
1956 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
1957 VLAN_VID_MASK));
1958 if (ret) {
1959 dev_info(&vsi->back->pdev->dev,
1960 "remove VLAN failed, ret=%d, aq_err=%d\n",
1961 ret, pf->hw.aq.asq_last_status);
1962 }
1963 }
1964 if (vlan_id || qos)
1965 ret = i40e_vsi_add_pvid(vsi,
1966 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
1967 else
Greg Rose6c12fcb2013-11-28 06:39:34 +00001968 i40e_vsi_remove_pvid(vsi);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001969
1970 if (vlan_id) {
1971 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
1972 vlan_id, qos, vf_id);
1973
1974 /* add new VLAN filter */
1975 ret = i40e_vsi_add_vlan(vsi, vlan_id);
1976 if (ret) {
1977 dev_info(&vsi->back->pdev->dev,
1978 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
1979 vsi->back->hw.aq.asq_last_status);
1980 goto error_pvid;
1981 }
1982 }
1983
1984 if (ret) {
1985 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
1986 goto error_pvid;
1987 }
Greg Rose6c12fcb2013-11-28 06:39:34 +00001988 /* The Port VLAN needs to be saved across resets the same as the
1989 * default LAN MAC address.
1990 */
1991 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001992 ret = 0;
1993
1994error_pvid:
1995 return ret;
1996}
1997
1998/**
1999 * i40e_ndo_set_vf_bw
2000 * @netdev: network interface device structure
2001 * @vf_id: vf identifier
2002 * @tx_rate: tx rate
2003 *
2004 * configure vf tx rate
2005 **/
2006int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int tx_rate)
2007{
2008 return -EOPNOTSUPP;
2009}
2010
2011/**
2012 * i40e_ndo_get_vf_config
2013 * @netdev: network interface device structure
2014 * @vf_id: vf identifier
2015 * @ivi: vf configuration structure
2016 *
2017 * return vf configuration
2018 **/
2019int i40e_ndo_get_vf_config(struct net_device *netdev,
2020 int vf_id, struct ifla_vf_info *ivi)
2021{
2022 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002023 struct i40e_vsi *vsi = np->vsi;
2024 struct i40e_pf *pf = vsi->back;
2025 struct i40e_vf *vf;
2026 int ret = 0;
2027
2028 /* validate the request */
2029 if (vf_id >= pf->num_alloc_vfs) {
2030 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2031 ret = -EINVAL;
2032 goto error_param;
2033 }
2034
2035 vf = &(pf->vf[vf_id]);
2036 /* first vsi is always the LAN vsi */
2037 vsi = pf->vsi[vf->lan_vsi_index];
2038 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2039 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2040 ret = -EINVAL;
2041 goto error_param;
2042 }
2043
2044 ivi->vf = vf_id;
2045
Mitch Williamsf4a1c5c2013-11-28 06:39:34 +00002046 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002047
2048 ivi->tx_rate = 0;
2049 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2050 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2051 I40E_VLAN_PRIORITY_SHIFT;
2052 ret = 0;
2053
2054error_param:
2055 return ret;
2056}