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