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