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