net/liquidio: add APIs to start device and update link
[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 /**
45  * Atomically writes the link status information into global
46  * structure rte_eth_dev.
47  *
48  * @param eth_dev
49  *   - Pointer to the structure rte_eth_dev to read from.
50  *   - Pointer to the buffer to be saved with the link status.
51  *
52  * @return
53  *   - On success, zero.
54  *   - On failure, negative value.
55  */
56 static inline int
57 lio_dev_atomic_write_link_status(struct rte_eth_dev *eth_dev,
58                                  struct rte_eth_link *link)
59 {
60         struct rte_eth_link *dst = &eth_dev->data->dev_link;
61         struct rte_eth_link *src = link;
62
63         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
64                                 *(uint64_t *)src) == 0)
65                 return -1;
66
67         return 0;
68 }
69
70 static uint64_t
71 lio_hweight64(uint64_t w)
72 {
73         uint64_t res = w - ((w >> 1) & 0x5555555555555555ul);
74
75         res =
76             (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
77         res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
78         res = res + (res >> 8);
79         res = res + (res >> 16);
80
81         return (res + (res >> 32)) & 0x00000000000000FFul;
82 }
83
84 static int
85 lio_dev_link_update(struct rte_eth_dev *eth_dev,
86                     int wait_to_complete __rte_unused)
87 {
88         struct lio_device *lio_dev = LIO_DEV(eth_dev);
89         struct rte_eth_link link, old;
90
91         /* Initialize */
92         link.link_status = ETH_LINK_DOWN;
93         link.link_speed = ETH_SPEED_NUM_NONE;
94         link.link_duplex = ETH_LINK_HALF_DUPLEX;
95         memset(&old, 0, sizeof(old));
96
97         /* Return what we found */
98         if (lio_dev->linfo.link.s.link_up == 0) {
99                 /* Interface is down */
100                 if (lio_dev_atomic_write_link_status(eth_dev, &link))
101                         return -1;
102                 if (link.link_status == old.link_status)
103                         return -1;
104                 return 0;
105         }
106
107         link.link_status = ETH_LINK_UP; /* Interface is up */
108         link.link_duplex = ETH_LINK_FULL_DUPLEX;
109         switch (lio_dev->linfo.link.s.speed) {
110         case LIO_LINK_SPEED_10000:
111                 link.link_speed = ETH_SPEED_NUM_10G;
112                 break;
113         default:
114                 link.link_speed = ETH_SPEED_NUM_NONE;
115                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
116         }
117
118         if (lio_dev_atomic_write_link_status(eth_dev, &link))
119                 return -1;
120
121         if (link.link_status == old.link_status)
122                 return -1;
123
124         return 0;
125 }
126
127 /**
128  * Setup our receive queue/ringbuffer. This is the
129  * queue the Octeon uses to send us packets and
130  * responses. We are given a memory pool for our
131  * packet buffers that are used to populate the receive
132  * queue.
133  *
134  * @param eth_dev
135  *    Pointer to the structure rte_eth_dev
136  * @param q_no
137  *    Queue number
138  * @param num_rx_descs
139  *    Number of entries in the queue
140  * @param socket_id
141  *    Where to allocate memory
142  * @param rx_conf
143  *    Pointer to the struction rte_eth_rxconf
144  * @param mp
145  *    Pointer to the packet pool
146  *
147  * @return
148  *    - On success, return 0
149  *    - On failure, return -1
150  */
151 static int
152 lio_dev_rx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
153                        uint16_t num_rx_descs, unsigned int socket_id,
154                        const struct rte_eth_rxconf *rx_conf __rte_unused,
155                        struct rte_mempool *mp)
156 {
157         struct lio_device *lio_dev = LIO_DEV(eth_dev);
158         struct rte_pktmbuf_pool_private *mbp_priv;
159         uint32_t fw_mapped_oq;
160         uint16_t buf_size;
161
162         if (q_no >= lio_dev->nb_rx_queues) {
163                 lio_dev_err(lio_dev, "Invalid rx queue number %u\n", q_no);
164                 return -EINVAL;
165         }
166
167         lio_dev_dbg(lio_dev, "setting up rx queue %u\n", q_no);
168
169         fw_mapped_oq = lio_dev->linfo.rxpciq[q_no].s.q_no;
170
171         if ((lio_dev->droq[fw_mapped_oq]) &&
172             (num_rx_descs != lio_dev->droq[fw_mapped_oq]->max_count)) {
173                 lio_dev_err(lio_dev,
174                             "Reconfiguring Rx descs not supported. Configure descs to same value %u or restart application\n",
175                             lio_dev->droq[fw_mapped_oq]->max_count);
176                 return -ENOTSUP;
177         }
178
179         mbp_priv = rte_mempool_get_priv(mp);
180         buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
181
182         if (lio_setup_droq(lio_dev, fw_mapped_oq, num_rx_descs, buf_size, mp,
183                            socket_id)) {
184                 lio_dev_err(lio_dev, "droq allocation failed\n");
185                 return -1;
186         }
187
188         eth_dev->data->rx_queues[q_no] = lio_dev->droq[fw_mapped_oq];
189
190         return 0;
191 }
192
193 /**
194  * Release the receive queue/ringbuffer. Called by
195  * the upper layers.
196  *
197  * @param rxq
198  *    Opaque pointer to the receive queue to release
199  *
200  * @return
201  *    - nothing
202  */
203 static void
204 lio_dev_rx_queue_release(void *rxq)
205 {
206         struct lio_droq *droq = rxq;
207         struct lio_device *lio_dev = droq->lio_dev;
208         int oq_no;
209
210         /* Run time queue deletion not supported */
211         if (lio_dev->port_configured)
212                 return;
213
214         if (droq != NULL) {
215                 oq_no = droq->q_no;
216                 lio_delete_droq_queue(droq->lio_dev, oq_no);
217         }
218 }
219
220 /**
221  * Allocate and initialize SW ring. Initialize associated HW registers.
222  *
223  * @param eth_dev
224  *   Pointer to structure rte_eth_dev
225  *
226  * @param q_no
227  *   Queue number
228  *
229  * @param num_tx_descs
230  *   Number of ringbuffer descriptors
231  *
232  * @param socket_id
233  *   NUMA socket id, used for memory allocations
234  *
235  * @param tx_conf
236  *   Pointer to the structure rte_eth_txconf
237  *
238  * @return
239  *   - On success, return 0
240  *   - On failure, return -errno value
241  */
242 static int
243 lio_dev_tx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
244                        uint16_t num_tx_descs, unsigned int socket_id,
245                        const struct rte_eth_txconf *tx_conf __rte_unused)
246 {
247         struct lio_device *lio_dev = LIO_DEV(eth_dev);
248         int fw_mapped_iq = lio_dev->linfo.txpciq[q_no].s.q_no;
249         int retval;
250
251         if (q_no >= lio_dev->nb_tx_queues) {
252                 lio_dev_err(lio_dev, "Invalid tx queue number %u\n", q_no);
253                 return -EINVAL;
254         }
255
256         lio_dev_dbg(lio_dev, "setting up tx queue %u\n", q_no);
257
258         if ((lio_dev->instr_queue[fw_mapped_iq] != NULL) &&
259             (num_tx_descs != lio_dev->instr_queue[fw_mapped_iq]->max_count)) {
260                 lio_dev_err(lio_dev,
261                             "Reconfiguring Tx descs not supported. Configure descs to same value %u or restart application\n",
262                             lio_dev->instr_queue[fw_mapped_iq]->max_count);
263                 return -ENOTSUP;
264         }
265
266         retval = lio_setup_iq(lio_dev, q_no, lio_dev->linfo.txpciq[q_no],
267                               num_tx_descs, lio_dev, socket_id);
268
269         if (retval) {
270                 lio_dev_err(lio_dev, "Runtime IQ(TxQ) creation failed.\n");
271                 return retval;
272         }
273
274         retval = lio_setup_sglists(lio_dev, q_no, fw_mapped_iq,
275                                 lio_dev->instr_queue[fw_mapped_iq]->max_count,
276                                 socket_id);
277
278         if (retval) {
279                 lio_delete_instruction_queue(lio_dev, fw_mapped_iq);
280                 return retval;
281         }
282
283         eth_dev->data->tx_queues[q_no] = lio_dev->instr_queue[fw_mapped_iq];
284
285         return 0;
286 }
287
288 /**
289  * Release the transmit queue/ringbuffer. Called by
290  * the upper layers.
291  *
292  * @param txq
293  *    Opaque pointer to the transmit queue to release
294  *
295  * @return
296  *    - nothing
297  */
298 static void
299 lio_dev_tx_queue_release(void *txq)
300 {
301         struct lio_instr_queue *tq = txq;
302         struct lio_device *lio_dev = tq->lio_dev;
303         uint32_t fw_mapped_iq_no;
304
305         /* Run time queue deletion not supported */
306         if (lio_dev->port_configured)
307                 return;
308
309         if (tq != NULL) {
310                 /* Free sg_list */
311                 lio_delete_sglist(tq);
312
313                 fw_mapped_iq_no = tq->txpciq.s.q_no;
314                 lio_delete_instruction_queue(tq->lio_dev, fw_mapped_iq_no);
315         }
316 }
317
318 /**
319  * Api to check link state.
320  */
321 static void
322 lio_dev_get_link_status(struct rte_eth_dev *eth_dev)
323 {
324         struct lio_device *lio_dev = LIO_DEV(eth_dev);
325         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
326         struct lio_link_status_resp *resp;
327         union octeon_link_status *ls;
328         struct lio_soft_command *sc;
329         uint32_t resp_size;
330
331         if (!lio_dev->intf_open)
332                 return;
333
334         resp_size = sizeof(struct lio_link_status_resp);
335         sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
336         if (sc == NULL)
337                 return;
338
339         resp = (struct lio_link_status_resp *)sc->virtrptr;
340         lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
341                                  LIO_OPCODE_INFO, 0, 0, 0);
342
343         /* Setting wait time in seconds */
344         sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
345
346         if (lio_send_soft_command(lio_dev, sc) == LIO_IQ_SEND_FAILED)
347                 goto get_status_fail;
348
349         while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
350                 lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
351                 rte_delay_ms(1);
352         }
353
354         if (resp->status)
355                 goto get_status_fail;
356
357         ls = &resp->link_info.link;
358
359         lio_swap_8B_data((uint64_t *)ls, sizeof(union octeon_link_status) >> 3);
360
361         if (lio_dev->linfo.link.link_status64 != ls->link_status64) {
362                 lio_dev->linfo.link.link_status64 = ls->link_status64;
363                 lio_dev_link_update(eth_dev, 0);
364         }
365
366         lio_free_soft_command(sc);
367
368         return;
369
370 get_status_fail:
371         lio_free_soft_command(sc);
372 }
373
374 /* This function will be invoked every LSC_TIMEOUT ns (100ms)
375  * and will update link state if it changes.
376  */
377 static void
378 lio_sync_link_state_check(void *eth_dev)
379 {
380         struct lio_device *lio_dev =
381                 (((struct rte_eth_dev *)eth_dev)->data->dev_private);
382
383         if (lio_dev->port_configured)
384                 lio_dev_get_link_status(eth_dev);
385
386         /* Schedule periodic link status check.
387          * Stop check if interface is close and start again while opening.
388          */
389         if (lio_dev->intf_open)
390                 rte_eal_alarm_set(LIO_LSC_TIMEOUT, lio_sync_link_state_check,
391                                   eth_dev);
392 }
393
394 static int
395 lio_dev_start(struct rte_eth_dev *eth_dev)
396 {
397         struct lio_device *lio_dev = LIO_DEV(eth_dev);
398         int ret = 0;
399
400         lio_dev_info(lio_dev, "Starting port %d\n", eth_dev->data->port_id);
401
402         if (lio_dev->fn_list.enable_io_queues(lio_dev))
403                 return -1;
404
405         /* Ready for link status updates */
406         lio_dev->intf_open = 1;
407         rte_mb();
408
409         /* start polling for lsc */
410         ret = rte_eal_alarm_set(LIO_LSC_TIMEOUT,
411                                 lio_sync_link_state_check,
412                                 eth_dev);
413         if (ret) {
414                 lio_dev_err(lio_dev,
415                             "link state check handler creation failed\n");
416                 goto dev_lsc_handle_error;
417         }
418
419         return 0;
420
421 dev_lsc_handle_error:
422         lio_dev->intf_open = 0;
423
424         return ret;
425 }
426
427 static int lio_dev_configure(struct rte_eth_dev *eth_dev)
428 {
429         struct lio_device *lio_dev = LIO_DEV(eth_dev);
430         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
431         int retval, num_iqueues, num_oqueues;
432         uint8_t mac[ETHER_ADDR_LEN], i;
433         struct lio_if_cfg_resp *resp;
434         struct lio_soft_command *sc;
435         union lio_if_cfg if_cfg;
436         uint32_t resp_size;
437
438         PMD_INIT_FUNC_TRACE();
439
440         /* Re-configuring firmware not supported.
441          * Can't change tx/rx queues per port from initial value.
442          */
443         if (lio_dev->port_configured) {
444                 if ((lio_dev->nb_rx_queues != eth_dev->data->nb_rx_queues) ||
445                     (lio_dev->nb_tx_queues != eth_dev->data->nb_tx_queues)) {
446                         lio_dev_err(lio_dev,
447                                     "rxq/txq re-conf not supported. Restart application with new value.\n");
448                         return -ENOTSUP;
449                 }
450                 return 0;
451         }
452
453         lio_dev->nb_rx_queues = eth_dev->data->nb_rx_queues;
454         lio_dev->nb_tx_queues = eth_dev->data->nb_tx_queues;
455
456         resp_size = sizeof(struct lio_if_cfg_resp);
457         sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
458         if (sc == NULL)
459                 return -ENOMEM;
460
461         resp = (struct lio_if_cfg_resp *)sc->virtrptr;
462
463         /* Firmware doesn't have capability to reconfigure the queues,
464          * Claim all queues, and use as many required
465          */
466         if_cfg.if_cfg64 = 0;
467         if_cfg.s.num_iqueues = lio_dev->nb_tx_queues;
468         if_cfg.s.num_oqueues = lio_dev->nb_rx_queues;
469         if_cfg.s.base_queue = 0;
470
471         if_cfg.s.gmx_port_id = lio_dev->pf_num;
472
473         lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
474                                  LIO_OPCODE_IF_CFG, 0,
475                                  if_cfg.if_cfg64, 0);
476
477         /* Setting wait time in seconds */
478         sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
479
480         retval = lio_send_soft_command(lio_dev, sc);
481         if (retval == LIO_IQ_SEND_FAILED) {
482                 lio_dev_err(lio_dev, "iq/oq config failed status: %x\n",
483                             retval);
484                 /* Soft instr is freed by driver in case of failure. */
485                 goto nic_config_fail;
486         }
487
488         /* Sleep on a wait queue till the cond flag indicates that the
489          * response arrived or timed-out.
490          */
491         while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
492                 lio_flush_iq(lio_dev, lio_dev->instr_queue[sc->iq_no]);
493                 lio_process_ordered_list(lio_dev);
494                 rte_delay_ms(1);
495         }
496
497         retval = resp->status;
498         if (retval) {
499                 lio_dev_err(lio_dev, "iq/oq config failed\n");
500                 goto nic_config_fail;
501         }
502
503         lio_swap_8B_data((uint64_t *)(&resp->cfg_info),
504                          sizeof(struct octeon_if_cfg_info) >> 3);
505
506         num_iqueues = lio_hweight64(resp->cfg_info.iqmask);
507         num_oqueues = lio_hweight64(resp->cfg_info.oqmask);
508
509         if (!(num_iqueues) || !(num_oqueues)) {
510                 lio_dev_err(lio_dev,
511                             "Got bad iqueues (%016lx) or oqueues (%016lx) from firmware.\n",
512                             (unsigned long)resp->cfg_info.iqmask,
513                             (unsigned long)resp->cfg_info.oqmask);
514                 goto nic_config_fail;
515         }
516
517         lio_dev_dbg(lio_dev,
518                     "interface %d, iqmask %016lx, oqmask %016lx, numiqueues %d, numoqueues %d\n",
519                     eth_dev->data->port_id,
520                     (unsigned long)resp->cfg_info.iqmask,
521                     (unsigned long)resp->cfg_info.oqmask,
522                     num_iqueues, num_oqueues);
523
524         lio_dev->linfo.num_rxpciq = num_oqueues;
525         lio_dev->linfo.num_txpciq = num_iqueues;
526
527         for (i = 0; i < num_oqueues; i++) {
528                 lio_dev->linfo.rxpciq[i].rxpciq64 =
529                     resp->cfg_info.linfo.rxpciq[i].rxpciq64;
530                 lio_dev_dbg(lio_dev, "index %d OQ %d\n",
531                             i, lio_dev->linfo.rxpciq[i].s.q_no);
532         }
533
534         for (i = 0; i < num_iqueues; i++) {
535                 lio_dev->linfo.txpciq[i].txpciq64 =
536                     resp->cfg_info.linfo.txpciq[i].txpciq64;
537                 lio_dev_dbg(lio_dev, "index %d IQ %d\n",
538                             i, lio_dev->linfo.txpciq[i].s.q_no);
539         }
540
541         lio_dev->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
542         lio_dev->linfo.gmxport = resp->cfg_info.linfo.gmxport;
543         lio_dev->linfo.link.link_status64 =
544                         resp->cfg_info.linfo.link.link_status64;
545
546         /* 64-bit swap required on LE machines */
547         lio_swap_8B_data(&lio_dev->linfo.hw_addr, 1);
548         for (i = 0; i < ETHER_ADDR_LEN; i++)
549                 mac[i] = *((uint8_t *)(((uint8_t *)&lio_dev->linfo.hw_addr) +
550                                        2 + i));
551
552         /* Copy the permanent MAC address */
553         ether_addr_copy((struct ether_addr *)mac, &eth_dev->data->mac_addrs[0]);
554
555         lio_dev->glist_lock =
556             rte_zmalloc(NULL, sizeof(*lio_dev->glist_lock) * num_iqueues, 0);
557         if (lio_dev->glist_lock == NULL)
558                 return -ENOMEM;
559
560         lio_dev->glist_head =
561                 rte_zmalloc(NULL, sizeof(*lio_dev->glist_head) * num_iqueues,
562                             0);
563         if (lio_dev->glist_head == NULL) {
564                 rte_free(lio_dev->glist_lock);
565                 lio_dev->glist_lock = NULL;
566                 return -ENOMEM;
567         }
568
569         lio_dev_link_update(eth_dev, 0);
570
571         lio_dev->port_configured = 1;
572
573         lio_free_soft_command(sc);
574
575         /* Disable iq_0 for reconf */
576         lio_dev->fn_list.disable_io_queues(lio_dev);
577
578         /* Reset ioq regs */
579         lio_dev->fn_list.setup_device_regs(lio_dev);
580
581         /* Free iq_0 used during init */
582         lio_free_instr_queue0(lio_dev);
583
584         return 0;
585
586 nic_config_fail:
587         lio_dev_err(lio_dev, "Failed retval %d\n", retval);
588         lio_free_soft_command(sc);
589         lio_free_instr_queue0(lio_dev);
590
591         return -ENODEV;
592 }
593
594 /* Define our ethernet definitions */
595 static const struct eth_dev_ops liovf_eth_dev_ops = {
596         .dev_configure          = lio_dev_configure,
597         .dev_start              = lio_dev_start,
598         .link_update            = lio_dev_link_update,
599         .rx_queue_setup         = lio_dev_rx_queue_setup,
600         .rx_queue_release       = lio_dev_rx_queue_release,
601         .tx_queue_setup         = lio_dev_tx_queue_setup,
602         .tx_queue_release       = lio_dev_tx_queue_release,
603 };
604
605 static void
606 lio_check_pf_hs_response(void *lio_dev)
607 {
608         struct lio_device *dev = lio_dev;
609
610         /* check till response arrives */
611         if (dev->pfvf_hsword.coproc_tics_per_us)
612                 return;
613
614         cn23xx_vf_handle_mbox(dev);
615
616         rte_eal_alarm_set(1, lio_check_pf_hs_response, lio_dev);
617 }
618
619 /**
620  * \brief Identify the LIO device and to map the BAR address space
621  * @param lio_dev lio device
622  */
623 static int
624 lio_chip_specific_setup(struct lio_device *lio_dev)
625 {
626         struct rte_pci_device *pdev = lio_dev->pci_dev;
627         uint32_t dev_id = pdev->id.device_id;
628         const char *s;
629         int ret = 1;
630
631         switch (dev_id) {
632         case LIO_CN23XX_VF_VID:
633                 lio_dev->chip_id = LIO_CN23XX_VF_VID;
634                 ret = cn23xx_vf_setup_device(lio_dev);
635                 s = "CN23XX VF";
636                 break;
637         default:
638                 s = "?";
639                 lio_dev_err(lio_dev, "Unsupported Chip\n");
640         }
641
642         if (!ret)
643                 lio_dev_info(lio_dev, "DEVICE : %s\n", s);
644
645         return ret;
646 }
647
648 static int
649 lio_first_time_init(struct lio_device *lio_dev,
650                     struct rte_pci_device *pdev)
651 {
652         int dpdk_queues;
653
654         PMD_INIT_FUNC_TRACE();
655
656         /* set dpdk specific pci device pointer */
657         lio_dev->pci_dev = pdev;
658
659         /* Identify the LIO type and set device ops */
660         if (lio_chip_specific_setup(lio_dev)) {
661                 lio_dev_err(lio_dev, "Chip specific setup failed\n");
662                 return -1;
663         }
664
665         /* Initialize soft command buffer pool */
666         if (lio_setup_sc_buffer_pool(lio_dev)) {
667                 lio_dev_err(lio_dev, "sc buffer pool allocation failed\n");
668                 return -1;
669         }
670
671         /* Initialize lists to manage the requests of different types that
672          * arrive from applications for this lio device.
673          */
674         lio_setup_response_list(lio_dev);
675
676         if (lio_dev->fn_list.setup_mbox(lio_dev)) {
677                 lio_dev_err(lio_dev, "Mailbox setup failed\n");
678                 goto error;
679         }
680
681         /* Check PF response */
682         lio_check_pf_hs_response((void *)lio_dev);
683
684         /* Do handshake and exit if incompatible PF driver */
685         if (cn23xx_pfvf_handshake(lio_dev))
686                 goto error;
687
688         /* Initial reset */
689         cn23xx_vf_ask_pf_to_do_flr(lio_dev);
690         /* Wait for FLR for 100ms per SRIOV specification */
691         rte_delay_ms(100);
692
693         if (cn23xx_vf_set_io_queues_off(lio_dev)) {
694                 lio_dev_err(lio_dev, "Setting io queues off failed\n");
695                 goto error;
696         }
697
698         if (lio_dev->fn_list.setup_device_regs(lio_dev)) {
699                 lio_dev_err(lio_dev, "Failed to configure device registers\n");
700                 goto error;
701         }
702
703         if (lio_setup_instr_queue0(lio_dev)) {
704                 lio_dev_err(lio_dev, "Failed to setup instruction queue 0\n");
705                 goto error;
706         }
707
708         dpdk_queues = (int)lio_dev->sriov_info.rings_per_vf;
709
710         lio_dev->max_tx_queues = dpdk_queues;
711         lio_dev->max_rx_queues = dpdk_queues;
712
713         /* Enable input and output queues for this device */
714         if (lio_dev->fn_list.enable_io_queues(lio_dev))
715                 goto error;
716
717         return 0;
718
719 error:
720         lio_free_sc_buffer_pool(lio_dev);
721         if (lio_dev->mbox[0])
722                 lio_dev->fn_list.free_mbox(lio_dev);
723         if (lio_dev->instr_queue[0])
724                 lio_free_instr_queue0(lio_dev);
725
726         return -1;
727 }
728
729 static int
730 lio_eth_dev_uninit(struct rte_eth_dev *eth_dev)
731 {
732         struct lio_device *lio_dev = LIO_DEV(eth_dev);
733
734         PMD_INIT_FUNC_TRACE();
735
736         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
737                 return -EPERM;
738
739         /* lio_free_sc_buffer_pool */
740         lio_free_sc_buffer_pool(lio_dev);
741
742         rte_free(eth_dev->data->mac_addrs);
743         eth_dev->data->mac_addrs = NULL;
744
745         eth_dev->rx_pkt_burst = NULL;
746         eth_dev->tx_pkt_burst = NULL;
747
748         return 0;
749 }
750
751 static int
752 lio_eth_dev_init(struct rte_eth_dev *eth_dev)
753 {
754         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(eth_dev->device);
755         struct lio_device *lio_dev = LIO_DEV(eth_dev);
756
757         PMD_INIT_FUNC_TRACE();
758
759         eth_dev->rx_pkt_burst = &lio_dev_recv_pkts;
760         eth_dev->tx_pkt_burst = &lio_dev_xmit_pkts;
761
762         /* Primary does the initialization. */
763         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
764                 return 0;
765
766         rte_eth_copy_pci_info(eth_dev, pdev);
767         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
768
769         if (pdev->mem_resource[0].addr) {
770                 lio_dev->hw_addr = pdev->mem_resource[0].addr;
771         } else {
772                 PMD_INIT_LOG(ERR, "ERROR: Failed to map BAR0\n");
773                 return -ENODEV;
774         }
775
776         lio_dev->eth_dev = eth_dev;
777         /* set lio device print string */
778         snprintf(lio_dev->dev_string, sizeof(lio_dev->dev_string),
779                  "%s[%02x:%02x.%x]", pdev->driver->driver.name,
780                  pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
781
782         lio_dev->port_id = eth_dev->data->port_id;
783
784         if (lio_first_time_init(lio_dev, pdev)) {
785                 lio_dev_err(lio_dev, "Device init failed\n");
786                 return -EINVAL;
787         }
788
789         eth_dev->dev_ops = &liovf_eth_dev_ops;
790         eth_dev->data->mac_addrs = rte_zmalloc("lio", ETHER_ADDR_LEN, 0);
791         if (eth_dev->data->mac_addrs == NULL) {
792                 lio_dev_err(lio_dev,
793                             "MAC addresses memory allocation failed\n");
794                 eth_dev->dev_ops = NULL;
795                 eth_dev->rx_pkt_burst = NULL;
796                 eth_dev->tx_pkt_burst = NULL;
797                 return -ENOMEM;
798         }
799
800         rte_atomic64_set(&lio_dev->status, LIO_DEV_RUNNING);
801         rte_wmb();
802
803         lio_dev->port_configured = 0;
804         /* Always allow unicast packets */
805         lio_dev->ifflags |= LIO_IFFLAG_UNICAST;
806
807         return 0;
808 }
809
810 /* Set of PCI devices this driver supports */
811 static const struct rte_pci_id pci_id_liovf_map[] = {
812         { RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_VF_VID) },
813         { .vendor_id = 0, /* sentinel */ }
814 };
815
816 static struct eth_driver rte_liovf_pmd = {
817         .pci_drv = {
818                 .id_table       = pci_id_liovf_map,
819                 .drv_flags      = RTE_PCI_DRV_NEED_MAPPING,
820                 .probe          = rte_eth_dev_pci_probe,
821                 .remove         = rte_eth_dev_pci_remove,
822         },
823         .eth_dev_init           = lio_eth_dev_init,
824         .eth_dev_uninit         = lio_eth_dev_uninit,
825         .dev_private_size       = sizeof(struct lio_device),
826 };
827
828 RTE_PMD_REGISTER_PCI(net_liovf, rte_liovf_pmd.pci_drv);
829 RTE_PMD_REGISTER_PCI_TABLE(net_liovf, pci_id_liovf_map);
830 RTE_PMD_REGISTER_KMOD_DEP(net_liovf, "* igb_uio | vfio");