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