blob: 4f056efecba408b0d96120e494ed2ce2c28e00cb [file] [log] [blame]
Greg Rose62683ab2013-12-21 06:13:01 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
Mitch Williamsef8693e2014-02-13 03:48:53 -08004 * Copyright(c) 2013 - 2014 Intel Corporation.
Greg Rose62683ab2013-12-21 06:13:01 +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 *
Jesse Brandeburgb8316072014-04-05 07:46:11 +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/>.
17 *
Greg Rose62683ab2013-12-21 06:13:01 +000018 * 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 "i40evf.h"
28#include "i40e_prototype.h"
29
30/* busy wait delay in msec */
31#define I40EVF_BUSY_WAIT_DELAY 10
32#define I40EVF_BUSY_WAIT_COUNT 50
33
34/**
35 * i40evf_send_pf_msg
36 * @adapter: adapter structure
37 * @op: virtual channel opcode
38 * @msg: pointer to message buffer
39 * @len: message length
40 *
41 * Send message to PF and print status if failure.
42 **/
43static int i40evf_send_pf_msg(struct i40evf_adapter *adapter,
44 enum i40e_virtchnl_ops op, u8 *msg, u16 len)
45{
46 struct i40e_hw *hw = &adapter->hw;
47 i40e_status err;
48
Mitch Williamsef8693e2014-02-13 03:48:53 -080049 if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
50 return 0; /* nothing to see here, move along */
51
Greg Rose62683ab2013-12-21 06:13:01 +000052 err = i40e_aq_send_msg_to_pf(hw, op, 0, msg, len, NULL);
53 if (err)
Shannon Nelsonf1c7e722015-06-04 16:24:01 -040054 dev_err(&adapter->pdev->dev, "Unable to send opcode %d to PF, err %s, aq_err %s\n",
55 op, i40evf_stat_str(hw, err),
56 i40evf_aq_str(hw, hw->aq.asq_last_status));
Greg Rose62683ab2013-12-21 06:13:01 +000057 return err;
58}
59
60/**
61 * i40evf_send_api_ver
62 * @adapter: adapter structure
63 *
64 * Send API version admin queue message to the PF. The reply is not checked
65 * in this function. Returns 0 if the message was successfully
66 * sent, or one of the I40E_ADMIN_QUEUE_ERROR_ statuses if not.
67 **/
68int i40evf_send_api_ver(struct i40evf_adapter *adapter)
69{
70 struct i40e_virtchnl_version_info vvi;
71
72 vvi.major = I40E_VIRTCHNL_VERSION_MAJOR;
73 vvi.minor = I40E_VIRTCHNL_VERSION_MINOR;
74
75 return i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_VERSION, (u8 *)&vvi,
76 sizeof(vvi));
77}
78
79/**
80 * i40evf_verify_api_ver
81 * @adapter: adapter structure
82 *
83 * Compare API versions with the PF. Must be called after admin queue is
Mitch Williams6a8e93d2014-06-05 00:09:17 +000084 * initialized. Returns 0 if API versions match, -EIO if they do not,
85 * I40E_ERR_ADMIN_QUEUE_NO_WORK if the admin queue is empty, and any errors
86 * from the firmware are propagated.
Greg Rose62683ab2013-12-21 06:13:01 +000087 **/
88int i40evf_verify_api_ver(struct i40evf_adapter *adapter)
89{
90 struct i40e_virtchnl_version_info *pf_vvi;
91 struct i40e_hw *hw = &adapter->hw;
92 struct i40e_arq_event_info event;
Mitch Williamsf8d4db32014-10-25 03:24:33 +000093 enum i40e_virtchnl_ops op;
Greg Rose62683ab2013-12-21 06:13:01 +000094 i40e_status err;
95
Mitch Williams1001dc32014-11-11 20:02:19 +000096 event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
97 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Greg Rose62683ab2013-12-21 06:13:01 +000098 if (!event.msg_buf) {
99 err = -ENOMEM;
100 goto out;
101 }
102
Mitch Williamsf8d4db32014-10-25 03:24:33 +0000103 while (1) {
104 err = i40evf_clean_arq_element(hw, &event, NULL);
105 /* When the AQ is empty, i40evf_clean_arq_element will return
106 * nonzero and this loop will terminate.
107 */
108 if (err)
109 goto out_alloc;
110 op =
111 (enum i40e_virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
112 if (op == I40E_VIRTCHNL_OP_VERSION)
113 break;
114 }
115
Greg Rose62683ab2013-12-21 06:13:01 +0000116
117 err = (i40e_status)le32_to_cpu(event.desc.cookie_low);
Mitch Williams6a8e93d2014-06-05 00:09:17 +0000118 if (err)
Greg Rose62683ab2013-12-21 06:13:01 +0000119 goto out_alloc;
Greg Rose62683ab2013-12-21 06:13:01 +0000120
Mitch Williamsf8d4db32014-10-25 03:24:33 +0000121 if (op != I40E_VIRTCHNL_OP_VERSION) {
Mitch Williams6a8e93d2014-06-05 00:09:17 +0000122 dev_info(&adapter->pdev->dev, "Invalid reply type %d from PF\n",
Mitch Williamsf8d4db32014-10-25 03:24:33 +0000123 op);
Greg Rose62683ab2013-12-21 06:13:01 +0000124 err = -EIO;
125 goto out_alloc;
126 }
127
128 pf_vvi = (struct i40e_virtchnl_version_info *)event.msg_buf;
Mitch Williamsee1693e2015-06-04 16:23:59 -0400129 adapter->pf_version = *pf_vvi;
130
131 if ((pf_vvi->major > I40E_VIRTCHNL_VERSION_MAJOR) ||
132 ((pf_vvi->major == I40E_VIRTCHNL_VERSION_MAJOR) &&
133 (pf_vvi->minor > I40E_VIRTCHNL_VERSION_MINOR)))
Greg Rose62683ab2013-12-21 06:13:01 +0000134 err = -EIO;
135
136out_alloc:
137 kfree(event.msg_buf);
138out:
139 return err;
140}
141
142/**
143 * i40evf_send_vf_config_msg
144 * @adapter: adapter structure
145 *
146 * Send VF configuration request admin queue message to the PF. The reply
147 * is not checked in this function. Returns 0 if the message was
148 * successfully sent, or one of the I40E_ADMIN_QUEUE_ERROR_ statuses if not.
149 **/
150int i40evf_send_vf_config_msg(struct i40evf_adapter *adapter)
151{
Mitch Williamse6d038d2015-06-04 16:23:58 -0400152 u32 caps;
153
154 adapter->current_op = I40E_VIRTCHNL_OP_GET_VF_RESOURCES;
155 adapter->aq_required &= ~I40EVF_FLAG_AQ_GET_CONFIG;
156 caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
157 I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ |
158 I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
159 I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
160 adapter->current_op = I40E_VIRTCHNL_OP_GET_VF_RESOURCES;
161 adapter->aq_required &= ~I40EVF_FLAG_AQ_GET_CONFIG;
162 if (PF_IS_V11(adapter))
163 return i40evf_send_pf_msg(adapter,
164 I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
165 (u8 *)&caps, sizeof(caps));
166 else
167 return i40evf_send_pf_msg(adapter,
168 I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
169 NULL, 0);
Greg Rose62683ab2013-12-21 06:13:01 +0000170}
171
172/**
173 * i40evf_get_vf_config
174 * @hw: pointer to the hardware structure
175 * @len: length of buffer
176 *
177 * Get VF configuration from PF and populate hw structure. Must be called after
178 * admin queue is initialized. Busy waits until response is received from PF,
179 * with maximum timeout. Response from PF is returned in the buffer for further
180 * processing by the caller.
181 **/
182int i40evf_get_vf_config(struct i40evf_adapter *adapter)
183{
184 struct i40e_hw *hw = &adapter->hw;
185 struct i40e_arq_event_info event;
Mitch Williamsf8d4db32014-10-25 03:24:33 +0000186 enum i40e_virtchnl_ops op;
Greg Rose62683ab2013-12-21 06:13:01 +0000187 i40e_status err;
Mitch Williamsf8d4db32014-10-25 03:24:33 +0000188 u16 len;
Greg Rose62683ab2013-12-21 06:13:01 +0000189
190 len = sizeof(struct i40e_virtchnl_vf_resource) +
191 I40E_MAX_VF_VSI * sizeof(struct i40e_virtchnl_vsi_resource);
Mitch Williams1001dc32014-11-11 20:02:19 +0000192 event.buf_len = len;
193 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Greg Rose62683ab2013-12-21 06:13:01 +0000194 if (!event.msg_buf) {
195 err = -ENOMEM;
196 goto out;
197 }
198
Mitch Williamsf8d4db32014-10-25 03:24:33 +0000199 while (1) {
Mitch Williamsf8d4db32014-10-25 03:24:33 +0000200 /* When the AQ is empty, i40evf_clean_arq_element will return
201 * nonzero and this loop will terminate.
202 */
203 err = i40evf_clean_arq_element(hw, &event, NULL);
204 if (err)
205 goto out_alloc;
206 op =
207 (enum i40e_virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
208 if (op == I40E_VIRTCHNL_OP_GET_VF_RESOURCES)
209 break;
210 }
Greg Rose62683ab2013-12-21 06:13:01 +0000211
212 err = (i40e_status)le32_to_cpu(event.desc.cookie_low);
Mitch Williams1001dc32014-11-11 20:02:19 +0000213 memcpy(adapter->vf_res, event.msg_buf, min(event.msg_len, len));
Greg Rose62683ab2013-12-21 06:13:01 +0000214
215 i40e_vf_parse_hw_config(hw, adapter->vf_res);
216out_alloc:
217 kfree(event.msg_buf);
218out:
219 return err;
220}
221
222/**
223 * i40evf_configure_queues
224 * @adapter: adapter structure
225 *
226 * Request that the PF set up our (previously allocated) queues.
227 **/
228void i40evf_configure_queues(struct i40evf_adapter *adapter)
229{
230 struct i40e_virtchnl_vsi_queue_config_info *vqci;
231 struct i40e_virtchnl_queue_pair_info *vqpi;
Mitch Williamscc052922014-10-25 03:24:34 +0000232 int pairs = adapter->num_active_queues;
Greg Rose62683ab2013-12-21 06:13:01 +0000233 int i, len;
234
235 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
236 /* bail because we already have a command pending */
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400237 dev_err(&adapter->pdev->dev, "Cannot configure queues, command %d pending\n",
238 adapter->current_op);
Greg Rose62683ab2013-12-21 06:13:01 +0000239 return;
240 }
241 adapter->current_op = I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES;
242 len = sizeof(struct i40e_virtchnl_vsi_queue_config_info) +
243 (sizeof(struct i40e_virtchnl_queue_pair_info) * pairs);
244 vqci = kzalloc(len, GFP_ATOMIC);
Mitch Williams249c8b82014-05-10 04:49:04 +0000245 if (!vqci)
Greg Rose62683ab2013-12-21 06:13:01 +0000246 return;
Mitch Williams249c8b82014-05-10 04:49:04 +0000247
Greg Rose62683ab2013-12-21 06:13:01 +0000248 vqci->vsi_id = adapter->vsi_res->vsi_id;
249 vqci->num_queue_pairs = pairs;
250 vqpi = vqci->qpair;
251 /* Size check is not needed here - HW max is 16 queue pairs, and we
252 * can fit info for 31 of them into the AQ buffer before it overflows.
253 */
254 for (i = 0; i < pairs; i++) {
255 vqpi->txq.vsi_id = vqci->vsi_id;
256 vqpi->txq.queue_id = i;
257 vqpi->txq.ring_len = adapter->tx_rings[i]->count;
258 vqpi->txq.dma_ring_addr = adapter->tx_rings[i]->dma;
Ashish Shah5d298962014-05-22 06:31:25 +0000259 vqpi->txq.headwb_enabled = 1;
260 vqpi->txq.dma_headwb_addr = vqpi->txq.dma_ring_addr +
261 (vqpi->txq.ring_len * sizeof(struct i40e_tx_desc));
Greg Rose62683ab2013-12-21 06:13:01 +0000262
263 vqpi->rxq.vsi_id = vqci->vsi_id;
264 vqpi->rxq.queue_id = i;
265 vqpi->rxq.ring_len = adapter->rx_rings[i]->count;
266 vqpi->rxq.dma_ring_addr = adapter->rx_rings[i]->dma;
267 vqpi->rxq.max_pkt_size = adapter->netdev->mtu
268 + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN;
269 vqpi->rxq.databuffer_size = adapter->rx_rings[i]->rx_buf_len;
270 vqpi++;
271 }
272
Mitch Williamsfc86a972014-06-04 20:41:38 +0000273 adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
Greg Rose62683ab2013-12-21 06:13:01 +0000274 i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
275 (u8 *)vqci, len);
276 kfree(vqci);
Greg Rose62683ab2013-12-21 06:13:01 +0000277}
278
279/**
280 * i40evf_enable_queues
281 * @adapter: adapter structure
282 *
283 * Request that the PF enable all of our queues.
284 **/
285void i40evf_enable_queues(struct i40evf_adapter *adapter)
286{
287 struct i40e_virtchnl_queue_select vqs;
288
289 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
290 /* bail because we already have a command pending */
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400291 dev_err(&adapter->pdev->dev, "Cannot enable queues, command %d pending\n",
292 adapter->current_op);
Greg Rose62683ab2013-12-21 06:13:01 +0000293 return;
294 }
295 adapter->current_op = I40E_VIRTCHNL_OP_ENABLE_QUEUES;
296 vqs.vsi_id = adapter->vsi_res->vsi_id;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400297 vqs.tx_queues = BIT(adapter->num_active_queues) - 1;
Greg Rose62683ab2013-12-21 06:13:01 +0000298 vqs.rx_queues = vqs.tx_queues;
Greg Rose62683ab2013-12-21 06:13:01 +0000299 adapter->aq_required &= ~I40EVF_FLAG_AQ_ENABLE_QUEUES;
Mitch Williamsfc86a972014-06-04 20:41:38 +0000300 i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
301 (u8 *)&vqs, sizeof(vqs));
Greg Rose62683ab2013-12-21 06:13:01 +0000302}
303
304/**
305 * i40evf_disable_queues
306 * @adapter: adapter structure
307 *
308 * Request that the PF disable all of our queues.
309 **/
310void i40evf_disable_queues(struct i40evf_adapter *adapter)
311{
312 struct i40e_virtchnl_queue_select vqs;
313
314 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
315 /* bail because we already have a command pending */
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400316 dev_err(&adapter->pdev->dev, "Cannot disable queues, command %d pending\n",
317 adapter->current_op);
Greg Rose62683ab2013-12-21 06:13:01 +0000318 return;
319 }
320 adapter->current_op = I40E_VIRTCHNL_OP_DISABLE_QUEUES;
321 vqs.vsi_id = adapter->vsi_res->vsi_id;
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400322 vqs.tx_queues = BIT(adapter->num_active_queues) - 1;
Greg Rose62683ab2013-12-21 06:13:01 +0000323 vqs.rx_queues = vqs.tx_queues;
Greg Rose62683ab2013-12-21 06:13:01 +0000324 adapter->aq_required &= ~I40EVF_FLAG_AQ_DISABLE_QUEUES;
Mitch Williamsfc86a972014-06-04 20:41:38 +0000325 i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
326 (u8 *)&vqs, sizeof(vqs));
Greg Rose62683ab2013-12-21 06:13:01 +0000327}
328
329/**
330 * i40evf_map_queues
331 * @adapter: adapter structure
332 *
333 * Request that the PF map queues to interrupt vectors. Misc causes, including
334 * admin queue, are always mapped to vector 0.
335 **/
336void i40evf_map_queues(struct i40evf_adapter *adapter)
337{
338 struct i40e_virtchnl_irq_map_info *vimi;
339 int v_idx, q_vectors, len;
340 struct i40e_q_vector *q_vector;
341
342 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
343 /* bail because we already have a command pending */
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400344 dev_err(&adapter->pdev->dev, "Cannot map queues to vectors, command %d pending\n",
345 adapter->current_op);
Greg Rose62683ab2013-12-21 06:13:01 +0000346 return;
347 }
348 adapter->current_op = I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP;
349
350 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
351
352 len = sizeof(struct i40e_virtchnl_irq_map_info) +
353 (adapter->num_msix_vectors *
354 sizeof(struct i40e_virtchnl_vector_map));
355 vimi = kzalloc(len, GFP_ATOMIC);
Mitch Williams249c8b82014-05-10 04:49:04 +0000356 if (!vimi)
Greg Rose62683ab2013-12-21 06:13:01 +0000357 return;
Greg Rose62683ab2013-12-21 06:13:01 +0000358
359 vimi->num_vectors = adapter->num_msix_vectors;
360 /* Queue vectors first */
361 for (v_idx = 0; v_idx < q_vectors; v_idx++) {
362 q_vector = adapter->q_vector[v_idx];
363 vimi->vecmap[v_idx].vsi_id = adapter->vsi_res->vsi_id;
364 vimi->vecmap[v_idx].vector_id = v_idx + NONQ_VECS;
365 vimi->vecmap[v_idx].txq_map = q_vector->ring_mask;
366 vimi->vecmap[v_idx].rxq_map = q_vector->ring_mask;
367 }
368 /* Misc vector last - this is only for AdminQ messages */
369 vimi->vecmap[v_idx].vsi_id = adapter->vsi_res->vsi_id;
370 vimi->vecmap[v_idx].vector_id = 0;
371 vimi->vecmap[v_idx].txq_map = 0;
372 vimi->vecmap[v_idx].rxq_map = 0;
373
Mitch Williamsfc86a972014-06-04 20:41:38 +0000374 adapter->aq_required &= ~I40EVF_FLAG_AQ_MAP_VECTORS;
Greg Rose62683ab2013-12-21 06:13:01 +0000375 i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
376 (u8 *)vimi, len);
377 kfree(vimi);
Greg Rose62683ab2013-12-21 06:13:01 +0000378}
379
380/**
381 * i40evf_add_ether_addrs
382 * @adapter: adapter structure
383 * @addrs: the MAC address filters to add (contiguous)
384 * @count: number of filters
385 *
386 * Request that the PF add one or more addresses to our filters.
387 **/
388void i40evf_add_ether_addrs(struct i40evf_adapter *adapter)
389{
390 struct i40e_virtchnl_ether_addr_list *veal;
391 int len, i = 0, count = 0;
392 struct i40evf_mac_filter *f;
393
394 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
395 /* bail because we already have a command pending */
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400396 dev_err(&adapter->pdev->dev, "Cannot add filters, command %d pending\n",
397 adapter->current_op);
Greg Rose62683ab2013-12-21 06:13:01 +0000398 return;
399 }
400 list_for_each_entry(f, &adapter->mac_filter_list, list) {
401 if (f->add)
402 count++;
403 }
404 if (!count) {
405 adapter->aq_required &= ~I40EVF_FLAG_AQ_ADD_MAC_FILTER;
406 return;
407 }
408 adapter->current_op = I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS;
409
410 len = sizeof(struct i40e_virtchnl_ether_addr_list) +
411 (count * sizeof(struct i40e_virtchnl_ether_addr));
412 if (len > I40EVF_MAX_AQ_BUF_SIZE) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400413 dev_warn(&adapter->pdev->dev, "Too many add MAC changes in one request\n");
Greg Rose62683ab2013-12-21 06:13:01 +0000414 count = (I40EVF_MAX_AQ_BUF_SIZE -
415 sizeof(struct i40e_virtchnl_ether_addr_list)) /
416 sizeof(struct i40e_virtchnl_ether_addr);
417 len = I40EVF_MAX_AQ_BUF_SIZE;
418 }
419
420 veal = kzalloc(len, GFP_ATOMIC);
Mitch Williams249c8b82014-05-10 04:49:04 +0000421 if (!veal)
Greg Rose62683ab2013-12-21 06:13:01 +0000422 return;
Mitch Williams249c8b82014-05-10 04:49:04 +0000423
Greg Rose62683ab2013-12-21 06:13:01 +0000424 veal->vsi_id = adapter->vsi_res->vsi_id;
425 veal->num_elements = count;
426 list_for_each_entry(f, &adapter->mac_filter_list, list) {
427 if (f->add) {
Greg Rose9a173902014-05-22 06:32:02 +0000428 ether_addr_copy(veal->list[i].addr, f->macaddr);
Greg Rose62683ab2013-12-21 06:13:01 +0000429 i++;
430 f->add = false;
431 }
432 }
Mitch Williamsfc86a972014-06-04 20:41:38 +0000433 adapter->aq_required &= ~I40EVF_FLAG_AQ_ADD_MAC_FILTER;
Greg Rose62683ab2013-12-21 06:13:01 +0000434 i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
435 (u8 *)veal, len);
436 kfree(veal);
Greg Rose62683ab2013-12-21 06:13:01 +0000437}
438
439/**
440 * i40evf_del_ether_addrs
441 * @adapter: adapter structure
442 * @addrs: the MAC address filters to remove (contiguous)
443 * @count: number of filtes
444 *
445 * Request that the PF remove one or more addresses from our filters.
446 **/
447void i40evf_del_ether_addrs(struct i40evf_adapter *adapter)
448{
449 struct i40e_virtchnl_ether_addr_list *veal;
450 struct i40evf_mac_filter *f, *ftmp;
451 int len, i = 0, count = 0;
452
453 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
454 /* bail because we already have a command pending */
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400455 dev_err(&adapter->pdev->dev, "Cannot remove filters, command %d pending\n",
456 adapter->current_op);
Greg Rose62683ab2013-12-21 06:13:01 +0000457 return;
458 }
459 list_for_each_entry(f, &adapter->mac_filter_list, list) {
460 if (f->remove)
461 count++;
462 }
463 if (!count) {
464 adapter->aq_required &= ~I40EVF_FLAG_AQ_DEL_MAC_FILTER;
465 return;
466 }
467 adapter->current_op = I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS;
468
469 len = sizeof(struct i40e_virtchnl_ether_addr_list) +
470 (count * sizeof(struct i40e_virtchnl_ether_addr));
471 if (len > I40EVF_MAX_AQ_BUF_SIZE) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400472 dev_warn(&adapter->pdev->dev, "Too many delete MAC changes in one request\n");
Greg Rose62683ab2013-12-21 06:13:01 +0000473 count = (I40EVF_MAX_AQ_BUF_SIZE -
474 sizeof(struct i40e_virtchnl_ether_addr_list)) /
475 sizeof(struct i40e_virtchnl_ether_addr);
476 len = I40EVF_MAX_AQ_BUF_SIZE;
477 }
478 veal = kzalloc(len, GFP_ATOMIC);
Mitch Williams249c8b82014-05-10 04:49:04 +0000479 if (!veal)
Greg Rose62683ab2013-12-21 06:13:01 +0000480 return;
Mitch Williams249c8b82014-05-10 04:49:04 +0000481
Greg Rose62683ab2013-12-21 06:13:01 +0000482 veal->vsi_id = adapter->vsi_res->vsi_id;
483 veal->num_elements = count;
484 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
485 if (f->remove) {
Greg Rose9a173902014-05-22 06:32:02 +0000486 ether_addr_copy(veal->list[i].addr, f->macaddr);
Greg Rose62683ab2013-12-21 06:13:01 +0000487 i++;
488 list_del(&f->list);
489 kfree(f);
490 }
491 }
Mitch Williamsfc86a972014-06-04 20:41:38 +0000492 adapter->aq_required &= ~I40EVF_FLAG_AQ_DEL_MAC_FILTER;
Greg Rose62683ab2013-12-21 06:13:01 +0000493 i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
494 (u8 *)veal, len);
495 kfree(veal);
Greg Rose62683ab2013-12-21 06:13:01 +0000496}
497
498/**
499 * i40evf_add_vlans
500 * @adapter: adapter structure
501 * @vlans: the VLANs to add
502 * @count: number of VLANs
503 *
504 * Request that the PF add one or more VLAN filters to our VSI.
505 **/
506void i40evf_add_vlans(struct i40evf_adapter *adapter)
507{
508 struct i40e_virtchnl_vlan_filter_list *vvfl;
509 int len, i = 0, count = 0;
510 struct i40evf_vlan_filter *f;
511
512 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
513 /* bail because we already have a command pending */
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400514 dev_err(&adapter->pdev->dev, "Cannot add VLANs, command %d pending\n",
515 adapter->current_op);
Greg Rose62683ab2013-12-21 06:13:01 +0000516 return;
517 }
518
519 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
520 if (f->add)
521 count++;
522 }
523 if (!count) {
524 adapter->aq_required &= ~I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
525 return;
526 }
527 adapter->current_op = I40E_VIRTCHNL_OP_ADD_VLAN;
528
529 len = sizeof(struct i40e_virtchnl_vlan_filter_list) +
530 (count * sizeof(u16));
531 if (len > I40EVF_MAX_AQ_BUF_SIZE) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400532 dev_warn(&adapter->pdev->dev, "Too many add VLAN changes in one request\n");
Greg Rose62683ab2013-12-21 06:13:01 +0000533 count = (I40EVF_MAX_AQ_BUF_SIZE -
534 sizeof(struct i40e_virtchnl_vlan_filter_list)) /
535 sizeof(u16);
536 len = I40EVF_MAX_AQ_BUF_SIZE;
537 }
538 vvfl = kzalloc(len, GFP_ATOMIC);
Mitch Williams249c8b82014-05-10 04:49:04 +0000539 if (!vvfl)
Greg Rose62683ab2013-12-21 06:13:01 +0000540 return;
Mitch Williams249c8b82014-05-10 04:49:04 +0000541
Greg Rose62683ab2013-12-21 06:13:01 +0000542 vvfl->vsi_id = adapter->vsi_res->vsi_id;
543 vvfl->num_elements = count;
544 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
545 if (f->add) {
546 vvfl->vlan_id[i] = f->vlan;
547 i++;
548 f->add = false;
549 }
550 }
Greg Rose62683ab2013-12-21 06:13:01 +0000551 adapter->aq_required &= ~I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
Mitch Williamsfc86a972014-06-04 20:41:38 +0000552 i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_ADD_VLAN, (u8 *)vvfl, len);
553 kfree(vvfl);
Greg Rose62683ab2013-12-21 06:13:01 +0000554}
555
556/**
557 * i40evf_del_vlans
558 * @adapter: adapter structure
559 * @vlans: the VLANs to remove
560 * @count: number of VLANs
561 *
562 * Request that the PF remove one or more VLAN filters from our VSI.
563 **/
564void i40evf_del_vlans(struct i40evf_adapter *adapter)
565{
566 struct i40e_virtchnl_vlan_filter_list *vvfl;
567 struct i40evf_vlan_filter *f, *ftmp;
568 int len, i = 0, count = 0;
569
570 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
571 /* bail because we already have a command pending */
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400572 dev_err(&adapter->pdev->dev, "Cannot remove VLANs, command %d pending\n",
573 adapter->current_op);
Greg Rose62683ab2013-12-21 06:13:01 +0000574 return;
575 }
576
577 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
578 if (f->remove)
579 count++;
580 }
581 if (!count) {
582 adapter->aq_required &= ~I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
583 return;
584 }
585 adapter->current_op = I40E_VIRTCHNL_OP_DEL_VLAN;
586
587 len = sizeof(struct i40e_virtchnl_vlan_filter_list) +
588 (count * sizeof(u16));
589 if (len > I40EVF_MAX_AQ_BUF_SIZE) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400590 dev_warn(&adapter->pdev->dev, "Too many delete VLAN changes in one request\n");
Greg Rose62683ab2013-12-21 06:13:01 +0000591 count = (I40EVF_MAX_AQ_BUF_SIZE -
592 sizeof(struct i40e_virtchnl_vlan_filter_list)) /
593 sizeof(u16);
594 len = I40EVF_MAX_AQ_BUF_SIZE;
595 }
596 vvfl = kzalloc(len, GFP_ATOMIC);
Mitch Williams249c8b82014-05-10 04:49:04 +0000597 if (!vvfl)
Greg Rose62683ab2013-12-21 06:13:01 +0000598 return;
Mitch Williams249c8b82014-05-10 04:49:04 +0000599
Greg Rose62683ab2013-12-21 06:13:01 +0000600 vvfl->vsi_id = adapter->vsi_res->vsi_id;
601 vvfl->num_elements = count;
602 list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
603 if (f->remove) {
604 vvfl->vlan_id[i] = f->vlan;
605 i++;
606 list_del(&f->list);
607 kfree(f);
608 }
609 }
Greg Rose62683ab2013-12-21 06:13:01 +0000610 adapter->aq_required &= ~I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
Mitch Williamsfc86a972014-06-04 20:41:38 +0000611 i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_DEL_VLAN, (u8 *)vvfl, len);
612 kfree(vvfl);
Greg Rose62683ab2013-12-21 06:13:01 +0000613}
614
615/**
616 * i40evf_set_promiscuous
617 * @adapter: adapter structure
618 * @flags: bitmask to control unicast/multicast promiscuous.
619 *
620 * Request that the PF enable promiscuous mode for our VSI.
621 **/
622void i40evf_set_promiscuous(struct i40evf_adapter *adapter, int flags)
623{
624 struct i40e_virtchnl_promisc_info vpi;
625
626 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
627 /* bail because we already have a command pending */
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400628 dev_err(&adapter->pdev->dev, "Cannot set promiscuous mode, command %d pending\n",
629 adapter->current_op);
Greg Rose62683ab2013-12-21 06:13:01 +0000630 return;
631 }
632 adapter->current_op = I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
633 vpi.vsi_id = adapter->vsi_res->vsi_id;
634 vpi.flags = flags;
635 i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
636 (u8 *)&vpi, sizeof(vpi));
637}
638
639/**
640 * i40evf_request_stats
641 * @adapter: adapter structure
642 *
643 * Request VSI statistics from PF.
644 **/
645void i40evf_request_stats(struct i40evf_adapter *adapter)
646{
647 struct i40e_virtchnl_queue_select vqs;
Mitch Williams75a64432014-11-11 20:02:42 +0000648
Greg Rose62683ab2013-12-21 06:13:01 +0000649 if (adapter->current_op != I40E_VIRTCHNL_OP_UNKNOWN) {
650 /* no error message, this isn't crucial */
651 return;
652 }
653 adapter->current_op = I40E_VIRTCHNL_OP_GET_STATS;
654 vqs.vsi_id = adapter->vsi_res->vsi_id;
655 /* queue maps are ignored for this message - only the vsi is used */
656 if (i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_GET_STATS,
657 (u8 *)&vqs, sizeof(vqs)))
658 /* if the request failed, don't lock out others */
659 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
660}
Mitch Williams625777e2014-02-20 19:29:05 -0800661/**
662 * i40evf_request_reset
663 * @adapter: adapter structure
664 *
665 * Request that the PF reset this VF. No response is expected.
666 **/
667void i40evf_request_reset(struct i40evf_adapter *adapter)
668{
669 /* Don't check CURRENT_OP - this is always higher priority */
670 i40evf_send_pf_msg(adapter, I40E_VIRTCHNL_OP_RESET_VF, NULL, 0);
671 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
672}
Greg Rose62683ab2013-12-21 06:13:01 +0000673
674/**
675 * i40evf_virtchnl_completion
676 * @adapter: adapter structure
677 * @v_opcode: opcode sent by PF
678 * @v_retval: retval sent by PF
679 * @msg: message sent by PF
680 * @msglen: message length
681 *
682 * Asynchronous completion function for admin queue messages. Rather than busy
683 * wait, we fire off our requests and assume that no errors will be returned.
684 * This function handles the reply messages.
685 **/
686void i40evf_virtchnl_completion(struct i40evf_adapter *adapter,
687 enum i40e_virtchnl_ops v_opcode,
688 i40e_status v_retval,
689 u8 *msg, u16 msglen)
690{
691 struct net_device *netdev = adapter->netdev;
692
693 if (v_opcode == I40E_VIRTCHNL_OP_EVENT) {
694 struct i40e_virtchnl_pf_event *vpe =
695 (struct i40e_virtchnl_pf_event *)msg;
696 switch (vpe->event) {
697 case I40E_VIRTCHNL_EVENT_LINK_CHANGE:
698 adapter->link_up =
699 vpe->event_data.link_event.link_status;
700 if (adapter->link_up && !netif_carrier_ok(netdev)) {
701 dev_info(&adapter->pdev->dev, "NIC Link is Up\n");
702 netif_carrier_on(netdev);
703 netif_tx_wake_all_queues(netdev);
704 } else if (!adapter->link_up) {
705 dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
706 netif_carrier_off(netdev);
707 netif_tx_stop_all_queues(netdev);
708 }
709 break;
710 case I40E_VIRTCHNL_EVENT_RESET_IMPENDING:
Mitch Williamsef8693e2014-02-13 03:48:53 -0800711 dev_info(&adapter->pdev->dev, "PF reset warning received\n");
712 if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING)) {
713 adapter->flags |= I40EVF_FLAG_RESET_PENDING;
714 dev_info(&adapter->pdev->dev, "Scheduling reset task\n");
715 schedule_work(&adapter->reset_task);
716 }
Greg Rose62683ab2013-12-21 06:13:01 +0000717 break;
718 default:
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400719 dev_err(&adapter->pdev->dev, "Unknown event %d from PF\n",
720 vpe->event);
Greg Rose62683ab2013-12-21 06:13:01 +0000721 break;
Greg Rose62683ab2013-12-21 06:13:01 +0000722 }
723 return;
724 }
Greg Rose62683ab2013-12-21 06:13:01 +0000725 if (v_retval) {
Shannon Nelsonfb43201f2015-08-26 15:14:17 -0400726 dev_err(&adapter->pdev->dev, "PF returned error %d (%s) to our request %d\n",
727 v_retval, i40evf_stat_str(&adapter->hw, v_retval),
728 v_opcode);
Greg Rose62683ab2013-12-21 06:13:01 +0000729 }
730 switch (v_opcode) {
731 case I40E_VIRTCHNL_OP_GET_STATS: {
732 struct i40e_eth_stats *stats =
733 (struct i40e_eth_stats *)msg;
734 adapter->net_stats.rx_packets = stats->rx_unicast +
735 stats->rx_multicast +
736 stats->rx_broadcast;
737 adapter->net_stats.tx_packets = stats->tx_unicast +
738 stats->tx_multicast +
739 stats->tx_broadcast;
740 adapter->net_stats.rx_bytes = stats->rx_bytes;
741 adapter->net_stats.tx_bytes = stats->tx_bytes;
Greg Rose62683ab2013-12-21 06:13:01 +0000742 adapter->net_stats.tx_errors = stats->tx_errors;
Shannon Nelson03da6f62014-04-23 04:50:19 +0000743 adapter->net_stats.rx_dropped = stats->rx_discards;
Greg Rose62683ab2013-12-21 06:13:01 +0000744 adapter->net_stats.tx_dropped = stats->tx_discards;
745 adapter->current_stats = *stats;
746 }
747 break;
Mitch Williamse6d038d2015-06-04 16:23:58 -0400748 case I40E_VIRTCHNL_OP_GET_VF_RESOURCES: {
749 u16 len = sizeof(struct i40e_virtchnl_vf_resource) +
750 I40E_MAX_VF_VSI *
751 sizeof(struct i40e_virtchnl_vsi_resource);
752 memcpy(adapter->vf_res, msg, min(msglen, len));
753 i40e_vf_parse_hw_config(&adapter->hw, adapter->vf_res);
Mitch Williams8552d852015-08-31 19:54:51 -0400754 /* restore current mac address */
755 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
Mitch Williamse6d038d2015-06-04 16:23:58 -0400756 i40evf_process_config(adapter);
757 }
758 break;
Greg Rose62683ab2013-12-21 06:13:01 +0000759 case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
Greg Rose62683ab2013-12-21 06:13:01 +0000760 /* enable transmits */
761 i40evf_irq_enable(adapter, true);
762 netif_tx_start_all_queues(adapter->netdev);
763 netif_carrier_on(adapter->netdev);
764 break;
765 case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
Mitch Williamse284fc82015-03-27 00:12:09 -0700766 i40evf_free_all_tx_resources(adapter);
767 i40evf_free_all_rx_resources(adapter);
Greg Rose62683ab2013-12-21 06:13:01 +0000768 break;
Mitch Williamsed636962015-04-07 19:45:32 -0400769 case I40E_VIRTCHNL_OP_VERSION:
Greg Rose62683ab2013-12-21 06:13:01 +0000770 case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
Mitch Williamsed636962015-04-07 19:45:32 -0400771 /* Don't display an error if we get these out of sequence.
772 * If the firmware needed to get kicked, we'll get these and
773 * it's no problem.
774 */
775 if (v_opcode != adapter->current_op)
776 return;
Greg Rose62683ab2013-12-21 06:13:01 +0000777 break;
778 default:
Mitch Williamsed636962015-04-07 19:45:32 -0400779 if (v_opcode != adapter->current_op)
780 dev_warn(&adapter->pdev->dev, "Expected response %d from PF, received %d\n",
781 adapter->current_op, v_opcode);
Greg Rose62683ab2013-12-21 06:13:01 +0000782 break;
783 } /* switch v_opcode */
784 adapter->current_op = I40E_VIRTCHNL_OP_UNKNOWN;
785}