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