2 * Copyright 2008-2014 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
5 * Copyright (c) 2014, Cisco Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
35 #include <rte_memzone.h>
36 #include <rte_memcpy.h>
37 #include <rte_string_fns.h>
40 #include "vnic_resource.h"
41 #include "vnic_devcmd.h"
42 #include "vnic_stats.h"
45 enum vnic_proxy_type {
57 struct vnic_intr_coal_timer_info {
65 struct rte_pci_device *pdev;
66 struct vnic_res res[RES_TYPE_MAX];
67 enum vnic_dev_intr_mode intr_mode;
68 struct vnic_devcmd __iomem *devcmd;
69 struct vnic_devcmd_notify *notify;
70 struct vnic_devcmd_notify notify_copy;
73 dma_addr_t linkstatus_pa;
74 struct vnic_stats *stats;
76 struct vnic_devcmd_fw_info *fw_info;
77 dma_addr_t fw_info_pa;
78 enum vnic_proxy_type proxy;
80 u64 args[VNIC_DEVCMD_NARGS];
83 struct vnic_intr_coal_timer_info intr_coal_timer_info;
84 void *(*alloc_consistent)(void *priv, size_t size,
85 dma_addr_t *dma_handle, u8 *name);
86 void (*free_consistent)(void *priv,
87 size_t size, void *vaddr,
88 dma_addr_t dma_handle);
91 #define VNIC_MAX_RES_HDR_SIZE \
92 (sizeof(struct vnic_resource_header) + \
93 sizeof(struct vnic_resource) * RES_TYPE_MAX)
94 #define VNIC_RES_STRIDE 128
96 void *vnic_dev_priv(struct vnic_dev *vdev)
101 void vnic_register_cbacks(struct vnic_dev *vdev,
102 void *(*alloc_consistent)(void *priv, size_t size,
103 dma_addr_t *dma_handle, u8 *name),
104 void (*free_consistent)(void *priv,
105 size_t size, void *vaddr,
106 dma_addr_t dma_handle))
108 vdev->alloc_consistent = alloc_consistent;
109 vdev->free_consistent = free_consistent;
112 static int vnic_dev_discover_res(struct vnic_dev *vdev,
113 struct vnic_dev_bar *bar, unsigned int num_bars)
115 struct vnic_resource_header __iomem *rh;
116 struct mgmt_barmap_hdr __iomem *mrh;
117 struct vnic_resource __iomem *r;
123 if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
124 pr_err("vNIC BAR0 res hdr length error\n");
131 pr_err("vNIC BAR0 res hdr not mem-mapped\n");
135 /* Check for mgmt vnic in addition to normal vnic */
136 if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
137 (ioread32(&rh->version) != VNIC_RES_VERSION)) {
138 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
139 (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
140 pr_err("vNIC BAR0 res magic/version error " \
141 "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
142 VNIC_RES_MAGIC, VNIC_RES_VERSION,
143 MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
144 ioread32(&rh->magic), ioread32(&rh->version));
149 if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
150 r = (struct vnic_resource __iomem *)(mrh + 1);
152 r = (struct vnic_resource __iomem *)(rh + 1);
155 while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
156 u8 bar_num = ioread8(&r->bar);
157 u32 bar_offset = ioread32(&r->bar_offset);
158 u32 count = ioread32(&r->count);
163 if (bar_num >= num_bars)
166 if (!bar[bar_num].len || !bar[bar_num].vaddr)
173 case RES_TYPE_INTR_CTRL:
174 /* each count is stride bytes long */
175 len = count * VNIC_RES_STRIDE;
176 if (len + bar_offset > bar[bar_num].len) {
177 pr_err("vNIC BAR0 resource %d " \
178 "out-of-bounds, offset 0x%x + " \
179 "size 0x%x > bar len 0x%lx\n",
186 case RES_TYPE_INTR_PBA_LEGACY:
187 case RES_TYPE_DEVCMD:
194 vdev->res[type].count = count;
195 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
197 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
203 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
204 enum vnic_res_type type)
206 return vdev->res[type].count;
209 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
212 if (!vdev->res[type].vaddr)
219 case RES_TYPE_INTR_CTRL:
220 return (char __iomem *)vdev->res[type].vaddr +
221 index * VNIC_RES_STRIDE;
223 return (char __iomem *)vdev->res[type].vaddr;
227 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
228 unsigned int desc_count, unsigned int desc_size)
230 /* The base address of the desc rings must be 512 byte aligned.
231 * Descriptor count is aligned to groups of 32 descriptors. A
232 * count of 0 means the maximum 4096 descriptors. Descriptor
233 * size is aligned to 16 bytes.
236 unsigned int count_align = 32;
237 unsigned int desc_align = 16;
239 ring->base_align = 512;
244 ring->desc_count = VNIC_ALIGN(desc_count, count_align);
246 ring->desc_size = VNIC_ALIGN(desc_size, desc_align);
248 ring->size = ring->desc_count * ring->desc_size;
249 ring->size_unaligned = ring->size + ring->base_align;
251 return ring->size_unaligned;
254 void vnic_set_hdr_split_size(struct vnic_dev *vdev, u16 size)
256 vdev->split_hdr_size = size;
259 u16 vnic_get_hdr_split_size(struct vnic_dev *vdev)
261 return vdev->split_hdr_size;
264 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
266 memset(ring->descs, 0, ring->size);
269 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev,
270 struct vnic_dev_ring *ring,
271 unsigned int desc_count, unsigned int desc_size,
272 __attribute__((unused)) unsigned int socket_id,
275 void *alloc_addr = NULL;
276 dma_addr_t alloc_pa = 0;
278 vnic_dev_desc_ring_size(ring, desc_count, desc_size);
279 alloc_addr = vdev->alloc_consistent(vdev->priv,
280 ring->size_unaligned,
281 &alloc_pa, (u8 *)z_name);
283 pr_err("Failed to allocate ring (size=%d), aborting\n",
287 ring->descs_unaligned = alloc_addr;
289 pr_err("Failed to map allocated ring (size=%d), aborting\n",
291 vdev->free_consistent(vdev->priv,
292 ring->size_unaligned,
297 ring->base_addr_unaligned = alloc_pa;
299 ring->base_addr = VNIC_ALIGN(ring->base_addr_unaligned,
301 ring->descs = (u8 *)ring->descs_unaligned +
302 (ring->base_addr - ring->base_addr_unaligned);
304 vnic_dev_clear_desc_ring(ring);
306 ring->desc_avail = ring->desc_count - 1;
311 void vnic_dev_free_desc_ring(__attribute__((unused)) struct vnic_dev *vdev,
312 struct vnic_dev_ring *ring)
315 vdev->free_consistent(vdev->priv,
316 ring->size_unaligned,
317 ring->descs_unaligned,
318 ring->base_addr_unaligned);
323 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
326 struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
332 status = ioread32(&devcmd->status);
333 if (status == 0xFFFFFFFF) {
334 /* PCI-e target device is gone */
337 if (status & STAT_BUSY) {
339 pr_err("Busy devcmd %d\n", _CMD_N(cmd));
343 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
344 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
345 writeq(vdev->args[i], &devcmd->args[i]);
346 wmb(); /* complete all writes initiated till now */
349 iowrite32(cmd, &devcmd->cmd);
351 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
354 for (delay = 0; delay < wait; delay++) {
358 status = ioread32(&devcmd->status);
359 if (status == 0xFFFFFFFF) {
360 /* PCI-e target device is gone */
364 if (!(status & STAT_BUSY)) {
365 if (status & STAT_ERROR) {
366 err = -(int)readq(&devcmd->args[0]);
367 if (cmd != CMD_CAPABILITY)
368 pr_err("Devcmd %d failed " \
369 "with error code %d\n",
374 if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
375 rmb();/* finish all reads initiated till now */
376 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
377 vdev->args[i] = readq(&devcmd->args[i]);
384 pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
388 static int vnic_dev_cmd_proxy(struct vnic_dev *vdev,
389 enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd,
390 u64 *args, int nargs, int wait)
396 * Proxy command consumes 2 arguments. One for proxy index,
397 * the other is for command to be proxied
399 if (nargs > VNIC_DEVCMD_NARGS - 2) {
400 pr_err("number of args %d exceeds the maximum\n", nargs);
403 memset(vdev->args, 0, sizeof(vdev->args));
405 vdev->args[0] = vdev->proxy_index;
407 memcpy(&vdev->args[2], args, nargs * sizeof(args[0]));
409 err = _vnic_dev_cmd(vdev, proxy_cmd, wait);
413 status = (u32)vdev->args[0];
414 if (status & STAT_ERROR) {
415 err = (int)vdev->args[1];
416 if (err != ERR_ECMDUNKNOWN ||
417 cmd != CMD_CAPABILITY)
418 pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
422 memcpy(args, &vdev->args[1], nargs * sizeof(args[0]));
427 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
428 enum vnic_devcmd_cmd cmd, u64 *args, int nargs, int wait)
432 if (nargs > VNIC_DEVCMD_NARGS) {
433 pr_err("number of args %d exceeds the maximum\n", nargs);
436 memset(vdev->args, 0, sizeof(vdev->args));
437 memcpy(vdev->args, args, nargs * sizeof(args[0]));
439 err = _vnic_dev_cmd(vdev, cmd, wait);
441 memcpy(args, vdev->args, nargs * sizeof(args[0]));
446 void vnic_dev_cmd_proxy_by_index_start(struct vnic_dev *vdev, u16 index)
448 vdev->proxy = PROXY_BY_INDEX;
449 vdev->proxy_index = index;
452 void vnic_dev_cmd_proxy_by_bdf_start(struct vnic_dev *vdev, u16 bdf)
454 vdev->proxy = PROXY_BY_BDF;
455 vdev->proxy_index = bdf;
458 void vnic_dev_cmd_proxy_end(struct vnic_dev *vdev)
460 vdev->proxy = PROXY_NONE;
461 vdev->proxy_index = 0;
464 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
465 u64 *a0, u64 *a1, int wait)
472 memset(vdev->args, 0, sizeof(vdev->args));
474 switch (vdev->proxy) {
476 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
477 args, ARRAY_SIZE(args), wait);
480 err = vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
481 args, ARRAY_SIZE(args), wait);
485 err = vnic_dev_cmd_no_proxy(vdev, cmd, args, 2, wait);
497 int vnic_dev_cmd_args(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
498 u64 *args, int nargs, int wait)
500 switch (vdev->proxy) {
502 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
505 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
509 return vnic_dev_cmd_no_proxy(vdev, cmd, args, nargs, wait);
513 static int vnic_dev_advanced_filters_cap(struct vnic_dev *vdev, u64 *args,
516 memset(args, 0, nargs * sizeof(*args));
517 args[0] = CMD_ADD_ADV_FILTER;
518 args[1] = FILTER_CAP_MODE_V1_FLAG;
519 return vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, nargs, 1000);
522 int vnic_dev_capable_adv_filters(struct vnic_dev *vdev)
524 u64 a0 = CMD_ADD_ADV_FILTER, a1 = 0;
528 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
531 return (a1 >= (u32)FILTER_DPDK_1);
534 /* Determine the "best" filtering mode VIC is capaible of. Returns one of 3
535 * value or 0 on error:
536 * FILTER_DPDK_1- advanced filters availabile
537 * FILTER_USNIC_IP_FLAG - advanced filters but with the restriction that
538 * the IP layer must explicitly specified. I.e. cannot have a UDP
539 * filter that matches both IPv4 and IPv6.
540 * FILTER_IPV4_5TUPLE - fallback if either of the 2 above aren't available.
541 * all other filter types are not available.
542 * Retrun true in filter_tags if supported
544 int vnic_dev_capable_filter_mode(struct vnic_dev *vdev, u32 *mode,
551 err = vnic_dev_advanced_filters_cap(vdev, args, 4);
553 /* determine if filter tags are available */
556 if ((args[2] == FILTER_CAP_MODE_V1) &&
557 (args[3] & FILTER_ACTION_FILTER_ID_FLAG))
562 if (err || ((args[0] == 1) && (args[1] == 0))) {
563 /* Adv filter Command not supported or adv filters available but
564 * not enabled. Try the normal filter capability command.
566 args[0] = CMD_ADD_FILTER;
568 err = vnic_dev_cmd_args(vdev, CMD_CAPABILITY, args, 2, 1000);
572 goto parse_max_level;
573 } else if (args[2] == FILTER_CAP_MODE_V1) {
574 /* parse filter capability mask in args[1] */
575 if (args[1] & FILTER_DPDK_1_FLAG)
576 *mode = FILTER_DPDK_1;
577 else if (args[1] & FILTER_USNIC_IP_FLAG)
578 *mode = FILTER_USNIC_IP;
579 else if (args[1] & FILTER_IPV4_5TUPLE_FLAG)
580 *mode = FILTER_IPV4_5TUPLE;
585 if (max_level >= (u32)FILTER_USNIC_IP)
586 *mode = FILTER_USNIC_IP;
588 *mode = FILTER_IPV4_5TUPLE;
592 int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
594 u64 a0 = (u32)cmd, a1 = 0;
598 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
603 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size,
613 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
617 *(u8 *)value = (u8)a0;
620 *(u16 *)value = (u16)a0;
623 *(u32 *)value = (u32)a0;
636 int vnic_dev_stats_clear(struct vnic_dev *vdev)
641 return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
644 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
652 snprintf((char *)name, sizeof(name),
653 "vnic_stats-%d", instance++);
654 vdev->stats = vdev->alloc_consistent(vdev->priv,
655 sizeof(struct vnic_stats), &vdev->stats_pa, (u8 *)name);
660 *stats = vdev->stats;
662 a1 = sizeof(struct vnic_stats);
664 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
667 int vnic_dev_close(struct vnic_dev *vdev)
672 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
675 /** Deprecated. @see vnic_dev_enable_wait */
676 int vnic_dev_enable(struct vnic_dev *vdev)
681 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
684 int vnic_dev_enable_wait(struct vnic_dev *vdev)
689 if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
690 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
692 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
695 int vnic_dev_disable(struct vnic_dev *vdev)
700 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
703 int vnic_dev_open(struct vnic_dev *vdev, int arg)
705 u64 a0 = (u32)arg, a1 = 0;
708 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
711 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
719 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
728 int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
730 u64 a0 = (u32)arg, a1 = 0;
733 return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
736 int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
744 err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
753 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
759 for (i = 0; i < ETH_ALEN; i++)
762 err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
766 for (i = 0; i < ETH_ALEN; i++)
767 mac_addr[i] = ((u8 *)&a0)[i];
772 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
773 int broadcast, int promisc, int allmulti)
779 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
780 (multicast ? CMD_PFILTER_MULTICAST : 0) |
781 (broadcast ? CMD_PFILTER_BROADCAST : 0) |
782 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
783 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
785 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
787 pr_err("Can't set packet filter\n");
792 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
799 for (i = 0; i < ETH_ALEN; i++)
800 ((u8 *)&a0)[i] = addr[i];
802 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
804 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
805 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
811 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
818 for (i = 0; i < ETH_ALEN; i++)
819 ((u8 *)&a0)[i] = addr[i];
821 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
823 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
824 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
830 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
831 u8 ig_vlan_rewrite_mode)
833 u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
836 if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
837 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
843 int vnic_dev_raise_intr(struct vnic_dev *vdev, u16 intr)
845 u64 a0 = intr, a1 = 0;
849 err = vnic_dev_cmd(vdev, CMD_IAR, &a0, &a1, wait);
851 pr_err("Failed to raise INTR[%d], err %d\n", intr, err);
856 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state)
858 vdev->in_reset = state;
861 static inline int vnic_dev_in_reset(struct vnic_dev *vdev)
863 return vdev->in_reset;
866 int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
867 void *notify_addr, dma_addr_t notify_pa, u16 intr)
873 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
874 if (!vnic_dev_in_reset(vdev)) {
875 vdev->notify = notify_addr;
876 vdev->notify_pa = notify_pa;
880 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
881 a1 += sizeof(struct vnic_devcmd_notify);
883 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
884 if (!vnic_dev_in_reset(vdev))
885 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
890 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
892 void *notify_addr = NULL;
893 dma_addr_t notify_pa = 0;
897 if (vdev->notify || vdev->notify_pa) {
898 return vnic_dev_notify_setcmd(vdev, vdev->notify,
899 vdev->notify_pa, intr);
901 if (!vnic_dev_in_reset(vdev)) {
902 snprintf((char *)name, sizeof(name),
903 "vnic_notify-%d", instance++);
904 notify_addr = vdev->alloc_consistent(vdev->priv,
905 sizeof(struct vnic_devcmd_notify),
906 ¬ify_pa, (u8 *)name);
911 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
914 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
920 a0 = 0; /* paddr = 0 to unset notify buffer */
921 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
922 a1 += sizeof(struct vnic_devcmd_notify);
924 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
925 if (!vnic_dev_in_reset(vdev)) {
934 int vnic_dev_notify_unset(struct vnic_dev *vdev)
936 if (vdev->notify && !vnic_dev_in_reset(vdev)) {
937 vdev->free_consistent(vdev->priv,
938 sizeof(struct vnic_devcmd_notify),
943 return vnic_dev_notify_unsetcmd(vdev);
946 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
949 unsigned int nwords = vdev->notify_sz / 4;
953 if (!vdev->notify || !vdev->notify_sz)
958 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
959 words = (u32 *)&vdev->notify_copy;
960 for (i = 1; i < nwords; i++)
962 } while (csum != words[0]);
967 int vnic_dev_init(struct vnic_dev *vdev, int arg)
969 u64 a0 = (u32)arg, a1 = 0;
973 if (vnic_dev_capable(vdev, CMD_INIT))
974 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
976 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
977 if (a0 & CMD_INITF_DEFAULT_MAC) {
978 /* Emulate these for old CMD_INIT_v1 which
979 * didn't pass a0 so no CMD_INITF_*.
981 vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
982 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
988 int vnic_dev_deinit(struct vnic_dev *vdev)
993 return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait);
996 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
998 /* Default: hardware intr coal timer is in units of 1.5 usecs */
999 vdev->intr_coal_timer_info.mul = 2;
1000 vdev->intr_coal_timer_info.div = 3;
1001 vdev->intr_coal_timer_info.max_usec =
1002 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
1005 int vnic_dev_link_status(struct vnic_dev *vdev)
1007 if (!vnic_dev_notify_ready(vdev))
1010 return vdev->notify_copy.link_state;
1013 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
1015 if (!vnic_dev_notify_ready(vdev))
1018 return vdev->notify_copy.port_speed;
1021 void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
1022 enum vnic_dev_intr_mode intr_mode)
1024 vdev->intr_mode = intr_mode;
1027 enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
1028 struct vnic_dev *vdev)
1030 return vdev->intr_mode;
1033 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
1035 return (usec * vdev->intr_coal_timer_info.mul) /
1036 vdev->intr_coal_timer_info.div;
1039 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
1041 return (hw_cycles * vdev->intr_coal_timer_info.div) /
1042 vdev->intr_coal_timer_info.mul;
1045 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
1047 return vdev->intr_coal_timer_info.max_usec;
1050 void vnic_dev_unregister(struct vnic_dev *vdev)
1054 vdev->free_consistent(vdev->priv,
1055 sizeof(struct vnic_devcmd_notify),
1059 vdev->free_consistent(vdev->priv,
1060 sizeof(struct vnic_stats),
1061 vdev->stats, vdev->stats_pa);
1063 vdev->free_consistent(vdev->priv,
1064 sizeof(struct vnic_devcmd_fw_info),
1065 vdev->fw_info, vdev->fw_info_pa);
1070 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
1071 void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar,
1072 unsigned int num_bars)
1075 vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
1083 if (vnic_dev_discover_res(vdev, bar, num_bars))
1086 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
1093 vnic_dev_unregister(vdev);
1097 struct rte_pci_device *vnic_dev_get_pdev(struct vnic_dev *vdev)
1102 int vnic_dev_set_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
1108 for (i = 0; i < ETH_ALEN; i++)
1109 ((u8 *)&a0)[i] = mac_addr[i];
1111 return vnic_dev_cmd(vdev, CMD_SET_MAC_ADDR, &a0, &a1, wait);
1115 * vnic_dev_classifier: Add/Delete classifier entries
1116 * @vdev: vdev of the device
1117 * @cmd: CLSF_ADD for Add filter
1118 * CLSF_DEL for Delete filter
1119 * @entry: In case of ADD filter, the caller passes the RQ number in this
1121 * This function stores the filter_id returned by the
1122 * firmware in the same variable before return;
1124 * In case of DEL filter, the caller passes the RQ number. Return
1125 * value is irrelevant.
1126 * @data: filter data
1127 * @action: action data
1129 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
1130 struct filter_v2 *data, struct filter_action_v2 *action_v2)
1136 struct filter_tlv *tlv, *tlv_va;
1138 u32 filter_size, action_size;
1139 static unsigned int unique_id;
1140 char z_name[RTE_MEMZONE_NAMESIZE];
1141 enum vnic_devcmd_cmd dev_cmd;
1143 if (cmd == CLSF_ADD) {
1144 dev_cmd = (data->type >= FILTER_DPDK_1) ?
1145 CMD_ADD_ADV_FILTER : CMD_ADD_FILTER;
1147 filter_size = vnic_filter_size(data);
1148 action_size = vnic_action_size(action_v2);
1150 tlv_size = filter_size + action_size +
1151 2*sizeof(struct filter_tlv);
1152 snprintf((char *)z_name, sizeof(z_name),
1153 "vnic_clsf_%d", unique_id++);
1154 tlv_va = vdev->alloc_consistent(vdev->priv,
1155 tlv_size, &tlv_pa, (u8 *)z_name);
1161 memset(tlv, 0, tlv_size);
1162 tlv->type = CLSF_TLV_FILTER;
1163 tlv->length = filter_size;
1164 memcpy(&tlv->val, (void *)data, filter_size);
1166 tlv = (struct filter_tlv *)((char *)tlv +
1167 sizeof(struct filter_tlv) +
1170 tlv->type = CLSF_TLV_ACTION;
1171 tlv->length = action_size;
1172 memcpy(&tlv->val, (void *)action_v2, action_size);
1173 ret = vnic_dev_cmd(vdev, dev_cmd, &a0, &a1, wait);
1175 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa);
1176 } else if (cmd == CLSF_DEL) {
1178 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);