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