1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2008-2017 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
6 #include <rte_memzone.h>
7 #include <rte_memcpy.h>
8 #include <rte_string_fns.h>
12 #include "vnic_resource.h"
13 #include "vnic_devcmd.h"
15 #include "vnic_stats.h"
16 #include "vnic_flowman.h"
19 enum vnic_proxy_type {
31 struct vnic_intr_coal_timer_info {
39 struct rte_pci_device *pdev;
40 struct vnic_res res[RES_TYPE_MAX];
41 enum vnic_dev_intr_mode intr_mode;
42 struct vnic_devcmd __iomem *devcmd;
43 struct vnic_devcmd_notify *notify;
44 struct vnic_devcmd_notify notify_copy;
47 dma_addr_t linkstatus_pa;
48 struct vnic_stats *stats;
50 struct vnic_devcmd_fw_info *fw_info;
51 dma_addr_t fw_info_pa;
52 struct fm_info *flowman_info;
53 dma_addr_t flowman_info_pa;
54 enum vnic_proxy_type proxy;
56 uint64_t args[VNIC_DEVCMD_NARGS];
58 struct vnic_intr_coal_timer_info intr_coal_timer_info;
59 void *(*alloc_consistent)(void *priv, size_t size,
60 dma_addr_t *dma_handle, uint8_t *name);
61 void (*free_consistent)(void *priv,
62 size_t size, void *vaddr,
63 dma_addr_t dma_handle);
65 * Used to serialize devcmd access, currently from PF and its
66 * VF representors. When there are no representors, lock is
70 void (*lock)(void *priv);
71 void (*unlock)(void *priv);
72 struct vnic_dev *pf_vdev;
76 #define VNIC_MAX_RES_HDR_SIZE \
77 (sizeof(struct vnic_resource_header) + \
78 sizeof(struct vnic_resource) * RES_TYPE_MAX)
79 #define VNIC_RES_STRIDE 128
81 void *vnic_dev_priv(struct vnic_dev *vdev)
86 void vnic_register_cbacks(struct vnic_dev *vdev,
87 void *(*alloc_consistent)(void *priv, size_t size,
88 dma_addr_t *dma_handle, uint8_t *name),
89 void (*free_consistent)(void *priv,
90 size_t size, void *vaddr,
91 dma_addr_t dma_handle))
93 vdev->alloc_consistent = alloc_consistent;
94 vdev->free_consistent = free_consistent;
97 void vnic_register_lock(struct vnic_dev *vdev, void (*lock)(void *priv),
98 void (*unlock)(void *priv))
101 vdev->unlock = unlock;
105 static int vnic_dev_discover_res(struct vnic_dev *vdev,
106 struct vnic_dev_bar *bar, unsigned int num_bars)
108 struct vnic_resource_header __iomem *rh;
109 struct mgmt_barmap_hdr __iomem *mrh;
110 struct vnic_resource __iomem *r;
116 if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
117 pr_err("vNIC BAR0 res hdr length error\n");
124 pr_err("vNIC BAR0 res hdr not mem-mapped\n");
128 /* Check for mgmt vnic in addition to normal vnic */
129 if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
130 (ioread32(&rh->version) != VNIC_RES_VERSION)) {
131 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
132 (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
133 pr_err("vNIC BAR0 res magic/version error " \
134 "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
135 VNIC_RES_MAGIC, VNIC_RES_VERSION,
136 MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
137 ioread32(&rh->magic), ioread32(&rh->version));
142 if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
143 r = (struct vnic_resource __iomem *)(mrh + 1);
145 r = (struct vnic_resource __iomem *)(rh + 1);
148 while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
149 uint8_t bar_num = ioread8(&r->bar);
150 uint32_t bar_offset = ioread32(&r->bar_offset);
151 uint32_t count = ioread32(&r->count);
156 if (bar_num >= num_bars)
159 if (!bar[bar_num].len || !bar[bar_num].vaddr)
166 case RES_TYPE_INTR_CTRL:
167 /* each count is stride bytes long */
168 len = count * VNIC_RES_STRIDE;
169 if (len + bar_offset > bar[bar_num].len) {
170 pr_err("vNIC BAR0 resource %d " \
171 "out-of-bounds, offset 0x%x + " \
172 "size 0x%x > bar len 0x%lx\n",
179 case RES_TYPE_INTR_PBA_LEGACY:
180 case RES_TYPE_DEVCMD:
187 vdev->res[type].count = count;
188 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
190 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
196 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
197 enum vnic_res_type type)
199 return vdev->res[type].count;
202 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
205 if (!vdev->res[type].vaddr)
212 case RES_TYPE_INTR_CTRL:
213 return (char __iomem *)vdev->res[type].vaddr +
214 index * VNIC_RES_STRIDE;
216 return (char __iomem *)vdev->res[type].vaddr;
220 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
221 unsigned int desc_count, unsigned int desc_size)
223 /* The base address of the desc rings must be 512 byte aligned.
224 * Descriptor count is aligned to groups of 32 descriptors. A
225 * count of 0 means the maximum 4096 descriptors. Descriptor
226 * size is aligned to 16 bytes.
229 unsigned int count_align = 32;
230 unsigned int desc_align = 16;
232 ring->base_align = 512;
237 ring->desc_count = VNIC_ALIGN(desc_count, count_align);
239 ring->desc_size = VNIC_ALIGN(desc_size, desc_align);
241 ring->size = ring->desc_count * ring->desc_size;
242 ring->size_unaligned = ring->size + ring->base_align;
244 return ring->size_unaligned;
247 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
249 memset(ring->descs, 0, ring->size);
252 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev,
253 struct vnic_dev_ring *ring,
254 unsigned int desc_count, unsigned int desc_size,
255 __rte_unused unsigned int socket_id,
259 dma_addr_t alloc_pa = 0;
261 vnic_dev_desc_ring_size(ring, desc_count, desc_size);
262 alloc_addr = vdev->alloc_consistent(vdev->priv,
263 ring->size_unaligned,
264 &alloc_pa, (uint8_t *)z_name);
266 pr_err("Failed to allocate ring (size=%d), aborting\n",
270 ring->descs_unaligned = alloc_addr;
272 pr_err("Failed to map allocated ring (size=%d), aborting\n",
274 vdev->free_consistent(vdev->priv,
275 ring->size_unaligned,
280 ring->base_addr_unaligned = alloc_pa;
282 ring->base_addr = VNIC_ALIGN(ring->base_addr_unaligned,
284 ring->descs = (uint8_t *)ring->descs_unaligned +
285 (ring->base_addr - ring->base_addr_unaligned);
287 vnic_dev_clear_desc_ring(ring);
289 ring->desc_avail = ring->desc_count - 1;
294 void vnic_dev_free_desc_ring(__rte_unused struct vnic_dev *vdev,
295 struct vnic_dev_ring *ring)
298 vdev->free_consistent(vdev->priv,
299 ring->size_unaligned,
300 ring->descs_unaligned,
301 ring->base_addr_unaligned);
306 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
309 struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
315 status = ioread32(&devcmd->status);
316 if (status == 0xFFFFFFFF) {
317 /* PCI-e target device is gone */
320 if (status & STAT_BUSY) {
322 pr_err("Busy devcmd %d\n", _CMD_N(cmd));
326 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
327 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
328 writeq(vdev->args[i], &devcmd->args[i]);
329 rte_wmb(); /* complete all writes initiated till now */
332 iowrite32(cmd, &devcmd->cmd);
334 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
337 for (delay = 0; delay < wait; delay++) {
341 status = ioread32(&devcmd->status);
342 if (status == 0xFFFFFFFF) {
343 /* PCI-e target device is gone */
347 if (!(status & STAT_BUSY)) {
348 if (status & STAT_ERROR) {
349 err = -(int)readq(&devcmd->args[0]);
350 if (cmd != CMD_CAPABILITY &&
351 cmd != CMD_OVERLAY_OFFLOAD_CTRL &&
352 cmd != CMD_GET_SUPP_FEATURE_VER)
353 pr_err("Devcmd %d failed " \
354 "with error code %d\n",
359 if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
360 rte_rmb();/* finish all reads */
361 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
362 vdev->args[i] = readq(&devcmd->args[i]);
369 pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
373 static int vnic_dev_cmd_proxy(struct vnic_dev *vdev,
374 enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd,
375 uint64_t *args, int nargs, int wait)
381 * Proxy command consumes 2 arguments. One for proxy index,
382 * the other is for command to be proxied
384 if (nargs > VNIC_DEVCMD_NARGS - 2) {
385 pr_err("number of args %d exceeds the maximum\n", nargs);
388 memset(vdev->args, 0, sizeof(vdev->args));
390 vdev->args[0] = vdev->proxy_index;
392 memcpy(&vdev->args[2], args, nargs * sizeof(args[0]));
394 err = _vnic_dev_cmd(vdev, proxy_cmd, wait);
398 status = (uint32_t)vdev->args[0];
399 if (status & STAT_ERROR) {
400 err = (int)vdev->args[1];
401 if (err != ERR_ECMDUNKNOWN ||
402 cmd != CMD_CAPABILITY)
403 pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
407 memcpy(args, &vdev->args[1], nargs * sizeof(args[0]));
412 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
413 enum vnic_devcmd_cmd cmd, uint64_t *args, int nargs, int wait)
417 if (nargs > VNIC_DEVCMD_NARGS) {
418 pr_err("number of args %d exceeds the maximum\n", nargs);
421 memset(vdev->args, 0, sizeof(vdev->args));
422 memcpy(vdev->args, args, nargs * sizeof(args[0]));
424 err = _vnic_dev_cmd(vdev, cmd, wait);
426 memcpy(args, vdev->args, nargs * sizeof(args[0]));
431 void vnic_dev_cmd_proxy_by_index_start(struct vnic_dev *vdev, uint16_t index)
433 vdev->proxy = PROXY_BY_INDEX;
434 vdev->proxy_index = index;
437 void vnic_dev_cmd_proxy_end(struct vnic_dev *vdev)
439 vdev->proxy = PROXY_NONE;
440 vdev->proxy_index = 0;
443 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
444 uint64_t *a0, uint64_t *a1, int wait)
454 vf_idx = vdev->vf_id;
455 /* Everything below assumes PF vdev */
456 vdev = vdev->pf_vdev;
459 vdev->lock(vdev->priv);
460 /* For VF representor, proxy devcmd to VF index */
462 vnic_dev_cmd_proxy_by_index_start(vdev, vf_idx);
466 memset(vdev->args, 0, sizeof(vdev->args));
468 switch (vdev->proxy) {
470 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
471 args, ARRAY_SIZE(args), wait);
474 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
475 args, ARRAY_SIZE(args), wait);
479 err = vnic_dev_cmd_no_proxy(vdev, cmd, args, 2, wait);
484 vnic_dev_cmd_proxy_end(vdev);
486 vdev->unlock(vdev->priv);
495 int vnic_dev_cmd_args(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
496 uint64_t *args, int nargs, int wait)
505 vf_idx = vdev->vf_id;
506 vdev = vdev->pf_vdev;
509 vdev->lock(vdev->priv);
511 vnic_dev_cmd_proxy_by_index_start(vdev, vf_idx);
513 switch (vdev->proxy) {
515 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
519 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
524 err = vnic_dev_cmd_no_proxy(vdev, cmd, args, nargs, wait);
529 vnic_dev_cmd_proxy_end(vdev);
531 vdev->unlock(vdev->priv);
535 int vnic_dev_fw_info(struct vnic_dev *vdev,
536 struct vnic_devcmd_fw_info **fw_info)
538 char name[RTE_MEMZONE_NAMESIZE];
542 static uint32_t instance;
544 if (!vdev->fw_info) {
545 snprintf((char *)name, sizeof(name), "vnic_fw_info-%u",
547 vdev->fw_info = vdev->alloc_consistent(vdev->priv,
548 sizeof(struct vnic_devcmd_fw_info),
549 &vdev->fw_info_pa, (uint8_t *)name);
552 a0 = vdev->fw_info_pa;
553 a1 = sizeof(struct vnic_devcmd_fw_info);
554 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO,
557 *fw_info = vdev->fw_info;
561 static int vnic_dev_advanced_filters_cap(struct vnic_dev *vdev, uint64_t *args,
564 memset(args, 0, nargs * sizeof(*args));
565 args[0] = CMD_ADD_ADV_FILTER;
566 args[1] = FILTER_CAP_MODE_V1_FLAG;
567 return vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, nargs, 1000);
570 int vnic_dev_capable_adv_filters(struct vnic_dev *vdev)
572 uint64_t a0 = CMD_ADD_ADV_FILTER, a1 = 0;
576 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
579 return (a1 >= (uint32_t)FILTER_DPDK_1);
582 int vnic_dev_flowman_cmd(struct vnic_dev *vdev, uint64_t *args, int nargs)
586 return vnic_dev_cmd_args(vdev, CMD_FLOW_MANAGER_OP, args, nargs, wait);
589 static int vnic_dev_flowman_enable(struct vnic_dev *vdev, uint32_t *mode,
590 uint8_t *filter_actions)
592 char name[RTE_MEMZONE_NAMESIZE];
595 static uint32_t instance;
597 /* Advanced filtering is a prerequisite */
598 if (!vnic_dev_capable_adv_filters(vdev))
600 /* flowman devcmd available? */
601 if (!vnic_dev_capable(vdev, CMD_FLOW_MANAGER_OP))
603 /* Have the version we are using? */
604 args[0] = FM_API_VERSION_QUERY;
605 if (vnic_dev_flowman_cmd(vdev, args, 1))
607 if ((args[0] & (1ULL << FM_VERSION)) == 0)
609 /* Select the version */
610 args[0] = FM_API_VERSION_SELECT;
611 args[1] = FM_VERSION;
612 if (vnic_dev_flowman_cmd(vdev, args, 2))
614 /* Can we get fm_info? */
615 if (!vdev->flowman_info) {
616 snprintf((char *)name, sizeof(name), "vnic_fm_info-%u",
618 vdev->flowman_info = vdev->alloc_consistent(vdev->priv,
619 sizeof(struct fm_info),
620 &vdev->flowman_info_pa, (uint8_t *)name);
621 if (!vdev->flowman_info)
624 args[0] = FM_INFO_QUERY;
625 args[1] = vdev->flowman_info_pa;
626 args[2] = sizeof(struct fm_info);
627 if (vnic_dev_flowman_cmd(vdev, args, 3))
629 /* Have required operations? */
630 ops = (1ULL << FMOP_END) |
631 (1ULL << FMOP_DROP) |
632 (1ULL << FMOP_RQ_STEER) |
633 (1ULL << FMOP_EXACT_MATCH) |
634 (1ULL << FMOP_MARK) |
636 (1ULL << FMOP_EG_HAIRPIN) |
637 (1ULL << FMOP_ENCAP) |
638 (1ULL << FMOP_DECAP_NOSTRIP);
639 if ((vdev->flowman_info->fm_op_mask & ops) != ops)
641 /* Good to use flowman now */
642 *mode = FILTER_FLOWMAN;
643 *filter_actions = FILTER_ACTION_RQ_STEERING_FLAG |
644 FILTER_ACTION_FILTER_ID_FLAG |
645 FILTER_ACTION_COUNTER_FLAG |
646 FILTER_ACTION_DROP_FLAG;
650 /* Determine the "best" filtering mode VIC is capable of. Returns one of 4
651 * value or 0 if filtering is unavailble:
652 * FILTER_FLOWMAN- flowman api capable
653 * FILTER_DPDK_1- advanced filters availabile
654 * FILTER_USNIC_IP_FLAG - advanced filters but with the restriction that
655 * the IP layer must explicitly specified. I.e. cannot have a UDP
656 * filter that matches both IPv4 and IPv6.
657 * FILTER_IPV4_5TUPLE - fallback if either of the 2 above aren't available.
658 * all other filter types are not available.
659 * Retrun true in filter_tags if supported
661 int vnic_dev_capable_filter_mode(struct vnic_dev *vdev, uint32_t *mode,
662 uint8_t *filter_actions)
666 uint32_t max_level = 0;
668 /* If flowman is available, use it as it is the most capable API */
669 if (vnic_dev_flowman_enable(vdev, mode, filter_actions))
672 err = vnic_dev_advanced_filters_cap(vdev, args, 4);
674 /* determine supported filter actions */
675 *filter_actions = FILTER_ACTION_RQ_STEERING_FLAG; /* always available */
676 if (args[2] == FILTER_CAP_MODE_V1)
677 *filter_actions = args[3];
679 if (err || ((args[0] == 1) && (args[1] == 0))) {
680 /* Adv filter Command not supported or adv filters available but
681 * not enabled. Try the normal filter capability command.
683 args[0] = CMD_ADD_FILTER;
685 err = vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, 2, 1000);
687 * ERR_EPERM may be returned if, for example, vNIC is
688 * on a VF. It simply means no filtering is available
690 if (err == -ERR_EPERM) {
697 goto parse_max_level;
698 } else if (args[2] == FILTER_CAP_MODE_V1) {
699 /* parse filter capability mask in args[1] */
700 if (args[1] & FILTER_DPDK_1_FLAG)
701 *mode = FILTER_DPDK_1;
702 else if (args[1] & FILTER_USNIC_IP_FLAG)
703 *mode = FILTER_USNIC_IP;
704 else if (args[1] & FILTER_IPV4_5TUPLE_FLAG)
705 *mode = FILTER_IPV4_5TUPLE;
710 if (max_level >= (uint32_t)FILTER_USNIC_IP)
711 *mode = FILTER_USNIC_IP;
713 *mode = FILTER_IPV4_5TUPLE;
717 void vnic_dev_capable_udp_rss_weak(struct vnic_dev *vdev, bool *cfg_chk,
720 uint64_t a0 = CMD_NIC_CFG, a1 = 0;
726 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
727 if (err == 0 && a0 != 0 && a1 != 0) {
729 *weak = !!((a1 >> 32) & CMD_NIC_CFG_CAPF_UDP_WEAK);
733 int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
735 uint64_t a0 = (uint32_t)cmd, a1 = 0;
739 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
744 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size,
754 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
758 *(uint8_t *)value = (uint8_t)a0;
761 *(uint16_t *)value = (uint16_t)a0;
764 *(uint32_t *)value = (uint32_t)a0;
767 *(uint64_t *)value = a0;
777 int vnic_dev_stats_clear(struct vnic_dev *vdev)
779 uint64_t a0 = 0, a1 = 0;
782 return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
785 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
793 *stats = vdev->stats;
795 a1 = sizeof(struct vnic_stats);
797 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
800 int vnic_dev_close(struct vnic_dev *vdev)
802 uint64_t a0 = 0, a1 = 0;
805 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
808 int vnic_dev_enable_wait(struct vnic_dev *vdev)
810 uint64_t a0 = 0, a1 = 0;
813 if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
814 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
816 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
819 int vnic_dev_disable(struct vnic_dev *vdev)
821 uint64_t a0 = 0, a1 = 0;
824 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
827 int vnic_dev_open(struct vnic_dev *vdev, int arg)
829 uint64_t a0 = (uint32_t)arg, a1 = 0;
832 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
835 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
837 uint64_t a0 = 0, a1 = 0;
843 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
852 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, uint8_t *mac_addr)
854 uint64_t a0 = 0, a1 = 0;
858 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
861 err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
865 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
866 mac_addr[i] = ((uint8_t *)&a0)[i];
871 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
872 int broadcast, int promisc, int allmulti)
878 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
879 (multicast ? CMD_PFILTER_MULTICAST : 0) |
880 (broadcast ? CMD_PFILTER_BROADCAST : 0) |
881 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
882 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
884 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
886 pr_err("Can't set packet filter\n");
891 int vnic_dev_add_addr(struct vnic_dev *vdev, uint8_t *addr)
893 uint64_t a0 = 0, a1 = 0;
898 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
899 ((uint8_t *)&a0)[i] = addr[i];
901 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
903 pr_err("Can't add addr [" RTE_ETHER_ADDR_PRT_FMT "], %d\n",
904 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
910 int vnic_dev_del_addr(struct vnic_dev *vdev, uint8_t *addr)
912 uint64_t a0 = 0, a1 = 0;
917 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
918 ((uint8_t *)&a0)[i] = addr[i];
920 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
922 pr_err("Can't del addr [" RTE_ETHER_ADDR_PRT_FMT "], %d\n",
923 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
929 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
930 uint8_t ig_vlan_rewrite_mode)
932 uint64_t a0 = ig_vlan_rewrite_mode, a1 = 0;
935 if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
936 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
942 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
944 vdev->in_reset = state;
947 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
949 return vdev->in_reset;
952 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
953 void *notify_addr, dma_addr_t notify_pa, uint16_t intr)
959 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
960 if (!vnic_dev_in_reset(vdev)) {
961 vdev->notify = notify_addr;
962 vdev->notify_pa = notify_pa;
965 a0 = (uint64_t)notify_pa;
966 a1 = ((uint64_t)intr << 32) & 0x0000ffff00000000ULL;
967 a1 += sizeof(struct vnic_devcmd_notify);
969 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
970 if (!vnic_dev_in_reset(vdev))
971 vdev->notify_sz = (r == 0) ? (uint32_t)a1 : 0;
976 int vnic_dev_notify_set(struct vnic_dev *vdev, uint16_t intr)
978 void *notify_addr = NULL;
979 dma_addr_t notify_pa = 0;
980 char name[RTE_MEMZONE_NAMESIZE];
981 static uint32_t instance;
983 if (vdev->notify || vdev->notify_pa) {
984 return vnic_dev_notify_setcmd(vdev, vdev->notify,
985 vdev->notify_pa, intr);
987 if (!vnic_dev_in_reset(vdev)) {
988 snprintf((char *)name, sizeof(name),
989 "vnic_notify-%u", instance++);
990 notify_addr = vdev->alloc_consistent(vdev->priv,
991 sizeof(struct vnic_devcmd_notify),
992 ¬ify_pa, (uint8_t *)name);
997 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
1000 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
1006 a0 = 0; /* paddr = 0 to unset notify buffer */
1007 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
1008 a1 += sizeof(struct vnic_devcmd_notify);
1010 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
1011 if (!vnic_dev_in_reset(vdev)) {
1012 vdev->notify = NULL;
1013 vdev->notify_pa = 0;
1014 vdev->notify_sz = 0;
1020 int vnic_dev_notify_unset(struct vnic_dev *vdev)
1022 if (vdev->notify && !vnic_dev_in_reset(vdev)) {
1023 vdev->free_consistent(vdev->priv,
1024 sizeof(struct vnic_devcmd_notify),
1029 return vnic_dev_notify_unsetcmd(vdev);
1032 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
1035 unsigned int nwords = vdev->notify_sz / 4;
1039 if (!vdev->notify || !vdev->notify_sz)
1044 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
1045 words = (uint32_t *)&vdev->notify_copy;
1046 for (i = 1; i < nwords; i++)
1048 } while (csum != words[0]);
1053 int vnic_dev_init(struct vnic_dev *vdev, int arg)
1055 uint64_t a0 = (uint32_t)arg, a1 = 0;
1059 if (vnic_dev_capable(vdev, CMD_INIT))
1060 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
1062 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
1063 if (a0 & CMD_INITF_DEFAULT_MAC) {
1064 /* Emulate these for old CMD_INIT_v1 which
1065 * didn't pass a0 so no CMD_INITF_*.
1067 vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
1068 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
1074 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
1076 /* Default: hardware intr coal timer is in units of 1.5 usecs */
1077 vdev->intr_coal_timer_info.mul = 2;
1078 vdev->intr_coal_timer_info.div = 3;
1079 vdev->intr_coal_timer_info.max_usec =
1080 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
1083 int vnic_dev_link_status(struct vnic_dev *vdev)
1085 if (!vnic_dev_notify_ready(vdev))
1088 return vdev->notify_copy.link_state;
1091 uint32_t vnic_dev_port_speed(struct vnic_dev *vdev)
1093 if (!vnic_dev_notify_ready(vdev))
1096 return vdev->notify_copy.port_speed;
1099 uint32_t vnic_dev_mtu(struct vnic_dev *vdev)
1101 if (!vnic_dev_notify_ready(vdev))
1104 return vdev->notify_copy.mtu;
1107 uint32_t vnic_dev_uif(struct vnic_dev *vdev)
1109 if (!vnic_dev_notify_ready(vdev))
1112 return vdev->notify_copy.uif;
1115 uint32_t vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev,
1118 return (usec * vdev->intr_coal_timer_info.mul) /
1119 vdev->intr_coal_timer_info.div;
1122 uint32_t vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev,
1125 return (hw_cycles * vdev->intr_coal_timer_info.div) /
1126 vdev->intr_coal_timer_info.mul;
1129 uint32_t vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
1131 return vdev->intr_coal_timer_info.max_usec;
1134 int vnic_dev_alloc_stats_mem(struct vnic_dev *vdev)
1136 char name[RTE_MEMZONE_NAMESIZE];
1137 static uint32_t instance;
1139 snprintf((char *)name, sizeof(name), "vnic_stats-%u", instance++);
1140 vdev->stats = vdev->alloc_consistent(vdev->priv,
1141 sizeof(struct vnic_stats),
1142 &vdev->stats_pa, (uint8_t *)name);
1143 return vdev->stats == NULL ? -ENOMEM : 0;
1146 void vnic_dev_unregister(struct vnic_dev *vdev)
1150 vdev->free_consistent(vdev->priv,
1151 sizeof(struct vnic_devcmd_notify),
1155 vdev->free_consistent(vdev->priv,
1156 sizeof(struct vnic_stats),
1157 vdev->stats, vdev->stats_pa);
1158 if (vdev->flowman_info)
1159 vdev->free_consistent(vdev->priv,
1160 sizeof(struct fm_info),
1161 vdev->flowman_info, vdev->flowman_info_pa);
1163 vdev->free_consistent(vdev->priv,
1164 sizeof(struct vnic_devcmd_fw_info),
1165 vdev->fw_info, vdev->fw_info_pa);
1170 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
1171 void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar,
1172 unsigned int num_bars)
1175 char name[RTE_MEMZONE_NAMESIZE];
1176 snprintf((char *)name, sizeof(name), "%s-vnic",
1178 vdev = (struct vnic_dev *)rte_zmalloc_socket(name,
1179 sizeof(struct vnic_dev),
1180 RTE_CACHE_LINE_SIZE,
1181 pdev->device.numa_node);
1189 if (vnic_dev_discover_res(vdev, bar, num_bars))
1192 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
1199 vnic_dev_unregister(vdev);
1203 struct vnic_dev *vnic_vf_rep_register(void *priv, struct vnic_dev *pf_vdev,
1206 struct vnic_dev *vdev;
1208 vdev = (struct vnic_dev *)rte_zmalloc("enic-vf-rep-vdev",
1209 sizeof(struct vnic_dev), RTE_CACHE_LINE_SIZE);
1213 vdev->pf_vdev = pf_vdev;
1214 vdev->vf_id = vf_id;
1215 vdev->alloc_consistent = pf_vdev->alloc_consistent;
1216 vdev->free_consistent = pf_vdev->free_consistent;
1221 * vnic_dev_classifier: Add/Delete classifier entries
1222 * @vdev: vdev of the device
1223 * @cmd: CLSF_ADD for Add filter
1224 * CLSF_DEL for Delete filter
1225 * @entry: In case of ADD filter, the caller passes the RQ number in this
1227 * This function stores the filter_id returned by the
1228 * firmware in the same variable before return;
1230 * In case of DEL filter, the caller passes the RQ number. Return
1231 * value is irrelevant.
1232 * @data: filter data
1233 * @action: action data
1235 int vnic_dev_classifier(struct vnic_dev *vdev, uint8_t cmd, uint16_t *entry,
1236 struct filter_v2 *data, struct filter_action_v2 *action_v2)
1238 uint64_t a0 = 0, a1 = 0;
1242 struct filter_tlv *tlv, *tlv_va;
1244 uint32_t filter_size, action_size;
1245 static unsigned int unique_id;
1246 char z_name[RTE_MEMZONE_NAMESIZE];
1247 enum vnic_devcmd_cmd dev_cmd;
1249 if (cmd == CLSF_ADD) {
1250 dev_cmd = (data->type >= FILTER_DPDK_1) ?
1251 CMD_ADD_ADV_FILTER : CMD_ADD_FILTER;
1253 filter_size = vnic_filter_size(data);
1254 action_size = vnic_action_size(action_v2);
1256 tlv_size = filter_size + action_size +
1257 2*sizeof(struct filter_tlv);
1258 snprintf((char *)z_name, sizeof(z_name),
1259 "vnic_clsf_%u", unique_id++);
1260 tlv_va = vdev->alloc_consistent(vdev->priv,
1261 tlv_size, &tlv_pa, (uint8_t *)z_name);
1267 memset(tlv, 0, tlv_size);
1268 tlv->type = CLSF_TLV_FILTER;
1269 tlv->length = filter_size;
1270 memcpy(&tlv->val, (void *)data, filter_size);
1272 tlv = (struct filter_tlv *)((char *)tlv +
1273 sizeof(struct filter_tlv) +
1276 tlv->type = CLSF_TLV_ACTION;
1277 tlv->length = action_size;
1278 memcpy(&tlv->val, (void *)action_v2, action_size);
1279 ret = vnic_dev_cmd(vdev, dev_cmd, &a0, &a1, wait);
1280 *entry = (uint16_t)a0;
1281 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1282 } else if (cmd == CLSF_DEL) {
1284 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);
1290 int vnic_dev_overlay_offload_ctrl(struct vnic_dev *vdev, uint8_t overlay,
1293 uint64_t a0 = overlay;
1294 uint64_t a1 = config;
1297 return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CTRL, &a0, &a1, wait);
1300 int vnic_dev_overlay_offload_cfg(struct vnic_dev *vdev, uint8_t overlay,
1301 uint16_t vxlan_udp_port_number)
1303 uint64_t a1 = vxlan_udp_port_number;
1304 uint64_t a0 = overlay;
1307 return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CFG, &a0, &a1, wait);
1310 int vnic_dev_capable_vxlan(struct vnic_dev *vdev)
1312 uint64_t a0 = VIC_FEATURE_VXLAN;
1317 ret = vnic_dev_cmd(vdev, CMD_GET_SUPP_FEATURE_VER, &a0, &a1, wait);
1318 /* 1 if the NIC can do VXLAN for both IPv4 and IPv6 with multiple WQs */
1320 (a1 & (FEATURE_VXLAN_IPV6 | FEATURE_VXLAN_MULTI_WQ)) ==
1321 (FEATURE_VXLAN_IPV6 | FEATURE_VXLAN_MULTI_WQ);
1324 int vnic_dev_capable_geneve(struct vnic_dev *vdev)
1326 uint64_t a0 = VIC_FEATURE_GENEVE;
1331 ret = vnic_dev_cmd(vdev, CMD_GET_SUPP_FEATURE_VER, &a0, &a1, wait);
1332 return ret == 0 && !!(a1 & FEATURE_GENEVE_OPTIONS);
1335 uint64_t vnic_dev_capable_cq_entry_size(struct vnic_dev *vdev)
1337 uint64_t a0 = CMD_CQ_ENTRY_SIZE_SET;
1342 ret = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
1343 /* All models support 16B CQ entry by default */
1344 if (!(ret == 0 && a0 == 0))
1345 a1 = VNIC_RQ_CQ_ENTRY_SIZE_16_CAPABLE;
1349 int vnic_dev_set_cq_entry_size(struct vnic_dev *vdev, uint32_t rq_idx,
1352 uint64_t a0 = rq_idx;
1353 uint64_t a1 = size_flag;
1356 return vnic_dev_cmd(vdev, CMD_CQ_ENTRY_SIZE_SET, &a0, &a1, wait);