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