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.
44 #include <rte_memzone.h>
45 #include <rte_malloc.h>
47 #include <rte_string_fns.h>
48 #include <rte_ethdev.h>
50 #include "enic_compat.h"
52 #include "wq_enet_desc.h"
53 #include "rq_enet_desc.h"
54 #include "cq_enet_desc.h"
55 #include "vnic_enet.h"
60 #include "vnic_intr.h"
63 static inline int enic_is_sriov_vf(struct enic *enic)
65 return enic->pdev->id.device_id == PCI_DEVICE_ID_CISCO_VIC_ENET_VF;
68 static int is_zero_addr(char *addr)
70 return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
73 static int is_mcast_addr(char *addr)
78 static int is_eth_addr_valid(char *addr)
80 return !is_mcast_addr(addr) && !is_zero_addr(addr);
83 static inline struct rte_mbuf *
84 enic_rxmbuf_alloc(struct rte_mempool *mp)
88 m = __rte_mbuf_raw_alloc(mp);
89 __rte_mbuf_sanity_check_raw(m, 0);
93 static const struct rte_memzone *ring_dma_zone_reserve(
94 struct rte_eth_dev *dev, const char *ring_name,
95 uint16_t queue_id, uint32_t ring_size, int socket_id)
97 char z_name[RTE_MEMZONE_NAMESIZE];
98 const struct rte_memzone *mz;
100 snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
101 dev->driver->pci_drv.name, ring_name,
102 dev->data->port_id, queue_id);
104 mz = rte_memzone_lookup(z_name);
108 return rte_memzone_reserve_aligned(z_name, (uint64_t) ring_size,
109 socket_id, RTE_MEMZONE_1GB, ENIC_ALIGN);
112 void enic_set_hdr_split_size(struct enic *enic, u16 split_hdr_size)
114 vnic_set_hdr_split_size(enic->vdev, split_hdr_size);
117 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
119 struct rte_mbuf *mbuf = (struct rte_mbuf *)buf->os_buf;
121 rte_mempool_put(mbuf->pool, mbuf);
125 static void enic_wq_free_buf(struct vnic_wq *wq,
126 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
128 enic_free_wq_buf(wq, buf);
131 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
132 u8 type, u16 q_number, u16 completed_index, void *opaque)
134 struct enic *enic = vnic_dev_priv(vdev);
136 vnic_wq_service(&enic->wq[q_number], cq_desc,
137 completed_index, enic_wq_free_buf,
143 static void enic_log_q_error(struct enic *enic)
148 for (i = 0; i < enic->wq_count; i++) {
149 error_status = vnic_wq_error_status(&enic->wq[i]);
151 dev_err(enic, "WQ[%d] error_status %d\n", i,
155 for (i = 0; i < enic->rq_count; i++) {
156 error_status = vnic_rq_error_status(&enic->rq[i]);
158 dev_err(enic, "RQ[%d] error_status %d\n", i,
163 unsigned int enic_cleanup_wq(struct enic *enic, struct vnic_wq *wq)
165 unsigned int cq = enic_cq_wq(enic, wq->index);
167 /* Return the work done */
168 return vnic_cq_service(&enic->cq[cq],
169 -1 /*wq_work_to_do*/, enic_wq_service, NULL);
173 int enic_send_pkt(struct enic *enic, struct vnic_wq *wq,
174 struct rte_mbuf *tx_pkt, unsigned short len,
175 u_int8_t sop, u_int8_t eop,
176 u_int16_t ol_flags, u_int16_t vlan_tag)
178 struct wq_enet_desc *desc = vnic_wq_next_desc(wq);
180 u_int16_t header_length = 0;
181 u_int8_t cq_entry = eop;
182 u_int8_t vlan_tag_insert = 0;
183 unsigned char *buf = (unsigned char *)(tx_pkt->buf_addr) +
184 RTE_PKTMBUF_HEADROOM;
185 u_int64_t bus_addr = (dma_addr_t)
186 (tx_pkt->buf_physaddr + RTE_PKTMBUF_HEADROOM);
189 if (ol_flags & PKT_TX_VLAN_PKT)
192 if (enic->hw_ip_checksum) {
193 if (ol_flags & PKT_TX_IP_CKSUM)
194 mss |= ENIC_CALC_IP_CKSUM;
196 if (ol_flags & PKT_TX_TCP_UDP_CKSUM)
197 mss |= ENIC_CALC_TCP_UDP_CKSUM;
201 wq_enet_desc_enc(desc,
205 0 /* header_length */,
206 0 /* offload_mode WQ_ENET_OFFLOAD_MODE_CSUM */,
214 vnic_wq_post(wq, (void *)tx_pkt, bus_addr, len,
218 0 /*compressed send*/,
224 void enic_dev_stats_clear(struct enic *enic)
226 if (vnic_dev_stats_clear(enic->vdev))
227 dev_err(enic, "Error in clearing stats\n");
230 void enic_dev_stats_get(struct enic *enic, struct rte_eth_stats *r_stats)
232 struct vnic_stats *stats;
234 memset(r_stats, 0, sizeof(*r_stats));
235 if (vnic_dev_stats_dump(enic->vdev, &stats)) {
236 dev_err(enic, "Error in getting stats\n");
240 r_stats->ipackets = stats->rx.rx_frames_ok;
241 r_stats->opackets = stats->tx.tx_frames_ok;
243 r_stats->ibytes = stats->rx.rx_bytes_ok;
244 r_stats->obytes = stats->tx.tx_bytes_ok;
246 r_stats->ierrors = stats->rx.rx_errors;
247 r_stats->oerrors = stats->tx.tx_errors;
249 r_stats->imcasts = stats->rx.rx_multicast_frames_ok;
250 r_stats->rx_nombuf = stats->rx.rx_no_bufs;
253 void enic_del_mac_address(struct enic *enic)
255 if (vnic_dev_del_addr(enic->vdev, enic->mac_addr))
256 dev_err(enic, "del mac addr failed\n");
259 void enic_set_mac_address(struct enic *enic, uint8_t *mac_addr)
263 if (!is_eth_addr_valid(mac_addr)) {
264 dev_err(enic, "invalid mac address\n");
268 err = vnic_dev_del_addr(enic->vdev, mac_addr);
270 dev_err(enic, "del mac addr failed\n");
274 ether_addr_copy((struct ether_addr *)mac_addr,
275 (struct ether_addr *)enic->mac_addr);
277 err = vnic_dev_add_addr(enic->vdev, mac_addr);
279 dev_err(enic, "add mac addr failed\n");
284 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
286 struct enic *enic = vnic_dev_priv(rq->vdev);
291 rte_pktmbuf_free((struct rte_mbuf *)buf->os_buf);
295 void enic_init_vnic_resources(struct enic *enic)
297 unsigned int error_interrupt_enable = 1;
298 unsigned int error_interrupt_offset = 0;
300 unsigned int cq_index = 0;
302 for (index = 0; index < enic->rq_count; index++) {
303 vnic_rq_init(&enic->rq[index],
304 enic_cq_rq(enic, index),
305 error_interrupt_enable,
306 error_interrupt_offset);
309 for (index = 0; index < enic->wq_count; index++) {
310 vnic_wq_init(&enic->wq[index],
311 enic_cq_wq(enic, index),
312 error_interrupt_enable,
313 error_interrupt_offset);
316 vnic_dev_stats_clear(enic->vdev);
318 for (index = 0; index < enic->cq_count; index++) {
319 vnic_cq_init(&enic->cq[index],
320 0 /* flow_control_enable */,
321 1 /* color_enable */,
324 1 /* cq_tail_color */,
325 0 /* interrupt_enable */,
326 1 /* cq_entry_enable */,
327 0 /* cq_message_enable */,
328 0 /* interrupt offset */,
329 0 /* cq_message_addr */);
332 vnic_intr_init(&enic->intr,
333 enic->config.intr_timer_usec,
334 enic->config.intr_timer_type,
335 /*mask_on_assertion*/1);
339 static int enic_rq_alloc_buf(struct vnic_rq *rq)
341 struct enic *enic = vnic_dev_priv(rq->vdev);
344 struct rq_enet_desc *desc = vnic_rq_next_desc(rq);
345 u_int8_t type = RQ_ENET_TYPE_ONLY_SOP;
346 u_int16_t len = ENIC_MAX_MTU + VLAN_ETH_HLEN;
347 u16 split_hdr_size = vnic_get_hdr_split_size(enic->vdev);
348 struct rte_mbuf *mbuf = enic_rxmbuf_alloc(rq->mp);
349 struct rte_mbuf *hdr_mbuf = NULL;
352 dev_err(enic, "mbuf alloc in enic_rq_alloc_buf failed\n");
356 if (unlikely(split_hdr_size)) {
357 if (vnic_rq_desc_avail(rq) < 2) {
358 rte_mempool_put(mbuf->pool, mbuf);
361 hdr_mbuf = enic_rxmbuf_alloc(rq->mp);
363 rte_mempool_put(mbuf->pool, mbuf);
365 "hdr_mbuf alloc in enic_rq_alloc_buf failed\n");
369 hdr_mbuf->data_off = RTE_PKTMBUF_HEADROOM;
370 buf = rte_pktmbuf_mtod(hdr_mbuf, void *);
372 hdr_mbuf->nb_segs = 2;
373 hdr_mbuf->port = rq->index;
374 hdr_mbuf->next = mbuf;
376 dma_addr = (dma_addr_t)
377 (hdr_mbuf->buf_physaddr + hdr_mbuf->data_off);
379 rq_enet_desc_enc(desc, dma_addr, type, split_hdr_size);
381 vnic_rq_post(rq, (void *)hdr_mbuf, 0 /*os_buf_index*/, dma_addr,
382 (unsigned int)split_hdr_size, 0 /*wrid*/);
384 desc = vnic_rq_next_desc(rq);
385 type = RQ_ENET_TYPE_NOT_SOP;
388 mbuf->port = rq->index;
391 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
392 buf = rte_pktmbuf_mtod(mbuf, void *);
395 dma_addr = (dma_addr_t)
396 (mbuf->buf_physaddr + mbuf->data_off);
398 rq_enet_desc_enc(desc, dma_addr, type, mbuf->buf_len);
400 vnic_rq_post(rq, (void *)mbuf, 0 /*os_buf_index*/, dma_addr,
401 (unsigned int)mbuf->buf_len, 0 /*wrid*/);
406 static int enic_rq_indicate_buf(struct vnic_rq *rq,
407 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
408 int skipped, void *opaque)
410 struct enic *enic = vnic_dev_priv(rq->vdev);
411 struct rte_mbuf **rx_pkt_bucket = (struct rte_mbuf **)opaque;
412 struct rte_mbuf *rx_pkt = NULL;
413 struct rte_mbuf *hdr_rx_pkt = NULL;
415 u8 type, color, eop, sop, ingress_port, vlan_stripped;
416 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
417 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
418 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
420 u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
423 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
424 &type, &color, &q_number, &completed_index,
425 &ingress_port, &fcoe, &eop, &sop, &rss_type,
426 &csum_not_calc, &rss_hash, &bytes_written,
427 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
428 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
429 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
430 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
434 dev_err(enic, "packet error\n");
438 rx_pkt = (struct rte_mbuf *)buf->os_buf;
441 if (unlikely(skipped)) {
442 rx_pkt->data_len = 0;
446 if (likely(!vnic_get_hdr_split_size(enic->vdev))) {
447 /* No header split configured */
448 *rx_pkt_bucket = rx_pkt;
449 rx_pkt->pkt_len = bytes_written;
452 rx_pkt->ol_flags |= PKT_RX_IPV4_HDR;
453 if (!csum_not_calc) {
454 if (unlikely(!ipv4_csum_ok))
455 rx_pkt->ol_flags |= PKT_RX_IP_CKSUM_BAD;
457 if ((tcp || udp) && (!tcp_udp_csum_ok))
458 rx_pkt->ol_flags |= PKT_RX_L4_CKSUM_BAD;
461 rx_pkt->ol_flags |= PKT_RX_IPV6_HDR;
465 /* This piece is header */
466 *rx_pkt_bucket = rx_pkt;
467 rx_pkt->pkt_len = bytes_written;
470 /* The packet is smaller than split_hdr_size */
471 *rx_pkt_bucket = rx_pkt;
472 rx_pkt->pkt_len = bytes_written;
474 rx_pkt->ol_flags |= PKT_RX_IPV4_HDR;
475 if (!csum_not_calc) {
476 if (unlikely(!ipv4_csum_ok))
486 rx_pkt->ol_flags |= PKT_RX_IPV6_HDR;
489 hdr_rx_pkt = *rx_pkt_bucket;
490 hdr_rx_pkt->pkt_len += bytes_written;
492 hdr_rx_pkt->ol_flags |= PKT_RX_IPV4_HDR;
493 if (!csum_not_calc) {
494 if (unlikely(!ipv4_csum_ok))
495 hdr_rx_pkt->ol_flags |=
500 hdr_rx_pkt->ol_flags |=
504 hdr_rx_pkt->ol_flags |= PKT_RX_IPV6_HDR;
510 rx_pkt->data_len = bytes_written;
513 rx_pkt->ol_flags |= PKT_RX_RSS_HASH;
514 rx_pkt->hash.rss = rss_hash;
518 rx_pkt->ol_flags |= PKT_RX_VLAN_PKT;
519 rx_pkt->vlan_tci = vlan_tci;
525 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
526 u8 type, u16 q_number, u16 completed_index, void *opaque)
528 struct enic *enic = vnic_dev_priv(vdev);
530 return vnic_rq_service(&enic->rq[q_number], cq_desc,
531 completed_index, VNIC_RQ_RETURN_DESC,
532 enic_rq_indicate_buf, opaque);
536 int enic_poll(struct vnic_rq *rq, struct rte_mbuf **rx_pkts,
537 unsigned int budget, unsigned int *work_done)
539 struct enic *enic = vnic_dev_priv(rq->vdev);
540 unsigned int cq = enic_cq_rq(enic, rq->index);
543 *work_done = vnic_cq_service(&enic->cq[cq],
544 budget, enic_rq_service, (void *)rx_pkts);
547 vnic_rq_fill(rq, enic_rq_alloc_buf);
549 /* Need at least one buffer on ring to get going */
550 if (vnic_rq_desc_used(rq) == 0) {
551 dev_err(enic, "Unable to alloc receive buffers\n");
558 void *enic_alloc_consistent(void *priv, size_t size,
559 dma_addr_t *dma_handle, u8 *name)
561 struct enic *enic = (struct enic *)priv;
563 const struct rte_memzone *rz;
566 rz = rte_memzone_reserve_aligned(name, size, 0, 0, ENIC_ALIGN);
568 pr_err("%s : Failed to allocate memory requested for %s",
574 *dma_handle = (dma_addr_t)rz->phys_addr;
579 void enic_free_consistent(struct rte_pci_device *hwdev, size_t size,
580 void *vaddr, dma_addr_t dma_handle)
582 /* Nothing to be done */
585 void enic_intr_handler(__rte_unused struct rte_intr_handle *handle,
588 struct enic *enic = pmd_priv((struct rte_eth_dev *)arg);
590 dev_err(enic, "Err intr.\n");
591 vnic_intr_return_all_credits(&enic->intr);
593 enic_log_q_error(enic);
596 int enic_enable(struct enic *enic)
600 char mz_name[RTE_MEMZONE_NAMESIZE];
601 const struct rte_memzone *rmz;
602 struct rte_eth_dev *eth_dev = enic->rte_dev;
604 eth_dev->data->dev_link.link_speed = vnic_dev_port_speed(enic->vdev);
605 eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
606 vnic_dev_notify_set(enic->vdev, -1); /* No Intr for notify */
608 if (enic_clsf_init(enic))
609 dev_warning(enic, "Init of hash table for clsf failed."\
610 "Flow director feature will not work\n");
613 for (index = 0; index < enic->rq_count; index++) {
614 vnic_rq_fill(&enic->rq[index], enic_rq_alloc_buf);
616 /* Need at least one buffer on ring to get going
618 if (vnic_rq_desc_used(&enic->rq[index]) == 0) {
619 dev_err(enic, "Unable to alloc receive buffers\n");
624 for (index = 0; index < enic->wq_count; index++)
625 vnic_wq_enable(&enic->wq[index]);
626 for (index = 0; index < enic->rq_count; index++)
627 vnic_rq_enable(&enic->rq[index]);
629 vnic_dev_enable_wait(enic->vdev);
632 /* Register and enable error interrupt */
633 rte_intr_callback_register(&(enic->pdev->intr_handle),
634 enic_intr_handler, (void *)enic->rte_dev);
636 rte_intr_enable(&(enic->pdev->intr_handle));
638 vnic_intr_unmask(&enic->intr);
643 int enic_alloc_intr_resources(struct enic *enic)
647 dev_info(enic, "vNIC resources used: "\
648 "wq %d rq %d cq %d intr %d\n",
649 enic->wq_count, enic->rq_count,
650 enic->cq_count, enic->intr_count);
652 err = vnic_intr_alloc(enic->vdev, &enic->intr, 0);
654 enic_free_vnic_resources(enic);
659 void enic_free_rq(void *rxq)
661 struct vnic_rq *rq = (struct vnic_rq *)rxq;
662 struct enic *enic = vnic_dev_priv(rq->vdev);
665 vnic_cq_free(&enic->cq[rq->index]);
668 void enic_start_wq(struct enic *enic, uint16_t queue_idx)
670 vnic_wq_enable(&enic->wq[queue_idx]);
673 int enic_stop_wq(struct enic *enic, uint16_t queue_idx)
675 return vnic_wq_disable(&enic->wq[queue_idx]);
678 void enic_start_rq(struct enic *enic, uint16_t queue_idx)
680 vnic_rq_enable(&enic->rq[queue_idx]);
683 int enic_stop_rq(struct enic *enic, uint16_t queue_idx)
685 return vnic_rq_disable(&enic->rq[queue_idx]);
688 int enic_alloc_rq(struct enic *enic, uint16_t queue_idx,
689 unsigned int socket_id, struct rte_mempool *mp,
693 struct vnic_rq *rq = &enic->rq[queue_idx];
695 rq->socket_id = socket_id;
699 if (nb_desc > enic->config.rq_desc_count) {
701 "RQ %d - number of rx desc in cmd line (%d)"\
702 "is greater than that in the UCSM/CIMC adapter"\
703 "policy. Applying the value in the adapter "\
705 queue_idx, nb_desc, enic->config.rq_desc_count);
706 } else if (nb_desc != enic->config.rq_desc_count) {
707 enic->config.rq_desc_count = nb_desc;
709 "RX Queues - effective number of descs:%d\n",
714 /* Allocate queue resources */
715 err = vnic_rq_alloc(enic->vdev, &enic->rq[queue_idx], queue_idx,
716 enic->config.rq_desc_count,
717 sizeof(struct rq_enet_desc));
719 dev_err(enic, "error in allocation of rq\n");
723 err = vnic_cq_alloc(enic->vdev, &enic->cq[queue_idx], queue_idx,
724 socket_id, enic->config.rq_desc_count,
725 sizeof(struct cq_enet_rq_desc));
728 dev_err(enic, "error in allocation of cq for rq\n");
734 void enic_free_wq(void *txq)
736 struct vnic_wq *wq = (struct vnic_wq *)txq;
737 struct enic *enic = vnic_dev_priv(wq->vdev);
740 vnic_cq_free(&enic->cq[enic->rq_count + wq->index]);
743 int enic_alloc_wq(struct enic *enic, uint16_t queue_idx,
744 unsigned int socket_id, uint16_t nb_desc)
747 struct vnic_wq *wq = &enic->wq[queue_idx];
748 unsigned int cq_index = enic_cq_wq(enic, queue_idx);
750 wq->socket_id = socket_id;
752 if (nb_desc > enic->config.wq_desc_count) {
754 "WQ %d - number of tx desc in cmd line (%d)"\
755 "is greater than that in the UCSM/CIMC adapter"\
756 "policy. Applying the value in the adapter "\
758 queue_idx, nb_desc, enic->config.wq_desc_count);
759 } else if (nb_desc != enic->config.wq_desc_count) {
760 enic->config.wq_desc_count = nb_desc;
762 "TX Queues - effective number of descs:%d\n",
767 /* Allocate queue resources */
768 err = vnic_wq_alloc(enic->vdev, &enic->wq[queue_idx], queue_idx,
769 enic->config.wq_desc_count,
770 sizeof(struct wq_enet_desc));
772 dev_err(enic, "error in allocation of wq\n");
776 err = vnic_cq_alloc(enic->vdev, &enic->cq[cq_index], cq_index,
777 socket_id, enic->config.wq_desc_count,
778 sizeof(struct cq_enet_wq_desc));
781 dev_err(enic, "error in allocation of cq for wq\n");
787 int enic_disable(struct enic *enic)
792 vnic_intr_mask(&enic->intr);
793 (void)vnic_intr_masked(&enic->intr); /* flush write */
795 vnic_dev_disable(enic->vdev);
797 enic_clsf_destroy(enic);
799 if (!enic_is_sriov_vf(enic))
800 vnic_dev_del_addr(enic->vdev, enic->mac_addr);
802 for (i = 0; i < enic->wq_count; i++) {
803 err = vnic_wq_disable(&enic->wq[i]);
807 for (i = 0; i < enic->rq_count; i++) {
808 err = vnic_rq_disable(&enic->rq[i]);
813 vnic_dev_set_reset_flag(enic->vdev, 1);
814 vnic_dev_notify_unset(enic->vdev);
816 for (i = 0; i < enic->wq_count; i++)
817 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
818 for (i = 0; i < enic->rq_count; i++)
819 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
820 for (i = 0; i < enic->cq_count; i++)
821 vnic_cq_clean(&enic->cq[i]);
822 vnic_intr_clean(&enic->intr);
827 static int enic_dev_wait(struct vnic_dev *vdev,
828 int (*start)(struct vnic_dev *, int),
829 int (*finished)(struct vnic_dev *, int *),
836 err = start(vdev, arg);
840 /* Wait for func to complete...2 seconds max */
841 for (i = 0; i < 2000; i++) {
842 err = finished(vdev, &done);
852 static int enic_dev_open(struct enic *enic)
856 err = enic_dev_wait(enic->vdev, vnic_dev_open,
857 vnic_dev_open_done, 0);
859 dev_err(enic_get_dev(enic),
860 "vNIC device open failed, err %d\n", err);
865 static int enic_set_rsskey(struct enic *enic)
867 dma_addr_t rss_key_buf_pa;
868 union vnic_rss_key *rss_key_buf_va = NULL;
869 union vnic_rss_key rss_key = {
870 .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
871 .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
872 .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
873 .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
878 snprintf(name, NAME_MAX, "rss_key-%s", enic->bdf_name);
879 rss_key_buf_va = enic_alloc_consistent(enic, sizeof(union vnic_rss_key),
880 &rss_key_buf_pa, name);
884 rte_memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
886 err = enic_set_rss_key(enic,
888 sizeof(union vnic_rss_key));
890 enic_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
891 rss_key_buf_va, rss_key_buf_pa);
896 static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
898 dma_addr_t rss_cpu_buf_pa;
899 union vnic_rss_cpu *rss_cpu_buf_va = NULL;
904 snprintf(name, NAME_MAX, "rss_cpu-%s", enic->bdf_name);
905 rss_cpu_buf_va = enic_alloc_consistent(enic, sizeof(union vnic_rss_cpu),
906 &rss_cpu_buf_pa, name);
910 for (i = 0; i < (1 << rss_hash_bits); i++)
911 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
913 err = enic_set_rss_cpu(enic,
915 sizeof(union vnic_rss_cpu));
917 enic_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
918 rss_cpu_buf_va, rss_cpu_buf_pa);
923 static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
924 u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
926 const u8 tso_ipid_split_en = 0;
929 /* Enable VLAN tag stripping */
931 err = enic_set_nic_cfg(enic,
932 rss_default_cpu, rss_hash_type,
933 rss_hash_bits, rss_base_cpu,
934 rss_enable, tso_ipid_split_en,
935 enic->ig_vlan_strip_en);
940 int enic_set_rss_nic_cfg(struct enic *enic)
942 const u8 rss_default_cpu = 0;
943 const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
944 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
945 NIC_CFG_RSS_HASH_TYPE_IPV6 |
946 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
947 const u8 rss_hash_bits = 7;
948 const u8 rss_base_cpu = 0;
949 u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
952 if (!enic_set_rsskey(enic)) {
953 if (enic_set_rsscpu(enic, rss_hash_bits)) {
955 dev_warning(enic, "RSS disabled, "\
956 "Failed to set RSS cpu indirection table.");
961 "RSS disabled, Failed to set RSS key.\n");
965 return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
966 rss_hash_bits, rss_base_cpu, rss_enable);
969 int enic_setup_finish(struct enic *enic)
973 ret = enic_set_rss_nic_cfg(enic);
975 dev_err(enic, "Failed to config nic, aborting.\n");
979 vnic_dev_add_addr(enic->vdev, enic->mac_addr);
982 vnic_dev_packet_filter(enic->vdev,
996 static void enic_eventfd_init(struct enic *enic)
998 enic->eventfd = enic->pdev->intr_handle.fd;
1001 void *enic_err_intr_handler(void *arg)
1003 struct enic *enic = (struct enic *)arg;
1004 unsigned int intr = enic_msix_err_intr(enic);
1009 size = read(enic->eventfd, &data, sizeof(data));
1010 dev_err(enic, "Err intr.\n");
1011 vnic_intr_return_all_credits(&enic->intr);
1013 enic_log_q_error(enic);
1020 void enic_add_packet_filter(struct enic *enic)
1022 /* Args -> directed, multicast, broadcast, promisc, allmulti */
1023 vnic_dev_packet_filter(enic->vdev, 1, 1, 1,
1024 enic->promisc, enic->allmulti);
1027 int enic_get_link_status(struct enic *enic)
1029 return vnic_dev_link_status(enic->vdev);
1034 static int enic_create_err_intr_thread(struct enic *enic)
1036 pthread_attr_t intr_attr;
1038 /* create threads for error interrupt handling */
1039 pthread_attr_init(&intr_attr);
1040 pthread_attr_setstacksize(&intr_attr, 0x100000);
1043 if (pthread_create(&enic->err_intr_thread, &intr_attr,
1044 enic_err_intr_handler, (void *)enic)) {
1045 dev_err(enic, "Failed to create err interrupt handler threads\n");
1049 pthread_attr_destroy(&intr_attr);
1055 static int enic_set_intr_mode(struct enic *enic)
1057 struct vfio_irq_set *irq_set;
1063 if (enic->intr_count < 1) {
1064 dev_err(enic, "Unsupported resource conf.\n");
1067 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSIX);
1069 enic->intr_count = 1;
1071 enic_eventfd_init(enic);
1072 size = sizeof(*irq_set) + (sizeof(int));
1074 irq_set = rte_zmalloc("enic_vfio_irq", size, 0);
1075 irq_set->argsz = size;
1076 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
1078 irq_set->count = 1; /* For error interrupt only */
1079 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
1080 VFIO_IRQ_SET_ACTION_TRIGGER;
1081 fds = (int *)&irq_set->data;
1083 fds[0] = enic->eventfd;
1085 ret = ioctl(enic->pdev->intr_handle.vfio_dev_fd,
1086 VFIO_DEVICE_SET_IRQS, irq_set);
1089 dev_err(enic, "Failed to set eventfds for interrupts\n");
1093 enic_create_err_intr_thread(enic);
1097 static void enic_clear_intr_mode(struct enic *enic)
1099 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
1103 static void enic_dev_deinit(struct enic *enic)
1106 struct rte_eth_dev *eth_dev = enic->rte_dev;
1108 if (eth_dev->data->mac_addrs)
1109 rte_free(eth_dev->data->mac_addrs);
1112 enic_clear_intr_mode(enic);
1117 int enic_set_vnic_res(struct enic *enic)
1119 struct rte_eth_dev *eth_dev = enic->rte_dev;
1121 if ((enic->rq_count < eth_dev->data->nb_rx_queues) ||
1122 (enic->wq_count < eth_dev->data->nb_tx_queues)) {
1123 dev_err(dev, "Not enough resources configured, aborting\n");
1127 enic->rq_count = eth_dev->data->nb_rx_queues;
1128 enic->wq_count = eth_dev->data->nb_tx_queues;
1129 if (enic->cq_count < (enic->rq_count + enic->wq_count)) {
1130 dev_err(dev, "Not enough resources configured, aborting\n");
1134 enic->cq_count = enic->rq_count + enic->wq_count;
1138 static int enic_dev_init(struct enic *enic)
1142 struct rte_eth_dev *eth_dev = enic->rte_dev;
1144 vnic_dev_intr_coal_timer_info_default(enic->vdev);
1146 /* Get vNIC configuration
1148 err = enic_get_vnic_config(enic);
1150 dev_err(dev, "Get vNIC configuration failed, aborting\n");
1154 eth_dev->data->mac_addrs = rte_zmalloc("enic_mac_addr", ETH_ALEN, 0);
1155 if (!eth_dev->data->mac_addrs) {
1156 dev_err(enic, "mac addr storage alloc failed, aborting.\n");
1159 ether_addr_copy((struct ether_addr *) enic->mac_addr,
1160 ð_dev->data->mac_addrs[0]);
1163 /* Get available resource counts
1165 enic_get_res_counts(enic);
1168 /* Set interrupt mode based on resource counts and system
1171 err = enic_set_intr_mode(enic);
1173 rte_free(eth_dev->data->mac_addrs);
1174 enic_clear_intr_mode(enic);
1175 dev_err(dev, "Failed to set intr mode based on resource "\
1176 "counts and system capabilities, aborting\n");
1181 vnic_dev_set_reset_flag(enic->vdev, 0);
1187 int enic_probe(struct enic *enic)
1189 const char *bdf = enic->bdf_name;
1190 struct rte_pci_device *pdev = enic->pdev;
1191 struct rte_eth_dev *eth_dev = enic->rte_dev;
1195 dev_info(enic, " Initializing ENIC PMD version %s\n", DRV_VERSION);
1197 enic->bar0.vaddr = (void *)pdev->mem_resource[0].addr;
1198 enic->bar0.len = pdev->mem_resource[0].len;
1200 /* Register vNIC device */
1201 enic->vdev = vnic_dev_register(NULL, enic, enic->pdev, &enic->bar0, 1);
1203 dev_err(enic, "vNIC registration failed, aborting\n");
1207 vnic_register_cbacks(enic->vdev,
1208 enic_alloc_consistent,
1209 enic_free_consistent);
1211 /* Issue device open to get device in known state */
1212 err = enic_dev_open(enic);
1214 dev_err(enic, "vNIC dev open failed, aborting\n");
1215 goto err_out_unregister;
1218 /* Set ingress vlan rewrite mode before vnic initialization */
1219 err = vnic_dev_set_ig_vlan_rewrite_mode(enic->vdev,
1220 IG_VLAN_REWRITE_MODE_PRIORITY_TAG_DEFAULT_VLAN);
1223 "Failed to set ingress vlan rewrite mode, aborting.\n");
1224 goto err_out_dev_close;
1227 /* Issue device init to initialize the vnic-to-switch link.
1228 * We'll start with carrier off and wait for link UP
1229 * notification later to turn on carrier. We don't need
1230 * to wait here for the vnic-to-switch link initialization
1231 * to complete; link UP notification is the indication that
1232 * the process is complete.
1235 err = vnic_dev_init(enic->vdev, 0);
1237 dev_err(enic, "vNIC dev init failed, aborting\n");
1238 goto err_out_dev_close;
1241 err = enic_dev_init(enic);
1243 dev_err(enic, "Device initialization failed, aborting\n");
1244 goto err_out_dev_close;
1250 vnic_dev_close(enic->vdev);
1252 vnic_dev_unregister(enic->vdev);
1257 void enic_remove(struct enic *enic)
1259 enic_dev_deinit(enic);
1260 vnic_dev_close(enic->vdev);
1261 vnic_dev_unregister(enic->vdev);