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"
15 #include "vnic_flowman.h"
18 enum vnic_proxy_type {
30 struct vnic_intr_coal_timer_info {
38 struct rte_pci_device *pdev;
39 struct vnic_res res[RES_TYPE_MAX];
40 enum vnic_dev_intr_mode intr_mode;
41 struct vnic_devcmd __iomem *devcmd;
42 struct vnic_devcmd_notify *notify;
43 struct vnic_devcmd_notify notify_copy;
46 dma_addr_t linkstatus_pa;
47 struct vnic_stats *stats;
49 struct vnic_devcmd_fw_info *fw_info;
50 dma_addr_t fw_info_pa;
51 struct fm_info *flowman_info;
52 dma_addr_t flowman_info_pa;
53 enum vnic_proxy_type proxy;
55 u64 args[VNIC_DEVCMD_NARGS];
57 struct vnic_intr_coal_timer_info intr_coal_timer_info;
58 void *(*alloc_consistent)(void *priv, size_t size,
59 dma_addr_t *dma_handle, u8 *name);
60 void (*free_consistent)(void *priv,
61 size_t size, void *vaddr,
62 dma_addr_t dma_handle);
65 #define VNIC_MAX_RES_HDR_SIZE \
66 (sizeof(struct vnic_resource_header) + \
67 sizeof(struct vnic_resource) * RES_TYPE_MAX)
68 #define VNIC_RES_STRIDE 128
70 void *vnic_dev_priv(struct vnic_dev *vdev)
75 void vnic_register_cbacks(struct vnic_dev *vdev,
76 void *(*alloc_consistent)(void *priv, size_t size,
77 dma_addr_t *dma_handle, u8 *name),
78 void (*free_consistent)(void *priv,
79 size_t size, void *vaddr,
80 dma_addr_t dma_handle))
82 vdev->alloc_consistent = alloc_consistent;
83 vdev->free_consistent = free_consistent;
86 static int vnic_dev_discover_res(struct vnic_dev *vdev,
87 struct vnic_dev_bar *bar, unsigned int num_bars)
89 struct vnic_resource_header __iomem *rh;
90 struct mgmt_barmap_hdr __iomem *mrh;
91 struct vnic_resource __iomem *r;
97 if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
98 pr_err("vNIC BAR0 res hdr length error\n");
105 pr_err("vNIC BAR0 res hdr not mem-mapped\n");
109 /* Check for mgmt vnic in addition to normal vnic */
110 if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
111 (ioread32(&rh->version) != VNIC_RES_VERSION)) {
112 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
113 (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
114 pr_err("vNIC BAR0 res magic/version error " \
115 "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
116 VNIC_RES_MAGIC, VNIC_RES_VERSION,
117 MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
118 ioread32(&rh->magic), ioread32(&rh->version));
123 if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
124 r = (struct vnic_resource __iomem *)(mrh + 1);
126 r = (struct vnic_resource __iomem *)(rh + 1);
129 while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
130 u8 bar_num = ioread8(&r->bar);
131 u32 bar_offset = ioread32(&r->bar_offset);
132 u32 count = ioread32(&r->count);
137 if (bar_num >= num_bars)
140 if (!bar[bar_num].len || !bar[bar_num].vaddr)
147 case RES_TYPE_INTR_CTRL:
148 /* each count is stride bytes long */
149 len = count * VNIC_RES_STRIDE;
150 if (len + bar_offset > bar[bar_num].len) {
151 pr_err("vNIC BAR0 resource %d " \
152 "out-of-bounds, offset 0x%x + " \
153 "size 0x%x > bar len 0x%lx\n",
160 case RES_TYPE_INTR_PBA_LEGACY:
161 case RES_TYPE_DEVCMD:
168 vdev->res[type].count = count;
169 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
171 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
177 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
178 enum vnic_res_type type)
180 return vdev->res[type].count;
183 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
186 if (!vdev->res[type].vaddr)
193 case RES_TYPE_INTR_CTRL:
194 return (char __iomem *)vdev->res[type].vaddr +
195 index * VNIC_RES_STRIDE;
197 return (char __iomem *)vdev->res[type].vaddr;
201 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
202 unsigned int desc_count, unsigned int desc_size)
204 /* The base address of the desc rings must be 512 byte aligned.
205 * Descriptor count is aligned to groups of 32 descriptors. A
206 * count of 0 means the maximum 4096 descriptors. Descriptor
207 * size is aligned to 16 bytes.
210 unsigned int count_align = 32;
211 unsigned int desc_align = 16;
213 ring->base_align = 512;
218 ring->desc_count = VNIC_ALIGN(desc_count, count_align);
220 ring->desc_size = VNIC_ALIGN(desc_size, desc_align);
222 ring->size = ring->desc_count * ring->desc_size;
223 ring->size_unaligned = ring->size + ring->base_align;
225 return ring->size_unaligned;
228 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
230 memset(ring->descs, 0, ring->size);
233 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev,
234 struct vnic_dev_ring *ring,
235 unsigned int desc_count, unsigned int desc_size,
236 __attribute__((unused)) unsigned int socket_id,
240 dma_addr_t alloc_pa = 0;
242 vnic_dev_desc_ring_size(ring, desc_count, desc_size);
243 alloc_addr = vdev->alloc_consistent(vdev->priv,
244 ring->size_unaligned,
245 &alloc_pa, (u8 *)z_name);
247 pr_err("Failed to allocate ring (size=%d), aborting\n",
251 ring->descs_unaligned = alloc_addr;
253 pr_err("Failed to map allocated ring (size=%d), aborting\n",
255 vdev->free_consistent(vdev->priv,
256 ring->size_unaligned,
261 ring->base_addr_unaligned = alloc_pa;
263 ring->base_addr = VNIC_ALIGN(ring->base_addr_unaligned,
265 ring->descs = (u8 *)ring->descs_unaligned +
266 (ring->base_addr - ring->base_addr_unaligned);
268 vnic_dev_clear_desc_ring(ring);
270 ring->desc_avail = ring->desc_count - 1;
275 void vnic_dev_free_desc_ring(__attribute__((unused)) struct vnic_dev *vdev,
276 struct vnic_dev_ring *ring)
279 vdev->free_consistent(vdev->priv,
280 ring->size_unaligned,
281 ring->descs_unaligned,
282 ring->base_addr_unaligned);
287 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
290 struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
296 status = ioread32(&devcmd->status);
297 if (status == 0xFFFFFFFF) {
298 /* PCI-e target device is gone */
301 if (status & STAT_BUSY) {
303 pr_err("Busy devcmd %d\n", _CMD_N(cmd));
307 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
308 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
309 writeq(vdev->args[i], &devcmd->args[i]);
310 wmb(); /* complete all writes initiated till now */
313 iowrite32(cmd, &devcmd->cmd);
315 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
318 for (delay = 0; delay < wait; delay++) {
322 status = ioread32(&devcmd->status);
323 if (status == 0xFFFFFFFF) {
324 /* PCI-e target device is gone */
328 if (!(status & STAT_BUSY)) {
329 if (status & STAT_ERROR) {
330 err = -(int)readq(&devcmd->args[0]);
331 if (cmd != CMD_CAPABILITY)
332 pr_err("Devcmd %d failed " \
333 "with error code %d\n",
338 if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
339 rmb();/* finish all reads initiated till now */
340 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
341 vdev->args[i] = readq(&devcmd->args[i]);
348 pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
352 static int vnic_dev_cmd_proxy(struct vnic_dev *vdev,
353 enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd,
354 u64 *args, int nargs, int wait)
360 * Proxy command consumes 2 arguments. One for proxy index,
361 * the other is for command to be proxied
363 if (nargs > VNIC_DEVCMD_NARGS - 2) {
364 pr_err("number of args %d exceeds the maximum\n", nargs);
367 memset(vdev->args, 0, sizeof(vdev->args));
369 vdev->args[0] = vdev->proxy_index;
371 memcpy(&vdev->args[2], args, nargs * sizeof(args[0]));
373 err = _vnic_dev_cmd(vdev, proxy_cmd, wait);
377 status = (u32)vdev->args[0];
378 if (status & STAT_ERROR) {
379 err = (int)vdev->args[1];
380 if (err != ERR_ECMDUNKNOWN ||
381 cmd != CMD_CAPABILITY)
382 pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
386 memcpy(args, &vdev->args[1], nargs * sizeof(args[0]));
391 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
392 enum vnic_devcmd_cmd cmd, u64 *args, int nargs, int wait)
396 if (nargs > VNIC_DEVCMD_NARGS) {
397 pr_err("number of args %d exceeds the maximum\n", nargs);
400 memset(vdev->args, 0, sizeof(vdev->args));
401 memcpy(vdev->args, args, nargs * sizeof(args[0]));
403 err = _vnic_dev_cmd(vdev, cmd, wait);
405 memcpy(args, vdev->args, nargs * sizeof(args[0]));
410 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
411 u64 *a0, u64 *a1, int wait)
418 memset(vdev->args, 0, sizeof(vdev->args));
420 switch (vdev->proxy) {
422 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
423 args, ARRAY_SIZE(args), wait);
426 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
427 args, ARRAY_SIZE(args), wait);
431 err = vnic_dev_cmd_no_proxy(vdev, cmd, args, 2, wait);
443 int vnic_dev_cmd_args(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
444 u64 *args, int nargs, int wait)
446 switch (vdev->proxy) {
448 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
451 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
455 return vnic_dev_cmd_no_proxy(vdev, cmd, args, nargs, wait);
459 int vnic_dev_fw_info(struct vnic_dev *vdev,
460 struct vnic_devcmd_fw_info **fw_info)
468 if (!vdev->fw_info) {
469 snprintf((char *)name, sizeof(name), "vnic_fw_info-%u",
471 vdev->fw_info = vdev->alloc_consistent(vdev->priv,
472 sizeof(struct vnic_devcmd_fw_info),
473 &vdev->fw_info_pa, (u8 *)name);
476 a0 = vdev->fw_info_pa;
477 a1 = sizeof(struct vnic_devcmd_fw_info);
478 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO,
481 *fw_info = vdev->fw_info;
485 static int vnic_dev_advanced_filters_cap(struct vnic_dev *vdev, u64 *args,
488 memset(args, 0, nargs * sizeof(*args));
489 args[0] = CMD_ADD_ADV_FILTER;
490 args[1] = FILTER_CAP_MODE_V1_FLAG;
491 return vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, nargs, 1000);
494 int vnic_dev_capable_adv_filters(struct vnic_dev *vdev)
496 u64 a0 = CMD_ADD_ADV_FILTER, a1 = 0;
500 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
503 return (a1 >= (u32)FILTER_DPDK_1);
506 int vnic_dev_flowman_cmd(struct vnic_dev *vdev, u64 *args, int nargs)
510 return vnic_dev_cmd_args(vdev, CMD_FLOW_MANAGER_OP, args, nargs, wait);
513 static int vnic_dev_flowman_enable(struct vnic_dev *vdev, u32 *mode,
521 /* flowman devcmd available? */
522 if (!vnic_dev_capable(vdev, CMD_FLOW_MANAGER_OP))
524 /* Have the version we are using? */
525 args[0] = FM_API_VERSION_QUERY;
526 if (vnic_dev_flowman_cmd(vdev, args, 1))
528 if ((args[0] & (1ULL << FM_VERSION)) == 0)
530 /* Select the version */
531 args[0] = FM_API_VERSION_SELECT;
532 args[1] = FM_VERSION;
533 if (vnic_dev_flowman_cmd(vdev, args, 2))
535 /* Can we get fm_info? */
536 if (!vdev->flowman_info) {
537 snprintf((char *)name, sizeof(name), "vnic_flowman_info-%u",
539 vdev->flowman_info = vdev->alloc_consistent(vdev->priv,
540 sizeof(struct fm_info),
541 &vdev->flowman_info_pa, (u8 *)name);
542 if (!vdev->flowman_info)
545 args[0] = FM_INFO_QUERY;
546 args[1] = vdev->flowman_info_pa;
547 args[2] = sizeof(struct fm_info);
548 if (vnic_dev_flowman_cmd(vdev, args, 3))
550 /* Have required operations? */
551 ops = (1ULL << FMOP_END) |
552 (1ULL << FMOP_DROP) |
553 (1ULL << FMOP_RQ_STEER) |
554 (1ULL << FMOP_EXACT_MATCH) |
555 (1ULL << FMOP_MARK) |
557 (1ULL << FMOP_EG_HAIRPIN) |
558 (1ULL << FMOP_ENCAP) |
559 (1ULL << FMOP_DECAP_NOSTRIP);
560 if ((vdev->flowman_info->fm_op_mask & ops) != ops)
562 /* Good to use flowman now */
563 *mode = FILTER_FLOWMAN;
564 *filter_actions = FILTER_ACTION_RQ_STEERING_FLAG |
565 FILTER_ACTION_FILTER_ID_FLAG |
566 FILTER_ACTION_COUNTER_FLAG |
567 FILTER_ACTION_DROP_FLAG;
571 /* Determine the "best" filtering mode VIC is capaible of. Returns one of 4
572 * value or 0 on error:
573 * FILTER_FLOWMAN- flowman api capable
574 * FILTER_DPDK_1- advanced filters availabile
575 * FILTER_USNIC_IP_FLAG - advanced filters but with the restriction that
576 * the IP layer must explicitly specified. I.e. cannot have a UDP
577 * filter that matches both IPv4 and IPv6.
578 * FILTER_IPV4_5TUPLE - fallback if either of the 2 above aren't available.
579 * all other filter types are not available.
580 * Retrun true in filter_tags if supported
582 int vnic_dev_capable_filter_mode(struct vnic_dev *vdev, u32 *mode,
589 /* If flowman is available, use it as it is the most capable API */
590 if (vnic_dev_flowman_enable(vdev, mode, filter_actions))
593 err = vnic_dev_advanced_filters_cap(vdev, args, 4);
595 /* determine supported filter actions */
596 *filter_actions = FILTER_ACTION_RQ_STEERING_FLAG; /* always available */
597 if (args[2] == FILTER_CAP_MODE_V1)
598 *filter_actions = args[3];
600 if (err || ((args[0] == 1) && (args[1] == 0))) {
601 /* Adv filter Command not supported or adv filters available but
602 * not enabled. Try the normal filter capability command.
604 args[0] = CMD_ADD_FILTER;
606 err = vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, 2, 1000);
610 goto parse_max_level;
611 } else if (args[2] == FILTER_CAP_MODE_V1) {
612 /* parse filter capability mask in args[1] */
613 if (args[1] & FILTER_DPDK_1_FLAG)
614 *mode = FILTER_DPDK_1;
615 else if (args[1] & FILTER_USNIC_IP_FLAG)
616 *mode = FILTER_USNIC_IP;
617 else if (args[1] & FILTER_IPV4_5TUPLE_FLAG)
618 *mode = FILTER_IPV4_5TUPLE;
623 if (max_level >= (u32)FILTER_USNIC_IP)
624 *mode = FILTER_USNIC_IP;
626 *mode = FILTER_IPV4_5TUPLE;
630 void vnic_dev_capable_udp_rss_weak(struct vnic_dev *vdev, bool *cfg_chk,
633 u64 a0 = CMD_NIC_CFG, a1 = 0;
639 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
640 if (err == 0 && a0 != 0 && a1 != 0) {
642 *weak = !!((a1 >> 32) & CMD_NIC_CFG_CAPF_UDP_WEAK);
646 int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
648 u64 a0 = (u32)cmd, a1 = 0;
652 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
657 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size,
667 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
671 *(u8 *)value = (u8)a0;
674 *(u16 *)value = (u16)a0;
677 *(u32 *)value = (u32)a0;
690 int vnic_dev_stats_clear(struct vnic_dev *vdev)
695 return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
698 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
706 *stats = vdev->stats;
708 a1 = sizeof(struct vnic_stats);
710 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
713 int vnic_dev_close(struct vnic_dev *vdev)
718 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
721 int vnic_dev_enable_wait(struct vnic_dev *vdev)
726 if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
727 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
729 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
732 int vnic_dev_disable(struct vnic_dev *vdev)
737 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
740 int vnic_dev_open(struct vnic_dev *vdev, int arg)
742 u64 a0 = (u32)arg, a1 = 0;
745 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
748 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
756 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
765 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
771 for (i = 0; i < ETH_ALEN; i++)
774 err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
778 for (i = 0; i < ETH_ALEN; i++)
779 mac_addr[i] = ((u8 *)&a0)[i];
784 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
785 int broadcast, int promisc, int allmulti)
791 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
792 (multicast ? CMD_PFILTER_MULTICAST : 0) |
793 (broadcast ? CMD_PFILTER_BROADCAST : 0) |
794 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
795 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
797 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
799 pr_err("Can't set packet filter\n");
804 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
811 for (i = 0; i < ETH_ALEN; i++)
812 ((u8 *)&a0)[i] = addr[i];
814 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
816 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
817 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
823 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
830 for (i = 0; i < ETH_ALEN; i++)
831 ((u8 *)&a0)[i] = addr[i];
833 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
835 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
836 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
842 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
843 u8 ig_vlan_rewrite_mode)
845 u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
848 if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
849 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
855 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
857 vdev->in_reset = state;
860 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
862 return vdev->in_reset;
865 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
866 void *notify_addr, dma_addr_t notify_pa, u16 intr)
872 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
873 if (!vnic_dev_in_reset(vdev)) {
874 vdev->notify = notify_addr;
875 vdev->notify_pa = notify_pa;
879 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
880 a1 += sizeof(struct vnic_devcmd_notify);
882 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
883 if (!vnic_dev_in_reset(vdev))
884 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
889 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
891 void *notify_addr = NULL;
892 dma_addr_t notify_pa = 0;
896 if (vdev->notify || vdev->notify_pa) {
897 return vnic_dev_notify_setcmd(vdev, vdev->notify,
898 vdev->notify_pa, intr);
900 if (!vnic_dev_in_reset(vdev)) {
901 snprintf((char *)name, sizeof(name),
902 "vnic_notify-%u", instance++);
903 notify_addr = vdev->alloc_consistent(vdev->priv,
904 sizeof(struct vnic_devcmd_notify),
905 ¬ify_pa, (u8 *)name);
910 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
913 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
919 a0 = 0; /* paddr = 0 to unset notify buffer */
920 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
921 a1 += sizeof(struct vnic_devcmd_notify);
923 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
924 if (!vnic_dev_in_reset(vdev)) {
933 int vnic_dev_notify_unset(struct vnic_dev *vdev)
935 if (vdev->notify && !vnic_dev_in_reset(vdev)) {
936 vdev->free_consistent(vdev->priv,
937 sizeof(struct vnic_devcmd_notify),
942 return vnic_dev_notify_unsetcmd(vdev);
945 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
948 unsigned int nwords = vdev->notify_sz / 4;
952 if (!vdev->notify || !vdev->notify_sz)
957 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
958 words = (u32 *)&vdev->notify_copy;
959 for (i = 1; i < nwords; i++)
961 } while (csum != words[0]);
966 int vnic_dev_init(struct vnic_dev *vdev, int arg)
968 u64 a0 = (u32)arg, a1 = 0;
972 if (vnic_dev_capable(vdev, CMD_INIT))
973 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
975 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
976 if (a0 & CMD_INITF_DEFAULT_MAC) {
977 /* Emulate these for old CMD_INIT_v1 which
978 * didn't pass a0 so no CMD_INITF_*.
980 vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
981 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
987 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
989 /* Default: hardware intr coal timer is in units of 1.5 usecs */
990 vdev->intr_coal_timer_info.mul = 2;
991 vdev->intr_coal_timer_info.div = 3;
992 vdev->intr_coal_timer_info.max_usec =
993 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
996 int vnic_dev_link_status(struct vnic_dev *vdev)
998 if (!vnic_dev_notify_ready(vdev))
1001 return vdev->notify_copy.link_state;
1004 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
1006 if (!vnic_dev_notify_ready(vdev))
1009 return vdev->notify_copy.port_speed;
1012 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
1014 return (usec * vdev->intr_coal_timer_info.mul) /
1015 vdev->intr_coal_timer_info.div;
1018 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
1020 return (hw_cycles * vdev->intr_coal_timer_info.div) /
1021 vdev->intr_coal_timer_info.mul;
1024 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
1026 return vdev->intr_coal_timer_info.max_usec;
1029 int vnic_dev_alloc_stats_mem(struct vnic_dev *vdev)
1031 char name[NAME_MAX];
1032 static u32 instance;
1034 snprintf((char *)name, sizeof(name), "vnic_stats-%u", instance++);
1035 vdev->stats = vdev->alloc_consistent(vdev->priv,
1036 sizeof(struct vnic_stats),
1037 &vdev->stats_pa, (u8 *)name);
1038 return vdev->stats == NULL ? -ENOMEM : 0;
1041 void vnic_dev_unregister(struct vnic_dev *vdev)
1045 vdev->free_consistent(vdev->priv,
1046 sizeof(struct vnic_devcmd_notify),
1050 vdev->free_consistent(vdev->priv,
1051 sizeof(struct vnic_stats),
1052 vdev->stats, vdev->stats_pa);
1053 if (vdev->flowman_info)
1054 vdev->free_consistent(vdev->priv,
1055 sizeof(struct fm_info),
1056 vdev->flowman_info, vdev->flowman_info_pa);
1058 vdev->free_consistent(vdev->priv,
1059 sizeof(struct vnic_devcmd_fw_info),
1060 vdev->fw_info, vdev->fw_info_pa);
1065 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
1066 void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar,
1067 unsigned int num_bars)
1070 char name[NAME_MAX];
1071 snprintf((char *)name, sizeof(name), "%s-vnic",
1073 vdev = (struct vnic_dev *)rte_zmalloc_socket(name,
1074 sizeof(struct vnic_dev),
1075 RTE_CACHE_LINE_SIZE,
1076 pdev->device.numa_node);
1084 if (vnic_dev_discover_res(vdev, bar, num_bars))
1087 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
1094 vnic_dev_unregister(vdev);
1099 * vnic_dev_classifier: Add/Delete classifier entries
1100 * @vdev: vdev of the device
1101 * @cmd: CLSF_ADD for Add filter
1102 * CLSF_DEL for Delete filter
1103 * @entry: In case of ADD filter, the caller passes the RQ number in this
1105 * This function stores the filter_id returned by the
1106 * firmware in the same variable before return;
1108 * In case of DEL filter, the caller passes the RQ number. Return
1109 * value is irrelevant.
1110 * @data: filter data
1111 * @action: action data
1113 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1114 struct filter_v2 *data, struct filter_action_v2 *action_v2)
1120 struct filter_tlv *tlv, *tlv_va;
1122 u32 filter_size, action_size;
1123 static unsigned int unique_id;
1124 char z_name[RTE_MEMZONE_NAMESIZE];
1125 enum vnic_devcmd_cmd dev_cmd;
1127 if (cmd == CLSF_ADD) {
1128 dev_cmd = (data->type >= FILTER_DPDK_1) ?
1129 CMD_ADD_ADV_FILTER : CMD_ADD_FILTER;
1131 filter_size = vnic_filter_size(data);
1132 action_size = vnic_action_size(action_v2);
1134 tlv_size = filter_size + action_size +
1135 2*sizeof(struct filter_tlv);
1136 snprintf((char *)z_name, sizeof(z_name),
1137 "vnic_clsf_%u", unique_id++);
1138 tlv_va = vdev->alloc_consistent(vdev->priv,
1139 tlv_size, &tlv_pa, (u8 *)z_name);
1145 memset(tlv, 0, tlv_size);
1146 tlv->type = CLSF_TLV_FILTER;
1147 tlv->length = filter_size;
1148 memcpy(&tlv->val, (void *)data, filter_size);
1150 tlv = (struct filter_tlv *)((char *)tlv +
1151 sizeof(struct filter_tlv) +
1154 tlv->type = CLSF_TLV_ACTION;
1155 tlv->length = action_size;
1156 memcpy(&tlv->val, (void *)action_v2, action_size);
1157 ret = vnic_dev_cmd(vdev, dev_cmd, &a0, &a1, wait);
1159 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1160 } else if (cmd == CLSF_DEL) {
1162 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);
1168 int vnic_dev_overlay_offload_ctrl(struct vnic_dev *vdev, u8 overlay, u8 config)
1174 return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CTRL, &a0, &a1, wait);
1177 int vnic_dev_overlay_offload_cfg(struct vnic_dev *vdev, u8 overlay,
1178 u16 vxlan_udp_port_number)
1180 u64 a1 = vxlan_udp_port_number;
1184 return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CFG, &a0, &a1, wait);
1187 int vnic_dev_capable_vxlan(struct vnic_dev *vdev)
1189 u64 a0 = VIC_FEATURE_VXLAN;
1194 ret = vnic_dev_cmd(vdev, CMD_GET_SUPP_FEATURE_VER, &a0, &a1, wait);
1195 /* 1 if the NIC can do VXLAN for both IPv4 and IPv6 with multiple WQs */
1197 (a1 & (FEATURE_VXLAN_IPV6 | FEATURE_VXLAN_MULTI_WQ)) ==
1198 (FEATURE_VXLAN_IPV6 | FEATURE_VXLAN_MULTI_WQ);
1201 int vnic_dev_capable_geneve(struct vnic_dev *vdev)
1203 u64 a0 = VIC_FEATURE_GENEVE;
1208 ret = vnic_dev_cmd(vdev, CMD_GET_SUPP_FEATURE_VER, &a0, &a1, wait);
1209 return ret == 0 && (a1 & FEATURE_GENEVE_OPTIONS);