blob: 8df2b529289bba317047d910655dbd8a183713f8 [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"
Mitch Williams055b2952015-04-07 19:45:33 -040028static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000029
30/***********************misc routines*****************************/
31
32/**
Greg Rosef9b4b622014-03-06 09:02:28 +000033 * i40e_vc_disable_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000034 * @pf: pointer to the PF info
35 * @vf: pointer to the VF info
Greg Rosef9b4b622014-03-06 09:02:28 +000036 *
37 * Disable the VF through a SW reset
38 **/
39static inline void i40e_vc_disable_vf(struct i40e_pf *pf, struct i40e_vf *vf)
40{
41 struct i40e_hw *hw = &pf->hw;
42 u32 reg;
43
44 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
45 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
46 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
47 i40e_flush(hw);
48}
49
50/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000051 * i40e_vc_isvalid_vsi_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000052 * @vf: pointer to the VF info
53 * @vsi_id: VF relative VSI id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000054 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000055 * check for the valid VSI id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000056 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -070057static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000058{
59 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -070060 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000061
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -070062 return (vsi && (vsi->vf_id == vf->vf_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000063}
64
65/**
66 * i40e_vc_isvalid_queue_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000067 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000068 * @vsi_id: vsi id
69 * @qid: vsi relative queue id
70 *
71 * check for the valid queue id
72 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -070073static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000074 u8 qid)
75{
76 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -070077 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000078
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -070079 return (vsi && (qid < vsi->alloc_queue_pairs));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000080}
81
82/**
83 * i40e_vc_isvalid_vector_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +000084 * @vf: pointer to the VF info
85 * @vector_id: VF relative vector id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000086 *
87 * check for the valid vector id
88 **/
89static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
90{
91 struct i40e_pf *pf = vf->pf;
92
Mitch Williams9347eb72014-02-11 08:26:32 +000093 return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000094}
95
96/***********************vf resource mgmt routines*****************/
97
98/**
99 * i40e_vc_get_pf_queue_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000100 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700101 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000102 * @vsi_queue_id: vsi relative queue id
103 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000104 * return PF relative queue id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000105 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700106static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000107 u8 vsi_queue_id)
108{
109 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700110 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000111 u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
112
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700113 if (!vsi)
114 return pf_queue_id;
115
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000116 if (le16_to_cpu(vsi->info.mapping_flags) &
117 I40E_AQ_VSI_QUE_MAP_NONCONTIG)
118 pf_queue_id =
119 le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
120 else
121 pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
122 vsi_queue_id;
123
124 return pf_queue_id;
125}
126
127/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000128 * i40e_config_irq_link_list
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000129 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700130 * @vsi_id: id of VSI as given by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000131 * @vecmap: irq map info
132 *
133 * configure irq link list from the map
134 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700135static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000136 struct i40e_virtchnl_vector_map *vecmap)
137{
138 unsigned long linklistmap = 0, tempmap;
139 struct i40e_pf *pf = vf->pf;
140 struct i40e_hw *hw = &pf->hw;
141 u16 vsi_queue_id, pf_queue_id;
142 enum i40e_queue_type qtype;
143 u16 next_q, vector_id;
144 u32 reg, reg_idx;
145 u16 itr_idx = 0;
146
147 vector_id = vecmap->vector_id;
148 /* setup the head */
149 if (0 == vector_id)
150 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
151 else
152 reg_idx = I40E_VPINT_LNKLSTN(
Mitch Williams9347eb72014-02-11 08:26:32 +0000153 ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
154 (vector_id - 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000155
156 if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
157 /* Special case - No queues mapped on this vector */
158 wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
159 goto irq_list_done;
160 }
161 tempmap = vecmap->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000162 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000163 linklistmap |= (1 <<
164 (I40E_VIRTCHNL_SUPPORTED_QTYPES *
165 vsi_queue_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000166 }
167
168 tempmap = vecmap->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000169 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000170 linklistmap |= (1 <<
171 (I40E_VIRTCHNL_SUPPORTED_QTYPES * vsi_queue_id
172 + 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000173 }
174
175 next_q = find_first_bit(&linklistmap,
176 (I40E_MAX_VSI_QP *
177 I40E_VIRTCHNL_SUPPORTED_QTYPES));
178 vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
179 qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700180 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000181 reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
182
183 wr32(hw, reg_idx, reg);
184
185 while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
186 switch (qtype) {
187 case I40E_QUEUE_TYPE_RX:
188 reg_idx = I40E_QINT_RQCTL(pf_queue_id);
189 itr_idx = vecmap->rxitr_idx;
190 break;
191 case I40E_QUEUE_TYPE_TX:
192 reg_idx = I40E_QINT_TQCTL(pf_queue_id);
193 itr_idx = vecmap->txitr_idx;
194 break;
195 default:
196 break;
197 }
198
199 next_q = find_next_bit(&linklistmap,
200 (I40E_MAX_VSI_QP *
201 I40E_VIRTCHNL_SUPPORTED_QTYPES),
202 next_q + 1);
Mitch Williams829af3a2013-12-18 13:46:00 +0000203 if (next_q <
204 (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000205 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
206 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700207 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000208 vsi_queue_id);
209 } else {
210 pf_queue_id = I40E_QUEUE_END_OF_LIST;
211 qtype = 0;
212 }
213
214 /* format for the RQCTL & TQCTL regs is same */
215 reg = (vector_id) |
216 (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
217 (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
218 (1 << I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
219 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
220 wr32(hw, reg_idx, reg);
221 }
222
223irq_list_done:
224 i40e_flush(hw);
225}
226
227/**
228 * i40e_config_vsi_tx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000229 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700230 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000231 * @vsi_queue_id: vsi relative queue index
232 * @info: config. info
233 *
234 * configure tx queue
235 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700236static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000237 u16 vsi_queue_id,
238 struct i40e_virtchnl_txq_info *info)
239{
240 struct i40e_pf *pf = vf->pf;
241 struct i40e_hw *hw = &pf->hw;
242 struct i40e_hmc_obj_txq tx_ctx;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700243 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000244 u16 pf_queue_id;
245 u32 qtx_ctl;
246 int ret = 0;
247
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700248 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
249 vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000250
251 /* clear the context structure first */
252 memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
253
254 /* only set the required fields */
255 tx_ctx.base = info->dma_ring_addr / 128;
256 tx_ctx.qlen = info->ring_len;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700257 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000258 tx_ctx.rdylist_act = 0;
Ashish Shah5d298962014-05-22 06:31:25 +0000259 tx_ctx.head_wb_ena = info->headwb_enabled;
260 tx_ctx.head_wb_addr = info->dma_headwb_addr;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000261
262 /* clear the context in the HMC */
263 ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
264 if (ret) {
265 dev_err(&pf->pdev->dev,
266 "Failed to clear VF LAN Tx queue context %d, error: %d\n",
267 pf_queue_id, ret);
268 ret = -ENOENT;
269 goto error_context;
270 }
271
272 /* set the context in the HMC */
273 ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
274 if (ret) {
275 dev_err(&pf->pdev->dev,
276 "Failed to set VF LAN Tx queue context %d error: %d\n",
277 pf_queue_id, ret);
278 ret = -ENOENT;
279 goto error_context;
280 }
281
282 /* associate this queue with the PCI VF function */
283 qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +0000284 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000285 & I40E_QTX_CTL_PF_INDX_MASK);
286 qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
287 << I40E_QTX_CTL_VFVM_INDX_SHIFT)
288 & I40E_QTX_CTL_VFVM_INDX_MASK);
289 wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
290 i40e_flush(hw);
291
292error_context:
293 return ret;
294}
295
296/**
297 * i40e_config_vsi_rx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000298 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700299 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000300 * @vsi_queue_id: vsi relative queue index
301 * @info: config. info
302 *
303 * configure rx queue
304 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700305static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000306 u16 vsi_queue_id,
307 struct i40e_virtchnl_rxq_info *info)
308{
309 struct i40e_pf *pf = vf->pf;
310 struct i40e_hw *hw = &pf->hw;
311 struct i40e_hmc_obj_rxq rx_ctx;
312 u16 pf_queue_id;
313 int ret = 0;
314
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700315 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000316
317 /* clear the context structure first */
318 memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
319
320 /* only set the required fields */
321 rx_ctx.base = info->dma_ring_addr / 128;
322 rx_ctx.qlen = info->ring_len;
323
324 if (info->splithdr_enabled) {
325 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
326 I40E_RX_SPLIT_IP |
327 I40E_RX_SPLIT_TCP_UDP |
328 I40E_RX_SPLIT_SCTP;
329 /* header length validation */
330 if (info->hdr_size > ((2 * 1024) - 64)) {
331 ret = -EINVAL;
332 goto error_param;
333 }
334 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
335
336 /* set splitalways mode 10b */
337 rx_ctx.dtype = 0x2;
338 }
339
340 /* databuffer length validation */
341 if (info->databuffer_size > ((16 * 1024) - 128)) {
342 ret = -EINVAL;
343 goto error_param;
344 }
345 rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
346
347 /* max pkt. length validation */
348 if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
349 ret = -EINVAL;
350 goto error_param;
351 }
352 rx_ctx.rxmax = info->max_pkt_size;
353
354 /* enable 32bytes desc always */
355 rx_ctx.dsize = 1;
356
357 /* default values */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000358 rx_ctx.lrxqthresh = 2;
359 rx_ctx.crcstrip = 1;
Mitch Williams50d41652014-04-09 05:58:55 +0000360 rx_ctx.prefena = 1;
Shannon Nelsonc1d11ce2014-07-29 04:01:03 +0000361 rx_ctx.l2tsel = 1;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000362
363 /* clear the context in the HMC */
364 ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
365 if (ret) {
366 dev_err(&pf->pdev->dev,
367 "Failed to clear VF LAN Rx queue context %d, error: %d\n",
368 pf_queue_id, ret);
369 ret = -ENOENT;
370 goto error_param;
371 }
372
373 /* set the context in the HMC */
374 ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
375 if (ret) {
376 dev_err(&pf->pdev->dev,
377 "Failed to set VF LAN Rx queue context %d error: %d\n",
378 pf_queue_id, ret);
379 ret = -ENOENT;
380 goto error_param;
381 }
382
383error_param:
384 return ret;
385}
386
387/**
388 * i40e_alloc_vsi_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000389 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000390 * @type: type of VSI to allocate
391 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000392 * alloc VF vsi context & resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000393 **/
394static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
395{
396 struct i40e_mac_filter *f = NULL;
397 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000398 struct i40e_vsi *vsi;
399 int ret = 0;
400
401 vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
402
403 if (!vsi) {
404 dev_err(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000405 "add vsi failed for VF %d, aq_err %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000406 vf->vf_id, pf->hw.aq.asq_last_status);
407 ret = -ENOENT;
408 goto error_alloc_vsi_res;
409 }
410 if (type == I40E_VSI_SRIOV) {
Greg Rose1a103702013-11-28 06:42:39 +0000411 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700412 vf->lan_vsi_idx = vsi->idx;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000413 vf->lan_vsi_id = vsi->id;
Greg Rose6c12fcb2013-11-28 06:39:34 +0000414 /* If the port VLAN has been configured and then the
415 * VF driver was removed then the VSI port VLAN
416 * configuration was destroyed. Check if there is
417 * a port VLAN and restore the VSI configuration if
418 * needed.
419 */
420 if (vf->port_vlan_id)
421 i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000422 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
Greg Rose6c12fcb2013-11-28 06:39:34 +0000423 vf->port_vlan_id, true, false);
Greg Rose1a103702013-11-28 06:42:39 +0000424 if (!f)
425 dev_info(&pf->pdev->dev,
426 "Could not allocate VF MAC addr\n");
427 f = i40e_add_filter(vsi, brdcast, vf->port_vlan_id,
428 true, false);
429 if (!f)
430 dev_info(&pf->pdev->dev,
431 "Could not allocate VF broadcast filter\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000432 }
Neerav Parikh6dbbbfb2013-11-26 10:49:24 +0000433
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000434 /* program mac filter */
435 ret = i40e_sync_vsi_filters(vsi);
Mitch Williamsfd1646e2014-02-13 03:48:44 -0800436 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000437 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000438
Mitch Williams6b192892014-03-06 09:02:29 +0000439 /* Set VF bandwidth if specified */
440 if (vf->tx_rate) {
441 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
442 vf->tx_rate / 50, 0, NULL);
443 if (ret)
444 dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
445 vf->vf_id, ret);
446 }
447
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000448error_alloc_vsi_res:
449 return ret;
450}
451
452/**
Mitch Williams805bd5b2013-11-28 06:39:26 +0000453 * i40e_enable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000454 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000455 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000456 * enable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000457 **/
458static void i40e_enable_vf_mappings(struct i40e_vf *vf)
459{
460 struct i40e_pf *pf = vf->pf;
461 struct i40e_hw *hw = &pf->hw;
462 u32 reg, total_queue_pairs = 0;
463 int j;
464
465 /* Tell the hardware we're using noncontiguous mapping. HW requires
466 * that VF queues be mapped using this method, even when they are
467 * contiguous in real life
468 */
469 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
470 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
471
472 /* enable VF vplan_qtable mappings */
473 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
474 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
475
476 /* map PF queues to VF queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700477 for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
478 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
Mitch Williams805bd5b2013-11-28 06:39:26 +0000479 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
480 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
481 total_queue_pairs++;
482 }
483
484 /* map PF queues to VSI */
485 for (j = 0; j < 7; j++) {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700486 if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
Mitch Williams805bd5b2013-11-28 06:39:26 +0000487 reg = 0x07FF07FF; /* unused */
488 } else {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700489 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000490 j * 2);
491 reg = qid;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700492 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000493 (j * 2) + 1);
494 reg |= qid << 16;
495 }
496 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
497 }
498
499 i40e_flush(hw);
500}
501
502/**
503 * i40e_disable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000504 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000505 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000506 * disable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000507 **/
508static void i40e_disable_vf_mappings(struct i40e_vf *vf)
509{
510 struct i40e_pf *pf = vf->pf;
511 struct i40e_hw *hw = &pf->hw;
512 int i;
513
514 /* disable qp mappings */
515 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
516 for (i = 0; i < I40E_MAX_VSI_QP; i++)
517 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
518 I40E_QUEUE_END_OF_LIST);
519 i40e_flush(hw);
520}
521
522/**
523 * i40e_free_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000524 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000525 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000526 * free VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000527 **/
528static void i40e_free_vf_res(struct i40e_vf *vf)
529{
530 struct i40e_pf *pf = vf->pf;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000531 struct i40e_hw *hw = &pf->hw;
532 u32 reg_idx, reg;
533 int i, msix_vf;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000534
535 /* free vsi & disconnect it from the parent uplink */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700536 if (vf->lan_vsi_idx) {
537 i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
538 vf->lan_vsi_idx = 0;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000539 vf->lan_vsi_id = 0;
540 }
Mitch Williams9347eb72014-02-11 08:26:32 +0000541 msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
542
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000543 /* disable interrupts so the VF starts in a known state */
544 for (i = 0; i < msix_vf; i++) {
545 /* format is same for both registers */
546 if (0 == i)
547 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
548 else
549 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
550 (vf->vf_id))
551 + (i - 1));
552 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
553 i40e_flush(hw);
554 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000555
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000556 /* clear the irq settings */
557 for (i = 0; i < msix_vf; i++) {
558 /* format is same for both registers */
559 if (0 == i)
560 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
561 else
562 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
563 (vf->vf_id))
564 + (i - 1));
565 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
566 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
567 wr32(hw, reg_idx, reg);
568 i40e_flush(hw);
569 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000570 /* reset some of the state varibles keeping
571 * track of the resources
572 */
573 vf->num_queue_pairs = 0;
574 vf->vf_states = 0;
575}
576
577/**
578 * i40e_alloc_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000579 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000580 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000581 * allocate VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000582 **/
583static int i40e_alloc_vf_res(struct i40e_vf *vf)
584{
585 struct i40e_pf *pf = vf->pf;
586 int total_queue_pairs = 0;
587 int ret;
588
589 /* allocate hw vsi context & associated resources */
590 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
591 if (ret)
592 goto error_alloc;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700593 total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000594 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
595
596 /* store the total qps number for the runtime
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000597 * VF req validation
Mitch Williams805bd5b2013-11-28 06:39:26 +0000598 */
599 vf->num_queue_pairs = total_queue_pairs;
600
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000601 /* VF is now completely initialized */
Mitch Williams805bd5b2013-11-28 06:39:26 +0000602 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
603
604error_alloc:
605 if (ret)
606 i40e_free_vf_res(vf);
607
608 return ret;
609}
610
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000611#define VF_DEVICE_STATUS 0xAA
612#define VF_TRANS_PENDING_MASK 0x20
613/**
614 * i40e_quiesce_vf_pci
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000615 * @vf: pointer to the VF structure
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000616 *
617 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
618 * if the transactions never clear.
619 **/
620static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
621{
622 struct i40e_pf *pf = vf->pf;
623 struct i40e_hw *hw = &pf->hw;
624 int vf_abs_id, i;
625 u32 reg;
626
Mitch Williamsb141d612013-11-28 06:39:36 +0000627 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000628
629 wr32(hw, I40E_PF_PCI_CIAA,
630 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
631 for (i = 0; i < 100; i++) {
632 reg = rd32(hw, I40E_PF_PCI_CIAD);
633 if ((reg & VF_TRANS_PENDING_MASK) == 0)
634 return 0;
635 udelay(1);
636 }
637 return -EIO;
638}
639
Mitch Williams805bd5b2013-11-28 06:39:26 +0000640/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000641 * i40e_reset_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000642 * @vf: pointer to the VF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000643 * @flr: VFLR was issued or not
644 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000645 * reset the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000646 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000647void i40e_reset_vf(struct i40e_vf *vf, bool flr)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000648{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000649 struct i40e_pf *pf = vf->pf;
650 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000651 bool rsd = false;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000652 int i;
653 u32 reg;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000654
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000655 if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
656 return;
657
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000658 /* warn the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000659 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
660
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000661 /* In the case of a VFLR, the HW has already reset the VF and we
662 * just need to clean up, so don't hit the VFRTRIG register.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000663 */
664 if (!flr) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000665 /* reset VF using VPGEN_VFRTRIG reg */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000666 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
667 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000668 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
669 i40e_flush(hw);
670 }
671
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000672 if (i40e_quiesce_vf_pci(vf))
673 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
674 vf->vf_id);
675
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000676 /* poll VPGEN_VFRSTAT reg to make sure
677 * that reset is complete
678 */
Mitch Williams1750a222015-01-09 11:18:13 +0000679 for (i = 0; i < 10; i++) {
680 /* VF reset requires driver to first reset the VF and then
681 * poll the status register to make sure that the reset
682 * completed successfully. Due to internal HW FIFO flushes,
683 * we must wait 10ms before the register will be valid.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000684 */
Mitch Williams1750a222015-01-09 11:18:13 +0000685 usleep_range(10000, 20000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000686 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
687 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
688 rsd = true;
689 break;
690 }
691 }
692
693 if (!rsd)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000694 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000695 vf->vf_id);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000696 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000697 /* clear the reset bit in the VPGEN_VFRTRIG reg */
698 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
699 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
700 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000701
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000702 /* On initial reset, we won't have any queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700703 if (vf->lan_vsi_idx == 0)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000704 goto complete_reset;
705
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700706 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000707complete_reset:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000708 /* reallocate VF resources to reset the VSI state */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000709 i40e_free_vf_res(vf);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000710 i40e_alloc_vf_res(vf);
711 i40e_enable_vf_mappings(vf);
Mitch Williamsc17b3622014-02-13 03:48:45 -0800712 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000713
714 /* tell the VF the reset is done */
715 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
716 i40e_flush(hw);
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000717 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000718}
Greg Rosec3542292013-12-13 08:38:38 +0000719
720/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000721 * i40e_free_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000722 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000723 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000724 * free VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000725 **/
726void i40e_free_vfs(struct i40e_pf *pf)
727{
Mitch Williamsf7414532013-11-28 06:39:40 +0000728 struct i40e_hw *hw = &pf->hw;
729 u32 reg_idx, bit_idx;
730 int i, tmp, vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000731
732 if (!pf->vf)
733 return;
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000734 while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
735 usleep_range(1000, 2000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000736
Mitch Williams44434632015-04-07 11:32:55 -0700737 for (i = 0; i < pf->num_alloc_vfs; i++)
738 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
739 i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
740 false);
741
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000742 /* Disable IOV before freeing resources. This lets any VF drivers
743 * running in the host get themselves cleaned up before we yank
744 * the carpet out from underneath their feet.
745 */
746 if (!pci_vfs_assigned(pf->pdev))
747 pci_disable_sriov(pf->pdev);
Mitch Williams6d7b9672015-03-31 00:45:02 -0700748 else
749 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000750
751 msleep(20); /* let any messages in transit get finished up */
752
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000753 /* free up VF resources */
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000754 tmp = pf->num_alloc_vfs;
755 pf->num_alloc_vfs = 0;
756 for (i = 0; i < tmp; i++) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000757 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
758 i40e_free_vf_res(&pf->vf[i]);
759 /* disable qp mappings */
760 i40e_disable_vf_mappings(&pf->vf[i]);
761 }
762
763 kfree(pf->vf);
764 pf->vf = NULL;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000765
Mitch Williams9e5634d2014-04-04 04:43:14 +0000766 /* This check is for when the driver is unloaded while VFs are
767 * assigned. Setting the number of VFs to 0 through sysfs is caught
768 * before this function ever gets called.
769 */
Ethan Zhaoc24817b2014-07-22 18:36:43 +0000770 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williamsf7414532013-11-28 06:39:40 +0000771 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
772 * work correctly when SR-IOV gets re-enabled.
773 */
774 for (vf_id = 0; vf_id < tmp; vf_id++) {
775 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
776 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
777 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
778 }
Greg Rosec3542292013-12-13 08:38:38 +0000779 }
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000780 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000781}
782
783#ifdef CONFIG_PCI_IOV
784/**
785 * i40e_alloc_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000786 * @pf: pointer to the PF structure
787 * @num_alloc_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000788 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000789 * allocate VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000790 **/
Mitch Williams4aeec012014-02-13 03:48:47 -0800791int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000792{
793 struct i40e_vf *vfs;
794 int i, ret = 0;
795
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000796 /* Disable interrupt 0 so we don't try to handle the VFLR. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000797 i40e_irq_dynamic_disable_icr0(pf);
798
Mitch Williams4aeec012014-02-13 03:48:47 -0800799 /* Check to see if we're just allocating resources for extant VFs */
800 if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
801 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
802 if (ret) {
803 dev_err(&pf->pdev->dev,
804 "Failed to enable SR-IOV, error %d.\n", ret);
805 pf->num_alloc_vfs = 0;
806 goto err_iov;
807 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000808 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000809 /* allocate memory */
Akeem G Abodunrincc6456a2014-02-06 05:51:02 +0000810 vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000811 if (!vfs) {
812 ret = -ENOMEM;
813 goto err_alloc;
814 }
Mitch Williamsc674d122014-05-20 08:01:40 +0000815 pf->vf = vfs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000816
817 /* apply default profile */
818 for (i = 0; i < num_alloc_vfs; i++) {
819 vfs[i].pf = pf;
820 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
821 vfs[i].vf_id = i;
822
823 /* assign default capabilities */
824 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
Mitch Williamsc674d122014-05-20 08:01:40 +0000825 vfs[i].spoofchk = true;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000826 /* VF resources get allocated during reset */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000827 i40e_reset_vf(&vfs[i], false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000828
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000829 /* enable VF vplan_qtable mappings */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000830 i40e_enable_vf_mappings(&vfs[i]);
831 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000832 pf->num_alloc_vfs = num_alloc_vfs;
833
834err_alloc:
835 if (ret)
836 i40e_free_vfs(pf);
837err_iov:
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000838 /* Re-enable interrupt 0. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000839 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000840 return ret;
841}
842
843#endif
844/**
845 * i40e_pci_sriov_enable
846 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000847 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000848 *
849 * Enable or change the number of VFs
850 **/
851static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
852{
853#ifdef CONFIG_PCI_IOV
854 struct i40e_pf *pf = pci_get_drvdata(pdev);
855 int pre_existing_vfs = pci_num_vf(pdev);
856 int err = 0;
857
858 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
859 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
860 i40e_free_vfs(pf);
861 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
862 goto out;
863
864 if (num_vfs > pf->num_req_vfs) {
865 err = -EPERM;
866 goto err_out;
867 }
868
869 err = i40e_alloc_vfs(pf, num_vfs);
870 if (err) {
871 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
872 goto err_out;
873 }
874
875out:
876 return num_vfs;
877
878err_out:
879 return err;
880#endif
881 return 0;
882}
883
884/**
885 * i40e_pci_sriov_configure
886 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000887 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000888 *
889 * Enable or change the number of VFs. Called when the user updates the number
890 * of VFs in sysfs.
891 **/
892int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
893{
894 struct i40e_pf *pf = pci_get_drvdata(pdev);
895
896 if (num_vfs)
897 return i40e_pci_sriov_enable(pdev, num_vfs);
898
Ethan Zhaoc24817b2014-07-22 18:36:43 +0000899 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williams9e5634d2014-04-04 04:43:14 +0000900 i40e_free_vfs(pf);
901 } else {
902 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
903 return -EINVAL;
904 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000905 return 0;
906}
907
908/***********************virtual channel routines******************/
909
910/**
911 * i40e_vc_send_msg_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000912 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000913 * @v_opcode: virtual channel opcode
914 * @v_retval: virtual channel return value
915 * @msg: pointer to the msg buffer
916 * @msglen: msg length
917 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000918 * send msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000919 **/
920static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
921 u32 v_retval, u8 *msg, u16 msglen)
922{
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +0000923 struct i40e_pf *pf;
924 struct i40e_hw *hw;
925 int abs_vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000926 i40e_status aq_ret;
927
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +0000928 /* validate the request */
929 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
930 return -EINVAL;
931
932 pf = vf->pf;
933 hw = &pf->hw;
934 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
935
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000936 /* single place to detect unsuccessful return values */
937 if (v_retval) {
938 vf->num_invalid_msgs++;
939 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
940 v_opcode, v_retval);
941 if (vf->num_invalid_msgs >
942 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
943 dev_err(&pf->pdev->dev,
944 "Number of invalid messages exceeded for VF %d\n",
945 vf->vf_id);
946 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
947 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
948 }
949 } else {
950 vf->num_valid_msgs++;
951 }
952
Ashish Shahf19efbb2014-08-01 13:27:06 -0700953 aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Mitch Williams7efa84b2013-11-28 06:39:41 +0000954 msg, msglen, NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000955 if (aq_ret) {
956 dev_err(&pf->pdev->dev,
957 "Unable to send the message to VF %d aq_err %d\n",
958 vf->vf_id, pf->hw.aq.asq_last_status);
959 return -EIO;
960 }
961
962 return 0;
963}
964
965/**
966 * i40e_vc_send_resp_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000967 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000968 * @opcode: operation code
969 * @retval: return value
970 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000971 * send resp msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000972 **/
973static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
974 enum i40e_virtchnl_ops opcode,
975 i40e_status retval)
976{
977 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
978}
979
980/**
981 * i40e_vc_get_version_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000982 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000983 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000984 * called from the VF to request the API version used by the PF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000985 **/
986static int i40e_vc_get_version_msg(struct i40e_vf *vf)
987{
988 struct i40e_virtchnl_version_info info = {
989 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
990 };
991
992 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
993 I40E_SUCCESS, (u8 *)&info,
994 sizeof(struct
995 i40e_virtchnl_version_info));
996}
997
998/**
999 * i40e_vc_get_vf_resources_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001000 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001001 * @msg: pointer to the msg buffer
1002 * @msglen: msg length
1003 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001004 * called from the VF to request its resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001005 **/
1006static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf)
1007{
1008 struct i40e_virtchnl_vf_resource *vfres = NULL;
1009 struct i40e_pf *pf = vf->pf;
1010 i40e_status aq_ret = 0;
1011 struct i40e_vsi *vsi;
1012 int i = 0, len = 0;
1013 int num_vsis = 1;
1014 int ret;
1015
1016 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1017 aq_ret = I40E_ERR_PARAM;
1018 goto err;
1019 }
1020
1021 len = (sizeof(struct i40e_virtchnl_vf_resource) +
1022 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1023
1024 vfres = kzalloc(len, GFP_KERNEL);
1025 if (!vfres) {
1026 aq_ret = I40E_ERR_NO_MEMORY;
1027 len = 0;
1028 goto err;
1029 }
1030
1031 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001032 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001033 if (!vsi->info.pvid)
1034 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1035
1036 vfres->num_vsis = num_vsis;
1037 vfres->num_queue_pairs = vf->num_queue_pairs;
1038 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001039 if (vf->lan_vsi_idx) {
1040 vfres->vsi_res[i].vsi_id = vf->lan_vsi_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001041 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1042 vfres->vsi_res[i].num_queue_pairs =
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001043 pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001044 memcpy(vfres->vsi_res[i].default_mac_addr,
1045 vf->default_lan_addr.addr, ETH_ALEN);
1046 i++;
1047 }
1048 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1049
1050err:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001051 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001052 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1053 aq_ret, (u8 *)vfres, len);
1054
1055 kfree(vfres);
1056 return ret;
1057}
1058
1059/**
1060 * i40e_vc_reset_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001061 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001062 * @msg: pointer to the msg buffer
1063 * @msglen: msg length
1064 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001065 * called from the VF to reset itself,
1066 * unlike other virtchnl messages, PF driver
1067 * doesn't send the response back to the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001068 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001069static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001070{
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001071 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1072 i40e_reset_vf(vf, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001073}
1074
1075/**
1076 * i40e_vc_config_promiscuous_mode_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001077 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001078 * @msg: pointer to the msg buffer
1079 * @msglen: msg length
1080 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001081 * called from the VF to configure the promiscuous mode of
1082 * VF vsis
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001083 **/
1084static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1085 u8 *msg, u16 msglen)
1086{
1087 struct i40e_virtchnl_promisc_info *info =
1088 (struct i40e_virtchnl_promisc_info *)msg;
1089 struct i40e_pf *pf = vf->pf;
1090 struct i40e_hw *hw = &pf->hw;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001091 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001092 bool allmulti = false;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001093 i40e_status aq_ret;
1094
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001095 vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001096 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1097 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1098 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001099 (vsi->type != I40E_VSI_FCOE)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001100 aq_ret = I40E_ERR_PARAM;
1101 goto error_param;
1102 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001103 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1104 allmulti = true;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001105 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001106 allmulti, NULL);
1107
1108error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001109 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001110 return i40e_vc_send_resp_to_vf(vf,
1111 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1112 aq_ret);
1113}
1114
1115/**
1116 * i40e_vc_config_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001117 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001118 * @msg: pointer to the msg buffer
1119 * @msglen: msg length
1120 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001121 * called from the VF to configure the rx/tx
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001122 * queues
1123 **/
1124static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1125{
1126 struct i40e_virtchnl_vsi_queue_config_info *qci =
1127 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1128 struct i40e_virtchnl_queue_pair_info *qpi;
Ashish Shah5f5e33b2014-07-10 07:58:15 +00001129 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001130 u16 vsi_id, vsi_queue_id;
1131 i40e_status aq_ret = 0;
1132 int i;
1133
1134 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1135 aq_ret = I40E_ERR_PARAM;
1136 goto error_param;
1137 }
1138
1139 vsi_id = qci->vsi_id;
1140 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1141 aq_ret = I40E_ERR_PARAM;
1142 goto error_param;
1143 }
1144 for (i = 0; i < qci->num_queue_pairs; i++) {
1145 qpi = &qci->qpair[i];
1146 vsi_queue_id = qpi->txq.queue_id;
1147 if ((qpi->txq.vsi_id != vsi_id) ||
1148 (qpi->rxq.vsi_id != vsi_id) ||
1149 (qpi->rxq.queue_id != vsi_queue_id) ||
1150 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1151 aq_ret = I40E_ERR_PARAM;
1152 goto error_param;
1153 }
1154
1155 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1156 &qpi->rxq) ||
1157 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1158 &qpi->txq)) {
1159 aq_ret = I40E_ERR_PARAM;
1160 goto error_param;
1161 }
1162 }
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001163 /* set vsi num_queue_pairs in use to num configured by VF */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001164 pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001165
1166error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001167 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001168 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1169 aq_ret);
1170}
1171
1172/**
1173 * i40e_vc_config_irq_map_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001174 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001175 * @msg: pointer to the msg buffer
1176 * @msglen: msg length
1177 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001178 * called from the VF to configure the irq to
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001179 * queue map
1180 **/
1181static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1182{
1183 struct i40e_virtchnl_irq_map_info *irqmap_info =
1184 (struct i40e_virtchnl_irq_map_info *)msg;
1185 struct i40e_virtchnl_vector_map *map;
1186 u16 vsi_id, vsi_queue_id, vector_id;
1187 i40e_status aq_ret = 0;
1188 unsigned long tempmap;
1189 int i;
1190
1191 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1192 aq_ret = I40E_ERR_PARAM;
1193 goto error_param;
1194 }
1195
1196 for (i = 0; i < irqmap_info->num_vectors; i++) {
1197 map = &irqmap_info->vecmap[i];
1198
1199 vector_id = map->vector_id;
1200 vsi_id = map->vsi_id;
1201 /* validate msg params */
1202 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1203 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1204 aq_ret = I40E_ERR_PARAM;
1205 goto error_param;
1206 }
1207
1208 /* lookout for the invalid queue index */
1209 tempmap = map->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001210 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001211 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1212 vsi_queue_id)) {
1213 aq_ret = I40E_ERR_PARAM;
1214 goto error_param;
1215 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001216 }
1217
1218 tempmap = map->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001219 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001220 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1221 vsi_queue_id)) {
1222 aq_ret = I40E_ERR_PARAM;
1223 goto error_param;
1224 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001225 }
1226
1227 i40e_config_irq_link_list(vf, vsi_id, map);
1228 }
1229error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001230 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001231 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1232 aq_ret);
1233}
1234
1235/**
1236 * i40e_vc_enable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001237 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001238 * @msg: pointer to the msg buffer
1239 * @msglen: msg length
1240 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001241 * called from the VF to enable all or specific queue(s)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001242 **/
1243static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1244{
1245 struct i40e_virtchnl_queue_select *vqs =
1246 (struct i40e_virtchnl_queue_select *)msg;
1247 struct i40e_pf *pf = vf->pf;
1248 u16 vsi_id = vqs->vsi_id;
1249 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001250
1251 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1252 aq_ret = I40E_ERR_PARAM;
1253 goto error_param;
1254 }
1255
1256 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1257 aq_ret = I40E_ERR_PARAM;
1258 goto error_param;
1259 }
1260
1261 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1262 aq_ret = I40E_ERR_PARAM;
1263 goto error_param;
1264 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001265
1266 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], true))
Mitch Williams88f65632013-11-28 06:39:28 +00001267 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001268error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001269 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001270 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1271 aq_ret);
1272}
1273
1274/**
1275 * i40e_vc_disable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001276 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001277 * @msg: pointer to the msg buffer
1278 * @msglen: msg length
1279 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001280 * called from the VF to disable all or specific
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001281 * queue(s)
1282 **/
1283static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1284{
1285 struct i40e_virtchnl_queue_select *vqs =
1286 (struct i40e_virtchnl_queue_select *)msg;
1287 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001288 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001289
1290 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1291 aq_ret = I40E_ERR_PARAM;
1292 goto error_param;
1293 }
1294
1295 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1296 aq_ret = I40E_ERR_PARAM;
1297 goto error_param;
1298 }
1299
1300 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1301 aq_ret = I40E_ERR_PARAM;
1302 goto error_param;
1303 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001304
1305 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false))
Mitch Williams88f65632013-11-28 06:39:28 +00001306 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001307
1308error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001309 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001310 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1311 aq_ret);
1312}
1313
1314/**
1315 * i40e_vc_get_stats_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001316 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001317 * @msg: pointer to the msg buffer
1318 * @msglen: msg length
1319 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001320 * called from the VF to get vsi stats
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001321 **/
1322static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1323{
1324 struct i40e_virtchnl_queue_select *vqs =
1325 (struct i40e_virtchnl_queue_select *)msg;
1326 struct i40e_pf *pf = vf->pf;
1327 struct i40e_eth_stats stats;
1328 i40e_status aq_ret = 0;
1329 struct i40e_vsi *vsi;
1330
1331 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1332
1333 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1334 aq_ret = I40E_ERR_PARAM;
1335 goto error_param;
1336 }
1337
1338 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1339 aq_ret = I40E_ERR_PARAM;
1340 goto error_param;
1341 }
1342
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001343 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001344 if (!vsi) {
1345 aq_ret = I40E_ERR_PARAM;
1346 goto error_param;
1347 }
1348 i40e_update_eth_stats(vsi);
Mitch Williams5a9769c2013-11-28 06:39:38 +00001349 stats = vsi->eth_stats;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001350
1351error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001352 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001353 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1354 (u8 *)&stats, sizeof(stats));
1355}
1356
1357/**
Greg Rosef657a6e2013-11-28 06:39:42 +00001358 * i40e_check_vf_permission
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001359 * @vf: pointer to the VF info
Greg Rosef657a6e2013-11-28 06:39:42 +00001360 * @macaddr: pointer to the MAC Address being checked
1361 *
1362 * Check if the VF has permission to add or delete unicast MAC address
1363 * filters and return error code -EPERM if not. Then check if the
1364 * address filter requested is broadcast or zero and if so return
1365 * an invalid MAC address error code.
1366 **/
1367static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1368{
1369 struct i40e_pf *pf = vf->pf;
1370 int ret = 0;
1371
1372 if (is_broadcast_ether_addr(macaddr) ||
1373 is_zero_ether_addr(macaddr)) {
1374 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1375 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rose5017c2a2013-12-07 10:36:54 +00001376 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1377 !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001378 /* If the host VMM administrator has set the VF MAC address
1379 * administratively via the ndo_set_vf_mac command then deny
1380 * permission to the VF to add or delete unicast MAC addresses.
Greg Rose5017c2a2013-12-07 10:36:54 +00001381 * The VF may request to set the MAC address filter already
1382 * assigned to it so do not return an error in that case.
Greg Rosef657a6e2013-11-28 06:39:42 +00001383 */
1384 dev_err(&pf->pdev->dev,
1385 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1386 ret = -EPERM;
1387 }
1388 return ret;
1389}
1390
1391/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001392 * i40e_vc_add_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001393 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001394 * @msg: pointer to the msg buffer
1395 * @msglen: msg length
1396 *
1397 * add guest mac address filter
1398 **/
1399static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1400{
1401 struct i40e_virtchnl_ether_addr_list *al =
1402 (struct i40e_virtchnl_ether_addr_list *)msg;
1403 struct i40e_pf *pf = vf->pf;
1404 struct i40e_vsi *vsi = NULL;
1405 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001406 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001407 int i;
1408
1409 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1410 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1411 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001412 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001413 goto error_param;
1414 }
1415
1416 for (i = 0; i < al->num_elements; i++) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001417 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1418 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001419 goto error_param;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001420 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001421 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001422
1423 /* add new addresses to the list */
1424 for (i = 0; i < al->num_elements; i++) {
1425 struct i40e_mac_filter *f;
1426
1427 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
Mitch Williams7e68edf92013-11-16 10:00:41 +00001428 if (!f) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001429 if (i40e_is_vsi_in_vlan(vsi))
1430 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1431 true, false);
1432 else
1433 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1434 true, false);
1435 }
1436
1437 if (!f) {
1438 dev_err(&pf->pdev->dev,
1439 "Unable to add VF MAC filter\n");
Greg Rosef657a6e2013-11-28 06:39:42 +00001440 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001441 goto error_param;
1442 }
1443 }
1444
1445 /* program the updated filter list */
1446 if (i40e_sync_vsi_filters(vsi))
1447 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1448
1449error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001450 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001451 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001452 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001453}
1454
1455/**
1456 * i40e_vc_del_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001457 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001458 * @msg: pointer to the msg buffer
1459 * @msglen: msg length
1460 *
1461 * remove guest mac address filter
1462 **/
1463static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1464{
1465 struct i40e_virtchnl_ether_addr_list *al =
1466 (struct i40e_virtchnl_ether_addr_list *)msg;
1467 struct i40e_pf *pf = vf->pf;
1468 struct i40e_vsi *vsi = NULL;
1469 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001470 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001471 int i;
1472
1473 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1474 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1475 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001476 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001477 goto error_param;
1478 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001479
1480 for (i = 0; i < al->num_elements; i++) {
Mitch Williams700bbf62013-12-21 05:44:45 +00001481 if (is_broadcast_ether_addr(al->list[i].addr) ||
1482 is_zero_ether_addr(al->list[i].addr)) {
1483 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
1484 al->list[i].addr);
1485 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rosef657a6e2013-11-28 06:39:42 +00001486 goto error_param;
Mitch Williams700bbf62013-12-21 05:44:45 +00001487 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001488 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001489 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001490
1491 /* delete addresses from the list */
1492 for (i = 0; i < al->num_elements; i++)
1493 i40e_del_filter(vsi, al->list[i].addr,
1494 I40E_VLAN_ANY, true, false);
1495
1496 /* program the updated filter list */
1497 if (i40e_sync_vsi_filters(vsi))
1498 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1499
1500error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001501 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001502 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001503 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001504}
1505
1506/**
1507 * i40e_vc_add_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001508 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001509 * @msg: pointer to the msg buffer
1510 * @msglen: msg length
1511 *
1512 * program guest vlan id
1513 **/
1514static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1515{
1516 struct i40e_virtchnl_vlan_filter_list *vfl =
1517 (struct i40e_virtchnl_vlan_filter_list *)msg;
1518 struct i40e_pf *pf = vf->pf;
1519 struct i40e_vsi *vsi = NULL;
1520 u16 vsi_id = vfl->vsi_id;
1521 i40e_status aq_ret = 0;
1522 int i;
1523
1524 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1525 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1526 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1527 aq_ret = I40E_ERR_PARAM;
1528 goto error_param;
1529 }
1530
1531 for (i = 0; i < vfl->num_elements; i++) {
1532 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1533 aq_ret = I40E_ERR_PARAM;
1534 dev_err(&pf->pdev->dev,
1535 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1536 goto error_param;
1537 }
1538 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001539 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001540 if (vsi->info.pvid) {
1541 aq_ret = I40E_ERR_PARAM;
1542 goto error_param;
1543 }
1544
1545 i40e_vlan_stripping_enable(vsi);
1546 for (i = 0; i < vfl->num_elements; i++) {
1547 /* add new VLAN filter */
1548 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1549 if (ret)
1550 dev_err(&pf->pdev->dev,
1551 "Unable to add VF vlan filter %d, error %d\n",
1552 vfl->vlan_id[i], ret);
1553 }
1554
1555error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001556 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001557 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1558}
1559
1560/**
1561 * i40e_vc_remove_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001562 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001563 * @msg: pointer to the msg buffer
1564 * @msglen: msg length
1565 *
1566 * remove programmed guest vlan id
1567 **/
1568static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1569{
1570 struct i40e_virtchnl_vlan_filter_list *vfl =
1571 (struct i40e_virtchnl_vlan_filter_list *)msg;
1572 struct i40e_pf *pf = vf->pf;
1573 struct i40e_vsi *vsi = NULL;
1574 u16 vsi_id = vfl->vsi_id;
1575 i40e_status aq_ret = 0;
1576 int i;
1577
1578 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1579 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1580 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1581 aq_ret = I40E_ERR_PARAM;
1582 goto error_param;
1583 }
1584
1585 for (i = 0; i < vfl->num_elements; i++) {
1586 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1587 aq_ret = I40E_ERR_PARAM;
1588 goto error_param;
1589 }
1590 }
1591
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001592 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001593 if (vsi->info.pvid) {
1594 aq_ret = I40E_ERR_PARAM;
1595 goto error_param;
1596 }
1597
1598 for (i = 0; i < vfl->num_elements; i++) {
1599 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1600 if (ret)
1601 dev_err(&pf->pdev->dev,
1602 "Unable to delete VF vlan filter %d, error %d\n",
1603 vfl->vlan_id[i], ret);
1604 }
1605
1606error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001607 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001608 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1609}
1610
1611/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001612 * i40e_vc_validate_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001613 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001614 * @msg: pointer to the msg buffer
1615 * @msglen: msg length
1616 * @msghndl: msg handle
1617 *
1618 * validate msg
1619 **/
1620static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1621 u32 v_retval, u8 *msg, u16 msglen)
1622{
1623 bool err_msg_format = false;
1624 int valid_len;
1625
1626 /* Check if VF is disabled. */
1627 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1628 return I40E_ERR_PARAM;
1629
1630 /* Validate message length. */
1631 switch (v_opcode) {
1632 case I40E_VIRTCHNL_OP_VERSION:
1633 valid_len = sizeof(struct i40e_virtchnl_version_info);
1634 break;
1635 case I40E_VIRTCHNL_OP_RESET_VF:
1636 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1637 valid_len = 0;
1638 break;
1639 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1640 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1641 break;
1642 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1643 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1644 break;
1645 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1646 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1647 if (msglen >= valid_len) {
1648 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1649 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1650 valid_len += (vqc->num_queue_pairs *
1651 sizeof(struct
1652 i40e_virtchnl_queue_pair_info));
1653 if (vqc->num_queue_pairs == 0)
1654 err_msg_format = true;
1655 }
1656 break;
1657 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1658 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1659 if (msglen >= valid_len) {
1660 struct i40e_virtchnl_irq_map_info *vimi =
1661 (struct i40e_virtchnl_irq_map_info *)msg;
1662 valid_len += (vimi->num_vectors *
1663 sizeof(struct i40e_virtchnl_vector_map));
1664 if (vimi->num_vectors == 0)
1665 err_msg_format = true;
1666 }
1667 break;
1668 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1669 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1670 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1671 break;
1672 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1673 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1674 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1675 if (msglen >= valid_len) {
1676 struct i40e_virtchnl_ether_addr_list *veal =
1677 (struct i40e_virtchnl_ether_addr_list *)msg;
1678 valid_len += veal->num_elements *
1679 sizeof(struct i40e_virtchnl_ether_addr);
1680 if (veal->num_elements == 0)
1681 err_msg_format = true;
1682 }
1683 break;
1684 case I40E_VIRTCHNL_OP_ADD_VLAN:
1685 case I40E_VIRTCHNL_OP_DEL_VLAN:
1686 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1687 if (msglen >= valid_len) {
1688 struct i40e_virtchnl_vlan_filter_list *vfl =
1689 (struct i40e_virtchnl_vlan_filter_list *)msg;
1690 valid_len += vfl->num_elements * sizeof(u16);
1691 if (vfl->num_elements == 0)
1692 err_msg_format = true;
1693 }
1694 break;
1695 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1696 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1697 break;
1698 case I40E_VIRTCHNL_OP_GET_STATS:
1699 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1700 break;
1701 /* These are always errors coming from the VF. */
1702 case I40E_VIRTCHNL_OP_EVENT:
1703 case I40E_VIRTCHNL_OP_UNKNOWN:
1704 default:
1705 return -EPERM;
1706 break;
1707 }
1708 /* few more checks */
1709 if ((valid_len != msglen) || (err_msg_format)) {
1710 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1711 return -EINVAL;
1712 } else {
1713 return 0;
1714 }
1715}
1716
1717/**
1718 * i40e_vc_process_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001719 * @pf: pointer to the PF structure
1720 * @vf_id: source VF id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001721 * @msg: pointer to the msg buffer
1722 * @msglen: msg length
1723 * @msghndl: msg handle
1724 *
1725 * called from the common aeq/arq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001726 * process request from VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001727 **/
1728int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1729 u32 v_retval, u8 *msg, u16 msglen)
1730{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001731 struct i40e_hw *hw = &pf->hw;
Dan Carpenterc243e962014-01-15 06:43:39 +00001732 unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001733 struct i40e_vf *vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001734 int ret;
1735
1736 pf->vf_aq_requests++;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001737 if (local_vf_id >= pf->num_alloc_vfs)
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001738 return -EINVAL;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001739 vf = &(pf->vf[local_vf_id]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001740 /* perform basic checks on the msg */
1741 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1742
1743 if (ret) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001744 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001745 local_vf_id, v_opcode, msglen);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001746 return ret;
1747 }
Mitch Williamsbae3cae2014-01-14 00:49:49 -08001748
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001749 switch (v_opcode) {
1750 case I40E_VIRTCHNL_OP_VERSION:
1751 ret = i40e_vc_get_version_msg(vf);
1752 break;
1753 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1754 ret = i40e_vc_get_vf_resources_msg(vf);
1755 break;
1756 case I40E_VIRTCHNL_OP_RESET_VF:
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001757 i40e_vc_reset_vf_msg(vf);
1758 ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001759 break;
1760 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1761 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1762 break;
1763 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1764 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1765 break;
1766 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1767 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1768 break;
1769 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1770 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
Mitch Williams055b2952015-04-07 19:45:33 -04001771 i40e_vc_notify_vf_link_state(vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001772 break;
1773 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1774 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1775 break;
1776 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1777 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1778 break;
1779 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1780 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1781 break;
1782 case I40E_VIRTCHNL_OP_ADD_VLAN:
1783 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1784 break;
1785 case I40E_VIRTCHNL_OP_DEL_VLAN:
1786 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1787 break;
1788 case I40E_VIRTCHNL_OP_GET_STATS:
1789 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1790 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001791 case I40E_VIRTCHNL_OP_UNKNOWN:
1792 default:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001793 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001794 v_opcode, local_vf_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001795 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1796 I40E_ERR_NOT_IMPLEMENTED);
1797 break;
1798 }
1799
1800 return ret;
1801}
1802
1803/**
1804 * i40e_vc_process_vflr_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001805 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001806 *
1807 * called from the vlfr irq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001808 * free up VF resources and state variables
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001809 **/
1810int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1811{
1812 u32 reg, reg_idx, bit_idx, vf_id;
1813 struct i40e_hw *hw = &pf->hw;
1814 struct i40e_vf *vf;
1815
1816 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1817 return 0;
1818
Mitch Williamsc5c2f7c2014-11-11 03:15:04 +00001819 /* re-enable vflr interrupt cause */
1820 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1821 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1822 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1823 i40e_flush(hw);
1824
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001825 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1826 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1827 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1828 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001829 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001830 vf = &pf->vf[vf_id];
1831 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
1832 if (reg & (1 << bit_idx)) {
1833 /* clear the bit in GLGEN_VFLRSTAT */
1834 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
1835
Mitch Williamseb2d80b2014-02-13 03:48:48 -08001836 if (!test_bit(__I40E_DOWN, &pf->state))
1837 i40e_reset_vf(vf, true);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001838 }
1839 }
1840
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001841 return 0;
1842}
1843
1844/**
1845 * i40e_vc_vf_broadcast
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001846 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001847 * @opcode: operation code
1848 * @retval: return value
1849 * @msg: pointer to the msg buffer
1850 * @msglen: msg length
1851 *
1852 * send a message to all VFs on a given PF
1853 **/
1854static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
1855 enum i40e_virtchnl_ops v_opcode,
1856 i40e_status v_retval, u8 *msg,
1857 u16 msglen)
1858{
1859 struct i40e_hw *hw = &pf->hw;
1860 struct i40e_vf *vf = pf->vf;
1861 int i;
1862
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001863 for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
1864 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001865 /* Not all VFs are enabled so skip the ones that are not */
Anjali Singhai Jain478c9e72014-07-10 07:58:18 +00001866 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
1867 !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1868 continue;
1869
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001870 /* Ignore return value on purpose - a given VF may fail, but
1871 * we need to keep going and send to all of them
1872 */
Ashish Shahf19efbb2014-08-01 13:27:06 -07001873 i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001874 msg, msglen, NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001875 }
1876}
1877
1878/**
1879 * i40e_vc_notify_link_state
Mitch Williams055b2952015-04-07 19:45:33 -04001880 * @vf: pointer to the VF structure
1881 *
1882 * send a link status message to a single VF
1883 **/
1884static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
1885{
1886 struct i40e_virtchnl_pf_event pfe;
1887 struct i40e_pf *pf = vf->pf;
1888 struct i40e_hw *hw = &pf->hw;
1889 struct i40e_link_status *ls = &pf->hw.phy.link_info;
1890 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1891
1892 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
1893 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
1894 if (vf->link_forced) {
1895 pfe.event_data.link_event.link_status = vf->link_up;
1896 pfe.event_data.link_event.link_speed =
1897 (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
1898 } else {
1899 pfe.event_data.link_event.link_status =
1900 ls->link_info & I40E_AQ_LINK_UP;
1901 pfe.event_data.link_event.link_speed = ls->link_speed;
1902 }
1903 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
1904 0, (u8 *)&pfe, sizeof(pfe), NULL);
1905}
1906
1907/**
1908 * i40e_vc_notify_link_state
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001909 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001910 *
1911 * send a link status message to all VFs on a given PF
1912 **/
1913void i40e_vc_notify_link_state(struct i40e_pf *pf)
1914{
Mitch Williams588aefa2014-02-11 08:27:49 +00001915 int i;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001916
Mitch Williams055b2952015-04-07 19:45:33 -04001917 for (i = 0; i < pf->num_alloc_vfs; i++)
1918 i40e_vc_notify_vf_link_state(&pf->vf[i]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001919}
1920
1921/**
1922 * i40e_vc_notify_reset
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001923 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001924 *
1925 * indicate a pending reset to all VFs on a given PF
1926 **/
1927void i40e_vc_notify_reset(struct i40e_pf *pf)
1928{
1929 struct i40e_virtchnl_pf_event pfe;
1930
1931 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1932 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
1933 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
1934 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
1935}
1936
1937/**
1938 * i40e_vc_notify_vf_reset
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001939 * @vf: pointer to the VF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001940 *
1941 * indicate a pending reset to the given VF
1942 **/
1943void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
1944{
1945 struct i40e_virtchnl_pf_event pfe;
Anjali Singhai Jain478c9e72014-07-10 07:58:18 +00001946 int abs_vf_id;
1947
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001948 /* validate the request */
1949 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1950 return;
1951
Anjali Singhai Jain478c9e72014-07-10 07:58:18 +00001952 /* verify if the VF is in either init or active before proceeding */
1953 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
1954 !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1955 return;
1956
1957 abs_vf_id = vf->vf_id + vf->pf->hw.func_caps.vf_base_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001958
1959 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
1960 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
Ashish Shahf19efbb2014-08-01 13:27:06 -07001961 i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001962 I40E_SUCCESS, (u8 *)&pfe,
1963 sizeof(struct i40e_virtchnl_pf_event), NULL);
1964}
1965
1966/**
1967 * i40e_ndo_set_vf_mac
1968 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001969 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001970 * @mac: mac address
1971 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001972 * program VF mac address
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001973 **/
1974int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
1975{
1976 struct i40e_netdev_priv *np = netdev_priv(netdev);
1977 struct i40e_vsi *vsi = np->vsi;
1978 struct i40e_pf *pf = vsi->back;
1979 struct i40e_mac_filter *f;
1980 struct i40e_vf *vf;
1981 int ret = 0;
1982
1983 /* validate the request */
1984 if (vf_id >= pf->num_alloc_vfs) {
1985 dev_err(&pf->pdev->dev,
1986 "Invalid VF Identifier %d\n", vf_id);
1987 ret = -EINVAL;
1988 goto error_param;
1989 }
1990
1991 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001992 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001993 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1994 dev_err(&pf->pdev->dev,
1995 "Uninitialized VF %d\n", vf_id);
1996 ret = -EINVAL;
1997 goto error_param;
1998 }
1999
2000 if (!is_valid_ether_addr(mac)) {
2001 dev_err(&pf->pdev->dev,
2002 "Invalid VF ethernet address\n");
2003 ret = -EINVAL;
2004 goto error_param;
2005 }
2006
2007 /* delete the temporary mac address */
Greg Rose37cc0d22014-04-01 07:11:44 +00002008 i40e_del_filter(vsi, vf->default_lan_addr.addr, vf->port_vlan_id,
2009 true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002010
Greg Rose29f71bb2014-05-20 08:01:45 +00002011 /* Delete all the filters for this VSI - we're going to kill it
2012 * anyway.
2013 */
2014 list_for_each_entry(f, &vsi->mac_filter_list, list)
2015 i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002016
2017 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2018 /* program mac filter */
2019 if (i40e_sync_vsi_filters(vsi)) {
2020 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2021 ret = -EIO;
2022 goto error_param;
2023 }
Greg Rose9a173902014-05-22 06:32:02 +00002024 ether_addr_copy(vf->default_lan_addr.addr, mac);
Greg Rosef657a6e2013-11-28 06:39:42 +00002025 vf->pf_set_mac = true;
Greg Rose17413a82014-06-04 01:23:13 +00002026 /* Force the VF driver stop so it has to reload with new MAC address */
2027 i40e_vc_disable_vf(pf, vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002028 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002029
2030error_param:
2031 return ret;
2032}
2033
2034/**
2035 * i40e_ndo_set_vf_port_vlan
2036 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002037 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002038 * @vlan_id: mac address
2039 * @qos: priority setting
2040 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002041 * program VF vlan id and/or qos
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002042 **/
2043int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2044 int vf_id, u16 vlan_id, u8 qos)
2045{
2046 struct i40e_netdev_priv *np = netdev_priv(netdev);
2047 struct i40e_pf *pf = np->vsi->back;
2048 struct i40e_vsi *vsi;
2049 struct i40e_vf *vf;
2050 int ret = 0;
2051
2052 /* validate the request */
2053 if (vf_id >= pf->num_alloc_vfs) {
2054 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2055 ret = -EINVAL;
2056 goto error_pvid;
2057 }
2058
2059 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2060 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2061 ret = -EINVAL;
2062 goto error_pvid;
2063 }
2064
2065 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002066 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002067 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2068 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2069 ret = -EINVAL;
2070 goto error_pvid;
2071 }
2072
Greg Rosef9b4b622014-03-06 09:02:28 +00002073 if (vsi->info.pvid == 0 && i40e_is_vsi_in_vlan(vsi)) {
Greg Rose99a49732014-01-13 16:13:03 -08002074 dev_err(&pf->pdev->dev,
2075 "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",
2076 vf_id);
Greg Rosef9b4b622014-03-06 09:02:28 +00002077 /* Administrator Error - knock the VF offline until he does
2078 * the right thing by reconfiguring his network correctly
2079 * and then reloading the VF driver.
2080 */
2081 i40e_vc_disable_vf(pf, vf);
2082 }
Greg Rose99a49732014-01-13 16:13:03 -08002083
Greg Rose8d82a7c2014-01-13 16:13:04 -08002084 /* Check for condition where there was already a port VLAN ID
2085 * filter set and now it is being deleted by setting it to zero.
Greg Rose1315f7c2014-03-14 07:32:20 +00002086 * Additionally check for the condition where there was a port
2087 * VLAN but now there is a new and different port VLAN being set.
Greg Rose8d82a7c2014-01-13 16:13:04 -08002088 * Before deleting all the old VLAN filters we must add new ones
2089 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2090 * MAC addresses deleted.
2091 */
Greg Rose1315f7c2014-03-14 07:32:20 +00002092 if ((!(vlan_id || qos) ||
2093 (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
2094 vsi->info.pvid)
Greg Rose8d82a7c2014-01-13 16:13:04 -08002095 ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2096
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002097 if (vsi->info.pvid) {
2098 /* kill old VLAN */
2099 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2100 VLAN_VID_MASK));
2101 if (ret) {
2102 dev_info(&vsi->back->pdev->dev,
2103 "remove VLAN failed, ret=%d, aq_err=%d\n",
2104 ret, pf->hw.aq.asq_last_status);
2105 }
2106 }
2107 if (vlan_id || qos)
2108 ret = i40e_vsi_add_pvid(vsi,
2109 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
2110 else
Greg Rose6c12fcb2013-11-28 06:39:34 +00002111 i40e_vsi_remove_pvid(vsi);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002112
2113 if (vlan_id) {
2114 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2115 vlan_id, qos, vf_id);
2116
2117 /* add new VLAN filter */
2118 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2119 if (ret) {
2120 dev_info(&vsi->back->pdev->dev,
2121 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2122 vsi->back->hw.aq.asq_last_status);
2123 goto error_pvid;
2124 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002125 /* Kill non-vlan MAC filters - ignore error return since
2126 * there might not be any non-vlan MAC filters.
2127 */
2128 i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002129 }
2130
2131 if (ret) {
2132 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2133 goto error_pvid;
2134 }
Greg Rose6c12fcb2013-11-28 06:39:34 +00002135 /* The Port VLAN needs to be saved across resets the same as the
2136 * default LAN MAC address.
2137 */
2138 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002139 ret = 0;
2140
2141error_pvid:
2142 return ret;
2143}
2144
Mitch Williams84590fd2014-04-09 05:58:57 +00002145#define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
2146#define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002147/**
2148 * i40e_ndo_set_vf_bw
2149 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002150 * @vf_id: VF identifier
2151 * @tx_rate: Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002152 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002153 * configure VF Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002154 **/
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002155int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2156 int max_tx_rate)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002157{
Mitch Williams6b192892014-03-06 09:02:29 +00002158 struct i40e_netdev_priv *np = netdev_priv(netdev);
2159 struct i40e_pf *pf = np->vsi->back;
2160 struct i40e_vsi *vsi;
2161 struct i40e_vf *vf;
2162 int speed = 0;
2163 int ret = 0;
2164
2165 /* validate the request */
2166 if (vf_id >= pf->num_alloc_vfs) {
2167 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2168 ret = -EINVAL;
2169 goto error;
2170 }
2171
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002172 if (min_tx_rate) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002173 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 -04002174 min_tx_rate, vf_id);
2175 return -EINVAL;
2176 }
2177
Mitch Williams6b192892014-03-06 09:02:29 +00002178 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002179 vsi = pf->vsi[vf->lan_vsi_idx];
Mitch Williams6b192892014-03-06 09:02:29 +00002180 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2181 dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
2182 ret = -EINVAL;
2183 goto error;
2184 }
2185
2186 switch (pf->hw.phy.link_info.link_speed) {
2187 case I40E_LINK_SPEED_40GB:
2188 speed = 40000;
2189 break;
2190 case I40E_LINK_SPEED_10GB:
2191 speed = 10000;
2192 break;
2193 case I40E_LINK_SPEED_1GB:
2194 speed = 1000;
2195 break;
2196 default:
2197 break;
2198 }
2199
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002200 if (max_tx_rate > speed) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002201 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002202 max_tx_rate, vf->vf_id);
Mitch Williams6b192892014-03-06 09:02:29 +00002203 ret = -EINVAL;
2204 goto error;
2205 }
2206
Mitch Williamsdac9b312014-04-09 05:58:56 +00002207 if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2208 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2209 max_tx_rate = 50;
2210 }
2211
Mitch Williams6b192892014-03-06 09:02:29 +00002212 /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
Mitch Williams84590fd2014-04-09 05:58:57 +00002213 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2214 max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2215 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
Mitch Williams6b192892014-03-06 09:02:29 +00002216 if (ret) {
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002217 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
Mitch Williams6b192892014-03-06 09:02:29 +00002218 ret);
2219 ret = -EIO;
2220 goto error;
2221 }
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002222 vf->tx_rate = max_tx_rate;
Mitch Williams6b192892014-03-06 09:02:29 +00002223error:
2224 return ret;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002225}
2226
2227/**
2228 * i40e_ndo_get_vf_config
2229 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002230 * @vf_id: VF identifier
2231 * @ivi: VF configuration structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002232 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002233 * return VF configuration
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002234 **/
2235int i40e_ndo_get_vf_config(struct net_device *netdev,
2236 int vf_id, struct ifla_vf_info *ivi)
2237{
2238 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002239 struct i40e_vsi *vsi = np->vsi;
2240 struct i40e_pf *pf = vsi->back;
2241 struct i40e_vf *vf;
2242 int ret = 0;
2243
2244 /* validate the request */
2245 if (vf_id >= pf->num_alloc_vfs) {
2246 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2247 ret = -EINVAL;
2248 goto error_param;
2249 }
2250
2251 vf = &(pf->vf[vf_id]);
2252 /* first vsi is always the LAN vsi */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002253 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002254 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2255 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2256 ret = -EINVAL;
2257 goto error_param;
2258 }
2259
2260 ivi->vf = vf_id;
2261
Mitch Williamsf4a1c5c2013-11-28 06:39:34 +00002262 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002263
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002264 ivi->max_tx_rate = vf->tx_rate;
2265 ivi->min_tx_rate = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002266 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2267 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2268 I40E_VLAN_PRIORITY_SHIFT;
Mitch Williams84ca55a2014-03-14 07:32:24 +00002269 if (vf->link_forced == false)
2270 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2271 else if (vf->link_up == true)
2272 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2273 else
2274 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
Mitch Williamsc674d122014-05-20 08:01:40 +00002275 ivi->spoofchk = vf->spoofchk;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002276 ret = 0;
2277
2278error_param:
2279 return ret;
2280}
Mitch Williams588aefa2014-02-11 08:27:49 +00002281
2282/**
2283 * i40e_ndo_set_vf_link_state
2284 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002285 * @vf_id: VF identifier
Mitch Williams588aefa2014-02-11 08:27:49 +00002286 * @link: required link state
2287 *
2288 * Set the link state of a specified VF, regardless of physical link state
2289 **/
2290int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
2291{
2292 struct i40e_netdev_priv *np = netdev_priv(netdev);
2293 struct i40e_pf *pf = np->vsi->back;
2294 struct i40e_virtchnl_pf_event pfe;
2295 struct i40e_hw *hw = &pf->hw;
2296 struct i40e_vf *vf;
Ashish Shahf19efbb2014-08-01 13:27:06 -07002297 int abs_vf_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002298 int ret = 0;
2299
2300 /* validate the request */
2301 if (vf_id >= pf->num_alloc_vfs) {
2302 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2303 ret = -EINVAL;
2304 goto error_out;
2305 }
2306
2307 vf = &pf->vf[vf_id];
Ashish Shahf19efbb2014-08-01 13:27:06 -07002308 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002309
2310 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
2311 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
2312
2313 switch (link) {
2314 case IFLA_VF_LINK_STATE_AUTO:
2315 vf->link_forced = false;
2316 pfe.event_data.link_event.link_status =
2317 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
2318 pfe.event_data.link_event.link_speed =
2319 pf->hw.phy.link_info.link_speed;
2320 break;
2321 case IFLA_VF_LINK_STATE_ENABLE:
2322 vf->link_forced = true;
2323 vf->link_up = true;
2324 pfe.event_data.link_event.link_status = true;
2325 pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
2326 break;
2327 case IFLA_VF_LINK_STATE_DISABLE:
2328 vf->link_forced = true;
2329 vf->link_up = false;
2330 pfe.event_data.link_event.link_status = false;
2331 pfe.event_data.link_event.link_speed = 0;
2332 break;
2333 default:
2334 ret = -EINVAL;
2335 goto error_out;
2336 }
2337 /* Notify the VF of its new link state */
Ashish Shahf19efbb2014-08-01 13:27:06 -07002338 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Mitch Williams588aefa2014-02-11 08:27:49 +00002339 0, (u8 *)&pfe, sizeof(pfe), NULL);
2340
2341error_out:
2342 return ret;
2343}
Mitch Williamsc674d122014-05-20 08:01:40 +00002344
2345/**
2346 * i40e_ndo_set_vf_spoofchk
2347 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002348 * @vf_id: VF identifier
Mitch Williamsc674d122014-05-20 08:01:40 +00002349 * @enable: flag to enable or disable feature
2350 *
2351 * Enable or disable VF spoof checking
2352 **/
Serey Konge6d90042014-07-12 07:28:14 +00002353int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
Mitch Williamsc674d122014-05-20 08:01:40 +00002354{
2355 struct i40e_netdev_priv *np = netdev_priv(netdev);
2356 struct i40e_vsi *vsi = np->vsi;
2357 struct i40e_pf *pf = vsi->back;
2358 struct i40e_vsi_context ctxt;
2359 struct i40e_hw *hw = &pf->hw;
2360 struct i40e_vf *vf;
2361 int ret = 0;
2362
2363 /* validate the request */
2364 if (vf_id >= pf->num_alloc_vfs) {
2365 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2366 ret = -EINVAL;
2367 goto out;
2368 }
2369
2370 vf = &(pf->vf[vf_id]);
2371
2372 if (enable == vf->spoofchk)
2373 goto out;
2374
2375 vf->spoofchk = enable;
2376 memset(&ctxt, 0, sizeof(ctxt));
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002377 ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
Mitch Williamsc674d122014-05-20 08:01:40 +00002378 ctxt.pf_num = pf->hw.pf_id;
2379 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
2380 if (enable)
Greg Rose30d71af2015-01-29 07:17:17 +00002381 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
2382 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
Mitch Williamsc674d122014-05-20 08:01:40 +00002383 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
2384 if (ret) {
2385 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
2386 ret);
2387 ret = -EIO;
2388 }
2389out:
2390 return ret;
2391}