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.
43 #include <linux/vfio.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
50 #include <rte_string_fns.h>
51 #include <rte_ethdev.h>
53 #include "enic_compat.h"
55 #include "wq_enet_desc.h"
56 #include "rq_enet_desc.h"
57 #include "cq_enet_desc.h"
58 #include "vnic_enet.h"
63 #include "vnic_intr.h"
66 static inline int enic_is_sriov_vf(struct enic *enic)
68 return enic->pdev->id.device_id == PCI_DEVICE_ID_CISCO_VIC_ENET_VF;
71 static int is_zero_addr(char *addr)
73 return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
76 static int is_mcast_addr(char *addr)
81 static int is_eth_addr_valid(char *addr)
83 return !is_mcast_addr(addr) && !is_zero_addr(addr);
86 static inline struct rte_mbuf *
87 enic_rxmbuf_alloc(struct rte_mempool *mp)
91 m = __rte_mbuf_raw_alloc(mp);
92 __rte_mbuf_sanity_check_raw(m, 0);
96 static const struct rte_memzone *ring_dma_zone_reserve(
97 struct rte_eth_dev *dev, const char *ring_name,
98 uint16_t queue_id, uint32_t ring_size, int socket_id)
100 char z_name[RTE_MEMZONE_NAMESIZE];
101 const struct rte_memzone *mz;
103 snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
104 dev->driver->pci_drv.name, ring_name,
105 dev->data->port_id, queue_id);
107 mz = rte_memzone_lookup(z_name);
111 return rte_memzone_reserve_aligned(z_name, (uint64_t) ring_size,
112 socket_id, RTE_MEMZONE_1GB, ENIC_ALIGN);
115 void enic_set_hdr_split_size(struct enic *enic, u16 split_hdr_size)
117 vnic_set_hdr_split_size(enic->vdev, split_hdr_size);
120 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
122 struct rte_mbuf *mbuf = (struct rte_mbuf *)buf->os_buf;
124 rte_mempool_put(mbuf->pool, mbuf);
128 static void enic_wq_free_buf(struct vnic_wq *wq,
129 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
131 enic_free_wq_buf(wq, buf);
134 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
135 u8 type, u16 q_number, u16 completed_index, void *opaque)
137 struct enic *enic = vnic_dev_priv(vdev);
139 vnic_wq_service(&enic->wq[q_number], cq_desc,
140 completed_index, enic_wq_free_buf,
146 static void enic_log_q_error(struct enic *enic)
151 for (i = 0; i < enic->wq_count; i++) {
152 error_status = vnic_wq_error_status(&enic->wq[i]);
154 dev_err(enic, "WQ[%d] error_status %d\n", i,
158 for (i = 0; i < enic->rq_count; i++) {
159 error_status = vnic_rq_error_status(&enic->rq[i]);
161 dev_err(enic, "RQ[%d] error_status %d\n", i,
166 unsigned int enic_cleanup_wq(struct enic *enic, struct vnic_wq *wq)
168 unsigned int cq = enic_cq_wq(enic, wq->index);
170 /* Return the work done */
171 return vnic_cq_service(&enic->cq[cq],
172 -1 /*wq_work_to_do*/, enic_wq_service, NULL);
176 int enic_send_pkt(struct enic *enic, struct vnic_wq *wq,
177 struct rte_mbuf *tx_pkt, unsigned short len,
178 u_int8_t sop, u_int8_t eop,
179 u_int16_t ol_flags, u_int16_t vlan_tag)
181 struct wq_enet_desc *desc = vnic_wq_next_desc(wq);
183 u_int16_t header_length = 0;
184 u_int8_t cq_entry = eop;
185 u_int8_t vlan_tag_insert = 0;
186 unsigned char *buf = (unsigned char *)(tx_pkt->buf_addr) +
187 RTE_PKTMBUF_HEADROOM;
188 u_int64_t bus_addr = (dma_addr_t)
189 (tx_pkt->buf_physaddr + RTE_PKTMBUF_HEADROOM);
192 if (ol_flags & PKT_TX_VLAN_PKT)
195 if (enic->hw_ip_checksum) {
196 if (ol_flags & PKT_TX_IP_CKSUM)
197 mss |= ENIC_CALC_IP_CKSUM;
199 if (ol_flags & PKT_TX_TCP_UDP_CKSUM)
200 mss |= ENIC_CALC_TCP_UDP_CKSUM;
204 wq_enet_desc_enc(desc,
208 0 /* header_length */,
209 0 /* offload_mode WQ_ENET_OFFLOAD_MODE_CSUM */,
217 vnic_wq_post(wq, (void *)tx_pkt, bus_addr, len,
221 0 /*compressed send*/,
227 void enic_dev_stats_clear(struct enic *enic)
229 if (vnic_dev_stats_clear(enic->vdev))
230 dev_err(enic, "Error in clearing stats\n");
233 void enic_dev_stats_get(struct enic *enic, struct rte_eth_stats *r_stats)
235 struct vnic_stats *stats;
237 memset(r_stats, 0, sizeof(*r_stats));
238 if (vnic_dev_stats_dump(enic->vdev, &stats)) {
239 dev_err(enic, "Error in getting stats\n");
243 r_stats->ipackets = stats->rx.rx_frames_ok;
244 r_stats->opackets = stats->tx.tx_frames_ok;
246 r_stats->ibytes = stats->rx.rx_bytes_ok;
247 r_stats->obytes = stats->tx.tx_bytes_ok;
249 r_stats->ierrors = stats->rx.rx_errors;
250 r_stats->oerrors = stats->tx.tx_errors;
252 r_stats->imcasts = stats->rx.rx_multicast_frames_ok;
253 r_stats->rx_nombuf = stats->rx.rx_no_bufs;
256 void enic_del_mac_address(struct enic *enic)
258 if (vnic_dev_del_addr(enic->vdev, enic->mac_addr))
259 dev_err(enic, "del mac addr failed\n");
262 void enic_set_mac_address(struct enic *enic, uint8_t *mac_addr)
266 if (!is_eth_addr_valid(mac_addr)) {
267 dev_err(enic, "invalid mac address\n");
271 err = vnic_dev_del_addr(enic->vdev, mac_addr);
273 dev_err(enic, "del mac addr failed\n");
277 ether_addr_copy((struct ether_addr *)mac_addr,
278 (struct ether_addr *)enic->mac_addr);
280 err = vnic_dev_add_addr(enic->vdev, mac_addr);
282 dev_err(enic, "add mac addr failed\n");
287 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
289 struct enic *enic = vnic_dev_priv(rq->vdev);
294 rte_pktmbuf_free((struct rte_mbuf *)buf->os_buf);
298 void enic_init_vnic_resources(struct enic *enic)
300 unsigned int error_interrupt_enable = 1;
301 unsigned int error_interrupt_offset = 0;
303 unsigned int cq_index = 0;
305 for (index = 0; index < enic->rq_count; index++) {
306 vnic_rq_init(&enic->rq[index],
307 enic_cq_rq(enic, index),
308 error_interrupt_enable,
309 error_interrupt_offset);
312 for (index = 0; index < enic->wq_count; index++) {
313 vnic_wq_init(&enic->wq[index],
314 enic_cq_wq(enic, index),
315 error_interrupt_enable,
316 error_interrupt_offset);
319 vnic_dev_stats_clear(enic->vdev);
321 for (index = 0; index < enic->cq_count; index++) {
322 vnic_cq_init(&enic->cq[index],
323 0 /* flow_control_enable */,
324 1 /* color_enable */,
327 1 /* cq_tail_color */,
328 0 /* interrupt_enable */,
329 1 /* cq_entry_enable */,
330 0 /* cq_message_enable */,
331 0 /* interrupt offset */,
332 0 /* cq_message_addr */);
335 vnic_intr_init(&enic->intr,
336 enic->config.intr_timer_usec,
337 enic->config.intr_timer_type,
338 /*mask_on_assertion*/1);
342 static int enic_rq_alloc_buf(struct vnic_rq *rq)
344 struct enic *enic = vnic_dev_priv(rq->vdev);
347 struct rq_enet_desc *desc = vnic_rq_next_desc(rq);
348 u_int8_t type = RQ_ENET_TYPE_ONLY_SOP;
349 u_int16_t len = ENIC_MAX_MTU + VLAN_ETH_HLEN;
350 u16 split_hdr_size = vnic_get_hdr_split_size(enic->vdev);
351 struct rte_mbuf *mbuf = enic_rxmbuf_alloc(rq->mp);
352 struct rte_mbuf *hdr_mbuf = NULL;
355 dev_err(enic, "mbuf alloc in enic_rq_alloc_buf failed\n");
359 if (unlikely(split_hdr_size)) {
360 if (vnic_rq_desc_avail(rq) < 2) {
361 rte_mempool_put(mbuf->pool, mbuf);
364 hdr_mbuf = enic_rxmbuf_alloc(rq->mp);
366 rte_mempool_put(mbuf->pool, mbuf);
368 "hdr_mbuf alloc in enic_rq_alloc_buf failed\n");
372 hdr_mbuf->data_off = RTE_PKTMBUF_HEADROOM;
373 buf = rte_pktmbuf_mtod(hdr_mbuf, void *);
375 hdr_mbuf->nb_segs = 2;
376 hdr_mbuf->port = rq->index;
377 hdr_mbuf->next = mbuf;
379 dma_addr = (dma_addr_t)
380 (hdr_mbuf->buf_physaddr + hdr_mbuf->data_off);
382 rq_enet_desc_enc(desc, dma_addr, type, split_hdr_size);
384 vnic_rq_post(rq, (void *)hdr_mbuf, 0 /*os_buf_index*/, dma_addr,
385 (unsigned int)split_hdr_size, 0 /*wrid*/);
387 desc = vnic_rq_next_desc(rq);
388 type = RQ_ENET_TYPE_NOT_SOP;
391 mbuf->port = rq->index;
394 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
395 buf = rte_pktmbuf_mtod(mbuf, void *);
398 dma_addr = (dma_addr_t)
399 (mbuf->buf_physaddr + mbuf->data_off);
401 rq_enet_desc_enc(desc, dma_addr, type, mbuf->buf_len);
403 vnic_rq_post(rq, (void *)mbuf, 0 /*os_buf_index*/, dma_addr,
404 (unsigned int)mbuf->buf_len, 0 /*wrid*/);
409 static int enic_rq_indicate_buf(struct vnic_rq *rq,
410 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
411 int skipped, void *opaque)
413 struct enic *enic = vnic_dev_priv(rq->vdev);
414 struct rte_mbuf **rx_pkt_bucket = (struct rte_mbuf **)opaque;
415 struct rte_mbuf *rx_pkt = NULL;
416 struct rte_mbuf *hdr_rx_pkt = NULL;
418 u8 type, color, eop, sop, ingress_port, vlan_stripped;
419 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
420 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
421 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
423 u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
426 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
427 &type, &color, &q_number, &completed_index,
428 &ingress_port, &fcoe, &eop, &sop, &rss_type,
429 &csum_not_calc, &rss_hash, &bytes_written,
430 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
431 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
432 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
433 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
437 dev_err(enic, "packet error\n");
441 rx_pkt = (struct rte_mbuf *)buf->os_buf;
444 if (unlikely(skipped)) {
445 rx_pkt->data_len = 0;
449 if (likely(!vnic_get_hdr_split_size(enic->vdev))) {
450 /* No header split configured */
451 *rx_pkt_bucket = rx_pkt;
452 rx_pkt->pkt_len = bytes_written;
455 rx_pkt->ol_flags |= PKT_RX_IPV4_HDR;
456 if (!csum_not_calc) {
457 if (unlikely(!ipv4_csum_ok))
458 rx_pkt->ol_flags |= PKT_RX_IP_CKSUM_BAD;
460 if ((tcp || udp) && (!tcp_udp_csum_ok))
461 rx_pkt->ol_flags |= PKT_RX_L4_CKSUM_BAD;
464 rx_pkt->ol_flags |= PKT_RX_IPV6_HDR;
468 /* This piece is header */
469 *rx_pkt_bucket = rx_pkt;
470 rx_pkt->pkt_len = bytes_written;
473 /* The packet is smaller than split_hdr_size */
474 *rx_pkt_bucket = rx_pkt;
475 rx_pkt->pkt_len = bytes_written;
477 rx_pkt->ol_flags |= PKT_RX_IPV4_HDR;
478 if (!csum_not_calc) {
479 if (unlikely(!ipv4_csum_ok))
489 rx_pkt->ol_flags |= PKT_RX_IPV6_HDR;
492 hdr_rx_pkt = *rx_pkt_bucket;
493 hdr_rx_pkt->pkt_len += bytes_written;
495 hdr_rx_pkt->ol_flags |= PKT_RX_IPV4_HDR;
496 if (!csum_not_calc) {
497 if (unlikely(!ipv4_csum_ok))
498 hdr_rx_pkt->ol_flags |=
503 hdr_rx_pkt->ol_flags |=
507 hdr_rx_pkt->ol_flags |= PKT_RX_IPV6_HDR;
513 rx_pkt->data_len = bytes_written;
516 rx_pkt->ol_flags |= PKT_RX_RSS_HASH;
517 rx_pkt->hash.rss = rss_hash;
521 rx_pkt->ol_flags |= PKT_RX_VLAN_PKT;
522 rx_pkt->vlan_tci = vlan_tci;
528 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
529 u8 type, u16 q_number, u16 completed_index, void *opaque)
531 struct enic *enic = vnic_dev_priv(vdev);
533 return vnic_rq_service(&enic->rq[q_number], cq_desc,
534 completed_index, VNIC_RQ_RETURN_DESC,
535 enic_rq_indicate_buf, opaque);
539 int enic_poll(struct vnic_rq *rq, struct rte_mbuf **rx_pkts,
540 unsigned int budget, unsigned int *work_done)
542 struct enic *enic = vnic_dev_priv(rq->vdev);
543 unsigned int cq = enic_cq_rq(enic, rq->index);
546 *work_done = vnic_cq_service(&enic->cq[cq],
547 budget, enic_rq_service, (void *)rx_pkts);
550 vnic_rq_fill(rq, enic_rq_alloc_buf);
552 /* Need at least one buffer on ring to get going */
553 if (vnic_rq_desc_used(rq) == 0) {
554 dev_err(enic, "Unable to alloc receive buffers\n");
561 void *enic_alloc_consistent(void *priv, size_t size,
562 dma_addr_t *dma_handle, u8 *name)
564 struct enic *enic = (struct enic *)priv;
566 const struct rte_memzone *rz;
569 rz = rte_memzone_reserve_aligned(name, size, 0, 0, ENIC_ALIGN);
571 pr_err("%s : Failed to allocate memory requested for %s",
577 *dma_handle = (dma_addr_t)rz->phys_addr;
582 void enic_free_consistent(struct rte_pci_device *hwdev, size_t size,
583 void *vaddr, dma_addr_t dma_handle)
585 /* Nothing to be done */
588 void enic_intr_handler(__rte_unused struct rte_intr_handle *handle,
591 struct enic *enic = pmd_priv((struct rte_eth_dev *)arg);
593 dev_err(enic, "Err intr.\n");
594 vnic_intr_return_all_credits(&enic->intr);
596 enic_log_q_error(enic);
599 int enic_enable(struct enic *enic)
603 char mz_name[RTE_MEMZONE_NAMESIZE];
604 const struct rte_memzone *rmz;
605 struct rte_eth_dev *eth_dev = enic->rte_dev;
607 eth_dev->data->dev_link.link_speed = vnic_dev_port_speed(enic->vdev);
608 eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
609 vnic_dev_notify_set(enic->vdev, -1); /* No Intr for notify */
611 if (enic_clsf_init(enic))
612 dev_warning(enic, "Init of hash table for clsf failed."\
613 "Flow director feature will not work\n");
616 for (index = 0; index < enic->rq_count; index++) {
617 vnic_rq_fill(&enic->rq[index], enic_rq_alloc_buf);
619 /* Need at least one buffer on ring to get going
621 if (vnic_rq_desc_used(&enic->rq[index]) == 0) {
622 dev_err(enic, "Unable to alloc receive buffers\n");
627 for (index = 0; index < enic->wq_count; index++)
628 vnic_wq_enable(&enic->wq[index]);
629 for (index = 0; index < enic->rq_count; index++)
630 vnic_rq_enable(&enic->rq[index]);
632 vnic_dev_enable_wait(enic->vdev);
635 /* Register and enable error interrupt */
636 rte_intr_callback_register(&(enic->pdev->intr_handle),
637 enic_intr_handler, (void *)enic->rte_dev);
639 rte_intr_enable(&(enic->pdev->intr_handle));
641 vnic_intr_unmask(&enic->intr);
646 int enic_alloc_intr_resources(struct enic *enic)
650 dev_info(enic, "vNIC resources used: "\
651 "wq %d rq %d cq %d intr %d\n",
652 enic->wq_count, enic->rq_count,
653 enic->cq_count, enic->intr_count);
655 err = vnic_intr_alloc(enic->vdev, &enic->intr, 0);
657 enic_free_vnic_resources(enic);
662 void enic_free_rq(void *rxq)
664 struct vnic_rq *rq = (struct vnic_rq *)rxq;
665 struct enic *enic = vnic_dev_priv(rq->vdev);
668 vnic_cq_free(&enic->cq[rq->index]);
671 void enic_start_wq(struct enic *enic, uint16_t queue_idx)
673 vnic_wq_enable(&enic->wq[queue_idx]);
676 int enic_stop_wq(struct enic *enic, uint16_t queue_idx)
678 return vnic_wq_disable(&enic->wq[queue_idx]);
681 void enic_start_rq(struct enic *enic, uint16_t queue_idx)
683 vnic_rq_enable(&enic->rq[queue_idx]);
686 int enic_stop_rq(struct enic *enic, uint16_t queue_idx)
688 return vnic_rq_disable(&enic->rq[queue_idx]);
691 int enic_alloc_rq(struct enic *enic, uint16_t queue_idx,
692 unsigned int socket_id, struct rte_mempool *mp,
696 struct vnic_rq *rq = &enic->rq[queue_idx];
698 rq->socket_id = socket_id;
702 if (nb_desc > enic->config.rq_desc_count) {
704 "RQ %d - number of rx desc in cmd line (%d)"\
705 "is greater than that in the UCSM/CIMC adapter"\
706 "policy. Applying the value in the adapter "\
708 queue_idx, nb_desc, enic->config.rq_desc_count);
709 } else if (nb_desc != enic->config.rq_desc_count) {
710 enic->config.rq_desc_count = nb_desc;
712 "RX Queues - effective number of descs:%d\n",
717 /* Allocate queue resources */
718 err = vnic_rq_alloc(enic->vdev, &enic->rq[queue_idx], queue_idx,
719 enic->config.rq_desc_count,
720 sizeof(struct rq_enet_desc));
722 dev_err(enic, "error in allocation of rq\n");
726 err = vnic_cq_alloc(enic->vdev, &enic->cq[queue_idx], queue_idx,
727 socket_id, enic->config.rq_desc_count,
728 sizeof(struct cq_enet_rq_desc));
731 dev_err(enic, "error in allocation of cq for rq\n");
737 void enic_free_wq(void *txq)
739 struct vnic_wq *wq = (struct vnic_wq *)txq;
740 struct enic *enic = vnic_dev_priv(wq->vdev);
743 vnic_cq_free(&enic->cq[enic->rq_count + wq->index]);
746 int enic_alloc_wq(struct enic *enic, uint16_t queue_idx,
747 unsigned int socket_id, uint16_t nb_desc)
750 struct vnic_wq *wq = &enic->wq[queue_idx];
751 unsigned int cq_index = enic_cq_wq(enic, queue_idx);
753 wq->socket_id = socket_id;
755 if (nb_desc > enic->config.wq_desc_count) {
757 "WQ %d - number of tx desc in cmd line (%d)"\
758 "is greater than that in the UCSM/CIMC adapter"\
759 "policy. Applying the value in the adapter "\
761 queue_idx, nb_desc, enic->config.wq_desc_count);
762 } else if (nb_desc != enic->config.wq_desc_count) {
763 enic->config.wq_desc_count = nb_desc;
765 "TX Queues - effective number of descs:%d\n",
770 /* Allocate queue resources */
771 err = vnic_wq_alloc(enic->vdev, &enic->wq[queue_idx], queue_idx,
772 enic->config.wq_desc_count,
773 sizeof(struct wq_enet_desc));
775 dev_err(enic, "error in allocation of wq\n");
779 err = vnic_cq_alloc(enic->vdev, &enic->cq[cq_index], cq_index,
780 socket_id, enic->config.wq_desc_count,
781 sizeof(struct cq_enet_wq_desc));
784 dev_err(enic, "error in allocation of cq for wq\n");
790 int enic_disable(struct enic *enic)
795 vnic_intr_mask(&enic->intr);
796 (void)vnic_intr_masked(&enic->intr); /* flush write */
798 vnic_dev_disable(enic->vdev);
800 enic_clsf_destroy(enic);
802 if (!enic_is_sriov_vf(enic))
803 vnic_dev_del_addr(enic->vdev, enic->mac_addr);
805 for (i = 0; i < enic->wq_count; i++) {
806 err = vnic_wq_disable(&enic->wq[i]);
810 for (i = 0; i < enic->rq_count; i++) {
811 err = vnic_rq_disable(&enic->rq[i]);
816 vnic_dev_set_reset_flag(enic->vdev, 1);
817 vnic_dev_notify_unset(enic->vdev);
819 for (i = 0; i < enic->wq_count; i++)
820 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
821 for (i = 0; i < enic->rq_count; i++)
822 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
823 for (i = 0; i < enic->cq_count; i++)
824 vnic_cq_clean(&enic->cq[i]);
825 vnic_intr_clean(&enic->intr);
830 static int enic_dev_wait(struct vnic_dev *vdev,
831 int (*start)(struct vnic_dev *, int),
832 int (*finished)(struct vnic_dev *, int *),
839 err = start(vdev, arg);
843 /* Wait for func to complete...2 seconds max */
844 for (i = 0; i < 2000; i++) {
845 err = finished(vdev, &done);
855 static int enic_dev_open(struct enic *enic)
859 err = enic_dev_wait(enic->vdev, vnic_dev_open,
860 vnic_dev_open_done, 0);
862 dev_err(enic_get_dev(enic),
863 "vNIC device open failed, err %d\n", err);
868 static int enic_set_rsskey(struct enic *enic)
870 dma_addr_t rss_key_buf_pa;
871 union vnic_rss_key *rss_key_buf_va = NULL;
872 union vnic_rss_key rss_key = {
873 .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
874 .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
875 .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
876 .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
881 snprintf(name, NAME_MAX, "rss_key-%s", enic->bdf_name);
882 rss_key_buf_va = enic_alloc_consistent(enic, sizeof(union vnic_rss_key),
883 &rss_key_buf_pa, name);
887 rte_memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
889 err = enic_set_rss_key(enic,
891 sizeof(union vnic_rss_key));
893 enic_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
894 rss_key_buf_va, rss_key_buf_pa);
899 static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
901 dma_addr_t rss_cpu_buf_pa;
902 union vnic_rss_cpu *rss_cpu_buf_va = NULL;
907 snprintf(name, NAME_MAX, "rss_cpu-%s", enic->bdf_name);
908 rss_cpu_buf_va = enic_alloc_consistent(enic, sizeof(union vnic_rss_cpu),
909 &rss_cpu_buf_pa, name);
913 for (i = 0; i < (1 << rss_hash_bits); i++)
914 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
916 err = enic_set_rss_cpu(enic,
918 sizeof(union vnic_rss_cpu));
920 enic_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
921 rss_cpu_buf_va, rss_cpu_buf_pa);
926 static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
927 u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
929 const u8 tso_ipid_split_en = 0;
932 /* Enable VLAN tag stripping */
934 err = enic_set_nic_cfg(enic,
935 rss_default_cpu, rss_hash_type,
936 rss_hash_bits, rss_base_cpu,
937 rss_enable, tso_ipid_split_en,
938 enic->ig_vlan_strip_en);
943 int enic_set_rss_nic_cfg(struct enic *enic)
945 const u8 rss_default_cpu = 0;
946 const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
947 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
948 NIC_CFG_RSS_HASH_TYPE_IPV6 |
949 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
950 const u8 rss_hash_bits = 7;
951 const u8 rss_base_cpu = 0;
952 u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
955 if (!enic_set_rsskey(enic)) {
956 if (enic_set_rsscpu(enic, rss_hash_bits)) {
958 dev_warning(enic, "RSS disabled, "\
959 "Failed to set RSS cpu indirection table.");
964 "RSS disabled, Failed to set RSS key.\n");
968 return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
969 rss_hash_bits, rss_base_cpu, rss_enable);
972 int enic_setup_finish(struct enic *enic)
976 ret = enic_set_rss_nic_cfg(enic);
978 dev_err(enic, "Failed to config nic, aborting.\n");
982 vnic_dev_add_addr(enic->vdev, enic->mac_addr);
985 vnic_dev_packet_filter(enic->vdev,
999 static void enic_eventfd_init(struct enic *enic)
1001 enic->eventfd = enic->pdev->intr_handle.fd;
1004 void *enic_err_intr_handler(void *arg)
1006 struct enic *enic = (struct enic *)arg;
1007 unsigned int intr = enic_msix_err_intr(enic);
1012 size = read(enic->eventfd, &data, sizeof(data));
1013 dev_err(enic, "Err intr.\n");
1014 vnic_intr_return_all_credits(&enic->intr);
1016 enic_log_q_error(enic);
1023 void enic_add_packet_filter(struct enic *enic)
1025 /* Args -> directed, multicast, broadcast, promisc, allmulti */
1026 vnic_dev_packet_filter(enic->vdev, 1, 1, 1,
1027 enic->promisc, enic->allmulti);
1030 int enic_get_link_status(struct enic *enic)
1032 return vnic_dev_link_status(enic->vdev);
1037 static int enic_create_err_intr_thread(struct enic *enic)
1039 pthread_attr_t intr_attr;
1041 /* create threads for error interrupt handling */
1042 pthread_attr_init(&intr_attr);
1043 pthread_attr_setstacksize(&intr_attr, 0x100000);
1046 if (pthread_create(&enic->err_intr_thread, &intr_attr,
1047 enic_err_intr_handler, (void *)enic)) {
1048 dev_err(enic, "Failed to create err interrupt handler threads\n");
1052 pthread_attr_destroy(&intr_attr);
1058 static int enic_set_intr_mode(struct enic *enic)
1060 struct vfio_irq_set *irq_set;
1066 if (enic->intr_count < 1) {
1067 dev_err(enic, "Unsupported resource conf.\n");
1070 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSIX);
1072 enic->intr_count = 1;
1074 enic_eventfd_init(enic);
1075 size = sizeof(*irq_set) + (sizeof(int));
1077 irq_set = rte_zmalloc("enic_vfio_irq", size, 0);
1078 irq_set->argsz = size;
1079 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
1081 irq_set->count = 1; /* For error interrupt only */
1082 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
1083 VFIO_IRQ_SET_ACTION_TRIGGER;
1084 fds = (int *)&irq_set->data;
1086 fds[0] = enic->eventfd;
1088 ret = ioctl(enic->pdev->intr_handle.vfio_dev_fd,
1089 VFIO_DEVICE_SET_IRQS, irq_set);
1092 dev_err(enic, "Failed to set eventfds for interrupts\n");
1096 enic_create_err_intr_thread(enic);
1100 static void enic_clear_intr_mode(struct enic *enic)
1102 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
1106 static void enic_dev_deinit(struct enic *enic)
1109 struct rte_eth_dev *eth_dev = enic->rte_dev;
1111 if (eth_dev->data->mac_addrs)
1112 rte_free(eth_dev->data->mac_addrs);
1115 enic_clear_intr_mode(enic);
1120 int enic_set_vnic_res(struct enic *enic)
1122 struct rte_eth_dev *eth_dev = enic->rte_dev;
1124 if ((enic->rq_count < eth_dev->data->nb_rx_queues) ||
1125 (enic->wq_count < eth_dev->data->nb_tx_queues)) {
1126 dev_err(dev, "Not enough resources configured, aborting\n");
1130 enic->rq_count = eth_dev->data->nb_rx_queues;
1131 enic->wq_count = eth_dev->data->nb_tx_queues;
1132 if (enic->cq_count < (enic->rq_count + enic->wq_count)) {
1133 dev_err(dev, "Not enough resources configured, aborting\n");
1137 enic->cq_count = enic->rq_count + enic->wq_count;
1141 static int enic_dev_init(struct enic *enic)
1145 struct rte_eth_dev *eth_dev = enic->rte_dev;
1147 vnic_dev_intr_coal_timer_info_default(enic->vdev);
1149 /* Get vNIC configuration
1151 err = enic_get_vnic_config(enic);
1153 dev_err(dev, "Get vNIC configuration failed, aborting\n");
1157 eth_dev->data->mac_addrs = rte_zmalloc("enic_mac_addr", ETH_ALEN, 0);
1158 if (!eth_dev->data->mac_addrs) {
1159 dev_err(enic, "mac addr storage alloc failed, aborting.\n");
1162 ether_addr_copy((struct ether_addr *) enic->mac_addr,
1163 ð_dev->data->mac_addrs[0]);
1166 /* Get available resource counts
1168 enic_get_res_counts(enic);
1171 /* Set interrupt mode based on resource counts and system
1174 err = enic_set_intr_mode(enic);
1176 rte_free(eth_dev->data->mac_addrs);
1177 enic_clear_intr_mode(enic);
1178 dev_err(dev, "Failed to set intr mode based on resource "\
1179 "counts and system capabilities, aborting\n");
1184 vnic_dev_set_reset_flag(enic->vdev, 0);
1190 int enic_probe(struct enic *enic)
1192 const char *bdf = enic->bdf_name;
1193 struct rte_pci_device *pdev = enic->pdev;
1194 struct rte_eth_dev *eth_dev = enic->rte_dev;
1198 dev_info(enic, " Initializing ENIC PMD version %s\n", DRV_VERSION);
1200 enic->bar0.vaddr = (void *)pdev->mem_resource[0].addr;
1201 enic->bar0.len = pdev->mem_resource[0].len;
1203 /* Register vNIC device */
1204 enic->vdev = vnic_dev_register(NULL, enic, enic->pdev, &enic->bar0, 1);
1206 dev_err(enic, "vNIC registration failed, aborting\n");
1210 vnic_register_cbacks(enic->vdev,
1211 enic_alloc_consistent,
1212 enic_free_consistent);
1214 /* Issue device open to get device in known state */
1215 err = enic_dev_open(enic);
1217 dev_err(enic, "vNIC dev open failed, aborting\n");
1218 goto err_out_unregister;
1221 /* Set ingress vlan rewrite mode before vnic initialization */
1222 err = vnic_dev_set_ig_vlan_rewrite_mode(enic->vdev,
1223 IG_VLAN_REWRITE_MODE_PRIORITY_TAG_DEFAULT_VLAN);
1226 "Failed to set ingress vlan rewrite mode, aborting.\n");
1227 goto err_out_dev_close;
1230 /* Issue device init to initialize the vnic-to-switch link.
1231 * We'll start with carrier off and wait for link UP
1232 * notification later to turn on carrier. We don't need
1233 * to wait here for the vnic-to-switch link initialization
1234 * to complete; link UP notification is the indication that
1235 * the process is complete.
1238 err = vnic_dev_init(enic->vdev, 0);
1240 dev_err(enic, "vNIC dev init failed, aborting\n");
1241 goto err_out_dev_close;
1244 err = enic_dev_init(enic);
1246 dev_err(enic, "Device initialization failed, aborting\n");
1247 goto err_out_dev_close;
1253 vnic_dev_close(enic->vdev);
1255 vnic_dev_unregister(enic->vdev);
1260 void enic_remove(struct enic *enic)
1262 enic_dev_deinit(enic);
1263 vnic_dev_close(enic->vdev);
1264 vnic_dev_unregister(enic->vdev);