1// SPDX-License-Identifier: GPL-2.0
2/* Copyright(c) 2013 - 2018 Intel Corporation. */
3
4#include "iavf.h"
5#include "iavf_prototype.h"
6#include "iavf_client.h"
7
8/* busy wait delay in msec */
9#define IAVF_BUSY_WAIT_DELAY 10
10#define IAVF_BUSY_WAIT_COUNT 50
11
12/**
13 * iavf_send_pf_msg
14 * @adapter: adapter structure
15 * @op: virtual channel opcode
16 * @msg: pointer to message buffer
17 * @len: message length
18 *
19 * Send message to PF and print status if failure.
20 **/
21static int iavf_send_pf_msg(struct iavf_adapter *adapter,
22			    enum virtchnl_ops op, u8 *msg, u16 len)
23{
24	struct iavf_hw *hw = &adapter->hw;
25	enum iavf_status err;
26
27	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
28		return 0; /* nothing to see here, move along */
29
30	err = iavf_aq_send_msg_to_pf(hw, op, 0, msg, len, NULL);
31	if (err)
32		dev_dbg(&adapter->pdev->dev, "Unable to send opcode %d to PF, err %s, aq_err %s\n",
33			op, iavf_stat_str(hw, err),
34			iavf_aq_str(hw, hw->aq.asq_last_status));
35	return err;
36}
37
38/**
39 * iavf_send_api_ver
40 * @adapter: adapter structure
41 *
42 * Send API version admin queue message to the PF. The reply is not checked
43 * in this function. Returns 0 if the message was successfully
44 * sent, or one of the IAVF_ADMIN_QUEUE_ERROR_ statuses if not.
45 **/
46int iavf_send_api_ver(struct iavf_adapter *adapter)
47{
48	struct virtchnl_version_info vvi;
49
50	vvi.major = VIRTCHNL_VERSION_MAJOR;
51	vvi.minor = VIRTCHNL_VERSION_MINOR;
52
53	return iavf_send_pf_msg(adapter, VIRTCHNL_OP_VERSION, (u8 *)&vvi,
54				sizeof(vvi));
55}
56
57/**
58 * iavf_verify_api_ver
59 * @adapter: adapter structure
60 *
61 * Compare API versions with the PF. Must be called after admin queue is
62 * initialized. Returns 0 if API versions match, -EIO if they do not,
63 * IAVF_ERR_ADMIN_QUEUE_NO_WORK if the admin queue is empty, and any errors
64 * from the firmware are propagated.
65 **/
66int iavf_verify_api_ver(struct iavf_adapter *adapter)
67{
68	struct virtchnl_version_info *pf_vvi;
69	struct iavf_hw *hw = &adapter->hw;
70	struct iavf_arq_event_info event;
71	enum virtchnl_ops op;
72	enum iavf_status err;
73
74	event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
75	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
76	if (!event.msg_buf) {
77		err = -ENOMEM;
78		goto out;
79	}
80
81	while (1) {
82		err = iavf_clean_arq_element(hw, &event, NULL);
83		/* When the AQ is empty, iavf_clean_arq_element will return
84		 * nonzero and this loop will terminate.
85		 */
86		if (err)
87			goto out_alloc;
88		op =
89		    (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
90		if (op == VIRTCHNL_OP_VERSION)
91			break;
92	}
93
94
95	err = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
96	if (err)
97		goto out_alloc;
98
99	if (op != VIRTCHNL_OP_VERSION) {
100		dev_info(&adapter->pdev->dev, "Invalid reply type %d from PF\n",
101			op);
102		err = -EIO;
103		goto out_alloc;
104	}
105
106	pf_vvi = (struct virtchnl_version_info *)event.msg_buf;
107	adapter->pf_version = *pf_vvi;
108
109	if ((pf_vvi->major > VIRTCHNL_VERSION_MAJOR) ||
110	    ((pf_vvi->major == VIRTCHNL_VERSION_MAJOR) &&
111	     (pf_vvi->minor > VIRTCHNL_VERSION_MINOR)))
112		err = -EIO;
113
114out_alloc:
115	kfree(event.msg_buf);
116out:
117	return err;
118}
119
120/**
121 * iavf_send_vf_config_msg
122 * @adapter: adapter structure
123 *
124 * Send VF configuration request admin queue message to the PF. The reply
125 * is not checked in this function. Returns 0 if the message was
126 * successfully sent, or one of the IAVF_ADMIN_QUEUE_ERROR_ statuses if not.
127 **/
128int iavf_send_vf_config_msg(struct iavf_adapter *adapter)
129{
130	u32 caps;
131
132	caps = VIRTCHNL_VF_OFFLOAD_L2 |
133	       VIRTCHNL_VF_OFFLOAD_RSS_PF |
134	       VIRTCHNL_VF_OFFLOAD_RSS_AQ |
135	       VIRTCHNL_VF_OFFLOAD_RSS_REG |
136	       VIRTCHNL_VF_OFFLOAD_VLAN |
137	       VIRTCHNL_VF_OFFLOAD_WB_ON_ITR |
138	       VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2 |
139	       VIRTCHNL_VF_OFFLOAD_ENCAP |
140	       VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM |
141	       VIRTCHNL_VF_OFFLOAD_REQ_QUEUES |
142	       VIRTCHNL_VF_OFFLOAD_ADQ |
143	       VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
144
145	adapter->current_op = VIRTCHNL_OP_GET_VF_RESOURCES;
146	adapter->aq_required &= ~IAVF_FLAG_AQ_GET_CONFIG;
147	if (PF_IS_V11(adapter))
148		return iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_VF_RESOURCES,
149					(u8 *)&caps, sizeof(caps));
150	else
151		return iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_VF_RESOURCES,
152					NULL, 0);
153}
154
155/**
156 * iavf_validate_num_queues
157 * @adapter: adapter structure
158 *
159 * Validate that the number of queues the PF has sent in
160 * VIRTCHNL_OP_GET_VF_RESOURCES is not larger than the VF can handle.
161 **/
162static void iavf_validate_num_queues(struct iavf_adapter *adapter)
163{
164	if (adapter->vf_res->num_queue_pairs > IAVF_MAX_REQ_QUEUES) {
165		struct virtchnl_vsi_resource *vsi_res;
166		int i;
167
168		dev_info(&adapter->pdev->dev, "Received %d queues, but can only have a max of %d\n",
169			 adapter->vf_res->num_queue_pairs,
170			 IAVF_MAX_REQ_QUEUES);
171		dev_info(&adapter->pdev->dev, "Fixing by reducing queues to %d\n",
172			 IAVF_MAX_REQ_QUEUES);
173		adapter->vf_res->num_queue_pairs = IAVF_MAX_REQ_QUEUES;
174		for (i = 0; i < adapter->vf_res->num_vsis; i++) {
175			vsi_res = &adapter->vf_res->vsi_res[i];
176			vsi_res->num_queue_pairs = IAVF_MAX_REQ_QUEUES;
177		}
178	}
179}
180
181/**
182 * iavf_get_vf_config
183 * @adapter: private adapter structure
184 *
185 * Get VF configuration from PF and populate hw structure. Must be called after
186 * admin queue is initialized. Busy waits until response is received from PF,
187 * with maximum timeout. Response from PF is returned in the buffer for further
188 * processing by the caller.
189 **/
190int iavf_get_vf_config(struct iavf_adapter *adapter)
191{
192	struct iavf_hw *hw = &adapter->hw;
193	struct iavf_arq_event_info event;
194	enum virtchnl_ops op;
195	enum iavf_status err;
196	u16 len;
197
198	len =  sizeof(struct virtchnl_vf_resource) +
199		IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource);
200	event.buf_len = len;
201	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
202	if (!event.msg_buf) {
203		err = -ENOMEM;
204		goto out;
205	}
206
207	while (1) {
208		/* When the AQ is empty, iavf_clean_arq_element will return
209		 * nonzero and this loop will terminate.
210		 */
211		err = iavf_clean_arq_element(hw, &event, NULL);
212		if (err)
213			goto out_alloc;
214		op =
215		    (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
216		if (op == VIRTCHNL_OP_GET_VF_RESOURCES)
217			break;
218	}
219
220	err = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
221	memcpy(adapter->vf_res, event.msg_buf, min(event.msg_len, len));
222
223	/* some PFs send more queues than we should have so validate that
224	 * we aren't getting too many queues
225	 */
226	if (!err)
227		iavf_validate_num_queues(adapter);
228	iavf_vf_parse_hw_config(hw, adapter->vf_res);
229out_alloc:
230	kfree(event.msg_buf);
231out:
232	return err;
233}
234
235/**
236 * iavf_configure_queues
237 * @adapter: adapter structure
238 *
239 * Request that the PF set up our (previously allocated) queues.
240 **/
241void iavf_configure_queues(struct iavf_adapter *adapter)
242{
243	struct virtchnl_vsi_queue_config_info *vqci;
244	int i, max_frame = adapter->vf_res->max_mtu;
245	int pairs = adapter->num_active_queues;
246	struct virtchnl_queue_pair_info *vqpi;
247	size_t len;
248
249	if (max_frame > IAVF_MAX_RXBUFFER || !max_frame)
250		max_frame = IAVF_MAX_RXBUFFER;
251
252	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
253		/* bail because we already have a command pending */
254		dev_err(&adapter->pdev->dev, "Cannot configure queues, command %d pending\n",
255			adapter->current_op);
256		return;
257	}
258	adapter->current_op = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
259	len = struct_size(vqci, qpair, pairs);
260	vqci = kzalloc(len, GFP_KERNEL);
261	if (!vqci)
262		return;
263
264	/* Limit maximum frame size when jumbo frames is not enabled */
265	if (!(adapter->flags & IAVF_FLAG_LEGACY_RX) &&
266	    (adapter->netdev->mtu <= ETH_DATA_LEN))
267		max_frame = IAVF_RXBUFFER_1536 - NET_IP_ALIGN;
268
269	vqci->vsi_id = adapter->vsi_res->vsi_id;
270	vqci->num_queue_pairs = pairs;
271	vqpi = vqci->qpair;
272	/* Size check is not needed here - HW max is 16 queue pairs, and we
273	 * can fit info for 31 of them into the AQ buffer before it overflows.
274	 */
275	for (i = 0; i < pairs; i++) {
276		vqpi->txq.vsi_id = vqci->vsi_id;
277		vqpi->txq.queue_id = i;
278		vqpi->txq.ring_len = adapter->tx_rings[i].count;
279		vqpi->txq.dma_ring_addr = adapter->tx_rings[i].dma;
280		vqpi->rxq.vsi_id = vqci->vsi_id;
281		vqpi->rxq.queue_id = i;
282		vqpi->rxq.ring_len = adapter->rx_rings[i].count;
283		vqpi->rxq.dma_ring_addr = adapter->rx_rings[i].dma;
284		vqpi->rxq.max_pkt_size = max_frame;
285		vqpi->rxq.databuffer_size =
286			ALIGN(adapter->rx_rings[i].rx_buf_len,
287			      BIT_ULL(IAVF_RXQ_CTX_DBUFF_SHIFT));
288		vqpi++;
289	}
290
291	adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_QUEUES;
292	iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
293			 (u8 *)vqci, len);
294	kfree(vqci);
295}
296
297/**
298 * iavf_enable_queues
299 * @adapter: adapter structure
300 *
301 * Request that the PF enable all of our queues.
302 **/
303void iavf_enable_queues(struct iavf_adapter *adapter)
304{
305	struct virtchnl_queue_select vqs;
306
307	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
308		/* bail because we already have a command pending */
309		dev_err(&adapter->pdev->dev, "Cannot enable queues, command %d pending\n",
310			adapter->current_op);
311		return;
312	}
313	adapter->current_op = VIRTCHNL_OP_ENABLE_QUEUES;
314	vqs.vsi_id = adapter->vsi_res->vsi_id;
315	vqs.tx_queues = BIT(adapter->num_active_queues) - 1;
316	vqs.rx_queues = vqs.tx_queues;
317	adapter->aq_required &= ~IAVF_FLAG_AQ_ENABLE_QUEUES;
318	iavf_send_pf_msg(adapter, VIRTCHNL_OP_ENABLE_QUEUES,
319			 (u8 *)&vqs, sizeof(vqs));
320}
321
322/**
323 * iavf_disable_queues
324 * @adapter: adapter structure
325 *
326 * Request that the PF disable all of our queues.
327 **/
328void iavf_disable_queues(struct iavf_adapter *adapter)
329{
330	struct virtchnl_queue_select vqs;
331
332	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
333		/* bail because we already have a command pending */
334		dev_err(&adapter->pdev->dev, "Cannot disable queues, command %d pending\n",
335			adapter->current_op);
336		return;
337	}
338	adapter->current_op = VIRTCHNL_OP_DISABLE_QUEUES;
339	vqs.vsi_id = adapter->vsi_res->vsi_id;
340	vqs.tx_queues = BIT(adapter->num_active_queues) - 1;
341	vqs.rx_queues = vqs.tx_queues;
342	adapter->aq_required &= ~IAVF_FLAG_AQ_DISABLE_QUEUES;
343	iavf_send_pf_msg(adapter, VIRTCHNL_OP_DISABLE_QUEUES,
344			 (u8 *)&vqs, sizeof(vqs));
345}
346
347/**
348 * iavf_map_queues
349 * @adapter: adapter structure
350 *
351 * Request that the PF map queues to interrupt vectors. Misc causes, including
352 * admin queue, are always mapped to vector 0.
353 **/
354void iavf_map_queues(struct iavf_adapter *adapter)
355{
356	struct virtchnl_irq_map_info *vimi;
357	struct virtchnl_vector_map *vecmap;
358	struct iavf_q_vector *q_vector;
359	int v_idx, q_vectors;
360	size_t len;
361
362	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
363		/* bail because we already have a command pending */
364		dev_err(&adapter->pdev->dev, "Cannot map queues to vectors, command %d pending\n",
365			adapter->current_op);
366		return;
367	}
368	adapter->current_op = VIRTCHNL_OP_CONFIG_IRQ_MAP;
369
370	q_vectors = adapter->num_msix_vectors - NONQ_VECS;
371
372	len = struct_size(vimi, vecmap, adapter->num_msix_vectors);
373	vimi = kzalloc(len, GFP_KERNEL);
374	if (!vimi)
375		return;
376
377	vimi->num_vectors = adapter->num_msix_vectors;
378	/* Queue vectors first */
379	for (v_idx = 0; v_idx < q_vectors; v_idx++) {
380		q_vector = &adapter->q_vectors[v_idx];
381		vecmap = &vimi->vecmap[v_idx];
382
383		vecmap->vsi_id = adapter->vsi_res->vsi_id;
384		vecmap->vector_id = v_idx + NONQ_VECS;
385		vecmap->txq_map = q_vector->ring_mask;
386		vecmap->rxq_map = q_vector->ring_mask;
387		vecmap->rxitr_idx = IAVF_RX_ITR;
388		vecmap->txitr_idx = IAVF_TX_ITR;
389	}
390	/* Misc vector last - this is only for AdminQ messages */
391	vecmap = &vimi->vecmap[v_idx];
392	vecmap->vsi_id = adapter->vsi_res->vsi_id;
393	vecmap->vector_id = 0;
394	vecmap->txq_map = 0;
395	vecmap->rxq_map = 0;
396
397	adapter->aq_required &= ~IAVF_FLAG_AQ_MAP_VECTORS;
398	iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_IRQ_MAP,
399			 (u8 *)vimi, len);
400	kfree(vimi);
401}
402
403/**
404 * iavf_add_ether_addrs
405 * @adapter: adapter structure
406 *
407 * Request that the PF add one or more addresses to our filters.
408 **/
409void iavf_add_ether_addrs(struct iavf_adapter *adapter)
410{
411	struct virtchnl_ether_addr_list *veal;
412	struct iavf_mac_filter *f;
413	int i = 0, count = 0;
414	bool more = false;
415	size_t len;
416
417	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
418		/* bail because we already have a command pending */
419		dev_err(&adapter->pdev->dev, "Cannot add filters, command %d pending\n",
420			adapter->current_op);
421		return;
422	}
423
424	spin_lock_bh(&adapter->mac_vlan_list_lock);
425
426	list_for_each_entry(f, &adapter->mac_filter_list, list) {
427		if (f->add)
428			count++;
429	}
430	if (!count) {
431		adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_MAC_FILTER;
432		spin_unlock_bh(&adapter->mac_vlan_list_lock);
433		return;
434	}
435	adapter->current_op = VIRTCHNL_OP_ADD_ETH_ADDR;
436
437	len = struct_size(veal, list, count);
438	if (len > IAVF_MAX_AQ_BUF_SIZE) {
439		dev_warn(&adapter->pdev->dev, "Too many add MAC changes in one request\n");
440		count = (IAVF_MAX_AQ_BUF_SIZE -
441			 sizeof(struct virtchnl_ether_addr_list)) /
442			sizeof(struct virtchnl_ether_addr);
443		len = struct_size(veal, list, count);
444		more = true;
445	}
446
447	veal = kzalloc(len, GFP_ATOMIC);
448	if (!veal) {
449		spin_unlock_bh(&adapter->mac_vlan_list_lock);
450		return;
451	}
452
453	veal->vsi_id = adapter->vsi_res->vsi_id;
454	veal->num_elements = count;
455	list_for_each_entry(f, &adapter->mac_filter_list, list) {
456		if (f->add) {
457			ether_addr_copy(veal->list[i].addr, f->macaddr);
458			i++;
459			f->add = false;
460			if (i == count)
461				break;
462		}
463	}
464	if (!more)
465		adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_MAC_FILTER;
466
467	spin_unlock_bh(&adapter->mac_vlan_list_lock);
468
469	iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_ETH_ADDR, (u8 *)veal, len);
470	kfree(veal);
471}
472
473/**
474 * iavf_del_ether_addrs
475 * @adapter: adapter structure
476 *
477 * Request that the PF remove one or more addresses from our filters.
478 **/
479void iavf_del_ether_addrs(struct iavf_adapter *adapter)
480{
481	struct virtchnl_ether_addr_list *veal;
482	struct iavf_mac_filter *f, *ftmp;
483	int i = 0, count = 0;
484	bool more = false;
485	size_t len;
486
487	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
488		/* bail because we already have a command pending */
489		dev_err(&adapter->pdev->dev, "Cannot remove filters, command %d pending\n",
490			adapter->current_op);
491		return;
492	}
493
494	spin_lock_bh(&adapter->mac_vlan_list_lock);
495
496	list_for_each_entry(f, &adapter->mac_filter_list, list) {
497		if (f->remove)
498			count++;
499	}
500	if (!count) {
501		adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_MAC_FILTER;
502		spin_unlock_bh(&adapter->mac_vlan_list_lock);
503		return;
504	}
505	adapter->current_op = VIRTCHNL_OP_DEL_ETH_ADDR;
506
507	len = struct_size(veal, list, count);
508	if (len > IAVF_MAX_AQ_BUF_SIZE) {
509		dev_warn(&adapter->pdev->dev, "Too many delete MAC changes in one request\n");
510		count = (IAVF_MAX_AQ_BUF_SIZE -
511			 sizeof(struct virtchnl_ether_addr_list)) /
512			sizeof(struct virtchnl_ether_addr);
513		len = struct_size(veal, list, count);
514		more = true;
515	}
516	veal = kzalloc(len, GFP_ATOMIC);
517	if (!veal) {
518		spin_unlock_bh(&adapter->mac_vlan_list_lock);
519		return;
520	}
521
522	veal->vsi_id = adapter->vsi_res->vsi_id;
523	veal->num_elements = count;
524	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
525		if (f->remove) {
526			ether_addr_copy(veal->list[i].addr, f->macaddr);
527			i++;
528			list_del(&f->list);
529			kfree(f);
530			if (i == count)
531				break;
532		}
533	}
534	if (!more)
535		adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_MAC_FILTER;
536
537	spin_unlock_bh(&adapter->mac_vlan_list_lock);
538
539	iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_ETH_ADDR, (u8 *)veal, len);
540	kfree(veal);
541}
542
543/**
544 * iavf_mac_add_ok
545 * @adapter: adapter structure
546 *
547 * Submit list of filters based on PF response.
548 **/
549static void iavf_mac_add_ok(struct iavf_adapter *adapter)
550{
551	struct iavf_mac_filter *f, *ftmp;
552
553	spin_lock_bh(&adapter->mac_vlan_list_lock);
554	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
555		f->is_new_mac = false;
556	}
557	spin_unlock_bh(&adapter->mac_vlan_list_lock);
558}
559
560/**
561 * iavf_mac_add_reject
562 * @adapter: adapter structure
563 *
564 * Remove filters from list based on PF response.
565 **/
566static void iavf_mac_add_reject(struct iavf_adapter *adapter)
567{
568	struct net_device *netdev = adapter->netdev;
569	struct iavf_mac_filter *f, *ftmp;
570
571	spin_lock_bh(&adapter->mac_vlan_list_lock);
572	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
573		if (f->remove && ether_addr_equal(f->macaddr, netdev->dev_addr))
574			f->remove = false;
575
576		if (f->is_new_mac) {
577			list_del(&f->list);
578			kfree(f);
579		}
580	}
581	spin_unlock_bh(&adapter->mac_vlan_list_lock);
582}
583
584/**
585 * iavf_add_vlans
586 * @adapter: adapter structure
587 *
588 * Request that the PF add one or more VLAN filters to our VSI.
589 **/
590void iavf_add_vlans(struct iavf_adapter *adapter)
591{
592	struct virtchnl_vlan_filter_list *vvfl;
593	int len, i = 0, count = 0;
594	struct iavf_vlan_filter *f;
595	bool more = false;
596
597	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
598		/* bail because we already have a command pending */
599		dev_err(&adapter->pdev->dev, "Cannot add VLANs, command %d pending\n",
600			adapter->current_op);
601		return;
602	}
603
604	spin_lock_bh(&adapter->mac_vlan_list_lock);
605
606	list_for_each_entry(f, &adapter->vlan_filter_list, list) {
607		if (f->add)
608			count++;
609	}
610	if (!count) {
611		adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_VLAN_FILTER;
612		spin_unlock_bh(&adapter->mac_vlan_list_lock);
613		return;
614	}
615	adapter->current_op = VIRTCHNL_OP_ADD_VLAN;
616
617	len = sizeof(struct virtchnl_vlan_filter_list) +
618	      (count * sizeof(u16));
619	if (len > IAVF_MAX_AQ_BUF_SIZE) {
620		dev_warn(&adapter->pdev->dev, "Too many add VLAN changes in one request\n");
621		count = (IAVF_MAX_AQ_BUF_SIZE -
622			 sizeof(struct virtchnl_vlan_filter_list)) /
623			sizeof(u16);
624		len = sizeof(struct virtchnl_vlan_filter_list) +
625		      (count * sizeof(u16));
626		more = true;
627	}
628	vvfl = kzalloc(len, GFP_ATOMIC);
629	if (!vvfl) {
630		spin_unlock_bh(&adapter->mac_vlan_list_lock);
631		return;
632	}
633
634	vvfl->vsi_id = adapter->vsi_res->vsi_id;
635	vvfl->num_elements = count;
636	list_for_each_entry(f, &adapter->vlan_filter_list, list) {
637		if (f->add) {
638			vvfl->vlan_id[i] = f->vlan;
639			i++;
640			f->add = false;
641			if (i == count)
642				break;
643		}
644	}
645	if (!more)
646		adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_VLAN_FILTER;
647
648	spin_unlock_bh(&adapter->mac_vlan_list_lock);
649
650	iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_VLAN, (u8 *)vvfl, len);
651	kfree(vvfl);
652}
653
654/**
655 * iavf_del_vlans
656 * @adapter: adapter structure
657 *
658 * Request that the PF remove one or more VLAN filters from our VSI.
659 **/
660void iavf_del_vlans(struct iavf_adapter *adapter)
661{
662	struct virtchnl_vlan_filter_list *vvfl;
663	struct iavf_vlan_filter *f, *ftmp;
664	int len, i = 0, count = 0;
665	bool more = false;
666
667	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
668		/* bail because we already have a command pending */
669		dev_err(&adapter->pdev->dev, "Cannot remove VLANs, command %d pending\n",
670			adapter->current_op);
671		return;
672	}
673
674	spin_lock_bh(&adapter->mac_vlan_list_lock);
675
676	list_for_each_entry(f, &adapter->vlan_filter_list, list) {
677		if (f->remove)
678			count++;
679	}
680	if (!count) {
681		adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_VLAN_FILTER;
682		spin_unlock_bh(&adapter->mac_vlan_list_lock);
683		return;
684	}
685	adapter->current_op = VIRTCHNL_OP_DEL_VLAN;
686
687	len = sizeof(struct virtchnl_vlan_filter_list) +
688	      (count * sizeof(u16));
689	if (len > IAVF_MAX_AQ_BUF_SIZE) {
690		dev_warn(&adapter->pdev->dev, "Too many delete VLAN changes in one request\n");
691		count = (IAVF_MAX_AQ_BUF_SIZE -
692			 sizeof(struct virtchnl_vlan_filter_list)) /
693			sizeof(u16);
694		len = sizeof(struct virtchnl_vlan_filter_list) +
695		      (count * sizeof(u16));
696		more = true;
697	}
698	vvfl = kzalloc(len, GFP_ATOMIC);
699	if (!vvfl) {
700		spin_unlock_bh(&adapter->mac_vlan_list_lock);
701		return;
702	}
703
704	vvfl->vsi_id = adapter->vsi_res->vsi_id;
705	vvfl->num_elements = count;
706	list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
707		if (f->remove) {
708			vvfl->vlan_id[i] = f->vlan;
709			i++;
710			list_del(&f->list);
711			kfree(f);
712			if (i == count)
713				break;
714		}
715	}
716	if (!more)
717		adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_VLAN_FILTER;
718
719	spin_unlock_bh(&adapter->mac_vlan_list_lock);
720
721	iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_VLAN, (u8 *)vvfl, len);
722	kfree(vvfl);
723}
724
725/**
726 * iavf_set_promiscuous
727 * @adapter: adapter structure
728 * @flags: bitmask to control unicast/multicast promiscuous.
729 *
730 * Request that the PF enable promiscuous mode for our VSI.
731 **/
732void iavf_set_promiscuous(struct iavf_adapter *adapter, int flags)
733{
734	struct virtchnl_promisc_info vpi;
735	int promisc_all;
736
737	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
738		/* bail because we already have a command pending */
739		dev_err(&adapter->pdev->dev, "Cannot set promiscuous mode, command %d pending\n",
740			adapter->current_op);
741		return;
742	}
743
744	promisc_all = FLAG_VF_UNICAST_PROMISC |
745		      FLAG_VF_MULTICAST_PROMISC;
746	if ((flags & promisc_all) == promisc_all) {
747		adapter->flags |= IAVF_FLAG_PROMISC_ON;
748		adapter->aq_required &= ~IAVF_FLAG_AQ_REQUEST_PROMISC;
749		dev_info(&adapter->pdev->dev, "Entering promiscuous mode\n");
750	}
751
752	if (flags & FLAG_VF_MULTICAST_PROMISC) {
753		adapter->flags |= IAVF_FLAG_ALLMULTI_ON;
754		adapter->aq_required &= ~IAVF_FLAG_AQ_REQUEST_ALLMULTI;
755		dev_info(&adapter->pdev->dev, "Entering multicast promiscuous mode\n");
756	}
757
758	if (!flags) {
759		adapter->flags &= ~(IAVF_FLAG_PROMISC_ON |
760				    IAVF_FLAG_ALLMULTI_ON);
761		adapter->aq_required &= ~(IAVF_FLAG_AQ_RELEASE_PROMISC |
762					  IAVF_FLAG_AQ_RELEASE_ALLMULTI);
763		dev_info(&adapter->pdev->dev, "Leaving promiscuous mode\n");
764	}
765
766	adapter->current_op = VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
767	vpi.vsi_id = adapter->vsi_res->vsi_id;
768	vpi.flags = flags;
769	iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
770			 (u8 *)&vpi, sizeof(vpi));
771}
772
773/**
774 * iavf_request_stats
775 * @adapter: adapter structure
776 *
777 * Request VSI statistics from PF.
778 **/
779void iavf_request_stats(struct iavf_adapter *adapter)
780{
781	struct virtchnl_queue_select vqs;
782
783	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
784		/* no error message, this isn't crucial */
785		return;
786	}
787	adapter->current_op = VIRTCHNL_OP_GET_STATS;
788	vqs.vsi_id = adapter->vsi_res->vsi_id;
789	/* queue maps are ignored for this message - only the vsi is used */
790	if (iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_STATS, (u8 *)&vqs,
791			     sizeof(vqs)))
792		/* if the request failed, don't lock out others */
793		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
794}
795
796/**
797 * iavf_get_hena
798 * @adapter: adapter structure
799 *
800 * Request hash enable capabilities from PF
801 **/
802void iavf_get_hena(struct iavf_adapter *adapter)
803{
804	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
805		/* bail because we already have a command pending */
806		dev_err(&adapter->pdev->dev, "Cannot get RSS hash capabilities, command %d pending\n",
807			adapter->current_op);
808		return;
809	}
810	adapter->current_op = VIRTCHNL_OP_GET_RSS_HENA_CAPS;
811	adapter->aq_required &= ~IAVF_FLAG_AQ_GET_HENA;
812	iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_RSS_HENA_CAPS, NULL, 0);
813}
814
815/**
816 * iavf_set_hena
817 * @adapter: adapter structure
818 *
819 * Request the PF to set our RSS hash capabilities
820 **/
821void iavf_set_hena(struct iavf_adapter *adapter)
822{
823	struct virtchnl_rss_hena vrh;
824
825	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
826		/* bail because we already have a command pending */
827		dev_err(&adapter->pdev->dev, "Cannot set RSS hash enable, command %d pending\n",
828			adapter->current_op);
829		return;
830	}
831	vrh.hena = adapter->hena;
832	adapter->current_op = VIRTCHNL_OP_SET_RSS_HENA;
833	adapter->aq_required &= ~IAVF_FLAG_AQ_SET_HENA;
834	iavf_send_pf_msg(adapter, VIRTCHNL_OP_SET_RSS_HENA, (u8 *)&vrh,
835			 sizeof(vrh));
836}
837
838/**
839 * iavf_set_rss_key
840 * @adapter: adapter structure
841 *
842 * Request the PF to set our RSS hash key
843 **/
844void iavf_set_rss_key(struct iavf_adapter *adapter)
845{
846	struct virtchnl_rss_key *vrk;
847	int len;
848
849	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
850		/* bail because we already have a command pending */
851		dev_err(&adapter->pdev->dev, "Cannot set RSS key, command %d pending\n",
852			adapter->current_op);
853		return;
854	}
855	len = sizeof(struct virtchnl_rss_key) +
856	      (adapter->rss_key_size * sizeof(u8)) - 1;
857	vrk = kzalloc(len, GFP_KERNEL);
858	if (!vrk)
859		return;
860	vrk->vsi_id = adapter->vsi.id;
861	vrk->key_len = adapter->rss_key_size;
862	memcpy(vrk->key, adapter->rss_key, adapter->rss_key_size);
863
864	adapter->current_op = VIRTCHNL_OP_CONFIG_RSS_KEY;
865	adapter->aq_required &= ~IAVF_FLAG_AQ_SET_RSS_KEY;
866	iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_RSS_KEY, (u8 *)vrk, len);
867	kfree(vrk);
868}
869
870/**
871 * iavf_set_rss_lut
872 * @adapter: adapter structure
873 *
874 * Request the PF to set our RSS lookup table
875 **/
876void iavf_set_rss_lut(struct iavf_adapter *adapter)
877{
878	struct virtchnl_rss_lut *vrl;
879	int len;
880
881	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
882		/* bail because we already have a command pending */
883		dev_err(&adapter->pdev->dev, "Cannot set RSS LUT, command %d pending\n",
884			adapter->current_op);
885		return;
886	}
887	len = sizeof(struct virtchnl_rss_lut) +
888	      (adapter->rss_lut_size * sizeof(u8)) - 1;
889	vrl = kzalloc(len, GFP_KERNEL);
890	if (!vrl)
891		return;
892	vrl->vsi_id = adapter->vsi.id;
893	vrl->lut_entries = adapter->rss_lut_size;
894	memcpy(vrl->lut, adapter->rss_lut, adapter->rss_lut_size);
895	adapter->current_op = VIRTCHNL_OP_CONFIG_RSS_LUT;
896	adapter->aq_required &= ~IAVF_FLAG_AQ_SET_RSS_LUT;
897	iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_RSS_LUT, (u8 *)vrl, len);
898	kfree(vrl);
899}
900
901/**
902 * iavf_enable_vlan_stripping
903 * @adapter: adapter structure
904 *
905 * Request VLAN header stripping to be enabled
906 **/
907void iavf_enable_vlan_stripping(struct iavf_adapter *adapter)
908{
909	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
910		/* bail because we already have a command pending */
911		dev_err(&adapter->pdev->dev, "Cannot enable stripping, command %d pending\n",
912			adapter->current_op);
913		return;
914	}
915	adapter->current_op = VIRTCHNL_OP_ENABLE_VLAN_STRIPPING;
916	adapter->aq_required &= ~IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
917	iavf_send_pf_msg(adapter, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING, NULL, 0);
918}
919
920/**
921 * iavf_disable_vlan_stripping
922 * @adapter: adapter structure
923 *
924 * Request VLAN header stripping to be disabled
925 **/
926void iavf_disable_vlan_stripping(struct iavf_adapter *adapter)
927{
928	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
929		/* bail because we already have a command pending */
930		dev_err(&adapter->pdev->dev, "Cannot disable stripping, command %d pending\n",
931			adapter->current_op);
932		return;
933	}
934	adapter->current_op = VIRTCHNL_OP_DISABLE_VLAN_STRIPPING;
935	adapter->aq_required &= ~IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
936	iavf_send_pf_msg(adapter, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING, NULL, 0);
937}
938
939#define IAVF_MAX_SPEED_STRLEN	13
940
941/**
942 * iavf_print_link_message - print link up or down
943 * @adapter: adapter structure
944 *
945 * Log a message telling the world of our wonderous link status
946 */
947static void iavf_print_link_message(struct iavf_adapter *adapter)
948{
949	struct net_device *netdev = adapter->netdev;
950	int link_speed_mbps;
951	char *speed;
952
953	if (!adapter->link_up) {
954		netdev_info(netdev, "NIC Link is Down\n");
955		return;
956	}
957
958	speed = kcalloc(1, IAVF_MAX_SPEED_STRLEN, GFP_KERNEL);
959	if (!speed)
960		return;
961
962	if (ADV_LINK_SUPPORT(adapter)) {
963		link_speed_mbps = adapter->link_speed_mbps;
964		goto print_link_msg;
965	}
966
967	switch (adapter->link_speed) {
968	case VIRTCHNL_LINK_SPEED_40GB:
969		link_speed_mbps = SPEED_40000;
970		break;
971	case VIRTCHNL_LINK_SPEED_25GB:
972		link_speed_mbps = SPEED_25000;
973		break;
974	case VIRTCHNL_LINK_SPEED_20GB:
975		link_speed_mbps = SPEED_20000;
976		break;
977	case VIRTCHNL_LINK_SPEED_10GB:
978		link_speed_mbps = SPEED_10000;
979		break;
980	case VIRTCHNL_LINK_SPEED_5GB:
981		link_speed_mbps = SPEED_5000;
982		break;
983	case VIRTCHNL_LINK_SPEED_2_5GB:
984		link_speed_mbps = SPEED_2500;
985		break;
986	case VIRTCHNL_LINK_SPEED_1GB:
987		link_speed_mbps = SPEED_1000;
988		break;
989	case VIRTCHNL_LINK_SPEED_100MB:
990		link_speed_mbps = SPEED_100;
991		break;
992	default:
993		link_speed_mbps = SPEED_UNKNOWN;
994		break;
995	}
996
997print_link_msg:
998	if (link_speed_mbps > SPEED_1000) {
999		if (link_speed_mbps == SPEED_2500)
1000			snprintf(speed, IAVF_MAX_SPEED_STRLEN, "2.5 Gbps");
1001		else
1002			/* convert to Gbps inline */
1003			snprintf(speed, IAVF_MAX_SPEED_STRLEN, "%d %s",
1004				 link_speed_mbps / 1000, "Gbps");
1005	} else if (link_speed_mbps == SPEED_UNKNOWN) {
1006		snprintf(speed, IAVF_MAX_SPEED_STRLEN, "%s", "Unknown Mbps");
1007	} else {
1008		snprintf(speed, IAVF_MAX_SPEED_STRLEN, "%u %s",
1009			 link_speed_mbps, "Mbps");
1010	}
1011
1012	netdev_info(netdev, "NIC Link is Up Speed is %s Full Duplex\n", speed);
1013	kfree(speed);
1014}
1015
1016/**
1017 * iavf_get_vpe_link_status
1018 * @adapter: adapter structure
1019 * @vpe: virtchnl_pf_event structure
1020 *
1021 * Helper function for determining the link status
1022 **/
1023static bool
1024iavf_get_vpe_link_status(struct iavf_adapter *adapter,
1025			 struct virtchnl_pf_event *vpe)
1026{
1027	if (ADV_LINK_SUPPORT(adapter))
1028		return vpe->event_data.link_event_adv.link_status;
1029	else
1030		return vpe->event_data.link_event.link_status;
1031}
1032
1033/**
1034 * iavf_set_adapter_link_speed_from_vpe
1035 * @adapter: adapter structure for which we are setting the link speed
1036 * @vpe: virtchnl_pf_event structure that contains the link speed we are setting
1037 *
1038 * Helper function for setting iavf_adapter link speed
1039 **/
1040static void
1041iavf_set_adapter_link_speed_from_vpe(struct iavf_adapter *adapter,
1042				     struct virtchnl_pf_event *vpe)
1043{
1044	if (ADV_LINK_SUPPORT(adapter))
1045		adapter->link_speed_mbps =
1046			vpe->event_data.link_event_adv.link_speed;
1047	else
1048		adapter->link_speed = vpe->event_data.link_event.link_speed;
1049}
1050
1051/**
1052 * iavf_enable_channel
1053 * @adapter: adapter structure
1054 *
1055 * Request that the PF enable channels as specified by
1056 * the user via tc tool.
1057 **/
1058void iavf_enable_channels(struct iavf_adapter *adapter)
1059{
1060	struct virtchnl_tc_info *vti = NULL;
1061	size_t len;
1062	int i;
1063
1064	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1065		/* bail because we already have a command pending */
1066		dev_err(&adapter->pdev->dev, "Cannot configure mqprio, command %d pending\n",
1067			adapter->current_op);
1068		return;
1069	}
1070
1071	len = struct_size(vti, list, adapter->num_tc - 1);
1072	vti = kzalloc(len, GFP_KERNEL);
1073	if (!vti)
1074		return;
1075	vti->num_tc = adapter->num_tc;
1076	for (i = 0; i < vti->num_tc; i++) {
1077		vti->list[i].count = adapter->ch_config.ch_info[i].count;
1078		vti->list[i].offset = adapter->ch_config.ch_info[i].offset;
1079		vti->list[i].pad = 0;
1080		vti->list[i].max_tx_rate =
1081				adapter->ch_config.ch_info[i].max_tx_rate;
1082	}
1083
1084	adapter->ch_config.state = __IAVF_TC_RUNNING;
1085	adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1086	adapter->current_op = VIRTCHNL_OP_ENABLE_CHANNELS;
1087	adapter->aq_required &= ~IAVF_FLAG_AQ_ENABLE_CHANNELS;
1088	iavf_send_pf_msg(adapter, VIRTCHNL_OP_ENABLE_CHANNELS, (u8 *)vti, len);
1089	kfree(vti);
1090}
1091
1092/**
1093 * iavf_disable_channel
1094 * @adapter: adapter structure
1095 *
1096 * Request that the PF disable channels that are configured
1097 **/
1098void iavf_disable_channels(struct iavf_adapter *adapter)
1099{
1100	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1101		/* bail because we already have a command pending */
1102		dev_err(&adapter->pdev->dev, "Cannot configure mqprio, command %d pending\n",
1103			adapter->current_op);
1104		return;
1105	}
1106
1107	adapter->ch_config.state = __IAVF_TC_INVALID;
1108	adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1109	adapter->current_op = VIRTCHNL_OP_DISABLE_CHANNELS;
1110	adapter->aq_required &= ~IAVF_FLAG_AQ_DISABLE_CHANNELS;
1111	iavf_send_pf_msg(adapter, VIRTCHNL_OP_DISABLE_CHANNELS, NULL, 0);
1112}
1113
1114/**
1115 * iavf_print_cloud_filter
1116 * @adapter: adapter structure
1117 * @f: cloud filter to print
1118 *
1119 * Print the cloud filter
1120 **/
1121static void iavf_print_cloud_filter(struct iavf_adapter *adapter,
1122				    struct virtchnl_filter *f)
1123{
1124	switch (f->flow_type) {
1125	case VIRTCHNL_TCP_V4_FLOW:
1126		dev_info(&adapter->pdev->dev, "dst_mac: %pM src_mac: %pM vlan_id: %hu dst_ip: %pI4 src_ip %pI4 dst_port %hu src_port %hu\n",
1127			 &f->data.tcp_spec.dst_mac,
1128			 &f->data.tcp_spec.src_mac,
1129			 ntohs(f->data.tcp_spec.vlan_id),
1130			 &f->data.tcp_spec.dst_ip[0],
1131			 &f->data.tcp_spec.src_ip[0],
1132			 ntohs(f->data.tcp_spec.dst_port),
1133			 ntohs(f->data.tcp_spec.src_port));
1134		break;
1135	case VIRTCHNL_TCP_V6_FLOW:
1136		dev_info(&adapter->pdev->dev, "dst_mac: %pM src_mac: %pM vlan_id: %hu dst_ip: %pI6 src_ip %pI6 dst_port %hu src_port %hu\n",
1137			 &f->data.tcp_spec.dst_mac,
1138			 &f->data.tcp_spec.src_mac,
1139			 ntohs(f->data.tcp_spec.vlan_id),
1140			 &f->data.tcp_spec.dst_ip,
1141			 &f->data.tcp_spec.src_ip,
1142			 ntohs(f->data.tcp_spec.dst_port),
1143			 ntohs(f->data.tcp_spec.src_port));
1144		break;
1145	}
1146}
1147
1148/**
1149 * iavf_add_cloud_filter
1150 * @adapter: adapter structure
1151 *
1152 * Request that the PF add cloud filters as specified
1153 * by the user via tc tool.
1154 **/
1155void iavf_add_cloud_filter(struct iavf_adapter *adapter)
1156{
1157	struct iavf_cloud_filter *cf;
1158	struct virtchnl_filter *f;
1159	int len = 0, count = 0;
1160
1161	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1162		/* bail because we already have a command pending */
1163		dev_err(&adapter->pdev->dev, "Cannot add cloud filter, command %d pending\n",
1164			adapter->current_op);
1165		return;
1166	}
1167	list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1168		if (cf->add) {
1169			count++;
1170			break;
1171		}
1172	}
1173	if (!count) {
1174		adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
1175		return;
1176	}
1177	adapter->current_op = VIRTCHNL_OP_ADD_CLOUD_FILTER;
1178
1179	len = sizeof(struct virtchnl_filter);
1180	f = kzalloc(len, GFP_KERNEL);
1181	if (!f)
1182		return;
1183
1184	list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1185		if (cf->add) {
1186			memcpy(f, &cf->f, sizeof(struct virtchnl_filter));
1187			cf->add = false;
1188			cf->state = __IAVF_CF_ADD_PENDING;
1189			iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_CLOUD_FILTER,
1190					 (u8 *)f, len);
1191		}
1192	}
1193	kfree(f);
1194}
1195
1196/**
1197 * iavf_del_cloud_filter
1198 * @adapter: adapter structure
1199 *
1200 * Request that the PF delete cloud filters as specified
1201 * by the user via tc tool.
1202 **/
1203void iavf_del_cloud_filter(struct iavf_adapter *adapter)
1204{
1205	struct iavf_cloud_filter *cf, *cftmp;
1206	struct virtchnl_filter *f;
1207	int len = 0, count = 0;
1208
1209	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1210		/* bail because we already have a command pending */
1211		dev_err(&adapter->pdev->dev, "Cannot remove cloud filter, command %d pending\n",
1212			adapter->current_op);
1213		return;
1214	}
1215	list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1216		if (cf->del) {
1217			count++;
1218			break;
1219		}
1220	}
1221	if (!count) {
1222		adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1223		return;
1224	}
1225	adapter->current_op = VIRTCHNL_OP_DEL_CLOUD_FILTER;
1226
1227	len = sizeof(struct virtchnl_filter);
1228	f = kzalloc(len, GFP_KERNEL);
1229	if (!f)
1230		return;
1231
1232	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
1233		if (cf->del) {
1234			memcpy(f, &cf->f, sizeof(struct virtchnl_filter));
1235			cf->del = false;
1236			cf->state = __IAVF_CF_DEL_PENDING;
1237			iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_CLOUD_FILTER,
1238					 (u8 *)f, len);
1239		}
1240	}
1241	kfree(f);
1242}
1243
1244/**
1245 * iavf_request_reset
1246 * @adapter: adapter structure
1247 *
1248 * Request that the PF reset this VF. No response is expected.
1249 **/
1250void iavf_request_reset(struct iavf_adapter *adapter)
1251{
1252	/* Don't check CURRENT_OP - this is always higher priority */
1253	iavf_send_pf_msg(adapter, VIRTCHNL_OP_RESET_VF, NULL, 0);
1254	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1255}
1256
1257/**
1258 * iavf_virtchnl_completion
1259 * @adapter: adapter structure
1260 * @v_opcode: opcode sent by PF
1261 * @v_retval: retval sent by PF
1262 * @msg: message sent by PF
1263 * @msglen: message length
1264 *
1265 * Asynchronous completion function for admin queue messages. Rather than busy
1266 * wait, we fire off our requests and assume that no errors will be returned.
1267 * This function handles the reply messages.
1268 **/
1269void iavf_virtchnl_completion(struct iavf_adapter *adapter,
1270			      enum virtchnl_ops v_opcode,
1271			      enum iavf_status v_retval, u8 *msg, u16 msglen)
1272{
1273	struct net_device *netdev = adapter->netdev;
1274
1275	if (v_opcode == VIRTCHNL_OP_EVENT) {
1276		struct virtchnl_pf_event *vpe =
1277			(struct virtchnl_pf_event *)msg;
1278		bool link_up = iavf_get_vpe_link_status(adapter, vpe);
1279
1280		switch (vpe->event) {
1281		case VIRTCHNL_EVENT_LINK_CHANGE:
1282			iavf_set_adapter_link_speed_from_vpe(adapter, vpe);
1283
1284			/* we've already got the right link status, bail */
1285			if (adapter->link_up == link_up)
1286				break;
1287
1288			if (link_up) {
1289				/* If we get link up message and start queues
1290				 * before our queues are configured it will
1291				 * trigger a TX hang. In that case, just ignore
1292				 * the link status message,we'll get another one
1293				 * after we enable queues and actually prepared
1294				 * to send traffic.
1295				 */
1296				if (adapter->state != __IAVF_RUNNING)
1297					break;
1298
1299				/* For ADq enabled VF, we reconfigure VSIs and
1300				 * re-allocate queues. Hence wait till all
1301				 * queues are enabled.
1302				 */
1303				if (adapter->flags &
1304				    IAVF_FLAG_QUEUES_DISABLED)
1305					break;
1306			}
1307
1308			adapter->link_up = link_up;
1309			if (link_up) {
1310				netif_tx_start_all_queues(netdev);
1311				netif_carrier_on(netdev);
1312			} else {
1313				netif_tx_stop_all_queues(netdev);
1314				netif_carrier_off(netdev);
1315			}
1316			iavf_print_link_message(adapter);
1317			break;
1318		case VIRTCHNL_EVENT_RESET_IMPENDING:
1319			dev_info(&adapter->pdev->dev, "Reset warning received from the PF\n");
1320			if (!(adapter->flags & IAVF_FLAG_RESET_PENDING)) {
1321				adapter->flags |= IAVF_FLAG_RESET_PENDING;
1322				dev_info(&adapter->pdev->dev, "Scheduling reset task\n");
1323				queue_work(iavf_wq, &adapter->reset_task);
1324			}
1325			break;
1326		default:
1327			dev_err(&adapter->pdev->dev, "Unknown event %d from PF\n",
1328				vpe->event);
1329			break;
1330		}
1331		return;
1332	}
1333	if (v_retval) {
1334		switch (v_opcode) {
1335		case VIRTCHNL_OP_ADD_VLAN:
1336			dev_err(&adapter->pdev->dev, "Failed to add VLAN filter, error %s\n",
1337				iavf_stat_str(&adapter->hw, v_retval));
1338			break;
1339		case VIRTCHNL_OP_ADD_ETH_ADDR:
1340			dev_err(&adapter->pdev->dev, "Failed to add MAC filter, error %s\n",
1341				iavf_stat_str(&adapter->hw, v_retval));
1342			iavf_mac_add_reject(adapter);
1343			/* restore administratively set MAC address */
1344			ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
1345			break;
1346		case VIRTCHNL_OP_DEL_VLAN:
1347			dev_err(&adapter->pdev->dev, "Failed to delete VLAN filter, error %s\n",
1348				iavf_stat_str(&adapter->hw, v_retval));
1349			break;
1350		case VIRTCHNL_OP_DEL_ETH_ADDR:
1351			dev_err(&adapter->pdev->dev, "Failed to delete MAC filter, error %s\n",
1352				iavf_stat_str(&adapter->hw, v_retval));
1353			break;
1354		case VIRTCHNL_OP_ENABLE_CHANNELS:
1355			dev_err(&adapter->pdev->dev, "Failed to configure queue channels, error %s\n",
1356				iavf_stat_str(&adapter->hw, v_retval));
1357			adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1358			adapter->ch_config.state = __IAVF_TC_INVALID;
1359			netdev_reset_tc(netdev);
1360			netif_tx_start_all_queues(netdev);
1361			break;
1362		case VIRTCHNL_OP_DISABLE_CHANNELS:
1363			dev_err(&adapter->pdev->dev, "Failed to disable queue channels, error %s\n",
1364				iavf_stat_str(&adapter->hw, v_retval));
1365			adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1366			adapter->ch_config.state = __IAVF_TC_RUNNING;
1367			netif_tx_start_all_queues(netdev);
1368			break;
1369		case VIRTCHNL_OP_ADD_CLOUD_FILTER: {
1370			struct iavf_cloud_filter *cf, *cftmp;
1371
1372			list_for_each_entry_safe(cf, cftmp,
1373						 &adapter->cloud_filter_list,
1374						 list) {
1375				if (cf->state == __IAVF_CF_ADD_PENDING) {
1376					cf->state = __IAVF_CF_INVALID;
1377					dev_info(&adapter->pdev->dev, "Failed to add cloud filter, error %s\n",
1378						 iavf_stat_str(&adapter->hw,
1379							       v_retval));
1380					iavf_print_cloud_filter(adapter,
1381								&cf->f);
1382					list_del(&cf->list);
1383					kfree(cf);
1384					adapter->num_cloud_filters--;
1385				}
1386			}
1387			}
1388			break;
1389		case VIRTCHNL_OP_DEL_CLOUD_FILTER: {
1390			struct iavf_cloud_filter *cf;
1391
1392			list_for_each_entry(cf, &adapter->cloud_filter_list,
1393					    list) {
1394				if (cf->state == __IAVF_CF_DEL_PENDING) {
1395					cf->state = __IAVF_CF_ACTIVE;
1396					dev_info(&adapter->pdev->dev, "Failed to del cloud filter, error %s\n",
1397						 iavf_stat_str(&adapter->hw,
1398							       v_retval));
1399					iavf_print_cloud_filter(adapter,
1400								&cf->f);
1401				}
1402			}
1403			}
1404			break;
1405		default:
1406			dev_err(&adapter->pdev->dev, "PF returned error %d (%s) to our request %d\n",
1407				v_retval, iavf_stat_str(&adapter->hw, v_retval),
1408				v_opcode);
1409		}
1410	}
1411	switch (v_opcode) {
1412	case VIRTCHNL_OP_ADD_ETH_ADDR:
1413		if (!v_retval)
1414			iavf_mac_add_ok(adapter);
1415		if (!ether_addr_equal(netdev->dev_addr, adapter->hw.mac.addr))
1416			ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
1417		break;
1418	case VIRTCHNL_OP_GET_STATS: {
1419		struct iavf_eth_stats *stats =
1420			(struct iavf_eth_stats *)msg;
1421		netdev->stats.rx_packets = stats->rx_unicast +
1422					   stats->rx_multicast +
1423					   stats->rx_broadcast;
1424		netdev->stats.tx_packets = stats->tx_unicast +
1425					   stats->tx_multicast +
1426					   stats->tx_broadcast;
1427		netdev->stats.rx_bytes = stats->rx_bytes;
1428		netdev->stats.tx_bytes = stats->tx_bytes;
1429		netdev->stats.tx_errors = stats->tx_errors;
1430		netdev->stats.rx_dropped = stats->rx_discards;
1431		netdev->stats.tx_dropped = stats->tx_discards;
1432		adapter->current_stats = *stats;
1433		}
1434		break;
1435	case VIRTCHNL_OP_GET_VF_RESOURCES: {
1436		u16 len = sizeof(struct virtchnl_vf_resource) +
1437			  IAVF_MAX_VF_VSI *
1438			  sizeof(struct virtchnl_vsi_resource);
1439		memcpy(adapter->vf_res, msg, min(msglen, len));
1440		iavf_validate_num_queues(adapter);
1441		iavf_vf_parse_hw_config(&adapter->hw, adapter->vf_res);
1442		if (is_zero_ether_addr(adapter->hw.mac.addr)) {
1443			/* restore current mac address */
1444			ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
1445		} else {
1446			/* refresh current mac address if changed */
1447			ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
1448			ether_addr_copy(netdev->perm_addr,
1449					adapter->hw.mac.addr);
1450		}
1451		spin_lock_bh(&adapter->mac_vlan_list_lock);
1452		iavf_add_filter(adapter, adapter->hw.mac.addr);
1453		spin_unlock_bh(&adapter->mac_vlan_list_lock);
1454		iavf_process_config(adapter);
1455		}
1456		break;
1457	case VIRTCHNL_OP_ENABLE_QUEUES:
1458		/* enable transmits */
1459		iavf_irq_enable(adapter, true);
1460		adapter->flags &= ~IAVF_FLAG_QUEUES_DISABLED;
1461		break;
1462	case VIRTCHNL_OP_DISABLE_QUEUES:
1463		iavf_free_all_tx_resources(adapter);
1464		iavf_free_all_rx_resources(adapter);
1465		if (adapter->state == __IAVF_DOWN_PENDING) {
1466			iavf_change_state(adapter, __IAVF_DOWN);
1467			wake_up(&adapter->down_waitqueue);
1468		}
1469		break;
1470	case VIRTCHNL_OP_VERSION:
1471	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
1472		/* Don't display an error if we get these out of sequence.
1473		 * If the firmware needed to get kicked, we'll get these and
1474		 * it's no problem.
1475		 */
1476		if (v_opcode != adapter->current_op)
1477			return;
1478		break;
1479	case VIRTCHNL_OP_IWARP:
1480		/* Gobble zero-length replies from the PF. They indicate that
1481		 * a previous message was received OK, and the client doesn't
1482		 * care about that.
1483		 */
1484		if (msglen && CLIENT_ENABLED(adapter))
1485			iavf_notify_client_message(&adapter->vsi, msg, msglen);
1486		break;
1487
1488	case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
1489		adapter->client_pending &=
1490				~(BIT(VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP));
1491		break;
1492	case VIRTCHNL_OP_GET_RSS_HENA_CAPS: {
1493		struct virtchnl_rss_hena *vrh = (struct virtchnl_rss_hena *)msg;
1494
1495		if (msglen == sizeof(*vrh))
1496			adapter->hena = vrh->hena;
1497		else
1498			dev_warn(&adapter->pdev->dev,
1499				 "Invalid message %d from PF\n", v_opcode);
1500		}
1501		break;
1502	case VIRTCHNL_OP_REQUEST_QUEUES: {
1503		struct virtchnl_vf_res_request *vfres =
1504			(struct virtchnl_vf_res_request *)msg;
1505
1506		if (vfres->num_queue_pairs != adapter->num_req_queues) {
1507			dev_info(&adapter->pdev->dev,
1508				 "Requested %d queues, PF can support %d\n",
1509				 adapter->num_req_queues,
1510				 vfres->num_queue_pairs);
1511			adapter->num_req_queues = 0;
1512			adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1513		}
1514		}
1515		break;
1516	case VIRTCHNL_OP_ADD_CLOUD_FILTER: {
1517		struct iavf_cloud_filter *cf;
1518
1519		list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1520			if (cf->state == __IAVF_CF_ADD_PENDING)
1521				cf->state = __IAVF_CF_ACTIVE;
1522		}
1523		}
1524		break;
1525	case VIRTCHNL_OP_DEL_CLOUD_FILTER: {
1526		struct iavf_cloud_filter *cf, *cftmp;
1527
1528		list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
1529					 list) {
1530			if (cf->state == __IAVF_CF_DEL_PENDING) {
1531				cf->state = __IAVF_CF_INVALID;
1532				list_del(&cf->list);
1533				kfree(cf);
1534				adapter->num_cloud_filters--;
1535			}
1536		}
1537		}
1538		break;
1539	default:
1540		if (adapter->current_op && (v_opcode != adapter->current_op))
1541			dev_warn(&adapter->pdev->dev, "Expected response %d from PF, received %d\n",
1542				 adapter->current_op, v_opcode);
1543		break;
1544	} /* switch v_opcode */
1545	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1546}
1547