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