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>
11 #include "vnic_resource.h"
12 #include "vnic_devcmd.h"
14 #include "vnic_stats.h"
17 enum vnic_proxy_type {
29 struct vnic_intr_coal_timer_info {
37 struct rte_pci_device *pdev;
38 struct vnic_res res[RES_TYPE_MAX];
39 enum vnic_dev_intr_mode intr_mode;
40 struct vnic_devcmd __iomem *devcmd;
41 struct vnic_devcmd_notify *notify;
42 struct vnic_devcmd_notify notify_copy;
45 dma_addr_t linkstatus_pa;
46 struct vnic_stats *stats;
48 struct vnic_devcmd_fw_info *fw_info;
49 dma_addr_t fw_info_pa;
50 enum vnic_proxy_type proxy;
52 u64 args[VNIC_DEVCMD_NARGS];
54 struct vnic_intr_coal_timer_info intr_coal_timer_info;
55 void *(*alloc_consistent)(void *priv, size_t size,
56 dma_addr_t *dma_handle, u8 *name);
57 void (*free_consistent)(void *priv,
58 size_t size, void *vaddr,
59 dma_addr_t dma_handle);
60 struct vnic_counter_counts *flow_counters;
61 dma_addr_t flow_counters_pa;
64 #define VNIC_MAX_RES_HDR_SIZE \
65 (sizeof(struct vnic_resource_header) + \
66 sizeof(struct vnic_resource) * RES_TYPE_MAX)
67 #define VNIC_RES_STRIDE 128
69 #define VNIC_MAX_FLOW_COUNTERS 2048
71 void *vnic_dev_priv(struct vnic_dev *vdev)
76 void vnic_register_cbacks(struct vnic_dev *vdev,
77 void *(*alloc_consistent)(void *priv, size_t size,
78 dma_addr_t *dma_handle, u8 *name),
79 void (*free_consistent)(void *priv,
80 size_t size, void *vaddr,
81 dma_addr_t dma_handle))
83 vdev->alloc_consistent = alloc_consistent;
84 vdev->free_consistent = free_consistent;
87 static int vnic_dev_discover_res(struct vnic_dev *vdev,
88 struct vnic_dev_bar *bar, unsigned int num_bars)
90 struct vnic_resource_header __iomem *rh;
91 struct mgmt_barmap_hdr __iomem *mrh;
92 struct vnic_resource __iomem *r;
98 if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
99 pr_err("vNIC BAR0 res hdr length error\n");
106 pr_err("vNIC BAR0 res hdr not mem-mapped\n");
110 /* Check for mgmt vnic in addition to normal vnic */
111 if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
112 (ioread32(&rh->version) != VNIC_RES_VERSION)) {
113 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
114 (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
115 pr_err("vNIC BAR0 res magic/version error " \
116 "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
117 VNIC_RES_MAGIC, VNIC_RES_VERSION,
118 MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
119 ioread32(&rh->magic), ioread32(&rh->version));
124 if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
125 r = (struct vnic_resource __iomem *)(mrh + 1);
127 r = (struct vnic_resource __iomem *)(rh + 1);
130 while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
131 u8 bar_num = ioread8(&r->bar);
132 u32 bar_offset = ioread32(&r->bar_offset);
133 u32 count = ioread32(&r->count);
138 if (bar_num >= num_bars)
141 if (!bar[bar_num].len || !bar[bar_num].vaddr)
148 case RES_TYPE_INTR_CTRL:
149 /* each count is stride bytes long */
150 len = count * VNIC_RES_STRIDE;
151 if (len + bar_offset > bar[bar_num].len) {
152 pr_err("vNIC BAR0 resource %d " \
153 "out-of-bounds, offset 0x%x + " \
154 "size 0x%x > bar len 0x%lx\n",
161 case RES_TYPE_INTR_PBA_LEGACY:
162 case RES_TYPE_DEVCMD:
169 vdev->res[type].count = count;
170 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
172 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
178 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
179 enum vnic_res_type type)
181 return vdev->res[type].count;
184 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
187 if (!vdev->res[type].vaddr)
194 case RES_TYPE_INTR_CTRL:
195 return (char __iomem *)vdev->res[type].vaddr +
196 index * VNIC_RES_STRIDE;
198 return (char __iomem *)vdev->res[type].vaddr;
202 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
203 unsigned int desc_count, unsigned int desc_size)
205 /* The base address of the desc rings must be 512 byte aligned.
206 * Descriptor count is aligned to groups of 32 descriptors. A
207 * count of 0 means the maximum 4096 descriptors. Descriptor
208 * size is aligned to 16 bytes.
211 unsigned int count_align = 32;
212 unsigned int desc_align = 16;
214 ring->base_align = 512;
219 ring->desc_count = VNIC_ALIGN(desc_count, count_align);
221 ring->desc_size = VNIC_ALIGN(desc_size, desc_align);
223 ring->size = ring->desc_count * ring->desc_size;
224 ring->size_unaligned = ring->size + ring->base_align;
226 return ring->size_unaligned;
229 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
231 memset(ring->descs, 0, ring->size);
234 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev,
235 struct vnic_dev_ring *ring,
236 unsigned int desc_count, unsigned int desc_size,
237 __attribute__((unused)) unsigned int socket_id,
241 dma_addr_t alloc_pa = 0;
243 vnic_dev_desc_ring_size(ring, desc_count, desc_size);
244 alloc_addr = vdev->alloc_consistent(vdev->priv,
245 ring->size_unaligned,
246 &alloc_pa, (u8 *)z_name);
248 pr_err("Failed to allocate ring (size=%d), aborting\n",
252 ring->descs_unaligned = alloc_addr;
254 pr_err("Failed to map allocated ring (size=%d), aborting\n",
256 vdev->free_consistent(vdev->priv,
257 ring->size_unaligned,
262 ring->base_addr_unaligned = alloc_pa;
264 ring->base_addr = VNIC_ALIGN(ring->base_addr_unaligned,
266 ring->descs = (u8 *)ring->descs_unaligned +
267 (ring->base_addr - ring->base_addr_unaligned);
269 vnic_dev_clear_desc_ring(ring);
271 ring->desc_avail = ring->desc_count - 1;
276 void vnic_dev_free_desc_ring(__attribute__((unused)) struct vnic_dev *vdev,
277 struct vnic_dev_ring *ring)
280 vdev->free_consistent(vdev->priv,
281 ring->size_unaligned,
282 ring->descs_unaligned,
283 ring->base_addr_unaligned);
288 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
291 struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
297 status = ioread32(&devcmd->status);
298 if (status == 0xFFFFFFFF) {
299 /* PCI-e target device is gone */
302 if (status & STAT_BUSY) {
304 pr_err("Busy devcmd %d\n", _CMD_N(cmd));
308 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
309 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
310 writeq(vdev->args[i], &devcmd->args[i]);
311 wmb(); /* complete all writes initiated till now */
314 iowrite32(cmd, &devcmd->cmd);
316 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
319 for (delay = 0; delay < wait; delay++) {
323 status = ioread32(&devcmd->status);
324 if (status == 0xFFFFFFFF) {
325 /* PCI-e target device is gone */
329 if (!(status & STAT_BUSY)) {
330 if (status & STAT_ERROR) {
331 err = -(int)readq(&devcmd->args[0]);
332 if (cmd != CMD_CAPABILITY)
333 pr_err("Devcmd %d failed " \
334 "with error code %d\n",
339 if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
340 rmb();/* finish all reads initiated till now */
341 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
342 vdev->args[i] = readq(&devcmd->args[i]);
349 pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
353 static int vnic_dev_cmd_proxy(struct vnic_dev *vdev,
354 enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd,
355 u64 *args, int nargs, int wait)
361 * Proxy command consumes 2 arguments. One for proxy index,
362 * the other is for command to be proxied
364 if (nargs > VNIC_DEVCMD_NARGS - 2) {
365 pr_err("number of args %d exceeds the maximum\n", nargs);
368 memset(vdev->args, 0, sizeof(vdev->args));
370 vdev->args[0] = vdev->proxy_index;
372 memcpy(&vdev->args[2], args, nargs * sizeof(args[0]));
374 err = _vnic_dev_cmd(vdev, proxy_cmd, wait);
378 status = (u32)vdev->args[0];
379 if (status & STAT_ERROR) {
380 err = (int)vdev->args[1];
381 if (err != ERR_ECMDUNKNOWN ||
382 cmd != CMD_CAPABILITY)
383 pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
387 memcpy(args, &vdev->args[1], nargs * sizeof(args[0]));
392 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
393 enum vnic_devcmd_cmd cmd, u64 *args, int nargs, int wait)
397 if (nargs > VNIC_DEVCMD_NARGS) {
398 pr_err("number of args %d exceeds the maximum\n", nargs);
401 memset(vdev->args, 0, sizeof(vdev->args));
402 memcpy(vdev->args, args, nargs * sizeof(args[0]));
404 err = _vnic_dev_cmd(vdev, cmd, wait);
406 memcpy(args, vdev->args, nargs * sizeof(args[0]));
411 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
412 u64 *a0, u64 *a1, int wait)
419 memset(vdev->args, 0, sizeof(vdev->args));
421 switch (vdev->proxy) {
423 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
424 args, ARRAY_SIZE(args), wait);
427 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
428 args, ARRAY_SIZE(args), wait);
432 err = vnic_dev_cmd_no_proxy(vdev, cmd, args, 2, wait);
444 int vnic_dev_cmd_args(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
445 u64 *args, int nargs, int wait)
447 switch (vdev->proxy) {
449 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
452 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
456 return vnic_dev_cmd_no_proxy(vdev, cmd, args, nargs, wait);
460 static int vnic_dev_advanced_filters_cap(struct vnic_dev *vdev, u64 *args,
463 memset(args, 0, nargs * sizeof(*args));
464 args[0] = CMD_ADD_ADV_FILTER;
465 args[1] = FILTER_CAP_MODE_V1_FLAG;
466 return vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, nargs, 1000);
469 int vnic_dev_capable_adv_filters(struct vnic_dev *vdev)
471 u64 a0 = CMD_ADD_ADV_FILTER, a1 = 0;
475 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
478 return (a1 >= (u32)FILTER_DPDK_1);
481 /* Determine the "best" filtering mode VIC is capaible of. Returns one of 3
482 * value or 0 on error:
483 * FILTER_DPDK_1- advanced filters availabile
484 * FILTER_USNIC_IP_FLAG - advanced filters but with the restriction that
485 * the IP layer must explicitly specified. I.e. cannot have a UDP
486 * filter that matches both IPv4 and IPv6.
487 * FILTER_IPV4_5TUPLE - fallback if either of the 2 above aren't available.
488 * all other filter types are not available.
489 * Retrun true in filter_tags if supported
491 int vnic_dev_capable_filter_mode(struct vnic_dev *vdev, u32 *mode,
498 err = vnic_dev_advanced_filters_cap(vdev, args, 4);
500 /* determine supported filter actions */
501 *filter_actions = FILTER_ACTION_RQ_STEERING_FLAG; /* always available */
502 if (args[2] == FILTER_CAP_MODE_V1)
503 *filter_actions = args[3];
505 if (err || ((args[0] == 1) && (args[1] == 0))) {
506 /* Adv filter Command not supported or adv filters available but
507 * not enabled. Try the normal filter capability command.
509 args[0] = CMD_ADD_FILTER;
511 err = vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, 2, 1000);
515 goto parse_max_level;
516 } else if (args[2] == FILTER_CAP_MODE_V1) {
517 /* parse filter capability mask in args[1] */
518 if (args[1] & FILTER_DPDK_1_FLAG)
519 *mode = FILTER_DPDK_1;
520 else if (args[1] & FILTER_USNIC_IP_FLAG)
521 *mode = FILTER_USNIC_IP;
522 else if (args[1] & FILTER_IPV4_5TUPLE_FLAG)
523 *mode = FILTER_IPV4_5TUPLE;
528 if (max_level >= (u32)FILTER_USNIC_IP)
529 *mode = FILTER_USNIC_IP;
531 *mode = FILTER_IPV4_5TUPLE;
535 void vnic_dev_capable_udp_rss_weak(struct vnic_dev *vdev, bool *cfg_chk,
538 u64 a0 = CMD_NIC_CFG, a1 = 0;
544 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
545 if (err == 0 && a0 != 0 && a1 != 0) {
547 *weak = !!((a1 >> 32) & CMD_NIC_CFG_CAPF_UDP_WEAK);
551 int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
553 u64 a0 = (u32)cmd, a1 = 0;
557 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
562 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size,
572 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
576 *(u8 *)value = (u8)a0;
579 *(u16 *)value = (u16)a0;
582 *(u32 *)value = (u32)a0;
595 int vnic_dev_stats_clear(struct vnic_dev *vdev)
600 return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
603 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
611 *stats = vdev->stats;
613 a1 = sizeof(struct vnic_stats);
615 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
619 * Configure counter DMA
621 int vnic_dev_counter_dma_cfg(struct vnic_dev *vdev, u32 period, u32 counter_idx)
626 if (!vdev->flow_counters || counter_idx >= VNIC_MAX_FLOW_COUNTERS)
629 args[0] = counter_idx + 1;
630 args[1] = vdev->flow_counters_pa;
632 return vnic_dev_cmd_args(vdev, CMD_COUNTER_DMA_CONFIG, args, 3, wait);
635 int vnic_dev_close(struct vnic_dev *vdev)
640 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
643 int vnic_dev_enable_wait(struct vnic_dev *vdev)
648 if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
649 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
651 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
654 int vnic_dev_disable(struct vnic_dev *vdev)
659 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
662 int vnic_dev_open(struct vnic_dev *vdev, int arg)
664 u64 a0 = (u32)arg, a1 = 0;
667 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
670 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
678 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
687 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
693 for (i = 0; i < ETH_ALEN; i++)
696 err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
700 for (i = 0; i < ETH_ALEN; i++)
701 mac_addr[i] = ((u8 *)&a0)[i];
706 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
707 int broadcast, int promisc, int allmulti)
713 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
714 (multicast ? CMD_PFILTER_MULTICAST : 0) |
715 (broadcast ? CMD_PFILTER_BROADCAST : 0) |
716 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
717 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
719 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
721 pr_err("Can't set packet filter\n");
726 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
733 for (i = 0; i < ETH_ALEN; i++)
734 ((u8 *)&a0)[i] = addr[i];
736 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
738 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
739 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
745 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
752 for (i = 0; i < ETH_ALEN; i++)
753 ((u8 *)&a0)[i] = addr[i];
755 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
757 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
758 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
764 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
765 u8 ig_vlan_rewrite_mode)
767 u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
770 if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
771 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
777 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
779 vdev->in_reset = state;
782 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
784 return vdev->in_reset;
787 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
788 void *notify_addr, dma_addr_t notify_pa, u16 intr)
794 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
795 if (!vnic_dev_in_reset(vdev)) {
796 vdev->notify = notify_addr;
797 vdev->notify_pa = notify_pa;
801 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
802 a1 += sizeof(struct vnic_devcmd_notify);
804 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
805 if (!vnic_dev_in_reset(vdev))
806 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
811 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
813 void *notify_addr = NULL;
814 dma_addr_t notify_pa = 0;
818 if (vdev->notify || vdev->notify_pa) {
819 return vnic_dev_notify_setcmd(vdev, vdev->notify,
820 vdev->notify_pa, intr);
822 if (!vnic_dev_in_reset(vdev)) {
823 snprintf((char *)name, sizeof(name),
824 "vnic_notify-%u", instance++);
825 notify_addr = vdev->alloc_consistent(vdev->priv,
826 sizeof(struct vnic_devcmd_notify),
827 ¬ify_pa, (u8 *)name);
832 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
835 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
841 a0 = 0; /* paddr = 0 to unset notify buffer */
842 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
843 a1 += sizeof(struct vnic_devcmd_notify);
845 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
846 if (!vnic_dev_in_reset(vdev)) {
855 int vnic_dev_notify_unset(struct vnic_dev *vdev)
857 if (vdev->notify && !vnic_dev_in_reset(vdev)) {
858 vdev->free_consistent(vdev->priv,
859 sizeof(struct vnic_devcmd_notify),
864 return vnic_dev_notify_unsetcmd(vdev);
867 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
870 unsigned int nwords = vdev->notify_sz / 4;
874 if (!vdev->notify || !vdev->notify_sz)
879 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
880 words = (u32 *)&vdev->notify_copy;
881 for (i = 1; i < nwords; i++)
883 } while (csum != words[0]);
888 int vnic_dev_init(struct vnic_dev *vdev, int arg)
890 u64 a0 = (u32)arg, a1 = 0;
894 if (vnic_dev_capable(vdev, CMD_INIT))
895 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
897 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
898 if (a0 & CMD_INITF_DEFAULT_MAC) {
899 /* Emulate these for old CMD_INIT_v1 which
900 * didn't pass a0 so no CMD_INITF_*.
902 vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
903 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
909 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
911 /* Default: hardware intr coal timer is in units of 1.5 usecs */
912 vdev->intr_coal_timer_info.mul = 2;
913 vdev->intr_coal_timer_info.div = 3;
914 vdev->intr_coal_timer_info.max_usec =
915 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
918 int vnic_dev_link_status(struct vnic_dev *vdev)
920 if (!vnic_dev_notify_ready(vdev))
923 return vdev->notify_copy.link_state;
926 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
928 if (!vnic_dev_notify_ready(vdev))
931 return vdev->notify_copy.port_speed;
934 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
936 return (usec * vdev->intr_coal_timer_info.mul) /
937 vdev->intr_coal_timer_info.div;
940 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
942 return (hw_cycles * vdev->intr_coal_timer_info.div) /
943 vdev->intr_coal_timer_info.mul;
946 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
948 return vdev->intr_coal_timer_info.max_usec;
951 int vnic_dev_alloc_stats_mem(struct vnic_dev *vdev)
956 snprintf((char *)name, sizeof(name), "vnic_stats-%u", instance++);
957 vdev->stats = vdev->alloc_consistent(vdev->priv,
958 sizeof(struct vnic_stats),
959 &vdev->stats_pa, (u8 *)name);
960 return vdev->stats == NULL ? -ENOMEM : 0;
964 * Initialize for up to VNIC_MAX_FLOW_COUNTERS
966 int vnic_dev_alloc_counter_mem(struct vnic_dev *vdev)
971 snprintf((char *)name, sizeof(name), "vnic_flow_ctrs-%u", instance++);
972 vdev->flow_counters = vdev->alloc_consistent(vdev->priv,
973 sizeof(struct vnic_counter_counts)
974 * VNIC_MAX_FLOW_COUNTERS,
975 &vdev->flow_counters_pa,
977 return vdev->flow_counters == NULL ? -ENOMEM : 0;
980 void vnic_dev_unregister(struct vnic_dev *vdev)
984 vdev->free_consistent(vdev->priv,
985 sizeof(struct vnic_devcmd_notify),
989 vdev->free_consistent(vdev->priv,
990 sizeof(struct vnic_stats),
991 vdev->stats, vdev->stats_pa);
992 if (vdev->flow_counters) {
993 /* turn off counter DMAs before freeing memory */
994 vnic_dev_counter_dma_cfg(vdev, 0, 0);
996 vdev->free_consistent(vdev->priv,
997 sizeof(struct vnic_counter_counts)
998 * VNIC_MAX_FLOW_COUNTERS,
999 vdev->flow_counters, vdev->flow_counters_pa);
1002 vdev->free_consistent(vdev->priv,
1003 sizeof(struct vnic_devcmd_fw_info),
1004 vdev->fw_info, vdev->fw_info_pa);
1009 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
1010 void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar,
1011 unsigned int num_bars)
1014 char name[NAME_MAX];
1015 snprintf((char *)name, sizeof(name), "%s-vnic",
1017 vdev = (struct vnic_dev *)rte_zmalloc_socket(name,
1018 sizeof(struct vnic_dev),
1019 RTE_CACHE_LINE_SIZE,
1020 pdev->device.numa_node);
1028 if (vnic_dev_discover_res(vdev, bar, num_bars))
1031 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
1038 vnic_dev_unregister(vdev);
1043 * vnic_dev_classifier: Add/Delete classifier entries
1044 * @vdev: vdev of the device
1045 * @cmd: CLSF_ADD for Add filter
1046 * CLSF_DEL for Delete filter
1047 * @entry: In case of ADD filter, the caller passes the RQ number in this
1049 * This function stores the filter_id returned by the
1050 * firmware in the same variable before return;
1052 * In case of DEL filter, the caller passes the RQ number. Return
1053 * value is irrelevant.
1054 * @data: filter data
1055 * @action: action data
1057 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1058 struct filter_v2 *data, struct filter_action_v2 *action_v2)
1064 struct filter_tlv *tlv, *tlv_va;
1066 u32 filter_size, action_size;
1067 static unsigned int unique_id;
1068 char z_name[RTE_MEMZONE_NAMESIZE];
1069 enum vnic_devcmd_cmd dev_cmd;
1071 if (cmd == CLSF_ADD) {
1072 dev_cmd = (data->type >= FILTER_DPDK_1) ?
1073 CMD_ADD_ADV_FILTER : CMD_ADD_FILTER;
1075 filter_size = vnic_filter_size(data);
1076 action_size = vnic_action_size(action_v2);
1078 tlv_size = filter_size + action_size +
1079 2*sizeof(struct filter_tlv);
1080 snprintf((char *)z_name, sizeof(z_name),
1081 "vnic_clsf_%u", unique_id++);
1082 tlv_va = vdev->alloc_consistent(vdev->priv,
1083 tlv_size, &tlv_pa, (u8 *)z_name);
1089 memset(tlv, 0, tlv_size);
1090 tlv->type = CLSF_TLV_FILTER;
1091 tlv->length = filter_size;
1092 memcpy(&tlv->val, (void *)data, filter_size);
1094 tlv = (struct filter_tlv *)((char *)tlv +
1095 sizeof(struct filter_tlv) +
1098 tlv->type = CLSF_TLV_ACTION;
1099 tlv->length = action_size;
1100 memcpy(&tlv->val, (void *)action_v2, action_size);
1101 ret = vnic_dev_cmd(vdev, dev_cmd, &a0, &a1, wait);
1103 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1104 } else if (cmd == CLSF_DEL) {
1106 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);
1112 int vnic_dev_overlay_offload_ctrl(struct vnic_dev *vdev, u8 overlay, u8 config)
1118 return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CTRL, &a0, &a1, wait);
1121 int vnic_dev_overlay_offload_cfg(struct vnic_dev *vdev, u8 overlay,
1122 u16 vxlan_udp_port_number)
1124 u64 a1 = vxlan_udp_port_number;
1128 return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CFG, &a0, &a1, wait);
1131 int vnic_dev_capable_vxlan(struct vnic_dev *vdev)
1133 u64 a0 = VIC_FEATURE_VXLAN;
1138 ret = vnic_dev_cmd(vdev, CMD_GET_SUPP_FEATURE_VER, &a0, &a1, wait);
1139 /* 1 if the NIC can do VXLAN for both IPv4 and IPv6 with multiple WQs */
1141 (a1 & (FEATURE_VXLAN_IPV6 | FEATURE_VXLAN_MULTI_WQ)) ==
1142 (FEATURE_VXLAN_IPV6 | FEATURE_VXLAN_MULTI_WQ);
1145 bool vnic_dev_counter_alloc(struct vnic_dev *vdev, uint32_t *idx)
1151 if (vnic_dev_cmd(vdev, CMD_COUNTER_ALLOC, &a0, &a1, wait))
1153 *idx = (uint32_t)a0;
1157 bool vnic_dev_counter_free(struct vnic_dev *vdev, uint32_t idx)
1163 return vnic_dev_cmd(vdev, CMD_COUNTER_FREE, &a0, &a1,
1167 bool vnic_dev_counter_query(struct vnic_dev *vdev, uint32_t idx,
1168 bool reset, uint64_t *packets, uint64_t *bytes)
1171 u64 a1 = reset ? 1 : 0;
1174 if (vdev->flow_counters) {
1175 /* Using counter DMA API, so counters avail in host memory */
1176 *packets = vdev->flow_counters[idx].vcc_packets;
1177 *bytes = vdev->flow_counters[idx].vcc_bytes;
1179 if (vnic_dev_cmd(vdev, CMD_COUNTER_QUERY, &a0, &a1,
1183 if (vnic_dev_cmd(vdev, CMD_COUNTER_QUERY, &a0, &a1, wait))