net/liquidio: add APIs to enable and disable multicast
[dpdk.git] / drivers / net / liquidio / lio_ethdev.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Cavium, Inc.. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Cavium, Inc. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_ethdev.h>
35 #include <rte_cycles.h>
36 #include <rte_malloc.h>
37 #include <rte_alarm.h>
38
39 #include "lio_logs.h"
40 #include "lio_23xx_vf.h"
41 #include "lio_ethdev.h"
42 #include "lio_rxtx.h"
43
44 /* Default RSS key in use */
45 static uint8_t lio_rss_key[40] = {
46         0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
47         0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
48         0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
49         0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
50         0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA,
51 };
52
53 static const struct rte_eth_desc_lim lio_rx_desc_lim = {
54         .nb_max         = CN23XX_MAX_OQ_DESCRIPTORS,
55         .nb_min         = CN23XX_MIN_OQ_DESCRIPTORS,
56         .nb_align       = 1,
57 };
58
59 static const struct rte_eth_desc_lim lio_tx_desc_lim = {
60         .nb_max         = CN23XX_MAX_IQ_DESCRIPTORS,
61         .nb_min         = CN23XX_MIN_IQ_DESCRIPTORS,
62         .nb_align       = 1,
63 };
64
65 /* Wait for control command to reach nic. */
66 static uint16_t
67 lio_wait_for_ctrl_cmd(struct lio_device *lio_dev,
68                       struct lio_dev_ctrl_cmd *ctrl_cmd)
69 {
70         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
71
72         while ((ctrl_cmd->cond == 0) && --timeout) {
73                 lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
74                 rte_delay_ms(1);
75         }
76
77         return !timeout;
78 }
79
80 /**
81  * \brief Send Rx control command
82  * @param eth_dev Pointer to the structure rte_eth_dev
83  * @param start_stop whether to start or stop
84  */
85 static int
86 lio_send_rx_ctrl_cmd(struct rte_eth_dev *eth_dev, int start_stop)
87 {
88         struct lio_device *lio_dev = LIO_DEV(eth_dev);
89         struct lio_dev_ctrl_cmd ctrl_cmd;
90         struct lio_ctrl_pkt ctrl_pkt;
91
92         /* flush added to prevent cmd failure
93          * incase the queue is full
94          */
95         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
96
97         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
98         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
99
100         ctrl_cmd.eth_dev = eth_dev;
101         ctrl_cmd.cond = 0;
102
103         ctrl_pkt.ncmd.s.cmd = LIO_CMD_RX_CTL;
104         ctrl_pkt.ncmd.s.param1 = start_stop;
105         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
106
107         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
108                 lio_dev_err(lio_dev, "Failed to send RX Control message\n");
109                 return -1;
110         }
111
112         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
113                 lio_dev_err(lio_dev, "RX Control command timed out\n");
114                 return -1;
115         }
116
117         return 0;
118 }
119
120 static void
121 lio_dev_info_get(struct rte_eth_dev *eth_dev,
122                  struct rte_eth_dev_info *devinfo)
123 {
124         struct lio_device *lio_dev = LIO_DEV(eth_dev);
125
126         devinfo->max_rx_queues = lio_dev->max_rx_queues;
127         devinfo->max_tx_queues = lio_dev->max_tx_queues;
128
129         devinfo->min_rx_bufsize = LIO_MIN_RX_BUF_SIZE;
130         devinfo->max_rx_pktlen = LIO_MAX_RX_PKTLEN;
131
132         devinfo->max_mac_addrs = 1;
133
134         devinfo->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM           |
135                                     DEV_RX_OFFLOAD_UDP_CKSUM            |
136                                     DEV_RX_OFFLOAD_TCP_CKSUM);
137         devinfo->tx_offload_capa = (DEV_TX_OFFLOAD_IPV4_CKSUM           |
138                                     DEV_TX_OFFLOAD_UDP_CKSUM            |
139                                     DEV_TX_OFFLOAD_TCP_CKSUM);
140
141         devinfo->rx_desc_lim = lio_rx_desc_lim;
142         devinfo->tx_desc_lim = lio_tx_desc_lim;
143
144         devinfo->reta_size = LIO_RSS_MAX_TABLE_SZ;
145         devinfo->hash_key_size = LIO_RSS_MAX_KEY_SZ;
146         devinfo->flow_type_rss_offloads = (ETH_RSS_IPV4                 |
147                                            ETH_RSS_NONFRAG_IPV4_TCP     |
148                                            ETH_RSS_IPV6                 |
149                                            ETH_RSS_NONFRAG_IPV6_TCP     |
150                                            ETH_RSS_IPV6_EX              |
151                                            ETH_RSS_IPV6_TCP_EX);
152 }
153
154 static int
155 lio_dev_validate_vf_mtu(struct rte_eth_dev *eth_dev, uint16_t new_mtu)
156 {
157         struct lio_device *lio_dev = LIO_DEV(eth_dev);
158
159         PMD_INIT_FUNC_TRACE();
160
161         if (!lio_dev->intf_open) {
162                 lio_dev_err(lio_dev, "Port %d down, can't check MTU\n",
163                             lio_dev->port_id);
164                 return -EINVAL;
165         }
166
167         /* Limit the MTU to make sure the ethernet packets are between
168          * ETHER_MIN_MTU bytes and PF's MTU
169          */
170         if ((new_mtu < ETHER_MIN_MTU) ||
171                         (new_mtu > lio_dev->linfo.link.s.mtu)) {
172                 lio_dev_err(lio_dev, "Invalid MTU: %d\n", new_mtu);
173                 lio_dev_err(lio_dev, "Valid range %d and %d\n",
174                             ETHER_MIN_MTU, lio_dev->linfo.link.s.mtu);
175                 return -EINVAL;
176         }
177
178         return 0;
179 }
180
181 static int
182 lio_dev_rss_reta_update(struct rte_eth_dev *eth_dev,
183                         struct rte_eth_rss_reta_entry64 *reta_conf,
184                         uint16_t reta_size)
185 {
186         struct lio_device *lio_dev = LIO_DEV(eth_dev);
187         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
188         struct lio_rss_set *rss_param;
189         struct lio_dev_ctrl_cmd ctrl_cmd;
190         struct lio_ctrl_pkt ctrl_pkt;
191         int i, j, index;
192
193         if (!lio_dev->intf_open) {
194                 lio_dev_err(lio_dev, "Port %d down, can't update reta\n",
195                             lio_dev->port_id);
196                 return -EINVAL;
197         }
198
199         if (reta_size != LIO_RSS_MAX_TABLE_SZ) {
200                 lio_dev_err(lio_dev,
201                             "The size of hash lookup table configured (%d) doesn't match the number hardware can supported (%d)\n",
202                             reta_size, LIO_RSS_MAX_TABLE_SZ);
203                 return -EINVAL;
204         }
205
206         /* flush added to prevent cmd failure
207          * incase the queue is full
208          */
209         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
210
211         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
212         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
213
214         rss_param = (struct lio_rss_set *)&ctrl_pkt.udd[0];
215
216         ctrl_cmd.eth_dev = eth_dev;
217         ctrl_cmd.cond = 0;
218
219         ctrl_pkt.ncmd.s.cmd = LIO_CMD_SET_RSS;
220         ctrl_pkt.ncmd.s.more = sizeof(struct lio_rss_set) >> 3;
221         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
222
223         rss_param->param.flags = 0xF;
224         rss_param->param.flags &= ~LIO_RSS_PARAM_ITABLE_UNCHANGED;
225         rss_param->param.itablesize = LIO_RSS_MAX_TABLE_SZ;
226
227         for (i = 0; i < (reta_size / RTE_RETA_GROUP_SIZE); i++) {
228                 for (j = 0; j < RTE_RETA_GROUP_SIZE; j++) {
229                         if ((reta_conf[i].mask) & ((uint64_t)1 << j)) {
230                                 index = (i * RTE_RETA_GROUP_SIZE) + j;
231                                 rss_state->itable[index] = reta_conf[i].reta[j];
232                         }
233                 }
234         }
235
236         rss_state->itable_size = LIO_RSS_MAX_TABLE_SZ;
237         memcpy(rss_param->itable, rss_state->itable, rss_state->itable_size);
238
239         lio_swap_8B_data((uint64_t *)rss_param, LIO_RSS_PARAM_SIZE >> 3);
240
241         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
242                 lio_dev_err(lio_dev, "Failed to set rss hash\n");
243                 return -1;
244         }
245
246         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
247                 lio_dev_err(lio_dev, "Set rss hash timed out\n");
248                 return -1;
249         }
250
251         return 0;
252 }
253
254 static int
255 lio_dev_rss_reta_query(struct rte_eth_dev *eth_dev,
256                        struct rte_eth_rss_reta_entry64 *reta_conf,
257                        uint16_t reta_size)
258 {
259         struct lio_device *lio_dev = LIO_DEV(eth_dev);
260         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
261         int i, num;
262
263         if (reta_size != LIO_RSS_MAX_TABLE_SZ) {
264                 lio_dev_err(lio_dev,
265                             "The size of hash lookup table configured (%d) doesn't match the number hardware can supported (%d)\n",
266                             reta_size, LIO_RSS_MAX_TABLE_SZ);
267                 return -EINVAL;
268         }
269
270         num = reta_size / RTE_RETA_GROUP_SIZE;
271
272         for (i = 0; i < num; i++) {
273                 memcpy(reta_conf->reta,
274                        &rss_state->itable[i * RTE_RETA_GROUP_SIZE],
275                        RTE_RETA_GROUP_SIZE);
276                 reta_conf++;
277         }
278
279         return 0;
280 }
281
282 static int
283 lio_dev_rss_hash_conf_get(struct rte_eth_dev *eth_dev,
284                           struct rte_eth_rss_conf *rss_conf)
285 {
286         struct lio_device *lio_dev = LIO_DEV(eth_dev);
287         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
288         uint8_t *hash_key = NULL;
289         uint64_t rss_hf = 0;
290
291         if (rss_state->hash_disable) {
292                 lio_dev_info(lio_dev, "RSS disabled in nic\n");
293                 rss_conf->rss_hf = 0;
294                 return 0;
295         }
296
297         /* Get key value */
298         hash_key = rss_conf->rss_key;
299         if (hash_key != NULL)
300                 memcpy(hash_key, rss_state->hash_key, rss_state->hash_key_size);
301
302         if (rss_state->ip)
303                 rss_hf |= ETH_RSS_IPV4;
304         if (rss_state->tcp_hash)
305                 rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
306         if (rss_state->ipv6)
307                 rss_hf |= ETH_RSS_IPV6;
308         if (rss_state->ipv6_tcp_hash)
309                 rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
310         if (rss_state->ipv6_ex)
311                 rss_hf |= ETH_RSS_IPV6_EX;
312         if (rss_state->ipv6_tcp_ex_hash)
313                 rss_hf |= ETH_RSS_IPV6_TCP_EX;
314
315         rss_conf->rss_hf = rss_hf;
316
317         return 0;
318 }
319
320 static int
321 lio_dev_rss_hash_update(struct rte_eth_dev *eth_dev,
322                         struct rte_eth_rss_conf *rss_conf)
323 {
324         struct lio_device *lio_dev = LIO_DEV(eth_dev);
325         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
326         struct lio_rss_set *rss_param;
327         struct lio_dev_ctrl_cmd ctrl_cmd;
328         struct lio_ctrl_pkt ctrl_pkt;
329
330         if (!lio_dev->intf_open) {
331                 lio_dev_err(lio_dev, "Port %d down, can't update hash\n",
332                             lio_dev->port_id);
333                 return -EINVAL;
334         }
335
336         /* flush added to prevent cmd failure
337          * incase the queue is full
338          */
339         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
340
341         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
342         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
343
344         rss_param = (struct lio_rss_set *)&ctrl_pkt.udd[0];
345
346         ctrl_cmd.eth_dev = eth_dev;
347         ctrl_cmd.cond = 0;
348
349         ctrl_pkt.ncmd.s.cmd = LIO_CMD_SET_RSS;
350         ctrl_pkt.ncmd.s.more = sizeof(struct lio_rss_set) >> 3;
351         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
352
353         rss_param->param.flags = 0xF;
354
355         if (rss_conf->rss_key) {
356                 rss_param->param.flags &= ~LIO_RSS_PARAM_HASH_KEY_UNCHANGED;
357                 rss_state->hash_key_size = LIO_RSS_MAX_KEY_SZ;
358                 rss_param->param.hashkeysize = LIO_RSS_MAX_KEY_SZ;
359                 memcpy(rss_state->hash_key, rss_conf->rss_key,
360                        rss_state->hash_key_size);
361                 memcpy(rss_param->key, rss_state->hash_key,
362                        rss_state->hash_key_size);
363         }
364
365         if ((rss_conf->rss_hf & LIO_RSS_OFFLOAD_ALL) == 0) {
366                 /* Can't disable rss through hash flags,
367                  * if it is enabled by default during init
368                  */
369                 if (!rss_state->hash_disable)
370                         return -EINVAL;
371
372                 /* This is for --disable-rss during testpmd launch */
373                 rss_param->param.flags |= LIO_RSS_PARAM_DISABLE_RSS;
374         } else {
375                 uint32_t hashinfo = 0;
376
377                 /* Can't enable rss if disabled by default during init */
378                 if (rss_state->hash_disable)
379                         return -EINVAL;
380
381                 if (rss_conf->rss_hf & ETH_RSS_IPV4) {
382                         hashinfo |= LIO_RSS_HASH_IPV4;
383                         rss_state->ip = 1;
384                 } else {
385                         rss_state->ip = 0;
386                 }
387
388                 if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP) {
389                         hashinfo |= LIO_RSS_HASH_TCP_IPV4;
390                         rss_state->tcp_hash = 1;
391                 } else {
392                         rss_state->tcp_hash = 0;
393                 }
394
395                 if (rss_conf->rss_hf & ETH_RSS_IPV6) {
396                         hashinfo |= LIO_RSS_HASH_IPV6;
397                         rss_state->ipv6 = 1;
398                 } else {
399                         rss_state->ipv6 = 0;
400                 }
401
402                 if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP) {
403                         hashinfo |= LIO_RSS_HASH_TCP_IPV6;
404                         rss_state->ipv6_tcp_hash = 1;
405                 } else {
406                         rss_state->ipv6_tcp_hash = 0;
407                 }
408
409                 if (rss_conf->rss_hf & ETH_RSS_IPV6_EX) {
410                         hashinfo |= LIO_RSS_HASH_IPV6_EX;
411                         rss_state->ipv6_ex = 1;
412                 } else {
413                         rss_state->ipv6_ex = 0;
414                 }
415
416                 if (rss_conf->rss_hf & ETH_RSS_IPV6_TCP_EX) {
417                         hashinfo |= LIO_RSS_HASH_TCP_IPV6_EX;
418                         rss_state->ipv6_tcp_ex_hash = 1;
419                 } else {
420                         rss_state->ipv6_tcp_ex_hash = 0;
421                 }
422
423                 rss_param->param.flags &= ~LIO_RSS_PARAM_HASH_INFO_UNCHANGED;
424                 rss_param->param.hashinfo = hashinfo;
425         }
426
427         lio_swap_8B_data((uint64_t *)rss_param, LIO_RSS_PARAM_SIZE >> 3);
428
429         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
430                 lio_dev_err(lio_dev, "Failed to set rss hash\n");
431                 return -1;
432         }
433
434         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
435                 lio_dev_err(lio_dev, "Set rss hash timed out\n");
436                 return -1;
437         }
438
439         return 0;
440 }
441
442 /**
443  * Atomically writes the link status information into global
444  * structure rte_eth_dev.
445  *
446  * @param eth_dev
447  *   - Pointer to the structure rte_eth_dev to read from.
448  *   - Pointer to the buffer to be saved with the link status.
449  *
450  * @return
451  *   - On success, zero.
452  *   - On failure, negative value.
453  */
454 static inline int
455 lio_dev_atomic_write_link_status(struct rte_eth_dev *eth_dev,
456                                  struct rte_eth_link *link)
457 {
458         struct rte_eth_link *dst = &eth_dev->data->dev_link;
459         struct rte_eth_link *src = link;
460
461         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
462                                 *(uint64_t *)src) == 0)
463                 return -1;
464
465         return 0;
466 }
467
468 static uint64_t
469 lio_hweight64(uint64_t w)
470 {
471         uint64_t res = w - ((w >> 1) & 0x5555555555555555ul);
472
473         res =
474             (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
475         res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
476         res = res + (res >> 8);
477         res = res + (res >> 16);
478
479         return (res + (res >> 32)) & 0x00000000000000FFul;
480 }
481
482 static int
483 lio_dev_link_update(struct rte_eth_dev *eth_dev,
484                     int wait_to_complete __rte_unused)
485 {
486         struct lio_device *lio_dev = LIO_DEV(eth_dev);
487         struct rte_eth_link link, old;
488
489         /* Initialize */
490         link.link_status = ETH_LINK_DOWN;
491         link.link_speed = ETH_SPEED_NUM_NONE;
492         link.link_duplex = ETH_LINK_HALF_DUPLEX;
493         memset(&old, 0, sizeof(old));
494
495         /* Return what we found */
496         if (lio_dev->linfo.link.s.link_up == 0) {
497                 /* Interface is down */
498                 if (lio_dev_atomic_write_link_status(eth_dev, &link))
499                         return -1;
500                 if (link.link_status == old.link_status)
501                         return -1;
502                 return 0;
503         }
504
505         link.link_status = ETH_LINK_UP; /* Interface is up */
506         link.link_duplex = ETH_LINK_FULL_DUPLEX;
507         switch (lio_dev->linfo.link.s.speed) {
508         case LIO_LINK_SPEED_10000:
509                 link.link_speed = ETH_SPEED_NUM_10G;
510                 break;
511         default:
512                 link.link_speed = ETH_SPEED_NUM_NONE;
513                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
514         }
515
516         if (lio_dev_atomic_write_link_status(eth_dev, &link))
517                 return -1;
518
519         if (link.link_status == old.link_status)
520                 return -1;
521
522         return 0;
523 }
524
525 /**
526  * \brief Net device enable, disable allmulticast
527  * @param eth_dev Pointer to the structure rte_eth_dev
528  */
529 static void
530 lio_change_dev_flag(struct rte_eth_dev *eth_dev)
531 {
532         struct lio_device *lio_dev = LIO_DEV(eth_dev);
533         struct lio_dev_ctrl_cmd ctrl_cmd;
534         struct lio_ctrl_pkt ctrl_pkt;
535
536         /* flush added to prevent cmd failure
537          * incase the queue is full
538          */
539         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
540
541         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
542         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
543
544         ctrl_cmd.eth_dev = eth_dev;
545         ctrl_cmd.cond = 0;
546
547         /* Create a ctrl pkt command to be sent to core app. */
548         ctrl_pkt.ncmd.s.cmd = LIO_CMD_CHANGE_DEVFLAGS;
549         ctrl_pkt.ncmd.s.param1 = lio_dev->ifflags;
550         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
551
552         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
553                 lio_dev_err(lio_dev, "Failed to send change flag message\n");
554                 return;
555         }
556
557         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd))
558                 lio_dev_err(lio_dev, "Change dev flag command timed out\n");
559 }
560
561 static void
562 lio_dev_allmulticast_enable(struct rte_eth_dev *eth_dev)
563 {
564         struct lio_device *lio_dev = LIO_DEV(eth_dev);
565
566         if (!lio_dev->intf_open) {
567                 lio_dev_err(lio_dev, "Port %d down, can't enable multicast\n",
568                             lio_dev->port_id);
569                 return;
570         }
571
572         lio_dev->ifflags |= LIO_IFFLAG_ALLMULTI;
573         lio_change_dev_flag(eth_dev);
574 }
575
576 static void
577 lio_dev_allmulticast_disable(struct rte_eth_dev *eth_dev)
578 {
579         struct lio_device *lio_dev = LIO_DEV(eth_dev);
580
581         if (!lio_dev->intf_open) {
582                 lio_dev_err(lio_dev, "Port %d down, can't disable multicast\n",
583                             lio_dev->port_id);
584                 return;
585         }
586
587         lio_dev->ifflags &= ~LIO_IFFLAG_ALLMULTI;
588         lio_change_dev_flag(eth_dev);
589 }
590
591 static void
592 lio_dev_rss_configure(struct rte_eth_dev *eth_dev)
593 {
594         struct lio_device *lio_dev = LIO_DEV(eth_dev);
595         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
596         struct rte_eth_rss_reta_entry64 reta_conf[8];
597         struct rte_eth_rss_conf rss_conf;
598         uint16_t i;
599
600         /* Configure the RSS key and the RSS protocols used to compute
601          * the RSS hash of input packets.
602          */
603         rss_conf = eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
604         if ((rss_conf.rss_hf & LIO_RSS_OFFLOAD_ALL) == 0) {
605                 rss_state->hash_disable = 1;
606                 lio_dev_rss_hash_update(eth_dev, &rss_conf);
607                 return;
608         }
609
610         if (rss_conf.rss_key == NULL)
611                 rss_conf.rss_key = lio_rss_key; /* Default hash key */
612
613         lio_dev_rss_hash_update(eth_dev, &rss_conf);
614
615         memset(reta_conf, 0, sizeof(reta_conf));
616         for (i = 0; i < LIO_RSS_MAX_TABLE_SZ; i++) {
617                 uint8_t q_idx, conf_idx, reta_idx;
618
619                 q_idx = (uint8_t)((eth_dev->data->nb_rx_queues > 1) ?
620                                   i % eth_dev->data->nb_rx_queues : 0);
621                 conf_idx = i / RTE_RETA_GROUP_SIZE;
622                 reta_idx = i % RTE_RETA_GROUP_SIZE;
623                 reta_conf[conf_idx].reta[reta_idx] = q_idx;
624                 reta_conf[conf_idx].mask |= ((uint64_t)1 << reta_idx);
625         }
626
627         lio_dev_rss_reta_update(eth_dev, reta_conf, LIO_RSS_MAX_TABLE_SZ);
628 }
629
630 static void
631 lio_dev_mq_rx_configure(struct rte_eth_dev *eth_dev)
632 {
633         struct lio_device *lio_dev = LIO_DEV(eth_dev);
634         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
635         struct rte_eth_rss_conf rss_conf;
636
637         switch (eth_dev->data->dev_conf.rxmode.mq_mode) {
638         case ETH_MQ_RX_RSS:
639                 lio_dev_rss_configure(eth_dev);
640                 break;
641         case ETH_MQ_RX_NONE:
642         /* if mq_mode is none, disable rss mode. */
643         default:
644                 memset(&rss_conf, 0, sizeof(rss_conf));
645                 rss_state->hash_disable = 1;
646                 lio_dev_rss_hash_update(eth_dev, &rss_conf);
647         }
648 }
649
650 /**
651  * Setup our receive queue/ringbuffer. This is the
652  * queue the Octeon uses to send us packets and
653  * responses. We are given a memory pool for our
654  * packet buffers that are used to populate the receive
655  * queue.
656  *
657  * @param eth_dev
658  *    Pointer to the structure rte_eth_dev
659  * @param q_no
660  *    Queue number
661  * @param num_rx_descs
662  *    Number of entries in the queue
663  * @param socket_id
664  *    Where to allocate memory
665  * @param rx_conf
666  *    Pointer to the struction rte_eth_rxconf
667  * @param mp
668  *    Pointer to the packet pool
669  *
670  * @return
671  *    - On success, return 0
672  *    - On failure, return -1
673  */
674 static int
675 lio_dev_rx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
676                        uint16_t num_rx_descs, unsigned int socket_id,
677                        const struct rte_eth_rxconf *rx_conf __rte_unused,
678                        struct rte_mempool *mp)
679 {
680         struct lio_device *lio_dev = LIO_DEV(eth_dev);
681         struct rte_pktmbuf_pool_private *mbp_priv;
682         uint32_t fw_mapped_oq;
683         uint16_t buf_size;
684
685         if (q_no >= lio_dev->nb_rx_queues) {
686                 lio_dev_err(lio_dev, "Invalid rx queue number %u\n", q_no);
687                 return -EINVAL;
688         }
689
690         lio_dev_dbg(lio_dev, "setting up rx queue %u\n", q_no);
691
692         fw_mapped_oq = lio_dev->linfo.rxpciq[q_no].s.q_no;
693
694         if ((lio_dev->droq[fw_mapped_oq]) &&
695             (num_rx_descs != lio_dev->droq[fw_mapped_oq]->max_count)) {
696                 lio_dev_err(lio_dev,
697                             "Reconfiguring Rx descs not supported. Configure descs to same value %u or restart application\n",
698                             lio_dev->droq[fw_mapped_oq]->max_count);
699                 return -ENOTSUP;
700         }
701
702         mbp_priv = rte_mempool_get_priv(mp);
703         buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
704
705         if (lio_setup_droq(lio_dev, fw_mapped_oq, num_rx_descs, buf_size, mp,
706                            socket_id)) {
707                 lio_dev_err(lio_dev, "droq allocation failed\n");
708                 return -1;
709         }
710
711         eth_dev->data->rx_queues[q_no] = lio_dev->droq[fw_mapped_oq];
712
713         return 0;
714 }
715
716 /**
717  * Release the receive queue/ringbuffer. Called by
718  * the upper layers.
719  *
720  * @param rxq
721  *    Opaque pointer to the receive queue to release
722  *
723  * @return
724  *    - nothing
725  */
726 static void
727 lio_dev_rx_queue_release(void *rxq)
728 {
729         struct lio_droq *droq = rxq;
730         struct lio_device *lio_dev = droq->lio_dev;
731         int oq_no;
732
733         /* Run time queue deletion not supported */
734         if (lio_dev->port_configured)
735                 return;
736
737         if (droq != NULL) {
738                 oq_no = droq->q_no;
739                 lio_delete_droq_queue(droq->lio_dev, oq_no);
740         }
741 }
742
743 /**
744  * Allocate and initialize SW ring. Initialize associated HW registers.
745  *
746  * @param eth_dev
747  *   Pointer to structure rte_eth_dev
748  *
749  * @param q_no
750  *   Queue number
751  *
752  * @param num_tx_descs
753  *   Number of ringbuffer descriptors
754  *
755  * @param socket_id
756  *   NUMA socket id, used for memory allocations
757  *
758  * @param tx_conf
759  *   Pointer to the structure rte_eth_txconf
760  *
761  * @return
762  *   - On success, return 0
763  *   - On failure, return -errno value
764  */
765 static int
766 lio_dev_tx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
767                        uint16_t num_tx_descs, unsigned int socket_id,
768                        const struct rte_eth_txconf *tx_conf __rte_unused)
769 {
770         struct lio_device *lio_dev = LIO_DEV(eth_dev);
771         int fw_mapped_iq = lio_dev->linfo.txpciq[q_no].s.q_no;
772         int retval;
773
774         if (q_no >= lio_dev->nb_tx_queues) {
775                 lio_dev_err(lio_dev, "Invalid tx queue number %u\n", q_no);
776                 return -EINVAL;
777         }
778
779         lio_dev_dbg(lio_dev, "setting up tx queue %u\n", q_no);
780
781         if ((lio_dev->instr_queue[fw_mapped_iq] != NULL) &&
782             (num_tx_descs != lio_dev->instr_queue[fw_mapped_iq]->max_count)) {
783                 lio_dev_err(lio_dev,
784                             "Reconfiguring Tx descs not supported. Configure descs to same value %u or restart application\n",
785                             lio_dev->instr_queue[fw_mapped_iq]->max_count);
786                 return -ENOTSUP;
787         }
788
789         retval = lio_setup_iq(lio_dev, q_no, lio_dev->linfo.txpciq[q_no],
790                               num_tx_descs, lio_dev, socket_id);
791
792         if (retval) {
793                 lio_dev_err(lio_dev, "Runtime IQ(TxQ) creation failed.\n");
794                 return retval;
795         }
796
797         retval = lio_setup_sglists(lio_dev, q_no, fw_mapped_iq,
798                                 lio_dev->instr_queue[fw_mapped_iq]->max_count,
799                                 socket_id);
800
801         if (retval) {
802                 lio_delete_instruction_queue(lio_dev, fw_mapped_iq);
803                 return retval;
804         }
805
806         eth_dev->data->tx_queues[q_no] = lio_dev->instr_queue[fw_mapped_iq];
807
808         return 0;
809 }
810
811 /**
812  * Release the transmit queue/ringbuffer. Called by
813  * the upper layers.
814  *
815  * @param txq
816  *    Opaque pointer to the transmit queue to release
817  *
818  * @return
819  *    - nothing
820  */
821 static void
822 lio_dev_tx_queue_release(void *txq)
823 {
824         struct lio_instr_queue *tq = txq;
825         struct lio_device *lio_dev = tq->lio_dev;
826         uint32_t fw_mapped_iq_no;
827
828         /* Run time queue deletion not supported */
829         if (lio_dev->port_configured)
830                 return;
831
832         if (tq != NULL) {
833                 /* Free sg_list */
834                 lio_delete_sglist(tq);
835
836                 fw_mapped_iq_no = tq->txpciq.s.q_no;
837                 lio_delete_instruction_queue(tq->lio_dev, fw_mapped_iq_no);
838         }
839 }
840
841 /**
842  * Api to check link state.
843  */
844 static void
845 lio_dev_get_link_status(struct rte_eth_dev *eth_dev)
846 {
847         struct lio_device *lio_dev = LIO_DEV(eth_dev);
848         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
849         struct lio_link_status_resp *resp;
850         union octeon_link_status *ls;
851         struct lio_soft_command *sc;
852         uint32_t resp_size;
853
854         if (!lio_dev->intf_open)
855                 return;
856
857         resp_size = sizeof(struct lio_link_status_resp);
858         sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
859         if (sc == NULL)
860                 return;
861
862         resp = (struct lio_link_status_resp *)sc->virtrptr;
863         lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
864                                  LIO_OPCODE_INFO, 0, 0, 0);
865
866         /* Setting wait time in seconds */
867         sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
868
869         if (lio_send_soft_command(lio_dev, sc) == LIO_IQ_SEND_FAILED)
870                 goto get_status_fail;
871
872         while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
873                 lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
874                 rte_delay_ms(1);
875         }
876
877         if (resp->status)
878                 goto get_status_fail;
879
880         ls = &resp->link_info.link;
881
882         lio_swap_8B_data((uint64_t *)ls, sizeof(union octeon_link_status) >> 3);
883
884         if (lio_dev->linfo.link.link_status64 != ls->link_status64) {
885                 lio_dev->linfo.link.link_status64 = ls->link_status64;
886                 lio_dev_link_update(eth_dev, 0);
887         }
888
889         lio_free_soft_command(sc);
890
891         return;
892
893 get_status_fail:
894         lio_free_soft_command(sc);
895 }
896
897 /* This function will be invoked every LSC_TIMEOUT ns (100ms)
898  * and will update link state if it changes.
899  */
900 static void
901 lio_sync_link_state_check(void *eth_dev)
902 {
903         struct lio_device *lio_dev =
904                 (((struct rte_eth_dev *)eth_dev)->data->dev_private);
905
906         if (lio_dev->port_configured)
907                 lio_dev_get_link_status(eth_dev);
908
909         /* Schedule periodic link status check.
910          * Stop check if interface is close and start again while opening.
911          */
912         if (lio_dev->intf_open)
913                 rte_eal_alarm_set(LIO_LSC_TIMEOUT, lio_sync_link_state_check,
914                                   eth_dev);
915 }
916
917 static int
918 lio_dev_start(struct rte_eth_dev *eth_dev)
919 {
920         uint16_t mtu = eth_dev->data->dev_conf.rxmode.max_rx_pkt_len;
921         struct lio_device *lio_dev = LIO_DEV(eth_dev);
922         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
923         int ret = 0;
924
925         lio_dev_info(lio_dev, "Starting port %d\n", eth_dev->data->port_id);
926
927         if (lio_dev->fn_list.enable_io_queues(lio_dev))
928                 return -1;
929
930         if (lio_send_rx_ctrl_cmd(eth_dev, 1))
931                 return -1;
932
933         /* Ready for link status updates */
934         lio_dev->intf_open = 1;
935         rte_mb();
936
937         /* Configure RSS if device configured with multiple RX queues. */
938         lio_dev_mq_rx_configure(eth_dev);
939
940         /* start polling for lsc */
941         ret = rte_eal_alarm_set(LIO_LSC_TIMEOUT,
942                                 lio_sync_link_state_check,
943                                 eth_dev);
944         if (ret) {
945                 lio_dev_err(lio_dev,
946                             "link state check handler creation failed\n");
947                 goto dev_lsc_handle_error;
948         }
949
950         while ((lio_dev->linfo.link.link_status64 == 0) && (--timeout))
951                 rte_delay_ms(1);
952
953         if (lio_dev->linfo.link.link_status64 == 0) {
954                 ret = -1;
955                 goto dev_mtu_check_error;
956         }
957
958         if (lio_dev->linfo.link.s.mtu != mtu) {
959                 ret = lio_dev_validate_vf_mtu(eth_dev, mtu);
960                 if (ret)
961                         goto dev_mtu_check_error;
962         }
963
964         return 0;
965
966 dev_mtu_check_error:
967         rte_eal_alarm_cancel(lio_sync_link_state_check, eth_dev);
968
969 dev_lsc_handle_error:
970         lio_dev->intf_open = 0;
971         lio_send_rx_ctrl_cmd(eth_dev, 0);
972
973         return ret;
974 }
975
976 static int lio_dev_configure(struct rte_eth_dev *eth_dev)
977 {
978         struct lio_device *lio_dev = LIO_DEV(eth_dev);
979         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
980         int retval, num_iqueues, num_oqueues;
981         uint8_t mac[ETHER_ADDR_LEN], i;
982         struct lio_if_cfg_resp *resp;
983         struct lio_soft_command *sc;
984         union lio_if_cfg if_cfg;
985         uint32_t resp_size;
986
987         PMD_INIT_FUNC_TRACE();
988
989         /* Re-configuring firmware not supported.
990          * Can't change tx/rx queues per port from initial value.
991          */
992         if (lio_dev->port_configured) {
993                 if ((lio_dev->nb_rx_queues != eth_dev->data->nb_rx_queues) ||
994                     (lio_dev->nb_tx_queues != eth_dev->data->nb_tx_queues)) {
995                         lio_dev_err(lio_dev,
996                                     "rxq/txq re-conf not supported. Restart application with new value.\n");
997                         return -ENOTSUP;
998                 }
999                 return 0;
1000         }
1001
1002         lio_dev->nb_rx_queues = eth_dev->data->nb_rx_queues;
1003         lio_dev->nb_tx_queues = eth_dev->data->nb_tx_queues;
1004
1005         resp_size = sizeof(struct lio_if_cfg_resp);
1006         sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
1007         if (sc == NULL)
1008                 return -ENOMEM;
1009
1010         resp = (struct lio_if_cfg_resp *)sc->virtrptr;
1011
1012         /* Firmware doesn't have capability to reconfigure the queues,
1013          * Claim all queues, and use as many required
1014          */
1015         if_cfg.if_cfg64 = 0;
1016         if_cfg.s.num_iqueues = lio_dev->nb_tx_queues;
1017         if_cfg.s.num_oqueues = lio_dev->nb_rx_queues;
1018         if_cfg.s.base_queue = 0;
1019
1020         if_cfg.s.gmx_port_id = lio_dev->pf_num;
1021
1022         lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
1023                                  LIO_OPCODE_IF_CFG, 0,
1024                                  if_cfg.if_cfg64, 0);
1025
1026         /* Setting wait time in seconds */
1027         sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
1028
1029         retval = lio_send_soft_command(lio_dev, sc);
1030         if (retval == LIO_IQ_SEND_FAILED) {
1031                 lio_dev_err(lio_dev, "iq/oq config failed status: %x\n",
1032                             retval);
1033                 /* Soft instr is freed by driver in case of failure. */
1034                 goto nic_config_fail;
1035         }
1036
1037         /* Sleep on a wait queue till the cond flag indicates that the
1038          * response arrived or timed-out.
1039          */
1040         while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
1041                 lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
1042                 lio_process_ordered_list(lio_dev);
1043                 rte_delay_ms(1);
1044         }
1045
1046         retval = resp->status;
1047         if (retval) {
1048                 lio_dev_err(lio_dev, "iq/oq config failed\n");
1049                 goto nic_config_fail;
1050         }
1051
1052         lio_swap_8B_data((uint64_t *)(&resp->cfg_info),
1053                          sizeof(struct octeon_if_cfg_info) >> 3);
1054
1055         num_iqueues = lio_hweight64(resp->cfg_info.iqmask);
1056         num_oqueues = lio_hweight64(resp->cfg_info.oqmask);
1057
1058         if (!(num_iqueues) || !(num_oqueues)) {
1059                 lio_dev_err(lio_dev,
1060                             "Got bad iqueues (%016lx) or oqueues (%016lx) from firmware.\n",
1061                             (unsigned long)resp->cfg_info.iqmask,
1062                             (unsigned long)resp->cfg_info.oqmask);
1063                 goto nic_config_fail;
1064         }
1065
1066         lio_dev_dbg(lio_dev,
1067                     "interface %d, iqmask %016lx, oqmask %016lx, numiqueues %d, numoqueues %d\n",
1068                     eth_dev->data->port_id,
1069                     (unsigned long)resp->cfg_info.iqmask,
1070                     (unsigned long)resp->cfg_info.oqmask,
1071                     num_iqueues, num_oqueues);
1072
1073         lio_dev->linfo.num_rxpciq = num_oqueues;
1074         lio_dev->linfo.num_txpciq = num_iqueues;
1075
1076         for (i = 0; i < num_oqueues; i++) {
1077                 lio_dev->linfo.rxpciq[i].rxpciq64 =
1078                     resp->cfg_info.linfo.rxpciq[i].rxpciq64;
1079                 lio_dev_dbg(lio_dev, "index %d OQ %d\n",
1080                             i, lio_dev->linfo.rxpciq[i].s.q_no);
1081         }
1082
1083         for (i = 0; i < num_iqueues; i++) {
1084                 lio_dev->linfo.txpciq[i].txpciq64 =
1085                     resp->cfg_info.linfo.txpciq[i].txpciq64;
1086                 lio_dev_dbg(lio_dev, "index %d IQ %d\n",
1087                             i, lio_dev->linfo.txpciq[i].s.q_no);
1088         }
1089
1090         lio_dev->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
1091         lio_dev->linfo.gmxport = resp->cfg_info.linfo.gmxport;
1092         lio_dev->linfo.link.link_status64 =
1093                         resp->cfg_info.linfo.link.link_status64;
1094
1095         /* 64-bit swap required on LE machines */
1096         lio_swap_8B_data(&lio_dev->linfo.hw_addr, 1);
1097         for (i = 0; i < ETHER_ADDR_LEN; i++)
1098                 mac[i] = *((uint8_t *)(((uint8_t *)&lio_dev->linfo.hw_addr) +
1099                                        2 + i));
1100
1101         /* Copy the permanent MAC address */
1102         ether_addr_copy((struct ether_addr *)mac, &eth_dev->data->mac_addrs[0]);
1103
1104         lio_dev->glist_lock =
1105             rte_zmalloc(NULL, sizeof(*lio_dev->glist_lock) * num_iqueues, 0);
1106         if (lio_dev->glist_lock == NULL)
1107                 return -ENOMEM;
1108
1109         lio_dev->glist_head =
1110                 rte_zmalloc(NULL, sizeof(*lio_dev->glist_head) * num_iqueues,
1111                             0);
1112         if (lio_dev->glist_head == NULL) {
1113                 rte_free(lio_dev->glist_lock);
1114                 lio_dev->glist_lock = NULL;
1115                 return -ENOMEM;
1116         }
1117
1118         lio_dev_link_update(eth_dev, 0);
1119
1120         lio_dev->port_configured = 1;
1121
1122         lio_free_soft_command(sc);
1123
1124         /* Disable iq_0 for reconf */
1125         lio_dev->fn_list.disable_io_queues(lio_dev);
1126
1127         /* Reset ioq regs */
1128         lio_dev->fn_list.setup_device_regs(lio_dev);
1129
1130         /* Free iq_0 used during init */
1131         lio_free_instr_queue0(lio_dev);
1132
1133         return 0;
1134
1135 nic_config_fail:
1136         lio_dev_err(lio_dev, "Failed retval %d\n", retval);
1137         lio_free_soft_command(sc);
1138         lio_free_instr_queue0(lio_dev);
1139
1140         return -ENODEV;
1141 }
1142
1143 /* Define our ethernet definitions */
1144 static const struct eth_dev_ops liovf_eth_dev_ops = {
1145         .dev_configure          = lio_dev_configure,
1146         .dev_start              = lio_dev_start,
1147         .allmulticast_enable    = lio_dev_allmulticast_enable,
1148         .allmulticast_disable   = lio_dev_allmulticast_disable,
1149         .link_update            = lio_dev_link_update,
1150         .dev_infos_get          = lio_dev_info_get,
1151         .rx_queue_setup         = lio_dev_rx_queue_setup,
1152         .rx_queue_release       = lio_dev_rx_queue_release,
1153         .tx_queue_setup         = lio_dev_tx_queue_setup,
1154         .tx_queue_release       = lio_dev_tx_queue_release,
1155         .reta_update            = lio_dev_rss_reta_update,
1156         .reta_query             = lio_dev_rss_reta_query,
1157         .rss_hash_conf_get      = lio_dev_rss_hash_conf_get,
1158         .rss_hash_update        = lio_dev_rss_hash_update,
1159 };
1160
1161 static void
1162 lio_check_pf_hs_response(void *lio_dev)
1163 {
1164         struct lio_device *dev = lio_dev;
1165
1166         /* check till response arrives */
1167         if (dev->pfvf_hsword.coproc_tics_per_us)
1168                 return;
1169
1170         cn23xx_vf_handle_mbox(dev);
1171
1172         rte_eal_alarm_set(1, lio_check_pf_hs_response, lio_dev);
1173 }
1174
1175 /**
1176  * \brief Identify the LIO device and to map the BAR address space
1177  * @param lio_dev lio device
1178  */
1179 static int
1180 lio_chip_specific_setup(struct lio_device *lio_dev)
1181 {
1182         struct rte_pci_device *pdev = lio_dev->pci_dev;
1183         uint32_t dev_id = pdev->id.device_id;
1184         const char *s;
1185         int ret = 1;
1186
1187         switch (dev_id) {
1188         case LIO_CN23XX_VF_VID:
1189                 lio_dev->chip_id = LIO_CN23XX_VF_VID;
1190                 ret = cn23xx_vf_setup_device(lio_dev);
1191                 s = "CN23XX VF";
1192                 break;
1193         default:
1194                 s = "?";
1195                 lio_dev_err(lio_dev, "Unsupported Chip\n");
1196         }
1197
1198         if (!ret)
1199                 lio_dev_info(lio_dev, "DEVICE : %s\n", s);
1200
1201         return ret;
1202 }
1203
1204 static int
1205 lio_first_time_init(struct lio_device *lio_dev,
1206                     struct rte_pci_device *pdev)
1207 {
1208         int dpdk_queues;
1209
1210         PMD_INIT_FUNC_TRACE();
1211
1212         /* set dpdk specific pci device pointer */
1213         lio_dev->pci_dev = pdev;
1214
1215         /* Identify the LIO type and set device ops */
1216         if (lio_chip_specific_setup(lio_dev)) {
1217                 lio_dev_err(lio_dev, "Chip specific setup failed\n");
1218                 return -1;
1219         }
1220
1221         /* Initialize soft command buffer pool */
1222         if (lio_setup_sc_buffer_pool(lio_dev)) {
1223                 lio_dev_err(lio_dev, "sc buffer pool allocation failed\n");
1224                 return -1;
1225         }
1226
1227         /* Initialize lists to manage the requests of different types that
1228          * arrive from applications for this lio device.
1229          */
1230         lio_setup_response_list(lio_dev);
1231
1232         if (lio_dev->fn_list.setup_mbox(lio_dev)) {
1233                 lio_dev_err(lio_dev, "Mailbox setup failed\n");
1234                 goto error;
1235         }
1236
1237         /* Check PF response */
1238         lio_check_pf_hs_response((void *)lio_dev);
1239
1240         /* Do handshake and exit if incompatible PF driver */
1241         if (cn23xx_pfvf_handshake(lio_dev))
1242                 goto error;
1243
1244         /* Initial reset */
1245         cn23xx_vf_ask_pf_to_do_flr(lio_dev);
1246         /* Wait for FLR for 100ms per SRIOV specification */
1247         rte_delay_ms(100);
1248
1249         if (cn23xx_vf_set_io_queues_off(lio_dev)) {
1250                 lio_dev_err(lio_dev, "Setting io queues off failed\n");
1251                 goto error;
1252         }
1253
1254         if (lio_dev->fn_list.setup_device_regs(lio_dev)) {
1255                 lio_dev_err(lio_dev, "Failed to configure device registers\n");
1256                 goto error;
1257         }
1258
1259         if (lio_setup_instr_queue0(lio_dev)) {
1260                 lio_dev_err(lio_dev, "Failed to setup instruction queue 0\n");
1261                 goto error;
1262         }
1263
1264         dpdk_queues = (int)lio_dev->sriov_info.rings_per_vf;
1265
1266         lio_dev->max_tx_queues = dpdk_queues;
1267         lio_dev->max_rx_queues = dpdk_queues;
1268
1269         /* Enable input and output queues for this device */
1270         if (lio_dev->fn_list.enable_io_queues(lio_dev))
1271                 goto error;
1272
1273         return 0;
1274
1275 error:
1276         lio_free_sc_buffer_pool(lio_dev);
1277         if (lio_dev->mbox[0])
1278                 lio_dev->fn_list.free_mbox(lio_dev);
1279         if (lio_dev->instr_queue[0])
1280                 lio_free_instr_queue0(lio_dev);
1281
1282         return -1;
1283 }
1284
1285 static int
1286 lio_eth_dev_uninit(struct rte_eth_dev *eth_dev)
1287 {
1288         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1289
1290         PMD_INIT_FUNC_TRACE();
1291
1292         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1293                 return -EPERM;
1294
1295         /* lio_free_sc_buffer_pool */
1296         lio_free_sc_buffer_pool(lio_dev);
1297
1298         rte_free(eth_dev->data->mac_addrs);
1299         eth_dev->data->mac_addrs = NULL;
1300
1301         eth_dev->rx_pkt_burst = NULL;
1302         eth_dev->tx_pkt_burst = NULL;
1303
1304         return 0;
1305 }
1306
1307 static int
1308 lio_eth_dev_init(struct rte_eth_dev *eth_dev)
1309 {
1310         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(eth_dev->device);
1311         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1312
1313         PMD_INIT_FUNC_TRACE();
1314
1315         eth_dev->rx_pkt_burst = &lio_dev_recv_pkts;
1316         eth_dev->tx_pkt_burst = &lio_dev_xmit_pkts;
1317
1318         /* Primary does the initialization. */
1319         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1320                 return 0;
1321
1322         rte_eth_copy_pci_info(eth_dev, pdev);
1323         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
1324
1325         if (pdev->mem_resource[0].addr) {
1326                 lio_dev->hw_addr = pdev->mem_resource[0].addr;
1327         } else {
1328                 PMD_INIT_LOG(ERR, "ERROR: Failed to map BAR0\n");
1329                 return -ENODEV;
1330         }
1331
1332         lio_dev->eth_dev = eth_dev;
1333         /* set lio device print string */
1334         snprintf(lio_dev->dev_string, sizeof(lio_dev->dev_string),
1335                  "%s[%02x:%02x.%x]", pdev->driver->driver.name,
1336                  pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
1337
1338         lio_dev->port_id = eth_dev->data->port_id;
1339
1340         if (lio_first_time_init(lio_dev, pdev)) {
1341                 lio_dev_err(lio_dev, "Device init failed\n");
1342                 return -EINVAL;
1343         }
1344
1345         eth_dev->dev_ops = &liovf_eth_dev_ops;
1346         eth_dev->data->mac_addrs = rte_zmalloc("lio", ETHER_ADDR_LEN, 0);
1347         if (eth_dev->data->mac_addrs == NULL) {
1348                 lio_dev_err(lio_dev,
1349                             "MAC addresses memory allocation failed\n");
1350                 eth_dev->dev_ops = NULL;
1351                 eth_dev->rx_pkt_burst = NULL;
1352                 eth_dev->tx_pkt_burst = NULL;
1353                 return -ENOMEM;
1354         }
1355
1356         rte_atomic64_set(&lio_dev->status, LIO_DEV_RUNNING);
1357         rte_wmb();
1358
1359         lio_dev->port_configured = 0;
1360         /* Always allow unicast packets */
1361         lio_dev->ifflags |= LIO_IFFLAG_UNICAST;
1362
1363         return 0;
1364 }
1365
1366 /* Set of PCI devices this driver supports */
1367 static const struct rte_pci_id pci_id_liovf_map[] = {
1368         { RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_VF_VID) },
1369         { .vendor_id = 0, /* sentinel */ }
1370 };
1371
1372 static struct eth_driver rte_liovf_pmd = {
1373         .pci_drv = {
1374                 .id_table       = pci_id_liovf_map,
1375                 .drv_flags      = RTE_PCI_DRV_NEED_MAPPING,
1376                 .probe          = rte_eth_dev_pci_probe,
1377                 .remove         = rte_eth_dev_pci_remove,
1378         },
1379         .eth_dev_init           = lio_eth_dev_init,
1380         .eth_dev_uninit         = lio_eth_dev_uninit,
1381         .dev_private_size       = sizeof(struct lio_device),
1382 };
1383
1384 RTE_PMD_REGISTER_PCI(net_liovf, rte_liovf_pmd.pci_drv);
1385 RTE_PMD_REGISTER_PCI_TABLE(net_liovf, pci_id_liovf_map);
1386 RTE_PMD_REGISTER_KMOD_DEP(net_liovf, "* igb_uio | vfio");