blob: 504bdf5939b51dfc4948cbd9be08ff82cf350021 [file] [log] [blame]
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Greg Rosedc641b72013-12-18 13:45:51 +00004 * Copyright(c) 2013 - 2014 Intel Corporation.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00005 *
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 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * 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/>.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000017 *
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/**
Greg Rosef9b4b622014-03-06 09:02:28 +000032 * i40e_vc_disable_vf
33 * @pf: pointer to the pf info
34 * @vf: pointer to the vf info
35 *
36 * Disable the VF through a SW reset
37 **/
38static inline void i40e_vc_disable_vf(struct i40e_pf *pf, struct i40e_vf *vf)
39{
40 struct i40e_hw *hw = &pf->hw;
41 u32 reg;
42
43 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
44 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
45 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
46 i40e_flush(hw);
47}
48
49/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000050 * i40e_vc_isvalid_vsi_id
51 * @vf: pointer to the vf info
52 * @vsi_id: vf relative vsi id
53 *
54 * check for the valid vsi id
55 **/
56static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u8 vsi_id)
57{
58 struct i40e_pf *pf = vf->pf;
59
60 return pf->vsi[vsi_id]->vf_id == vf->vf_id;
61}
62
63/**
64 * i40e_vc_isvalid_queue_id
65 * @vf: pointer to the vf info
66 * @vsi_id: vsi id
67 * @qid: vsi relative queue id
68 *
69 * check for the valid queue id
70 **/
71static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u8 vsi_id,
72 u8 qid)
73{
74 struct i40e_pf *pf = vf->pf;
75
76 return qid < pf->vsi[vsi_id]->num_queue_pairs;
77}
78
79/**
80 * i40e_vc_isvalid_vector_id
81 * @vf: pointer to the vf info
82 * @vector_id: vf relative vector id
83 *
84 * check for the valid vector id
85 **/
86static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
87{
88 struct i40e_pf *pf = vf->pf;
89
Mitch Williams9347eb72014-02-11 08:26:32 +000090 return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000091}
92
93/***********************vf resource mgmt routines*****************/
94
95/**
96 * i40e_vc_get_pf_queue_id
97 * @vf: pointer to the vf info
98 * @vsi_idx: index of VSI in PF struct
99 * @vsi_queue_id: vsi relative queue id
100 *
101 * return pf relative queue id
102 **/
103static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u8 vsi_idx,
104 u8 vsi_queue_id)
105{
106 struct i40e_pf *pf = vf->pf;
107 struct i40e_vsi *vsi = pf->vsi[vsi_idx];
108 u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
109
110 if (le16_to_cpu(vsi->info.mapping_flags) &
111 I40E_AQ_VSI_QUE_MAP_NONCONTIG)
112 pf_queue_id =
113 le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
114 else
115 pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
116 vsi_queue_id;
117
118 return pf_queue_id;
119}
120
121/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000122 * i40e_config_irq_link_list
123 * @vf: pointer to the vf info
124 * @vsi_idx: index of VSI in PF struct
125 * @vecmap: irq map info
126 *
127 * configure irq link list from the map
128 **/
129static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_idx,
130 struct i40e_virtchnl_vector_map *vecmap)
131{
132 unsigned long linklistmap = 0, tempmap;
133 struct i40e_pf *pf = vf->pf;
134 struct i40e_hw *hw = &pf->hw;
135 u16 vsi_queue_id, pf_queue_id;
136 enum i40e_queue_type qtype;
137 u16 next_q, vector_id;
138 u32 reg, reg_idx;
139 u16 itr_idx = 0;
140
141 vector_id = vecmap->vector_id;
142 /* setup the head */
143 if (0 == vector_id)
144 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
145 else
146 reg_idx = I40E_VPINT_LNKLSTN(
Mitch Williams9347eb72014-02-11 08:26:32 +0000147 ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
148 (vector_id - 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000149
150 if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
151 /* Special case - No queues mapped on this vector */
152 wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
153 goto irq_list_done;
154 }
155 tempmap = vecmap->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000156 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000157 linklistmap |= (1 <<
158 (I40E_VIRTCHNL_SUPPORTED_QTYPES *
159 vsi_queue_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000160 }
161
162 tempmap = vecmap->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000163 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000164 linklistmap |= (1 <<
165 (I40E_VIRTCHNL_SUPPORTED_QTYPES * vsi_queue_id
166 + 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000167 }
168
169 next_q = find_first_bit(&linklistmap,
170 (I40E_MAX_VSI_QP *
171 I40E_VIRTCHNL_SUPPORTED_QTYPES));
172 vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
173 qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
174 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx, vsi_queue_id);
175 reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
176
177 wr32(hw, reg_idx, reg);
178
179 while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
180 switch (qtype) {
181 case I40E_QUEUE_TYPE_RX:
182 reg_idx = I40E_QINT_RQCTL(pf_queue_id);
183 itr_idx = vecmap->rxitr_idx;
184 break;
185 case I40E_QUEUE_TYPE_TX:
186 reg_idx = I40E_QINT_TQCTL(pf_queue_id);
187 itr_idx = vecmap->txitr_idx;
188 break;
189 default:
190 break;
191 }
192
193 next_q = find_next_bit(&linklistmap,
194 (I40E_MAX_VSI_QP *
195 I40E_VIRTCHNL_SUPPORTED_QTYPES),
196 next_q + 1);
Mitch Williams829af3a2013-12-18 13:46:00 +0000197 if (next_q <
198 (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000199 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
200 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
201 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx,
202 vsi_queue_id);
203 } else {
204 pf_queue_id = I40E_QUEUE_END_OF_LIST;
205 qtype = 0;
206 }
207
208 /* format for the RQCTL & TQCTL regs is same */
209 reg = (vector_id) |
210 (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
211 (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
212 (1 << I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
213 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
214 wr32(hw, reg_idx, reg);
215 }
216
217irq_list_done:
218 i40e_flush(hw);
219}
220
221/**
222 * i40e_config_vsi_tx_queue
223 * @vf: pointer to the vf info
224 * @vsi_idx: index of VSI in PF struct
225 * @vsi_queue_id: vsi relative queue index
226 * @info: config. info
227 *
228 * configure tx queue
229 **/
230static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_idx,
231 u16 vsi_queue_id,
232 struct i40e_virtchnl_txq_info *info)
233{
234 struct i40e_pf *pf = vf->pf;
235 struct i40e_hw *hw = &pf->hw;
236 struct i40e_hmc_obj_txq tx_ctx;
237 u16 pf_queue_id;
238 u32 qtx_ctl;
239 int ret = 0;
240
241 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx, vsi_queue_id);
242
243 /* clear the context structure first */
244 memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
245
246 /* only set the required fields */
247 tx_ctx.base = info->dma_ring_addr / 128;
248 tx_ctx.qlen = info->ring_len;
249 tx_ctx.rdylist = le16_to_cpu(pf->vsi[vsi_idx]->info.qs_handle[0]);
250 tx_ctx.rdylist_act = 0;
Ashish Shah5d298962014-05-22 06:31:25 +0000251 tx_ctx.head_wb_ena = info->headwb_enabled;
252 tx_ctx.head_wb_addr = info->dma_headwb_addr;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000253
254 /* clear the context in the HMC */
255 ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
256 if (ret) {
257 dev_err(&pf->pdev->dev,
258 "Failed to clear VF LAN Tx queue context %d, error: %d\n",
259 pf_queue_id, ret);
260 ret = -ENOENT;
261 goto error_context;
262 }
263
264 /* set the context in the HMC */
265 ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
266 if (ret) {
267 dev_err(&pf->pdev->dev,
268 "Failed to set VF LAN Tx queue context %d error: %d\n",
269 pf_queue_id, ret);
270 ret = -ENOENT;
271 goto error_context;
272 }
273
274 /* associate this queue with the PCI VF function */
275 qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +0000276 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000277 & I40E_QTX_CTL_PF_INDX_MASK);
278 qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
279 << I40E_QTX_CTL_VFVM_INDX_SHIFT)
280 & I40E_QTX_CTL_VFVM_INDX_MASK);
281 wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
282 i40e_flush(hw);
283
284error_context:
285 return ret;
286}
287
288/**
289 * i40e_config_vsi_rx_queue
290 * @vf: pointer to the vf info
291 * @vsi_idx: index of VSI in PF struct
292 * @vsi_queue_id: vsi relative queue index
293 * @info: config. info
294 *
295 * configure rx queue
296 **/
297static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_idx,
298 u16 vsi_queue_id,
299 struct i40e_virtchnl_rxq_info *info)
300{
301 struct i40e_pf *pf = vf->pf;
302 struct i40e_hw *hw = &pf->hw;
303 struct i40e_hmc_obj_rxq rx_ctx;
304 u16 pf_queue_id;
305 int ret = 0;
306
307 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx, vsi_queue_id);
308
309 /* clear the context structure first */
310 memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
311
312 /* only set the required fields */
313 rx_ctx.base = info->dma_ring_addr / 128;
314 rx_ctx.qlen = info->ring_len;
315
316 if (info->splithdr_enabled) {
317 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
318 I40E_RX_SPLIT_IP |
319 I40E_RX_SPLIT_TCP_UDP |
320 I40E_RX_SPLIT_SCTP;
321 /* header length validation */
322 if (info->hdr_size > ((2 * 1024) - 64)) {
323 ret = -EINVAL;
324 goto error_param;
325 }
326 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
327
328 /* set splitalways mode 10b */
329 rx_ctx.dtype = 0x2;
330 }
331
332 /* databuffer length validation */
333 if (info->databuffer_size > ((16 * 1024) - 128)) {
334 ret = -EINVAL;
335 goto error_param;
336 }
337 rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
338
339 /* max pkt. length validation */
340 if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
341 ret = -EINVAL;
342 goto error_param;
343 }
344 rx_ctx.rxmax = info->max_pkt_size;
345
346 /* enable 32bytes desc always */
347 rx_ctx.dsize = 1;
348
349 /* default values */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000350 rx_ctx.lrxqthresh = 2;
351 rx_ctx.crcstrip = 1;
Mitch Williams50d41652014-04-09 05:58:55 +0000352 rx_ctx.prefena = 1;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000353
354 /* clear the context in the HMC */
355 ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
356 if (ret) {
357 dev_err(&pf->pdev->dev,
358 "Failed to clear VF LAN Rx queue context %d, error: %d\n",
359 pf_queue_id, ret);
360 ret = -ENOENT;
361 goto error_param;
362 }
363
364 /* set the context in the HMC */
365 ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
366 if (ret) {
367 dev_err(&pf->pdev->dev,
368 "Failed to set VF LAN Rx queue context %d error: %d\n",
369 pf_queue_id, ret);
370 ret = -ENOENT;
371 goto error_param;
372 }
373
374error_param:
375 return ret;
376}
377
378/**
379 * i40e_alloc_vsi_res
380 * @vf: pointer to the vf info
381 * @type: type of VSI to allocate
382 *
383 * alloc vf vsi context & resources
384 **/
385static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
386{
387 struct i40e_mac_filter *f = NULL;
388 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000389 struct i40e_vsi *vsi;
390 int ret = 0;
391
392 vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
393
394 if (!vsi) {
395 dev_err(&pf->pdev->dev,
396 "add vsi failed for vf %d, aq_err %d\n",
397 vf->vf_id, pf->hw.aq.asq_last_status);
398 ret = -ENOENT;
399 goto error_alloc_vsi_res;
400 }
401 if (type == I40E_VSI_SRIOV) {
Greg Rose1a103702013-11-28 06:42:39 +0000402 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000403 vf->lan_vsi_index = vsi->idx;
404 vf->lan_vsi_id = vsi->id;
405 dev_info(&pf->pdev->dev,
Mitch Williamsd4194992014-01-14 00:49:51 -0800406 "VF %d assigned LAN VSI index %d, VSI id %d\n",
407 vf->vf_id, vsi->idx, vsi->id);
Greg Rose6c12fcb2013-11-28 06:39:34 +0000408 /* If the port VLAN has been configured and then the
409 * VF driver was removed then the VSI port VLAN
410 * configuration was destroyed. Check if there is
411 * a port VLAN and restore the VSI configuration if
412 * needed.
413 */
414 if (vf->port_vlan_id)
415 i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000416 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
Greg Rose6c12fcb2013-11-28 06:39:34 +0000417 vf->port_vlan_id, true, false);
Greg Rose1a103702013-11-28 06:42:39 +0000418 if (!f)
419 dev_info(&pf->pdev->dev,
420 "Could not allocate VF MAC addr\n");
421 f = i40e_add_filter(vsi, brdcast, vf->port_vlan_id,
422 true, false);
423 if (!f)
424 dev_info(&pf->pdev->dev,
425 "Could not allocate VF broadcast filter\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000426 }
Neerav Parikh6dbbbfb2013-11-26 10:49:24 +0000427
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000428 /* program mac filter */
429 ret = i40e_sync_vsi_filters(vsi);
Mitch Williamsfd1646e2014-02-13 03:48:44 -0800430 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000431 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000432
Mitch Williams6b192892014-03-06 09:02:29 +0000433 /* Set VF bandwidth if specified */
434 if (vf->tx_rate) {
435 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
436 vf->tx_rate / 50, 0, NULL);
437 if (ret)
438 dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
439 vf->vf_id, ret);
440 }
441
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000442error_alloc_vsi_res:
443 return ret;
444}
445
446/**
Mitch Williams805bd5b2013-11-28 06:39:26 +0000447 * i40e_enable_vf_mappings
448 * @vf: pointer to the vf info
449 *
450 * enable vf mappings
451 **/
452static void i40e_enable_vf_mappings(struct i40e_vf *vf)
453{
454 struct i40e_pf *pf = vf->pf;
455 struct i40e_hw *hw = &pf->hw;
456 u32 reg, total_queue_pairs = 0;
457 int j;
458
459 /* Tell the hardware we're using noncontiguous mapping. HW requires
460 * that VF queues be mapped using this method, even when they are
461 * contiguous in real life
462 */
463 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
464 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
465
466 /* enable VF vplan_qtable mappings */
467 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
468 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
469
470 /* map PF queues to VF queues */
471 for (j = 0; j < pf->vsi[vf->lan_vsi_index]->num_queue_pairs; j++) {
472 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index, j);
473 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
474 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
475 total_queue_pairs++;
476 }
477
478 /* map PF queues to VSI */
479 for (j = 0; j < 7; j++) {
480 if (j * 2 >= pf->vsi[vf->lan_vsi_index]->num_queue_pairs) {
481 reg = 0x07FF07FF; /* unused */
482 } else {
483 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index,
484 j * 2);
485 reg = qid;
486 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index,
487 (j * 2) + 1);
488 reg |= qid << 16;
489 }
490 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
491 }
492
493 i40e_flush(hw);
494}
495
496/**
497 * i40e_disable_vf_mappings
498 * @vf: pointer to the vf info
499 *
500 * disable vf mappings
501 **/
502static void i40e_disable_vf_mappings(struct i40e_vf *vf)
503{
504 struct i40e_pf *pf = vf->pf;
505 struct i40e_hw *hw = &pf->hw;
506 int i;
507
508 /* disable qp mappings */
509 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
510 for (i = 0; i < I40E_MAX_VSI_QP; i++)
511 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
512 I40E_QUEUE_END_OF_LIST);
513 i40e_flush(hw);
514}
515
516/**
517 * i40e_free_vf_res
518 * @vf: pointer to the vf info
519 *
520 * free vf resources
521 **/
522static void i40e_free_vf_res(struct i40e_vf *vf)
523{
524 struct i40e_pf *pf = vf->pf;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000525 struct i40e_hw *hw = &pf->hw;
526 u32 reg_idx, reg;
527 int i, msix_vf;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000528
529 /* free vsi & disconnect it from the parent uplink */
530 if (vf->lan_vsi_index) {
531 i40e_vsi_release(pf->vsi[vf->lan_vsi_index]);
532 vf->lan_vsi_index = 0;
533 vf->lan_vsi_id = 0;
534 }
Mitch Williams9347eb72014-02-11 08:26:32 +0000535 msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
536
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000537 /* disable interrupts so the VF starts in a known state */
538 for (i = 0; i < msix_vf; i++) {
539 /* format is same for both registers */
540 if (0 == i)
541 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
542 else
543 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
544 (vf->vf_id))
545 + (i - 1));
546 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
547 i40e_flush(hw);
548 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000549
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000550 /* clear the irq settings */
551 for (i = 0; i < msix_vf; i++) {
552 /* format is same for both registers */
553 if (0 == i)
554 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
555 else
556 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
557 (vf->vf_id))
558 + (i - 1));
559 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
560 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
561 wr32(hw, reg_idx, reg);
562 i40e_flush(hw);
563 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000564 /* reset some of the state varibles keeping
565 * track of the resources
566 */
567 vf->num_queue_pairs = 0;
568 vf->vf_states = 0;
569}
570
571/**
572 * i40e_alloc_vf_res
573 * @vf: pointer to the vf info
574 *
575 * allocate vf resources
576 **/
577static int i40e_alloc_vf_res(struct i40e_vf *vf)
578{
579 struct i40e_pf *pf = vf->pf;
580 int total_queue_pairs = 0;
581 int ret;
582
583 /* allocate hw vsi context & associated resources */
584 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
585 if (ret)
586 goto error_alloc;
587 total_queue_pairs += pf->vsi[vf->lan_vsi_index]->num_queue_pairs;
588 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
589
590 /* store the total qps number for the runtime
591 * vf req validation
592 */
593 vf->num_queue_pairs = total_queue_pairs;
594
595 /* vf is now completely initialized */
596 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
597
598error_alloc:
599 if (ret)
600 i40e_free_vf_res(vf);
601
602 return ret;
603}
604
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000605#define VF_DEVICE_STATUS 0xAA
606#define VF_TRANS_PENDING_MASK 0x20
607/**
608 * i40e_quiesce_vf_pci
609 * @vf: pointer to the vf structure
610 *
611 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
612 * if the transactions never clear.
613 **/
614static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
615{
616 struct i40e_pf *pf = vf->pf;
617 struct i40e_hw *hw = &pf->hw;
618 int vf_abs_id, i;
619 u32 reg;
620
Mitch Williamsb141d612013-11-28 06:39:36 +0000621 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000622
623 wr32(hw, I40E_PF_PCI_CIAA,
624 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
625 for (i = 0; i < 100; i++) {
626 reg = rd32(hw, I40E_PF_PCI_CIAD);
627 if ((reg & VF_TRANS_PENDING_MASK) == 0)
628 return 0;
629 udelay(1);
630 }
631 return -EIO;
632}
633
Mitch Williams805bd5b2013-11-28 06:39:26 +0000634/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000635 * i40e_reset_vf
636 * @vf: pointer to the vf structure
637 * @flr: VFLR was issued or not
638 *
639 * reset the vf
640 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000641void i40e_reset_vf(struct i40e_vf *vf, bool flr)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000642{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000643 struct i40e_pf *pf = vf->pf;
644 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000645 bool rsd = false;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000646 int i;
647 u32 reg;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000648
649 /* warn the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000650 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
651
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000652 /* In the case of a VFLR, the HW has already reset the VF and we
653 * just need to clean up, so don't hit the VFRTRIG register.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000654 */
655 if (!flr) {
656 /* reset vf using VPGEN_VFRTRIG reg */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000657 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
658 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000659 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
660 i40e_flush(hw);
661 }
662
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000663 if (i40e_quiesce_vf_pci(vf))
664 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
665 vf->vf_id);
666
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000667 /* poll VPGEN_VFRSTAT reg to make sure
668 * that reset is complete
669 */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000670 for (i = 0; i < 100; i++) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000671 /* vf reset requires driver to first reset the
672 * vf & than poll the status register to make sure
673 * that the requested op was completed
674 * successfully
675 */
676 udelay(10);
677 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
678 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
679 rsd = true;
680 break;
681 }
682 }
683
684 if (!rsd)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000685 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000686 vf->vf_id);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000687 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000688 /* clear the reset bit in the VPGEN_VFRTRIG reg */
689 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
690 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
691 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000692
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000693 /* On initial reset, we won't have any queues */
694 if (vf->lan_vsi_index == 0)
695 goto complete_reset;
696
697 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_index], false);
698complete_reset:
699 /* reallocate vf resources to reset the VSI state */
700 i40e_free_vf_res(vf);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000701 i40e_alloc_vf_res(vf);
702 i40e_enable_vf_mappings(vf);
Mitch Williamsc17b3622014-02-13 03:48:45 -0800703 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000704
705 /* tell the VF the reset is done */
706 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
707 i40e_flush(hw);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000708}
709
710/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000711 * i40e_vfs_are_assigned
712 * @pf: pointer to the pf structure
713 *
714 * Determine if any VFs are assigned to VMs
715 **/
716static bool i40e_vfs_are_assigned(struct i40e_pf *pf)
717{
718 struct pci_dev *pdev = pf->pdev;
719 struct pci_dev *vfdev;
720
721 /* loop through all the VFs to see if we own any that are assigned */
Shannon Nelsonab600852014-01-17 15:36:39 -0800722 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, I40E_DEV_ID_VF , NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000723 while (vfdev) {
724 /* if we don't own it we don't care */
725 if (vfdev->is_virtfn && pci_physfn(vfdev) == pdev) {
726 /* if it is assigned we cannot release it */
727 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
728 return true;
729 }
730
731 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL,
Shannon Nelsonab600852014-01-17 15:36:39 -0800732 I40E_DEV_ID_VF,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000733 vfdev);
734 }
735
736 return false;
737}
Greg Rosec3542292013-12-13 08:38:38 +0000738#ifdef CONFIG_PCI_IOV
739
740/**
741 * i40e_enable_pf_switch_lb
742 * @pf: pointer to the pf structure
743 *
744 * enable switch loop back or die - no point in a return value
745 **/
746static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
747{
748 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
749 struct i40e_vsi_context ctxt;
750 int aq_ret;
751
752 ctxt.seid = pf->main_vsi_seid;
753 ctxt.pf_num = pf->hw.pf_id;
754 ctxt.vf_num = 0;
755 aq_ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
756 if (aq_ret) {
757 dev_info(&pf->pdev->dev,
758 "%s couldn't get pf vsi config, err %d, aq_err %d\n",
759 __func__, aq_ret, pf->hw.aq.asq_last_status);
760 return;
761 }
762 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
763 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
764 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
765
766 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
767 if (aq_ret) {
768 dev_info(&pf->pdev->dev,
769 "%s: update vsi switch failed, aq_err=%d\n",
770 __func__, vsi->back->hw.aq.asq_last_status);
771 }
772}
773#endif
774
775/**
776 * i40e_disable_pf_switch_lb
777 * @pf: pointer to the pf structure
778 *
779 * disable switch loop back or die - no point in a return value
780 **/
781static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
782{
783 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
784 struct i40e_vsi_context ctxt;
785 int aq_ret;
786
787 ctxt.seid = pf->main_vsi_seid;
788 ctxt.pf_num = pf->hw.pf_id;
789 ctxt.vf_num = 0;
790 aq_ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
791 if (aq_ret) {
792 dev_info(&pf->pdev->dev,
793 "%s couldn't get pf vsi config, err %d, aq_err %d\n",
794 __func__, aq_ret, pf->hw.aq.asq_last_status);
795 return;
796 }
797 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
798 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
799 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
800
801 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
802 if (aq_ret) {
803 dev_info(&pf->pdev->dev,
804 "%s: update vsi switch failed, aq_err=%d\n",
805 __func__, vsi->back->hw.aq.asq_last_status);
806 }
807}
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000808
809/**
810 * i40e_free_vfs
811 * @pf: pointer to the pf structure
812 *
813 * free vf resources
814 **/
815void i40e_free_vfs(struct i40e_pf *pf)
816{
Mitch Williamsf7414532013-11-28 06:39:40 +0000817 struct i40e_hw *hw = &pf->hw;
818 u32 reg_idx, bit_idx;
819 int i, tmp, vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000820
821 if (!pf->vf)
822 return;
823
824 /* Disable interrupt 0 so we don't try to handle the VFLR. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000825 i40e_irq_dynamic_disable_icr0(pf);
826
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000827 mdelay(10); /* let any messages in transit get finished up */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000828 /* free up vf resources */
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000829 tmp = pf->num_alloc_vfs;
830 pf->num_alloc_vfs = 0;
831 for (i = 0; i < tmp; i++) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000832 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
833 i40e_free_vf_res(&pf->vf[i]);
834 /* disable qp mappings */
835 i40e_disable_vf_mappings(&pf->vf[i]);
836 }
837
838 kfree(pf->vf);
839 pf->vf = NULL;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000840
Mitch Williams9e5634d2014-04-04 04:43:14 +0000841 /* This check is for when the driver is unloaded while VFs are
842 * assigned. Setting the number of VFs to 0 through sysfs is caught
843 * before this function ever gets called.
844 */
Mitch Williamsf7414532013-11-28 06:39:40 +0000845 if (!i40e_vfs_are_assigned(pf)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000846 pci_disable_sriov(pf->pdev);
Mitch Williamsf7414532013-11-28 06:39:40 +0000847 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
848 * work correctly when SR-IOV gets re-enabled.
849 */
850 for (vf_id = 0; vf_id < tmp; vf_id++) {
851 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
852 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
853 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
854 }
Greg Rosec3542292013-12-13 08:38:38 +0000855 i40e_disable_pf_switch_lb(pf);
856 } else {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000857 dev_warn(&pf->pdev->dev,
858 "unable to disable SR-IOV because VFs are assigned.\n");
Greg Rosec3542292013-12-13 08:38:38 +0000859 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000860
861 /* Re-enable interrupt 0. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000862 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000863}
864
865#ifdef CONFIG_PCI_IOV
866/**
867 * i40e_alloc_vfs
868 * @pf: pointer to the pf structure
869 * @num_alloc_vfs: number of vfs to allocate
870 *
871 * allocate vf resources
872 **/
Mitch Williams4aeec012014-02-13 03:48:47 -0800873int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000874{
875 struct i40e_vf *vfs;
876 int i, ret = 0;
877
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000878 /* Disable interrupt 0 so we don't try to handle the VFLR. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000879 i40e_irq_dynamic_disable_icr0(pf);
880
Mitch Williams4aeec012014-02-13 03:48:47 -0800881 /* Check to see if we're just allocating resources for extant VFs */
882 if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
883 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
884 if (ret) {
885 dev_err(&pf->pdev->dev,
886 "Failed to enable SR-IOV, error %d.\n", ret);
887 pf->num_alloc_vfs = 0;
888 goto err_iov;
889 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000890 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000891 /* allocate memory */
Akeem G Abodunrincc6456a2014-02-06 05:51:02 +0000892 vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000893 if (!vfs) {
894 ret = -ENOMEM;
895 goto err_alloc;
896 }
Mitch Williamsc674d122014-05-20 08:01:40 +0000897 pf->vf = vfs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000898
899 /* apply default profile */
900 for (i = 0; i < num_alloc_vfs; i++) {
901 vfs[i].pf = pf;
902 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
903 vfs[i].vf_id = i;
904
905 /* assign default capabilities */
906 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
Mitch Williamsc674d122014-05-20 08:01:40 +0000907 vfs[i].spoofchk = true;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000908 /* vf resources get allocated during reset */
909 i40e_reset_vf(&vfs[i], false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000910
911 /* enable vf vplan_qtable mappings */
912 i40e_enable_vf_mappings(&vfs[i]);
913 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000914 pf->num_alloc_vfs = num_alloc_vfs;
915
Greg Rosec3542292013-12-13 08:38:38 +0000916 i40e_enable_pf_switch_lb(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000917err_alloc:
918 if (ret)
919 i40e_free_vfs(pf);
920err_iov:
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000921 /* Re-enable interrupt 0. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000922 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000923 return ret;
924}
925
926#endif
927/**
928 * i40e_pci_sriov_enable
929 * @pdev: pointer to a pci_dev structure
930 * @num_vfs: number of vfs to allocate
931 *
932 * Enable or change the number of VFs
933 **/
934static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
935{
936#ifdef CONFIG_PCI_IOV
937 struct i40e_pf *pf = pci_get_drvdata(pdev);
938 int pre_existing_vfs = pci_num_vf(pdev);
939 int err = 0;
940
941 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
942 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
943 i40e_free_vfs(pf);
944 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
945 goto out;
946
947 if (num_vfs > pf->num_req_vfs) {
948 err = -EPERM;
949 goto err_out;
950 }
951
952 err = i40e_alloc_vfs(pf, num_vfs);
953 if (err) {
954 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
955 goto err_out;
956 }
957
958out:
959 return num_vfs;
960
961err_out:
962 return err;
963#endif
964 return 0;
965}
966
967/**
968 * i40e_pci_sriov_configure
969 * @pdev: pointer to a pci_dev structure
970 * @num_vfs: number of vfs to allocate
971 *
972 * Enable or change the number of VFs. Called when the user updates the number
973 * of VFs in sysfs.
974 **/
975int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
976{
977 struct i40e_pf *pf = pci_get_drvdata(pdev);
978
979 if (num_vfs)
980 return i40e_pci_sriov_enable(pdev, num_vfs);
981
Mitch Williams9e5634d2014-04-04 04:43:14 +0000982 if (!i40e_vfs_are_assigned(pf)) {
983 i40e_free_vfs(pf);
984 } else {
985 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
986 return -EINVAL;
987 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000988 return 0;
989}
990
991/***********************virtual channel routines******************/
992
993/**
994 * i40e_vc_send_msg_to_vf
995 * @vf: pointer to the vf info
996 * @v_opcode: virtual channel opcode
997 * @v_retval: virtual channel return value
998 * @msg: pointer to the msg buffer
999 * @msglen: msg length
1000 *
1001 * send msg to vf
1002 **/
1003static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1004 u32 v_retval, u8 *msg, u16 msglen)
1005{
1006 struct i40e_pf *pf = vf->pf;
1007 struct i40e_hw *hw = &pf->hw;
Ashish Shahf19efbb2014-08-01 13:27:06 -07001008 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001009 i40e_status aq_ret;
1010
1011 /* single place to detect unsuccessful return values */
1012 if (v_retval) {
1013 vf->num_invalid_msgs++;
1014 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
1015 v_opcode, v_retval);
1016 if (vf->num_invalid_msgs >
1017 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1018 dev_err(&pf->pdev->dev,
1019 "Number of invalid messages exceeded for VF %d\n",
1020 vf->vf_id);
1021 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1022 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1023 }
1024 } else {
1025 vf->num_valid_msgs++;
1026 }
1027
Ashish Shahf19efbb2014-08-01 13:27:06 -07001028 aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Mitch Williams7efa84b2013-11-28 06:39:41 +00001029 msg, msglen, NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001030 if (aq_ret) {
1031 dev_err(&pf->pdev->dev,
1032 "Unable to send the message to VF %d aq_err %d\n",
1033 vf->vf_id, pf->hw.aq.asq_last_status);
1034 return -EIO;
1035 }
1036
1037 return 0;
1038}
1039
1040/**
1041 * i40e_vc_send_resp_to_vf
1042 * @vf: pointer to the vf info
1043 * @opcode: operation code
1044 * @retval: return value
1045 *
1046 * send resp msg to vf
1047 **/
1048static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1049 enum i40e_virtchnl_ops opcode,
1050 i40e_status retval)
1051{
1052 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1053}
1054
1055/**
1056 * i40e_vc_get_version_msg
1057 * @vf: pointer to the vf info
1058 *
1059 * called from the vf to request the API version used by the PF
1060 **/
1061static int i40e_vc_get_version_msg(struct i40e_vf *vf)
1062{
1063 struct i40e_virtchnl_version_info info = {
1064 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
1065 };
1066
1067 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
1068 I40E_SUCCESS, (u8 *)&info,
1069 sizeof(struct
1070 i40e_virtchnl_version_info));
1071}
1072
1073/**
1074 * i40e_vc_get_vf_resources_msg
1075 * @vf: pointer to the vf info
1076 * @msg: pointer to the msg buffer
1077 * @msglen: msg length
1078 *
1079 * called from the vf to request its resources
1080 **/
1081static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf)
1082{
1083 struct i40e_virtchnl_vf_resource *vfres = NULL;
1084 struct i40e_pf *pf = vf->pf;
1085 i40e_status aq_ret = 0;
1086 struct i40e_vsi *vsi;
1087 int i = 0, len = 0;
1088 int num_vsis = 1;
1089 int ret;
1090
1091 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1092 aq_ret = I40E_ERR_PARAM;
1093 goto err;
1094 }
1095
1096 len = (sizeof(struct i40e_virtchnl_vf_resource) +
1097 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1098
1099 vfres = kzalloc(len, GFP_KERNEL);
1100 if (!vfres) {
1101 aq_ret = I40E_ERR_NO_MEMORY;
1102 len = 0;
1103 goto err;
1104 }
1105
1106 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
1107 vsi = pf->vsi[vf->lan_vsi_index];
1108 if (!vsi->info.pvid)
1109 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1110
1111 vfres->num_vsis = num_vsis;
1112 vfres->num_queue_pairs = vf->num_queue_pairs;
1113 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1114 if (vf->lan_vsi_index) {
1115 vfres->vsi_res[i].vsi_id = vf->lan_vsi_index;
1116 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1117 vfres->vsi_res[i].num_queue_pairs =
1118 pf->vsi[vf->lan_vsi_index]->num_queue_pairs;
1119 memcpy(vfres->vsi_res[i].default_mac_addr,
1120 vf->default_lan_addr.addr, ETH_ALEN);
1121 i++;
1122 }
1123 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1124
1125err:
1126 /* send the response back to the vf */
1127 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1128 aq_ret, (u8 *)vfres, len);
1129
1130 kfree(vfres);
1131 return ret;
1132}
1133
1134/**
1135 * i40e_vc_reset_vf_msg
1136 * @vf: pointer to the vf info
1137 * @msg: pointer to the msg buffer
1138 * @msglen: msg length
1139 *
1140 * called from the vf to reset itself,
1141 * unlike other virtchnl messages, pf driver
1142 * doesn't send the response back to the vf
1143 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001144static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001145{
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001146 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1147 i40e_reset_vf(vf, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001148}
1149
1150/**
1151 * i40e_vc_config_promiscuous_mode_msg
1152 * @vf: pointer to the vf info
1153 * @msg: pointer to the msg buffer
1154 * @msglen: msg length
1155 *
1156 * called from the vf to configure the promiscuous mode of
1157 * vf vsis
1158 **/
1159static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1160 u8 *msg, u16 msglen)
1161{
1162 struct i40e_virtchnl_promisc_info *info =
1163 (struct i40e_virtchnl_promisc_info *)msg;
1164 struct i40e_pf *pf = vf->pf;
1165 struct i40e_hw *hw = &pf->hw;
1166 bool allmulti = false;
1167 bool promisc = false;
1168 i40e_status aq_ret;
1169
1170 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1171 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1172 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1173 (pf->vsi[info->vsi_id]->type != I40E_VSI_FCOE)) {
1174 aq_ret = I40E_ERR_PARAM;
1175 goto error_param;
1176 }
1177
1178 if (info->flags & I40E_FLAG_VF_UNICAST_PROMISC)
1179 promisc = true;
1180 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, info->vsi_id,
1181 promisc, NULL);
1182 if (aq_ret)
1183 goto error_param;
1184
1185 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1186 allmulti = true;
1187 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, info->vsi_id,
1188 allmulti, NULL);
1189
1190error_param:
1191 /* send the response to the vf */
1192 return i40e_vc_send_resp_to_vf(vf,
1193 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1194 aq_ret);
1195}
1196
1197/**
1198 * i40e_vc_config_queues_msg
1199 * @vf: pointer to the vf info
1200 * @msg: pointer to the msg buffer
1201 * @msglen: msg length
1202 *
1203 * called from the vf to configure the rx/tx
1204 * queues
1205 **/
1206static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1207{
1208 struct i40e_virtchnl_vsi_queue_config_info *qci =
1209 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1210 struct i40e_virtchnl_queue_pair_info *qpi;
1211 u16 vsi_id, vsi_queue_id;
1212 i40e_status aq_ret = 0;
1213 int i;
1214
1215 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1216 aq_ret = I40E_ERR_PARAM;
1217 goto error_param;
1218 }
1219
1220 vsi_id = qci->vsi_id;
1221 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1222 aq_ret = I40E_ERR_PARAM;
1223 goto error_param;
1224 }
1225 for (i = 0; i < qci->num_queue_pairs; i++) {
1226 qpi = &qci->qpair[i];
1227 vsi_queue_id = qpi->txq.queue_id;
1228 if ((qpi->txq.vsi_id != vsi_id) ||
1229 (qpi->rxq.vsi_id != vsi_id) ||
1230 (qpi->rxq.queue_id != vsi_queue_id) ||
1231 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1232 aq_ret = I40E_ERR_PARAM;
1233 goto error_param;
1234 }
1235
1236 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1237 &qpi->rxq) ||
1238 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1239 &qpi->txq)) {
1240 aq_ret = I40E_ERR_PARAM;
1241 goto error_param;
1242 }
1243 }
1244
1245error_param:
1246 /* send the response to the vf */
1247 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1248 aq_ret);
1249}
1250
1251/**
1252 * i40e_vc_config_irq_map_msg
1253 * @vf: pointer to the vf info
1254 * @msg: pointer to the msg buffer
1255 * @msglen: msg length
1256 *
1257 * called from the vf to configure the irq to
1258 * queue map
1259 **/
1260static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1261{
1262 struct i40e_virtchnl_irq_map_info *irqmap_info =
1263 (struct i40e_virtchnl_irq_map_info *)msg;
1264 struct i40e_virtchnl_vector_map *map;
1265 u16 vsi_id, vsi_queue_id, vector_id;
1266 i40e_status aq_ret = 0;
1267 unsigned long tempmap;
1268 int i;
1269
1270 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1271 aq_ret = I40E_ERR_PARAM;
1272 goto error_param;
1273 }
1274
1275 for (i = 0; i < irqmap_info->num_vectors; i++) {
1276 map = &irqmap_info->vecmap[i];
1277
1278 vector_id = map->vector_id;
1279 vsi_id = map->vsi_id;
1280 /* validate msg params */
1281 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1282 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1283 aq_ret = I40E_ERR_PARAM;
1284 goto error_param;
1285 }
1286
1287 /* lookout for the invalid queue index */
1288 tempmap = map->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001289 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001290 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1291 vsi_queue_id)) {
1292 aq_ret = I40E_ERR_PARAM;
1293 goto error_param;
1294 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001295 }
1296
1297 tempmap = map->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001298 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001299 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1300 vsi_queue_id)) {
1301 aq_ret = I40E_ERR_PARAM;
1302 goto error_param;
1303 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001304 }
1305
1306 i40e_config_irq_link_list(vf, vsi_id, map);
1307 }
1308error_param:
1309 /* send the response to the vf */
1310 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1311 aq_ret);
1312}
1313
1314/**
1315 * i40e_vc_enable_queues_msg
1316 * @vf: pointer to the vf info
1317 * @msg: pointer to the msg buffer
1318 * @msglen: msg length
1319 *
1320 * called from the vf to enable all or specific queue(s)
1321 **/
1322static int i40e_vc_enable_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;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001329
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, 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 }
Mitch Williams88f65632013-11-28 06:39:28 +00001344 if (i40e_vsi_control_rings(pf->vsi[vsi_id], true))
1345 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001346error_param:
1347 /* send the response to the vf */
1348 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1349 aq_ret);
1350}
1351
1352/**
1353 * i40e_vc_disable_queues_msg
1354 * @vf: pointer to the vf info
1355 * @msg: pointer to the msg buffer
1356 * @msglen: msg length
1357 *
1358 * called from the vf to disable all or specific
1359 * queue(s)
1360 **/
1361static int i40e_vc_disable_queues_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 u16 vsi_id = vqs->vsi_id;
1367 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001368
1369 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1370 aq_ret = I40E_ERR_PARAM;
1371 goto error_param;
1372 }
1373
1374 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1375 aq_ret = I40E_ERR_PARAM;
1376 goto error_param;
1377 }
1378
1379 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1380 aq_ret = I40E_ERR_PARAM;
1381 goto error_param;
1382 }
Mitch Williams88f65632013-11-28 06:39:28 +00001383 if (i40e_vsi_control_rings(pf->vsi[vsi_id], false))
1384 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001385
1386error_param:
1387 /* send the response to the vf */
1388 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1389 aq_ret);
1390}
1391
1392/**
1393 * i40e_vc_get_stats_msg
1394 * @vf: pointer to the vf info
1395 * @msg: pointer to the msg buffer
1396 * @msglen: msg length
1397 *
1398 * called from the vf to get vsi stats
1399 **/
1400static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1401{
1402 struct i40e_virtchnl_queue_select *vqs =
1403 (struct i40e_virtchnl_queue_select *)msg;
1404 struct i40e_pf *pf = vf->pf;
1405 struct i40e_eth_stats stats;
1406 i40e_status aq_ret = 0;
1407 struct i40e_vsi *vsi;
1408
1409 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1410
1411 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1412 aq_ret = I40E_ERR_PARAM;
1413 goto error_param;
1414 }
1415
1416 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1417 aq_ret = I40E_ERR_PARAM;
1418 goto error_param;
1419 }
1420
1421 vsi = pf->vsi[vqs->vsi_id];
1422 if (!vsi) {
1423 aq_ret = I40E_ERR_PARAM;
1424 goto error_param;
1425 }
1426 i40e_update_eth_stats(vsi);
Mitch Williams5a9769c2013-11-28 06:39:38 +00001427 stats = vsi->eth_stats;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001428
1429error_param:
1430 /* send the response back to the vf */
1431 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1432 (u8 *)&stats, sizeof(stats));
1433}
1434
1435/**
Greg Rosef657a6e2013-11-28 06:39:42 +00001436 * i40e_check_vf_permission
1437 * @vf: pointer to the vf info
1438 * @macaddr: pointer to the MAC Address being checked
1439 *
1440 * Check if the VF has permission to add or delete unicast MAC address
1441 * filters and return error code -EPERM if not. Then check if the
1442 * address filter requested is broadcast or zero and if so return
1443 * an invalid MAC address error code.
1444 **/
1445static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1446{
1447 struct i40e_pf *pf = vf->pf;
1448 int ret = 0;
1449
1450 if (is_broadcast_ether_addr(macaddr) ||
1451 is_zero_ether_addr(macaddr)) {
1452 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1453 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rose5017c2a2013-12-07 10:36:54 +00001454 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1455 !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001456 /* If the host VMM administrator has set the VF MAC address
1457 * administratively via the ndo_set_vf_mac command then deny
1458 * permission to the VF to add or delete unicast MAC addresses.
Greg Rose5017c2a2013-12-07 10:36:54 +00001459 * The VF may request to set the MAC address filter already
1460 * assigned to it so do not return an error in that case.
Greg Rosef657a6e2013-11-28 06:39:42 +00001461 */
1462 dev_err(&pf->pdev->dev,
1463 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1464 ret = -EPERM;
1465 }
1466 return ret;
1467}
1468
1469/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001470 * i40e_vc_add_mac_addr_msg
1471 * @vf: pointer to the vf info
1472 * @msg: pointer to the msg buffer
1473 * @msglen: msg length
1474 *
1475 * add guest mac address filter
1476 **/
1477static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1478{
1479 struct i40e_virtchnl_ether_addr_list *al =
1480 (struct i40e_virtchnl_ether_addr_list *)msg;
1481 struct i40e_pf *pf = vf->pf;
1482 struct i40e_vsi *vsi = NULL;
1483 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001484 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001485 int i;
1486
1487 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1488 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1489 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001490 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001491 goto error_param;
1492 }
1493
1494 for (i = 0; i < al->num_elements; i++) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001495 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1496 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001497 goto error_param;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001498 }
1499 vsi = pf->vsi[vsi_id];
1500
1501 /* add new addresses to the list */
1502 for (i = 0; i < al->num_elements; i++) {
1503 struct i40e_mac_filter *f;
1504
1505 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
Mitch Williams7e68edf92013-11-16 10:00:41 +00001506 if (!f) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001507 if (i40e_is_vsi_in_vlan(vsi))
1508 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1509 true, false);
1510 else
1511 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1512 true, false);
1513 }
1514
1515 if (!f) {
1516 dev_err(&pf->pdev->dev,
1517 "Unable to add VF MAC filter\n");
Greg Rosef657a6e2013-11-28 06:39:42 +00001518 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001519 goto error_param;
1520 }
1521 }
1522
1523 /* program the updated filter list */
1524 if (i40e_sync_vsi_filters(vsi))
1525 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1526
1527error_param:
1528 /* send the response to the vf */
1529 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001530 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001531}
1532
1533/**
1534 * i40e_vc_del_mac_addr_msg
1535 * @vf: pointer to the vf info
1536 * @msg: pointer to the msg buffer
1537 * @msglen: msg length
1538 *
1539 * remove guest mac address filter
1540 **/
1541static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1542{
1543 struct i40e_virtchnl_ether_addr_list *al =
1544 (struct i40e_virtchnl_ether_addr_list *)msg;
1545 struct i40e_pf *pf = vf->pf;
1546 struct i40e_vsi *vsi = NULL;
1547 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001548 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001549 int i;
1550
1551 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1552 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1553 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001554 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001555 goto error_param;
1556 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001557
1558 for (i = 0; i < al->num_elements; i++) {
Mitch Williams700bbf62013-12-21 05:44:45 +00001559 if (is_broadcast_ether_addr(al->list[i].addr) ||
1560 is_zero_ether_addr(al->list[i].addr)) {
1561 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
1562 al->list[i].addr);
1563 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rosef657a6e2013-11-28 06:39:42 +00001564 goto error_param;
Mitch Williams700bbf62013-12-21 05:44:45 +00001565 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001566 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001567 vsi = pf->vsi[vsi_id];
1568
1569 /* delete addresses from the list */
1570 for (i = 0; i < al->num_elements; i++)
1571 i40e_del_filter(vsi, al->list[i].addr,
1572 I40E_VLAN_ANY, true, false);
1573
1574 /* program the updated filter list */
1575 if (i40e_sync_vsi_filters(vsi))
1576 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1577
1578error_param:
1579 /* send the response to the vf */
1580 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001581 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001582}
1583
1584/**
1585 * i40e_vc_add_vlan_msg
1586 * @vf: pointer to the vf info
1587 * @msg: pointer to the msg buffer
1588 * @msglen: msg length
1589 *
1590 * program guest vlan id
1591 **/
1592static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1593{
1594 struct i40e_virtchnl_vlan_filter_list *vfl =
1595 (struct i40e_virtchnl_vlan_filter_list *)msg;
1596 struct i40e_pf *pf = vf->pf;
1597 struct i40e_vsi *vsi = NULL;
1598 u16 vsi_id = vfl->vsi_id;
1599 i40e_status aq_ret = 0;
1600 int i;
1601
1602 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1603 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1604 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1605 aq_ret = I40E_ERR_PARAM;
1606 goto error_param;
1607 }
1608
1609 for (i = 0; i < vfl->num_elements; i++) {
1610 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1611 aq_ret = I40E_ERR_PARAM;
1612 dev_err(&pf->pdev->dev,
1613 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1614 goto error_param;
1615 }
1616 }
1617 vsi = pf->vsi[vsi_id];
1618 if (vsi->info.pvid) {
1619 aq_ret = I40E_ERR_PARAM;
1620 goto error_param;
1621 }
1622
1623 i40e_vlan_stripping_enable(vsi);
1624 for (i = 0; i < vfl->num_elements; i++) {
1625 /* add new VLAN filter */
1626 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1627 if (ret)
1628 dev_err(&pf->pdev->dev,
1629 "Unable to add VF vlan filter %d, error %d\n",
1630 vfl->vlan_id[i], ret);
1631 }
1632
1633error_param:
1634 /* send the response to the vf */
1635 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1636}
1637
1638/**
1639 * i40e_vc_remove_vlan_msg
1640 * @vf: pointer to the vf info
1641 * @msg: pointer to the msg buffer
1642 * @msglen: msg length
1643 *
1644 * remove programmed guest vlan id
1645 **/
1646static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1647{
1648 struct i40e_virtchnl_vlan_filter_list *vfl =
1649 (struct i40e_virtchnl_vlan_filter_list *)msg;
1650 struct i40e_pf *pf = vf->pf;
1651 struct i40e_vsi *vsi = NULL;
1652 u16 vsi_id = vfl->vsi_id;
1653 i40e_status aq_ret = 0;
1654 int i;
1655
1656 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1657 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1658 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1659 aq_ret = I40E_ERR_PARAM;
1660 goto error_param;
1661 }
1662
1663 for (i = 0; i < vfl->num_elements; i++) {
1664 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1665 aq_ret = I40E_ERR_PARAM;
1666 goto error_param;
1667 }
1668 }
1669
1670 vsi = pf->vsi[vsi_id];
1671 if (vsi->info.pvid) {
1672 aq_ret = I40E_ERR_PARAM;
1673 goto error_param;
1674 }
1675
1676 for (i = 0; i < vfl->num_elements; i++) {
1677 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1678 if (ret)
1679 dev_err(&pf->pdev->dev,
1680 "Unable to delete VF vlan filter %d, error %d\n",
1681 vfl->vlan_id[i], ret);
1682 }
1683
1684error_param:
1685 /* send the response to the vf */
1686 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1687}
1688
1689/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001690 * i40e_vc_validate_vf_msg
1691 * @vf: pointer to the vf info
1692 * @msg: pointer to the msg buffer
1693 * @msglen: msg length
1694 * @msghndl: msg handle
1695 *
1696 * validate msg
1697 **/
1698static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1699 u32 v_retval, u8 *msg, u16 msglen)
1700{
1701 bool err_msg_format = false;
1702 int valid_len;
1703
1704 /* Check if VF is disabled. */
1705 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1706 return I40E_ERR_PARAM;
1707
1708 /* Validate message length. */
1709 switch (v_opcode) {
1710 case I40E_VIRTCHNL_OP_VERSION:
1711 valid_len = sizeof(struct i40e_virtchnl_version_info);
1712 break;
1713 case I40E_VIRTCHNL_OP_RESET_VF:
1714 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1715 valid_len = 0;
1716 break;
1717 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1718 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1719 break;
1720 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1721 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1722 break;
1723 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1724 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1725 if (msglen >= valid_len) {
1726 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1727 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1728 valid_len += (vqc->num_queue_pairs *
1729 sizeof(struct
1730 i40e_virtchnl_queue_pair_info));
1731 if (vqc->num_queue_pairs == 0)
1732 err_msg_format = true;
1733 }
1734 break;
1735 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1736 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1737 if (msglen >= valid_len) {
1738 struct i40e_virtchnl_irq_map_info *vimi =
1739 (struct i40e_virtchnl_irq_map_info *)msg;
1740 valid_len += (vimi->num_vectors *
1741 sizeof(struct i40e_virtchnl_vector_map));
1742 if (vimi->num_vectors == 0)
1743 err_msg_format = true;
1744 }
1745 break;
1746 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1747 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1748 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1749 break;
1750 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1751 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1752 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1753 if (msglen >= valid_len) {
1754 struct i40e_virtchnl_ether_addr_list *veal =
1755 (struct i40e_virtchnl_ether_addr_list *)msg;
1756 valid_len += veal->num_elements *
1757 sizeof(struct i40e_virtchnl_ether_addr);
1758 if (veal->num_elements == 0)
1759 err_msg_format = true;
1760 }
1761 break;
1762 case I40E_VIRTCHNL_OP_ADD_VLAN:
1763 case I40E_VIRTCHNL_OP_DEL_VLAN:
1764 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1765 if (msglen >= valid_len) {
1766 struct i40e_virtchnl_vlan_filter_list *vfl =
1767 (struct i40e_virtchnl_vlan_filter_list *)msg;
1768 valid_len += vfl->num_elements * sizeof(u16);
1769 if (vfl->num_elements == 0)
1770 err_msg_format = true;
1771 }
1772 break;
1773 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1774 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1775 break;
1776 case I40E_VIRTCHNL_OP_GET_STATS:
1777 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1778 break;
1779 /* These are always errors coming from the VF. */
1780 case I40E_VIRTCHNL_OP_EVENT:
1781 case I40E_VIRTCHNL_OP_UNKNOWN:
1782 default:
1783 return -EPERM;
1784 break;
1785 }
1786 /* few more checks */
1787 if ((valid_len != msglen) || (err_msg_format)) {
1788 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1789 return -EINVAL;
1790 } else {
1791 return 0;
1792 }
1793}
1794
1795/**
1796 * i40e_vc_process_vf_msg
1797 * @pf: pointer to the pf structure
1798 * @vf_id: source vf id
1799 * @msg: pointer to the msg buffer
1800 * @msglen: msg length
1801 * @msghndl: msg handle
1802 *
1803 * called from the common aeq/arq handler to
1804 * process request from vf
1805 **/
1806int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1807 u32 v_retval, u8 *msg, u16 msglen)
1808{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001809 struct i40e_hw *hw = &pf->hw;
Dan Carpenterc243e962014-01-15 06:43:39 +00001810 unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001811 struct i40e_vf *vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001812 int ret;
1813
1814 pf->vf_aq_requests++;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001815 if (local_vf_id >= pf->num_alloc_vfs)
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001816 return -EINVAL;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001817 vf = &(pf->vf[local_vf_id]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001818 /* perform basic checks on the msg */
1819 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1820
1821 if (ret) {
Mitch Williams499ec802013-11-28 06:39:31 +00001822 dev_err(&pf->pdev->dev, "Invalid message from vf %d, opcode %d, len %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001823 local_vf_id, v_opcode, msglen);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001824 return ret;
1825 }
Mitch Williamsbae3cae2014-01-14 00:49:49 -08001826
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001827 switch (v_opcode) {
1828 case I40E_VIRTCHNL_OP_VERSION:
1829 ret = i40e_vc_get_version_msg(vf);
1830 break;
1831 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1832 ret = i40e_vc_get_vf_resources_msg(vf);
1833 break;
1834 case I40E_VIRTCHNL_OP_RESET_VF:
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001835 i40e_vc_reset_vf_msg(vf);
1836 ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001837 break;
1838 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1839 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1840 break;
1841 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1842 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1843 break;
1844 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1845 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1846 break;
1847 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1848 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
1849 break;
1850 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1851 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1852 break;
1853 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1854 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1855 break;
1856 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1857 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1858 break;
1859 case I40E_VIRTCHNL_OP_ADD_VLAN:
1860 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1861 break;
1862 case I40E_VIRTCHNL_OP_DEL_VLAN:
1863 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1864 break;
1865 case I40E_VIRTCHNL_OP_GET_STATS:
1866 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1867 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001868 case I40E_VIRTCHNL_OP_UNKNOWN:
1869 default:
Mitch Williams7efa84b2013-11-28 06:39:41 +00001870 dev_err(&pf->pdev->dev, "Unsupported opcode %d from vf %d\n",
1871 v_opcode, local_vf_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001872 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1873 I40E_ERR_NOT_IMPLEMENTED);
1874 break;
1875 }
1876
1877 return ret;
1878}
1879
1880/**
1881 * i40e_vc_process_vflr_event
1882 * @pf: pointer to the pf structure
1883 *
1884 * called from the vlfr irq handler to
1885 * free up vf resources and state variables
1886 **/
1887int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1888{
1889 u32 reg, reg_idx, bit_idx, vf_id;
1890 struct i40e_hw *hw = &pf->hw;
1891 struct i40e_vf *vf;
1892
1893 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1894 return 0;
1895
1896 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1897 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1898 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1899 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1900 /* read GLGEN_VFLRSTAT register to find out the flr vfs */
1901 vf = &pf->vf[vf_id];
1902 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
1903 if (reg & (1 << bit_idx)) {
1904 /* clear the bit in GLGEN_VFLRSTAT */
1905 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
1906
Mitch Williamseb2d80b2014-02-13 03:48:48 -08001907 if (!test_bit(__I40E_DOWN, &pf->state))
1908 i40e_reset_vf(vf, true);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001909 }
1910 }
1911
1912 /* re-enable vflr interrupt cause */
1913 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1914 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1915 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1916 i40e_flush(hw);
1917
1918 return 0;
1919}
1920
1921/**
1922 * i40e_vc_vf_broadcast
1923 * @pf: pointer to the pf structure
1924 * @opcode: operation code
1925 * @retval: return value
1926 * @msg: pointer to the msg buffer
1927 * @msglen: msg length
1928 *
1929 * send a message to all VFs on a given PF
1930 **/
1931static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
1932 enum i40e_virtchnl_ops v_opcode,
1933 i40e_status v_retval, u8 *msg,
1934 u16 msglen)
1935{
1936 struct i40e_hw *hw = &pf->hw;
1937 struct i40e_vf *vf = pf->vf;
Ashish Shahf19efbb2014-08-01 13:27:06 -07001938 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001939 int i;
1940
1941 for (i = 0; i < pf->num_alloc_vfs; i++) {
1942 /* Ignore return value on purpose - a given VF may fail, but
1943 * we need to keep going and send to all of them
1944 */
Ashish Shahf19efbb2014-08-01 13:27:06 -07001945 i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001946 msg, msglen, NULL);
1947 vf++;
Ashish Shahf19efbb2014-08-01 13:27:06 -07001948 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001949 }
1950}
1951
1952/**
1953 * i40e_vc_notify_link_state
1954 * @pf: pointer to the pf structure
1955 *
1956 * send a link status message to all VFs on a given PF
1957 **/
1958void i40e_vc_notify_link_state(struct i40e_pf *pf)
1959{
1960 struct i40e_virtchnl_pf_event pfe;
Mitch Williams588aefa2014-02-11 08:27:49 +00001961 struct i40e_hw *hw = &pf->hw;
1962 struct i40e_vf *vf = pf->vf;
1963 struct i40e_link_status *ls = &pf->hw.phy.link_info;
Ashish Shahf19efbb2014-08-01 13:27:06 -07001964 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00001965 int i;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001966
1967 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
1968 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
Mitch Williams588aefa2014-02-11 08:27:49 +00001969 for (i = 0; i < pf->num_alloc_vfs; i++) {
1970 if (vf->link_forced) {
1971 pfe.event_data.link_event.link_status = vf->link_up;
1972 pfe.event_data.link_event.link_speed =
1973 (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
1974 } else {
1975 pfe.event_data.link_event.link_status =
1976 ls->link_info & I40E_AQ_LINK_UP;
1977 pfe.event_data.link_event.link_speed = ls->link_speed;
1978 }
Ashish Shahf19efbb2014-08-01 13:27:06 -07001979 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Mitch Williams588aefa2014-02-11 08:27:49 +00001980 0, (u8 *)&pfe, sizeof(pfe),
1981 NULL);
1982 vf++;
Ashish Shahf19efbb2014-08-01 13:27:06 -07001983 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00001984 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001985}
1986
1987/**
1988 * i40e_vc_notify_reset
1989 * @pf: pointer to the pf structure
1990 *
1991 * indicate a pending reset to all VFs on a given PF
1992 **/
1993void i40e_vc_notify_reset(struct i40e_pf *pf)
1994{
1995 struct i40e_virtchnl_pf_event pfe;
1996
1997 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1998 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
1999 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
2000 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
2001}
2002
2003/**
2004 * i40e_vc_notify_vf_reset
2005 * @vf: pointer to the vf structure
2006 *
2007 * indicate a pending reset to the given VF
2008 **/
2009void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
2010{
2011 struct i40e_virtchnl_pf_event pfe;
Ashish Shahf19efbb2014-08-01 13:27:06 -07002012 int abs_vf_id = vf->vf_id + vf->pf->hw.func_caps.vf_base_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002013
2014 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
2015 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
Ashish Shahf19efbb2014-08-01 13:27:06 -07002016 i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002017 I40E_SUCCESS, (u8 *)&pfe,
2018 sizeof(struct i40e_virtchnl_pf_event), NULL);
2019}
2020
2021/**
2022 * i40e_ndo_set_vf_mac
2023 * @netdev: network interface device structure
2024 * @vf_id: vf identifier
2025 * @mac: mac address
2026 *
2027 * program vf mac address
2028 **/
2029int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2030{
2031 struct i40e_netdev_priv *np = netdev_priv(netdev);
2032 struct i40e_vsi *vsi = np->vsi;
2033 struct i40e_pf *pf = vsi->back;
2034 struct i40e_mac_filter *f;
2035 struct i40e_vf *vf;
2036 int ret = 0;
2037
2038 /* validate the request */
2039 if (vf_id >= pf->num_alloc_vfs) {
2040 dev_err(&pf->pdev->dev,
2041 "Invalid VF Identifier %d\n", vf_id);
2042 ret = -EINVAL;
2043 goto error_param;
2044 }
2045
2046 vf = &(pf->vf[vf_id]);
2047 vsi = pf->vsi[vf->lan_vsi_index];
2048 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2049 dev_err(&pf->pdev->dev,
2050 "Uninitialized VF %d\n", vf_id);
2051 ret = -EINVAL;
2052 goto error_param;
2053 }
2054
2055 if (!is_valid_ether_addr(mac)) {
2056 dev_err(&pf->pdev->dev,
2057 "Invalid VF ethernet address\n");
2058 ret = -EINVAL;
2059 goto error_param;
2060 }
2061
2062 /* delete the temporary mac address */
Greg Rose37cc0d22014-04-01 07:11:44 +00002063 i40e_del_filter(vsi, vf->default_lan_addr.addr, vf->port_vlan_id,
2064 true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002065
Greg Rose29f71bb2014-05-20 08:01:45 +00002066 /* Delete all the filters for this VSI - we're going to kill it
2067 * anyway.
2068 */
2069 list_for_each_entry(f, &vsi->mac_filter_list, list)
2070 i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002071
2072 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2073 /* program mac filter */
2074 if (i40e_sync_vsi_filters(vsi)) {
2075 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2076 ret = -EIO;
2077 goto error_param;
2078 }
Greg Rose9a173902014-05-22 06:32:02 +00002079 ether_addr_copy(vf->default_lan_addr.addr, mac);
Greg Rosef657a6e2013-11-28 06:39:42 +00002080 vf->pf_set_mac = true;
Greg Rose17413a82014-06-04 01:23:13 +00002081 /* Force the VF driver stop so it has to reload with new MAC address */
2082 i40e_vc_disable_vf(pf, vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002083 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
2084 ret = 0;
2085
2086error_param:
2087 return ret;
2088}
2089
2090/**
2091 * i40e_ndo_set_vf_port_vlan
2092 * @netdev: network interface device structure
2093 * @vf_id: vf identifier
2094 * @vlan_id: mac address
2095 * @qos: priority setting
2096 *
2097 * program vf vlan id and/or qos
2098 **/
2099int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2100 int vf_id, u16 vlan_id, u8 qos)
2101{
2102 struct i40e_netdev_priv *np = netdev_priv(netdev);
2103 struct i40e_pf *pf = np->vsi->back;
2104 struct i40e_vsi *vsi;
2105 struct i40e_vf *vf;
2106 int ret = 0;
2107
2108 /* validate the request */
2109 if (vf_id >= pf->num_alloc_vfs) {
2110 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2111 ret = -EINVAL;
2112 goto error_pvid;
2113 }
2114
2115 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2116 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2117 ret = -EINVAL;
2118 goto error_pvid;
2119 }
2120
2121 vf = &(pf->vf[vf_id]);
2122 vsi = pf->vsi[vf->lan_vsi_index];
2123 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2124 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2125 ret = -EINVAL;
2126 goto error_pvid;
2127 }
2128
Greg Rosef9b4b622014-03-06 09:02:28 +00002129 if (vsi->info.pvid == 0 && i40e_is_vsi_in_vlan(vsi)) {
Greg Rose99a49732014-01-13 16:13:03 -08002130 dev_err(&pf->pdev->dev,
2131 "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",
2132 vf_id);
Greg Rosef9b4b622014-03-06 09:02:28 +00002133 /* Administrator Error - knock the VF offline until he does
2134 * the right thing by reconfiguring his network correctly
2135 * and then reloading the VF driver.
2136 */
2137 i40e_vc_disable_vf(pf, vf);
2138 }
Greg Rose99a49732014-01-13 16:13:03 -08002139
Greg Rose8d82a7c2014-01-13 16:13:04 -08002140 /* Check for condition where there was already a port VLAN ID
2141 * filter set and now it is being deleted by setting it to zero.
Greg Rose1315f7c2014-03-14 07:32:20 +00002142 * Additionally check for the condition where there was a port
2143 * VLAN but now there is a new and different port VLAN being set.
Greg Rose8d82a7c2014-01-13 16:13:04 -08002144 * Before deleting all the old VLAN filters we must add new ones
2145 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2146 * MAC addresses deleted.
2147 */
Greg Rose1315f7c2014-03-14 07:32:20 +00002148 if ((!(vlan_id || qos) ||
2149 (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
2150 vsi->info.pvid)
Greg Rose8d82a7c2014-01-13 16:13:04 -08002151 ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2152
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002153 if (vsi->info.pvid) {
2154 /* kill old VLAN */
2155 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2156 VLAN_VID_MASK));
2157 if (ret) {
2158 dev_info(&vsi->back->pdev->dev,
2159 "remove VLAN failed, ret=%d, aq_err=%d\n",
2160 ret, pf->hw.aq.asq_last_status);
2161 }
2162 }
2163 if (vlan_id || qos)
2164 ret = i40e_vsi_add_pvid(vsi,
2165 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
2166 else
Greg Rose6c12fcb2013-11-28 06:39:34 +00002167 i40e_vsi_remove_pvid(vsi);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002168
2169 if (vlan_id) {
2170 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2171 vlan_id, qos, vf_id);
2172
2173 /* add new VLAN filter */
2174 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2175 if (ret) {
2176 dev_info(&vsi->back->pdev->dev,
2177 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2178 vsi->back->hw.aq.asq_last_status);
2179 goto error_pvid;
2180 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002181 /* Kill non-vlan MAC filters - ignore error return since
2182 * there might not be any non-vlan MAC filters.
2183 */
2184 i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002185 }
2186
2187 if (ret) {
2188 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2189 goto error_pvid;
2190 }
Greg Rose6c12fcb2013-11-28 06:39:34 +00002191 /* The Port VLAN needs to be saved across resets the same as the
2192 * default LAN MAC address.
2193 */
2194 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002195 ret = 0;
2196
2197error_pvid:
2198 return ret;
2199}
2200
Mitch Williams84590fd2014-04-09 05:58:57 +00002201#define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
2202#define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002203/**
2204 * i40e_ndo_set_vf_bw
2205 * @netdev: network interface device structure
2206 * @vf_id: vf identifier
2207 * @tx_rate: tx rate
2208 *
2209 * configure vf tx rate
2210 **/
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002211int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2212 int max_tx_rate)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002213{
Mitch Williams6b192892014-03-06 09:02:29 +00002214 struct i40e_netdev_priv *np = netdev_priv(netdev);
2215 struct i40e_pf *pf = np->vsi->back;
2216 struct i40e_vsi *vsi;
2217 struct i40e_vf *vf;
2218 int speed = 0;
2219 int ret = 0;
2220
2221 /* validate the request */
2222 if (vf_id >= pf->num_alloc_vfs) {
2223 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2224 ret = -EINVAL;
2225 goto error;
2226 }
2227
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002228 if (min_tx_rate) {
2229 dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for vf %d.\n",
2230 min_tx_rate, vf_id);
2231 return -EINVAL;
2232 }
2233
Mitch Williams6b192892014-03-06 09:02:29 +00002234 vf = &(pf->vf[vf_id]);
2235 vsi = pf->vsi[vf->lan_vsi_index];
2236 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2237 dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
2238 ret = -EINVAL;
2239 goto error;
2240 }
2241
2242 switch (pf->hw.phy.link_info.link_speed) {
2243 case I40E_LINK_SPEED_40GB:
2244 speed = 40000;
2245 break;
2246 case I40E_LINK_SPEED_10GB:
2247 speed = 10000;
2248 break;
2249 case I40E_LINK_SPEED_1GB:
2250 speed = 1000;
2251 break;
2252 default:
2253 break;
2254 }
2255
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002256 if (max_tx_rate > speed) {
2257 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for vf %d.",
2258 max_tx_rate, vf->vf_id);
Mitch Williams6b192892014-03-06 09:02:29 +00002259 ret = -EINVAL;
2260 goto error;
2261 }
2262
Mitch Williamsdac9b312014-04-09 05:58:56 +00002263 if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2264 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2265 max_tx_rate = 50;
2266 }
2267
Mitch Williams6b192892014-03-06 09:02:29 +00002268 /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
Mitch Williams84590fd2014-04-09 05:58:57 +00002269 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2270 max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2271 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
Mitch Williams6b192892014-03-06 09:02:29 +00002272 if (ret) {
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002273 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
Mitch Williams6b192892014-03-06 09:02:29 +00002274 ret);
2275 ret = -EIO;
2276 goto error;
2277 }
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002278 vf->tx_rate = max_tx_rate;
Mitch Williams6b192892014-03-06 09:02:29 +00002279error:
2280 return ret;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002281}
2282
2283/**
2284 * i40e_ndo_get_vf_config
2285 * @netdev: network interface device structure
2286 * @vf_id: vf identifier
2287 * @ivi: vf configuration structure
2288 *
2289 * return vf configuration
2290 **/
2291int i40e_ndo_get_vf_config(struct net_device *netdev,
2292 int vf_id, struct ifla_vf_info *ivi)
2293{
2294 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002295 struct i40e_vsi *vsi = np->vsi;
2296 struct i40e_pf *pf = vsi->back;
2297 struct i40e_vf *vf;
2298 int ret = 0;
2299
2300 /* validate the request */
2301 if (vf_id >= pf->num_alloc_vfs) {
2302 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2303 ret = -EINVAL;
2304 goto error_param;
2305 }
2306
2307 vf = &(pf->vf[vf_id]);
2308 /* first vsi is always the LAN vsi */
2309 vsi = pf->vsi[vf->lan_vsi_index];
2310 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2311 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2312 ret = -EINVAL;
2313 goto error_param;
2314 }
2315
2316 ivi->vf = vf_id;
2317
Mitch Williamsf4a1c5c2013-11-28 06:39:34 +00002318 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002319
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002320 ivi->max_tx_rate = vf->tx_rate;
2321 ivi->min_tx_rate = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002322 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2323 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2324 I40E_VLAN_PRIORITY_SHIFT;
Mitch Williams84ca55a2014-03-14 07:32:24 +00002325 if (vf->link_forced == false)
2326 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2327 else if (vf->link_up == true)
2328 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2329 else
2330 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
Mitch Williamsc674d122014-05-20 08:01:40 +00002331 ivi->spoofchk = vf->spoofchk;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002332 ret = 0;
2333
2334error_param:
2335 return ret;
2336}
Mitch Williams588aefa2014-02-11 08:27:49 +00002337
2338/**
2339 * i40e_ndo_set_vf_link_state
2340 * @netdev: network interface device structure
2341 * @vf_id: vf identifier
2342 * @link: required link state
2343 *
2344 * Set the link state of a specified VF, regardless of physical link state
2345 **/
2346int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
2347{
2348 struct i40e_netdev_priv *np = netdev_priv(netdev);
2349 struct i40e_pf *pf = np->vsi->back;
2350 struct i40e_virtchnl_pf_event pfe;
2351 struct i40e_hw *hw = &pf->hw;
2352 struct i40e_vf *vf;
Ashish Shahf19efbb2014-08-01 13:27:06 -07002353 int abs_vf_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002354 int ret = 0;
2355
2356 /* validate the request */
2357 if (vf_id >= pf->num_alloc_vfs) {
2358 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2359 ret = -EINVAL;
2360 goto error_out;
2361 }
2362
2363 vf = &pf->vf[vf_id];
Ashish Shahf19efbb2014-08-01 13:27:06 -07002364 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002365
2366 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
2367 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
2368
2369 switch (link) {
2370 case IFLA_VF_LINK_STATE_AUTO:
2371 vf->link_forced = false;
2372 pfe.event_data.link_event.link_status =
2373 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
2374 pfe.event_data.link_event.link_speed =
2375 pf->hw.phy.link_info.link_speed;
2376 break;
2377 case IFLA_VF_LINK_STATE_ENABLE:
2378 vf->link_forced = true;
2379 vf->link_up = true;
2380 pfe.event_data.link_event.link_status = true;
2381 pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
2382 break;
2383 case IFLA_VF_LINK_STATE_DISABLE:
2384 vf->link_forced = true;
2385 vf->link_up = false;
2386 pfe.event_data.link_event.link_status = false;
2387 pfe.event_data.link_event.link_speed = 0;
2388 break;
2389 default:
2390 ret = -EINVAL;
2391 goto error_out;
2392 }
2393 /* Notify the VF of its new link state */
Ashish Shahf19efbb2014-08-01 13:27:06 -07002394 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Mitch Williams588aefa2014-02-11 08:27:49 +00002395 0, (u8 *)&pfe, sizeof(pfe), NULL);
2396
2397error_out:
2398 return ret;
2399}
Mitch Williamsc674d122014-05-20 08:01:40 +00002400
2401/**
2402 * i40e_ndo_set_vf_spoofchk
2403 * @netdev: network interface device structure
2404 * @vf_id: vf identifier
2405 * @enable: flag to enable or disable feature
2406 *
2407 * Enable or disable VF spoof checking
2408 **/
2409int i40e_ndo_set_vf_spoofck(struct net_device *netdev, int vf_id, bool enable)
2410{
2411 struct i40e_netdev_priv *np = netdev_priv(netdev);
2412 struct i40e_vsi *vsi = np->vsi;
2413 struct i40e_pf *pf = vsi->back;
2414 struct i40e_vsi_context ctxt;
2415 struct i40e_hw *hw = &pf->hw;
2416 struct i40e_vf *vf;
2417 int ret = 0;
2418
2419 /* validate the request */
2420 if (vf_id >= pf->num_alloc_vfs) {
2421 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2422 ret = -EINVAL;
2423 goto out;
2424 }
2425
2426 vf = &(pf->vf[vf_id]);
2427
2428 if (enable == vf->spoofchk)
2429 goto out;
2430
2431 vf->spoofchk = enable;
2432 memset(&ctxt, 0, sizeof(ctxt));
2433 ctxt.seid = pf->vsi[vf->lan_vsi_index]->seid;
2434 ctxt.pf_num = pf->hw.pf_id;
2435 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
2436 if (enable)
2437 ctxt.info.sec_flags |= I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK;
2438 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
2439 if (ret) {
2440 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
2441 ret);
2442 ret = -EIO;
2443 }
2444out:
2445 return ret;
2446}