blob: d29d4062addf51141dbeefc152d1613b8171c981 [file] [log] [blame]
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Neerav Parikh51616012015-02-06 08:52:14 +00004 * Copyright(c) 2013 - 2015 Intel Corporation.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27#include "i40e.h"
28
Mitch Williams532b0452015-04-07 19:45:34 -040029/*********************notification routines***********************/
30
31/**
32 * i40e_vc_vf_broadcast
33 * @pf: pointer to the PF structure
34 * @opcode: operation code
35 * @retval: return value
36 * @msg: pointer to the msg buffer
37 * @msglen: msg length
38 *
39 * send a message to all VFs on a given PF
40 **/
41static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
42 enum i40e_virtchnl_ops v_opcode,
43 i40e_status v_retval, u8 *msg,
44 u16 msglen)
45{
46 struct i40e_hw *hw = &pf->hw;
47 struct i40e_vf *vf = pf->vf;
48 int i;
49
50 for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
51 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
52 /* Not all vfs are enabled so skip the ones that are not */
53 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
54 !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
55 continue;
56
57 /* Ignore return value on purpose - a given VF may fail, but
58 * we need to keep going and send to all of them
59 */
60 i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
61 msg, msglen, NULL);
62 }
63}
64
65/**
66 * i40e_vc_notify_link_state
67 * @vf: pointer to the VF structure
68 *
69 * send a link status message to a single VF
70 **/
71static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
72{
73 struct i40e_virtchnl_pf_event pfe;
74 struct i40e_pf *pf = vf->pf;
75 struct i40e_hw *hw = &pf->hw;
76 struct i40e_link_status *ls = &pf->hw.phy.link_info;
77 int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
78
79 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
80 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
81 if (vf->link_forced) {
82 pfe.event_data.link_event.link_status = vf->link_up;
83 pfe.event_data.link_event.link_speed =
84 (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
85 } else {
86 pfe.event_data.link_event.link_status =
87 ls->link_info & I40E_AQ_LINK_UP;
88 pfe.event_data.link_event.link_speed = ls->link_speed;
89 }
90 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
91 0, (u8 *)&pfe, sizeof(pfe), NULL);
92}
93
94/**
95 * i40e_vc_notify_link_state
96 * @pf: pointer to the PF structure
97 *
98 * send a link status message to all VFs on a given PF
99 **/
100void i40e_vc_notify_link_state(struct i40e_pf *pf)
101{
102 int i;
103
104 for (i = 0; i < pf->num_alloc_vfs; i++)
105 i40e_vc_notify_vf_link_state(&pf->vf[i]);
106}
107
108/**
109 * i40e_vc_notify_reset
110 * @pf: pointer to the PF structure
111 *
112 * indicate a pending reset to all VFs on a given PF
113 **/
114void i40e_vc_notify_reset(struct i40e_pf *pf)
115{
116 struct i40e_virtchnl_pf_event pfe;
117
118 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
119 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
120 i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, 0,
121 (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
122}
123
124/**
125 * i40e_vc_notify_vf_reset
126 * @vf: pointer to the VF structure
127 *
128 * indicate a pending reset to the given VF
129 **/
130void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
131{
132 struct i40e_virtchnl_pf_event pfe;
133 int abs_vf_id;
134
135 /* validate the request */
136 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
137 return;
138
139 /* verify if the VF is in either init or active before proceeding */
140 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
141 !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
142 return;
143
144 abs_vf_id = vf->vf_id + vf->pf->hw.func_caps.vf_base_id;
145
146 pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
147 pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
148 i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
149 0, (u8 *)&pfe,
150 sizeof(struct i40e_virtchnl_pf_event), NULL);
151}
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000152/***********************misc routines*****************************/
153
154/**
Greg Rosef9b4b622014-03-06 09:02:28 +0000155 * i40e_vc_disable_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000156 * @pf: pointer to the PF info
157 * @vf: pointer to the VF info
Greg Rosef9b4b622014-03-06 09:02:28 +0000158 *
159 * Disable the VF through a SW reset
160 **/
161static inline void i40e_vc_disable_vf(struct i40e_pf *pf, struct i40e_vf *vf)
162{
Mitch Williams54f455e2015-04-27 14:57:14 -0400163 i40e_vc_notify_vf_reset(vf);
164 i40e_reset_vf(vf, false);
Greg Rosef9b4b622014-03-06 09:02:28 +0000165}
166
167/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000168 * i40e_vc_isvalid_vsi_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000169 * @vf: pointer to the VF info
170 * @vsi_id: VF relative VSI id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000171 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000172 * check for the valid VSI id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000173 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700174static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000175{
176 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700177 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000178
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700179 return (vsi && (vsi->vf_id == vf->vf_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000180}
181
182/**
183 * i40e_vc_isvalid_queue_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000184 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000185 * @vsi_id: vsi id
186 * @qid: vsi relative queue id
187 *
188 * check for the valid queue id
189 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700190static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000191 u8 qid)
192{
193 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700194 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000195
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700196 return (vsi && (qid < vsi->alloc_queue_pairs));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000197}
198
199/**
200 * i40e_vc_isvalid_vector_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000201 * @vf: pointer to the VF info
202 * @vector_id: VF relative vector id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000203 *
204 * check for the valid vector id
205 **/
206static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
207{
208 struct i40e_pf *pf = vf->pf;
209
Mitch Williams9347eb72014-02-11 08:26:32 +0000210 return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000211}
212
213/***********************vf resource mgmt routines*****************/
214
215/**
216 * i40e_vc_get_pf_queue_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000217 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700218 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000219 * @vsi_queue_id: vsi relative queue id
220 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000221 * return PF relative queue id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000222 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700223static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000224 u8 vsi_queue_id)
225{
226 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700227 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000228 u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
229
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700230 if (!vsi)
231 return pf_queue_id;
232
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000233 if (le16_to_cpu(vsi->info.mapping_flags) &
234 I40E_AQ_VSI_QUE_MAP_NONCONTIG)
235 pf_queue_id =
236 le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
237 else
238 pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
239 vsi_queue_id;
240
241 return pf_queue_id;
242}
243
244/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000245 * i40e_config_irq_link_list
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000246 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700247 * @vsi_id: id of VSI as given by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000248 * @vecmap: irq map info
249 *
250 * configure irq link list from the map
251 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700252static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000253 struct i40e_virtchnl_vector_map *vecmap)
254{
255 unsigned long linklistmap = 0, tempmap;
256 struct i40e_pf *pf = vf->pf;
257 struct i40e_hw *hw = &pf->hw;
258 u16 vsi_queue_id, pf_queue_id;
259 enum i40e_queue_type qtype;
260 u16 next_q, vector_id;
261 u32 reg, reg_idx;
262 u16 itr_idx = 0;
263
264 vector_id = vecmap->vector_id;
265 /* setup the head */
266 if (0 == vector_id)
267 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
268 else
269 reg_idx = I40E_VPINT_LNKLSTN(
Mitch Williams9347eb72014-02-11 08:26:32 +0000270 ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
271 (vector_id - 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000272
273 if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
274 /* Special case - No queues mapped on this vector */
275 wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
276 goto irq_list_done;
277 }
278 tempmap = vecmap->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000279 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400280 linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
281 vsi_queue_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000282 }
283
284 tempmap = vecmap->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000285 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400286 linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
287 vsi_queue_id + 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000288 }
289
290 next_q = find_first_bit(&linklistmap,
291 (I40E_MAX_VSI_QP *
292 I40E_VIRTCHNL_SUPPORTED_QTYPES));
293 vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
294 qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700295 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000296 reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
297
298 wr32(hw, reg_idx, reg);
299
300 while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
301 switch (qtype) {
302 case I40E_QUEUE_TYPE_RX:
303 reg_idx = I40E_QINT_RQCTL(pf_queue_id);
304 itr_idx = vecmap->rxitr_idx;
305 break;
306 case I40E_QUEUE_TYPE_TX:
307 reg_idx = I40E_QINT_TQCTL(pf_queue_id);
308 itr_idx = vecmap->txitr_idx;
309 break;
310 default:
311 break;
312 }
313
314 next_q = find_next_bit(&linklistmap,
315 (I40E_MAX_VSI_QP *
316 I40E_VIRTCHNL_SUPPORTED_QTYPES),
317 next_q + 1);
Mitch Williams829af3a2013-12-18 13:46:00 +0000318 if (next_q <
319 (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000320 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
321 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700322 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000323 vsi_queue_id);
324 } else {
325 pf_queue_id = I40E_QUEUE_END_OF_LIST;
326 qtype = 0;
327 }
328
329 /* format for the RQCTL & TQCTL regs is same */
330 reg = (vector_id) |
331 (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
332 (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400333 BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000334 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
335 wr32(hw, reg_idx, reg);
336 }
337
338irq_list_done:
339 i40e_flush(hw);
340}
341
342/**
343 * i40e_config_vsi_tx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000344 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700345 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000346 * @vsi_queue_id: vsi relative queue index
347 * @info: config. info
348 *
349 * configure tx queue
350 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700351static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000352 u16 vsi_queue_id,
353 struct i40e_virtchnl_txq_info *info)
354{
355 struct i40e_pf *pf = vf->pf;
356 struct i40e_hw *hw = &pf->hw;
357 struct i40e_hmc_obj_txq tx_ctx;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700358 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000359 u16 pf_queue_id;
360 u32 qtx_ctl;
361 int ret = 0;
362
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700363 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
364 vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000365
366 /* clear the context structure first */
367 memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
368
369 /* only set the required fields */
370 tx_ctx.base = info->dma_ring_addr / 128;
371 tx_ctx.qlen = info->ring_len;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700372 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000373 tx_ctx.rdylist_act = 0;
Ashish Shah5d298962014-05-22 06:31:25 +0000374 tx_ctx.head_wb_ena = info->headwb_enabled;
375 tx_ctx.head_wb_addr = info->dma_headwb_addr;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000376
377 /* clear the context in the HMC */
378 ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
379 if (ret) {
380 dev_err(&pf->pdev->dev,
381 "Failed to clear VF LAN Tx queue context %d, error: %d\n",
382 pf_queue_id, ret);
383 ret = -ENOENT;
384 goto error_context;
385 }
386
387 /* set the context in the HMC */
388 ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
389 if (ret) {
390 dev_err(&pf->pdev->dev,
391 "Failed to set VF LAN Tx queue context %d error: %d\n",
392 pf_queue_id, ret);
393 ret = -ENOENT;
394 goto error_context;
395 }
396
397 /* associate this queue with the PCI VF function */
398 qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +0000399 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000400 & I40E_QTX_CTL_PF_INDX_MASK);
401 qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
402 << I40E_QTX_CTL_VFVM_INDX_SHIFT)
403 & I40E_QTX_CTL_VFVM_INDX_MASK);
404 wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
405 i40e_flush(hw);
406
407error_context:
408 return ret;
409}
410
411/**
412 * i40e_config_vsi_rx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000413 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700414 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000415 * @vsi_queue_id: vsi relative queue index
416 * @info: config. info
417 *
418 * configure rx queue
419 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700420static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000421 u16 vsi_queue_id,
422 struct i40e_virtchnl_rxq_info *info)
423{
424 struct i40e_pf *pf = vf->pf;
425 struct i40e_hw *hw = &pf->hw;
426 struct i40e_hmc_obj_rxq rx_ctx;
427 u16 pf_queue_id;
428 int ret = 0;
429
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700430 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000431
432 /* clear the context structure first */
433 memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
434
435 /* only set the required fields */
436 rx_ctx.base = info->dma_ring_addr / 128;
437 rx_ctx.qlen = info->ring_len;
438
439 if (info->splithdr_enabled) {
440 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
441 I40E_RX_SPLIT_IP |
442 I40E_RX_SPLIT_TCP_UDP |
443 I40E_RX_SPLIT_SCTP;
444 /* header length validation */
445 if (info->hdr_size > ((2 * 1024) - 64)) {
446 ret = -EINVAL;
447 goto error_param;
448 }
449 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
450
451 /* set splitalways mode 10b */
452 rx_ctx.dtype = 0x2;
453 }
454
455 /* databuffer length validation */
456 if (info->databuffer_size > ((16 * 1024) - 128)) {
457 ret = -EINVAL;
458 goto error_param;
459 }
460 rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
461
462 /* max pkt. length validation */
463 if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
464 ret = -EINVAL;
465 goto error_param;
466 }
467 rx_ctx.rxmax = info->max_pkt_size;
468
469 /* enable 32bytes desc always */
470 rx_ctx.dsize = 1;
471
472 /* default values */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000473 rx_ctx.lrxqthresh = 2;
474 rx_ctx.crcstrip = 1;
Mitch Williams50d41652014-04-09 05:58:55 +0000475 rx_ctx.prefena = 1;
Shannon Nelsonc1d11ce2014-07-29 04:01:03 +0000476 rx_ctx.l2tsel = 1;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000477
478 /* clear the context in the HMC */
479 ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
480 if (ret) {
481 dev_err(&pf->pdev->dev,
482 "Failed to clear VF LAN Rx queue context %d, error: %d\n",
483 pf_queue_id, ret);
484 ret = -ENOENT;
485 goto error_param;
486 }
487
488 /* set the context in the HMC */
489 ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
490 if (ret) {
491 dev_err(&pf->pdev->dev,
492 "Failed to set VF LAN Rx queue context %d error: %d\n",
493 pf_queue_id, ret);
494 ret = -ENOENT;
495 goto error_param;
496 }
497
498error_param:
499 return ret;
500}
501
502/**
503 * i40e_alloc_vsi_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000504 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000505 * @type: type of VSI to allocate
506 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000507 * alloc VF vsi context & resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000508 **/
509static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
510{
511 struct i40e_mac_filter *f = NULL;
512 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000513 struct i40e_vsi *vsi;
514 int ret = 0;
515
516 vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
517
518 if (!vsi) {
519 dev_err(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000520 "add vsi failed for VF %d, aq_err %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000521 vf->vf_id, pf->hw.aq.asq_last_status);
522 ret = -ENOENT;
523 goto error_alloc_vsi_res;
524 }
525 if (type == I40E_VSI_SRIOV) {
Greg Rose1a103702013-11-28 06:42:39 +0000526 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700527 vf->lan_vsi_idx = vsi->idx;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000528 vf->lan_vsi_id = vsi->id;
Greg Rose6c12fcb2013-11-28 06:39:34 +0000529 /* If the port VLAN has been configured and then the
530 * VF driver was removed then the VSI port VLAN
531 * configuration was destroyed. Check if there is
532 * a port VLAN and restore the VSI configuration if
533 * needed.
534 */
535 if (vf->port_vlan_id)
536 i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000537 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
Mitch Williamse9951632015-04-27 14:57:13 -0400538 vf->port_vlan_id ? vf->port_vlan_id : -1,
539 true, false);
Greg Rose1a103702013-11-28 06:42:39 +0000540 if (!f)
541 dev_info(&pf->pdev->dev,
542 "Could not allocate VF MAC addr\n");
Mitch Williamse9951632015-04-27 14:57:13 -0400543 f = i40e_add_filter(vsi, brdcast,
544 vf->port_vlan_id ? vf->port_vlan_id : -1,
Greg Rose1a103702013-11-28 06:42:39 +0000545 true, false);
546 if (!f)
547 dev_info(&pf->pdev->dev,
548 "Could not allocate VF broadcast filter\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000549 }
Neerav Parikh6dbbbfb2013-11-26 10:49:24 +0000550
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000551 /* program mac filter */
552 ret = i40e_sync_vsi_filters(vsi);
Mitch Williamsfd1646e2014-02-13 03:48:44 -0800553 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000554 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000555
Mitch Williams6b192892014-03-06 09:02:29 +0000556 /* Set VF bandwidth if specified */
557 if (vf->tx_rate) {
558 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
559 vf->tx_rate / 50, 0, NULL);
560 if (ret)
561 dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
562 vf->vf_id, ret);
563 }
564
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000565error_alloc_vsi_res:
566 return ret;
567}
568
569/**
Mitch Williams805bd5b2013-11-28 06:39:26 +0000570 * i40e_enable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000571 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000572 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000573 * enable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000574 **/
575static void i40e_enable_vf_mappings(struct i40e_vf *vf)
576{
577 struct i40e_pf *pf = vf->pf;
578 struct i40e_hw *hw = &pf->hw;
579 u32 reg, total_queue_pairs = 0;
580 int j;
581
582 /* Tell the hardware we're using noncontiguous mapping. HW requires
583 * that VF queues be mapped using this method, even when they are
584 * contiguous in real life
585 */
586 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
587 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
588
589 /* enable VF vplan_qtable mappings */
590 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
591 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
592
593 /* map PF queues to VF queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700594 for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
595 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
Mitch Williams805bd5b2013-11-28 06:39:26 +0000596 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
597 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
598 total_queue_pairs++;
599 }
600
601 /* map PF queues to VSI */
602 for (j = 0; j < 7; j++) {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700603 if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
Mitch Williams805bd5b2013-11-28 06:39:26 +0000604 reg = 0x07FF07FF; /* unused */
605 } else {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700606 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000607 j * 2);
608 reg = qid;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700609 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000610 (j * 2) + 1);
611 reg |= qid << 16;
612 }
613 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
614 }
615
616 i40e_flush(hw);
617}
618
619/**
620 * i40e_disable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000621 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000622 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000623 * disable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000624 **/
625static void i40e_disable_vf_mappings(struct i40e_vf *vf)
626{
627 struct i40e_pf *pf = vf->pf;
628 struct i40e_hw *hw = &pf->hw;
629 int i;
630
631 /* disable qp mappings */
632 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
633 for (i = 0; i < I40E_MAX_VSI_QP; i++)
634 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
635 I40E_QUEUE_END_OF_LIST);
636 i40e_flush(hw);
637}
638
639/**
640 * i40e_free_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000641 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000642 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000643 * free VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000644 **/
645static void i40e_free_vf_res(struct i40e_vf *vf)
646{
647 struct i40e_pf *pf = vf->pf;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000648 struct i40e_hw *hw = &pf->hw;
649 u32 reg_idx, reg;
650 int i, msix_vf;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000651
652 /* free vsi & disconnect it from the parent uplink */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700653 if (vf->lan_vsi_idx) {
654 i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
655 vf->lan_vsi_idx = 0;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000656 vf->lan_vsi_id = 0;
657 }
Mitch Williams9347eb72014-02-11 08:26:32 +0000658 msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
659
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000660 /* disable interrupts so the VF starts in a known state */
661 for (i = 0; i < msix_vf; i++) {
662 /* format is same for both registers */
663 if (0 == i)
664 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
665 else
666 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
667 (vf->vf_id))
668 + (i - 1));
669 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
670 i40e_flush(hw);
671 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000672
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000673 /* clear the irq settings */
674 for (i = 0; i < msix_vf; i++) {
675 /* format is same for both registers */
676 if (0 == i)
677 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
678 else
679 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
680 (vf->vf_id))
681 + (i - 1));
682 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
683 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
684 wr32(hw, reg_idx, reg);
685 i40e_flush(hw);
686 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000687 /* reset some of the state varibles keeping
688 * track of the resources
689 */
690 vf->num_queue_pairs = 0;
691 vf->vf_states = 0;
692}
693
694/**
695 * i40e_alloc_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000696 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000697 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000698 * allocate VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000699 **/
700static int i40e_alloc_vf_res(struct i40e_vf *vf)
701{
702 struct i40e_pf *pf = vf->pf;
703 int total_queue_pairs = 0;
704 int ret;
705
706 /* allocate hw vsi context & associated resources */
707 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
708 if (ret)
709 goto error_alloc;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700710 total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000711 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
712
713 /* store the total qps number for the runtime
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000714 * VF req validation
Mitch Williams805bd5b2013-11-28 06:39:26 +0000715 */
716 vf->num_queue_pairs = total_queue_pairs;
717
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000718 /* VF is now completely initialized */
Mitch Williams805bd5b2013-11-28 06:39:26 +0000719 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
720
721error_alloc:
722 if (ret)
723 i40e_free_vf_res(vf);
724
725 return ret;
726}
727
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000728#define VF_DEVICE_STATUS 0xAA
729#define VF_TRANS_PENDING_MASK 0x20
730/**
731 * i40e_quiesce_vf_pci
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000732 * @vf: pointer to the VF structure
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000733 *
734 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
735 * if the transactions never clear.
736 **/
737static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
738{
739 struct i40e_pf *pf = vf->pf;
740 struct i40e_hw *hw = &pf->hw;
741 int vf_abs_id, i;
742 u32 reg;
743
Mitch Williamsb141d612013-11-28 06:39:36 +0000744 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000745
746 wr32(hw, I40E_PF_PCI_CIAA,
747 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
748 for (i = 0; i < 100; i++) {
749 reg = rd32(hw, I40E_PF_PCI_CIAD);
750 if ((reg & VF_TRANS_PENDING_MASK) == 0)
751 return 0;
752 udelay(1);
753 }
754 return -EIO;
755}
756
Mitch Williams805bd5b2013-11-28 06:39:26 +0000757/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000758 * i40e_reset_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000759 * @vf: pointer to the VF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000760 * @flr: VFLR was issued or not
761 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000762 * reset the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000763 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000764void i40e_reset_vf(struct i40e_vf *vf, bool flr)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000765{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000766 struct i40e_pf *pf = vf->pf;
767 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000768 bool rsd = false;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000769 int i;
770 u32 reg;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000771
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000772 if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
773 return;
774
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000775 /* warn the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000776 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
777
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000778 /* In the case of a VFLR, the HW has already reset the VF and we
779 * just need to clean up, so don't hit the VFRTRIG register.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000780 */
781 if (!flr) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000782 /* reset VF using VPGEN_VFRTRIG reg */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000783 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
784 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000785 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
786 i40e_flush(hw);
787 }
788
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000789 if (i40e_quiesce_vf_pci(vf))
790 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
791 vf->vf_id);
792
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000793 /* poll VPGEN_VFRSTAT reg to make sure
794 * that reset is complete
795 */
Mitch Williams1750a222015-01-09 11:18:13 +0000796 for (i = 0; i < 10; i++) {
797 /* VF reset requires driver to first reset the VF and then
798 * poll the status register to make sure that the reset
799 * completed successfully. Due to internal HW FIFO flushes,
800 * we must wait 10ms before the register will be valid.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000801 */
Mitch Williams1750a222015-01-09 11:18:13 +0000802 usleep_range(10000, 20000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000803 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
804 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
805 rsd = true;
806 break;
807 }
808 }
809
Anjali Singhai Jain57175ac2015-04-07 19:45:35 -0400810 if (flr)
811 usleep_range(10000, 20000);
812
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000813 if (!rsd)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000814 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000815 vf->vf_id);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000816 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000817 /* clear the reset bit in the VPGEN_VFRTRIG reg */
818 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
819 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
820 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000821
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000822 /* On initial reset, we won't have any queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700823 if (vf->lan_vsi_idx == 0)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000824 goto complete_reset;
825
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700826 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000827complete_reset:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000828 /* reallocate VF resources to reset the VSI state */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000829 i40e_free_vf_res(vf);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000830 i40e_alloc_vf_res(vf);
831 i40e_enable_vf_mappings(vf);
Mitch Williamsc17b3622014-02-13 03:48:45 -0800832 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
Mitch Williams5b8f8502015-04-27 14:57:15 -0400833 clear_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000834
835 /* tell the VF the reset is done */
836 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
837 i40e_flush(hw);
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000838 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000839}
Greg Rosec3542292013-12-13 08:38:38 +0000840
841/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000842 * i40e_free_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000843 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000844 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000845 * free VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000846 **/
847void i40e_free_vfs(struct i40e_pf *pf)
848{
Mitch Williamsf7414532013-11-28 06:39:40 +0000849 struct i40e_hw *hw = &pf->hw;
850 u32 reg_idx, bit_idx;
851 int i, tmp, vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000852
853 if (!pf->vf)
854 return;
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000855 while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
856 usleep_range(1000, 2000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000857
Mitch Williams44434632015-04-07 11:32:55 -0700858 for (i = 0; i < pf->num_alloc_vfs; i++)
859 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
860 i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
861 false);
862
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000863 /* Disable IOV before freeing resources. This lets any VF drivers
864 * running in the host get themselves cleaned up before we yank
865 * the carpet out from underneath their feet.
866 */
867 if (!pci_vfs_assigned(pf->pdev))
868 pci_disable_sriov(pf->pdev);
Mitch Williams6d7b9672015-03-31 00:45:02 -0700869 else
870 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000871
872 msleep(20); /* let any messages in transit get finished up */
873
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000874 /* free up VF resources */
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000875 tmp = pf->num_alloc_vfs;
876 pf->num_alloc_vfs = 0;
877 for (i = 0; i < tmp; i++) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000878 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
879 i40e_free_vf_res(&pf->vf[i]);
880 /* disable qp mappings */
881 i40e_disable_vf_mappings(&pf->vf[i]);
882 }
883
884 kfree(pf->vf);
885 pf->vf = NULL;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000886
Mitch Williams9e5634d2014-04-04 04:43:14 +0000887 /* This check is for when the driver is unloaded while VFs are
888 * assigned. Setting the number of VFs to 0 through sysfs is caught
889 * before this function ever gets called.
890 */
Ethan Zhaoc24817b2014-07-22 18:36:43 +0000891 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williamsf7414532013-11-28 06:39:40 +0000892 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
893 * work correctly when SR-IOV gets re-enabled.
894 */
895 for (vf_id = 0; vf_id < tmp; vf_id++) {
896 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
897 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400898 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
Mitch Williamsf7414532013-11-28 06:39:40 +0000899 }
Greg Rosec3542292013-12-13 08:38:38 +0000900 }
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000901 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000902}
903
904#ifdef CONFIG_PCI_IOV
905/**
906 * i40e_alloc_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000907 * @pf: pointer to the PF structure
908 * @num_alloc_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000909 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000910 * allocate VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000911 **/
Mitch Williams4aeec012014-02-13 03:48:47 -0800912int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000913{
914 struct i40e_vf *vfs;
915 int i, ret = 0;
916
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000917 /* Disable interrupt 0 so we don't try to handle the VFLR. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000918 i40e_irq_dynamic_disable_icr0(pf);
919
Mitch Williams4aeec012014-02-13 03:48:47 -0800920 /* Check to see if we're just allocating resources for extant VFs */
921 if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
922 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
923 if (ret) {
924 dev_err(&pf->pdev->dev,
925 "Failed to enable SR-IOV, error %d.\n", ret);
926 pf->num_alloc_vfs = 0;
927 goto err_iov;
928 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000929 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000930 /* allocate memory */
Akeem G Abodunrincc6456a2014-02-06 05:51:02 +0000931 vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000932 if (!vfs) {
933 ret = -ENOMEM;
934 goto err_alloc;
935 }
Mitch Williamsc674d122014-05-20 08:01:40 +0000936 pf->vf = vfs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000937
938 /* apply default profile */
939 for (i = 0; i < num_alloc_vfs; i++) {
940 vfs[i].pf = pf;
941 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
942 vfs[i].vf_id = i;
943
944 /* assign default capabilities */
945 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
Mitch Williamsc674d122014-05-20 08:01:40 +0000946 vfs[i].spoofchk = true;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000947 /* VF resources get allocated during reset */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000948 i40e_reset_vf(&vfs[i], false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000949
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000950 /* enable VF vplan_qtable mappings */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000951 i40e_enable_vf_mappings(&vfs[i]);
952 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000953 pf->num_alloc_vfs = num_alloc_vfs;
954
955err_alloc:
956 if (ret)
957 i40e_free_vfs(pf);
958err_iov:
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000959 /* Re-enable interrupt 0. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000960 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000961 return ret;
962}
963
964#endif
965/**
966 * i40e_pci_sriov_enable
967 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000968 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000969 *
970 * Enable or change the number of VFs
971 **/
972static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
973{
974#ifdef CONFIG_PCI_IOV
975 struct i40e_pf *pf = pci_get_drvdata(pdev);
976 int pre_existing_vfs = pci_num_vf(pdev);
977 int err = 0;
978
Greg Rosee17bc412015-04-16 20:05:59 -0400979 if (pf->state & __I40E_TESTING) {
980 dev_warn(&pdev->dev,
981 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
982 err = -EPERM;
983 goto err_out;
984 }
985
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000986 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
987 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
988 i40e_free_vfs(pf);
989 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
990 goto out;
991
992 if (num_vfs > pf->num_req_vfs) {
993 err = -EPERM;
994 goto err_out;
995 }
996
997 err = i40e_alloc_vfs(pf, num_vfs);
998 if (err) {
999 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1000 goto err_out;
1001 }
1002
1003out:
1004 return num_vfs;
1005
1006err_out:
1007 return err;
1008#endif
1009 return 0;
1010}
1011
1012/**
1013 * i40e_pci_sriov_configure
1014 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001015 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001016 *
1017 * Enable or change the number of VFs. Called when the user updates the number
1018 * of VFs in sysfs.
1019 **/
1020int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1021{
1022 struct i40e_pf *pf = pci_get_drvdata(pdev);
1023
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07001024 if (num_vfs) {
1025 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1026 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1027 i40e_do_reset_safe(pf,
1028 BIT_ULL(__I40E_PF_RESET_REQUESTED));
1029 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001030 return i40e_pci_sriov_enable(pdev, num_vfs);
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07001031 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001032
Ethan Zhaoc24817b2014-07-22 18:36:43 +00001033 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williams9e5634d2014-04-04 04:43:14 +00001034 i40e_free_vfs(pf);
Anjali Singhai Jainfc608612015-05-08 15:35:57 -07001035 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1036 i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
Mitch Williams9e5634d2014-04-04 04:43:14 +00001037 } else {
1038 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1039 return -EINVAL;
1040 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001041 return 0;
1042}
1043
1044/***********************virtual channel routines******************/
1045
1046/**
1047 * i40e_vc_send_msg_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001048 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001049 * @v_opcode: virtual channel opcode
1050 * @v_retval: virtual channel return value
1051 * @msg: pointer to the msg buffer
1052 * @msglen: msg length
1053 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001054 * send msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001055 **/
1056static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1057 u32 v_retval, u8 *msg, u16 msglen)
1058{
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001059 struct i40e_pf *pf;
1060 struct i40e_hw *hw;
1061 int abs_vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001062 i40e_status aq_ret;
1063
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001064 /* validate the request */
1065 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1066 return -EINVAL;
1067
1068 pf = vf->pf;
1069 hw = &pf->hw;
1070 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1071
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001072 /* single place to detect unsuccessful return values */
1073 if (v_retval) {
1074 vf->num_invalid_msgs++;
1075 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
1076 v_opcode, v_retval);
1077 if (vf->num_invalid_msgs >
1078 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1079 dev_err(&pf->pdev->dev,
1080 "Number of invalid messages exceeded for VF %d\n",
1081 vf->vf_id);
1082 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1083 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1084 }
1085 } else {
1086 vf->num_valid_msgs++;
1087 }
1088
Ashish Shahf19efbb2014-08-01 13:27:06 -07001089 aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Mitch Williams7efa84b2013-11-28 06:39:41 +00001090 msg, msglen, NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001091 if (aq_ret) {
1092 dev_err(&pf->pdev->dev,
1093 "Unable to send the message to VF %d aq_err %d\n",
1094 vf->vf_id, pf->hw.aq.asq_last_status);
1095 return -EIO;
1096 }
1097
1098 return 0;
1099}
1100
1101/**
1102 * i40e_vc_send_resp_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001103 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001104 * @opcode: operation code
1105 * @retval: return value
1106 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001107 * send resp msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001108 **/
1109static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1110 enum i40e_virtchnl_ops opcode,
1111 i40e_status retval)
1112{
1113 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1114}
1115
1116/**
1117 * i40e_vc_get_version_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001118 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001119 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001120 * called from the VF to request the API version used by the PF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001121 **/
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001122static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001123{
1124 struct i40e_virtchnl_version_info info = {
1125 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
1126 };
1127
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001128 vf->vf_ver = *(struct i40e_virtchnl_version_info *)msg;
Mitch Williams606a5482015-06-04 16:24:00 -04001129 /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1130 if (VF_IS_V10(vf))
1131 info.minor = I40E_VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001132 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
1133 I40E_SUCCESS, (u8 *)&info,
1134 sizeof(struct
1135 i40e_virtchnl_version_info));
1136}
1137
1138/**
1139 * i40e_vc_get_vf_resources_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001140 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001141 * @msg: pointer to the msg buffer
1142 * @msglen: msg length
1143 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001144 * called from the VF to request its resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001145 **/
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001146static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001147{
1148 struct i40e_virtchnl_vf_resource *vfres = NULL;
1149 struct i40e_pf *pf = vf->pf;
1150 i40e_status aq_ret = 0;
1151 struct i40e_vsi *vsi;
1152 int i = 0, len = 0;
1153 int num_vsis = 1;
1154 int ret;
1155
1156 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1157 aq_ret = I40E_ERR_PARAM;
1158 goto err;
1159 }
1160
1161 len = (sizeof(struct i40e_virtchnl_vf_resource) +
1162 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1163
1164 vfres = kzalloc(len, GFP_KERNEL);
1165 if (!vfres) {
1166 aq_ret = I40E_ERR_NO_MEMORY;
1167 len = 0;
1168 goto err;
1169 }
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001170 if (VF_IS_V11(vf))
1171 vf->driver_caps = *(u32 *)msg;
1172 else
1173 vf->driver_caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
1174 I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
1175 I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001176
1177 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001178 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001179 if (!vsi->info.pvid)
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001180 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN |
1181 I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001182
1183 vfres->num_vsis = num_vsis;
1184 vfres->num_queue_pairs = vf->num_queue_pairs;
1185 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001186 if (vf->lan_vsi_idx) {
1187 vfres->vsi_res[i].vsi_id = vf->lan_vsi_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001188 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1189 vfres->vsi_res[i].num_queue_pairs =
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001190 pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001191 memcpy(vfres->vsi_res[i].default_mac_addr,
1192 vf->default_lan_addr.addr, ETH_ALEN);
1193 i++;
1194 }
1195 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1196
1197err:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001198 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001199 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1200 aq_ret, (u8 *)vfres, len);
1201
1202 kfree(vfres);
1203 return ret;
1204}
1205
1206/**
1207 * i40e_vc_reset_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001208 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001209 * @msg: pointer to the msg buffer
1210 * @msglen: msg length
1211 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001212 * called from the VF to reset itself,
1213 * unlike other virtchnl messages, PF driver
1214 * doesn't send the response back to the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001215 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001216static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001217{
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001218 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1219 i40e_reset_vf(vf, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001220}
1221
1222/**
1223 * i40e_vc_config_promiscuous_mode_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001224 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001225 * @msg: pointer to the msg buffer
1226 * @msglen: msg length
1227 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001228 * called from the VF to configure the promiscuous mode of
1229 * VF vsis
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001230 **/
1231static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1232 u8 *msg, u16 msglen)
1233{
1234 struct i40e_virtchnl_promisc_info *info =
1235 (struct i40e_virtchnl_promisc_info *)msg;
1236 struct i40e_pf *pf = vf->pf;
1237 struct i40e_hw *hw = &pf->hw;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001238 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001239 bool allmulti = false;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001240 i40e_status aq_ret;
1241
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001242 vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001243 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1244 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1245 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001246 (vsi->type != I40E_VSI_FCOE)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001247 aq_ret = I40E_ERR_PARAM;
1248 goto error_param;
1249 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001250 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1251 allmulti = true;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001252 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001253 allmulti, NULL);
1254
1255error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001256 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001257 return i40e_vc_send_resp_to_vf(vf,
1258 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1259 aq_ret);
1260}
1261
1262/**
1263 * i40e_vc_config_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001264 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001265 * @msg: pointer to the msg buffer
1266 * @msglen: msg length
1267 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001268 * called from the VF to configure the rx/tx
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001269 * queues
1270 **/
1271static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1272{
1273 struct i40e_virtchnl_vsi_queue_config_info *qci =
1274 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1275 struct i40e_virtchnl_queue_pair_info *qpi;
Ashish Shah5f5e33b2014-07-10 07:58:15 +00001276 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001277 u16 vsi_id, vsi_queue_id;
1278 i40e_status aq_ret = 0;
1279 int i;
1280
1281 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1282 aq_ret = I40E_ERR_PARAM;
1283 goto error_param;
1284 }
1285
1286 vsi_id = qci->vsi_id;
1287 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1288 aq_ret = I40E_ERR_PARAM;
1289 goto error_param;
1290 }
1291 for (i = 0; i < qci->num_queue_pairs; i++) {
1292 qpi = &qci->qpair[i];
1293 vsi_queue_id = qpi->txq.queue_id;
1294 if ((qpi->txq.vsi_id != vsi_id) ||
1295 (qpi->rxq.vsi_id != vsi_id) ||
1296 (qpi->rxq.queue_id != vsi_queue_id) ||
1297 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1298 aq_ret = I40E_ERR_PARAM;
1299 goto error_param;
1300 }
1301
1302 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1303 &qpi->rxq) ||
1304 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1305 &qpi->txq)) {
1306 aq_ret = I40E_ERR_PARAM;
1307 goto error_param;
1308 }
1309 }
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001310 /* set vsi num_queue_pairs in use to num configured by VF */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001311 pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001312
1313error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001314 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001315 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1316 aq_ret);
1317}
1318
1319/**
1320 * i40e_vc_config_irq_map_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001321 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001322 * @msg: pointer to the msg buffer
1323 * @msglen: msg length
1324 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001325 * called from the VF to configure the irq to
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001326 * queue map
1327 **/
1328static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1329{
1330 struct i40e_virtchnl_irq_map_info *irqmap_info =
1331 (struct i40e_virtchnl_irq_map_info *)msg;
1332 struct i40e_virtchnl_vector_map *map;
1333 u16 vsi_id, vsi_queue_id, vector_id;
1334 i40e_status aq_ret = 0;
1335 unsigned long tempmap;
1336 int i;
1337
1338 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1339 aq_ret = I40E_ERR_PARAM;
1340 goto error_param;
1341 }
1342
1343 for (i = 0; i < irqmap_info->num_vectors; i++) {
1344 map = &irqmap_info->vecmap[i];
1345
1346 vector_id = map->vector_id;
1347 vsi_id = map->vsi_id;
1348 /* validate msg params */
1349 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1350 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1351 aq_ret = I40E_ERR_PARAM;
1352 goto error_param;
1353 }
1354
1355 /* lookout for the invalid queue index */
1356 tempmap = map->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001357 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001358 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1359 vsi_queue_id)) {
1360 aq_ret = I40E_ERR_PARAM;
1361 goto error_param;
1362 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001363 }
1364
1365 tempmap = map->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001366 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001367 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1368 vsi_queue_id)) {
1369 aq_ret = I40E_ERR_PARAM;
1370 goto error_param;
1371 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001372 }
1373
1374 i40e_config_irq_link_list(vf, vsi_id, map);
1375 }
1376error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001377 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001378 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1379 aq_ret);
1380}
1381
1382/**
1383 * i40e_vc_enable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001384 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001385 * @msg: pointer to the msg buffer
1386 * @msglen: msg length
1387 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001388 * called from the VF to enable all or specific queue(s)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001389 **/
1390static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1391{
1392 struct i40e_virtchnl_queue_select *vqs =
1393 (struct i40e_virtchnl_queue_select *)msg;
1394 struct i40e_pf *pf = vf->pf;
1395 u16 vsi_id = vqs->vsi_id;
1396 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001397
1398 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1399 aq_ret = I40E_ERR_PARAM;
1400 goto error_param;
1401 }
1402
1403 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1404 aq_ret = I40E_ERR_PARAM;
1405 goto error_param;
1406 }
1407
1408 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1409 aq_ret = I40E_ERR_PARAM;
1410 goto error_param;
1411 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001412
1413 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], true))
Mitch Williams88f65632013-11-28 06:39:28 +00001414 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001415error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001416 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001417 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1418 aq_ret);
1419}
1420
1421/**
1422 * i40e_vc_disable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001423 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001424 * @msg: pointer to the msg buffer
1425 * @msglen: msg length
1426 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001427 * called from the VF to disable all or specific
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001428 * queue(s)
1429 **/
1430static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1431{
1432 struct i40e_virtchnl_queue_select *vqs =
1433 (struct i40e_virtchnl_queue_select *)msg;
1434 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001435 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001436
1437 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1438 aq_ret = I40E_ERR_PARAM;
1439 goto error_param;
1440 }
1441
1442 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1443 aq_ret = I40E_ERR_PARAM;
1444 goto error_param;
1445 }
1446
1447 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1448 aq_ret = I40E_ERR_PARAM;
1449 goto error_param;
1450 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001451
1452 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false))
Mitch Williams88f65632013-11-28 06:39:28 +00001453 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001454
1455error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001456 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001457 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1458 aq_ret);
1459}
1460
1461/**
1462 * i40e_vc_get_stats_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001463 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001464 * @msg: pointer to the msg buffer
1465 * @msglen: msg length
1466 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001467 * called from the VF to get vsi stats
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001468 **/
1469static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1470{
1471 struct i40e_virtchnl_queue_select *vqs =
1472 (struct i40e_virtchnl_queue_select *)msg;
1473 struct i40e_pf *pf = vf->pf;
1474 struct i40e_eth_stats stats;
1475 i40e_status aq_ret = 0;
1476 struct i40e_vsi *vsi;
1477
1478 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1479
1480 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1481 aq_ret = I40E_ERR_PARAM;
1482 goto error_param;
1483 }
1484
1485 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1486 aq_ret = I40E_ERR_PARAM;
1487 goto error_param;
1488 }
1489
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001490 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001491 if (!vsi) {
1492 aq_ret = I40E_ERR_PARAM;
1493 goto error_param;
1494 }
1495 i40e_update_eth_stats(vsi);
Mitch Williams5a9769c2013-11-28 06:39:38 +00001496 stats = vsi->eth_stats;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001497
1498error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001499 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001500 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1501 (u8 *)&stats, sizeof(stats));
1502}
1503
1504/**
Greg Rosef657a6e2013-11-28 06:39:42 +00001505 * i40e_check_vf_permission
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001506 * @vf: pointer to the VF info
Greg Rosef657a6e2013-11-28 06:39:42 +00001507 * @macaddr: pointer to the MAC Address being checked
1508 *
1509 * Check if the VF has permission to add or delete unicast MAC address
1510 * filters and return error code -EPERM if not. Then check if the
1511 * address filter requested is broadcast or zero and if so return
1512 * an invalid MAC address error code.
1513 **/
1514static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1515{
1516 struct i40e_pf *pf = vf->pf;
1517 int ret = 0;
1518
1519 if (is_broadcast_ether_addr(macaddr) ||
1520 is_zero_ether_addr(macaddr)) {
1521 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1522 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rose5017c2a2013-12-07 10:36:54 +00001523 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1524 !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001525 /* If the host VMM administrator has set the VF MAC address
1526 * administratively via the ndo_set_vf_mac command then deny
1527 * permission to the VF to add or delete unicast MAC addresses.
Greg Rose5017c2a2013-12-07 10:36:54 +00001528 * The VF may request to set the MAC address filter already
1529 * assigned to it so do not return an error in that case.
Greg Rosef657a6e2013-11-28 06:39:42 +00001530 */
1531 dev_err(&pf->pdev->dev,
1532 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1533 ret = -EPERM;
1534 }
1535 return ret;
1536}
1537
1538/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001539 * i40e_vc_add_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001540 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001541 * @msg: pointer to the msg buffer
1542 * @msglen: msg length
1543 *
1544 * add guest mac address filter
1545 **/
1546static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1547{
1548 struct i40e_virtchnl_ether_addr_list *al =
1549 (struct i40e_virtchnl_ether_addr_list *)msg;
1550 struct i40e_pf *pf = vf->pf;
1551 struct i40e_vsi *vsi = NULL;
1552 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001553 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001554 int i;
1555
1556 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1557 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1558 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001559 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001560 goto error_param;
1561 }
1562
1563 for (i = 0; i < al->num_elements; i++) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001564 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1565 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001566 goto error_param;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001567 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001568 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001569
1570 /* add new addresses to the list */
1571 for (i = 0; i < al->num_elements; i++) {
1572 struct i40e_mac_filter *f;
1573
1574 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
Mitch Williams7e68edf92013-11-16 10:00:41 +00001575 if (!f) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001576 if (i40e_is_vsi_in_vlan(vsi))
1577 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1578 true, false);
1579 else
1580 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1581 true, false);
1582 }
1583
1584 if (!f) {
1585 dev_err(&pf->pdev->dev,
1586 "Unable to add VF MAC filter\n");
Greg Rosef657a6e2013-11-28 06:39:42 +00001587 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001588 goto error_param;
1589 }
1590 }
1591
1592 /* program the updated filter list */
1593 if (i40e_sync_vsi_filters(vsi))
1594 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1595
1596error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001597 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001598 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001599 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001600}
1601
1602/**
1603 * i40e_vc_del_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001604 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001605 * @msg: pointer to the msg buffer
1606 * @msglen: msg length
1607 *
1608 * remove guest mac address filter
1609 **/
1610static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1611{
1612 struct i40e_virtchnl_ether_addr_list *al =
1613 (struct i40e_virtchnl_ether_addr_list *)msg;
1614 struct i40e_pf *pf = vf->pf;
1615 struct i40e_vsi *vsi = NULL;
1616 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001617 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001618 int i;
1619
1620 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1621 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1622 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001623 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001624 goto error_param;
1625 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001626
1627 for (i = 0; i < al->num_elements; i++) {
Mitch Williams700bbf62013-12-21 05:44:45 +00001628 if (is_broadcast_ether_addr(al->list[i].addr) ||
1629 is_zero_ether_addr(al->list[i].addr)) {
1630 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
1631 al->list[i].addr);
1632 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rosef657a6e2013-11-28 06:39:42 +00001633 goto error_param;
Mitch Williams700bbf62013-12-21 05:44:45 +00001634 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001635 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001636 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001637
1638 /* delete addresses from the list */
1639 for (i = 0; i < al->num_elements; i++)
1640 i40e_del_filter(vsi, al->list[i].addr,
1641 I40E_VLAN_ANY, true, false);
1642
1643 /* program the updated filter list */
1644 if (i40e_sync_vsi_filters(vsi))
1645 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1646
1647error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001648 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001649 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001650 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001651}
1652
1653/**
1654 * i40e_vc_add_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001655 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001656 * @msg: pointer to the msg buffer
1657 * @msglen: msg length
1658 *
1659 * program guest vlan id
1660 **/
1661static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1662{
1663 struct i40e_virtchnl_vlan_filter_list *vfl =
1664 (struct i40e_virtchnl_vlan_filter_list *)msg;
1665 struct i40e_pf *pf = vf->pf;
1666 struct i40e_vsi *vsi = NULL;
1667 u16 vsi_id = vfl->vsi_id;
1668 i40e_status aq_ret = 0;
1669 int i;
1670
1671 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1672 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1673 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1674 aq_ret = I40E_ERR_PARAM;
1675 goto error_param;
1676 }
1677
1678 for (i = 0; i < vfl->num_elements; i++) {
1679 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1680 aq_ret = I40E_ERR_PARAM;
1681 dev_err(&pf->pdev->dev,
1682 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1683 goto error_param;
1684 }
1685 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001686 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001687 if (vsi->info.pvid) {
1688 aq_ret = I40E_ERR_PARAM;
1689 goto error_param;
1690 }
1691
1692 i40e_vlan_stripping_enable(vsi);
1693 for (i = 0; i < vfl->num_elements; i++) {
1694 /* add new VLAN filter */
1695 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1696 if (ret)
1697 dev_err(&pf->pdev->dev,
1698 "Unable to add VF vlan filter %d, error %d\n",
1699 vfl->vlan_id[i], ret);
1700 }
1701
1702error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001703 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001704 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1705}
1706
1707/**
1708 * i40e_vc_remove_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001709 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001710 * @msg: pointer to the msg buffer
1711 * @msglen: msg length
1712 *
1713 * remove programmed guest vlan id
1714 **/
1715static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1716{
1717 struct i40e_virtchnl_vlan_filter_list *vfl =
1718 (struct i40e_virtchnl_vlan_filter_list *)msg;
1719 struct i40e_pf *pf = vf->pf;
1720 struct i40e_vsi *vsi = NULL;
1721 u16 vsi_id = vfl->vsi_id;
1722 i40e_status aq_ret = 0;
1723 int i;
1724
1725 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1726 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1727 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1728 aq_ret = I40E_ERR_PARAM;
1729 goto error_param;
1730 }
1731
1732 for (i = 0; i < vfl->num_elements; i++) {
1733 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1734 aq_ret = I40E_ERR_PARAM;
1735 goto error_param;
1736 }
1737 }
1738
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001739 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001740 if (vsi->info.pvid) {
1741 aq_ret = I40E_ERR_PARAM;
1742 goto error_param;
1743 }
1744
1745 for (i = 0; i < vfl->num_elements; i++) {
1746 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1747 if (ret)
1748 dev_err(&pf->pdev->dev,
1749 "Unable to delete VF vlan filter %d, error %d\n",
1750 vfl->vlan_id[i], ret);
1751 }
1752
1753error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001754 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001755 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1756}
1757
1758/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001759 * i40e_vc_validate_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001760 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001761 * @msg: pointer to the msg buffer
1762 * @msglen: msg length
1763 * @msghndl: msg handle
1764 *
1765 * validate msg
1766 **/
1767static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1768 u32 v_retval, u8 *msg, u16 msglen)
1769{
1770 bool err_msg_format = false;
1771 int valid_len;
1772
1773 /* Check if VF is disabled. */
1774 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1775 return I40E_ERR_PARAM;
1776
1777 /* Validate message length. */
1778 switch (v_opcode) {
1779 case I40E_VIRTCHNL_OP_VERSION:
1780 valid_len = sizeof(struct i40e_virtchnl_version_info);
1781 break;
1782 case I40E_VIRTCHNL_OP_RESET_VF:
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001783 valid_len = 0;
1784 break;
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001785 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1786 if (VF_IS_V11(vf))
1787 valid_len = sizeof(u32);
1788 else
1789 valid_len = 0;
1790 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001791 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1792 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1793 break;
1794 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1795 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1796 break;
1797 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1798 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1799 if (msglen >= valid_len) {
1800 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1801 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1802 valid_len += (vqc->num_queue_pairs *
1803 sizeof(struct
1804 i40e_virtchnl_queue_pair_info));
1805 if (vqc->num_queue_pairs == 0)
1806 err_msg_format = true;
1807 }
1808 break;
1809 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1810 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1811 if (msglen >= valid_len) {
1812 struct i40e_virtchnl_irq_map_info *vimi =
1813 (struct i40e_virtchnl_irq_map_info *)msg;
1814 valid_len += (vimi->num_vectors *
1815 sizeof(struct i40e_virtchnl_vector_map));
1816 if (vimi->num_vectors == 0)
1817 err_msg_format = true;
1818 }
1819 break;
1820 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1821 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1822 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1823 break;
1824 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1825 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1826 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1827 if (msglen >= valid_len) {
1828 struct i40e_virtchnl_ether_addr_list *veal =
1829 (struct i40e_virtchnl_ether_addr_list *)msg;
1830 valid_len += veal->num_elements *
1831 sizeof(struct i40e_virtchnl_ether_addr);
1832 if (veal->num_elements == 0)
1833 err_msg_format = true;
1834 }
1835 break;
1836 case I40E_VIRTCHNL_OP_ADD_VLAN:
1837 case I40E_VIRTCHNL_OP_DEL_VLAN:
1838 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1839 if (msglen >= valid_len) {
1840 struct i40e_virtchnl_vlan_filter_list *vfl =
1841 (struct i40e_virtchnl_vlan_filter_list *)msg;
1842 valid_len += vfl->num_elements * sizeof(u16);
1843 if (vfl->num_elements == 0)
1844 err_msg_format = true;
1845 }
1846 break;
1847 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1848 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1849 break;
1850 case I40E_VIRTCHNL_OP_GET_STATS:
1851 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1852 break;
1853 /* These are always errors coming from the VF. */
1854 case I40E_VIRTCHNL_OP_EVENT:
1855 case I40E_VIRTCHNL_OP_UNKNOWN:
1856 default:
1857 return -EPERM;
1858 break;
1859 }
1860 /* few more checks */
1861 if ((valid_len != msglen) || (err_msg_format)) {
1862 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1863 return -EINVAL;
1864 } else {
1865 return 0;
1866 }
1867}
1868
1869/**
1870 * i40e_vc_process_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001871 * @pf: pointer to the PF structure
1872 * @vf_id: source VF id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001873 * @msg: pointer to the msg buffer
1874 * @msglen: msg length
1875 * @msghndl: msg handle
1876 *
1877 * called from the common aeq/arq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001878 * process request from VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001879 **/
1880int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1881 u32 v_retval, u8 *msg, u16 msglen)
1882{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001883 struct i40e_hw *hw = &pf->hw;
Dan Carpenterc243e962014-01-15 06:43:39 +00001884 unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001885 struct i40e_vf *vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001886 int ret;
1887
1888 pf->vf_aq_requests++;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001889 if (local_vf_id >= pf->num_alloc_vfs)
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001890 return -EINVAL;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001891 vf = &(pf->vf[local_vf_id]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001892 /* perform basic checks on the msg */
1893 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1894
1895 if (ret) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001896 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001897 local_vf_id, v_opcode, msglen);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001898 return ret;
1899 }
Mitch Williamsbae3cae2014-01-14 00:49:49 -08001900
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001901 switch (v_opcode) {
1902 case I40E_VIRTCHNL_OP_VERSION:
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001903 ret = i40e_vc_get_version_msg(vf, msg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001904 break;
1905 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
Mitch Williamsf4ca1a22015-06-04 16:23:57 -04001906 ret = i40e_vc_get_vf_resources_msg(vf, msg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001907 break;
1908 case I40E_VIRTCHNL_OP_RESET_VF:
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001909 i40e_vc_reset_vf_msg(vf);
1910 ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001911 break;
1912 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1913 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1914 break;
1915 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1916 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1917 break;
1918 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1919 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1920 break;
1921 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1922 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
Mitch Williams055b2952015-04-07 19:45:33 -04001923 i40e_vc_notify_vf_link_state(vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001924 break;
1925 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1926 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1927 break;
1928 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1929 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1930 break;
1931 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1932 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1933 break;
1934 case I40E_VIRTCHNL_OP_ADD_VLAN:
1935 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1936 break;
1937 case I40E_VIRTCHNL_OP_DEL_VLAN:
1938 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1939 break;
1940 case I40E_VIRTCHNL_OP_GET_STATS:
1941 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1942 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001943 case I40E_VIRTCHNL_OP_UNKNOWN:
1944 default:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001945 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001946 v_opcode, local_vf_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001947 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1948 I40E_ERR_NOT_IMPLEMENTED);
1949 break;
1950 }
1951
1952 return ret;
1953}
1954
1955/**
1956 * i40e_vc_process_vflr_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001957 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001958 *
1959 * called from the vlfr irq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001960 * free up VF resources and state variables
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001961 **/
1962int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1963{
1964 u32 reg, reg_idx, bit_idx, vf_id;
1965 struct i40e_hw *hw = &pf->hw;
1966 struct i40e_vf *vf;
1967
1968 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1969 return 0;
1970
Mitch Williamsc5c2f7c2014-11-11 03:15:04 +00001971 /* re-enable vflr interrupt cause */
1972 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1973 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1974 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1975 i40e_flush(hw);
1976
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001977 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1978 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1979 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1980 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001981 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001982 vf = &pf->vf[vf_id];
1983 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001984 if (reg & BIT(bit_idx)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001985 /* clear the bit in GLGEN_VFLRSTAT */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -04001986 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001987
Mitch Williamseb2d80b2014-02-13 03:48:48 -08001988 if (!test_bit(__I40E_DOWN, &pf->state))
1989 i40e_reset_vf(vf, true);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001990 }
1991 }
1992
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001993 return 0;
1994}
1995
1996/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001997 * i40e_ndo_set_vf_mac
1998 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001999 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002000 * @mac: mac address
2001 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002002 * program VF mac address
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002003 **/
2004int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2005{
2006 struct i40e_netdev_priv *np = netdev_priv(netdev);
2007 struct i40e_vsi *vsi = np->vsi;
2008 struct i40e_pf *pf = vsi->back;
2009 struct i40e_mac_filter *f;
2010 struct i40e_vf *vf;
2011 int ret = 0;
2012
2013 /* validate the request */
2014 if (vf_id >= pf->num_alloc_vfs) {
2015 dev_err(&pf->pdev->dev,
2016 "Invalid VF Identifier %d\n", vf_id);
2017 ret = -EINVAL;
2018 goto error_param;
2019 }
2020
2021 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002022 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002023 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2024 dev_err(&pf->pdev->dev,
2025 "Uninitialized VF %d\n", vf_id);
2026 ret = -EINVAL;
2027 goto error_param;
2028 }
2029
2030 if (!is_valid_ether_addr(mac)) {
2031 dev_err(&pf->pdev->dev,
2032 "Invalid VF ethernet address\n");
2033 ret = -EINVAL;
2034 goto error_param;
2035 }
2036
2037 /* delete the temporary mac address */
Mitch Williamse9951632015-04-27 14:57:13 -04002038 i40e_del_filter(vsi, vf->default_lan_addr.addr,
2039 vf->port_vlan_id ? vf->port_vlan_id : -1,
Greg Rose37cc0d22014-04-01 07:11:44 +00002040 true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002041
Greg Rose29f71bb2014-05-20 08:01:45 +00002042 /* Delete all the filters for this VSI - we're going to kill it
2043 * anyway.
2044 */
2045 list_for_each_entry(f, &vsi->mac_filter_list, list)
2046 i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002047
2048 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2049 /* program mac filter */
2050 if (i40e_sync_vsi_filters(vsi)) {
2051 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2052 ret = -EIO;
2053 goto error_param;
2054 }
Greg Rose9a173902014-05-22 06:32:02 +00002055 ether_addr_copy(vf->default_lan_addr.addr, mac);
Greg Rosef657a6e2013-11-28 06:39:42 +00002056 vf->pf_set_mac = true;
Greg Rose17413a82014-06-04 01:23:13 +00002057 /* Force the VF driver stop so it has to reload with new MAC address */
2058 i40e_vc_disable_vf(pf, vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002059 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002060
2061error_param:
2062 return ret;
2063}
2064
2065/**
2066 * i40e_ndo_set_vf_port_vlan
2067 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002068 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002069 * @vlan_id: mac address
2070 * @qos: priority setting
2071 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002072 * program VF vlan id and/or qos
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002073 **/
2074int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2075 int vf_id, u16 vlan_id, u8 qos)
2076{
2077 struct i40e_netdev_priv *np = netdev_priv(netdev);
2078 struct i40e_pf *pf = np->vsi->back;
2079 struct i40e_vsi *vsi;
2080 struct i40e_vf *vf;
2081 int ret = 0;
2082
2083 /* validate the request */
2084 if (vf_id >= pf->num_alloc_vfs) {
2085 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2086 ret = -EINVAL;
2087 goto error_pvid;
2088 }
2089
2090 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2091 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2092 ret = -EINVAL;
2093 goto error_pvid;
2094 }
2095
2096 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002097 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002098 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2099 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2100 ret = -EINVAL;
2101 goto error_pvid;
2102 }
2103
Mitch Williams85927ec2015-04-27 14:57:10 -04002104 if (vsi->info.pvid == (vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT)))
2105 /* duplicate request, so just return success */
2106 goto error_pvid;
2107
Greg Rosef9b4b622014-03-06 09:02:28 +00002108 if (vsi->info.pvid == 0 && i40e_is_vsi_in_vlan(vsi)) {
Greg Rose99a49732014-01-13 16:13:03 -08002109 dev_err(&pf->pdev->dev,
2110 "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",
2111 vf_id);
Greg Rosef9b4b622014-03-06 09:02:28 +00002112 /* Administrator Error - knock the VF offline until he does
2113 * the right thing by reconfiguring his network correctly
2114 * and then reloading the VF driver.
2115 */
2116 i40e_vc_disable_vf(pf, vf);
2117 }
Greg Rose99a49732014-01-13 16:13:03 -08002118
Greg Rose8d82a7c2014-01-13 16:13:04 -08002119 /* Check for condition where there was already a port VLAN ID
2120 * filter set and now it is being deleted by setting it to zero.
Greg Rose1315f7c2014-03-14 07:32:20 +00002121 * Additionally check for the condition where there was a port
2122 * VLAN but now there is a new and different port VLAN being set.
Greg Rose8d82a7c2014-01-13 16:13:04 -08002123 * Before deleting all the old VLAN filters we must add new ones
2124 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2125 * MAC addresses deleted.
2126 */
Greg Rose1315f7c2014-03-14 07:32:20 +00002127 if ((!(vlan_id || qos) ||
2128 (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
2129 vsi->info.pvid)
Greg Rose8d82a7c2014-01-13 16:13:04 -08002130 ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2131
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002132 if (vsi->info.pvid) {
2133 /* kill old VLAN */
2134 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2135 VLAN_VID_MASK));
2136 if (ret) {
2137 dev_info(&vsi->back->pdev->dev,
2138 "remove VLAN failed, ret=%d, aq_err=%d\n",
2139 ret, pf->hw.aq.asq_last_status);
2140 }
2141 }
2142 if (vlan_id || qos)
2143 ret = i40e_vsi_add_pvid(vsi,
2144 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
2145 else
Greg Rose6c12fcb2013-11-28 06:39:34 +00002146 i40e_vsi_remove_pvid(vsi);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002147
2148 if (vlan_id) {
2149 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2150 vlan_id, qos, vf_id);
2151
2152 /* add new VLAN filter */
2153 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2154 if (ret) {
2155 dev_info(&vsi->back->pdev->dev,
2156 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2157 vsi->back->hw.aq.asq_last_status);
2158 goto error_pvid;
2159 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002160 /* Kill non-vlan MAC filters - ignore error return since
2161 * there might not be any non-vlan MAC filters.
2162 */
2163 i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002164 }
2165
2166 if (ret) {
2167 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2168 goto error_pvid;
2169 }
Greg Rose6c12fcb2013-11-28 06:39:34 +00002170 /* The Port VLAN needs to be saved across resets the same as the
2171 * default LAN MAC address.
2172 */
2173 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002174 ret = 0;
2175
2176error_pvid:
2177 return ret;
2178}
2179
Mitch Williams84590fd2014-04-09 05:58:57 +00002180#define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
2181#define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002182/**
2183 * i40e_ndo_set_vf_bw
2184 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002185 * @vf_id: VF identifier
2186 * @tx_rate: Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002187 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002188 * configure VF Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002189 **/
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002190int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2191 int max_tx_rate)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002192{
Mitch Williams6b192892014-03-06 09:02:29 +00002193 struct i40e_netdev_priv *np = netdev_priv(netdev);
2194 struct i40e_pf *pf = np->vsi->back;
2195 struct i40e_vsi *vsi;
2196 struct i40e_vf *vf;
2197 int speed = 0;
2198 int ret = 0;
2199
2200 /* validate the request */
2201 if (vf_id >= pf->num_alloc_vfs) {
2202 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2203 ret = -EINVAL;
2204 goto error;
2205 }
2206
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002207 if (min_tx_rate) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002208 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 -04002209 min_tx_rate, vf_id);
2210 return -EINVAL;
2211 }
2212
Mitch Williams6b192892014-03-06 09:02:29 +00002213 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002214 vsi = pf->vsi[vf->lan_vsi_idx];
Mitch Williams6b192892014-03-06 09:02:29 +00002215 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2216 dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
2217 ret = -EINVAL;
2218 goto error;
2219 }
2220
2221 switch (pf->hw.phy.link_info.link_speed) {
2222 case I40E_LINK_SPEED_40GB:
2223 speed = 40000;
2224 break;
2225 case I40E_LINK_SPEED_10GB:
2226 speed = 10000;
2227 break;
2228 case I40E_LINK_SPEED_1GB:
2229 speed = 1000;
2230 break;
2231 default:
2232 break;
2233 }
2234
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002235 if (max_tx_rate > speed) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002236 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002237 max_tx_rate, vf->vf_id);
Mitch Williams6b192892014-03-06 09:02:29 +00002238 ret = -EINVAL;
2239 goto error;
2240 }
2241
Mitch Williamsdac9b312014-04-09 05:58:56 +00002242 if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2243 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2244 max_tx_rate = 50;
2245 }
2246
Mitch Williams6b192892014-03-06 09:02:29 +00002247 /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
Mitch Williams84590fd2014-04-09 05:58:57 +00002248 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2249 max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2250 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
Mitch Williams6b192892014-03-06 09:02:29 +00002251 if (ret) {
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002252 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
Mitch Williams6b192892014-03-06 09:02:29 +00002253 ret);
2254 ret = -EIO;
2255 goto error;
2256 }
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002257 vf->tx_rate = max_tx_rate;
Mitch Williams6b192892014-03-06 09:02:29 +00002258error:
2259 return ret;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002260}
2261
2262/**
2263 * i40e_ndo_get_vf_config
2264 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002265 * @vf_id: VF identifier
2266 * @ivi: VF configuration structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002267 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002268 * return VF configuration
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002269 **/
2270int i40e_ndo_get_vf_config(struct net_device *netdev,
2271 int vf_id, struct ifla_vf_info *ivi)
2272{
2273 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002274 struct i40e_vsi *vsi = np->vsi;
2275 struct i40e_pf *pf = vsi->back;
2276 struct i40e_vf *vf;
2277 int ret = 0;
2278
2279 /* validate the request */
2280 if (vf_id >= pf->num_alloc_vfs) {
2281 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2282 ret = -EINVAL;
2283 goto error_param;
2284 }
2285
2286 vf = &(pf->vf[vf_id]);
2287 /* first vsi is always the LAN vsi */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002288 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002289 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2290 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2291 ret = -EINVAL;
2292 goto error_param;
2293 }
2294
2295 ivi->vf = vf_id;
2296
Mitch Williamsf4a1c5c2013-11-28 06:39:34 +00002297 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002298
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002299 ivi->max_tx_rate = vf->tx_rate;
2300 ivi->min_tx_rate = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002301 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2302 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2303 I40E_VLAN_PRIORITY_SHIFT;
Mitch Williams84ca55a2014-03-14 07:32:24 +00002304 if (vf->link_forced == false)
2305 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2306 else if (vf->link_up == true)
2307 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2308 else
2309 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
Mitch Williamsc674d122014-05-20 08:01:40 +00002310 ivi->spoofchk = vf->spoofchk;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002311 ret = 0;
2312
2313error_param:
2314 return ret;
2315}
Mitch Williams588aefa2014-02-11 08:27:49 +00002316
2317/**
2318 * i40e_ndo_set_vf_link_state
2319 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002320 * @vf_id: VF identifier
Mitch Williams588aefa2014-02-11 08:27:49 +00002321 * @link: required link state
2322 *
2323 * Set the link state of a specified VF, regardless of physical link state
2324 **/
2325int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
2326{
2327 struct i40e_netdev_priv *np = netdev_priv(netdev);
2328 struct i40e_pf *pf = np->vsi->back;
2329 struct i40e_virtchnl_pf_event pfe;
2330 struct i40e_hw *hw = &pf->hw;
2331 struct i40e_vf *vf;
Ashish Shahf19efbb2014-08-01 13:27:06 -07002332 int abs_vf_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002333 int ret = 0;
2334
2335 /* validate the request */
2336 if (vf_id >= pf->num_alloc_vfs) {
2337 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2338 ret = -EINVAL;
2339 goto error_out;
2340 }
2341
2342 vf = &pf->vf[vf_id];
Ashish Shahf19efbb2014-08-01 13:27:06 -07002343 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002344
2345 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
2346 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
2347
2348 switch (link) {
2349 case IFLA_VF_LINK_STATE_AUTO:
2350 vf->link_forced = false;
2351 pfe.event_data.link_event.link_status =
2352 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
2353 pfe.event_data.link_event.link_speed =
2354 pf->hw.phy.link_info.link_speed;
2355 break;
2356 case IFLA_VF_LINK_STATE_ENABLE:
2357 vf->link_forced = true;
2358 vf->link_up = true;
2359 pfe.event_data.link_event.link_status = true;
2360 pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
2361 break;
2362 case IFLA_VF_LINK_STATE_DISABLE:
2363 vf->link_forced = true;
2364 vf->link_up = false;
2365 pfe.event_data.link_event.link_status = false;
2366 pfe.event_data.link_event.link_speed = 0;
2367 break;
2368 default:
2369 ret = -EINVAL;
2370 goto error_out;
2371 }
2372 /* Notify the VF of its new link state */
Ashish Shahf19efbb2014-08-01 13:27:06 -07002373 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Mitch Williams588aefa2014-02-11 08:27:49 +00002374 0, (u8 *)&pfe, sizeof(pfe), NULL);
2375
2376error_out:
2377 return ret;
2378}
Mitch Williamsc674d122014-05-20 08:01:40 +00002379
2380/**
2381 * i40e_ndo_set_vf_spoofchk
2382 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002383 * @vf_id: VF identifier
Mitch Williamsc674d122014-05-20 08:01:40 +00002384 * @enable: flag to enable or disable feature
2385 *
2386 * Enable or disable VF spoof checking
2387 **/
Serey Konge6d90042014-07-12 07:28:14 +00002388int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
Mitch Williamsc674d122014-05-20 08:01:40 +00002389{
2390 struct i40e_netdev_priv *np = netdev_priv(netdev);
2391 struct i40e_vsi *vsi = np->vsi;
2392 struct i40e_pf *pf = vsi->back;
2393 struct i40e_vsi_context ctxt;
2394 struct i40e_hw *hw = &pf->hw;
2395 struct i40e_vf *vf;
2396 int ret = 0;
2397
2398 /* validate the request */
2399 if (vf_id >= pf->num_alloc_vfs) {
2400 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2401 ret = -EINVAL;
2402 goto out;
2403 }
2404
2405 vf = &(pf->vf[vf_id]);
2406
2407 if (enable == vf->spoofchk)
2408 goto out;
2409
2410 vf->spoofchk = enable;
2411 memset(&ctxt, 0, sizeof(ctxt));
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002412 ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
Mitch Williamsc674d122014-05-20 08:01:40 +00002413 ctxt.pf_num = pf->hw.pf_id;
2414 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
2415 if (enable)
Greg Rose30d71af2015-01-29 07:17:17 +00002416 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
2417 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
Mitch Williamsc674d122014-05-20 08:01:40 +00002418 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
2419 if (ret) {
2420 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
2421 ret);
2422 ret = -EIO;
2423 }
2424out:
2425 return ret;
2426}