blob: d99c116032f368cb25cd40e1e63bf2ab240780c0 [file] [log] [blame]
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Neerav Parikh51616012015-02-06 08:52:14 +00004 * Copyright(c) 2013 - 2015 Intel Corporation.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27#include "i40e.h"
28
Mitch Williams532b0452015-04-07 19:45:34 -040029/*********************notification routines***********************/
30
31/**
32 * i40e_vc_vf_broadcast
33 * @pf: pointer to the PF structure
34 * @opcode: operation code
35 * @retval: return value
36 * @msg: pointer to the msg buffer
37 * @msglen: msg length
38 *
39 * send a message to all VFs on a given PF
40 **/
41static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
42 enum i40e_virtchnl_ops v_opcode,
43 i40e_status v_retval, u8 *msg,
44 u16 msglen)
45{
46 struct i40e_hw *hw = &pf->hw;
47 struct i40e_vf *vf = pf->vf;
48 int i;
49
50 for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
51 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
52 /* Not all vfs are enabled so skip the ones that are not */
53 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
54 !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
55 continue;
56
57 /* Ignore return value on purpose - a given VF may fail, but
58 * we need to keep going and send to all of them
59 */
60 i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
61 msg, msglen, NULL);
62 }
63}
64
65/**
66 * i40e_vc_notify_link_state
67 * @vf: pointer to the VF structure
68 *
69 * send a link status message to a single VF
70 **/
71static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
72{
73 struct i40e_virtchnl_pf_event pfe;
74 struct i40e_pf *pf = vf->pf;
75 struct i40e_hw *hw = &pf->hw;
76 struct i40e_link_status *ls = &pf->hw.phy.link_info;
77 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
78
79 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
80 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
81 if (vf->link_forced) {
82 pfe.event_data.link_event.link_status = vf->link_up;
83 pfe.event_data.link_event.link_speed =
84 (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
85 } else {
86 pfe.event_data.link_event.link_status =
87 ls->link_info & I40E_AQ_LINK_UP;
88 pfe.event_data.link_event.link_speed = ls->link_speed;
89 }
90 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
91 0, (u8 *)&pfe, sizeof(pfe), NULL);
92}
93
94/**
95 * i40e_vc_notify_link_state
96 * @pf: pointer to the PF structure
97 *
98 * send a link status message to all VFs on a given PF
99 **/
100void i40e_vc_notify_link_state(struct i40e_pf *pf)
101{
102 int i;
103
104 for (i = 0; i < pf->num_alloc_vfs; i++)
105 i40e_vc_notify_vf_link_state(&pf->vf[i]);
106}
107
108/**
109 * i40e_vc_notify_reset
110 * @pf: pointer to the PF structure
111 *
112 * indicate a pending reset to all VFs on a given PF
113 **/
114void i40e_vc_notify_reset(struct i40e_pf *pf)
115{
116 struct i40e_virtchnl_pf_event pfe;
117
118 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
119 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
120 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, 0,
121 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
122}
123
124/**
125 * i40e_vc_notify_vf_reset
126 * @vf: pointer to the VF structure
127 *
128 * indicate a pending reset to the given VF
129 **/
130void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
131{
132 struct i40e_virtchnl_pf_event pfe;
133 int abs_vf_id;
134
135 /* validate the request */
136 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
137 return;
138
139 /* verify if the VF is in either init or active before proceeding */
140 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
141 !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
142 return;
143
144 abs_vf_id = vf->vf_id + vf->pf->hw.func_caps.vf_base_id;
145
146 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
147 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
148 i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
149 0, (u8 *)&pfe,
150 sizeof(struct i40e_virtchnl_pf_event), NULL);
151}
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000152/***********************misc routines*****************************/
153
154/**
Greg Rosef9b4b622014-03-06 09:02:28 +0000155 * i40e_vc_disable_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000156 * @pf: pointer to the PF info
157 * @vf: pointer to the VF info
Greg Rosef9b4b622014-03-06 09:02:28 +0000158 *
159 * Disable the VF through a SW reset
160 **/
161static inline void i40e_vc_disable_vf(struct i40e_pf *pf, struct i40e_vf *vf)
162{
Mitch Williams54f455e2015-04-27 14:57:14 -0400163 i40e_vc_notify_vf_reset(vf);
164 i40e_reset_vf(vf, false);
Greg Rosef9b4b622014-03-06 09:02:28 +0000165}
166
167/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000168 * i40e_vc_isvalid_vsi_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000169 * @vf: pointer to the VF info
170 * @vsi_id: VF relative VSI id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000171 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000172 * check for the valid VSI id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000173 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700174static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000175{
176 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700177 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000178
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700179 return (vsi && (vsi->vf_id == vf->vf_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000180}
181
182/**
183 * i40e_vc_isvalid_queue_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000184 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000185 * @vsi_id: vsi id
186 * @qid: vsi relative queue id
187 *
188 * check for the valid queue id
189 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700190static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000191 u8 qid)
192{
193 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700194 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000195
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700196 return (vsi && (qid < vsi->alloc_queue_pairs));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000197}
198
199/**
200 * i40e_vc_isvalid_vector_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000201 * @vf: pointer to the VF info
202 * @vector_id: VF relative vector id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000203 *
204 * check for the valid vector id
205 **/
206static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
207{
208 struct i40e_pf *pf = vf->pf;
209
Mitch Williams9347eb72014-02-11 08:26:32 +0000210 return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000211}
212
213/***********************vf resource mgmt routines*****************/
214
215/**
216 * i40e_vc_get_pf_queue_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000217 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700218 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000219 * @vsi_queue_id: vsi relative queue id
220 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000221 * return PF relative queue id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000222 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700223static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000224 u8 vsi_queue_id)
225{
226 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700227 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000228 u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
229
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700230 if (!vsi)
231 return pf_queue_id;
232
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000233 if (le16_to_cpu(vsi->info.mapping_flags) &
234 I40E_AQ_VSI_QUE_MAP_NONCONTIG)
235 pf_queue_id =
236 le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
237 else
238 pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
239 vsi_queue_id;
240
241 return pf_queue_id;
242}
243
244/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000245 * i40e_config_irq_link_list
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000246 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700247 * @vsi_id: id of VSI as given by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000248 * @vecmap: irq map info
249 *
250 * configure irq link list from the map
251 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700252static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000253 struct i40e_virtchnl_vector_map *vecmap)
254{
255 unsigned long linklistmap = 0, tempmap;
256 struct i40e_pf *pf = vf->pf;
257 struct i40e_hw *hw = &pf->hw;
258 u16 vsi_queue_id, pf_queue_id;
259 enum i40e_queue_type qtype;
260 u16 next_q, vector_id;
261 u32 reg, reg_idx;
262 u16 itr_idx = 0;
263
264 vector_id = vecmap->vector_id;
265 /* setup the head */
266 if (0 == vector_id)
267 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
268 else
269 reg_idx = I40E_VPINT_LNKLSTN(
Mitch Williams9347eb72014-02-11 08:26:32 +0000270 ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
271 (vector_id - 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000272
273 if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
274 /* Special case - No queues mapped on this vector */
275 wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
276 goto irq_list_done;
277 }
278 tempmap = vecmap->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000279 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400280 linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
281 vsi_queue_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000282 }
283
284 tempmap = vecmap->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000285 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400286 linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
287 vsi_queue_id + 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000288 }
289
290 next_q = find_first_bit(&linklistmap,
291 (I40E_MAX_VSI_QP *
292 I40E_VIRTCHNL_SUPPORTED_QTYPES));
293 vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
294 qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700295 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000296 reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
297
298 wr32(hw, reg_idx, reg);
299
300 while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
301 switch (qtype) {
302 case I40E_QUEUE_TYPE_RX:
303 reg_idx = I40E_QINT_RQCTL(pf_queue_id);
304 itr_idx = vecmap->rxitr_idx;
305 break;
306 case I40E_QUEUE_TYPE_TX:
307 reg_idx = I40E_QINT_TQCTL(pf_queue_id);
308 itr_idx = vecmap->txitr_idx;
309 break;
310 default:
311 break;
312 }
313
314 next_q = find_next_bit(&linklistmap,
315 (I40E_MAX_VSI_QP *
316 I40E_VIRTCHNL_SUPPORTED_QTYPES),
317 next_q + 1);
Mitch Williams829af3a2013-12-18 13:46:00 +0000318 if (next_q <
319 (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000320 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
321 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700322 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000323 vsi_queue_id);
324 } else {
325 pf_queue_id = I40E_QUEUE_END_OF_LIST;
326 qtype = 0;
327 }
328
329 /* format for the RQCTL & TQCTL regs is same */
330 reg = (vector_id) |
331 (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
332 (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400333 BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000334 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
335 wr32(hw, reg_idx, reg);
336 }
337
Anjali Singhai Jainb8262a62015-07-10 19:36:08 -0400338 /* if the vf is running in polling mode and using interrupt zero,
339 * need to disable auto-mask on enabling zero interrupt for VFs.
340 */
341 if ((vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
342 (vector_id == 0)) {
343 reg = rd32(hw, I40E_GLINT_CTL);
344 if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
345 reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
346 wr32(hw, I40E_GLINT_CTL, reg);
347 }
348 }
349
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000350irq_list_done:
351 i40e_flush(hw);
352}
353
354/**
355 * i40e_config_vsi_tx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000356 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700357 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000358 * @vsi_queue_id: vsi relative queue index
359 * @info: config. info
360 *
361 * configure tx queue
362 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700363static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000364 u16 vsi_queue_id,
365 struct i40e_virtchnl_txq_info *info)
366{
367 struct i40e_pf *pf = vf->pf;
368 struct i40e_hw *hw = &pf->hw;
369 struct i40e_hmc_obj_txq tx_ctx;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700370 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000371 u16 pf_queue_id;
372 u32 qtx_ctl;
373 int ret = 0;
374
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700375 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
376 vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000377
378 /* clear the context structure first */
379 memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
380
381 /* only set the required fields */
382 tx_ctx.base = info->dma_ring_addr / 128;
383 tx_ctx.qlen = info->ring_len;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700384 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000385 tx_ctx.rdylist_act = 0;
Ashish Shah5d298962014-05-22 06:31:25 +0000386 tx_ctx.head_wb_ena = info->headwb_enabled;
387 tx_ctx.head_wb_addr = info->dma_headwb_addr;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000388
389 /* clear the context in the HMC */
390 ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
391 if (ret) {
392 dev_err(&pf->pdev->dev,
393 "Failed to clear VF LAN Tx queue context %d, error: %d\n",
394 pf_queue_id, ret);
395 ret = -ENOENT;
396 goto error_context;
397 }
398
399 /* set the context in the HMC */
400 ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
401 if (ret) {
402 dev_err(&pf->pdev->dev,
403 "Failed to set VF LAN Tx queue context %d error: %d\n",
404 pf_queue_id, ret);
405 ret = -ENOENT;
406 goto error_context;
407 }
408
409 /* associate this queue with the PCI VF function */
410 qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +0000411 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000412 & I40E_QTX_CTL_PF_INDX_MASK);
413 qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
414 << I40E_QTX_CTL_VFVM_INDX_SHIFT)
415 & I40E_QTX_CTL_VFVM_INDX_MASK);
416 wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
417 i40e_flush(hw);
418
419error_context:
420 return ret;
421}
422
423/**
424 * i40e_config_vsi_rx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000425 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700426 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000427 * @vsi_queue_id: vsi relative queue index
428 * @info: config. info
429 *
430 * configure rx queue
431 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700432static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000433 u16 vsi_queue_id,
434 struct i40e_virtchnl_rxq_info *info)
435{
436 struct i40e_pf *pf = vf->pf;
437 struct i40e_hw *hw = &pf->hw;
438 struct i40e_hmc_obj_rxq rx_ctx;
439 u16 pf_queue_id;
440 int ret = 0;
441
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700442 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000443
444 /* clear the context structure first */
445 memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
446
447 /* only set the required fields */
448 rx_ctx.base = info->dma_ring_addr / 128;
449 rx_ctx.qlen = info->ring_len;
450
451 if (info->splithdr_enabled) {
452 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
453 I40E_RX_SPLIT_IP |
454 I40E_RX_SPLIT_TCP_UDP |
455 I40E_RX_SPLIT_SCTP;
456 /* header length validation */
457 if (info->hdr_size > ((2 * 1024) - 64)) {
458 ret = -EINVAL;
459 goto error_param;
460 }
461 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
462
463 /* set splitalways mode 10b */
464 rx_ctx.dtype = 0x2;
465 }
466
467 /* databuffer length validation */
468 if (info->databuffer_size > ((16 * 1024) - 128)) {
469 ret = -EINVAL;
470 goto error_param;
471 }
472 rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
473
474 /* max pkt. length validation */
475 if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
476 ret = -EINVAL;
477 goto error_param;
478 }
479 rx_ctx.rxmax = info->max_pkt_size;
480
481 /* enable 32bytes desc always */
482 rx_ctx.dsize = 1;
483
484 /* default values */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000485 rx_ctx.lrxqthresh = 2;
486 rx_ctx.crcstrip = 1;
Mitch Williams50d41652014-04-09 05:58:55 +0000487 rx_ctx.prefena = 1;
Shannon Nelsonc1d11ce2014-07-29 04:01:03 +0000488 rx_ctx.l2tsel = 1;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000489
490 /* clear the context in the HMC */
491 ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
492 if (ret) {
493 dev_err(&pf->pdev->dev,
494 "Failed to clear VF LAN Rx queue context %d, error: %d\n",
495 pf_queue_id, ret);
496 ret = -ENOENT;
497 goto error_param;
498 }
499
500 /* set the context in the HMC */
501 ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
502 if (ret) {
503 dev_err(&pf->pdev->dev,
504 "Failed to set VF LAN Rx queue context %d error: %d\n",
505 pf_queue_id, ret);
506 ret = -ENOENT;
507 goto error_param;
508 }
509
510error_param:
511 return ret;
512}
513
514/**
515 * i40e_alloc_vsi_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000516 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000517 * @type: type of VSI to allocate
518 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000519 * alloc VF vsi context & resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000520 **/
521static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
522{
523 struct i40e_mac_filter *f = NULL;
524 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000525 struct i40e_vsi *vsi;
526 int ret = 0;
527
528 vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
529
530 if (!vsi) {
531 dev_err(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000532 "add vsi failed for VF %d, aq_err %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000533 vf->vf_id, pf->hw.aq.asq_last_status);
534 ret = -ENOENT;
535 goto error_alloc_vsi_res;
536 }
537 if (type == I40E_VSI_SRIOV) {
Greg Rose1a103702013-11-28 06:42:39 +0000538 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700539 vf->lan_vsi_idx = vsi->idx;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000540 vf->lan_vsi_id = vsi->id;
Greg Rose6c12fcb2013-11-28 06:39:34 +0000541 /* If the port VLAN has been configured and then the
542 * VF driver was removed then the VSI port VLAN
543 * configuration was destroyed. Check if there is
544 * a port VLAN and restore the VSI configuration if
545 * needed.
546 */
547 if (vf->port_vlan_id)
548 i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000549 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
Mitch Williamse9951632015-04-27 14:57:13 -0400550 vf->port_vlan_id ? vf->port_vlan_id : -1,
551 true, false);
Greg Rose1a103702013-11-28 06:42:39 +0000552 if (!f)
553 dev_info(&pf->pdev->dev,
554 "Could not allocate VF MAC addr\n");
Mitch Williamse9951632015-04-27 14:57:13 -0400555 f = i40e_add_filter(vsi, brdcast,
556 vf->port_vlan_id ? vf->port_vlan_id : -1,
Greg Rose1a103702013-11-28 06:42:39 +0000557 true, false);
558 if (!f)
559 dev_info(&pf->pdev->dev,
560 "Could not allocate VF broadcast filter\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000561 }
Neerav Parikh6dbbbfb2013-11-26 10:49:24 +0000562
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000563 /* program mac filter */
564 ret = i40e_sync_vsi_filters(vsi);
Mitch Williamsfd1646e2014-02-13 03:48:44 -0800565 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000566 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000567
Mitch Williams6b192892014-03-06 09:02:29 +0000568 /* Set VF bandwidth if specified */
569 if (vf->tx_rate) {
570 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
571 vf->tx_rate / 50, 0, NULL);
572 if (ret)
573 dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
574 vf->vf_id, ret);
575 }
576
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000577error_alloc_vsi_res:
578 return ret;
579}
580
581/**
Mitch Williams805bd5b2013-11-28 06:39:26 +0000582 * i40e_enable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000583 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000584 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000585 * enable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000586 **/
587static void i40e_enable_vf_mappings(struct i40e_vf *vf)
588{
589 struct i40e_pf *pf = vf->pf;
590 struct i40e_hw *hw = &pf->hw;
591 u32 reg, total_queue_pairs = 0;
592 int j;
593
594 /* Tell the hardware we're using noncontiguous mapping. HW requires
595 * that VF queues be mapped using this method, even when they are
596 * contiguous in real life
597 */
598 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
599 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
600
601 /* enable VF vplan_qtable mappings */
602 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
603 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
604
605 /* map PF queues to VF queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700606 for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
607 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
Mitch Williams805bd5b2013-11-28 06:39:26 +0000608 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
609 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
610 total_queue_pairs++;
611 }
612
613 /* map PF queues to VSI */
614 for (j = 0; j < 7; j++) {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700615 if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
Mitch Williams805bd5b2013-11-28 06:39:26 +0000616 reg = 0x07FF07FF; /* unused */
617 } else {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700618 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000619 j * 2);
620 reg = qid;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700621 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000622 (j * 2) + 1);
623 reg |= qid << 16;
624 }
625 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
626 }
627
628 i40e_flush(hw);
629}
630
631/**
632 * i40e_disable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000633 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000634 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000635 * disable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000636 **/
637static void i40e_disable_vf_mappings(struct i40e_vf *vf)
638{
639 struct i40e_pf *pf = vf->pf;
640 struct i40e_hw *hw = &pf->hw;
641 int i;
642
643 /* disable qp mappings */
644 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
645 for (i = 0; i < I40E_MAX_VSI_QP; i++)
646 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
647 I40E_QUEUE_END_OF_LIST);
648 i40e_flush(hw);
649}
650
651/**
652 * i40e_free_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000653 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000654 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000655 * free VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000656 **/
657static void i40e_free_vf_res(struct i40e_vf *vf)
658{
659 struct i40e_pf *pf = vf->pf;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000660 struct i40e_hw *hw = &pf->hw;
661 u32 reg_idx, reg;
662 int i, msix_vf;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000663
664 /* free vsi & disconnect it from the parent uplink */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700665 if (vf->lan_vsi_idx) {
666 i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
667 vf->lan_vsi_idx = 0;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000668 vf->lan_vsi_id = 0;
669 }
Mitch Williams9347eb72014-02-11 08:26:32 +0000670 msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
671
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000672 /* disable interrupts so the VF starts in a known state */
673 for (i = 0; i < msix_vf; i++) {
674 /* format is same for both registers */
675 if (0 == i)
676 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
677 else
678 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
679 (vf->vf_id))
680 + (i - 1));
681 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
682 i40e_flush(hw);
683 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000684
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000685 /* clear the irq settings */
686 for (i = 0; i < msix_vf; i++) {
687 /* format is same for both registers */
688 if (0 == i)
689 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
690 else
691 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
692 (vf->vf_id))
693 + (i - 1));
694 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
695 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
696 wr32(hw, reg_idx, reg);
697 i40e_flush(hw);
698 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000699 /* reset some of the state varibles keeping
700 * track of the resources
701 */
702 vf->num_queue_pairs = 0;
703 vf->vf_states = 0;
704}
705
706/**
707 * i40e_alloc_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000708 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000709 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000710 * allocate VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000711 **/
712static int i40e_alloc_vf_res(struct i40e_vf *vf)
713{
714 struct i40e_pf *pf = vf->pf;
715 int total_queue_pairs = 0;
716 int ret;
717
718 /* allocate hw vsi context & associated resources */
719 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
720 if (ret)
721 goto error_alloc;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700722 total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000723 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
724
725 /* store the total qps number for the runtime
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000726 * VF req validation
Mitch Williams805bd5b2013-11-28 06:39:26 +0000727 */
728 vf->num_queue_pairs = total_queue_pairs;
729
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000730 /* VF is now completely initialized */
Mitch Williams805bd5b2013-11-28 06:39:26 +0000731 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
732
733error_alloc:
734 if (ret)
735 i40e_free_vf_res(vf);
736
737 return ret;
738}
739
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000740#define VF_DEVICE_STATUS 0xAA
741#define VF_TRANS_PENDING_MASK 0x20
742/**
743 * i40e_quiesce_vf_pci
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000744 * @vf: pointer to the VF structure
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000745 *
746 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
747 * if the transactions never clear.
748 **/
749static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
750{
751 struct i40e_pf *pf = vf->pf;
752 struct i40e_hw *hw = &pf->hw;
753 int vf_abs_id, i;
754 u32 reg;
755
Mitch Williamsb141d612013-11-28 06:39:36 +0000756 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000757
758 wr32(hw, I40E_PF_PCI_CIAA,
759 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
760 for (i = 0; i < 100; i++) {
761 reg = rd32(hw, I40E_PF_PCI_CIAD);
762 if ((reg & VF_TRANS_PENDING_MASK) == 0)
763 return 0;
764 udelay(1);
765 }
766 return -EIO;
767}
768
Mitch Williams805bd5b2013-11-28 06:39:26 +0000769/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000770 * i40e_reset_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000771 * @vf: pointer to the VF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000772 * @flr: VFLR was issued or not
773 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000774 * reset the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000775 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000776void i40e_reset_vf(struct i40e_vf *vf, bool flr)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000777{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000778 struct i40e_pf *pf = vf->pf;
779 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000780 bool rsd = false;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000781 int i;
782 u32 reg;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000783
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000784 if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
785 return;
786
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000787 /* warn the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000788 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
789
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000790 /* In the case of a VFLR, the HW has already reset the VF and we
791 * just need to clean up, so don't hit the VFRTRIG register.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000792 */
793 if (!flr) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000794 /* reset VF using VPGEN_VFRTRIG reg */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000795 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
796 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000797 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
798 i40e_flush(hw);
799 }
800
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000801 if (i40e_quiesce_vf_pci(vf))
802 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
803 vf->vf_id);
804
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000805 /* poll VPGEN_VFRSTAT reg to make sure
806 * that reset is complete
807 */
Mitch Williams1750a222015-01-09 11:18:13 +0000808 for (i = 0; i < 10; i++) {
809 /* VF reset requires driver to first reset the VF and then
810 * poll the status register to make sure that the reset
811 * completed successfully. Due to internal HW FIFO flushes,
812 * we must wait 10ms before the register will be valid.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000813 */
Mitch Williams1750a222015-01-09 11:18:13 +0000814 usleep_range(10000, 20000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000815 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
816 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
817 rsd = true;
818 break;
819 }
820 }
821
Anjali Singhai Jain57175ac2015-04-07 19:45:35 -0400822 if (flr)
823 usleep_range(10000, 20000);
824
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000825 if (!rsd)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000826 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000827 vf->vf_id);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000828 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000829 /* clear the reset bit in the VPGEN_VFRTRIG reg */
830 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
831 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
832 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000833
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000834 /* On initial reset, we won't have any queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700835 if (vf->lan_vsi_idx == 0)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000836 goto complete_reset;
837
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700838 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000839complete_reset:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000840 /* reallocate VF resources to reset the VSI state */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000841 i40e_free_vf_res(vf);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000842 i40e_alloc_vf_res(vf);
843 i40e_enable_vf_mappings(vf);
Mitch Williamsc17b3622014-02-13 03:48:45 -0800844 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
Mitch Williams5b8f8502015-04-27 14:57:15 -0400845 clear_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000846
847 /* tell the VF the reset is done */
848 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
849 i40e_flush(hw);
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000850 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000851}
Greg Rosec3542292013-12-13 08:38:38 +0000852
853/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000854 * i40e_free_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000855 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000856 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000857 * free VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000858 **/
859void i40e_free_vfs(struct i40e_pf *pf)
860{
Mitch Williamsf7414532013-11-28 06:39:40 +0000861 struct i40e_hw *hw = &pf->hw;
862 u32 reg_idx, bit_idx;
863 int i, tmp, vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000864
865 if (!pf->vf)
866 return;
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000867 while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
868 usleep_range(1000, 2000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000869
Mitch Williams44434632015-04-07 11:32:55 -0700870 for (i = 0; i < pf->num_alloc_vfs; i++)
871 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
872 i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
873 false);
874
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000875 /* Disable IOV before freeing resources. This lets any VF drivers
876 * running in the host get themselves cleaned up before we yank
877 * the carpet out from underneath their feet.
878 */
879 if (!pci_vfs_assigned(pf->pdev))
880 pci_disable_sriov(pf->pdev);
Mitch Williams6d7b9672015-03-31 00:45:02 -0700881 else
882 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000883
884 msleep(20); /* let any messages in transit get finished up */
885
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000886 /* free up VF resources */
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000887 tmp = pf->num_alloc_vfs;
888 pf->num_alloc_vfs = 0;
889 for (i = 0; i < tmp; i++) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000890 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
891 i40e_free_vf_res(&pf->vf[i]);
892 /* disable qp mappings */
893 i40e_disable_vf_mappings(&pf->vf[i]);
894 }
895
896 kfree(pf->vf);
897 pf->vf = NULL;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000898
Mitch Williams9e5634d2014-04-04 04:43:14 +0000899 /* This check is for when the driver is unloaded while VFs are
900 * assigned. Setting the number of VFs to 0 through sysfs is caught
901 * before this function ever gets called.
902 */
Ethan Zhaoc24817b2014-07-22 18:36:43 +0000903 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williamsf7414532013-11-28 06:39:40 +0000904 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
905 * work correctly when SR-IOV gets re-enabled.
906 */
907 for (vf_id = 0; vf_id < tmp; vf_id++) {
908 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
909 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400910 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
Mitch Williamsf7414532013-11-28 06:39:40 +0000911 }
Greg Rosec3542292013-12-13 08:38:38 +0000912 }
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000913 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000914}
915
916#ifdef CONFIG_PCI_IOV
917/**
918 * i40e_alloc_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000919 * @pf: pointer to the PF structure
920 * @num_alloc_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000921 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000922 * allocate VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000923 **/
Mitch Williams4aeec012014-02-13 03:48:47 -0800924int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000925{
926 struct i40e_vf *vfs;
927 int i, ret = 0;
928
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000929 /* Disable interrupt 0 so we don't try to handle the VFLR. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000930 i40e_irq_dynamic_disable_icr0(pf);
931
Mitch Williams4aeec012014-02-13 03:48:47 -0800932 /* Check to see if we're just allocating resources for extant VFs */
933 if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
934 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
935 if (ret) {
Mitch Williams4aeec012014-02-13 03:48:47 -0800936 pf->num_alloc_vfs = 0;
937 goto err_iov;
938 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000939 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000940 /* allocate memory */
Akeem G Abodunrincc6456a2014-02-06 05:51:02 +0000941 vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000942 if (!vfs) {
943 ret = -ENOMEM;
944 goto err_alloc;
945 }
Mitch Williamsc674d122014-05-20 08:01:40 +0000946 pf->vf = vfs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000947
948 /* apply default profile */
949 for (i = 0; i < num_alloc_vfs; i++) {
950 vfs[i].pf = pf;
951 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
952 vfs[i].vf_id = i;
953
954 /* assign default capabilities */
955 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
Mitch Williamsc674d122014-05-20 08:01:40 +0000956 vfs[i].spoofchk = true;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000957 /* VF resources get allocated during reset */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000958 i40e_reset_vf(&vfs[i], false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000959
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000960 /* enable VF vplan_qtable mappings */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000961 i40e_enable_vf_mappings(&vfs[i]);
962 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000963 pf->num_alloc_vfs = num_alloc_vfs;
964
965err_alloc:
966 if (ret)
967 i40e_free_vfs(pf);
968err_iov:
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000969 /* Re-enable interrupt 0. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000970 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000971 return ret;
972}
973
974#endif
975/**
976 * i40e_pci_sriov_enable
977 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000978 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000979 *
980 * Enable or change the number of VFs
981 **/
982static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
983{
984#ifdef CONFIG_PCI_IOV
985 struct i40e_pf *pf = pci_get_drvdata(pdev);
986 int pre_existing_vfs = pci_num_vf(pdev);
987 int err = 0;
988
Greg Rosee17bc412015-04-16 20:05:59 -0400989 if (pf->state & __I40E_TESTING) {
990 dev_warn(&pdev->dev,
991 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
992 err = -EPERM;
993 goto err_out;
994 }
995
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000996 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
997 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
998 i40e_free_vfs(pf);
999 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1000 goto out;
1001
1002 if (num_vfs > pf->num_req_vfs) {
1003 err = -EPERM;
1004 goto err_out;
1005 }
1006
1007 err = i40e_alloc_vfs(pf, num_vfs);
1008 if (err) {
1009 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1010 goto err_out;
1011 }
1012
1013out:
1014 return num_vfs;
1015
1016err_out:
1017 return err;
1018#endif
1019 return 0;
1020}
1021
1022/**
1023 * i40e_pci_sriov_configure
1024 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001025 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001026 *
1027 * Enable or change the number of VFs. Called when the user updates the number
1028 * of VFs in sysfs.
1029 **/
1030int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1031{
1032 struct i40e_pf *pf = pci_get_drvdata(pdev);
1033
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07001034 if (num_vfs) {
1035 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1036 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1037 i40e_do_reset_safe(pf,
1038 BIT_ULL(__I40E_PF_RESET_REQUESTED));
1039 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001040 return i40e_pci_sriov_enable(pdev, num_vfs);
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07001041 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001042
Ethan Zhaoc24817b2014-07-22 18:36:43 +00001043 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williams9e5634d2014-04-04 04:43:14 +00001044 i40e_free_vfs(pf);
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07001045 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1046 i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Mitch Williams9e5634d2014-04-04 04:43:14 +00001047 } else {
1048 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1049 return -EINVAL;
1050 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001051 return 0;
1052}
1053
1054/***********************virtual channel routines******************/
1055
1056/**
1057 * i40e_vc_send_msg_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001058 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001059 * @v_opcode: virtual channel opcode
1060 * @v_retval: virtual channel return value
1061 * @msg: pointer to the msg buffer
1062 * @msglen: msg length
1063 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001064 * send msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001065 **/
1066static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1067 u32 v_retval, u8 *msg, u16 msglen)
1068{
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001069 struct i40e_pf *pf;
1070 struct i40e_hw *hw;
1071 int abs_vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001072 i40e_status aq_ret;
1073
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001074 /* validate the request */
1075 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1076 return -EINVAL;
1077
1078 pf = vf->pf;
1079 hw = &pf->hw;
1080 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1081
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001082 /* single place to detect unsuccessful return values */
1083 if (v_retval) {
1084 vf->num_invalid_msgs++;
1085 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
1086 v_opcode, v_retval);
1087 if (vf->num_invalid_msgs >
1088 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1089 dev_err(&pf->pdev->dev,
1090 "Number of invalid messages exceeded for VF %d\n",
1091 vf->vf_id);
1092 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1093 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1094 }
1095 } else {
1096 vf->num_valid_msgs++;
1097 }
1098
Ashish Shahf19efbb2014-08-01 13:27:06 -07001099 aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Mitch Williams7efa84b2013-11-28 06:39:41 +00001100 msg, msglen, NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001101 if (aq_ret) {
1102 dev_err(&pf->pdev->dev,
1103 "Unable to send the message to VF %d aq_err %d\n",
1104 vf->vf_id, pf->hw.aq.asq_last_status);
1105 return -EIO;
1106 }
1107
1108 return 0;
1109}
1110
1111/**
1112 * i40e_vc_send_resp_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001113 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001114 * @opcode: operation code
1115 * @retval: return value
1116 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001117 * send resp msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001118 **/
1119static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1120 enum i40e_virtchnl_ops opcode,
1121 i40e_status retval)
1122{
1123 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1124}
1125
1126/**
1127 * i40e_vc_get_version_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001128 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001129 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001130 * called from the VF to request the API version used by the PF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001131 **/
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001132static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001133{
1134 struct i40e_virtchnl_version_info info = {
1135 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
1136 };
1137
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001138 vf->vf_ver = *(struct i40e_virtchnl_version_info *)msg;
Mitch Williams606a5482015-06-04 16:24:00 -04001139 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1140 if (VF_IS_V10(vf))
1141 info.minor = I40E_VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001142 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
1143 I40E_SUCCESS, (u8 *)&info,
1144 sizeof(struct
1145 i40e_virtchnl_version_info));
1146}
1147
1148/**
1149 * i40e_vc_get_vf_resources_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001150 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001151 * @msg: pointer to the msg buffer
1152 * @msglen: msg length
1153 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001154 * called from the VF to request its resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001155 **/
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001156static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001157{
1158 struct i40e_virtchnl_vf_resource *vfres = NULL;
1159 struct i40e_pf *pf = vf->pf;
1160 i40e_status aq_ret = 0;
1161 struct i40e_vsi *vsi;
1162 int i = 0, len = 0;
1163 int num_vsis = 1;
1164 int ret;
1165
1166 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1167 aq_ret = I40E_ERR_PARAM;
1168 goto err;
1169 }
1170
1171 len = (sizeof(struct i40e_virtchnl_vf_resource) +
1172 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1173
1174 vfres = kzalloc(len, GFP_KERNEL);
1175 if (!vfres) {
1176 aq_ret = I40E_ERR_NO_MEMORY;
1177 len = 0;
1178 goto err;
1179 }
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001180 if (VF_IS_V11(vf))
1181 vf->driver_caps = *(u32 *)msg;
1182 else
1183 vf->driver_caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
1184 I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
1185 I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001186
1187 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001188 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001189 if (!vsi->info.pvid)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001190 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1191 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
1192 if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ)
1193 vfres->vf_offload_flags |=
1194 I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1195 } else {
1196 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG;
1197 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001198 vfres->num_vsis = num_vsis;
1199 vfres->num_queue_pairs = vf->num_queue_pairs;
1200 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001201 if (vf->lan_vsi_idx) {
1202 vfres->vsi_res[i].vsi_id = vf->lan_vsi_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001203 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1204 vfres->vsi_res[i].num_queue_pairs =
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001205 pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001206 memcpy(vfres->vsi_res[i].default_mac_addr,
1207 vf->default_lan_addr.addr, ETH_ALEN);
1208 i++;
1209 }
1210 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1211
1212err:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001213 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001214 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1215 aq_ret, (u8 *)vfres, len);
1216
1217 kfree(vfres);
1218 return ret;
1219}
1220
1221/**
1222 * i40e_vc_reset_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001223 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001224 * @msg: pointer to the msg buffer
1225 * @msglen: msg length
1226 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001227 * called from the VF to reset itself,
1228 * unlike other virtchnl messages, PF driver
1229 * doesn't send the response back to the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001230 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001231static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001232{
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001233 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1234 i40e_reset_vf(vf, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001235}
1236
1237/**
1238 * i40e_vc_config_promiscuous_mode_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001239 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001240 * @msg: pointer to the msg buffer
1241 * @msglen: msg length
1242 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001243 * called from the VF to configure the promiscuous mode of
1244 * VF vsis
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001245 **/
1246static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1247 u8 *msg, u16 msglen)
1248{
1249 struct i40e_virtchnl_promisc_info *info =
1250 (struct i40e_virtchnl_promisc_info *)msg;
1251 struct i40e_pf *pf = vf->pf;
1252 struct i40e_hw *hw = &pf->hw;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001253 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001254 bool allmulti = false;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001255 i40e_status aq_ret;
1256
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001257 vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001258 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1259 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1260 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001261 (vsi->type != I40E_VSI_FCOE)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001262 aq_ret = I40E_ERR_PARAM;
1263 goto error_param;
1264 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001265 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1266 allmulti = true;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001267 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001268 allmulti, NULL);
1269
1270error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001271 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001272 return i40e_vc_send_resp_to_vf(vf,
1273 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1274 aq_ret);
1275}
1276
1277/**
1278 * i40e_vc_config_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001279 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001280 * @msg: pointer to the msg buffer
1281 * @msglen: msg length
1282 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001283 * called from the VF to configure the rx/tx
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001284 * queues
1285 **/
1286static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1287{
1288 struct i40e_virtchnl_vsi_queue_config_info *qci =
1289 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1290 struct i40e_virtchnl_queue_pair_info *qpi;
Ashish Shah5f5e33b2014-07-10 07:58:15 +00001291 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001292 u16 vsi_id, vsi_queue_id;
1293 i40e_status aq_ret = 0;
1294 int i;
1295
1296 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1297 aq_ret = I40E_ERR_PARAM;
1298 goto error_param;
1299 }
1300
1301 vsi_id = qci->vsi_id;
1302 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1303 aq_ret = I40E_ERR_PARAM;
1304 goto error_param;
1305 }
1306 for (i = 0; i < qci->num_queue_pairs; i++) {
1307 qpi = &qci->qpair[i];
1308 vsi_queue_id = qpi->txq.queue_id;
1309 if ((qpi->txq.vsi_id != vsi_id) ||
1310 (qpi->rxq.vsi_id != vsi_id) ||
1311 (qpi->rxq.queue_id != vsi_queue_id) ||
1312 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1313 aq_ret = I40E_ERR_PARAM;
1314 goto error_param;
1315 }
1316
1317 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1318 &qpi->rxq) ||
1319 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1320 &qpi->txq)) {
1321 aq_ret = I40E_ERR_PARAM;
1322 goto error_param;
1323 }
1324 }
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001325 /* set vsi num_queue_pairs in use to num configured by VF */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001326 pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001327
1328error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001329 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001330 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1331 aq_ret);
1332}
1333
1334/**
1335 * i40e_vc_config_irq_map_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001336 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001337 * @msg: pointer to the msg buffer
1338 * @msglen: msg length
1339 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001340 * called from the VF to configure the irq to
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001341 * queue map
1342 **/
1343static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1344{
1345 struct i40e_virtchnl_irq_map_info *irqmap_info =
1346 (struct i40e_virtchnl_irq_map_info *)msg;
1347 struct i40e_virtchnl_vector_map *map;
1348 u16 vsi_id, vsi_queue_id, vector_id;
1349 i40e_status aq_ret = 0;
1350 unsigned long tempmap;
1351 int i;
1352
1353 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1354 aq_ret = I40E_ERR_PARAM;
1355 goto error_param;
1356 }
1357
1358 for (i = 0; i < irqmap_info->num_vectors; i++) {
1359 map = &irqmap_info->vecmap[i];
1360
1361 vector_id = map->vector_id;
1362 vsi_id = map->vsi_id;
1363 /* validate msg params */
1364 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1365 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1366 aq_ret = I40E_ERR_PARAM;
1367 goto error_param;
1368 }
1369
1370 /* lookout for the invalid queue index */
1371 tempmap = map->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001372 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001373 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1374 vsi_queue_id)) {
1375 aq_ret = I40E_ERR_PARAM;
1376 goto error_param;
1377 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001378 }
1379
1380 tempmap = map->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001381 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001382 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1383 vsi_queue_id)) {
1384 aq_ret = I40E_ERR_PARAM;
1385 goto error_param;
1386 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001387 }
1388
1389 i40e_config_irq_link_list(vf, vsi_id, map);
1390 }
1391error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001392 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001393 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1394 aq_ret);
1395}
1396
1397/**
1398 * i40e_vc_enable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001399 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001400 * @msg: pointer to the msg buffer
1401 * @msglen: msg length
1402 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001403 * called from the VF to enable all or specific queue(s)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001404 **/
1405static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1406{
1407 struct i40e_virtchnl_queue_select *vqs =
1408 (struct i40e_virtchnl_queue_select *)msg;
1409 struct i40e_pf *pf = vf->pf;
1410 u16 vsi_id = vqs->vsi_id;
1411 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001412
1413 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1414 aq_ret = I40E_ERR_PARAM;
1415 goto error_param;
1416 }
1417
1418 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1419 aq_ret = I40E_ERR_PARAM;
1420 goto error_param;
1421 }
1422
1423 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1424 aq_ret = I40E_ERR_PARAM;
1425 goto error_param;
1426 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001427
1428 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], true))
Mitch Williams88f65632013-11-28 06:39:28 +00001429 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001430error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001431 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001432 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1433 aq_ret);
1434}
1435
1436/**
1437 * i40e_vc_disable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001438 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001439 * @msg: pointer to the msg buffer
1440 * @msglen: msg length
1441 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001442 * called from the VF to disable all or specific
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001443 * queue(s)
1444 **/
1445static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1446{
1447 struct i40e_virtchnl_queue_select *vqs =
1448 (struct i40e_virtchnl_queue_select *)msg;
1449 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001450 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001451
1452 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1453 aq_ret = I40E_ERR_PARAM;
1454 goto error_param;
1455 }
1456
1457 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1458 aq_ret = I40E_ERR_PARAM;
1459 goto error_param;
1460 }
1461
1462 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1463 aq_ret = I40E_ERR_PARAM;
1464 goto error_param;
1465 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001466
1467 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false))
Mitch Williams88f65632013-11-28 06:39:28 +00001468 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001469
1470error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001471 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001472 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1473 aq_ret);
1474}
1475
1476/**
1477 * i40e_vc_get_stats_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001478 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001479 * @msg: pointer to the msg buffer
1480 * @msglen: msg length
1481 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001482 * called from the VF to get vsi stats
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001483 **/
1484static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1485{
1486 struct i40e_virtchnl_queue_select *vqs =
1487 (struct i40e_virtchnl_queue_select *)msg;
1488 struct i40e_pf *pf = vf->pf;
1489 struct i40e_eth_stats stats;
1490 i40e_status aq_ret = 0;
1491 struct i40e_vsi *vsi;
1492
1493 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1494
1495 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1496 aq_ret = I40E_ERR_PARAM;
1497 goto error_param;
1498 }
1499
1500 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1501 aq_ret = I40E_ERR_PARAM;
1502 goto error_param;
1503 }
1504
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001505 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001506 if (!vsi) {
1507 aq_ret = I40E_ERR_PARAM;
1508 goto error_param;
1509 }
1510 i40e_update_eth_stats(vsi);
Mitch Williams5a9769c2013-11-28 06:39:38 +00001511 stats = vsi->eth_stats;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001512
1513error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001514 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001515 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1516 (u8 *)&stats, sizeof(stats));
1517}
1518
1519/**
Greg Rosef657a6e2013-11-28 06:39:42 +00001520 * i40e_check_vf_permission
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001521 * @vf: pointer to the VF info
Greg Rosef657a6e2013-11-28 06:39:42 +00001522 * @macaddr: pointer to the MAC Address being checked
1523 *
1524 * Check if the VF has permission to add or delete unicast MAC address
1525 * filters and return error code -EPERM if not. Then check if the
1526 * address filter requested is broadcast or zero and if so return
1527 * an invalid MAC address error code.
1528 **/
1529static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1530{
1531 struct i40e_pf *pf = vf->pf;
1532 int ret = 0;
1533
1534 if (is_broadcast_ether_addr(macaddr) ||
1535 is_zero_ether_addr(macaddr)) {
1536 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1537 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rose5017c2a2013-12-07 10:36:54 +00001538 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1539 !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001540 /* If the host VMM administrator has set the VF MAC address
1541 * administratively via the ndo_set_vf_mac command then deny
1542 * permission to the VF to add or delete unicast MAC addresses.
Greg Rose5017c2a2013-12-07 10:36:54 +00001543 * The VF may request to set the MAC address filter already
1544 * assigned to it so do not return an error in that case.
Greg Rosef657a6e2013-11-28 06:39:42 +00001545 */
1546 dev_err(&pf->pdev->dev,
1547 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1548 ret = -EPERM;
1549 }
1550 return ret;
1551}
1552
1553/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001554 * i40e_vc_add_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001555 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001556 * @msg: pointer to the msg buffer
1557 * @msglen: msg length
1558 *
1559 * add guest mac address filter
1560 **/
1561static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1562{
1563 struct i40e_virtchnl_ether_addr_list *al =
1564 (struct i40e_virtchnl_ether_addr_list *)msg;
1565 struct i40e_pf *pf = vf->pf;
1566 struct i40e_vsi *vsi = NULL;
1567 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001568 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001569 int i;
1570
1571 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1572 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1573 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001574 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001575 goto error_param;
1576 }
1577
1578 for (i = 0; i < al->num_elements; i++) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001579 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1580 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001581 goto error_param;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001582 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001583 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001584
1585 /* add new addresses to the list */
1586 for (i = 0; i < al->num_elements; i++) {
1587 struct i40e_mac_filter *f;
1588
1589 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
Mitch Williams7e68edf92013-11-16 10:00:41 +00001590 if (!f) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001591 if (i40e_is_vsi_in_vlan(vsi))
1592 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1593 true, false);
1594 else
1595 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1596 true, false);
1597 }
1598
1599 if (!f) {
1600 dev_err(&pf->pdev->dev,
1601 "Unable to add VF MAC filter\n");
Greg Rosef657a6e2013-11-28 06:39:42 +00001602 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001603 goto error_param;
1604 }
1605 }
1606
1607 /* program the updated filter list */
1608 if (i40e_sync_vsi_filters(vsi))
1609 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1610
1611error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001612 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001613 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001614 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001615}
1616
1617/**
1618 * i40e_vc_del_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001619 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001620 * @msg: pointer to the msg buffer
1621 * @msglen: msg length
1622 *
1623 * remove guest mac address filter
1624 **/
1625static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1626{
1627 struct i40e_virtchnl_ether_addr_list *al =
1628 (struct i40e_virtchnl_ether_addr_list *)msg;
1629 struct i40e_pf *pf = vf->pf;
1630 struct i40e_vsi *vsi = NULL;
1631 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001632 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001633 int i;
1634
1635 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1636 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1637 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001638 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001639 goto error_param;
1640 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001641
1642 for (i = 0; i < al->num_elements; i++) {
Mitch Williams700bbf62013-12-21 05:44:45 +00001643 if (is_broadcast_ether_addr(al->list[i].addr) ||
1644 is_zero_ether_addr(al->list[i].addr)) {
1645 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
1646 al->list[i].addr);
1647 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rosef657a6e2013-11-28 06:39:42 +00001648 goto error_param;
Mitch Williams700bbf62013-12-21 05:44:45 +00001649 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001650 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001651 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001652
1653 /* delete addresses from the list */
1654 for (i = 0; i < al->num_elements; i++)
1655 i40e_del_filter(vsi, al->list[i].addr,
1656 I40E_VLAN_ANY, true, false);
1657
1658 /* program the updated filter list */
1659 if (i40e_sync_vsi_filters(vsi))
1660 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1661
1662error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001663 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001664 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001665 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001666}
1667
1668/**
1669 * i40e_vc_add_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001670 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001671 * @msg: pointer to the msg buffer
1672 * @msglen: msg length
1673 *
1674 * program guest vlan id
1675 **/
1676static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1677{
1678 struct i40e_virtchnl_vlan_filter_list *vfl =
1679 (struct i40e_virtchnl_vlan_filter_list *)msg;
1680 struct i40e_pf *pf = vf->pf;
1681 struct i40e_vsi *vsi = NULL;
1682 u16 vsi_id = vfl->vsi_id;
1683 i40e_status aq_ret = 0;
1684 int i;
1685
1686 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1687 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1688 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1689 aq_ret = I40E_ERR_PARAM;
1690 goto error_param;
1691 }
1692
1693 for (i = 0; i < vfl->num_elements; i++) {
1694 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1695 aq_ret = I40E_ERR_PARAM;
1696 dev_err(&pf->pdev->dev,
1697 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1698 goto error_param;
1699 }
1700 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001701 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001702 if (vsi->info.pvid) {
1703 aq_ret = I40E_ERR_PARAM;
1704 goto error_param;
1705 }
1706
1707 i40e_vlan_stripping_enable(vsi);
1708 for (i = 0; i < vfl->num_elements; i++) {
1709 /* add new VLAN filter */
1710 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1711 if (ret)
1712 dev_err(&pf->pdev->dev,
1713 "Unable to add VF vlan filter %d, error %d\n",
1714 vfl->vlan_id[i], ret);
1715 }
1716
1717error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001718 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001719 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1720}
1721
1722/**
1723 * i40e_vc_remove_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001724 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001725 * @msg: pointer to the msg buffer
1726 * @msglen: msg length
1727 *
1728 * remove programmed guest vlan id
1729 **/
1730static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1731{
1732 struct i40e_virtchnl_vlan_filter_list *vfl =
1733 (struct i40e_virtchnl_vlan_filter_list *)msg;
1734 struct i40e_pf *pf = vf->pf;
1735 struct i40e_vsi *vsi = NULL;
1736 u16 vsi_id = vfl->vsi_id;
1737 i40e_status aq_ret = 0;
1738 int i;
1739
1740 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1741 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1742 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1743 aq_ret = I40E_ERR_PARAM;
1744 goto error_param;
1745 }
1746
1747 for (i = 0; i < vfl->num_elements; i++) {
1748 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1749 aq_ret = I40E_ERR_PARAM;
1750 goto error_param;
1751 }
1752 }
1753
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001754 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001755 if (vsi->info.pvid) {
1756 aq_ret = I40E_ERR_PARAM;
1757 goto error_param;
1758 }
1759
1760 for (i = 0; i < vfl->num_elements; i++) {
1761 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1762 if (ret)
1763 dev_err(&pf->pdev->dev,
1764 "Unable to delete VF vlan filter %d, error %d\n",
1765 vfl->vlan_id[i], ret);
1766 }
1767
1768error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001769 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001770 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1771}
1772
1773/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001774 * i40e_vc_validate_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001775 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001776 * @msg: pointer to the msg buffer
1777 * @msglen: msg length
1778 * @msghndl: msg handle
1779 *
1780 * validate msg
1781 **/
1782static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1783 u32 v_retval, u8 *msg, u16 msglen)
1784{
1785 bool err_msg_format = false;
1786 int valid_len;
1787
1788 /* Check if VF is disabled. */
1789 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1790 return I40E_ERR_PARAM;
1791
1792 /* Validate message length. */
1793 switch (v_opcode) {
1794 case I40E_VIRTCHNL_OP_VERSION:
1795 valid_len = sizeof(struct i40e_virtchnl_version_info);
1796 break;
1797 case I40E_VIRTCHNL_OP_RESET_VF:
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001798 valid_len = 0;
1799 break;
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001800 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1801 if (VF_IS_V11(vf))
1802 valid_len = sizeof(u32);
1803 else
1804 valid_len = 0;
1805 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001806 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1807 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1808 break;
1809 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1810 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1811 break;
1812 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1813 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1814 if (msglen >= valid_len) {
1815 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1816 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1817 valid_len += (vqc->num_queue_pairs *
1818 sizeof(struct
1819 i40e_virtchnl_queue_pair_info));
1820 if (vqc->num_queue_pairs == 0)
1821 err_msg_format = true;
1822 }
1823 break;
1824 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1825 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1826 if (msglen >= valid_len) {
1827 struct i40e_virtchnl_irq_map_info *vimi =
1828 (struct i40e_virtchnl_irq_map_info *)msg;
1829 valid_len += (vimi->num_vectors *
1830 sizeof(struct i40e_virtchnl_vector_map));
1831 if (vimi->num_vectors == 0)
1832 err_msg_format = true;
1833 }
1834 break;
1835 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1836 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1837 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1838 break;
1839 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1840 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1841 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1842 if (msglen >= valid_len) {
1843 struct i40e_virtchnl_ether_addr_list *veal =
1844 (struct i40e_virtchnl_ether_addr_list *)msg;
1845 valid_len += veal->num_elements *
1846 sizeof(struct i40e_virtchnl_ether_addr);
1847 if (veal->num_elements == 0)
1848 err_msg_format = true;
1849 }
1850 break;
1851 case I40E_VIRTCHNL_OP_ADD_VLAN:
1852 case I40E_VIRTCHNL_OP_DEL_VLAN:
1853 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1854 if (msglen >= valid_len) {
1855 struct i40e_virtchnl_vlan_filter_list *vfl =
1856 (struct i40e_virtchnl_vlan_filter_list *)msg;
1857 valid_len += vfl->num_elements * sizeof(u16);
1858 if (vfl->num_elements == 0)
1859 err_msg_format = true;
1860 }
1861 break;
1862 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1863 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1864 break;
1865 case I40E_VIRTCHNL_OP_GET_STATS:
1866 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1867 break;
1868 /* These are always errors coming from the VF. */
1869 case I40E_VIRTCHNL_OP_EVENT:
1870 case I40E_VIRTCHNL_OP_UNKNOWN:
1871 default:
1872 return -EPERM;
1873 break;
1874 }
1875 /* few more checks */
1876 if ((valid_len != msglen) || (err_msg_format)) {
1877 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1878 return -EINVAL;
1879 } else {
1880 return 0;
1881 }
1882}
1883
1884/**
1885 * i40e_vc_process_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001886 * @pf: pointer to the PF structure
1887 * @vf_id: source VF id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001888 * @msg: pointer to the msg buffer
1889 * @msglen: msg length
1890 * @msghndl: msg handle
1891 *
1892 * called from the common aeq/arq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001893 * process request from VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001894 **/
1895int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1896 u32 v_retval, u8 *msg, u16 msglen)
1897{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001898 struct i40e_hw *hw = &pf->hw;
Dan Carpenterc243e962014-01-15 06:43:39 +00001899 unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001900 struct i40e_vf *vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001901 int ret;
1902
1903 pf->vf_aq_requests++;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001904 if (local_vf_id >= pf->num_alloc_vfs)
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001905 return -EINVAL;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001906 vf = &(pf->vf[local_vf_id]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001907 /* perform basic checks on the msg */
1908 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1909
1910 if (ret) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001911 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001912 local_vf_id, v_opcode, msglen);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001913 return ret;
1914 }
Mitch Williamsbae3cae2014-01-14 00:49:49 -08001915
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001916 switch (v_opcode) {
1917 case I40E_VIRTCHNL_OP_VERSION:
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001918 ret = i40e_vc_get_version_msg(vf, msg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001919 break;
1920 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001921 ret = i40e_vc_get_vf_resources_msg(vf, msg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001922 break;
1923 case I40E_VIRTCHNL_OP_RESET_VF:
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001924 i40e_vc_reset_vf_msg(vf);
1925 ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001926 break;
1927 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1928 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1929 break;
1930 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1931 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1932 break;
1933 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1934 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1935 break;
1936 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1937 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
Mitch Williams055b2952015-04-07 19:45:33 -04001938 i40e_vc_notify_vf_link_state(vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001939 break;
1940 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1941 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1942 break;
1943 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1944 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1945 break;
1946 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1947 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1948 break;
1949 case I40E_VIRTCHNL_OP_ADD_VLAN:
1950 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1951 break;
1952 case I40E_VIRTCHNL_OP_DEL_VLAN:
1953 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1954 break;
1955 case I40E_VIRTCHNL_OP_GET_STATS:
1956 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1957 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001958 case I40E_VIRTCHNL_OP_UNKNOWN:
1959 default:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001960 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001961 v_opcode, local_vf_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001962 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1963 I40E_ERR_NOT_IMPLEMENTED);
1964 break;
1965 }
1966
1967 return ret;
1968}
1969
1970/**
1971 * i40e_vc_process_vflr_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001972 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001973 *
1974 * called from the vlfr irq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001975 * free up VF resources and state variables
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001976 **/
1977int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1978{
1979 u32 reg, reg_idx, bit_idx, vf_id;
1980 struct i40e_hw *hw = &pf->hw;
1981 struct i40e_vf *vf;
1982
1983 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1984 return 0;
1985
Mitch Williamsc5c2f7c2014-11-11 03:15:04 +00001986 /* re-enable vflr interrupt cause */
1987 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1988 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1989 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1990 i40e_flush(hw);
1991
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001992 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1993 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1994 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1995 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001996 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001997 vf = &pf->vf[vf_id];
1998 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001999 if (reg & BIT(bit_idx)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002000 /* clear the bit in GLGEN_VFLRSTAT */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002001 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002002
Mitch Williamseb2d80b2014-02-13 03:48:48 -08002003 if (!test_bit(__I40E_DOWN, &pf->state))
2004 i40e_reset_vf(vf, true);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002005 }
2006 }
2007
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002008 return 0;
2009}
2010
2011/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002012 * i40e_ndo_set_vf_mac
2013 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002014 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002015 * @mac: mac address
2016 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002017 * program VF mac address
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002018 **/
2019int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2020{
2021 struct i40e_netdev_priv *np = netdev_priv(netdev);
2022 struct i40e_vsi *vsi = np->vsi;
2023 struct i40e_pf *pf = vsi->back;
2024 struct i40e_mac_filter *f;
2025 struct i40e_vf *vf;
2026 int ret = 0;
2027
2028 /* validate the request */
2029 if (vf_id >= pf->num_alloc_vfs) {
2030 dev_err(&pf->pdev->dev,
2031 "Invalid VF Identifier %d\n", vf_id);
2032 ret = -EINVAL;
2033 goto error_param;
2034 }
2035
2036 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002037 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002038 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2039 dev_err(&pf->pdev->dev,
2040 "Uninitialized VF %d\n", vf_id);
2041 ret = -EINVAL;
2042 goto error_param;
2043 }
2044
2045 if (!is_valid_ether_addr(mac)) {
2046 dev_err(&pf->pdev->dev,
2047 "Invalid VF ethernet address\n");
2048 ret = -EINVAL;
2049 goto error_param;
2050 }
2051
2052 /* delete the temporary mac address */
Mitch Williamse9951632015-04-27 14:57:13 -04002053 i40e_del_filter(vsi, vf->default_lan_addr.addr,
2054 vf->port_vlan_id ? vf->port_vlan_id : -1,
Greg Rose37cc0d22014-04-01 07:11:44 +00002055 true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002056
Greg Rose29f71bb2014-05-20 08:01:45 +00002057 /* Delete all the filters for this VSI - we're going to kill it
2058 * anyway.
2059 */
2060 list_for_each_entry(f, &vsi->mac_filter_list, list)
2061 i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002062
2063 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2064 /* program mac filter */
2065 if (i40e_sync_vsi_filters(vsi)) {
2066 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2067 ret = -EIO;
2068 goto error_param;
2069 }
Greg Rose9a173902014-05-22 06:32:02 +00002070 ether_addr_copy(vf->default_lan_addr.addr, mac);
Greg Rosef657a6e2013-11-28 06:39:42 +00002071 vf->pf_set_mac = true;
Greg Rose17413a82014-06-04 01:23:13 +00002072 /* Force the VF driver stop so it has to reload with new MAC address */
2073 i40e_vc_disable_vf(pf, vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002074 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002075
2076error_param:
2077 return ret;
2078}
2079
2080/**
2081 * i40e_ndo_set_vf_port_vlan
2082 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002083 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002084 * @vlan_id: mac address
2085 * @qos: priority setting
2086 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002087 * program VF vlan id and/or qos
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002088 **/
2089int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2090 int vf_id, u16 vlan_id, u8 qos)
2091{
2092 struct i40e_netdev_priv *np = netdev_priv(netdev);
2093 struct i40e_pf *pf = np->vsi->back;
2094 struct i40e_vsi *vsi;
2095 struct i40e_vf *vf;
2096 int ret = 0;
2097
2098 /* validate the request */
2099 if (vf_id >= pf->num_alloc_vfs) {
2100 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2101 ret = -EINVAL;
2102 goto error_pvid;
2103 }
2104
2105 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2106 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2107 ret = -EINVAL;
2108 goto error_pvid;
2109 }
2110
2111 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002112 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002113 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2114 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2115 ret = -EINVAL;
2116 goto error_pvid;
2117 }
2118
Mitch Williamsecbb44e2015-07-10 19:35:56 -04002119 if (le16_to_cpu(vsi->info.pvid) ==
2120 (vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT)))
Mitch Williams85927ec2015-04-27 14:57:10 -04002121 /* duplicate request, so just return success */
2122 goto error_pvid;
2123
Mitch Williamsecbb44e2015-07-10 19:35:56 -04002124 if (le16_to_cpu(vsi->info.pvid) == 0 && i40e_is_vsi_in_vlan(vsi)) {
Greg Rose99a49732014-01-13 16:13:03 -08002125 dev_err(&pf->pdev->dev,
2126 "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",
2127 vf_id);
Greg Rosef9b4b622014-03-06 09:02:28 +00002128 /* Administrator Error - knock the VF offline until he does
2129 * the right thing by reconfiguring his network correctly
2130 * and then reloading the VF driver.
2131 */
2132 i40e_vc_disable_vf(pf, vf);
2133 }
Greg Rose99a49732014-01-13 16:13:03 -08002134
Greg Rose8d82a7c2014-01-13 16:13:04 -08002135 /* Check for condition where there was already a port VLAN ID
2136 * filter set and now it is being deleted by setting it to zero.
Greg Rose1315f7c2014-03-14 07:32:20 +00002137 * Additionally check for the condition where there was a port
2138 * VLAN but now there is a new and different port VLAN being set.
Greg Rose8d82a7c2014-01-13 16:13:04 -08002139 * Before deleting all the old VLAN filters we must add new ones
2140 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2141 * MAC addresses deleted.
2142 */
Greg Rose1315f7c2014-03-14 07:32:20 +00002143 if ((!(vlan_id || qos) ||
2144 (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
2145 vsi->info.pvid)
Greg Rose8d82a7c2014-01-13 16:13:04 -08002146 ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2147
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002148 if (vsi->info.pvid) {
2149 /* kill old VLAN */
2150 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2151 VLAN_VID_MASK));
2152 if (ret) {
2153 dev_info(&vsi->back->pdev->dev,
2154 "remove VLAN failed, ret=%d, aq_err=%d\n",
2155 ret, pf->hw.aq.asq_last_status);
2156 }
2157 }
2158 if (vlan_id || qos)
2159 ret = i40e_vsi_add_pvid(vsi,
2160 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
2161 else
Greg Rose6c12fcb2013-11-28 06:39:34 +00002162 i40e_vsi_remove_pvid(vsi);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002163
2164 if (vlan_id) {
2165 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2166 vlan_id, qos, vf_id);
2167
2168 /* add new VLAN filter */
2169 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2170 if (ret) {
2171 dev_info(&vsi->back->pdev->dev,
2172 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2173 vsi->back->hw.aq.asq_last_status);
2174 goto error_pvid;
2175 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002176 /* Kill non-vlan MAC filters - ignore error return since
2177 * there might not be any non-vlan MAC filters.
2178 */
2179 i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002180 }
2181
2182 if (ret) {
2183 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2184 goto error_pvid;
2185 }
Greg Rose6c12fcb2013-11-28 06:39:34 +00002186 /* The Port VLAN needs to be saved across resets the same as the
2187 * default LAN MAC address.
2188 */
2189 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002190 ret = 0;
2191
2192error_pvid:
2193 return ret;
2194}
2195
Mitch Williams84590fd2014-04-09 05:58:57 +00002196#define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
2197#define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002198/**
2199 * i40e_ndo_set_vf_bw
2200 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002201 * @vf_id: VF identifier
2202 * @tx_rate: Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002203 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002204 * configure VF Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002205 **/
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002206int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2207 int max_tx_rate)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002208{
Mitch Williams6b192892014-03-06 09:02:29 +00002209 struct i40e_netdev_priv *np = netdev_priv(netdev);
2210 struct i40e_pf *pf = np->vsi->back;
2211 struct i40e_vsi *vsi;
2212 struct i40e_vf *vf;
2213 int speed = 0;
2214 int ret = 0;
2215
2216 /* validate the request */
2217 if (vf_id >= pf->num_alloc_vfs) {
2218 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2219 ret = -EINVAL;
2220 goto error;
2221 }
2222
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002223 if (min_tx_rate) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002224 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 -04002225 min_tx_rate, vf_id);
2226 return -EINVAL;
2227 }
2228
Mitch Williams6b192892014-03-06 09:02:29 +00002229 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002230 vsi = pf->vsi[vf->lan_vsi_idx];
Mitch Williams6b192892014-03-06 09:02:29 +00002231 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2232 dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
2233 ret = -EINVAL;
2234 goto error;
2235 }
2236
2237 switch (pf->hw.phy.link_info.link_speed) {
2238 case I40E_LINK_SPEED_40GB:
2239 speed = 40000;
2240 break;
2241 case I40E_LINK_SPEED_10GB:
2242 speed = 10000;
2243 break;
2244 case I40E_LINK_SPEED_1GB:
2245 speed = 1000;
2246 break;
2247 default:
2248 break;
2249 }
2250
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002251 if (max_tx_rate > speed) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002252 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002253 max_tx_rate, vf->vf_id);
Mitch Williams6b192892014-03-06 09:02:29 +00002254 ret = -EINVAL;
2255 goto error;
2256 }
2257
Mitch Williamsdac9b312014-04-09 05:58:56 +00002258 if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2259 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2260 max_tx_rate = 50;
2261 }
2262
Mitch Williams6b192892014-03-06 09:02:29 +00002263 /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
Mitch Williams84590fd2014-04-09 05:58:57 +00002264 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2265 max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2266 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
Mitch Williams6b192892014-03-06 09:02:29 +00002267 if (ret) {
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002268 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
Mitch Williams6b192892014-03-06 09:02:29 +00002269 ret);
2270 ret = -EIO;
2271 goto error;
2272 }
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002273 vf->tx_rate = max_tx_rate;
Mitch Williams6b192892014-03-06 09:02:29 +00002274error:
2275 return ret;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002276}
2277
2278/**
2279 * i40e_ndo_get_vf_config
2280 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002281 * @vf_id: VF identifier
2282 * @ivi: VF configuration structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002283 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002284 * return VF configuration
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002285 **/
2286int i40e_ndo_get_vf_config(struct net_device *netdev,
2287 int vf_id, struct ifla_vf_info *ivi)
2288{
2289 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002290 struct i40e_vsi *vsi = np->vsi;
2291 struct i40e_pf *pf = vsi->back;
2292 struct i40e_vf *vf;
2293 int ret = 0;
2294
2295 /* validate the request */
2296 if (vf_id >= pf->num_alloc_vfs) {
2297 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2298 ret = -EINVAL;
2299 goto error_param;
2300 }
2301
2302 vf = &(pf->vf[vf_id]);
2303 /* first vsi is always the LAN vsi */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002304 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002305 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2306 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2307 ret = -EINVAL;
2308 goto error_param;
2309 }
2310
2311 ivi->vf = vf_id;
2312
Mitch Williamsf4a1c5c2013-11-28 06:39:34 +00002313 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002314
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002315 ivi->max_tx_rate = vf->tx_rate;
2316 ivi->min_tx_rate = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002317 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2318 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2319 I40E_VLAN_PRIORITY_SHIFT;
Mitch Williams84ca55a2014-03-14 07:32:24 +00002320 if (vf->link_forced == false)
2321 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2322 else if (vf->link_up == true)
2323 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2324 else
2325 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
Mitch Williamsc674d122014-05-20 08:01:40 +00002326 ivi->spoofchk = vf->spoofchk;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002327 ret = 0;
2328
2329error_param:
2330 return ret;
2331}
Mitch Williams588aefa2014-02-11 08:27:49 +00002332
2333/**
2334 * i40e_ndo_set_vf_link_state
2335 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002336 * @vf_id: VF identifier
Mitch Williams588aefa2014-02-11 08:27:49 +00002337 * @link: required link state
2338 *
2339 * Set the link state of a specified VF, regardless of physical link state
2340 **/
2341int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
2342{
2343 struct i40e_netdev_priv *np = netdev_priv(netdev);
2344 struct i40e_pf *pf = np->vsi->back;
2345 struct i40e_virtchnl_pf_event pfe;
2346 struct i40e_hw *hw = &pf->hw;
2347 struct i40e_vf *vf;
Ashish Shahf19efbb2014-08-01 13:27:06 -07002348 int abs_vf_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002349 int ret = 0;
2350
2351 /* validate the request */
2352 if (vf_id >= pf->num_alloc_vfs) {
2353 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2354 ret = -EINVAL;
2355 goto error_out;
2356 }
2357
2358 vf = &pf->vf[vf_id];
Ashish Shahf19efbb2014-08-01 13:27:06 -07002359 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002360
2361 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
2362 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
2363
2364 switch (link) {
2365 case IFLA_VF_LINK_STATE_AUTO:
2366 vf->link_forced = false;
2367 pfe.event_data.link_event.link_status =
2368 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
2369 pfe.event_data.link_event.link_speed =
2370 pf->hw.phy.link_info.link_speed;
2371 break;
2372 case IFLA_VF_LINK_STATE_ENABLE:
2373 vf->link_forced = true;
2374 vf->link_up = true;
2375 pfe.event_data.link_event.link_status = true;
2376 pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
2377 break;
2378 case IFLA_VF_LINK_STATE_DISABLE:
2379 vf->link_forced = true;
2380 vf->link_up = false;
2381 pfe.event_data.link_event.link_status = false;
2382 pfe.event_data.link_event.link_speed = 0;
2383 break;
2384 default:
2385 ret = -EINVAL;
2386 goto error_out;
2387 }
2388 /* Notify the VF of its new link state */
Ashish Shahf19efbb2014-08-01 13:27:06 -07002389 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Mitch Williams588aefa2014-02-11 08:27:49 +00002390 0, (u8 *)&pfe, sizeof(pfe), NULL);
2391
2392error_out:
2393 return ret;
2394}
Mitch Williamsc674d122014-05-20 08:01:40 +00002395
2396/**
2397 * i40e_ndo_set_vf_spoofchk
2398 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002399 * @vf_id: VF identifier
Mitch Williamsc674d122014-05-20 08:01:40 +00002400 * @enable: flag to enable or disable feature
2401 *
2402 * Enable or disable VF spoof checking
2403 **/
Serey Konge6d90042014-07-12 07:28:14 +00002404int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
Mitch Williamsc674d122014-05-20 08:01:40 +00002405{
2406 struct i40e_netdev_priv *np = netdev_priv(netdev);
2407 struct i40e_vsi *vsi = np->vsi;
2408 struct i40e_pf *pf = vsi->back;
2409 struct i40e_vsi_context ctxt;
2410 struct i40e_hw *hw = &pf->hw;
2411 struct i40e_vf *vf;
2412 int ret = 0;
2413
2414 /* validate the request */
2415 if (vf_id >= pf->num_alloc_vfs) {
2416 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2417 ret = -EINVAL;
2418 goto out;
2419 }
2420
2421 vf = &(pf->vf[vf_id]);
2422
2423 if (enable == vf->spoofchk)
2424 goto out;
2425
2426 vf->spoofchk = enable;
2427 memset(&ctxt, 0, sizeof(ctxt));
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002428 ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
Mitch Williamsc674d122014-05-20 08:01:40 +00002429 ctxt.pf_num = pf->hw.pf_id;
2430 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
2431 if (enable)
Greg Rose30d71af2015-01-29 07:17:17 +00002432 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
2433 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
Mitch Williamsc674d122014-05-20 08:01:40 +00002434 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
2435 if (ret) {
2436 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
2437 ret);
2438 ret = -EIO;
2439 }
2440out:
2441 return ret;
2442}