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"
13 #include "vnic_stats.h"
16 enum vnic_proxy_type {
28 struct vnic_intr_coal_timer_info {
36 struct rte_pci_device *pdev;
37 struct vnic_res res[RES_TYPE_MAX];
38 enum vnic_dev_intr_mode intr_mode;
39 struct vnic_devcmd __iomem *devcmd;
40 struct vnic_devcmd_notify *notify;
41 struct vnic_devcmd_notify notify_copy;
44 dma_addr_t linkstatus_pa;
45 struct vnic_stats *stats;
47 struct vnic_devcmd_fw_info *fw_info;
48 dma_addr_t fw_info_pa;
49 enum vnic_proxy_type proxy;
51 u64 args[VNIC_DEVCMD_NARGS];
53 struct vnic_intr_coal_timer_info intr_coal_timer_info;
54 void *(*alloc_consistent)(void *priv, size_t size,
55 dma_addr_t *dma_handle, u8 *name);
56 void (*free_consistent)(void *priv,
57 size_t size, void *vaddr,
58 dma_addr_t dma_handle);
61 #define VNIC_MAX_RES_HDR_SIZE \
62 (sizeof(struct vnic_resource_header) + \
63 sizeof(struct vnic_resource) * RES_TYPE_MAX)
64 #define VNIC_RES_STRIDE 128
66 void *vnic_dev_priv(struct vnic_dev *vdev)
71 void vnic_register_cbacks(struct vnic_dev *vdev,
72 void *(*alloc_consistent)(void *priv, size_t size,
73 dma_addr_t *dma_handle, u8 *name),
74 void (*free_consistent)(void *priv,
75 size_t size, void *vaddr,
76 dma_addr_t dma_handle))
78 vdev->alloc_consistent = alloc_consistent;
79 vdev->free_consistent = free_consistent;
82 static int vnic_dev_discover_res(struct vnic_dev *vdev,
83 struct vnic_dev_bar *bar, unsigned int num_bars)
85 struct vnic_resource_header __iomem *rh;
86 struct mgmt_barmap_hdr __iomem *mrh;
87 struct vnic_resource __iomem *r;
93 if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
94 pr_err("vNIC BAR0 res hdr length error\n");
101 pr_err("vNIC BAR0 res hdr not mem-mapped\n");
105 /* Check for mgmt vnic in addition to normal vnic */
106 if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
107 (ioread32(&rh->version) != VNIC_RES_VERSION)) {
108 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
109 (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
110 pr_err("vNIC BAR0 res magic/version error " \
111 "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
112 VNIC_RES_MAGIC, VNIC_RES_VERSION,
113 MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
114 ioread32(&rh->magic), ioread32(&rh->version));
119 if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
120 r = (struct vnic_resource __iomem *)(mrh + 1);
122 r = (struct vnic_resource __iomem *)(rh + 1);
125 while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
126 u8 bar_num = ioread8(&r->bar);
127 u32 bar_offset = ioread32(&r->bar_offset);
128 u32 count = ioread32(&r->count);
133 if (bar_num >= num_bars)
136 if (!bar[bar_num].len || !bar[bar_num].vaddr)
143 case RES_TYPE_INTR_CTRL:
144 /* each count is stride bytes long */
145 len = count * VNIC_RES_STRIDE;
146 if (len + bar_offset > bar[bar_num].len) {
147 pr_err("vNIC BAR0 resource %d " \
148 "out-of-bounds, offset 0x%x + " \
149 "size 0x%x > bar len 0x%lx\n",
156 case RES_TYPE_INTR_PBA_LEGACY:
157 case RES_TYPE_DEVCMD:
164 vdev->res[type].count = count;
165 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
167 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
173 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
174 enum vnic_res_type type)
176 return vdev->res[type].count;
179 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
182 if (!vdev->res[type].vaddr)
189 case RES_TYPE_INTR_CTRL:
190 return (char __iomem *)vdev->res[type].vaddr +
191 index * VNIC_RES_STRIDE;
193 return (char __iomem *)vdev->res[type].vaddr;
197 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
198 unsigned int desc_count, unsigned int desc_size)
200 /* The base address of the desc rings must be 512 byte aligned.
201 * Descriptor count is aligned to groups of 32 descriptors. A
202 * count of 0 means the maximum 4096 descriptors. Descriptor
203 * size is aligned to 16 bytes.
206 unsigned int count_align = 32;
207 unsigned int desc_align = 16;
209 ring->base_align = 512;
214 ring->desc_count = VNIC_ALIGN(desc_count, count_align);
216 ring->desc_size = VNIC_ALIGN(desc_size, desc_align);
218 ring->size = ring->desc_count * ring->desc_size;
219 ring->size_unaligned = ring->size + ring->base_align;
221 return ring->size_unaligned;
224 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
226 memset(ring->descs, 0, ring->size);
229 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev,
230 struct vnic_dev_ring *ring,
231 unsigned int desc_count, unsigned int desc_size,
232 __attribute__((unused)) unsigned int socket_id,
236 dma_addr_t alloc_pa = 0;
238 vnic_dev_desc_ring_size(ring, desc_count, desc_size);
239 alloc_addr = vdev->alloc_consistent(vdev->priv,
240 ring->size_unaligned,
241 &alloc_pa, (u8 *)z_name);
243 pr_err("Failed to allocate ring (size=%d), aborting\n",
247 ring->descs_unaligned = alloc_addr;
249 pr_err("Failed to map allocated ring (size=%d), aborting\n",
251 vdev->free_consistent(vdev->priv,
252 ring->size_unaligned,
257 ring->base_addr_unaligned = alloc_pa;
259 ring->base_addr = VNIC_ALIGN(ring->base_addr_unaligned,
261 ring->descs = (u8 *)ring->descs_unaligned +
262 (ring->base_addr - ring->base_addr_unaligned);
264 vnic_dev_clear_desc_ring(ring);
266 ring->desc_avail = ring->desc_count - 1;
271 void vnic_dev_free_desc_ring(__attribute__((unused)) struct vnic_dev *vdev,
272 struct vnic_dev_ring *ring)
275 vdev->free_consistent(vdev->priv,
276 ring->size_unaligned,
277 ring->descs_unaligned,
278 ring->base_addr_unaligned);
283 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
286 struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
292 status = ioread32(&devcmd->status);
293 if (status == 0xFFFFFFFF) {
294 /* PCI-e target device is gone */
297 if (status & STAT_BUSY) {
299 pr_err("Busy devcmd %d\n", _CMD_N(cmd));
303 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
304 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
305 writeq(vdev->args[i], &devcmd->args[i]);
306 wmb(); /* complete all writes initiated till now */
309 iowrite32(cmd, &devcmd->cmd);
311 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
314 for (delay = 0; delay < wait; delay++) {
318 status = ioread32(&devcmd->status);
319 if (status == 0xFFFFFFFF) {
320 /* PCI-e target device is gone */
324 if (!(status & STAT_BUSY)) {
325 if (status & STAT_ERROR) {
326 err = -(int)readq(&devcmd->args[0]);
327 if (cmd != CMD_CAPABILITY)
328 pr_err("Devcmd %d failed " \
329 "with error code %d\n",
334 if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
335 rmb();/* finish all reads initiated till now */
336 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
337 vdev->args[i] = readq(&devcmd->args[i]);
344 pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
348 static int vnic_dev_cmd_proxy(struct vnic_dev *vdev,
349 enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd,
350 u64 *args, int nargs, int wait)
356 * Proxy command consumes 2 arguments. One for proxy index,
357 * the other is for command to be proxied
359 if (nargs > VNIC_DEVCMD_NARGS - 2) {
360 pr_err("number of args %d exceeds the maximum\n", nargs);
363 memset(vdev->args, 0, sizeof(vdev->args));
365 vdev->args[0] = vdev->proxy_index;
367 memcpy(&vdev->args[2], args, nargs * sizeof(args[0]));
369 err = _vnic_dev_cmd(vdev, proxy_cmd, wait);
373 status = (u32)vdev->args[0];
374 if (status & STAT_ERROR) {
375 err = (int)vdev->args[1];
376 if (err != ERR_ECMDUNKNOWN ||
377 cmd != CMD_CAPABILITY)
378 pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
382 memcpy(args, &vdev->args[1], nargs * sizeof(args[0]));
387 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
388 enum vnic_devcmd_cmd cmd, u64 *args, int nargs, int wait)
392 if (nargs > VNIC_DEVCMD_NARGS) {
393 pr_err("number of args %d exceeds the maximum\n", nargs);
396 memset(vdev->args, 0, sizeof(vdev->args));
397 memcpy(vdev->args, args, nargs * sizeof(args[0]));
399 err = _vnic_dev_cmd(vdev, cmd, wait);
401 memcpy(args, vdev->args, nargs * sizeof(args[0]));
406 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
407 u64 *a0, u64 *a1, int wait)
414 memset(vdev->args, 0, sizeof(vdev->args));
416 switch (vdev->proxy) {
418 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
419 args, ARRAY_SIZE(args), wait);
422 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
423 args, ARRAY_SIZE(args), wait);
427 err = vnic_dev_cmd_no_proxy(vdev, cmd, args, 2, wait);
439 int vnic_dev_cmd_args(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
440 u64 *args, int nargs, int wait)
442 switch (vdev->proxy) {
444 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
447 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
451 return vnic_dev_cmd_no_proxy(vdev, cmd, args, nargs, wait);
455 static int vnic_dev_advanced_filters_cap(struct vnic_dev *vdev, u64 *args,
458 memset(args, 0, nargs * sizeof(*args));
459 args[0] = CMD_ADD_ADV_FILTER;
460 args[1] = FILTER_CAP_MODE_V1_FLAG;
461 return vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, nargs, 1000);
464 int vnic_dev_capable_adv_filters(struct vnic_dev *vdev)
466 u64 a0 = CMD_ADD_ADV_FILTER, a1 = 0;
470 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
473 return (a1 >= (u32)FILTER_DPDK_1);
476 /* Determine the "best" filtering mode VIC is capaible of. Returns one of 3
477 * value or 0 on error:
478 * FILTER_DPDK_1- advanced filters availabile
479 * FILTER_USNIC_IP_FLAG - advanced filters but with the restriction that
480 * the IP layer must explicitly specified. I.e. cannot have a UDP
481 * filter that matches both IPv4 and IPv6.
482 * FILTER_IPV4_5TUPLE - fallback if either of the 2 above aren't available.
483 * all other filter types are not available.
484 * Retrun true in filter_tags if supported
486 int vnic_dev_capable_filter_mode(struct vnic_dev *vdev, u32 *mode,
493 err = vnic_dev_advanced_filters_cap(vdev, args, 4);
495 /* determine if filter tags are available */
498 if ((args[2] == FILTER_CAP_MODE_V1) &&
499 (args[3] & FILTER_ACTION_FILTER_ID_FLAG))
504 if (err || ((args[0] == 1) && (args[1] == 0))) {
505 /* Adv filter Command not supported or adv filters available but
506 * not enabled. Try the normal filter capability command.
508 args[0] = CMD_ADD_FILTER;
510 err = vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, 2, 1000);
514 goto parse_max_level;
515 } else if (args[2] == FILTER_CAP_MODE_V1) {
516 /* parse filter capability mask in args[1] */
517 if (args[1] & FILTER_DPDK_1_FLAG)
518 *mode = FILTER_DPDK_1;
519 else if (args[1] & FILTER_USNIC_IP_FLAG)
520 *mode = FILTER_USNIC_IP;
521 else if (args[1] & FILTER_IPV4_5TUPLE_FLAG)
522 *mode = FILTER_IPV4_5TUPLE;
527 if (max_level >= (u32)FILTER_USNIC_IP)
528 *mode = FILTER_USNIC_IP;
530 *mode = FILTER_IPV4_5TUPLE;
534 int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
536 u64 a0 = (u32)cmd, a1 = 0;
540 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
545 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size,
555 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
559 *(u8 *)value = (u8)a0;
562 *(u16 *)value = (u16)a0;
565 *(u32 *)value = (u32)a0;
578 int vnic_dev_stats_clear(struct vnic_dev *vdev)
583 return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
586 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
594 *stats = vdev->stats;
596 a1 = sizeof(struct vnic_stats);
598 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
601 int vnic_dev_close(struct vnic_dev *vdev)
606 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
609 int vnic_dev_enable_wait(struct vnic_dev *vdev)
614 if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
615 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
617 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
620 int vnic_dev_disable(struct vnic_dev *vdev)
625 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
628 int vnic_dev_open(struct vnic_dev *vdev, int arg)
630 u64 a0 = (u32)arg, a1 = 0;
633 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
636 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
644 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
653 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
659 for (i = 0; i < ETH_ALEN; i++)
662 err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
666 for (i = 0; i < ETH_ALEN; i++)
667 mac_addr[i] = ((u8 *)&a0)[i];
672 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
673 int broadcast, int promisc, int allmulti)
679 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
680 (multicast ? CMD_PFILTER_MULTICAST : 0) |
681 (broadcast ? CMD_PFILTER_BROADCAST : 0) |
682 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
683 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
685 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
687 pr_err("Can't set packet filter\n");
692 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
699 for (i = 0; i < ETH_ALEN; i++)
700 ((u8 *)&a0)[i] = addr[i];
702 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
704 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
705 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
711 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
718 for (i = 0; i < ETH_ALEN; i++)
719 ((u8 *)&a0)[i] = addr[i];
721 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
723 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
724 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
730 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
731 u8 ig_vlan_rewrite_mode)
733 u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
736 if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
737 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
743 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
745 vdev->in_reset = state;
748 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
750 return vdev->in_reset;
753 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
754 void *notify_addr, dma_addr_t notify_pa, u16 intr)
760 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
761 if (!vnic_dev_in_reset(vdev)) {
762 vdev->notify = notify_addr;
763 vdev->notify_pa = notify_pa;
767 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
768 a1 += sizeof(struct vnic_devcmd_notify);
770 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
771 if (!vnic_dev_in_reset(vdev))
772 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
777 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
779 void *notify_addr = NULL;
780 dma_addr_t notify_pa = 0;
784 if (vdev->notify || vdev->notify_pa) {
785 return vnic_dev_notify_setcmd(vdev, vdev->notify,
786 vdev->notify_pa, intr);
788 if (!vnic_dev_in_reset(vdev)) {
789 snprintf((char *)name, sizeof(name),
790 "vnic_notify-%u", instance++);
791 notify_addr = vdev->alloc_consistent(vdev->priv,
792 sizeof(struct vnic_devcmd_notify),
793 ¬ify_pa, (u8 *)name);
798 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
801 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
807 a0 = 0; /* paddr = 0 to unset notify buffer */
808 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
809 a1 += sizeof(struct vnic_devcmd_notify);
811 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
812 if (!vnic_dev_in_reset(vdev)) {
821 int vnic_dev_notify_unset(struct vnic_dev *vdev)
823 if (vdev->notify && !vnic_dev_in_reset(vdev)) {
824 vdev->free_consistent(vdev->priv,
825 sizeof(struct vnic_devcmd_notify),
830 return vnic_dev_notify_unsetcmd(vdev);
833 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
836 unsigned int nwords = vdev->notify_sz / 4;
840 if (!vdev->notify || !vdev->notify_sz)
845 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
846 words = (u32 *)&vdev->notify_copy;
847 for (i = 1; i < nwords; i++)
849 } while (csum != words[0]);
854 int vnic_dev_init(struct vnic_dev *vdev, int arg)
856 u64 a0 = (u32)arg, a1 = 0;
860 if (vnic_dev_capable(vdev, CMD_INIT))
861 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
863 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
864 if (a0 & CMD_INITF_DEFAULT_MAC) {
865 /* Emulate these for old CMD_INIT_v1 which
866 * didn't pass a0 so no CMD_INITF_*.
868 vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
869 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
875 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
877 /* Default: hardware intr coal timer is in units of 1.5 usecs */
878 vdev->intr_coal_timer_info.mul = 2;
879 vdev->intr_coal_timer_info.div = 3;
880 vdev->intr_coal_timer_info.max_usec =
881 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
884 int vnic_dev_link_status(struct vnic_dev *vdev)
886 if (!vnic_dev_notify_ready(vdev))
889 return vdev->notify_copy.link_state;
892 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
894 if (!vnic_dev_notify_ready(vdev))
897 return vdev->notify_copy.port_speed;
900 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
902 return (usec * vdev->intr_coal_timer_info.mul) /
903 vdev->intr_coal_timer_info.div;
906 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
908 return (hw_cycles * vdev->intr_coal_timer_info.div) /
909 vdev->intr_coal_timer_info.mul;
912 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
914 return vdev->intr_coal_timer_info.max_usec;
917 int vnic_dev_alloc_stats_mem(struct vnic_dev *vdev)
922 snprintf((char *)name, sizeof(name), "vnic_stats-%u", instance++);
923 vdev->stats = vdev->alloc_consistent(vdev->priv,
924 sizeof(struct vnic_stats),
925 &vdev->stats_pa, (u8 *)name);
926 return vdev->stats == NULL ? -ENOMEM : 0;
929 void vnic_dev_unregister(struct vnic_dev *vdev)
933 vdev->free_consistent(vdev->priv,
934 sizeof(struct vnic_devcmd_notify),
938 vdev->free_consistent(vdev->priv,
939 sizeof(struct vnic_stats),
940 vdev->stats, vdev->stats_pa);
942 vdev->free_consistent(vdev->priv,
943 sizeof(struct vnic_devcmd_fw_info),
944 vdev->fw_info, vdev->fw_info_pa);
949 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
950 void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar,
951 unsigned int num_bars)
955 snprintf((char *)name, sizeof(name), "%s-vnic",
957 vdev = (struct vnic_dev *)rte_zmalloc_socket(name,
958 sizeof(struct vnic_dev),
960 pdev->device.numa_node);
968 if (vnic_dev_discover_res(vdev, bar, num_bars))
971 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
978 vnic_dev_unregister(vdev);
983 * vnic_dev_classifier: Add/Delete classifier entries
984 * @vdev: vdev of the device
985 * @cmd: CLSF_ADD for Add filter
986 * CLSF_DEL for Delete filter
987 * @entry: In case of ADD filter, the caller passes the RQ number in this
989 * This function stores the filter_id returned by the
990 * firmware in the same variable before return;
992 * In case of DEL filter, the caller passes the RQ number. Return
993 * value is irrelevant.
995 * @action: action data
997 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
998 struct filter_v2 *data, struct filter_action_v2 *action_v2)
1004 struct filter_tlv *tlv, *tlv_va;
1006 u32 filter_size, action_size;
1007 static unsigned int unique_id;
1008 char z_name[RTE_MEMZONE_NAMESIZE];
1009 enum vnic_devcmd_cmd dev_cmd;
1011 if (cmd == CLSF_ADD) {
1012 dev_cmd = (data->type >= FILTER_DPDK_1) ?
1013 CMD_ADD_ADV_FILTER : CMD_ADD_FILTER;
1015 filter_size = vnic_filter_size(data);
1016 action_size = vnic_action_size(action_v2);
1018 tlv_size = filter_size + action_size +
1019 2*sizeof(struct filter_tlv);
1020 snprintf((char *)z_name, sizeof(z_name),
1021 "vnic_clsf_%u", unique_id++);
1022 tlv_va = vdev->alloc_consistent(vdev->priv,
1023 tlv_size, &tlv_pa, (u8 *)z_name);
1029 memset(tlv, 0, tlv_size);
1030 tlv->type = CLSF_TLV_FILTER;
1031 tlv->length = filter_size;
1032 memcpy(&tlv->val, (void *)data, filter_size);
1034 tlv = (struct filter_tlv *)((char *)tlv +
1035 sizeof(struct filter_tlv) +
1038 tlv->type = CLSF_TLV_ACTION;
1039 tlv->length = action_size;
1040 memcpy(&tlv->val, (void *)action_v2, action_size);
1041 ret = vnic_dev_cmd(vdev, dev_cmd, &a0, &a1, wait);
1043 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1044 } else if (cmd == CLSF_DEL) {
1046 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);