net/liquidio: add API to validate VF MTU
[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 static void
526 lio_dev_rss_configure(struct rte_eth_dev *eth_dev)
527 {
528         struct lio_device *lio_dev = LIO_DEV(eth_dev);
529         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
530         struct rte_eth_rss_reta_entry64 reta_conf[8];
531         struct rte_eth_rss_conf rss_conf;
532         uint16_t i;
533
534         /* Configure the RSS key and the RSS protocols used to compute
535          * the RSS hash of input packets.
536          */
537         rss_conf = eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
538         if ((rss_conf.rss_hf & LIO_RSS_OFFLOAD_ALL) == 0) {
539                 rss_state->hash_disable = 1;
540                 lio_dev_rss_hash_update(eth_dev, &rss_conf);
541                 return;
542         }
543
544         if (rss_conf.rss_key == NULL)
545                 rss_conf.rss_key = lio_rss_key; /* Default hash key */
546
547         lio_dev_rss_hash_update(eth_dev, &rss_conf);
548
549         memset(reta_conf, 0, sizeof(reta_conf));
550         for (i = 0; i < LIO_RSS_MAX_TABLE_SZ; i++) {
551                 uint8_t q_idx, conf_idx, reta_idx;
552
553                 q_idx = (uint8_t)((eth_dev->data->nb_rx_queues > 1) ?
554                                   i % eth_dev->data->nb_rx_queues : 0);
555                 conf_idx = i / RTE_RETA_GROUP_SIZE;
556                 reta_idx = i % RTE_RETA_GROUP_SIZE;
557                 reta_conf[conf_idx].reta[reta_idx] = q_idx;
558                 reta_conf[conf_idx].mask |= ((uint64_t)1 << reta_idx);
559         }
560
561         lio_dev_rss_reta_update(eth_dev, reta_conf, LIO_RSS_MAX_TABLE_SZ);
562 }
563
564 static void
565 lio_dev_mq_rx_configure(struct rte_eth_dev *eth_dev)
566 {
567         struct lio_device *lio_dev = LIO_DEV(eth_dev);
568         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
569         struct rte_eth_rss_conf rss_conf;
570
571         switch (eth_dev->data->dev_conf.rxmode.mq_mode) {
572         case ETH_MQ_RX_RSS:
573                 lio_dev_rss_configure(eth_dev);
574                 break;
575         case ETH_MQ_RX_NONE:
576         /* if mq_mode is none, disable rss mode. */
577         default:
578                 memset(&rss_conf, 0, sizeof(rss_conf));
579                 rss_state->hash_disable = 1;
580                 lio_dev_rss_hash_update(eth_dev, &rss_conf);
581         }
582 }
583
584 /**
585  * Setup our receive queue/ringbuffer. This is the
586  * queue the Octeon uses to send us packets and
587  * responses. We are given a memory pool for our
588  * packet buffers that are used to populate the receive
589  * queue.
590  *
591  * @param eth_dev
592  *    Pointer to the structure rte_eth_dev
593  * @param q_no
594  *    Queue number
595  * @param num_rx_descs
596  *    Number of entries in the queue
597  * @param socket_id
598  *    Where to allocate memory
599  * @param rx_conf
600  *    Pointer to the struction rte_eth_rxconf
601  * @param mp
602  *    Pointer to the packet pool
603  *
604  * @return
605  *    - On success, return 0
606  *    - On failure, return -1
607  */
608 static int
609 lio_dev_rx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
610                        uint16_t num_rx_descs, unsigned int socket_id,
611                        const struct rte_eth_rxconf *rx_conf __rte_unused,
612                        struct rte_mempool *mp)
613 {
614         struct lio_device *lio_dev = LIO_DEV(eth_dev);
615         struct rte_pktmbuf_pool_private *mbp_priv;
616         uint32_t fw_mapped_oq;
617         uint16_t buf_size;
618
619         if (q_no >= lio_dev->nb_rx_queues) {
620                 lio_dev_err(lio_dev, "Invalid rx queue number %u\n", q_no);
621                 return -EINVAL;
622         }
623
624         lio_dev_dbg(lio_dev, "setting up rx queue %u\n", q_no);
625
626         fw_mapped_oq = lio_dev->linfo.rxpciq[q_no].s.q_no;
627
628         if ((lio_dev->droq[fw_mapped_oq]) &&
629             (num_rx_descs != lio_dev->droq[fw_mapped_oq]->max_count)) {
630                 lio_dev_err(lio_dev,
631                             "Reconfiguring Rx descs not supported. Configure descs to same value %u or restart application\n",
632                             lio_dev->droq[fw_mapped_oq]->max_count);
633                 return -ENOTSUP;
634         }
635
636         mbp_priv = rte_mempool_get_priv(mp);
637         buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
638
639         if (lio_setup_droq(lio_dev, fw_mapped_oq, num_rx_descs, buf_size, mp,
640                            socket_id)) {
641                 lio_dev_err(lio_dev, "droq allocation failed\n");
642                 return -1;
643         }
644
645         eth_dev->data->rx_queues[q_no] = lio_dev->droq[fw_mapped_oq];
646
647         return 0;
648 }
649
650 /**
651  * Release the receive queue/ringbuffer. Called by
652  * the upper layers.
653  *
654  * @param rxq
655  *    Opaque pointer to the receive queue to release
656  *
657  * @return
658  *    - nothing
659  */
660 static void
661 lio_dev_rx_queue_release(void *rxq)
662 {
663         struct lio_droq *droq = rxq;
664         struct lio_device *lio_dev = droq->lio_dev;
665         int oq_no;
666
667         /* Run time queue deletion not supported */
668         if (lio_dev->port_configured)
669                 return;
670
671         if (droq != NULL) {
672                 oq_no = droq->q_no;
673                 lio_delete_droq_queue(droq->lio_dev, oq_no);
674         }
675 }
676
677 /**
678  * Allocate and initialize SW ring. Initialize associated HW registers.
679  *
680  * @param eth_dev
681  *   Pointer to structure rte_eth_dev
682  *
683  * @param q_no
684  *   Queue number
685  *
686  * @param num_tx_descs
687  *   Number of ringbuffer descriptors
688  *
689  * @param socket_id
690  *   NUMA socket id, used for memory allocations
691  *
692  * @param tx_conf
693  *   Pointer to the structure rte_eth_txconf
694  *
695  * @return
696  *   - On success, return 0
697  *   - On failure, return -errno value
698  */
699 static int
700 lio_dev_tx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
701                        uint16_t num_tx_descs, unsigned int socket_id,
702                        const struct rte_eth_txconf *tx_conf __rte_unused)
703 {
704         struct lio_device *lio_dev = LIO_DEV(eth_dev);
705         int fw_mapped_iq = lio_dev->linfo.txpciq[q_no].s.q_no;
706         int retval;
707
708         if (q_no >= lio_dev->nb_tx_queues) {
709                 lio_dev_err(lio_dev, "Invalid tx queue number %u\n", q_no);
710                 return -EINVAL;
711         }
712
713         lio_dev_dbg(lio_dev, "setting up tx queue %u\n", q_no);
714
715         if ((lio_dev->instr_queue[fw_mapped_iq] != NULL) &&
716             (num_tx_descs != lio_dev->instr_queue[fw_mapped_iq]->max_count)) {
717                 lio_dev_err(lio_dev,
718                             "Reconfiguring Tx descs not supported. Configure descs to same value %u or restart application\n",
719                             lio_dev->instr_queue[fw_mapped_iq]->max_count);
720                 return -ENOTSUP;
721         }
722
723         retval = lio_setup_iq(lio_dev, q_no, lio_dev->linfo.txpciq[q_no],
724                               num_tx_descs, lio_dev, socket_id);
725
726         if (retval) {
727                 lio_dev_err(lio_dev, "Runtime IQ(TxQ) creation failed.\n");
728                 return retval;
729         }
730
731         retval = lio_setup_sglists(lio_dev, q_no, fw_mapped_iq,
732                                 lio_dev->instr_queue[fw_mapped_iq]->max_count,
733                                 socket_id);
734
735         if (retval) {
736                 lio_delete_instruction_queue(lio_dev, fw_mapped_iq);
737                 return retval;
738         }
739
740         eth_dev->data->tx_queues[q_no] = lio_dev->instr_queue[fw_mapped_iq];
741
742         return 0;
743 }
744
745 /**
746  * Release the transmit queue/ringbuffer. Called by
747  * the upper layers.
748  *
749  * @param txq
750  *    Opaque pointer to the transmit queue to release
751  *
752  * @return
753  *    - nothing
754  */
755 static void
756 lio_dev_tx_queue_release(void *txq)
757 {
758         struct lio_instr_queue *tq = txq;
759         struct lio_device *lio_dev = tq->lio_dev;
760         uint32_t fw_mapped_iq_no;
761
762         /* Run time queue deletion not supported */
763         if (lio_dev->port_configured)
764                 return;
765
766         if (tq != NULL) {
767                 /* Free sg_list */
768                 lio_delete_sglist(tq);
769
770                 fw_mapped_iq_no = tq->txpciq.s.q_no;
771                 lio_delete_instruction_queue(tq->lio_dev, fw_mapped_iq_no);
772         }
773 }
774
775 /**
776  * Api to check link state.
777  */
778 static void
779 lio_dev_get_link_status(struct rte_eth_dev *eth_dev)
780 {
781         struct lio_device *lio_dev = LIO_DEV(eth_dev);
782         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
783         struct lio_link_status_resp *resp;
784         union octeon_link_status *ls;
785         struct lio_soft_command *sc;
786         uint32_t resp_size;
787
788         if (!lio_dev->intf_open)
789                 return;
790
791         resp_size = sizeof(struct lio_link_status_resp);
792         sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
793         if (sc == NULL)
794                 return;
795
796         resp = (struct lio_link_status_resp *)sc->virtrptr;
797         lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
798                                  LIO_OPCODE_INFO, 0, 0, 0);
799
800         /* Setting wait time in seconds */
801         sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
802
803         if (lio_send_soft_command(lio_dev, sc) == LIO_IQ_SEND_FAILED)
804                 goto get_status_fail;
805
806         while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
807                 lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
808                 rte_delay_ms(1);
809         }
810
811         if (resp->status)
812                 goto get_status_fail;
813
814         ls = &resp->link_info.link;
815
816         lio_swap_8B_data((uint64_t *)ls, sizeof(union octeon_link_status) >> 3);
817
818         if (lio_dev->linfo.link.link_status64 != ls->link_status64) {
819                 lio_dev->linfo.link.link_status64 = ls->link_status64;
820                 lio_dev_link_update(eth_dev, 0);
821         }
822
823         lio_free_soft_command(sc);
824
825         return;
826
827 get_status_fail:
828         lio_free_soft_command(sc);
829 }
830
831 /* This function will be invoked every LSC_TIMEOUT ns (100ms)
832  * and will update link state if it changes.
833  */
834 static void
835 lio_sync_link_state_check(void *eth_dev)
836 {
837         struct lio_device *lio_dev =
838                 (((struct rte_eth_dev *)eth_dev)->data->dev_private);
839
840         if (lio_dev->port_configured)
841                 lio_dev_get_link_status(eth_dev);
842
843         /* Schedule periodic link status check.
844          * Stop check if interface is close and start again while opening.
845          */
846         if (lio_dev->intf_open)
847                 rte_eal_alarm_set(LIO_LSC_TIMEOUT, lio_sync_link_state_check,
848                                   eth_dev);
849 }
850
851 static int
852 lio_dev_start(struct rte_eth_dev *eth_dev)
853 {
854         uint16_t mtu = eth_dev->data->dev_conf.rxmode.max_rx_pkt_len;
855         struct lio_device *lio_dev = LIO_DEV(eth_dev);
856         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
857         int ret = 0;
858
859         lio_dev_info(lio_dev, "Starting port %d\n", eth_dev->data->port_id);
860
861         if (lio_dev->fn_list.enable_io_queues(lio_dev))
862                 return -1;
863
864         if (lio_send_rx_ctrl_cmd(eth_dev, 1))
865                 return -1;
866
867         /* Ready for link status updates */
868         lio_dev->intf_open = 1;
869         rte_mb();
870
871         /* Configure RSS if device configured with multiple RX queues. */
872         lio_dev_mq_rx_configure(eth_dev);
873
874         /* start polling for lsc */
875         ret = rte_eal_alarm_set(LIO_LSC_TIMEOUT,
876                                 lio_sync_link_state_check,
877                                 eth_dev);
878         if (ret) {
879                 lio_dev_err(lio_dev,
880                             "link state check handler creation failed\n");
881                 goto dev_lsc_handle_error;
882         }
883
884         while ((lio_dev->linfo.link.link_status64 == 0) && (--timeout))
885                 rte_delay_ms(1);
886
887         if (lio_dev->linfo.link.link_status64 == 0) {
888                 ret = -1;
889                 goto dev_mtu_check_error;
890         }
891
892         if (lio_dev->linfo.link.s.mtu != mtu) {
893                 ret = lio_dev_validate_vf_mtu(eth_dev, mtu);
894                 if (ret)
895                         goto dev_mtu_check_error;
896         }
897
898         return 0;
899
900 dev_mtu_check_error:
901         rte_eal_alarm_cancel(lio_sync_link_state_check, eth_dev);
902
903 dev_lsc_handle_error:
904         lio_dev->intf_open = 0;
905         lio_send_rx_ctrl_cmd(eth_dev, 0);
906
907         return ret;
908 }
909
910 static int lio_dev_configure(struct rte_eth_dev *eth_dev)
911 {
912         struct lio_device *lio_dev = LIO_DEV(eth_dev);
913         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
914         int retval, num_iqueues, num_oqueues;
915         uint8_t mac[ETHER_ADDR_LEN], i;
916         struct lio_if_cfg_resp *resp;
917         struct lio_soft_command *sc;
918         union lio_if_cfg if_cfg;
919         uint32_t resp_size;
920
921         PMD_INIT_FUNC_TRACE();
922
923         /* Re-configuring firmware not supported.
924          * Can't change tx/rx queues per port from initial value.
925          */
926         if (lio_dev->port_configured) {
927                 if ((lio_dev->nb_rx_queues != eth_dev->data->nb_rx_queues) ||
928                     (lio_dev->nb_tx_queues != eth_dev->data->nb_tx_queues)) {
929                         lio_dev_err(lio_dev,
930                                     "rxq/txq re-conf not supported. Restart application with new value.\n");
931                         return -ENOTSUP;
932                 }
933                 return 0;
934         }
935
936         lio_dev->nb_rx_queues = eth_dev->data->nb_rx_queues;
937         lio_dev->nb_tx_queues = eth_dev->data->nb_tx_queues;
938
939         resp_size = sizeof(struct lio_if_cfg_resp);
940         sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
941         if (sc == NULL)
942                 return -ENOMEM;
943
944         resp = (struct lio_if_cfg_resp *)sc->virtrptr;
945
946         /* Firmware doesn't have capability to reconfigure the queues,
947          * Claim all queues, and use as many required
948          */
949         if_cfg.if_cfg64 = 0;
950         if_cfg.s.num_iqueues = lio_dev->nb_tx_queues;
951         if_cfg.s.num_oqueues = lio_dev->nb_rx_queues;
952         if_cfg.s.base_queue = 0;
953
954         if_cfg.s.gmx_port_id = lio_dev->pf_num;
955
956         lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
957                                  LIO_OPCODE_IF_CFG, 0,
958                                  if_cfg.if_cfg64, 0);
959
960         /* Setting wait time in seconds */
961         sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
962
963         retval = lio_send_soft_command(lio_dev, sc);
964         if (retval == LIO_IQ_SEND_FAILED) {
965                 lio_dev_err(lio_dev, "iq/oq config failed status: %x\n",
966                             retval);
967                 /* Soft instr is freed by driver in case of failure. */
968                 goto nic_config_fail;
969         }
970
971         /* Sleep on a wait queue till the cond flag indicates that the
972          * response arrived or timed-out.
973          */
974         while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
975                 lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
976                 lio_process_ordered_list(lio_dev);
977                 rte_delay_ms(1);
978         }
979
980         retval = resp->status;
981         if (retval) {
982                 lio_dev_err(lio_dev, "iq/oq config failed\n");
983                 goto nic_config_fail;
984         }
985
986         lio_swap_8B_data((uint64_t *)(&resp->cfg_info),
987                          sizeof(struct octeon_if_cfg_info) >> 3);
988
989         num_iqueues = lio_hweight64(resp->cfg_info.iqmask);
990         num_oqueues = lio_hweight64(resp->cfg_info.oqmask);
991
992         if (!(num_iqueues) || !(num_oqueues)) {
993                 lio_dev_err(lio_dev,
994                             "Got bad iqueues (%016lx) or oqueues (%016lx) from firmware.\n",
995                             (unsigned long)resp->cfg_info.iqmask,
996                             (unsigned long)resp->cfg_info.oqmask);
997                 goto nic_config_fail;
998         }
999
1000         lio_dev_dbg(lio_dev,
1001                     "interface %d, iqmask %016lx, oqmask %016lx, numiqueues %d, numoqueues %d\n",
1002                     eth_dev->data->port_id,
1003                     (unsigned long)resp->cfg_info.iqmask,
1004                     (unsigned long)resp->cfg_info.oqmask,
1005                     num_iqueues, num_oqueues);
1006
1007         lio_dev->linfo.num_rxpciq = num_oqueues;
1008         lio_dev->linfo.num_txpciq = num_iqueues;
1009
1010         for (i = 0; i < num_oqueues; i++) {
1011                 lio_dev->linfo.rxpciq[i].rxpciq64 =
1012                     resp->cfg_info.linfo.rxpciq[i].rxpciq64;
1013                 lio_dev_dbg(lio_dev, "index %d OQ %d\n",
1014                             i, lio_dev->linfo.rxpciq[i].s.q_no);
1015         }
1016
1017         for (i = 0; i < num_iqueues; i++) {
1018                 lio_dev->linfo.txpciq[i].txpciq64 =
1019                     resp->cfg_info.linfo.txpciq[i].txpciq64;
1020                 lio_dev_dbg(lio_dev, "index %d IQ %d\n",
1021                             i, lio_dev->linfo.txpciq[i].s.q_no);
1022         }
1023
1024         lio_dev->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
1025         lio_dev->linfo.gmxport = resp->cfg_info.linfo.gmxport;
1026         lio_dev->linfo.link.link_status64 =
1027                         resp->cfg_info.linfo.link.link_status64;
1028
1029         /* 64-bit swap required on LE machines */
1030         lio_swap_8B_data(&lio_dev->linfo.hw_addr, 1);
1031         for (i = 0; i < ETHER_ADDR_LEN; i++)
1032                 mac[i] = *((uint8_t *)(((uint8_t *)&lio_dev->linfo.hw_addr) +
1033                                        2 + i));
1034
1035         /* Copy the permanent MAC address */
1036         ether_addr_copy((struct ether_addr *)mac, &eth_dev->data->mac_addrs[0]);
1037
1038         lio_dev->glist_lock =
1039             rte_zmalloc(NULL, sizeof(*lio_dev->glist_lock) * num_iqueues, 0);
1040         if (lio_dev->glist_lock == NULL)
1041                 return -ENOMEM;
1042
1043         lio_dev->glist_head =
1044                 rte_zmalloc(NULL, sizeof(*lio_dev->glist_head) * num_iqueues,
1045                             0);
1046         if (lio_dev->glist_head == NULL) {
1047                 rte_free(lio_dev->glist_lock);
1048                 lio_dev->glist_lock = NULL;
1049                 return -ENOMEM;
1050         }
1051
1052         lio_dev_link_update(eth_dev, 0);
1053
1054         lio_dev->port_configured = 1;
1055
1056         lio_free_soft_command(sc);
1057
1058         /* Disable iq_0 for reconf */
1059         lio_dev->fn_list.disable_io_queues(lio_dev);
1060
1061         /* Reset ioq regs */
1062         lio_dev->fn_list.setup_device_regs(lio_dev);
1063
1064         /* Free iq_0 used during init */
1065         lio_free_instr_queue0(lio_dev);
1066
1067         return 0;
1068
1069 nic_config_fail:
1070         lio_dev_err(lio_dev, "Failed retval %d\n", retval);
1071         lio_free_soft_command(sc);
1072         lio_free_instr_queue0(lio_dev);
1073
1074         return -ENODEV;
1075 }
1076
1077 /* Define our ethernet definitions */
1078 static const struct eth_dev_ops liovf_eth_dev_ops = {
1079         .dev_configure          = lio_dev_configure,
1080         .dev_start              = lio_dev_start,
1081         .link_update            = lio_dev_link_update,
1082         .dev_infos_get          = lio_dev_info_get,
1083         .rx_queue_setup         = lio_dev_rx_queue_setup,
1084         .rx_queue_release       = lio_dev_rx_queue_release,
1085         .tx_queue_setup         = lio_dev_tx_queue_setup,
1086         .tx_queue_release       = lio_dev_tx_queue_release,
1087         .reta_update            = lio_dev_rss_reta_update,
1088         .reta_query             = lio_dev_rss_reta_query,
1089         .rss_hash_conf_get      = lio_dev_rss_hash_conf_get,
1090         .rss_hash_update        = lio_dev_rss_hash_update,
1091 };
1092
1093 static void
1094 lio_check_pf_hs_response(void *lio_dev)
1095 {
1096         struct lio_device *dev = lio_dev;
1097
1098         /* check till response arrives */
1099         if (dev->pfvf_hsword.coproc_tics_per_us)
1100                 return;
1101
1102         cn23xx_vf_handle_mbox(dev);
1103
1104         rte_eal_alarm_set(1, lio_check_pf_hs_response, lio_dev);
1105 }
1106
1107 /**
1108  * \brief Identify the LIO device and to map the BAR address space
1109  * @param lio_dev lio device
1110  */
1111 static int
1112 lio_chip_specific_setup(struct lio_device *lio_dev)
1113 {
1114         struct rte_pci_device *pdev = lio_dev->pci_dev;
1115         uint32_t dev_id = pdev->id.device_id;
1116         const char *s;
1117         int ret = 1;
1118
1119         switch (dev_id) {
1120         case LIO_CN23XX_VF_VID:
1121                 lio_dev->chip_id = LIO_CN23XX_VF_VID;
1122                 ret = cn23xx_vf_setup_device(lio_dev);
1123                 s = "CN23XX VF";
1124                 break;
1125         default:
1126                 s = "?";
1127                 lio_dev_err(lio_dev, "Unsupported Chip\n");
1128         }
1129
1130         if (!ret)
1131                 lio_dev_info(lio_dev, "DEVICE : %s\n", s);
1132
1133         return ret;
1134 }
1135
1136 static int
1137 lio_first_time_init(struct lio_device *lio_dev,
1138                     struct rte_pci_device *pdev)
1139 {
1140         int dpdk_queues;
1141
1142         PMD_INIT_FUNC_TRACE();
1143
1144         /* set dpdk specific pci device pointer */
1145         lio_dev->pci_dev = pdev;
1146
1147         /* Identify the LIO type and set device ops */
1148         if (lio_chip_specific_setup(lio_dev)) {
1149                 lio_dev_err(lio_dev, "Chip specific setup failed\n");
1150                 return -1;
1151         }
1152
1153         /* Initialize soft command buffer pool */
1154         if (lio_setup_sc_buffer_pool(lio_dev)) {
1155                 lio_dev_err(lio_dev, "sc buffer pool allocation failed\n");
1156                 return -1;
1157         }
1158
1159         /* Initialize lists to manage the requests of different types that
1160          * arrive from applications for this lio device.
1161          */
1162         lio_setup_response_list(lio_dev);
1163
1164         if (lio_dev->fn_list.setup_mbox(lio_dev)) {
1165                 lio_dev_err(lio_dev, "Mailbox setup failed\n");
1166                 goto error;
1167         }
1168
1169         /* Check PF response */
1170         lio_check_pf_hs_response((void *)lio_dev);
1171
1172         /* Do handshake and exit if incompatible PF driver */
1173         if (cn23xx_pfvf_handshake(lio_dev))
1174                 goto error;
1175
1176         /* Initial reset */
1177         cn23xx_vf_ask_pf_to_do_flr(lio_dev);
1178         /* Wait for FLR for 100ms per SRIOV specification */
1179         rte_delay_ms(100);
1180
1181         if (cn23xx_vf_set_io_queues_off(lio_dev)) {
1182                 lio_dev_err(lio_dev, "Setting io queues off failed\n");
1183                 goto error;
1184         }
1185
1186         if (lio_dev->fn_list.setup_device_regs(lio_dev)) {
1187                 lio_dev_err(lio_dev, "Failed to configure device registers\n");
1188                 goto error;
1189         }
1190
1191         if (lio_setup_instr_queue0(lio_dev)) {
1192                 lio_dev_err(lio_dev, "Failed to setup instruction queue 0\n");
1193                 goto error;
1194         }
1195
1196         dpdk_queues = (int)lio_dev->sriov_info.rings_per_vf;
1197
1198         lio_dev->max_tx_queues = dpdk_queues;
1199         lio_dev->max_rx_queues = dpdk_queues;
1200
1201         /* Enable input and output queues for this device */
1202         if (lio_dev->fn_list.enable_io_queues(lio_dev))
1203                 goto error;
1204
1205         return 0;
1206
1207 error:
1208         lio_free_sc_buffer_pool(lio_dev);
1209         if (lio_dev->mbox[0])
1210                 lio_dev->fn_list.free_mbox(lio_dev);
1211         if (lio_dev->instr_queue[0])
1212                 lio_free_instr_queue0(lio_dev);
1213
1214         return -1;
1215 }
1216
1217 static int
1218 lio_eth_dev_uninit(struct rte_eth_dev *eth_dev)
1219 {
1220         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1221
1222         PMD_INIT_FUNC_TRACE();
1223
1224         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1225                 return -EPERM;
1226
1227         /* lio_free_sc_buffer_pool */
1228         lio_free_sc_buffer_pool(lio_dev);
1229
1230         rte_free(eth_dev->data->mac_addrs);
1231         eth_dev->data->mac_addrs = NULL;
1232
1233         eth_dev->rx_pkt_burst = NULL;
1234         eth_dev->tx_pkt_burst = NULL;
1235
1236         return 0;
1237 }
1238
1239 static int
1240 lio_eth_dev_init(struct rte_eth_dev *eth_dev)
1241 {
1242         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(eth_dev->device);
1243         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1244
1245         PMD_INIT_FUNC_TRACE();
1246
1247         eth_dev->rx_pkt_burst = &lio_dev_recv_pkts;
1248         eth_dev->tx_pkt_burst = &lio_dev_xmit_pkts;
1249
1250         /* Primary does the initialization. */
1251         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1252                 return 0;
1253
1254         rte_eth_copy_pci_info(eth_dev, pdev);
1255         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
1256
1257         if (pdev->mem_resource[0].addr) {
1258                 lio_dev->hw_addr = pdev->mem_resource[0].addr;
1259         } else {
1260                 PMD_INIT_LOG(ERR, "ERROR: Failed to map BAR0\n");
1261                 return -ENODEV;
1262         }
1263
1264         lio_dev->eth_dev = eth_dev;
1265         /* set lio device print string */
1266         snprintf(lio_dev->dev_string, sizeof(lio_dev->dev_string),
1267                  "%s[%02x:%02x.%x]", pdev->driver->driver.name,
1268                  pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
1269
1270         lio_dev->port_id = eth_dev->data->port_id;
1271
1272         if (lio_first_time_init(lio_dev, pdev)) {
1273                 lio_dev_err(lio_dev, "Device init failed\n");
1274                 return -EINVAL;
1275         }
1276
1277         eth_dev->dev_ops = &liovf_eth_dev_ops;
1278         eth_dev->data->mac_addrs = rte_zmalloc("lio", ETHER_ADDR_LEN, 0);
1279         if (eth_dev->data->mac_addrs == NULL) {
1280                 lio_dev_err(lio_dev,
1281                             "MAC addresses memory allocation failed\n");
1282                 eth_dev->dev_ops = NULL;
1283                 eth_dev->rx_pkt_burst = NULL;
1284                 eth_dev->tx_pkt_burst = NULL;
1285                 return -ENOMEM;
1286         }
1287
1288         rte_atomic64_set(&lio_dev->status, LIO_DEV_RUNNING);
1289         rte_wmb();
1290
1291         lio_dev->port_configured = 0;
1292         /* Always allow unicast packets */
1293         lio_dev->ifflags |= LIO_IFFLAG_UNICAST;
1294
1295         return 0;
1296 }
1297
1298 /* Set of PCI devices this driver supports */
1299 static const struct rte_pci_id pci_id_liovf_map[] = {
1300         { RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_VF_VID) },
1301         { .vendor_id = 0, /* sentinel */ }
1302 };
1303
1304 static struct eth_driver rte_liovf_pmd = {
1305         .pci_drv = {
1306                 .id_table       = pci_id_liovf_map,
1307                 .drv_flags      = RTE_PCI_DRV_NEED_MAPPING,
1308                 .probe          = rte_eth_dev_pci_probe,
1309                 .remove         = rte_eth_dev_pci_remove,
1310         },
1311         .eth_dev_init           = lio_eth_dev_init,
1312         .eth_dev_uninit         = lio_eth_dev_uninit,
1313         .dev_private_size       = sizeof(struct lio_device),
1314 };
1315
1316 RTE_PMD_REGISTER_PCI(net_liovf, rte_liovf_pmd.pci_drv);
1317 RTE_PMD_REGISTER_PCI_TABLE(net_liovf, pci_id_liovf_map);
1318 RTE_PMD_REGISTER_KMOD_DEP(net_liovf, "* igb_uio | vfio");