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