blob: ae7548a5286eae6095d76f222d73c71c11c8f29b [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};
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400539
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700540 vf->lan_vsi_idx = vsi->idx;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000541 vf->lan_vsi_id = vsi->id;
Greg Rose6c12fcb2013-11-28 06:39:34 +0000542 /* If the port VLAN has been configured and then the
543 * VF driver was removed then the VSI port VLAN
544 * configuration was destroyed. Check if there is
545 * a port VLAN and restore the VSI configuration if
546 * needed.
547 */
548 if (vf->port_vlan_id)
549 i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000550 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
Mitch Williamse9951632015-04-27 14:57:13 -0400551 vf->port_vlan_id ? vf->port_vlan_id : -1,
552 true, false);
Greg Rose1a103702013-11-28 06:42:39 +0000553 if (!f)
554 dev_info(&pf->pdev->dev,
555 "Could not allocate VF MAC addr\n");
Mitch Williamse9951632015-04-27 14:57:13 -0400556 f = i40e_add_filter(vsi, brdcast,
557 vf->port_vlan_id ? vf->port_vlan_id : -1,
Greg Rose1a103702013-11-28 06:42:39 +0000558 true, false);
559 if (!f)
560 dev_info(&pf->pdev->dev,
561 "Could not allocate VF broadcast filter\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000562 }
Neerav Parikh6dbbbfb2013-11-26 10:49:24 +0000563
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000564 /* program mac filter */
Anjali Singhai30e25612015-09-28 13:37:12 -0700565 ret = i40e_sync_vsi_filters(vsi, false);
Mitch Williamsfd1646e2014-02-13 03:48:44 -0800566 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000567 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000568
Mitch Williams6b192892014-03-06 09:02:29 +0000569 /* Set VF bandwidth if specified */
570 if (vf->tx_rate) {
571 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
572 vf->tx_rate / 50, 0, NULL);
573 if (ret)
574 dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
575 vf->vf_id, ret);
576 }
577
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000578error_alloc_vsi_res:
579 return ret;
580}
581
582/**
Mitch Williams805bd5b2013-11-28 06:39:26 +0000583 * i40e_enable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000584 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000585 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000586 * enable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000587 **/
588static void i40e_enable_vf_mappings(struct i40e_vf *vf)
589{
590 struct i40e_pf *pf = vf->pf;
591 struct i40e_hw *hw = &pf->hw;
592 u32 reg, total_queue_pairs = 0;
593 int j;
594
595 /* Tell the hardware we're using noncontiguous mapping. HW requires
596 * that VF queues be mapped using this method, even when they are
597 * contiguous in real life
598 */
599 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
600 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
601
602 /* enable VF vplan_qtable mappings */
603 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
604 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
605
606 /* map PF queues to VF queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700607 for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
608 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400609
Mitch Williams805bd5b2013-11-28 06:39:26 +0000610 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
611 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
612 total_queue_pairs++;
613 }
614
615 /* map PF queues to VSI */
616 for (j = 0; j < 7; j++) {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700617 if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
Mitch Williams805bd5b2013-11-28 06:39:26 +0000618 reg = 0x07FF07FF; /* unused */
619 } else {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700620 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000621 j * 2);
622 reg = qid;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700623 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000624 (j * 2) + 1);
625 reg |= qid << 16;
626 }
627 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
628 }
629
630 i40e_flush(hw);
631}
632
633/**
634 * i40e_disable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000635 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000636 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000637 * disable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000638 **/
639static void i40e_disable_vf_mappings(struct i40e_vf *vf)
640{
641 struct i40e_pf *pf = vf->pf;
642 struct i40e_hw *hw = &pf->hw;
643 int i;
644
645 /* disable qp mappings */
646 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
647 for (i = 0; i < I40E_MAX_VSI_QP; i++)
648 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
649 I40E_QUEUE_END_OF_LIST);
650 i40e_flush(hw);
651}
652
653/**
654 * i40e_free_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000655 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000656 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000657 * free VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000658 **/
659static void i40e_free_vf_res(struct i40e_vf *vf)
660{
661 struct i40e_pf *pf = vf->pf;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000662 struct i40e_hw *hw = &pf->hw;
663 u32 reg_idx, reg;
664 int i, msix_vf;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000665
666 /* free vsi & disconnect it from the parent uplink */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700667 if (vf->lan_vsi_idx) {
668 i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
669 vf->lan_vsi_idx = 0;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000670 vf->lan_vsi_id = 0;
671 }
Mitch Williams9347eb72014-02-11 08:26:32 +0000672 msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
673
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000674 /* disable interrupts so the VF starts in a known state */
675 for (i = 0; i < msix_vf; i++) {
676 /* format is same for both registers */
677 if (0 == i)
678 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
679 else
680 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
681 (vf->vf_id))
682 + (i - 1));
683 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
684 i40e_flush(hw);
685 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000686
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000687 /* clear the irq settings */
688 for (i = 0; i < msix_vf; i++) {
689 /* format is same for both registers */
690 if (0 == i)
691 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
692 else
693 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
694 (vf->vf_id))
695 + (i - 1));
696 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
697 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
698 wr32(hw, reg_idx, reg);
699 i40e_flush(hw);
700 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000701 /* reset some of the state varibles keeping
702 * track of the resources
703 */
704 vf->num_queue_pairs = 0;
705 vf->vf_states = 0;
Mitch Williams21be99e2015-08-31 19:54:48 -0400706 clear_bit(I40E_VF_STAT_INIT, &vf->vf_states);
Mitch Williams805bd5b2013-11-28 06:39:26 +0000707}
708
709/**
710 * i40e_alloc_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000711 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000712 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000713 * allocate VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000714 **/
715static int i40e_alloc_vf_res(struct i40e_vf *vf)
716{
717 struct i40e_pf *pf = vf->pf;
718 int total_queue_pairs = 0;
719 int ret;
720
721 /* allocate hw vsi context & associated resources */
722 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
723 if (ret)
724 goto error_alloc;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700725 total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000726 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
727
728 /* store the total qps number for the runtime
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000729 * VF req validation
Mitch Williams805bd5b2013-11-28 06:39:26 +0000730 */
731 vf->num_queue_pairs = total_queue_pairs;
732
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000733 /* VF is now completely initialized */
Mitch Williams805bd5b2013-11-28 06:39:26 +0000734 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
735
736error_alloc:
737 if (ret)
738 i40e_free_vf_res(vf);
739
740 return ret;
741}
742
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000743#define VF_DEVICE_STATUS 0xAA
744#define VF_TRANS_PENDING_MASK 0x20
745/**
746 * i40e_quiesce_vf_pci
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000747 * @vf: pointer to the VF structure
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000748 *
749 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
750 * if the transactions never clear.
751 **/
752static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
753{
754 struct i40e_pf *pf = vf->pf;
755 struct i40e_hw *hw = &pf->hw;
756 int vf_abs_id, i;
757 u32 reg;
758
Mitch Williamsb141d612013-11-28 06:39:36 +0000759 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000760
761 wr32(hw, I40E_PF_PCI_CIAA,
762 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
763 for (i = 0; i < 100; i++) {
764 reg = rd32(hw, I40E_PF_PCI_CIAD);
765 if ((reg & VF_TRANS_PENDING_MASK) == 0)
766 return 0;
767 udelay(1);
768 }
769 return -EIO;
770}
771
Mitch Williams805bd5b2013-11-28 06:39:26 +0000772/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000773 * i40e_reset_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000774 * @vf: pointer to the VF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000775 * @flr: VFLR was issued or not
776 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000777 * reset the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000778 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000779void i40e_reset_vf(struct i40e_vf *vf, bool flr)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000780{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000781 struct i40e_pf *pf = vf->pf;
782 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000783 bool rsd = false;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000784 int i;
785 u32 reg;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000786
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000787 if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
788 return;
789
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000790 /* warn the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000791 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
792
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000793 /* In the case of a VFLR, the HW has already reset the VF and we
794 * just need to clean up, so don't hit the VFRTRIG register.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000795 */
796 if (!flr) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000797 /* reset VF using VPGEN_VFRTRIG reg */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000798 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
799 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000800 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
801 i40e_flush(hw);
802 }
803
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000804 if (i40e_quiesce_vf_pci(vf))
805 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
806 vf->vf_id);
807
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000808 /* poll VPGEN_VFRSTAT reg to make sure
809 * that reset is complete
810 */
Mitch Williams1750a222015-01-09 11:18:13 +0000811 for (i = 0; i < 10; i++) {
812 /* VF reset requires driver to first reset the VF and then
813 * poll the status register to make sure that the reset
814 * completed successfully. Due to internal HW FIFO flushes,
815 * we must wait 10ms before the register will be valid.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000816 */
Mitch Williams1750a222015-01-09 11:18:13 +0000817 usleep_range(10000, 20000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000818 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
819 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
820 rsd = true;
821 break;
822 }
823 }
824
Anjali Singhai Jain57175ac2015-04-07 19:45:35 -0400825 if (flr)
826 usleep_range(10000, 20000);
827
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000828 if (!rsd)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000829 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000830 vf->vf_id);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000831 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000832 /* clear the reset bit in the VPGEN_VFRTRIG reg */
833 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
834 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
835 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000836
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000837 /* On initial reset, we won't have any queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700838 if (vf->lan_vsi_idx == 0)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000839 goto complete_reset;
840
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700841 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000842complete_reset:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000843 /* reallocate VF resources to reset the VSI state */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000844 i40e_free_vf_res(vf);
Mitch Williams21be99e2015-08-31 19:54:48 -0400845 if (!i40e_alloc_vf_res(vf)) {
846 i40e_enable_vf_mappings(vf);
847 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
848 clear_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
849 }
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000850 /* tell the VF the reset is done */
851 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
852 i40e_flush(hw);
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000853 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000854}
Greg Rosec3542292013-12-13 08:38:38 +0000855
856/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000857 * i40e_free_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000858 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000859 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000860 * free VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000861 **/
862void i40e_free_vfs(struct i40e_pf *pf)
863{
Mitch Williamsf7414532013-11-28 06:39:40 +0000864 struct i40e_hw *hw = &pf->hw;
865 u32 reg_idx, bit_idx;
866 int i, tmp, vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000867
868 if (!pf->vf)
869 return;
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000870 while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
871 usleep_range(1000, 2000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000872
Mitch Williams44434632015-04-07 11:32:55 -0700873 for (i = 0; i < pf->num_alloc_vfs; i++)
874 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
875 i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
876 false);
877
Mitch Williams0325fca2015-08-26 15:14:09 -0400878 for (i = 0; i < pf->num_alloc_vfs; i++)
879 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
880 i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
881 false);
882
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000883 /* Disable IOV before freeing resources. This lets any VF drivers
884 * running in the host get themselves cleaned up before we yank
885 * the carpet out from underneath their feet.
886 */
887 if (!pci_vfs_assigned(pf->pdev))
888 pci_disable_sriov(pf->pdev);
Mitch Williams6d7b9672015-03-31 00:45:02 -0700889 else
890 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000891
892 msleep(20); /* let any messages in transit get finished up */
893
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000894 /* free up VF resources */
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000895 tmp = pf->num_alloc_vfs;
896 pf->num_alloc_vfs = 0;
897 for (i = 0; i < tmp; i++) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000898 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
899 i40e_free_vf_res(&pf->vf[i]);
900 /* disable qp mappings */
901 i40e_disable_vf_mappings(&pf->vf[i]);
902 }
903
904 kfree(pf->vf);
905 pf->vf = NULL;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000906
Mitch Williams9e5634d2014-04-04 04:43:14 +0000907 /* This check is for when the driver is unloaded while VFs are
908 * assigned. Setting the number of VFs to 0 through sysfs is caught
909 * before this function ever gets called.
910 */
Ethan Zhaoc24817b2014-07-22 18:36:43 +0000911 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williamsf7414532013-11-28 06:39:40 +0000912 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
913 * work correctly when SR-IOV gets re-enabled.
914 */
915 for (vf_id = 0; vf_id < tmp; vf_id++) {
916 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
917 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400918 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
Mitch Williamsf7414532013-11-28 06:39:40 +0000919 }
Greg Rosec3542292013-12-13 08:38:38 +0000920 }
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000921 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000922}
923
924#ifdef CONFIG_PCI_IOV
925/**
926 * i40e_alloc_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000927 * @pf: pointer to the PF structure
928 * @num_alloc_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000929 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000930 * allocate VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000931 **/
Mitch Williams4aeec012014-02-13 03:48:47 -0800932int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000933{
934 struct i40e_vf *vfs;
935 int i, ret = 0;
936
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000937 /* Disable interrupt 0 so we don't try to handle the VFLR. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000938 i40e_irq_dynamic_disable_icr0(pf);
939
Mitch Williams4aeec012014-02-13 03:48:47 -0800940 /* Check to see if we're just allocating resources for extant VFs */
941 if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
942 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
943 if (ret) {
Mitch Williams4aeec012014-02-13 03:48:47 -0800944 pf->num_alloc_vfs = 0;
945 goto err_iov;
946 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000947 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000948 /* allocate memory */
Akeem G Abodunrincc6456a2014-02-06 05:51:02 +0000949 vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000950 if (!vfs) {
951 ret = -ENOMEM;
952 goto err_alloc;
953 }
Mitch Williamsc674d122014-05-20 08:01:40 +0000954 pf->vf = vfs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000955
956 /* apply default profile */
957 for (i = 0; i < num_alloc_vfs; i++) {
958 vfs[i].pf = pf;
959 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
960 vfs[i].vf_id = i;
961
962 /* assign default capabilities */
963 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
Mitch Williamsc674d122014-05-20 08:01:40 +0000964 vfs[i].spoofchk = true;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000965 /* VF resources get allocated during reset */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000966 i40e_reset_vf(&vfs[i], false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000967
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000968 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000969 pf->num_alloc_vfs = num_alloc_vfs;
970
971err_alloc:
972 if (ret)
973 i40e_free_vfs(pf);
974err_iov:
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000975 /* Re-enable interrupt 0. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000976 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000977 return ret;
978}
979
980#endif
981/**
982 * i40e_pci_sriov_enable
983 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000984 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000985 *
986 * Enable or change the number of VFs
987 **/
988static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
989{
990#ifdef CONFIG_PCI_IOV
991 struct i40e_pf *pf = pci_get_drvdata(pdev);
992 int pre_existing_vfs = pci_num_vf(pdev);
993 int err = 0;
994
Jesse Brandeburg6995b362015-08-28 17:55:54 -0400995 if (test_bit(__I40E_TESTING, &pf->state)) {
Greg Rosee17bc412015-04-16 20:05:59 -0400996 dev_warn(&pdev->dev,
997 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
998 err = -EPERM;
999 goto err_out;
1000 }
1001
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001002 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1003 i40e_free_vfs(pf);
1004 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1005 goto out;
1006
1007 if (num_vfs > pf->num_req_vfs) {
Mitch Williams96c8d072015-08-27 11:42:35 -04001008 dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
1009 num_vfs, pf->num_req_vfs);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001010 err = -EPERM;
1011 goto err_out;
1012 }
1013
Mitch Williams96c8d072015-08-27 11:42:35 -04001014 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001015 err = i40e_alloc_vfs(pf, num_vfs);
1016 if (err) {
1017 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1018 goto err_out;
1019 }
1020
1021out:
1022 return num_vfs;
1023
1024err_out:
1025 return err;
1026#endif
1027 return 0;
1028}
1029
1030/**
1031 * i40e_pci_sriov_configure
1032 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001033 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001034 *
1035 * Enable or change the number of VFs. Called when the user updates the number
1036 * of VFs in sysfs.
1037 **/
1038int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1039{
1040 struct i40e_pf *pf = pci_get_drvdata(pdev);
1041
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07001042 if (num_vfs) {
1043 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1044 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1045 i40e_do_reset_safe(pf,
1046 BIT_ULL(__I40E_PF_RESET_REQUESTED));
1047 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001048 return i40e_pci_sriov_enable(pdev, num_vfs);
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07001049 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001050
Ethan Zhaoc24817b2014-07-22 18:36:43 +00001051 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williams9e5634d2014-04-04 04:43:14 +00001052 i40e_free_vfs(pf);
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07001053 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1054 i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Mitch Williams9e5634d2014-04-04 04:43:14 +00001055 } else {
1056 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1057 return -EINVAL;
1058 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001059 return 0;
1060}
1061
1062/***********************virtual channel routines******************/
1063
1064/**
1065 * i40e_vc_send_msg_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001066 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001067 * @v_opcode: virtual channel opcode
1068 * @v_retval: virtual channel return value
1069 * @msg: pointer to the msg buffer
1070 * @msglen: msg length
1071 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001072 * send msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001073 **/
1074static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1075 u32 v_retval, u8 *msg, u16 msglen)
1076{
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001077 struct i40e_pf *pf;
1078 struct i40e_hw *hw;
1079 int abs_vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001080 i40e_status aq_ret;
1081
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001082 /* validate the request */
1083 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1084 return -EINVAL;
1085
1086 pf = vf->pf;
1087 hw = &pf->hw;
1088 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1089
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001090 /* single place to detect unsuccessful return values */
1091 if (v_retval) {
1092 vf->num_invalid_msgs++;
1093 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
1094 v_opcode, v_retval);
1095 if (vf->num_invalid_msgs >
1096 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1097 dev_err(&pf->pdev->dev,
1098 "Number of invalid messages exceeded for VF %d\n",
1099 vf->vf_id);
1100 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1101 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1102 }
1103 } else {
1104 vf->num_valid_msgs++;
1105 }
1106
Ashish Shahf19efbb2014-08-01 13:27:06 -07001107 aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Mitch Williams7efa84b2013-11-28 06:39:41 +00001108 msg, msglen, NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001109 if (aq_ret) {
1110 dev_err(&pf->pdev->dev,
1111 "Unable to send the message to VF %d aq_err %d\n",
1112 vf->vf_id, pf->hw.aq.asq_last_status);
1113 return -EIO;
1114 }
1115
1116 return 0;
1117}
1118
1119/**
1120 * i40e_vc_send_resp_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001121 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001122 * @opcode: operation code
1123 * @retval: return value
1124 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001125 * send resp msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001126 **/
1127static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1128 enum i40e_virtchnl_ops opcode,
1129 i40e_status retval)
1130{
1131 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1132}
1133
1134/**
1135 * i40e_vc_get_version_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001136 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001137 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001138 * called from the VF to request the API version used by the PF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001139 **/
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001140static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001141{
1142 struct i40e_virtchnl_version_info info = {
1143 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
1144 };
1145
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001146 vf->vf_ver = *(struct i40e_virtchnl_version_info *)msg;
Mitch Williams606a5482015-06-04 16:24:00 -04001147 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1148 if (VF_IS_V10(vf))
1149 info.minor = I40E_VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001150 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
1151 I40E_SUCCESS, (u8 *)&info,
1152 sizeof(struct
1153 i40e_virtchnl_version_info));
1154}
1155
1156/**
1157 * i40e_vc_get_vf_resources_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001158 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001159 * @msg: pointer to the msg buffer
1160 * @msglen: msg length
1161 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001162 * called from the VF to request its resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001163 **/
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001164static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001165{
1166 struct i40e_virtchnl_vf_resource *vfres = NULL;
1167 struct i40e_pf *pf = vf->pf;
1168 i40e_status aq_ret = 0;
1169 struct i40e_vsi *vsi;
1170 int i = 0, len = 0;
1171 int num_vsis = 1;
1172 int ret;
1173
1174 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1175 aq_ret = I40E_ERR_PARAM;
1176 goto err;
1177 }
1178
1179 len = (sizeof(struct i40e_virtchnl_vf_resource) +
1180 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1181
1182 vfres = kzalloc(len, GFP_KERNEL);
1183 if (!vfres) {
1184 aq_ret = I40E_ERR_NO_MEMORY;
1185 len = 0;
1186 goto err;
1187 }
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001188 if (VF_IS_V11(vf))
1189 vf->driver_caps = *(u32 *)msg;
1190 else
1191 vf->driver_caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
1192 I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
1193 I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001194
1195 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001196 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001197 if (!vsi->info.pvid)
Anjali Singhai Jaine25d00b2015-06-23 19:00:04 -04001198 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1199 if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
1200 if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ)
1201 vfres->vf_offload_flags |=
1202 I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1203 } else {
1204 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG;
1205 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001206 vfres->num_vsis = num_vsis;
1207 vfres->num_queue_pairs = vf->num_queue_pairs;
1208 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001209 if (vf->lan_vsi_idx) {
1210 vfres->vsi_res[i].vsi_id = vf->lan_vsi_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001211 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
Mitch Williamsf578f5f2015-08-28 17:55:58 -04001212 vfres->vsi_res[i].num_queue_pairs = vsi->alloc_queue_pairs;
1213 /* VFs only use TC 0 */
1214 vfres->vsi_res[i].qset_handle
1215 = le16_to_cpu(vsi->info.qs_handle[0]);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001216 ether_addr_copy(vfres->vsi_res[i].default_mac_addr,
1217 vf->default_lan_addr.addr);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001218 i++;
1219 }
1220 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1221
1222err:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001223 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001224 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1225 aq_ret, (u8 *)vfres, len);
1226
1227 kfree(vfres);
1228 return ret;
1229}
1230
1231/**
1232 * i40e_vc_reset_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001233 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001234 * @msg: pointer to the msg buffer
1235 * @msglen: msg length
1236 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001237 * called from the VF to reset itself,
1238 * unlike other virtchnl messages, PF driver
1239 * doesn't send the response back to the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001240 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001241static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001242{
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001243 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1244 i40e_reset_vf(vf, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001245}
1246
1247/**
1248 * i40e_vc_config_promiscuous_mode_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001249 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001250 * @msg: pointer to the msg buffer
1251 * @msglen: msg length
1252 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001253 * called from the VF to configure the promiscuous mode of
1254 * VF vsis
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001255 **/
1256static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1257 u8 *msg, u16 msglen)
1258{
1259 struct i40e_virtchnl_promisc_info *info =
1260 (struct i40e_virtchnl_promisc_info *)msg;
1261 struct i40e_pf *pf = vf->pf;
1262 struct i40e_hw *hw = &pf->hw;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001263 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001264 bool allmulti = false;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001265 i40e_status aq_ret;
1266
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001267 vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001268 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1269 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1270 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001271 (vsi->type != I40E_VSI_FCOE)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001272 aq_ret = I40E_ERR_PARAM;
1273 goto error_param;
1274 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001275 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1276 allmulti = true;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001277 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001278 allmulti, NULL);
1279
1280error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001281 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001282 return i40e_vc_send_resp_to_vf(vf,
1283 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1284 aq_ret);
1285}
1286
1287/**
1288 * i40e_vc_config_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001289 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001290 * @msg: pointer to the msg buffer
1291 * @msglen: msg length
1292 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001293 * called from the VF to configure the rx/tx
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001294 * queues
1295 **/
1296static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1297{
1298 struct i40e_virtchnl_vsi_queue_config_info *qci =
1299 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1300 struct i40e_virtchnl_queue_pair_info *qpi;
Ashish Shah5f5e33b2014-07-10 07:58:15 +00001301 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001302 u16 vsi_id, vsi_queue_id;
1303 i40e_status aq_ret = 0;
1304 int i;
1305
1306 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1307 aq_ret = I40E_ERR_PARAM;
1308 goto error_param;
1309 }
1310
1311 vsi_id = qci->vsi_id;
1312 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1313 aq_ret = I40E_ERR_PARAM;
1314 goto error_param;
1315 }
1316 for (i = 0; i < qci->num_queue_pairs; i++) {
1317 qpi = &qci->qpair[i];
1318 vsi_queue_id = qpi->txq.queue_id;
1319 if ((qpi->txq.vsi_id != vsi_id) ||
1320 (qpi->rxq.vsi_id != vsi_id) ||
1321 (qpi->rxq.queue_id != vsi_queue_id) ||
1322 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1323 aq_ret = I40E_ERR_PARAM;
1324 goto error_param;
1325 }
1326
1327 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1328 &qpi->rxq) ||
1329 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1330 &qpi->txq)) {
1331 aq_ret = I40E_ERR_PARAM;
1332 goto error_param;
1333 }
1334 }
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001335 /* set vsi num_queue_pairs in use to num configured by VF */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001336 pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001337
1338error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001339 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001340 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1341 aq_ret);
1342}
1343
1344/**
1345 * i40e_vc_config_irq_map_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001346 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001347 * @msg: pointer to the msg buffer
1348 * @msglen: msg length
1349 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001350 * called from the VF to configure the irq to
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001351 * queue map
1352 **/
1353static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1354{
1355 struct i40e_virtchnl_irq_map_info *irqmap_info =
1356 (struct i40e_virtchnl_irq_map_info *)msg;
1357 struct i40e_virtchnl_vector_map *map;
1358 u16 vsi_id, vsi_queue_id, vector_id;
1359 i40e_status aq_ret = 0;
1360 unsigned long tempmap;
1361 int i;
1362
1363 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1364 aq_ret = I40E_ERR_PARAM;
1365 goto error_param;
1366 }
1367
1368 for (i = 0; i < irqmap_info->num_vectors; i++) {
1369 map = &irqmap_info->vecmap[i];
1370
1371 vector_id = map->vector_id;
1372 vsi_id = map->vsi_id;
1373 /* validate msg params */
1374 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1375 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1376 aq_ret = I40E_ERR_PARAM;
1377 goto error_param;
1378 }
1379
1380 /* lookout for the invalid queue index */
1381 tempmap = map->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001382 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001383 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1384 vsi_queue_id)) {
1385 aq_ret = I40E_ERR_PARAM;
1386 goto error_param;
1387 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001388 }
1389
1390 tempmap = map->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001391 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001392 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1393 vsi_queue_id)) {
1394 aq_ret = I40E_ERR_PARAM;
1395 goto error_param;
1396 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001397 }
1398
1399 i40e_config_irq_link_list(vf, vsi_id, map);
1400 }
1401error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001402 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001403 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1404 aq_ret);
1405}
1406
1407/**
1408 * i40e_vc_enable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001409 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001410 * @msg: pointer to the msg buffer
1411 * @msglen: msg length
1412 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001413 * called from the VF to enable all or specific queue(s)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001414 **/
1415static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1416{
1417 struct i40e_virtchnl_queue_select *vqs =
1418 (struct i40e_virtchnl_queue_select *)msg;
1419 struct i40e_pf *pf = vf->pf;
1420 u16 vsi_id = vqs->vsi_id;
1421 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001422
1423 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1424 aq_ret = I40E_ERR_PARAM;
1425 goto error_param;
1426 }
1427
1428 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1429 aq_ret = I40E_ERR_PARAM;
1430 goto error_param;
1431 }
1432
1433 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1434 aq_ret = I40E_ERR_PARAM;
1435 goto error_param;
1436 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001437
1438 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], true))
Mitch Williams88f65632013-11-28 06:39:28 +00001439 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001440error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001441 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001442 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1443 aq_ret);
1444}
1445
1446/**
1447 * i40e_vc_disable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001448 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001449 * @msg: pointer to the msg buffer
1450 * @msglen: msg length
1451 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001452 * called from the VF to disable all or specific
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001453 * queue(s)
1454 **/
1455static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1456{
1457 struct i40e_virtchnl_queue_select *vqs =
1458 (struct i40e_virtchnl_queue_select *)msg;
1459 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001460 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001461
1462 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1463 aq_ret = I40E_ERR_PARAM;
1464 goto error_param;
1465 }
1466
1467 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1468 aq_ret = I40E_ERR_PARAM;
1469 goto error_param;
1470 }
1471
1472 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1473 aq_ret = I40E_ERR_PARAM;
1474 goto error_param;
1475 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001476
1477 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false))
Mitch Williams88f65632013-11-28 06:39:28 +00001478 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001479
1480error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001481 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001482 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1483 aq_ret);
1484}
1485
1486/**
1487 * i40e_vc_get_stats_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001488 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001489 * @msg: pointer to the msg buffer
1490 * @msglen: msg length
1491 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001492 * called from the VF to get vsi stats
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001493 **/
1494static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1495{
1496 struct i40e_virtchnl_queue_select *vqs =
1497 (struct i40e_virtchnl_queue_select *)msg;
1498 struct i40e_pf *pf = vf->pf;
1499 struct i40e_eth_stats stats;
1500 i40e_status aq_ret = 0;
1501 struct i40e_vsi *vsi;
1502
1503 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1504
1505 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1506 aq_ret = I40E_ERR_PARAM;
1507 goto error_param;
1508 }
1509
1510 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1511 aq_ret = I40E_ERR_PARAM;
1512 goto error_param;
1513 }
1514
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001515 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001516 if (!vsi) {
1517 aq_ret = I40E_ERR_PARAM;
1518 goto error_param;
1519 }
1520 i40e_update_eth_stats(vsi);
Mitch Williams5a9769c2013-11-28 06:39:38 +00001521 stats = vsi->eth_stats;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001522
1523error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001524 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001525 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1526 (u8 *)&stats, sizeof(stats));
1527}
1528
1529/**
Greg Rosef657a6e2013-11-28 06:39:42 +00001530 * i40e_check_vf_permission
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001531 * @vf: pointer to the VF info
Greg Rosef657a6e2013-11-28 06:39:42 +00001532 * @macaddr: pointer to the MAC Address being checked
1533 *
1534 * Check if the VF has permission to add or delete unicast MAC address
1535 * filters and return error code -EPERM if not. Then check if the
1536 * address filter requested is broadcast or zero and if so return
1537 * an invalid MAC address error code.
1538 **/
1539static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1540{
1541 struct i40e_pf *pf = vf->pf;
1542 int ret = 0;
1543
1544 if (is_broadcast_ether_addr(macaddr) ||
1545 is_zero_ether_addr(macaddr)) {
1546 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1547 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rose5017c2a2013-12-07 10:36:54 +00001548 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1549 !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001550 /* If the host VMM administrator has set the VF MAC address
1551 * administratively via the ndo_set_vf_mac command then deny
1552 * permission to the VF to add or delete unicast MAC addresses.
Greg Rose5017c2a2013-12-07 10:36:54 +00001553 * The VF may request to set the MAC address filter already
1554 * assigned to it so do not return an error in that case.
Greg Rosef657a6e2013-11-28 06:39:42 +00001555 */
1556 dev_err(&pf->pdev->dev,
1557 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1558 ret = -EPERM;
1559 }
1560 return ret;
1561}
1562
1563/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001564 * i40e_vc_add_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001565 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001566 * @msg: pointer to the msg buffer
1567 * @msglen: msg length
1568 *
1569 * add guest mac address filter
1570 **/
1571static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1572{
1573 struct i40e_virtchnl_ether_addr_list *al =
1574 (struct i40e_virtchnl_ether_addr_list *)msg;
1575 struct i40e_pf *pf = vf->pf;
1576 struct i40e_vsi *vsi = NULL;
1577 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001578 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001579 int i;
1580
1581 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1582 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1583 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001584 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001585 goto error_param;
1586 }
1587
1588 for (i = 0; i < al->num_elements; i++) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001589 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1590 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001591 goto error_param;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001592 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001593 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001594
1595 /* add new addresses to the list */
1596 for (i = 0; i < al->num_elements; i++) {
1597 struct i40e_mac_filter *f;
1598
1599 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
Mitch Williams7e68edf92013-11-16 10:00:41 +00001600 if (!f) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001601 if (i40e_is_vsi_in_vlan(vsi))
1602 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1603 true, false);
1604 else
1605 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1606 true, false);
1607 }
1608
1609 if (!f) {
1610 dev_err(&pf->pdev->dev,
1611 "Unable to add VF MAC filter\n");
Greg Rosef657a6e2013-11-28 06:39:42 +00001612 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001613 goto error_param;
1614 }
1615 }
1616
1617 /* program the updated filter list */
Anjali Singhai30e25612015-09-28 13:37:12 -07001618 if (i40e_sync_vsi_filters(vsi, false))
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001619 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1620
1621error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001622 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001623 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001624 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001625}
1626
1627/**
1628 * i40e_vc_del_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001629 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001630 * @msg: pointer to the msg buffer
1631 * @msglen: msg length
1632 *
1633 * remove guest mac address filter
1634 **/
1635static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1636{
1637 struct i40e_virtchnl_ether_addr_list *al =
1638 (struct i40e_virtchnl_ether_addr_list *)msg;
1639 struct i40e_pf *pf = vf->pf;
1640 struct i40e_vsi *vsi = NULL;
1641 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001642 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001643 int i;
1644
1645 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1646 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1647 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001648 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001649 goto error_param;
1650 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001651
1652 for (i = 0; i < al->num_elements; i++) {
Mitch Williams700bbf62013-12-21 05:44:45 +00001653 if (is_broadcast_ether_addr(al->list[i].addr) ||
1654 is_zero_ether_addr(al->list[i].addr)) {
1655 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
1656 al->list[i].addr);
1657 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rosef657a6e2013-11-28 06:39:42 +00001658 goto error_param;
Mitch Williams700bbf62013-12-21 05:44:45 +00001659 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001660 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001661 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001662
1663 /* delete addresses from the list */
1664 for (i = 0; i < al->num_elements; i++)
1665 i40e_del_filter(vsi, al->list[i].addr,
1666 I40E_VLAN_ANY, true, false);
1667
1668 /* program the updated filter list */
Anjali Singhai30e25612015-09-28 13:37:12 -07001669 if (i40e_sync_vsi_filters(vsi, false))
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001670 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1671
1672error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001673 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001674 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001675 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001676}
1677
1678/**
1679 * i40e_vc_add_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001680 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001681 * @msg: pointer to the msg buffer
1682 * @msglen: msg length
1683 *
1684 * program guest vlan id
1685 **/
1686static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1687{
1688 struct i40e_virtchnl_vlan_filter_list *vfl =
1689 (struct i40e_virtchnl_vlan_filter_list *)msg;
1690 struct i40e_pf *pf = vf->pf;
1691 struct i40e_vsi *vsi = NULL;
1692 u16 vsi_id = vfl->vsi_id;
1693 i40e_status aq_ret = 0;
1694 int i;
1695
1696 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1697 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1698 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1699 aq_ret = I40E_ERR_PARAM;
1700 goto error_param;
1701 }
1702
1703 for (i = 0; i < vfl->num_elements; i++) {
1704 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1705 aq_ret = I40E_ERR_PARAM;
1706 dev_err(&pf->pdev->dev,
1707 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1708 goto error_param;
1709 }
1710 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001711 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001712 if (vsi->info.pvid) {
1713 aq_ret = I40E_ERR_PARAM;
1714 goto error_param;
1715 }
1716
1717 i40e_vlan_stripping_enable(vsi);
1718 for (i = 0; i < vfl->num_elements; i++) {
1719 /* add new VLAN filter */
1720 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001721
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001722 if (ret)
1723 dev_err(&pf->pdev->dev,
1724 "Unable to add VF vlan filter %d, error %d\n",
1725 vfl->vlan_id[i], ret);
1726 }
1727
1728error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001729 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001730 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1731}
1732
1733/**
1734 * i40e_vc_remove_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001735 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001736 * @msg: pointer to the msg buffer
1737 * @msglen: msg length
1738 *
1739 * remove programmed guest vlan id
1740 **/
1741static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1742{
1743 struct i40e_virtchnl_vlan_filter_list *vfl =
1744 (struct i40e_virtchnl_vlan_filter_list *)msg;
1745 struct i40e_pf *pf = vf->pf;
1746 struct i40e_vsi *vsi = NULL;
1747 u16 vsi_id = vfl->vsi_id;
1748 i40e_status aq_ret = 0;
1749 int i;
1750
1751 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1752 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1753 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1754 aq_ret = I40E_ERR_PARAM;
1755 goto error_param;
1756 }
1757
1758 for (i = 0; i < vfl->num_elements; i++) {
1759 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1760 aq_ret = I40E_ERR_PARAM;
1761 goto error_param;
1762 }
1763 }
1764
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001765 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001766 if (vsi->info.pvid) {
1767 aq_ret = I40E_ERR_PARAM;
1768 goto error_param;
1769 }
1770
1771 for (i = 0; i < vfl->num_elements; i++) {
1772 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
Jesse Brandeburg6995b362015-08-28 17:55:54 -04001773
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001774 if (ret)
1775 dev_err(&pf->pdev->dev,
1776 "Unable to delete VF vlan filter %d, error %d\n",
1777 vfl->vlan_id[i], ret);
1778 }
1779
1780error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001781 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001782 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1783}
1784
1785/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001786 * i40e_vc_validate_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001787 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001788 * @msg: pointer to the msg buffer
1789 * @msglen: msg length
1790 * @msghndl: msg handle
1791 *
1792 * validate msg
1793 **/
1794static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1795 u32 v_retval, u8 *msg, u16 msglen)
1796{
1797 bool err_msg_format = false;
1798 int valid_len;
1799
1800 /* Check if VF is disabled. */
1801 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1802 return I40E_ERR_PARAM;
1803
1804 /* Validate message length. */
1805 switch (v_opcode) {
1806 case I40E_VIRTCHNL_OP_VERSION:
1807 valid_len = sizeof(struct i40e_virtchnl_version_info);
1808 break;
1809 case I40E_VIRTCHNL_OP_RESET_VF:
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001810 valid_len = 0;
1811 break;
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001812 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1813 if (VF_IS_V11(vf))
1814 valid_len = sizeof(u32);
1815 else
1816 valid_len = 0;
1817 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001818 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1819 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1820 break;
1821 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1822 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1823 break;
1824 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1825 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1826 if (msglen >= valid_len) {
1827 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1828 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1829 valid_len += (vqc->num_queue_pairs *
1830 sizeof(struct
1831 i40e_virtchnl_queue_pair_info));
1832 if (vqc->num_queue_pairs == 0)
1833 err_msg_format = true;
1834 }
1835 break;
1836 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1837 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1838 if (msglen >= valid_len) {
1839 struct i40e_virtchnl_irq_map_info *vimi =
1840 (struct i40e_virtchnl_irq_map_info *)msg;
1841 valid_len += (vimi->num_vectors *
1842 sizeof(struct i40e_virtchnl_vector_map));
1843 if (vimi->num_vectors == 0)
1844 err_msg_format = true;
1845 }
1846 break;
1847 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1848 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1849 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1850 break;
1851 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1852 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1853 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1854 if (msglen >= valid_len) {
1855 struct i40e_virtchnl_ether_addr_list *veal =
1856 (struct i40e_virtchnl_ether_addr_list *)msg;
1857 valid_len += veal->num_elements *
1858 sizeof(struct i40e_virtchnl_ether_addr);
1859 if (veal->num_elements == 0)
1860 err_msg_format = true;
1861 }
1862 break;
1863 case I40E_VIRTCHNL_OP_ADD_VLAN:
1864 case I40E_VIRTCHNL_OP_DEL_VLAN:
1865 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1866 if (msglen >= valid_len) {
1867 struct i40e_virtchnl_vlan_filter_list *vfl =
1868 (struct i40e_virtchnl_vlan_filter_list *)msg;
1869 valid_len += vfl->num_elements * sizeof(u16);
1870 if (vfl->num_elements == 0)
1871 err_msg_format = true;
1872 }
1873 break;
1874 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1875 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1876 break;
1877 case I40E_VIRTCHNL_OP_GET_STATS:
1878 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1879 break;
1880 /* These are always errors coming from the VF. */
1881 case I40E_VIRTCHNL_OP_EVENT:
1882 case I40E_VIRTCHNL_OP_UNKNOWN:
1883 default:
1884 return -EPERM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001885 }
1886 /* few more checks */
1887 if ((valid_len != msglen) || (err_msg_format)) {
1888 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1889 return -EINVAL;
1890 } else {
1891 return 0;
1892 }
1893}
1894
1895/**
1896 * i40e_vc_process_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001897 * @pf: pointer to the PF structure
1898 * @vf_id: source VF id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001899 * @msg: pointer to the msg buffer
1900 * @msglen: msg length
1901 * @msghndl: msg handle
1902 *
1903 * called from the common aeq/arq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001904 * process request from VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001905 **/
1906int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1907 u32 v_retval, u8 *msg, u16 msglen)
1908{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001909 struct i40e_hw *hw = &pf->hw;
Dan Carpenterc243e962014-01-15 06:43:39 +00001910 unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001911 struct i40e_vf *vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001912 int ret;
1913
1914 pf->vf_aq_requests++;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001915 if (local_vf_id >= pf->num_alloc_vfs)
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001916 return -EINVAL;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001917 vf = &(pf->vf[local_vf_id]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001918 /* perform basic checks on the msg */
1919 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1920
1921 if (ret) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001922 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001923 local_vf_id, v_opcode, msglen);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001924 return ret;
1925 }
Mitch Williamsbae3cae2014-01-14 00:49:49 -08001926
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001927 switch (v_opcode) {
1928 case I40E_VIRTCHNL_OP_VERSION:
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001929 ret = i40e_vc_get_version_msg(vf, msg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001930 break;
1931 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001932 ret = i40e_vc_get_vf_resources_msg(vf, msg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001933 break;
1934 case I40E_VIRTCHNL_OP_RESET_VF:
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001935 i40e_vc_reset_vf_msg(vf);
1936 ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001937 break;
1938 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1939 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1940 break;
1941 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1942 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1943 break;
1944 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1945 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1946 break;
1947 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1948 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
Mitch Williams055b2952015-04-07 19:45:33 -04001949 i40e_vc_notify_vf_link_state(vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001950 break;
1951 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1952 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1953 break;
1954 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1955 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1956 break;
1957 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1958 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1959 break;
1960 case I40E_VIRTCHNL_OP_ADD_VLAN:
1961 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1962 break;
1963 case I40E_VIRTCHNL_OP_DEL_VLAN:
1964 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1965 break;
1966 case I40E_VIRTCHNL_OP_GET_STATS:
1967 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1968 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001969 case I40E_VIRTCHNL_OP_UNKNOWN:
1970 default:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001971 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001972 v_opcode, local_vf_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001973 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1974 I40E_ERR_NOT_IMPLEMENTED);
1975 break;
1976 }
1977
1978 return ret;
1979}
1980
1981/**
1982 * i40e_vc_process_vflr_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001983 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001984 *
1985 * called from the vlfr irq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001986 * free up VF resources and state variables
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001987 **/
1988int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1989{
1990 u32 reg, reg_idx, bit_idx, vf_id;
1991 struct i40e_hw *hw = &pf->hw;
1992 struct i40e_vf *vf;
1993
1994 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1995 return 0;
1996
Mitch Williamsc5c2f7c2014-11-11 03:15:04 +00001997 /* re-enable vflr interrupt cause */
1998 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1999 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
2000 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
2001 i40e_flush(hw);
2002
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002003 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2004 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
2005 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
2006 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002007 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002008 vf = &pf->vf[vf_id];
2009 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002010 if (reg & BIT(bit_idx)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002011 /* clear the bit in GLGEN_VFLRSTAT */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04002012 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002013
Mitch Williamseb2d80b2014-02-13 03:48:48 -08002014 if (!test_bit(__I40E_DOWN, &pf->state))
2015 i40e_reset_vf(vf, true);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002016 }
2017 }
2018
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002019 return 0;
2020}
2021
2022/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002023 * i40e_ndo_set_vf_mac
2024 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002025 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002026 * @mac: mac address
2027 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002028 * program VF mac address
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002029 **/
2030int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2031{
2032 struct i40e_netdev_priv *np = netdev_priv(netdev);
2033 struct i40e_vsi *vsi = np->vsi;
2034 struct i40e_pf *pf = vsi->back;
2035 struct i40e_mac_filter *f;
2036 struct i40e_vf *vf;
2037 int ret = 0;
2038
2039 /* validate the request */
2040 if (vf_id >= pf->num_alloc_vfs) {
2041 dev_err(&pf->pdev->dev,
2042 "Invalid VF Identifier %d\n", vf_id);
2043 ret = -EINVAL;
2044 goto error_param;
2045 }
2046
2047 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002048 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002049 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2050 dev_err(&pf->pdev->dev,
2051 "Uninitialized VF %d\n", vf_id);
2052 ret = -EINVAL;
2053 goto error_param;
2054 }
2055
2056 if (!is_valid_ether_addr(mac)) {
2057 dev_err(&pf->pdev->dev,
2058 "Invalid VF ethernet address\n");
2059 ret = -EINVAL;
2060 goto error_param;
2061 }
2062
2063 /* delete the temporary mac address */
Mitch Williamse9951632015-04-27 14:57:13 -04002064 i40e_del_filter(vsi, vf->default_lan_addr.addr,
2065 vf->port_vlan_id ? vf->port_vlan_id : -1,
Greg Rose37cc0d22014-04-01 07:11:44 +00002066 true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002067
Greg Rose29f71bb2014-05-20 08:01:45 +00002068 /* Delete all the filters for this VSI - we're going to kill it
2069 * anyway.
2070 */
2071 list_for_each_entry(f, &vsi->mac_filter_list, list)
2072 i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002073
2074 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2075 /* program mac filter */
Anjali Singhai30e25612015-09-28 13:37:12 -07002076 if (i40e_sync_vsi_filters(vsi, false)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002077 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2078 ret = -EIO;
2079 goto error_param;
2080 }
Greg Rose9a173902014-05-22 06:32:02 +00002081 ether_addr_copy(vf->default_lan_addr.addr, mac);
Greg Rosef657a6e2013-11-28 06:39:42 +00002082 vf->pf_set_mac = true;
Greg Rose17413a82014-06-04 01:23:13 +00002083 /* Force the VF driver stop so it has to reload with new MAC address */
2084 i40e_vc_disable_vf(pf, vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002085 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002086
2087error_param:
2088 return ret;
2089}
2090
2091/**
2092 * i40e_ndo_set_vf_port_vlan
2093 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002094 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002095 * @vlan_id: mac address
2096 * @qos: priority setting
2097 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002098 * program VF vlan id and/or qos
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002099 **/
2100int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2101 int vf_id, u16 vlan_id, u8 qos)
2102{
Mitch Williamsf7fc2f22015-07-23 16:54:36 -04002103 u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002104 struct i40e_netdev_priv *np = netdev_priv(netdev);
2105 struct i40e_pf *pf = np->vsi->back;
2106 struct i40e_vsi *vsi;
2107 struct i40e_vf *vf;
2108 int ret = 0;
2109
2110 /* validate the request */
2111 if (vf_id >= pf->num_alloc_vfs) {
2112 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2113 ret = -EINVAL;
2114 goto error_pvid;
2115 }
2116
2117 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2118 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2119 ret = -EINVAL;
2120 goto error_pvid;
2121 }
2122
2123 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002124 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002125 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2126 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2127 ret = -EINVAL;
2128 goto error_pvid;
2129 }
2130
Mitch Williamsf7fc2f22015-07-23 16:54:36 -04002131 if (le16_to_cpu(vsi->info.pvid) == vlanprio)
Mitch Williams85927ec2015-04-27 14:57:10 -04002132 /* duplicate request, so just return success */
2133 goto error_pvid;
2134
Mitch Williamsecbb44e2015-07-10 19:35:56 -04002135 if (le16_to_cpu(vsi->info.pvid) == 0 && i40e_is_vsi_in_vlan(vsi)) {
Greg Rose99a49732014-01-13 16:13:03 -08002136 dev_err(&pf->pdev->dev,
2137 "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",
2138 vf_id);
Greg Rosef9b4b622014-03-06 09:02:28 +00002139 /* Administrator Error - knock the VF offline until he does
2140 * the right thing by reconfiguring his network correctly
2141 * and then reloading the VF driver.
2142 */
2143 i40e_vc_disable_vf(pf, vf);
2144 }
Greg Rose99a49732014-01-13 16:13:03 -08002145
Greg Rose8d82a7c2014-01-13 16:13:04 -08002146 /* Check for condition where there was already a port VLAN ID
2147 * filter set and now it is being deleted by setting it to zero.
Greg Rose1315f7c2014-03-14 07:32:20 +00002148 * Additionally check for the condition where there was a port
2149 * VLAN but now there is a new and different port VLAN being set.
Greg Rose8d82a7c2014-01-13 16:13:04 -08002150 * Before deleting all the old VLAN filters we must add new ones
2151 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2152 * MAC addresses deleted.
2153 */
Greg Rose1315f7c2014-03-14 07:32:20 +00002154 if ((!(vlan_id || qos) ||
Mitch Williamsf7fc2f22015-07-23 16:54:36 -04002155 vlanprio != le16_to_cpu(vsi->info.pvid)) &&
Greg Rose1315f7c2014-03-14 07:32:20 +00002156 vsi->info.pvid)
Greg Rose8d82a7c2014-01-13 16:13:04 -08002157 ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2158
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002159 if (vsi->info.pvid) {
2160 /* kill old VLAN */
2161 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2162 VLAN_VID_MASK));
2163 if (ret) {
2164 dev_info(&vsi->back->pdev->dev,
2165 "remove VLAN failed, ret=%d, aq_err=%d\n",
2166 ret, pf->hw.aq.asq_last_status);
2167 }
2168 }
2169 if (vlan_id || qos)
Mitch Williamsf7fc2f22015-07-23 16:54:36 -04002170 ret = i40e_vsi_add_pvid(vsi, vlanprio);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002171 else
Greg Rose6c12fcb2013-11-28 06:39:34 +00002172 i40e_vsi_remove_pvid(vsi);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002173
2174 if (vlan_id) {
2175 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2176 vlan_id, qos, vf_id);
2177
2178 /* add new VLAN filter */
2179 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2180 if (ret) {
2181 dev_info(&vsi->back->pdev->dev,
2182 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2183 vsi->back->hw.aq.asq_last_status);
2184 goto error_pvid;
2185 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002186 /* Kill non-vlan MAC filters - ignore error return since
2187 * there might not be any non-vlan MAC filters.
2188 */
2189 i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002190 }
2191
2192 if (ret) {
2193 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2194 goto error_pvid;
2195 }
Greg Rose6c12fcb2013-11-28 06:39:34 +00002196 /* The Port VLAN needs to be saved across resets the same as the
2197 * default LAN MAC address.
2198 */
2199 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002200 ret = 0;
2201
2202error_pvid:
2203 return ret;
2204}
2205
Mitch Williams84590fd2014-04-09 05:58:57 +00002206#define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
2207#define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002208/**
2209 * i40e_ndo_set_vf_bw
2210 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002211 * @vf_id: VF identifier
2212 * @tx_rate: Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002213 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002214 * configure VF Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002215 **/
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002216int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2217 int max_tx_rate)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002218{
Mitch Williams6b192892014-03-06 09:02:29 +00002219 struct i40e_netdev_priv *np = netdev_priv(netdev);
2220 struct i40e_pf *pf = np->vsi->back;
2221 struct i40e_vsi *vsi;
2222 struct i40e_vf *vf;
2223 int speed = 0;
2224 int ret = 0;
2225
2226 /* validate the request */
2227 if (vf_id >= pf->num_alloc_vfs) {
2228 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2229 ret = -EINVAL;
2230 goto error;
2231 }
2232
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002233 if (min_tx_rate) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002234 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 -04002235 min_tx_rate, vf_id);
2236 return -EINVAL;
2237 }
2238
Mitch Williams6b192892014-03-06 09:02:29 +00002239 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002240 vsi = pf->vsi[vf->lan_vsi_idx];
Mitch Williams6b192892014-03-06 09:02:29 +00002241 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2242 dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
2243 ret = -EINVAL;
2244 goto error;
2245 }
2246
2247 switch (pf->hw.phy.link_info.link_speed) {
2248 case I40E_LINK_SPEED_40GB:
2249 speed = 40000;
2250 break;
2251 case I40E_LINK_SPEED_10GB:
2252 speed = 10000;
2253 break;
2254 case I40E_LINK_SPEED_1GB:
2255 speed = 1000;
2256 break;
2257 default:
2258 break;
2259 }
2260
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002261 if (max_tx_rate > speed) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002262 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002263 max_tx_rate, vf->vf_id);
Mitch Williams6b192892014-03-06 09:02:29 +00002264 ret = -EINVAL;
2265 goto error;
2266 }
2267
Mitch Williamsdac9b312014-04-09 05:58:56 +00002268 if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2269 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2270 max_tx_rate = 50;
2271 }
2272
Mitch Williams6b192892014-03-06 09:02:29 +00002273 /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
Mitch Williams84590fd2014-04-09 05:58:57 +00002274 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2275 max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2276 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
Mitch Williams6b192892014-03-06 09:02:29 +00002277 if (ret) {
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002278 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
Mitch Williams6b192892014-03-06 09:02:29 +00002279 ret);
2280 ret = -EIO;
2281 goto error;
2282 }
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002283 vf->tx_rate = max_tx_rate;
Mitch Williams6b192892014-03-06 09:02:29 +00002284error:
2285 return ret;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002286}
2287
2288/**
2289 * i40e_ndo_get_vf_config
2290 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002291 * @vf_id: VF identifier
2292 * @ivi: VF configuration structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002293 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002294 * return VF configuration
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002295 **/
2296int i40e_ndo_get_vf_config(struct net_device *netdev,
2297 int vf_id, struct ifla_vf_info *ivi)
2298{
2299 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002300 struct i40e_vsi *vsi = np->vsi;
2301 struct i40e_pf *pf = vsi->back;
2302 struct i40e_vf *vf;
2303 int ret = 0;
2304
2305 /* validate the request */
2306 if (vf_id >= pf->num_alloc_vfs) {
2307 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2308 ret = -EINVAL;
2309 goto error_param;
2310 }
2311
2312 vf = &(pf->vf[vf_id]);
2313 /* first vsi is always the LAN vsi */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002314 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002315 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2316 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2317 ret = -EINVAL;
2318 goto error_param;
2319 }
2320
2321 ivi->vf = vf_id;
2322
Jesse Brandeburg6995b362015-08-28 17:55:54 -04002323 ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002324
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002325 ivi->max_tx_rate = vf->tx_rate;
2326 ivi->min_tx_rate = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002327 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2328 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2329 I40E_VLAN_PRIORITY_SHIFT;
Mitch Williams84ca55a2014-03-14 07:32:24 +00002330 if (vf->link_forced == false)
2331 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2332 else if (vf->link_up == true)
2333 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2334 else
2335 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
Mitch Williamsc674d122014-05-20 08:01:40 +00002336 ivi->spoofchk = vf->spoofchk;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002337 ret = 0;
2338
2339error_param:
2340 return ret;
2341}
Mitch Williams588aefa2014-02-11 08:27:49 +00002342
2343/**
2344 * i40e_ndo_set_vf_link_state
2345 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002346 * @vf_id: VF identifier
Mitch Williams588aefa2014-02-11 08:27:49 +00002347 * @link: required link state
2348 *
2349 * Set the link state of a specified VF, regardless of physical link state
2350 **/
2351int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
2352{
2353 struct i40e_netdev_priv *np = netdev_priv(netdev);
2354 struct i40e_pf *pf = np->vsi->back;
2355 struct i40e_virtchnl_pf_event pfe;
2356 struct i40e_hw *hw = &pf->hw;
2357 struct i40e_vf *vf;
Ashish Shahf19efbb2014-08-01 13:27:06 -07002358 int abs_vf_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002359 int ret = 0;
2360
2361 /* validate the request */
2362 if (vf_id >= pf->num_alloc_vfs) {
2363 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2364 ret = -EINVAL;
2365 goto error_out;
2366 }
2367
2368 vf = &pf->vf[vf_id];
Ashish Shahf19efbb2014-08-01 13:27:06 -07002369 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002370
2371 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
2372 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
2373
2374 switch (link) {
2375 case IFLA_VF_LINK_STATE_AUTO:
2376 vf->link_forced = false;
2377 pfe.event_data.link_event.link_status =
2378 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
2379 pfe.event_data.link_event.link_speed =
2380 pf->hw.phy.link_info.link_speed;
2381 break;
2382 case IFLA_VF_LINK_STATE_ENABLE:
2383 vf->link_forced = true;
2384 vf->link_up = true;
2385 pfe.event_data.link_event.link_status = true;
2386 pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
2387 break;
2388 case IFLA_VF_LINK_STATE_DISABLE:
2389 vf->link_forced = true;
2390 vf->link_up = false;
2391 pfe.event_data.link_event.link_status = false;
2392 pfe.event_data.link_event.link_speed = 0;
2393 break;
2394 default:
2395 ret = -EINVAL;
2396 goto error_out;
2397 }
2398 /* Notify the VF of its new link state */
Ashish Shahf19efbb2014-08-01 13:27:06 -07002399 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Mitch Williams588aefa2014-02-11 08:27:49 +00002400 0, (u8 *)&pfe, sizeof(pfe), NULL);
2401
2402error_out:
2403 return ret;
2404}
Mitch Williamsc674d122014-05-20 08:01:40 +00002405
2406/**
2407 * i40e_ndo_set_vf_spoofchk
2408 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002409 * @vf_id: VF identifier
Mitch Williamsc674d122014-05-20 08:01:40 +00002410 * @enable: flag to enable or disable feature
2411 *
2412 * Enable or disable VF spoof checking
2413 **/
Serey Konge6d90042014-07-12 07:28:14 +00002414int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
Mitch Williamsc674d122014-05-20 08:01:40 +00002415{
2416 struct i40e_netdev_priv *np = netdev_priv(netdev);
2417 struct i40e_vsi *vsi = np->vsi;
2418 struct i40e_pf *pf = vsi->back;
2419 struct i40e_vsi_context ctxt;
2420 struct i40e_hw *hw = &pf->hw;
2421 struct i40e_vf *vf;
2422 int ret = 0;
2423
2424 /* validate the request */
2425 if (vf_id >= pf->num_alloc_vfs) {
2426 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2427 ret = -EINVAL;
2428 goto out;
2429 }
2430
2431 vf = &(pf->vf[vf_id]);
2432
2433 if (enable == vf->spoofchk)
2434 goto out;
2435
2436 vf->spoofchk = enable;
2437 memset(&ctxt, 0, sizeof(ctxt));
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002438 ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
Mitch Williamsc674d122014-05-20 08:01:40 +00002439 ctxt.pf_num = pf->hw.pf_id;
2440 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
2441 if (enable)
Greg Rose30d71af2015-01-29 07:17:17 +00002442 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
2443 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
Mitch Williamsc674d122014-05-20 08:01:40 +00002444 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
2445 if (ret) {
2446 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
2447 ret);
2448 ret = -EIO;
2449 }
2450out:
2451 return ret;
2452}