blob: 184b00390f570984b805473fca79c135d9058131 [file] [log] [blame]
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Neerav Parikh51616012015-02-06 08:52:14 +00004 * Copyright(c) 2013 - 2015 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
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000033 * @pf: pointer to the PF info
34 * @vf: pointer to the VF info
Greg Rosef9b4b622014-03-06 09:02:28 +000035 *
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
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000051 * @vf: pointer to the VF info
52 * @vsi_id: VF relative VSI id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000053 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000054 * check for the valid VSI id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000055 **/
56static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u8 vsi_id)
57{
58 struct i40e_pf *pf = vf->pf;
59
Mitch Williamsb57459e2015-03-31 00:45:00 -070060 if (vsi_id > pf->num_alloc_vsi)
61 return false;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000062 return pf->vsi[vsi_id]->vf_id == vf->vf_id;
63}
64
65/**
66 * i40e_vc_isvalid_queue_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000067 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000068 * @vsi_id: vsi id
69 * @qid: vsi relative queue id
70 *
71 * check for the valid queue id
72 **/
73static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u8 vsi_id,
74 u8 qid)
75{
76 struct i40e_pf *pf = vf->pf;
77
Ashish Shah5f5e33b2014-07-10 07:58:15 +000078 return qid < pf->vsi[vsi_id]->alloc_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000079}
80
81/**
82 * i40e_vc_isvalid_vector_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000083 * @vf: pointer to the VF info
84 * @vector_id: VF relative vector id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000085 *
86 * check for the valid vector id
87 **/
88static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
89{
90 struct i40e_pf *pf = vf->pf;
91
Mitch Williams9347eb72014-02-11 08:26:32 +000092 return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000093}
94
95/***********************vf resource mgmt routines*****************/
96
97/**
98 * i40e_vc_get_pf_queue_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000099 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000100 * @vsi_idx: index of VSI in PF struct
101 * @vsi_queue_id: vsi relative queue id
102 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000103 * return PF relative queue id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000104 **/
105static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u8 vsi_idx,
106 u8 vsi_queue_id)
107{
108 struct i40e_pf *pf = vf->pf;
109 struct i40e_vsi *vsi = pf->vsi[vsi_idx];
110 u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
111
112 if (le16_to_cpu(vsi->info.mapping_flags) &
113 I40E_AQ_VSI_QUE_MAP_NONCONTIG)
114 pf_queue_id =
115 le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
116 else
117 pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
118 vsi_queue_id;
119
120 return pf_queue_id;
121}
122
123/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000124 * i40e_config_irq_link_list
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000125 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000126 * @vsi_idx: index of VSI in PF struct
127 * @vecmap: irq map info
128 *
129 * configure irq link list from the map
130 **/
131static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_idx,
132 struct i40e_virtchnl_vector_map *vecmap)
133{
134 unsigned long linklistmap = 0, tempmap;
135 struct i40e_pf *pf = vf->pf;
136 struct i40e_hw *hw = &pf->hw;
137 u16 vsi_queue_id, pf_queue_id;
138 enum i40e_queue_type qtype;
139 u16 next_q, vector_id;
140 u32 reg, reg_idx;
141 u16 itr_idx = 0;
142
143 vector_id = vecmap->vector_id;
144 /* setup the head */
145 if (0 == vector_id)
146 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
147 else
148 reg_idx = I40E_VPINT_LNKLSTN(
Mitch Williams9347eb72014-02-11 08:26:32 +0000149 ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
150 (vector_id - 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000151
152 if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
153 /* Special case - No queues mapped on this vector */
154 wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
155 goto irq_list_done;
156 }
157 tempmap = vecmap->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000158 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000159 linklistmap |= (1 <<
160 (I40E_VIRTCHNL_SUPPORTED_QTYPES *
161 vsi_queue_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000162 }
163
164 tempmap = vecmap->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000165 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000166 linklistmap |= (1 <<
167 (I40E_VIRTCHNL_SUPPORTED_QTYPES * vsi_queue_id
168 + 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000169 }
170
171 next_q = find_first_bit(&linklistmap,
172 (I40E_MAX_VSI_QP *
173 I40E_VIRTCHNL_SUPPORTED_QTYPES));
174 vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
175 qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
176 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx, vsi_queue_id);
177 reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
178
179 wr32(hw, reg_idx, reg);
180
181 while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
182 switch (qtype) {
183 case I40E_QUEUE_TYPE_RX:
184 reg_idx = I40E_QINT_RQCTL(pf_queue_id);
185 itr_idx = vecmap->rxitr_idx;
186 break;
187 case I40E_QUEUE_TYPE_TX:
188 reg_idx = I40E_QINT_TQCTL(pf_queue_id);
189 itr_idx = vecmap->txitr_idx;
190 break;
191 default:
192 break;
193 }
194
195 next_q = find_next_bit(&linklistmap,
196 (I40E_MAX_VSI_QP *
197 I40E_VIRTCHNL_SUPPORTED_QTYPES),
198 next_q + 1);
Mitch Williams829af3a2013-12-18 13:46:00 +0000199 if (next_q <
200 (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000201 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
202 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
203 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx,
204 vsi_queue_id);
205 } else {
206 pf_queue_id = I40E_QUEUE_END_OF_LIST;
207 qtype = 0;
208 }
209
210 /* format for the RQCTL & TQCTL regs is same */
211 reg = (vector_id) |
212 (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
213 (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
214 (1 << I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
215 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
216 wr32(hw, reg_idx, reg);
217 }
218
219irq_list_done:
220 i40e_flush(hw);
221}
222
223/**
224 * i40e_config_vsi_tx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000225 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000226 * @vsi_idx: index of VSI in PF struct
227 * @vsi_queue_id: vsi relative queue index
228 * @info: config. info
229 *
230 * configure tx queue
231 **/
232static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_idx,
233 u16 vsi_queue_id,
234 struct i40e_virtchnl_txq_info *info)
235{
236 struct i40e_pf *pf = vf->pf;
237 struct i40e_hw *hw = &pf->hw;
238 struct i40e_hmc_obj_txq tx_ctx;
239 u16 pf_queue_id;
240 u32 qtx_ctl;
241 int ret = 0;
242
243 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx, vsi_queue_id);
244
245 /* clear the context structure first */
246 memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
247
248 /* only set the required fields */
249 tx_ctx.base = info->dma_ring_addr / 128;
250 tx_ctx.qlen = info->ring_len;
251 tx_ctx.rdylist = le16_to_cpu(pf->vsi[vsi_idx]->info.qs_handle[0]);
252 tx_ctx.rdylist_act = 0;
Ashish Shah5d298962014-05-22 06:31:25 +0000253 tx_ctx.head_wb_ena = info->headwb_enabled;
254 tx_ctx.head_wb_addr = info->dma_headwb_addr;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000255
256 /* clear the context in the HMC */
257 ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
258 if (ret) {
259 dev_err(&pf->pdev->dev,
260 "Failed to clear VF LAN Tx queue context %d, error: %d\n",
261 pf_queue_id, ret);
262 ret = -ENOENT;
263 goto error_context;
264 }
265
266 /* set the context in the HMC */
267 ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
268 if (ret) {
269 dev_err(&pf->pdev->dev,
270 "Failed to set VF LAN Tx queue context %d error: %d\n",
271 pf_queue_id, ret);
272 ret = -ENOENT;
273 goto error_context;
274 }
275
276 /* associate this queue with the PCI VF function */
277 qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +0000278 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000279 & I40E_QTX_CTL_PF_INDX_MASK);
280 qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
281 << I40E_QTX_CTL_VFVM_INDX_SHIFT)
282 & I40E_QTX_CTL_VFVM_INDX_MASK);
283 wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
284 i40e_flush(hw);
285
286error_context:
287 return ret;
288}
289
290/**
291 * i40e_config_vsi_rx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000292 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000293 * @vsi_idx: index of VSI in PF struct
294 * @vsi_queue_id: vsi relative queue index
295 * @info: config. info
296 *
297 * configure rx queue
298 **/
299static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_idx,
300 u16 vsi_queue_id,
301 struct i40e_virtchnl_rxq_info *info)
302{
303 struct i40e_pf *pf = vf->pf;
304 struct i40e_hw *hw = &pf->hw;
305 struct i40e_hmc_obj_rxq rx_ctx;
306 u16 pf_queue_id;
307 int ret = 0;
308
309 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_idx, vsi_queue_id);
310
311 /* clear the context structure first */
312 memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
313
314 /* only set the required fields */
315 rx_ctx.base = info->dma_ring_addr / 128;
316 rx_ctx.qlen = info->ring_len;
317
318 if (info->splithdr_enabled) {
319 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
320 I40E_RX_SPLIT_IP |
321 I40E_RX_SPLIT_TCP_UDP |
322 I40E_RX_SPLIT_SCTP;
323 /* header length validation */
324 if (info->hdr_size > ((2 * 1024) - 64)) {
325 ret = -EINVAL;
326 goto error_param;
327 }
328 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
329
330 /* set splitalways mode 10b */
331 rx_ctx.dtype = 0x2;
332 }
333
334 /* databuffer length validation */
335 if (info->databuffer_size > ((16 * 1024) - 128)) {
336 ret = -EINVAL;
337 goto error_param;
338 }
339 rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
340
341 /* max pkt. length validation */
342 if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
343 ret = -EINVAL;
344 goto error_param;
345 }
346 rx_ctx.rxmax = info->max_pkt_size;
347
348 /* enable 32bytes desc always */
349 rx_ctx.dsize = 1;
350
351 /* default values */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000352 rx_ctx.lrxqthresh = 2;
353 rx_ctx.crcstrip = 1;
Mitch Williams50d41652014-04-09 05:58:55 +0000354 rx_ctx.prefena = 1;
Shannon Nelsonc1d11ce2014-07-29 04:01:03 +0000355 rx_ctx.l2tsel = 1;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000356
357 /* clear the context in the HMC */
358 ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
359 if (ret) {
360 dev_err(&pf->pdev->dev,
361 "Failed to clear VF LAN Rx queue context %d, error: %d\n",
362 pf_queue_id, ret);
363 ret = -ENOENT;
364 goto error_param;
365 }
366
367 /* set the context in the HMC */
368 ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
369 if (ret) {
370 dev_err(&pf->pdev->dev,
371 "Failed to set VF LAN Rx queue context %d error: %d\n",
372 pf_queue_id, ret);
373 ret = -ENOENT;
374 goto error_param;
375 }
376
377error_param:
378 return ret;
379}
380
381/**
382 * i40e_alloc_vsi_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000383 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000384 * @type: type of VSI to allocate
385 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000386 * alloc VF vsi context & resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000387 **/
388static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
389{
390 struct i40e_mac_filter *f = NULL;
391 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000392 struct i40e_vsi *vsi;
393 int ret = 0;
394
395 vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
396
397 if (!vsi) {
398 dev_err(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000399 "add vsi failed for VF %d, aq_err %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000400 vf->vf_id, pf->hw.aq.asq_last_status);
401 ret = -ENOENT;
402 goto error_alloc_vsi_res;
403 }
404 if (type == I40E_VSI_SRIOV) {
Greg Rose1a103702013-11-28 06:42:39 +0000405 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000406 vf->lan_vsi_index = vsi->idx;
407 vf->lan_vsi_id = 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
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000448 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000449 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000450 * enable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000451 **/
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 */
Ashish Shah5f5e33b2014-07-10 07:58:15 +0000471 for (j = 0; j < pf->vsi[vf->lan_vsi_index]->alloc_queue_pairs; j++) {
Mitch Williams805bd5b2013-11-28 06:39:26 +0000472 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++) {
Ashish Shah5f5e33b2014-07-10 07:58:15 +0000480 if (j * 2 >= pf->vsi[vf->lan_vsi_index]->alloc_queue_pairs) {
Mitch Williams805bd5b2013-11-28 06:39:26 +0000481 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
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000498 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000499 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000500 * disable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000501 **/
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
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000518 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000519 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000520 * free VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000521 **/
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
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000573 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000574 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000575 * allocate VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000576 **/
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;
Ashish Shah5f5e33b2014-07-10 07:58:15 +0000587 total_queue_pairs += pf->vsi[vf->lan_vsi_index]->alloc_queue_pairs;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000588 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
589
590 /* store the total qps number for the runtime
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000591 * VF req validation
Mitch Williams805bd5b2013-11-28 06:39:26 +0000592 */
593 vf->num_queue_pairs = total_queue_pairs;
594
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000595 /* VF is now completely initialized */
Mitch Williams805bd5b2013-11-28 06:39:26 +0000596 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
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000609 * @vf: pointer to the VF structure
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000610 *
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
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000636 * @vf: pointer to the VF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000637 * @flr: VFLR was issued or not
638 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000639 * reset the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000640 **/
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
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000649 if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
650 return;
651
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000652 /* warn the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000653 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
654
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000655 /* In the case of a VFLR, the HW has already reset the VF and we
656 * just need to clean up, so don't hit the VFRTRIG register.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000657 */
658 if (!flr) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000659 /* reset VF using VPGEN_VFRTRIG reg */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000660 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
661 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000662 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
663 i40e_flush(hw);
664 }
665
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000666 if (i40e_quiesce_vf_pci(vf))
667 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
668 vf->vf_id);
669
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000670 /* poll VPGEN_VFRSTAT reg to make sure
671 * that reset is complete
672 */
Mitch Williams1750a222015-01-09 11:18:13 +0000673 for (i = 0; i < 10; i++) {
674 /* VF reset requires driver to first reset the VF and then
675 * poll the status register to make sure that the reset
676 * completed successfully. Due to internal HW FIFO flushes,
677 * we must wait 10ms before the register will be valid.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000678 */
Mitch Williams1750a222015-01-09 11:18:13 +0000679 usleep_range(10000, 20000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000680 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
681 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
682 rsd = true;
683 break;
684 }
685 }
686
687 if (!rsd)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000688 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000689 vf->vf_id);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000690 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000691 /* clear the reset bit in the VPGEN_VFRTRIG reg */
692 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
693 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
694 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000695
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000696 /* On initial reset, we won't have any queues */
697 if (vf->lan_vsi_index == 0)
698 goto complete_reset;
699
700 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_index], false);
701complete_reset:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000702 /* reallocate VF resources to reset the VSI state */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000703 i40e_free_vf_res(vf);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000704 i40e_alloc_vf_res(vf);
705 i40e_enable_vf_mappings(vf);
Mitch Williamsc17b3622014-02-13 03:48:45 -0800706 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000707
708 /* tell the VF the reset is done */
709 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
710 i40e_flush(hw);
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000711 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000712}
Greg Rosec3542292013-12-13 08:38:38 +0000713
714/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000715 * i40e_free_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000716 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000717 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000718 * free VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000719 **/
720void i40e_free_vfs(struct i40e_pf *pf)
721{
Mitch Williamsf7414532013-11-28 06:39:40 +0000722 struct i40e_hw *hw = &pf->hw;
723 u32 reg_idx, bit_idx;
724 int i, tmp, vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000725
726 if (!pf->vf)
727 return;
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000728 while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
729 usleep_range(1000, 2000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000730
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000731 /* Disable IOV before freeing resources. This lets any VF drivers
732 * running in the host get themselves cleaned up before we yank
733 * the carpet out from underneath their feet.
734 */
735 if (!pci_vfs_assigned(pf->pdev))
736 pci_disable_sriov(pf->pdev);
Mitch Williams6d7b9672015-03-31 00:45:02 -0700737 else
738 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000739
740 msleep(20); /* let any messages in transit get finished up */
741
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000742 /* free up VF resources */
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000743 tmp = pf->num_alloc_vfs;
744 pf->num_alloc_vfs = 0;
745 for (i = 0; i < tmp; i++) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000746 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
747 i40e_free_vf_res(&pf->vf[i]);
748 /* disable qp mappings */
749 i40e_disable_vf_mappings(&pf->vf[i]);
750 }
751
752 kfree(pf->vf);
753 pf->vf = NULL;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000754
Mitch Williams9e5634d2014-04-04 04:43:14 +0000755 /* This check is for when the driver is unloaded while VFs are
756 * assigned. Setting the number of VFs to 0 through sysfs is caught
757 * before this function ever gets called.
758 */
Ethan Zhaoc24817b2014-07-22 18:36:43 +0000759 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williamsf7414532013-11-28 06:39:40 +0000760 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
761 * work correctly when SR-IOV gets re-enabled.
762 */
763 for (vf_id = 0; vf_id < tmp; vf_id++) {
764 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
765 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
766 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
767 }
Greg Rosec3542292013-12-13 08:38:38 +0000768 }
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000769 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000770}
771
772#ifdef CONFIG_PCI_IOV
773/**
774 * i40e_alloc_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000775 * @pf: pointer to the PF structure
776 * @num_alloc_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000777 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000778 * allocate VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000779 **/
Mitch Williams4aeec012014-02-13 03:48:47 -0800780int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000781{
782 struct i40e_vf *vfs;
783 int i, ret = 0;
784
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000785 /* Disable interrupt 0 so we don't try to handle the VFLR. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000786 i40e_irq_dynamic_disable_icr0(pf);
787
Mitch Williams4aeec012014-02-13 03:48:47 -0800788 /* Check to see if we're just allocating resources for extant VFs */
789 if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
790 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
791 if (ret) {
792 dev_err(&pf->pdev->dev,
793 "Failed to enable SR-IOV, error %d.\n", ret);
794 pf->num_alloc_vfs = 0;
795 goto err_iov;
796 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000797 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000798 /* allocate memory */
Akeem G Abodunrincc6456a2014-02-06 05:51:02 +0000799 vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000800 if (!vfs) {
801 ret = -ENOMEM;
802 goto err_alloc;
803 }
Mitch Williamsc674d122014-05-20 08:01:40 +0000804 pf->vf = vfs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000805
806 /* apply default profile */
807 for (i = 0; i < num_alloc_vfs; i++) {
808 vfs[i].pf = pf;
809 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
810 vfs[i].vf_id = i;
811
812 /* assign default capabilities */
813 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
Mitch Williamsc674d122014-05-20 08:01:40 +0000814 vfs[i].spoofchk = true;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000815 /* VF resources get allocated during reset */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000816 i40e_reset_vf(&vfs[i], false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000817
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000818 /* enable VF vplan_qtable mappings */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000819 i40e_enable_vf_mappings(&vfs[i]);
820 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000821 pf->num_alloc_vfs = num_alloc_vfs;
822
823err_alloc:
824 if (ret)
825 i40e_free_vfs(pf);
826err_iov:
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000827 /* Re-enable interrupt 0. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000828 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000829 return ret;
830}
831
832#endif
833/**
834 * i40e_pci_sriov_enable
835 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000836 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000837 *
838 * Enable or change the number of VFs
839 **/
840static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
841{
842#ifdef CONFIG_PCI_IOV
843 struct i40e_pf *pf = pci_get_drvdata(pdev);
844 int pre_existing_vfs = pci_num_vf(pdev);
845 int err = 0;
846
847 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
848 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
849 i40e_free_vfs(pf);
850 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
851 goto out;
852
853 if (num_vfs > pf->num_req_vfs) {
854 err = -EPERM;
855 goto err_out;
856 }
857
858 err = i40e_alloc_vfs(pf, num_vfs);
859 if (err) {
860 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
861 goto err_out;
862 }
863
864out:
865 return num_vfs;
866
867err_out:
868 return err;
869#endif
870 return 0;
871}
872
873/**
874 * i40e_pci_sriov_configure
875 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000876 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000877 *
878 * Enable or change the number of VFs. Called when the user updates the number
879 * of VFs in sysfs.
880 **/
881int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
882{
883 struct i40e_pf *pf = pci_get_drvdata(pdev);
884
885 if (num_vfs)
886 return i40e_pci_sriov_enable(pdev, num_vfs);
887
Ethan Zhaoc24817b2014-07-22 18:36:43 +0000888 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williams9e5634d2014-04-04 04:43:14 +0000889 i40e_free_vfs(pf);
890 } else {
891 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
892 return -EINVAL;
893 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000894 return 0;
895}
896
897/***********************virtual channel routines******************/
898
899/**
900 * i40e_vc_send_msg_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000901 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000902 * @v_opcode: virtual channel opcode
903 * @v_retval: virtual channel return value
904 * @msg: pointer to the msg buffer
905 * @msglen: msg length
906 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000907 * send msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000908 **/
909static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
910 u32 v_retval, u8 *msg, u16 msglen)
911{
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +0000912 struct i40e_pf *pf;
913 struct i40e_hw *hw;
914 int abs_vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000915 i40e_status aq_ret;
916
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +0000917 /* validate the request */
918 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
919 return -EINVAL;
920
921 pf = vf->pf;
922 hw = &pf->hw;
923 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
924
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000925 /* single place to detect unsuccessful return values */
926 if (v_retval) {
927 vf->num_invalid_msgs++;
928 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
929 v_opcode, v_retval);
930 if (vf->num_invalid_msgs >
931 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
932 dev_err(&pf->pdev->dev,
933 "Number of invalid messages exceeded for VF %d\n",
934 vf->vf_id);
935 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
936 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
937 }
938 } else {
939 vf->num_valid_msgs++;
940 }
941
Ashish Shahf19efbb2014-08-01 13:27:06 -0700942 aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Mitch Williams7efa84b2013-11-28 06:39:41 +0000943 msg, msglen, NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000944 if (aq_ret) {
945 dev_err(&pf->pdev->dev,
946 "Unable to send the message to VF %d aq_err %d\n",
947 vf->vf_id, pf->hw.aq.asq_last_status);
948 return -EIO;
949 }
950
951 return 0;
952}
953
954/**
955 * i40e_vc_send_resp_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000956 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000957 * @opcode: operation code
958 * @retval: return value
959 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000960 * send resp msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000961 **/
962static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
963 enum i40e_virtchnl_ops opcode,
964 i40e_status retval)
965{
966 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
967}
968
969/**
970 * i40e_vc_get_version_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000971 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000972 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000973 * called from the VF to request the API version used by the PF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000974 **/
975static int i40e_vc_get_version_msg(struct i40e_vf *vf)
976{
977 struct i40e_virtchnl_version_info info = {
978 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
979 };
980
981 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
982 I40E_SUCCESS, (u8 *)&info,
983 sizeof(struct
984 i40e_virtchnl_version_info));
985}
986
987/**
988 * i40e_vc_get_vf_resources_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000989 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000990 * @msg: pointer to the msg buffer
991 * @msglen: msg length
992 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000993 * called from the VF to request its resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000994 **/
995static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf)
996{
997 struct i40e_virtchnl_vf_resource *vfres = NULL;
998 struct i40e_pf *pf = vf->pf;
999 i40e_status aq_ret = 0;
1000 struct i40e_vsi *vsi;
1001 int i = 0, len = 0;
1002 int num_vsis = 1;
1003 int ret;
1004
1005 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1006 aq_ret = I40E_ERR_PARAM;
1007 goto err;
1008 }
1009
1010 len = (sizeof(struct i40e_virtchnl_vf_resource) +
1011 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1012
1013 vfres = kzalloc(len, GFP_KERNEL);
1014 if (!vfres) {
1015 aq_ret = I40E_ERR_NO_MEMORY;
1016 len = 0;
1017 goto err;
1018 }
1019
1020 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
1021 vsi = pf->vsi[vf->lan_vsi_index];
1022 if (!vsi->info.pvid)
1023 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1024
1025 vfres->num_vsis = num_vsis;
1026 vfres->num_queue_pairs = vf->num_queue_pairs;
1027 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1028 if (vf->lan_vsi_index) {
1029 vfres->vsi_res[i].vsi_id = vf->lan_vsi_index;
1030 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1031 vfres->vsi_res[i].num_queue_pairs =
Ashish Shah5f5e33b2014-07-10 07:58:15 +00001032 pf->vsi[vf->lan_vsi_index]->alloc_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001033 memcpy(vfres->vsi_res[i].default_mac_addr,
1034 vf->default_lan_addr.addr, ETH_ALEN);
1035 i++;
1036 }
1037 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1038
1039err:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001040 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001041 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1042 aq_ret, (u8 *)vfres, len);
1043
1044 kfree(vfres);
1045 return ret;
1046}
1047
1048/**
1049 * i40e_vc_reset_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001050 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001051 * @msg: pointer to the msg buffer
1052 * @msglen: msg length
1053 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001054 * called from the VF to reset itself,
1055 * unlike other virtchnl messages, PF driver
1056 * doesn't send the response back to the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001057 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001058static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001059{
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001060 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1061 i40e_reset_vf(vf, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001062}
1063
1064/**
1065 * i40e_vc_config_promiscuous_mode_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001066 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001067 * @msg: pointer to the msg buffer
1068 * @msglen: msg length
1069 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001070 * called from the VF to configure the promiscuous mode of
1071 * VF vsis
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001072 **/
1073static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1074 u8 *msg, u16 msglen)
1075{
1076 struct i40e_virtchnl_promisc_info *info =
1077 (struct i40e_virtchnl_promisc_info *)msg;
1078 struct i40e_pf *pf = vf->pf;
1079 struct i40e_hw *hw = &pf->hw;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001080 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001081 bool allmulti = false;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001082 i40e_status aq_ret;
1083
1084 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1085 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1086 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1087 (pf->vsi[info->vsi_id]->type != I40E_VSI_FCOE)) {
1088 aq_ret = I40E_ERR_PARAM;
1089 goto error_param;
1090 }
Ashish Shah89cb86c2014-08-01 13:27:10 -07001091 vsi = pf->vsi[info->vsi_id];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001092 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1093 allmulti = true;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001094 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001095 allmulti, NULL);
1096
1097error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001098 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001099 return i40e_vc_send_resp_to_vf(vf,
1100 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1101 aq_ret);
1102}
1103
1104/**
1105 * i40e_vc_config_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001106 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001107 * @msg: pointer to the msg buffer
1108 * @msglen: msg length
1109 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001110 * called from the VF to configure the rx/tx
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001111 * queues
1112 **/
1113static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1114{
1115 struct i40e_virtchnl_vsi_queue_config_info *qci =
1116 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1117 struct i40e_virtchnl_queue_pair_info *qpi;
Ashish Shah5f5e33b2014-07-10 07:58:15 +00001118 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001119 u16 vsi_id, vsi_queue_id;
1120 i40e_status aq_ret = 0;
1121 int i;
1122
1123 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1124 aq_ret = I40E_ERR_PARAM;
1125 goto error_param;
1126 }
1127
1128 vsi_id = qci->vsi_id;
1129 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1130 aq_ret = I40E_ERR_PARAM;
1131 goto error_param;
1132 }
1133 for (i = 0; i < qci->num_queue_pairs; i++) {
1134 qpi = &qci->qpair[i];
1135 vsi_queue_id = qpi->txq.queue_id;
1136 if ((qpi->txq.vsi_id != vsi_id) ||
1137 (qpi->rxq.vsi_id != vsi_id) ||
1138 (qpi->rxq.queue_id != vsi_queue_id) ||
1139 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1140 aq_ret = I40E_ERR_PARAM;
1141 goto error_param;
1142 }
1143
1144 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1145 &qpi->rxq) ||
1146 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1147 &qpi->txq)) {
1148 aq_ret = I40E_ERR_PARAM;
1149 goto error_param;
1150 }
1151 }
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001152 /* set vsi num_queue_pairs in use to num configured by VF */
Ashish Shah5f5e33b2014-07-10 07:58:15 +00001153 pf->vsi[vf->lan_vsi_index]->num_queue_pairs = qci->num_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001154
1155error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001156 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001157 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1158 aq_ret);
1159}
1160
1161/**
1162 * i40e_vc_config_irq_map_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001163 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001164 * @msg: pointer to the msg buffer
1165 * @msglen: msg length
1166 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001167 * called from the VF to configure the irq to
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001168 * queue map
1169 **/
1170static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1171{
1172 struct i40e_virtchnl_irq_map_info *irqmap_info =
1173 (struct i40e_virtchnl_irq_map_info *)msg;
1174 struct i40e_virtchnl_vector_map *map;
1175 u16 vsi_id, vsi_queue_id, vector_id;
1176 i40e_status aq_ret = 0;
1177 unsigned long tempmap;
1178 int i;
1179
1180 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1181 aq_ret = I40E_ERR_PARAM;
1182 goto error_param;
1183 }
1184
1185 for (i = 0; i < irqmap_info->num_vectors; i++) {
1186 map = &irqmap_info->vecmap[i];
1187
1188 vector_id = map->vector_id;
1189 vsi_id = map->vsi_id;
1190 /* validate msg params */
1191 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1192 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1193 aq_ret = I40E_ERR_PARAM;
1194 goto error_param;
1195 }
1196
1197 /* lookout for the invalid queue index */
1198 tempmap = map->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001199 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001200 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1201 vsi_queue_id)) {
1202 aq_ret = I40E_ERR_PARAM;
1203 goto error_param;
1204 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001205 }
1206
1207 tempmap = map->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001208 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001209 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1210 vsi_queue_id)) {
1211 aq_ret = I40E_ERR_PARAM;
1212 goto error_param;
1213 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001214 }
1215
1216 i40e_config_irq_link_list(vf, vsi_id, map);
1217 }
1218error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001219 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001220 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1221 aq_ret);
1222}
1223
1224/**
1225 * i40e_vc_enable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001226 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001227 * @msg: pointer to the msg buffer
1228 * @msglen: msg length
1229 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001230 * called from the VF to enable all or specific queue(s)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001231 **/
1232static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1233{
1234 struct i40e_virtchnl_queue_select *vqs =
1235 (struct i40e_virtchnl_queue_select *)msg;
1236 struct i40e_pf *pf = vf->pf;
1237 u16 vsi_id = vqs->vsi_id;
1238 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001239
1240 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1241 aq_ret = I40E_ERR_PARAM;
1242 goto error_param;
1243 }
1244
1245 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1246 aq_ret = I40E_ERR_PARAM;
1247 goto error_param;
1248 }
1249
1250 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1251 aq_ret = I40E_ERR_PARAM;
1252 goto error_param;
1253 }
Mitch Williams88f65632013-11-28 06:39:28 +00001254 if (i40e_vsi_control_rings(pf->vsi[vsi_id], true))
1255 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001256error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001257 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001258 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1259 aq_ret);
1260}
1261
1262/**
1263 * i40e_vc_disable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001264 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001265 * @msg: pointer to the msg buffer
1266 * @msglen: msg length
1267 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001268 * called from the VF to disable all or specific
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001269 * queue(s)
1270 **/
1271static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1272{
1273 struct i40e_virtchnl_queue_select *vqs =
1274 (struct i40e_virtchnl_queue_select *)msg;
1275 struct i40e_pf *pf = vf->pf;
1276 u16 vsi_id = vqs->vsi_id;
1277 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001278
1279 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1280 aq_ret = I40E_ERR_PARAM;
1281 goto error_param;
1282 }
1283
1284 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1285 aq_ret = I40E_ERR_PARAM;
1286 goto error_param;
1287 }
1288
1289 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1290 aq_ret = I40E_ERR_PARAM;
1291 goto error_param;
1292 }
Mitch Williams88f65632013-11-28 06:39:28 +00001293 if (i40e_vsi_control_rings(pf->vsi[vsi_id], false))
1294 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001295
1296error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001297 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001298 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1299 aq_ret);
1300}
1301
1302/**
1303 * i40e_vc_get_stats_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001304 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001305 * @msg: pointer to the msg buffer
1306 * @msglen: msg length
1307 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001308 * called from the VF to get vsi stats
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001309 **/
1310static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1311{
1312 struct i40e_virtchnl_queue_select *vqs =
1313 (struct i40e_virtchnl_queue_select *)msg;
1314 struct i40e_pf *pf = vf->pf;
1315 struct i40e_eth_stats stats;
1316 i40e_status aq_ret = 0;
1317 struct i40e_vsi *vsi;
1318
1319 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1320
1321 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1322 aq_ret = I40E_ERR_PARAM;
1323 goto error_param;
1324 }
1325
1326 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1327 aq_ret = I40E_ERR_PARAM;
1328 goto error_param;
1329 }
1330
1331 vsi = pf->vsi[vqs->vsi_id];
1332 if (!vsi) {
1333 aq_ret = I40E_ERR_PARAM;
1334 goto error_param;
1335 }
1336 i40e_update_eth_stats(vsi);
Mitch Williams5a9769c2013-11-28 06:39:38 +00001337 stats = vsi->eth_stats;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001338
1339error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001340 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001341 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1342 (u8 *)&stats, sizeof(stats));
1343}
1344
1345/**
Greg Rosef657a6e2013-11-28 06:39:42 +00001346 * i40e_check_vf_permission
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001347 * @vf: pointer to the VF info
Greg Rosef657a6e2013-11-28 06:39:42 +00001348 * @macaddr: pointer to the MAC Address being checked
1349 *
1350 * Check if the VF has permission to add or delete unicast MAC address
1351 * filters and return error code -EPERM if not. Then check if the
1352 * address filter requested is broadcast or zero and if so return
1353 * an invalid MAC address error code.
1354 **/
1355static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1356{
1357 struct i40e_pf *pf = vf->pf;
1358 int ret = 0;
1359
1360 if (is_broadcast_ether_addr(macaddr) ||
1361 is_zero_ether_addr(macaddr)) {
1362 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1363 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rose5017c2a2013-12-07 10:36:54 +00001364 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1365 !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001366 /* If the host VMM administrator has set the VF MAC address
1367 * administratively via the ndo_set_vf_mac command then deny
1368 * permission to the VF to add or delete unicast MAC addresses.
Greg Rose5017c2a2013-12-07 10:36:54 +00001369 * The VF may request to set the MAC address filter already
1370 * assigned to it so do not return an error in that case.
Greg Rosef657a6e2013-11-28 06:39:42 +00001371 */
1372 dev_err(&pf->pdev->dev,
1373 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1374 ret = -EPERM;
1375 }
1376 return ret;
1377}
1378
1379/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001380 * i40e_vc_add_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001381 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001382 * @msg: pointer to the msg buffer
1383 * @msglen: msg length
1384 *
1385 * add guest mac address filter
1386 **/
1387static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1388{
1389 struct i40e_virtchnl_ether_addr_list *al =
1390 (struct i40e_virtchnl_ether_addr_list *)msg;
1391 struct i40e_pf *pf = vf->pf;
1392 struct i40e_vsi *vsi = NULL;
1393 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001394 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001395 int i;
1396
1397 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1398 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1399 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001400 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001401 goto error_param;
1402 }
1403
1404 for (i = 0; i < al->num_elements; i++) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001405 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1406 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001407 goto error_param;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001408 }
1409 vsi = pf->vsi[vsi_id];
1410
1411 /* add new addresses to the list */
1412 for (i = 0; i < al->num_elements; i++) {
1413 struct i40e_mac_filter *f;
1414
1415 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
Mitch Williams7e68edf92013-11-16 10:00:41 +00001416 if (!f) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001417 if (i40e_is_vsi_in_vlan(vsi))
1418 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1419 true, false);
1420 else
1421 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1422 true, false);
1423 }
1424
1425 if (!f) {
1426 dev_err(&pf->pdev->dev,
1427 "Unable to add VF MAC filter\n");
Greg Rosef657a6e2013-11-28 06:39:42 +00001428 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001429 goto error_param;
1430 }
1431 }
1432
1433 /* program the updated filter list */
1434 if (i40e_sync_vsi_filters(vsi))
1435 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1436
1437error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001438 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001439 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001440 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001441}
1442
1443/**
1444 * i40e_vc_del_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001445 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001446 * @msg: pointer to the msg buffer
1447 * @msglen: msg length
1448 *
1449 * remove guest mac address filter
1450 **/
1451static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1452{
1453 struct i40e_virtchnl_ether_addr_list *al =
1454 (struct i40e_virtchnl_ether_addr_list *)msg;
1455 struct i40e_pf *pf = vf->pf;
1456 struct i40e_vsi *vsi = NULL;
1457 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001458 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001459 int i;
1460
1461 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1462 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1463 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001464 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001465 goto error_param;
1466 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001467
1468 for (i = 0; i < al->num_elements; i++) {
Mitch Williams700bbf62013-12-21 05:44:45 +00001469 if (is_broadcast_ether_addr(al->list[i].addr) ||
1470 is_zero_ether_addr(al->list[i].addr)) {
1471 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
1472 al->list[i].addr);
1473 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rosef657a6e2013-11-28 06:39:42 +00001474 goto error_param;
Mitch Williams700bbf62013-12-21 05:44:45 +00001475 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001476 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001477 vsi = pf->vsi[vsi_id];
1478
1479 /* delete addresses from the list */
1480 for (i = 0; i < al->num_elements; i++)
1481 i40e_del_filter(vsi, al->list[i].addr,
1482 I40E_VLAN_ANY, true, false);
1483
1484 /* program the updated filter list */
1485 if (i40e_sync_vsi_filters(vsi))
1486 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1487
1488error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001489 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001490 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001491 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001492}
1493
1494/**
1495 * i40e_vc_add_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001496 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001497 * @msg: pointer to the msg buffer
1498 * @msglen: msg length
1499 *
1500 * program guest vlan id
1501 **/
1502static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1503{
1504 struct i40e_virtchnl_vlan_filter_list *vfl =
1505 (struct i40e_virtchnl_vlan_filter_list *)msg;
1506 struct i40e_pf *pf = vf->pf;
1507 struct i40e_vsi *vsi = NULL;
1508 u16 vsi_id = vfl->vsi_id;
1509 i40e_status aq_ret = 0;
1510 int i;
1511
1512 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1513 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1514 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1515 aq_ret = I40E_ERR_PARAM;
1516 goto error_param;
1517 }
1518
1519 for (i = 0; i < vfl->num_elements; i++) {
1520 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1521 aq_ret = I40E_ERR_PARAM;
1522 dev_err(&pf->pdev->dev,
1523 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1524 goto error_param;
1525 }
1526 }
1527 vsi = pf->vsi[vsi_id];
1528 if (vsi->info.pvid) {
1529 aq_ret = I40E_ERR_PARAM;
1530 goto error_param;
1531 }
1532
1533 i40e_vlan_stripping_enable(vsi);
1534 for (i = 0; i < vfl->num_elements; i++) {
1535 /* add new VLAN filter */
1536 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1537 if (ret)
1538 dev_err(&pf->pdev->dev,
1539 "Unable to add VF vlan filter %d, error %d\n",
1540 vfl->vlan_id[i], ret);
1541 }
1542
1543error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001544 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001545 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1546}
1547
1548/**
1549 * i40e_vc_remove_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001550 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001551 * @msg: pointer to the msg buffer
1552 * @msglen: msg length
1553 *
1554 * remove programmed guest vlan id
1555 **/
1556static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1557{
1558 struct i40e_virtchnl_vlan_filter_list *vfl =
1559 (struct i40e_virtchnl_vlan_filter_list *)msg;
1560 struct i40e_pf *pf = vf->pf;
1561 struct i40e_vsi *vsi = NULL;
1562 u16 vsi_id = vfl->vsi_id;
1563 i40e_status aq_ret = 0;
1564 int i;
1565
1566 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1567 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1568 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1569 aq_ret = I40E_ERR_PARAM;
1570 goto error_param;
1571 }
1572
1573 for (i = 0; i < vfl->num_elements; i++) {
1574 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1575 aq_ret = I40E_ERR_PARAM;
1576 goto error_param;
1577 }
1578 }
1579
1580 vsi = pf->vsi[vsi_id];
1581 if (vsi->info.pvid) {
1582 aq_ret = I40E_ERR_PARAM;
1583 goto error_param;
1584 }
1585
1586 for (i = 0; i < vfl->num_elements; i++) {
1587 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1588 if (ret)
1589 dev_err(&pf->pdev->dev,
1590 "Unable to delete VF vlan filter %d, error %d\n",
1591 vfl->vlan_id[i], ret);
1592 }
1593
1594error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001595 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001596 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1597}
1598
1599/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001600 * i40e_vc_validate_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001601 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001602 * @msg: pointer to the msg buffer
1603 * @msglen: msg length
1604 * @msghndl: msg handle
1605 *
1606 * validate msg
1607 **/
1608static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1609 u32 v_retval, u8 *msg, u16 msglen)
1610{
1611 bool err_msg_format = false;
1612 int valid_len;
1613
1614 /* Check if VF is disabled. */
1615 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1616 return I40E_ERR_PARAM;
1617
1618 /* Validate message length. */
1619 switch (v_opcode) {
1620 case I40E_VIRTCHNL_OP_VERSION:
1621 valid_len = sizeof(struct i40e_virtchnl_version_info);
1622 break;
1623 case I40E_VIRTCHNL_OP_RESET_VF:
1624 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1625 valid_len = 0;
1626 break;
1627 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1628 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1629 break;
1630 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1631 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1632 break;
1633 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1634 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1635 if (msglen >= valid_len) {
1636 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1637 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1638 valid_len += (vqc->num_queue_pairs *
1639 sizeof(struct
1640 i40e_virtchnl_queue_pair_info));
1641 if (vqc->num_queue_pairs == 0)
1642 err_msg_format = true;
1643 }
1644 break;
1645 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1646 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1647 if (msglen >= valid_len) {
1648 struct i40e_virtchnl_irq_map_info *vimi =
1649 (struct i40e_virtchnl_irq_map_info *)msg;
1650 valid_len += (vimi->num_vectors *
1651 sizeof(struct i40e_virtchnl_vector_map));
1652 if (vimi->num_vectors == 0)
1653 err_msg_format = true;
1654 }
1655 break;
1656 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1657 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1658 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1659 break;
1660 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1661 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1662 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1663 if (msglen >= valid_len) {
1664 struct i40e_virtchnl_ether_addr_list *veal =
1665 (struct i40e_virtchnl_ether_addr_list *)msg;
1666 valid_len += veal->num_elements *
1667 sizeof(struct i40e_virtchnl_ether_addr);
1668 if (veal->num_elements == 0)
1669 err_msg_format = true;
1670 }
1671 break;
1672 case I40E_VIRTCHNL_OP_ADD_VLAN:
1673 case I40E_VIRTCHNL_OP_DEL_VLAN:
1674 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1675 if (msglen >= valid_len) {
1676 struct i40e_virtchnl_vlan_filter_list *vfl =
1677 (struct i40e_virtchnl_vlan_filter_list *)msg;
1678 valid_len += vfl->num_elements * sizeof(u16);
1679 if (vfl->num_elements == 0)
1680 err_msg_format = true;
1681 }
1682 break;
1683 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1684 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1685 break;
1686 case I40E_VIRTCHNL_OP_GET_STATS:
1687 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1688 break;
1689 /* These are always errors coming from the VF. */
1690 case I40E_VIRTCHNL_OP_EVENT:
1691 case I40E_VIRTCHNL_OP_UNKNOWN:
1692 default:
1693 return -EPERM;
1694 break;
1695 }
1696 /* few more checks */
1697 if ((valid_len != msglen) || (err_msg_format)) {
1698 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1699 return -EINVAL;
1700 } else {
1701 return 0;
1702 }
1703}
1704
1705/**
1706 * i40e_vc_process_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001707 * @pf: pointer to the PF structure
1708 * @vf_id: source VF id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001709 * @msg: pointer to the msg buffer
1710 * @msglen: msg length
1711 * @msghndl: msg handle
1712 *
1713 * called from the common aeq/arq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001714 * process request from VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001715 **/
1716int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1717 u32 v_retval, u8 *msg, u16 msglen)
1718{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001719 struct i40e_hw *hw = &pf->hw;
Dan Carpenterc243e962014-01-15 06:43:39 +00001720 unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001721 struct i40e_vf *vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001722 int ret;
1723
1724 pf->vf_aq_requests++;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001725 if (local_vf_id >= pf->num_alloc_vfs)
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001726 return -EINVAL;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001727 vf = &(pf->vf[local_vf_id]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001728 /* perform basic checks on the msg */
1729 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1730
1731 if (ret) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001732 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001733 local_vf_id, v_opcode, msglen);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001734 return ret;
1735 }
Mitch Williamsbae3cae2014-01-14 00:49:49 -08001736
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001737 switch (v_opcode) {
1738 case I40E_VIRTCHNL_OP_VERSION:
1739 ret = i40e_vc_get_version_msg(vf);
1740 break;
1741 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1742 ret = i40e_vc_get_vf_resources_msg(vf);
1743 break;
1744 case I40E_VIRTCHNL_OP_RESET_VF:
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001745 i40e_vc_reset_vf_msg(vf);
1746 ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001747 break;
1748 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1749 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1750 break;
1751 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1752 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1753 break;
1754 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1755 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1756 break;
1757 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1758 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
1759 break;
1760 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1761 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1762 break;
1763 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1764 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1765 break;
1766 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1767 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1768 break;
1769 case I40E_VIRTCHNL_OP_ADD_VLAN:
1770 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1771 break;
1772 case I40E_VIRTCHNL_OP_DEL_VLAN:
1773 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1774 break;
1775 case I40E_VIRTCHNL_OP_GET_STATS:
1776 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1777 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001778 case I40E_VIRTCHNL_OP_UNKNOWN:
1779 default:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001780 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001781 v_opcode, local_vf_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001782 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1783 I40E_ERR_NOT_IMPLEMENTED);
1784 break;
1785 }
1786
1787 return ret;
1788}
1789
1790/**
1791 * i40e_vc_process_vflr_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001792 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001793 *
1794 * called from the vlfr irq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001795 * free up VF resources and state variables
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001796 **/
1797int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1798{
1799 u32 reg, reg_idx, bit_idx, vf_id;
1800 struct i40e_hw *hw = &pf->hw;
1801 struct i40e_vf *vf;
1802
1803 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1804 return 0;
1805
Mitch Williamsc5c2f7c2014-11-11 03:15:04 +00001806 /* re-enable vflr interrupt cause */
1807 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1808 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1809 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1810 i40e_flush(hw);
1811
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001812 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1813 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1814 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1815 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001816 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001817 vf = &pf->vf[vf_id];
1818 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
1819 if (reg & (1 << bit_idx)) {
1820 /* clear the bit in GLGEN_VFLRSTAT */
1821 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
1822
Mitch Williamseb2d80b2014-02-13 03:48:48 -08001823 if (!test_bit(__I40E_DOWN, &pf->state))
1824 i40e_reset_vf(vf, true);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001825 }
1826 }
1827
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001828 return 0;
1829}
1830
1831/**
1832 * i40e_vc_vf_broadcast
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001833 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001834 * @opcode: operation code
1835 * @retval: return value
1836 * @msg: pointer to the msg buffer
1837 * @msglen: msg length
1838 *
1839 * send a message to all VFs on a given PF
1840 **/
1841static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
1842 enum i40e_virtchnl_ops v_opcode,
1843 i40e_status v_retval, u8 *msg,
1844 u16 msglen)
1845{
1846 struct i40e_hw *hw = &pf->hw;
1847 struct i40e_vf *vf = pf->vf;
1848 int i;
1849
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001850 for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
1851 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001852 /* Not all VFs are enabled so skip the ones that are not */
Anjali Singhai Jain478c9e72014-07-10 07:58:18 +00001853 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
1854 !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1855 continue;
1856
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001857 /* Ignore return value on purpose - a given VF may fail, but
1858 * we need to keep going and send to all of them
1859 */
Ashish Shahf19efbb2014-08-01 13:27:06 -07001860 i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001861 msg, msglen, NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001862 }
1863}
1864
1865/**
1866 * i40e_vc_notify_link_state
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001867 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001868 *
1869 * send a link status message to all VFs on a given PF
1870 **/
1871void i40e_vc_notify_link_state(struct i40e_pf *pf)
1872{
1873 struct i40e_virtchnl_pf_event pfe;
Mitch Williams588aefa2014-02-11 08:27:49 +00001874 struct i40e_hw *hw = &pf->hw;
1875 struct i40e_vf *vf = pf->vf;
1876 struct i40e_link_status *ls = &pf->hw.phy.link_info;
1877 int i;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001878
1879 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
1880 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001881 for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
1882 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00001883 if (vf->link_forced) {
1884 pfe.event_data.link_event.link_status = vf->link_up;
1885 pfe.event_data.link_event.link_speed =
1886 (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
1887 } else {
1888 pfe.event_data.link_event.link_status =
1889 ls->link_info & I40E_AQ_LINK_UP;
1890 pfe.event_data.link_event.link_speed = ls->link_speed;
1891 }
Ashish Shahf19efbb2014-08-01 13:27:06 -07001892 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Mitch Williams588aefa2014-02-11 08:27:49 +00001893 0, (u8 *)&pfe, sizeof(pfe),
1894 NULL);
Mitch Williams588aefa2014-02-11 08:27:49 +00001895 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001896}
1897
1898/**
1899 * i40e_vc_notify_reset
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001900 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001901 *
1902 * indicate a pending reset to all VFs on a given PF
1903 **/
1904void i40e_vc_notify_reset(struct i40e_pf *pf)
1905{
1906 struct i40e_virtchnl_pf_event pfe;
1907
1908 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1909 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
1910 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
1911 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
1912}
1913
1914/**
1915 * i40e_vc_notify_vf_reset
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001916 * @vf: pointer to the VF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001917 *
1918 * indicate a pending reset to the given VF
1919 **/
1920void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
1921{
1922 struct i40e_virtchnl_pf_event pfe;
Anjali Singhai Jain478c9e72014-07-10 07:58:18 +00001923 int abs_vf_id;
1924
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001925 /* validate the request */
1926 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1927 return;
1928
Anjali Singhai Jain478c9e72014-07-10 07:58:18 +00001929 /* verify if the VF is in either init or active before proceeding */
1930 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
1931 !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1932 return;
1933
1934 abs_vf_id = vf->vf_id + vf->pf->hw.func_caps.vf_base_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001935
1936 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1937 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
Ashish Shahf19efbb2014-08-01 13:27:06 -07001938 i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001939 I40E_SUCCESS, (u8 *)&pfe,
1940 sizeof(struct i40e_virtchnl_pf_event), NULL);
1941}
1942
1943/**
1944 * i40e_ndo_set_vf_mac
1945 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001946 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001947 * @mac: mac address
1948 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001949 * program VF mac address
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001950 **/
1951int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
1952{
1953 struct i40e_netdev_priv *np = netdev_priv(netdev);
1954 struct i40e_vsi *vsi = np->vsi;
1955 struct i40e_pf *pf = vsi->back;
1956 struct i40e_mac_filter *f;
1957 struct i40e_vf *vf;
1958 int ret = 0;
1959
1960 /* validate the request */
1961 if (vf_id >= pf->num_alloc_vfs) {
1962 dev_err(&pf->pdev->dev,
1963 "Invalid VF Identifier %d\n", vf_id);
1964 ret = -EINVAL;
1965 goto error_param;
1966 }
1967
1968 vf = &(pf->vf[vf_id]);
1969 vsi = pf->vsi[vf->lan_vsi_index];
1970 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1971 dev_err(&pf->pdev->dev,
1972 "Uninitialized VF %d\n", vf_id);
1973 ret = -EINVAL;
1974 goto error_param;
1975 }
1976
1977 if (!is_valid_ether_addr(mac)) {
1978 dev_err(&pf->pdev->dev,
1979 "Invalid VF ethernet address\n");
1980 ret = -EINVAL;
1981 goto error_param;
1982 }
1983
1984 /* delete the temporary mac address */
Greg Rose37cc0d22014-04-01 07:11:44 +00001985 i40e_del_filter(vsi, vf->default_lan_addr.addr, vf->port_vlan_id,
1986 true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001987
Greg Rose29f71bb2014-05-20 08:01:45 +00001988 /* Delete all the filters for this VSI - we're going to kill it
1989 * anyway.
1990 */
1991 list_for_each_entry(f, &vsi->mac_filter_list, list)
1992 i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001993
1994 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
1995 /* program mac filter */
1996 if (i40e_sync_vsi_filters(vsi)) {
1997 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
1998 ret = -EIO;
1999 goto error_param;
2000 }
Greg Rose9a173902014-05-22 06:32:02 +00002001 ether_addr_copy(vf->default_lan_addr.addr, mac);
Greg Rosef657a6e2013-11-28 06:39:42 +00002002 vf->pf_set_mac = true;
Greg Rose17413a82014-06-04 01:23:13 +00002003 /* Force the VF driver stop so it has to reload with new MAC address */
2004 i40e_vc_disable_vf(pf, vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002005 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002006
2007error_param:
2008 return ret;
2009}
2010
2011/**
2012 * i40e_ndo_set_vf_port_vlan
2013 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002014 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002015 * @vlan_id: mac address
2016 * @qos: priority setting
2017 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002018 * program VF vlan id and/or qos
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002019 **/
2020int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2021 int vf_id, u16 vlan_id, u8 qos)
2022{
2023 struct i40e_netdev_priv *np = netdev_priv(netdev);
2024 struct i40e_pf *pf = np->vsi->back;
2025 struct i40e_vsi *vsi;
2026 struct i40e_vf *vf;
2027 int ret = 0;
2028
2029 /* validate the request */
2030 if (vf_id >= pf->num_alloc_vfs) {
2031 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2032 ret = -EINVAL;
2033 goto error_pvid;
2034 }
2035
2036 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2037 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2038 ret = -EINVAL;
2039 goto error_pvid;
2040 }
2041
2042 vf = &(pf->vf[vf_id]);
2043 vsi = pf->vsi[vf->lan_vsi_index];
2044 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2045 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2046 ret = -EINVAL;
2047 goto error_pvid;
2048 }
2049
Greg Rosef9b4b622014-03-06 09:02:28 +00002050 if (vsi->info.pvid == 0 && i40e_is_vsi_in_vlan(vsi)) {
Greg Rose99a49732014-01-13 16:13:03 -08002051 dev_err(&pf->pdev->dev,
2052 "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",
2053 vf_id);
Greg Rosef9b4b622014-03-06 09:02:28 +00002054 /* Administrator Error - knock the VF offline until he does
2055 * the right thing by reconfiguring his network correctly
2056 * and then reloading the VF driver.
2057 */
2058 i40e_vc_disable_vf(pf, vf);
2059 }
Greg Rose99a49732014-01-13 16:13:03 -08002060
Greg Rose8d82a7c2014-01-13 16:13:04 -08002061 /* Check for condition where there was already a port VLAN ID
2062 * filter set and now it is being deleted by setting it to zero.
Greg Rose1315f7c2014-03-14 07:32:20 +00002063 * Additionally check for the condition where there was a port
2064 * VLAN but now there is a new and different port VLAN being set.
Greg Rose8d82a7c2014-01-13 16:13:04 -08002065 * Before deleting all the old VLAN filters we must add new ones
2066 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2067 * MAC addresses deleted.
2068 */
Greg Rose1315f7c2014-03-14 07:32:20 +00002069 if ((!(vlan_id || qos) ||
2070 (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
2071 vsi->info.pvid)
Greg Rose8d82a7c2014-01-13 16:13:04 -08002072 ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2073
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002074 if (vsi->info.pvid) {
2075 /* kill old VLAN */
2076 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2077 VLAN_VID_MASK));
2078 if (ret) {
2079 dev_info(&vsi->back->pdev->dev,
2080 "remove VLAN failed, ret=%d, aq_err=%d\n",
2081 ret, pf->hw.aq.asq_last_status);
2082 }
2083 }
2084 if (vlan_id || qos)
2085 ret = i40e_vsi_add_pvid(vsi,
2086 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
2087 else
Greg Rose6c12fcb2013-11-28 06:39:34 +00002088 i40e_vsi_remove_pvid(vsi);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002089
2090 if (vlan_id) {
2091 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2092 vlan_id, qos, vf_id);
2093
2094 /* add new VLAN filter */
2095 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2096 if (ret) {
2097 dev_info(&vsi->back->pdev->dev,
2098 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2099 vsi->back->hw.aq.asq_last_status);
2100 goto error_pvid;
2101 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002102 /* Kill non-vlan MAC filters - ignore error return since
2103 * there might not be any non-vlan MAC filters.
2104 */
2105 i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002106 }
2107
2108 if (ret) {
2109 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2110 goto error_pvid;
2111 }
Greg Rose6c12fcb2013-11-28 06:39:34 +00002112 /* The Port VLAN needs to be saved across resets the same as the
2113 * default LAN MAC address.
2114 */
2115 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002116 ret = 0;
2117
2118error_pvid:
2119 return ret;
2120}
2121
Mitch Williams84590fd2014-04-09 05:58:57 +00002122#define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
2123#define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002124/**
2125 * i40e_ndo_set_vf_bw
2126 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002127 * @vf_id: VF identifier
2128 * @tx_rate: Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002129 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002130 * configure VF Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002131 **/
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002132int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2133 int max_tx_rate)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002134{
Mitch Williams6b192892014-03-06 09:02:29 +00002135 struct i40e_netdev_priv *np = netdev_priv(netdev);
2136 struct i40e_pf *pf = np->vsi->back;
2137 struct i40e_vsi *vsi;
2138 struct i40e_vf *vf;
2139 int speed = 0;
2140 int ret = 0;
2141
2142 /* validate the request */
2143 if (vf_id >= pf->num_alloc_vfs) {
2144 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2145 ret = -EINVAL;
2146 goto error;
2147 }
2148
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002149 if (min_tx_rate) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002150 dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002151 min_tx_rate, vf_id);
2152 return -EINVAL;
2153 }
2154
Mitch Williams6b192892014-03-06 09:02:29 +00002155 vf = &(pf->vf[vf_id]);
2156 vsi = pf->vsi[vf->lan_vsi_index];
2157 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2158 dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
2159 ret = -EINVAL;
2160 goto error;
2161 }
2162
2163 switch (pf->hw.phy.link_info.link_speed) {
2164 case I40E_LINK_SPEED_40GB:
2165 speed = 40000;
2166 break;
2167 case I40E_LINK_SPEED_10GB:
2168 speed = 10000;
2169 break;
2170 case I40E_LINK_SPEED_1GB:
2171 speed = 1000;
2172 break;
2173 default:
2174 break;
2175 }
2176
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002177 if (max_tx_rate > speed) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002178 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002179 max_tx_rate, vf->vf_id);
Mitch Williams6b192892014-03-06 09:02:29 +00002180 ret = -EINVAL;
2181 goto error;
2182 }
2183
Mitch Williamsdac9b312014-04-09 05:58:56 +00002184 if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2185 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2186 max_tx_rate = 50;
2187 }
2188
Mitch Williams6b192892014-03-06 09:02:29 +00002189 /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
Mitch Williams84590fd2014-04-09 05:58:57 +00002190 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2191 max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2192 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
Mitch Williams6b192892014-03-06 09:02:29 +00002193 if (ret) {
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002194 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
Mitch Williams6b192892014-03-06 09:02:29 +00002195 ret);
2196 ret = -EIO;
2197 goto error;
2198 }
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002199 vf->tx_rate = max_tx_rate;
Mitch Williams6b192892014-03-06 09:02:29 +00002200error:
2201 return ret;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002202}
2203
2204/**
2205 * i40e_ndo_get_vf_config
2206 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002207 * @vf_id: VF identifier
2208 * @ivi: VF configuration structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002209 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002210 * return VF configuration
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002211 **/
2212int i40e_ndo_get_vf_config(struct net_device *netdev,
2213 int vf_id, struct ifla_vf_info *ivi)
2214{
2215 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002216 struct i40e_vsi *vsi = np->vsi;
2217 struct i40e_pf *pf = vsi->back;
2218 struct i40e_vf *vf;
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_param;
2226 }
2227
2228 vf = &(pf->vf[vf_id]);
2229 /* first vsi is always the LAN vsi */
2230 vsi = pf->vsi[vf->lan_vsi_index];
2231 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2232 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2233 ret = -EINVAL;
2234 goto error_param;
2235 }
2236
2237 ivi->vf = vf_id;
2238
Mitch Williamsf4a1c5c2013-11-28 06:39:34 +00002239 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002240
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002241 ivi->max_tx_rate = vf->tx_rate;
2242 ivi->min_tx_rate = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002243 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2244 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2245 I40E_VLAN_PRIORITY_SHIFT;
Mitch Williams84ca55a2014-03-14 07:32:24 +00002246 if (vf->link_forced == false)
2247 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2248 else if (vf->link_up == true)
2249 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2250 else
2251 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
Mitch Williamsc674d122014-05-20 08:01:40 +00002252 ivi->spoofchk = vf->spoofchk;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002253 ret = 0;
2254
2255error_param:
2256 return ret;
2257}
Mitch Williams588aefa2014-02-11 08:27:49 +00002258
2259/**
2260 * i40e_ndo_set_vf_link_state
2261 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002262 * @vf_id: VF identifier
Mitch Williams588aefa2014-02-11 08:27:49 +00002263 * @link: required link state
2264 *
2265 * Set the link state of a specified VF, regardless of physical link state
2266 **/
2267int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
2268{
2269 struct i40e_netdev_priv *np = netdev_priv(netdev);
2270 struct i40e_pf *pf = np->vsi->back;
2271 struct i40e_virtchnl_pf_event pfe;
2272 struct i40e_hw *hw = &pf->hw;
2273 struct i40e_vf *vf;
Ashish Shahf19efbb2014-08-01 13:27:06 -07002274 int abs_vf_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002275 int ret = 0;
2276
2277 /* validate the request */
2278 if (vf_id >= pf->num_alloc_vfs) {
2279 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2280 ret = -EINVAL;
2281 goto error_out;
2282 }
2283
2284 vf = &pf->vf[vf_id];
Ashish Shahf19efbb2014-08-01 13:27:06 -07002285 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002286
2287 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
2288 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
2289
2290 switch (link) {
2291 case IFLA_VF_LINK_STATE_AUTO:
2292 vf->link_forced = false;
2293 pfe.event_data.link_event.link_status =
2294 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
2295 pfe.event_data.link_event.link_speed =
2296 pf->hw.phy.link_info.link_speed;
2297 break;
2298 case IFLA_VF_LINK_STATE_ENABLE:
2299 vf->link_forced = true;
2300 vf->link_up = true;
2301 pfe.event_data.link_event.link_status = true;
2302 pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
2303 break;
2304 case IFLA_VF_LINK_STATE_DISABLE:
2305 vf->link_forced = true;
2306 vf->link_up = false;
2307 pfe.event_data.link_event.link_status = false;
2308 pfe.event_data.link_event.link_speed = 0;
2309 break;
2310 default:
2311 ret = -EINVAL;
2312 goto error_out;
2313 }
2314 /* Notify the VF of its new link state */
Ashish Shahf19efbb2014-08-01 13:27:06 -07002315 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Mitch Williams588aefa2014-02-11 08:27:49 +00002316 0, (u8 *)&pfe, sizeof(pfe), NULL);
2317
2318error_out:
2319 return ret;
2320}
Mitch Williamsc674d122014-05-20 08:01:40 +00002321
2322/**
2323 * i40e_ndo_set_vf_spoofchk
2324 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002325 * @vf_id: VF identifier
Mitch Williamsc674d122014-05-20 08:01:40 +00002326 * @enable: flag to enable or disable feature
2327 *
2328 * Enable or disable VF spoof checking
2329 **/
Serey Konge6d90042014-07-12 07:28:14 +00002330int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
Mitch Williamsc674d122014-05-20 08:01:40 +00002331{
2332 struct i40e_netdev_priv *np = netdev_priv(netdev);
2333 struct i40e_vsi *vsi = np->vsi;
2334 struct i40e_pf *pf = vsi->back;
2335 struct i40e_vsi_context ctxt;
2336 struct i40e_hw *hw = &pf->hw;
2337 struct i40e_vf *vf;
2338 int ret = 0;
2339
2340 /* validate the request */
2341 if (vf_id >= pf->num_alloc_vfs) {
2342 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2343 ret = -EINVAL;
2344 goto out;
2345 }
2346
2347 vf = &(pf->vf[vf_id]);
2348
2349 if (enable == vf->spoofchk)
2350 goto out;
2351
2352 vf->spoofchk = enable;
2353 memset(&ctxt, 0, sizeof(ctxt));
2354 ctxt.seid = pf->vsi[vf->lan_vsi_index]->seid;
2355 ctxt.pf_num = pf->hw.pf_id;
2356 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
2357 if (enable)
Greg Rose30d71af2015-01-29 07:17:17 +00002358 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
2359 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
Mitch Williamsc674d122014-05-20 08:01:40 +00002360 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
2361 if (ret) {
2362 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
2363 ret);
2364 ret = -EIO;
2365 }
2366out:
2367 return ret;
2368}