net/liquidio: add API to get device info
[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_rss_reta_update(struct rte_eth_dev *eth_dev,
156                         struct rte_eth_rss_reta_entry64 *reta_conf,
157                         uint16_t reta_size)
158 {
159         struct lio_device *lio_dev = LIO_DEV(eth_dev);
160         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
161         struct lio_rss_set *rss_param;
162         struct lio_dev_ctrl_cmd ctrl_cmd;
163         struct lio_ctrl_pkt ctrl_pkt;
164         int i, j, index;
165
166         if (!lio_dev->intf_open) {
167                 lio_dev_err(lio_dev, "Port %d down, can't update reta\n",
168                             lio_dev->port_id);
169                 return -EINVAL;
170         }
171
172         if (reta_size != LIO_RSS_MAX_TABLE_SZ) {
173                 lio_dev_err(lio_dev,
174                             "The size of hash lookup table configured (%d) doesn't match the number hardware can supported (%d)\n",
175                             reta_size, LIO_RSS_MAX_TABLE_SZ);
176                 return -EINVAL;
177         }
178
179         /* flush added to prevent cmd failure
180          * incase the queue is full
181          */
182         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
183
184         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
185         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
186
187         rss_param = (struct lio_rss_set *)&ctrl_pkt.udd[0];
188
189         ctrl_cmd.eth_dev = eth_dev;
190         ctrl_cmd.cond = 0;
191
192         ctrl_pkt.ncmd.s.cmd = LIO_CMD_SET_RSS;
193         ctrl_pkt.ncmd.s.more = sizeof(struct lio_rss_set) >> 3;
194         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
195
196         rss_param->param.flags = 0xF;
197         rss_param->param.flags &= ~LIO_RSS_PARAM_ITABLE_UNCHANGED;
198         rss_param->param.itablesize = LIO_RSS_MAX_TABLE_SZ;
199
200         for (i = 0; i < (reta_size / RTE_RETA_GROUP_SIZE); i++) {
201                 for (j = 0; j < RTE_RETA_GROUP_SIZE; j++) {
202                         if ((reta_conf[i].mask) & ((uint64_t)1 << j)) {
203                                 index = (i * RTE_RETA_GROUP_SIZE) + j;
204                                 rss_state->itable[index] = reta_conf[i].reta[j];
205                         }
206                 }
207         }
208
209         rss_state->itable_size = LIO_RSS_MAX_TABLE_SZ;
210         memcpy(rss_param->itable, rss_state->itable, rss_state->itable_size);
211
212         lio_swap_8B_data((uint64_t *)rss_param, LIO_RSS_PARAM_SIZE >> 3);
213
214         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
215                 lio_dev_err(lio_dev, "Failed to set rss hash\n");
216                 return -1;
217         }
218
219         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
220                 lio_dev_err(lio_dev, "Set rss hash timed out\n");
221                 return -1;
222         }
223
224         return 0;
225 }
226
227 static int
228 lio_dev_rss_reta_query(struct rte_eth_dev *eth_dev,
229                        struct rte_eth_rss_reta_entry64 *reta_conf,
230                        uint16_t reta_size)
231 {
232         struct lio_device *lio_dev = LIO_DEV(eth_dev);
233         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
234         int i, num;
235
236         if (reta_size != LIO_RSS_MAX_TABLE_SZ) {
237                 lio_dev_err(lio_dev,
238                             "The size of hash lookup table configured (%d) doesn't match the number hardware can supported (%d)\n",
239                             reta_size, LIO_RSS_MAX_TABLE_SZ);
240                 return -EINVAL;
241         }
242
243         num = reta_size / RTE_RETA_GROUP_SIZE;
244
245         for (i = 0; i < num; i++) {
246                 memcpy(reta_conf->reta,
247                        &rss_state->itable[i * RTE_RETA_GROUP_SIZE],
248                        RTE_RETA_GROUP_SIZE);
249                 reta_conf++;
250         }
251
252         return 0;
253 }
254
255 static int
256 lio_dev_rss_hash_conf_get(struct rte_eth_dev *eth_dev,
257                           struct rte_eth_rss_conf *rss_conf)
258 {
259         struct lio_device *lio_dev = LIO_DEV(eth_dev);
260         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
261         uint8_t *hash_key = NULL;
262         uint64_t rss_hf = 0;
263
264         if (rss_state->hash_disable) {
265                 lio_dev_info(lio_dev, "RSS disabled in nic\n");
266                 rss_conf->rss_hf = 0;
267                 return 0;
268         }
269
270         /* Get key value */
271         hash_key = rss_conf->rss_key;
272         if (hash_key != NULL)
273                 memcpy(hash_key, rss_state->hash_key, rss_state->hash_key_size);
274
275         if (rss_state->ip)
276                 rss_hf |= ETH_RSS_IPV4;
277         if (rss_state->tcp_hash)
278                 rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
279         if (rss_state->ipv6)
280                 rss_hf |= ETH_RSS_IPV6;
281         if (rss_state->ipv6_tcp_hash)
282                 rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
283         if (rss_state->ipv6_ex)
284                 rss_hf |= ETH_RSS_IPV6_EX;
285         if (rss_state->ipv6_tcp_ex_hash)
286                 rss_hf |= ETH_RSS_IPV6_TCP_EX;
287
288         rss_conf->rss_hf = rss_hf;
289
290         return 0;
291 }
292
293 static int
294 lio_dev_rss_hash_update(struct rte_eth_dev *eth_dev,
295                         struct rte_eth_rss_conf *rss_conf)
296 {
297         struct lio_device *lio_dev = LIO_DEV(eth_dev);
298         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
299         struct lio_rss_set *rss_param;
300         struct lio_dev_ctrl_cmd ctrl_cmd;
301         struct lio_ctrl_pkt ctrl_pkt;
302
303         if (!lio_dev->intf_open) {
304                 lio_dev_err(lio_dev, "Port %d down, can't update hash\n",
305                             lio_dev->port_id);
306                 return -EINVAL;
307         }
308
309         /* flush added to prevent cmd failure
310          * incase the queue is full
311          */
312         lio_flush_iq(lio_dev, lio_dev->instr_queue[0]);
313
314         memset(&ctrl_pkt, 0, sizeof(struct lio_ctrl_pkt));
315         memset(&ctrl_cmd, 0, sizeof(struct lio_dev_ctrl_cmd));
316
317         rss_param = (struct lio_rss_set *)&ctrl_pkt.udd[0];
318
319         ctrl_cmd.eth_dev = eth_dev;
320         ctrl_cmd.cond = 0;
321
322         ctrl_pkt.ncmd.s.cmd = LIO_CMD_SET_RSS;
323         ctrl_pkt.ncmd.s.more = sizeof(struct lio_rss_set) >> 3;
324         ctrl_pkt.ctrl_cmd = &ctrl_cmd;
325
326         rss_param->param.flags = 0xF;
327
328         if (rss_conf->rss_key) {
329                 rss_param->param.flags &= ~LIO_RSS_PARAM_HASH_KEY_UNCHANGED;
330                 rss_state->hash_key_size = LIO_RSS_MAX_KEY_SZ;
331                 rss_param->param.hashkeysize = LIO_RSS_MAX_KEY_SZ;
332                 memcpy(rss_state->hash_key, rss_conf->rss_key,
333                        rss_state->hash_key_size);
334                 memcpy(rss_param->key, rss_state->hash_key,
335                        rss_state->hash_key_size);
336         }
337
338         if ((rss_conf->rss_hf & LIO_RSS_OFFLOAD_ALL) == 0) {
339                 /* Can't disable rss through hash flags,
340                  * if it is enabled by default during init
341                  */
342                 if (!rss_state->hash_disable)
343                         return -EINVAL;
344
345                 /* This is for --disable-rss during testpmd launch */
346                 rss_param->param.flags |= LIO_RSS_PARAM_DISABLE_RSS;
347         } else {
348                 uint32_t hashinfo = 0;
349
350                 /* Can't enable rss if disabled by default during init */
351                 if (rss_state->hash_disable)
352                         return -EINVAL;
353
354                 if (rss_conf->rss_hf & ETH_RSS_IPV4) {
355                         hashinfo |= LIO_RSS_HASH_IPV4;
356                         rss_state->ip = 1;
357                 } else {
358                         rss_state->ip = 0;
359                 }
360
361                 if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP) {
362                         hashinfo |= LIO_RSS_HASH_TCP_IPV4;
363                         rss_state->tcp_hash = 1;
364                 } else {
365                         rss_state->tcp_hash = 0;
366                 }
367
368                 if (rss_conf->rss_hf & ETH_RSS_IPV6) {
369                         hashinfo |= LIO_RSS_HASH_IPV6;
370                         rss_state->ipv6 = 1;
371                 } else {
372                         rss_state->ipv6 = 0;
373                 }
374
375                 if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP) {
376                         hashinfo |= LIO_RSS_HASH_TCP_IPV6;
377                         rss_state->ipv6_tcp_hash = 1;
378                 } else {
379                         rss_state->ipv6_tcp_hash = 0;
380                 }
381
382                 if (rss_conf->rss_hf & ETH_RSS_IPV6_EX) {
383                         hashinfo |= LIO_RSS_HASH_IPV6_EX;
384                         rss_state->ipv6_ex = 1;
385                 } else {
386                         rss_state->ipv6_ex = 0;
387                 }
388
389                 if (rss_conf->rss_hf & ETH_RSS_IPV6_TCP_EX) {
390                         hashinfo |= LIO_RSS_HASH_TCP_IPV6_EX;
391                         rss_state->ipv6_tcp_ex_hash = 1;
392                 } else {
393                         rss_state->ipv6_tcp_ex_hash = 0;
394                 }
395
396                 rss_param->param.flags &= ~LIO_RSS_PARAM_HASH_INFO_UNCHANGED;
397                 rss_param->param.hashinfo = hashinfo;
398         }
399
400         lio_swap_8B_data((uint64_t *)rss_param, LIO_RSS_PARAM_SIZE >> 3);
401
402         if (lio_send_ctrl_pkt(lio_dev, &ctrl_pkt)) {
403                 lio_dev_err(lio_dev, "Failed to set rss hash\n");
404                 return -1;
405         }
406
407         if (lio_wait_for_ctrl_cmd(lio_dev, &ctrl_cmd)) {
408                 lio_dev_err(lio_dev, "Set rss hash timed out\n");
409                 return -1;
410         }
411
412         return 0;
413 }
414
415 /**
416  * Atomically writes the link status information into global
417  * structure rte_eth_dev.
418  *
419  * @param eth_dev
420  *   - Pointer to the structure rte_eth_dev to read from.
421  *   - Pointer to the buffer to be saved with the link status.
422  *
423  * @return
424  *   - On success, zero.
425  *   - On failure, negative value.
426  */
427 static inline int
428 lio_dev_atomic_write_link_status(struct rte_eth_dev *eth_dev,
429                                  struct rte_eth_link *link)
430 {
431         struct rte_eth_link *dst = &eth_dev->data->dev_link;
432         struct rte_eth_link *src = link;
433
434         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
435                                 *(uint64_t *)src) == 0)
436                 return -1;
437
438         return 0;
439 }
440
441 static uint64_t
442 lio_hweight64(uint64_t w)
443 {
444         uint64_t res = w - ((w >> 1) & 0x5555555555555555ul);
445
446         res =
447             (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
448         res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
449         res = res + (res >> 8);
450         res = res + (res >> 16);
451
452         return (res + (res >> 32)) & 0x00000000000000FFul;
453 }
454
455 static int
456 lio_dev_link_update(struct rte_eth_dev *eth_dev,
457                     int wait_to_complete __rte_unused)
458 {
459         struct lio_device *lio_dev = LIO_DEV(eth_dev);
460         struct rte_eth_link link, old;
461
462         /* Initialize */
463         link.link_status = ETH_LINK_DOWN;
464         link.link_speed = ETH_SPEED_NUM_NONE;
465         link.link_duplex = ETH_LINK_HALF_DUPLEX;
466         memset(&old, 0, sizeof(old));
467
468         /* Return what we found */
469         if (lio_dev->linfo.link.s.link_up == 0) {
470                 /* Interface is down */
471                 if (lio_dev_atomic_write_link_status(eth_dev, &link))
472                         return -1;
473                 if (link.link_status == old.link_status)
474                         return -1;
475                 return 0;
476         }
477
478         link.link_status = ETH_LINK_UP; /* Interface is up */
479         link.link_duplex = ETH_LINK_FULL_DUPLEX;
480         switch (lio_dev->linfo.link.s.speed) {
481         case LIO_LINK_SPEED_10000:
482                 link.link_speed = ETH_SPEED_NUM_10G;
483                 break;
484         default:
485                 link.link_speed = ETH_SPEED_NUM_NONE;
486                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
487         }
488
489         if (lio_dev_atomic_write_link_status(eth_dev, &link))
490                 return -1;
491
492         if (link.link_status == old.link_status)
493                 return -1;
494
495         return 0;
496 }
497
498 static void
499 lio_dev_rss_configure(struct rte_eth_dev *eth_dev)
500 {
501         struct lio_device *lio_dev = LIO_DEV(eth_dev);
502         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
503         struct rte_eth_rss_reta_entry64 reta_conf[8];
504         struct rte_eth_rss_conf rss_conf;
505         uint16_t i;
506
507         /* Configure the RSS key and the RSS protocols used to compute
508          * the RSS hash of input packets.
509          */
510         rss_conf = eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
511         if ((rss_conf.rss_hf & LIO_RSS_OFFLOAD_ALL) == 0) {
512                 rss_state->hash_disable = 1;
513                 lio_dev_rss_hash_update(eth_dev, &rss_conf);
514                 return;
515         }
516
517         if (rss_conf.rss_key == NULL)
518                 rss_conf.rss_key = lio_rss_key; /* Default hash key */
519
520         lio_dev_rss_hash_update(eth_dev, &rss_conf);
521
522         memset(reta_conf, 0, sizeof(reta_conf));
523         for (i = 0; i < LIO_RSS_MAX_TABLE_SZ; i++) {
524                 uint8_t q_idx, conf_idx, reta_idx;
525
526                 q_idx = (uint8_t)((eth_dev->data->nb_rx_queues > 1) ?
527                                   i % eth_dev->data->nb_rx_queues : 0);
528                 conf_idx = i / RTE_RETA_GROUP_SIZE;
529                 reta_idx = i % RTE_RETA_GROUP_SIZE;
530                 reta_conf[conf_idx].reta[reta_idx] = q_idx;
531                 reta_conf[conf_idx].mask |= ((uint64_t)1 << reta_idx);
532         }
533
534         lio_dev_rss_reta_update(eth_dev, reta_conf, LIO_RSS_MAX_TABLE_SZ);
535 }
536
537 static void
538 lio_dev_mq_rx_configure(struct rte_eth_dev *eth_dev)
539 {
540         struct lio_device *lio_dev = LIO_DEV(eth_dev);
541         struct lio_rss_ctx *rss_state = &lio_dev->rss_state;
542         struct rte_eth_rss_conf rss_conf;
543
544         switch (eth_dev->data->dev_conf.rxmode.mq_mode) {
545         case ETH_MQ_RX_RSS:
546                 lio_dev_rss_configure(eth_dev);
547                 break;
548         case ETH_MQ_RX_NONE:
549         /* if mq_mode is none, disable rss mode. */
550         default:
551                 memset(&rss_conf, 0, sizeof(rss_conf));
552                 rss_state->hash_disable = 1;
553                 lio_dev_rss_hash_update(eth_dev, &rss_conf);
554         }
555 }
556
557 /**
558  * Setup our receive queue/ringbuffer. This is the
559  * queue the Octeon uses to send us packets and
560  * responses. We are given a memory pool for our
561  * packet buffers that are used to populate the receive
562  * queue.
563  *
564  * @param eth_dev
565  *    Pointer to the structure rte_eth_dev
566  * @param q_no
567  *    Queue number
568  * @param num_rx_descs
569  *    Number of entries in the queue
570  * @param socket_id
571  *    Where to allocate memory
572  * @param rx_conf
573  *    Pointer to the struction rte_eth_rxconf
574  * @param mp
575  *    Pointer to the packet pool
576  *
577  * @return
578  *    - On success, return 0
579  *    - On failure, return -1
580  */
581 static int
582 lio_dev_rx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
583                        uint16_t num_rx_descs, unsigned int socket_id,
584                        const struct rte_eth_rxconf *rx_conf __rte_unused,
585                        struct rte_mempool *mp)
586 {
587         struct lio_device *lio_dev = LIO_DEV(eth_dev);
588         struct rte_pktmbuf_pool_private *mbp_priv;
589         uint32_t fw_mapped_oq;
590         uint16_t buf_size;
591
592         if (q_no >= lio_dev->nb_rx_queues) {
593                 lio_dev_err(lio_dev, "Invalid rx queue number %u\n", q_no);
594                 return -EINVAL;
595         }
596
597         lio_dev_dbg(lio_dev, "setting up rx queue %u\n", q_no);
598
599         fw_mapped_oq = lio_dev->linfo.rxpciq[q_no].s.q_no;
600
601         if ((lio_dev->droq[fw_mapped_oq]) &&
602             (num_rx_descs != lio_dev->droq[fw_mapped_oq]->max_count)) {
603                 lio_dev_err(lio_dev,
604                             "Reconfiguring Rx descs not supported. Configure descs to same value %u or restart application\n",
605                             lio_dev->droq[fw_mapped_oq]->max_count);
606                 return -ENOTSUP;
607         }
608
609         mbp_priv = rte_mempool_get_priv(mp);
610         buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
611
612         if (lio_setup_droq(lio_dev, fw_mapped_oq, num_rx_descs, buf_size, mp,
613                            socket_id)) {
614                 lio_dev_err(lio_dev, "droq allocation failed\n");
615                 return -1;
616         }
617
618         eth_dev->data->rx_queues[q_no] = lio_dev->droq[fw_mapped_oq];
619
620         return 0;
621 }
622
623 /**
624  * Release the receive queue/ringbuffer. Called by
625  * the upper layers.
626  *
627  * @param rxq
628  *    Opaque pointer to the receive queue to release
629  *
630  * @return
631  *    - nothing
632  */
633 static void
634 lio_dev_rx_queue_release(void *rxq)
635 {
636         struct lio_droq *droq = rxq;
637         struct lio_device *lio_dev = droq->lio_dev;
638         int oq_no;
639
640         /* Run time queue deletion not supported */
641         if (lio_dev->port_configured)
642                 return;
643
644         if (droq != NULL) {
645                 oq_no = droq->q_no;
646                 lio_delete_droq_queue(droq->lio_dev, oq_no);
647         }
648 }
649
650 /**
651  * Allocate and initialize SW ring. Initialize associated HW registers.
652  *
653  * @param eth_dev
654  *   Pointer to structure rte_eth_dev
655  *
656  * @param q_no
657  *   Queue number
658  *
659  * @param num_tx_descs
660  *   Number of ringbuffer descriptors
661  *
662  * @param socket_id
663  *   NUMA socket id, used for memory allocations
664  *
665  * @param tx_conf
666  *   Pointer to the structure rte_eth_txconf
667  *
668  * @return
669  *   - On success, return 0
670  *   - On failure, return -errno value
671  */
672 static int
673 lio_dev_tx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
674                        uint16_t num_tx_descs, unsigned int socket_id,
675                        const struct rte_eth_txconf *tx_conf __rte_unused)
676 {
677         struct lio_device *lio_dev = LIO_DEV(eth_dev);
678         int fw_mapped_iq = lio_dev->linfo.txpciq[q_no].s.q_no;
679         int retval;
680
681         if (q_no >= lio_dev->nb_tx_queues) {
682                 lio_dev_err(lio_dev, "Invalid tx queue number %u\n", q_no);
683                 return -EINVAL;
684         }
685
686         lio_dev_dbg(lio_dev, "setting up tx queue %u\n", q_no);
687
688         if ((lio_dev->instr_queue[fw_mapped_iq] != NULL) &&
689             (num_tx_descs != lio_dev->instr_queue[fw_mapped_iq]->max_count)) {
690                 lio_dev_err(lio_dev,
691                             "Reconfiguring Tx descs not supported. Configure descs to same value %u or restart application\n",
692                             lio_dev->instr_queue[fw_mapped_iq]->max_count);
693                 return -ENOTSUP;
694         }
695
696         retval = lio_setup_iq(lio_dev, q_no, lio_dev->linfo.txpciq[q_no],
697                               num_tx_descs, lio_dev, socket_id);
698
699         if (retval) {
700                 lio_dev_err(lio_dev, "Runtime IQ(TxQ) creation failed.\n");
701                 return retval;
702         }
703
704         retval = lio_setup_sglists(lio_dev, q_no, fw_mapped_iq,
705                                 lio_dev->instr_queue[fw_mapped_iq]->max_count,
706                                 socket_id);
707
708         if (retval) {
709                 lio_delete_instruction_queue(lio_dev, fw_mapped_iq);
710                 return retval;
711         }
712
713         eth_dev->data->tx_queues[q_no] = lio_dev->instr_queue[fw_mapped_iq];
714
715         return 0;
716 }
717
718 /**
719  * Release the transmit queue/ringbuffer. Called by
720  * the upper layers.
721  *
722  * @param txq
723  *    Opaque pointer to the transmit queue to release
724  *
725  * @return
726  *    - nothing
727  */
728 static void
729 lio_dev_tx_queue_release(void *txq)
730 {
731         struct lio_instr_queue *tq = txq;
732         struct lio_device *lio_dev = tq->lio_dev;
733         uint32_t fw_mapped_iq_no;
734
735         /* Run time queue deletion not supported */
736         if (lio_dev->port_configured)
737                 return;
738
739         if (tq != NULL) {
740                 /* Free sg_list */
741                 lio_delete_sglist(tq);
742
743                 fw_mapped_iq_no = tq->txpciq.s.q_no;
744                 lio_delete_instruction_queue(tq->lio_dev, fw_mapped_iq_no);
745         }
746 }
747
748 /**
749  * Api to check link state.
750  */
751 static void
752 lio_dev_get_link_status(struct rte_eth_dev *eth_dev)
753 {
754         struct lio_device *lio_dev = LIO_DEV(eth_dev);
755         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
756         struct lio_link_status_resp *resp;
757         union octeon_link_status *ls;
758         struct lio_soft_command *sc;
759         uint32_t resp_size;
760
761         if (!lio_dev->intf_open)
762                 return;
763
764         resp_size = sizeof(struct lio_link_status_resp);
765         sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
766         if (sc == NULL)
767                 return;
768
769         resp = (struct lio_link_status_resp *)sc->virtrptr;
770         lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
771                                  LIO_OPCODE_INFO, 0, 0, 0);
772
773         /* Setting wait time in seconds */
774         sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
775
776         if (lio_send_soft_command(lio_dev, sc) == LIO_IQ_SEND_FAILED)
777                 goto get_status_fail;
778
779         while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
780                 lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
781                 rte_delay_ms(1);
782         }
783
784         if (resp->status)
785                 goto get_status_fail;
786
787         ls = &resp->link_info.link;
788
789         lio_swap_8B_data((uint64_t *)ls, sizeof(union octeon_link_status) >> 3);
790
791         if (lio_dev->linfo.link.link_status64 != ls->link_status64) {
792                 lio_dev->linfo.link.link_status64 = ls->link_status64;
793                 lio_dev_link_update(eth_dev, 0);
794         }
795
796         lio_free_soft_command(sc);
797
798         return;
799
800 get_status_fail:
801         lio_free_soft_command(sc);
802 }
803
804 /* This function will be invoked every LSC_TIMEOUT ns (100ms)
805  * and will update link state if it changes.
806  */
807 static void
808 lio_sync_link_state_check(void *eth_dev)
809 {
810         struct lio_device *lio_dev =
811                 (((struct rte_eth_dev *)eth_dev)->data->dev_private);
812
813         if (lio_dev->port_configured)
814                 lio_dev_get_link_status(eth_dev);
815
816         /* Schedule periodic link status check.
817          * Stop check if interface is close and start again while opening.
818          */
819         if (lio_dev->intf_open)
820                 rte_eal_alarm_set(LIO_LSC_TIMEOUT, lio_sync_link_state_check,
821                                   eth_dev);
822 }
823
824 static int
825 lio_dev_start(struct rte_eth_dev *eth_dev)
826 {
827         struct lio_device *lio_dev = LIO_DEV(eth_dev);
828         int ret = 0;
829
830         lio_dev_info(lio_dev, "Starting port %d\n", eth_dev->data->port_id);
831
832         if (lio_dev->fn_list.enable_io_queues(lio_dev))
833                 return -1;
834
835         if (lio_send_rx_ctrl_cmd(eth_dev, 1))
836                 return -1;
837
838         /* Ready for link status updates */
839         lio_dev->intf_open = 1;
840         rte_mb();
841
842         /* Configure RSS if device configured with multiple RX queues. */
843         lio_dev_mq_rx_configure(eth_dev);
844
845         /* start polling for lsc */
846         ret = rte_eal_alarm_set(LIO_LSC_TIMEOUT,
847                                 lio_sync_link_state_check,
848                                 eth_dev);
849         if (ret) {
850                 lio_dev_err(lio_dev,
851                             "link state check handler creation failed\n");
852                 goto dev_lsc_handle_error;
853         }
854
855         return 0;
856
857 dev_lsc_handle_error:
858         lio_dev->intf_open = 0;
859         lio_send_rx_ctrl_cmd(eth_dev, 0);
860
861         return ret;
862 }
863
864 static int lio_dev_configure(struct rte_eth_dev *eth_dev)
865 {
866         struct lio_device *lio_dev = LIO_DEV(eth_dev);
867         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
868         int retval, num_iqueues, num_oqueues;
869         uint8_t mac[ETHER_ADDR_LEN], i;
870         struct lio_if_cfg_resp *resp;
871         struct lio_soft_command *sc;
872         union lio_if_cfg if_cfg;
873         uint32_t resp_size;
874
875         PMD_INIT_FUNC_TRACE();
876
877         /* Re-configuring firmware not supported.
878          * Can't change tx/rx queues per port from initial value.
879          */
880         if (lio_dev->port_configured) {
881                 if ((lio_dev->nb_rx_queues != eth_dev->data->nb_rx_queues) ||
882                     (lio_dev->nb_tx_queues != eth_dev->data->nb_tx_queues)) {
883                         lio_dev_err(lio_dev,
884                                     "rxq/txq re-conf not supported. Restart application with new value.\n");
885                         return -ENOTSUP;
886                 }
887                 return 0;
888         }
889
890         lio_dev->nb_rx_queues = eth_dev->data->nb_rx_queues;
891         lio_dev->nb_tx_queues = eth_dev->data->nb_tx_queues;
892
893         resp_size = sizeof(struct lio_if_cfg_resp);
894         sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
895         if (sc == NULL)
896                 return -ENOMEM;
897
898         resp = (struct lio_if_cfg_resp *)sc->virtrptr;
899
900         /* Firmware doesn't have capability to reconfigure the queues,
901          * Claim all queues, and use as many required
902          */
903         if_cfg.if_cfg64 = 0;
904         if_cfg.s.num_iqueues = lio_dev->nb_tx_queues;
905         if_cfg.s.num_oqueues = lio_dev->nb_rx_queues;
906         if_cfg.s.base_queue = 0;
907
908         if_cfg.s.gmx_port_id = lio_dev->pf_num;
909
910         lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
911                                  LIO_OPCODE_IF_CFG, 0,
912                                  if_cfg.if_cfg64, 0);
913
914         /* Setting wait time in seconds */
915         sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
916
917         retval = lio_send_soft_command(lio_dev, sc);
918         if (retval == LIO_IQ_SEND_FAILED) {
919                 lio_dev_err(lio_dev, "iq/oq config failed status: %x\n",
920                             retval);
921                 /* Soft instr is freed by driver in case of failure. */
922                 goto nic_config_fail;
923         }
924
925         /* Sleep on a wait queue till the cond flag indicates that the
926          * response arrived or timed-out.
927          */
928         while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
929                 lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
930                 lio_process_ordered_list(lio_dev);
931                 rte_delay_ms(1);
932         }
933
934         retval = resp->status;
935         if (retval) {
936                 lio_dev_err(lio_dev, "iq/oq config failed\n");
937                 goto nic_config_fail;
938         }
939
940         lio_swap_8B_data((uint64_t *)(&resp->cfg_info),
941                          sizeof(struct octeon_if_cfg_info) >> 3);
942
943         num_iqueues = lio_hweight64(resp->cfg_info.iqmask);
944         num_oqueues = lio_hweight64(resp->cfg_info.oqmask);
945
946         if (!(num_iqueues) || !(num_oqueues)) {
947                 lio_dev_err(lio_dev,
948                             "Got bad iqueues (%016lx) or oqueues (%016lx) from firmware.\n",
949                             (unsigned long)resp->cfg_info.iqmask,
950                             (unsigned long)resp->cfg_info.oqmask);
951                 goto nic_config_fail;
952         }
953
954         lio_dev_dbg(lio_dev,
955                     "interface %d, iqmask %016lx, oqmask %016lx, numiqueues %d, numoqueues %d\n",
956                     eth_dev->data->port_id,
957                     (unsigned long)resp->cfg_info.iqmask,
958                     (unsigned long)resp->cfg_info.oqmask,
959                     num_iqueues, num_oqueues);
960
961         lio_dev->linfo.num_rxpciq = num_oqueues;
962         lio_dev->linfo.num_txpciq = num_iqueues;
963
964         for (i = 0; i < num_oqueues; i++) {
965                 lio_dev->linfo.rxpciq[i].rxpciq64 =
966                     resp->cfg_info.linfo.rxpciq[i].rxpciq64;
967                 lio_dev_dbg(lio_dev, "index %d OQ %d\n",
968                             i, lio_dev->linfo.rxpciq[i].s.q_no);
969         }
970
971         for (i = 0; i < num_iqueues; i++) {
972                 lio_dev->linfo.txpciq[i].txpciq64 =
973                     resp->cfg_info.linfo.txpciq[i].txpciq64;
974                 lio_dev_dbg(lio_dev, "index %d IQ %d\n",
975                             i, lio_dev->linfo.txpciq[i].s.q_no);
976         }
977
978         lio_dev->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
979         lio_dev->linfo.gmxport = resp->cfg_info.linfo.gmxport;
980         lio_dev->linfo.link.link_status64 =
981                         resp->cfg_info.linfo.link.link_status64;
982
983         /* 64-bit swap required on LE machines */
984         lio_swap_8B_data(&lio_dev->linfo.hw_addr, 1);
985         for (i = 0; i < ETHER_ADDR_LEN; i++)
986                 mac[i] = *((uint8_t *)(((uint8_t *)&lio_dev->linfo.hw_addr) +
987                                        2 + i));
988
989         /* Copy the permanent MAC address */
990         ether_addr_copy((struct ether_addr *)mac, &eth_dev->data->mac_addrs[0]);
991
992         lio_dev->glist_lock =
993             rte_zmalloc(NULL, sizeof(*lio_dev->glist_lock) * num_iqueues, 0);
994         if (lio_dev->glist_lock == NULL)
995                 return -ENOMEM;
996
997         lio_dev->glist_head =
998                 rte_zmalloc(NULL, sizeof(*lio_dev->glist_head) * num_iqueues,
999                             0);
1000         if (lio_dev->glist_head == NULL) {
1001                 rte_free(lio_dev->glist_lock);
1002                 lio_dev->glist_lock = NULL;
1003                 return -ENOMEM;
1004         }
1005
1006         lio_dev_link_update(eth_dev, 0);
1007
1008         lio_dev->port_configured = 1;
1009
1010         lio_free_soft_command(sc);
1011
1012         /* Disable iq_0 for reconf */
1013         lio_dev->fn_list.disable_io_queues(lio_dev);
1014
1015         /* Reset ioq regs */
1016         lio_dev->fn_list.setup_device_regs(lio_dev);
1017
1018         /* Free iq_0 used during init */
1019         lio_free_instr_queue0(lio_dev);
1020
1021         return 0;
1022
1023 nic_config_fail:
1024         lio_dev_err(lio_dev, "Failed retval %d\n", retval);
1025         lio_free_soft_command(sc);
1026         lio_free_instr_queue0(lio_dev);
1027
1028         return -ENODEV;
1029 }
1030
1031 /* Define our ethernet definitions */
1032 static const struct eth_dev_ops liovf_eth_dev_ops = {
1033         .dev_configure          = lio_dev_configure,
1034         .dev_start              = lio_dev_start,
1035         .link_update            = lio_dev_link_update,
1036         .dev_infos_get          = lio_dev_info_get,
1037         .rx_queue_setup         = lio_dev_rx_queue_setup,
1038         .rx_queue_release       = lio_dev_rx_queue_release,
1039         .tx_queue_setup         = lio_dev_tx_queue_setup,
1040         .tx_queue_release       = lio_dev_tx_queue_release,
1041         .reta_update            = lio_dev_rss_reta_update,
1042         .reta_query             = lio_dev_rss_reta_query,
1043         .rss_hash_conf_get      = lio_dev_rss_hash_conf_get,
1044         .rss_hash_update        = lio_dev_rss_hash_update,
1045 };
1046
1047 static void
1048 lio_check_pf_hs_response(void *lio_dev)
1049 {
1050         struct lio_device *dev = lio_dev;
1051
1052         /* check till response arrives */
1053         if (dev->pfvf_hsword.coproc_tics_per_us)
1054                 return;
1055
1056         cn23xx_vf_handle_mbox(dev);
1057
1058         rte_eal_alarm_set(1, lio_check_pf_hs_response, lio_dev);
1059 }
1060
1061 /**
1062  * \brief Identify the LIO device and to map the BAR address space
1063  * @param lio_dev lio device
1064  */
1065 static int
1066 lio_chip_specific_setup(struct lio_device *lio_dev)
1067 {
1068         struct rte_pci_device *pdev = lio_dev->pci_dev;
1069         uint32_t dev_id = pdev->id.device_id;
1070         const char *s;
1071         int ret = 1;
1072
1073         switch (dev_id) {
1074         case LIO_CN23XX_VF_VID:
1075                 lio_dev->chip_id = LIO_CN23XX_VF_VID;
1076                 ret = cn23xx_vf_setup_device(lio_dev);
1077                 s = "CN23XX VF";
1078                 break;
1079         default:
1080                 s = "?";
1081                 lio_dev_err(lio_dev, "Unsupported Chip\n");
1082         }
1083
1084         if (!ret)
1085                 lio_dev_info(lio_dev, "DEVICE : %s\n", s);
1086
1087         return ret;
1088 }
1089
1090 static int
1091 lio_first_time_init(struct lio_device *lio_dev,
1092                     struct rte_pci_device *pdev)
1093 {
1094         int dpdk_queues;
1095
1096         PMD_INIT_FUNC_TRACE();
1097
1098         /* set dpdk specific pci device pointer */
1099         lio_dev->pci_dev = pdev;
1100
1101         /* Identify the LIO type and set device ops */
1102         if (lio_chip_specific_setup(lio_dev)) {
1103                 lio_dev_err(lio_dev, "Chip specific setup failed\n");
1104                 return -1;
1105         }
1106
1107         /* Initialize soft command buffer pool */
1108         if (lio_setup_sc_buffer_pool(lio_dev)) {
1109                 lio_dev_err(lio_dev, "sc buffer pool allocation failed\n");
1110                 return -1;
1111         }
1112
1113         /* Initialize lists to manage the requests of different types that
1114          * arrive from applications for this lio device.
1115          */
1116         lio_setup_response_list(lio_dev);
1117
1118         if (lio_dev->fn_list.setup_mbox(lio_dev)) {
1119                 lio_dev_err(lio_dev, "Mailbox setup failed\n");
1120                 goto error;
1121         }
1122
1123         /* Check PF response */
1124         lio_check_pf_hs_response((void *)lio_dev);
1125
1126         /* Do handshake and exit if incompatible PF driver */
1127         if (cn23xx_pfvf_handshake(lio_dev))
1128                 goto error;
1129
1130         /* Initial reset */
1131         cn23xx_vf_ask_pf_to_do_flr(lio_dev);
1132         /* Wait for FLR for 100ms per SRIOV specification */
1133         rte_delay_ms(100);
1134
1135         if (cn23xx_vf_set_io_queues_off(lio_dev)) {
1136                 lio_dev_err(lio_dev, "Setting io queues off failed\n");
1137                 goto error;
1138         }
1139
1140         if (lio_dev->fn_list.setup_device_regs(lio_dev)) {
1141                 lio_dev_err(lio_dev, "Failed to configure device registers\n");
1142                 goto error;
1143         }
1144
1145         if (lio_setup_instr_queue0(lio_dev)) {
1146                 lio_dev_err(lio_dev, "Failed to setup instruction queue 0\n");
1147                 goto error;
1148         }
1149
1150         dpdk_queues = (int)lio_dev->sriov_info.rings_per_vf;
1151
1152         lio_dev->max_tx_queues = dpdk_queues;
1153         lio_dev->max_rx_queues = dpdk_queues;
1154
1155         /* Enable input and output queues for this device */
1156         if (lio_dev->fn_list.enable_io_queues(lio_dev))
1157                 goto error;
1158
1159         return 0;
1160
1161 error:
1162         lio_free_sc_buffer_pool(lio_dev);
1163         if (lio_dev->mbox[0])
1164                 lio_dev->fn_list.free_mbox(lio_dev);
1165         if (lio_dev->instr_queue[0])
1166                 lio_free_instr_queue0(lio_dev);
1167
1168         return -1;
1169 }
1170
1171 static int
1172 lio_eth_dev_uninit(struct rte_eth_dev *eth_dev)
1173 {
1174         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1175
1176         PMD_INIT_FUNC_TRACE();
1177
1178         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1179                 return -EPERM;
1180
1181         /* lio_free_sc_buffer_pool */
1182         lio_free_sc_buffer_pool(lio_dev);
1183
1184         rte_free(eth_dev->data->mac_addrs);
1185         eth_dev->data->mac_addrs = NULL;
1186
1187         eth_dev->rx_pkt_burst = NULL;
1188         eth_dev->tx_pkt_burst = NULL;
1189
1190         return 0;
1191 }
1192
1193 static int
1194 lio_eth_dev_init(struct rte_eth_dev *eth_dev)
1195 {
1196         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(eth_dev->device);
1197         struct lio_device *lio_dev = LIO_DEV(eth_dev);
1198
1199         PMD_INIT_FUNC_TRACE();
1200
1201         eth_dev->rx_pkt_burst = &lio_dev_recv_pkts;
1202         eth_dev->tx_pkt_burst = &lio_dev_xmit_pkts;
1203
1204         /* Primary does the initialization. */
1205         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1206                 return 0;
1207
1208         rte_eth_copy_pci_info(eth_dev, pdev);
1209         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
1210
1211         if (pdev->mem_resource[0].addr) {
1212                 lio_dev->hw_addr = pdev->mem_resource[0].addr;
1213         } else {
1214                 PMD_INIT_LOG(ERR, "ERROR: Failed to map BAR0\n");
1215                 return -ENODEV;
1216         }
1217
1218         lio_dev->eth_dev = eth_dev;
1219         /* set lio device print string */
1220         snprintf(lio_dev->dev_string, sizeof(lio_dev->dev_string),
1221                  "%s[%02x:%02x.%x]", pdev->driver->driver.name,
1222                  pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
1223
1224         lio_dev->port_id = eth_dev->data->port_id;
1225
1226         if (lio_first_time_init(lio_dev, pdev)) {
1227                 lio_dev_err(lio_dev, "Device init failed\n");
1228                 return -EINVAL;
1229         }
1230
1231         eth_dev->dev_ops = &liovf_eth_dev_ops;
1232         eth_dev->data->mac_addrs = rte_zmalloc("lio", ETHER_ADDR_LEN, 0);
1233         if (eth_dev->data->mac_addrs == NULL) {
1234                 lio_dev_err(lio_dev,
1235                             "MAC addresses memory allocation failed\n");
1236                 eth_dev->dev_ops = NULL;
1237                 eth_dev->rx_pkt_burst = NULL;
1238                 eth_dev->tx_pkt_burst = NULL;
1239                 return -ENOMEM;
1240         }
1241
1242         rte_atomic64_set(&lio_dev->status, LIO_DEV_RUNNING);
1243         rte_wmb();
1244
1245         lio_dev->port_configured = 0;
1246         /* Always allow unicast packets */
1247         lio_dev->ifflags |= LIO_IFFLAG_UNICAST;
1248
1249         return 0;
1250 }
1251
1252 /* Set of PCI devices this driver supports */
1253 static const struct rte_pci_id pci_id_liovf_map[] = {
1254         { RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_VF_VID) },
1255         { .vendor_id = 0, /* sentinel */ }
1256 };
1257
1258 static struct eth_driver rte_liovf_pmd = {
1259         .pci_drv = {
1260                 .id_table       = pci_id_liovf_map,
1261                 .drv_flags      = RTE_PCI_DRV_NEED_MAPPING,
1262                 .probe          = rte_eth_dev_pci_probe,
1263                 .remove         = rte_eth_dev_pci_remove,
1264         },
1265         .eth_dev_init           = lio_eth_dev_init,
1266         .eth_dev_uninit         = lio_eth_dev_uninit,
1267         .dev_private_size       = sizeof(struct lio_device),
1268 };
1269
1270 RTE_PMD_REGISTER_PCI(net_liovf, rte_liovf_pmd.pci_drv);
1271 RTE_PMD_REGISTER_PCI_TABLE(net_liovf, pci_id_liovf_map);
1272 RTE_PMD_REGISTER_KMOD_DEP(net_liovf, "* igb_uio | vfio");