xref: /dpdk/drivers/net/liquidio/lio_ethdev.c (revision 2c7845a7)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Cavium, Inc
3  */
4 
5 #include <rte_string_fns.h>
6 #include <rte_ethdev_driver.h>
7 #include <rte_ethdev_pci.h>
8 #include <rte_cycles.h>
9 #include <rte_malloc.h>
10 #include <rte_alarm.h>
11 #include <rte_ether.h>
12 
13 #include "lio_logs.h"
14 #include "lio_23xx_vf.h"
15 #include "lio_ethdev.h"
16 #include "lio_rxtx.h"
17 
18 int lio_logtype_init;
19 int lio_logtype_driver;
20 
21 /* Default RSS key in use */
22 static uint8_t lio_rss_key[40] = {
23 	0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
24 	0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
25 	0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
26 	0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
27 	0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA,
28 };
29 
30 static const struct rte_eth_desc_lim lio_rx_desc_lim = {
31 	.nb_max		= CN23XX_MAX_OQ_DESCRIPTORS,
32 	.nb_min		= CN23XX_MIN_OQ_DESCRIPTORS,
33 	.nb_align	= 1,
34 };
35 
36 static const struct rte_eth_desc_lim lio_tx_desc_lim = {
37 	.nb_max		= CN23XX_MAX_IQ_DESCRIPTORS,
38 	.nb_min		= CN23XX_MIN_IQ_DESCRIPTORS,
39 	.nb_align	= 1,
40 };
41 
42 /* Wait for control command to reach nic. */
43 static uint16_t
44 lio_wait_for_ctrl_cmd(struct lio_device *lio_dev,
45 		      struct lio_dev_ctrl_cmd *ctrl_cmd)
46 {
47 	uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
48 
49 	while ((ctrl_cmd->cond == 0) && --timeout) {
50 		lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
51 		rte_delay_ms(1);
52 	}
53 
54 	return !timeout;
55 }
56 
57 /**
58  * \brief Send Rx control command
59  * @param eth_dev Pointer to the structure rte_eth_dev
60  * @param start_stop whether to start or stop
61  */
62 static int
63 lio_send_rx_ctrl_cmd(struct rte_eth_dev *eth_dev, int start_stop)
64 {
65 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
66 	struct lio_dev_ctrl_cmd ctrl_cmd;
67 	struct lio_ctrl_pkt ctrl_pkt;
68 
69 	/* flush added to prevent cmd failure
70 	 * incase the queue is full
71 	 */
72 	lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
73 
74 	memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
75 	memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
76 
77 	ctrl_cmd.eth_dev = eth_dev;
78 	ctrl_cmd.cond = 0;
79 
80 	ctrl_pkt.ncmd.s.cmd = LIO_CMD_RX_CTL;
81 	ctrl_pkt.ncmd.s.param1 = start_stop;
82 	ctrl_pkt.ctrl_cmd = &ctrl_cmd;
83 
84 	if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
85 		lio_dev_err(lio_dev, "Failed to send RX Control message\n");
86 		return -1;
87 	}
88 
89 	if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
90 		lio_dev_err(lio_dev, "RX Control command timed out\n");
91 		return -1;
92 	}
93 
94 	return 0;
95 }
96 
97 /* store statistics names and its offset in stats structure */
98 struct rte_lio_xstats_name_off {
99 	char name[RTE_ETH_XSTATS_NAME_SIZE];
100 	unsigned int offset;
101 };
102 
103 static const struct rte_lio_xstats_name_off rte_lio_stats_strings[] = {
104 	{"rx_pkts", offsetof(struct octeon_rx_stats, total_rcvd)},
105 	{"rx_bytes", offsetof(struct octeon_rx_stats, bytes_rcvd)},
106 	{"rx_broadcast_pkts", offsetof(struct octeon_rx_stats, total_bcst)},
107 	{"rx_multicast_pkts", offsetof(struct octeon_rx_stats, total_mcst)},
108 	{"rx_flow_ctrl_pkts", offsetof(struct octeon_rx_stats, ctl_rcvd)},
109 	{"rx_fifo_err", offsetof(struct octeon_rx_stats, fifo_err)},
110 	{"rx_dmac_drop", offsetof(struct octeon_rx_stats, dmac_drop)},
111 	{"rx_fcs_err", offsetof(struct octeon_rx_stats, fcs_err)},
112 	{"rx_jabber_err", offsetof(struct octeon_rx_stats, jabber_err)},
113 	{"rx_l2_err", offsetof(struct octeon_rx_stats, l2_err)},
114 	{"rx_vxlan_pkts", offsetof(struct octeon_rx_stats, fw_rx_vxlan)},
115 	{"rx_vxlan_err", offsetof(struct octeon_rx_stats, fw_rx_vxlan_err)},
116 	{"rx_lro_pkts", offsetof(struct octeon_rx_stats, fw_lro_pkts)},
117 	{"tx_pkts", (offsetof(struct octeon_tx_stats, total_pkts_sent)) +
118 						sizeof(struct octeon_rx_stats)},
119 	{"tx_bytes", (offsetof(struct octeon_tx_stats, total_bytes_sent)) +
120 						sizeof(struct octeon_rx_stats)},
121 	{"tx_broadcast_pkts",
122 		(offsetof(struct octeon_tx_stats, bcast_pkts_sent)) +
123 			sizeof(struct octeon_rx_stats)},
124 	{"tx_multicast_pkts",
125 		(offsetof(struct octeon_tx_stats, mcast_pkts_sent)) +
126 			sizeof(struct octeon_rx_stats)},
127 	{"tx_flow_ctrl_pkts", (offsetof(struct octeon_tx_stats, ctl_sent)) +
128 						sizeof(struct octeon_rx_stats)},
129 	{"tx_fifo_err", (offsetof(struct octeon_tx_stats, fifo_err)) +
130 						sizeof(struct octeon_rx_stats)},
131 	{"tx_total_collisions", (offsetof(struct octeon_tx_stats,
132 					  total_collisions)) +
133 						sizeof(struct octeon_rx_stats)},
134 	{"tx_tso", (offsetof(struct octeon_tx_stats, fw_tso)) +
135 						sizeof(struct octeon_rx_stats)},
136 	{"tx_vxlan_pkts", (offsetof(struct octeon_tx_stats, fw_tx_vxlan)) +
137 						sizeof(struct octeon_rx_stats)},
138 };
139 
140 #define LIO_NB_XSTATS	RTE_DIM(rte_lio_stats_strings)
141 
142 /* Get hw stats of the port */
143 static int
144 lio_dev_xstats_get(struct rte_eth_dev *eth_dev, struct rte_eth_xstat *xstats,
145 		   unsigned int n)
146 {
147 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
148 	uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
149 	struct octeon_link_stats *hw_stats;
150 	struct lio_link_stats_resp *resp;
151 	struct lio_soft_command *sc;
152 	uint32_t resp_size;
153 	unsigned int i;
154 	int retval;
155 
156 	if (!lio_dev->intf_open) {
157 		lio_dev_err(lio_dev, "Port %d down\n",
158 			    lio_dev->port_id);
159 		return -EINVAL;
160 	}
161 
162 	if (n < LIO_NB_XSTATS)
163 		return LIO_NB_XSTATS;
164 
165 	resp_size = sizeof(struct lio_link_stats_resp);
166 	sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
167 	if (sc == NULL)
168 		return -ENOMEM;
169 
170 	resp = (struct lio_link_stats_resp *)sc->virtrptr;
171 	lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
172 				 LIO_OPCODE_PORT_STATS, 0, 0, 0);
173 
174 	/* Setting wait time in seconds */
175 	sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
176 
177 	retval = lio_send_soft_command(lio_dev, sc);
178 	if (retval == LIO_IQ_SEND_FAILED) {
179 		lio_dev_err(lio_dev, "failed to get port stats from firmware. status: %x\n",
180 			    retval);
181 		goto get_stats_fail;
182 	}
183 
184 	while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
185 		lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
186 		lio_process_ordered_list(lio_dev);
187 		rte_delay_ms(1);
188 	}
189 
190 	retval = resp->status;
191 	if (retval) {
192 		lio_dev_err(lio_dev, "failed to get port stats from firmware\n");
193 		goto get_stats_fail;
194 	}
195 
196 	lio_swap_8B_data((uint64_t *)(&resp->link_stats),
197 			 sizeof(struct octeon_link_stats) >> 3);
198 
199 	hw_stats = &resp->link_stats;
200 
201 	for (i = 0; i < LIO_NB_XSTATS; i++) {
202 		xstats[i].id = i;
203 		xstats[i].value =
204 		    *(uint64_t *)(((char *)hw_stats) +
205 					rte_lio_stats_strings[i].offset);
206 	}
207 
208 	lio_free_soft_command(sc);
209 
210 	return LIO_NB_XSTATS;
211 
212 get_stats_fail:
213 	lio_free_soft_command(sc);
214 
215 	return -1;
216 }
217 
218 static int
219 lio_dev_xstats_get_names(struct rte_eth_dev *eth_dev,
220 			 struct rte_eth_xstat_name *xstats_names,
221 			 unsigned limit __rte_unused)
222 {
223 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
224 	unsigned int i;
225 
226 	if (!lio_dev->intf_open) {
227 		lio_dev_err(lio_dev, "Port %d down\n",
228 			    lio_dev->port_id);
229 		return -EINVAL;
230 	}
231 
232 	if (xstats_names == NULL)
233 		return LIO_NB_XSTATS;
234 
235 	/* Note: limit checked in rte_eth_xstats_names() */
236 
237 	for (i = 0; i < LIO_NB_XSTATS; i++) {
238 		snprintf(xstats_names[i].name, sizeof(xstats_names[i].name),
239 			 "%s", rte_lio_stats_strings[i].name);
240 	}
241 
242 	return LIO_NB_XSTATS;
243 }
244 
245 /* Reset hw stats for the port */
246 static int
247 lio_dev_xstats_reset(struct rte_eth_dev *eth_dev)
248 {
249 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
250 	struct lio_dev_ctrl_cmd ctrl_cmd;
251 	struct lio_ctrl_pkt ctrl_pkt;
252 	int ret;
253 
254 	if (!lio_dev->intf_open) {
255 		lio_dev_err(lio_dev, "Port %d down\n",
256 			    lio_dev->port_id);
257 		return -EINVAL;
258 	}
259 
260 	/* flush added to prevent cmd failure
261 	 * incase the queue is full
262 	 */
263 	lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
264 
265 	memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
266 	memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
267 
268 	ctrl_cmd.eth_dev = eth_dev;
269 	ctrl_cmd.cond = 0;
270 
271 	ctrl_pkt.ncmd.s.cmd = LIO_CMD_CLEAR_STATS;
272 	ctrl_pkt.ctrl_cmd = &ctrl_cmd;
273 
274 	ret = lio_send_ctrl_pkt(lio_dev, &ctrl_pkt);
275 	if (ret != 0) {
276 		lio_dev_err(lio_dev, "Failed to send clear stats command\n");
277 		return ret;
278 	}
279 
280 	ret = lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd);
281 	if (ret != 0) {
282 		lio_dev_err(lio_dev, "Clear stats command timed out\n");
283 		return ret;
284 	}
285 
286 	/* clear stored per queue stats */
287 	RTE_FUNC_PTR_OR_ERR_RET(*eth_dev->dev_ops->stats_reset, 0);
288 	return (*eth_dev->dev_ops->stats_reset)(eth_dev);
289 }
290 
291 /* Retrieve the device statistics (# packets in/out, # bytes in/out, etc */
292 static int
293 lio_dev_stats_get(struct rte_eth_dev *eth_dev,
294 		  struct rte_eth_stats *stats)
295 {
296 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
297 	struct lio_droq_stats *oq_stats;
298 	struct lio_iq_stats *iq_stats;
299 	struct lio_instr_queue *txq;
300 	struct lio_droq *droq;
301 	int i, iq_no, oq_no;
302 	uint64_t bytes = 0;
303 	uint64_t pkts = 0;
304 	uint64_t drop = 0;
305 
306 	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
307 		iq_no = lio_dev->linfo.txpciq[i].s.q_no;
308 		txq = lio_dev->instr_queue[iq_no];
309 		if (txq != NULL) {
310 			iq_stats = &txq->stats;
311 			pkts += iq_stats->tx_done;
312 			drop += iq_stats->tx_dropped;
313 			bytes += iq_stats->tx_tot_bytes;
314 		}
315 	}
316 
317 	stats->opackets = pkts;
318 	stats->obytes = bytes;
319 	stats->oerrors = drop;
320 
321 	pkts = 0;
322 	drop = 0;
323 	bytes = 0;
324 
325 	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
326 		oq_no = lio_dev->linfo.rxpciq[i].s.q_no;
327 		droq = lio_dev->droq[oq_no];
328 		if (droq != NULL) {
329 			oq_stats = &droq->stats;
330 			pkts += oq_stats->rx_pkts_received;
331 			drop += (oq_stats->rx_dropped +
332 					oq_stats->dropped_toomany +
333 					oq_stats->dropped_nomem);
334 			bytes += oq_stats->rx_bytes_received;
335 		}
336 	}
337 	stats->ibytes = bytes;
338 	stats->ipackets = pkts;
339 	stats->ierrors = drop;
340 
341 	return 0;
342 }
343 
344 static int
345 lio_dev_stats_reset(struct rte_eth_dev *eth_dev)
346 {
347 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
348 	struct lio_droq_stats *oq_stats;
349 	struct lio_iq_stats *iq_stats;
350 	struct lio_instr_queue *txq;
351 	struct lio_droq *droq;
352 	int i, iq_no, oq_no;
353 
354 	for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
355 		iq_no = lio_dev->linfo.txpciq[i].s.q_no;
356 		txq = lio_dev->instr_queue[iq_no];
357 		if (txq != NULL) {
358 			iq_stats = &txq->stats;
359 			memset(iq_stats, 0, sizeof(struct lio_iq_stats));
360 		}
361 	}
362 
363 	for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
364 		oq_no = lio_dev->linfo.rxpciq[i].s.q_no;
365 		droq = lio_dev->droq[oq_no];
366 		if (droq != NULL) {
367 			oq_stats = &droq->stats;
368 			memset(oq_stats, 0, sizeof(struct lio_droq_stats));
369 		}
370 	}
371 
372 	return 0;
373 }
374 
375 static int
376 lio_dev_info_get(struct rte_eth_dev *eth_dev,
377 		 struct rte_eth_dev_info *devinfo)
378 {
379 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
380 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
381 
382 	switch (pci_dev->id.subsystem_device_id) {
383 	/* CN23xx 10G cards */
384 	case PCI_SUBSYS_DEV_ID_CN2350_210:
385 	case PCI_SUBSYS_DEV_ID_CN2360_210:
386 	case PCI_SUBSYS_DEV_ID_CN2350_210SVPN3:
387 	case PCI_SUBSYS_DEV_ID_CN2360_210SVPN3:
388 	case PCI_SUBSYS_DEV_ID_CN2350_210SVPT:
389 	case PCI_SUBSYS_DEV_ID_CN2360_210SVPT:
390 		devinfo->speed_capa = ETH_LINK_SPEED_10G;
391 		break;
392 	/* CN23xx 25G cards */
393 	case PCI_SUBSYS_DEV_ID_CN2350_225:
394 	case PCI_SUBSYS_DEV_ID_CN2360_225:
395 		devinfo->speed_capa = ETH_LINK_SPEED_25G;
396 		break;
397 	default:
398 		devinfo->speed_capa = ETH_LINK_SPEED_10G;
399 		lio_dev_err(lio_dev,
400 			    "Unknown CN23XX subsystem device id. Setting 10G as default link speed.\n");
401 		return -EINVAL;
402 	}
403 
404 	devinfo->max_rx_queues = lio_dev->max_rx_queues;
405 	devinfo->max_tx_queues = lio_dev->max_tx_queues;
406 
407 	devinfo->min_rx_bufsize = LIO_MIN_RX_BUF_SIZE;
408 	devinfo->max_rx_pktlen = LIO_MAX_RX_PKTLEN;
409 
410 	devinfo->max_mac_addrs = 1;
411 
412 	devinfo->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM		|
413 				    DEV_RX_OFFLOAD_UDP_CKSUM		|
414 				    DEV_RX_OFFLOAD_TCP_CKSUM		|
415 				    DEV_RX_OFFLOAD_VLAN_STRIP);
416 	devinfo->tx_offload_capa = (DEV_TX_OFFLOAD_IPV4_CKSUM		|
417 				    DEV_TX_OFFLOAD_UDP_CKSUM		|
418 				    DEV_TX_OFFLOAD_TCP_CKSUM		|
419 				    DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM);
420 
421 	devinfo->rx_desc_lim = lio_rx_desc_lim;
422 	devinfo->tx_desc_lim = lio_tx_desc_lim;
423 
424 	devinfo->reta_size = LIO_RSS_MAX_TABLE_SZ;
425 	devinfo->hash_key_size = LIO_RSS_MAX_KEY_SZ;
426 	devinfo->flow_type_rss_offloads = (ETH_RSS_IPV4			|
427 					   ETH_RSS_NONFRAG_IPV4_TCP	|
428 					   ETH_RSS_IPV6			|
429 					   ETH_RSS_NONFRAG_IPV6_TCP	|
430 					   ETH_RSS_IPV6_EX		|
431 					   ETH_RSS_IPV6_TCP_EX);
432 	return 0;
433 }
434 
435 static int
436 lio_dev_mtu_set(struct rte_eth_dev *eth_dev, uint16_t mtu)
437 {
438 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
439 	uint16_t pf_mtu = lio_dev->linfo.link.s.mtu;
440 	uint32_t frame_len = mtu + RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
441 	struct lio_dev_ctrl_cmd ctrl_cmd;
442 	struct lio_ctrl_pkt ctrl_pkt;
443 
444 	PMD_INIT_FUNC_TRACE();
445 
446 	if (!lio_dev->intf_open) {
447 		lio_dev_err(lio_dev, "Port %d down, can't set MTU\n",
448 			    lio_dev->port_id);
449 		return -EINVAL;
450 	}
451 
452 	/* check if VF MTU is within allowed range.
453 	 * New value should not exceed PF MTU.
454 	 */
455 	if (mtu < RTE_ETHER_MIN_MTU || mtu > pf_mtu) {
456 		lio_dev_err(lio_dev, "VF MTU should be >= %d and <= %d\n",
457 			    RTE_ETHER_MIN_MTU, pf_mtu);
458 		return -EINVAL;
459 	}
460 
461 	/* flush added to prevent cmd failure
462 	 * incase the queue is full
463 	 */
464 	lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
465 
466 	memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
467 	memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
468 
469 	ctrl_cmd.eth_dev = eth_dev;
470 	ctrl_cmd.cond = 0;
471 
472 	ctrl_pkt.ncmd.s.cmd = LIO_CMD_CHANGE_MTU;
473 	ctrl_pkt.ncmd.s.param1 = mtu;
474 	ctrl_pkt.ctrl_cmd = &ctrl_cmd;
475 
476 	if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
477 		lio_dev_err(lio_dev, "Failed to send command to change MTU\n");
478 		return -1;
479 	}
480 
481 	if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
482 		lio_dev_err(lio_dev, "Command to change MTU timed out\n");
483 		return -1;
484 	}
485 
486 	if (frame_len > RTE_ETHER_MAX_LEN)
487 		eth_dev->data->dev_conf.rxmode.offloads |=
488 			DEV_RX_OFFLOAD_JUMBO_FRAME;
489 	else
490 		eth_dev->data->dev_conf.rxmode.offloads &=
491 			~DEV_RX_OFFLOAD_JUMBO_FRAME;
492 
493 	eth_dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_len;
494 	eth_dev->data->mtu = mtu;
495 
496 	return 0;
497 }
498 
499 static int
500 lio_dev_rss_reta_update(struct rte_eth_dev *eth_dev,
501 			struct rte_eth_rss_reta_entry64 *reta_conf,
502 			uint16_t reta_size)
503 {
504 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
505 	struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
506 	struct lio_rss_set *rss_param;
507 	struct lio_dev_ctrl_cmd ctrl_cmd;
508 	struct lio_ctrl_pkt ctrl_pkt;
509 	int i, j, index;
510 
511 	if (!lio_dev->intf_open) {
512 		lio_dev_err(lio_dev, "Port %d down, can't update reta\n",
513 			    lio_dev->port_id);
514 		return -EINVAL;
515 	}
516 
517 	if (reta_size != LIO_RSS_MAX_TABLE_SZ) {
518 		lio_dev_err(lio_dev,
519 			    "The size of hash lookup table configured (%d) doesn't match the number hardware can supported (%d)\n",
520 			    reta_size, LIO_RSS_MAX_TABLE_SZ);
521 		return -EINVAL;
522 	}
523 
524 	/* flush added to prevent cmd failure
525 	 * incase the queue is full
526 	 */
527 	lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
528 
529 	memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
530 	memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
531 
532 	rss_param = (struct lio_rss_set *)&ctrl_pkt.udd[0];
533 
534 	ctrl_cmd.eth_dev = eth_dev;
535 	ctrl_cmd.cond = 0;
536 
537 	ctrl_pkt.ncmd.s.cmd = LIO_CMD_SET_RSS;
538 	ctrl_pkt.ncmd.s.more = sizeof(struct lio_rss_set) >> 3;
539 	ctrl_pkt.ctrl_cmd = &ctrl_cmd;
540 
541 	rss_param->param.flags = 0xF;
542 	rss_param->param.flags &= ~LIO_RSS_PARAM_ITABLE_UNCHANGED;
543 	rss_param->param.itablesize = LIO_RSS_MAX_TABLE_SZ;
544 
545 	for (i = 0; i < (reta_size / RTE_RETA_GROUP_SIZE); i++) {
546 		for (j = 0; j < RTE_RETA_GROUP_SIZE; j++) {
547 			if ((reta_conf[i].mask) & ((uint64_t)1 << j)) {
548 				index = (i * RTE_RETA_GROUP_SIZE) + j;
549 				rss_state->itable[index] = reta_conf[i].reta[j];
550 			}
551 		}
552 	}
553 
554 	rss_state->itable_size = LIO_RSS_MAX_TABLE_SZ;
555 	memcpy(rss_param->itable, rss_state->itable, rss_state->itable_size);
556 
557 	lio_swap_8B_data((uint64_t *)rss_param, LIO_RSS_PARAM_SIZE >> 3);
558 
559 	if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
560 		lio_dev_err(lio_dev, "Failed to set rss hash\n");
561 		return -1;
562 	}
563 
564 	if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
565 		lio_dev_err(lio_dev, "Set rss hash timed out\n");
566 		return -1;
567 	}
568 
569 	return 0;
570 }
571 
572 static int
573 lio_dev_rss_reta_query(struct rte_eth_dev *eth_dev,
574 		       struct rte_eth_rss_reta_entry64 *reta_conf,
575 		       uint16_t reta_size)
576 {
577 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
578 	struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
579 	int i, num;
580 
581 	if (reta_size != LIO_RSS_MAX_TABLE_SZ) {
582 		lio_dev_err(lio_dev,
583 			    "The size of hash lookup table configured (%d) doesn't match the number hardware can supported (%d)\n",
584 			    reta_size, LIO_RSS_MAX_TABLE_SZ);
585 		return -EINVAL;
586 	}
587 
588 	num = reta_size / RTE_RETA_GROUP_SIZE;
589 
590 	for (i = 0; i < num; i++) {
591 		memcpy(reta_conf->reta,
592 		       &rss_state->itable[i * RTE_RETA_GROUP_SIZE],
593 		       RTE_RETA_GROUP_SIZE);
594 		reta_conf++;
595 	}
596 
597 	return 0;
598 }
599 
600 static int
601 lio_dev_rss_hash_conf_get(struct rte_eth_dev *eth_dev,
602 			  struct rte_eth_rss_conf *rss_conf)
603 {
604 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
605 	struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
606 	uint8_t *hash_key = NULL;
607 	uint64_t rss_hf = 0;
608 
609 	if (rss_state->hash_disable) {
610 		lio_dev_info(lio_dev, "RSS disabled in nic\n");
611 		rss_conf->rss_hf = 0;
612 		return 0;
613 	}
614 
615 	/* Get key value */
616 	hash_key = rss_conf->rss_key;
617 	if (hash_key != NULL)
618 		memcpy(hash_key, rss_state->hash_key, rss_state->hash_key_size);
619 
620 	if (rss_state->ip)
621 		rss_hf |= ETH_RSS_IPV4;
622 	if (rss_state->tcp_hash)
623 		rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
624 	if (rss_state->ipv6)
625 		rss_hf |= ETH_RSS_IPV6;
626 	if (rss_state->ipv6_tcp_hash)
627 		rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
628 	if (rss_state->ipv6_ex)
629 		rss_hf |= ETH_RSS_IPV6_EX;
630 	if (rss_state->ipv6_tcp_ex_hash)
631 		rss_hf |= ETH_RSS_IPV6_TCP_EX;
632 
633 	rss_conf->rss_hf = rss_hf;
634 
635 	return 0;
636 }
637 
638 static int
639 lio_dev_rss_hash_update(struct rte_eth_dev *eth_dev,
640 			struct rte_eth_rss_conf *rss_conf)
641 {
642 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
643 	struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
644 	struct lio_rss_set *rss_param;
645 	struct lio_dev_ctrl_cmd ctrl_cmd;
646 	struct lio_ctrl_pkt ctrl_pkt;
647 
648 	if (!lio_dev->intf_open) {
649 		lio_dev_err(lio_dev, "Port %d down, can't update hash\n",
650 			    lio_dev->port_id);
651 		return -EINVAL;
652 	}
653 
654 	/* flush added to prevent cmd failure
655 	 * incase the queue is full
656 	 */
657 	lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
658 
659 	memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
660 	memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
661 
662 	rss_param = (struct lio_rss_set *)&ctrl_pkt.udd[0];
663 
664 	ctrl_cmd.eth_dev = eth_dev;
665 	ctrl_cmd.cond = 0;
666 
667 	ctrl_pkt.ncmd.s.cmd = LIO_CMD_SET_RSS;
668 	ctrl_pkt.ncmd.s.more = sizeof(struct lio_rss_set) >> 3;
669 	ctrl_pkt.ctrl_cmd = &ctrl_cmd;
670 
671 	rss_param->param.flags = 0xF;
672 
673 	if (rss_conf->rss_key) {
674 		rss_param->param.flags &= ~LIO_RSS_PARAM_HASH_KEY_UNCHANGED;
675 		rss_state->hash_key_size = LIO_RSS_MAX_KEY_SZ;
676 		rss_param->param.hashkeysize = LIO_RSS_MAX_KEY_SZ;
677 		memcpy(rss_state->hash_key, rss_conf->rss_key,
678 		       rss_state->hash_key_size);
679 		memcpy(rss_param->key, rss_state->hash_key,
680 		       rss_state->hash_key_size);
681 	}
682 
683 	if ((rss_conf->rss_hf & LIO_RSS_OFFLOAD_ALL) == 0) {
684 		/* Can't disable rss through hash flags,
685 		 * if it is enabled by default during init
686 		 */
687 		if (!rss_state->hash_disable)
688 			return -EINVAL;
689 
690 		/* This is for --disable-rss during testpmd launch */
691 		rss_param->param.flags |= LIO_RSS_PARAM_DISABLE_RSS;
692 	} else {
693 		uint32_t hashinfo = 0;
694 
695 		/* Can't enable rss if disabled by default during init */
696 		if (rss_state->hash_disable)
697 			return -EINVAL;
698 
699 		if (rss_conf->rss_hf & ETH_RSS_IPV4) {
700 			hashinfo |= LIO_RSS_HASH_IPV4;
701 			rss_state->ip = 1;
702 		} else {
703 			rss_state->ip = 0;
704 		}
705 
706 		if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP) {
707 			hashinfo |= LIO_RSS_HASH_TCP_IPV4;
708 			rss_state->tcp_hash = 1;
709 		} else {
710 			rss_state->tcp_hash = 0;
711 		}
712 
713 		if (rss_conf->rss_hf & ETH_RSS_IPV6) {
714 			hashinfo |= LIO_RSS_HASH_IPV6;
715 			rss_state->ipv6 = 1;
716 		} else {
717 			rss_state->ipv6 = 0;
718 		}
719 
720 		if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP) {
721 			hashinfo |= LIO_RSS_HASH_TCP_IPV6;
722 			rss_state->ipv6_tcp_hash = 1;
723 		} else {
724 			rss_state->ipv6_tcp_hash = 0;
725 		}
726 
727 		if (rss_conf->rss_hf & ETH_RSS_IPV6_EX) {
728 			hashinfo |= LIO_RSS_HASH_IPV6_EX;
729 			rss_state->ipv6_ex = 1;
730 		} else {
731 			rss_state->ipv6_ex = 0;
732 		}
733 
734 		if (rss_conf->rss_hf & ETH_RSS_IPV6_TCP_EX) {
735 			hashinfo |= LIO_RSS_HASH_TCP_IPV6_EX;
736 			rss_state->ipv6_tcp_ex_hash = 1;
737 		} else {
738 			rss_state->ipv6_tcp_ex_hash = 0;
739 		}
740 
741 		rss_param->param.flags &= ~LIO_RSS_PARAM_HASH_INFO_UNCHANGED;
742 		rss_param->param.hashinfo = hashinfo;
743 	}
744 
745 	lio_swap_8B_data((uint64_t *)rss_param, LIO_RSS_PARAM_SIZE >> 3);
746 
747 	if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
748 		lio_dev_err(lio_dev, "Failed to set rss hash\n");
749 		return -1;
750 	}
751 
752 	if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
753 		lio_dev_err(lio_dev, "Set rss hash timed out\n");
754 		return -1;
755 	}
756 
757 	return 0;
758 }
759 
760 /**
761  * Add vxlan dest udp port for an interface.
762  *
763  * @param eth_dev
764  *  Pointer to the structure rte_eth_dev
765  * @param udp_tnl
766  *  udp tunnel conf
767  *
768  * @return
769  *  On success return 0
770  *  On failure return -1
771  */
772 static int
773 lio_dev_udp_tunnel_add(struct rte_eth_dev *eth_dev,
774 		       struct rte_eth_udp_tunnel *udp_tnl)
775 {
776 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
777 	struct lio_dev_ctrl_cmd ctrl_cmd;
778 	struct lio_ctrl_pkt ctrl_pkt;
779 
780 	if (udp_tnl == NULL)
781 		return -EINVAL;
782 
783 	if (udp_tnl->prot_type != RTE_TUNNEL_TYPE_VXLAN) {
784 		lio_dev_err(lio_dev, "Unsupported tunnel type\n");
785 		return -1;
786 	}
787 
788 	/* flush added to prevent cmd failure
789 	 * incase the queue is full
790 	 */
791 	lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
792 
793 	memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
794 	memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
795 
796 	ctrl_cmd.eth_dev = eth_dev;
797 	ctrl_cmd.cond = 0;
798 
799 	ctrl_pkt.ncmd.s.cmd = LIO_CMD_VXLAN_PORT_CONFIG;
800 	ctrl_pkt.ncmd.s.param1 = udp_tnl->udp_port;
801 	ctrl_pkt.ncmd.s.more = LIO_CMD_VXLAN_PORT_ADD;
802 	ctrl_pkt.ctrl_cmd = &ctrl_cmd;
803 
804 	if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
805 		lio_dev_err(lio_dev, "Failed to send VXLAN_PORT_ADD command\n");
806 		return -1;
807 	}
808 
809 	if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
810 		lio_dev_err(lio_dev, "VXLAN_PORT_ADD command timed out\n");
811 		return -1;
812 	}
813 
814 	return 0;
815 }
816 
817 /**
818  * Remove vxlan dest udp port for an interface.
819  *
820  * @param eth_dev
821  *  Pointer to the structure rte_eth_dev
822  * @param udp_tnl
823  *  udp tunnel conf
824  *
825  * @return
826  *  On success return 0
827  *  On failure return -1
828  */
829 static int
830 lio_dev_udp_tunnel_del(struct rte_eth_dev *eth_dev,
831 		       struct rte_eth_udp_tunnel *udp_tnl)
832 {
833 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
834 	struct lio_dev_ctrl_cmd ctrl_cmd;
835 	struct lio_ctrl_pkt ctrl_pkt;
836 
837 	if (udp_tnl == NULL)
838 		return -EINVAL;
839 
840 	if (udp_tnl->prot_type != RTE_TUNNEL_TYPE_VXLAN) {
841 		lio_dev_err(lio_dev, "Unsupported tunnel type\n");
842 		return -1;
843 	}
844 
845 	/* flush added to prevent cmd failure
846 	 * incase the queue is full
847 	 */
848 	lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
849 
850 	memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
851 	memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
852 
853 	ctrl_cmd.eth_dev = eth_dev;
854 	ctrl_cmd.cond = 0;
855 
856 	ctrl_pkt.ncmd.s.cmd = LIO_CMD_VXLAN_PORT_CONFIG;
857 	ctrl_pkt.ncmd.s.param1 = udp_tnl->udp_port;
858 	ctrl_pkt.ncmd.s.more = LIO_CMD_VXLAN_PORT_DEL;
859 	ctrl_pkt.ctrl_cmd = &ctrl_cmd;
860 
861 	if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
862 		lio_dev_err(lio_dev, "Failed to send VXLAN_PORT_DEL command\n");
863 		return -1;
864 	}
865 
866 	if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
867 		lio_dev_err(lio_dev, "VXLAN_PORT_DEL command timed out\n");
868 		return -1;
869 	}
870 
871 	return 0;
872 }
873 
874 static int
875 lio_dev_vlan_filter_set(struct rte_eth_dev *eth_dev, uint16_t vlan_id, int on)
876 {
877 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
878 	struct lio_dev_ctrl_cmd ctrl_cmd;
879 	struct lio_ctrl_pkt ctrl_pkt;
880 
881 	if (lio_dev->linfo.vlan_is_admin_assigned)
882 		return -EPERM;
883 
884 	/* flush added to prevent cmd failure
885 	 * incase the queue is full
886 	 */
887 	lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
888 
889 	memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
890 	memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
891 
892 	ctrl_cmd.eth_dev = eth_dev;
893 	ctrl_cmd.cond = 0;
894 
895 	ctrl_pkt.ncmd.s.cmd = on ?
896 			LIO_CMD_ADD_VLAN_FILTER : LIO_CMD_DEL_VLAN_FILTER;
897 	ctrl_pkt.ncmd.s.param1 = vlan_id;
898 	ctrl_pkt.ctrl_cmd = &ctrl_cmd;
899 
900 	if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
901 		lio_dev_err(lio_dev, "Failed to %s VLAN port\n",
902 			    on ? "add" : "remove");
903 		return -1;
904 	}
905 
906 	if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
907 		lio_dev_err(lio_dev, "Command to %s VLAN port timed out\n",
908 			    on ? "add" : "remove");
909 		return -1;
910 	}
911 
912 	return 0;
913 }
914 
915 static uint64_t
916 lio_hweight64(uint64_t w)
917 {
918 	uint64_t res = w - ((w >> 1) & 0x5555555555555555ul);
919 
920 	res =
921 	    (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
922 	res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
923 	res = res + (res >> 8);
924 	res = res + (res >> 16);
925 
926 	return (res + (res >> 32)) & 0x00000000000000FFul;
927 }
928 
929 static int
930 lio_dev_link_update(struct rte_eth_dev *eth_dev,
931 		    int wait_to_complete __rte_unused)
932 {
933 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
934 	struct rte_eth_link link;
935 
936 	/* Initialize */
937 	memset(&link, 0, sizeof(link));
938 	link.link_status = ETH_LINK_DOWN;
939 	link.link_speed = ETH_SPEED_NUM_NONE;
940 	link.link_duplex = ETH_LINK_HALF_DUPLEX;
941 	link.link_autoneg = ETH_LINK_AUTONEG;
942 
943 	/* Return what we found */
944 	if (lio_dev->linfo.link.s.link_up == 0) {
945 		/* Interface is down */
946 		return rte_eth_linkstatus_set(eth_dev, &link);
947 	}
948 
949 	link.link_status = ETH_LINK_UP; /* Interface is up */
950 	link.link_duplex = ETH_LINK_FULL_DUPLEX;
951 	switch (lio_dev->linfo.link.s.speed) {
952 	case LIO_LINK_SPEED_10000:
953 		link.link_speed = ETH_SPEED_NUM_10G;
954 		break;
955 	case LIO_LINK_SPEED_25000:
956 		link.link_speed = ETH_SPEED_NUM_25G;
957 		break;
958 	default:
959 		link.link_speed = ETH_SPEED_NUM_NONE;
960 		link.link_duplex = ETH_LINK_HALF_DUPLEX;
961 	}
962 
963 	return rte_eth_linkstatus_set(eth_dev, &link);
964 }
965 
966 /**
967  * \brief Net device enable, disable allmulticast
968  * @param eth_dev Pointer to the structure rte_eth_dev
969  *
970  * @return
971  *  On success return 0
972  *  On failure return negative errno
973  */
974 static int
975 lio_change_dev_flag(struct rte_eth_dev *eth_dev)
976 {
977 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
978 	struct lio_dev_ctrl_cmd ctrl_cmd;
979 	struct lio_ctrl_pkt ctrl_pkt;
980 
981 	/* flush added to prevent cmd failure
982 	 * incase the queue is full
983 	 */
984 	lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
985 
986 	memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
987 	memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
988 
989 	ctrl_cmd.eth_dev = eth_dev;
990 	ctrl_cmd.cond = 0;
991 
992 	/* Create a ctrl pkt command to be sent to core app. */
993 	ctrl_pkt.ncmd.s.cmd = LIO_CMD_CHANGE_DEVFLAGS;
994 	ctrl_pkt.ncmd.s.param1 = lio_dev->ifflags;
995 	ctrl_pkt.ctrl_cmd = &ctrl_cmd;
996 
997 	if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
998 		lio_dev_err(lio_dev, "Failed to send change flag message\n");
999 		return -EAGAIN;
1000 	}
1001 
1002 	if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
1003 		lio_dev_err(lio_dev, "Change dev flag command timed out\n");
1004 		return -ETIMEDOUT;
1005 	}
1006 
1007 	return 0;
1008 }
1009 
1010 static int
1011 lio_dev_promiscuous_enable(struct rte_eth_dev *eth_dev)
1012 {
1013 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1014 
1015 	if (strcmp(lio_dev->firmware_version, LIO_VF_TRUST_MIN_VERSION) < 0) {
1016 		lio_dev_err(lio_dev, "Require firmware version >= %s\n",
1017 			    LIO_VF_TRUST_MIN_VERSION);
1018 		return -EAGAIN;
1019 	}
1020 
1021 	if (!lio_dev->intf_open) {
1022 		lio_dev_err(lio_dev, "Port %d down, can't enable promiscuous\n",
1023 			    lio_dev->port_id);
1024 		return -EAGAIN;
1025 	}
1026 
1027 	lio_dev->ifflags |= LIO_IFFLAG_PROMISC;
1028 	return lio_change_dev_flag(eth_dev);
1029 }
1030 
1031 static int
1032 lio_dev_promiscuous_disable(struct rte_eth_dev *eth_dev)
1033 {
1034 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1035 
1036 	if (strcmp(lio_dev->firmware_version, LIO_VF_TRUST_MIN_VERSION) < 0) {
1037 		lio_dev_err(lio_dev, "Require firmware version >= %s\n",
1038 			    LIO_VF_TRUST_MIN_VERSION);
1039 		return -EAGAIN;
1040 	}
1041 
1042 	if (!lio_dev->intf_open) {
1043 		lio_dev_err(lio_dev, "Port %d down, can't disable promiscuous\n",
1044 			    lio_dev->port_id);
1045 		return -EAGAIN;
1046 	}
1047 
1048 	lio_dev->ifflags &= ~LIO_IFFLAG_PROMISC;
1049 	return lio_change_dev_flag(eth_dev);
1050 }
1051 
1052 static int
1053 lio_dev_allmulticast_enable(struct rte_eth_dev *eth_dev)
1054 {
1055 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1056 
1057 	if (!lio_dev->intf_open) {
1058 		lio_dev_err(lio_dev, "Port %d down, can't enable multicast\n",
1059 			    lio_dev->port_id);
1060 		return -EAGAIN;
1061 	}
1062 
1063 	lio_dev->ifflags |= LIO_IFFLAG_ALLMULTI;
1064 	return lio_change_dev_flag(eth_dev);
1065 }
1066 
1067 static int
1068 lio_dev_allmulticast_disable(struct rte_eth_dev *eth_dev)
1069 {
1070 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1071 
1072 	if (!lio_dev->intf_open) {
1073 		lio_dev_err(lio_dev, "Port %d down, can't disable multicast\n",
1074 			    lio_dev->port_id);
1075 		return -EAGAIN;
1076 	}
1077 
1078 	lio_dev->ifflags &= ~LIO_IFFLAG_ALLMULTI;
1079 	return lio_change_dev_flag(eth_dev);
1080 }
1081 
1082 static void
1083 lio_dev_rss_configure(struct rte_eth_dev *eth_dev)
1084 {
1085 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1086 	struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
1087 	struct rte_eth_rss_reta_entry64 reta_conf[8];
1088 	struct rte_eth_rss_conf rss_conf;
1089 	uint16_t i;
1090 
1091 	/* Configure the RSS key and the RSS protocols used to compute
1092 	 * the RSS hash of input packets.
1093 	 */
1094 	rss_conf = eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
1095 	if ((rss_conf.rss_hf & LIO_RSS_OFFLOAD_ALL) == 0) {
1096 		rss_state->hash_disable = 1;
1097 		lio_dev_rss_hash_update(eth_dev, &rss_conf);
1098 		return;
1099 	}
1100 
1101 	if (rss_conf.rss_key == NULL)
1102 		rss_conf.rss_key = lio_rss_key; /* Default hash key */
1103 
1104 	lio_dev_rss_hash_update(eth_dev, &rss_conf);
1105 
1106 	memset(reta_conf, 0, sizeof(reta_conf));
1107 	for (i = 0; i < LIO_RSS_MAX_TABLE_SZ; i++) {
1108 		uint8_t q_idx, conf_idx, reta_idx;
1109 
1110 		q_idx = (uint8_t)((eth_dev->data->nb_rx_queues > 1) ?
1111 				  i % eth_dev->data->nb_rx_queues : 0);
1112 		conf_idx = i / RTE_RETA_GROUP_SIZE;
1113 		reta_idx = i % RTE_RETA_GROUP_SIZE;
1114 		reta_conf[conf_idx].reta[reta_idx] = q_idx;
1115 		reta_conf[conf_idx].mask |= ((uint64_t)1 << reta_idx);
1116 	}
1117 
1118 	lio_dev_rss_reta_update(eth_dev, reta_conf, LIO_RSS_MAX_TABLE_SZ);
1119 }
1120 
1121 static void
1122 lio_dev_mq_rx_configure(struct rte_eth_dev *eth_dev)
1123 {
1124 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1125 	struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
1126 	struct rte_eth_rss_conf rss_conf;
1127 
1128 	switch (eth_dev->data->dev_conf.rxmode.mq_mode) {
1129 	case ETH_MQ_RX_RSS:
1130 		lio_dev_rss_configure(eth_dev);
1131 		break;
1132 	case ETH_MQ_RX_NONE:
1133 	/* if mq_mode is none, disable rss mode. */
1134 	default:
1135 		memset(&rss_conf, 0, sizeof(rss_conf));
1136 		rss_state->hash_disable = 1;
1137 		lio_dev_rss_hash_update(eth_dev, &rss_conf);
1138 	}
1139 }
1140 
1141 /**
1142  * Setup our receive queue/ringbuffer. This is the
1143  * queue the Octeon uses to send us packets and
1144  * responses. We are given a memory pool for our
1145  * packet buffers that are used to populate the receive
1146  * queue.
1147  *
1148  * @param eth_dev
1149  *    Pointer to the structure rte_eth_dev
1150  * @param q_no
1151  *    Queue number
1152  * @param num_rx_descs
1153  *    Number of entries in the queue
1154  * @param socket_id
1155  *    Where to allocate memory
1156  * @param rx_conf
1157  *    Pointer to the struction rte_eth_rxconf
1158  * @param mp
1159  *    Pointer to the packet pool
1160  *
1161  * @return
1162  *    - On success, return 0
1163  *    - On failure, return -1
1164  */
1165 static int
1166 lio_dev_rx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
1167 		       uint16_t num_rx_descs, unsigned int socket_id,
1168 		       const struct rte_eth_rxconf *rx_conf __rte_unused,
1169 		       struct rte_mempool *mp)
1170 {
1171 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1172 	struct rte_pktmbuf_pool_private *mbp_priv;
1173 	uint32_t fw_mapped_oq;
1174 	uint16_t buf_size;
1175 
1176 	if (q_no >= lio_dev->nb_rx_queues) {
1177 		lio_dev_err(lio_dev, "Invalid rx queue number %u\n", q_no);
1178 		return -EINVAL;
1179 	}
1180 
1181 	lio_dev_dbg(lio_dev, "setting up rx queue %u\n", q_no);
1182 
1183 	fw_mapped_oq = lio_dev->linfo.rxpciq[q_no].s.q_no;
1184 
1185 	/* Free previous allocation if any */
1186 	if (eth_dev->data->rx_queues[q_no] != NULL) {
1187 		lio_dev_rx_queue_release(eth_dev->data->rx_queues[q_no]);
1188 		eth_dev->data->rx_queues[q_no] = NULL;
1189 	}
1190 
1191 	mbp_priv = rte_mempool_get_priv(mp);
1192 	buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
1193 
1194 	if (lio_setup_droq(lio_dev, fw_mapped_oq, num_rx_descs, buf_size, mp,
1195 			   socket_id)) {
1196 		lio_dev_err(lio_dev, "droq allocation failed\n");
1197 		return -1;
1198 	}
1199 
1200 	eth_dev->data->rx_queues[q_no] = lio_dev->droq[fw_mapped_oq];
1201 
1202 	return 0;
1203 }
1204 
1205 /**
1206  * Release the receive queue/ringbuffer. Called by
1207  * the upper layers.
1208  *
1209  * @param rxq
1210  *    Opaque pointer to the receive queue to release
1211  *
1212  * @return
1213  *    - nothing
1214  */
1215 void
1216 lio_dev_rx_queue_release(void *rxq)
1217 {
1218 	struct lio_droq *droq = rxq;
1219 	int oq_no;
1220 
1221 	if (droq) {
1222 		oq_no = droq->q_no;
1223 		lio_delete_droq_queue(droq->lio_dev, oq_no);
1224 	}
1225 }
1226 
1227 /**
1228  * Allocate and initialize SW ring. Initialize associated HW registers.
1229  *
1230  * @param eth_dev
1231  *   Pointer to structure rte_eth_dev
1232  *
1233  * @param q_no
1234  *   Queue number
1235  *
1236  * @param num_tx_descs
1237  *   Number of ringbuffer descriptors
1238  *
1239  * @param socket_id
1240  *   NUMA socket id, used for memory allocations
1241  *
1242  * @param tx_conf
1243  *   Pointer to the structure rte_eth_txconf
1244  *
1245  * @return
1246  *   - On success, return 0
1247  *   - On failure, return -errno value
1248  */
1249 static int
1250 lio_dev_tx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
1251 		       uint16_t num_tx_descs, unsigned int socket_id,
1252 		       const struct rte_eth_txconf *tx_conf __rte_unused)
1253 {
1254 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1255 	int fw_mapped_iq = lio_dev->linfo.txpciq[q_no].s.q_no;
1256 	int retval;
1257 
1258 	if (q_no >= lio_dev->nb_tx_queues) {
1259 		lio_dev_err(lio_dev, "Invalid tx queue number %u\n", q_no);
1260 		return -EINVAL;
1261 	}
1262 
1263 	lio_dev_dbg(lio_dev, "setting up tx queue %u\n", q_no);
1264 
1265 	/* Free previous allocation if any */
1266 	if (eth_dev->data->tx_queues[q_no] != NULL) {
1267 		lio_dev_tx_queue_release(eth_dev->data->tx_queues[q_no]);
1268 		eth_dev->data->tx_queues[q_no] = NULL;
1269 	}
1270 
1271 	retval = lio_setup_iq(lio_dev, q_no, lio_dev->linfo.txpciq[q_no],
1272 			      num_tx_descs, lio_dev, socket_id);
1273 
1274 	if (retval) {
1275 		lio_dev_err(lio_dev, "Runtime IQ(TxQ) creation failed.\n");
1276 		return retval;
1277 	}
1278 
1279 	retval = lio_setup_sglists(lio_dev, q_no, fw_mapped_iq,
1280 				lio_dev->instr_queue[fw_mapped_iq]->nb_desc,
1281 				socket_id);
1282 
1283 	if (retval) {
1284 		lio_delete_instruction_queue(lio_dev, fw_mapped_iq);
1285 		return retval;
1286 	}
1287 
1288 	eth_dev->data->tx_queues[q_no] = lio_dev->instr_queue[fw_mapped_iq];
1289 
1290 	return 0;
1291 }
1292 
1293 /**
1294  * Release the transmit queue/ringbuffer. Called by
1295  * the upper layers.
1296  *
1297  * @param txq
1298  *    Opaque pointer to the transmit queue to release
1299  *
1300  * @return
1301  *    - nothing
1302  */
1303 void
1304 lio_dev_tx_queue_release(void *txq)
1305 {
1306 	struct lio_instr_queue *tq = txq;
1307 	uint32_t fw_mapped_iq_no;
1308 
1309 
1310 	if (tq) {
1311 		/* Free sg_list */
1312 		lio_delete_sglist(tq);
1313 
1314 		fw_mapped_iq_no = tq->txpciq.s.q_no;
1315 		lio_delete_instruction_queue(tq->lio_dev, fw_mapped_iq_no);
1316 	}
1317 }
1318 
1319 /**
1320  * Api to check link state.
1321  */
1322 static void
1323 lio_dev_get_link_status(struct rte_eth_dev *eth_dev)
1324 {
1325 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1326 	uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
1327 	struct lio_link_status_resp *resp;
1328 	union octeon_link_status *ls;
1329 	struct lio_soft_command *sc;
1330 	uint32_t resp_size;
1331 
1332 	if (!lio_dev->intf_open)
1333 		return;
1334 
1335 	resp_size = sizeof(struct lio_link_status_resp);
1336 	sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
1337 	if (sc == NULL)
1338 		return;
1339 
1340 	resp = (struct lio_link_status_resp *)sc->virtrptr;
1341 	lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
1342 				 LIO_OPCODE_INFO, 0, 0, 0);
1343 
1344 	/* Setting wait time in seconds */
1345 	sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
1346 
1347 	if (lio_send_soft_command(lio_dev, sc) == LIO_IQ_SEND_FAILED)
1348 		goto get_status_fail;
1349 
1350 	while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
1351 		lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
1352 		rte_delay_ms(1);
1353 	}
1354 
1355 	if (resp->status)
1356 		goto get_status_fail;
1357 
1358 	ls = &resp->link_info.link;
1359 
1360 	lio_swap_8B_data((uint64_t *)ls, sizeof(union octeon_link_status) >> 3);
1361 
1362 	if (lio_dev->linfo.link.link_status64 != ls->link_status64) {
1363 		if (ls->s.mtu < eth_dev->data->mtu) {
1364 			lio_dev_info(lio_dev, "Lowered VF MTU to %d as PF MTU dropped\n",
1365 				     ls->s.mtu);
1366 			eth_dev->data->mtu = ls->s.mtu;
1367 		}
1368 		lio_dev->linfo.link.link_status64 = ls->link_status64;
1369 		lio_dev_link_update(eth_dev, 0);
1370 	}
1371 
1372 	lio_free_soft_command(sc);
1373 
1374 	return;
1375 
1376 get_status_fail:
1377 	lio_free_soft_command(sc);
1378 }
1379 
1380 /* This function will be invoked every LSC_TIMEOUT ns (100ms)
1381  * and will update link state if it changes.
1382  */
1383 static void
1384 lio_sync_link_state_check(void *eth_dev)
1385 {
1386 	struct lio_device *lio_dev =
1387 		(((struct rte_eth_dev *)eth_dev)->data->dev_private);
1388 
1389 	if (lio_dev->port_configured)
1390 		lio_dev_get_link_status(eth_dev);
1391 
1392 	/* Schedule periodic link status check.
1393 	 * Stop check if interface is close and start again while opening.
1394 	 */
1395 	if (lio_dev->intf_open)
1396 		rte_eal_alarm_set(LIO_LSC_TIMEOUT, lio_sync_link_state_check,
1397 				  eth_dev);
1398 }
1399 
1400 static int
1401 lio_dev_start(struct rte_eth_dev *eth_dev)
1402 {
1403 	uint16_t mtu;
1404 	uint32_t frame_len = eth_dev->data->dev_conf.rxmode.max_rx_pkt_len;
1405 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1406 	uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
1407 	int ret = 0;
1408 
1409 	lio_dev_info(lio_dev, "Starting port %d\n", eth_dev->data->port_id);
1410 
1411 	if (lio_dev->fn_list.enable_io_queues(lio_dev))
1412 		return -1;
1413 
1414 	if (lio_send_rx_ctrl_cmd(eth_dev, 1))
1415 		return -1;
1416 
1417 	/* Ready for link status updates */
1418 	lio_dev->intf_open = 1;
1419 	rte_mb();
1420 
1421 	/* Configure RSS if device configured with multiple RX queues. */
1422 	lio_dev_mq_rx_configure(eth_dev);
1423 
1424 	/* Before update the link info,
1425 	 * must set linfo.link.link_status64 to 0.
1426 	 */
1427 	lio_dev->linfo.link.link_status64 = 0;
1428 
1429 	/* start polling for lsc */
1430 	ret = rte_eal_alarm_set(LIO_LSC_TIMEOUT,
1431 				lio_sync_link_state_check,
1432 				eth_dev);
1433 	if (ret) {
1434 		lio_dev_err(lio_dev,
1435 			    "link state check handler creation failed\n");
1436 		goto dev_lsc_handle_error;
1437 	}
1438 
1439 	while ((lio_dev->linfo.link.link_status64 == 0) && (--timeout))
1440 		rte_delay_ms(1);
1441 
1442 	if (lio_dev->linfo.link.link_status64 == 0) {
1443 		ret = -1;
1444 		goto dev_mtu_set_error;
1445 	}
1446 
1447 	mtu = (uint16_t)(frame_len - RTE_ETHER_HDR_LEN - RTE_ETHER_CRC_LEN);
1448 	if (mtu < RTE_ETHER_MIN_MTU)
1449 		mtu = RTE_ETHER_MIN_MTU;
1450 
1451 	if (eth_dev->data->mtu != mtu) {
1452 		ret = lio_dev_mtu_set(eth_dev, mtu);
1453 		if (ret)
1454 			goto dev_mtu_set_error;
1455 	}
1456 
1457 	return 0;
1458 
1459 dev_mtu_set_error:
1460 	rte_eal_alarm_cancel(lio_sync_link_state_check, eth_dev);
1461 
1462 dev_lsc_handle_error:
1463 	lio_dev->intf_open = 0;
1464 	lio_send_rx_ctrl_cmd(eth_dev, 0);
1465 
1466 	return ret;
1467 }
1468 
1469 /* Stop device and disable input/output functions */
1470 static void
1471 lio_dev_stop(struct rte_eth_dev *eth_dev)
1472 {
1473 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1474 
1475 	lio_dev_info(lio_dev, "Stopping port %d\n", eth_dev->data->port_id);
1476 	lio_dev->intf_open = 0;
1477 	rte_mb();
1478 
1479 	/* Cancel callback if still running. */
1480 	rte_eal_alarm_cancel(lio_sync_link_state_check, eth_dev);
1481 
1482 	lio_send_rx_ctrl_cmd(eth_dev, 0);
1483 
1484 	lio_wait_for_instr_fetch(lio_dev);
1485 
1486 	/* Clear recorded link status */
1487 	lio_dev->linfo.link.link_status64 = 0;
1488 }
1489 
1490 static int
1491 lio_dev_set_link_up(struct rte_eth_dev *eth_dev)
1492 {
1493 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1494 
1495 	if (!lio_dev->intf_open) {
1496 		lio_dev_info(lio_dev, "Port is stopped, Start the port first\n");
1497 		return 0;
1498 	}
1499 
1500 	if (lio_dev->linfo.link.s.link_up) {
1501 		lio_dev_info(lio_dev, "Link is already UP\n");
1502 		return 0;
1503 	}
1504 
1505 	if (lio_send_rx_ctrl_cmd(eth_dev, 1)) {
1506 		lio_dev_err(lio_dev, "Unable to set Link UP\n");
1507 		return -1;
1508 	}
1509 
1510 	lio_dev->linfo.link.s.link_up = 1;
1511 	eth_dev->data->dev_link.link_status = ETH_LINK_UP;
1512 
1513 	return 0;
1514 }
1515 
1516 static int
1517 lio_dev_set_link_down(struct rte_eth_dev *eth_dev)
1518 {
1519 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1520 
1521 	if (!lio_dev->intf_open) {
1522 		lio_dev_info(lio_dev, "Port is stopped, Start the port first\n");
1523 		return 0;
1524 	}
1525 
1526 	if (!lio_dev->linfo.link.s.link_up) {
1527 		lio_dev_info(lio_dev, "Link is already DOWN\n");
1528 		return 0;
1529 	}
1530 
1531 	lio_dev->linfo.link.s.link_up = 0;
1532 	eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
1533 
1534 	if (lio_send_rx_ctrl_cmd(eth_dev, 0)) {
1535 		lio_dev->linfo.link.s.link_up = 1;
1536 		eth_dev->data->dev_link.link_status = ETH_LINK_UP;
1537 		lio_dev_err(lio_dev, "Unable to set Link Down\n");
1538 		return -1;
1539 	}
1540 
1541 	return 0;
1542 }
1543 
1544 /**
1545  * Reset and stop the device. This occurs on the first
1546  * call to this routine. Subsequent calls will simply
1547  * return. NB: This will require the NIC to be rebooted.
1548  *
1549  * @param eth_dev
1550  *    Pointer to the structure rte_eth_dev
1551  *
1552  * @return
1553  *    - nothing
1554  */
1555 static void
1556 lio_dev_close(struct rte_eth_dev *eth_dev)
1557 {
1558 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1559 
1560 	lio_dev_info(lio_dev, "closing port %d\n", eth_dev->data->port_id);
1561 
1562 	if (lio_dev->intf_open)
1563 		lio_dev_stop(eth_dev);
1564 
1565 	/* Reset ioq regs */
1566 	lio_dev->fn_list.setup_device_regs(lio_dev);
1567 
1568 	if (lio_dev->pci_dev->kdrv == RTE_KDRV_IGB_UIO) {
1569 		cn23xx_vf_ask_pf_to_do_flr(lio_dev);
1570 		rte_delay_ms(LIO_PCI_FLR_WAIT);
1571 	}
1572 
1573 	/* lio_free_mbox */
1574 	lio_dev->fn_list.free_mbox(lio_dev);
1575 
1576 	/* Free glist resources */
1577 	rte_free(lio_dev->glist_head);
1578 	rte_free(lio_dev->glist_lock);
1579 	lio_dev->glist_head = NULL;
1580 	lio_dev->glist_lock = NULL;
1581 
1582 	lio_dev->port_configured = 0;
1583 
1584 	 /* Delete all queues */
1585 	lio_dev_clear_queues(eth_dev);
1586 }
1587 
1588 /**
1589  * Enable tunnel rx checksum verification from firmware.
1590  */
1591 static void
1592 lio_enable_hw_tunnel_rx_checksum(struct rte_eth_dev *eth_dev)
1593 {
1594 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1595 	struct lio_dev_ctrl_cmd ctrl_cmd;
1596 	struct lio_ctrl_pkt ctrl_pkt;
1597 
1598 	/* flush added to prevent cmd failure
1599 	 * incase the queue is full
1600 	 */
1601 	lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
1602 
1603 	memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
1604 	memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
1605 
1606 	ctrl_cmd.eth_dev = eth_dev;
1607 	ctrl_cmd.cond = 0;
1608 
1609 	ctrl_pkt.ncmd.s.cmd = LIO_CMD_TNL_RX_CSUM_CTL;
1610 	ctrl_pkt.ncmd.s.param1 = LIO_CMD_RXCSUM_ENABLE;
1611 	ctrl_pkt.ctrl_cmd = &ctrl_cmd;
1612 
1613 	if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
1614 		lio_dev_err(lio_dev, "Failed to send TNL_RX_CSUM command\n");
1615 		return;
1616 	}
1617 
1618 	if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd))
1619 		lio_dev_err(lio_dev, "TNL_RX_CSUM command timed out\n");
1620 }
1621 
1622 /**
1623  * Enable checksum calculation for inner packet in a tunnel.
1624  */
1625 static void
1626 lio_enable_hw_tunnel_tx_checksum(struct rte_eth_dev *eth_dev)
1627 {
1628 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1629 	struct lio_dev_ctrl_cmd ctrl_cmd;
1630 	struct lio_ctrl_pkt ctrl_pkt;
1631 
1632 	/* flush added to prevent cmd failure
1633 	 * incase the queue is full
1634 	 */
1635 	lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
1636 
1637 	memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
1638 	memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
1639 
1640 	ctrl_cmd.eth_dev = eth_dev;
1641 	ctrl_cmd.cond = 0;
1642 
1643 	ctrl_pkt.ncmd.s.cmd = LIO_CMD_TNL_TX_CSUM_CTL;
1644 	ctrl_pkt.ncmd.s.param1 = LIO_CMD_TXCSUM_ENABLE;
1645 	ctrl_pkt.ctrl_cmd = &ctrl_cmd;
1646 
1647 	if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
1648 		lio_dev_err(lio_dev, "Failed to send TNL_TX_CSUM command\n");
1649 		return;
1650 	}
1651 
1652 	if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd))
1653 		lio_dev_err(lio_dev, "TNL_TX_CSUM command timed out\n");
1654 }
1655 
1656 static int
1657 lio_send_queue_count_update(struct rte_eth_dev *eth_dev, int num_txq,
1658 			    int num_rxq)
1659 {
1660 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1661 	struct lio_dev_ctrl_cmd ctrl_cmd;
1662 	struct lio_ctrl_pkt ctrl_pkt;
1663 
1664 	if (strcmp(lio_dev->firmware_version, LIO_Q_RECONF_MIN_VERSION) < 0) {
1665 		lio_dev_err(lio_dev, "Require firmware version >= %s\n",
1666 			    LIO_Q_RECONF_MIN_VERSION);
1667 		return -ENOTSUP;
1668 	}
1669 
1670 	/* flush added to prevent cmd failure
1671 	 * incase the queue is full
1672 	 */
1673 	lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
1674 
1675 	memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
1676 	memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
1677 
1678 	ctrl_cmd.eth_dev = eth_dev;
1679 	ctrl_cmd.cond = 0;
1680 
1681 	ctrl_pkt.ncmd.s.cmd = LIO_CMD_QUEUE_COUNT_CTL;
1682 	ctrl_pkt.ncmd.s.param1 = num_txq;
1683 	ctrl_pkt.ncmd.s.param2 = num_rxq;
1684 	ctrl_pkt.ctrl_cmd = &ctrl_cmd;
1685 
1686 	if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
1687 		lio_dev_err(lio_dev, "Failed to send queue count control command\n");
1688 		return -1;
1689 	}
1690 
1691 	if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
1692 		lio_dev_err(lio_dev, "Queue count control command timed out\n");
1693 		return -1;
1694 	}
1695 
1696 	return 0;
1697 }
1698 
1699 static int
1700 lio_reconf_queues(struct rte_eth_dev *eth_dev, int num_txq, int num_rxq)
1701 {
1702 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1703 
1704 	if (lio_dev->nb_rx_queues != num_rxq ||
1705 	    lio_dev->nb_tx_queues != num_txq) {
1706 		if (lio_send_queue_count_update(eth_dev, num_txq, num_rxq))
1707 			return -1;
1708 		lio_dev->nb_rx_queues = num_rxq;
1709 		lio_dev->nb_tx_queues = num_txq;
1710 	}
1711 
1712 	if (lio_dev->intf_open)
1713 		lio_dev_stop(eth_dev);
1714 
1715 	/* Reset ioq registers */
1716 	if (lio_dev->fn_list.setup_device_regs(lio_dev)) {
1717 		lio_dev_err(lio_dev, "Failed to configure device registers\n");
1718 		return -1;
1719 	}
1720 
1721 	return 0;
1722 }
1723 
1724 static int
1725 lio_dev_configure(struct rte_eth_dev *eth_dev)
1726 {
1727 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
1728 	uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
1729 	int retval, num_iqueues, num_oqueues;
1730 	uint8_t mac[RTE_ETHER_ADDR_LEN], i;
1731 	struct lio_if_cfg_resp *resp;
1732 	struct lio_soft_command *sc;
1733 	union lio_if_cfg if_cfg;
1734 	uint32_t resp_size;
1735 
1736 	PMD_INIT_FUNC_TRACE();
1737 
1738 	/* Inform firmware about change in number of queues to use.
1739 	 * Disable IO queues and reset registers for re-configuration.
1740 	 */
1741 	if (lio_dev->port_configured)
1742 		return lio_reconf_queues(eth_dev,
1743 					 eth_dev->data->nb_tx_queues,
1744 					 eth_dev->data->nb_rx_queues);
1745 
1746 	lio_dev->nb_rx_queues = eth_dev->data->nb_rx_queues;
1747 	lio_dev->nb_tx_queues = eth_dev->data->nb_tx_queues;
1748 
1749 	/* Set max number of queues which can be re-configured. */
1750 	lio_dev->max_rx_queues = eth_dev->data->nb_rx_queues;
1751 	lio_dev->max_tx_queues = eth_dev->data->nb_tx_queues;
1752 
1753 	resp_size = sizeof(struct lio_if_cfg_resp);
1754 	sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
1755 	if (sc == NULL)
1756 		return -ENOMEM;
1757 
1758 	resp = (struct lio_if_cfg_resp *)sc->virtrptr;
1759 
1760 	/* Firmware doesn't have capability to reconfigure the queues,
1761 	 * Claim all queues, and use as many required
1762 	 */
1763 	if_cfg.if_cfg64 = 0;
1764 	if_cfg.s.num_iqueues = lio_dev->nb_tx_queues;
1765 	if_cfg.s.num_oqueues = lio_dev->nb_rx_queues;
1766 	if_cfg.s.base_queue = 0;
1767 
1768 	if_cfg.s.gmx_port_id = lio_dev->pf_num;
1769 
1770 	lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
1771 				 LIO_OPCODE_IF_CFG, 0,
1772 				 if_cfg.if_cfg64, 0);
1773 
1774 	/* Setting wait time in seconds */
1775 	sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
1776 
1777 	retval = lio_send_soft_command(lio_dev, sc);
1778 	if (retval == LIO_IQ_SEND_FAILED) {
1779 		lio_dev_err(lio_dev, "iq/oq config failed status: %x\n",
1780 			    retval);
1781 		/* Soft instr is freed by driver in case of failure. */
1782 		goto nic_config_fail;
1783 	}
1784 
1785 	/* Sleep on a wait queue till the cond flag indicates that the
1786 	 * response arrived or timed-out.
1787 	 */
1788 	while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
1789 		lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
1790 		lio_process_ordered_list(lio_dev);
1791 		rte_delay_ms(1);
1792 	}
1793 
1794 	retval = resp->status;
1795 	if (retval) {
1796 		lio_dev_err(lio_dev, "iq/oq config failed\n");
1797 		goto nic_config_fail;
1798 	}
1799 
1800 	strlcpy(lio_dev->firmware_version,
1801 		resp->cfg_info.lio_firmware_version, LIO_FW_VERSION_LENGTH);
1802 
1803 	lio_swap_8B_data((uint64_t *)(&resp->cfg_info),
1804 			 sizeof(struct octeon_if_cfg_info) >> 3);
1805 
1806 	num_iqueues = lio_hweight64(resp->cfg_info.iqmask);
1807 	num_oqueues = lio_hweight64(resp->cfg_info.oqmask);
1808 
1809 	if (!(num_iqueues) || !(num_oqueues)) {
1810 		lio_dev_err(lio_dev,
1811 			    "Got bad iqueues (%016lx) or oqueues (%016lx) from firmware.\n",
1812 			    (unsigned long)resp->cfg_info.iqmask,
1813 			    (unsigned long)resp->cfg_info.oqmask);
1814 		goto nic_config_fail;
1815 	}
1816 
1817 	lio_dev_dbg(lio_dev,
1818 		    "interface %d, iqmask %016lx, oqmask %016lx, numiqueues %d, numoqueues %d\n",
1819 		    eth_dev->data->port_id,
1820 		    (unsigned long)resp->cfg_info.iqmask,
1821 		    (unsigned long)resp->cfg_info.oqmask,
1822 		    num_iqueues, num_oqueues);
1823 
1824 	lio_dev->linfo.num_rxpciq = num_oqueues;
1825 	lio_dev->linfo.num_txpciq = num_iqueues;
1826 
1827 	for (i = 0; i < num_oqueues; i++) {
1828 		lio_dev->linfo.rxpciq[i].rxpciq64 =
1829 		    resp->cfg_info.linfo.rxpciq[i].rxpciq64;
1830 		lio_dev_dbg(lio_dev, "index %d OQ %d\n",
1831 			    i, lio_dev->linfo.rxpciq[i].s.q_no);
1832 	}
1833 
1834 	for (i = 0; i < num_iqueues; i++) {
1835 		lio_dev->linfo.txpciq[i].txpciq64 =
1836 		    resp->cfg_info.linfo.txpciq[i].txpciq64;
1837 		lio_dev_dbg(lio_dev, "index %d IQ %d\n",
1838 			    i, lio_dev->linfo.txpciq[i].s.q_no);
1839 	}
1840 
1841 	lio_dev->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
1842 	lio_dev->linfo.gmxport = resp->cfg_info.linfo.gmxport;
1843 	lio_dev->linfo.link.link_status64 =
1844 			resp->cfg_info.linfo.link.link_status64;
1845 
1846 	/* 64-bit swap required on LE machines */
1847 	lio_swap_8B_data(&lio_dev->linfo.hw_addr, 1);
1848 	for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
1849 		mac[i] = *((uint8_t *)(((uint8_t *)&lio_dev->linfo.hw_addr) +
1850 				       2 + i));
1851 
1852 	/* Copy the permanent MAC address */
1853 	rte_ether_addr_copy((struct rte_ether_addr *)mac,
1854 			&eth_dev->data->mac_addrs[0]);
1855 
1856 	/* enable firmware checksum support for tunnel packets */
1857 	lio_enable_hw_tunnel_rx_checksum(eth_dev);
1858 	lio_enable_hw_tunnel_tx_checksum(eth_dev);
1859 
1860 	lio_dev->glist_lock =
1861 	    rte_zmalloc(NULL, sizeof(*lio_dev->glist_lock) * num_iqueues, 0);
1862 	if (lio_dev->glist_lock == NULL)
1863 		return -ENOMEM;
1864 
1865 	lio_dev->glist_head =
1866 		rte_zmalloc(NULL, sizeof(*lio_dev->glist_head) * num_iqueues,
1867 			    0);
1868 	if (lio_dev->glist_head == NULL) {
1869 		rte_free(lio_dev->glist_lock);
1870 		lio_dev->glist_lock = NULL;
1871 		return -ENOMEM;
1872 	}
1873 
1874 	lio_dev_link_update(eth_dev, 0);
1875 
1876 	lio_dev->port_configured = 1;
1877 
1878 	lio_free_soft_command(sc);
1879 
1880 	/* Reset ioq regs */
1881 	lio_dev->fn_list.setup_device_regs(lio_dev);
1882 
1883 	/* Free iq_0 used during init */
1884 	lio_free_instr_queue0(lio_dev);
1885 
1886 	return 0;
1887 
1888 nic_config_fail:
1889 	lio_dev_err(lio_dev, "Failed retval %d\n", retval);
1890 	lio_free_soft_command(sc);
1891 	lio_free_instr_queue0(lio_dev);
1892 
1893 	return -ENODEV;
1894 }
1895 
1896 /* Define our ethernet definitions */
1897 static const struct eth_dev_ops liovf_eth_dev_ops = {
1898 	.dev_configure		= lio_dev_configure,
1899 	.dev_start		= lio_dev_start,
1900 	.dev_stop		= lio_dev_stop,
1901 	.dev_set_link_up	= lio_dev_set_link_up,
1902 	.dev_set_link_down	= lio_dev_set_link_down,
1903 	.dev_close		= lio_dev_close,
1904 	.promiscuous_enable	= lio_dev_promiscuous_enable,
1905 	.promiscuous_disable	= lio_dev_promiscuous_disable,
1906 	.allmulticast_enable	= lio_dev_allmulticast_enable,
1907 	.allmulticast_disable	= lio_dev_allmulticast_disable,
1908 	.link_update		= lio_dev_link_update,
1909 	.stats_get		= lio_dev_stats_get,
1910 	.xstats_get		= lio_dev_xstats_get,
1911 	.xstats_get_names	= lio_dev_xstats_get_names,
1912 	.stats_reset		= lio_dev_stats_reset,
1913 	.xstats_reset		= lio_dev_xstats_reset,
1914 	.dev_infos_get		= lio_dev_info_get,
1915 	.vlan_filter_set	= lio_dev_vlan_filter_set,
1916 	.rx_queue_setup		= lio_dev_rx_queue_setup,
1917 	.rx_queue_release	= lio_dev_rx_queue_release,
1918 	.tx_queue_setup		= lio_dev_tx_queue_setup,
1919 	.tx_queue_release	= lio_dev_tx_queue_release,
1920 	.reta_update		= lio_dev_rss_reta_update,
1921 	.reta_query		= lio_dev_rss_reta_query,
1922 	.rss_hash_conf_get	= lio_dev_rss_hash_conf_get,
1923 	.rss_hash_update	= lio_dev_rss_hash_update,
1924 	.udp_tunnel_port_add	= lio_dev_udp_tunnel_add,
1925 	.udp_tunnel_port_del	= lio_dev_udp_tunnel_del,
1926 	.mtu_set		= lio_dev_mtu_set,
1927 };
1928 
1929 static void
1930 lio_check_pf_hs_response(void *lio_dev)
1931 {
1932 	struct lio_device *dev = lio_dev;
1933 
1934 	/* check till response arrives */
1935 	if (dev->pfvf_hsword.coproc_tics_per_us)
1936 		return;
1937 
1938 	cn23xx_vf_handle_mbox(dev);
1939 
1940 	rte_eal_alarm_set(1, lio_check_pf_hs_response, lio_dev);
1941 }
1942 
1943 /**
1944  * \brief Identify the LIO device and to map the BAR address space
1945  * @param lio_dev lio device
1946  */
1947 static int
1948 lio_chip_specific_setup(struct lio_device *lio_dev)
1949 {
1950 	struct rte_pci_device *pdev = lio_dev->pci_dev;
1951 	uint32_t dev_id = pdev->id.device_id;
1952 	const char *s;
1953 	int ret = 1;
1954 
1955 	switch (dev_id) {
1956 	case LIO_CN23XX_VF_VID:
1957 		lio_dev->chip_id = LIO_CN23XX_VF_VID;
1958 		ret = cn23xx_vf_setup_device(lio_dev);
1959 		s = "CN23XX VF";
1960 		break;
1961 	default:
1962 		s = "?";
1963 		lio_dev_err(lio_dev, "Unsupported Chip\n");
1964 	}
1965 
1966 	if (!ret)
1967 		lio_dev_info(lio_dev, "DEVICE : %s\n", s);
1968 
1969 	return ret;
1970 }
1971 
1972 static int
1973 lio_first_time_init(struct lio_device *lio_dev,
1974 		    struct rte_pci_device *pdev)
1975 {
1976 	int dpdk_queues;
1977 
1978 	PMD_INIT_FUNC_TRACE();
1979 
1980 	/* set dpdk specific pci device pointer */
1981 	lio_dev->pci_dev = pdev;
1982 
1983 	/* Identify the LIO type and set device ops */
1984 	if (lio_chip_specific_setup(lio_dev)) {
1985 		lio_dev_err(lio_dev, "Chip specific setup failed\n");
1986 		return -1;
1987 	}
1988 
1989 	/* Initialize soft command buffer pool */
1990 	if (lio_setup_sc_buffer_pool(lio_dev)) {
1991 		lio_dev_err(lio_dev, "sc buffer pool allocation failed\n");
1992 		return -1;
1993 	}
1994 
1995 	/* Initialize lists to manage the requests of different types that
1996 	 * arrive from applications for this lio device.
1997 	 */
1998 	lio_setup_response_list(lio_dev);
1999 
2000 	if (lio_dev->fn_list.setup_mbox(lio_dev)) {
2001 		lio_dev_err(lio_dev, "Mailbox setup failed\n");
2002 		goto error;
2003 	}
2004 
2005 	/* Check PF response */
2006 	lio_check_pf_hs_response((void *)lio_dev);
2007 
2008 	/* Do handshake and exit if incompatible PF driver */
2009 	if (cn23xx_pfvf_handshake(lio_dev))
2010 		goto error;
2011 
2012 	/* Request and wait for device reset. */
2013 	if (pdev->kdrv == RTE_KDRV_IGB_UIO) {
2014 		cn23xx_vf_ask_pf_to_do_flr(lio_dev);
2015 		/* FLR wait time doubled as a precaution. */
2016 		rte_delay_ms(LIO_PCI_FLR_WAIT * 2);
2017 	}
2018 
2019 	if (lio_dev->fn_list.setup_device_regs(lio_dev)) {
2020 		lio_dev_err(lio_dev, "Failed to configure device registers\n");
2021 		goto error;
2022 	}
2023 
2024 	if (lio_setup_instr_queue0(lio_dev)) {
2025 		lio_dev_err(lio_dev, "Failed to setup instruction queue 0\n");
2026 		goto error;
2027 	}
2028 
2029 	dpdk_queues = (int)lio_dev->sriov_info.rings_per_vf;
2030 
2031 	lio_dev->max_tx_queues = dpdk_queues;
2032 	lio_dev->max_rx_queues = dpdk_queues;
2033 
2034 	/* Enable input and output queues for this device */
2035 	if (lio_dev->fn_list.enable_io_queues(lio_dev))
2036 		goto error;
2037 
2038 	return 0;
2039 
2040 error:
2041 	lio_free_sc_buffer_pool(lio_dev);
2042 	if (lio_dev->mbox[0])
2043 		lio_dev->fn_list.free_mbox(lio_dev);
2044 	if (lio_dev->instr_queue[0])
2045 		lio_free_instr_queue0(lio_dev);
2046 
2047 	return -1;
2048 }
2049 
2050 static int
2051 lio_eth_dev_uninit(struct rte_eth_dev *eth_dev)
2052 {
2053 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
2054 
2055 	PMD_INIT_FUNC_TRACE();
2056 
2057 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2058 		return 0;
2059 
2060 	/* lio_free_sc_buffer_pool */
2061 	lio_free_sc_buffer_pool(lio_dev);
2062 
2063 	eth_dev->dev_ops = NULL;
2064 	eth_dev->rx_pkt_burst = NULL;
2065 	eth_dev->tx_pkt_burst = NULL;
2066 
2067 	return 0;
2068 }
2069 
2070 static int
2071 lio_eth_dev_init(struct rte_eth_dev *eth_dev)
2072 {
2073 	struct rte_pci_device *pdev = RTE_ETH_DEV_TO_PCI(eth_dev);
2074 	struct lio_device *lio_dev = LIO_DEV(eth_dev);
2075 
2076 	PMD_INIT_FUNC_TRACE();
2077 
2078 	eth_dev->rx_pkt_burst = &lio_dev_recv_pkts;
2079 	eth_dev->tx_pkt_burst = &lio_dev_xmit_pkts;
2080 
2081 	/* Primary does the initialization. */
2082 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2083 		return 0;
2084 
2085 	rte_eth_copy_pci_info(eth_dev, pdev);
2086 
2087 	if (pdev->mem_resource[0].addr) {
2088 		lio_dev->hw_addr = pdev->mem_resource[0].addr;
2089 	} else {
2090 		PMD_INIT_LOG(ERR, "ERROR: Failed to map BAR0\n");
2091 		return -ENODEV;
2092 	}
2093 
2094 	lio_dev->eth_dev = eth_dev;
2095 	/* set lio device print string */
2096 	snprintf(lio_dev->dev_string, sizeof(lio_dev->dev_string),
2097 		 "%s[%02x:%02x.%x]", pdev->driver->driver.name,
2098 		 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
2099 
2100 	lio_dev->port_id = eth_dev->data->port_id;
2101 
2102 	if (lio_first_time_init(lio_dev, pdev)) {
2103 		lio_dev_err(lio_dev, "Device init failed\n");
2104 		return -EINVAL;
2105 	}
2106 
2107 	eth_dev->dev_ops = &liovf_eth_dev_ops;
2108 	eth_dev->data->mac_addrs = rte_zmalloc("lio", RTE_ETHER_ADDR_LEN, 0);
2109 	if (eth_dev->data->mac_addrs == NULL) {
2110 		lio_dev_err(lio_dev,
2111 			    "MAC addresses memory allocation failed\n");
2112 		eth_dev->dev_ops = NULL;
2113 		eth_dev->rx_pkt_burst = NULL;
2114 		eth_dev->tx_pkt_burst = NULL;
2115 		return -ENOMEM;
2116 	}
2117 
2118 	rte_atomic64_set(&lio_dev->status, LIO_DEV_RUNNING);
2119 	rte_wmb();
2120 
2121 	lio_dev->port_configured = 0;
2122 	/* Always allow unicast packets */
2123 	lio_dev->ifflags |= LIO_IFFLAG_UNICAST;
2124 
2125 	return 0;
2126 }
2127 
2128 static int
2129 lio_eth_dev_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2130 		      struct rte_pci_device *pci_dev)
2131 {
2132 	return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct lio_device),
2133 			lio_eth_dev_init);
2134 }
2135 
2136 static int
2137 lio_eth_dev_pci_remove(struct rte_pci_device *pci_dev)
2138 {
2139 	return rte_eth_dev_pci_generic_remove(pci_dev,
2140 					      lio_eth_dev_uninit);
2141 }
2142 
2143 /* Set of PCI devices this driver supports */
2144 static const struct rte_pci_id pci_id_liovf_map[] = {
2145 	{ RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_VF_VID) },
2146 	{ .vendor_id = 0, /* sentinel */ }
2147 };
2148 
2149 static struct rte_pci_driver rte_liovf_pmd = {
2150 	.id_table	= pci_id_liovf_map,
2151 	.drv_flags      = RTE_PCI_DRV_NEED_MAPPING,
2152 	.probe		= lio_eth_dev_pci_probe,
2153 	.remove		= lio_eth_dev_pci_remove,
2154 };
2155 
2156 RTE_PMD_REGISTER_PCI(net_liovf, rte_liovf_pmd);
2157 RTE_PMD_REGISTER_PCI_TABLE(net_liovf, pci_id_liovf_map);
2158 RTE_PMD_REGISTER_KMOD_DEP(net_liovf, "* igb_uio | vfio-pci");
2159 
2160 RTE_INIT(lio_init_log)
2161 {
2162 	lio_logtype_init = rte_log_register("pmd.net.liquidio.init");
2163 	if (lio_logtype_init >= 0)
2164 		rte_log_set_level(lio_logtype_init, RTE_LOG_NOTICE);
2165 	lio_logtype_driver = rte_log_register("pmd.net.liquidio.driver");
2166 	if (lio_logtype_driver >= 0)
2167 		rte_log_set_level(lio_logtype_driver, RTE_LOG_NOTICE);
2168 }
2169