blob: 51aff707219568e231700290f2a607c29e8f4696 [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 Brandeburg5c3c48a2013-09-11 08:40:07 +0000280 linklistmap |= (1 <<
281 (I40E_VIRTCHNL_SUPPORTED_QTYPES *
282 vsi_queue_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000283 }
284
285 tempmap = vecmap->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000286 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000287 linklistmap |= (1 <<
288 (I40E_VIRTCHNL_SUPPORTED_QTYPES * vsi_queue_id
289 + 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000290 }
291
292 next_q = find_first_bit(&linklistmap,
293 (I40E_MAX_VSI_QP *
294 I40E_VIRTCHNL_SUPPORTED_QTYPES));
295 vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
296 qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700297 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000298 reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
299
300 wr32(hw, reg_idx, reg);
301
302 while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
303 switch (qtype) {
304 case I40E_QUEUE_TYPE_RX:
305 reg_idx = I40E_QINT_RQCTL(pf_queue_id);
306 itr_idx = vecmap->rxitr_idx;
307 break;
308 case I40E_QUEUE_TYPE_TX:
309 reg_idx = I40E_QINT_TQCTL(pf_queue_id);
310 itr_idx = vecmap->txitr_idx;
311 break;
312 default:
313 break;
314 }
315
316 next_q = find_next_bit(&linklistmap,
317 (I40E_MAX_VSI_QP *
318 I40E_VIRTCHNL_SUPPORTED_QTYPES),
319 next_q + 1);
Mitch Williams829af3a2013-12-18 13:46:00 +0000320 if (next_q <
321 (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000322 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
323 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700324 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000325 vsi_queue_id);
326 } else {
327 pf_queue_id = I40E_QUEUE_END_OF_LIST;
328 qtype = 0;
329 }
330
331 /* format for the RQCTL & TQCTL regs is same */
332 reg = (vector_id) |
333 (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
334 (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
335 (1 << I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
336 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
337 wr32(hw, reg_idx, reg);
338 }
339
340irq_list_done:
341 i40e_flush(hw);
342}
343
344/**
345 * i40e_config_vsi_tx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000346 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700347 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000348 * @vsi_queue_id: vsi relative queue index
349 * @info: config. info
350 *
351 * configure tx queue
352 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700353static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000354 u16 vsi_queue_id,
355 struct i40e_virtchnl_txq_info *info)
356{
357 struct i40e_pf *pf = vf->pf;
358 struct i40e_hw *hw = &pf->hw;
359 struct i40e_hmc_obj_txq tx_ctx;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700360 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000361 u16 pf_queue_id;
362 u32 qtx_ctl;
363 int ret = 0;
364
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700365 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
366 vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000367
368 /* clear the context structure first */
369 memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
370
371 /* only set the required fields */
372 tx_ctx.base = info->dma_ring_addr / 128;
373 tx_ctx.qlen = info->ring_len;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700374 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000375 tx_ctx.rdylist_act = 0;
Ashish Shah5d298962014-05-22 06:31:25 +0000376 tx_ctx.head_wb_ena = info->headwb_enabled;
377 tx_ctx.head_wb_addr = info->dma_headwb_addr;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000378
379 /* clear the context in the HMC */
380 ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
381 if (ret) {
382 dev_err(&pf->pdev->dev,
383 "Failed to clear VF LAN Tx queue context %d, error: %d\n",
384 pf_queue_id, ret);
385 ret = -ENOENT;
386 goto error_context;
387 }
388
389 /* set the context in the HMC */
390 ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
391 if (ret) {
392 dev_err(&pf->pdev->dev,
393 "Failed to set VF LAN Tx queue context %d error: %d\n",
394 pf_queue_id, ret);
395 ret = -ENOENT;
396 goto error_context;
397 }
398
399 /* associate this queue with the PCI VF function */
400 qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +0000401 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000402 & I40E_QTX_CTL_PF_INDX_MASK);
403 qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
404 << I40E_QTX_CTL_VFVM_INDX_SHIFT)
405 & I40E_QTX_CTL_VFVM_INDX_MASK);
406 wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
407 i40e_flush(hw);
408
409error_context:
410 return ret;
411}
412
413/**
414 * i40e_config_vsi_rx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000415 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700416 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000417 * @vsi_queue_id: vsi relative queue index
418 * @info: config. info
419 *
420 * configure rx queue
421 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700422static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000423 u16 vsi_queue_id,
424 struct i40e_virtchnl_rxq_info *info)
425{
426 struct i40e_pf *pf = vf->pf;
427 struct i40e_hw *hw = &pf->hw;
428 struct i40e_hmc_obj_rxq rx_ctx;
429 u16 pf_queue_id;
430 int ret = 0;
431
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700432 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000433
434 /* clear the context structure first */
435 memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
436
437 /* only set the required fields */
438 rx_ctx.base = info->dma_ring_addr / 128;
439 rx_ctx.qlen = info->ring_len;
440
441 if (info->splithdr_enabled) {
442 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
443 I40E_RX_SPLIT_IP |
444 I40E_RX_SPLIT_TCP_UDP |
445 I40E_RX_SPLIT_SCTP;
446 /* header length validation */
447 if (info->hdr_size > ((2 * 1024) - 64)) {
448 ret = -EINVAL;
449 goto error_param;
450 }
451 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
452
453 /* set splitalways mode 10b */
454 rx_ctx.dtype = 0x2;
455 }
456
457 /* databuffer length validation */
458 if (info->databuffer_size > ((16 * 1024) - 128)) {
459 ret = -EINVAL;
460 goto error_param;
461 }
462 rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
463
464 /* max pkt. length validation */
465 if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
466 ret = -EINVAL;
467 goto error_param;
468 }
469 rx_ctx.rxmax = info->max_pkt_size;
470
471 /* enable 32bytes desc always */
472 rx_ctx.dsize = 1;
473
474 /* default values */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000475 rx_ctx.lrxqthresh = 2;
476 rx_ctx.crcstrip = 1;
Mitch Williams50d41652014-04-09 05:58:55 +0000477 rx_ctx.prefena = 1;
Shannon Nelsonc1d11ce2014-07-29 04:01:03 +0000478 rx_ctx.l2tsel = 1;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000479
480 /* clear the context in the HMC */
481 ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
482 if (ret) {
483 dev_err(&pf->pdev->dev,
484 "Failed to clear VF LAN Rx queue context %d, error: %d\n",
485 pf_queue_id, ret);
486 ret = -ENOENT;
487 goto error_param;
488 }
489
490 /* set the context in the HMC */
491 ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
492 if (ret) {
493 dev_err(&pf->pdev->dev,
494 "Failed to set VF LAN Rx queue context %d error: %d\n",
495 pf_queue_id, ret);
496 ret = -ENOENT;
497 goto error_param;
498 }
499
500error_param:
501 return ret;
502}
503
504/**
505 * i40e_alloc_vsi_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000506 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000507 * @type: type of VSI to allocate
508 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000509 * alloc VF vsi context & resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000510 **/
511static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
512{
513 struct i40e_mac_filter *f = NULL;
514 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000515 struct i40e_vsi *vsi;
516 int ret = 0;
517
518 vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
519
520 if (!vsi) {
521 dev_err(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000522 "add vsi failed for VF %d, aq_err %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000523 vf->vf_id, pf->hw.aq.asq_last_status);
524 ret = -ENOENT;
525 goto error_alloc_vsi_res;
526 }
527 if (type == I40E_VSI_SRIOV) {
Greg Rose1a103702013-11-28 06:42:39 +0000528 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700529 vf->lan_vsi_idx = vsi->idx;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000530 vf->lan_vsi_id = vsi->id;
Greg Rose6c12fcb2013-11-28 06:39:34 +0000531 /* If the port VLAN has been configured and then the
532 * VF driver was removed then the VSI port VLAN
533 * configuration was destroyed. Check if there is
534 * a port VLAN and restore the VSI configuration if
535 * needed.
536 */
537 if (vf->port_vlan_id)
538 i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000539 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
Mitch Williamse9951632015-04-27 14:57:13 -0400540 vf->port_vlan_id ? vf->port_vlan_id : -1,
541 true, false);
Greg Rose1a103702013-11-28 06:42:39 +0000542 if (!f)
543 dev_info(&pf->pdev->dev,
544 "Could not allocate VF MAC addr\n");
Mitch Williamse9951632015-04-27 14:57:13 -0400545 f = i40e_add_filter(vsi, brdcast,
546 vf->port_vlan_id ? vf->port_vlan_id : -1,
Greg Rose1a103702013-11-28 06:42:39 +0000547 true, false);
548 if (!f)
549 dev_info(&pf->pdev->dev,
550 "Could not allocate VF broadcast filter\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000551 }
Neerav Parikh6dbbbfb2013-11-26 10:49:24 +0000552
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000553 /* program mac filter */
554 ret = i40e_sync_vsi_filters(vsi);
Mitch Williamsfd1646e2014-02-13 03:48:44 -0800555 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000556 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000557
Mitch Williams6b192892014-03-06 09:02:29 +0000558 /* Set VF bandwidth if specified */
559 if (vf->tx_rate) {
560 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
561 vf->tx_rate / 50, 0, NULL);
562 if (ret)
563 dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
564 vf->vf_id, ret);
565 }
566
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000567error_alloc_vsi_res:
568 return ret;
569}
570
571/**
Mitch Williams805bd5b2013-11-28 06:39:26 +0000572 * i40e_enable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000573 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000574 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000575 * enable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000576 **/
577static void i40e_enable_vf_mappings(struct i40e_vf *vf)
578{
579 struct i40e_pf *pf = vf->pf;
580 struct i40e_hw *hw = &pf->hw;
581 u32 reg, total_queue_pairs = 0;
582 int j;
583
584 /* Tell the hardware we're using noncontiguous mapping. HW requires
585 * that VF queues be mapped using this method, even when they are
586 * contiguous in real life
587 */
588 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
589 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
590
591 /* enable VF vplan_qtable mappings */
592 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
593 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
594
595 /* map PF queues to VF queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700596 for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
597 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
Mitch Williams805bd5b2013-11-28 06:39:26 +0000598 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
599 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
600 total_queue_pairs++;
601 }
602
603 /* map PF queues to VSI */
604 for (j = 0; j < 7; j++) {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700605 if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
Mitch Williams805bd5b2013-11-28 06:39:26 +0000606 reg = 0x07FF07FF; /* unused */
607 } else {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700608 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000609 j * 2);
610 reg = qid;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700611 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000612 (j * 2) + 1);
613 reg |= qid << 16;
614 }
615 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
616 }
617
618 i40e_flush(hw);
619}
620
621/**
622 * i40e_disable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000623 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000624 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000625 * disable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000626 **/
627static void i40e_disable_vf_mappings(struct i40e_vf *vf)
628{
629 struct i40e_pf *pf = vf->pf;
630 struct i40e_hw *hw = &pf->hw;
631 int i;
632
633 /* disable qp mappings */
634 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
635 for (i = 0; i < I40E_MAX_VSI_QP; i++)
636 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
637 I40E_QUEUE_END_OF_LIST);
638 i40e_flush(hw);
639}
640
641/**
642 * i40e_free_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000643 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000644 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000645 * free VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000646 **/
647static void i40e_free_vf_res(struct i40e_vf *vf)
648{
649 struct i40e_pf *pf = vf->pf;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000650 struct i40e_hw *hw = &pf->hw;
651 u32 reg_idx, reg;
652 int i, msix_vf;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000653
654 /* free vsi & disconnect it from the parent uplink */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700655 if (vf->lan_vsi_idx) {
656 i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
657 vf->lan_vsi_idx = 0;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000658 vf->lan_vsi_id = 0;
659 }
Mitch Williams9347eb72014-02-11 08:26:32 +0000660 msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
661
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000662 /* disable interrupts so the VF starts in a known state */
663 for (i = 0; i < msix_vf; i++) {
664 /* format is same for both registers */
665 if (0 == i)
666 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
667 else
668 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
669 (vf->vf_id))
670 + (i - 1));
671 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
672 i40e_flush(hw);
673 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000674
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000675 /* clear the irq settings */
676 for (i = 0; i < msix_vf; i++) {
677 /* format is same for both registers */
678 if (0 == i)
679 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
680 else
681 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
682 (vf->vf_id))
683 + (i - 1));
684 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
685 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
686 wr32(hw, reg_idx, reg);
687 i40e_flush(hw);
688 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000689 /* reset some of the state varibles keeping
690 * track of the resources
691 */
692 vf->num_queue_pairs = 0;
693 vf->vf_states = 0;
694}
695
696/**
697 * i40e_alloc_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000698 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000699 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000700 * allocate VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000701 **/
702static int i40e_alloc_vf_res(struct i40e_vf *vf)
703{
704 struct i40e_pf *pf = vf->pf;
705 int total_queue_pairs = 0;
706 int ret;
707
708 /* allocate hw vsi context & associated resources */
709 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
710 if (ret)
711 goto error_alloc;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700712 total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000713 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
714
715 /* store the total qps number for the runtime
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000716 * VF req validation
Mitch Williams805bd5b2013-11-28 06:39:26 +0000717 */
718 vf->num_queue_pairs = total_queue_pairs;
719
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000720 /* VF is now completely initialized */
Mitch Williams805bd5b2013-11-28 06:39:26 +0000721 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
722
723error_alloc:
724 if (ret)
725 i40e_free_vf_res(vf);
726
727 return ret;
728}
729
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000730#define VF_DEVICE_STATUS 0xAA
731#define VF_TRANS_PENDING_MASK 0x20
732/**
733 * i40e_quiesce_vf_pci
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000734 * @vf: pointer to the VF structure
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000735 *
736 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
737 * if the transactions never clear.
738 **/
739static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
740{
741 struct i40e_pf *pf = vf->pf;
742 struct i40e_hw *hw = &pf->hw;
743 int vf_abs_id, i;
744 u32 reg;
745
Mitch Williamsb141d612013-11-28 06:39:36 +0000746 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000747
748 wr32(hw, I40E_PF_PCI_CIAA,
749 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
750 for (i = 0; i < 100; i++) {
751 reg = rd32(hw, I40E_PF_PCI_CIAD);
752 if ((reg & VF_TRANS_PENDING_MASK) == 0)
753 return 0;
754 udelay(1);
755 }
756 return -EIO;
757}
758
Mitch Williams805bd5b2013-11-28 06:39:26 +0000759/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000760 * i40e_reset_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000761 * @vf: pointer to the VF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000762 * @flr: VFLR was issued or not
763 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000764 * reset the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000765 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000766void i40e_reset_vf(struct i40e_vf *vf, bool flr)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000767{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000768 struct i40e_pf *pf = vf->pf;
769 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000770 bool rsd = false;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000771 int i;
772 u32 reg;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000773
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000774 if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
775 return;
776
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000777 /* warn the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000778 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
779
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000780 /* In the case of a VFLR, the HW has already reset the VF and we
781 * just need to clean up, so don't hit the VFRTRIG register.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000782 */
783 if (!flr) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000784 /* reset VF using VPGEN_VFRTRIG reg */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000785 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
786 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000787 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
788 i40e_flush(hw);
789 }
790
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000791 if (i40e_quiesce_vf_pci(vf))
792 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
793 vf->vf_id);
794
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000795 /* poll VPGEN_VFRSTAT reg to make sure
796 * that reset is complete
797 */
Mitch Williams1750a222015-01-09 11:18:13 +0000798 for (i = 0; i < 10; i++) {
799 /* VF reset requires driver to first reset the VF and then
800 * poll the status register to make sure that the reset
801 * completed successfully. Due to internal HW FIFO flushes,
802 * we must wait 10ms before the register will be valid.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000803 */
Mitch Williams1750a222015-01-09 11:18:13 +0000804 usleep_range(10000, 20000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000805 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
806 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
807 rsd = true;
808 break;
809 }
810 }
811
Anjali Singhai Jain57175ac2015-04-07 19:45:35 -0400812 if (flr)
813 usleep_range(10000, 20000);
814
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000815 if (!rsd)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000816 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000817 vf->vf_id);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000818 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000819 /* clear the reset bit in the VPGEN_VFRTRIG reg */
820 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
821 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
822 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000823
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000824 /* On initial reset, we won't have any queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700825 if (vf->lan_vsi_idx == 0)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000826 goto complete_reset;
827
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700828 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000829complete_reset:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000830 /* reallocate VF resources to reset the VSI state */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000831 i40e_free_vf_res(vf);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000832 i40e_alloc_vf_res(vf);
833 i40e_enable_vf_mappings(vf);
Mitch Williamsc17b3622014-02-13 03:48:45 -0800834 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
Mitch Williams5b8f8502015-04-27 14:57:15 -0400835 clear_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000836
837 /* tell the VF the reset is done */
838 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
839 i40e_flush(hw);
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000840 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000841}
Greg Rosec3542292013-12-13 08:38:38 +0000842
843/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000844 * i40e_free_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000845 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000846 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000847 * free VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000848 **/
849void i40e_free_vfs(struct i40e_pf *pf)
850{
Mitch Williamsf7414532013-11-28 06:39:40 +0000851 struct i40e_hw *hw = &pf->hw;
852 u32 reg_idx, bit_idx;
853 int i, tmp, vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000854
855 if (!pf->vf)
856 return;
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000857 while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
858 usleep_range(1000, 2000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000859
Mitch Williams44434632015-04-07 11:32:55 -0700860 for (i = 0; i < pf->num_alloc_vfs; i++)
861 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
862 i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
863 false);
864
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000865 /* Disable IOV before freeing resources. This lets any VF drivers
866 * running in the host get themselves cleaned up before we yank
867 * the carpet out from underneath their feet.
868 */
869 if (!pci_vfs_assigned(pf->pdev))
870 pci_disable_sriov(pf->pdev);
Mitch Williams6d7b9672015-03-31 00:45:02 -0700871 else
872 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000873
874 msleep(20); /* let any messages in transit get finished up */
875
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000876 /* free up VF resources */
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000877 tmp = pf->num_alloc_vfs;
878 pf->num_alloc_vfs = 0;
879 for (i = 0; i < tmp; i++) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000880 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
881 i40e_free_vf_res(&pf->vf[i]);
882 /* disable qp mappings */
883 i40e_disable_vf_mappings(&pf->vf[i]);
884 }
885
886 kfree(pf->vf);
887 pf->vf = NULL;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000888
Mitch Williams9e5634d2014-04-04 04:43:14 +0000889 /* This check is for when the driver is unloaded while VFs are
890 * assigned. Setting the number of VFs to 0 through sysfs is caught
891 * before this function ever gets called.
892 */
Ethan Zhaoc24817b2014-07-22 18:36:43 +0000893 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williamsf7414532013-11-28 06:39:40 +0000894 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
895 * work correctly when SR-IOV gets re-enabled.
896 */
897 for (vf_id = 0; vf_id < tmp; vf_id++) {
898 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
899 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
900 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
901 }
Greg Rosec3542292013-12-13 08:38:38 +0000902 }
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000903 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000904}
905
906#ifdef CONFIG_PCI_IOV
907/**
908 * i40e_alloc_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000909 * @pf: pointer to the PF structure
910 * @num_alloc_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000911 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000912 * allocate VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000913 **/
Mitch Williams4aeec012014-02-13 03:48:47 -0800914int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000915{
916 struct i40e_vf *vfs;
917 int i, ret = 0;
918
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000919 /* Disable interrupt 0 so we don't try to handle the VFLR. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000920 i40e_irq_dynamic_disable_icr0(pf);
921
Mitch Williams4aeec012014-02-13 03:48:47 -0800922 /* Check to see if we're just allocating resources for extant VFs */
923 if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
924 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
925 if (ret) {
926 dev_err(&pf->pdev->dev,
927 "Failed to enable SR-IOV, error %d.\n", ret);
928 pf->num_alloc_vfs = 0;
929 goto err_iov;
930 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000931 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000932 /* allocate memory */
Akeem G Abodunrincc6456a2014-02-06 05:51:02 +0000933 vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000934 if (!vfs) {
935 ret = -ENOMEM;
936 goto err_alloc;
937 }
Mitch Williamsc674d122014-05-20 08:01:40 +0000938 pf->vf = vfs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000939
940 /* apply default profile */
941 for (i = 0; i < num_alloc_vfs; i++) {
942 vfs[i].pf = pf;
943 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
944 vfs[i].vf_id = i;
945
946 /* assign default capabilities */
947 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
Mitch Williamsc674d122014-05-20 08:01:40 +0000948 vfs[i].spoofchk = true;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000949 /* VF resources get allocated during reset */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000950 i40e_reset_vf(&vfs[i], false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000951
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000952 /* enable VF vplan_qtable mappings */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000953 i40e_enable_vf_mappings(&vfs[i]);
954 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000955 pf->num_alloc_vfs = num_alloc_vfs;
956
957err_alloc:
958 if (ret)
959 i40e_free_vfs(pf);
960err_iov:
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000961 /* Re-enable interrupt 0. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000962 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000963 return ret;
964}
965
966#endif
967/**
968 * i40e_pci_sriov_enable
969 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000970 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000971 *
972 * Enable or change the number of VFs
973 **/
974static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
975{
976#ifdef CONFIG_PCI_IOV
977 struct i40e_pf *pf = pci_get_drvdata(pdev);
978 int pre_existing_vfs = pci_num_vf(pdev);
979 int err = 0;
980
Greg Rosee17bc412015-04-16 20:05:59 -0400981 if (pf->state & __I40E_TESTING) {
982 dev_warn(&pdev->dev,
983 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
984 err = -EPERM;
985 goto err_out;
986 }
987
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000988 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
989 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
990 i40e_free_vfs(pf);
991 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
992 goto out;
993
994 if (num_vfs > pf->num_req_vfs) {
995 err = -EPERM;
996 goto err_out;
997 }
998
999 err = i40e_alloc_vfs(pf, num_vfs);
1000 if (err) {
1001 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1002 goto err_out;
1003 }
1004
1005out:
1006 return num_vfs;
1007
1008err_out:
1009 return err;
1010#endif
1011 return 0;
1012}
1013
1014/**
1015 * i40e_pci_sriov_configure
1016 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001017 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001018 *
1019 * Enable or change the number of VFs. Called when the user updates the number
1020 * of VFs in sysfs.
1021 **/
1022int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1023{
1024 struct i40e_pf *pf = pci_get_drvdata(pdev);
1025
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07001026 if (num_vfs) {
1027 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1028 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1029 i40e_do_reset_safe(pf,
1030 BIT_ULL(__I40E_PF_RESET_REQUESTED));
1031 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001032 return i40e_pci_sriov_enable(pdev, num_vfs);
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07001033 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001034
Ethan Zhaoc24817b2014-07-22 18:36:43 +00001035 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williams9e5634d2014-04-04 04:43:14 +00001036 i40e_free_vfs(pf);
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07001037 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1038 i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Mitch Williams9e5634d2014-04-04 04:43:14 +00001039 } else {
1040 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1041 return -EINVAL;
1042 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001043 return 0;
1044}
1045
1046/***********************virtual channel routines******************/
1047
1048/**
1049 * i40e_vc_send_msg_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001050 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001051 * @v_opcode: virtual channel opcode
1052 * @v_retval: virtual channel return value
1053 * @msg: pointer to the msg buffer
1054 * @msglen: msg length
1055 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001056 * send msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001057 **/
1058static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1059 u32 v_retval, u8 *msg, u16 msglen)
1060{
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001061 struct i40e_pf *pf;
1062 struct i40e_hw *hw;
1063 int abs_vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001064 i40e_status aq_ret;
1065
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001066 /* validate the request */
1067 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1068 return -EINVAL;
1069
1070 pf = vf->pf;
1071 hw = &pf->hw;
1072 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1073
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001074 /* single place to detect unsuccessful return values */
1075 if (v_retval) {
1076 vf->num_invalid_msgs++;
1077 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
1078 v_opcode, v_retval);
1079 if (vf->num_invalid_msgs >
1080 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1081 dev_err(&pf->pdev->dev,
1082 "Number of invalid messages exceeded for VF %d\n",
1083 vf->vf_id);
1084 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1085 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1086 }
1087 } else {
1088 vf->num_valid_msgs++;
1089 }
1090
Ashish Shahf19efbb2014-08-01 13:27:06 -07001091 aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Mitch Williams7efa84b2013-11-28 06:39:41 +00001092 msg, msglen, NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001093 if (aq_ret) {
1094 dev_err(&pf->pdev->dev,
1095 "Unable to send the message to VF %d aq_err %d\n",
1096 vf->vf_id, pf->hw.aq.asq_last_status);
1097 return -EIO;
1098 }
1099
1100 return 0;
1101}
1102
1103/**
1104 * i40e_vc_send_resp_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001105 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001106 * @opcode: operation code
1107 * @retval: return value
1108 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001109 * send resp msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001110 **/
1111static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1112 enum i40e_virtchnl_ops opcode,
1113 i40e_status retval)
1114{
1115 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1116}
1117
1118/**
1119 * i40e_vc_get_version_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001120 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001121 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001122 * called from the VF to request the API version used by the PF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001123 **/
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001124static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001125{
1126 struct i40e_virtchnl_version_info info = {
1127 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
1128 };
1129
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001130 vf->vf_ver = *(struct i40e_virtchnl_version_info *)msg;
Mitch Williams606a5482015-06-04 16:24:00 -04001131 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1132 if (VF_IS_V10(vf))
1133 info.minor = I40E_VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001134 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
1135 I40E_SUCCESS, (u8 *)&info,
1136 sizeof(struct
1137 i40e_virtchnl_version_info));
1138}
1139
1140/**
1141 * i40e_vc_get_vf_resources_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001142 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001143 * @msg: pointer to the msg buffer
1144 * @msglen: msg length
1145 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001146 * called from the VF to request its resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001147 **/
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001148static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001149{
1150 struct i40e_virtchnl_vf_resource *vfres = NULL;
1151 struct i40e_pf *pf = vf->pf;
1152 i40e_status aq_ret = 0;
1153 struct i40e_vsi *vsi;
1154 int i = 0, len = 0;
1155 int num_vsis = 1;
1156 int ret;
1157
1158 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1159 aq_ret = I40E_ERR_PARAM;
1160 goto err;
1161 }
1162
1163 len = (sizeof(struct i40e_virtchnl_vf_resource) +
1164 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1165
1166 vfres = kzalloc(len, GFP_KERNEL);
1167 if (!vfres) {
1168 aq_ret = I40E_ERR_NO_MEMORY;
1169 len = 0;
1170 goto err;
1171 }
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001172 if (VF_IS_V11(vf))
1173 vf->driver_caps = *(u32 *)msg;
1174 else
1175 vf->driver_caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
1176 I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
1177 I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001178
1179 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001180 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001181 if (!vsi->info.pvid)
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001182 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN |
1183 I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001184
1185 vfres->num_vsis = num_vsis;
1186 vfres->num_queue_pairs = vf->num_queue_pairs;
1187 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001188 if (vf->lan_vsi_idx) {
1189 vfres->vsi_res[i].vsi_id = vf->lan_vsi_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001190 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1191 vfres->vsi_res[i].num_queue_pairs =
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001192 pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001193 memcpy(vfres->vsi_res[i].default_mac_addr,
1194 vf->default_lan_addr.addr, ETH_ALEN);
1195 i++;
1196 }
1197 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1198
1199err:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001200 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001201 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1202 aq_ret, (u8 *)vfres, len);
1203
1204 kfree(vfres);
1205 return ret;
1206}
1207
1208/**
1209 * i40e_vc_reset_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001210 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001211 * @msg: pointer to the msg buffer
1212 * @msglen: msg length
1213 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001214 * called from the VF to reset itself,
1215 * unlike other virtchnl messages, PF driver
1216 * doesn't send the response back to the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001217 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001218static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001219{
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001220 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1221 i40e_reset_vf(vf, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001222}
1223
1224/**
1225 * i40e_vc_config_promiscuous_mode_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001226 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001227 * @msg: pointer to the msg buffer
1228 * @msglen: msg length
1229 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001230 * called from the VF to configure the promiscuous mode of
1231 * VF vsis
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001232 **/
1233static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1234 u8 *msg, u16 msglen)
1235{
1236 struct i40e_virtchnl_promisc_info *info =
1237 (struct i40e_virtchnl_promisc_info *)msg;
1238 struct i40e_pf *pf = vf->pf;
1239 struct i40e_hw *hw = &pf->hw;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001240 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001241 bool allmulti = false;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001242 i40e_status aq_ret;
1243
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001244 vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001245 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1246 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1247 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001248 (vsi->type != I40E_VSI_FCOE)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001249 aq_ret = I40E_ERR_PARAM;
1250 goto error_param;
1251 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001252 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1253 allmulti = true;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001254 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001255 allmulti, NULL);
1256
1257error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001258 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001259 return i40e_vc_send_resp_to_vf(vf,
1260 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1261 aq_ret);
1262}
1263
1264/**
1265 * i40e_vc_config_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001266 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001267 * @msg: pointer to the msg buffer
1268 * @msglen: msg length
1269 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001270 * called from the VF to configure the rx/tx
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001271 * queues
1272 **/
1273static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1274{
1275 struct i40e_virtchnl_vsi_queue_config_info *qci =
1276 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1277 struct i40e_virtchnl_queue_pair_info *qpi;
Ashish Shah5f5e33b2014-07-10 07:58:15 +00001278 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001279 u16 vsi_id, vsi_queue_id;
1280 i40e_status aq_ret = 0;
1281 int i;
1282
1283 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1284 aq_ret = I40E_ERR_PARAM;
1285 goto error_param;
1286 }
1287
1288 vsi_id = qci->vsi_id;
1289 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1290 aq_ret = I40E_ERR_PARAM;
1291 goto error_param;
1292 }
1293 for (i = 0; i < qci->num_queue_pairs; i++) {
1294 qpi = &qci->qpair[i];
1295 vsi_queue_id = qpi->txq.queue_id;
1296 if ((qpi->txq.vsi_id != vsi_id) ||
1297 (qpi->rxq.vsi_id != vsi_id) ||
1298 (qpi->rxq.queue_id != vsi_queue_id) ||
1299 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1300 aq_ret = I40E_ERR_PARAM;
1301 goto error_param;
1302 }
1303
1304 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1305 &qpi->rxq) ||
1306 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1307 &qpi->txq)) {
1308 aq_ret = I40E_ERR_PARAM;
1309 goto error_param;
1310 }
1311 }
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001312 /* set vsi num_queue_pairs in use to num configured by VF */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001313 pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001314
1315error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001316 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001317 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1318 aq_ret);
1319}
1320
1321/**
1322 * i40e_vc_config_irq_map_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001323 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001324 * @msg: pointer to the msg buffer
1325 * @msglen: msg length
1326 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001327 * called from the VF to configure the irq to
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001328 * queue map
1329 **/
1330static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1331{
1332 struct i40e_virtchnl_irq_map_info *irqmap_info =
1333 (struct i40e_virtchnl_irq_map_info *)msg;
1334 struct i40e_virtchnl_vector_map *map;
1335 u16 vsi_id, vsi_queue_id, vector_id;
1336 i40e_status aq_ret = 0;
1337 unsigned long tempmap;
1338 int i;
1339
1340 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1341 aq_ret = I40E_ERR_PARAM;
1342 goto error_param;
1343 }
1344
1345 for (i = 0; i < irqmap_info->num_vectors; i++) {
1346 map = &irqmap_info->vecmap[i];
1347
1348 vector_id = map->vector_id;
1349 vsi_id = map->vsi_id;
1350 /* validate msg params */
1351 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1352 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1353 aq_ret = I40E_ERR_PARAM;
1354 goto error_param;
1355 }
1356
1357 /* lookout for the invalid queue index */
1358 tempmap = map->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001359 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001360 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1361 vsi_queue_id)) {
1362 aq_ret = I40E_ERR_PARAM;
1363 goto error_param;
1364 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001365 }
1366
1367 tempmap = map->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001368 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001369 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1370 vsi_queue_id)) {
1371 aq_ret = I40E_ERR_PARAM;
1372 goto error_param;
1373 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001374 }
1375
1376 i40e_config_irq_link_list(vf, vsi_id, map);
1377 }
1378error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001379 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001380 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1381 aq_ret);
1382}
1383
1384/**
1385 * i40e_vc_enable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001386 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001387 * @msg: pointer to the msg buffer
1388 * @msglen: msg length
1389 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001390 * called from the VF to enable all or specific queue(s)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001391 **/
1392static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1393{
1394 struct i40e_virtchnl_queue_select *vqs =
1395 (struct i40e_virtchnl_queue_select *)msg;
1396 struct i40e_pf *pf = vf->pf;
1397 u16 vsi_id = vqs->vsi_id;
1398 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001399
1400 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1401 aq_ret = I40E_ERR_PARAM;
1402 goto error_param;
1403 }
1404
1405 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1406 aq_ret = I40E_ERR_PARAM;
1407 goto error_param;
1408 }
1409
1410 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1411 aq_ret = I40E_ERR_PARAM;
1412 goto error_param;
1413 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001414
1415 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], true))
Mitch Williams88f65632013-11-28 06:39:28 +00001416 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001417error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001418 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001419 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1420 aq_ret);
1421}
1422
1423/**
1424 * i40e_vc_disable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001425 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001426 * @msg: pointer to the msg buffer
1427 * @msglen: msg length
1428 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001429 * called from the VF to disable all or specific
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001430 * queue(s)
1431 **/
1432static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1433{
1434 struct i40e_virtchnl_queue_select *vqs =
1435 (struct i40e_virtchnl_queue_select *)msg;
1436 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001437 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001438
1439 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1440 aq_ret = I40E_ERR_PARAM;
1441 goto error_param;
1442 }
1443
1444 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1445 aq_ret = I40E_ERR_PARAM;
1446 goto error_param;
1447 }
1448
1449 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1450 aq_ret = I40E_ERR_PARAM;
1451 goto error_param;
1452 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001453
1454 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false))
Mitch Williams88f65632013-11-28 06:39:28 +00001455 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001456
1457error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001458 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001459 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1460 aq_ret);
1461}
1462
1463/**
1464 * i40e_vc_get_stats_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001465 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001466 * @msg: pointer to the msg buffer
1467 * @msglen: msg length
1468 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001469 * called from the VF to get vsi stats
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001470 **/
1471static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1472{
1473 struct i40e_virtchnl_queue_select *vqs =
1474 (struct i40e_virtchnl_queue_select *)msg;
1475 struct i40e_pf *pf = vf->pf;
1476 struct i40e_eth_stats stats;
1477 i40e_status aq_ret = 0;
1478 struct i40e_vsi *vsi;
1479
1480 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1481
1482 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1483 aq_ret = I40E_ERR_PARAM;
1484 goto error_param;
1485 }
1486
1487 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1488 aq_ret = I40E_ERR_PARAM;
1489 goto error_param;
1490 }
1491
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001492 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001493 if (!vsi) {
1494 aq_ret = I40E_ERR_PARAM;
1495 goto error_param;
1496 }
1497 i40e_update_eth_stats(vsi);
Mitch Williams5a9769c2013-11-28 06:39:38 +00001498 stats = vsi->eth_stats;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001499
1500error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001501 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001502 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1503 (u8 *)&stats, sizeof(stats));
1504}
1505
1506/**
Greg Rosef657a6e2013-11-28 06:39:42 +00001507 * i40e_check_vf_permission
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001508 * @vf: pointer to the VF info
Greg Rosef657a6e2013-11-28 06:39:42 +00001509 * @macaddr: pointer to the MAC Address being checked
1510 *
1511 * Check if the VF has permission to add or delete unicast MAC address
1512 * filters and return error code -EPERM if not. Then check if the
1513 * address filter requested is broadcast or zero and if so return
1514 * an invalid MAC address error code.
1515 **/
1516static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1517{
1518 struct i40e_pf *pf = vf->pf;
1519 int ret = 0;
1520
1521 if (is_broadcast_ether_addr(macaddr) ||
1522 is_zero_ether_addr(macaddr)) {
1523 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1524 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rose5017c2a2013-12-07 10:36:54 +00001525 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1526 !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001527 /* If the host VMM administrator has set the VF MAC address
1528 * administratively via the ndo_set_vf_mac command then deny
1529 * permission to the VF to add or delete unicast MAC addresses.
Greg Rose5017c2a2013-12-07 10:36:54 +00001530 * The VF may request to set the MAC address filter already
1531 * assigned to it so do not return an error in that case.
Greg Rosef657a6e2013-11-28 06:39:42 +00001532 */
1533 dev_err(&pf->pdev->dev,
1534 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1535 ret = -EPERM;
1536 }
1537 return ret;
1538}
1539
1540/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001541 * i40e_vc_add_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001542 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001543 * @msg: pointer to the msg buffer
1544 * @msglen: msg length
1545 *
1546 * add guest mac address filter
1547 **/
1548static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1549{
1550 struct i40e_virtchnl_ether_addr_list *al =
1551 (struct i40e_virtchnl_ether_addr_list *)msg;
1552 struct i40e_pf *pf = vf->pf;
1553 struct i40e_vsi *vsi = NULL;
1554 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001555 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001556 int i;
1557
1558 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1559 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1560 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001561 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001562 goto error_param;
1563 }
1564
1565 for (i = 0; i < al->num_elements; i++) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001566 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1567 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001568 goto error_param;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001569 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001570 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001571
1572 /* add new addresses to the list */
1573 for (i = 0; i < al->num_elements; i++) {
1574 struct i40e_mac_filter *f;
1575
1576 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
Mitch Williams7e68edf92013-11-16 10:00:41 +00001577 if (!f) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001578 if (i40e_is_vsi_in_vlan(vsi))
1579 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1580 true, false);
1581 else
1582 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1583 true, false);
1584 }
1585
1586 if (!f) {
1587 dev_err(&pf->pdev->dev,
1588 "Unable to add VF MAC filter\n");
Greg Rosef657a6e2013-11-28 06:39:42 +00001589 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001590 goto error_param;
1591 }
1592 }
1593
1594 /* program the updated filter list */
1595 if (i40e_sync_vsi_filters(vsi))
1596 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1597
1598error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001599 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001600 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001601 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001602}
1603
1604/**
1605 * i40e_vc_del_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001606 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001607 * @msg: pointer to the msg buffer
1608 * @msglen: msg length
1609 *
1610 * remove guest mac address filter
1611 **/
1612static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1613{
1614 struct i40e_virtchnl_ether_addr_list *al =
1615 (struct i40e_virtchnl_ether_addr_list *)msg;
1616 struct i40e_pf *pf = vf->pf;
1617 struct i40e_vsi *vsi = NULL;
1618 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001619 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001620 int i;
1621
1622 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1623 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1624 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001625 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001626 goto error_param;
1627 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001628
1629 for (i = 0; i < al->num_elements; i++) {
Mitch Williams700bbf62013-12-21 05:44:45 +00001630 if (is_broadcast_ether_addr(al->list[i].addr) ||
1631 is_zero_ether_addr(al->list[i].addr)) {
1632 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
1633 al->list[i].addr);
1634 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rosef657a6e2013-11-28 06:39:42 +00001635 goto error_param;
Mitch Williams700bbf62013-12-21 05:44:45 +00001636 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001637 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001638 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001639
1640 /* delete addresses from the list */
1641 for (i = 0; i < al->num_elements; i++)
1642 i40e_del_filter(vsi, al->list[i].addr,
1643 I40E_VLAN_ANY, true, false);
1644
1645 /* program the updated filter list */
1646 if (i40e_sync_vsi_filters(vsi))
1647 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1648
1649error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001650 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001651 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001652 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001653}
1654
1655/**
1656 * i40e_vc_add_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001657 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001658 * @msg: pointer to the msg buffer
1659 * @msglen: msg length
1660 *
1661 * program guest vlan id
1662 **/
1663static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1664{
1665 struct i40e_virtchnl_vlan_filter_list *vfl =
1666 (struct i40e_virtchnl_vlan_filter_list *)msg;
1667 struct i40e_pf *pf = vf->pf;
1668 struct i40e_vsi *vsi = NULL;
1669 u16 vsi_id = vfl->vsi_id;
1670 i40e_status aq_ret = 0;
1671 int i;
1672
1673 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1674 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1675 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1676 aq_ret = I40E_ERR_PARAM;
1677 goto error_param;
1678 }
1679
1680 for (i = 0; i < vfl->num_elements; i++) {
1681 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1682 aq_ret = I40E_ERR_PARAM;
1683 dev_err(&pf->pdev->dev,
1684 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1685 goto error_param;
1686 }
1687 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001688 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001689 if (vsi->info.pvid) {
1690 aq_ret = I40E_ERR_PARAM;
1691 goto error_param;
1692 }
1693
1694 i40e_vlan_stripping_enable(vsi);
1695 for (i = 0; i < vfl->num_elements; i++) {
1696 /* add new VLAN filter */
1697 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1698 if (ret)
1699 dev_err(&pf->pdev->dev,
1700 "Unable to add VF vlan filter %d, error %d\n",
1701 vfl->vlan_id[i], ret);
1702 }
1703
1704error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001705 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001706 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1707}
1708
1709/**
1710 * i40e_vc_remove_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001711 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001712 * @msg: pointer to the msg buffer
1713 * @msglen: msg length
1714 *
1715 * remove programmed guest vlan id
1716 **/
1717static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1718{
1719 struct i40e_virtchnl_vlan_filter_list *vfl =
1720 (struct i40e_virtchnl_vlan_filter_list *)msg;
1721 struct i40e_pf *pf = vf->pf;
1722 struct i40e_vsi *vsi = NULL;
1723 u16 vsi_id = vfl->vsi_id;
1724 i40e_status aq_ret = 0;
1725 int i;
1726
1727 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1728 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1729 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1730 aq_ret = I40E_ERR_PARAM;
1731 goto error_param;
1732 }
1733
1734 for (i = 0; i < vfl->num_elements; i++) {
1735 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1736 aq_ret = I40E_ERR_PARAM;
1737 goto error_param;
1738 }
1739 }
1740
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001741 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001742 if (vsi->info.pvid) {
1743 aq_ret = I40E_ERR_PARAM;
1744 goto error_param;
1745 }
1746
1747 for (i = 0; i < vfl->num_elements; i++) {
1748 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1749 if (ret)
1750 dev_err(&pf->pdev->dev,
1751 "Unable to delete VF vlan filter %d, error %d\n",
1752 vfl->vlan_id[i], ret);
1753 }
1754
1755error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001756 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001757 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1758}
1759
1760/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001761 * i40e_vc_validate_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001762 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001763 * @msg: pointer to the msg buffer
1764 * @msglen: msg length
1765 * @msghndl: msg handle
1766 *
1767 * validate msg
1768 **/
1769static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1770 u32 v_retval, u8 *msg, u16 msglen)
1771{
1772 bool err_msg_format = false;
1773 int valid_len;
1774
1775 /* Check if VF is disabled. */
1776 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1777 return I40E_ERR_PARAM;
1778
1779 /* Validate message length. */
1780 switch (v_opcode) {
1781 case I40E_VIRTCHNL_OP_VERSION:
1782 valid_len = sizeof(struct i40e_virtchnl_version_info);
1783 break;
1784 case I40E_VIRTCHNL_OP_RESET_VF:
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001785 valid_len = 0;
1786 break;
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001787 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1788 if (VF_IS_V11(vf))
1789 valid_len = sizeof(u32);
1790 else
1791 valid_len = 0;
1792 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001793 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1794 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1795 break;
1796 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1797 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1798 break;
1799 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1800 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1801 if (msglen >= valid_len) {
1802 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1803 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1804 valid_len += (vqc->num_queue_pairs *
1805 sizeof(struct
1806 i40e_virtchnl_queue_pair_info));
1807 if (vqc->num_queue_pairs == 0)
1808 err_msg_format = true;
1809 }
1810 break;
1811 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1812 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1813 if (msglen >= valid_len) {
1814 struct i40e_virtchnl_irq_map_info *vimi =
1815 (struct i40e_virtchnl_irq_map_info *)msg;
1816 valid_len += (vimi->num_vectors *
1817 sizeof(struct i40e_virtchnl_vector_map));
1818 if (vimi->num_vectors == 0)
1819 err_msg_format = true;
1820 }
1821 break;
1822 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1823 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1824 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1825 break;
1826 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1827 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1828 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1829 if (msglen >= valid_len) {
1830 struct i40e_virtchnl_ether_addr_list *veal =
1831 (struct i40e_virtchnl_ether_addr_list *)msg;
1832 valid_len += veal->num_elements *
1833 sizeof(struct i40e_virtchnl_ether_addr);
1834 if (veal->num_elements == 0)
1835 err_msg_format = true;
1836 }
1837 break;
1838 case I40E_VIRTCHNL_OP_ADD_VLAN:
1839 case I40E_VIRTCHNL_OP_DEL_VLAN:
1840 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1841 if (msglen >= valid_len) {
1842 struct i40e_virtchnl_vlan_filter_list *vfl =
1843 (struct i40e_virtchnl_vlan_filter_list *)msg;
1844 valid_len += vfl->num_elements * sizeof(u16);
1845 if (vfl->num_elements == 0)
1846 err_msg_format = true;
1847 }
1848 break;
1849 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1850 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1851 break;
1852 case I40E_VIRTCHNL_OP_GET_STATS:
1853 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1854 break;
1855 /* These are always errors coming from the VF. */
1856 case I40E_VIRTCHNL_OP_EVENT:
1857 case I40E_VIRTCHNL_OP_UNKNOWN:
1858 default:
1859 return -EPERM;
1860 break;
1861 }
1862 /* few more checks */
1863 if ((valid_len != msglen) || (err_msg_format)) {
1864 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1865 return -EINVAL;
1866 } else {
1867 return 0;
1868 }
1869}
1870
1871/**
1872 * i40e_vc_process_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001873 * @pf: pointer to the PF structure
1874 * @vf_id: source VF id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001875 * @msg: pointer to the msg buffer
1876 * @msglen: msg length
1877 * @msghndl: msg handle
1878 *
1879 * called from the common aeq/arq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001880 * process request from VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001881 **/
1882int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1883 u32 v_retval, u8 *msg, u16 msglen)
1884{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001885 struct i40e_hw *hw = &pf->hw;
Dan Carpenterc243e962014-01-15 06:43:39 +00001886 unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001887 struct i40e_vf *vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001888 int ret;
1889
1890 pf->vf_aq_requests++;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001891 if (local_vf_id >= pf->num_alloc_vfs)
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001892 return -EINVAL;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001893 vf = &(pf->vf[local_vf_id]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001894 /* perform basic checks on the msg */
1895 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1896
1897 if (ret) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001898 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001899 local_vf_id, v_opcode, msglen);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001900 return ret;
1901 }
Mitch Williamsbae3cae2014-01-14 00:49:49 -08001902
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001903 switch (v_opcode) {
1904 case I40E_VIRTCHNL_OP_VERSION:
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001905 ret = i40e_vc_get_version_msg(vf, msg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001906 break;
1907 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001908 ret = i40e_vc_get_vf_resources_msg(vf, msg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001909 break;
1910 case I40E_VIRTCHNL_OP_RESET_VF:
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001911 i40e_vc_reset_vf_msg(vf);
1912 ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001913 break;
1914 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1915 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1916 break;
1917 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1918 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1919 break;
1920 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1921 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1922 break;
1923 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1924 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
Mitch Williams055b2952015-04-07 19:45:33 -04001925 i40e_vc_notify_vf_link_state(vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001926 break;
1927 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1928 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1929 break;
1930 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1931 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1932 break;
1933 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1934 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1935 break;
1936 case I40E_VIRTCHNL_OP_ADD_VLAN:
1937 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1938 break;
1939 case I40E_VIRTCHNL_OP_DEL_VLAN:
1940 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1941 break;
1942 case I40E_VIRTCHNL_OP_GET_STATS:
1943 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1944 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001945 case I40E_VIRTCHNL_OP_UNKNOWN:
1946 default:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001947 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001948 v_opcode, local_vf_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001949 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1950 I40E_ERR_NOT_IMPLEMENTED);
1951 break;
1952 }
1953
1954 return ret;
1955}
1956
1957/**
1958 * i40e_vc_process_vflr_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001959 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001960 *
1961 * called from the vlfr irq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001962 * free up VF resources and state variables
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001963 **/
1964int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1965{
1966 u32 reg, reg_idx, bit_idx, vf_id;
1967 struct i40e_hw *hw = &pf->hw;
1968 struct i40e_vf *vf;
1969
1970 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1971 return 0;
1972
Mitch Williamsc5c2f7c2014-11-11 03:15:04 +00001973 /* re-enable vflr interrupt cause */
1974 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1975 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1976 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1977 i40e_flush(hw);
1978
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001979 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1980 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1981 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1982 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001983 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001984 vf = &pf->vf[vf_id];
1985 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
1986 if (reg & (1 << bit_idx)) {
1987 /* clear the bit in GLGEN_VFLRSTAT */
1988 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
1989
Mitch Williamseb2d80b2014-02-13 03:48:48 -08001990 if (!test_bit(__I40E_DOWN, &pf->state))
1991 i40e_reset_vf(vf, true);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001992 }
1993 }
1994
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001995 return 0;
1996}
1997
1998/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001999 * i40e_ndo_set_vf_mac
2000 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002001 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002002 * @mac: mac address
2003 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002004 * program VF mac address
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002005 **/
2006int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2007{
2008 struct i40e_netdev_priv *np = netdev_priv(netdev);
2009 struct i40e_vsi *vsi = np->vsi;
2010 struct i40e_pf *pf = vsi->back;
2011 struct i40e_mac_filter *f;
2012 struct i40e_vf *vf;
2013 int ret = 0;
2014
2015 /* validate the request */
2016 if (vf_id >= pf->num_alloc_vfs) {
2017 dev_err(&pf->pdev->dev,
2018 "Invalid VF Identifier %d\n", vf_id);
2019 ret = -EINVAL;
2020 goto error_param;
2021 }
2022
2023 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002024 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002025 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2026 dev_err(&pf->pdev->dev,
2027 "Uninitialized VF %d\n", vf_id);
2028 ret = -EINVAL;
2029 goto error_param;
2030 }
2031
2032 if (!is_valid_ether_addr(mac)) {
2033 dev_err(&pf->pdev->dev,
2034 "Invalid VF ethernet address\n");
2035 ret = -EINVAL;
2036 goto error_param;
2037 }
2038
2039 /* delete the temporary mac address */
Mitch Williamse9951632015-04-27 14:57:13 -04002040 i40e_del_filter(vsi, vf->default_lan_addr.addr,
2041 vf->port_vlan_id ? vf->port_vlan_id : -1,
Greg Rose37cc0d22014-04-01 07:11:44 +00002042 true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002043
Greg Rose29f71bb2014-05-20 08:01:45 +00002044 /* Delete all the filters for this VSI - we're going to kill it
2045 * anyway.
2046 */
2047 list_for_each_entry(f, &vsi->mac_filter_list, list)
2048 i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002049
2050 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2051 /* program mac filter */
2052 if (i40e_sync_vsi_filters(vsi)) {
2053 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2054 ret = -EIO;
2055 goto error_param;
2056 }
Greg Rose9a173902014-05-22 06:32:02 +00002057 ether_addr_copy(vf->default_lan_addr.addr, mac);
Greg Rosef657a6e2013-11-28 06:39:42 +00002058 vf->pf_set_mac = true;
Greg Rose17413a82014-06-04 01:23:13 +00002059 /* Force the VF driver stop so it has to reload with new MAC address */
2060 i40e_vc_disable_vf(pf, vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002061 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002062
2063error_param:
2064 return ret;
2065}
2066
2067/**
2068 * i40e_ndo_set_vf_port_vlan
2069 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002070 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002071 * @vlan_id: mac address
2072 * @qos: priority setting
2073 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002074 * program VF vlan id and/or qos
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002075 **/
2076int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2077 int vf_id, u16 vlan_id, u8 qos)
2078{
2079 struct i40e_netdev_priv *np = netdev_priv(netdev);
2080 struct i40e_pf *pf = np->vsi->back;
2081 struct i40e_vsi *vsi;
2082 struct i40e_vf *vf;
2083 int ret = 0;
2084
2085 /* validate the request */
2086 if (vf_id >= pf->num_alloc_vfs) {
2087 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2088 ret = -EINVAL;
2089 goto error_pvid;
2090 }
2091
2092 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2093 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2094 ret = -EINVAL;
2095 goto error_pvid;
2096 }
2097
2098 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002099 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002100 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2101 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2102 ret = -EINVAL;
2103 goto error_pvid;
2104 }
2105
Mitch Williams85927ec2015-04-27 14:57:10 -04002106 if (vsi->info.pvid == (vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT)))
2107 /* duplicate request, so just return success */
2108 goto error_pvid;
2109
Greg Rosef9b4b622014-03-06 09:02:28 +00002110 if (vsi->info.pvid == 0 && i40e_is_vsi_in_vlan(vsi)) {
Greg Rose99a49732014-01-13 16:13:03 -08002111 dev_err(&pf->pdev->dev,
2112 "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",
2113 vf_id);
Greg Rosef9b4b622014-03-06 09:02:28 +00002114 /* Administrator Error - knock the VF offline until he does
2115 * the right thing by reconfiguring his network correctly
2116 * and then reloading the VF driver.
2117 */
2118 i40e_vc_disable_vf(pf, vf);
2119 }
Greg Rose99a49732014-01-13 16:13:03 -08002120
Greg Rose8d82a7c2014-01-13 16:13:04 -08002121 /* Check for condition where there was already a port VLAN ID
2122 * filter set and now it is being deleted by setting it to zero.
Greg Rose1315f7c2014-03-14 07:32:20 +00002123 * Additionally check for the condition where there was a port
2124 * VLAN but now there is a new and different port VLAN being set.
Greg Rose8d82a7c2014-01-13 16:13:04 -08002125 * Before deleting all the old VLAN filters we must add new ones
2126 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2127 * MAC addresses deleted.
2128 */
Greg Rose1315f7c2014-03-14 07:32:20 +00002129 if ((!(vlan_id || qos) ||
2130 (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
2131 vsi->info.pvid)
Greg Rose8d82a7c2014-01-13 16:13:04 -08002132 ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2133
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002134 if (vsi->info.pvid) {
2135 /* kill old VLAN */
2136 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2137 VLAN_VID_MASK));
2138 if (ret) {
2139 dev_info(&vsi->back->pdev->dev,
2140 "remove VLAN failed, ret=%d, aq_err=%d\n",
2141 ret, pf->hw.aq.asq_last_status);
2142 }
2143 }
2144 if (vlan_id || qos)
2145 ret = i40e_vsi_add_pvid(vsi,
2146 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
2147 else
Greg Rose6c12fcb2013-11-28 06:39:34 +00002148 i40e_vsi_remove_pvid(vsi);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002149
2150 if (vlan_id) {
2151 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2152 vlan_id, qos, vf_id);
2153
2154 /* add new VLAN filter */
2155 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2156 if (ret) {
2157 dev_info(&vsi->back->pdev->dev,
2158 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2159 vsi->back->hw.aq.asq_last_status);
2160 goto error_pvid;
2161 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002162 /* Kill non-vlan MAC filters - ignore error return since
2163 * there might not be any non-vlan MAC filters.
2164 */
2165 i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002166 }
2167
2168 if (ret) {
2169 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2170 goto error_pvid;
2171 }
Greg Rose6c12fcb2013-11-28 06:39:34 +00002172 /* The Port VLAN needs to be saved across resets the same as the
2173 * default LAN MAC address.
2174 */
2175 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002176 ret = 0;
2177
2178error_pvid:
2179 return ret;
2180}
2181
Mitch Williams84590fd2014-04-09 05:58:57 +00002182#define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
2183#define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002184/**
2185 * i40e_ndo_set_vf_bw
2186 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002187 * @vf_id: VF identifier
2188 * @tx_rate: Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002189 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002190 * configure VF Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002191 **/
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002192int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2193 int max_tx_rate)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002194{
Mitch Williams6b192892014-03-06 09:02:29 +00002195 struct i40e_netdev_priv *np = netdev_priv(netdev);
2196 struct i40e_pf *pf = np->vsi->back;
2197 struct i40e_vsi *vsi;
2198 struct i40e_vf *vf;
2199 int speed = 0;
2200 int ret = 0;
2201
2202 /* validate the request */
2203 if (vf_id >= pf->num_alloc_vfs) {
2204 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2205 ret = -EINVAL;
2206 goto error;
2207 }
2208
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002209 if (min_tx_rate) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002210 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 -04002211 min_tx_rate, vf_id);
2212 return -EINVAL;
2213 }
2214
Mitch Williams6b192892014-03-06 09:02:29 +00002215 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002216 vsi = pf->vsi[vf->lan_vsi_idx];
Mitch Williams6b192892014-03-06 09:02:29 +00002217 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2218 dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
2219 ret = -EINVAL;
2220 goto error;
2221 }
2222
2223 switch (pf->hw.phy.link_info.link_speed) {
2224 case I40E_LINK_SPEED_40GB:
2225 speed = 40000;
2226 break;
2227 case I40E_LINK_SPEED_10GB:
2228 speed = 10000;
2229 break;
2230 case I40E_LINK_SPEED_1GB:
2231 speed = 1000;
2232 break;
2233 default:
2234 break;
2235 }
2236
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002237 if (max_tx_rate > speed) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002238 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002239 max_tx_rate, vf->vf_id);
Mitch Williams6b192892014-03-06 09:02:29 +00002240 ret = -EINVAL;
2241 goto error;
2242 }
2243
Mitch Williamsdac9b312014-04-09 05:58:56 +00002244 if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2245 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2246 max_tx_rate = 50;
2247 }
2248
Mitch Williams6b192892014-03-06 09:02:29 +00002249 /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
Mitch Williams84590fd2014-04-09 05:58:57 +00002250 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2251 max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2252 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
Mitch Williams6b192892014-03-06 09:02:29 +00002253 if (ret) {
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002254 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
Mitch Williams6b192892014-03-06 09:02:29 +00002255 ret);
2256 ret = -EIO;
2257 goto error;
2258 }
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002259 vf->tx_rate = max_tx_rate;
Mitch Williams6b192892014-03-06 09:02:29 +00002260error:
2261 return ret;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002262}
2263
2264/**
2265 * i40e_ndo_get_vf_config
2266 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002267 * @vf_id: VF identifier
2268 * @ivi: VF configuration structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002269 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002270 * return VF configuration
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002271 **/
2272int i40e_ndo_get_vf_config(struct net_device *netdev,
2273 int vf_id, struct ifla_vf_info *ivi)
2274{
2275 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002276 struct i40e_vsi *vsi = np->vsi;
2277 struct i40e_pf *pf = vsi->back;
2278 struct i40e_vf *vf;
2279 int ret = 0;
2280
2281 /* validate the request */
2282 if (vf_id >= pf->num_alloc_vfs) {
2283 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2284 ret = -EINVAL;
2285 goto error_param;
2286 }
2287
2288 vf = &(pf->vf[vf_id]);
2289 /* first vsi is always the LAN vsi */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002290 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002291 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2292 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2293 ret = -EINVAL;
2294 goto error_param;
2295 }
2296
2297 ivi->vf = vf_id;
2298
Mitch Williamsf4a1c5c2013-11-28 06:39:34 +00002299 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002300
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002301 ivi->max_tx_rate = vf->tx_rate;
2302 ivi->min_tx_rate = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002303 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2304 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2305 I40E_VLAN_PRIORITY_SHIFT;
Mitch Williams84ca55a2014-03-14 07:32:24 +00002306 if (vf->link_forced == false)
2307 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2308 else if (vf->link_up == true)
2309 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2310 else
2311 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
Mitch Williamsc674d122014-05-20 08:01:40 +00002312 ivi->spoofchk = vf->spoofchk;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002313 ret = 0;
2314
2315error_param:
2316 return ret;
2317}
Mitch Williams588aefa2014-02-11 08:27:49 +00002318
2319/**
2320 * i40e_ndo_set_vf_link_state
2321 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002322 * @vf_id: VF identifier
Mitch Williams588aefa2014-02-11 08:27:49 +00002323 * @link: required link state
2324 *
2325 * Set the link state of a specified VF, regardless of physical link state
2326 **/
2327int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
2328{
2329 struct i40e_netdev_priv *np = netdev_priv(netdev);
2330 struct i40e_pf *pf = np->vsi->back;
2331 struct i40e_virtchnl_pf_event pfe;
2332 struct i40e_hw *hw = &pf->hw;
2333 struct i40e_vf *vf;
Ashish Shahf19efbb2014-08-01 13:27:06 -07002334 int abs_vf_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002335 int ret = 0;
2336
2337 /* validate the request */
2338 if (vf_id >= pf->num_alloc_vfs) {
2339 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2340 ret = -EINVAL;
2341 goto error_out;
2342 }
2343
2344 vf = &pf->vf[vf_id];
Ashish Shahf19efbb2014-08-01 13:27:06 -07002345 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002346
2347 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
2348 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
2349
2350 switch (link) {
2351 case IFLA_VF_LINK_STATE_AUTO:
2352 vf->link_forced = false;
2353 pfe.event_data.link_event.link_status =
2354 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
2355 pfe.event_data.link_event.link_speed =
2356 pf->hw.phy.link_info.link_speed;
2357 break;
2358 case IFLA_VF_LINK_STATE_ENABLE:
2359 vf->link_forced = true;
2360 vf->link_up = true;
2361 pfe.event_data.link_event.link_status = true;
2362 pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
2363 break;
2364 case IFLA_VF_LINK_STATE_DISABLE:
2365 vf->link_forced = true;
2366 vf->link_up = false;
2367 pfe.event_data.link_event.link_status = false;
2368 pfe.event_data.link_event.link_speed = 0;
2369 break;
2370 default:
2371 ret = -EINVAL;
2372 goto error_out;
2373 }
2374 /* Notify the VF of its new link state */
Ashish Shahf19efbb2014-08-01 13:27:06 -07002375 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Mitch Williams588aefa2014-02-11 08:27:49 +00002376 0, (u8 *)&pfe, sizeof(pfe), NULL);
2377
2378error_out:
2379 return ret;
2380}
Mitch Williamsc674d122014-05-20 08:01:40 +00002381
2382/**
2383 * i40e_ndo_set_vf_spoofchk
2384 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002385 * @vf_id: VF identifier
Mitch Williamsc674d122014-05-20 08:01:40 +00002386 * @enable: flag to enable or disable feature
2387 *
2388 * Enable or disable VF spoof checking
2389 **/
Serey Konge6d90042014-07-12 07:28:14 +00002390int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
Mitch Williamsc674d122014-05-20 08:01:40 +00002391{
2392 struct i40e_netdev_priv *np = netdev_priv(netdev);
2393 struct i40e_vsi *vsi = np->vsi;
2394 struct i40e_pf *pf = vsi->back;
2395 struct i40e_vsi_context ctxt;
2396 struct i40e_hw *hw = &pf->hw;
2397 struct i40e_vf *vf;
2398 int ret = 0;
2399
2400 /* validate the request */
2401 if (vf_id >= pf->num_alloc_vfs) {
2402 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2403 ret = -EINVAL;
2404 goto out;
2405 }
2406
2407 vf = &(pf->vf[vf_id]);
2408
2409 if (enable == vf->spoofchk)
2410 goto out;
2411
2412 vf->spoofchk = enable;
2413 memset(&ctxt, 0, sizeof(ctxt));
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002414 ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
Mitch Williamsc674d122014-05-20 08:01:40 +00002415 ctxt.pf_num = pf->hw.pf_id;
2416 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
2417 if (enable)
Greg Rose30d71af2015-01-29 07:17:17 +00002418 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
2419 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
Mitch Williamsc674d122014-05-20 08:01:40 +00002420 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
2421 if (ret) {
2422 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
2423 ret);
2424 ret = -EIO;
2425 }
2426out:
2427 return ret;
2428}