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