blob: 98e09219ffb7fd6bbc12b3ef1db7247088e0c29b [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{
163 struct i40e_hw *hw = &pf->hw;
164 u32 reg;
165
166 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
167 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
168 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
169 i40e_flush(hw);
170}
171
172/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000173 * i40e_vc_isvalid_vsi_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000174 * @vf: pointer to the VF info
175 * @vsi_id: VF relative VSI id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000176 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000177 * check for the valid VSI id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000178 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700179static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000180{
181 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700182 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000183
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700184 return (vsi && (vsi->vf_id == vf->vf_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000185}
186
187/**
188 * i40e_vc_isvalid_queue_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000189 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000190 * @vsi_id: vsi id
191 * @qid: vsi relative queue id
192 *
193 * check for the valid queue id
194 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700195static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000196 u8 qid)
197{
198 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700199 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000200
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700201 return (vsi && (qid < vsi->alloc_queue_pairs));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000202}
203
204/**
205 * i40e_vc_isvalid_vector_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000206 * @vf: pointer to the VF info
207 * @vector_id: VF relative vector id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000208 *
209 * check for the valid vector id
210 **/
211static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
212{
213 struct i40e_pf *pf = vf->pf;
214
Mitch Williams9347eb72014-02-11 08:26:32 +0000215 return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000216}
217
218/***********************vf resource mgmt routines*****************/
219
220/**
221 * i40e_vc_get_pf_queue_id
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000222 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700223 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000224 * @vsi_queue_id: vsi relative queue id
225 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000226 * return PF relative queue id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000227 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700228static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000229 u8 vsi_queue_id)
230{
231 struct i40e_pf *pf = vf->pf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700232 struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000233 u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
234
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700235 if (!vsi)
236 return pf_queue_id;
237
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000238 if (le16_to_cpu(vsi->info.mapping_flags) &
239 I40E_AQ_VSI_QUE_MAP_NONCONTIG)
240 pf_queue_id =
241 le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
242 else
243 pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
244 vsi_queue_id;
245
246 return pf_queue_id;
247}
248
249/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000250 * i40e_config_irq_link_list
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000251 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700252 * @vsi_id: id of VSI as given by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000253 * @vecmap: irq map info
254 *
255 * configure irq link list from the map
256 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700257static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000258 struct i40e_virtchnl_vector_map *vecmap)
259{
260 unsigned long linklistmap = 0, tempmap;
261 struct i40e_pf *pf = vf->pf;
262 struct i40e_hw *hw = &pf->hw;
263 u16 vsi_queue_id, pf_queue_id;
264 enum i40e_queue_type qtype;
265 u16 next_q, vector_id;
266 u32 reg, reg_idx;
267 u16 itr_idx = 0;
268
269 vector_id = vecmap->vector_id;
270 /* setup the head */
271 if (0 == vector_id)
272 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
273 else
274 reg_idx = I40E_VPINT_LNKLSTN(
Mitch Williams9347eb72014-02-11 08:26:32 +0000275 ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
276 (vector_id - 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000277
278 if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
279 /* Special case - No queues mapped on this vector */
280 wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
281 goto irq_list_done;
282 }
283 tempmap = vecmap->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000284 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000285 linklistmap |= (1 <<
286 (I40E_VIRTCHNL_SUPPORTED_QTYPES *
287 vsi_queue_id));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000288 }
289
290 tempmap = vecmap->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +0000291 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000292 linklistmap |= (1 <<
293 (I40E_VIRTCHNL_SUPPORTED_QTYPES * vsi_queue_id
294 + 1));
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000295 }
296
297 next_q = find_first_bit(&linklistmap,
298 (I40E_MAX_VSI_QP *
299 I40E_VIRTCHNL_SUPPORTED_QTYPES));
300 vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
301 qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700302 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000303 reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
304
305 wr32(hw, reg_idx, reg);
306
307 while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
308 switch (qtype) {
309 case I40E_QUEUE_TYPE_RX:
310 reg_idx = I40E_QINT_RQCTL(pf_queue_id);
311 itr_idx = vecmap->rxitr_idx;
312 break;
313 case I40E_QUEUE_TYPE_TX:
314 reg_idx = I40E_QINT_TQCTL(pf_queue_id);
315 itr_idx = vecmap->txitr_idx;
316 break;
317 default:
318 break;
319 }
320
321 next_q = find_next_bit(&linklistmap,
322 (I40E_MAX_VSI_QP *
323 I40E_VIRTCHNL_SUPPORTED_QTYPES),
324 next_q + 1);
Mitch Williams829af3a2013-12-18 13:46:00 +0000325 if (next_q <
326 (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000327 vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
328 qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700329 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000330 vsi_queue_id);
331 } else {
332 pf_queue_id = I40E_QUEUE_END_OF_LIST;
333 qtype = 0;
334 }
335
336 /* format for the RQCTL & TQCTL regs is same */
337 reg = (vector_id) |
338 (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
339 (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
340 (1 << I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
341 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
342 wr32(hw, reg_idx, reg);
343 }
344
345irq_list_done:
346 i40e_flush(hw);
347}
348
349/**
350 * i40e_config_vsi_tx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000351 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700352 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000353 * @vsi_queue_id: vsi relative queue index
354 * @info: config. info
355 *
356 * configure tx queue
357 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700358static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000359 u16 vsi_queue_id,
360 struct i40e_virtchnl_txq_info *info)
361{
362 struct i40e_pf *pf = vf->pf;
363 struct i40e_hw *hw = &pf->hw;
364 struct i40e_hmc_obj_txq tx_ctx;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700365 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000366 u16 pf_queue_id;
367 u32 qtx_ctl;
368 int ret = 0;
369
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700370 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
371 vsi = i40e_find_vsi_from_id(pf, vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000372
373 /* clear the context structure first */
374 memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
375
376 /* only set the required fields */
377 tx_ctx.base = info->dma_ring_addr / 128;
378 tx_ctx.qlen = info->ring_len;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700379 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000380 tx_ctx.rdylist_act = 0;
Ashish Shah5d298962014-05-22 06:31:25 +0000381 tx_ctx.head_wb_ena = info->headwb_enabled;
382 tx_ctx.head_wb_addr = info->dma_headwb_addr;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000383
384 /* clear the context in the HMC */
385 ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
386 if (ret) {
387 dev_err(&pf->pdev->dev,
388 "Failed to clear VF LAN Tx queue context %d, error: %d\n",
389 pf_queue_id, ret);
390 ret = -ENOENT;
391 goto error_context;
392 }
393
394 /* set the context in the HMC */
395 ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
396 if (ret) {
397 dev_err(&pf->pdev->dev,
398 "Failed to set VF LAN Tx queue context %d error: %d\n",
399 pf_queue_id, ret);
400 ret = -ENOENT;
401 goto error_context;
402 }
403
404 /* associate this queue with the PCI VF function */
405 qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
Shannon Nelson13fd9772013-09-28 07:14:19 +0000406 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000407 & I40E_QTX_CTL_PF_INDX_MASK);
408 qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
409 << I40E_QTX_CTL_VFVM_INDX_SHIFT)
410 & I40E_QTX_CTL_VFVM_INDX_MASK);
411 wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
412 i40e_flush(hw);
413
414error_context:
415 return ret;
416}
417
418/**
419 * i40e_config_vsi_rx_queue
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000420 * @vf: pointer to the VF info
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700421 * @vsi_id: id of VSI as provided by the FW
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000422 * @vsi_queue_id: vsi relative queue index
423 * @info: config. info
424 *
425 * configure rx queue
426 **/
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700427static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000428 u16 vsi_queue_id,
429 struct i40e_virtchnl_rxq_info *info)
430{
431 struct i40e_pf *pf = vf->pf;
432 struct i40e_hw *hw = &pf->hw;
433 struct i40e_hmc_obj_rxq rx_ctx;
434 u16 pf_queue_id;
435 int ret = 0;
436
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700437 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000438
439 /* clear the context structure first */
440 memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
441
442 /* only set the required fields */
443 rx_ctx.base = info->dma_ring_addr / 128;
444 rx_ctx.qlen = info->ring_len;
445
446 if (info->splithdr_enabled) {
447 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
448 I40E_RX_SPLIT_IP |
449 I40E_RX_SPLIT_TCP_UDP |
450 I40E_RX_SPLIT_SCTP;
451 /* header length validation */
452 if (info->hdr_size > ((2 * 1024) - 64)) {
453 ret = -EINVAL;
454 goto error_param;
455 }
456 rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
457
458 /* set splitalways mode 10b */
459 rx_ctx.dtype = 0x2;
460 }
461
462 /* databuffer length validation */
463 if (info->databuffer_size > ((16 * 1024) - 128)) {
464 ret = -EINVAL;
465 goto error_param;
466 }
467 rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
468
469 /* max pkt. length validation */
470 if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
471 ret = -EINVAL;
472 goto error_param;
473 }
474 rx_ctx.rxmax = info->max_pkt_size;
475
476 /* enable 32bytes desc always */
477 rx_ctx.dsize = 1;
478
479 /* default values */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000480 rx_ctx.lrxqthresh = 2;
481 rx_ctx.crcstrip = 1;
Mitch Williams50d41652014-04-09 05:58:55 +0000482 rx_ctx.prefena = 1;
Shannon Nelsonc1d11ce2014-07-29 04:01:03 +0000483 rx_ctx.l2tsel = 1;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000484
485 /* clear the context in the HMC */
486 ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
487 if (ret) {
488 dev_err(&pf->pdev->dev,
489 "Failed to clear VF LAN Rx queue context %d, error: %d\n",
490 pf_queue_id, ret);
491 ret = -ENOENT;
492 goto error_param;
493 }
494
495 /* set the context in the HMC */
496 ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
497 if (ret) {
498 dev_err(&pf->pdev->dev,
499 "Failed to set VF LAN Rx queue context %d error: %d\n",
500 pf_queue_id, ret);
501 ret = -ENOENT;
502 goto error_param;
503 }
504
505error_param:
506 return ret;
507}
508
509/**
510 * i40e_alloc_vsi_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000511 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000512 * @type: type of VSI to allocate
513 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000514 * alloc VF vsi context & resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000515 **/
516static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
517{
518 struct i40e_mac_filter *f = NULL;
519 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000520 struct i40e_vsi *vsi;
521 int ret = 0;
522
523 vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
524
525 if (!vsi) {
526 dev_err(&pf->pdev->dev,
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000527 "add vsi failed for VF %d, aq_err %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000528 vf->vf_id, pf->hw.aq.asq_last_status);
529 ret = -ENOENT;
530 goto error_alloc_vsi_res;
531 }
532 if (type == I40E_VSI_SRIOV) {
Greg Rose1a103702013-11-28 06:42:39 +0000533 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700534 vf->lan_vsi_idx = vsi->idx;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000535 vf->lan_vsi_id = vsi->id;
Greg Rose6c12fcb2013-11-28 06:39:34 +0000536 /* If the port VLAN has been configured and then the
537 * VF driver was removed then the VSI port VLAN
538 * configuration was destroyed. Check if there is
539 * a port VLAN and restore the VSI configuration if
540 * needed.
541 */
542 if (vf->port_vlan_id)
543 i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000544 f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
Greg Rose6c12fcb2013-11-28 06:39:34 +0000545 vf->port_vlan_id, true, false);
Greg Rose1a103702013-11-28 06:42:39 +0000546 if (!f)
547 dev_info(&pf->pdev->dev,
548 "Could not allocate VF MAC addr\n");
549 f = i40e_add_filter(vsi, brdcast, vf->port_vlan_id,
550 true, false);
551 if (!f)
552 dev_info(&pf->pdev->dev,
553 "Could not allocate VF broadcast filter\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000554 }
Neerav Parikh6dbbbfb2013-11-26 10:49:24 +0000555
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000556 /* program mac filter */
557 ret = i40e_sync_vsi_filters(vsi);
Mitch Williamsfd1646e2014-02-13 03:48:44 -0800558 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000559 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000560
Mitch Williams6b192892014-03-06 09:02:29 +0000561 /* Set VF bandwidth if specified */
562 if (vf->tx_rate) {
563 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
564 vf->tx_rate / 50, 0, NULL);
565 if (ret)
566 dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
567 vf->vf_id, ret);
568 }
569
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000570error_alloc_vsi_res:
571 return ret;
572}
573
574/**
Mitch Williams805bd5b2013-11-28 06:39:26 +0000575 * i40e_enable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000576 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000577 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000578 * enable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000579 **/
580static void i40e_enable_vf_mappings(struct i40e_vf *vf)
581{
582 struct i40e_pf *pf = vf->pf;
583 struct i40e_hw *hw = &pf->hw;
584 u32 reg, total_queue_pairs = 0;
585 int j;
586
587 /* Tell the hardware we're using noncontiguous mapping. HW requires
588 * that VF queues be mapped using this method, even when they are
589 * contiguous in real life
590 */
591 wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
592 I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
593
594 /* enable VF vplan_qtable mappings */
595 reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
596 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
597
598 /* map PF queues to VF queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700599 for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
600 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
Mitch Williams805bd5b2013-11-28 06:39:26 +0000601 reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
602 wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
603 total_queue_pairs++;
604 }
605
606 /* map PF queues to VSI */
607 for (j = 0; j < 7; j++) {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700608 if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
Mitch Williams805bd5b2013-11-28 06:39:26 +0000609 reg = 0x07FF07FF; /* unused */
610 } else {
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700611 u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000612 j * 2);
613 reg = qid;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700614 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
Mitch Williams805bd5b2013-11-28 06:39:26 +0000615 (j * 2) + 1);
616 reg |= qid << 16;
617 }
618 wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
619 }
620
621 i40e_flush(hw);
622}
623
624/**
625 * i40e_disable_vf_mappings
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000626 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000627 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000628 * disable VF mappings
Mitch Williams805bd5b2013-11-28 06:39:26 +0000629 **/
630static void i40e_disable_vf_mappings(struct i40e_vf *vf)
631{
632 struct i40e_pf *pf = vf->pf;
633 struct i40e_hw *hw = &pf->hw;
634 int i;
635
636 /* disable qp mappings */
637 wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
638 for (i = 0; i < I40E_MAX_VSI_QP; i++)
639 wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
640 I40E_QUEUE_END_OF_LIST);
641 i40e_flush(hw);
642}
643
644/**
645 * i40e_free_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000646 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000647 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000648 * free VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000649 **/
650static void i40e_free_vf_res(struct i40e_vf *vf)
651{
652 struct i40e_pf *pf = vf->pf;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000653 struct i40e_hw *hw = &pf->hw;
654 u32 reg_idx, reg;
655 int i, msix_vf;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000656
657 /* free vsi & disconnect it from the parent uplink */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700658 if (vf->lan_vsi_idx) {
659 i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
660 vf->lan_vsi_idx = 0;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000661 vf->lan_vsi_id = 0;
662 }
Mitch Williams9347eb72014-02-11 08:26:32 +0000663 msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
664
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000665 /* disable interrupts so the VF starts in a known state */
666 for (i = 0; i < msix_vf; i++) {
667 /* format is same for both registers */
668 if (0 == i)
669 reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
670 else
671 reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
672 (vf->vf_id))
673 + (i - 1));
674 wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
675 i40e_flush(hw);
676 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000677
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000678 /* clear the irq settings */
679 for (i = 0; i < msix_vf; i++) {
680 /* format is same for both registers */
681 if (0 == i)
682 reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
683 else
684 reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
685 (vf->vf_id))
686 + (i - 1));
687 reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
688 I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
689 wr32(hw, reg_idx, reg);
690 i40e_flush(hw);
691 }
Mitch Williams805bd5b2013-11-28 06:39:26 +0000692 /* reset some of the state varibles keeping
693 * track of the resources
694 */
695 vf->num_queue_pairs = 0;
696 vf->vf_states = 0;
697}
698
699/**
700 * i40e_alloc_vf_res
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000701 * @vf: pointer to the VF info
Mitch Williams805bd5b2013-11-28 06:39:26 +0000702 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000703 * allocate VF resources
Mitch Williams805bd5b2013-11-28 06:39:26 +0000704 **/
705static int i40e_alloc_vf_res(struct i40e_vf *vf)
706{
707 struct i40e_pf *pf = vf->pf;
708 int total_queue_pairs = 0;
709 int ret;
710
711 /* allocate hw vsi context & associated resources */
712 ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
713 if (ret)
714 goto error_alloc;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700715 total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Mitch Williams805bd5b2013-11-28 06:39:26 +0000716 set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
717
718 /* store the total qps number for the runtime
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000719 * VF req validation
Mitch Williams805bd5b2013-11-28 06:39:26 +0000720 */
721 vf->num_queue_pairs = total_queue_pairs;
722
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000723 /* VF is now completely initialized */
Mitch Williams805bd5b2013-11-28 06:39:26 +0000724 set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
725
726error_alloc:
727 if (ret)
728 i40e_free_vf_res(vf);
729
730 return ret;
731}
732
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000733#define VF_DEVICE_STATUS 0xAA
734#define VF_TRANS_PENDING_MASK 0x20
735/**
736 * i40e_quiesce_vf_pci
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000737 * @vf: pointer to the VF structure
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000738 *
739 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
740 * if the transactions never clear.
741 **/
742static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
743{
744 struct i40e_pf *pf = vf->pf;
745 struct i40e_hw *hw = &pf->hw;
746 int vf_abs_id, i;
747 u32 reg;
748
Mitch Williamsb141d612013-11-28 06:39:36 +0000749 vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000750
751 wr32(hw, I40E_PF_PCI_CIAA,
752 VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
753 for (i = 0; i < 100; i++) {
754 reg = rd32(hw, I40E_PF_PCI_CIAD);
755 if ((reg & VF_TRANS_PENDING_MASK) == 0)
756 return 0;
757 udelay(1);
758 }
759 return -EIO;
760}
761
Mitch Williams805bd5b2013-11-28 06:39:26 +0000762/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000763 * i40e_reset_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000764 * @vf: pointer to the VF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000765 * @flr: VFLR was issued or not
766 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000767 * reset the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000768 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000769void i40e_reset_vf(struct i40e_vf *vf, bool flr)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000770{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000771 struct i40e_pf *pf = vf->pf;
772 struct i40e_hw *hw = &pf->hw;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000773 bool rsd = false;
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000774 int i;
775 u32 reg;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000776
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000777 if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
778 return;
779
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000780 /* warn the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000781 clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
782
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000783 /* In the case of a VFLR, the HW has already reset the VF and we
784 * just need to clean up, so don't hit the VFRTRIG register.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000785 */
786 if (!flr) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000787 /* reset VF using VPGEN_VFRTRIG reg */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000788 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
789 reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000790 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
791 i40e_flush(hw);
792 }
793
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000794 if (i40e_quiesce_vf_pci(vf))
795 dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
796 vf->vf_id);
797
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000798 /* poll VPGEN_VFRSTAT reg to make sure
799 * that reset is complete
800 */
Mitch Williams1750a222015-01-09 11:18:13 +0000801 for (i = 0; i < 10; i++) {
802 /* VF reset requires driver to first reset the VF and then
803 * poll the status register to make sure that the reset
804 * completed successfully. Due to internal HW FIFO flushes,
805 * we must wait 10ms before the register will be valid.
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000806 */
Mitch Williams1750a222015-01-09 11:18:13 +0000807 usleep_range(10000, 20000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000808 reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
809 if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
810 rsd = true;
811 break;
812 }
813 }
814
815 if (!rsd)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000816 dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000817 vf->vf_id);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000818 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000819 /* clear the reset bit in the VPGEN_VFRTRIG reg */
820 reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
821 reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
822 wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000823
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000824 /* On initial reset, we won't have any queues */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700825 if (vf->lan_vsi_idx == 0)
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000826 goto complete_reset;
827
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -0700828 i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000829complete_reset:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000830 /* reallocate VF resources to reset the VSI state */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000831 i40e_free_vf_res(vf);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000832 i40e_alloc_vf_res(vf);
833 i40e_enable_vf_mappings(vf);
Mitch Williamsc17b3622014-02-13 03:48:45 -0800834 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000835
836 /* tell the VF the reset is done */
837 wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
838 i40e_flush(hw);
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000839 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000840}
Greg Rosec3542292013-12-13 08:38:38 +0000841
842/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000843 * i40e_free_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000844 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000845 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000846 * free VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000847 **/
848void i40e_free_vfs(struct i40e_pf *pf)
849{
Mitch Williamsf7414532013-11-28 06:39:40 +0000850 struct i40e_hw *hw = &pf->hw;
851 u32 reg_idx, bit_idx;
852 int i, tmp, vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000853
854 if (!pf->vf)
855 return;
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000856 while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
857 usleep_range(1000, 2000);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000858
Mitch Williams44434632015-04-07 11:32:55 -0700859 for (i = 0; i < pf->num_alloc_vfs; i++)
860 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
861 i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
862 false);
863
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000864 /* Disable IOV before freeing resources. This lets any VF drivers
865 * running in the host get themselves cleaned up before we yank
866 * the carpet out from underneath their feet.
867 */
868 if (!pci_vfs_assigned(pf->pdev))
869 pci_disable_sriov(pf->pdev);
Mitch Williams6d7b9672015-03-31 00:45:02 -0700870 else
871 dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
Mitch A Williams6a9ddb32014-12-09 08:53:01 +0000872
873 msleep(20); /* let any messages in transit get finished up */
874
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000875 /* free up VF resources */
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000876 tmp = pf->num_alloc_vfs;
877 pf->num_alloc_vfs = 0;
878 for (i = 0; i < tmp; i++) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000879 if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
880 i40e_free_vf_res(&pf->vf[i]);
881 /* disable qp mappings */
882 i40e_disable_vf_mappings(&pf->vf[i]);
883 }
884
885 kfree(pf->vf);
886 pf->vf = NULL;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000887
Mitch Williams9e5634d2014-04-04 04:43:14 +0000888 /* This check is for when the driver is unloaded while VFs are
889 * assigned. Setting the number of VFs to 0 through sysfs is caught
890 * before this function ever gets called.
891 */
Ethan Zhaoc24817b2014-07-22 18:36:43 +0000892 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williamsf7414532013-11-28 06:39:40 +0000893 /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
894 * work correctly when SR-IOV gets re-enabled.
895 */
896 for (vf_id = 0; vf_id < tmp; vf_id++) {
897 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
898 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
899 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
900 }
Greg Rosec3542292013-12-13 08:38:38 +0000901 }
Mitch Williams3ba9bcb2015-01-09 11:18:15 +0000902 clear_bit(__I40E_VF_DISABLE, &pf->state);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000903}
904
905#ifdef CONFIG_PCI_IOV
906/**
907 * i40e_alloc_vfs
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000908 * @pf: pointer to the PF structure
909 * @num_alloc_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000910 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000911 * allocate VF resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000912 **/
Mitch Williams4aeec012014-02-13 03:48:47 -0800913int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000914{
915 struct i40e_vf *vfs;
916 int i, ret = 0;
917
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000918 /* Disable interrupt 0 so we don't try to handle the VFLR. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000919 i40e_irq_dynamic_disable_icr0(pf);
920
Mitch Williams4aeec012014-02-13 03:48:47 -0800921 /* Check to see if we're just allocating resources for extant VFs */
922 if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
923 ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
924 if (ret) {
925 dev_err(&pf->pdev->dev,
926 "Failed to enable SR-IOV, error %d.\n", ret);
927 pf->num_alloc_vfs = 0;
928 goto err_iov;
929 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000930 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000931 /* allocate memory */
Akeem G Abodunrincc6456a2014-02-06 05:51:02 +0000932 vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000933 if (!vfs) {
934 ret = -ENOMEM;
935 goto err_alloc;
936 }
Mitch Williamsc674d122014-05-20 08:01:40 +0000937 pf->vf = vfs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000938
939 /* apply default profile */
940 for (i = 0; i < num_alloc_vfs; i++) {
941 vfs[i].pf = pf;
942 vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
943 vfs[i].vf_id = i;
944
945 /* assign default capabilities */
946 set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
Mitch Williamsc674d122014-05-20 08:01:40 +0000947 vfs[i].spoofchk = true;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000948 /* VF resources get allocated during reset */
Mitch Williamsfc18eaa2013-11-28 06:39:27 +0000949 i40e_reset_vf(&vfs[i], false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000950
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000951 /* enable VF vplan_qtable mappings */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000952 i40e_enable_vf_mappings(&vfs[i]);
953 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000954 pf->num_alloc_vfs = num_alloc_vfs;
955
956err_alloc:
957 if (ret)
958 i40e_free_vfs(pf);
959err_iov:
Mitch Williams6c1b5bf2013-11-28 06:39:30 +0000960 /* Re-enable interrupt 0. */
Mitch Williams2ef28cf2013-11-28 06:39:32 +0000961 i40e_irq_dynamic_enable_icr0(pf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000962 return ret;
963}
964
965#endif
966/**
967 * i40e_pci_sriov_enable
968 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +0000969 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +0000970 *
971 * Enable or change the number of VFs
972 **/
973static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
974{
975#ifdef CONFIG_PCI_IOV
976 struct i40e_pf *pf = pci_get_drvdata(pdev);
977 int pre_existing_vfs = pci_num_vf(pdev);
978 int err = 0;
979
980 dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
981 if (pre_existing_vfs && pre_existing_vfs != num_vfs)
982 i40e_free_vfs(pf);
983 else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
984 goto out;
985
986 if (num_vfs > pf->num_req_vfs) {
987 err = -EPERM;
988 goto err_out;
989 }
990
991 err = i40e_alloc_vfs(pf, num_vfs);
992 if (err) {
993 dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
994 goto err_out;
995 }
996
997out:
998 return num_vfs;
999
1000err_out:
1001 return err;
1002#endif
1003 return 0;
1004}
1005
1006/**
1007 * i40e_pci_sriov_configure
1008 * @pdev: pointer to a pci_dev structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001009 * @num_vfs: number of VFs to allocate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001010 *
1011 * Enable or change the number of VFs. Called when the user updates the number
1012 * of VFs in sysfs.
1013 **/
1014int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1015{
1016 struct i40e_pf *pf = pci_get_drvdata(pdev);
1017
1018 if (num_vfs)
1019 return i40e_pci_sriov_enable(pdev, num_vfs);
1020
Ethan Zhaoc24817b2014-07-22 18:36:43 +00001021 if (!pci_vfs_assigned(pf->pdev)) {
Mitch Williams9e5634d2014-04-04 04:43:14 +00001022 i40e_free_vfs(pf);
1023 } else {
1024 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1025 return -EINVAL;
1026 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001027 return 0;
1028}
1029
1030/***********************virtual channel routines******************/
1031
1032/**
1033 * i40e_vc_send_msg_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001034 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001035 * @v_opcode: virtual channel opcode
1036 * @v_retval: virtual channel return value
1037 * @msg: pointer to the msg buffer
1038 * @msglen: msg length
1039 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001040 * send msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001041 **/
1042static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1043 u32 v_retval, u8 *msg, u16 msglen)
1044{
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001045 struct i40e_pf *pf;
1046 struct i40e_hw *hw;
1047 int abs_vf_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001048 i40e_status aq_ret;
1049
Anjali Singhai Jain6e7b5bd2014-07-10 07:58:21 +00001050 /* validate the request */
1051 if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1052 return -EINVAL;
1053
1054 pf = vf->pf;
1055 hw = &pf->hw;
1056 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1057
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001058 /* single place to detect unsuccessful return values */
1059 if (v_retval) {
1060 vf->num_invalid_msgs++;
1061 dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
1062 v_opcode, v_retval);
1063 if (vf->num_invalid_msgs >
1064 I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1065 dev_err(&pf->pdev->dev,
1066 "Number of invalid messages exceeded for VF %d\n",
1067 vf->vf_id);
1068 dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1069 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1070 }
1071 } else {
1072 vf->num_valid_msgs++;
1073 }
1074
Ashish Shahf19efbb2014-08-01 13:27:06 -07001075 aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
Mitch Williams7efa84b2013-11-28 06:39:41 +00001076 msg, msglen, NULL);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001077 if (aq_ret) {
1078 dev_err(&pf->pdev->dev,
1079 "Unable to send the message to VF %d aq_err %d\n",
1080 vf->vf_id, pf->hw.aq.asq_last_status);
1081 return -EIO;
1082 }
1083
1084 return 0;
1085}
1086
1087/**
1088 * i40e_vc_send_resp_to_vf
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001089 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001090 * @opcode: operation code
1091 * @retval: return value
1092 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001093 * send resp msg to VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001094 **/
1095static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1096 enum i40e_virtchnl_ops opcode,
1097 i40e_status retval)
1098{
1099 return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1100}
1101
1102/**
1103 * i40e_vc_get_version_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001104 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001105 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001106 * called from the VF to request the API version used by the PF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001107 **/
1108static int i40e_vc_get_version_msg(struct i40e_vf *vf)
1109{
1110 struct i40e_virtchnl_version_info info = {
1111 I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
1112 };
1113
1114 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
1115 I40E_SUCCESS, (u8 *)&info,
1116 sizeof(struct
1117 i40e_virtchnl_version_info));
1118}
1119
1120/**
1121 * i40e_vc_get_vf_resources_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001122 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001123 * @msg: pointer to the msg buffer
1124 * @msglen: msg length
1125 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001126 * called from the VF to request its resources
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001127 **/
1128static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf)
1129{
1130 struct i40e_virtchnl_vf_resource *vfres = NULL;
1131 struct i40e_pf *pf = vf->pf;
1132 i40e_status aq_ret = 0;
1133 struct i40e_vsi *vsi;
1134 int i = 0, len = 0;
1135 int num_vsis = 1;
1136 int ret;
1137
1138 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1139 aq_ret = I40E_ERR_PARAM;
1140 goto err;
1141 }
1142
1143 len = (sizeof(struct i40e_virtchnl_vf_resource) +
1144 sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1145
1146 vfres = kzalloc(len, GFP_KERNEL);
1147 if (!vfres) {
1148 aq_ret = I40E_ERR_NO_MEMORY;
1149 len = 0;
1150 goto err;
1151 }
1152
1153 vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001154 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001155 if (!vsi->info.pvid)
1156 vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1157
1158 vfres->num_vsis = num_vsis;
1159 vfres->num_queue_pairs = vf->num_queue_pairs;
1160 vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001161 if (vf->lan_vsi_idx) {
1162 vfres->vsi_res[i].vsi_id = vf->lan_vsi_id;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001163 vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1164 vfres->vsi_res[i].num_queue_pairs =
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001165 pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001166 memcpy(vfres->vsi_res[i].default_mac_addr,
1167 vf->default_lan_addr.addr, ETH_ALEN);
1168 i++;
1169 }
1170 set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1171
1172err:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001173 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001174 ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1175 aq_ret, (u8 *)vfres, len);
1176
1177 kfree(vfres);
1178 return ret;
1179}
1180
1181/**
1182 * i40e_vc_reset_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001183 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001184 * @msg: pointer to the msg buffer
1185 * @msglen: msg length
1186 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001187 * called from the VF to reset itself,
1188 * unlike other virtchnl messages, PF driver
1189 * doesn't send the response back to the VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001190 **/
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001191static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001192{
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001193 if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1194 i40e_reset_vf(vf, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001195}
1196
1197/**
1198 * i40e_vc_config_promiscuous_mode_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001199 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001200 * @msg: pointer to the msg buffer
1201 * @msglen: msg length
1202 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001203 * called from the VF to configure the promiscuous mode of
1204 * VF vsis
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001205 **/
1206static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1207 u8 *msg, u16 msglen)
1208{
1209 struct i40e_virtchnl_promisc_info *info =
1210 (struct i40e_virtchnl_promisc_info *)msg;
1211 struct i40e_pf *pf = vf->pf;
1212 struct i40e_hw *hw = &pf->hw;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001213 struct i40e_vsi *vsi;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001214 bool allmulti = false;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001215 i40e_status aq_ret;
1216
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001217 vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001218 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1219 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1220 !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001221 (vsi->type != I40E_VSI_FCOE)) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001222 aq_ret = I40E_ERR_PARAM;
1223 goto error_param;
1224 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001225 if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1226 allmulti = true;
Ashish Shah89cb86c2014-08-01 13:27:10 -07001227 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001228 allmulti, NULL);
1229
1230error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001231 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001232 return i40e_vc_send_resp_to_vf(vf,
1233 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1234 aq_ret);
1235}
1236
1237/**
1238 * i40e_vc_config_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001239 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001240 * @msg: pointer to the msg buffer
1241 * @msglen: msg length
1242 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001243 * called from the VF to configure the rx/tx
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001244 * queues
1245 **/
1246static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1247{
1248 struct i40e_virtchnl_vsi_queue_config_info *qci =
1249 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1250 struct i40e_virtchnl_queue_pair_info *qpi;
Ashish Shah5f5e33b2014-07-10 07:58:15 +00001251 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001252 u16 vsi_id, vsi_queue_id;
1253 i40e_status aq_ret = 0;
1254 int i;
1255
1256 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1257 aq_ret = I40E_ERR_PARAM;
1258 goto error_param;
1259 }
1260
1261 vsi_id = qci->vsi_id;
1262 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1263 aq_ret = I40E_ERR_PARAM;
1264 goto error_param;
1265 }
1266 for (i = 0; i < qci->num_queue_pairs; i++) {
1267 qpi = &qci->qpair[i];
1268 vsi_queue_id = qpi->txq.queue_id;
1269 if ((qpi->txq.vsi_id != vsi_id) ||
1270 (qpi->rxq.vsi_id != vsi_id) ||
1271 (qpi->rxq.queue_id != vsi_queue_id) ||
1272 !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1273 aq_ret = I40E_ERR_PARAM;
1274 goto error_param;
1275 }
1276
1277 if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1278 &qpi->rxq) ||
1279 i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1280 &qpi->txq)) {
1281 aq_ret = I40E_ERR_PARAM;
1282 goto error_param;
1283 }
1284 }
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001285 /* set vsi num_queue_pairs in use to num configured by VF */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001286 pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001287
1288error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001289 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001290 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1291 aq_ret);
1292}
1293
1294/**
1295 * i40e_vc_config_irq_map_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001296 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001297 * @msg: pointer to the msg buffer
1298 * @msglen: msg length
1299 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001300 * called from the VF to configure the irq to
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001301 * queue map
1302 **/
1303static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1304{
1305 struct i40e_virtchnl_irq_map_info *irqmap_info =
1306 (struct i40e_virtchnl_irq_map_info *)msg;
1307 struct i40e_virtchnl_vector_map *map;
1308 u16 vsi_id, vsi_queue_id, vector_id;
1309 i40e_status aq_ret = 0;
1310 unsigned long tempmap;
1311 int i;
1312
1313 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1314 aq_ret = I40E_ERR_PARAM;
1315 goto error_param;
1316 }
1317
1318 for (i = 0; i < irqmap_info->num_vectors; i++) {
1319 map = &irqmap_info->vecmap[i];
1320
1321 vector_id = map->vector_id;
1322 vsi_id = map->vsi_id;
1323 /* validate msg params */
1324 if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1325 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1326 aq_ret = I40E_ERR_PARAM;
1327 goto error_param;
1328 }
1329
1330 /* lookout for the invalid queue index */
1331 tempmap = map->rxq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001332 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001333 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1334 vsi_queue_id)) {
1335 aq_ret = I40E_ERR_PARAM;
1336 goto error_param;
1337 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001338 }
1339
1340 tempmap = map->txq_map;
Wei Yongjun48366502013-09-24 05:17:36 +00001341 for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001342 if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1343 vsi_queue_id)) {
1344 aq_ret = I40E_ERR_PARAM;
1345 goto error_param;
1346 }
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001347 }
1348
1349 i40e_config_irq_link_list(vf, vsi_id, map);
1350 }
1351error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001352 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001353 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1354 aq_ret);
1355}
1356
1357/**
1358 * i40e_vc_enable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001359 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001360 * @msg: pointer to the msg buffer
1361 * @msglen: msg length
1362 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001363 * called from the VF to enable all or specific queue(s)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001364 **/
1365static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1366{
1367 struct i40e_virtchnl_queue_select *vqs =
1368 (struct i40e_virtchnl_queue_select *)msg;
1369 struct i40e_pf *pf = vf->pf;
1370 u16 vsi_id = vqs->vsi_id;
1371 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001372
1373 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1374 aq_ret = I40E_ERR_PARAM;
1375 goto error_param;
1376 }
1377
1378 if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1379 aq_ret = I40E_ERR_PARAM;
1380 goto error_param;
1381 }
1382
1383 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1384 aq_ret = I40E_ERR_PARAM;
1385 goto error_param;
1386 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001387
1388 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], true))
Mitch Williams88f65632013-11-28 06:39:28 +00001389 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001390error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001391 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001392 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1393 aq_ret);
1394}
1395
1396/**
1397 * i40e_vc_disable_queues_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001398 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001399 * @msg: pointer to the msg buffer
1400 * @msglen: msg length
1401 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001402 * called from the VF to disable all or specific
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001403 * queue(s)
1404 **/
1405static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1406{
1407 struct i40e_virtchnl_queue_select *vqs =
1408 (struct i40e_virtchnl_queue_select *)msg;
1409 struct i40e_pf *pf = vf->pf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001410 i40e_status aq_ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001411
1412 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1413 aq_ret = I40E_ERR_PARAM;
1414 goto error_param;
1415 }
1416
1417 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1418 aq_ret = I40E_ERR_PARAM;
1419 goto error_param;
1420 }
1421
1422 if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1423 aq_ret = I40E_ERR_PARAM;
1424 goto error_param;
1425 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001426
1427 if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false))
Mitch Williams88f65632013-11-28 06:39:28 +00001428 aq_ret = I40E_ERR_TIMEOUT;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001429
1430error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001431 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001432 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1433 aq_ret);
1434}
1435
1436/**
1437 * i40e_vc_get_stats_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001438 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001439 * @msg: pointer to the msg buffer
1440 * @msglen: msg length
1441 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001442 * called from the VF to get vsi stats
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001443 **/
1444static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1445{
1446 struct i40e_virtchnl_queue_select *vqs =
1447 (struct i40e_virtchnl_queue_select *)msg;
1448 struct i40e_pf *pf = vf->pf;
1449 struct i40e_eth_stats stats;
1450 i40e_status aq_ret = 0;
1451 struct i40e_vsi *vsi;
1452
1453 memset(&stats, 0, sizeof(struct i40e_eth_stats));
1454
1455 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1456 aq_ret = I40E_ERR_PARAM;
1457 goto error_param;
1458 }
1459
1460 if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1461 aq_ret = I40E_ERR_PARAM;
1462 goto error_param;
1463 }
1464
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001465 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001466 if (!vsi) {
1467 aq_ret = I40E_ERR_PARAM;
1468 goto error_param;
1469 }
1470 i40e_update_eth_stats(vsi);
Mitch Williams5a9769c2013-11-28 06:39:38 +00001471 stats = vsi->eth_stats;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001472
1473error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001474 /* send the response back to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001475 return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1476 (u8 *)&stats, sizeof(stats));
1477}
1478
1479/**
Greg Rosef657a6e2013-11-28 06:39:42 +00001480 * i40e_check_vf_permission
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001481 * @vf: pointer to the VF info
Greg Rosef657a6e2013-11-28 06:39:42 +00001482 * @macaddr: pointer to the MAC Address being checked
1483 *
1484 * Check if the VF has permission to add or delete unicast MAC address
1485 * filters and return error code -EPERM if not. Then check if the
1486 * address filter requested is broadcast or zero and if so return
1487 * an invalid MAC address error code.
1488 **/
1489static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1490{
1491 struct i40e_pf *pf = vf->pf;
1492 int ret = 0;
1493
1494 if (is_broadcast_ether_addr(macaddr) ||
1495 is_zero_ether_addr(macaddr)) {
1496 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1497 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rose5017c2a2013-12-07 10:36:54 +00001498 } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1499 !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001500 /* If the host VMM administrator has set the VF MAC address
1501 * administratively via the ndo_set_vf_mac command then deny
1502 * permission to the VF to add or delete unicast MAC addresses.
Greg Rose5017c2a2013-12-07 10:36:54 +00001503 * The VF may request to set the MAC address filter already
1504 * assigned to it so do not return an error in that case.
Greg Rosef657a6e2013-11-28 06:39:42 +00001505 */
1506 dev_err(&pf->pdev->dev,
1507 "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1508 ret = -EPERM;
1509 }
1510 return ret;
1511}
1512
1513/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001514 * i40e_vc_add_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001515 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001516 * @msg: pointer to the msg buffer
1517 * @msglen: msg length
1518 *
1519 * add guest mac address filter
1520 **/
1521static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1522{
1523 struct i40e_virtchnl_ether_addr_list *al =
1524 (struct i40e_virtchnl_ether_addr_list *)msg;
1525 struct i40e_pf *pf = vf->pf;
1526 struct i40e_vsi *vsi = NULL;
1527 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001528 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001529 int i;
1530
1531 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1532 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1533 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001534 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001535 goto error_param;
1536 }
1537
1538 for (i = 0; i < al->num_elements; i++) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001539 ret = i40e_check_vf_permission(vf, al->list[i].addr);
1540 if (ret)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001541 goto error_param;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001542 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001543 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001544
1545 /* add new addresses to the list */
1546 for (i = 0; i < al->num_elements; i++) {
1547 struct i40e_mac_filter *f;
1548
1549 f = i40e_find_mac(vsi, al->list[i].addr, true, false);
Mitch Williams7e68edf92013-11-16 10:00:41 +00001550 if (!f) {
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001551 if (i40e_is_vsi_in_vlan(vsi))
1552 f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1553 true, false);
1554 else
1555 f = i40e_add_filter(vsi, al->list[i].addr, -1,
1556 true, false);
1557 }
1558
1559 if (!f) {
1560 dev_err(&pf->pdev->dev,
1561 "Unable to add VF MAC filter\n");
Greg Rosef657a6e2013-11-28 06:39:42 +00001562 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001563 goto error_param;
1564 }
1565 }
1566
1567 /* program the updated filter list */
1568 if (i40e_sync_vsi_filters(vsi))
1569 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1570
1571error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001572 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001573 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001574 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001575}
1576
1577/**
1578 * i40e_vc_del_mac_addr_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001579 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001580 * @msg: pointer to the msg buffer
1581 * @msglen: msg length
1582 *
1583 * remove guest mac address filter
1584 **/
1585static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1586{
1587 struct i40e_virtchnl_ether_addr_list *al =
1588 (struct i40e_virtchnl_ether_addr_list *)msg;
1589 struct i40e_pf *pf = vf->pf;
1590 struct i40e_vsi *vsi = NULL;
1591 u16 vsi_id = al->vsi_id;
Greg Rosef657a6e2013-11-28 06:39:42 +00001592 i40e_status ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001593 int i;
1594
1595 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1596 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1597 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
Greg Rosef657a6e2013-11-28 06:39:42 +00001598 ret = I40E_ERR_PARAM;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001599 goto error_param;
1600 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001601
1602 for (i = 0; i < al->num_elements; i++) {
Mitch Williams700bbf62013-12-21 05:44:45 +00001603 if (is_broadcast_ether_addr(al->list[i].addr) ||
1604 is_zero_ether_addr(al->list[i].addr)) {
1605 dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
1606 al->list[i].addr);
1607 ret = I40E_ERR_INVALID_MAC_ADDR;
Greg Rosef657a6e2013-11-28 06:39:42 +00001608 goto error_param;
Mitch Williams700bbf62013-12-21 05:44:45 +00001609 }
Greg Rosef657a6e2013-11-28 06:39:42 +00001610 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001611 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001612
1613 /* delete addresses from the list */
1614 for (i = 0; i < al->num_elements; i++)
1615 i40e_del_filter(vsi, al->list[i].addr,
1616 I40E_VLAN_ANY, true, false);
1617
1618 /* program the updated filter list */
1619 if (i40e_sync_vsi_filters(vsi))
1620 dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1621
1622error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001623 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001624 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
Greg Rosef657a6e2013-11-28 06:39:42 +00001625 ret);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001626}
1627
1628/**
1629 * i40e_vc_add_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001630 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001631 * @msg: pointer to the msg buffer
1632 * @msglen: msg length
1633 *
1634 * program guest vlan id
1635 **/
1636static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1637{
1638 struct i40e_virtchnl_vlan_filter_list *vfl =
1639 (struct i40e_virtchnl_vlan_filter_list *)msg;
1640 struct i40e_pf *pf = vf->pf;
1641 struct i40e_vsi *vsi = NULL;
1642 u16 vsi_id = vfl->vsi_id;
1643 i40e_status aq_ret = 0;
1644 int i;
1645
1646 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1647 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1648 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1649 aq_ret = I40E_ERR_PARAM;
1650 goto error_param;
1651 }
1652
1653 for (i = 0; i < vfl->num_elements; i++) {
1654 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1655 aq_ret = I40E_ERR_PARAM;
1656 dev_err(&pf->pdev->dev,
1657 "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1658 goto error_param;
1659 }
1660 }
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001661 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001662 if (vsi->info.pvid) {
1663 aq_ret = I40E_ERR_PARAM;
1664 goto error_param;
1665 }
1666
1667 i40e_vlan_stripping_enable(vsi);
1668 for (i = 0; i < vfl->num_elements; i++) {
1669 /* add new VLAN filter */
1670 int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1671 if (ret)
1672 dev_err(&pf->pdev->dev,
1673 "Unable to add VF vlan filter %d, error %d\n",
1674 vfl->vlan_id[i], ret);
1675 }
1676
1677error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001678 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001679 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1680}
1681
1682/**
1683 * i40e_vc_remove_vlan_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001684 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001685 * @msg: pointer to the msg buffer
1686 * @msglen: msg length
1687 *
1688 * remove programmed guest vlan id
1689 **/
1690static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1691{
1692 struct i40e_virtchnl_vlan_filter_list *vfl =
1693 (struct i40e_virtchnl_vlan_filter_list *)msg;
1694 struct i40e_pf *pf = vf->pf;
1695 struct i40e_vsi *vsi = NULL;
1696 u16 vsi_id = vfl->vsi_id;
1697 i40e_status aq_ret = 0;
1698 int i;
1699
1700 if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1701 !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1702 !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1703 aq_ret = I40E_ERR_PARAM;
1704 goto error_param;
1705 }
1706
1707 for (i = 0; i < vfl->num_elements; i++) {
1708 if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1709 aq_ret = I40E_ERR_PARAM;
1710 goto error_param;
1711 }
1712 }
1713
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001714 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001715 if (vsi->info.pvid) {
1716 aq_ret = I40E_ERR_PARAM;
1717 goto error_param;
1718 }
1719
1720 for (i = 0; i < vfl->num_elements; i++) {
1721 int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1722 if (ret)
1723 dev_err(&pf->pdev->dev,
1724 "Unable to delete VF vlan filter %d, error %d\n",
1725 vfl->vlan_id[i], ret);
1726 }
1727
1728error_param:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001729 /* send the response to the VF */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001730 return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1731}
1732
1733/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001734 * i40e_vc_validate_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001735 * @vf: pointer to the VF info
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001736 * @msg: pointer to the msg buffer
1737 * @msglen: msg length
1738 * @msghndl: msg handle
1739 *
1740 * validate msg
1741 **/
1742static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1743 u32 v_retval, u8 *msg, u16 msglen)
1744{
1745 bool err_msg_format = false;
1746 int valid_len;
1747
1748 /* Check if VF is disabled. */
1749 if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1750 return I40E_ERR_PARAM;
1751
1752 /* Validate message length. */
1753 switch (v_opcode) {
1754 case I40E_VIRTCHNL_OP_VERSION:
1755 valid_len = sizeof(struct i40e_virtchnl_version_info);
1756 break;
1757 case I40E_VIRTCHNL_OP_RESET_VF:
1758 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1759 valid_len = 0;
1760 break;
1761 case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1762 valid_len = sizeof(struct i40e_virtchnl_txq_info);
1763 break;
1764 case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1765 valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1766 break;
1767 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1768 valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1769 if (msglen >= valid_len) {
1770 struct i40e_virtchnl_vsi_queue_config_info *vqc =
1771 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1772 valid_len += (vqc->num_queue_pairs *
1773 sizeof(struct
1774 i40e_virtchnl_queue_pair_info));
1775 if (vqc->num_queue_pairs == 0)
1776 err_msg_format = true;
1777 }
1778 break;
1779 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1780 valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1781 if (msglen >= valid_len) {
1782 struct i40e_virtchnl_irq_map_info *vimi =
1783 (struct i40e_virtchnl_irq_map_info *)msg;
1784 valid_len += (vimi->num_vectors *
1785 sizeof(struct i40e_virtchnl_vector_map));
1786 if (vimi->num_vectors == 0)
1787 err_msg_format = true;
1788 }
1789 break;
1790 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1791 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1792 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1793 break;
1794 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1795 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1796 valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1797 if (msglen >= valid_len) {
1798 struct i40e_virtchnl_ether_addr_list *veal =
1799 (struct i40e_virtchnl_ether_addr_list *)msg;
1800 valid_len += veal->num_elements *
1801 sizeof(struct i40e_virtchnl_ether_addr);
1802 if (veal->num_elements == 0)
1803 err_msg_format = true;
1804 }
1805 break;
1806 case I40E_VIRTCHNL_OP_ADD_VLAN:
1807 case I40E_VIRTCHNL_OP_DEL_VLAN:
1808 valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1809 if (msglen >= valid_len) {
1810 struct i40e_virtchnl_vlan_filter_list *vfl =
1811 (struct i40e_virtchnl_vlan_filter_list *)msg;
1812 valid_len += vfl->num_elements * sizeof(u16);
1813 if (vfl->num_elements == 0)
1814 err_msg_format = true;
1815 }
1816 break;
1817 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1818 valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1819 break;
1820 case I40E_VIRTCHNL_OP_GET_STATS:
1821 valid_len = sizeof(struct i40e_virtchnl_queue_select);
1822 break;
1823 /* These are always errors coming from the VF. */
1824 case I40E_VIRTCHNL_OP_EVENT:
1825 case I40E_VIRTCHNL_OP_UNKNOWN:
1826 default:
1827 return -EPERM;
1828 break;
1829 }
1830 /* few more checks */
1831 if ((valid_len != msglen) || (err_msg_format)) {
1832 i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1833 return -EINVAL;
1834 } else {
1835 return 0;
1836 }
1837}
1838
1839/**
1840 * i40e_vc_process_vf_msg
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001841 * @pf: pointer to the PF structure
1842 * @vf_id: source VF id
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001843 * @msg: pointer to the msg buffer
1844 * @msglen: msg length
1845 * @msghndl: msg handle
1846 *
1847 * called from the common aeq/arq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001848 * process request from VF
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001849 **/
1850int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1851 u32 v_retval, u8 *msg, u16 msglen)
1852{
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001853 struct i40e_hw *hw = &pf->hw;
Dan Carpenterc243e962014-01-15 06:43:39 +00001854 unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001855 struct i40e_vf *vf;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001856 int ret;
1857
1858 pf->vf_aq_requests++;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001859 if (local_vf_id >= pf->num_alloc_vfs)
Mitch Williams6c1b5bf2013-11-28 06:39:30 +00001860 return -EINVAL;
Mitch Williams7efa84b2013-11-28 06:39:41 +00001861 vf = &(pf->vf[local_vf_id]);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001862 /* perform basic checks on the msg */
1863 ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1864
1865 if (ret) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001866 dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001867 local_vf_id, v_opcode, msglen);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001868 return ret;
1869 }
Mitch Williamsbae3cae2014-01-14 00:49:49 -08001870
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001871 switch (v_opcode) {
1872 case I40E_VIRTCHNL_OP_VERSION:
1873 ret = i40e_vc_get_version_msg(vf);
1874 break;
1875 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1876 ret = i40e_vc_get_vf_resources_msg(vf);
1877 break;
1878 case I40E_VIRTCHNL_OP_RESET_VF:
Mitch Williamsfc18eaa2013-11-28 06:39:27 +00001879 i40e_vc_reset_vf_msg(vf);
1880 ret = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001881 break;
1882 case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1883 ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1884 break;
1885 case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1886 ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1887 break;
1888 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1889 ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1890 break;
1891 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1892 ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
Mitch Williams055b2952015-04-07 19:45:33 -04001893 i40e_vc_notify_vf_link_state(vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001894 break;
1895 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1896 ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1897 break;
1898 case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1899 ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1900 break;
1901 case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1902 ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1903 break;
1904 case I40E_VIRTCHNL_OP_ADD_VLAN:
1905 ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1906 break;
1907 case I40E_VIRTCHNL_OP_DEL_VLAN:
1908 ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1909 break;
1910 case I40E_VIRTCHNL_OP_GET_STATS:
1911 ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1912 break;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001913 case I40E_VIRTCHNL_OP_UNKNOWN:
1914 default:
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001915 dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
Mitch Williams7efa84b2013-11-28 06:39:41 +00001916 v_opcode, local_vf_id);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001917 ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1918 I40E_ERR_NOT_IMPLEMENTED);
1919 break;
1920 }
1921
1922 return ret;
1923}
1924
1925/**
1926 * i40e_vc_process_vflr_event
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001927 * @pf: pointer to the PF structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001928 *
1929 * called from the vlfr irq handler to
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001930 * free up VF resources and state variables
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001931 **/
1932int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1933{
1934 u32 reg, reg_idx, bit_idx, vf_id;
1935 struct i40e_hw *hw = &pf->hw;
1936 struct i40e_vf *vf;
1937
1938 if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1939 return 0;
1940
Mitch Williamsc5c2f7c2014-11-11 03:15:04 +00001941 /* re-enable vflr interrupt cause */
1942 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1943 reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1944 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1945 i40e_flush(hw);
1946
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001947 clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1948 for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1949 reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1950 bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001951 /* read GLGEN_VFLRSTAT register to find out the flr VFs */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001952 vf = &pf->vf[vf_id];
1953 reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
1954 if (reg & (1 << bit_idx)) {
1955 /* clear the bit in GLGEN_VFLRSTAT */
1956 wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
1957
Mitch Williamseb2d80b2014-02-13 03:48:48 -08001958 if (!test_bit(__I40E_DOWN, &pf->state))
1959 i40e_reset_vf(vf, true);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001960 }
1961 }
1962
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001963 return 0;
1964}
1965
1966/**
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001967 * i40e_ndo_set_vf_mac
1968 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001969 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001970 * @mac: mac address
1971 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00001972 * program VF mac address
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001973 **/
1974int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
1975{
1976 struct i40e_netdev_priv *np = netdev_priv(netdev);
1977 struct i40e_vsi *vsi = np->vsi;
1978 struct i40e_pf *pf = vsi->back;
1979 struct i40e_mac_filter *f;
1980 struct i40e_vf *vf;
1981 int ret = 0;
1982
1983 /* validate the request */
1984 if (vf_id >= pf->num_alloc_vfs) {
1985 dev_err(&pf->pdev->dev,
1986 "Invalid VF Identifier %d\n", vf_id);
1987 ret = -EINVAL;
1988 goto error_param;
1989 }
1990
1991 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07001992 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00001993 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1994 dev_err(&pf->pdev->dev,
1995 "Uninitialized VF %d\n", vf_id);
1996 ret = -EINVAL;
1997 goto error_param;
1998 }
1999
2000 if (!is_valid_ether_addr(mac)) {
2001 dev_err(&pf->pdev->dev,
2002 "Invalid VF ethernet address\n");
2003 ret = -EINVAL;
2004 goto error_param;
2005 }
2006
2007 /* delete the temporary mac address */
Greg Rose37cc0d22014-04-01 07:11:44 +00002008 i40e_del_filter(vsi, vf->default_lan_addr.addr, vf->port_vlan_id,
2009 true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002010
Greg Rose29f71bb2014-05-20 08:01:45 +00002011 /* Delete all the filters for this VSI - we're going to kill it
2012 * anyway.
2013 */
2014 list_for_each_entry(f, &vsi->mac_filter_list, list)
2015 i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002016
2017 dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2018 /* program mac filter */
2019 if (i40e_sync_vsi_filters(vsi)) {
2020 dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2021 ret = -EIO;
2022 goto error_param;
2023 }
Greg Rose9a173902014-05-22 06:32:02 +00002024 ether_addr_copy(vf->default_lan_addr.addr, mac);
Greg Rosef657a6e2013-11-28 06:39:42 +00002025 vf->pf_set_mac = true;
Greg Rose17413a82014-06-04 01:23:13 +00002026 /* Force the VF driver stop so it has to reload with new MAC address */
2027 i40e_vc_disable_vf(pf, vf);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002028 dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002029
2030error_param:
2031 return ret;
2032}
2033
2034/**
2035 * i40e_ndo_set_vf_port_vlan
2036 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002037 * @vf_id: VF identifier
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002038 * @vlan_id: mac address
2039 * @qos: priority setting
2040 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002041 * program VF vlan id and/or qos
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002042 **/
2043int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2044 int vf_id, u16 vlan_id, u8 qos)
2045{
2046 struct i40e_netdev_priv *np = netdev_priv(netdev);
2047 struct i40e_pf *pf = np->vsi->back;
2048 struct i40e_vsi *vsi;
2049 struct i40e_vf *vf;
2050 int ret = 0;
2051
2052 /* validate the request */
2053 if (vf_id >= pf->num_alloc_vfs) {
2054 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2055 ret = -EINVAL;
2056 goto error_pvid;
2057 }
2058
2059 if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2060 dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2061 ret = -EINVAL;
2062 goto error_pvid;
2063 }
2064
2065 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002066 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002067 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2068 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2069 ret = -EINVAL;
2070 goto error_pvid;
2071 }
2072
Greg Rosef9b4b622014-03-06 09:02:28 +00002073 if (vsi->info.pvid == 0 && i40e_is_vsi_in_vlan(vsi)) {
Greg Rose99a49732014-01-13 16:13:03 -08002074 dev_err(&pf->pdev->dev,
2075 "VF %d has already configured VLAN filters and the administrator is requesting a port VLAN override.\nPlease unload and reload the VF driver for this change to take effect.\n",
2076 vf_id);
Greg Rosef9b4b622014-03-06 09:02:28 +00002077 /* Administrator Error - knock the VF offline until he does
2078 * the right thing by reconfiguring his network correctly
2079 * and then reloading the VF driver.
2080 */
2081 i40e_vc_disable_vf(pf, vf);
2082 }
Greg Rose99a49732014-01-13 16:13:03 -08002083
Greg Rose8d82a7c2014-01-13 16:13:04 -08002084 /* Check for condition where there was already a port VLAN ID
2085 * filter set and now it is being deleted by setting it to zero.
Greg Rose1315f7c2014-03-14 07:32:20 +00002086 * Additionally check for the condition where there was a port
2087 * VLAN but now there is a new and different port VLAN being set.
Greg Rose8d82a7c2014-01-13 16:13:04 -08002088 * Before deleting all the old VLAN filters we must add new ones
2089 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2090 * MAC addresses deleted.
2091 */
Greg Rose1315f7c2014-03-14 07:32:20 +00002092 if ((!(vlan_id || qos) ||
2093 (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
2094 vsi->info.pvid)
Greg Rose8d82a7c2014-01-13 16:13:04 -08002095 ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2096
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002097 if (vsi->info.pvid) {
2098 /* kill old VLAN */
2099 ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2100 VLAN_VID_MASK));
2101 if (ret) {
2102 dev_info(&vsi->back->pdev->dev,
2103 "remove VLAN failed, ret=%d, aq_err=%d\n",
2104 ret, pf->hw.aq.asq_last_status);
2105 }
2106 }
2107 if (vlan_id || qos)
2108 ret = i40e_vsi_add_pvid(vsi,
2109 vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
2110 else
Greg Rose6c12fcb2013-11-28 06:39:34 +00002111 i40e_vsi_remove_pvid(vsi);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002112
2113 if (vlan_id) {
2114 dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2115 vlan_id, qos, vf_id);
2116
2117 /* add new VLAN filter */
2118 ret = i40e_vsi_add_vlan(vsi, vlan_id);
2119 if (ret) {
2120 dev_info(&vsi->back->pdev->dev,
2121 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2122 vsi->back->hw.aq.asq_last_status);
2123 goto error_pvid;
2124 }
Greg Rose8d82a7c2014-01-13 16:13:04 -08002125 /* Kill non-vlan MAC filters - ignore error return since
2126 * there might not be any non-vlan MAC filters.
2127 */
2128 i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002129 }
2130
2131 if (ret) {
2132 dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2133 goto error_pvid;
2134 }
Greg Rose6c12fcb2013-11-28 06:39:34 +00002135 /* The Port VLAN needs to be saved across resets the same as the
2136 * default LAN MAC address.
2137 */
2138 vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002139 ret = 0;
2140
2141error_pvid:
2142 return ret;
2143}
2144
Mitch Williams84590fd2014-04-09 05:58:57 +00002145#define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
2146#define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002147/**
2148 * i40e_ndo_set_vf_bw
2149 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002150 * @vf_id: VF identifier
2151 * @tx_rate: Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002152 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002153 * configure VF Tx rate
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002154 **/
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002155int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2156 int max_tx_rate)
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002157{
Mitch Williams6b192892014-03-06 09:02:29 +00002158 struct i40e_netdev_priv *np = netdev_priv(netdev);
2159 struct i40e_pf *pf = np->vsi->back;
2160 struct i40e_vsi *vsi;
2161 struct i40e_vf *vf;
2162 int speed = 0;
2163 int ret = 0;
2164
2165 /* validate the request */
2166 if (vf_id >= pf->num_alloc_vfs) {
2167 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2168 ret = -EINVAL;
2169 goto error;
2170 }
2171
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002172 if (min_tx_rate) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002173 dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002174 min_tx_rate, vf_id);
2175 return -EINVAL;
2176 }
2177
Mitch Williams6b192892014-03-06 09:02:29 +00002178 vf = &(pf->vf[vf_id]);
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002179 vsi = pf->vsi[vf->lan_vsi_idx];
Mitch Williams6b192892014-03-06 09:02:29 +00002180 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2181 dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
2182 ret = -EINVAL;
2183 goto error;
2184 }
2185
2186 switch (pf->hw.phy.link_info.link_speed) {
2187 case I40E_LINK_SPEED_40GB:
2188 speed = 40000;
2189 break;
2190 case I40E_LINK_SPEED_10GB:
2191 speed = 10000;
2192 break;
2193 case I40E_LINK_SPEED_1GB:
2194 speed = 1000;
2195 break;
2196 default:
2197 break;
2198 }
2199
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002200 if (max_tx_rate > speed) {
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002201 dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002202 max_tx_rate, vf->vf_id);
Mitch Williams6b192892014-03-06 09:02:29 +00002203 ret = -EINVAL;
2204 goto error;
2205 }
2206
Mitch Williamsdac9b312014-04-09 05:58:56 +00002207 if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2208 dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2209 max_tx_rate = 50;
2210 }
2211
Mitch Williams6b192892014-03-06 09:02:29 +00002212 /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
Mitch Williams84590fd2014-04-09 05:58:57 +00002213 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2214 max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2215 I40E_MAX_BW_INACTIVE_ACCUM, NULL);
Mitch Williams6b192892014-03-06 09:02:29 +00002216 if (ret) {
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002217 dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
Mitch Williams6b192892014-03-06 09:02:29 +00002218 ret);
2219 ret = -EIO;
2220 goto error;
2221 }
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002222 vf->tx_rate = max_tx_rate;
Mitch Williams6b192892014-03-06 09:02:29 +00002223error:
2224 return ret;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002225}
2226
2227/**
2228 * i40e_ndo_get_vf_config
2229 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002230 * @vf_id: VF identifier
2231 * @ivi: VF configuration structure
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002232 *
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002233 * return VF configuration
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002234 **/
2235int i40e_ndo_get_vf_config(struct net_device *netdev,
2236 int vf_id, struct ifla_vf_info *ivi)
2237{
2238 struct i40e_netdev_priv *np = netdev_priv(netdev);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002239 struct i40e_vsi *vsi = np->vsi;
2240 struct i40e_pf *pf = vsi->back;
2241 struct i40e_vf *vf;
2242 int ret = 0;
2243
2244 /* validate the request */
2245 if (vf_id >= pf->num_alloc_vfs) {
2246 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2247 ret = -EINVAL;
2248 goto error_param;
2249 }
2250
2251 vf = &(pf->vf[vf_id]);
2252 /* first vsi is always the LAN vsi */
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002253 vsi = pf->vsi[vf->lan_vsi_idx];
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002254 if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2255 dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2256 ret = -EINVAL;
2257 goto error_param;
2258 }
2259
2260 ivi->vf = vf_id;
2261
Mitch Williamsf4a1c5c2013-11-28 06:39:34 +00002262 memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002263
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002264 ivi->max_tx_rate = vf->tx_rate;
2265 ivi->min_tx_rate = 0;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002266 ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2267 ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2268 I40E_VLAN_PRIORITY_SHIFT;
Mitch Williams84ca55a2014-03-14 07:32:24 +00002269 if (vf->link_forced == false)
2270 ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2271 else if (vf->link_up == true)
2272 ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2273 else
2274 ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
Mitch Williamsc674d122014-05-20 08:01:40 +00002275 ivi->spoofchk = vf->spoofchk;
Jesse Brandeburg5c3c48a2013-09-11 08:40:07 +00002276 ret = 0;
2277
2278error_param:
2279 return ret;
2280}
Mitch Williams588aefa2014-02-11 08:27:49 +00002281
2282/**
2283 * i40e_ndo_set_vf_link_state
2284 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002285 * @vf_id: VF identifier
Mitch Williams588aefa2014-02-11 08:27:49 +00002286 * @link: required link state
2287 *
2288 * Set the link state of a specified VF, regardless of physical link state
2289 **/
2290int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
2291{
2292 struct i40e_netdev_priv *np = netdev_priv(netdev);
2293 struct i40e_pf *pf = np->vsi->back;
2294 struct i40e_virtchnl_pf_event pfe;
2295 struct i40e_hw *hw = &pf->hw;
2296 struct i40e_vf *vf;
Ashish Shahf19efbb2014-08-01 13:27:06 -07002297 int abs_vf_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002298 int ret = 0;
2299
2300 /* validate the request */
2301 if (vf_id >= pf->num_alloc_vfs) {
2302 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2303 ret = -EINVAL;
2304 goto error_out;
2305 }
2306
2307 vf = &pf->vf[vf_id];
Ashish Shahf19efbb2014-08-01 13:27:06 -07002308 abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
Mitch Williams588aefa2014-02-11 08:27:49 +00002309
2310 pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
2311 pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
2312
2313 switch (link) {
2314 case IFLA_VF_LINK_STATE_AUTO:
2315 vf->link_forced = false;
2316 pfe.event_data.link_event.link_status =
2317 pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
2318 pfe.event_data.link_event.link_speed =
2319 pf->hw.phy.link_info.link_speed;
2320 break;
2321 case IFLA_VF_LINK_STATE_ENABLE:
2322 vf->link_forced = true;
2323 vf->link_up = true;
2324 pfe.event_data.link_event.link_status = true;
2325 pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
2326 break;
2327 case IFLA_VF_LINK_STATE_DISABLE:
2328 vf->link_forced = true;
2329 vf->link_up = false;
2330 pfe.event_data.link_event.link_status = false;
2331 pfe.event_data.link_event.link_speed = 0;
2332 break;
2333 default:
2334 ret = -EINVAL;
2335 goto error_out;
2336 }
2337 /* Notify the VF of its new link state */
Ashish Shahf19efbb2014-08-01 13:27:06 -07002338 i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
Mitch Williams588aefa2014-02-11 08:27:49 +00002339 0, (u8 *)&pfe, sizeof(pfe), NULL);
2340
2341error_out:
2342 return ret;
2343}
Mitch Williamsc674d122014-05-20 08:01:40 +00002344
2345/**
2346 * i40e_ndo_set_vf_spoofchk
2347 * @netdev: network interface device structure
Jeff Kirsherb40c82e2015-02-27 09:18:34 +00002348 * @vf_id: VF identifier
Mitch Williamsc674d122014-05-20 08:01:40 +00002349 * @enable: flag to enable or disable feature
2350 *
2351 * Enable or disable VF spoof checking
2352 **/
Serey Konge6d90042014-07-12 07:28:14 +00002353int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
Mitch Williamsc674d122014-05-20 08:01:40 +00002354{
2355 struct i40e_netdev_priv *np = netdev_priv(netdev);
2356 struct i40e_vsi *vsi = np->vsi;
2357 struct i40e_pf *pf = vsi->back;
2358 struct i40e_vsi_context ctxt;
2359 struct i40e_hw *hw = &pf->hw;
2360 struct i40e_vf *vf;
2361 int ret = 0;
2362
2363 /* validate the request */
2364 if (vf_id >= pf->num_alloc_vfs) {
2365 dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2366 ret = -EINVAL;
2367 goto out;
2368 }
2369
2370 vf = &(pf->vf[vf_id]);
2371
2372 if (enable == vf->spoofchk)
2373 goto out;
2374
2375 vf->spoofchk = enable;
2376 memset(&ctxt, 0, sizeof(ctxt));
Anjali Singhai Jainfdf0e0b2015-03-31 00:45:05 -07002377 ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
Mitch Williamsc674d122014-05-20 08:01:40 +00002378 ctxt.pf_num = pf->hw.pf_id;
2379 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
2380 if (enable)
Greg Rose30d71af2015-01-29 07:17:17 +00002381 ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
2382 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
Mitch Williamsc674d122014-05-20 08:01:40 +00002383 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
2384 if (ret) {
2385 dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
2386 ret);
2387 ret = -EIO;
2388 }
2389out:
2390 return ret;
2391}