2 * Copyright 2008-2014 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
5 * Copyright (c) 2014, Cisco Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
41 #include <rte_ethdev.h>
42 #include <rte_string_fns.h>
44 #include "vnic_intr.h"
48 #include "vnic_enet.h"
51 #ifdef RTE_LIBRTE_ENIC_DEBUG
52 #define ENICPMD_FUNC_TRACE() \
53 RTE_LOG(DEBUG, PMD, "ENICPMD trace: %s\n", __func__)
55 #define ENICPMD_FUNC_TRACE() (void)0
59 * The set of PCI devices this driver supports
61 static const struct rte_pci_id pci_id_enic_map[] = {
62 #define RTE_PCI_DEV_ID_DECL_ENIC(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
63 #ifndef PCI_VENDOR_ID_CISCO
64 #define PCI_VENDOR_ID_CISCO 0x1137
66 #include "rte_pci_dev_ids.h"
67 RTE_PCI_DEV_ID_DECL_ENIC(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET)
68 RTE_PCI_DEV_ID_DECL_ENIC(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_VF)
69 {.vendor_id = 0, /* Sentinal */},
73 enicpmd_fdir_ctrl_func(struct rte_eth_dev *eth_dev,
74 enum rte_filter_op filter_op, void *arg)
76 struct enic *enic = pmd_priv(eth_dev);
80 if (filter_op == RTE_ETH_FILTER_NOP)
83 if (arg == NULL && filter_op != RTE_ETH_FILTER_FLUSH)
87 case RTE_ETH_FILTER_ADD:
88 case RTE_ETH_FILTER_UPDATE:
89 ret = enic_fdir_add_fltr(enic,
90 (struct rte_eth_fdir_filter *)arg);
93 case RTE_ETH_FILTER_DELETE:
94 ret = enic_fdir_del_fltr(enic,
95 (struct rte_eth_fdir_filter *)arg);
98 case RTE_ETH_FILTER_STATS:
99 enic_fdir_stats_get(enic, (struct rte_eth_fdir_stats *)arg);
102 case RTE_ETH_FILTER_FLUSH:
103 case RTE_ETH_FILTER_INFO:
104 dev_warning(enic, "unsupported operation %u", filter_op);
108 dev_err(enic, "unknown operation %u", filter_op);
116 enicpmd_dev_filter_ctrl(struct rte_eth_dev *dev,
117 enum rte_filter_type filter_type,
118 enum rte_filter_op filter_op,
123 if (RTE_ETH_FILTER_FDIR == filter_type)
124 ret = enicpmd_fdir_ctrl_func(dev, filter_op, arg);
126 dev_warning(enic, "Filter type (%d) not supported",
132 static void enicpmd_dev_tx_queue_release(void *txq)
134 ENICPMD_FUNC_TRACE();
138 static int enicpmd_dev_setup_intr(struct enic *enic)
143 ENICPMD_FUNC_TRACE();
145 /* Are we done with the init of all the queues? */
146 for (index = 0; index < enic->cq_count; index++) {
147 if (!enic->cq[index].ctrl)
151 if (enic->cq_count != index)
154 ret = enic_alloc_intr_resources(enic);
156 dev_err(enic, "alloc intr failed\n");
159 enic_init_vnic_resources(enic);
161 ret = enic_setup_finish(enic);
163 dev_err(enic, "setup could not be finished\n");
168 static int enicpmd_dev_tx_queue_setup(struct rte_eth_dev *eth_dev,
171 unsigned int socket_id,
172 __rte_unused const struct rte_eth_txconf *tx_conf)
175 struct enic *enic = pmd_priv(eth_dev);
177 ENICPMD_FUNC_TRACE();
178 eth_dev->data->tx_queues[queue_idx] = (void *)&enic->wq[queue_idx];
180 ret = enic_alloc_wq(enic, queue_idx, socket_id, nb_desc);
182 dev_err(enic, "error in allocating wq\n");
186 return enicpmd_dev_setup_intr(enic);
189 static int enicpmd_dev_tx_queue_start(struct rte_eth_dev *eth_dev,
192 struct enic *enic = pmd_priv(eth_dev);
194 ENICPMD_FUNC_TRACE();
196 enic_start_wq(enic, queue_idx);
201 static int enicpmd_dev_tx_queue_stop(struct rte_eth_dev *eth_dev,
205 struct enic *enic = pmd_priv(eth_dev);
207 ENICPMD_FUNC_TRACE();
209 ret = enic_stop_wq(enic, queue_idx);
211 dev_err(enic, "error in stopping wq %d\n", queue_idx);
216 static int enicpmd_dev_rx_queue_start(struct rte_eth_dev *eth_dev,
219 struct enic *enic = pmd_priv(eth_dev);
221 ENICPMD_FUNC_TRACE();
223 enic_start_rq(enic, queue_idx);
228 static int enicpmd_dev_rx_queue_stop(struct rte_eth_dev *eth_dev,
232 struct enic *enic = pmd_priv(eth_dev);
234 ENICPMD_FUNC_TRACE();
236 ret = enic_stop_rq(enic, queue_idx);
238 dev_err(enic, "error in stopping rq %d\n", queue_idx);
243 static void enicpmd_dev_rx_queue_release(void *rxq)
245 ENICPMD_FUNC_TRACE();
249 static int enicpmd_dev_rx_queue_setup(struct rte_eth_dev *eth_dev,
252 unsigned int socket_id,
253 __rte_unused const struct rte_eth_rxconf *rx_conf,
254 struct rte_mempool *mp)
257 struct enic *enic = pmd_priv(eth_dev);
259 ENICPMD_FUNC_TRACE();
260 eth_dev->data->rx_queues[queue_idx] = (void *)&enic->rq[queue_idx];
262 ret = enic_alloc_rq(enic, queue_idx, socket_id, mp, nb_desc);
264 dev_err(enic, "error in allocating rq\n");
268 return enicpmd_dev_setup_intr(enic);
271 static int enicpmd_vlan_filter_set(struct rte_eth_dev *eth_dev,
272 uint16_t vlan_id, int on)
274 struct enic *enic = pmd_priv(eth_dev);
276 ENICPMD_FUNC_TRACE();
278 enic_add_vlan(enic, vlan_id);
280 enic_del_vlan(enic, vlan_id);
284 static void enicpmd_vlan_offload_set(struct rte_eth_dev *eth_dev, int mask)
286 struct enic *enic = pmd_priv(eth_dev);
288 ENICPMD_FUNC_TRACE();
290 if (mask & ETH_VLAN_STRIP_MASK) {
291 if (eth_dev->data->dev_conf.rxmode.hw_vlan_strip)
292 enic->ig_vlan_strip_en = 1;
294 enic->ig_vlan_strip_en = 0;
296 enic_set_rss_nic_cfg(enic);
299 if (mask & ETH_VLAN_FILTER_MASK) {
301 "Configuration of VLAN filter is not supported\n");
304 if (mask & ETH_VLAN_EXTEND_MASK) {
306 "Configuration of extended VLAN is not supported\n");
310 static int enicpmd_dev_configure(struct rte_eth_dev *eth_dev)
313 struct enic *enic = pmd_priv(eth_dev);
315 ENICPMD_FUNC_TRACE();
316 ret = enic_set_vnic_res(enic);
318 dev_err(enic, "Set vNIC resource num failed, aborting\n");
322 if (eth_dev->data->dev_conf.rxmode.split_hdr_size &&
323 eth_dev->data->dev_conf.rxmode.header_split) {
324 /* Enable header-data-split */
325 enic_set_hdr_split_size(enic,
326 eth_dev->data->dev_conf.rxmode.split_hdr_size);
329 enic->hw_ip_checksum = eth_dev->data->dev_conf.rxmode.hw_ip_checksum;
334 * It returns 0 on success.
336 static int enicpmd_dev_start(struct rte_eth_dev *eth_dev)
338 struct enic *enic = pmd_priv(eth_dev);
340 ENICPMD_FUNC_TRACE();
341 return enic_enable(enic);
345 * Stop device: disable rx and tx functions to allow for reconfiguring.
347 static void enicpmd_dev_stop(struct rte_eth_dev *eth_dev)
349 struct rte_eth_link link;
350 struct enic *enic = pmd_priv(eth_dev);
352 ENICPMD_FUNC_TRACE();
354 memset(&link, 0, sizeof(link));
355 rte_atomic64_cmpset((uint64_t *)ð_dev->data->dev_link,
356 *(uint64_t *)ð_dev->data->dev_link,
363 static void enicpmd_dev_close(struct rte_eth_dev *eth_dev)
365 struct enic *enic = pmd_priv(eth_dev);
367 ENICPMD_FUNC_TRACE();
371 static int enicpmd_dev_link_update(struct rte_eth_dev *eth_dev,
372 __rte_unused int wait_to_complete)
374 struct enic *enic = pmd_priv(eth_dev);
378 ENICPMD_FUNC_TRACE();
379 link_status = enic_get_link_status(enic);
380 ret = (link_status == enic->link_status);
381 enic->link_status = link_status;
382 eth_dev->data->dev_link.link_status = link_status;
383 eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
384 eth_dev->data->dev_link.link_speed = vnic_dev_port_speed(enic->vdev);
388 static void enicpmd_dev_stats_get(struct rte_eth_dev *eth_dev,
389 struct rte_eth_stats *stats)
391 struct enic *enic = pmd_priv(eth_dev);
393 ENICPMD_FUNC_TRACE();
394 enic_dev_stats_get(enic, stats);
397 static void enicpmd_dev_stats_reset(struct rte_eth_dev *eth_dev)
399 struct enic *enic = pmd_priv(eth_dev);
401 ENICPMD_FUNC_TRACE();
402 enic_dev_stats_clear(enic);
405 static void enicpmd_dev_info_get(struct rte_eth_dev *eth_dev,
406 struct rte_eth_dev_info *device_info)
408 struct enic *enic = pmd_priv(eth_dev);
410 ENICPMD_FUNC_TRACE();
411 device_info->max_rx_queues = enic->rq_count;
412 device_info->max_tx_queues = enic->wq_count;
413 device_info->min_rx_bufsize = ENIC_MIN_MTU;
414 device_info->max_rx_pktlen = enic->config.mtu;
415 device_info->max_mac_addrs = 1;
416 device_info->rx_offload_capa =
417 DEV_RX_OFFLOAD_VLAN_STRIP |
418 DEV_RX_OFFLOAD_IPV4_CKSUM |
419 DEV_RX_OFFLOAD_UDP_CKSUM |
420 DEV_RX_OFFLOAD_TCP_CKSUM;
421 device_info->tx_offload_capa =
422 DEV_TX_OFFLOAD_VLAN_INSERT |
423 DEV_TX_OFFLOAD_IPV4_CKSUM |
424 DEV_TX_OFFLOAD_UDP_CKSUM |
425 DEV_TX_OFFLOAD_TCP_CKSUM;
428 static void enicpmd_dev_promiscuous_enable(struct rte_eth_dev *eth_dev)
430 struct enic *enic = pmd_priv(eth_dev);
432 ENICPMD_FUNC_TRACE();
434 enic_add_packet_filter(enic);
437 static void enicpmd_dev_promiscuous_disable(struct rte_eth_dev *eth_dev)
439 struct enic *enic = pmd_priv(eth_dev);
441 ENICPMD_FUNC_TRACE();
443 enic_add_packet_filter(enic);
446 static void enicpmd_dev_allmulticast_enable(struct rte_eth_dev *eth_dev)
448 struct enic *enic = pmd_priv(eth_dev);
450 ENICPMD_FUNC_TRACE();
452 enic_add_packet_filter(enic);
455 static void enicpmd_dev_allmulticast_disable(struct rte_eth_dev *eth_dev)
457 struct enic *enic = pmd_priv(eth_dev);
459 ENICPMD_FUNC_TRACE();
461 enic_add_packet_filter(enic);
464 static void enicpmd_add_mac_addr(struct rte_eth_dev *eth_dev,
465 struct ether_addr *mac_addr,
466 __rte_unused uint32_t index, __rte_unused uint32_t pool)
468 struct enic *enic = pmd_priv(eth_dev);
470 ENICPMD_FUNC_TRACE();
471 enic_set_mac_address(enic, mac_addr->addr_bytes);
474 static void enicpmd_remove_mac_addr(struct rte_eth_dev *eth_dev, __rte_unused uint32_t index)
476 struct enic *enic = pmd_priv(eth_dev);
478 ENICPMD_FUNC_TRACE();
479 enic_del_mac_address(enic);
483 static uint16_t enicpmd_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
488 unsigned int pkt_len;
489 unsigned int seg_len;
490 unsigned int inc_len;
491 unsigned int nb_segs;
492 struct rte_mbuf *tx_pkt;
493 struct vnic_wq *wq = (struct vnic_wq *)tx_queue;
494 struct enic *enic = vnic_dev_priv(wq->vdev);
495 unsigned short vlan_id;
496 unsigned short ol_flags;
498 for (index = 0; index < nb_pkts; index++) {
501 nb_segs = tx_pkt->nb_segs;
502 if (nb_segs > vnic_wq_desc_avail(wq)) {
503 /* wq cleanup and try again */
504 if (!enic_cleanup_wq(enic, wq) ||
505 (nb_segs > vnic_wq_desc_avail(wq)))
508 pkt_len = tx_pkt->pkt_len;
509 vlan_id = tx_pkt->vlan_tci;
510 ol_flags = tx_pkt->ol_flags;
511 for (frags = 0; inc_len < pkt_len; frags++) {
514 seg_len = tx_pkt->data_len;
516 if (enic_send_pkt(enic, wq, tx_pkt,
517 (unsigned short)seg_len, !frags,
518 (pkt_len == inc_len), ol_flags, vlan_id)) {
521 tx_pkt = tx_pkt->next;
525 enic_cleanup_wq(enic, wq);
529 static uint16_t enicpmd_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
532 struct vnic_rq *rq = (struct vnic_rq *)rx_queue;
533 unsigned int work_done;
535 if (enic_poll(rq, rx_pkts, (unsigned int)nb_pkts, &work_done))
536 dev_err(enic, "error in enicpmd poll\n");
541 static const struct eth_dev_ops enicpmd_eth_dev_ops = {
542 .dev_configure = enicpmd_dev_configure,
543 .dev_start = enicpmd_dev_start,
544 .dev_stop = enicpmd_dev_stop,
545 .dev_set_link_up = NULL,
546 .dev_set_link_down = NULL,
547 .dev_close = enicpmd_dev_close,
548 .promiscuous_enable = enicpmd_dev_promiscuous_enable,
549 .promiscuous_disable = enicpmd_dev_promiscuous_disable,
550 .allmulticast_enable = enicpmd_dev_allmulticast_enable,
551 .allmulticast_disable = enicpmd_dev_allmulticast_disable,
552 .link_update = enicpmd_dev_link_update,
553 .stats_get = enicpmd_dev_stats_get,
554 .stats_reset = enicpmd_dev_stats_reset,
555 .queue_stats_mapping_set = NULL,
556 .dev_infos_get = enicpmd_dev_info_get,
558 .vlan_filter_set = enicpmd_vlan_filter_set,
559 .vlan_tpid_set = NULL,
560 .vlan_offload_set = enicpmd_vlan_offload_set,
561 .vlan_strip_queue_set = NULL,
562 .rx_queue_start = enicpmd_dev_rx_queue_start,
563 .rx_queue_stop = enicpmd_dev_rx_queue_stop,
564 .tx_queue_start = enicpmd_dev_tx_queue_start,
565 .tx_queue_stop = enicpmd_dev_tx_queue_stop,
566 .rx_queue_setup = enicpmd_dev_rx_queue_setup,
567 .rx_queue_release = enicpmd_dev_rx_queue_release,
568 .rx_queue_count = NULL,
569 .rx_descriptor_done = NULL,
570 .tx_queue_setup = enicpmd_dev_tx_queue_setup,
571 .tx_queue_release = enicpmd_dev_tx_queue_release,
574 .flow_ctrl_get = NULL,
575 .flow_ctrl_set = NULL,
576 .priority_flow_ctrl_set = NULL,
577 .mac_addr_add = enicpmd_add_mac_addr,
578 .mac_addr_remove = enicpmd_remove_mac_addr,
579 .fdir_set_masks = NULL,
580 .filter_ctrl = enicpmd_dev_filter_ctrl,
583 struct enic *enicpmd_list_head = NULL;
584 /* Initialize the driver
585 * It returns 0 on success.
587 static int eth_enicpmd_dev_init(struct rte_eth_dev *eth_dev)
589 struct rte_pci_device *pdev;
590 struct rte_pci_addr *addr;
591 struct enic *enic = pmd_priv(eth_dev);
593 ENICPMD_FUNC_TRACE();
595 enic->port_id = eth_dev->data->port_id;
596 enic->rte_dev = eth_dev;
597 eth_dev->dev_ops = &enicpmd_eth_dev_ops;
598 eth_dev->rx_pkt_burst = &enicpmd_recv_pkts;
599 eth_dev->tx_pkt_burst = &enicpmd_xmit_pkts;
601 pdev = eth_dev->pci_dev;
605 snprintf(enic->bdf_name, ENICPMD_BDF_LENGTH, "%04x:%02x:%02x.%x",
606 addr->domain, addr->bus, addr->devid, addr->function);
608 return enic_probe(enic);
611 static struct eth_driver rte_enic_pmd = {
613 .name = "rte_enic_pmd",
614 .id_table = pci_id_enic_map,
615 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
617 .eth_dev_init = eth_enicpmd_dev_init,
618 .dev_private_size = sizeof(struct enic),
621 /* Driver initialization routine.
622 * Invoked once at EAL init time.
623 * Register as the [Poll Mode] Driver of Cisco ENIC device.
626 rte_enic_pmd_init(const char *name __rte_unused,
627 const char *params __rte_unused)
629 ENICPMD_FUNC_TRACE();
631 rte_eth_driver_register(&rte_enic_pmd);
635 static struct rte_driver rte_enic_driver = {
637 .init = rte_enic_pmd_init,
640 PMD_REGISTER_DRIVER(rte_enic_driver);