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 /**
9  * iavf_send_pf_msg
10  * @adapter: adapter structure
11  * @op: virtual channel opcode
12  * @msg: pointer to message buffer
13  * @len: message length
14  *
15  * Send message to PF and print status if failure.
16  **/
17 static int iavf_send_pf_msg(struct iavf_adapter *adapter,
18 			    enum virtchnl_ops op, u8 *msg, u16 len)
19 {
20 	struct iavf_hw *hw = &adapter->hw;
21 	enum iavf_status status;
22 
23 	if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
24 		return 0; /* nothing to see here, move along */
25 
26 	status = iavf_aq_send_msg_to_pf(hw, op, 0, msg, len, NULL);
27 	if (status)
28 		dev_dbg(&adapter->pdev->dev, "Unable to send opcode %d to PF, status %s, aq_err %s\n",
29 			op, iavf_stat_str(hw, status),
30 			iavf_aq_str(hw, hw->aq.asq_last_status));
31 	return iavf_status_to_errno(status);
32 }
33 
34 /**
35  * iavf_send_api_ver
36  * @adapter: adapter structure
37  *
38  * Send API version admin queue message to the PF. The reply is not checked
39  * in this function. Returns 0 if the message was successfully
40  * sent, or one of the IAVF_ADMIN_QUEUE_ERROR_ statuses if not.
41  **/
42 int iavf_send_api_ver(struct iavf_adapter *adapter)
43 {
44 	struct virtchnl_version_info vvi;
45 
46 	vvi.major = VIRTCHNL_VERSION_MAJOR;
47 	vvi.minor = VIRTCHNL_VERSION_MINOR;
48 
49 	return iavf_send_pf_msg(adapter, VIRTCHNL_OP_VERSION, (u8 *)&vvi,
50 				sizeof(vvi));
51 }
52 
53 /**
54  * iavf_poll_virtchnl_msg
55  * @hw: HW configuration structure
56  * @event: event to populate on success
57  * @op_to_poll: requested virtchnl op to poll for
58  *
59  * Initialize poll for virtchnl msg matching the requested_op. Returns 0
60  * if a message of the correct opcode is in the queue or an error code
61  * if no message matching the op code is waiting and other failures.
62  */
63 static int
64 iavf_poll_virtchnl_msg(struct iavf_hw *hw, struct iavf_arq_event_info *event,
65 		       enum virtchnl_ops op_to_poll)
66 {
67 	enum virtchnl_ops received_op;
68 	enum iavf_status status;
69 	u32 v_retval;
70 
71 	while (1) {
72 		/* When the AQ is empty, iavf_clean_arq_element will return
73 		 * nonzero and this loop will terminate.
74 		 */
75 		status = iavf_clean_arq_element(hw, event, NULL);
76 		if (status != IAVF_SUCCESS)
77 			return iavf_status_to_errno(status);
78 		received_op =
79 		    (enum virtchnl_ops)le32_to_cpu(event->desc.cookie_high);
80 		if (op_to_poll == received_op)
81 			break;
82 	}
83 
84 	v_retval = le32_to_cpu(event->desc.cookie_low);
85 	return virtchnl_status_to_errno((enum virtchnl_status_code)v_retval);
86 }
87 
88 /**
89  * iavf_verify_api_ver
90  * @adapter: adapter structure
91  *
92  * Compare API versions with the PF. Must be called after admin queue is
93  * initialized. Returns 0 if API versions match, -EIO if they do not,
94  * IAVF_ERR_ADMIN_QUEUE_NO_WORK if the admin queue is empty, and any errors
95  * from the firmware are propagated.
96  **/
97 int iavf_verify_api_ver(struct iavf_adapter *adapter)
98 {
99 	struct iavf_arq_event_info event;
100 	int err;
101 
102 	event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
103 	event.msg_buf = kzalloc(IAVF_MAX_AQ_BUF_SIZE, GFP_KERNEL);
104 	if (!event.msg_buf)
105 		return -ENOMEM;
106 
107 	err = iavf_poll_virtchnl_msg(&adapter->hw, &event, VIRTCHNL_OP_VERSION);
108 	if (!err) {
109 		struct virtchnl_version_info *pf_vvi =
110 			(struct virtchnl_version_info *)event.msg_buf;
111 		adapter->pf_version = *pf_vvi;
112 
113 		if (pf_vvi->major > VIRTCHNL_VERSION_MAJOR ||
114 		    (pf_vvi->major == VIRTCHNL_VERSION_MAJOR &&
115 		     pf_vvi->minor > VIRTCHNL_VERSION_MINOR))
116 			err = -EIO;
117 	}
118 
119 	kfree(event.msg_buf);
120 
121 	return err;
122 }
123 
124 /**
125  * iavf_send_vf_config_msg
126  * @adapter: adapter structure
127  *
128  * Send VF configuration request admin queue message to the PF. The reply
129  * is not checked in this function. Returns 0 if the message was
130  * successfully sent, or one of the IAVF_ADMIN_QUEUE_ERROR_ statuses if not.
131  **/
132 int iavf_send_vf_config_msg(struct iavf_adapter *adapter)
133 {
134 	u32 caps;
135 
136 	caps = VIRTCHNL_VF_OFFLOAD_L2 |
137 	       VIRTCHNL_VF_OFFLOAD_RSS_PF |
138 	       VIRTCHNL_VF_OFFLOAD_RSS_AQ |
139 	       VIRTCHNL_VF_OFFLOAD_RSS_REG |
140 	       VIRTCHNL_VF_OFFLOAD_VLAN |
141 	       VIRTCHNL_VF_OFFLOAD_WB_ON_ITR |
142 	       VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2 |
143 	       VIRTCHNL_VF_OFFLOAD_ENCAP |
144 	       VIRTCHNL_VF_OFFLOAD_VLAN_V2 |
145 	       VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM |
146 	       VIRTCHNL_VF_OFFLOAD_REQ_QUEUES |
147 	       VIRTCHNL_VF_OFFLOAD_ADQ |
148 	       VIRTCHNL_VF_OFFLOAD_USO |
149 	       VIRTCHNL_VF_OFFLOAD_FDIR_PF |
150 	       VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF |
151 	       VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
152 
153 	adapter->current_op = VIRTCHNL_OP_GET_VF_RESOURCES;
154 	adapter->aq_required &= ~IAVF_FLAG_AQ_GET_CONFIG;
155 	if (PF_IS_V11(adapter))
156 		return iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_VF_RESOURCES,
157 					(u8 *)&caps, sizeof(caps));
158 	else
159 		return iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_VF_RESOURCES,
160 					NULL, 0);
161 }
162 
163 int iavf_send_vf_offload_vlan_v2_msg(struct iavf_adapter *adapter)
164 {
165 	adapter->aq_required &= ~IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS;
166 
167 	if (!VLAN_V2_ALLOWED(adapter))
168 		return -EOPNOTSUPP;
169 
170 	adapter->current_op = VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS;
171 
172 	return iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS,
173 				NULL, 0);
174 }
175 
176 /**
177  * iavf_validate_num_queues
178  * @adapter: adapter structure
179  *
180  * Validate that the number of queues the PF has sent in
181  * VIRTCHNL_OP_GET_VF_RESOURCES is not larger than the VF can handle.
182  **/
183 static void iavf_validate_num_queues(struct iavf_adapter *adapter)
184 {
185 	if (adapter->vf_res->num_queue_pairs > IAVF_MAX_REQ_QUEUES) {
186 		struct virtchnl_vsi_resource *vsi_res;
187 		int i;
188 
189 		dev_info(&adapter->pdev->dev, "Received %d queues, but can only have a max of %d\n",
190 			 adapter->vf_res->num_queue_pairs,
191 			 IAVF_MAX_REQ_QUEUES);
192 		dev_info(&adapter->pdev->dev, "Fixing by reducing queues to %d\n",
193 			 IAVF_MAX_REQ_QUEUES);
194 		adapter->vf_res->num_queue_pairs = IAVF_MAX_REQ_QUEUES;
195 		for (i = 0; i < adapter->vf_res->num_vsis; i++) {
196 			vsi_res = &adapter->vf_res->vsi_res[i];
197 			vsi_res->num_queue_pairs = IAVF_MAX_REQ_QUEUES;
198 		}
199 	}
200 }
201 
202 /**
203  * iavf_get_vf_config
204  * @adapter: private adapter structure
205  *
206  * Get VF configuration from PF and populate hw structure. Must be called after
207  * admin queue is initialized. Busy waits until response is received from PF,
208  * with maximum timeout. Response from PF is returned in the buffer for further
209  * processing by the caller.
210  **/
211 int iavf_get_vf_config(struct iavf_adapter *adapter)
212 {
213 	struct iavf_hw *hw = &adapter->hw;
214 	struct iavf_arq_event_info event;
215 	u16 len;
216 	int err;
217 
218 	len = IAVF_VIRTCHNL_VF_RESOURCE_SIZE;
219 	event.buf_len = len;
220 	event.msg_buf = kzalloc(len, GFP_KERNEL);
221 	if (!event.msg_buf)
222 		return -ENOMEM;
223 
224 	err = iavf_poll_virtchnl_msg(hw, &event, VIRTCHNL_OP_GET_VF_RESOURCES);
225 	memcpy(adapter->vf_res, event.msg_buf, min(event.msg_len, len));
226 
227 	/* some PFs send more queues than we should have so validate that
228 	 * we aren't getting too many queues
229 	 */
230 	if (!err)
231 		iavf_validate_num_queues(adapter);
232 	iavf_vf_parse_hw_config(hw, adapter->vf_res);
233 
234 	kfree(event.msg_buf);
235 
236 	return err;
237 }
238 
239 int iavf_get_vf_vlan_v2_caps(struct iavf_adapter *adapter)
240 {
241 	struct iavf_arq_event_info event;
242 	int err;
243 	u16 len;
244 
245 	len = sizeof(struct virtchnl_vlan_caps);
246 	event.buf_len = len;
247 	event.msg_buf = kzalloc(len, GFP_KERNEL);
248 	if (!event.msg_buf)
249 		return -ENOMEM;
250 
251 	err = iavf_poll_virtchnl_msg(&adapter->hw, &event,
252 				     VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS);
253 	if (!err)
254 		memcpy(&adapter->vlan_v2_caps, event.msg_buf,
255 		       min(event.msg_len, len));
256 
257 	kfree(event.msg_buf);
258 
259 	return err;
260 }
261 
262 /**
263  * iavf_configure_queues
264  * @adapter: adapter structure
265  *
266  * Request that the PF set up our (previously allocated) queues.
267  **/
268 void iavf_configure_queues(struct iavf_adapter *adapter)
269 {
270 	struct virtchnl_vsi_queue_config_info *vqci;
271 	int i, max_frame = adapter->vf_res->max_mtu;
272 	int pairs = adapter->num_active_queues;
273 	struct virtchnl_queue_pair_info *vqpi;
274 	size_t len;
275 
276 	if (max_frame > IAVF_MAX_RXBUFFER || !max_frame)
277 		max_frame = IAVF_MAX_RXBUFFER;
278 
279 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
280 		/* bail because we already have a command pending */
281 		dev_err(&adapter->pdev->dev, "Cannot configure queues, command %d pending\n",
282 			adapter->current_op);
283 		return;
284 	}
285 	adapter->current_op = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
286 	len = virtchnl_struct_size(vqci, qpair, pairs);
287 	vqci = kzalloc(len, GFP_KERNEL);
288 	if (!vqci)
289 		return;
290 
291 	/* Limit maximum frame size when jumbo frames is not enabled */
292 	if (!(adapter->flags & IAVF_FLAG_LEGACY_RX) &&
293 	    (adapter->netdev->mtu <= ETH_DATA_LEN))
294 		max_frame = IAVF_RXBUFFER_1536 - NET_IP_ALIGN;
295 
296 	vqci->vsi_id = adapter->vsi_res->vsi_id;
297 	vqci->num_queue_pairs = pairs;
298 	vqpi = vqci->qpair;
299 	/* Size check is not needed here - HW max is 16 queue pairs, and we
300 	 * can fit info for 31 of them into the AQ buffer before it overflows.
301 	 */
302 	for (i = 0; i < pairs; i++) {
303 		vqpi->txq.vsi_id = vqci->vsi_id;
304 		vqpi->txq.queue_id = i;
305 		vqpi->txq.ring_len = adapter->tx_rings[i].count;
306 		vqpi->txq.dma_ring_addr = adapter->tx_rings[i].dma;
307 		vqpi->rxq.vsi_id = vqci->vsi_id;
308 		vqpi->rxq.queue_id = i;
309 		vqpi->rxq.ring_len = adapter->rx_rings[i].count;
310 		vqpi->rxq.dma_ring_addr = adapter->rx_rings[i].dma;
311 		vqpi->rxq.max_pkt_size = max_frame;
312 		vqpi->rxq.databuffer_size =
313 			ALIGN(adapter->rx_rings[i].rx_buf_len,
314 			      BIT_ULL(IAVF_RXQ_CTX_DBUFF_SHIFT));
315 		vqpi++;
316 	}
317 
318 	adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_QUEUES;
319 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
320 			 (u8 *)vqci, len);
321 	kfree(vqci);
322 }
323 
324 /**
325  * iavf_enable_queues
326  * @adapter: adapter structure
327  *
328  * Request that the PF enable all of our queues.
329  **/
330 void iavf_enable_queues(struct iavf_adapter *adapter)
331 {
332 	struct virtchnl_queue_select vqs;
333 
334 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
335 		/* bail because we already have a command pending */
336 		dev_err(&adapter->pdev->dev, "Cannot enable queues, command %d pending\n",
337 			adapter->current_op);
338 		return;
339 	}
340 	adapter->current_op = VIRTCHNL_OP_ENABLE_QUEUES;
341 	vqs.vsi_id = adapter->vsi_res->vsi_id;
342 	vqs.tx_queues = BIT(adapter->num_active_queues) - 1;
343 	vqs.rx_queues = vqs.tx_queues;
344 	adapter->aq_required &= ~IAVF_FLAG_AQ_ENABLE_QUEUES;
345 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_ENABLE_QUEUES,
346 			 (u8 *)&vqs, sizeof(vqs));
347 }
348 
349 /**
350  * iavf_disable_queues
351  * @adapter: adapter structure
352  *
353  * Request that the PF disable all of our queues.
354  **/
355 void iavf_disable_queues(struct iavf_adapter *adapter)
356 {
357 	struct virtchnl_queue_select vqs;
358 
359 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
360 		/* bail because we already have a command pending */
361 		dev_err(&adapter->pdev->dev, "Cannot disable queues, command %d pending\n",
362 			adapter->current_op);
363 		return;
364 	}
365 	adapter->current_op = VIRTCHNL_OP_DISABLE_QUEUES;
366 	vqs.vsi_id = adapter->vsi_res->vsi_id;
367 	vqs.tx_queues = BIT(adapter->num_active_queues) - 1;
368 	vqs.rx_queues = vqs.tx_queues;
369 	adapter->aq_required &= ~IAVF_FLAG_AQ_DISABLE_QUEUES;
370 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_DISABLE_QUEUES,
371 			 (u8 *)&vqs, sizeof(vqs));
372 }
373 
374 /**
375  * iavf_map_queues
376  * @adapter: adapter structure
377  *
378  * Request that the PF map queues to interrupt vectors. Misc causes, including
379  * admin queue, are always mapped to vector 0.
380  **/
381 void iavf_map_queues(struct iavf_adapter *adapter)
382 {
383 	struct virtchnl_irq_map_info *vimi;
384 	struct virtchnl_vector_map *vecmap;
385 	struct iavf_q_vector *q_vector;
386 	int v_idx, q_vectors;
387 	size_t len;
388 
389 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
390 		/* bail because we already have a command pending */
391 		dev_err(&adapter->pdev->dev, "Cannot map queues to vectors, command %d pending\n",
392 			adapter->current_op);
393 		return;
394 	}
395 	adapter->current_op = VIRTCHNL_OP_CONFIG_IRQ_MAP;
396 
397 	q_vectors = adapter->num_msix_vectors - NONQ_VECS;
398 
399 	len = virtchnl_struct_size(vimi, vecmap, adapter->num_msix_vectors);
400 	vimi = kzalloc(len, GFP_KERNEL);
401 	if (!vimi)
402 		return;
403 
404 	vimi->num_vectors = adapter->num_msix_vectors;
405 	/* Queue vectors first */
406 	for (v_idx = 0; v_idx < q_vectors; v_idx++) {
407 		q_vector = &adapter->q_vectors[v_idx];
408 		vecmap = &vimi->vecmap[v_idx];
409 
410 		vecmap->vsi_id = adapter->vsi_res->vsi_id;
411 		vecmap->vector_id = v_idx + NONQ_VECS;
412 		vecmap->txq_map = q_vector->ring_mask;
413 		vecmap->rxq_map = q_vector->ring_mask;
414 		vecmap->rxitr_idx = IAVF_RX_ITR;
415 		vecmap->txitr_idx = IAVF_TX_ITR;
416 	}
417 	/* Misc vector last - this is only for AdminQ messages */
418 	vecmap = &vimi->vecmap[v_idx];
419 	vecmap->vsi_id = adapter->vsi_res->vsi_id;
420 	vecmap->vector_id = 0;
421 	vecmap->txq_map = 0;
422 	vecmap->rxq_map = 0;
423 
424 	adapter->aq_required &= ~IAVF_FLAG_AQ_MAP_VECTORS;
425 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_IRQ_MAP,
426 			 (u8 *)vimi, len);
427 	kfree(vimi);
428 }
429 
430 /**
431  * iavf_set_mac_addr_type - Set the correct request type from the filter type
432  * @virtchnl_ether_addr: pointer to requested list element
433  * @filter: pointer to requested filter
434  **/
435 static void
436 iavf_set_mac_addr_type(struct virtchnl_ether_addr *virtchnl_ether_addr,
437 		       const struct iavf_mac_filter *filter)
438 {
439 	virtchnl_ether_addr->type = filter->is_primary ?
440 		VIRTCHNL_ETHER_ADDR_PRIMARY :
441 		VIRTCHNL_ETHER_ADDR_EXTRA;
442 }
443 
444 /**
445  * iavf_add_ether_addrs
446  * @adapter: adapter structure
447  *
448  * Request that the PF add one or more addresses to our filters.
449  **/
450 void iavf_add_ether_addrs(struct iavf_adapter *adapter)
451 {
452 	struct virtchnl_ether_addr_list *veal;
453 	struct iavf_mac_filter *f;
454 	int i = 0, count = 0;
455 	bool more = false;
456 	size_t len;
457 
458 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
459 		/* bail because we already have a command pending */
460 		dev_err(&adapter->pdev->dev, "Cannot add filters, command %d pending\n",
461 			adapter->current_op);
462 		return;
463 	}
464 
465 	spin_lock_bh(&adapter->mac_vlan_list_lock);
466 
467 	list_for_each_entry(f, &adapter->mac_filter_list, list) {
468 		if (f->add)
469 			count++;
470 	}
471 	if (!count) {
472 		adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_MAC_FILTER;
473 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
474 		return;
475 	}
476 	adapter->current_op = VIRTCHNL_OP_ADD_ETH_ADDR;
477 
478 	len = virtchnl_struct_size(veal, list, count);
479 	if (len > IAVF_MAX_AQ_BUF_SIZE) {
480 		dev_warn(&adapter->pdev->dev, "Too many add MAC changes in one request\n");
481 		while (len > IAVF_MAX_AQ_BUF_SIZE)
482 			len = virtchnl_struct_size(veal, list, --count);
483 		more = true;
484 	}
485 
486 	veal = kzalloc(len, GFP_ATOMIC);
487 	if (!veal) {
488 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
489 		return;
490 	}
491 
492 	veal->vsi_id = adapter->vsi_res->vsi_id;
493 	veal->num_elements = count;
494 	list_for_each_entry(f, &adapter->mac_filter_list, list) {
495 		if (f->add) {
496 			ether_addr_copy(veal->list[i].addr, f->macaddr);
497 			iavf_set_mac_addr_type(&veal->list[i], f);
498 			i++;
499 			f->add = false;
500 			if (i == count)
501 				break;
502 		}
503 	}
504 	if (!more)
505 		adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_MAC_FILTER;
506 
507 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
508 
509 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_ETH_ADDR, (u8 *)veal, len);
510 	kfree(veal);
511 }
512 
513 /**
514  * iavf_del_ether_addrs
515  * @adapter: adapter structure
516  *
517  * Request that the PF remove one or more addresses from our filters.
518  **/
519 void iavf_del_ether_addrs(struct iavf_adapter *adapter)
520 {
521 	struct virtchnl_ether_addr_list *veal;
522 	struct iavf_mac_filter *f, *ftmp;
523 	int i = 0, count = 0;
524 	bool more = false;
525 	size_t len;
526 
527 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
528 		/* bail because we already have a command pending */
529 		dev_err(&adapter->pdev->dev, "Cannot remove filters, command %d pending\n",
530 			adapter->current_op);
531 		return;
532 	}
533 
534 	spin_lock_bh(&adapter->mac_vlan_list_lock);
535 
536 	list_for_each_entry(f, &adapter->mac_filter_list, list) {
537 		if (f->remove)
538 			count++;
539 	}
540 	if (!count) {
541 		adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_MAC_FILTER;
542 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
543 		return;
544 	}
545 	adapter->current_op = VIRTCHNL_OP_DEL_ETH_ADDR;
546 
547 	len = virtchnl_struct_size(veal, list, count);
548 	if (len > IAVF_MAX_AQ_BUF_SIZE) {
549 		dev_warn(&adapter->pdev->dev, "Too many delete MAC changes in one request\n");
550 		while (len > IAVF_MAX_AQ_BUF_SIZE)
551 			len = virtchnl_struct_size(veal, list, --count);
552 		more = true;
553 	}
554 	veal = kzalloc(len, GFP_ATOMIC);
555 	if (!veal) {
556 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
557 		return;
558 	}
559 
560 	veal->vsi_id = adapter->vsi_res->vsi_id;
561 	veal->num_elements = count;
562 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
563 		if (f->remove) {
564 			ether_addr_copy(veal->list[i].addr, f->macaddr);
565 			iavf_set_mac_addr_type(&veal->list[i], f);
566 			i++;
567 			list_del(&f->list);
568 			kfree(f);
569 			if (i == count)
570 				break;
571 		}
572 	}
573 	if (!more)
574 		adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_MAC_FILTER;
575 
576 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
577 
578 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_ETH_ADDR, (u8 *)veal, len);
579 	kfree(veal);
580 }
581 
582 /**
583  * iavf_mac_add_ok
584  * @adapter: adapter structure
585  *
586  * Submit list of filters based on PF response.
587  **/
588 static void iavf_mac_add_ok(struct iavf_adapter *adapter)
589 {
590 	struct iavf_mac_filter *f, *ftmp;
591 
592 	spin_lock_bh(&adapter->mac_vlan_list_lock);
593 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
594 		f->is_new_mac = false;
595 		if (!f->add && !f->add_handled)
596 			f->add_handled = true;
597 	}
598 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
599 }
600 
601 /**
602  * iavf_mac_add_reject
603  * @adapter: adapter structure
604  *
605  * Remove filters from list based on PF response.
606  **/
607 static void iavf_mac_add_reject(struct iavf_adapter *adapter)
608 {
609 	struct net_device *netdev = adapter->netdev;
610 	struct iavf_mac_filter *f, *ftmp;
611 
612 	spin_lock_bh(&adapter->mac_vlan_list_lock);
613 	list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
614 		if (f->remove && ether_addr_equal(f->macaddr, netdev->dev_addr))
615 			f->remove = false;
616 
617 		if (!f->add && !f->add_handled)
618 			f->add_handled = true;
619 
620 		if (f->is_new_mac) {
621 			list_del(&f->list);
622 			kfree(f);
623 		}
624 	}
625 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
626 }
627 
628 /**
629  * iavf_vlan_add_reject
630  * @adapter: adapter structure
631  *
632  * Remove VLAN filters from list based on PF response.
633  **/
634 static void iavf_vlan_add_reject(struct iavf_adapter *adapter)
635 {
636 	struct iavf_vlan_filter *f, *ftmp;
637 
638 	spin_lock_bh(&adapter->mac_vlan_list_lock);
639 	list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
640 		if (f->state == IAVF_VLAN_IS_NEW) {
641 			list_del(&f->list);
642 			kfree(f);
643 			adapter->num_vlan_filters--;
644 		}
645 	}
646 	spin_unlock_bh(&adapter->mac_vlan_list_lock);
647 }
648 
649 /**
650  * iavf_add_vlans
651  * @adapter: adapter structure
652  *
653  * Request that the PF add one or more VLAN filters to our VSI.
654  **/
655 void iavf_add_vlans(struct iavf_adapter *adapter)
656 {
657 	int len, i = 0, count = 0;
658 	struct iavf_vlan_filter *f;
659 	bool more = false;
660 
661 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
662 		/* bail because we already have a command pending */
663 		dev_err(&adapter->pdev->dev, "Cannot add VLANs, command %d pending\n",
664 			adapter->current_op);
665 		return;
666 	}
667 
668 	spin_lock_bh(&adapter->mac_vlan_list_lock);
669 
670 	list_for_each_entry(f, &adapter->vlan_filter_list, list) {
671 		if (f->state == IAVF_VLAN_ADD)
672 			count++;
673 	}
674 	if (!count || !VLAN_FILTERING_ALLOWED(adapter)) {
675 		adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_VLAN_FILTER;
676 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
677 		return;
678 	}
679 
680 	if (VLAN_ALLOWED(adapter)) {
681 		struct virtchnl_vlan_filter_list *vvfl;
682 
683 		adapter->current_op = VIRTCHNL_OP_ADD_VLAN;
684 
685 		len = virtchnl_struct_size(vvfl, vlan_id, count);
686 		if (len > IAVF_MAX_AQ_BUF_SIZE) {
687 			dev_warn(&adapter->pdev->dev, "Too many add VLAN changes in one request\n");
688 			while (len > IAVF_MAX_AQ_BUF_SIZE)
689 				len = virtchnl_struct_size(vvfl, vlan_id,
690 							   --count);
691 			more = true;
692 		}
693 		vvfl = kzalloc(len, GFP_ATOMIC);
694 		if (!vvfl) {
695 			spin_unlock_bh(&adapter->mac_vlan_list_lock);
696 			return;
697 		}
698 
699 		vvfl->vsi_id = adapter->vsi_res->vsi_id;
700 		vvfl->num_elements = count;
701 		list_for_each_entry(f, &adapter->vlan_filter_list, list) {
702 			if (f->state == IAVF_VLAN_ADD) {
703 				vvfl->vlan_id[i] = f->vlan.vid;
704 				i++;
705 				f->state = IAVF_VLAN_IS_NEW;
706 				if (i == count)
707 					break;
708 			}
709 		}
710 		if (!more)
711 			adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_VLAN_FILTER;
712 
713 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
714 
715 		iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_VLAN, (u8 *)vvfl, len);
716 		kfree(vvfl);
717 	} else {
718 		u16 max_vlans = adapter->vlan_v2_caps.filtering.max_filters;
719 		u16 current_vlans = iavf_get_num_vlans_added(adapter);
720 		struct virtchnl_vlan_filter_list_v2 *vvfl_v2;
721 
722 		adapter->current_op = VIRTCHNL_OP_ADD_VLAN_V2;
723 
724 		if ((count + current_vlans) > max_vlans &&
725 		    current_vlans < max_vlans) {
726 			count = max_vlans - iavf_get_num_vlans_added(adapter);
727 			more = true;
728 		}
729 
730 		len = virtchnl_struct_size(vvfl_v2, filters, count);
731 		if (len > IAVF_MAX_AQ_BUF_SIZE) {
732 			dev_warn(&adapter->pdev->dev, "Too many add VLAN changes in one request\n");
733 			while (len > IAVF_MAX_AQ_BUF_SIZE)
734 				len = virtchnl_struct_size(vvfl_v2, filters,
735 							   --count);
736 			more = true;
737 		}
738 
739 		vvfl_v2 = kzalloc(len, GFP_ATOMIC);
740 		if (!vvfl_v2) {
741 			spin_unlock_bh(&adapter->mac_vlan_list_lock);
742 			return;
743 		}
744 
745 		vvfl_v2->vport_id = adapter->vsi_res->vsi_id;
746 		vvfl_v2->num_elements = count;
747 		list_for_each_entry(f, &adapter->vlan_filter_list, list) {
748 			if (f->state == IAVF_VLAN_ADD) {
749 				struct virtchnl_vlan_supported_caps *filtering_support =
750 					&adapter->vlan_v2_caps.filtering.filtering_support;
751 				struct virtchnl_vlan *vlan;
752 
753 				if (i == count)
754 					break;
755 
756 				/* give priority over outer if it's enabled */
757 				if (filtering_support->outer)
758 					vlan = &vvfl_v2->filters[i].outer;
759 				else
760 					vlan = &vvfl_v2->filters[i].inner;
761 
762 				vlan->tci = f->vlan.vid;
763 				vlan->tpid = f->vlan.tpid;
764 
765 				i++;
766 				f->state = IAVF_VLAN_IS_NEW;
767 			}
768 		}
769 
770 		if (!more)
771 			adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_VLAN_FILTER;
772 
773 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
774 
775 		iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_VLAN_V2,
776 				 (u8 *)vvfl_v2, len);
777 		kfree(vvfl_v2);
778 	}
779 }
780 
781 /**
782  * iavf_del_vlans
783  * @adapter: adapter structure
784  *
785  * Request that the PF remove one or more VLAN filters from our VSI.
786  **/
787 void iavf_del_vlans(struct iavf_adapter *adapter)
788 {
789 	struct iavf_vlan_filter *f, *ftmp;
790 	int len, i = 0, count = 0;
791 	bool more = false;
792 
793 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
794 		/* bail because we already have a command pending */
795 		dev_err(&adapter->pdev->dev, "Cannot remove VLANs, command %d pending\n",
796 			adapter->current_op);
797 		return;
798 	}
799 
800 	spin_lock_bh(&adapter->mac_vlan_list_lock);
801 
802 	list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
803 		/* since VLAN capabilities are not allowed, we dont want to send
804 		 * a VLAN delete request because it will most likely fail and
805 		 * create unnecessary errors/noise, so just free the VLAN
806 		 * filters marked for removal to enable bailing out before
807 		 * sending a virtchnl message
808 		 */
809 		if (f->state == IAVF_VLAN_REMOVE &&
810 		    !VLAN_FILTERING_ALLOWED(adapter)) {
811 			list_del(&f->list);
812 			kfree(f);
813 			adapter->num_vlan_filters--;
814 		} else if (f->state == IAVF_VLAN_DISABLE &&
815 		    !VLAN_FILTERING_ALLOWED(adapter)) {
816 			f->state = IAVF_VLAN_INACTIVE;
817 		} else if (f->state == IAVF_VLAN_REMOVE ||
818 			   f->state == IAVF_VLAN_DISABLE) {
819 			count++;
820 		}
821 	}
822 	if (!count || !VLAN_FILTERING_ALLOWED(adapter)) {
823 		adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_VLAN_FILTER;
824 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
825 		return;
826 	}
827 
828 	if (VLAN_ALLOWED(adapter)) {
829 		struct virtchnl_vlan_filter_list *vvfl;
830 
831 		adapter->current_op = VIRTCHNL_OP_DEL_VLAN;
832 
833 		len = virtchnl_struct_size(vvfl, vlan_id, count);
834 		if (len > IAVF_MAX_AQ_BUF_SIZE) {
835 			dev_warn(&adapter->pdev->dev, "Too many delete VLAN changes in one request\n");
836 			while (len > IAVF_MAX_AQ_BUF_SIZE)
837 				len = virtchnl_struct_size(vvfl, vlan_id,
838 							   --count);
839 			more = true;
840 		}
841 		vvfl = kzalloc(len, GFP_ATOMIC);
842 		if (!vvfl) {
843 			spin_unlock_bh(&adapter->mac_vlan_list_lock);
844 			return;
845 		}
846 
847 		vvfl->vsi_id = adapter->vsi_res->vsi_id;
848 		vvfl->num_elements = count;
849 		list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
850 			if (f->state == IAVF_VLAN_DISABLE) {
851 				vvfl->vlan_id[i] = f->vlan.vid;
852 				f->state = IAVF_VLAN_INACTIVE;
853 				i++;
854 				if (i == count)
855 					break;
856 			} else if (f->state == IAVF_VLAN_REMOVE) {
857 				vvfl->vlan_id[i] = f->vlan.vid;
858 				list_del(&f->list);
859 				kfree(f);
860 				adapter->num_vlan_filters--;
861 				i++;
862 				if (i == count)
863 					break;
864 			}
865 		}
866 
867 		if (!more)
868 			adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_VLAN_FILTER;
869 
870 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
871 
872 		iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_VLAN, (u8 *)vvfl, len);
873 		kfree(vvfl);
874 	} else {
875 		struct virtchnl_vlan_filter_list_v2 *vvfl_v2;
876 
877 		adapter->current_op = VIRTCHNL_OP_DEL_VLAN_V2;
878 
879 		len = virtchnl_struct_size(vvfl_v2, filters, count);
880 		if (len > IAVF_MAX_AQ_BUF_SIZE) {
881 			dev_warn(&adapter->pdev->dev, "Too many add VLAN changes in one request\n");
882 			while (len > IAVF_MAX_AQ_BUF_SIZE)
883 				len = virtchnl_struct_size(vvfl_v2, filters,
884 							   --count);
885 			more = true;
886 		}
887 
888 		vvfl_v2 = kzalloc(len, GFP_ATOMIC);
889 		if (!vvfl_v2) {
890 			spin_unlock_bh(&adapter->mac_vlan_list_lock);
891 			return;
892 		}
893 
894 		vvfl_v2->vport_id = adapter->vsi_res->vsi_id;
895 		vvfl_v2->num_elements = count;
896 		list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
897 			if (f->state == IAVF_VLAN_DISABLE ||
898 			    f->state == IAVF_VLAN_REMOVE) {
899 				struct virtchnl_vlan_supported_caps *filtering_support =
900 					&adapter->vlan_v2_caps.filtering.filtering_support;
901 				struct virtchnl_vlan *vlan;
902 
903 				/* give priority over outer if it's enabled */
904 				if (filtering_support->outer)
905 					vlan = &vvfl_v2->filters[i].outer;
906 				else
907 					vlan = &vvfl_v2->filters[i].inner;
908 
909 				vlan->tci = f->vlan.vid;
910 				vlan->tpid = f->vlan.tpid;
911 
912 				if (f->state == IAVF_VLAN_DISABLE) {
913 					f->state = IAVF_VLAN_INACTIVE;
914 				} else {
915 					list_del(&f->list);
916 					kfree(f);
917 					adapter->num_vlan_filters--;
918 				}
919 				i++;
920 				if (i == count)
921 					break;
922 			}
923 		}
924 
925 		if (!more)
926 			adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_VLAN_FILTER;
927 
928 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
929 
930 		iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_VLAN_V2,
931 				 (u8 *)vvfl_v2, len);
932 		kfree(vvfl_v2);
933 	}
934 }
935 
936 /**
937  * iavf_set_promiscuous
938  * @adapter: adapter structure
939  *
940  * Request that the PF enable promiscuous mode for our VSI.
941  **/
942 void iavf_set_promiscuous(struct iavf_adapter *adapter)
943 {
944 	struct net_device *netdev = adapter->netdev;
945 	struct virtchnl_promisc_info vpi;
946 	unsigned int flags;
947 
948 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
949 		/* bail because we already have a command pending */
950 		dev_err(&adapter->pdev->dev, "Cannot set promiscuous mode, command %d pending\n",
951 			adapter->current_op);
952 		return;
953 	}
954 
955 	/* prevent changes to promiscuous flags */
956 	spin_lock_bh(&adapter->current_netdev_promisc_flags_lock);
957 
958 	/* sanity check to prevent duplicate AQ calls */
959 	if (!iavf_promiscuous_mode_changed(adapter)) {
960 		adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_PROMISC_MODE;
961 		dev_dbg(&adapter->pdev->dev, "No change in promiscuous mode\n");
962 		/* allow changes to promiscuous flags */
963 		spin_unlock_bh(&adapter->current_netdev_promisc_flags_lock);
964 		return;
965 	}
966 
967 	/* there are 2 bits, but only 3 states */
968 	if (!(netdev->flags & IFF_PROMISC) &&
969 	    netdev->flags & IFF_ALLMULTI) {
970 		/* State 1  - only multicast promiscuous mode enabled
971 		 * - !IFF_PROMISC && IFF_ALLMULTI
972 		 */
973 		flags = FLAG_VF_MULTICAST_PROMISC;
974 		adapter->current_netdev_promisc_flags |= IFF_ALLMULTI;
975 		adapter->current_netdev_promisc_flags &= ~IFF_PROMISC;
976 		dev_info(&adapter->pdev->dev, "Entering multicast promiscuous mode\n");
977 	} else if (!(netdev->flags & IFF_PROMISC) &&
978 		   !(netdev->flags & IFF_ALLMULTI)) {
979 		/* State 2 - unicast/multicast promiscuous mode disabled
980 		 * - !IFF_PROMISC && !IFF_ALLMULTI
981 		 */
982 		flags = 0;
983 		adapter->current_netdev_promisc_flags &=
984 			~(IFF_PROMISC | IFF_ALLMULTI);
985 		dev_info(&adapter->pdev->dev, "Leaving promiscuous mode\n");
986 	} else {
987 		/* State 3 - unicast/multicast promiscuous mode enabled
988 		 * - IFF_PROMISC && IFF_ALLMULTI
989 		 * - IFF_PROMISC && !IFF_ALLMULTI
990 		 */
991 		flags = FLAG_VF_UNICAST_PROMISC | FLAG_VF_MULTICAST_PROMISC;
992 		adapter->current_netdev_promisc_flags |= IFF_PROMISC;
993 		if (netdev->flags & IFF_ALLMULTI)
994 			adapter->current_netdev_promisc_flags |= IFF_ALLMULTI;
995 		else
996 			adapter->current_netdev_promisc_flags &= ~IFF_ALLMULTI;
997 
998 		dev_info(&adapter->pdev->dev, "Entering promiscuous mode\n");
999 	}
1000 
1001 	adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_PROMISC_MODE;
1002 
1003 	/* allow changes to promiscuous flags */
1004 	spin_unlock_bh(&adapter->current_netdev_promisc_flags_lock);
1005 
1006 	adapter->current_op = VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
1007 	vpi.vsi_id = adapter->vsi_res->vsi_id;
1008 	vpi.flags = flags;
1009 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1010 			 (u8 *)&vpi, sizeof(vpi));
1011 }
1012 
1013 /**
1014  * iavf_request_stats
1015  * @adapter: adapter structure
1016  *
1017  * Request VSI statistics from PF.
1018  **/
1019 void iavf_request_stats(struct iavf_adapter *adapter)
1020 {
1021 	struct virtchnl_queue_select vqs;
1022 
1023 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1024 		/* no error message, this isn't crucial */
1025 		return;
1026 	}
1027 
1028 	adapter->aq_required &= ~IAVF_FLAG_AQ_REQUEST_STATS;
1029 	adapter->current_op = VIRTCHNL_OP_GET_STATS;
1030 	vqs.vsi_id = adapter->vsi_res->vsi_id;
1031 	/* queue maps are ignored for this message - only the vsi is used */
1032 	if (iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_STATS, (u8 *)&vqs,
1033 			     sizeof(vqs)))
1034 		/* if the request failed, don't lock out others */
1035 		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1036 }
1037 
1038 /**
1039  * iavf_get_hena
1040  * @adapter: adapter structure
1041  *
1042  * Request hash enable capabilities from PF
1043  **/
1044 void iavf_get_hena(struct iavf_adapter *adapter)
1045 {
1046 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1047 		/* bail because we already have a command pending */
1048 		dev_err(&adapter->pdev->dev, "Cannot get RSS hash capabilities, command %d pending\n",
1049 			adapter->current_op);
1050 		return;
1051 	}
1052 	adapter->current_op = VIRTCHNL_OP_GET_RSS_HENA_CAPS;
1053 	adapter->aq_required &= ~IAVF_FLAG_AQ_GET_HENA;
1054 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_RSS_HENA_CAPS, NULL, 0);
1055 }
1056 
1057 /**
1058  * iavf_set_hena
1059  * @adapter: adapter structure
1060  *
1061  * Request the PF to set our RSS hash capabilities
1062  **/
1063 void iavf_set_hena(struct iavf_adapter *adapter)
1064 {
1065 	struct virtchnl_rss_hena vrh;
1066 
1067 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1068 		/* bail because we already have a command pending */
1069 		dev_err(&adapter->pdev->dev, "Cannot set RSS hash enable, command %d pending\n",
1070 			adapter->current_op);
1071 		return;
1072 	}
1073 	vrh.hena = adapter->hena;
1074 	adapter->current_op = VIRTCHNL_OP_SET_RSS_HENA;
1075 	adapter->aq_required &= ~IAVF_FLAG_AQ_SET_HENA;
1076 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_SET_RSS_HENA, (u8 *)&vrh,
1077 			 sizeof(vrh));
1078 }
1079 
1080 /**
1081  * iavf_set_rss_key
1082  * @adapter: adapter structure
1083  *
1084  * Request the PF to set our RSS hash key
1085  **/
1086 void iavf_set_rss_key(struct iavf_adapter *adapter)
1087 {
1088 	struct virtchnl_rss_key *vrk;
1089 	int len;
1090 
1091 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1092 		/* bail because we already have a command pending */
1093 		dev_err(&adapter->pdev->dev, "Cannot set RSS key, command %d pending\n",
1094 			adapter->current_op);
1095 		return;
1096 	}
1097 	len = virtchnl_struct_size(vrk, key, adapter->rss_key_size);
1098 	vrk = kzalloc(len, GFP_KERNEL);
1099 	if (!vrk)
1100 		return;
1101 	vrk->vsi_id = adapter->vsi.id;
1102 	vrk->key_len = adapter->rss_key_size;
1103 	memcpy(vrk->key, adapter->rss_key, adapter->rss_key_size);
1104 
1105 	adapter->current_op = VIRTCHNL_OP_CONFIG_RSS_KEY;
1106 	adapter->aq_required &= ~IAVF_FLAG_AQ_SET_RSS_KEY;
1107 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_RSS_KEY, (u8 *)vrk, len);
1108 	kfree(vrk);
1109 }
1110 
1111 /**
1112  * iavf_set_rss_lut
1113  * @adapter: adapter structure
1114  *
1115  * Request the PF to set our RSS lookup table
1116  **/
1117 void iavf_set_rss_lut(struct iavf_adapter *adapter)
1118 {
1119 	struct virtchnl_rss_lut *vrl;
1120 	int len;
1121 
1122 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1123 		/* bail because we already have a command pending */
1124 		dev_err(&adapter->pdev->dev, "Cannot set RSS LUT, command %d pending\n",
1125 			adapter->current_op);
1126 		return;
1127 	}
1128 	len = virtchnl_struct_size(vrl, lut, adapter->rss_lut_size);
1129 	vrl = kzalloc(len, GFP_KERNEL);
1130 	if (!vrl)
1131 		return;
1132 	vrl->vsi_id = adapter->vsi.id;
1133 	vrl->lut_entries = adapter->rss_lut_size;
1134 	memcpy(vrl->lut, adapter->rss_lut, adapter->rss_lut_size);
1135 	adapter->current_op = VIRTCHNL_OP_CONFIG_RSS_LUT;
1136 	adapter->aq_required &= ~IAVF_FLAG_AQ_SET_RSS_LUT;
1137 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_RSS_LUT, (u8 *)vrl, len);
1138 	kfree(vrl);
1139 }
1140 
1141 /**
1142  * iavf_enable_vlan_stripping
1143  * @adapter: adapter structure
1144  *
1145  * Request VLAN header stripping to be enabled
1146  **/
1147 void iavf_enable_vlan_stripping(struct iavf_adapter *adapter)
1148 {
1149 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1150 		/* bail because we already have a command pending */
1151 		dev_err(&adapter->pdev->dev, "Cannot enable stripping, command %d pending\n",
1152 			adapter->current_op);
1153 		return;
1154 	}
1155 	adapter->current_op = VIRTCHNL_OP_ENABLE_VLAN_STRIPPING;
1156 	adapter->aq_required &= ~IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
1157 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING, NULL, 0);
1158 }
1159 
1160 /**
1161  * iavf_disable_vlan_stripping
1162  * @adapter: adapter structure
1163  *
1164  * Request VLAN header stripping to be disabled
1165  **/
1166 void iavf_disable_vlan_stripping(struct iavf_adapter *adapter)
1167 {
1168 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1169 		/* bail because we already have a command pending */
1170 		dev_err(&adapter->pdev->dev, "Cannot disable stripping, command %d pending\n",
1171 			adapter->current_op);
1172 		return;
1173 	}
1174 	adapter->current_op = VIRTCHNL_OP_DISABLE_VLAN_STRIPPING;
1175 	adapter->aq_required &= ~IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
1176 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING, NULL, 0);
1177 }
1178 
1179 /**
1180  * iavf_tpid_to_vc_ethertype - transform from VLAN TPID to virtchnl ethertype
1181  * @tpid: VLAN TPID (i.e. 0x8100, 0x88a8, etc.)
1182  */
1183 static u32 iavf_tpid_to_vc_ethertype(u16 tpid)
1184 {
1185 	switch (tpid) {
1186 	case ETH_P_8021Q:
1187 		return VIRTCHNL_VLAN_ETHERTYPE_8100;
1188 	case ETH_P_8021AD:
1189 		return VIRTCHNL_VLAN_ETHERTYPE_88A8;
1190 	}
1191 
1192 	return 0;
1193 }
1194 
1195 /**
1196  * iavf_set_vc_offload_ethertype - set virtchnl ethertype for offload message
1197  * @adapter: adapter structure
1198  * @msg: message structure used for updating offloads over virtchnl to update
1199  * @tpid: VLAN TPID (i.e. 0x8100, 0x88a8, etc.)
1200  * @offload_op: opcode used to determine which support structure to check
1201  */
1202 static int
1203 iavf_set_vc_offload_ethertype(struct iavf_adapter *adapter,
1204 			      struct virtchnl_vlan_setting *msg, u16 tpid,
1205 			      enum virtchnl_ops offload_op)
1206 {
1207 	struct virtchnl_vlan_supported_caps *offload_support;
1208 	u16 vc_ethertype = iavf_tpid_to_vc_ethertype(tpid);
1209 
1210 	/* reference the correct offload support structure */
1211 	switch (offload_op) {
1212 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2:
1213 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2:
1214 		offload_support =
1215 			&adapter->vlan_v2_caps.offloads.stripping_support;
1216 		break;
1217 	case VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2:
1218 	case VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2:
1219 		offload_support =
1220 			&adapter->vlan_v2_caps.offloads.insertion_support;
1221 		break;
1222 	default:
1223 		dev_err(&adapter->pdev->dev, "Invalid opcode %d for setting virtchnl ethertype to enable/disable VLAN offloads\n",
1224 			offload_op);
1225 		return -EINVAL;
1226 	}
1227 
1228 	/* make sure ethertype is supported */
1229 	if (offload_support->outer & vc_ethertype &&
1230 	    offload_support->outer & VIRTCHNL_VLAN_TOGGLE) {
1231 		msg->outer_ethertype_setting = vc_ethertype;
1232 	} else if (offload_support->inner & vc_ethertype &&
1233 		   offload_support->inner & VIRTCHNL_VLAN_TOGGLE) {
1234 		msg->inner_ethertype_setting = vc_ethertype;
1235 	} else {
1236 		dev_dbg(&adapter->pdev->dev, "opcode %d unsupported for VLAN TPID 0x%04x\n",
1237 			offload_op, tpid);
1238 		return -EINVAL;
1239 	}
1240 
1241 	return 0;
1242 }
1243 
1244 /**
1245  * iavf_clear_offload_v2_aq_required - clear AQ required bit for offload request
1246  * @adapter: adapter structure
1247  * @tpid: VLAN TPID
1248  * @offload_op: opcode used to determine which AQ required bit to clear
1249  */
1250 static void
1251 iavf_clear_offload_v2_aq_required(struct iavf_adapter *adapter, u16 tpid,
1252 				  enum virtchnl_ops offload_op)
1253 {
1254 	switch (offload_op) {
1255 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2:
1256 		if (tpid == ETH_P_8021Q)
1257 			adapter->aq_required &=
1258 				~IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING;
1259 		else if (tpid == ETH_P_8021AD)
1260 			adapter->aq_required &=
1261 				~IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING;
1262 		break;
1263 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2:
1264 		if (tpid == ETH_P_8021Q)
1265 			adapter->aq_required &=
1266 				~IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING;
1267 		else if (tpid == ETH_P_8021AD)
1268 			adapter->aq_required &=
1269 				~IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING;
1270 		break;
1271 	case VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2:
1272 		if (tpid == ETH_P_8021Q)
1273 			adapter->aq_required &=
1274 				~IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION;
1275 		else if (tpid == ETH_P_8021AD)
1276 			adapter->aq_required &=
1277 				~IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION;
1278 		break;
1279 	case VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2:
1280 		if (tpid == ETH_P_8021Q)
1281 			adapter->aq_required &=
1282 				~IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION;
1283 		else if (tpid == ETH_P_8021AD)
1284 			adapter->aq_required &=
1285 				~IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION;
1286 		break;
1287 	default:
1288 		dev_err(&adapter->pdev->dev, "Unsupported opcode %d specified for clearing aq_required bits for VIRTCHNL_VF_OFFLOAD_VLAN_V2 offload request\n",
1289 			offload_op);
1290 	}
1291 }
1292 
1293 /**
1294  * iavf_send_vlan_offload_v2 - send offload enable/disable over virtchnl
1295  * @adapter: adapter structure
1296  * @tpid: VLAN TPID used for the command (i.e. 0x8100 or 0x88a8)
1297  * @offload_op: offload_op used to make the request over virtchnl
1298  */
1299 static void
1300 iavf_send_vlan_offload_v2(struct iavf_adapter *adapter, u16 tpid,
1301 			  enum virtchnl_ops offload_op)
1302 {
1303 	struct virtchnl_vlan_setting *msg;
1304 	int len = sizeof(*msg);
1305 
1306 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1307 		/* bail because we already have a command pending */
1308 		dev_err(&adapter->pdev->dev, "Cannot send %d, command %d pending\n",
1309 			offload_op, adapter->current_op);
1310 		return;
1311 	}
1312 
1313 	adapter->current_op = offload_op;
1314 
1315 	msg = kzalloc(len, GFP_KERNEL);
1316 	if (!msg)
1317 		return;
1318 
1319 	msg->vport_id = adapter->vsi_res->vsi_id;
1320 
1321 	/* always clear to prevent unsupported and endless requests */
1322 	iavf_clear_offload_v2_aq_required(adapter, tpid, offload_op);
1323 
1324 	/* only send valid offload requests */
1325 	if (!iavf_set_vc_offload_ethertype(adapter, msg, tpid, offload_op))
1326 		iavf_send_pf_msg(adapter, offload_op, (u8 *)msg, len);
1327 	else
1328 		adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1329 
1330 	kfree(msg);
1331 }
1332 
1333 /**
1334  * iavf_enable_vlan_stripping_v2 - enable VLAN stripping
1335  * @adapter: adapter structure
1336  * @tpid: VLAN TPID used to enable VLAN stripping
1337  */
1338 void iavf_enable_vlan_stripping_v2(struct iavf_adapter *adapter, u16 tpid)
1339 {
1340 	iavf_send_vlan_offload_v2(adapter, tpid,
1341 				  VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2);
1342 }
1343 
1344 /**
1345  * iavf_disable_vlan_stripping_v2 - disable VLAN stripping
1346  * @adapter: adapter structure
1347  * @tpid: VLAN TPID used to disable VLAN stripping
1348  */
1349 void iavf_disable_vlan_stripping_v2(struct iavf_adapter *adapter, u16 tpid)
1350 {
1351 	iavf_send_vlan_offload_v2(adapter, tpid,
1352 				  VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2);
1353 }
1354 
1355 /**
1356  * iavf_enable_vlan_insertion_v2 - enable VLAN insertion
1357  * @adapter: adapter structure
1358  * @tpid: VLAN TPID used to enable VLAN insertion
1359  */
1360 void iavf_enable_vlan_insertion_v2(struct iavf_adapter *adapter, u16 tpid)
1361 {
1362 	iavf_send_vlan_offload_v2(adapter, tpid,
1363 				  VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2);
1364 }
1365 
1366 /**
1367  * iavf_disable_vlan_insertion_v2 - disable VLAN insertion
1368  * @adapter: adapter structure
1369  * @tpid: VLAN TPID used to disable VLAN insertion
1370  */
1371 void iavf_disable_vlan_insertion_v2(struct iavf_adapter *adapter, u16 tpid)
1372 {
1373 	iavf_send_vlan_offload_v2(adapter, tpid,
1374 				  VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2);
1375 }
1376 
1377 #define IAVF_MAX_SPEED_STRLEN	13
1378 
1379 /**
1380  * iavf_print_link_message - print link up or down
1381  * @adapter: adapter structure
1382  *
1383  * Log a message telling the world of our wonderous link status
1384  */
1385 static void iavf_print_link_message(struct iavf_adapter *adapter)
1386 {
1387 	struct net_device *netdev = adapter->netdev;
1388 	int link_speed_mbps;
1389 	char *speed;
1390 
1391 	if (!adapter->link_up) {
1392 		netdev_info(netdev, "NIC Link is Down\n");
1393 		return;
1394 	}
1395 
1396 	speed = kzalloc(IAVF_MAX_SPEED_STRLEN, GFP_KERNEL);
1397 	if (!speed)
1398 		return;
1399 
1400 	if (ADV_LINK_SUPPORT(adapter)) {
1401 		link_speed_mbps = adapter->link_speed_mbps;
1402 		goto print_link_msg;
1403 	}
1404 
1405 	switch (adapter->link_speed) {
1406 	case VIRTCHNL_LINK_SPEED_40GB:
1407 		link_speed_mbps = SPEED_40000;
1408 		break;
1409 	case VIRTCHNL_LINK_SPEED_25GB:
1410 		link_speed_mbps = SPEED_25000;
1411 		break;
1412 	case VIRTCHNL_LINK_SPEED_20GB:
1413 		link_speed_mbps = SPEED_20000;
1414 		break;
1415 	case VIRTCHNL_LINK_SPEED_10GB:
1416 		link_speed_mbps = SPEED_10000;
1417 		break;
1418 	case VIRTCHNL_LINK_SPEED_5GB:
1419 		link_speed_mbps = SPEED_5000;
1420 		break;
1421 	case VIRTCHNL_LINK_SPEED_2_5GB:
1422 		link_speed_mbps = SPEED_2500;
1423 		break;
1424 	case VIRTCHNL_LINK_SPEED_1GB:
1425 		link_speed_mbps = SPEED_1000;
1426 		break;
1427 	case VIRTCHNL_LINK_SPEED_100MB:
1428 		link_speed_mbps = SPEED_100;
1429 		break;
1430 	default:
1431 		link_speed_mbps = SPEED_UNKNOWN;
1432 		break;
1433 	}
1434 
1435 print_link_msg:
1436 	if (link_speed_mbps > SPEED_1000) {
1437 		if (link_speed_mbps == SPEED_2500)
1438 			snprintf(speed, IAVF_MAX_SPEED_STRLEN, "2.5 Gbps");
1439 		else
1440 			/* convert to Gbps inline */
1441 			snprintf(speed, IAVF_MAX_SPEED_STRLEN, "%d %s",
1442 				 link_speed_mbps / 1000, "Gbps");
1443 	} else if (link_speed_mbps == SPEED_UNKNOWN) {
1444 		snprintf(speed, IAVF_MAX_SPEED_STRLEN, "%s", "Unknown Mbps");
1445 	} else {
1446 		snprintf(speed, IAVF_MAX_SPEED_STRLEN, "%d %s",
1447 			 link_speed_mbps, "Mbps");
1448 	}
1449 
1450 	netdev_info(netdev, "NIC Link is Up Speed is %s Full Duplex\n", speed);
1451 	kfree(speed);
1452 }
1453 
1454 /**
1455  * iavf_get_vpe_link_status
1456  * @adapter: adapter structure
1457  * @vpe: virtchnl_pf_event structure
1458  *
1459  * Helper function for determining the link status
1460  **/
1461 static bool
1462 iavf_get_vpe_link_status(struct iavf_adapter *adapter,
1463 			 struct virtchnl_pf_event *vpe)
1464 {
1465 	if (ADV_LINK_SUPPORT(adapter))
1466 		return vpe->event_data.link_event_adv.link_status;
1467 	else
1468 		return vpe->event_data.link_event.link_status;
1469 }
1470 
1471 /**
1472  * iavf_set_adapter_link_speed_from_vpe
1473  * @adapter: adapter structure for which we are setting the link speed
1474  * @vpe: virtchnl_pf_event structure that contains the link speed we are setting
1475  *
1476  * Helper function for setting iavf_adapter link speed
1477  **/
1478 static void
1479 iavf_set_adapter_link_speed_from_vpe(struct iavf_adapter *adapter,
1480 				     struct virtchnl_pf_event *vpe)
1481 {
1482 	if (ADV_LINK_SUPPORT(adapter))
1483 		adapter->link_speed_mbps =
1484 			vpe->event_data.link_event_adv.link_speed;
1485 	else
1486 		adapter->link_speed = vpe->event_data.link_event.link_speed;
1487 }
1488 
1489 /**
1490  * iavf_enable_channels
1491  * @adapter: adapter structure
1492  *
1493  * Request that the PF enable channels as specified by
1494  * the user via tc tool.
1495  **/
1496 void iavf_enable_channels(struct iavf_adapter *adapter)
1497 {
1498 	struct virtchnl_tc_info *vti = NULL;
1499 	size_t len;
1500 	int i;
1501 
1502 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1503 		/* bail because we already have a command pending */
1504 		dev_err(&adapter->pdev->dev, "Cannot configure mqprio, command %d pending\n",
1505 			adapter->current_op);
1506 		return;
1507 	}
1508 
1509 	len = virtchnl_struct_size(vti, list, adapter->num_tc);
1510 	vti = kzalloc(len, GFP_KERNEL);
1511 	if (!vti)
1512 		return;
1513 	vti->num_tc = adapter->num_tc;
1514 	for (i = 0; i < vti->num_tc; i++) {
1515 		vti->list[i].count = adapter->ch_config.ch_info[i].count;
1516 		vti->list[i].offset = adapter->ch_config.ch_info[i].offset;
1517 		vti->list[i].pad = 0;
1518 		vti->list[i].max_tx_rate =
1519 				adapter->ch_config.ch_info[i].max_tx_rate;
1520 	}
1521 
1522 	adapter->ch_config.state = __IAVF_TC_RUNNING;
1523 	adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1524 	adapter->current_op = VIRTCHNL_OP_ENABLE_CHANNELS;
1525 	adapter->aq_required &= ~IAVF_FLAG_AQ_ENABLE_CHANNELS;
1526 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_ENABLE_CHANNELS, (u8 *)vti, len);
1527 	kfree(vti);
1528 }
1529 
1530 /**
1531  * iavf_disable_channels
1532  * @adapter: adapter structure
1533  *
1534  * Request that the PF disable channels that are configured
1535  **/
1536 void iavf_disable_channels(struct iavf_adapter *adapter)
1537 {
1538 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1539 		/* bail because we already have a command pending */
1540 		dev_err(&adapter->pdev->dev, "Cannot configure mqprio, command %d pending\n",
1541 			adapter->current_op);
1542 		return;
1543 	}
1544 
1545 	adapter->ch_config.state = __IAVF_TC_INVALID;
1546 	adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1547 	adapter->current_op = VIRTCHNL_OP_DISABLE_CHANNELS;
1548 	adapter->aq_required &= ~IAVF_FLAG_AQ_DISABLE_CHANNELS;
1549 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_DISABLE_CHANNELS, NULL, 0);
1550 }
1551 
1552 /**
1553  * iavf_print_cloud_filter
1554  * @adapter: adapter structure
1555  * @f: cloud filter to print
1556  *
1557  * Print the cloud filter
1558  **/
1559 static void iavf_print_cloud_filter(struct iavf_adapter *adapter,
1560 				    struct virtchnl_filter *f)
1561 {
1562 	switch (f->flow_type) {
1563 	case VIRTCHNL_TCP_V4_FLOW:
1564 		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",
1565 			 &f->data.tcp_spec.dst_mac,
1566 			 &f->data.tcp_spec.src_mac,
1567 			 ntohs(f->data.tcp_spec.vlan_id),
1568 			 &f->data.tcp_spec.dst_ip[0],
1569 			 &f->data.tcp_spec.src_ip[0],
1570 			 ntohs(f->data.tcp_spec.dst_port),
1571 			 ntohs(f->data.tcp_spec.src_port));
1572 		break;
1573 	case VIRTCHNL_TCP_V6_FLOW:
1574 		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",
1575 			 &f->data.tcp_spec.dst_mac,
1576 			 &f->data.tcp_spec.src_mac,
1577 			 ntohs(f->data.tcp_spec.vlan_id),
1578 			 &f->data.tcp_spec.dst_ip,
1579 			 &f->data.tcp_spec.src_ip,
1580 			 ntohs(f->data.tcp_spec.dst_port),
1581 			 ntohs(f->data.tcp_spec.src_port));
1582 		break;
1583 	}
1584 }
1585 
1586 /**
1587  * iavf_add_cloud_filter
1588  * @adapter: adapter structure
1589  *
1590  * Request that the PF add cloud filters as specified
1591  * by the user via tc tool.
1592  **/
1593 void iavf_add_cloud_filter(struct iavf_adapter *adapter)
1594 {
1595 	struct iavf_cloud_filter *cf;
1596 	struct virtchnl_filter *f;
1597 	int len = 0, count = 0;
1598 
1599 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1600 		/* bail because we already have a command pending */
1601 		dev_err(&adapter->pdev->dev, "Cannot add cloud filter, command %d pending\n",
1602 			adapter->current_op);
1603 		return;
1604 	}
1605 	list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1606 		if (cf->add) {
1607 			count++;
1608 			break;
1609 		}
1610 	}
1611 	if (!count) {
1612 		adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
1613 		return;
1614 	}
1615 	adapter->current_op = VIRTCHNL_OP_ADD_CLOUD_FILTER;
1616 
1617 	len = sizeof(struct virtchnl_filter);
1618 	f = kzalloc(len, GFP_KERNEL);
1619 	if (!f)
1620 		return;
1621 
1622 	list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1623 		if (cf->add) {
1624 			memcpy(f, &cf->f, sizeof(struct virtchnl_filter));
1625 			cf->add = false;
1626 			cf->state = __IAVF_CF_ADD_PENDING;
1627 			iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_CLOUD_FILTER,
1628 					 (u8 *)f, len);
1629 		}
1630 	}
1631 	kfree(f);
1632 }
1633 
1634 /**
1635  * iavf_del_cloud_filter
1636  * @adapter: adapter structure
1637  *
1638  * Request that the PF delete cloud filters as specified
1639  * by the user via tc tool.
1640  **/
1641 void iavf_del_cloud_filter(struct iavf_adapter *adapter)
1642 {
1643 	struct iavf_cloud_filter *cf, *cftmp;
1644 	struct virtchnl_filter *f;
1645 	int len = 0, count = 0;
1646 
1647 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1648 		/* bail because we already have a command pending */
1649 		dev_err(&adapter->pdev->dev, "Cannot remove cloud filter, command %d pending\n",
1650 			adapter->current_op);
1651 		return;
1652 	}
1653 	list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1654 		if (cf->del) {
1655 			count++;
1656 			break;
1657 		}
1658 	}
1659 	if (!count) {
1660 		adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1661 		return;
1662 	}
1663 	adapter->current_op = VIRTCHNL_OP_DEL_CLOUD_FILTER;
1664 
1665 	len = sizeof(struct virtchnl_filter);
1666 	f = kzalloc(len, GFP_KERNEL);
1667 	if (!f)
1668 		return;
1669 
1670 	list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
1671 		if (cf->del) {
1672 			memcpy(f, &cf->f, sizeof(struct virtchnl_filter));
1673 			cf->del = false;
1674 			cf->state = __IAVF_CF_DEL_PENDING;
1675 			iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_CLOUD_FILTER,
1676 					 (u8 *)f, len);
1677 		}
1678 	}
1679 	kfree(f);
1680 }
1681 
1682 /**
1683  * iavf_add_fdir_filter
1684  * @adapter: the VF adapter structure
1685  *
1686  * Request that the PF add Flow Director filters as specified
1687  * by the user via ethtool.
1688  **/
1689 void iavf_add_fdir_filter(struct iavf_adapter *adapter)
1690 {
1691 	struct iavf_fdir_fltr *fdir;
1692 	struct virtchnl_fdir_add *f;
1693 	bool process_fltr = false;
1694 	int len;
1695 
1696 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1697 		/* bail because we already have a command pending */
1698 		dev_err(&adapter->pdev->dev, "Cannot add Flow Director filter, command %d pending\n",
1699 			adapter->current_op);
1700 		return;
1701 	}
1702 
1703 	len = sizeof(struct virtchnl_fdir_add);
1704 	f = kzalloc(len, GFP_KERNEL);
1705 	if (!f)
1706 		return;
1707 
1708 	spin_lock_bh(&adapter->fdir_fltr_lock);
1709 	list_for_each_entry(fdir, &adapter->fdir_list_head, list) {
1710 		if (fdir->state == IAVF_FDIR_FLTR_ADD_REQUEST) {
1711 			process_fltr = true;
1712 			fdir->state = IAVF_FDIR_FLTR_ADD_PENDING;
1713 			memcpy(f, &fdir->vc_add_msg, len);
1714 			break;
1715 		}
1716 	}
1717 	spin_unlock_bh(&adapter->fdir_fltr_lock);
1718 
1719 	if (!process_fltr) {
1720 		/* prevent iavf_add_fdir_filter() from being called when there
1721 		 * are no filters to add
1722 		 */
1723 		adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_FDIR_FILTER;
1724 		kfree(f);
1725 		return;
1726 	}
1727 	adapter->current_op = VIRTCHNL_OP_ADD_FDIR_FILTER;
1728 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_FDIR_FILTER, (u8 *)f, len);
1729 	kfree(f);
1730 }
1731 
1732 /**
1733  * iavf_del_fdir_filter
1734  * @adapter: the VF adapter structure
1735  *
1736  * Request that the PF delete Flow Director filters as specified
1737  * by the user via ethtool.
1738  **/
1739 void iavf_del_fdir_filter(struct iavf_adapter *adapter)
1740 {
1741 	struct iavf_fdir_fltr *fdir;
1742 	struct virtchnl_fdir_del f;
1743 	bool process_fltr = false;
1744 	int len;
1745 
1746 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1747 		/* bail because we already have a command pending */
1748 		dev_err(&adapter->pdev->dev, "Cannot remove Flow Director filter, command %d pending\n",
1749 			adapter->current_op);
1750 		return;
1751 	}
1752 
1753 	len = sizeof(struct virtchnl_fdir_del);
1754 
1755 	spin_lock_bh(&adapter->fdir_fltr_lock);
1756 	list_for_each_entry(fdir, &adapter->fdir_list_head, list) {
1757 		if (fdir->state == IAVF_FDIR_FLTR_DEL_REQUEST) {
1758 			process_fltr = true;
1759 			memset(&f, 0, len);
1760 			f.vsi_id = fdir->vc_add_msg.vsi_id;
1761 			f.flow_id = fdir->flow_id;
1762 			fdir->state = IAVF_FDIR_FLTR_DEL_PENDING;
1763 			break;
1764 		}
1765 	}
1766 	spin_unlock_bh(&adapter->fdir_fltr_lock);
1767 
1768 	if (!process_fltr) {
1769 		adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_FDIR_FILTER;
1770 		return;
1771 	}
1772 
1773 	adapter->current_op = VIRTCHNL_OP_DEL_FDIR_FILTER;
1774 	iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_FDIR_FILTER, (u8 *)&f, len);
1775 }
1776 
1777 /**
1778  * iavf_add_adv_rss_cfg
1779  * @adapter: the VF adapter structure
1780  *
1781  * Request that the PF add RSS configuration as specified
1782  * by the user via ethtool.
1783  **/
1784 void iavf_add_adv_rss_cfg(struct iavf_adapter *adapter)
1785 {
1786 	struct virtchnl_rss_cfg *rss_cfg;
1787 	struct iavf_adv_rss *rss;
1788 	bool process_rss = false;
1789 	int len;
1790 
1791 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1792 		/* bail because we already have a command pending */
1793 		dev_err(&adapter->pdev->dev, "Cannot add RSS configuration, command %d pending\n",
1794 			adapter->current_op);
1795 		return;
1796 	}
1797 
1798 	len = sizeof(struct virtchnl_rss_cfg);
1799 	rss_cfg = kzalloc(len, GFP_KERNEL);
1800 	if (!rss_cfg)
1801 		return;
1802 
1803 	spin_lock_bh(&adapter->adv_rss_lock);
1804 	list_for_each_entry(rss, &adapter->adv_rss_list_head, list) {
1805 		if (rss->state == IAVF_ADV_RSS_ADD_REQUEST) {
1806 			process_rss = true;
1807 			rss->state = IAVF_ADV_RSS_ADD_PENDING;
1808 			memcpy(rss_cfg, &rss->cfg_msg, len);
1809 			iavf_print_adv_rss_cfg(adapter, rss,
1810 					       "Input set change for",
1811 					       "is pending");
1812 			break;
1813 		}
1814 	}
1815 	spin_unlock_bh(&adapter->adv_rss_lock);
1816 
1817 	if (process_rss) {
1818 		adapter->current_op = VIRTCHNL_OP_ADD_RSS_CFG;
1819 		iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_RSS_CFG,
1820 				 (u8 *)rss_cfg, len);
1821 	} else {
1822 		adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_ADV_RSS_CFG;
1823 	}
1824 
1825 	kfree(rss_cfg);
1826 }
1827 
1828 /**
1829  * iavf_del_adv_rss_cfg
1830  * @adapter: the VF adapter structure
1831  *
1832  * Request that the PF delete RSS configuration as specified
1833  * by the user via ethtool.
1834  **/
1835 void iavf_del_adv_rss_cfg(struct iavf_adapter *adapter)
1836 {
1837 	struct virtchnl_rss_cfg *rss_cfg;
1838 	struct iavf_adv_rss *rss;
1839 	bool process_rss = false;
1840 	int len;
1841 
1842 	if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1843 		/* bail because we already have a command pending */
1844 		dev_err(&adapter->pdev->dev, "Cannot remove RSS configuration, command %d pending\n",
1845 			adapter->current_op);
1846 		return;
1847 	}
1848 
1849 	len = sizeof(struct virtchnl_rss_cfg);
1850 	rss_cfg = kzalloc(len, GFP_KERNEL);
1851 	if (!rss_cfg)
1852 		return;
1853 
1854 	spin_lock_bh(&adapter->adv_rss_lock);
1855 	list_for_each_entry(rss, &adapter->adv_rss_list_head, list) {
1856 		if (rss->state == IAVF_ADV_RSS_DEL_REQUEST) {
1857 			process_rss = true;
1858 			rss->state = IAVF_ADV_RSS_DEL_PENDING;
1859 			memcpy(rss_cfg, &rss->cfg_msg, len);
1860 			break;
1861 		}
1862 	}
1863 	spin_unlock_bh(&adapter->adv_rss_lock);
1864 
1865 	if (process_rss) {
1866 		adapter->current_op = VIRTCHNL_OP_DEL_RSS_CFG;
1867 		iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_RSS_CFG,
1868 				 (u8 *)rss_cfg, len);
1869 	} else {
1870 		adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_ADV_RSS_CFG;
1871 	}
1872 
1873 	kfree(rss_cfg);
1874 }
1875 
1876 /**
1877  * iavf_request_reset
1878  * @adapter: adapter structure
1879  *
1880  * Request that the PF reset this VF. No response is expected.
1881  **/
1882 int iavf_request_reset(struct iavf_adapter *adapter)
1883 {
1884 	int err;
1885 	/* Don't check CURRENT_OP - this is always higher priority */
1886 	err = iavf_send_pf_msg(adapter, VIRTCHNL_OP_RESET_VF, NULL, 0);
1887 	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1888 	return err;
1889 }
1890 
1891 /**
1892  * iavf_netdev_features_vlan_strip_set - update vlan strip status
1893  * @netdev: ptr to netdev being adjusted
1894  * @enable: enable or disable vlan strip
1895  *
1896  * Helper function to change vlan strip status in netdev->features.
1897  */
1898 static void iavf_netdev_features_vlan_strip_set(struct net_device *netdev,
1899 						const bool enable)
1900 {
1901 	if (enable)
1902 		netdev->features |= NETIF_F_HW_VLAN_CTAG_RX;
1903 	else
1904 		netdev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
1905 }
1906 
1907 /**
1908  * iavf_virtchnl_completion
1909  * @adapter: adapter structure
1910  * @v_opcode: opcode sent by PF
1911  * @v_retval: retval sent by PF
1912  * @msg: message sent by PF
1913  * @msglen: message length
1914  *
1915  * Asynchronous completion function for admin queue messages. Rather than busy
1916  * wait, we fire off our requests and assume that no errors will be returned.
1917  * This function handles the reply messages.
1918  **/
1919 void iavf_virtchnl_completion(struct iavf_adapter *adapter,
1920 			      enum virtchnl_ops v_opcode,
1921 			      enum iavf_status v_retval, u8 *msg, u16 msglen)
1922 {
1923 	struct net_device *netdev = adapter->netdev;
1924 
1925 	if (v_opcode == VIRTCHNL_OP_EVENT) {
1926 		struct virtchnl_pf_event *vpe =
1927 			(struct virtchnl_pf_event *)msg;
1928 		bool link_up = iavf_get_vpe_link_status(adapter, vpe);
1929 
1930 		switch (vpe->event) {
1931 		case VIRTCHNL_EVENT_LINK_CHANGE:
1932 			iavf_set_adapter_link_speed_from_vpe(adapter, vpe);
1933 
1934 			/* we've already got the right link status, bail */
1935 			if (adapter->link_up == link_up)
1936 				break;
1937 
1938 			if (link_up) {
1939 				/* If we get link up message and start queues
1940 				 * before our queues are configured it will
1941 				 * trigger a TX hang. In that case, just ignore
1942 				 * the link status message,we'll get another one
1943 				 * after we enable queues and actually prepared
1944 				 * to send traffic.
1945 				 */
1946 				if (adapter->state != __IAVF_RUNNING)
1947 					break;
1948 
1949 				/* For ADq enabled VF, we reconfigure VSIs and
1950 				 * re-allocate queues. Hence wait till all
1951 				 * queues are enabled.
1952 				 */
1953 				if (adapter->flags &
1954 				    IAVF_FLAG_QUEUES_DISABLED)
1955 					break;
1956 			}
1957 
1958 			adapter->link_up = link_up;
1959 			if (link_up) {
1960 				netif_tx_start_all_queues(netdev);
1961 				netif_carrier_on(netdev);
1962 			} else {
1963 				netif_tx_stop_all_queues(netdev);
1964 				netif_carrier_off(netdev);
1965 			}
1966 			iavf_print_link_message(adapter);
1967 			break;
1968 		case VIRTCHNL_EVENT_RESET_IMPENDING:
1969 			dev_info(&adapter->pdev->dev, "Reset indication received from the PF\n");
1970 			if (!(adapter->flags & IAVF_FLAG_RESET_PENDING)) {
1971 				dev_info(&adapter->pdev->dev, "Scheduling reset task\n");
1972 				iavf_schedule_reset(adapter, IAVF_FLAG_RESET_PENDING);
1973 			}
1974 			break;
1975 		default:
1976 			dev_err(&adapter->pdev->dev, "Unknown event %d from PF\n",
1977 				vpe->event);
1978 			break;
1979 		}
1980 		return;
1981 	}
1982 	if (v_retval) {
1983 		switch (v_opcode) {
1984 		case VIRTCHNL_OP_ADD_VLAN:
1985 			dev_err(&adapter->pdev->dev, "Failed to add VLAN filter, error %s\n",
1986 				iavf_stat_str(&adapter->hw, v_retval));
1987 			break;
1988 		case VIRTCHNL_OP_ADD_ETH_ADDR:
1989 			dev_err(&adapter->pdev->dev, "Failed to add MAC filter, error %s\n",
1990 				iavf_stat_str(&adapter->hw, v_retval));
1991 			iavf_mac_add_reject(adapter);
1992 			/* restore administratively set MAC address */
1993 			ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
1994 			wake_up(&adapter->vc_waitqueue);
1995 			break;
1996 		case VIRTCHNL_OP_DEL_VLAN:
1997 			dev_err(&adapter->pdev->dev, "Failed to delete VLAN filter, error %s\n",
1998 				iavf_stat_str(&adapter->hw, v_retval));
1999 			break;
2000 		case VIRTCHNL_OP_DEL_ETH_ADDR:
2001 			dev_err(&adapter->pdev->dev, "Failed to delete MAC filter, error %s\n",
2002 				iavf_stat_str(&adapter->hw, v_retval));
2003 			break;
2004 		case VIRTCHNL_OP_ENABLE_CHANNELS:
2005 			dev_err(&adapter->pdev->dev, "Failed to configure queue channels, error %s\n",
2006 				iavf_stat_str(&adapter->hw, v_retval));
2007 			adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
2008 			adapter->ch_config.state = __IAVF_TC_INVALID;
2009 			netdev_reset_tc(netdev);
2010 			netif_tx_start_all_queues(netdev);
2011 			break;
2012 		case VIRTCHNL_OP_DISABLE_CHANNELS:
2013 			dev_err(&adapter->pdev->dev, "Failed to disable queue channels, error %s\n",
2014 				iavf_stat_str(&adapter->hw, v_retval));
2015 			adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
2016 			adapter->ch_config.state = __IAVF_TC_RUNNING;
2017 			netif_tx_start_all_queues(netdev);
2018 			break;
2019 		case VIRTCHNL_OP_ADD_CLOUD_FILTER: {
2020 			struct iavf_cloud_filter *cf, *cftmp;
2021 
2022 			list_for_each_entry_safe(cf, cftmp,
2023 						 &adapter->cloud_filter_list,
2024 						 list) {
2025 				if (cf->state == __IAVF_CF_ADD_PENDING) {
2026 					cf->state = __IAVF_CF_INVALID;
2027 					dev_info(&adapter->pdev->dev, "Failed to add cloud filter, error %s\n",
2028 						 iavf_stat_str(&adapter->hw,
2029 							       v_retval));
2030 					iavf_print_cloud_filter(adapter,
2031 								&cf->f);
2032 					list_del(&cf->list);
2033 					kfree(cf);
2034 					adapter->num_cloud_filters--;
2035 				}
2036 			}
2037 			}
2038 			break;
2039 		case VIRTCHNL_OP_DEL_CLOUD_FILTER: {
2040 			struct iavf_cloud_filter *cf;
2041 
2042 			list_for_each_entry(cf, &adapter->cloud_filter_list,
2043 					    list) {
2044 				if (cf->state == __IAVF_CF_DEL_PENDING) {
2045 					cf->state = __IAVF_CF_ACTIVE;
2046 					dev_info(&adapter->pdev->dev, "Failed to del cloud filter, error %s\n",
2047 						 iavf_stat_str(&adapter->hw,
2048 							       v_retval));
2049 					iavf_print_cloud_filter(adapter,
2050 								&cf->f);
2051 				}
2052 			}
2053 			}
2054 			break;
2055 		case VIRTCHNL_OP_ADD_FDIR_FILTER: {
2056 			struct iavf_fdir_fltr *fdir, *fdir_tmp;
2057 
2058 			spin_lock_bh(&adapter->fdir_fltr_lock);
2059 			list_for_each_entry_safe(fdir, fdir_tmp,
2060 						 &adapter->fdir_list_head,
2061 						 list) {
2062 				if (fdir->state == IAVF_FDIR_FLTR_ADD_PENDING) {
2063 					dev_info(&adapter->pdev->dev, "Failed to add Flow Director filter, error %s\n",
2064 						 iavf_stat_str(&adapter->hw,
2065 							       v_retval));
2066 					iavf_print_fdir_fltr(adapter, fdir);
2067 					if (msglen)
2068 						dev_err(&adapter->pdev->dev,
2069 							"%s\n", msg);
2070 					list_del(&fdir->list);
2071 					kfree(fdir);
2072 					adapter->fdir_active_fltr--;
2073 				}
2074 			}
2075 			spin_unlock_bh(&adapter->fdir_fltr_lock);
2076 			}
2077 			break;
2078 		case VIRTCHNL_OP_DEL_FDIR_FILTER: {
2079 			struct iavf_fdir_fltr *fdir;
2080 
2081 			spin_lock_bh(&adapter->fdir_fltr_lock);
2082 			list_for_each_entry(fdir, &adapter->fdir_list_head,
2083 					    list) {
2084 				if (fdir->state == IAVF_FDIR_FLTR_DEL_PENDING) {
2085 					fdir->state = IAVF_FDIR_FLTR_ACTIVE;
2086 					dev_info(&adapter->pdev->dev, "Failed to del Flow Director filter, error %s\n",
2087 						 iavf_stat_str(&adapter->hw,
2088 							       v_retval));
2089 					iavf_print_fdir_fltr(adapter, fdir);
2090 				}
2091 			}
2092 			spin_unlock_bh(&adapter->fdir_fltr_lock);
2093 			}
2094 			break;
2095 		case VIRTCHNL_OP_ADD_RSS_CFG: {
2096 			struct iavf_adv_rss *rss, *rss_tmp;
2097 
2098 			spin_lock_bh(&adapter->adv_rss_lock);
2099 			list_for_each_entry_safe(rss, rss_tmp,
2100 						 &adapter->adv_rss_list_head,
2101 						 list) {
2102 				if (rss->state == IAVF_ADV_RSS_ADD_PENDING) {
2103 					iavf_print_adv_rss_cfg(adapter, rss,
2104 							       "Failed to change the input set for",
2105 							       NULL);
2106 					list_del(&rss->list);
2107 					kfree(rss);
2108 				}
2109 			}
2110 			spin_unlock_bh(&adapter->adv_rss_lock);
2111 			}
2112 			break;
2113 		case VIRTCHNL_OP_DEL_RSS_CFG: {
2114 			struct iavf_adv_rss *rss;
2115 
2116 			spin_lock_bh(&adapter->adv_rss_lock);
2117 			list_for_each_entry(rss, &adapter->adv_rss_list_head,
2118 					    list) {
2119 				if (rss->state == IAVF_ADV_RSS_DEL_PENDING) {
2120 					rss->state = IAVF_ADV_RSS_ACTIVE;
2121 					dev_err(&adapter->pdev->dev, "Failed to delete RSS configuration, error %s\n",
2122 						iavf_stat_str(&adapter->hw,
2123 							      v_retval));
2124 				}
2125 			}
2126 			spin_unlock_bh(&adapter->adv_rss_lock);
2127 			}
2128 			break;
2129 		case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
2130 			dev_warn(&adapter->pdev->dev, "Changing VLAN Stripping is not allowed when Port VLAN is configured\n");
2131 			/* Vlan stripping could not be enabled by ethtool.
2132 			 * Disable it in netdev->features.
2133 			 */
2134 			iavf_netdev_features_vlan_strip_set(netdev, false);
2135 			break;
2136 		case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
2137 			dev_warn(&adapter->pdev->dev, "Changing VLAN Stripping is not allowed when Port VLAN is configured\n");
2138 			/* Vlan stripping could not be disabled by ethtool.
2139 			 * Enable it in netdev->features.
2140 			 */
2141 			iavf_netdev_features_vlan_strip_set(netdev, true);
2142 			break;
2143 		case VIRTCHNL_OP_ADD_VLAN_V2:
2144 			iavf_vlan_add_reject(adapter);
2145 			dev_warn(&adapter->pdev->dev, "Failed to add VLAN filter, error %s\n",
2146 				 iavf_stat_str(&adapter->hw, v_retval));
2147 			break;
2148 		default:
2149 			dev_err(&adapter->pdev->dev, "PF returned error %d (%s) to our request %d\n",
2150 				v_retval, iavf_stat_str(&adapter->hw, v_retval),
2151 				v_opcode);
2152 		}
2153 	}
2154 	switch (v_opcode) {
2155 	case VIRTCHNL_OP_ADD_ETH_ADDR:
2156 		if (!v_retval)
2157 			iavf_mac_add_ok(adapter);
2158 		if (!ether_addr_equal(netdev->dev_addr, adapter->hw.mac.addr))
2159 			if (!ether_addr_equal(netdev->dev_addr,
2160 					      adapter->hw.mac.addr)) {
2161 				netif_addr_lock_bh(netdev);
2162 				eth_hw_addr_set(netdev, adapter->hw.mac.addr);
2163 				netif_addr_unlock_bh(netdev);
2164 			}
2165 		wake_up(&adapter->vc_waitqueue);
2166 		break;
2167 	case VIRTCHNL_OP_GET_STATS: {
2168 		struct iavf_eth_stats *stats =
2169 			(struct iavf_eth_stats *)msg;
2170 		netdev->stats.rx_packets = stats->rx_unicast +
2171 					   stats->rx_multicast +
2172 					   stats->rx_broadcast;
2173 		netdev->stats.tx_packets = stats->tx_unicast +
2174 					   stats->tx_multicast +
2175 					   stats->tx_broadcast;
2176 		netdev->stats.rx_bytes = stats->rx_bytes;
2177 		netdev->stats.tx_bytes = stats->tx_bytes;
2178 		netdev->stats.tx_errors = stats->tx_errors;
2179 		netdev->stats.rx_dropped = stats->rx_discards;
2180 		netdev->stats.tx_dropped = stats->tx_discards;
2181 		adapter->current_stats = *stats;
2182 		}
2183 		break;
2184 	case VIRTCHNL_OP_GET_VF_RESOURCES: {
2185 		u16 len = IAVF_VIRTCHNL_VF_RESOURCE_SIZE;
2186 
2187 		memcpy(adapter->vf_res, msg, min(msglen, len));
2188 		iavf_validate_num_queues(adapter);
2189 		iavf_vf_parse_hw_config(&adapter->hw, adapter->vf_res);
2190 		if (is_zero_ether_addr(adapter->hw.mac.addr)) {
2191 			/* restore current mac address */
2192 			ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2193 		} else {
2194 			netif_addr_lock_bh(netdev);
2195 			/* refresh current mac address if changed */
2196 			ether_addr_copy(netdev->perm_addr,
2197 					adapter->hw.mac.addr);
2198 			netif_addr_unlock_bh(netdev);
2199 		}
2200 		spin_lock_bh(&adapter->mac_vlan_list_lock);
2201 		iavf_add_filter(adapter, adapter->hw.mac.addr);
2202 
2203 		if (VLAN_ALLOWED(adapter)) {
2204 			if (!list_empty(&adapter->vlan_filter_list)) {
2205 				struct iavf_vlan_filter *vlf;
2206 
2207 				/* re-add all VLAN filters over virtchnl */
2208 				list_for_each_entry(vlf,
2209 						    &adapter->vlan_filter_list,
2210 						    list)
2211 					vlf->state = IAVF_VLAN_ADD;
2212 
2213 				adapter->aq_required |=
2214 					IAVF_FLAG_AQ_ADD_VLAN_FILTER;
2215 			}
2216 		}
2217 
2218 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
2219 
2220 		iavf_parse_vf_resource_msg(adapter);
2221 
2222 		/* negotiated VIRTCHNL_VF_OFFLOAD_VLAN_V2, so wait for the
2223 		 * response to VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS to finish
2224 		 * configuration
2225 		 */
2226 		if (VLAN_V2_ALLOWED(adapter))
2227 			break;
2228 		/* fallthrough and finish config if VIRTCHNL_VF_OFFLOAD_VLAN_V2
2229 		 * wasn't successfully negotiated with the PF
2230 		 */
2231 		}
2232 		fallthrough;
2233 	case VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS: {
2234 		struct iavf_mac_filter *f;
2235 		bool was_mac_changed;
2236 		u64 aq_required = 0;
2237 
2238 		if (v_opcode == VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS)
2239 			memcpy(&adapter->vlan_v2_caps, msg,
2240 			       min_t(u16, msglen,
2241 				     sizeof(adapter->vlan_v2_caps)));
2242 
2243 		iavf_process_config(adapter);
2244 		adapter->flags |= IAVF_FLAG_SETUP_NETDEV_FEATURES;
2245 		iavf_schedule_finish_config(adapter);
2246 
2247 		iavf_set_queue_vlan_tag_loc(adapter);
2248 
2249 		was_mac_changed = !ether_addr_equal(netdev->dev_addr,
2250 						    adapter->hw.mac.addr);
2251 
2252 		spin_lock_bh(&adapter->mac_vlan_list_lock);
2253 
2254 		/* re-add all MAC filters */
2255 		list_for_each_entry(f, &adapter->mac_filter_list, list) {
2256 			if (was_mac_changed &&
2257 			    ether_addr_equal(netdev->dev_addr, f->macaddr))
2258 				ether_addr_copy(f->macaddr,
2259 						adapter->hw.mac.addr);
2260 
2261 			f->is_new_mac = true;
2262 			f->add = true;
2263 			f->add_handled = false;
2264 			f->remove = false;
2265 		}
2266 
2267 		/* re-add all VLAN filters */
2268 		if (VLAN_FILTERING_ALLOWED(adapter)) {
2269 			struct iavf_vlan_filter *vlf;
2270 
2271 			if (!list_empty(&adapter->vlan_filter_list)) {
2272 				list_for_each_entry(vlf,
2273 						    &adapter->vlan_filter_list,
2274 						    list)
2275 					vlf->state = IAVF_VLAN_ADD;
2276 
2277 				aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
2278 			}
2279 		}
2280 
2281 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
2282 
2283 		netif_addr_lock_bh(netdev);
2284 		eth_hw_addr_set(netdev, adapter->hw.mac.addr);
2285 		netif_addr_unlock_bh(netdev);
2286 
2287 		adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER |
2288 			aq_required;
2289 		}
2290 		break;
2291 	case VIRTCHNL_OP_ENABLE_QUEUES:
2292 		/* enable transmits */
2293 		iavf_irq_enable(adapter, true);
2294 		wake_up(&adapter->reset_waitqueue);
2295 		adapter->flags &= ~IAVF_FLAG_QUEUES_DISABLED;
2296 		break;
2297 	case VIRTCHNL_OP_DISABLE_QUEUES:
2298 		iavf_free_all_tx_resources(adapter);
2299 		iavf_free_all_rx_resources(adapter);
2300 		if (adapter->state == __IAVF_DOWN_PENDING) {
2301 			iavf_change_state(adapter, __IAVF_DOWN);
2302 			wake_up(&adapter->down_waitqueue);
2303 		}
2304 		break;
2305 	case VIRTCHNL_OP_VERSION:
2306 	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
2307 		/* Don't display an error if we get these out of sequence.
2308 		 * If the firmware needed to get kicked, we'll get these and
2309 		 * it's no problem.
2310 		 */
2311 		if (v_opcode != adapter->current_op)
2312 			return;
2313 		break;
2314 	case VIRTCHNL_OP_RDMA:
2315 		/* Gobble zero-length replies from the PF. They indicate that
2316 		 * a previous message was received OK, and the client doesn't
2317 		 * care about that.
2318 		 */
2319 		if (msglen && CLIENT_ENABLED(adapter))
2320 			iavf_notify_client_message(&adapter->vsi, msg, msglen);
2321 		break;
2322 
2323 	case VIRTCHNL_OP_CONFIG_RDMA_IRQ_MAP:
2324 		adapter->client_pending &=
2325 				~(BIT(VIRTCHNL_OP_CONFIG_RDMA_IRQ_MAP));
2326 		break;
2327 	case VIRTCHNL_OP_GET_RSS_HENA_CAPS: {
2328 		struct virtchnl_rss_hena *vrh = (struct virtchnl_rss_hena *)msg;
2329 
2330 		if (msglen == sizeof(*vrh))
2331 			adapter->hena = vrh->hena;
2332 		else
2333 			dev_warn(&adapter->pdev->dev,
2334 				 "Invalid message %d from PF\n", v_opcode);
2335 		}
2336 		break;
2337 	case VIRTCHNL_OP_REQUEST_QUEUES: {
2338 		struct virtchnl_vf_res_request *vfres =
2339 			(struct virtchnl_vf_res_request *)msg;
2340 
2341 		if (vfres->num_queue_pairs != adapter->num_req_queues) {
2342 			dev_info(&adapter->pdev->dev,
2343 				 "Requested %d queues, PF can support %d\n",
2344 				 adapter->num_req_queues,
2345 				 vfres->num_queue_pairs);
2346 			adapter->num_req_queues = 0;
2347 			adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
2348 		}
2349 		}
2350 		break;
2351 	case VIRTCHNL_OP_ADD_CLOUD_FILTER: {
2352 		struct iavf_cloud_filter *cf;
2353 
2354 		list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
2355 			if (cf->state == __IAVF_CF_ADD_PENDING)
2356 				cf->state = __IAVF_CF_ACTIVE;
2357 		}
2358 		}
2359 		break;
2360 	case VIRTCHNL_OP_DEL_CLOUD_FILTER: {
2361 		struct iavf_cloud_filter *cf, *cftmp;
2362 
2363 		list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
2364 					 list) {
2365 			if (cf->state == __IAVF_CF_DEL_PENDING) {
2366 				cf->state = __IAVF_CF_INVALID;
2367 				list_del(&cf->list);
2368 				kfree(cf);
2369 				adapter->num_cloud_filters--;
2370 			}
2371 		}
2372 		}
2373 		break;
2374 	case VIRTCHNL_OP_ADD_FDIR_FILTER: {
2375 		struct virtchnl_fdir_add *add_fltr = (struct virtchnl_fdir_add *)msg;
2376 		struct iavf_fdir_fltr *fdir, *fdir_tmp;
2377 
2378 		spin_lock_bh(&adapter->fdir_fltr_lock);
2379 		list_for_each_entry_safe(fdir, fdir_tmp,
2380 					 &adapter->fdir_list_head,
2381 					 list) {
2382 			if (fdir->state == IAVF_FDIR_FLTR_ADD_PENDING) {
2383 				if (add_fltr->status == VIRTCHNL_FDIR_SUCCESS) {
2384 					dev_info(&adapter->pdev->dev, "Flow Director filter with location %u is added\n",
2385 						 fdir->loc);
2386 					fdir->state = IAVF_FDIR_FLTR_ACTIVE;
2387 					fdir->flow_id = add_fltr->flow_id;
2388 				} else {
2389 					dev_info(&adapter->pdev->dev, "Failed to add Flow Director filter with status: %d\n",
2390 						 add_fltr->status);
2391 					iavf_print_fdir_fltr(adapter, fdir);
2392 					list_del(&fdir->list);
2393 					kfree(fdir);
2394 					adapter->fdir_active_fltr--;
2395 				}
2396 			}
2397 		}
2398 		spin_unlock_bh(&adapter->fdir_fltr_lock);
2399 		}
2400 		break;
2401 	case VIRTCHNL_OP_DEL_FDIR_FILTER: {
2402 		struct virtchnl_fdir_del *del_fltr = (struct virtchnl_fdir_del *)msg;
2403 		struct iavf_fdir_fltr *fdir, *fdir_tmp;
2404 
2405 		spin_lock_bh(&adapter->fdir_fltr_lock);
2406 		list_for_each_entry_safe(fdir, fdir_tmp, &adapter->fdir_list_head,
2407 					 list) {
2408 			if (fdir->state == IAVF_FDIR_FLTR_DEL_PENDING) {
2409 				if (del_fltr->status == VIRTCHNL_FDIR_SUCCESS) {
2410 					dev_info(&adapter->pdev->dev, "Flow Director filter with location %u is deleted\n",
2411 						 fdir->loc);
2412 					list_del(&fdir->list);
2413 					kfree(fdir);
2414 					adapter->fdir_active_fltr--;
2415 				} else {
2416 					fdir->state = IAVF_FDIR_FLTR_ACTIVE;
2417 					dev_info(&adapter->pdev->dev, "Failed to delete Flow Director filter with status: %d\n",
2418 						 del_fltr->status);
2419 					iavf_print_fdir_fltr(adapter, fdir);
2420 				}
2421 			}
2422 		}
2423 		spin_unlock_bh(&adapter->fdir_fltr_lock);
2424 		}
2425 		break;
2426 	case VIRTCHNL_OP_ADD_RSS_CFG: {
2427 		struct iavf_adv_rss *rss;
2428 
2429 		spin_lock_bh(&adapter->adv_rss_lock);
2430 		list_for_each_entry(rss, &adapter->adv_rss_list_head, list) {
2431 			if (rss->state == IAVF_ADV_RSS_ADD_PENDING) {
2432 				iavf_print_adv_rss_cfg(adapter, rss,
2433 						       "Input set change for",
2434 						       "successful");
2435 				rss->state = IAVF_ADV_RSS_ACTIVE;
2436 			}
2437 		}
2438 		spin_unlock_bh(&adapter->adv_rss_lock);
2439 		}
2440 		break;
2441 	case VIRTCHNL_OP_DEL_RSS_CFG: {
2442 		struct iavf_adv_rss *rss, *rss_tmp;
2443 
2444 		spin_lock_bh(&adapter->adv_rss_lock);
2445 		list_for_each_entry_safe(rss, rss_tmp,
2446 					 &adapter->adv_rss_list_head, list) {
2447 			if (rss->state == IAVF_ADV_RSS_DEL_PENDING) {
2448 				list_del(&rss->list);
2449 				kfree(rss);
2450 			}
2451 		}
2452 		spin_unlock_bh(&adapter->adv_rss_lock);
2453 		}
2454 		break;
2455 	case VIRTCHNL_OP_ADD_VLAN_V2: {
2456 		struct iavf_vlan_filter *f;
2457 
2458 		spin_lock_bh(&adapter->mac_vlan_list_lock);
2459 		list_for_each_entry(f, &adapter->vlan_filter_list, list) {
2460 			if (f->state == IAVF_VLAN_IS_NEW)
2461 				f->state = IAVF_VLAN_ACTIVE;
2462 		}
2463 		spin_unlock_bh(&adapter->mac_vlan_list_lock);
2464 		}
2465 		break;
2466 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
2467 		/* PF enabled vlan strip on this VF.
2468 		 * Update netdev->features if needed to be in sync with ethtool.
2469 		 */
2470 		if (!v_retval)
2471 			iavf_netdev_features_vlan_strip_set(netdev, true);
2472 		break;
2473 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
2474 		/* PF disabled vlan strip on this VF.
2475 		 * Update netdev->features if needed to be in sync with ethtool.
2476 		 */
2477 		if (!v_retval)
2478 			iavf_netdev_features_vlan_strip_set(netdev, false);
2479 		break;
2480 	default:
2481 		if (adapter->current_op && (v_opcode != adapter->current_op))
2482 			dev_warn(&adapter->pdev->dev, "Expected response %d from PF, received %d\n",
2483 				 adapter->current_op, v_opcode);
2484 		break;
2485 	} /* switch v_opcode */
2486 	adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2487 }
2488