blob: 4d69e1f04901553d4efe7a9e5357c1c8229235c8 [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 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -070056static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000057{
58 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -070059 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000060
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -070061 return (vsi && (vsi->vf_id == vf->vf_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000062}
63
64/**
65 * i40e_vc_isvalid_queue_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000066 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000067 * @vsi_id: vsi id
68 * @qid: vsi relative queue id
69 *
70 * check for the valid queue id
71 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -070072static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000073 u8 qid)
74{
75 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -070076 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000077
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -070078 return (vsi && (qid < vsi->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
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700100 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000101 * @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 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700105static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000106 u8 vsi_queue_id)
107{
108 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700109 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000110 u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
111
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700112 if (!vsi)
113 return pf_queue_id;
114
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000115 if (le16_to_cpu(vsi->info.mapping_flags) &
116 I40E_AQ_VSI_QUE_MAP_NONCONTIG)
117 pf_queue_id =
118 le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
119 else
120 pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
121 vsi_queue_id;
122
123 return pf_queue_id;
124}
125
126/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000127 * i40e_config_irq_link_list
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000128 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700129 * @vsi_id: id of VSI as given by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000130 * @vecmap: irq map info
131 *
132 * configure irq link list from the map
133 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700134static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000135 struct i40e_virtchnl_vector_map *vecmap)
136{
137 unsigned long linklistmap = 0, tempmap;
138 struct i40e_pf *pf = vf->pf;
139 struct i40e_hw *hw = &pf->hw;
140 u16 vsi_queue_id, pf_queue_id;
141 enum i40e_queue_type qtype;
142 u16 next_q, vector_id;
143 u32 reg, reg_idx;
144 u16 itr_idx = 0;
145
146 vector_id = vecmap->vector_id;
147 /* setup the head */
148 if (0 == vector_id)
149 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
150 else
151 reg_idx = I40E_VPINT_LNKLSTN(
Mitch Williams9347eb72014-02-11 08:26:32 +0000152 ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
153 (vector_id - 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000154
155 if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
156 /* Special case - No queues mapped on this vector */
157 wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
158 goto irq_list_done;
159 }
160 tempmap = vecmap->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000161 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000162 linklistmap |= (1 <<
163 (I40E_VIRTCHNL_SUPPORTED_QTYPES *
164 vsi_queue_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000165 }
166
167 tempmap = vecmap->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000168 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000169 linklistmap |= (1 <<
170 (I40E_VIRTCHNL_SUPPORTED_QTYPES * vsi_queue_id
171 + 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000172 }
173
174 next_q = find_first_bit(&linklistmap,
175 (I40E_MAX_VSI_QP *
176 I40E_VIRTCHNL_SUPPORTED_QTYPES));
177 vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
178 qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700179 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000180 reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
181
182 wr32(hw, reg_idx, reg);
183
184 while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
185 switch (qtype) {
186 case I40E_QUEUE_TYPE_RX:
187 reg_idx = I40E_QINT_RQCTL(pf_queue_id);
188 itr_idx = vecmap->rxitr_idx;
189 break;
190 case I40E_QUEUE_TYPE_TX:
191 reg_idx = I40E_QINT_TQCTL(pf_queue_id);
192 itr_idx = vecmap->txitr_idx;
193 break;
194 default:
195 break;
196 }
197
198 next_q = find_next_bit(&linklistmap,
199 (I40E_MAX_VSI_QP *
200 I40E_VIRTCHNL_SUPPORTED_QTYPES),
201 next_q + 1);
Mitch Williams829af3a2013-12-18 13:46:00 +0000202 if (next_q <
203 (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000204 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
205 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700206 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000207 vsi_queue_id);
208 } else {
209 pf_queue_id = I40E_QUEUE_END_OF_LIST;
210 qtype = 0;
211 }
212
213 /* format for the RQCTL & TQCTL regs is same */
214 reg = (vector_id) |
215 (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
216 (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
217 (1 << I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
218 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
219 wr32(hw, reg_idx, reg);
220 }
221
222irq_list_done:
223 i40e_flush(hw);
224}
225
226/**
227 * i40e_config_vsi_tx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000228 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700229 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000230 * @vsi_queue_id: vsi relative queue index
231 * @info: config. info
232 *
233 * configure tx queue
234 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700235static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000236 u16 vsi_queue_id,
237 struct i40e_virtchnl_txq_info *info)
238{
239 struct i40e_pf *pf = vf->pf;
240 struct i40e_hw *hw = &pf->hw;
241 struct i40e_hmc_obj_txq tx_ctx;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700242 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000243 u16 pf_queue_id;
244 u32 qtx_ctl;
245 int ret = 0;
246
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700247 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
248 vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000249
250 /* clear the context structure first */
251 memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
252
253 /* only set the required fields */
254 tx_ctx.base = info->dma_ring_addr / 128;
255 tx_ctx.qlen = info->ring_len;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700256 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000257 tx_ctx.rdylist_act = 0;
Ashish Shah5d298962014-05-22 06:31:25 +0000258 tx_ctx.head_wb_ena = info->headwb_enabled;
259 tx_ctx.head_wb_addr = info->dma_headwb_addr;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000260
261 /* clear the context in the HMC */
262 ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
263 if (ret) {
264 dev_err(&pf->pdev->dev,
265 "Failed to clear VF LAN Tx queue context %d, error: %d\n",
266 pf_queue_id, ret);
267 ret = -ENOENT;
268 goto error_context;
269 }
270
271 /* set the context in the HMC */
272 ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
273 if (ret) {
274 dev_err(&pf->pdev->dev,
275 "Failed to set VF LAN Tx queue context %d error: %d\n",
276 pf_queue_id, ret);
277 ret = -ENOENT;
278 goto error_context;
279 }
280
281 /* associate this queue with the PCI VF function */
282 qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +0000283 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000284 & I40E_QTX_CTL_PF_INDX_MASK);
285 qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
286 << I40E_QTX_CTL_VFVM_INDX_SHIFT)
287 & I40E_QTX_CTL_VFVM_INDX_MASK);
288 wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
289 i40e_flush(hw);
290
291error_context:
292 return ret;
293}
294
295/**
296 * i40e_config_vsi_rx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000297 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700298 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000299 * @vsi_queue_id: vsi relative queue index
300 * @info: config. info
301 *
302 * configure rx queue
303 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700304static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000305 u16 vsi_queue_id,
306 struct i40e_virtchnl_rxq_info *info)
307{
308 struct i40e_pf *pf = vf->pf;
309 struct i40e_hw *hw = &pf->hw;
310 struct i40e_hmc_obj_rxq rx_ctx;
311 u16 pf_queue_id;
312 int ret = 0;
313
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700314 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000315
316 /* clear the context structure first */
317 memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
318
319 /* only set the required fields */
320 rx_ctx.base = info->dma_ring_addr / 128;
321 rx_ctx.qlen = info->ring_len;
322
323 if (info->splithdr_enabled) {
324 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
325 I40E_RX_SPLIT_IP |
326 I40E_RX_SPLIT_TCP_UDP |
327 I40E_RX_SPLIT_SCTP;
328 /* header length validation */
329 if (info->hdr_size > ((2 * 1024) - 64)) {
330 ret = -EINVAL;
331 goto error_param;
332 }
333 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
334
335 /* set splitalways mode 10b */
336 rx_ctx.dtype = 0x2;
337 }
338
339 /* databuffer length validation */
340 if (info->databuffer_size > ((16 * 1024) - 128)) {
341 ret = -EINVAL;
342 goto error_param;
343 }
344 rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
345
346 /* max pkt. length validation */
347 if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
348 ret = -EINVAL;
349 goto error_param;
350 }
351 rx_ctx.rxmax = info->max_pkt_size;
352
353 /* enable 32bytes desc always */
354 rx_ctx.dsize = 1;
355
356 /* default values */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000357 rx_ctx.lrxqthresh = 2;
358 rx_ctx.crcstrip = 1;
Mitch Williams50d41652014-04-09 05:58:55 +0000359 rx_ctx.prefena = 1;
Shannon Nelsonc1d11ce2014-07-29 04:01:03 +0000360 rx_ctx.l2tsel = 1;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000361
362 /* clear the context in the HMC */
363 ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
364 if (ret) {
365 dev_err(&pf->pdev->dev,
366 "Failed to clear VF LAN Rx queue context %d, error: %d\n",
367 pf_queue_id, ret);
368 ret = -ENOENT;
369 goto error_param;
370 }
371
372 /* set the context in the HMC */
373 ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
374 if (ret) {
375 dev_err(&pf->pdev->dev,
376 "Failed to set VF LAN Rx queue context %d error: %d\n",
377 pf_queue_id, ret);
378 ret = -ENOENT;
379 goto error_param;
380 }
381
382error_param:
383 return ret;
384}
385
386/**
387 * i40e_alloc_vsi_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000388 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000389 * @type: type of VSI to allocate
390 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000391 * alloc VF vsi context & resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000392 **/
393static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
394{
395 struct i40e_mac_filter *f = NULL;
396 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000397 struct i40e_vsi *vsi;
398 int ret = 0;
399
400 vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
401
402 if (!vsi) {
403 dev_err(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000404 "add vsi failed for VF %d, aq_err %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000405 vf->vf_id, pf->hw.aq.asq_last_status);
406 ret = -ENOENT;
407 goto error_alloc_vsi_res;
408 }
409 if (type == I40E_VSI_SRIOV) {
Greg Rose1a103702013-11-28 06:42:39 +0000410 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700411 vf->lan_vsi_idx = vsi->idx;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000412 vf->lan_vsi_id = vsi->id;
Greg Rose6c12fcb2013-11-28 06:39:34 +0000413 /* If the port VLAN has been configured and then the
414 * VF driver was removed then the VSI port VLAN
415 * configuration was destroyed. Check if there is
416 * a port VLAN and restore the VSI configuration if
417 * needed.
418 */
419 if (vf->port_vlan_id)
420 i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000421 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
Greg Rose6c12fcb2013-11-28 06:39:34 +0000422 vf->port_vlan_id, true, false);
Greg Rose1a103702013-11-28 06:42:39 +0000423 if (!f)
424 dev_info(&pf->pdev->dev,
425 "Could not allocate VF MAC addr\n");
426 f = i40e_add_filter(vsi, brdcast, vf->port_vlan_id,
427 true, false);
428 if (!f)
429 dev_info(&pf->pdev->dev,
430 "Could not allocate VF broadcast filter\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000431 }
Neerav Parikh6dbbbfb2013-11-26 10:49:24 +0000432
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000433 /* program mac filter */
434 ret = i40e_sync_vsi_filters(vsi);
Mitch Williamsfd1646e2014-02-13 03:48:44 -0800435 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000436 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000437
Mitch Williams6b192892014-03-06 09:02:29 +0000438 /* Set VF bandwidth if specified */
439 if (vf->tx_rate) {
440 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
441 vf->tx_rate / 50, 0, NULL);
442 if (ret)
443 dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
444 vf->vf_id, ret);
445 }
446
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000447error_alloc_vsi_res:
448 return ret;
449}
450
451/**
Mitch Williams805bd5b2013-11-28 06:39:26 +0000452 * i40e_enable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000453 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000454 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000455 * enable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000456 **/
457static void i40e_enable_vf_mappings(struct i40e_vf *vf)
458{
459 struct i40e_pf *pf = vf->pf;
460 struct i40e_hw *hw = &pf->hw;
461 u32 reg, total_queue_pairs = 0;
462 int j;
463
464 /* Tell the hardware we're using noncontiguous mapping. HW requires
465 * that VF queues be mapped using this method, even when they are
466 * contiguous in real life
467 */
468 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
469 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
470
471 /* enable VF vplan_qtable mappings */
472 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
473 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
474
475 /* map PF queues to VF queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700476 for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
477 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
Mitch Williams805bd5b2013-11-28 06:39:26 +0000478 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
479 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
480 total_queue_pairs++;
481 }
482
483 /* map PF queues to VSI */
484 for (j = 0; j < 7; j++) {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700485 if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
Mitch Williams805bd5b2013-11-28 06:39:26 +0000486 reg = 0x07FF07FF; /* unused */
487 } else {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700488 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000489 j * 2);
490 reg = qid;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700491 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000492 (j * 2) + 1);
493 reg |= qid << 16;
494 }
495 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
496 }
497
498 i40e_flush(hw);
499}
500
501/**
502 * i40e_disable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000503 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000504 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000505 * disable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000506 **/
507static void i40e_disable_vf_mappings(struct i40e_vf *vf)
508{
509 struct i40e_pf *pf = vf->pf;
510 struct i40e_hw *hw = &pf->hw;
511 int i;
512
513 /* disable qp mappings */
514 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
515 for (i = 0; i < I40E_MAX_VSI_QP; i++)
516 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
517 I40E_QUEUE_END_OF_LIST);
518 i40e_flush(hw);
519}
520
521/**
522 * i40e_free_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000523 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000524 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000525 * free VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000526 **/
527static void i40e_free_vf_res(struct i40e_vf *vf)
528{
529 struct i40e_pf *pf = vf->pf;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000530 struct i40e_hw *hw = &pf->hw;
531 u32 reg_idx, reg;
532 int i, msix_vf;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000533
534 /* free vsi & disconnect it from the parent uplink */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700535 if (vf->lan_vsi_idx) {
536 i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
537 vf->lan_vsi_idx = 0;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000538 vf->lan_vsi_id = 0;
539 }
Mitch Williams9347eb72014-02-11 08:26:32 +0000540 msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
541
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000542 /* disable interrupts so the VF starts in a known state */
543 for (i = 0; i < msix_vf; i++) {
544 /* format is same for both registers */
545 if (0 == i)
546 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
547 else
548 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
549 (vf->vf_id))
550 + (i - 1));
551 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
552 i40e_flush(hw);
553 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000554
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000555 /* clear the irq settings */
556 for (i = 0; i < msix_vf; i++) {
557 /* format is same for both registers */
558 if (0 == i)
559 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
560 else
561 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
562 (vf->vf_id))
563 + (i - 1));
564 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
565 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
566 wr32(hw, reg_idx, reg);
567 i40e_flush(hw);
568 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000569 /* reset some of the state varibles keeping
570 * track of the resources
571 */
572 vf->num_queue_pairs = 0;
573 vf->vf_states = 0;
574}
575
576/**
577 * i40e_alloc_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000578 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000579 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000580 * allocate VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000581 **/
582static int i40e_alloc_vf_res(struct i40e_vf *vf)
583{
584 struct i40e_pf *pf = vf->pf;
585 int total_queue_pairs = 0;
586 int ret;
587
588 /* allocate hw vsi context & associated resources */
589 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
590 if (ret)
591 goto error_alloc;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700592 total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000593 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
594
595 /* store the total qps number for the runtime
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000596 * VF req validation
Mitch Williams805bd5b2013-11-28 06:39:26 +0000597 */
598 vf->num_queue_pairs = total_queue_pairs;
599
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000600 /* VF is now completely initialized */
Mitch Williams805bd5b2013-11-28 06:39:26 +0000601 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
602
603error_alloc:
604 if (ret)
605 i40e_free_vf_res(vf);
606
607 return ret;
608}
609
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000610#define VF_DEVICE_STATUS 0xAA
611#define VF_TRANS_PENDING_MASK 0x20
612/**
613 * i40e_quiesce_vf_pci
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000614 * @vf: pointer to the VF structure
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000615 *
616 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
617 * if the transactions never clear.
618 **/
619static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
620{
621 struct i40e_pf *pf = vf->pf;
622 struct i40e_hw *hw = &pf->hw;
623 int vf_abs_id, i;
624 u32 reg;
625
Mitch Williamsb141d612013-11-28 06:39:36 +0000626 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000627
628 wr32(hw, I40E_PF_PCI_CIAA,
629 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
630 for (i = 0; i < 100; i++) {
631 reg = rd32(hw, I40E_PF_PCI_CIAD);
632 if ((reg & VF_TRANS_PENDING_MASK) == 0)
633 return 0;
634 udelay(1);
635 }
636 return -EIO;
637}
638
Mitch Williams805bd5b2013-11-28 06:39:26 +0000639/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000640 * i40e_reset_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000641 * @vf: pointer to the VF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000642 * @flr: VFLR was issued or not
643 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000644 * reset the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000645 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000646void i40e_reset_vf(struct i40e_vf *vf, bool flr)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000647{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000648 struct i40e_pf *pf = vf->pf;
649 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000650 bool rsd = false;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000651 int i;
652 u32 reg;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000653
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000654 if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
655 return;
656
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000657 /* warn the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000658 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
659
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000660 /* In the case of a VFLR, the HW has already reset the VF and we
661 * just need to clean up, so don't hit the VFRTRIG register.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000662 */
663 if (!flr) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000664 /* reset VF using VPGEN_VFRTRIG reg */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000665 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
666 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000667 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
668 i40e_flush(hw);
669 }
670
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000671 if (i40e_quiesce_vf_pci(vf))
672 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
673 vf->vf_id);
674
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000675 /* poll VPGEN_VFRSTAT reg to make sure
676 * that reset is complete
677 */
Mitch Williams1750a222015-01-09 11:18:13 +0000678 for (i = 0; i < 10; i++) {
679 /* VF reset requires driver to first reset the VF and then
680 * poll the status register to make sure that the reset
681 * completed successfully. Due to internal HW FIFO flushes,
682 * we must wait 10ms before the register will be valid.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000683 */
Mitch Williams1750a222015-01-09 11:18:13 +0000684 usleep_range(10000, 20000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000685 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
686 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
687 rsd = true;
688 break;
689 }
690 }
691
692 if (!rsd)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000693 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000694 vf->vf_id);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000695 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000696 /* clear the reset bit in the VPGEN_VFRTRIG reg */
697 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
698 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
699 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000700
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000701 /* On initial reset, we won't have any queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700702 if (vf->lan_vsi_idx == 0)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000703 goto complete_reset;
704
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700705 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000706complete_reset:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000707 /* reallocate VF resources to reset the VSI state */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000708 i40e_free_vf_res(vf);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000709 i40e_alloc_vf_res(vf);
710 i40e_enable_vf_mappings(vf);
Mitch Williamsc17b3622014-02-13 03:48:45 -0800711 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000712
713 /* tell the VF the reset is done */
714 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
715 i40e_flush(hw);
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000716 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000717}
Greg Rosec3542292013-12-13 08:38:38 +0000718
719/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000720 * i40e_free_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000721 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000722 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000723 * free VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000724 **/
725void i40e_free_vfs(struct i40e_pf *pf)
726{
Mitch Williamsf7414532013-11-28 06:39:40 +0000727 struct i40e_hw *hw = &pf->hw;
728 u32 reg_idx, bit_idx;
729 int i, tmp, vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000730
731 if (!pf->vf)
732 return;
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000733 while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
734 usleep_range(1000, 2000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000735
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000736 /* Disable IOV before freeing resources. This lets any VF drivers
737 * running in the host get themselves cleaned up before we yank
738 * the carpet out from underneath their feet.
739 */
740 if (!pci_vfs_assigned(pf->pdev))
741 pci_disable_sriov(pf->pdev);
Mitch Williams6d7b9672015-03-31 00:45:02 -0700742 else
743 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000744
745 msleep(20); /* let any messages in transit get finished up */
746
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000747 /* free up VF resources */
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000748 tmp = pf->num_alloc_vfs;
749 pf->num_alloc_vfs = 0;
750 for (i = 0; i < tmp; i++) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000751 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
752 i40e_free_vf_res(&pf->vf[i]);
753 /* disable qp mappings */
754 i40e_disable_vf_mappings(&pf->vf[i]);
755 }
756
757 kfree(pf->vf);
758 pf->vf = NULL;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000759
Mitch Williams9e5634d2014-04-04 04:43:14 +0000760 /* This check is for when the driver is unloaded while VFs are
761 * assigned. Setting the number of VFs to 0 through sysfs is caught
762 * before this function ever gets called.
763 */
Ethan Zhaoc24817b2014-07-22 18:36:43 +0000764 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williamsf7414532013-11-28 06:39:40 +0000765 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
766 * work correctly when SR-IOV gets re-enabled.
767 */
768 for (vf_id = 0; vf_id < tmp; vf_id++) {
769 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
770 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
771 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
772 }
Greg Rosec3542292013-12-13 08:38:38 +0000773 }
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000774 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000775}
776
777#ifdef CONFIG_PCI_IOV
778/**
779 * i40e_alloc_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000780 * @pf: pointer to the PF structure
781 * @num_alloc_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000782 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000783 * allocate VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000784 **/
Mitch Williams4aeec012014-02-13 03:48:47 -0800785int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000786{
787 struct i40e_vf *vfs;
788 int i, ret = 0;
789
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000790 /* Disable interrupt 0 so we don't try to handle the VFLR. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000791 i40e_irq_dynamic_disable_icr0(pf);
792
Mitch Williams4aeec012014-02-13 03:48:47 -0800793 /* Check to see if we're just allocating resources for extant VFs */
794 if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
795 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
796 if (ret) {
797 dev_err(&pf->pdev->dev,
798 "Failed to enable SR-IOV, error %d.\n", ret);
799 pf->num_alloc_vfs = 0;
800 goto err_iov;
801 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000802 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000803 /* allocate memory */
Akeem G Abodunrincc6456a2014-02-06 05:51:02 +0000804 vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000805 if (!vfs) {
806 ret = -ENOMEM;
807 goto err_alloc;
808 }
Mitch Williamsc674d122014-05-20 08:01:40 +0000809 pf->vf = vfs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000810
811 /* apply default profile */
812 for (i = 0; i < num_alloc_vfs; i++) {
813 vfs[i].pf = pf;
814 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
815 vfs[i].vf_id = i;
816
817 /* assign default capabilities */
818 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
Mitch Williamsc674d122014-05-20 08:01:40 +0000819 vfs[i].spoofchk = true;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000820 /* VF resources get allocated during reset */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000821 i40e_reset_vf(&vfs[i], false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000822
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000823 /* enable VF vplan_qtable mappings */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000824 i40e_enable_vf_mappings(&vfs[i]);
825 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000826 pf->num_alloc_vfs = num_alloc_vfs;
827
828err_alloc:
829 if (ret)
830 i40e_free_vfs(pf);
831err_iov:
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000832 /* Re-enable interrupt 0. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000833 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000834 return ret;
835}
836
837#endif
838/**
839 * i40e_pci_sriov_enable
840 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000841 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000842 *
843 * Enable or change the number of VFs
844 **/
845static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
846{
847#ifdef CONFIG_PCI_IOV
848 struct i40e_pf *pf = pci_get_drvdata(pdev);
849 int pre_existing_vfs = pci_num_vf(pdev);
850 int err = 0;
851
852 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
853 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
854 i40e_free_vfs(pf);
855 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
856 goto out;
857
858 if (num_vfs > pf->num_req_vfs) {
859 err = -EPERM;
860 goto err_out;
861 }
862
863 err = i40e_alloc_vfs(pf, num_vfs);
864 if (err) {
865 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
866 goto err_out;
867 }
868
869out:
870 return num_vfs;
871
872err_out:
873 return err;
874#endif
875 return 0;
876}
877
878/**
879 * i40e_pci_sriov_configure
880 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000881 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000882 *
883 * Enable or change the number of VFs. Called when the user updates the number
884 * of VFs in sysfs.
885 **/
886int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
887{
888 struct i40e_pf *pf = pci_get_drvdata(pdev);
889
890 if (num_vfs)
891 return i40e_pci_sriov_enable(pdev, num_vfs);
892
Ethan Zhaoc24817b2014-07-22 18:36:43 +0000893 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williams9e5634d2014-04-04 04:43:14 +0000894 i40e_free_vfs(pf);
895 } else {
896 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
897 return -EINVAL;
898 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000899 return 0;
900}
901
902/***********************virtual channel routines******************/
903
904/**
905 * i40e_vc_send_msg_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000906 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000907 * @v_opcode: virtual channel opcode
908 * @v_retval: virtual channel return value
909 * @msg: pointer to the msg buffer
910 * @msglen: msg length
911 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000912 * send msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000913 **/
914static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
915 u32 v_retval, u8 *msg, u16 msglen)
916{
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +0000917 struct i40e_pf *pf;
918 struct i40e_hw *hw;
919 int abs_vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000920 i40e_status aq_ret;
921
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +0000922 /* validate the request */
923 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
924 return -EINVAL;
925
926 pf = vf->pf;
927 hw = &pf->hw;
928 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
929
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000930 /* single place to detect unsuccessful return values */
931 if (v_retval) {
932 vf->num_invalid_msgs++;
933 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
934 v_opcode, v_retval);
935 if (vf->num_invalid_msgs >
936 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
937 dev_err(&pf->pdev->dev,
938 "Number of invalid messages exceeded for VF %d\n",
939 vf->vf_id);
940 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
941 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
942 }
943 } else {
944 vf->num_valid_msgs++;
945 }
946
Ashish Shahf19efbb2014-08-01 13:27:06 -0700947 aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Mitch Williams7efa84b2013-11-28 06:39:41 +0000948 msg, msglen, NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000949 if (aq_ret) {
950 dev_err(&pf->pdev->dev,
951 "Unable to send the message to VF %d aq_err %d\n",
952 vf->vf_id, pf->hw.aq.asq_last_status);
953 return -EIO;
954 }
955
956 return 0;
957}
958
959/**
960 * i40e_vc_send_resp_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000961 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000962 * @opcode: operation code
963 * @retval: return value
964 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000965 * send resp msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000966 **/
967static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
968 enum i40e_virtchnl_ops opcode,
969 i40e_status retval)
970{
971 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
972}
973
974/**
975 * i40e_vc_get_version_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000976 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000977 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000978 * called from the VF to request the API version used by the PF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000979 **/
980static int i40e_vc_get_version_msg(struct i40e_vf *vf)
981{
982 struct i40e_virtchnl_version_info info = {
983 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
984 };
985
986 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
987 I40E_SUCCESS, (u8 *)&info,
988 sizeof(struct
989 i40e_virtchnl_version_info));
990}
991
992/**
993 * i40e_vc_get_vf_resources_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000994 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000995 * @msg: pointer to the msg buffer
996 * @msglen: msg length
997 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000998 * called from the VF to request its resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000999 **/
1000static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf)
1001{
1002 struct i40e_virtchnl_vf_resource *vfres = NULL;
1003 struct i40e_pf *pf = vf->pf;
1004 i40e_status aq_ret = 0;
1005 struct i40e_vsi *vsi;
1006 int i = 0, len = 0;
1007 int num_vsis = 1;
1008 int ret;
1009
1010 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1011 aq_ret = I40E_ERR_PARAM;
1012 goto err;
1013 }
1014
1015 len = (sizeof(struct i40e_virtchnl_vf_resource) +
1016 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1017
1018 vfres = kzalloc(len, GFP_KERNEL);
1019 if (!vfres) {
1020 aq_ret = I40E_ERR_NO_MEMORY;
1021 len = 0;
1022 goto err;
1023 }
1024
1025 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001026 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001027 if (!vsi->info.pvid)
1028 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1029
1030 vfres->num_vsis = num_vsis;
1031 vfres->num_queue_pairs = vf->num_queue_pairs;
1032 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001033 if (vf->lan_vsi_idx) {
1034 vfres->vsi_res[i].vsi_id = vf->lan_vsi_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001035 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1036 vfres->vsi_res[i].num_queue_pairs =
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001037 pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001038 memcpy(vfres->vsi_res[i].default_mac_addr,
1039 vf->default_lan_addr.addr, ETH_ALEN);
1040 i++;
1041 }
1042 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1043
1044err:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001045 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001046 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1047 aq_ret, (u8 *)vfres, len);
1048
1049 kfree(vfres);
1050 return ret;
1051}
1052
1053/**
1054 * i40e_vc_reset_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001055 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001056 * @msg: pointer to the msg buffer
1057 * @msglen: msg length
1058 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001059 * called from the VF to reset itself,
1060 * unlike other virtchnl messages, PF driver
1061 * doesn't send the response back to the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001062 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001063static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001064{
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001065 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1066 i40e_reset_vf(vf, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001067}
1068
1069/**
1070 * i40e_vc_config_promiscuous_mode_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001071 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001072 * @msg: pointer to the msg buffer
1073 * @msglen: msg length
1074 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001075 * called from the VF to configure the promiscuous mode of
1076 * VF vsis
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001077 **/
1078static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1079 u8 *msg, u16 msglen)
1080{
1081 struct i40e_virtchnl_promisc_info *info =
1082 (struct i40e_virtchnl_promisc_info *)msg;
1083 struct i40e_pf *pf = vf->pf;
1084 struct i40e_hw *hw = &pf->hw;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001085 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001086 bool allmulti = false;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001087 i40e_status aq_ret;
1088
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001089 vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001090 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1091 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1092 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001093 (vsi->type != I40E_VSI_FCOE)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001094 aq_ret = I40E_ERR_PARAM;
1095 goto error_param;
1096 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001097 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1098 allmulti = true;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001099 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001100 allmulti, NULL);
1101
1102error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001103 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001104 return i40e_vc_send_resp_to_vf(vf,
1105 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1106 aq_ret);
1107}
1108
1109/**
1110 * i40e_vc_config_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001111 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001112 * @msg: pointer to the msg buffer
1113 * @msglen: msg length
1114 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001115 * called from the VF to configure the rx/tx
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001116 * queues
1117 **/
1118static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1119{
1120 struct i40e_virtchnl_vsi_queue_config_info *qci =
1121 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1122 struct i40e_virtchnl_queue_pair_info *qpi;
Ashish Shah5f5e33b2014-07-10 07:58:15 +00001123 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001124 u16 vsi_id, vsi_queue_id;
1125 i40e_status aq_ret = 0;
1126 int i;
1127
1128 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1129 aq_ret = I40E_ERR_PARAM;
1130 goto error_param;
1131 }
1132
1133 vsi_id = qci->vsi_id;
1134 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1135 aq_ret = I40E_ERR_PARAM;
1136 goto error_param;
1137 }
1138 for (i = 0; i < qci->num_queue_pairs; i++) {
1139 qpi = &qci->qpair[i];
1140 vsi_queue_id = qpi->txq.queue_id;
1141 if ((qpi->txq.vsi_id != vsi_id) ||
1142 (qpi->rxq.vsi_id != vsi_id) ||
1143 (qpi->rxq.queue_id != vsi_queue_id) ||
1144 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1145 aq_ret = I40E_ERR_PARAM;
1146 goto error_param;
1147 }
1148
1149 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1150 &qpi->rxq) ||
1151 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1152 &qpi->txq)) {
1153 aq_ret = I40E_ERR_PARAM;
1154 goto error_param;
1155 }
1156 }
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001157 /* set vsi num_queue_pairs in use to num configured by VF */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001158 pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001159
1160error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001161 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001162 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1163 aq_ret);
1164}
1165
1166/**
1167 * i40e_vc_config_irq_map_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001168 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001169 * @msg: pointer to the msg buffer
1170 * @msglen: msg length
1171 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001172 * called from the VF to configure the irq to
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001173 * queue map
1174 **/
1175static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1176{
1177 struct i40e_virtchnl_irq_map_info *irqmap_info =
1178 (struct i40e_virtchnl_irq_map_info *)msg;
1179 struct i40e_virtchnl_vector_map *map;
1180 u16 vsi_id, vsi_queue_id, vector_id;
1181 i40e_status aq_ret = 0;
1182 unsigned long tempmap;
1183 int i;
1184
1185 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1186 aq_ret = I40E_ERR_PARAM;
1187 goto error_param;
1188 }
1189
1190 for (i = 0; i < irqmap_info->num_vectors; i++) {
1191 map = &irqmap_info->vecmap[i];
1192
1193 vector_id = map->vector_id;
1194 vsi_id = map->vsi_id;
1195 /* validate msg params */
1196 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1197 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1198 aq_ret = I40E_ERR_PARAM;
1199 goto error_param;
1200 }
1201
1202 /* lookout for the invalid queue index */
1203 tempmap = map->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001204 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001205 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1206 vsi_queue_id)) {
1207 aq_ret = I40E_ERR_PARAM;
1208 goto error_param;
1209 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001210 }
1211
1212 tempmap = map->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001213 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001214 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1215 vsi_queue_id)) {
1216 aq_ret = I40E_ERR_PARAM;
1217 goto error_param;
1218 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001219 }
1220
1221 i40e_config_irq_link_list(vf, vsi_id, map);
1222 }
1223error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001224 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001225 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1226 aq_ret);
1227}
1228
1229/**
1230 * i40e_vc_enable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001231 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001232 * @msg: pointer to the msg buffer
1233 * @msglen: msg length
1234 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001235 * called from the VF to enable all or specific queue(s)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001236 **/
1237static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1238{
1239 struct i40e_virtchnl_queue_select *vqs =
1240 (struct i40e_virtchnl_queue_select *)msg;
1241 struct i40e_pf *pf = vf->pf;
1242 u16 vsi_id = vqs->vsi_id;
1243 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001244
1245 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1246 aq_ret = I40E_ERR_PARAM;
1247 goto error_param;
1248 }
1249
1250 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1251 aq_ret = I40E_ERR_PARAM;
1252 goto error_param;
1253 }
1254
1255 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1256 aq_ret = I40E_ERR_PARAM;
1257 goto error_param;
1258 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001259
1260 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], true))
Mitch Williams88f65632013-11-28 06:39:28 +00001261 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001262error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001263 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001264 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1265 aq_ret);
1266}
1267
1268/**
1269 * i40e_vc_disable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001270 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001271 * @msg: pointer to the msg buffer
1272 * @msglen: msg length
1273 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001274 * called from the VF to disable all or specific
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001275 * queue(s)
1276 **/
1277static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1278{
1279 struct i40e_virtchnl_queue_select *vqs =
1280 (struct i40e_virtchnl_queue_select *)msg;
1281 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001282 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001283
1284 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1285 aq_ret = I40E_ERR_PARAM;
1286 goto error_param;
1287 }
1288
1289 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1290 aq_ret = I40E_ERR_PARAM;
1291 goto error_param;
1292 }
1293
1294 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1295 aq_ret = I40E_ERR_PARAM;
1296 goto error_param;
1297 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001298
1299 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false))
Mitch Williams88f65632013-11-28 06:39:28 +00001300 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001301
1302error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001303 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001304 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1305 aq_ret);
1306}
1307
1308/**
1309 * i40e_vc_get_stats_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001310 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001311 * @msg: pointer to the msg buffer
1312 * @msglen: msg length
1313 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001314 * called from the VF to get vsi stats
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001315 **/
1316static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1317{
1318 struct i40e_virtchnl_queue_select *vqs =
1319 (struct i40e_virtchnl_queue_select *)msg;
1320 struct i40e_pf *pf = vf->pf;
1321 struct i40e_eth_stats stats;
1322 i40e_status aq_ret = 0;
1323 struct i40e_vsi *vsi;
1324
1325 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1326
1327 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1328 aq_ret = I40E_ERR_PARAM;
1329 goto error_param;
1330 }
1331
1332 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1333 aq_ret = I40E_ERR_PARAM;
1334 goto error_param;
1335 }
1336
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001337 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001338 if (!vsi) {
1339 aq_ret = I40E_ERR_PARAM;
1340 goto error_param;
1341 }
1342 i40e_update_eth_stats(vsi);
Mitch Williams5a9769c2013-11-28 06:39:38 +00001343 stats = vsi->eth_stats;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001344
1345error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001346 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001347 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1348 (u8 *)&stats, sizeof(stats));
1349}
1350
1351/**
Greg Rosef657a6e2013-11-28 06:39:42 +00001352 * i40e_check_vf_permission
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001353 * @vf: pointer to the VF info
Greg Rosef657a6e2013-11-28 06:39:42 +00001354 * @macaddr: pointer to the MAC Address being checked
1355 *
1356 * Check if the VF has permission to add or delete unicast MAC address
1357 * filters and return error code -EPERM if not. Then check if the
1358 * address filter requested is broadcast or zero and if so return
1359 * an invalid MAC address error code.
1360 **/
1361static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1362{
1363 struct i40e_pf *pf = vf->pf;
1364 int ret = 0;
1365
1366 if (is_broadcast_ether_addr(macaddr) ||
1367 is_zero_ether_addr(macaddr)) {
1368 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1369 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rose5017c2a2013-12-07 10:36:54 +00001370 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1371 !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001372 /* If the host VMM administrator has set the VF MAC address
1373 * administratively via the ndo_set_vf_mac command then deny
1374 * permission to the VF to add or delete unicast MAC addresses.
Greg Rose5017c2a2013-12-07 10:36:54 +00001375 * The VF may request to set the MAC address filter already
1376 * assigned to it so do not return an error in that case.
Greg Rosef657a6e2013-11-28 06:39:42 +00001377 */
1378 dev_err(&pf->pdev->dev,
1379 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1380 ret = -EPERM;
1381 }
1382 return ret;
1383}
1384
1385/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001386 * i40e_vc_add_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001387 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001388 * @msg: pointer to the msg buffer
1389 * @msglen: msg length
1390 *
1391 * add guest mac address filter
1392 **/
1393static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1394{
1395 struct i40e_virtchnl_ether_addr_list *al =
1396 (struct i40e_virtchnl_ether_addr_list *)msg;
1397 struct i40e_pf *pf = vf->pf;
1398 struct i40e_vsi *vsi = NULL;
1399 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001400 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001401 int i;
1402
1403 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1404 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1405 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001406 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001407 goto error_param;
1408 }
1409
1410 for (i = 0; i < al->num_elements; i++) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001411 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1412 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001413 goto error_param;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001414 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001415 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001416
1417 /* add new addresses to the list */
1418 for (i = 0; i < al->num_elements; i++) {
1419 struct i40e_mac_filter *f;
1420
1421 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
Mitch Williams7e68edf92013-11-16 10:00:41 +00001422 if (!f) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001423 if (i40e_is_vsi_in_vlan(vsi))
1424 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1425 true, false);
1426 else
1427 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1428 true, false);
1429 }
1430
1431 if (!f) {
1432 dev_err(&pf->pdev->dev,
1433 "Unable to add VF MAC filter\n");
Greg Rosef657a6e2013-11-28 06:39:42 +00001434 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001435 goto error_param;
1436 }
1437 }
1438
1439 /* program the updated filter list */
1440 if (i40e_sync_vsi_filters(vsi))
1441 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1442
1443error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001444 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001445 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001446 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001447}
1448
1449/**
1450 * i40e_vc_del_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001451 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001452 * @msg: pointer to the msg buffer
1453 * @msglen: msg length
1454 *
1455 * remove guest mac address filter
1456 **/
1457static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1458{
1459 struct i40e_virtchnl_ether_addr_list *al =
1460 (struct i40e_virtchnl_ether_addr_list *)msg;
1461 struct i40e_pf *pf = vf->pf;
1462 struct i40e_vsi *vsi = NULL;
1463 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001464 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001465 int i;
1466
1467 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1468 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1469 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001470 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001471 goto error_param;
1472 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001473
1474 for (i = 0; i < al->num_elements; i++) {
Mitch Williams700bbf62013-12-21 05:44:45 +00001475 if (is_broadcast_ether_addr(al->list[i].addr) ||
1476 is_zero_ether_addr(al->list[i].addr)) {
1477 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
1478 al->list[i].addr);
1479 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rosef657a6e2013-11-28 06:39:42 +00001480 goto error_param;
Mitch Williams700bbf62013-12-21 05:44:45 +00001481 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001482 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001483 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001484
1485 /* delete addresses from the list */
1486 for (i = 0; i < al->num_elements; i++)
1487 i40e_del_filter(vsi, al->list[i].addr,
1488 I40E_VLAN_ANY, true, false);
1489
1490 /* program the updated filter list */
1491 if (i40e_sync_vsi_filters(vsi))
1492 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1493
1494error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001495 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001496 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001497 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001498}
1499
1500/**
1501 * i40e_vc_add_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001502 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001503 * @msg: pointer to the msg buffer
1504 * @msglen: msg length
1505 *
1506 * program guest vlan id
1507 **/
1508static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1509{
1510 struct i40e_virtchnl_vlan_filter_list *vfl =
1511 (struct i40e_virtchnl_vlan_filter_list *)msg;
1512 struct i40e_pf *pf = vf->pf;
1513 struct i40e_vsi *vsi = NULL;
1514 u16 vsi_id = vfl->vsi_id;
1515 i40e_status aq_ret = 0;
1516 int i;
1517
1518 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1519 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1520 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1521 aq_ret = I40E_ERR_PARAM;
1522 goto error_param;
1523 }
1524
1525 for (i = 0; i < vfl->num_elements; i++) {
1526 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1527 aq_ret = I40E_ERR_PARAM;
1528 dev_err(&pf->pdev->dev,
1529 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1530 goto error_param;
1531 }
1532 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001533 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001534 if (vsi->info.pvid) {
1535 aq_ret = I40E_ERR_PARAM;
1536 goto error_param;
1537 }
1538
1539 i40e_vlan_stripping_enable(vsi);
1540 for (i = 0; i < vfl->num_elements; i++) {
1541 /* add new VLAN filter */
1542 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1543 if (ret)
1544 dev_err(&pf->pdev->dev,
1545 "Unable to add VF vlan filter %d, error %d\n",
1546 vfl->vlan_id[i], ret);
1547 }
1548
1549error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001550 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001551 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1552}
1553
1554/**
1555 * i40e_vc_remove_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001556 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001557 * @msg: pointer to the msg buffer
1558 * @msglen: msg length
1559 *
1560 * remove programmed guest vlan id
1561 **/
1562static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1563{
1564 struct i40e_virtchnl_vlan_filter_list *vfl =
1565 (struct i40e_virtchnl_vlan_filter_list *)msg;
1566 struct i40e_pf *pf = vf->pf;
1567 struct i40e_vsi *vsi = NULL;
1568 u16 vsi_id = vfl->vsi_id;
1569 i40e_status aq_ret = 0;
1570 int i;
1571
1572 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1573 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1574 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1575 aq_ret = I40E_ERR_PARAM;
1576 goto error_param;
1577 }
1578
1579 for (i = 0; i < vfl->num_elements; i++) {
1580 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1581 aq_ret = I40E_ERR_PARAM;
1582 goto error_param;
1583 }
1584 }
1585
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001586 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001587 if (vsi->info.pvid) {
1588 aq_ret = I40E_ERR_PARAM;
1589 goto error_param;
1590 }
1591
1592 for (i = 0; i < vfl->num_elements; i++) {
1593 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1594 if (ret)
1595 dev_err(&pf->pdev->dev,
1596 "Unable to delete VF vlan filter %d, error %d\n",
1597 vfl->vlan_id[i], ret);
1598 }
1599
1600error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001601 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001602 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1603}
1604
1605/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001606 * i40e_vc_validate_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001607 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001608 * @msg: pointer to the msg buffer
1609 * @msglen: msg length
1610 * @msghndl: msg handle
1611 *
1612 * validate msg
1613 **/
1614static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1615 u32 v_retval, u8 *msg, u16 msglen)
1616{
1617 bool err_msg_format = false;
1618 int valid_len;
1619
1620 /* Check if VF is disabled. */
1621 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1622 return I40E_ERR_PARAM;
1623
1624 /* Validate message length. */
1625 switch (v_opcode) {
1626 case I40E_VIRTCHNL_OP_VERSION:
1627 valid_len = sizeof(struct i40e_virtchnl_version_info);
1628 break;
1629 case I40E_VIRTCHNL_OP_RESET_VF:
1630 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1631 valid_len = 0;
1632 break;
1633 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1634 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1635 break;
1636 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1637 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1638 break;
1639 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1640 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1641 if (msglen >= valid_len) {
1642 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1643 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1644 valid_len += (vqc->num_queue_pairs *
1645 sizeof(struct
1646 i40e_virtchnl_queue_pair_info));
1647 if (vqc->num_queue_pairs == 0)
1648 err_msg_format = true;
1649 }
1650 break;
1651 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1652 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1653 if (msglen >= valid_len) {
1654 struct i40e_virtchnl_irq_map_info *vimi =
1655 (struct i40e_virtchnl_irq_map_info *)msg;
1656 valid_len += (vimi->num_vectors *
1657 sizeof(struct i40e_virtchnl_vector_map));
1658 if (vimi->num_vectors == 0)
1659 err_msg_format = true;
1660 }
1661 break;
1662 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1663 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1664 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1665 break;
1666 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1667 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1668 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1669 if (msglen >= valid_len) {
1670 struct i40e_virtchnl_ether_addr_list *veal =
1671 (struct i40e_virtchnl_ether_addr_list *)msg;
1672 valid_len += veal->num_elements *
1673 sizeof(struct i40e_virtchnl_ether_addr);
1674 if (veal->num_elements == 0)
1675 err_msg_format = true;
1676 }
1677 break;
1678 case I40E_VIRTCHNL_OP_ADD_VLAN:
1679 case I40E_VIRTCHNL_OP_DEL_VLAN:
1680 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1681 if (msglen >= valid_len) {
1682 struct i40e_virtchnl_vlan_filter_list *vfl =
1683 (struct i40e_virtchnl_vlan_filter_list *)msg;
1684 valid_len += vfl->num_elements * sizeof(u16);
1685 if (vfl->num_elements == 0)
1686 err_msg_format = true;
1687 }
1688 break;
1689 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1690 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1691 break;
1692 case I40E_VIRTCHNL_OP_GET_STATS:
1693 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1694 break;
1695 /* These are always errors coming from the VF. */
1696 case I40E_VIRTCHNL_OP_EVENT:
1697 case I40E_VIRTCHNL_OP_UNKNOWN:
1698 default:
1699 return -EPERM;
1700 break;
1701 }
1702 /* few more checks */
1703 if ((valid_len != msglen) || (err_msg_format)) {
1704 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1705 return -EINVAL;
1706 } else {
1707 return 0;
1708 }
1709}
1710
1711/**
1712 * i40e_vc_process_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001713 * @pf: pointer to the PF structure
1714 * @vf_id: source VF id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001715 * @msg: pointer to the msg buffer
1716 * @msglen: msg length
1717 * @msghndl: msg handle
1718 *
1719 * called from the common aeq/arq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001720 * process request from VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001721 **/
1722int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1723 u32 v_retval, u8 *msg, u16 msglen)
1724{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001725 struct i40e_hw *hw = &pf->hw;
Dan Carpenterc243e962014-01-15 06:43:39 +00001726 unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001727 struct i40e_vf *vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001728 int ret;
1729
1730 pf->vf_aq_requests++;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001731 if (local_vf_id >= pf->num_alloc_vfs)
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001732 return -EINVAL;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001733 vf = &(pf->vf[local_vf_id]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001734 /* perform basic checks on the msg */
1735 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1736
1737 if (ret) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001738 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001739 local_vf_id, v_opcode, msglen);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001740 return ret;
1741 }
Mitch Williamsbae3cae2014-01-14 00:49:49 -08001742
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001743 switch (v_opcode) {
1744 case I40E_VIRTCHNL_OP_VERSION:
1745 ret = i40e_vc_get_version_msg(vf);
1746 break;
1747 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1748 ret = i40e_vc_get_vf_resources_msg(vf);
1749 break;
1750 case I40E_VIRTCHNL_OP_RESET_VF:
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001751 i40e_vc_reset_vf_msg(vf);
1752 ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001753 break;
1754 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1755 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1756 break;
1757 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1758 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1759 break;
1760 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1761 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1762 break;
1763 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1764 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
1765 break;
1766 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1767 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1768 break;
1769 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1770 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1771 break;
1772 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1773 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1774 break;
1775 case I40E_VIRTCHNL_OP_ADD_VLAN:
1776 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1777 break;
1778 case I40E_VIRTCHNL_OP_DEL_VLAN:
1779 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1780 break;
1781 case I40E_VIRTCHNL_OP_GET_STATS:
1782 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1783 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001784 case I40E_VIRTCHNL_OP_UNKNOWN:
1785 default:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001786 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001787 v_opcode, local_vf_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001788 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1789 I40E_ERR_NOT_IMPLEMENTED);
1790 break;
1791 }
1792
1793 return ret;
1794}
1795
1796/**
1797 * i40e_vc_process_vflr_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001798 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001799 *
1800 * called from the vlfr irq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001801 * free up VF resources and state variables
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001802 **/
1803int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1804{
1805 u32 reg, reg_idx, bit_idx, vf_id;
1806 struct i40e_hw *hw = &pf->hw;
1807 struct i40e_vf *vf;
1808
1809 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1810 return 0;
1811
Mitch Williamsc5c2f7c2014-11-11 03:15:04 +00001812 /* re-enable vflr interrupt cause */
1813 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1814 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1815 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1816 i40e_flush(hw);
1817
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001818 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1819 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1820 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1821 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001822 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001823 vf = &pf->vf[vf_id];
1824 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
1825 if (reg & (1 << bit_idx)) {
1826 /* clear the bit in GLGEN_VFLRSTAT */
1827 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
1828
Mitch Williamseb2d80b2014-02-13 03:48:48 -08001829 if (!test_bit(__I40E_DOWN, &pf->state))
1830 i40e_reset_vf(vf, true);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001831 }
1832 }
1833
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001834 return 0;
1835}
1836
1837/**
1838 * i40e_vc_vf_broadcast
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001839 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001840 * @opcode: operation code
1841 * @retval: return value
1842 * @msg: pointer to the msg buffer
1843 * @msglen: msg length
1844 *
1845 * send a message to all VFs on a given PF
1846 **/
1847static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
1848 enum i40e_virtchnl_ops v_opcode,
1849 i40e_status v_retval, u8 *msg,
1850 u16 msglen)
1851{
1852 struct i40e_hw *hw = &pf->hw;
1853 struct i40e_vf *vf = pf->vf;
1854 int i;
1855
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001856 for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
1857 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001858 /* Not all VFs are enabled so skip the ones that are not */
Anjali Singhai Jain478c9e72014-07-10 07:58:18 +00001859 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
1860 !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1861 continue;
1862
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001863 /* Ignore return value on purpose - a given VF may fail, but
1864 * we need to keep going and send to all of them
1865 */
Ashish Shahf19efbb2014-08-01 13:27:06 -07001866 i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001867 msg, msglen, NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001868 }
1869}
1870
1871/**
1872 * i40e_vc_notify_link_state
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001873 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001874 *
1875 * send a link status message to all VFs on a given PF
1876 **/
1877void i40e_vc_notify_link_state(struct i40e_pf *pf)
1878{
1879 struct i40e_virtchnl_pf_event pfe;
Mitch Williams588aefa2014-02-11 08:27:49 +00001880 struct i40e_hw *hw = &pf->hw;
1881 struct i40e_vf *vf = pf->vf;
1882 struct i40e_link_status *ls = &pf->hw.phy.link_info;
1883 int i;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001884
1885 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
1886 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001887 for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
1888 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00001889 if (vf->link_forced) {
1890 pfe.event_data.link_event.link_status = vf->link_up;
1891 pfe.event_data.link_event.link_speed =
1892 (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
1893 } else {
1894 pfe.event_data.link_event.link_status =
1895 ls->link_info & I40E_AQ_LINK_UP;
1896 pfe.event_data.link_event.link_speed = ls->link_speed;
1897 }
Ashish Shahf19efbb2014-08-01 13:27:06 -07001898 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Mitch Williams588aefa2014-02-11 08:27:49 +00001899 0, (u8 *)&pfe, sizeof(pfe),
1900 NULL);
Mitch Williams588aefa2014-02-11 08:27:49 +00001901 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001902}
1903
1904/**
1905 * i40e_vc_notify_reset
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001906 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001907 *
1908 * indicate a pending reset to all VFs on a given PF
1909 **/
1910void i40e_vc_notify_reset(struct i40e_pf *pf)
1911{
1912 struct i40e_virtchnl_pf_event pfe;
1913
1914 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1915 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
1916 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
1917 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
1918}
1919
1920/**
1921 * i40e_vc_notify_vf_reset
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001922 * @vf: pointer to the VF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001923 *
1924 * indicate a pending reset to the given VF
1925 **/
1926void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
1927{
1928 struct i40e_virtchnl_pf_event pfe;
Anjali Singhai Jain478c9e72014-07-10 07:58:18 +00001929 int abs_vf_id;
1930
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001931 /* validate the request */
1932 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1933 return;
1934
Anjali Singhai Jain478c9e72014-07-10 07:58:18 +00001935 /* verify if the VF is in either init or active before proceeding */
1936 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
1937 !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1938 return;
1939
1940 abs_vf_id = vf->vf_id + vf->pf->hw.func_caps.vf_base_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001941
1942 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1943 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
Ashish Shahf19efbb2014-08-01 13:27:06 -07001944 i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001945 I40E_SUCCESS, (u8 *)&pfe,
1946 sizeof(struct i40e_virtchnl_pf_event), NULL);
1947}
1948
1949/**
1950 * i40e_ndo_set_vf_mac
1951 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001952 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001953 * @mac: mac address
1954 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001955 * program VF mac address
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001956 **/
1957int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
1958{
1959 struct i40e_netdev_priv *np = netdev_priv(netdev);
1960 struct i40e_vsi *vsi = np->vsi;
1961 struct i40e_pf *pf = vsi->back;
1962 struct i40e_mac_filter *f;
1963 struct i40e_vf *vf;
1964 int ret = 0;
1965
1966 /* validate the request */
1967 if (vf_id >= pf->num_alloc_vfs) {
1968 dev_err(&pf->pdev->dev,
1969 "Invalid VF Identifier %d\n", vf_id);
1970 ret = -EINVAL;
1971 goto error_param;
1972 }
1973
1974 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001975 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001976 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1977 dev_err(&pf->pdev->dev,
1978 "Uninitialized VF %d\n", vf_id);
1979 ret = -EINVAL;
1980 goto error_param;
1981 }
1982
1983 if (!is_valid_ether_addr(mac)) {
1984 dev_err(&pf->pdev->dev,
1985 "Invalid VF ethernet address\n");
1986 ret = -EINVAL;
1987 goto error_param;
1988 }
1989
1990 /* delete the temporary mac address */
Greg Rose37cc0d22014-04-01 07:11:44 +00001991 i40e_del_filter(vsi, vf->default_lan_addr.addr, vf->port_vlan_id,
1992 true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001993
Greg Rose29f71bb2014-05-20 08:01:45 +00001994 /* Delete all the filters for this VSI - we're going to kill it
1995 * anyway.
1996 */
1997 list_for_each_entry(f, &vsi->mac_filter_list, list)
1998 i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001999
2000 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2001 /* program mac filter */
2002 if (i40e_sync_vsi_filters(vsi)) {
2003 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2004 ret = -EIO;
2005 goto error_param;
2006 }
Greg Rose9a173902014-05-22 06:32:02 +00002007 ether_addr_copy(vf->default_lan_addr.addr, mac);
Greg Rosef657a6e2013-11-28 06:39:42 +00002008 vf->pf_set_mac = true;
Greg Rose17413a82014-06-04 01:23:13 +00002009 /* Force the VF driver stop so it has to reload with new MAC address */
2010 i40e_vc_disable_vf(pf, vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002011 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002012
2013error_param:
2014 return ret;
2015}
2016
2017/**
2018 * i40e_ndo_set_vf_port_vlan
2019 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002020 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002021 * @vlan_id: mac address
2022 * @qos: priority setting
2023 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002024 * program VF vlan id and/or qos
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002025 **/
2026int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2027 int vf_id, u16 vlan_id, u8 qos)
2028{
2029 struct i40e_netdev_priv *np = netdev_priv(netdev);
2030 struct i40e_pf *pf = np->vsi->back;
2031 struct i40e_vsi *vsi;
2032 struct i40e_vf *vf;
2033 int ret = 0;
2034
2035 /* validate the request */
2036 if (vf_id >= pf->num_alloc_vfs) {
2037 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2038 ret = -EINVAL;
2039 goto error_pvid;
2040 }
2041
2042 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2043 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2044 ret = -EINVAL;
2045 goto error_pvid;
2046 }
2047
2048 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002049 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002050 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2051 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2052 ret = -EINVAL;
2053 goto error_pvid;
2054 }
2055
Greg Rosef9b4b622014-03-06 09:02:28 +00002056 if (vsi->info.pvid == 0 && i40e_is_vsi_in_vlan(vsi)) {
Greg Rose99a49732014-01-13 16:13:03 -08002057 dev_err(&pf->pdev->dev,
2058 "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",
2059 vf_id);
Greg Rosef9b4b622014-03-06 09:02:28 +00002060 /* Administrator Error - knock the VF offline until he does
2061 * the right thing by reconfiguring his network correctly
2062 * and then reloading the VF driver.
2063 */
2064 i40e_vc_disable_vf(pf, vf);
2065 }
Greg Rose99a49732014-01-13 16:13:03 -08002066
Greg Rose8d82a7c2014-01-13 16:13:04 -08002067 /* Check for condition where there was already a port VLAN ID
2068 * filter set and now it is being deleted by setting it to zero.
Greg Rose1315f7c2014-03-14 07:32:20 +00002069 * Additionally check for the condition where there was a port
2070 * VLAN but now there is a new and different port VLAN being set.
Greg Rose8d82a7c2014-01-13 16:13:04 -08002071 * Before deleting all the old VLAN filters we must add new ones
2072 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2073 * MAC addresses deleted.
2074 */
Greg Rose1315f7c2014-03-14 07:32:20 +00002075 if ((!(vlan_id || qos) ||
2076 (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
2077 vsi->info.pvid)
Greg Rose8d82a7c2014-01-13 16:13:04 -08002078 ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2079
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002080 if (vsi->info.pvid) {
2081 /* kill old VLAN */
2082 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2083 VLAN_VID_MASK));
2084 if (ret) {
2085 dev_info(&vsi->back->pdev->dev,
2086 "remove VLAN failed, ret=%d, aq_err=%d\n",
2087 ret, pf->hw.aq.asq_last_status);
2088 }
2089 }
2090 if (vlan_id || qos)
2091 ret = i40e_vsi_add_pvid(vsi,
2092 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
2093 else
Greg Rose6c12fcb2013-11-28 06:39:34 +00002094 i40e_vsi_remove_pvid(vsi);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002095
2096 if (vlan_id) {
2097 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2098 vlan_id, qos, vf_id);
2099
2100 /* add new VLAN filter */
2101 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2102 if (ret) {
2103 dev_info(&vsi->back->pdev->dev,
2104 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2105 vsi->back->hw.aq.asq_last_status);
2106 goto error_pvid;
2107 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002108 /* Kill non-vlan MAC filters - ignore error return since
2109 * there might not be any non-vlan MAC filters.
2110 */
2111 i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002112 }
2113
2114 if (ret) {
2115 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2116 goto error_pvid;
2117 }
Greg Rose6c12fcb2013-11-28 06:39:34 +00002118 /* The Port VLAN needs to be saved across resets the same as the
2119 * default LAN MAC address.
2120 */
2121 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002122 ret = 0;
2123
2124error_pvid:
2125 return ret;
2126}
2127
Mitch Williams84590fd2014-04-09 05:58:57 +00002128#define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
2129#define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002130/**
2131 * i40e_ndo_set_vf_bw
2132 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002133 * @vf_id: VF identifier
2134 * @tx_rate: Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002135 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002136 * configure VF Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002137 **/
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002138int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2139 int max_tx_rate)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002140{
Mitch Williams6b192892014-03-06 09:02:29 +00002141 struct i40e_netdev_priv *np = netdev_priv(netdev);
2142 struct i40e_pf *pf = np->vsi->back;
2143 struct i40e_vsi *vsi;
2144 struct i40e_vf *vf;
2145 int speed = 0;
2146 int ret = 0;
2147
2148 /* validate the request */
2149 if (vf_id >= pf->num_alloc_vfs) {
2150 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2151 ret = -EINVAL;
2152 goto error;
2153 }
2154
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002155 if (min_tx_rate) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002156 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 -04002157 min_tx_rate, vf_id);
2158 return -EINVAL;
2159 }
2160
Mitch Williams6b192892014-03-06 09:02:29 +00002161 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002162 vsi = pf->vsi[vf->lan_vsi_idx];
Mitch Williams6b192892014-03-06 09:02:29 +00002163 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2164 dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
2165 ret = -EINVAL;
2166 goto error;
2167 }
2168
2169 switch (pf->hw.phy.link_info.link_speed) {
2170 case I40E_LINK_SPEED_40GB:
2171 speed = 40000;
2172 break;
2173 case I40E_LINK_SPEED_10GB:
2174 speed = 10000;
2175 break;
2176 case I40E_LINK_SPEED_1GB:
2177 speed = 1000;
2178 break;
2179 default:
2180 break;
2181 }
2182
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002183 if (max_tx_rate > speed) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002184 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002185 max_tx_rate, vf->vf_id);
Mitch Williams6b192892014-03-06 09:02:29 +00002186 ret = -EINVAL;
2187 goto error;
2188 }
2189
Mitch Williamsdac9b312014-04-09 05:58:56 +00002190 if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2191 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2192 max_tx_rate = 50;
2193 }
2194
Mitch Williams6b192892014-03-06 09:02:29 +00002195 /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
Mitch Williams84590fd2014-04-09 05:58:57 +00002196 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2197 max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2198 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
Mitch Williams6b192892014-03-06 09:02:29 +00002199 if (ret) {
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002200 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
Mitch Williams6b192892014-03-06 09:02:29 +00002201 ret);
2202 ret = -EIO;
2203 goto error;
2204 }
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002205 vf->tx_rate = max_tx_rate;
Mitch Williams6b192892014-03-06 09:02:29 +00002206error:
2207 return ret;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002208}
2209
2210/**
2211 * i40e_ndo_get_vf_config
2212 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002213 * @vf_id: VF identifier
2214 * @ivi: VF configuration structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002215 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002216 * return VF configuration
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002217 **/
2218int i40e_ndo_get_vf_config(struct net_device *netdev,
2219 int vf_id, struct ifla_vf_info *ivi)
2220{
2221 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002222 struct i40e_vsi *vsi = np->vsi;
2223 struct i40e_pf *pf = vsi->back;
2224 struct i40e_vf *vf;
2225 int ret = 0;
2226
2227 /* validate the request */
2228 if (vf_id >= pf->num_alloc_vfs) {
2229 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2230 ret = -EINVAL;
2231 goto error_param;
2232 }
2233
2234 vf = &(pf->vf[vf_id]);
2235 /* first vsi is always the LAN vsi */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002236 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002237 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2238 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2239 ret = -EINVAL;
2240 goto error_param;
2241 }
2242
2243 ivi->vf = vf_id;
2244
Mitch Williamsf4a1c5c2013-11-28 06:39:34 +00002245 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002246
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002247 ivi->max_tx_rate = vf->tx_rate;
2248 ivi->min_tx_rate = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002249 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2250 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2251 I40E_VLAN_PRIORITY_SHIFT;
Mitch Williams84ca55a2014-03-14 07:32:24 +00002252 if (vf->link_forced == false)
2253 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2254 else if (vf->link_up == true)
2255 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2256 else
2257 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
Mitch Williamsc674d122014-05-20 08:01:40 +00002258 ivi->spoofchk = vf->spoofchk;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002259 ret = 0;
2260
2261error_param:
2262 return ret;
2263}
Mitch Williams588aefa2014-02-11 08:27:49 +00002264
2265/**
2266 * i40e_ndo_set_vf_link_state
2267 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002268 * @vf_id: VF identifier
Mitch Williams588aefa2014-02-11 08:27:49 +00002269 * @link: required link state
2270 *
2271 * Set the link state of a specified VF, regardless of physical link state
2272 **/
2273int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
2274{
2275 struct i40e_netdev_priv *np = netdev_priv(netdev);
2276 struct i40e_pf *pf = np->vsi->back;
2277 struct i40e_virtchnl_pf_event pfe;
2278 struct i40e_hw *hw = &pf->hw;
2279 struct i40e_vf *vf;
Ashish Shahf19efbb2014-08-01 13:27:06 -07002280 int abs_vf_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002281 int ret = 0;
2282
2283 /* validate the request */
2284 if (vf_id >= pf->num_alloc_vfs) {
2285 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2286 ret = -EINVAL;
2287 goto error_out;
2288 }
2289
2290 vf = &pf->vf[vf_id];
Ashish Shahf19efbb2014-08-01 13:27:06 -07002291 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002292
2293 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
2294 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
2295
2296 switch (link) {
2297 case IFLA_VF_LINK_STATE_AUTO:
2298 vf->link_forced = false;
2299 pfe.event_data.link_event.link_status =
2300 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
2301 pfe.event_data.link_event.link_speed =
2302 pf->hw.phy.link_info.link_speed;
2303 break;
2304 case IFLA_VF_LINK_STATE_ENABLE:
2305 vf->link_forced = true;
2306 vf->link_up = true;
2307 pfe.event_data.link_event.link_status = true;
2308 pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
2309 break;
2310 case IFLA_VF_LINK_STATE_DISABLE:
2311 vf->link_forced = true;
2312 vf->link_up = false;
2313 pfe.event_data.link_event.link_status = false;
2314 pfe.event_data.link_event.link_speed = 0;
2315 break;
2316 default:
2317 ret = -EINVAL;
2318 goto error_out;
2319 }
2320 /* Notify the VF of its new link state */
Ashish Shahf19efbb2014-08-01 13:27:06 -07002321 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Mitch Williams588aefa2014-02-11 08:27:49 +00002322 0, (u8 *)&pfe, sizeof(pfe), NULL);
2323
2324error_out:
2325 return ret;
2326}
Mitch Williamsc674d122014-05-20 08:01:40 +00002327
2328/**
2329 * i40e_ndo_set_vf_spoofchk
2330 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002331 * @vf_id: VF identifier
Mitch Williamsc674d122014-05-20 08:01:40 +00002332 * @enable: flag to enable or disable feature
2333 *
2334 * Enable or disable VF spoof checking
2335 **/
Serey Konge6d90042014-07-12 07:28:14 +00002336int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
Mitch Williamsc674d122014-05-20 08:01:40 +00002337{
2338 struct i40e_netdev_priv *np = netdev_priv(netdev);
2339 struct i40e_vsi *vsi = np->vsi;
2340 struct i40e_pf *pf = vsi->back;
2341 struct i40e_vsi_context ctxt;
2342 struct i40e_hw *hw = &pf->hw;
2343 struct i40e_vf *vf;
2344 int ret = 0;
2345
2346 /* validate the request */
2347 if (vf_id >= pf->num_alloc_vfs) {
2348 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2349 ret = -EINVAL;
2350 goto out;
2351 }
2352
2353 vf = &(pf->vf[vf_id]);
2354
2355 if (enable == vf->spoofchk)
2356 goto out;
2357
2358 vf->spoofchk = enable;
2359 memset(&ctxt, 0, sizeof(ctxt));
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002360 ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
Mitch Williamsc674d122014-05-20 08:01:40 +00002361 ctxt.pf_num = pf->hw.pf_id;
2362 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
2363 if (enable)
Greg Rose30d71af2015-01-29 07:17:17 +00002364 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
2365 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
Mitch Williamsc674d122014-05-20 08:01:40 +00002366 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
2367 if (ret) {
2368 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
2369 ret);
2370 ret = -EIO;
2371 }
2372out:
2373 return ret;
2374}