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