net/liquidio: move to new offload API
[dpdk.git] / drivers / net / liquidio / lio_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Cavium, Inc
3  */
4
5 #include <rte_ethdev_driver.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.offloads |=
482                         DEV_RX_OFFLOAD_JUMBO_FRAME;
483         else
484                 eth_dev->data->dev_conf.rxmode.offloads &=
485                         ~DEV_RX_OFFLOAD_JUMBO_FRAME;
486
487         eth_dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_len;
488         eth_dev->data->mtu = mtu;
489
490         return 0;
491 }
492
493 static int
494 lio_dev_rss_reta_update(struct rte_eth_dev *eth_dev,
495                         struct rte_eth_rss_reta_entry64 *reta_conf,
496                         uint16_t reta_size)
497 {
498         struct lio_device *lio_dev = LIO_DEV(eth_dev);
499         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
500         struct lio_rss_set *rss_param;
501         struct lio_dev_ctrl_cmd ctrl_cmd;
502         struct lio_ctrl_pkt ctrl_pkt;
503         int i, j, index;
504
505         if (!lio_dev->intf_open) {
506                 lio_dev_err(lio_dev, "Port %d down, can't update reta\n",
507                             lio_dev->port_id);
508                 return -EINVAL;
509         }
510
511         if (reta_size != LIO_RSS_MAX_TABLE_SZ) {
512                 lio_dev_err(lio_dev,
513                             "The size of hash lookup table configured (%d) doesn't match the number hardware can supported (%d)\n",
514                             reta_size, LIO_RSS_MAX_TABLE_SZ);
515                 return -EINVAL;
516         }
517
518         /* flush added to prevent cmd failure
519          * incase the queue is full
520          */
521         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
522
523         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
524         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
525
526         rss_param = (struct lio_rss_set *)&ctrl_pkt.udd[0];
527
528         ctrl_cmd.eth_dev = eth_dev;
529         ctrl_cmd.cond = 0;
530
531         ctrl_pkt.ncmd.s.cmd = LIO_CMD_SET_RSS;
532         ctrl_pkt.ncmd.s.more = sizeof(struct lio_rss_set) >> 3;
533         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
534
535         rss_param->param.flags = 0xF;
536         rss_param->param.flags &= ~LIO_RSS_PARAM_ITABLE_UNCHANGED;
537         rss_param->param.itablesize = LIO_RSS_MAX_TABLE_SZ;
538
539         for (i = 0; i < (reta_size / RTE_RETA_GROUP_SIZE); i++) {
540                 for (j = 0; j < RTE_RETA_GROUP_SIZE; j++) {
541                         if ((reta_conf[i].mask) & ((uint64_t)1 << j)) {
542                                 index = (i * RTE_RETA_GROUP_SIZE) + j;
543                                 rss_state->itable[index] = reta_conf[i].reta[j];
544                         }
545                 }
546         }
547
548         rss_state->itable_size = LIO_RSS_MAX_TABLE_SZ;
549         memcpy(rss_param->itable, rss_state->itable, rss_state->itable_size);
550
551         lio_swap_8B_data((uint64_t *)rss_param, LIO_RSS_PARAM_SIZE >> 3);
552
553         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
554                 lio_dev_err(lio_dev, "Failed to set rss hash\n");
555                 return -1;
556         }
557
558         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
559                 lio_dev_err(lio_dev, "Set rss hash timed out\n");
560                 return -1;
561         }
562
563         return 0;
564 }
565
566 static int
567 lio_dev_rss_reta_query(struct rte_eth_dev *eth_dev,
568                        struct rte_eth_rss_reta_entry64 *reta_conf,
569                        uint16_t reta_size)
570 {
571         struct lio_device *lio_dev = LIO_DEV(eth_dev);
572         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
573         int i, num;
574
575         if (reta_size != LIO_RSS_MAX_TABLE_SZ) {
576                 lio_dev_err(lio_dev,
577                             "The size of hash lookup table configured (%d) doesn't match the number hardware can supported (%d)\n",
578                             reta_size, LIO_RSS_MAX_TABLE_SZ);
579                 return -EINVAL;
580         }
581
582         num = reta_size / RTE_RETA_GROUP_SIZE;
583
584         for (i = 0; i < num; i++) {
585                 memcpy(reta_conf->reta,
586                        &rss_state->itable[i * RTE_RETA_GROUP_SIZE],
587                        RTE_RETA_GROUP_SIZE);
588                 reta_conf++;
589         }
590
591         return 0;
592 }
593
594 static int
595 lio_dev_rss_hash_conf_get(struct rte_eth_dev *eth_dev,
596                           struct rte_eth_rss_conf *rss_conf)
597 {
598         struct lio_device *lio_dev = LIO_DEV(eth_dev);
599         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
600         uint8_t *hash_key = NULL;
601         uint64_t rss_hf = 0;
602
603         if (rss_state->hash_disable) {
604                 lio_dev_info(lio_dev, "RSS disabled in nic\n");
605                 rss_conf->rss_hf = 0;
606                 return 0;
607         }
608
609         /* Get key value */
610         hash_key = rss_conf->rss_key;
611         if (hash_key != NULL)
612                 memcpy(hash_key, rss_state->hash_key, rss_state->hash_key_size);
613
614         if (rss_state->ip)
615                 rss_hf |= ETH_RSS_IPV4;
616         if (rss_state->tcp_hash)
617                 rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
618         if (rss_state->ipv6)
619                 rss_hf |= ETH_RSS_IPV6;
620         if (rss_state->ipv6_tcp_hash)
621                 rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
622         if (rss_state->ipv6_ex)
623                 rss_hf |= ETH_RSS_IPV6_EX;
624         if (rss_state->ipv6_tcp_ex_hash)
625                 rss_hf |= ETH_RSS_IPV6_TCP_EX;
626
627         rss_conf->rss_hf = rss_hf;
628
629         return 0;
630 }
631
632 static int
633 lio_dev_rss_hash_update(struct rte_eth_dev *eth_dev,
634                         struct rte_eth_rss_conf *rss_conf)
635 {
636         struct lio_device *lio_dev = LIO_DEV(eth_dev);
637         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
638         struct lio_rss_set *rss_param;
639         struct lio_dev_ctrl_cmd ctrl_cmd;
640         struct lio_ctrl_pkt ctrl_pkt;
641
642         if (!lio_dev->intf_open) {
643                 lio_dev_err(lio_dev, "Port %d down, can't update hash\n",
644                             lio_dev->port_id);
645                 return -EINVAL;
646         }
647
648         /* flush added to prevent cmd failure
649          * incase the queue is full
650          */
651         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
652
653         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
654         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
655
656         rss_param = (struct lio_rss_set *)&ctrl_pkt.udd[0];
657
658         ctrl_cmd.eth_dev = eth_dev;
659         ctrl_cmd.cond = 0;
660
661         ctrl_pkt.ncmd.s.cmd = LIO_CMD_SET_RSS;
662         ctrl_pkt.ncmd.s.more = sizeof(struct lio_rss_set) >> 3;
663         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
664
665         rss_param->param.flags = 0xF;
666
667         if (rss_conf->rss_key) {
668                 rss_param->param.flags &= ~LIO_RSS_PARAM_HASH_KEY_UNCHANGED;
669                 rss_state->hash_key_size = LIO_RSS_MAX_KEY_SZ;
670                 rss_param->param.hashkeysize = LIO_RSS_MAX_KEY_SZ;
671                 memcpy(rss_state->hash_key, rss_conf->rss_key,
672                        rss_state->hash_key_size);
673                 memcpy(rss_param->key, rss_state->hash_key,
674                        rss_state->hash_key_size);
675         }
676
677         if ((rss_conf->rss_hf & LIO_RSS_OFFLOAD_ALL) == 0) {
678                 /* Can't disable rss through hash flags,
679                  * if it is enabled by default during init
680                  */
681                 if (!rss_state->hash_disable)
682                         return -EINVAL;
683
684                 /* This is for --disable-rss during testpmd launch */
685                 rss_param->param.flags |= LIO_RSS_PARAM_DISABLE_RSS;
686         } else {
687                 uint32_t hashinfo = 0;
688
689                 /* Can't enable rss if disabled by default during init */
690                 if (rss_state->hash_disable)
691                         return -EINVAL;
692
693                 if (rss_conf->rss_hf & ETH_RSS_IPV4) {
694                         hashinfo |= LIO_RSS_HASH_IPV4;
695                         rss_state->ip = 1;
696                 } else {
697                         rss_state->ip = 0;
698                 }
699
700                 if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP) {
701                         hashinfo |= LIO_RSS_HASH_TCP_IPV4;
702                         rss_state->tcp_hash = 1;
703                 } else {
704                         rss_state->tcp_hash = 0;
705                 }
706
707                 if (rss_conf->rss_hf & ETH_RSS_IPV6) {
708                         hashinfo |= LIO_RSS_HASH_IPV6;
709                         rss_state->ipv6 = 1;
710                 } else {
711                         rss_state->ipv6 = 0;
712                 }
713
714                 if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP) {
715                         hashinfo |= LIO_RSS_HASH_TCP_IPV6;
716                         rss_state->ipv6_tcp_hash = 1;
717                 } else {
718                         rss_state->ipv6_tcp_hash = 0;
719                 }
720
721                 if (rss_conf->rss_hf & ETH_RSS_IPV6_EX) {
722                         hashinfo |= LIO_RSS_HASH_IPV6_EX;
723                         rss_state->ipv6_ex = 1;
724                 } else {
725                         rss_state->ipv6_ex = 0;
726                 }
727
728                 if (rss_conf->rss_hf & ETH_RSS_IPV6_TCP_EX) {
729                         hashinfo |= LIO_RSS_HASH_TCP_IPV6_EX;
730                         rss_state->ipv6_tcp_ex_hash = 1;
731                 } else {
732                         rss_state->ipv6_tcp_ex_hash = 0;
733                 }
734
735                 rss_param->param.flags &= ~LIO_RSS_PARAM_HASH_INFO_UNCHANGED;
736                 rss_param->param.hashinfo = hashinfo;
737         }
738
739         lio_swap_8B_data((uint64_t *)rss_param, LIO_RSS_PARAM_SIZE >> 3);
740
741         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
742                 lio_dev_err(lio_dev, "Failed to set rss hash\n");
743                 return -1;
744         }
745
746         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
747                 lio_dev_err(lio_dev, "Set rss hash timed out\n");
748                 return -1;
749         }
750
751         return 0;
752 }
753
754 /**
755  * Add vxlan dest udp port for an interface.
756  *
757  * @param eth_dev
758  *  Pointer to the structure rte_eth_dev
759  * @param udp_tnl
760  *  udp tunnel conf
761  *
762  * @return
763  *  On success return 0
764  *  On failure return -1
765  */
766 static int
767 lio_dev_udp_tunnel_add(struct rte_eth_dev *eth_dev,
768                        struct rte_eth_udp_tunnel *udp_tnl)
769 {
770         struct lio_device *lio_dev = LIO_DEV(eth_dev);
771         struct lio_dev_ctrl_cmd ctrl_cmd;
772         struct lio_ctrl_pkt ctrl_pkt;
773
774         if (udp_tnl == NULL)
775                 return -EINVAL;
776
777         if (udp_tnl->prot_type != RTE_TUNNEL_TYPE_VXLAN) {
778                 lio_dev_err(lio_dev, "Unsupported tunnel type\n");
779                 return -1;
780         }
781
782         /* flush added to prevent cmd failure
783          * incase the queue is full
784          */
785         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
786
787         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
788         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
789
790         ctrl_cmd.eth_dev = eth_dev;
791         ctrl_cmd.cond = 0;
792
793         ctrl_pkt.ncmd.s.cmd = LIO_CMD_VXLAN_PORT_CONFIG;
794         ctrl_pkt.ncmd.s.param1 = udp_tnl->udp_port;
795         ctrl_pkt.ncmd.s.more = LIO_CMD_VXLAN_PORT_ADD;
796         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
797
798         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
799                 lio_dev_err(lio_dev, "Failed to send VXLAN_PORT_ADD command\n");
800                 return -1;
801         }
802
803         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
804                 lio_dev_err(lio_dev, "VXLAN_PORT_ADD command timed out\n");
805                 return -1;
806         }
807
808         return 0;
809 }
810
811 /**
812  * Remove vxlan dest udp port for an interface.
813  *
814  * @param eth_dev
815  *  Pointer to the structure rte_eth_dev
816  * @param udp_tnl
817  *  udp tunnel conf
818  *
819  * @return
820  *  On success return 0
821  *  On failure return -1
822  */
823 static int
824 lio_dev_udp_tunnel_del(struct rte_eth_dev *eth_dev,
825                        struct rte_eth_udp_tunnel *udp_tnl)
826 {
827         struct lio_device *lio_dev = LIO_DEV(eth_dev);
828         struct lio_dev_ctrl_cmd ctrl_cmd;
829         struct lio_ctrl_pkt ctrl_pkt;
830
831         if (udp_tnl == NULL)
832                 return -EINVAL;
833
834         if (udp_tnl->prot_type != RTE_TUNNEL_TYPE_VXLAN) {
835                 lio_dev_err(lio_dev, "Unsupported tunnel type\n");
836                 return -1;
837         }
838
839         /* flush added to prevent cmd failure
840          * incase the queue is full
841          */
842         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
843
844         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
845         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
846
847         ctrl_cmd.eth_dev = eth_dev;
848         ctrl_cmd.cond = 0;
849
850         ctrl_pkt.ncmd.s.cmd = LIO_CMD_VXLAN_PORT_CONFIG;
851         ctrl_pkt.ncmd.s.param1 = udp_tnl->udp_port;
852         ctrl_pkt.ncmd.s.more = LIO_CMD_VXLAN_PORT_DEL;
853         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
854
855         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
856                 lio_dev_err(lio_dev, "Failed to send VXLAN_PORT_DEL command\n");
857                 return -1;
858         }
859
860         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
861                 lio_dev_err(lio_dev, "VXLAN_PORT_DEL command timed out\n");
862                 return -1;
863         }
864
865         return 0;
866 }
867
868 static int
869 lio_dev_vlan_filter_set(struct rte_eth_dev *eth_dev, uint16_t vlan_id, int on)
870 {
871         struct lio_device *lio_dev = LIO_DEV(eth_dev);
872         struct lio_dev_ctrl_cmd ctrl_cmd;
873         struct lio_ctrl_pkt ctrl_pkt;
874
875         if (lio_dev->linfo.vlan_is_admin_assigned)
876                 return -EPERM;
877
878         /* flush added to prevent cmd failure
879          * incase the queue is full
880          */
881         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
882
883         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
884         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
885
886         ctrl_cmd.eth_dev = eth_dev;
887         ctrl_cmd.cond = 0;
888
889         ctrl_pkt.ncmd.s.cmd = on ?
890                         LIO_CMD_ADD_VLAN_FILTER : LIO_CMD_DEL_VLAN_FILTER;
891         ctrl_pkt.ncmd.s.param1 = vlan_id;
892         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
893
894         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
895                 lio_dev_err(lio_dev, "Failed to %s VLAN port\n",
896                             on ? "add" : "remove");
897                 return -1;
898         }
899
900         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
901                 lio_dev_err(lio_dev, "Command to %s VLAN port timed out\n",
902                             on ? "add" : "remove");
903                 return -1;
904         }
905
906         return 0;
907 }
908
909 static uint64_t
910 lio_hweight64(uint64_t w)
911 {
912         uint64_t res = w - ((w >> 1) & 0x5555555555555555ul);
913
914         res =
915             (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
916         res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
917         res = res + (res >> 8);
918         res = res + (res >> 16);
919
920         return (res + (res >> 32)) & 0x00000000000000FFul;
921 }
922
923 static int
924 lio_dev_link_update(struct rte_eth_dev *eth_dev,
925                     int wait_to_complete __rte_unused)
926 {
927         struct lio_device *lio_dev = LIO_DEV(eth_dev);
928         struct rte_eth_link link;
929
930         /* Initialize */
931         memset(&link, 0, sizeof(link));
932         link.link_status = ETH_LINK_DOWN;
933         link.link_speed = ETH_SPEED_NUM_NONE;
934         link.link_duplex = ETH_LINK_HALF_DUPLEX;
935         link.link_autoneg = ETH_LINK_AUTONEG;
936
937         /* Return what we found */
938         if (lio_dev->linfo.link.s.link_up == 0) {
939                 /* Interface is down */
940                 return rte_eth_linkstatus_set(eth_dev, &link);
941         }
942
943         link.link_status = ETH_LINK_UP; /* Interface is up */
944         link.link_duplex = ETH_LINK_FULL_DUPLEX;
945         switch (lio_dev->linfo.link.s.speed) {
946         case LIO_LINK_SPEED_10000:
947                 link.link_speed = ETH_SPEED_NUM_10G;
948                 break;
949         case LIO_LINK_SPEED_25000:
950                 link.link_speed = ETH_SPEED_NUM_25G;
951                 break;
952         default:
953                 link.link_speed = ETH_SPEED_NUM_NONE;
954                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
955         }
956
957         return rte_eth_linkstatus_set(eth_dev, &link);
958 }
959
960 /**
961  * \brief Net device enable, disable allmulticast
962  * @param eth_dev Pointer to the structure rte_eth_dev
963  */
964 static void
965 lio_change_dev_flag(struct rte_eth_dev *eth_dev)
966 {
967         struct lio_device *lio_dev = LIO_DEV(eth_dev);
968         struct lio_dev_ctrl_cmd ctrl_cmd;
969         struct lio_ctrl_pkt ctrl_pkt;
970
971         /* flush added to prevent cmd failure
972          * incase the queue is full
973          */
974         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
975
976         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
977         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
978
979         ctrl_cmd.eth_dev = eth_dev;
980         ctrl_cmd.cond = 0;
981
982         /* Create a ctrl pkt command to be sent to core app. */
983         ctrl_pkt.ncmd.s.cmd = LIO_CMD_CHANGE_DEVFLAGS;
984         ctrl_pkt.ncmd.s.param1 = lio_dev->ifflags;
985         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
986
987         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
988                 lio_dev_err(lio_dev, "Failed to send change flag message\n");
989                 return;
990         }
991
992         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd))
993                 lio_dev_err(lio_dev, "Change dev flag command timed out\n");
994 }
995
996 static void
997 lio_dev_promiscuous_enable(struct rte_eth_dev *eth_dev)
998 {
999         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1000
1001         if (strcmp(lio_dev->firmware_version, LIO_VF_TRUST_MIN_VERSION) < 0) {
1002                 lio_dev_err(lio_dev, "Require firmware version >= %s\n",
1003                             LIO_VF_TRUST_MIN_VERSION);
1004                 return;
1005         }
1006
1007         if (!lio_dev->intf_open) {
1008                 lio_dev_err(lio_dev, "Port %d down, can't enable promiscuous\n",
1009                             lio_dev->port_id);
1010                 return;
1011         }
1012
1013         lio_dev->ifflags |= LIO_IFFLAG_PROMISC;
1014         lio_change_dev_flag(eth_dev);
1015 }
1016
1017 static void
1018 lio_dev_promiscuous_disable(struct rte_eth_dev *eth_dev)
1019 {
1020         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1021
1022         if (strcmp(lio_dev->firmware_version, LIO_VF_TRUST_MIN_VERSION) < 0) {
1023                 lio_dev_err(lio_dev, "Require firmware version >= %s\n",
1024                             LIO_VF_TRUST_MIN_VERSION);
1025                 return;
1026         }
1027
1028         if (!lio_dev->intf_open) {
1029                 lio_dev_err(lio_dev, "Port %d down, can't disable promiscuous\n",
1030                             lio_dev->port_id);
1031                 return;
1032         }
1033
1034         lio_dev->ifflags &= ~LIO_IFFLAG_PROMISC;
1035         lio_change_dev_flag(eth_dev);
1036 }
1037
1038 static void
1039 lio_dev_allmulticast_enable(struct rte_eth_dev *eth_dev)
1040 {
1041         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1042
1043         if (!lio_dev->intf_open) {
1044                 lio_dev_err(lio_dev, "Port %d down, can't enable multicast\n",
1045                             lio_dev->port_id);
1046                 return;
1047         }
1048
1049         lio_dev->ifflags |= LIO_IFFLAG_ALLMULTI;
1050         lio_change_dev_flag(eth_dev);
1051 }
1052
1053 static void
1054 lio_dev_allmulticast_disable(struct rte_eth_dev *eth_dev)
1055 {
1056         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1057
1058         if (!lio_dev->intf_open) {
1059                 lio_dev_err(lio_dev, "Port %d down, can't disable multicast\n",
1060                             lio_dev->port_id);
1061                 return;
1062         }
1063
1064         lio_dev->ifflags &= ~LIO_IFFLAG_ALLMULTI;
1065         lio_change_dev_flag(eth_dev);
1066 }
1067
1068 static void
1069 lio_dev_rss_configure(struct rte_eth_dev *eth_dev)
1070 {
1071         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1072         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
1073         struct rte_eth_rss_reta_entry64 reta_conf[8];
1074         struct rte_eth_rss_conf rss_conf;
1075         uint16_t i;
1076
1077         /* Configure the RSS key and the RSS protocols used to compute
1078          * the RSS hash of input packets.
1079          */
1080         rss_conf = eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
1081         if ((rss_conf.rss_hf & LIO_RSS_OFFLOAD_ALL) == 0) {
1082                 rss_state->hash_disable = 1;
1083                 lio_dev_rss_hash_update(eth_dev, &rss_conf);
1084                 return;
1085         }
1086
1087         if (rss_conf.rss_key == NULL)
1088                 rss_conf.rss_key = lio_rss_key; /* Default hash key */
1089
1090         lio_dev_rss_hash_update(eth_dev, &rss_conf);
1091
1092         memset(reta_conf, 0, sizeof(reta_conf));
1093         for (i = 0; i < LIO_RSS_MAX_TABLE_SZ; i++) {
1094                 uint8_t q_idx, conf_idx, reta_idx;
1095
1096                 q_idx = (uint8_t)((eth_dev->data->nb_rx_queues > 1) ?
1097                                   i % eth_dev->data->nb_rx_queues : 0);
1098                 conf_idx = i / RTE_RETA_GROUP_SIZE;
1099                 reta_idx = i % RTE_RETA_GROUP_SIZE;
1100                 reta_conf[conf_idx].reta[reta_idx] = q_idx;
1101                 reta_conf[conf_idx].mask |= ((uint64_t)1 << reta_idx);
1102         }
1103
1104         lio_dev_rss_reta_update(eth_dev, reta_conf, LIO_RSS_MAX_TABLE_SZ);
1105 }
1106
1107 static void
1108 lio_dev_mq_rx_configure(struct rte_eth_dev *eth_dev)
1109 {
1110         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1111         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
1112         struct rte_eth_rss_conf rss_conf;
1113
1114         switch (eth_dev->data->dev_conf.rxmode.mq_mode) {
1115         case ETH_MQ_RX_RSS:
1116                 lio_dev_rss_configure(eth_dev);
1117                 break;
1118         case ETH_MQ_RX_NONE:
1119         /* if mq_mode is none, disable rss mode. */
1120         default:
1121                 memset(&rss_conf, 0, sizeof(rss_conf));
1122                 rss_state->hash_disable = 1;
1123                 lio_dev_rss_hash_update(eth_dev, &rss_conf);
1124         }
1125 }
1126
1127 /**
1128  * Setup our receive queue/ringbuffer. This is the
1129  * queue the Octeon uses to send us packets and
1130  * responses. We are given a memory pool for our
1131  * packet buffers that are used to populate the receive
1132  * queue.
1133  *
1134  * @param eth_dev
1135  *    Pointer to the structure rte_eth_dev
1136  * @param q_no
1137  *    Queue number
1138  * @param num_rx_descs
1139  *    Number of entries in the queue
1140  * @param socket_id
1141  *    Where to allocate memory
1142  * @param rx_conf
1143  *    Pointer to the struction rte_eth_rxconf
1144  * @param mp
1145  *    Pointer to the packet pool
1146  *
1147  * @return
1148  *    - On success, return 0
1149  *    - On failure, return -1
1150  */
1151 static int
1152 lio_dev_rx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
1153                        uint16_t num_rx_descs, unsigned int socket_id,
1154                        const struct rte_eth_rxconf *rx_conf __rte_unused,
1155                        struct rte_mempool *mp)
1156 {
1157         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1158         struct rte_pktmbuf_pool_private *mbp_priv;
1159         uint32_t fw_mapped_oq;
1160         uint16_t buf_size;
1161
1162         if (q_no >= lio_dev->nb_rx_queues) {
1163                 lio_dev_err(lio_dev, "Invalid rx queue number %u\n", q_no);
1164                 return -EINVAL;
1165         }
1166
1167         lio_dev_dbg(lio_dev, "setting up rx queue %u\n", q_no);
1168
1169         fw_mapped_oq = lio_dev->linfo.rxpciq[q_no].s.q_no;
1170
1171         /* Free previous allocation if any */
1172         if (eth_dev->data->rx_queues[q_no] != NULL) {
1173                 lio_dev_rx_queue_release(eth_dev->data->rx_queues[q_no]);
1174                 eth_dev->data->rx_queues[q_no] = NULL;
1175         }
1176
1177         mbp_priv = rte_mempool_get_priv(mp);
1178         buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
1179
1180         if (lio_setup_droq(lio_dev, fw_mapped_oq, num_rx_descs, buf_size, mp,
1181                            socket_id)) {
1182                 lio_dev_err(lio_dev, "droq allocation failed\n");
1183                 return -1;
1184         }
1185
1186         eth_dev->data->rx_queues[q_no] = lio_dev->droq[fw_mapped_oq];
1187
1188         return 0;
1189 }
1190
1191 /**
1192  * Release the receive queue/ringbuffer. Called by
1193  * the upper layers.
1194  *
1195  * @param rxq
1196  *    Opaque pointer to the receive queue to release
1197  *
1198  * @return
1199  *    - nothing
1200  */
1201 void
1202 lio_dev_rx_queue_release(void *rxq)
1203 {
1204         struct lio_droq *droq = rxq;
1205         int oq_no;
1206
1207         if (droq) {
1208                 oq_no = droq->q_no;
1209                 lio_delete_droq_queue(droq->lio_dev, oq_no);
1210         }
1211 }
1212
1213 /**
1214  * Allocate and initialize SW ring. Initialize associated HW registers.
1215  *
1216  * @param eth_dev
1217  *   Pointer to structure rte_eth_dev
1218  *
1219  * @param q_no
1220  *   Queue number
1221  *
1222  * @param num_tx_descs
1223  *   Number of ringbuffer descriptors
1224  *
1225  * @param socket_id
1226  *   NUMA socket id, used for memory allocations
1227  *
1228  * @param tx_conf
1229  *   Pointer to the structure rte_eth_txconf
1230  *
1231  * @return
1232  *   - On success, return 0
1233  *   - On failure, return -errno value
1234  */
1235 static int
1236 lio_dev_tx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
1237                        uint16_t num_tx_descs, unsigned int socket_id,
1238                        const struct rte_eth_txconf *tx_conf __rte_unused)
1239 {
1240         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1241         int fw_mapped_iq = lio_dev->linfo.txpciq[q_no].s.q_no;
1242         int retval;
1243
1244         if (q_no >= lio_dev->nb_tx_queues) {
1245                 lio_dev_err(lio_dev, "Invalid tx queue number %u\n", q_no);
1246                 return -EINVAL;
1247         }
1248
1249         lio_dev_dbg(lio_dev, "setting up tx queue %u\n", q_no);
1250
1251         /* Free previous allocation if any */
1252         if (eth_dev->data->tx_queues[q_no] != NULL) {
1253                 lio_dev_tx_queue_release(eth_dev->data->tx_queues[q_no]);
1254                 eth_dev->data->tx_queues[q_no] = NULL;
1255         }
1256
1257         retval = lio_setup_iq(lio_dev, q_no, lio_dev->linfo.txpciq[q_no],
1258                               num_tx_descs, lio_dev, socket_id);
1259
1260         if (retval) {
1261                 lio_dev_err(lio_dev, "Runtime IQ(TxQ) creation failed.\n");
1262                 return retval;
1263         }
1264
1265         retval = lio_setup_sglists(lio_dev, q_no, fw_mapped_iq,
1266                                 lio_dev->instr_queue[fw_mapped_iq]->nb_desc,
1267                                 socket_id);
1268
1269         if (retval) {
1270                 lio_delete_instruction_queue(lio_dev, fw_mapped_iq);
1271                 return retval;
1272         }
1273
1274         eth_dev->data->tx_queues[q_no] = lio_dev->instr_queue[fw_mapped_iq];
1275
1276         return 0;
1277 }
1278
1279 /**
1280  * Release the transmit queue/ringbuffer. Called by
1281  * the upper layers.
1282  *
1283  * @param txq
1284  *    Opaque pointer to the transmit queue to release
1285  *
1286  * @return
1287  *    - nothing
1288  */
1289 void
1290 lio_dev_tx_queue_release(void *txq)
1291 {
1292         struct lio_instr_queue *tq = txq;
1293         uint32_t fw_mapped_iq_no;
1294
1295
1296         if (tq) {
1297                 /* Free sg_list */
1298                 lio_delete_sglist(tq);
1299
1300                 fw_mapped_iq_no = tq->txpciq.s.q_no;
1301                 lio_delete_instruction_queue(tq->lio_dev, fw_mapped_iq_no);
1302         }
1303 }
1304
1305 /**
1306  * Api to check link state.
1307  */
1308 static void
1309 lio_dev_get_link_status(struct rte_eth_dev *eth_dev)
1310 {
1311         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1312         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
1313         struct lio_link_status_resp *resp;
1314         union octeon_link_status *ls;
1315         struct lio_soft_command *sc;
1316         uint32_t resp_size;
1317
1318         if (!lio_dev->intf_open)
1319                 return;
1320
1321         resp_size = sizeof(struct lio_link_status_resp);
1322         sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
1323         if (sc == NULL)
1324                 return;
1325
1326         resp = (struct lio_link_status_resp *)sc->virtrptr;
1327         lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
1328                                  LIO_OPCODE_INFO, 0, 0, 0);
1329
1330         /* Setting wait time in seconds */
1331         sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
1332
1333         if (lio_send_soft_command(lio_dev, sc) == LIO_IQ_SEND_FAILED)
1334                 goto get_status_fail;
1335
1336         while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
1337                 lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
1338                 rte_delay_ms(1);
1339         }
1340
1341         if (resp->status)
1342                 goto get_status_fail;
1343
1344         ls = &resp->link_info.link;
1345
1346         lio_swap_8B_data((uint64_t *)ls, sizeof(union octeon_link_status) >> 3);
1347
1348         if (lio_dev->linfo.link.link_status64 != ls->link_status64) {
1349                 if (ls->s.mtu < eth_dev->data->mtu) {
1350                         lio_dev_info(lio_dev, "Lowered VF MTU to %d as PF MTU dropped\n",
1351                                      ls->s.mtu);
1352                         eth_dev->data->mtu = ls->s.mtu;
1353                 }
1354                 lio_dev->linfo.link.link_status64 = ls->link_status64;
1355                 lio_dev_link_update(eth_dev, 0);
1356         }
1357
1358         lio_free_soft_command(sc);
1359
1360         return;
1361
1362 get_status_fail:
1363         lio_free_soft_command(sc);
1364 }
1365
1366 /* This function will be invoked every LSC_TIMEOUT ns (100ms)
1367  * and will update link state if it changes.
1368  */
1369 static void
1370 lio_sync_link_state_check(void *eth_dev)
1371 {
1372         struct lio_device *lio_dev =
1373                 (((struct rte_eth_dev *)eth_dev)->data->dev_private);
1374
1375         if (lio_dev->port_configured)
1376                 lio_dev_get_link_status(eth_dev);
1377
1378         /* Schedule periodic link status check.
1379          * Stop check if interface is close and start again while opening.
1380          */
1381         if (lio_dev->intf_open)
1382                 rte_eal_alarm_set(LIO_LSC_TIMEOUT, lio_sync_link_state_check,
1383                                   eth_dev);
1384 }
1385
1386 static int
1387 lio_dev_start(struct rte_eth_dev *eth_dev)
1388 {
1389         uint16_t mtu;
1390         uint32_t frame_len = eth_dev->data->dev_conf.rxmode.max_rx_pkt_len;
1391         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1392         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
1393         int ret = 0;
1394
1395         lio_dev_info(lio_dev, "Starting port %d\n", eth_dev->data->port_id);
1396
1397         if (lio_dev->fn_list.enable_io_queues(lio_dev))
1398                 return -1;
1399
1400         if (lio_send_rx_ctrl_cmd(eth_dev, 1))
1401                 return -1;
1402
1403         /* Ready for link status updates */
1404         lio_dev->intf_open = 1;
1405         rte_mb();
1406
1407         /* Configure RSS if device configured with multiple RX queues. */
1408         lio_dev_mq_rx_configure(eth_dev);
1409
1410         /* start polling for lsc */
1411         ret = rte_eal_alarm_set(LIO_LSC_TIMEOUT,
1412                                 lio_sync_link_state_check,
1413                                 eth_dev);
1414         if (ret) {
1415                 lio_dev_err(lio_dev,
1416                             "link state check handler creation failed\n");
1417                 goto dev_lsc_handle_error;
1418         }
1419
1420         while ((lio_dev->linfo.link.link_status64 == 0) && (--timeout))
1421                 rte_delay_ms(1);
1422
1423         if (lio_dev->linfo.link.link_status64 == 0) {
1424                 ret = -1;
1425                 goto dev_mtu_set_error;
1426         }
1427
1428         mtu = (uint16_t)(frame_len - ETHER_HDR_LEN - ETHER_CRC_LEN);
1429         if (mtu < ETHER_MIN_MTU)
1430                 mtu = ETHER_MIN_MTU;
1431
1432         if (eth_dev->data->mtu != mtu) {
1433                 ret = lio_dev_mtu_set(eth_dev, mtu);
1434                 if (ret)
1435                         goto dev_mtu_set_error;
1436         }
1437
1438         return 0;
1439
1440 dev_mtu_set_error:
1441         rte_eal_alarm_cancel(lio_sync_link_state_check, eth_dev);
1442
1443 dev_lsc_handle_error:
1444         lio_dev->intf_open = 0;
1445         lio_send_rx_ctrl_cmd(eth_dev, 0);
1446
1447         return ret;
1448 }
1449
1450 /* Stop device and disable input/output functions */
1451 static void
1452 lio_dev_stop(struct rte_eth_dev *eth_dev)
1453 {
1454         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1455
1456         lio_dev_info(lio_dev, "Stopping port %d\n", eth_dev->data->port_id);
1457         lio_dev->intf_open = 0;
1458         rte_mb();
1459
1460         /* Cancel callback if still running. */
1461         rte_eal_alarm_cancel(lio_sync_link_state_check, eth_dev);
1462
1463         lio_send_rx_ctrl_cmd(eth_dev, 0);
1464
1465         lio_wait_for_instr_fetch(lio_dev);
1466
1467         /* Clear recorded link status */
1468         lio_dev->linfo.link.link_status64 = 0;
1469 }
1470
1471 static int
1472 lio_dev_set_link_up(struct rte_eth_dev *eth_dev)
1473 {
1474         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1475
1476         if (!lio_dev->intf_open) {
1477                 lio_dev_info(lio_dev, "Port is stopped, Start the port first\n");
1478                 return 0;
1479         }
1480
1481         if (lio_dev->linfo.link.s.link_up) {
1482                 lio_dev_info(lio_dev, "Link is already UP\n");
1483                 return 0;
1484         }
1485
1486         if (lio_send_rx_ctrl_cmd(eth_dev, 1)) {
1487                 lio_dev_err(lio_dev, "Unable to set Link UP\n");
1488                 return -1;
1489         }
1490
1491         lio_dev->linfo.link.s.link_up = 1;
1492         eth_dev->data->dev_link.link_status = ETH_LINK_UP;
1493
1494         return 0;
1495 }
1496
1497 static int
1498 lio_dev_set_link_down(struct rte_eth_dev *eth_dev)
1499 {
1500         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1501
1502         if (!lio_dev->intf_open) {
1503                 lio_dev_info(lio_dev, "Port is stopped, Start the port first\n");
1504                 return 0;
1505         }
1506
1507         if (!lio_dev->linfo.link.s.link_up) {
1508                 lio_dev_info(lio_dev, "Link is already DOWN\n");
1509                 return 0;
1510         }
1511
1512         lio_dev->linfo.link.s.link_up = 0;
1513         eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
1514
1515         if (lio_send_rx_ctrl_cmd(eth_dev, 0)) {
1516                 lio_dev->linfo.link.s.link_up = 1;
1517                 eth_dev->data->dev_link.link_status = ETH_LINK_UP;
1518                 lio_dev_err(lio_dev, "Unable to set Link Down\n");
1519                 return -1;
1520         }
1521
1522         return 0;
1523 }
1524
1525 /**
1526  * Reset and stop the device. This occurs on the first
1527  * call to this routine. Subsequent calls will simply
1528  * return. NB: This will require the NIC to be rebooted.
1529  *
1530  * @param eth_dev
1531  *    Pointer to the structure rte_eth_dev
1532  *
1533  * @return
1534  *    - nothing
1535  */
1536 static void
1537 lio_dev_close(struct rte_eth_dev *eth_dev)
1538 {
1539         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1540
1541         lio_dev_info(lio_dev, "closing port %d\n", eth_dev->data->port_id);
1542
1543         if (lio_dev->intf_open)
1544                 lio_dev_stop(eth_dev);
1545
1546         /* Reset ioq regs */
1547         lio_dev->fn_list.setup_device_regs(lio_dev);
1548
1549         if (lio_dev->pci_dev->kdrv == RTE_KDRV_IGB_UIO) {
1550                 cn23xx_vf_ask_pf_to_do_flr(lio_dev);
1551                 rte_delay_ms(LIO_PCI_FLR_WAIT);
1552         }
1553
1554         /* lio_free_mbox */
1555         lio_dev->fn_list.free_mbox(lio_dev);
1556
1557         /* Free glist resources */
1558         rte_free(lio_dev->glist_head);
1559         rte_free(lio_dev->glist_lock);
1560         lio_dev->glist_head = NULL;
1561         lio_dev->glist_lock = NULL;
1562
1563         lio_dev->port_configured = 0;
1564
1565          /* Delete all queues */
1566         lio_dev_clear_queues(eth_dev);
1567 }
1568
1569 /**
1570  * Enable tunnel rx checksum verification from firmware.
1571  */
1572 static void
1573 lio_enable_hw_tunnel_rx_checksum(struct rte_eth_dev *eth_dev)
1574 {
1575         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1576         struct lio_dev_ctrl_cmd ctrl_cmd;
1577         struct lio_ctrl_pkt ctrl_pkt;
1578
1579         /* flush added to prevent cmd failure
1580          * incase the queue is full
1581          */
1582         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
1583
1584         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
1585         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
1586
1587         ctrl_cmd.eth_dev = eth_dev;
1588         ctrl_cmd.cond = 0;
1589
1590         ctrl_pkt.ncmd.s.cmd = LIO_CMD_TNL_RX_CSUM_CTL;
1591         ctrl_pkt.ncmd.s.param1 = LIO_CMD_RXCSUM_ENABLE;
1592         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
1593
1594         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
1595                 lio_dev_err(lio_dev, "Failed to send TNL_RX_CSUM command\n");
1596                 return;
1597         }
1598
1599         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd))
1600                 lio_dev_err(lio_dev, "TNL_RX_CSUM command timed out\n");
1601 }
1602
1603 /**
1604  * Enable checksum calculation for inner packet in a tunnel.
1605  */
1606 static void
1607 lio_enable_hw_tunnel_tx_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_TX_CSUM_CTL;
1625         ctrl_pkt.ncmd.s.param1 = LIO_CMD_TXCSUM_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_TX_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_TX_CSUM command timed out\n");
1635 }
1636
1637 static int
1638 lio_send_queue_count_update(struct rte_eth_dev *eth_dev, int num_txq,
1639                             int num_rxq)
1640 {
1641         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1642         struct lio_dev_ctrl_cmd ctrl_cmd;
1643         struct lio_ctrl_pkt ctrl_pkt;
1644
1645         if (strcmp(lio_dev->firmware_version, LIO_Q_RECONF_MIN_VERSION) < 0) {
1646                 lio_dev_err(lio_dev, "Require firmware version >= %s\n",
1647                             LIO_Q_RECONF_MIN_VERSION);
1648                 return -ENOTSUP;
1649         }
1650
1651         /* flush added to prevent cmd failure
1652          * incase the queue is full
1653          */
1654         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
1655
1656         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
1657         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
1658
1659         ctrl_cmd.eth_dev = eth_dev;
1660         ctrl_cmd.cond = 0;
1661
1662         ctrl_pkt.ncmd.s.cmd = LIO_CMD_QUEUE_COUNT_CTL;
1663         ctrl_pkt.ncmd.s.param1 = num_txq;
1664         ctrl_pkt.ncmd.s.param2 = num_rxq;
1665         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
1666
1667         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
1668                 lio_dev_err(lio_dev, "Failed to send queue count control command\n");
1669                 return -1;
1670         }
1671
1672         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
1673                 lio_dev_err(lio_dev, "Queue count control command timed out\n");
1674                 return -1;
1675         }
1676
1677         return 0;
1678 }
1679
1680 static int
1681 lio_reconf_queues(struct rte_eth_dev *eth_dev, int num_txq, int num_rxq)
1682 {
1683         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1684
1685         if (lio_dev->nb_rx_queues != num_rxq ||
1686             lio_dev->nb_tx_queues != num_txq) {
1687                 if (lio_send_queue_count_update(eth_dev, num_txq, num_rxq))
1688                         return -1;
1689                 lio_dev->nb_rx_queues = num_rxq;
1690                 lio_dev->nb_tx_queues = num_txq;
1691         }
1692
1693         if (lio_dev->intf_open)
1694                 lio_dev_stop(eth_dev);
1695
1696         /* Reset ioq registers */
1697         if (lio_dev->fn_list.setup_device_regs(lio_dev)) {
1698                 lio_dev_err(lio_dev, "Failed to configure device registers\n");
1699                 return -1;
1700         }
1701
1702         return 0;
1703 }
1704
1705 static int
1706 lio_dev_configure(struct rte_eth_dev *eth_dev)
1707 {
1708         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1709         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
1710         int retval, num_iqueues, num_oqueues;
1711         uint8_t mac[ETHER_ADDR_LEN], i;
1712         struct lio_if_cfg_resp *resp;
1713         struct lio_soft_command *sc;
1714         union lio_if_cfg if_cfg;
1715         uint32_t resp_size;
1716
1717         PMD_INIT_FUNC_TRACE();
1718
1719         /* Inform firmware about change in number of queues to use.
1720          * Disable IO queues and reset registers for re-configuration.
1721          */
1722         if (lio_dev->port_configured)
1723                 return lio_reconf_queues(eth_dev,
1724                                          eth_dev->data->nb_tx_queues,
1725                                          eth_dev->data->nb_rx_queues);
1726
1727         lio_dev->nb_rx_queues = eth_dev->data->nb_rx_queues;
1728         lio_dev->nb_tx_queues = eth_dev->data->nb_tx_queues;
1729
1730         /* Set max number of queues which can be re-configured. */
1731         lio_dev->max_rx_queues = eth_dev->data->nb_rx_queues;
1732         lio_dev->max_tx_queues = eth_dev->data->nb_tx_queues;
1733
1734         resp_size = sizeof(struct lio_if_cfg_resp);
1735         sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
1736         if (sc == NULL)
1737                 return -ENOMEM;
1738
1739         resp = (struct lio_if_cfg_resp *)sc->virtrptr;
1740
1741         /* Firmware doesn't have capability to reconfigure the queues,
1742          * Claim all queues, and use as many required
1743          */
1744         if_cfg.if_cfg64 = 0;
1745         if_cfg.s.num_iqueues = lio_dev->nb_tx_queues;
1746         if_cfg.s.num_oqueues = lio_dev->nb_rx_queues;
1747         if_cfg.s.base_queue = 0;
1748
1749         if_cfg.s.gmx_port_id = lio_dev->pf_num;
1750
1751         lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
1752                                  LIO_OPCODE_IF_CFG, 0,
1753                                  if_cfg.if_cfg64, 0);
1754
1755         /* Setting wait time in seconds */
1756         sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
1757
1758         retval = lio_send_soft_command(lio_dev, sc);
1759         if (retval == LIO_IQ_SEND_FAILED) {
1760                 lio_dev_err(lio_dev, "iq/oq config failed status: %x\n",
1761                             retval);
1762                 /* Soft instr is freed by driver in case of failure. */
1763                 goto nic_config_fail;
1764         }
1765
1766         /* Sleep on a wait queue till the cond flag indicates that the
1767          * response arrived or timed-out.
1768          */
1769         while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
1770                 lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
1771                 lio_process_ordered_list(lio_dev);
1772                 rte_delay_ms(1);
1773         }
1774
1775         retval = resp->status;
1776         if (retval) {
1777                 lio_dev_err(lio_dev, "iq/oq config failed\n");
1778                 goto nic_config_fail;
1779         }
1780
1781         snprintf(lio_dev->firmware_version, LIO_FW_VERSION_LENGTH, "%s",
1782                  resp->cfg_info.lio_firmware_version);
1783
1784         lio_swap_8B_data((uint64_t *)(&resp->cfg_info),
1785                          sizeof(struct octeon_if_cfg_info) >> 3);
1786
1787         num_iqueues = lio_hweight64(resp->cfg_info.iqmask);
1788         num_oqueues = lio_hweight64(resp->cfg_info.oqmask);
1789
1790         if (!(num_iqueues) || !(num_oqueues)) {
1791                 lio_dev_err(lio_dev,
1792                             "Got bad iqueues (%016lx) or oqueues (%016lx) from firmware.\n",
1793                             (unsigned long)resp->cfg_info.iqmask,
1794                             (unsigned long)resp->cfg_info.oqmask);
1795                 goto nic_config_fail;
1796         }
1797
1798         lio_dev_dbg(lio_dev,
1799                     "interface %d, iqmask %016lx, oqmask %016lx, numiqueues %d, numoqueues %d\n",
1800                     eth_dev->data->port_id,
1801                     (unsigned long)resp->cfg_info.iqmask,
1802                     (unsigned long)resp->cfg_info.oqmask,
1803                     num_iqueues, num_oqueues);
1804
1805         lio_dev->linfo.num_rxpciq = num_oqueues;
1806         lio_dev->linfo.num_txpciq = num_iqueues;
1807
1808         for (i = 0; i < num_oqueues; i++) {
1809                 lio_dev->linfo.rxpciq[i].rxpciq64 =
1810                     resp->cfg_info.linfo.rxpciq[i].rxpciq64;
1811                 lio_dev_dbg(lio_dev, "index %d OQ %d\n",
1812                             i, lio_dev->linfo.rxpciq[i].s.q_no);
1813         }
1814
1815         for (i = 0; i < num_iqueues; i++) {
1816                 lio_dev->linfo.txpciq[i].txpciq64 =
1817                     resp->cfg_info.linfo.txpciq[i].txpciq64;
1818                 lio_dev_dbg(lio_dev, "index %d IQ %d\n",
1819                             i, lio_dev->linfo.txpciq[i].s.q_no);
1820         }
1821
1822         lio_dev->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
1823         lio_dev->linfo.gmxport = resp->cfg_info.linfo.gmxport;
1824         lio_dev->linfo.link.link_status64 =
1825                         resp->cfg_info.linfo.link.link_status64;
1826
1827         /* 64-bit swap required on LE machines */
1828         lio_swap_8B_data(&lio_dev->linfo.hw_addr, 1);
1829         for (i = 0; i < ETHER_ADDR_LEN; i++)
1830                 mac[i] = *((uint8_t *)(((uint8_t *)&lio_dev->linfo.hw_addr) +
1831                                        2 + i));
1832
1833         /* Copy the permanent MAC address */
1834         ether_addr_copy((struct ether_addr *)mac, &eth_dev->data->mac_addrs[0]);
1835
1836         /* enable firmware checksum support for tunnel packets */
1837         lio_enable_hw_tunnel_rx_checksum(eth_dev);
1838         lio_enable_hw_tunnel_tx_checksum(eth_dev);
1839
1840         lio_dev->glist_lock =
1841             rte_zmalloc(NULL, sizeof(*lio_dev->glist_lock) * num_iqueues, 0);
1842         if (lio_dev->glist_lock == NULL)
1843                 return -ENOMEM;
1844
1845         lio_dev->glist_head =
1846                 rte_zmalloc(NULL, sizeof(*lio_dev->glist_head) * num_iqueues,
1847                             0);
1848         if (lio_dev->glist_head == NULL) {
1849                 rte_free(lio_dev->glist_lock);
1850                 lio_dev->glist_lock = NULL;
1851                 return -ENOMEM;
1852         }
1853
1854         lio_dev_link_update(eth_dev, 0);
1855
1856         lio_dev->port_configured = 1;
1857
1858         lio_free_soft_command(sc);
1859
1860         /* Reset ioq regs */
1861         lio_dev->fn_list.setup_device_regs(lio_dev);
1862
1863         /* Free iq_0 used during init */
1864         lio_free_instr_queue0(lio_dev);
1865
1866         return 0;
1867
1868 nic_config_fail:
1869         lio_dev_err(lio_dev, "Failed retval %d\n", retval);
1870         lio_free_soft_command(sc);
1871         lio_free_instr_queue0(lio_dev);
1872
1873         return -ENODEV;
1874 }
1875
1876 /* Define our ethernet definitions */
1877 static const struct eth_dev_ops liovf_eth_dev_ops = {
1878         .dev_configure          = lio_dev_configure,
1879         .dev_start              = lio_dev_start,
1880         .dev_stop               = lio_dev_stop,
1881         .dev_set_link_up        = lio_dev_set_link_up,
1882         .dev_set_link_down      = lio_dev_set_link_down,
1883         .dev_close              = lio_dev_close,
1884         .promiscuous_enable     = lio_dev_promiscuous_enable,
1885         .promiscuous_disable    = lio_dev_promiscuous_disable,
1886         .allmulticast_enable    = lio_dev_allmulticast_enable,
1887         .allmulticast_disable   = lio_dev_allmulticast_disable,
1888         .link_update            = lio_dev_link_update,
1889         .stats_get              = lio_dev_stats_get,
1890         .xstats_get             = lio_dev_xstats_get,
1891         .xstats_get_names       = lio_dev_xstats_get_names,
1892         .stats_reset            = lio_dev_stats_reset,
1893         .xstats_reset           = lio_dev_xstats_reset,
1894         .dev_infos_get          = lio_dev_info_get,
1895         .vlan_filter_set        = lio_dev_vlan_filter_set,
1896         .rx_queue_setup         = lio_dev_rx_queue_setup,
1897         .rx_queue_release       = lio_dev_rx_queue_release,
1898         .tx_queue_setup         = lio_dev_tx_queue_setup,
1899         .tx_queue_release       = lio_dev_tx_queue_release,
1900         .reta_update            = lio_dev_rss_reta_update,
1901         .reta_query             = lio_dev_rss_reta_query,
1902         .rss_hash_conf_get      = lio_dev_rss_hash_conf_get,
1903         .rss_hash_update        = lio_dev_rss_hash_update,
1904         .udp_tunnel_port_add    = lio_dev_udp_tunnel_add,
1905         .udp_tunnel_port_del    = lio_dev_udp_tunnel_del,
1906         .mtu_set                = lio_dev_mtu_set,
1907 };
1908
1909 static void
1910 lio_check_pf_hs_response(void *lio_dev)
1911 {
1912         struct lio_device *dev = lio_dev;
1913
1914         /* check till response arrives */
1915         if (dev->pfvf_hsword.coproc_tics_per_us)
1916                 return;
1917
1918         cn23xx_vf_handle_mbox(dev);
1919
1920         rte_eal_alarm_set(1, lio_check_pf_hs_response, lio_dev);
1921 }
1922
1923 /**
1924  * \brief Identify the LIO device and to map the BAR address space
1925  * @param lio_dev lio device
1926  */
1927 static int
1928 lio_chip_specific_setup(struct lio_device *lio_dev)
1929 {
1930         struct rte_pci_device *pdev = lio_dev->pci_dev;
1931         uint32_t dev_id = pdev->id.device_id;
1932         const char *s;
1933         int ret = 1;
1934
1935         switch (dev_id) {
1936         case LIO_CN23XX_VF_VID:
1937                 lio_dev->chip_id = LIO_CN23XX_VF_VID;
1938                 ret = cn23xx_vf_setup_device(lio_dev);
1939                 s = "CN23XX VF";
1940                 break;
1941         default:
1942                 s = "?";
1943                 lio_dev_err(lio_dev, "Unsupported Chip\n");
1944         }
1945
1946         if (!ret)
1947                 lio_dev_info(lio_dev, "DEVICE : %s\n", s);
1948
1949         return ret;
1950 }
1951
1952 static int
1953 lio_first_time_init(struct lio_device *lio_dev,
1954                     struct rte_pci_device *pdev)
1955 {
1956         int dpdk_queues;
1957
1958         PMD_INIT_FUNC_TRACE();
1959
1960         /* set dpdk specific pci device pointer */
1961         lio_dev->pci_dev = pdev;
1962
1963         /* Identify the LIO type and set device ops */
1964         if (lio_chip_specific_setup(lio_dev)) {
1965                 lio_dev_err(lio_dev, "Chip specific setup failed\n");
1966                 return -1;
1967         }
1968
1969         /* Initialize soft command buffer pool */
1970         if (lio_setup_sc_buffer_pool(lio_dev)) {
1971                 lio_dev_err(lio_dev, "sc buffer pool allocation failed\n");
1972                 return -1;
1973         }
1974
1975         /* Initialize lists to manage the requests of different types that
1976          * arrive from applications for this lio device.
1977          */
1978         lio_setup_response_list(lio_dev);
1979
1980         if (lio_dev->fn_list.setup_mbox(lio_dev)) {
1981                 lio_dev_err(lio_dev, "Mailbox setup failed\n");
1982                 goto error;
1983         }
1984
1985         /* Check PF response */
1986         lio_check_pf_hs_response((void *)lio_dev);
1987
1988         /* Do handshake and exit if incompatible PF driver */
1989         if (cn23xx_pfvf_handshake(lio_dev))
1990                 goto error;
1991
1992         /* Request and wait for device reset. */
1993         if (pdev->kdrv == RTE_KDRV_IGB_UIO) {
1994                 cn23xx_vf_ask_pf_to_do_flr(lio_dev);
1995                 /* FLR wait time doubled as a precaution. */
1996                 rte_delay_ms(LIO_PCI_FLR_WAIT * 2);
1997         }
1998
1999         if (lio_dev->fn_list.setup_device_regs(lio_dev)) {
2000                 lio_dev_err(lio_dev, "Failed to configure device registers\n");
2001                 goto error;
2002         }
2003
2004         if (lio_setup_instr_queue0(lio_dev)) {
2005                 lio_dev_err(lio_dev, "Failed to setup instruction queue 0\n");
2006                 goto error;
2007         }
2008
2009         dpdk_queues = (int)lio_dev->sriov_info.rings_per_vf;
2010
2011         lio_dev->max_tx_queues = dpdk_queues;
2012         lio_dev->max_rx_queues = dpdk_queues;
2013
2014         /* Enable input and output queues for this device */
2015         if (lio_dev->fn_list.enable_io_queues(lio_dev))
2016                 goto error;
2017
2018         return 0;
2019
2020 error:
2021         lio_free_sc_buffer_pool(lio_dev);
2022         if (lio_dev->mbox[0])
2023                 lio_dev->fn_list.free_mbox(lio_dev);
2024         if (lio_dev->instr_queue[0])
2025                 lio_free_instr_queue0(lio_dev);
2026
2027         return -1;
2028 }
2029
2030 static int
2031 lio_eth_dev_uninit(struct rte_eth_dev *eth_dev)
2032 {
2033         struct lio_device *lio_dev = LIO_DEV(eth_dev);
2034
2035         PMD_INIT_FUNC_TRACE();
2036
2037         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2038                 return -EPERM;
2039
2040         /* lio_free_sc_buffer_pool */
2041         lio_free_sc_buffer_pool(lio_dev);
2042
2043         rte_free(eth_dev->data->mac_addrs);
2044         eth_dev->data->mac_addrs = NULL;
2045
2046         eth_dev->dev_ops = NULL;
2047         eth_dev->rx_pkt_burst = NULL;
2048         eth_dev->tx_pkt_burst = NULL;
2049
2050         return 0;
2051 }
2052
2053 static int
2054 lio_eth_dev_init(struct rte_eth_dev *eth_dev)
2055 {
2056         struct rte_pci_device *pdev = RTE_ETH_DEV_TO_PCI(eth_dev);
2057         struct lio_device *lio_dev = LIO_DEV(eth_dev);
2058
2059         PMD_INIT_FUNC_TRACE();
2060
2061         eth_dev->rx_pkt_burst = &lio_dev_recv_pkts;
2062         eth_dev->tx_pkt_burst = &lio_dev_xmit_pkts;
2063
2064         /* Primary does the initialization. */
2065         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2066                 return 0;
2067
2068         rte_eth_copy_pci_info(eth_dev, pdev);
2069
2070         if (pdev->mem_resource[0].addr) {
2071                 lio_dev->hw_addr = pdev->mem_resource[0].addr;
2072         } else {
2073                 PMD_INIT_LOG(ERR, "ERROR: Failed to map BAR0\n");
2074                 return -ENODEV;
2075         }
2076
2077         lio_dev->eth_dev = eth_dev;
2078         /* set lio device print string */
2079         snprintf(lio_dev->dev_string, sizeof(lio_dev->dev_string),
2080                  "%s[%02x:%02x.%x]", pdev->driver->driver.name,
2081                  pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
2082
2083         lio_dev->port_id = eth_dev->data->port_id;
2084
2085         if (lio_first_time_init(lio_dev, pdev)) {
2086                 lio_dev_err(lio_dev, "Device init failed\n");
2087                 return -EINVAL;
2088         }
2089
2090         eth_dev->dev_ops = &liovf_eth_dev_ops;
2091         eth_dev->data->mac_addrs = rte_zmalloc("lio", ETHER_ADDR_LEN, 0);
2092         if (eth_dev->data->mac_addrs == NULL) {
2093                 lio_dev_err(lio_dev,
2094                             "MAC addresses memory allocation failed\n");
2095                 eth_dev->dev_ops = NULL;
2096                 eth_dev->rx_pkt_burst = NULL;
2097                 eth_dev->tx_pkt_burst = NULL;
2098                 return -ENOMEM;
2099         }
2100
2101         rte_atomic64_set(&lio_dev->status, LIO_DEV_RUNNING);
2102         rte_wmb();
2103
2104         lio_dev->port_configured = 0;
2105         /* Always allow unicast packets */
2106         lio_dev->ifflags |= LIO_IFFLAG_UNICAST;
2107
2108         return 0;
2109 }
2110
2111 static int
2112 lio_eth_dev_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2113                       struct rte_pci_device *pci_dev)
2114 {
2115         struct rte_eth_dev *eth_dev;
2116         int ret;
2117
2118         eth_dev = rte_eth_dev_pci_allocate(pci_dev,
2119                                            sizeof(struct lio_device));
2120         if (eth_dev == NULL)
2121                 return -ENOMEM;
2122
2123         ret = lio_eth_dev_init(eth_dev);
2124         if (ret)
2125                 rte_eth_dev_pci_release(eth_dev);
2126
2127         return ret;
2128 }
2129
2130 static int
2131 lio_eth_dev_pci_remove(struct rte_pci_device *pci_dev)
2132 {
2133         return rte_eth_dev_pci_generic_remove(pci_dev,
2134                                               lio_eth_dev_uninit);
2135 }
2136
2137 /* Set of PCI devices this driver supports */
2138 static const struct rte_pci_id pci_id_liovf_map[] = {
2139         { RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_VF_VID) },
2140         { .vendor_id = 0, /* sentinel */ }
2141 };
2142
2143 static struct rte_pci_driver rte_liovf_pmd = {
2144         .id_table       = pci_id_liovf_map,
2145         .drv_flags      = RTE_PCI_DRV_NEED_MAPPING,
2146         .probe          = lio_eth_dev_pci_probe,
2147         .remove         = lio_eth_dev_pci_remove,
2148 };
2149
2150 RTE_PMD_REGISTER_PCI(net_liovf, rte_liovf_pmd);
2151 RTE_PMD_REGISTER_PCI_TABLE(net_liovf, pci_id_liovf_map);
2152 RTE_PMD_REGISTER_KMOD_DEP(net_liovf, "* igb_uio | vfio-pci");
2153
2154 RTE_INIT(lio_init_log);
2155 static void
2156 lio_init_log(void)
2157 {
2158         lio_logtype_init = rte_log_register("pmd.net.liquidio.init");
2159         if (lio_logtype_init >= 0)
2160                 rte_log_set_level(lio_logtype_init, RTE_LOG_NOTICE);
2161         lio_logtype_driver = rte_log_register("pmd.net.liquidio.driver");
2162         if (lio_logtype_driver >= 0)
2163                 rte_log_set_level(lio_logtype_driver, RTE_LOG_NOTICE);
2164 }