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);
62 #define VNIC_MAX_RES_HDR_SIZE \
63 (sizeof(struct vnic_resource_header) + \
64 sizeof(struct vnic_resource) * RES_TYPE_MAX)
65 #define VNIC_RES_STRIDE 128
67 void *vnic_dev_priv(struct vnic_dev *vdev)
72 void vnic_register_cbacks(struct vnic_dev *vdev,
73 void *(*alloc_consistent)(void *priv, size_t size,
74 dma_addr_t *dma_handle, u8 *name),
75 void (*free_consistent)(void *priv,
76 size_t size, void *vaddr,
77 dma_addr_t dma_handle))
79 vdev->alloc_consistent = alloc_consistent;
80 vdev->free_consistent = free_consistent;
83 static int vnic_dev_discover_res(struct vnic_dev *vdev,
84 struct vnic_dev_bar *bar, unsigned int num_bars)
86 struct vnic_resource_header __iomem *rh;
87 struct mgmt_barmap_hdr __iomem *mrh;
88 struct vnic_resource __iomem *r;
94 if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
95 pr_err("vNIC BAR0 res hdr length error\n");
102 pr_err("vNIC BAR0 res hdr not mem-mapped\n");
106 /* Check for mgmt vnic in addition to normal vnic */
107 if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
108 (ioread32(&rh->version) != VNIC_RES_VERSION)) {
109 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
110 (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
111 pr_err("vNIC BAR0 res magic/version error " \
112 "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
113 VNIC_RES_MAGIC, VNIC_RES_VERSION,
114 MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
115 ioread32(&rh->magic), ioread32(&rh->version));
120 if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
121 r = (struct vnic_resource __iomem *)(mrh + 1);
123 r = (struct vnic_resource __iomem *)(rh + 1);
126 while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
127 u8 bar_num = ioread8(&r->bar);
128 u32 bar_offset = ioread32(&r->bar_offset);
129 u32 count = ioread32(&r->count);
134 if (bar_num >= num_bars)
137 if (!bar[bar_num].len || !bar[bar_num].vaddr)
144 case RES_TYPE_INTR_CTRL:
145 /* each count is stride bytes long */
146 len = count * VNIC_RES_STRIDE;
147 if (len + bar_offset > bar[bar_num].len) {
148 pr_err("vNIC BAR0 resource %d " \
149 "out-of-bounds, offset 0x%x + " \
150 "size 0x%x > bar len 0x%lx\n",
157 case RES_TYPE_INTR_PBA_LEGACY:
158 case RES_TYPE_DEVCMD:
165 vdev->res[type].count = count;
166 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
168 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
174 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
175 enum vnic_res_type type)
177 return vdev->res[type].count;
180 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
183 if (!vdev->res[type].vaddr)
190 case RES_TYPE_INTR_CTRL:
191 return (char __iomem *)vdev->res[type].vaddr +
192 index * VNIC_RES_STRIDE;
194 return (char __iomem *)vdev->res[type].vaddr;
198 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
199 unsigned int desc_count, unsigned int desc_size)
201 /* The base address of the desc rings must be 512 byte aligned.
202 * Descriptor count is aligned to groups of 32 descriptors. A
203 * count of 0 means the maximum 4096 descriptors. Descriptor
204 * size is aligned to 16 bytes.
207 unsigned int count_align = 32;
208 unsigned int desc_align = 16;
210 ring->base_align = 512;
215 ring->desc_count = VNIC_ALIGN(desc_count, count_align);
217 ring->desc_size = VNIC_ALIGN(desc_size, desc_align);
219 ring->size = ring->desc_count * ring->desc_size;
220 ring->size_unaligned = ring->size + ring->base_align;
222 return ring->size_unaligned;
225 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
227 memset(ring->descs, 0, ring->size);
230 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev,
231 struct vnic_dev_ring *ring,
232 unsigned int desc_count, unsigned int desc_size,
233 __attribute__((unused)) unsigned int socket_id,
237 dma_addr_t alloc_pa = 0;
239 vnic_dev_desc_ring_size(ring, desc_count, desc_size);
240 alloc_addr = vdev->alloc_consistent(vdev->priv,
241 ring->size_unaligned,
242 &alloc_pa, (u8 *)z_name);
244 pr_err("Failed to allocate ring (size=%d), aborting\n",
248 ring->descs_unaligned = alloc_addr;
250 pr_err("Failed to map allocated ring (size=%d), aborting\n",
252 vdev->free_consistent(vdev->priv,
253 ring->size_unaligned,
258 ring->base_addr_unaligned = alloc_pa;
260 ring->base_addr = VNIC_ALIGN(ring->base_addr_unaligned,
262 ring->descs = (u8 *)ring->descs_unaligned +
263 (ring->base_addr - ring->base_addr_unaligned);
265 vnic_dev_clear_desc_ring(ring);
267 ring->desc_avail = ring->desc_count - 1;
272 void vnic_dev_free_desc_ring(__attribute__((unused)) struct vnic_dev *vdev,
273 struct vnic_dev_ring *ring)
276 vdev->free_consistent(vdev->priv,
277 ring->size_unaligned,
278 ring->descs_unaligned,
279 ring->base_addr_unaligned);
284 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
287 struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
293 status = ioread32(&devcmd->status);
294 if (status == 0xFFFFFFFF) {
295 /* PCI-e target device is gone */
298 if (status & STAT_BUSY) {
300 pr_err("Busy devcmd %d\n", _CMD_N(cmd));
304 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
305 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
306 writeq(vdev->args[i], &devcmd->args[i]);
307 wmb(); /* complete all writes initiated till now */
310 iowrite32(cmd, &devcmd->cmd);
312 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
315 for (delay = 0; delay < wait; delay++) {
319 status = ioread32(&devcmd->status);
320 if (status == 0xFFFFFFFF) {
321 /* PCI-e target device is gone */
325 if (!(status & STAT_BUSY)) {
326 if (status & STAT_ERROR) {
327 err = -(int)readq(&devcmd->args[0]);
328 if (cmd != CMD_CAPABILITY)
329 pr_err("Devcmd %d failed " \
330 "with error code %d\n",
335 if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
336 rmb();/* finish all reads initiated till now */
337 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
338 vdev->args[i] = readq(&devcmd->args[i]);
345 pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
349 static int vnic_dev_cmd_proxy(struct vnic_dev *vdev,
350 enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd,
351 u64 *args, int nargs, int wait)
357 * Proxy command consumes 2 arguments. One for proxy index,
358 * the other is for command to be proxied
360 if (nargs > VNIC_DEVCMD_NARGS - 2) {
361 pr_err("number of args %d exceeds the maximum\n", nargs);
364 memset(vdev->args, 0, sizeof(vdev->args));
366 vdev->args[0] = vdev->proxy_index;
368 memcpy(&vdev->args[2], args, nargs * sizeof(args[0]));
370 err = _vnic_dev_cmd(vdev, proxy_cmd, wait);
374 status = (u32)vdev->args[0];
375 if (status & STAT_ERROR) {
376 err = (int)vdev->args[1];
377 if (err != ERR_ECMDUNKNOWN ||
378 cmd != CMD_CAPABILITY)
379 pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
383 memcpy(args, &vdev->args[1], nargs * sizeof(args[0]));
388 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
389 enum vnic_devcmd_cmd cmd, u64 *args, int nargs, int wait)
393 if (nargs > VNIC_DEVCMD_NARGS) {
394 pr_err("number of args %d exceeds the maximum\n", nargs);
397 memset(vdev->args, 0, sizeof(vdev->args));
398 memcpy(vdev->args, args, nargs * sizeof(args[0]));
400 err = _vnic_dev_cmd(vdev, cmd, wait);
402 memcpy(args, vdev->args, nargs * sizeof(args[0]));
407 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
408 u64 *a0, u64 *a1, int wait)
415 memset(vdev->args, 0, sizeof(vdev->args));
417 switch (vdev->proxy) {
419 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
420 args, ARRAY_SIZE(args), wait);
423 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
424 args, ARRAY_SIZE(args), wait);
428 err = vnic_dev_cmd_no_proxy(vdev, cmd, args, 2, wait);
440 int vnic_dev_cmd_args(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
441 u64 *args, int nargs, int wait)
443 switch (vdev->proxy) {
445 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
448 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
452 return vnic_dev_cmd_no_proxy(vdev, cmd, args, nargs, wait);
456 static int vnic_dev_advanced_filters_cap(struct vnic_dev *vdev, u64 *args,
459 memset(args, 0, nargs * sizeof(*args));
460 args[0] = CMD_ADD_ADV_FILTER;
461 args[1] = FILTER_CAP_MODE_V1_FLAG;
462 return vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, nargs, 1000);
465 int vnic_dev_capable_adv_filters(struct vnic_dev *vdev)
467 u64 a0 = CMD_ADD_ADV_FILTER, a1 = 0;
471 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
474 return (a1 >= (u32)FILTER_DPDK_1);
477 /* Determine the "best" filtering mode VIC is capaible of. Returns one of 3
478 * value or 0 on error:
479 * FILTER_DPDK_1- advanced filters availabile
480 * FILTER_USNIC_IP_FLAG - advanced filters but with the restriction that
481 * the IP layer must explicitly specified. I.e. cannot have a UDP
482 * filter that matches both IPv4 and IPv6.
483 * FILTER_IPV4_5TUPLE - fallback if either of the 2 above aren't available.
484 * all other filter types are not available.
485 * Retrun true in filter_tags if supported
487 int vnic_dev_capable_filter_mode(struct vnic_dev *vdev, u32 *mode,
494 err = vnic_dev_advanced_filters_cap(vdev, args, 4);
496 /* determine if filter tags are available */
499 if ((args[2] == FILTER_CAP_MODE_V1) &&
500 (args[3] & FILTER_ACTION_FILTER_ID_FLAG))
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 int vnic_dev_capable_udp_rss(struct vnic_dev *vdev)
537 u64 a0 = CMD_NIC_CFG, a1 = 0;
542 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
547 rss_hash_type = (a1 >> NIC_CFG_RSS_HASH_TYPE_SHIFT) &
548 NIC_CFG_RSS_HASH_TYPE_MASK_FIELD;
549 return ((rss_hash_type & NIC_CFG_RSS_HASH_TYPE_UDP) ? 1 : 0);
552 int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
554 u64 a0 = (u32)cmd, a1 = 0;
558 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
563 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size,
573 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
577 *(u8 *)value = (u8)a0;
580 *(u16 *)value = (u16)a0;
583 *(u32 *)value = (u32)a0;
596 int vnic_dev_stats_clear(struct vnic_dev *vdev)
601 return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
604 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
612 *stats = vdev->stats;
614 a1 = sizeof(struct vnic_stats);
616 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
619 int vnic_dev_close(struct vnic_dev *vdev)
624 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
627 int vnic_dev_enable_wait(struct vnic_dev *vdev)
632 if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
633 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
635 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
638 int vnic_dev_disable(struct vnic_dev *vdev)
643 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
646 int vnic_dev_open(struct vnic_dev *vdev, int arg)
648 u64 a0 = (u32)arg, a1 = 0;
651 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
654 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
662 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
671 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
677 for (i = 0; i < ETH_ALEN; i++)
680 err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
684 for (i = 0; i < ETH_ALEN; i++)
685 mac_addr[i] = ((u8 *)&a0)[i];
690 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
691 int broadcast, int promisc, int allmulti)
697 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
698 (multicast ? CMD_PFILTER_MULTICAST : 0) |
699 (broadcast ? CMD_PFILTER_BROADCAST : 0) |
700 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
701 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
703 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
705 pr_err("Can't set packet filter\n");
710 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
717 for (i = 0; i < ETH_ALEN; i++)
718 ((u8 *)&a0)[i] = addr[i];
720 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
722 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
723 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
729 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
736 for (i = 0; i < ETH_ALEN; i++)
737 ((u8 *)&a0)[i] = addr[i];
739 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
741 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
742 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
748 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
749 u8 ig_vlan_rewrite_mode)
751 u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
754 if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
755 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
761 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
763 vdev->in_reset = state;
766 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
768 return vdev->in_reset;
771 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
772 void *notify_addr, dma_addr_t notify_pa, u16 intr)
778 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
779 if (!vnic_dev_in_reset(vdev)) {
780 vdev->notify = notify_addr;
781 vdev->notify_pa = notify_pa;
785 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
786 a1 += sizeof(struct vnic_devcmd_notify);
788 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
789 if (!vnic_dev_in_reset(vdev))
790 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
795 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
797 void *notify_addr = NULL;
798 dma_addr_t notify_pa = 0;
802 if (vdev->notify || vdev->notify_pa) {
803 return vnic_dev_notify_setcmd(vdev, vdev->notify,
804 vdev->notify_pa, intr);
806 if (!vnic_dev_in_reset(vdev)) {
807 snprintf((char *)name, sizeof(name),
808 "vnic_notify-%u", instance++);
809 notify_addr = vdev->alloc_consistent(vdev->priv,
810 sizeof(struct vnic_devcmd_notify),
811 ¬ify_pa, (u8 *)name);
816 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
819 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
825 a0 = 0; /* paddr = 0 to unset notify buffer */
826 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
827 a1 += sizeof(struct vnic_devcmd_notify);
829 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
830 if (!vnic_dev_in_reset(vdev)) {
839 int vnic_dev_notify_unset(struct vnic_dev *vdev)
841 if (vdev->notify && !vnic_dev_in_reset(vdev)) {
842 vdev->free_consistent(vdev->priv,
843 sizeof(struct vnic_devcmd_notify),
848 return vnic_dev_notify_unsetcmd(vdev);
851 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
854 unsigned int nwords = vdev->notify_sz / 4;
858 if (!vdev->notify || !vdev->notify_sz)
863 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
864 words = (u32 *)&vdev->notify_copy;
865 for (i = 1; i < nwords; i++)
867 } while (csum != words[0]);
872 int vnic_dev_init(struct vnic_dev *vdev, int arg)
874 u64 a0 = (u32)arg, a1 = 0;
878 if (vnic_dev_capable(vdev, CMD_INIT))
879 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
881 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
882 if (a0 & CMD_INITF_DEFAULT_MAC) {
883 /* Emulate these for old CMD_INIT_v1 which
884 * didn't pass a0 so no CMD_INITF_*.
886 vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
887 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
893 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
895 /* Default: hardware intr coal timer is in units of 1.5 usecs */
896 vdev->intr_coal_timer_info.mul = 2;
897 vdev->intr_coal_timer_info.div = 3;
898 vdev->intr_coal_timer_info.max_usec =
899 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
902 int vnic_dev_link_status(struct vnic_dev *vdev)
904 if (!vnic_dev_notify_ready(vdev))
907 return vdev->notify_copy.link_state;
910 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
912 if (!vnic_dev_notify_ready(vdev))
915 return vdev->notify_copy.port_speed;
918 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
920 return (usec * vdev->intr_coal_timer_info.mul) /
921 vdev->intr_coal_timer_info.div;
924 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
926 return (hw_cycles * vdev->intr_coal_timer_info.div) /
927 vdev->intr_coal_timer_info.mul;
930 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
932 return vdev->intr_coal_timer_info.max_usec;
935 int vnic_dev_alloc_stats_mem(struct vnic_dev *vdev)
940 snprintf((char *)name, sizeof(name), "vnic_stats-%u", instance++);
941 vdev->stats = vdev->alloc_consistent(vdev->priv,
942 sizeof(struct vnic_stats),
943 &vdev->stats_pa, (u8 *)name);
944 return vdev->stats == NULL ? -ENOMEM : 0;
947 void vnic_dev_unregister(struct vnic_dev *vdev)
951 vdev->free_consistent(vdev->priv,
952 sizeof(struct vnic_devcmd_notify),
956 vdev->free_consistent(vdev->priv,
957 sizeof(struct vnic_stats),
958 vdev->stats, vdev->stats_pa);
960 vdev->free_consistent(vdev->priv,
961 sizeof(struct vnic_devcmd_fw_info),
962 vdev->fw_info, vdev->fw_info_pa);
967 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
968 void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar,
969 unsigned int num_bars)
973 snprintf((char *)name, sizeof(name), "%s-vnic",
975 vdev = (struct vnic_dev *)rte_zmalloc_socket(name,
976 sizeof(struct vnic_dev),
978 pdev->device.numa_node);
986 if (vnic_dev_discover_res(vdev, bar, num_bars))
989 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
996 vnic_dev_unregister(vdev);
1001 * vnic_dev_classifier: Add/Delete classifier entries
1002 * @vdev: vdev of the device
1003 * @cmd: CLSF_ADD for Add filter
1004 * CLSF_DEL for Delete filter
1005 * @entry: In case of ADD filter, the caller passes the RQ number in this
1007 * This function stores the filter_id returned by the
1008 * firmware in the same variable before return;
1010 * In case of DEL filter, the caller passes the RQ number. Return
1011 * value is irrelevant.
1012 * @data: filter data
1013 * @action: action data
1015 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1016 struct filter_v2 *data, struct filter_action_v2 *action_v2)
1022 struct filter_tlv *tlv, *tlv_va;
1024 u32 filter_size, action_size;
1025 static unsigned int unique_id;
1026 char z_name[RTE_MEMZONE_NAMESIZE];
1027 enum vnic_devcmd_cmd dev_cmd;
1029 if (cmd == CLSF_ADD) {
1030 dev_cmd = (data->type >= FILTER_DPDK_1) ?
1031 CMD_ADD_ADV_FILTER : CMD_ADD_FILTER;
1033 filter_size = vnic_filter_size(data);
1034 action_size = vnic_action_size(action_v2);
1036 tlv_size = filter_size + action_size +
1037 2*sizeof(struct filter_tlv);
1038 snprintf((char *)z_name, sizeof(z_name),
1039 "vnic_clsf_%u", unique_id++);
1040 tlv_va = vdev->alloc_consistent(vdev->priv,
1041 tlv_size, &tlv_pa, (u8 *)z_name);
1047 memset(tlv, 0, tlv_size);
1048 tlv->type = CLSF_TLV_FILTER;
1049 tlv->length = filter_size;
1050 memcpy(&tlv->val, (void *)data, filter_size);
1052 tlv = (struct filter_tlv *)((char *)tlv +
1053 sizeof(struct filter_tlv) +
1056 tlv->type = CLSF_TLV_ACTION;
1057 tlv->length = action_size;
1058 memcpy(&tlv->val, (void *)action_v2, action_size);
1059 ret = vnic_dev_cmd(vdev, dev_cmd, &a0, &a1, wait);
1061 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1062 } else if (cmd == CLSF_DEL) {
1064 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);