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